alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/api/api.hpp"
8#include "alpaka/api/math.hpp"
10
11#include <cmath>
12
13namespace alpaka::math
14{
15 constexpr auto abs(auto const& arg)
16 {
17 auto const mathImpl = trait::getMathImpl(thisApi());
18 return internal::Abs::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
19 }
20
21 constexpr auto sin(auto const& arg)
22 {
23 auto const mathImpl = trait::getMathImpl(thisApi());
24 return internal::Sin::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
25 }
26
27 constexpr auto acosh(auto const& arg)
28 {
29 auto const mathImpl = trait::getMathImpl(thisApi());
30 return internal::Acosh::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
31 }
32
33 constexpr auto asinh(auto const& arg)
34 {
35 auto const mathImpl = trait::getMathImpl(thisApi());
36 return internal::Asinh::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
37 }
38
39 constexpr auto sinh(auto const& arg)
40 {
41 auto const mathImpl = trait::getMathImpl(thisApi());
42 return internal::Sinh::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
43 }
44
45 constexpr auto atan(auto const& arg)
46 {
47 auto const mathImpl = trait::getMathImpl(thisApi());
48 return internal::Atan::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
49 }
50
51 constexpr auto trunc(auto const& arg)
52 {
53 auto const mathImpl = trait::getMathImpl(thisApi());
54 return internal::Trunc::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
55 }
56
57 constexpr auto isinf(auto const& arg)
58 {
59 auto const mathImpl = trait::getMathImpl(thisApi());
60 return internal::Isinf::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
61 }
62
63 constexpr auto isfinite(auto const& arg)
64 {
65 auto const mathImpl = trait::getMathImpl(thisApi());
66 return internal::Isfinite::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
67 }
68
69 constexpr auto atanh(auto const& arg)
70 {
71 auto const mathImpl = trait::getMathImpl(thisApi());
72 return internal::Atanh::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
73 }
74
75 constexpr auto tanh(auto const& arg)
76 {
77 auto const mathImpl = trait::getMathImpl(thisApi());
78 return internal::Tanh::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
79 }
80
81 constexpr auto cbrt(auto const& arg)
82 {
83 auto const mathImpl = trait::getMathImpl(thisApi());
84 return internal::Cbrt::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
85 }
86
87 constexpr auto ceil(auto const& arg)
88 {
89 auto const mathImpl = trait::getMathImpl(thisApi());
90 return internal::Ceil::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
91 }
92
93 /** Computes the nearest integer value to arg (in floating-point format), rounding halfway cases away from zero,
94 * regardless of the current rounding mode.
95 */
96 constexpr auto round(auto const& arg)
97 {
98 auto const mathImpl = trait::getMathImpl(thisApi());
99 return internal::Round::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
100 }
101
102 /** Computes the nearest integer value to arg (in integer format), rounding halfway cases away from zero,
103 * regardless of the current rounding mode.
104 */
105 constexpr auto lround(auto const& arg)
106 {
107 auto const mathImpl = trait::getMathImpl(thisApi());
108 return internal::Lround::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
109 }
110
111 /** Computes the nearest integer value to arg (in integer format), rounding halfway cases away from zero,
112 * regardless of the current rounding mode.
113 */
114 constexpr auto llround(auto const& arg)
115 {
116 auto const mathImpl = trait::getMathImpl(thisApi());
117 return internal::Llround::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
118 }
119
120 /** Creates a value with the magnitude of mag and the sign of sgn. */
121 constexpr auto copysign(auto const& mag, auto const& sgn)
122 {
123 auto const mathImpl = trait::getMathImpl(thisApi());
124 return internal::Copysign::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(mag), ALPAKA_TYPEOF(sgn)>{}(
125 mathImpl,
126 mag,
127 sgn);
128 }
129
130 constexpr auto sincos(auto const& arg, auto& result_sin, auto& result_cos)
131 {
132 auto const mathImpl = trait::getMathImpl(thisApi());
134 mathImpl,
135 arg,
136 result_sin,
137 result_cos);
138 }
139
140 constexpr auto exp(auto const& arg)
141 {
142 auto const mathImpl = trait::getMathImpl(thisApi());
143 return internal::Exp::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
144 }
145
146 constexpr auto arg(auto const& arg)
147 {
148 auto const mathImpl = trait::getMathImpl(thisApi());
149 return internal::Arg::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
150 }
151
152 constexpr auto atan2(auto const& y, auto const& x)
153 {
154 auto const mathImpl = trait::getMathImpl(thisApi());
155 return internal::Atan2::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(y), ALPAKA_TYPEOF(x)>{}(mathImpl, y, x);
156 }
157
158 // Square root function
159 constexpr auto sqrt(auto const& arg)
160 {
161 auto const mathImpl = trait::getMathImpl(thisApi());
162 return internal::Sqrt::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
163 }
164
165 /* Computes the rsqrt.
166 *
167 * Valid real arguments are positive. For other values the result
168 * may depend on the backend and compilation options, will likely
169 * be NaN.
170 */
171 constexpr auto rsqrt(auto const& arg)
172 {
173 auto const mathImpl = trait::getMathImpl(thisApi());
174 return internal::Rsqrt::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
175 }
176
177 // Cosine function
178 constexpr auto cos(auto const& arg)
179 {
180 auto const mathImpl = trait::getMathImpl(thisApi());
181 return internal::Cos::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
182 }
183
184 constexpr auto cosh(auto const& arg)
185 {
186 auto const mathImpl = trait::getMathImpl(thisApi());
187 return internal::Cosh::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
188 }
189
190 constexpr auto erf(auto const& arg)
191 {
192 auto const mathImpl = trait::getMathImpl(thisApi());
193 return internal::Erf::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
194 }
195
196 constexpr auto floor(auto const& arg)
197 {
198 auto const mathImpl = trait::getMathImpl(thisApi());
199 return internal::Floor::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
200 }
201
202 /** Computes the natural (base e) logarithm of arg.
203 *
204 * Valid real arguments are non-negative. For other values the result
205 * may depend on the backend and compilation options, will likely
206 * be NaN.
207 */
208 constexpr auto log(auto const& arg)
209 {
210 auto const mathImpl = trait::getMathImpl(thisApi());
211 return internal::Log::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
212 }
213
214 /** Computes the base 2 logarithm of arg.
215 *
216 * Valid real arguments are non-negative. For other values the result
217 * may depend on the backend and compilation options, will likely
218 * be NaN.
219 */
220 constexpr auto log2(auto const& arg)
221 {
222 auto const mathImpl = trait::getMathImpl(thisApi());
223 return internal::Log2::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
224 }
225
226 /* Computes the base 10 logarithm of arg.
227 *
228 * Valid real arguments are non-negative. For other values the result
229 * may depend on the backend and compilation options, will likely
230 * be NaN.
231 */
232 constexpr auto log10(auto const& arg)
233 {
234 auto const mathImpl = trait::getMathImpl(thisApi());
235 return internal::Log10::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
236 }
237
238 // Tangent function
239 constexpr auto tan(auto const& arg)
240 {
241 auto const mathImpl = trait::getMathImpl(thisApi());
242 return internal::Tan::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
243 }
244
245 // Arc cosine function
246 constexpr auto acos(auto const& arg)
247 {
248 auto const mathImpl = trait::getMathImpl(thisApi());
249 return internal::Acos::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
250 }
251
252 // Arc sine function
253 constexpr auto asin(auto const& arg)
254 {
255 auto const mathImpl = trait::getMathImpl(thisApi());
256 return internal::Asin::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
257 }
258
259 constexpr auto isnan(auto const& arg)
260 {
261 auto const mathImpl = trait::getMathImpl(thisApi());
262 return internal::Isnan::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
263 }
264
265 //! Computes the complex conjugate of arg.
266 constexpr auto conj(auto const& arg)
267 {
268 auto const mathImpl = trait::getMathImpl(thisApi());
269 return internal::Conj::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(arg)>{}(mathImpl, arg);
270 }
271
272 constexpr auto min(auto const& a, auto const& b)
273 {
274 auto const mathImpl = trait::getMathImpl(thisApi());
275 return internal::Min::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(a), ALPAKA_TYPEOF(b)>{}(mathImpl, a, b);
276 }
277
278 constexpr auto max(auto const& a, auto const& b)
279 {
280 auto const mathImpl = trait::getMathImpl(thisApi());
281 return internal::Max::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(a), ALPAKA_TYPEOF(b)>{}(mathImpl, a, b);
282 }
283
284 constexpr auto pow(auto const& base, auto const& exp)
285 {
286 auto const mathImpl = trait::getMathImpl(thisApi());
288 mathImpl,
289 base,
290 exp);
291 }
292
293 constexpr auto fmod(auto const& x, auto const& y)
294 {
295 auto const mathImpl = trait::getMathImpl(thisApi());
296 return internal::Fmod::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(x), ALPAKA_TYPEOF(y)>{}(mathImpl, x, y);
297 }
298
299 constexpr auto remainder(auto const& x, auto const& y)
300 {
301 auto const mathImpl = trait::getMathImpl(thisApi());
302 return internal::Remainder::Op<ALPAKA_TYPEOF(mathImpl), ALPAKA_TYPEOF(x), ALPAKA_TYPEOF(y)>{}(mathImpl, x, y);
303 }
304
305 constexpr auto fma(auto const& x, auto const& y, auto const& z)
306 {
307 auto const mathImpl = trait::getMathImpl(thisApi());
309 mathImpl,
310 x,
311 y,
312 z);
313 }
314
315} // namespace alpaka::math
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
constexpr auto tanh(auto const &arg)
Definition math.hpp:75
constexpr auto exp(auto const &arg)
Definition math.hpp:140
constexpr auto floor(auto const &arg)
Definition math.hpp:196
constexpr auto acosh(auto const &arg)
Definition math.hpp:27
constexpr auto sin(auto const &arg)
Definition math.hpp:21
constexpr auto rsqrt(auto const &arg)
Definition math.hpp:171
constexpr auto asin(auto const &arg)
Definition math.hpp:253
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 sinh(auto const &arg)
Definition math.hpp:39
constexpr auto abs(auto const &arg)
Definition math.hpp:15
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 integer format), rounding halfway cases away from zero,...
Definition math.hpp:105
constexpr auto arg(auto const &arg)
Definition math.hpp:146
constexpr auto tan(auto const &arg)
Definition math.hpp:239
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 asinh(auto const &arg)
Definition math.hpp:33
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 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 acos(auto const &arg)
Definition math.hpp:246
constexpr auto erf(auto const &arg)
Definition math.hpp:190
constexpr auto log10(auto const &arg)
Definition math.hpp:232
constexpr auto min(auto const &a, auto const &b)
Definition math.hpp:272
constexpr auto conj(auto const &arg)
Computes the complex conjugate of arg.
Definition math.hpp:266
constexpr auto sqrt(auto const &arg)
Definition math.hpp:159
constexpr auto isfinite(auto const &arg)
Definition math.hpp:63
constexpr auto cosh(auto const &arg)
Definition math.hpp:184
constexpr auto llround(auto const &arg)
Computes the nearest integer value to arg (in integer format), rounding halfway cases away from zero,...
Definition math.hpp:114
constexpr auto atan(auto const &arg)
Definition math.hpp:45
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 atanh(auto const &arg)
Definition math.hpp:69
constexpr auto log(auto const &arg)
Computes the natural (base e) logarithm of arg.
Definition math.hpp:208
constexpr auto pow(auto const &base, auto const &exp)
Definition math.hpp:284
constexpr decltype(auto) getMathImpl(T_Api const api)
Definition trait.hpp:33
constexpr auto thisApi()
provides the API used during the execution of the current code path
Definition api.hpp:26