Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:49

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