ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
Loading...
Searching...
No Matches
__clang_cuda_cmath.h
Go to the documentation of this file.
1/*===---- __clang_cuda_cmath.h - Device-side CUDA cmath support ------------===
2 *
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 *
7 *===-----------------------------------------------------------------------===
8 */
9#ifndef __CLANG_CUDA_CMATH_H__
10#define __CLANG_CUDA_CMATH_H__
11#ifndef __CUDA__
12#error "This file is for CUDA compilation only."
13#endif
14
15#ifndef __OPENMP_NVPTX__
16#include <limits>
17#endif
18
19// CUDA lets us use various std math functions on the device side. This file
20// works in concert with __clang_cuda_math_forward_declares.h to make this work.
21//
22// Specifically, the forward-declares header declares __device__ overloads for
23// these functions in the global namespace, then pulls them into namespace std
24// with 'using' statements. Then this file implements those functions, after
25// their implementations have been pulled in.
26//
27// It's important that we declare the functions in the global namespace and pull
28// them into namespace std with using statements, as opposed to simply declaring
29// these functions in namespace std, because our device functions need to
30// overload the standard library functions, which may be declared in the global
31// namespace or in std, depending on the degree of conformance of the stdlib
32// implementation. Declaring in the global namespace and pulling into namespace
33// std covers all of the known knowns.
34
35#ifdef __OPENMP_NVPTX__
36#define __DEVICE__ static constexpr __attribute__((always_inline, nothrow))
37#else
38#define __DEVICE__ static __device__ __inline__ __attribute__((always_inline))
39#endif
40
41__DEVICE__ long long abs(long long __n) { return ::llabs(__n); }
42__DEVICE__ long abs(long __n) { return ::labs(__n); }
43__DEVICE__ float abs(float __x) { return ::fabsf(__x); }
44__DEVICE__ double abs(double __x) { return ::fabs(__x); }
45__DEVICE__ float acos(float __x) { return ::acosf(__x); }
46__DEVICE__ float asin(float __x) { return ::asinf(__x); }
47__DEVICE__ float atan(float __x) { return ::atanf(__x); }
48__DEVICE__ float atan2(float __x, float __y) { return ::atan2f(__x, __y); }
49__DEVICE__ float ceil(float __x) { return ::ceilf(__x); }
50__DEVICE__ float cos(float __x) { return ::cosf(__x); }
51__DEVICE__ float cosh(float __x) { return ::coshf(__x); }
52__DEVICE__ float exp(float __x) { return ::expf(__x); }
53__DEVICE__ float fabs(float __x) { return ::fabsf(__x); }
54__DEVICE__ float floor(float __x) { return ::floorf(__x); }
55__DEVICE__ float fmod(float __x, float __y) { return ::fmodf(__x, __y); }
57 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
58 FP_ZERO, __x);
59}
61 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
62 FP_ZERO, __x);
63}
64__DEVICE__ float frexp(float __arg, int *__exp) {
65 return ::frexpf(__arg, __exp);
66}
67
68// For inscrutable reasons, the CUDA headers define these functions for us on
69// Windows. For OpenMP we omit these as some old system headers have
70// non-conforming `isinf(float)` and `isnan(float)` implementations that return
71// an `int`. The system versions of these functions should be fine anyway.
72#if !defined(_MSC_VER) && !defined(__OPENMP_NVPTX__)
73__DEVICE__ bool isinf(float __x) { return ::__isinff(__x); }
74__DEVICE__ bool isinf(double __x) { return ::__isinf(__x); }
75__DEVICE__ bool isfinite(float __x) { return ::__finitef(__x); }
76// For inscrutable reasons, __finite(), the double-precision version of
77// __finitef, does not exist when compiling for MacOS. __isfinited is available
78// everywhere and is just as good.
79__DEVICE__ bool isfinite(double __x) { return ::__isfinited(__x); }
80__DEVICE__ bool isnan(float __x) { return ::__isnanf(__x); }
81__DEVICE__ bool isnan(double __x) { return ::__isnan(__x); }
82#endif
83
84__DEVICE__ bool isgreater(float __x, float __y) {
85 return __builtin_isgreater(__x, __y);
86}
87__DEVICE__ bool isgreater(double __x, double __y) {
88 return __builtin_isgreater(__x, __y);
89}
90__DEVICE__ bool isgreaterequal(float __x, float __y) {
91 return __builtin_isgreaterequal(__x, __y);
92}
93__DEVICE__ bool isgreaterequal(double __x, double __y) {
94 return __builtin_isgreaterequal(__x, __y);
95}
96__DEVICE__ bool isless(float __x, float __y) {
97 return __builtin_isless(__x, __y);
98}
99__DEVICE__ bool isless(double __x, double __y) {
100 return __builtin_isless(__x, __y);
101}
102__DEVICE__ bool islessequal(float __x, float __y) {
103 return __builtin_islessequal(__x, __y);
104}
105__DEVICE__ bool islessequal(double __x, double __y) {
106 return __builtin_islessequal(__x, __y);
107}
108__DEVICE__ bool islessgreater(float __x, float __y) {
109 return __builtin_islessgreater(__x, __y);
110}
111__DEVICE__ bool islessgreater(double __x, double __y) {
112 return __builtin_islessgreater(__x, __y);
113}
114__DEVICE__ bool isnormal(float __x) { return __builtin_isnormal(__x); }
115__DEVICE__ bool isnormal(double __x) { return __builtin_isnormal(__x); }
116__DEVICE__ bool isunordered(float __x, float __y) {
117 return __builtin_isunordered(__x, __y);
118}
119__DEVICE__ bool isunordered(double __x, double __y) {
120 return __builtin_isunordered(__x, __y);
121}
122__DEVICE__ float ldexp(float __arg, int __exp) {
123 return ::ldexpf(__arg, __exp);
124}
125__DEVICE__ float log(float __x) { return ::logf(__x); }
126__DEVICE__ float log10(float __x) { return ::log10f(__x); }
127__DEVICE__ float modf(float __x, float *__iptr) { return ::modff(__x, __iptr); }
128__DEVICE__ float pow(float __base, float __exp) {
129 return ::powf(__base, __exp);
130}
131__DEVICE__ float pow(float __base, int __iexp) {
132 return ::powif(__base, __iexp);
133}
134__DEVICE__ double pow(double __base, int __iexp) {
135 return ::powi(__base, __iexp);
136}
137__DEVICE__ bool signbit(float __x) { return ::__signbitf(__x); }
138__DEVICE__ bool signbit(double __x) { return ::__signbitd(__x); }
139__DEVICE__ float sin(float __x) { return ::sinf(__x); }
140__DEVICE__ float sinh(float __x) { return ::sinhf(__x); }
141__DEVICE__ float sqrt(float __x) { return ::sqrtf(__x); }
142__DEVICE__ float tan(float __x) { return ::tanf(__x); }
143__DEVICE__ float tanh(float __x) { return ::tanhf(__x); }
144
145// Notably missing above is nexttoward. We omit it because
146// libdevice doesn't provide an implementation, and we don't want to be in the
147// business of implementing tricky libm functions in this header.
148
149#ifndef __OPENMP_NVPTX__
150
151// Now we've defined everything we promised we'd define in
152// __clang_cuda_math_forward_declares.h. We need to do two additional things to
153// fix up our math functions.
154//
155// 1) Define __device__ overloads for e.g. sin(int). The CUDA headers define
156// only sin(float) and sin(double), which means that e.g. sin(0) is
157// ambiguous.
158//
159// 2) Pull the __device__ overloads of "foobarf" math functions into namespace
160// std. These are defined in the CUDA headers in the global namespace,
161// independent of everything else we've done here.
162
163// We can't use std::enable_if, because we want to be pre-C++11 compatible. But
164// we go ahead and unconditionally define functions that are only available when
165// compiling for C++11 to match the behavior of the CUDA headers.
166template<bool __B, class __T = void>
168
169template <class __T> struct __clang_cuda_enable_if<true, __T> {
170 typedef __T type;
171};
172
173// Defines an overload of __fn that accepts one integral argument, calls
174// __fn((double)x), and returns __retty.
175#define __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(__retty, __fn) \
176 template <typename __T> \
177 __DEVICE__ \
178 typename __clang_cuda_enable_if<std::numeric_limits<__T>::is_integer, \
179 __retty>::type \
180 __fn(__T __x) { \
181 return ::__fn((double)__x); \
182 }
183
184// Defines an overload of __fn that accepts one two arithmetic arguments, calls
185// __fn((double)x, (double)y), and returns a double.
186//
187// Note this is different from OVERLOAD_1, which generates an overload that
188// accepts only *integral* arguments.
189#define __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(__retty, __fn) \
190 template <typename __T1, typename __T2> \
191 __DEVICE__ typename __clang_cuda_enable_if< \
192 std::numeric_limits<__T1>::is_specialized && \
193 std::numeric_limits<__T2>::is_specialized, \
194 __retty>::type \
195 __fn(__T1 __x, __T2 __y) { \
196 return __fn((double)__x, (double)__y); \
197 }
198
259
260#undef __CUDA_CLANG_FN_INTEGER_OVERLOAD_1
261#undef __CUDA_CLANG_FN_INTEGER_OVERLOAD_2
262
263// Overloads for functions that don't match the patterns expected by
264// __CUDA_CLANG_FN_INTEGER_OVERLOAD_{1,2}.
265template <typename __T1, typename __T2, typename __T3>
267 std::numeric_limits<__T1>::is_specialized &&
268 std::numeric_limits<__T2>::is_specialized &&
269 std::numeric_limits<__T3>::is_specialized,
270 double>::type
271fma(__T1 __x, __T2 __y, __T3 __z) {
272 return std::fma((double)__x, (double)__y, (double)__z);
273}
274
275template <typename __T>
277 double>::type
278frexp(__T __x, int *__exp) {
279 return std::frexp((double)__x, __exp);
280}
281
282template <typename __T>
284 double>::type
285ldexp(__T __x, int __exp) {
286 return std::ldexp((double)__x, __exp);
287}
288
289template <typename __T1, typename __T2>
291 std::numeric_limits<__T1>::is_specialized &&
292 std::numeric_limits<__T2>::is_specialized,
293 double>::type
294remquo(__T1 __x, __T2 __y, int *__quo) {
295 return std::remquo((double)__x, (double)__y, __quo);
296}
297
298template <typename __T>
300 double>::type
301scalbln(__T __x, long __exp) {
302 return std::scalbln((double)__x, __exp);
303}
304
305template <typename __T>
307 double>::type
308scalbn(__T __x, int __exp) {
309 return std::scalbn((double)__x, __exp);
310}
311
312// We need to define these overloads in exactly the namespace our standard
313// library uses (including the right inline namespace), otherwise they won't be
314// picked up by other functions in the standard library (e.g. functions in
315// <complex>). Thus the ugliness below.
316#ifdef _LIBCPP_BEGIN_NAMESPACE_STD
317_LIBCPP_BEGIN_NAMESPACE_STD
318#else
319namespace std {
320#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
321_GLIBCXX_BEGIN_NAMESPACE_VERSION
322#endif
323#endif
324
325// Pull the new overloads we defined above into namespace std.
326using ::acos;
327using ::acosh;
328using ::asin;
329using ::asinh;
330using ::atan;
331using ::atan2;
332using ::atanh;
333using ::cbrt;
334using ::ceil;
335using ::copysign;
336using ::cos;
337using ::cosh;
338using ::erf;
339using ::erfc;
340using ::exp;
341using ::exp2;
342using ::expm1;
343using ::fabs;
344using ::fdim;
345using ::floor;
346using ::fma;
347using ::fmax;
348using ::fmin;
349using ::fmod;
350using ::fpclassify;
351using ::frexp;
352using ::hypot;
353using ::ilogb;
354using ::isfinite;
355using ::isgreater;
356using ::isgreaterequal;
357using ::isless;
358using ::islessequal;
359using ::islessgreater;
360using ::isnormal;
361using ::isunordered;
362using ::ldexp;
363using ::lgamma;
364using ::llrint;
365using ::llround;
366using ::log;
367using ::log10;
368using ::log1p;
369using ::log2;
370using ::logb;
371using ::lrint;
372using ::lround;
373using ::nearbyint;
374using ::nextafter;
375using ::pow;
376using ::remainder;
377using ::remquo;
378using ::rint;
379using ::round;
380using ::scalbln;
381using ::scalbn;
382using ::signbit;
383using ::sin;
384using ::sinh;
385using ::sqrt;
386using ::tan;
387using ::tanh;
388using ::tgamma;
389using ::trunc;
390
391// Well this is fun: We need to pull these symbols in for libc++, but we can't
392// pull them in with libstdc++, because its ::isinf and ::isnan are different
393// than its std::isinf and std::isnan.
394#ifndef __GLIBCXX__
395using ::isinf;
396using ::isnan;
397#endif
398
399// Finally, pull the "foobarf" functions that CUDA defines in its headers into
400// namespace std.
401using ::acosf;
402using ::acoshf;
403using ::asinf;
404using ::asinhf;
405using ::atan2f;
406using ::atanf;
407using ::atanhf;
408using ::cbrtf;
409using ::ceilf;
410using ::copysignf;
411using ::cosf;
412using ::coshf;
413using ::erfcf;
414using ::erff;
415using ::exp2f;
416using ::expf;
417using ::expm1f;
418using ::fabsf;
419using ::fdimf;
420using ::floorf;
421using ::fmaf;
422using ::fmaxf;
423using ::fminf;
424using ::fmodf;
425using ::frexpf;
426using ::hypotf;
427using ::ilogbf;
428using ::ldexpf;
429using ::lgammaf;
430using ::llrintf;
431using ::llroundf;
432using ::log10f;
433using ::log1pf;
434using ::log2f;
435using ::logbf;
436using ::logf;
437using ::lrintf;
438using ::lroundf;
439using ::modff;
440using ::nearbyintf;
441using ::nextafterf;
442using ::powf;
443using ::remainderf;
444using ::remquof;
445using ::rintf;
446using ::roundf;
447using ::scalblnf;
448using ::scalbnf;
449using ::sinf;
450using ::sinhf;
451using ::sqrtf;
452using ::tanf;
453using ::tanhf;
454using ::tgammaf;
455using ::truncf;
456
457#ifdef _LIBCPP_END_NAMESPACE_STD
458_LIBCPP_END_NAMESPACE_STD
459#else
460#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
461_GLIBCXX_END_NAMESPACE_VERSION
462#endif
463} // namespace std
464#endif
465
466#endif // __OPENMP_NVPTX__
467
468#undef __DEVICE__
469
470#endif
__DEVICE__ bool isunordered(float __x, float __y)
Test if arguments are unordered.
__DEVICE__ bool isgreater(float __x, float __y)
Returns the component-wise compare of x > y.
__DEVICE__ bool islessgreater(float __x, float __y)
Returns the component-wise compare of (x < y) || (x > y) .
__DEVICE__ bool isnan(float __x)
Test for a NaN.
__DEVICE__ int fpclassify(float __x)
__DEVICE__ bool isfinite(float __x)
Test for finite value.
__DEVICE__ bool signbit(float __x)
Test for sign bit.
__DEVICE__ bool isinf(float __x)
Test for infinity value (+ve or -ve) .
#define __DEVICE__
__DEVICE__ float modf(float __x, float *__iptr)
Decompose a floating-point number.
#define __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(__retty, __fn)
__DEVICE__ bool islessequal(float __x, float __y)
Returns the component-wise compare of x <= y.
#define __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(__retty, __fn)
__DEVICE__ long long abs(long long __n)
__DEVICE__ bool isless(float __x, float __y)
Returns the component-wise compare of x < y.
__DEVICE__ bool isnormal(float __x)
Test for a normal value.
__DEVICE__ bool isgreaterequal(float __x, float __y)
Returns the component-wise compare of x >= y.
static __inline unsigned char unsigned int __x
Definition adxintrin.h:22
static __inline unsigned char unsigned int unsigned int __y
Definition adxintrin.h:22
STL namespace.
#define true
Definition stdbool.h:16
#define sinh(__x)
Definition tgmath.h:373
#define asin(__x)
Definition tgmath.h:112
#define scalbln(__x, __y)
Definition tgmath.h:1182
#define sqrt(__x)
Definition tgmath.h:520
#define acos(__x)
Definition tgmath.h:83
#define fmin(__x, __y)
Definition tgmath.h:780
#define exp(__x)
Definition tgmath.h:431
#define ilogb(__x)
Definition tgmath.h:851
#define copysign(__x, __y)
Definition tgmath.h:618
#define erf(__x)
Definition tgmath.h:636
#define atanh(__x)
Definition tgmath.h:228
#define remquo(__x, __y, __z)
Definition tgmath.h:1111
#define nextafter(__x, __y)
Definition tgmath.h:1055
#define frexp(__x, __y)
Definition tgmath.h:816
#define asinh(__x)
Definition tgmath.h:199
#define erfc(__x)
Definition tgmath.h:653
#define atan2(__x, __y)
Definition tgmath.h:566
#define hypot(__x, __y)
Definition tgmath.h:833
#define exp2(__x)
Definition tgmath.h:670
#define sin(__x)
Definition tgmath.h:286
#define cbrt(__x)
Definition tgmath.h:584
#define log2(__x)
Definition tgmath.h:970
#define llround(__x)
Definition tgmath.h:919
#define cosh(__x)
Definition tgmath.h:344
#define trunc(__x)
Definition tgmath.h:1216
#define fmax(__x, __y)
Definition tgmath.h:762
#define ldexp(__x, __y)
Definition tgmath.h:868
#define acosh(__x)
Definition tgmath.h:170
#define tgamma(__x)
Definition tgmath.h:1199
#define scalbn(__x, __y)
Definition tgmath.h:1165
#define round(__x)
Definition tgmath.h:1148
#define fmod(__x, __y)
Definition tgmath.h:798
#define llrint(__x)
Definition tgmath.h:902
#define tan(__x)
Definition tgmath.h:315
#define cos(__x)
Definition tgmath.h:257
#define log10(__x)
Definition tgmath.h:936
#define fabs(__x)
Definition tgmath.h:549
#define pow(__x, __y)
Definition tgmath.h:490
#define log1p(__x)
Definition tgmath.h:953
#define rint(__x)
Definition tgmath.h:1131
#define expm1(__x)
Definition tgmath.h:687
#define remainder(__x, __y)
Definition tgmath.h:1090
#define fdim(__x, __y)
Definition tgmath.h:704
#define lgamma(__x)
Definition tgmath.h:885
#define tanh(__x)
Definition tgmath.h:402
#define lrint(__x)
Definition tgmath.h:1004
#define atan(__x)
Definition tgmath.h:141
#define floor(__x)
Definition tgmath.h:722
#define ceil(__x)
Definition tgmath.h:601
#define log(__x)
Definition tgmath.h:460
#define logb(__x)
Definition tgmath.h:987
#define nearbyint(__x)
Definition tgmath.h:1038
#define lround(__x)
Definition tgmath.h:1021
#define fma(__x, __y, __z)
Definition tgmath.h:742