tgmath.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 2004-2015 Free Software Foundation, Inc.
  2. Contributed by Apple, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /*
  20. * ISO C Standard: 7.22 Type-generic math <tgmath.h>
  21. */
  22. #ifndef _TGMATH_H
  23. #define _TGMATH_H
  24. #include <math.h>
  25. #ifndef __cplusplus
  26. #include <complex.h>
  27. /* Naming convention: generic macros are defining using
  28. __TGMATH_CPLX*, __TGMATH_REAL*, and __TGMATH_CPLX_ONLY. _CPLX
  29. means the generic argument(s) may be real or complex, _REAL means
  30. real only, _CPLX means complex only. If there is no suffix, we are
  31. defining a function of one generic argument. If the suffix is _n
  32. it is a function of n generic arguments. If the suffix is _m_n it
  33. is a function of n arguments, the first m of which are generic. We
  34. only define these macros for values of n and/or m that are needed. */
  35. /* The general rules for generic macros are given in 7.22 paragraphs 1 and 2.
  36. If any generic parameter is complex, we use a complex version. Otherwise
  37. we use a real version. If the real part of any generic parameter is long
  38. double, we use the long double version. Otherwise if the real part of any
  39. generic parameter is double or of integer type, we use the double version.
  40. Otherwise we use the float version. */
  41. #define __tg_cplx(expr) \
  42. __builtin_classify_type(expr) == 9
  43. #define __tg_ldbl(expr) \
  44. __builtin_types_compatible_p(__typeof__(expr), long double)
  45. #define __tg_dbl(expr) \
  46. (__builtin_types_compatible_p(__typeof__(expr), double) \
  47. || __builtin_classify_type(expr) == 1)
  48. #define __tg_choose(x,f,d,l) \
  49. __builtin_choose_expr(__tg_ldbl(x), l, \
  50. __builtin_choose_expr(__tg_dbl(x), d, \
  51. f))
  52. #define __tg_choose_2(x,y,f,d,l) \
  53. __builtin_choose_expr(__tg_ldbl(x) || __tg_ldbl(y), l, \
  54. __builtin_choose_expr(__tg_dbl(x) || __tg_dbl(y), d, \
  55. f))
  56. #define __tg_choose_3(x,y,z,f,d,l) \
  57. __builtin_choose_expr(__tg_ldbl(x) || __tg_ldbl(y) || __tg_ldbl(z), l, \
  58. __builtin_choose_expr(__tg_dbl(x) || __tg_dbl(y) \
  59. || __tg_dbl(z), d, \
  60. f))
  61. #define __TGMATH_CPLX(z,R,C) \
  62. __builtin_choose_expr (__tg_cplx(z), \
  63. __tg_choose (__real__(z), C##f(z), (C)(z), C##l(z)), \
  64. __tg_choose (z, R##f(z), (R)(z), R##l(z)))
  65. #define __TGMATH_CPLX_2(z1,z2,R,C) \
  66. __builtin_choose_expr (__tg_cplx(z1) || __tg_cplx(z2), \
  67. __tg_choose_2 (__real__(z1), __real__(z2), \
  68. C##f(z1,z2), (C)(z1,z2), C##l(z1,z2)), \
  69. __tg_choose_2 (z1, z2, \
  70. R##f(z1,z2), (R)(z1,z2), R##l(z1,z2)))
  71. #define __TGMATH_REAL(x,R) \
  72. __tg_choose (x, R##f(x), (R)(x), R##l(x))
  73. #define __TGMATH_REAL_2(x,y,R) \
  74. __tg_choose_2 (x, y, R##f(x,y), (R)(x,y), R##l(x,y))
  75. #define __TGMATH_REAL_3(x,y,z,R) \
  76. __tg_choose_3 (x, y, z, R##f(x,y,z), (R)(x,y,z), R##l(x,y,z))
  77. #define __TGMATH_REAL_1_2(x,y,R) \
  78. __tg_choose (x, R##f(x,y), (R)(x,y), R##l(x,y))
  79. #define __TGMATH_REAL_2_3(x,y,z,R) \
  80. __tg_choose_2 (x, y, R##f(x,y,z), (R)(x,y,z), R##l(x,y,z))
  81. #define __TGMATH_CPLX_ONLY(z,C) \
  82. __tg_choose (__real__(z), C##f(z), (C)(z), C##l(z))
  83. /* Functions defined in both <math.h> and <complex.h> (7.22p4) */
  84. #define acos(z) __TGMATH_CPLX(z, acos, cacos)
  85. #define asin(z) __TGMATH_CPLX(z, asin, casin)
  86. #define atan(z) __TGMATH_CPLX(z, atan, catan)
  87. #define acosh(z) __TGMATH_CPLX(z, acosh, cacosh)
  88. #define asinh(z) __TGMATH_CPLX(z, asinh, casinh)
  89. #define atanh(z) __TGMATH_CPLX(z, atanh, catanh)
  90. #define cos(z) __TGMATH_CPLX(z, cos, ccos)
  91. #define sin(z) __TGMATH_CPLX(z, sin, csin)
  92. #define tan(z) __TGMATH_CPLX(z, tan, ctan)
  93. #define cosh(z) __TGMATH_CPLX(z, cosh, ccosh)
  94. #define sinh(z) __TGMATH_CPLX(z, sinh, csinh)
  95. #define tanh(z) __TGMATH_CPLX(z, tanh, ctanh)
  96. #define exp(z) __TGMATH_CPLX(z, exp, cexp)
  97. #define log(z) __TGMATH_CPLX(z, log, clog)
  98. #define pow(z1,z2) __TGMATH_CPLX_2(z1, z2, pow, cpow)
  99. #define sqrt(z) __TGMATH_CPLX(z, sqrt, csqrt)
  100. #define fabs(z) __TGMATH_CPLX(z, fabs, cabs)
  101. /* Functions defined in <math.h> only (7.22p5) */
  102. #define atan2(x,y) __TGMATH_REAL_2(x, y, atan2)
  103. #define cbrt(x) __TGMATH_REAL(x, cbrt)
  104. #define ceil(x) __TGMATH_REAL(x, ceil)
  105. #define copysign(x,y) __TGMATH_REAL_2(x, y, copysign)
  106. #define erf(x) __TGMATH_REAL(x, erf)
  107. #define erfc(x) __TGMATH_REAL(x, erfc)
  108. #define exp2(x) __TGMATH_REAL(x, exp2)
  109. #define expm1(x) __TGMATH_REAL(x, expm1)
  110. #define fdim(x,y) __TGMATH_REAL_2(x, y, fdim)
  111. #define floor(x) __TGMATH_REAL(x, floor)
  112. #define fma(x,y,z) __TGMATH_REAL_3(x, y, z, fma)
  113. #define fmax(x,y) __TGMATH_REAL_2(x, y, fmax)
  114. #define fmin(x,y) __TGMATH_REAL_2(x, y, fmin)
  115. #define fmod(x,y) __TGMATH_REAL_2(x, y, fmod)
  116. #define frexp(x,y) __TGMATH_REAL_1_2(x, y, frexp)
  117. #define hypot(x,y) __TGMATH_REAL_2(x, y, hypot)
  118. #define ilogb(x) __TGMATH_REAL(x, ilogb)
  119. #define ldexp(x,y) __TGMATH_REAL_1_2(x, y, ldexp)
  120. #define lgamma(x) __TGMATH_REAL(x, lgamma)
  121. #define llrint(x) __TGMATH_REAL(x, llrint)
  122. #define llround(x) __TGMATH_REAL(x, llround)
  123. #define log10(x) __TGMATH_REAL(x, log10)
  124. #define log1p(x) __TGMATH_REAL(x, log1p)
  125. #define log2(x) __TGMATH_REAL(x, log2)
  126. #define logb(x) __TGMATH_REAL(x, logb)
  127. #define lrint(x) __TGMATH_REAL(x, lrint)
  128. #define lround(x) __TGMATH_REAL(x, lround)
  129. #define nearbyint(x) __TGMATH_REAL(x, nearbyint)
  130. #define nextafter(x,y) __TGMATH_REAL_2(x, y, nextafter)
  131. #define nexttoward(x,y) __TGMATH_REAL_1_2(x, y, nexttoward)
  132. #define remainder(x,y) __TGMATH_REAL_2(x, y, remainder)
  133. #define remquo(x,y,z) __TGMATH_REAL_2_3(x, y, z, remquo)
  134. #define rint(x) __TGMATH_REAL(x, rint)
  135. #define round(x) __TGMATH_REAL(x, round)
  136. #define scalbn(x,y) __TGMATH_REAL_1_2(x, y, scalbn)
  137. #define scalbln(x,y) __TGMATH_REAL_1_2(x, y, scalbln)
  138. #define tgamma(x) __TGMATH_REAL(x, tgamma)
  139. #define trunc(x) __TGMATH_REAL(x, trunc)
  140. /* Functions defined in <complex.h> only (7.22p6) */
  141. #define carg(z) __TGMATH_CPLX_ONLY(z, carg)
  142. #define cimag(z) __TGMATH_CPLX_ONLY(z, cimag)
  143. #define conj(z) __TGMATH_CPLX_ONLY(z, conj)
  144. #define cproj(z) __TGMATH_CPLX_ONLY(z, cproj)
  145. #define creal(z) __TGMATH_CPLX_ONLY(z, creal)
  146. #endif /* __cplusplus */
  147. #endif /* _TGMATH_H */