1 /*===-- __clang_cuda_complex_builtins - CUDA impls of runtime complex fns ---===
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
7 *===-----------------------------------------------------------------------===
10 #ifndef __CLANG_CUDA_COMPLEX_BUILTINS
11 #define __CLANG_CUDA_COMPLEX_BUILTINS
13 // This header defines __muldc3, __mulsc3, __divdc3, and __divsc3. These are
14 // libgcc functions that clang assumes are available when compiling c99 complex
15 // operations. (These implementations come from libc++, and have been modified
16 // to work with CUDA and OpenMP target offloading [in C and C++ mode].)
18 #pragma push_macro("__DEVICE__")
19 #if defined(__OPENMP_NVPTX__) || defined(__OPENMP_AMDGCN__)
20 #pragma omp declare target
21 #define __DEVICE__ __attribute__((noinline, nothrow, cold, weak))
23 #define __DEVICE__ __device__ inline
26 // To make the algorithms available for C and C++ in CUDA and OpenMP we select
27 // different but equivalent function versions. TODO: For OpenMP we currently
28 // select the native builtins as the overload support for templates is lacking.
29 #if !defined(__OPENMP_NVPTX__) && !defined(__OPENMP_AMDGCN__)
30 #define _ISNANd std::isnan
31 #define _ISNANf std::isnan
32 #define _ISINFd std::isinf
33 #define _ISINFf std::isinf
34 #define _ISFINITEd std::isfinite
35 #define _ISFINITEf std::isfinite
36 #define _COPYSIGNd std::copysign
37 #define _COPYSIGNf std::copysign
38 #define _SCALBNd std::scalbn
39 #define _SCALBNf std::scalbn
40 #define _ABSd std::abs
41 #define _ABSf std::abs
42 #define _LOGBd std::logb
43 #define _LOGBf std::logb
44 // Rather than pulling in std::max from algorithm everytime, use available ::max.
49 #define _ISNANd __ocml_isnan_f64
50 #define _ISNANf __ocml_isnan_f32
51 #define _ISINFd __ocml_isinf_f64
52 #define _ISINFf __ocml_isinf_f32
53 #define _ISFINITEd __ocml_isfinite_f64
54 #define _ISFINITEf __ocml_isfinite_f32
55 #define _COPYSIGNd __ocml_copysign_f64
56 #define _COPYSIGNf __ocml_copysign_f32
57 #define _SCALBNd __ocml_scalbn_f64
58 #define _SCALBNf __ocml_scalbn_f32
59 #define _ABSd __ocml_fabs_f64
60 #define _ABSf __ocml_fabs_f32
61 #define _LOGBd __ocml_logb_f64
62 #define _LOGBf __ocml_logb_f32
63 #define _fmaxd __ocml_fmax_f64
64 #define _fmaxf __ocml_fmax_f32
66 #define _ISNANd __nv_isnand
67 #define _ISNANf __nv_isnanf
68 #define _ISINFd __nv_isinfd
69 #define _ISINFf __nv_isinff
70 #define _ISFINITEd __nv_isfinited
71 #define _ISFINITEf __nv_finitef
72 #define _COPYSIGNd __nv_copysign
73 #define _COPYSIGNf __nv_copysignf
74 #define _SCALBNd __nv_scalbn
75 #define _SCALBNf __nv_scalbnf
76 #define _ABSd __nv_fabs
77 #define _ABSf __nv_fabsf
78 #define _LOGBd __nv_logb
79 #define _LOGBf __nv_logbf
80 #define _fmaxd __nv_fmax
81 #define _fmaxf __nv_fmaxf
85 #if defined(__cplusplus)
89 __DEVICE__
double _Complex
__muldc3(double __a
, double __b
, double __c
,
91 double __ac
= __a
* __c
;
92 double __bd
= __b
* __d
;
93 double __ad
= __a
* __d
;
94 double __bc
= __b
* __c
;
96 __real__(z
) = __ac
- __bd
;
97 __imag__(z
) = __ad
+ __bc
;
98 if (_ISNANd(__real__(z
)) && _ISNANd(__imag__(z
))) {
100 if (_ISINFd(__a
) || _ISINFd(__b
)) {
101 __a
= _COPYSIGNd(_ISINFd(__a
) ? 1 : 0, __a
);
102 __b
= _COPYSIGNd(_ISINFd(__b
) ? 1 : 0, __b
);
104 __c
= _COPYSIGNd(0, __c
);
106 __d
= _COPYSIGNd(0, __d
);
109 if (_ISINFd(__c
) || _ISINFd(__d
)) {
110 __c
= _COPYSIGNd(_ISINFd(__c
) ? 1 : 0, __c
);
111 __d
= _COPYSIGNd(_ISINFd(__d
) ? 1 : 0, __d
);
113 __a
= _COPYSIGNd(0, __a
);
115 __b
= _COPYSIGNd(0, __b
);
119 (_ISINFd(__ac
) || _ISINFd(__bd
) || _ISINFd(__ad
) || _ISINFd(__bc
))) {
121 __a
= _COPYSIGNd(0, __a
);
123 __b
= _COPYSIGNd(0, __b
);
125 __c
= _COPYSIGNd(0, __c
);
127 __d
= _COPYSIGNd(0, __d
);
131 // Can't use std::numeric_limits<double>::infinity() -- that doesn't have
132 // a device overload (and isn't constexpr before C++11, naturally).
133 __real__(z
) = __builtin_huge_val() * (__a
* __c
- __b
* __d
);
134 __imag__(z
) = __builtin_huge_val() * (__a
* __d
+ __b
* __c
);
140 __DEVICE__
float _Complex
__mulsc3(float __a
, float __b
, float __c
, float __d
) {
141 float __ac
= __a
* __c
;
142 float __bd
= __b
* __d
;
143 float __ad
= __a
* __d
;
144 float __bc
= __b
* __c
;
146 __real__(z
) = __ac
- __bd
;
147 __imag__(z
) = __ad
+ __bc
;
148 if (_ISNANf(__real__(z
)) && _ISNANf(__imag__(z
))) {
150 if (_ISINFf(__a
) || _ISINFf(__b
)) {
151 __a
= _COPYSIGNf(_ISINFf(__a
) ? 1 : 0, __a
);
152 __b
= _COPYSIGNf(_ISINFf(__b
) ? 1 : 0, __b
);
154 __c
= _COPYSIGNf(0, __c
);
156 __d
= _COPYSIGNf(0, __d
);
159 if (_ISINFf(__c
) || _ISINFf(__d
)) {
160 __c
= _COPYSIGNf(_ISINFf(__c
) ? 1 : 0, __c
);
161 __d
= _COPYSIGNf(_ISINFf(__d
) ? 1 : 0, __d
);
163 __a
= _COPYSIGNf(0, __a
);
165 __b
= _COPYSIGNf(0, __b
);
169 (_ISINFf(__ac
) || _ISINFf(__bd
) || _ISINFf(__ad
) || _ISINFf(__bc
))) {
171 __a
= _COPYSIGNf(0, __a
);
173 __b
= _COPYSIGNf(0, __b
);
175 __c
= _COPYSIGNf(0, __c
);
177 __d
= _COPYSIGNf(0, __d
);
181 __real__(z
) = __builtin_huge_valf() * (__a
* __c
- __b
* __d
);
182 __imag__(z
) = __builtin_huge_valf() * (__a
* __d
+ __b
* __c
);
188 __DEVICE__
double _Complex
__divdc3(double __a
, double __b
, double __c
,
191 // Can't use std::max, because that's defined in <algorithm>, and we don't
192 // want to pull that in for every compile. The CUDA headers define
193 // ::max(float, float) and ::max(double, double), which is sufficient for us.
194 double __logbw
= _LOGBd(_fmaxd(_ABSd(__c
), _ABSd(__d
)));
195 if (_ISFINITEd(__logbw
)) {
196 __ilogbw
= (int)__logbw
;
197 __c
= _SCALBNd(__c
, -__ilogbw
);
198 __d
= _SCALBNd(__d
, -__ilogbw
);
200 double __denom
= __c
* __c
+ __d
* __d
;
202 __real__(z
) = _SCALBNd((__a
* __c
+ __b
* __d
) / __denom
, -__ilogbw
);
203 __imag__(z
) = _SCALBNd((__b
* __c
- __a
* __d
) / __denom
, -__ilogbw
);
204 if (_ISNANd(__real__(z
)) && _ISNANd(__imag__(z
))) {
205 if ((__denom
== 0.0) && (!_ISNANd(__a
) || !_ISNANd(__b
))) {
206 __real__(z
) = _COPYSIGNd(__builtin_huge_val(), __c
) * __a
;
207 __imag__(z
) = _COPYSIGNd(__builtin_huge_val(), __c
) * __b
;
208 } else if ((_ISINFd(__a
) || _ISINFd(__b
)) && _ISFINITEd(__c
) &&
210 __a
= _COPYSIGNd(_ISINFd(__a
) ? 1.0 : 0.0, __a
);
211 __b
= _COPYSIGNd(_ISINFd(__b
) ? 1.0 : 0.0, __b
);
212 __real__(z
) = __builtin_huge_val() * (__a
* __c
+ __b
* __d
);
213 __imag__(z
) = __builtin_huge_val() * (__b
* __c
- __a
* __d
);
214 } else if (_ISINFd(__logbw
) && __logbw
> 0.0 && _ISFINITEd(__a
) &&
216 __c
= _COPYSIGNd(_ISINFd(__c
) ? 1.0 : 0.0, __c
);
217 __d
= _COPYSIGNd(_ISINFd(__d
) ? 1.0 : 0.0, __d
);
218 __real__(z
) = 0.0 * (__a
* __c
+ __b
* __d
);
219 __imag__(z
) = 0.0 * (__b
* __c
- __a
* __d
);
225 __DEVICE__
float _Complex
__divsc3(float __a
, float __b
, float __c
, float __d
) {
227 float __logbw
= _LOGBf(_fmaxf(_ABSf(__c
), _ABSf(__d
)));
228 if (_ISFINITEf(__logbw
)) {
229 __ilogbw
= (int)__logbw
;
230 __c
= _SCALBNf(__c
, -__ilogbw
);
231 __d
= _SCALBNf(__d
, -__ilogbw
);
233 float __denom
= __c
* __c
+ __d
* __d
;
235 __real__(z
) = _SCALBNf((__a
* __c
+ __b
* __d
) / __denom
, -__ilogbw
);
236 __imag__(z
) = _SCALBNf((__b
* __c
- __a
* __d
) / __denom
, -__ilogbw
);
237 if (_ISNANf(__real__(z
)) && _ISNANf(__imag__(z
))) {
238 if ((__denom
== 0) && (!_ISNANf(__a
) || !_ISNANf(__b
))) {
239 __real__(z
) = _COPYSIGNf(__builtin_huge_valf(), __c
) * __a
;
240 __imag__(z
) = _COPYSIGNf(__builtin_huge_valf(), __c
) * __b
;
241 } else if ((_ISINFf(__a
) || _ISINFf(__b
)) && _ISFINITEf(__c
) &&
243 __a
= _COPYSIGNf(_ISINFf(__a
) ? 1 : 0, __a
);
244 __b
= _COPYSIGNf(_ISINFf(__b
) ? 1 : 0, __b
);
245 __real__(z
) = __builtin_huge_valf() * (__a
* __c
+ __b
* __d
);
246 __imag__(z
) = __builtin_huge_valf() * (__b
* __c
- __a
* __d
);
247 } else if (_ISINFf(__logbw
) && __logbw
> 0 && _ISFINITEf(__a
) &&
249 __c
= _COPYSIGNf(_ISINFf(__c
) ? 1 : 0, __c
);
250 __d
= _COPYSIGNf(_ISINFf(__d
) ? 1 : 0, __d
);
251 __real__(z
) = 0 * (__a
* __c
+ __b
* __d
);
252 __imag__(z
) = 0 * (__b
* __c
- __a
* __d
);
258 #if defined(__cplusplus)
279 #if defined(__OPENMP_NVPTX__) || defined(__OPENMP_AMDGCN__)
280 #pragma omp end declare target
283 #pragma pop_macro("__DEVICE__")
285 #endif // __CLANG_CUDA_COMPLEX_BUILTINS