alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1/* Copyright 2023 Alexander Matthes, Axel Huebl, Benjamin Worpitz, Matthias Werner, Bernhard Manfred Gruber,
2 * Jeffrey Kelling, Sergei Bastrakov, Andrea Bocci, René Widera
3 * SPDX-License-Identifier: MPL-2.0
4 */
5
6#pragma once
7
8
11
12#include <cmath>
13#include <complex>
14#include <type_traits>
15
16namespace alpaka::math::internal
17{
18
19#define ALPAKA_MATH_UNARY_FUNCTOR(FUNC_NAME, OP_NAME) \
20 struct FUNC_NAME \
21 { \
22 template<typename T_MathImpl, typename T_Arg> \
23 struct Op \
24 { \
25 constexpr auto operator()(T_MathImpl, T_Arg const& argument) const \
26 { \
27 if constexpr(std::same_as<T_MathImpl, StlMath>) \
28 { \
29 /* use for ADL lookup namespace std only if StlMath is used */ \
30 using std::OP_NAME; \
31 return OP_NAME(argument); \
32 } \
33 else \
34 return OP_NAME(argument); \
35 } \
36 }; \
37 }
38
40
42 ALPAKA_MATH_UNARY_FUNCTOR(Acos, acos);
43 ALPAKA_MATH_UNARY_FUNCTOR(Acosh, acosh);
44 ALPAKA_MATH_UNARY_FUNCTOR(Cosh, cosh);
45
47 ALPAKA_MATH_UNARY_FUNCTOR(Asin, asin);
48 ALPAKA_MATH_UNARY_FUNCTOR(Asinh, asinh);
49 ALPAKA_MATH_UNARY_FUNCTOR(Sinh, sinh);
50
52 ALPAKA_MATH_UNARY_FUNCTOR(Atan, atan);
53 ALPAKA_MATH_UNARY_FUNCTOR(Atanh, atanh);
54 ALPAKA_MATH_UNARY_FUNCTOR(Tanh, tanh);
55
57
62
65
68 ALPAKA_MATH_UNARY_FUNCTOR(Log10, log10);
69
71 ALPAKA_MATH_UNARY_FUNCTOR(Sqrt, sqrt);
74
78
79 ALPAKA_MATH_UNARY_FUNCTOR(Conj, conj);
80
81#undef ALPAKA_MATH_UNARY_FUNCTOR
82
83 namespace detail
84 {
85 //! Fallback implementation when no better ADL match was found
86 template<typename T_Arg>
87 ALPAKA_FN_INLINE constexpr auto rsqrt(T_Arg const& arg)
88 {
89 // Still use ADL to try find sqrt(arg)
90 using std::sqrt;
91 return static_cast<T_Arg>(1) / sqrt(arg);
92 }
93 } // namespace detail
94
95 struct Rsqrt
96 {
97 template<typename T_MathImpl, typename T_Arg>
98 struct Op
99 {
100 constexpr auto operator()(T_MathImpl, T_Arg const& arg) const
101 {
102 if constexpr(std::same_as<T_MathImpl, StlMath>)
103 {
104 // use for ADL lookup namespace std only if StlMath is used
105 using detail::rsqrt;
106 return rsqrt(arg);
107 }
108 else
109 return rsqrt(arg);
110 }
111 };
112 };
113
114 struct Atan2
115 {
116 template<typename T_MathImpl, typename T_Y, typename T_X>
117 struct Op
118 {
119 constexpr auto operator()(T_MathImpl, T_Y const& y, T_X const& x) const
120 {
121 if constexpr(std::same_as<T_MathImpl, StlMath>)
122 {
123 // use for ADL lookup namespace std only if StlMath is used
124 using std::atan2;
125 return atan2(y, x);
126 }
127 else
128 return atan2(y, x);
129 }
130 };
131 };
132
133 namespace detail
134 {
135 //! Fallback implementation when no better ADL match was found
136 template<typename T_Arg>
137 constexpr auto sincos(T_Arg const& arg, T_Arg& result_sin, T_Arg& result_cos)
138 {
139 // Still use ADL to try find sin(arg) and cos(arg)
140 using std::sin;
141 result_sin = sin(arg);
142 using std::cos;
143 result_cos = cos(arg);
144 }
145 } // namespace detail
146
147 // Sincos function
148 struct SinCos
149 {
150 template<typename T_MathImpl, typename T_Arg>
151 struct Op
152 {
153 constexpr auto operator()(T_MathImpl, T_Arg const& arg, T_Arg& result_sin, T_Arg& result_cos) const
154 {
155 if constexpr(std::same_as<T_MathImpl, StlMath>)
156 {
157 // use for ADL lookup namespace std only if StlMath is used
158 using detail::sincos;
159 return sincos(arg, result_sin, result_cos);
160 }
161 else
162 return sincos(arg, result_sin, result_cos);
163 }
164 };
165 };
166
167 struct Copysign
168 {
169 template<typename T_MathImpl, typename T_Mag, typename T_Sgn>
170 struct Op
171 {
172 constexpr auto operator()(T_MathImpl, T_Mag const& mag, T_Sgn const& sgn) const
173 {
174 if constexpr(std::same_as<T_MathImpl, StlMath>)
175 {
176 // use for ADL lookup namespace std only if StlMath is used
177 using std::copysign;
178 return copysign(mag, sgn);
179 }
180 else
181 return copysign(mag, sgn);
182 }
183 };
184 };
185
186 struct Min
187 {
188 template<typename T_MathImpl, typename T_A, typename T_B>
189 struct Op
190 {
191 constexpr auto operator()(T_MathImpl, T_A const& a, T_B const& b) const
192 {
193 if constexpr(std::same_as<T_MathImpl, StlMath>)
194 {
195 // use for ADL lookup namespace std only if StlMath is used
196 using std::min;
197 return min(a, b);
198 }
199 else
200 return min(a, b);
201 }
202 };
203 };
204
205 struct Max
206 {
207 template<typename T_MathImpl, typename T_A, typename T_B>
208 struct Op
209 {
210 constexpr auto operator()(T_MathImpl, T_A const& a, T_B const& b) const
211 {
212 if constexpr(std::same_as<T_MathImpl, StlMath>)
213 {
214 // use for ADL lookup namespace std only if StlMath is used
215 using std::max;
216 return max(a, b);
217 }
218 else
219 return max(a, b);
220 }
221 };
222 };
223
224 struct Pow
225 {
226 template<typename T_MathImpl, typename T_Base, typename T_Exp>
227 struct Op
228 {
229 constexpr auto operator()(T_MathImpl, T_Base const& base, T_Exp const& exp) const
230 {
231 if constexpr(std::same_as<T_MathImpl, StlMath>)
232 {
233 // use for ADL lookup namespace std only if StlMath is used
234 using std::pow;
235 return pow(base, exp);
236 }
237 else
238 return pow(base, exp);
239 }
240 };
241 };
242
243 struct Fmod
244 {
245 template<typename T_MathImpl, typename T_X, typename T_Y>
246 struct Op
247 {
248 constexpr auto operator()(T_MathImpl, T_X const& x, T_Y const& y) const
249 {
250 if constexpr(std::same_as<T_MathImpl, StlMath>)
251 {
252 // use for ADL lookup namespace std only if StlMath is used
253 using std::fmod;
254 return fmod(x, y);
255 }
256 else
257 return fmod(x, y);
258 }
259 };
260 };
261
262 struct Remainder
263 {
264 template<typename T_MathImpl, typename T_X, typename T_Y>
265 struct Op
266 {
267 constexpr auto operator()(T_MathImpl, T_X const& x, T_Y const& y) const
268 {
269 if constexpr(std::same_as<T_MathImpl, StlMath>)
270 {
271 // use for ADL lookup namespace std only if StlMath is used
272 using std::remainder;
273 return remainder(x, y);
274 }
275 else
276 return remainder(x, y);
277 }
278 };
279 };
280
281 struct Fma
282 {
283 template<typename T_MathImpl, typename T_X, typename T_Y, typename T_Z>
284 struct Op
285 {
286 constexpr auto operator()(T_MathImpl, T_X const& x, T_Y const& y, T_Z const& z) const
287 {
288 if constexpr(std::same_as<T_MathImpl, StlMath>)
289 {
290 // use for ADL lookup namespace std only if StlMath is used
291 using std::fma;
292 return fma(x, y, z);
293 }
294 else
295 return fma(x, y, z);
296 }
297 };
298 };
299} // namespace alpaka::math::internal
300
#define ALPAKA_FN_INLINE
Macro defining the inline function attribute.
Definition common.hpp:87
#define ALPAKA_MATH_UNARY_FUNCTOR(FUNC_NAME, OP_NAME)
Definition math.hpp:19
constexpr auto floor(auto const &arg)
Definition math.hpp:196
constexpr auto sin(auto const &arg)
Definition math.hpp:21
constexpr auto rsqrt(auto const &arg)
Definition math.hpp:171
constexpr auto ceil(auto const &arg)
Definition math.hpp:87
constexpr auto fmod(auto const &x, auto const &y)
Definition math.hpp:293
constexpr auto fma(auto const &x, auto const &y, auto const &z)
Definition math.hpp:305
constexpr auto atan2(auto const &y, auto const &x)
Definition math.hpp:152
constexpr auto isnan(auto const &arg)
Definition math.hpp:259
constexpr auto lround(auto const &arg)
Computes the nearest integer value to arg (in in integer format), rounding halfway cases away from ze...
Definition math.hpp:105
constexpr auto copysign(auto const &mag, auto const &sgn)
Creates a value with the magnitude of mag and the sign of sgn.
Definition math.hpp:121
constexpr auto sincos(auto const &arg, auto &result_sin, auto &result_cos)
Definition math.hpp:130
constexpr auto cbrt(auto const &arg)
Definition math.hpp:81
constexpr auto remainder(auto const &x, auto const &y)
Definition math.hpp:299
constexpr auto isinf(auto const &arg)
Definition math.hpp:57
constexpr auto log2(auto const &arg)
Computes the natural (base 2) logarithm of arg.
Definition math.hpp:220
constexpr auto trunc(auto const &arg)
Definition math.hpp:51
constexpr auto round(auto const &arg)
Computes the nearest integer value to arg (in floating-point format), rounding halfway cases away fro...
Definition math.hpp:96
constexpr auto erf(auto const &arg)
Definition math.hpp:190
constexpr auto min(auto const &a, auto const &b)
Definition math.hpp:272
constexpr auto isfinite(auto const &arg)
Definition math.hpp:63
constexpr auto llround(auto const &arg)
Computes the nearest integer value to arg (in in integer format), rounding halfway cases away from ze...
Definition math.hpp:114
constexpr auto cos(auto const &arg)
Definition math.hpp:178
constexpr auto max(auto const &a, auto const &b)
Definition math.hpp:278
constexpr auto pow(auto const &base, auto const &exp)
Definition math.hpp:284