alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
MathStdLib.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
3 * SPDX-License-Identifier: MPL-2.0
4 */
5
6#pragma once
7
10
11#include <bit>
12#include <cstdint>
13#include <type_traits>
14
15namespace alpaka::math
16{
17 //! The standard library abs, implementation covered by the general template.
18 class AbsStdLib : public interface::Implements<ConceptMathAbs, AbsStdLib>
19 {
20 };
21
22 //! The standard library acos, implementation covered by the general template.
23 class AcosStdLib : public interface::Implements<ConceptMathAcos, AcosStdLib>
24 {
25 };
26
27 //! The standard library acos, implementation covered by the general template.
28 class AcoshStdLib : public interface::Implements<ConceptMathAcosh, AcoshStdLib>
29 {
30 };
31
32 //! The standard library arg, implementation covered by the general template.
33 class ArgStdLib : public interface::Implements<ConceptMathArg, ArgStdLib>
34 {
35 };
36
37 //! The standard library asin, implementation covered by the general template.
38 class AsinStdLib : public interface::Implements<ConceptMathAsin, AsinStdLib>
39 {
40 };
41
42 //! The standard library asinh, implementation covered by the general template.
43 class AsinhStdLib : public interface::Implements<ConceptMathAsinh, AsinhStdLib>
44 {
45 };
46
47 //! The standard library atan, implementation covered by the general template.
48 class AtanStdLib : public interface::Implements<ConceptMathAtan, AtanStdLib>
49 {
50 };
51
52 //! The standard library atanh, implementation covered by the general template.
53 class AtanhStdLib : public interface::Implements<ConceptMathAtanh, AtanhStdLib>
54 {
55 };
56
57 //! The standard library atan2, implementation covered by the general template.
58 class Atan2StdLib : public interface::Implements<ConceptMathAtan2, Atan2StdLib>
59 {
60 };
61
62 //! The standard library cbrt, implementation covered by the general template.
63 class CbrtStdLib : public interface::Implements<ConceptMathCbrt, CbrtStdLib>
64 {
65 };
66
67 //! The standard library ceil, implementation covered by the general template.
68 class CeilStdLib : public interface::Implements<ConceptMathCeil, CeilStdLib>
69 {
70 };
71
72 //! The standard library conj, implementation covered by the general template.
73 class ConjStdLib : public interface::Implements<ConceptMathConj, ConjStdLib>
74 {
75 };
76
77 //! The standard library copysign, implementation covered by the general template.
78 class CopysignStdLib : public interface::Implements<ConceptMathCopysign, CopysignStdLib>
79 {
80 };
81
82 //! The standard library cos, implementation covered by the general template.
83 class CosStdLib : public interface::Implements<ConceptMathCos, CosStdLib>
84 {
85 };
86
87 //! The standard library cosh, implementation covered by the general template.
88 class CoshStdLib : public interface::Implements<ConceptMathCosh, CoshStdLib>
89 {
90 };
91
92 //! The standard library erf, implementation covered by the general template.
93 class ErfStdLib : public interface::Implements<ConceptMathErf, ErfStdLib>
94 {
95 };
96
97 //! The standard library exp, implementation covered by the general template.
98 class ExpStdLib : public interface::Implements<ConceptMathExp, ExpStdLib>
99 {
100 };
101
102 //! The standard library floor, implementation covered by the general template.
103 class FloorStdLib : public interface::Implements<ConceptMathFloor, FloorStdLib>
104 {
105 };
106
107 //! The standard library fma, implementation covered by the general template.
108 class FmaStdLib : public interface::Implements<ConceptMathFma, FmaStdLib>
109 {
110 };
111
112 //! The standard library fmod, implementation covered by the general template.
113 class FmodStdLib : public interface::Implements<ConceptMathFmod, FmodStdLib>
114 {
115 };
116
117 //! The standard library isfinite, implementation covered by the general template.
118 class IsfiniteStdLib : public interface::Implements<ConceptMathIsfinite, IsfiniteStdLib>
119 {
120 };
121
122 //! The standard library isinf, implementation covered by the general template.
123 class IsinfStdLib : public interface::Implements<ConceptMathIsinf, IsinfStdLib>
124 {
125 };
126
127 //! The standard library isnan, implementation covered by the general template.
128 class IsnanStdLib : public interface::Implements<ConceptMathIsnan, IsnanStdLib>
129 {
130 };
131
132 //! The standard library log, implementation covered by the general template.
133 class LogStdLib : public interface::Implements<ConceptMathLog, LogStdLib>
134 {
135 };
136
137 //! The standard library log2, implementation covered by the general template.
138 class Log2StdLib : public interface::Implements<ConceptMathLog2, Log2StdLib>
139 {
140 };
141
142 //! The standard library log10, implementation covered by the general template.
143 class Log10StdLib : public interface::Implements<ConceptMathLog10, Log10StdLib>
144 {
145 };
146
147 //! The standard library max.
148 class MaxStdLib : public interface::Implements<ConceptMathMax, MaxStdLib>
149 {
150 };
151
152 //! The standard library min.
153 class MinStdLib : public interface::Implements<ConceptMathMin, MinStdLib>
154 {
155 };
156
157 //! The standard library pow, implementation covered by the general template.
158 class PowStdLib : public interface::Implements<ConceptMathPow, PowStdLib>
159 {
160 };
161
162 //! The standard library remainder, implementation covered by the general template.
163 class RemainderStdLib : public interface::Implements<ConceptMathRemainder, RemainderStdLib>
164 {
165 };
166
167 //! The standard library round, implementation covered by the general template.
168 class RoundStdLib : public interface::Implements<ConceptMathRound, RoundStdLib>
169 {
170 };
171
172 //! The standard library rsqrt, implementation covered by the general template.
173 class RsqrtStdLib : public interface::Implements<ConceptMathRsqrt, RsqrtStdLib>
174 {
175 };
176
177 //! The standard library sin, implementation covered by the general template.
178 class SinStdLib : public interface::Implements<ConceptMathSin, SinStdLib>
179 {
180 };
181
182 //! The standard library sinh, implementation covered by the general template.
183 class SinhStdLib : public interface::Implements<ConceptMathSinh, SinhStdLib>
184 {
185 };
186
187 //! The standard library sincos, implementation covered by the general template.
188 class SinCosStdLib : public interface::Implements<ConceptMathSinCos, SinCosStdLib>
189 {
190 };
191
192 //! The standard library sqrt, implementation covered by the general template.
193 class SqrtStdLib : public interface::Implements<ConceptMathSqrt, SqrtStdLib>
194 {
195 };
196
197 //! The standard library tan, implementation covered by the general template.
198 class TanStdLib : public interface::Implements<ConceptMathTan, TanStdLib>
199 {
200 };
201
202 //! The standard library tanh, implementation covered by the general template.
203 class TanhStdLib : public interface::Implements<ConceptMathTanh, TanhStdLib>
204 {
205 };
206
207 //! The standard library trunc, implementation covered by the general template.
208 class TruncStdLib : public interface::Implements<ConceptMathTrunc, TruncStdLib>
209 {
210 };
211
212 //! The standard library math trait specializations.
214 : public AbsStdLib
215 , public AcosStdLib
216 , public AcoshStdLib
217 , public ArgStdLib
218 , public AsinStdLib
219 , public AsinhStdLib
220 , public AtanStdLib
221 , public AtanhStdLib
222 , public Atan2StdLib
223 , public CbrtStdLib
224 , public CeilStdLib
225 , public ConjStdLib
226 , public CopysignStdLib
227 , public CosStdLib
228 , public CoshStdLib
229 , public ErfStdLib
230 , public ExpStdLib
231 , public FloorStdLib
232 , public FmaStdLib
233 , public FmodStdLib
234 , public LogStdLib
235 , public Log2StdLib
236 , public Log10StdLib
237 , public MaxStdLib
238 , public MinStdLib
239 , public PowStdLib
240 , public RemainderStdLib
241 , public RoundStdLib
242 , public RsqrtStdLib
243 , public SinStdLib
244 , public SinhStdLib
245 , public SinCosStdLib
246 , public SqrtStdLib
247 , public TanStdLib
248 , public TanhStdLib
249 , public TruncStdLib
250 , public IsnanStdLib
251 , public IsinfStdLib
252 , public IsfiniteStdLib
253 {
254 };
255
256 namespace trait
257 {
258 //! The standard library max trait specialization.
259 template<typename Tx, typename Ty>
260 struct Max<MaxStdLib, Tx, Ty, std::enable_if_t<std::is_arithmetic_v<Tx> && std::is_arithmetic_v<Ty>>>
261 {
262 ALPAKA_FN_HOST auto operator()(MaxStdLib const& /* max_ctx */, Tx const& x, Ty const& y)
263 {
264 using std::fmax;
265 using std::max;
266
267 if constexpr(std::is_integral_v<Tx> && std::is_integral_v<Ty>)
268 return max(x, y);
269 else if constexpr(
270 is_decayed_v<Tx, float> || is_decayed_v<Ty, float> || is_decayed_v<Tx, double>
271 || is_decayed_v<Ty, double>)
272 return fmax(x, y);
273 else
274 static_assert(!sizeof(Tx), "Unsupported data type");
275
276 ALPAKA_UNREACHABLE(std::common_type_t<Tx, Ty>{});
277 }
278 };
279
280 //! The standard library min trait specialization.
281 template<typename Tx, typename Ty>
282 struct Min<MinStdLib, Tx, Ty, std::enable_if_t<std::is_arithmetic_v<Tx> && std::is_arithmetic_v<Ty>>>
283 {
284 ALPAKA_FN_HOST auto operator()(MinStdLib const& /* min_ctx */, Tx const& x, Ty const& y)
285 {
286 using std::fmin;
287 using std::min;
288
289 if constexpr(std::is_integral_v<Tx> && std::is_integral_v<Ty>)
290 return min(x, y);
291 else if constexpr(
292 is_decayed_v<Tx, float> || is_decayed_v<Ty, float> || is_decayed_v<Tx, double>
293 || is_decayed_v<Ty, double>)
294 return fmin(x, y);
295 else
296 static_assert(!sizeof(Tx), "Unsupported data type");
297
298 ALPAKA_UNREACHABLE(std::common_type_t<Tx, Ty>{});
299 }
300 };
301
302 //! Custom IEEE 754 bitwise implementation of isfinite.
303 //! std counterpart does not work correctly for some compiler flags at CPU backend
304 template<typename TArg>
305 struct Isfinite<IsfiniteStdLib, TArg, std::enable_if_t<std::is_floating_point_v<TArg>>>
306 {
307 auto operator()(IsfiniteStdLib const& /* ctx */, TArg const& arg) -> bool
308 {
309 if constexpr(std::is_same_v<TArg, float>)
310 {
311 constexpr std::uint32_t expMask = 0x7F80'0000;
312 std::uint32_t bits = std::bit_cast<std::uint32_t>(arg);
313 bool result = (bits & expMask) != expMask;
314 return result;
315 }
316 else if constexpr(std::is_same_v<TArg, double>)
317 {
318 constexpr std::uint64_t expMask = 0x7FF0'0000'0000'0000ULL;
319 std::uint64_t bits = std::bit_cast<std::uint64_t>(arg);
320 bool result = (bits & expMask) != expMask;
321 return result;
322 }
323 else
324 {
325 static_assert(!sizeof(TArg), "Unsupported floating-point type");
326 ALPAKA_UNREACHABLE(TArg{});
327 }
328 }
329 };
330
331 //! Custom IEEE 754 bitwise implementation of isinf
332 //! std counterpart does not work correctly for some compiler flags at CPU backend
333 template<typename TArg>
334 struct Isinf<IsinfStdLib, TArg, std::enable_if_t<std::is_floating_point_v<TArg>>>
335 {
336 auto operator()(IsinfStdLib const& /* ctx */, TArg const& arg) -> bool
337 {
338 if constexpr(std::is_same_v<TArg, float>)
339 {
340 constexpr std::uint32_t expMask = 0x7F80'0000;
341 constexpr std::uint32_t fracMask = 0x007F'FFFF;
342 std::uint32_t bits = std::bit_cast<std::uint32_t>(arg);
343 bool result = ((bits & expMask) == expMask) && !(bits & fracMask);
344 return result;
345 }
346 else if constexpr(std::is_same_v<TArg, double>)
347 {
348 constexpr std::uint64_t expMask = 0x7FF0'0000'0000'0000ULL;
349 constexpr std::uint64_t fracMask = 0x000F'FFFF'FFFF'FFFFULL;
350 std::uint64_t bits = std::bit_cast<std::uint64_t>(arg);
351 bool result = ((bits & expMask) == expMask) && !(bits & fracMask);
352 return result;
353 }
354 else
355 {
356 static_assert(!sizeof(TArg), "Unsupported floating-point type");
357 ALPAKA_UNREACHABLE(TArg{});
358 }
359 }
360 };
361
362 //! Custom IEEE 754 bitwise implementation of isnan
363 //! std counterpart does not work correctly for some compiler flags at CPU backend
364 template<typename TArg>
365 struct Isnan<IsnanStdLib, TArg, std::enable_if_t<std::is_floating_point_v<TArg>>>
366 {
367 auto operator()(IsnanStdLib const& /* ctx */, TArg const& arg) -> bool
368 {
369 if constexpr(std::is_same_v<TArg, float>)
370 {
371 constexpr std::uint32_t expMask = 0x7F80'0000;
372 constexpr std::uint32_t fracMask = 0x007F'FFFF;
373 std::uint32_t bits = std::bit_cast<std::uint32_t>(arg);
374 bool result = ((bits & expMask) == expMask) && (bits & fracMask);
375 return result;
376 }
377 else if constexpr(std::is_same_v<TArg, double>)
378 {
379 constexpr std::uint64_t expMask = 0x7FF0'0000'0000'0000ULL;
380 constexpr std::uint64_t fracMask = 0x000F'FFFF'FFFF'FFFFULL;
381 std::uint64_t bits = std::bit_cast<std::uint64_t>(arg);
382 bool result = ((bits & expMask) == expMask) && (bits & fracMask);
383 return result;
384 }
385 else
386 {
387 static_assert(!sizeof(TArg), "Unsupported floating-point type");
388 ALPAKA_UNREACHABLE(TArg{});
389 }
390 }
391 };
392
393
394 } // namespace trait
395
396} // namespace alpaka::math
#define ALPAKA_UNREACHABLE(...)
Before CUDA 11.5 nvcc is unable to correctly identify return statements in 'if constexpr' branches....
The standard library abs, implementation covered by the general template.
The standard library acos, implementation covered by the general template.
The standard library acos, implementation covered by the general template.
The standard library arg, implementation covered by the general template.
The standard library asin, implementation covered by the general template.
The standard library asinh, implementation covered by the general template.
The standard library atan2, implementation covered by the general template.
The standard library atan, implementation covered by the general template.
The standard library atanh, implementation covered by the general template.
The standard library cbrt, implementation covered by the general template.
The standard library ceil, implementation covered by the general template.
The standard library conj, implementation covered by the general template.
The standard library copysign, implementation covered by the general template.
The standard library cos, implementation covered by the general template.
The standard library cosh, implementation covered by the general template.
The standard library erf, implementation covered by the general template.
The standard library exp, implementation covered by the general template.
The standard library floor, implementation covered by the general template.
The standard library fma, implementation covered by the general template.
The standard library fmod, implementation covered by the general template.
The standard library isfinite, implementation covered by the general template.
The standard library isinf, implementation covered by the general template.
The standard library isnan, implementation covered by the general template.
The standard library log10, implementation covered by the general template.
The standard library log2, implementation covered by the general template.
The standard library log, implementation covered by the general template.
The standard library math trait specializations.
The standard library max.
The standard library min.
The standard library pow, implementation covered by the general template.
The standard library remainder, implementation covered by the general template.
The standard library round, implementation covered by the general template.
The standard library rsqrt, implementation covered by the general template.
The standard library sincos, implementation covered by the general template.
The standard library sin, implementation covered by the general template.
The standard library sinh, implementation covered by the general template.
The standard library sqrt, implementation covered by the general template.
The standard library tan, implementation covered by the general template.
The standard library tanh, implementation covered by the general template.
The standard library trunc, implementation covered by the general template.
#define ALPAKA_FN_HOST
Definition Common.hpp:40
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto max(T const &max_ctx, Tx const &x, Ty const &y)
Returns the larger of two arguments. NaNs are treated as missing data (between a NaN and a numeric va...
Definition Traits.hpp:1263
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto min(T const &min_ctx, Tx const &x, Ty const &y)
Returns the smaller of two arguments. NaNs are treated as missing data (between a NaN and a numeric v...
Definition Traits.hpp:1280
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto arg(T const &arg_ctx, TArgument const &argument)
Computes the complex argument of the value.
Definition Traits.hpp:912
STL namespace.
Tag used in class inheritance hierarchies that describes that a specific interface (TInterface) is im...
Definition Interface.hpp:15
ALPAKA_FN_HOST_ACC auto operator()(T const &, TArg const &arg)
Definition Traits.hpp:560
ALPAKA_FN_HOST_ACC auto operator()(T const &, TArg const &arg)
Definition Traits.hpp:573
ALPAKA_FN_HOST_ACC auto operator()(T const &, TArg const &arg)
Definition Traits.hpp:586
ALPAKA_FN_HOST_ACC auto operator()(T const &, Tx const &x, Ty const &y)
Definition Traits.hpp:638
ALPAKA_FN_HOST_ACC auto operator()(T const &, Tx const &x, Ty const &y)
Definition Traits.hpp:651