__clang_cuda_complex_builtins.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*===-- __clang_cuda_complex_builtins - CUDA impls of runtime complex fns ---===
  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_COMPLEX_BUILTINS
  24. #define __CLANG_CUDA_COMPLEX_BUILTINS
  25. // This header defines __muldc3, __mulsc3, __divdc3, and __divsc3. These are
  26. // libgcc functions that clang assumes are available when compiling c99 complex
  27. // operations. (These implementations come from libc++, and have been modified
  28. // to work with CUDA.)
  29. extern "C" inline __device__ double _Complex __muldc3(double __a, double __b,
  30. double __c, double __d) {
  31. double __ac = __a * __c;
  32. double __bd = __b * __d;
  33. double __ad = __a * __d;
  34. double __bc = __b * __c;
  35. double _Complex z;
  36. __real__(z) = __ac - __bd;
  37. __imag__(z) = __ad + __bc;
  38. if (std::isnan(__real__(z)) && std::isnan(__imag__(z))) {
  39. int __recalc = 0;
  40. if (std::isinf(__a) || std::isinf(__b)) {
  41. __a = std::copysign(std::isinf(__a) ? 1 : 0, __a);
  42. __b = std::copysign(std::isinf(__b) ? 1 : 0, __b);
  43. if (std::isnan(__c))
  44. __c = std::copysign(0, __c);
  45. if (std::isnan(__d))
  46. __d = std::copysign(0, __d);
  47. __recalc = 1;
  48. }
  49. if (std::isinf(__c) || std::isinf(__d)) {
  50. __c = std::copysign(std::isinf(__c) ? 1 : 0, __c);
  51. __d = std::copysign(std::isinf(__d) ? 1 : 0, __d);
  52. if (std::isnan(__a))
  53. __a = std::copysign(0, __a);
  54. if (std::isnan(__b))
  55. __b = std::copysign(0, __b);
  56. __recalc = 1;
  57. }
  58. if (!__recalc && (std::isinf(__ac) || std::isinf(__bd) ||
  59. std::isinf(__ad) || std::isinf(__bc))) {
  60. if (std::isnan(__a))
  61. __a = std::copysign(0, __a);
  62. if (std::isnan(__b))
  63. __b = std::copysign(0, __b);
  64. if (std::isnan(__c))
  65. __c = std::copysign(0, __c);
  66. if (std::isnan(__d))
  67. __d = std::copysign(0, __d);
  68. __recalc = 1;
  69. }
  70. if (__recalc) {
  71. // Can't use std::numeric_limits<double>::infinity() -- that doesn't have
  72. // a device overload (and isn't constexpr before C++11, naturally).
  73. __real__(z) = __builtin_huge_valf() * (__a * __c - __b * __d);
  74. __imag__(z) = __builtin_huge_valf() * (__a * __d + __b * __c);
  75. }
  76. }
  77. return z;
  78. }
  79. extern "C" inline __device__ float _Complex __mulsc3(float __a, float __b,
  80. float __c, float __d) {
  81. float __ac = __a * __c;
  82. float __bd = __b * __d;
  83. float __ad = __a * __d;
  84. float __bc = __b * __c;
  85. float _Complex z;
  86. __real__(z) = __ac - __bd;
  87. __imag__(z) = __ad + __bc;
  88. if (std::isnan(__real__(z)) && std::isnan(__imag__(z))) {
  89. int __recalc = 0;
  90. if (std::isinf(__a) || std::isinf(__b)) {
  91. __a = std::copysign(std::isinf(__a) ? 1 : 0, __a);
  92. __b = std::copysign(std::isinf(__b) ? 1 : 0, __b);
  93. if (std::isnan(__c))
  94. __c = std::copysign(0, __c);
  95. if (std::isnan(__d))
  96. __d = std::copysign(0, __d);
  97. __recalc = 1;
  98. }
  99. if (std::isinf(__c) || std::isinf(__d)) {
  100. __c = std::copysign(std::isinf(__c) ? 1 : 0, __c);
  101. __d = std::copysign(std::isinf(__d) ? 1 : 0, __d);
  102. if (std::isnan(__a))
  103. __a = std::copysign(0, __a);
  104. if (std::isnan(__b))
  105. __b = std::copysign(0, __b);
  106. __recalc = 1;
  107. }
  108. if (!__recalc && (std::isinf(__ac) || std::isinf(__bd) ||
  109. std::isinf(__ad) || std::isinf(__bc))) {
  110. if (std::isnan(__a))
  111. __a = std::copysign(0, __a);
  112. if (std::isnan(__b))
  113. __b = std::copysign(0, __b);
  114. if (std::isnan(__c))
  115. __c = std::copysign(0, __c);
  116. if (std::isnan(__d))
  117. __d = std::copysign(0, __d);
  118. __recalc = 1;
  119. }
  120. if (__recalc) {
  121. __real__(z) = __builtin_huge_valf() * (__a * __c - __b * __d);
  122. __imag__(z) = __builtin_huge_valf() * (__a * __d + __b * __c);
  123. }
  124. }
  125. return z;
  126. }
  127. extern "C" inline __device__ double _Complex __divdc3(double __a, double __b,
  128. double __c, double __d) {
  129. int __ilogbw = 0;
  130. // Can't use std::max, because that's defined in <algorithm>, and we don't
  131. // want to pull that in for every compile. The CUDA headers define
  132. // ::max(float, float) and ::max(double, double), which is sufficient for us.
  133. double __logbw = std::logb(max(std::abs(__c), std::abs(__d)));
  134. if (std::isfinite(__logbw)) {
  135. __ilogbw = (int)__logbw;
  136. __c = std::scalbn(__c, -__ilogbw);
  137. __d = std::scalbn(__d, -__ilogbw);
  138. }
  139. double __denom = __c * __c + __d * __d;
  140. double _Complex z;
  141. __real__(z) = std::scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
  142. __imag__(z) = std::scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
  143. if (std::isnan(__real__(z)) && std::isnan(__imag__(z))) {
  144. if ((__denom == 0.0) && (!std::isnan(__a) || !std::isnan(__b))) {
  145. __real__(z) = std::copysign(__builtin_huge_valf(), __c) * __a;
  146. __imag__(z) = std::copysign(__builtin_huge_valf(), __c) * __b;
  147. } else if ((std::isinf(__a) || std::isinf(__b)) && std::isfinite(__c) &&
  148. std::isfinite(__d)) {
  149. __a = std::copysign(std::isinf(__a) ? 1.0 : 0.0, __a);
  150. __b = std::copysign(std::isinf(__b) ? 1.0 : 0.0, __b);
  151. __real__(z) = __builtin_huge_valf() * (__a * __c + __b * __d);
  152. __imag__(z) = __builtin_huge_valf() * (__b * __c - __a * __d);
  153. } else if (std::isinf(__logbw) && __logbw > 0.0 && std::isfinite(__a) &&
  154. std::isfinite(__b)) {
  155. __c = std::copysign(std::isinf(__c) ? 1.0 : 0.0, __c);
  156. __d = std::copysign(std::isinf(__d) ? 1.0 : 0.0, __d);
  157. __real__(z) = 0.0 * (__a * __c + __b * __d);
  158. __imag__(z) = 0.0 * (__b * __c - __a * __d);
  159. }
  160. }
  161. return z;
  162. }
  163. extern "C" inline __device__ float _Complex __divsc3(float __a, float __b,
  164. float __c, float __d) {
  165. int __ilogbw = 0;
  166. float __logbw = std::logb(max(std::abs(__c), std::abs(__d)));
  167. if (std::isfinite(__logbw)) {
  168. __ilogbw = (int)__logbw;
  169. __c = std::scalbn(__c, -__ilogbw);
  170. __d = std::scalbn(__d, -__ilogbw);
  171. }
  172. float __denom = __c * __c + __d * __d;
  173. float _Complex z;
  174. __real__(z) = std::scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
  175. __imag__(z) = std::scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
  176. if (std::isnan(__real__(z)) && std::isnan(__imag__(z))) {
  177. if ((__denom == 0) && (!std::isnan(__a) || !std::isnan(__b))) {
  178. __real__(z) = std::copysign(__builtin_huge_valf(), __c) * __a;
  179. __imag__(z) = std::copysign(__builtin_huge_valf(), __c) * __b;
  180. } else if ((std::isinf(__a) || std::isinf(__b)) && std::isfinite(__c) &&
  181. std::isfinite(__d)) {
  182. __a = std::copysign(std::isinf(__a) ? 1 : 0, __a);
  183. __b = std::copysign(std::isinf(__b) ? 1 : 0, __b);
  184. __real__(z) = __builtin_huge_valf() * (__a * __c + __b * __d);
  185. __imag__(z) = __builtin_huge_valf() * (__b * __c - __a * __d);
  186. } else if (std::isinf(__logbw) && __logbw > 0 && std::isfinite(__a) &&
  187. std::isfinite(__b)) {
  188. __c = std::copysign(std::isinf(__c) ? 1 : 0, __c);
  189. __d = std::copysign(std::isinf(__d) ? 1 : 0, __d);
  190. __real__(z) = 0 * (__a * __c + __b * __d);
  191. __imag__(z) = 0 * (__b * __c - __a * __d);
  192. }
  193. }
  194. return z;
  195. }
  196. #endif // __CLANG_CUDA_COMPLEX_BUILTINS