1 /*===---- __clang_cuda_cmath.h - Device-side CUDA cmath support ------------===
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
21 *===-----------------------------------------------------------------------===
23 #ifndef __CLANG_CUDA_CMATH_H__
24 #define __CLANG_CUDA_CMATH_H__
26 #error "This file is for CUDA compilation only."
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.
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.
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.
47 #define __DEVICE__ static __device__ __inline__ __attribute__((always_inline))
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); }
64 __DEVICE__ int fpclassify(float __x) {
65 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
68 __DEVICE__ int fpclassify(double __x) {
69 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
72 __DEVICE__ float frexp(float __arg, int *__exp) {
73 return ::frexpf(__arg, __exp);
75 __DEVICE__ bool isinf(float __x) { return ::__isinff(__x); }
76 __DEVICE__ bool isinf(double __x) { return ::__isinf(__x); }
77 __DEVICE__ bool isfinite(float __x) { return ::__finitef(__x); }
78 // For inscrutable reasons, __finite(), the double-precision version of
79 // __finitef, does not exist when compiling for MacOS. __isfinited is available
80 // everywhere and is just as good.
81 __DEVICE__ bool isfinite(double __x) { return ::__isfinited(__x); }
82 __DEVICE__ bool isgreater(float __x, float __y) {
83 return __builtin_isgreater(__x, __y);
85 __DEVICE__ bool isgreater(double __x, double __y) {
86 return __builtin_isgreater(__x, __y);
88 __DEVICE__ bool isgreaterequal(float __x, float __y) {
89 return __builtin_isgreaterequal(__x, __y);
91 __DEVICE__ bool isgreaterequal(double __x, double __y) {
92 return __builtin_isgreaterequal(__x, __y);
94 __DEVICE__ bool isless(float __x, float __y) {
95 return __builtin_isless(__x, __y);
97 __DEVICE__ bool isless(double __x, double __y) {
98 return __builtin_isless(__x, __y);
100 __DEVICE__ bool islessequal(float __x, float __y) {
101 return __builtin_islessequal(__x, __y);
103 __DEVICE__ bool islessequal(double __x, double __y) {
104 return __builtin_islessequal(__x, __y);
106 __DEVICE__ bool islessgreater(float __x, float __y) {
107 return __builtin_islessgreater(__x, __y);
109 __DEVICE__ bool islessgreater(double __x, double __y) {
110 return __builtin_islessgreater(__x, __y);
112 __DEVICE__ bool isnan(float __x) { return ::__isnanf(__x); }
113 __DEVICE__ bool isnan(double __x) { return ::__isnan(__x); }
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);
119 __DEVICE__ bool isunordered(double __x, double __y) {
120 return __builtin_isunordered(__x, __y);
122 __DEVICE__ float ldexp(float __arg, int __exp) {
123 return ::ldexpf(__arg, __exp);
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 nexttoward(float __from, double __to) {
129 return __builtin_nexttowardf(__from, __to);
131 __DEVICE__ double nexttoward(double __from, double __to) {
132 return __builtin_nexttoward(__from, __to);
134 __DEVICE__ float nexttowardf(float __from, double __to) {
135 return __builtin_nexttowardf(__from, __to);
137 __DEVICE__ float pow(float __base, float __exp) {
138 return ::powf(__base, __exp);
140 __DEVICE__ float pow(float __base, int __iexp) {
141 return ::powif(__base, __iexp);
143 __DEVICE__ double pow(double __base, int __iexp) {
144 return ::powi(__base, __iexp);
146 __DEVICE__ bool signbit(float __x) { return ::__signbitf(__x); }
147 __DEVICE__ bool signbit(double __x) { return ::__signbitd(__x); }
148 __DEVICE__ float sin(float __x) { return ::sinf(__x); }
149 __DEVICE__ float sinh(float __x) { return ::sinhf(__x); }
150 __DEVICE__ float sqrt(float __x) { return ::sqrtf(__x); }
151 __DEVICE__ float tan(float __x) { return ::tanf(__x); }
152 __DEVICE__ float tanh(float __x) { return ::tanhf(__x); }
154 // Now we've defined everything we promised we'd define in
155 // __clang_cuda_math_forward_declares.h. We need to do two additional things to
156 // fix up our math functions.
158 // 1) Define __device__ overloads for e.g. sin(int). The CUDA headers define
159 // only sin(float) and sin(double), which means that e.g. sin(0) is
162 // 2) Pull the __device__ overloads of "foobarf" math functions into namespace
163 // std. These are defined in the CUDA headers in the global namespace,
164 // independent of everything else we've done here.
166 // We can't use std::enable_if, because we want to be pre-C++11 compatible. But
167 // we go ahead and unconditionally define functions that are only available when
168 // compiling for C++11 to match the behavior of the CUDA headers.
169 template<bool __B, class __T = void>
170 struct __clang_cuda_enable_if {};
172 template <class __T> struct __clang_cuda_enable_if<true, __T> {
176 // Defines an overload of __fn that accepts one integral argument, calls
177 // __fn((double)x), and returns __retty.
178 #define __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(__retty, __fn) \
179 template <typename __T> \
181 typename __clang_cuda_enable_if<std::numeric_limits<__T>::is_integer, \
184 return ::__fn((double)__x); \
187 // Defines an overload of __fn that accepts one two arithmetic arguments, calls
188 // __fn((double)x, (double)y), and returns a double.
190 // Note this is different from OVERLOAD_1, which generates an overload that
191 // accepts only *integral* arguments.
192 #define __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(__retty, __fn) \
193 template <typename __T1, typename __T2> \
194 __DEVICE__ typename __clang_cuda_enable_if< \
195 std::numeric_limits<__T1>::is_specialized && \
196 std::numeric_limits<__T2>::is_specialized, \
198 __fn(__T1 __x, __T2 __y) { \
199 return __fn((double)__x, (double)__y); \
202 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, acos)
203 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, acosh)
204 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, asin)
205 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, asinh)
206 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, atan)
207 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, atan2);
208 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, atanh)
209 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, cbrt)
210 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, ceil)
211 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, copysign);
212 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, cos)
213 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, cosh)
214 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, erf)
215 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, erfc)
216 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, exp)
217 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, exp2)
218 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, expm1)
219 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, fabs)
220 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, fdim);
221 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, floor)
222 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, fmax);
223 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, fmin);
224 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, fmod);
225 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(int, fpclassify)
226 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, hypot);
227 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(int, ilogb)
228 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(bool, isfinite)
229 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(bool, isgreater);
230 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(bool, isgreaterequal);
231 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(bool, isinf);
232 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(bool, isless);
233 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(bool, islessequal);
234 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(bool, islessgreater);
235 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(bool, isnan);
236 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(bool, isnormal)
237 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(bool, isunordered);
238 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, lgamma)
239 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, log)
240 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, log10)
241 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, log1p)
242 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, log2)
243 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, logb)
244 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(long long, llrint)
245 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(long long, llround)
246 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(long, lrint)
247 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(long, lround)
248 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, nearbyint);
249 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, nextafter);
250 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, pow);
251 __CUDA_CLANG_FN_INTEGER_OVERLOAD_2(double, remainder);
252 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, rint);
253 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, round);
254 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(bool, signbit)
255 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, sin)
256 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, sinh)
257 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, sqrt)
258 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, tan)
259 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, tanh)
260 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, tgamma)
261 __CUDA_CLANG_FN_INTEGER_OVERLOAD_1(double, trunc);
263 #undef __CUDA_CLANG_FN_INTEGER_OVERLOAD_1
264 #undef __CUDA_CLANG_FN_INTEGER_OVERLOAD_2
266 // Overloads for functions that don't match the patterns expected by
267 // __CUDA_CLANG_FN_INTEGER_OVERLOAD_{1,2}.
268 template <typename __T1, typename __T2, typename __T3>
269 __DEVICE__ typename __clang_cuda_enable_if<
270 std::numeric_limits<__T1>::is_specialized &&
271 std::numeric_limits<__T2>::is_specialized &&
272 std::numeric_limits<__T3>::is_specialized,
274 fma(__T1 __x, __T2 __y, __T3 __z) {
275 return std::fma((double)__x, (double)__y, (double)__z);
278 template <typename __T>
279 __DEVICE__ typename __clang_cuda_enable_if<std::numeric_limits<__T>::is_integer,
281 frexp(__T __x, int *__exp) {
282 return std::frexp((double)__x, __exp);
285 template <typename __T>
286 __DEVICE__ typename __clang_cuda_enable_if<std::numeric_limits<__T>::is_integer,
288 ldexp(__T __x, int __exp) {
289 return std::ldexp((double)__x, __exp);
292 template <typename __T>
293 __DEVICE__ typename __clang_cuda_enable_if<std::numeric_limits<__T>::is_integer,
295 nexttoward(__T __from, double __to) {
296 return std::nexttoward((double)__from, __to);
299 template <typename __T1, typename __T2>
300 __DEVICE__ typename __clang_cuda_enable_if<
301 std::numeric_limits<__T1>::is_specialized &&
302 std::numeric_limits<__T2>::is_specialized,
304 remquo(__T1 __x, __T2 __y, int *__quo) {
305 return std::remquo((double)__x, (double)__y, __quo);
308 template <typename __T>
309 __DEVICE__ typename __clang_cuda_enable_if<std::numeric_limits<__T>::is_integer,
311 scalbln(__T __x, long __exp) {
312 return std::scalbln((double)__x, __exp);
315 template <typename __T>
316 __DEVICE__ typename __clang_cuda_enable_if<std::numeric_limits<__T>::is_integer,
318 scalbn(__T __x, int __exp) {
319 return std::scalbn((double)__x, __exp);
322 // We need to define these overloads in exactly the namespace our standard
323 // library uses (including the right inline namespace), otherwise they won't be
324 // picked up by other functions in the standard library (e.g. functions in
325 // <complex>). Thus the ugliness below.
326 #ifdef _LIBCPP_BEGIN_NAMESPACE_STD
327 _LIBCPP_BEGIN_NAMESPACE_STD
330 #ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
331 _GLIBCXX_BEGIN_NAMESPACE_VERSION
335 // Pull the new overloads we defined above into namespace std.
366 using ::isgreaterequal;
369 using ::islessgreater;
402 // Well this is fun: We need to pull these symbols in for libc++, but we can't
403 // pull them in with libstdc++, because its ::isinf and ::isnan are different
404 // than its std::isinf and std::isnan.
410 // Finally, pull the "foobarf" functions that CUDA defines in its headers into
470 #ifdef _LIBCPP_END_NAMESPACE_STD
471 _LIBCPP_END_NAMESPACE_STD
473 #ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION
474 _GLIBCXX_END_NAMESPACE_VERSION