openlibm_complex.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* $OpenBSD: complex.h,v 1.5 2014/03/16 18:38:30 guenther Exp $ */
  2. /*
  3. * Copyright (c) 2008 Martynas Venckus <martynas@openbsd.org>
  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. #ifndef OPENLIBM_COMPLEX_H
  18. #define OPENLIBM_COMPLEX_H
  19. #define complex _Complex
  20. #define _Complex_I 1.0fi
  21. #define I _Complex_I
  22. /*
  23. * Macros that can be used to construct complex values.
  24. *
  25. * The C99 standard intends x+I*y to be used for this, but x+I*y is
  26. * currently unusable in general since gcc introduces many overflow,
  27. * underflow, sign and efficiency bugs by rewriting I*y as
  28. * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
  29. * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
  30. * to -0.0+I*0.0.
  31. *
  32. * In C11, a CMPLX(x,y) macro was added to circumvent this limitation,
  33. * and gcc 4.7 added a __builtin_complex feature to simplify implementation
  34. * of CMPLX in libc, so we can take advantage of these features if they
  35. * are available. Clang simply allows complex values to be constructed
  36. * using a compound literal.
  37. *
  38. * If __builtin_complex is not available, resort to using inline
  39. * functions instead. These can unfortunately not be used to construct
  40. * compile-time constants.
  41. *
  42. * C99 specifies that complex numbers have the same representation as
  43. * an array of two elements, where the first element is the real part
  44. * and the second element is the imaginary part.
  45. */
  46. #ifdef __clang__
  47. # define CMPLXF(x, y) ((float complex){x, y})
  48. # define CMPLX(x, y) ((double complex){x, y})
  49. # define CMPLXL(x, y) ((long double complex){x, y})
  50. #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__INTEL_COMPILER)
  51. # define CMPLXF(x,y) __builtin_complex ((float) (x), (float) (y))
  52. # define CMPLX(x,y) __builtin_complex ((double) (x), (double) (y))
  53. # define CMPLXL(x,y) __builtin_complex ((long double) (x), (long double) (y))
  54. #else
  55. static inline float complex
  56. CMPLXF(float x, float y)
  57. {
  58. union {
  59. float a[2];
  60. float complex f;
  61. } z = {{ x, y }};
  62. return (z.f);
  63. }
  64. static inline double complex
  65. CMPLX(double x, double y)
  66. {
  67. union {
  68. double a[2];
  69. double complex f;
  70. } z = {{ x, y }};
  71. return (z.f);
  72. }
  73. static inline long double complex
  74. CMPLXL(long double x, long double y)
  75. {
  76. union {
  77. long double a[2];
  78. long double complex f;
  79. } z = {{ x, y }};
  80. return (z.f);
  81. }
  82. #endif
  83. /*
  84. * Double versions of C99 functions
  85. */
  86. double complex cacos(double complex);
  87. double complex casin(double complex);
  88. double complex catan(double complex);
  89. double complex ccos(double complex);
  90. double complex csin(double complex);
  91. double complex ctan(double complex);
  92. double complex cacosh(double complex);
  93. double complex casinh(double complex);
  94. double complex catanh(double complex);
  95. double complex ccosh(double complex);
  96. double complex csinh(double complex);
  97. double complex ctanh(double complex);
  98. double complex cexp(double complex);
  99. double complex clog(double complex);
  100. double cabs(double complex);
  101. double complex cpow(double complex, double complex);
  102. double complex csqrt(double complex);
  103. double carg(double complex);
  104. double cimag(double complex);
  105. double complex conj(double complex);
  106. double complex cproj(double complex);
  107. double creal(double complex);
  108. /*
  109. * Float versions of C99 functions
  110. */
  111. float complex cacosf(float complex);
  112. float complex casinf(float complex);
  113. float complex catanf(float complex);
  114. float complex ccosf(float complex);
  115. float complex csinf(float complex);
  116. float complex ctanf(float complex);
  117. float complex cacoshf(float complex);
  118. float complex casinhf(float complex);
  119. float complex catanhf(float complex);
  120. float complex ccoshf(float complex);
  121. float complex csinhf(float complex);
  122. float complex ctanhf(float complex);
  123. float complex cexpf(float complex);
  124. float complex clogf(float complex);
  125. float cabsf(float complex);
  126. float complex cpowf(float complex, float complex);
  127. float complex csqrtf(float complex);
  128. float cargf(float complex);
  129. float cimagf(float complex);
  130. float complex conjf(float complex);
  131. float complex cprojf(float complex);
  132. float crealf(float complex);
  133. /*
  134. * Long double versions of C99 functions
  135. */
  136. long double complex cacosl(long double complex);
  137. long double complex casinl(long double complex);
  138. long double complex catanl(long double complex);
  139. long double complex ccosl(long double complex);
  140. long double complex csinl(long double complex);
  141. long double complex ctanl(long double complex);
  142. long double complex cacoshl(long double complex);
  143. long double complex casinhl(long double complex);
  144. long double complex catanhl(long double complex);
  145. long double complex ccoshl(long double complex);
  146. long double complex csinhl(long double complex);
  147. long double complex ctanhl(long double complex);
  148. long double complex cexpl(long double complex);
  149. long double complex clogl(long double complex);
  150. long double cabsl(long double complex);
  151. long double complex cpowl(long double complex,
  152. long double complex);
  153. long double complex csqrtl(long double complex);
  154. long double cargl(long double complex);
  155. long double cimagl(long double complex);
  156. long double complex conjl(long double complex);
  157. long double complex cprojl(long double complex);
  158. long double creall(long double complex);
  159. #endif /* !OPENLIBM_COMPLEX_H */