openlibm_complex.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* $OpenBSD: complex.h,v 1.5 2014/03/16 18:38:30 guenther Exp $ */
  2. /*
  3. * Copyright (c) 2008 Martynas Venckus <[email protected]>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifdef OPENLIBM_USE_HOST_COMPLEX_H
  18. #include <complex.h>
  19. #else /* !OPENLIBM_USE_HOST_COMPLEX_H */
  20. #ifndef OPENLIBM_COMPLEX_H
  21. #define OPENLIBM_COMPLEX_H
  22. #define complex _Complex
  23. #define _Complex_I 1.0fi
  24. #define I _Complex_I
  25. /*
  26. * Macros that can be used to construct complex values.
  27. *
  28. * The C99 standard intends x+I*y to be used for this, but x+I*y is
  29. * currently unusable in general since gcc introduces many overflow,
  30. * underflow, sign and efficiency bugs by rewriting I*y as
  31. * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
  32. * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
  33. * to -0.0+I*0.0.
  34. *
  35. * In C11, a CMPLX(x,y) macro was added to circumvent this limitation,
  36. * and gcc 4.7 added a __builtin_complex feature to simplify implementation
  37. * of CMPLX in libc, so we can take advantage of these features if they
  38. * are available. Clang simply allows complex values to be constructed
  39. * using a compound literal.
  40. *
  41. * If __builtin_complex is not available, resort to using inline
  42. * functions instead. These can unfortunately not be used to construct
  43. * compile-time constants.
  44. *
  45. * C99 specifies that complex numbers have the same representation as
  46. * an array of two elements, where the first element is the real part
  47. * and the second element is the imaginary part.
  48. */
  49. #ifdef __clang__
  50. # define CMPLXF(x, y) ((float complex){x, y})
  51. # define CMPLX(x, y) ((double complex){x, y})
  52. # define CMPLXL(x, y) ((long double complex){x, y})
  53. #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__INTEL_COMPILER)
  54. # define CMPLXF(x,y) __builtin_complex ((float) (x), (float) (y))
  55. # define CMPLX(x,y) __builtin_complex ((double) (x), (double) (y))
  56. # define CMPLXL(x,y) __builtin_complex ((long double) (x), (long double) (y))
  57. #else
  58. static inline float complex
  59. CMPLXF(float x, float y)
  60. {
  61. union {
  62. float a[2];
  63. float complex f;
  64. } z = {{ x, y }};
  65. return (z.f);
  66. }
  67. static inline double complex
  68. CMPLX(double x, double y)
  69. {
  70. union {
  71. double a[2];
  72. double complex f;
  73. } z = {{ x, y }};
  74. return (z.f);
  75. }
  76. static inline long double complex
  77. CMPLXL(long double x, long double y)
  78. {
  79. union {
  80. long double a[2];
  81. long double complex f;
  82. } z = {{ x, y }};
  83. return (z.f);
  84. }
  85. #endif
  86. /*
  87. * Double versions of C99 functions
  88. */
  89. double complex cacos(double complex);
  90. double complex casin(double complex);
  91. double complex catan(double complex);
  92. double complex ccos(double complex);
  93. double complex csin(double complex);
  94. double complex ctan(double complex);
  95. double complex cacosh(double complex);
  96. double complex casinh(double complex);
  97. double complex catanh(double complex);
  98. double complex ccosh(double complex);
  99. double complex csinh(double complex);
  100. double complex ctanh(double complex);
  101. double complex cexp(double complex);
  102. double complex clog(double complex);
  103. double cabs(double complex);
  104. double complex cpow(double complex, double complex);
  105. double complex csqrt(double complex);
  106. double carg(double complex);
  107. double cimag(double complex);
  108. double complex conj(double complex);
  109. double complex cproj(double complex);
  110. double creal(double complex);
  111. /*
  112. * Float versions of C99 functions
  113. */
  114. float complex cacosf(float complex);
  115. float complex casinf(float complex);
  116. float complex catanf(float complex);
  117. float complex ccosf(float complex);
  118. float complex csinf(float complex);
  119. float complex ctanf(float complex);
  120. float complex cacoshf(float complex);
  121. float complex casinhf(float complex);
  122. float complex catanhf(float complex);
  123. float complex ccoshf(float complex);
  124. float complex csinhf(float complex);
  125. float complex ctanhf(float complex);
  126. float complex cexpf(float complex);
  127. float complex clogf(float complex);
  128. float cabsf(float complex);
  129. float complex cpowf(float complex, float complex);
  130. float complex csqrtf(float complex);
  131. float cargf(float complex);
  132. float cimagf(float complex);
  133. float complex conjf(float complex);
  134. float complex cprojf(float complex);
  135. float crealf(float complex);
  136. /*
  137. * Long double versions of C99 functions
  138. */
  139. long double complex cacosl(long double complex);
  140. long double complex casinl(long double complex);
  141. long double complex catanl(long double complex);
  142. long double complex ccosl(long double complex);
  143. long double complex csinl(long double complex);
  144. long double complex ctanl(long double complex);
  145. long double complex cacoshl(long double complex);
  146. long double complex casinhl(long double complex);
  147. long double complex catanhl(long double complex);
  148. long double complex ccoshl(long double complex);
  149. long double complex csinhl(long double complex);
  150. long double complex ctanhl(long double complex);
  151. long double complex cexpl(long double complex);
  152. long double complex clogl(long double complex);
  153. long double cabsl(long double complex);
  154. long double complex cpowl(long double complex,
  155. long double complex);
  156. long double complex csqrtl(long double complex);
  157. long double cargl(long double complex);
  158. long double cimagl(long double complex);
  159. long double complex conjl(long double complex);
  160. long double complex cprojl(long double complex);
  161. long double creall(long double complex);
  162. #endif /* !OPENLIBM_COMPLEX_H */
  163. #endif /* OPENLIBM_USE_HOST_COMPLEX_H */