clang  6.0.0svn
__clang_cuda_cmath.h
Go to the documentation of this file.
1 /*===---- __clang_cuda_cmath.h - Device-side CUDA cmath support ------------===
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  *
21  *===-----------------------------------------------------------------------===
22  */
23 #ifndef __CLANG_CUDA_CMATH_H__
24 #define __CLANG_CUDA_CMATH_H__
25 #ifndef __CUDA__
26 #error "This file is for CUDA compilation only."
27 #endif
28 
29 #include <limits>
30 
31 // CUDA lets us use various std math functions on the device side. This file
32 // works in concert with __clang_cuda_math_forward_declares.h to make this work.
33 //
34 // Specifically, the forward-declares header declares __device__ overloads for
35 // these functions in the global namespace, then pulls them into namespace std
36 // with 'using' statements. Then this file implements those functions, after
37 // their implementations have been pulled in.
38 //
39 // It's important that we declare the functions in the global namespace and pull
40 // them into namespace std with using statements, as opposed to simply declaring
41 // these functions in namespace std, because our device functions need to
42 // overload the standard library functions, which may be declared in the global
43 // namespace or in std, depending on the degree of conformance of the stdlib
44 // implementation. Declaring in the global namespace and pulling into namespace
45 // std covers all of the known knowns.
46 
47 #define __DEVICE__ static __device__ __inline__ __attribute__((always_inline))
48 
49 __DEVICE__ long long abs(long long __n) { return ::llabs(__n); }
50 __DEVICE__ long abs(long __n) { return ::labs(__n); }
51 __DEVICE__ float abs(float __x) { return ::fabsf(__x); }
52 __DEVICE__ double abs(double __x) { return ::fabs(__x); }
53 __DEVICE__ float acos(float __x) { return ::acosf(__x); }
54 __DEVICE__ float asin(float __x) { return ::asinf(__x); }
55 __DEVICE__ float atan(float __x) { return ::atanf(__x); }
56 __DEVICE__ float atan2(float __x, float __y) { return ::atan2f(__x, __y); }
57 __DEVICE__ float ceil(float __x) { return ::ceilf(__x); }
58 __DEVICE__ float cos(float __x) { return ::cosf(__x); }
59 __DEVICE__ float cosh(float __x) { return ::coshf(__x); }
60 __DEVICE__ float exp(float __x) { return ::expf(__x); }
61 __DEVICE__ float fabs(float __x) { return ::fabsf(__x); }
62 __DEVICE__ float floor(float __x) { return ::floorf(__x); }
63 __DEVICE__ float fmod(float __x, float __y) { return ::fmodf(__x, __y); }
65  return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
66  FP_ZERO, __x);
67 }
68 __DEVICE__ int fpclassify(double __x) {
69  return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
70  FP_ZERO, __x);
71 }
72 __DEVICE__ float frexp(float __arg, int *__exp) {
73  return ::frexpf(__arg, __exp);
74 }
75 
76 // For inscrutable reasons, the CUDA headers define these functions for us on
77 // Windows.
78 #ifndef _MSC_VER
79 __DEVICE__ bool isinf(float __x) { return ::__isinff(__x); }
80 __DEVICE__ bool isinf(double __x) { return ::__isinf(__x); }
81 __DEVICE__ bool isfinite(float __x) { return ::__finitef(__x); }
82 // For inscrutable reasons, __finite(), the double-precision version of
83 // __finitef, does not exist when compiling for MacOS. __isfinited is available
84 // everywhere and is just as good.
85 __DEVICE__ bool isfinite(double __x) { return ::__isfinited(__x); }
86 __DEVICE__ bool isnan(float __x) { return ::__isnanf(__x); }
87 __DEVICE__ bool isnan(double __x) { return ::__isnan(__x); }
88 #endif
89 
90 __DEVICE__ bool isgreater(float __x, float __y) {
91  return __builtin_isgreater(__x, __y);
92 }
93 __DEVICE__ bool isgreater(double __x, double __y) {
94  return __builtin_isgreater(__x, __y);
95 }
96 __DEVICE__ bool isgreaterequal(float __x, float __y) {
97  return __builtin_isgreaterequal(__x, __y);
98 }
99 __DEVICE__ bool isgreaterequal(double __x, double __y) {
100  return __builtin_isgreaterequal(__x, __y);
101 }
102 __DEVICE__ bool isless(float __x, float __y) {
103  return __builtin_isless(__x, __y);
104 }
105 __DEVICE__ bool isless(double __x, double __y) {
106  return __builtin_isless(__x, __y);
107 }
108 __DEVICE__ bool islessequal(float __x, float __y) {
109  return __builtin_islessequal(__x, __y);
110 }
111 __DEVICE__ bool islessequal(double __x, double __y) {
112  return __builtin_islessequal(__x, __y);
113 }
114 __DEVICE__ bool islessgreater(float __x, float __y) {
115  return __builtin_islessgreater(__x, __y);
116 }
117 __DEVICE__ bool islessgreater(double __x, double __y) {
118  return __builtin_islessgreater(__x, __y);
119 }
120 __DEVICE__ bool isnormal(float __x) { return __builtin_isnormal(__x); }
121 __DEVICE__ bool isnormal(double __x) { return __builtin_isnormal(__x); }
122 __DEVICE__ bool isunordered(float __x, float __y) {
123  return __builtin_isunordered(__x, __y);
124 }
125 __DEVICE__ bool isunordered(double __x, double __y) {
126  return __builtin_isunordered(__x, __y);
127 }
128 __DEVICE__ float ldexp(float __arg, int __exp) {
129  return ::ldexpf(__arg, __exp);
130 }
131 __DEVICE__ float log(float __x) { return ::logf(__x); }
132 __DEVICE__ float log10(float __x) { return ::log10f(__x); }
133 __DEVICE__ float modf(float __x, float *__iptr) { return ::modff(__x, __iptr); }
134 __DEVICE__ float nexttoward(float __from, double __to) {
135  return __builtin_nexttowardf(__from, __to);
136 }
137 __DEVICE__ double nexttoward(double __from, double __to) {
138  return __builtin_nexttoward(__from, __to);
139 }
140 __DEVICE__ float nexttowardf(float __from, double __to) {
141  return __builtin_nexttowardf(__from, __to);
142 }
143 __DEVICE__ float pow(float __base, float __exp) {
144  return ::powf(__base, __exp);
145 }
146 __DEVICE__ float pow(float __base, int __iexp) {
147  return ::powif(__base, __iexp);
148 }
149 __DEVICE__ double pow(double __base, int __iexp) {
150  return ::powi(__base, __iexp);
151 }
152 __DEVICE__ bool signbit(float __x) { return ::__signbitf(__x); }
153 __DEVICE__ bool signbit(double __x) { return ::__signbitd(__x); }
154 __DEVICE__ float sin(float __x) { return ::sinf(__x); }
155 __DEVICE__ float sinh(float __x) { return ::sinhf(__x); }
156 __DEVICE__ float sqrt(float __x) { return ::sqrtf(__x); }
157 __DEVICE__ float tan(float __x) { return ::tanf(__x); }
158 __DEVICE__ float tanh(float __x) { return ::tanhf(__x); }
159 
160 // Now we've defined everything we promised we'd define in
161 // __clang_cuda_math_forward_declares.h. We need to do two additional things to
162 // fix up our math functions.
163 //
164 // 1) Define __device__ overloads for e.g. sin(int). The CUDA headers define
165 // only sin(float) and sin(double), which means that e.g. sin(0) is
166 // ambiguous.
167 //
168 // 2) Pull the __device__ overloads of "foobarf" math functions into namespace
169 // std. These are defined in the CUDA headers in the global namespace,
170 // independent of everything else we've done here.
171 
172 // We can't use std::enable_if, because we want to be pre-C++11 compatible. But
173 // we go ahead and unconditionally define functions that are only available when
174 // compiling for C++11 to match the behavior of the CUDA headers.
175 template<bool __B, class __T = void>
177 
178 template <class __T> struct __clang_cuda_enable_if<true, __T> {
179  typedef __T type;
180 };
181 
182 // Defines an overload of __fn that accepts one integral argument, calls
183 // __fn((double)x), and returns __retty.
184 #define __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(__retty, __fn) \
185  template <typename __T> \
186  __DEVICE__ \
187  typename __clang_cuda_enable_if<std::numeric_limits<__T>::is_integer, \
188  __retty>::type \
189  __fn(__T __x) { \
190  return ::__fn((double)__x); \
191  }
192 
193 // Defines an overload of __fn that accepts one two arithmetic arguments, calls
194 // __fn((double)x, (double)y), and returns a double.
195 //
196 // Note this is different from OVERLOAD_1, which generates an overload that
197 // accepts only *integral* arguments.
198 #define __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(__retty, __fn) \
199  template <typename __T1, typename __T2> \
200  __DEVICE__ typename __clang_cuda_enable_if< \
201  std::numeric_limits<__T1>::is_specialized && \
202  std::numeric_limits<__T2>::is_specialized, \
203  __retty>::type \
204  __fn(__T1 __x, __T2 __y) { \
205  return __fn((double)__x, (double)__y); \
206  }
207 
268 
269 #undef __CUDA_CLANG_FN_INTEGER_OVERLOAD_1
270 #undef __CUDA_CLANG_FN_INTEGER_OVERLOAD_2
271 
272 // Overloads for functions that don't match the patterns expected by
273 // __CUDA_CLANG_FN_INTEGER_OVERLOAD_{1,2}.
274 template <typename __T1, typename __T2, typename __T3>
276  std::numeric_limits<__T1>::is_specialized &&
277  std::numeric_limits<__T2>::is_specialized &&
278  std::numeric_limits<__T3>::is_specialized,
279  double>::type
280 fma(__T1 __x, __T2 __y, __T3 __z) {
281  return std::fma((double)__x, (double)__y, (double)__z);
282 }
283 
284 template <typename __T>
286  double>::type
287 frexp(__T __x, int *__exp) {
288  return std::frexp((double)__x, __exp);
289 }
290 
291 template <typename __T>
293  double>::type
294 ldexp(__T __x, int __exp) {
295  return std::ldexp((double)__x, __exp);
296 }
297 
298 template <typename __T>
300  double>::type
301 nexttoward(__T __from, double __to) {
302  return std::nexttoward((double)__from, __to);
303 }
304 
305 template <typename __T1, typename __T2>
307  std::numeric_limits<__T1>::is_specialized &&
308  std::numeric_limits<__T2>::is_specialized,
309  double>::type
310 remquo(__T1 __x, __T2 __y, int *__quo) {
311  return std::remquo((double)__x, (double)__y, __quo);
312 }
313 
314 template <typename __T>
316  double>::type
317 scalbln(__T __x, long __exp) {
318  return std::scalbln((double)__x, __exp);
319 }
320 
321 template <typename __T>
323  double>::type
324 scalbn(__T __x, int __exp) {
325  return std::scalbn((double)__x, __exp);
326 }
327 
328 // We need to define these overloads in exactly the namespace our standard
329 // library uses (including the right inline namespace), otherwise they won't be
330 // picked up by other functions in the standard library (e.g. functions in
331 // <complex>). Thus the ugliness below.
332 #ifdef _LIBCPP_BEGIN_NAMESPACE_STD
333 _LIBCPP_BEGIN_NAMESPACE_STD
334 #else
335 namespace std {
336 #ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
337 _GLIBCXX_BEGIN_NAMESPACE_VERSION
338 #endif
339 #endif
340 
341 // Pull the new overloads we defined above into namespace std.
407 
408 // Well this is fun: We need to pull these symbols in for libc++, but we can't
409 // pull them in with libstdc++, because its ::isinf and ::isnan are different
410 // than its std::isinf and std::isnan.
411 #ifndef __GLIBCXX__
414 #endif
415 
416 // Finally, pull the "foobarf" functions that CUDA defines in its headers into
417 // namespace std.
418 using ::acosf;
419 using ::acoshf;
420 using ::asinf;
421 using ::asinhf;
422 using ::atan2f;
423 using ::atanf;
424 using ::atanhf;
425 using ::cbrtf;
426 using ::ceilf;
427 using ::copysignf;
428 using ::cosf;
429 using ::coshf;
430 using ::erfcf;
431 using ::erff;
432 using ::exp2f;
433 using ::expf;
434 using ::expm1f;
435 using ::fabsf;
436 using ::fdimf;
437 using ::floorf;
438 using ::fmaf;
439 using ::fmaxf;
440 using ::fminf;
441 using ::fmodf;
442 using ::frexpf;
443 using ::hypotf;
444 using ::ilogbf;
445 using ::ldexpf;
446 using ::lgammaf;
447 using ::llrintf;
448 using ::llroundf;
449 using ::log10f;
450 using ::log1pf;
451 using ::log2f;
452 using ::logbf;
453 using ::logf;
454 using ::lrintf;
455 using ::lroundf;
456 using ::modff;
457 using ::nearbyintf;
458 using ::nextafterf;
461 using ::powf;
462 using ::remainderf;
463 using ::remquof;
464 using ::rintf;
465 using ::roundf;
466 using ::scalblnf;
467 using ::scalbnf;
468 using ::sinf;
469 using ::sinhf;
470 using ::sqrtf;
471 using ::tanf;
472 using ::tanhf;
473 using ::tgammaf;
474 using ::truncf;
475 
476 #ifdef _LIBCPP_END_NAMESPACE_STD
477 _LIBCPP_END_NAMESPACE_STD
478 #else
479 #ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
480 _GLIBCXX_END_NAMESPACE_VERSION
481 #endif
482 } // namespace std
483 #endif
484 
485 #undef __DEVICE__
486 
487 #endif
static __inline unsigned char unsigned int unsigned int __y
Definition: adxintrin.h:36
__DEVICE__ __clang_cuda_enable_if< std::numeric_limits< __T1 >::is_specialized &&std::numeric_limits< __T2 >::is_specialized, double >::type remquo(__T1 __x, __T2 __y, int *__quo)
__DEVICE__ bool signbit(float __x)
Test for sign bit.
__DEVICE__ float sinh(float __x)
Compute hyperbolic sine.
#define log1p(__x)
Definition: tgmath.h:969
__DEVICE__ float atan2(float __x, float __y)
Arc tangent of y / x.
#define trunc(__x)
Definition: tgmath.h:1232
__DEVICE__ long long abs(long long __n)
#define atanh(__x)
Definition: tgmath.h:244
#define log2(__x)
Definition: tgmath.h:986
__DEVICE__ float floor(float __x)
Round to integral value using the round to -ve infinity rounding mode.
#define erfc(__x)
Definition: tgmath.h:669
__DEVICE__ float sqrt(float __x)
Compute square root.
__DEVICE__ float modf(float __x, float *__iptr)
Decompose a floating-point number.
__DEVICE__ float asin(float __x)
Arc sine function.
__DEVICE__ float ceil(float __x)
Round to integral value using the round to positive infinity rounding mode.
#define fmax(__x, __y)
Definition: tgmath.h:778
__DEVICE__ bool isinf(float __x)
Test for infinity value (+ve or -ve) .
#define __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(__retty, __fn)
#define __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(__retty, __fn)
__DEVICE__ float acos(float __x)
Arc cosine function.
__DEVICE__ float log(float __x)
Compute natural logarithm.
Definition: Format.h:1821
#define fmin(__x, __y)
Definition: tgmath.h:796
#define logb(__x)
Definition: tgmath.h:1003
__DEVICE__ int fpclassify(float __x)
__DEVICE__ float sin(float __x)
Compute sine.
#define __DEVICE__
__DEVICE__ long labs(long)
#define asinh(__x)
Definition: tgmath.h:215
#define remainder(__x, __y)
Definition: tgmath.h:1106
__DEVICE__ bool isfinite(float __x)
Test for finite value.
#define tgamma(__x)
Definition: tgmath.h:1215
__DEVICE__ float fmod(float __x, float __y)
Modulus.
__DEVICE__ float tan(float __x)
Compute tangent.
#define lrint(__x)
Definition: tgmath.h:1020
__DEVICE__ bool islessequal(float __x, float __y)
Returns the component-wise compare of x <= y.
#define cbrt(__x)
Definition: tgmath.h:600
__DEVICE__ float ldexp(float __arg, int __exp)
Multiply x by 2 to the power n.
#define copysign(__x, __y)
Definition: tgmath.h:634
__DEVICE__ float fabs(float __x)
Compute absolute value of a floating-point number.
__DEVICE__ float nexttoward(float __from, double __to)
__DEVICE__ bool isunordered(float __x, float __y)
Test if arguments are unordered.
__DEVICE__ float frexp(float __arg, int *__exp)
Extract mantissa and exponent from x.
#define lgamma(__x)
Definition: tgmath.h:901
#define expm1(__x)
Definition: tgmath.h:703
#define acosh(__x)
Definition: tgmath.h:186
static __inline unsigned char unsigned int __x
Definition: adxintrin.h:36
__DEVICE__ __clang_cuda_enable_if< std::numeric_limits< __T >::is_integer, double >::type scalbln(__T __x, long __exp)
__DEVICE__ bool isgreaterequal(float __x, float __y)
Returns the component-wise compare of x >= y.
#define rint(__x)
Definition: tgmath.h:1147
#define fdim(__x, __y)
Definition: tgmath.h:720
#define round(__x)
Definition: tgmath.h:1164
#define exp2(__x)
Definition: tgmath.h:686
#define ilogb(__x)
Definition: tgmath.h:867
#define lround(__x)
Definition: tgmath.h:1037
__DEVICE__ float cos(float __x)
Compute cosine.
__DEVICE__ float log10(float __x)
Compute a base 10 logarithm.
__DEVICE__ bool isnan(float __x)
Test for a NaN.
__DEVICE__ bool isgreater(float __x, float __y)
Returns the component-wise compare of x > y.
__DEVICE__ float atan(float __x)
Arc tangent function.
__DEVICE__ bool islessgreater(float __x, float __y)
Returns the component-wise compare of (x < y) || (x > y) .
__DEVICE__ long long llround(float)
__DEVICE__ long long llabs(long long)
__DEVICE__ __clang_cuda_enable_if< std::numeric_limits< __T >::is_integer, double >::type scalbn(__T __x, int __exp)
#define erf(__x)
Definition: tgmath.h:652
#define llrint(__x)
Definition: tgmath.h:918
#define nextafter(__x, __y)
Definition: tgmath.h:1071
#define nearbyint(__x)
Definition: tgmath.h:1054
__DEVICE__ float cosh(float __x)
Compute hyperbolic cosine.
__DEVICE__ float pow(float __base, float __exp)
Compute x to the power y.
__DEVICE__ __clang_cuda_enable_if< std::numeric_limits< __T1 >::is_specialized &&std::numeric_limits< __T2 >::is_specialized &&std::numeric_limits< __T3 >::is_specialized, double >::type fma(__T1 __x, __T2 __y, __T3 __z)
__DEVICE__ float nexttowardf(float __from, double __to)
__DEVICE__ bool isless(float __x, float __y)
Returns the component-wise compare of x < y.
__DEVICE__ float tanh(float __x)
Compute hyperbolic tangent.
#define true
Definition: stdbool.h:32
__DEVICE__ float exp(float __x)
Compute the base e exponential function of x.
#define hypot(__x, __y)
Definition: tgmath.h:849
__DEVICE__ bool isnormal(float __x)
Test for a normal value.