simde-complex.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* SPDX-License-Identifier: MIT
  2. *
  3. * Permission is hereby granted, free of charge, to any person
  4. * obtaining a copy of this software and associated documentation
  5. * files (the "Software"), to deal in the Software without
  6. * restriction, including without limitation the rights to use, copy,
  7. * modify, merge, publish, distribute, sublicense, and/or sell copies
  8. * of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Copyright:
  24. * 2020-2021 Evan Nemerson <evan@nemerson.com>
  25. */
  26. /* Support for complex math.
  27. *
  28. * We try to avoid including <complex> (in C++ mode) since it pulls in
  29. * a *lot* of code. Unfortunately this only works for GNU modes (i.e.,
  30. * -std=gnu++14 not -std=c++14) unless you pass -fext-numeric-literals,
  31. * but there is no way (AFAICT) to detect that flag so we have to rely
  32. * on __STRICT_ANSI__ to instead detect GNU mode.
  33. *
  34. * This header is separate from simde-math.h since there is a good
  35. * chance it will pull in <complex>, and most of the time we don't need
  36. * complex math (on x86 only SVML uses it). */
  37. #if !defined(SIMDE_COMPLEX_H)
  38. #define SIMDE_COMPLEX_H 1
  39. #include "simde-math.h"
  40. #if ( \
  41. HEDLEY_HAS_BUILTIN(__builtin_creal) || \
  42. HEDLEY_GCC_VERSION_CHECK(4,7,0) || \
  43. HEDLEY_INTEL_VERSION_CHECK(13,0,0) \
  44. ) && (!defined(__cplusplus) && !defined(__STRICT_ANSI__))
  45. HEDLEY_DIAGNOSTIC_PUSH
  46. SIMDE_DIAGNOSTIC_DISABLE_C99_EXTENSIONS_
  47. typedef __complex__ float simde_cfloat32;
  48. typedef __complex__ double simde_cfloat64;
  49. HEDLEY_DIAGNOSTIC_POP
  50. #define SIMDE_MATH_CMPLX(x, y) (HEDLEY_STATIC_CAST(double, x) + HEDLEY_STATIC_CAST(double, y) * (__extension__ 1.0j))
  51. #define SIMDE_MATH_CMPLXF(x, y) (HEDLEY_STATIC_CAST(float, x) + HEDLEY_STATIC_CAST(float, y) * (__extension__ 1.0fj))
  52. #if !defined(simde_math_creal)
  53. #define simde_math_crealf(z) __builtin_crealf(z)
  54. #endif
  55. #if !defined(simde_math_crealf)
  56. #define simde_math_creal(z) __builtin_creal(z)
  57. #endif
  58. #if !defined(simde_math_cimag)
  59. #define simde_math_cimagf(z) __builtin_cimagf(z)
  60. #endif
  61. #if !defined(simde_math_cimagf)
  62. #define simde_math_cimag(z) __builtin_cimag(z)
  63. #endif
  64. #if !defined(simde_math_cexp)
  65. #define simde_math_cexp(z) __builtin_cexp(z)
  66. #endif
  67. #if !defined(simde_math_cexpf)
  68. #define simde_math_cexpf(z) __builtin_cexpf(z)
  69. #endif
  70. #elif !defined(__cplusplus)
  71. #include <complex.h>
  72. #if !defined(HEDLEY_MSVC_VERSION)
  73. typedef float _Complex simde_cfloat32;
  74. typedef double _Complex simde_cfloat64;
  75. #else
  76. typedef _Fcomplex simde_cfloat32;
  77. typedef _Dcomplex simde_cfloat64;
  78. #endif
  79. #if defined(HEDLEY_MSVC_VERSION)
  80. #define SIMDE_MATH_CMPLX(x, y) ((simde_cfloat64) { (x), (y) })
  81. #define SIMDE_MATH_CMPLXF(x, y) ((simde_cfloat32) { (x), (y) })
  82. #elif defined(CMPLX) && defined(CMPLXF)
  83. #define SIMDE_MATH_CMPLX(x, y) CMPLX(x, y)
  84. #define SIMDE_MATH_CMPLXF(x, y) CMPLXF(x, y)
  85. #else
  86. #define SIMDE_MATH_CMPLX(x, y) (HEDLEY_STATIC_CAST(double, x) + HEDLEY_STATIC_CAST(double, y) * I)
  87. #define SIMDE_MATH_CMPLXF(x, y) (HEDLEY_STATIC_CAST(float, x) + HEDLEY_STATIC_CAST(float, y) * I)
  88. #endif
  89. #if !defined(simde_math_creal)
  90. #define simde_math_creal(z) creal(z)
  91. #endif
  92. #if !defined(simde_math_crealf)
  93. #define simde_math_crealf(z) crealf(z)
  94. #endif
  95. #if !defined(simde_math_cimag)
  96. #define simde_math_cimag(z) cimag(z)
  97. #endif
  98. #if !defined(simde_math_cimagf)
  99. #define simde_math_cimagf(z) cimagf(z)
  100. #endif
  101. #if !defined(simde_math_cexp)
  102. #define simde_math_cexp(z) cexp(z)
  103. #endif
  104. #if !defined(simde_math_cexpf)
  105. #define simde_math_cexpf(z) cexpf(z)
  106. #endif
  107. #else
  108. HEDLEY_DIAGNOSTIC_PUSH
  109. #if defined(HEDLEY_MSVC_VERSION)
  110. #pragma warning(disable:4530)
  111. #endif
  112. #include <complex>
  113. HEDLEY_DIAGNOSTIC_POP
  114. typedef std::complex<float> simde_cfloat32;
  115. typedef std::complex<double> simde_cfloat64;
  116. #define SIMDE_MATH_CMPLX(x, y) (std::complex<double>(x, y))
  117. #define SIMDE_MATH_CMPLXF(x, y) (std::complex<float>(x, y))
  118. #if !defined(simde_math_creal)
  119. #define simde_math_creal(z) ((z).real())
  120. #endif
  121. #if !defined(simde_math_crealf)
  122. #define simde_math_crealf(z) ((z).real())
  123. #endif
  124. #if !defined(simde_math_cimag)
  125. #define simde_math_cimag(z) ((z).imag())
  126. #endif
  127. #if !defined(simde_math_cimagf)
  128. #define simde_math_cimagf(z) ((z).imag())
  129. #endif
  130. #if !defined(simde_math_cexp)
  131. #define simde_math_cexp(z) std::exp(z)
  132. #endif
  133. #if !defined(simde_math_cexpf)
  134. #define simde_math_cexpf(z) std::exp(z)
  135. #endif
  136. #endif
  137. #endif /* !defined(SIMDE_COMPLEX_H) */