Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:52:42

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 /*!
0006  * \file corecel/math/ArrayOperators.hh
0007  * \brief Mathematical operators for the Array type.
0008  *
0009  * For performance reasons, avoid chaining these operators together: unroll
0010  * arithmetic when possible. Note that all types must be consistent (unless
0011  * promotion is automatically applied to scalar arguments), so you cannot
0012  * multiply an array of doubles with an array of ints without explicitly
0013  * converting first.
0014  */
0015 //---------------------------------------------------------------------------//
0016 #pragma once
0017 
0018 #include <type_traits>
0019 
0020 #include "corecel/cont/Array.hh"
0021 
0022 namespace celeritas
0023 {
0024 //---------------------------------------------------------------------------//
0025 #define CELER_DEFINE_ARRAY_ASSIGN(TOKEN)                                    \
0026     template<class T, size_type N>                                          \
0027     inline CELER_FUNCTION Array<T, N>& operator TOKEN(Array<T, N>& x,       \
0028                                                       Array<T, N> const& y) \
0029     {                                                                       \
0030         for (size_type i = 0; i != N; ++i)                                  \
0031         {                                                                   \
0032             x[i] TOKEN y[i];                                                \
0033         }                                                                   \
0034         return x;                                                           \
0035     }                                                                       \
0036                                                                             \
0037     template<class T, size_type N, class T2 = std::remove_cv_t<T>>          \
0038     inline CELER_FUNCTION Array<T, N>& operator TOKEN(Array<T, N>& x,       \
0039                                                       T2 const& y)          \
0040     {                                                                       \
0041         for (size_type i = 0; i != N; ++i)                                  \
0042         {                                                                   \
0043             x[i] TOKEN y;                                                   \
0044         }                                                                   \
0045         return x;                                                           \
0046     }
0047 
0048 #define CELER_DEFINE_ARRAY_ARITHM(TOKEN)                                   \
0049     template<class T, size_type N>                                         \
0050     inline CELER_FUNCTION Array<T, N> operator TOKEN(Array<T, N> const& x, \
0051                                                      Array<T, N> const& y) \
0052     {                                                                      \
0053         Array<T, N> result{x};                                             \
0054         return (result TOKEN## = y);                                       \
0055     }                                                                      \
0056                                                                            \
0057     template<class T, size_type N, class T2 = std::remove_cv_t<T>>         \
0058     inline CELER_FUNCTION Array<T, N> operator TOKEN(Array<T, N> const& x, \
0059                                                      T2 const& y)          \
0060     {                                                                      \
0061         Array<T, N> result{x};                                             \
0062         return (result TOKEN## = y);                                       \
0063     }
0064 
0065 //---------------------------------------------------------------------------//
0066 //!@{
0067 //! Assignment arithmetic
0068 CELER_DEFINE_ARRAY_ASSIGN(+=)
0069 CELER_DEFINE_ARRAY_ASSIGN(-=)
0070 CELER_DEFINE_ARRAY_ASSIGN(*=)
0071 CELER_DEFINE_ARRAY_ASSIGN(/=)
0072 //!@}
0073 
0074 //---------------------------------------------------------------------------//
0075 //!@{
0076 //! Arithmetic
0077 CELER_DEFINE_ARRAY_ARITHM(+)
0078 CELER_DEFINE_ARRAY_ARITHM(-)
0079 CELER_DEFINE_ARRAY_ARITHM(*)
0080 CELER_DEFINE_ARRAY_ARITHM(/)
0081 //!@}
0082 
0083 //! Left-multiply by scalar
0084 template<class T, size_type N, class T2 = std::remove_cv_t<T>>
0085 inline CELER_FUNCTION Array<T, N> operator*(T2 const& y, Array<T, N> const& x)
0086 {
0087     return x * y;
0088 }
0089 
0090 //---------------------------------------------------------------------------//
0091 /*!
0092  * Unary negation.
0093  */
0094 template<class T, size_type N>
0095 inline CELER_FUNCTION Array<T, N> operator-(Array<T, N> const& x)
0096 {
0097     Array<T, N> result;
0098     for (size_type i = 0; i != N; ++i)
0099     {
0100         result[i] = -x[i];
0101     }
0102     return result;
0103 }
0104 
0105 #undef CELER_DEFINE_ARRAY_ASSIGN
0106 #undef CELER_DEFINE_ARRAY_ARITHM
0107 //---------------------------------------------------------------------------//
0108 }  // namespace celeritas