tgmath.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #ifndef _TGMATH_H
  2. #define _TGMATH_H
  3. /*
  4. the return types are only correct with gcc (__GNUC__)
  5. otherwise they are long double or long double complex
  6. the long double version of a function is never chosen when
  7. sizeof(double) == sizeof(long double)
  8. (but the return type is set correctly with gcc)
  9. */
  10. #include <math.h>
  11. #include <complex.h>
  12. #define __IS_FP(x) !!((1?1:(x))/2)
  13. #define __IS_CX(x) (__IS_FP(x) && sizeof(x) == sizeof((x)+I))
  14. #define __IS_REAL(x) (__IS_FP(x) && 2*sizeof(x) == sizeof((x)+I))
  15. #define __FLT(x) (__IS_REAL(x) && sizeof(x) == sizeof(float))
  16. #define __LDBL(x) (__IS_REAL(x) && sizeof(x) == sizeof(long double) && sizeof(long double) != sizeof(double))
  17. #define __FLTCX(x) (__IS_CX(x) && sizeof(x) == sizeof(float complex))
  18. #define __DBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(double complex))
  19. #define __LDBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(long double complex) && sizeof(long double) != sizeof(double))
  20. /* return type */
  21. #ifdef __GNUC__
  22. /*
  23. the conditional expression (that selects the right function
  24. for evaluation) should be casted to the return type of the
  25. selected function (otherwise the result type is determined
  26. by the conversion rules applied to all the function return
  27. types so it is long double or long double _Complex)
  28. this cannot be done in c99 (c11 has _Generic that would help
  29. but is not yet widely supported), so the typeof extension of
  30. gcc is used and that ?: has special semantics when one of
  31. the operands is a null pointer constant
  32. unfortunately the standard is overly strict about the
  33. definition of null pointer constants (current gcc does not
  34. follow the standard but clang does) so we have to use a hack
  35. to be able to tell integer and floating-point types apart:
  36. both gcc and clang evaluate sizeof(void) to 1 even though it
  37. is a constraint violation, we only use this on clang for now
  38. */
  39. /* predicates that evaluate to integer constant expressions */
  40. #ifdef __clang__
  41. /* TODO: __c_IS_FP(1.0) is a constraint violation */
  42. #define __c_IS_FP(x) (!(sizeof*(__typeof__(0?(int*)0:(void*)__IS_FP(x)))0-1))
  43. #else
  44. #define __c_IS_FP(x) __IS_FP(x)
  45. #endif
  46. #define __c_IS_CX(x) (__c_IS_FP(x) && sizeof(x) == sizeof((x)+I))
  47. #define __c_FLTCX(x) (__c_IS_CX(x) && sizeof(x) == sizeof(float complex))
  48. #define __c_DBLCX(x) (__c_IS_CX(x) && sizeof(x) == sizeof(double complex))
  49. #define __c_LDBLCX(x) (__c_IS_CX(x) && sizeof(x) == sizeof(long double complex) && sizeof(long double) != sizeof(double))
  50. /* if c then t else void */
  51. #define __type1(c,t) __typeof__(*(0?(t*)0:(void*)!(c)))
  52. /* if c then t1 else t2 */
  53. #define __type2(c,t1,t2) __typeof__(*(0?(__type1(c,t1)*)0:(__type1(!(c),t2)*)0))
  54. /* cast to double when x is integral, otherwise use typeof(x) */
  55. #define __RETCAST(x) ( \
  56. __type2(__c_IS_FP(x), __typeof__(x), double))
  57. /* 2 args case, should work for complex types (cpow) */
  58. #define __RETCAST_2(x, y) ( \
  59. __type2(__c_IS_FP(x) && __c_IS_FP(y), \
  60. __typeof__((x)+(y)), \
  61. __typeof__((x)+(y)+1.0)))
  62. /* 3 args case (fma only) */
  63. #define __RETCAST_3(x, y, z) ( \
  64. __type2(__c_IS_FP(x) && __c_IS_FP(y) && __c_IS_FP(z), \
  65. __typeof__((x)+(y)+(z)), \
  66. __typeof__((x)+(y)+(z)+1.0)))
  67. /* drop complex from the type of x */
  68. /* TODO: wrong when sizeof(long double)==sizeof(double) */
  69. #define __RETCAST_REAL(x) ( \
  70. __type2(sizeof(__RETCAST(x)0+I)==sizeof(float complex), float, \
  71. __type2(sizeof(__RETCAST(x)0+I)==sizeof(double complex), double, \
  72. long double)))
  73. /* add complex to the type of x */
  74. #define __RETCAST_CX(x) (__typeof__(__RETCAST(x)0+I))
  75. #else
  76. #define __RETCAST(x)
  77. #define __RETCAST_2(x, y)
  78. #define __RETCAST_3(x, y, z)
  79. #define __RETCAST_REAL(x)
  80. #define __RETCAST_CX(x)
  81. #endif
  82. /* function selection */
  83. #define __tg_real_nocast(fun, x) ( \
  84. __FLT(x) ? fun ## f (x) : \
  85. __LDBL(x) ? fun ## l (x) : \
  86. fun(x) )
  87. #define __tg_real(fun, x) (__RETCAST(x)__tg_real_nocast(fun, x))
  88. #define __tg_real_2_1(fun, x, y) (__RETCAST(x)( \
  89. __FLT(x) ? fun ## f (x, y) : \
  90. __LDBL(x) ? fun ## l (x, y) : \
  91. fun(x, y) ))
  92. #define __tg_real_2(fun, x, y) (__RETCAST_2(x, y)( \
  93. __FLT(x) && __FLT(y) ? fun ## f (x, y) : \
  94. __LDBL((x)+(y)) ? fun ## l (x, y) : \
  95. fun(x, y) ))
  96. #define __tg_complex(fun, x) (__RETCAST_CX(x)( \
  97. __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \
  98. __LDBLCX((x)+I) ? fun ## l (x) : \
  99. fun(x) ))
  100. #define __tg_complex_retreal(fun, x) (__RETCAST_REAL(x)( \
  101. __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \
  102. __LDBLCX((x)+I) ? fun ## l (x) : \
  103. fun(x) ))
  104. #define __tg_real_complex(fun, x) (__RETCAST(x)( \
  105. __FLTCX(x) ? c ## fun ## f (x) : \
  106. __DBLCX(x) ? c ## fun (x) : \
  107. __LDBLCX(x) ? c ## fun ## l (x) : \
  108. __FLT(x) ? fun ## f (x) : \
  109. __LDBL(x) ? fun ## l (x) : \
  110. fun(x) ))
  111. /* special cases */
  112. #define __tg_real_remquo(x, y, z) (__RETCAST_2(x, y)( \
  113. __FLT(x) && __FLT(y) ? remquof(x, y, z) : \
  114. __LDBL((x)+(y)) ? remquol(x, y, z) : \
  115. remquo(x, y, z) ))
  116. #define __tg_real_fma(x, y, z) (__RETCAST_3(x, y, z)( \
  117. __FLT(x) && __FLT(y) && __FLT(z) ? fmaf(x, y, z) : \
  118. __LDBL((x)+(y)+(z)) ? fmal(x, y, z) : \
  119. fma(x, y, z) ))
  120. #define __tg_real_complex_pow(x, y) (__RETCAST_2(x, y)( \
  121. __FLTCX((x)+(y)) && __IS_FP(x) && __IS_FP(y) ? cpowf(x, y) : \
  122. __FLTCX((x)+(y)) ? cpow(x, y) : \
  123. __DBLCX((x)+(y)) ? cpow(x, y) : \
  124. __LDBLCX((x)+(y)) ? cpowl(x, y) : \
  125. __FLT(x) && __FLT(y) ? powf(x, y) : \
  126. __LDBL((x)+(y)) ? powl(x, y) : \
  127. pow(x, y) ))
  128. #define __tg_real_complex_fabs(x) (__RETCAST_REAL(x)( \
  129. __FLTCX(x) ? cabsf(x) : \
  130. __DBLCX(x) ? cabs(x) : \
  131. __LDBLCX(x) ? cabsl(x) : \
  132. __FLT(x) ? fabsf(x) : \
  133. __LDBL(x) ? fabsl(x) : \
  134. fabs(x) ))
  135. /* suppress any macros in math.h or complex.h */
  136. #undef acos
  137. #undef acosh
  138. #undef asin
  139. #undef asinh
  140. #undef atan
  141. #undef atan2
  142. #undef atanh
  143. #undef carg
  144. #undef cbrt
  145. #undef ceil
  146. #undef cimag
  147. #undef conj
  148. #undef copysign
  149. #undef cos
  150. #undef cosh
  151. #undef cproj
  152. #undef creal
  153. #undef erf
  154. #undef erfc
  155. #undef exp
  156. #undef exp2
  157. #undef expm1
  158. #undef fabs
  159. #undef fdim
  160. #undef floor
  161. #undef fma
  162. #undef fmax
  163. #undef fmin
  164. #undef fmod
  165. #undef frexp
  166. #undef hypot
  167. #undef ilogb
  168. #undef ldexp
  169. #undef lgamma
  170. #undef llrint
  171. #undef llround
  172. #undef log
  173. #undef log10
  174. #undef log1p
  175. #undef log2
  176. #undef logb
  177. #undef lrint
  178. #undef lround
  179. #undef nearbyint
  180. #undef nextafter
  181. #undef nexttoward
  182. #undef pow
  183. #undef remainder
  184. #undef remquo
  185. #undef rint
  186. #undef round
  187. #undef scalbln
  188. #undef scalbn
  189. #undef sin
  190. #undef sinh
  191. #undef sqrt
  192. #undef tan
  193. #undef tanh
  194. #undef tgamma
  195. #undef trunc
  196. /* tg functions */
  197. #define acos(x) __tg_real_complex(acos, (x))
  198. #define acosh(x) __tg_real_complex(acosh, (x))
  199. #define asin(x) __tg_real_complex(asin, (x))
  200. #define asinh(x) __tg_real_complex(asinh, (x))
  201. #define atan(x) __tg_real_complex(atan, (x))
  202. #define atan2(x,y) __tg_real_2(atan2, (x), (y))
  203. #define atanh(x) __tg_real_complex(atanh, (x))
  204. #define carg(x) __tg_complex_retreal(carg, (x))
  205. #define cbrt(x) __tg_real(cbrt, (x))
  206. #define ceil(x) __tg_real(ceil, (x))
  207. #define cimag(x) __tg_complex_retreal(cimag, (x))
  208. #define conj(x) __tg_complex(conj, (x))
  209. #define copysign(x,y) __tg_real_2(copysign, (x), (y))
  210. #define cos(x) __tg_real_complex(cos, (x))
  211. #define cosh(x) __tg_real_complex(cosh, (x))
  212. #define cproj(x) __tg_complex(cproj, (x))
  213. #define creal(x) __tg_complex_retreal(creal, (x))
  214. #define erf(x) __tg_real(erf, (x))
  215. #define erfc(x) __tg_real(erfc, (x))
  216. #define exp(x) __tg_real_complex(exp, (x))
  217. #define exp2(x) __tg_real(exp2, (x))
  218. #define expm1(x) __tg_real(expm1, (x))
  219. #define fabs(x) __tg_real_complex_fabs(x)
  220. #define fdim(x,y) __tg_real_2(fdim, (x), (y))
  221. #define floor(x) __tg_real(floor, (x))
  222. #define fma(x,y,z) __tg_real_fma((x), (y), (z))
  223. #define fmax(x,y) __tg_real_2(fmax, (x), (y))
  224. #define fmin(x,y) __tg_real_2(fmin, (x), (y))
  225. #define fmod(x,y) __tg_real_2(fmod, (x), (y))
  226. #define frexp(x,y) __tg_real_2_1(frexp, (x), (y))
  227. #define hypot(x,y) __tg_real_2(hypot, (x), (y))
  228. #define ilogb(x) __tg_real_nocast(ilogb, (x))
  229. #define ldexp(x,y) __tg_real_2_1(ldexp, (x), (y))
  230. #define lgamma(x) __tg_real(lgamma, (x))
  231. #define llrint(x) __tg_real_nocast(llrint, (x))
  232. #define llround(x) __tg_real_nocast(llround, (x))
  233. #define log(x) __tg_real_complex(log, (x))
  234. #define log10(x) __tg_real(log10, (x))
  235. #define log1p(x) __tg_real(log1p, (x))
  236. #define log2(x) __tg_real(log2, (x))
  237. #define logb(x) __tg_real(logb, (x))
  238. #define lrint(x) __tg_real_nocast(lrint, (x))
  239. #define lround(x) __tg_real_nocast(lround, (x))
  240. #define nearbyint(x) __tg_real(nearbyint, (x))
  241. #define nextafter(x,y) __tg_real_2(nextafter, (x), (y))
  242. #define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y))
  243. #define pow(x,y) __tg_real_complex_pow((x), (y))
  244. #define remainder(x,y) __tg_real_2(remainder, (x), (y))
  245. #define remquo(x,y,z) __tg_real_remquo((x), (y), (z))
  246. #define rint(x) __tg_real(rint, (x))
  247. #define round(x) __tg_real(round, (x))
  248. #define scalbln(x,y) __tg_real_2_1(scalbln, (x), (y))
  249. #define scalbn(x,y) __tg_real_2_1(scalbn, (x), (y))
  250. #define sin(x) __tg_real_complex(sin, (x))
  251. #define sinh(x) __tg_real_complex(sinh, (x))
  252. #define sqrt(x) __tg_real_complex(sqrt, (x))
  253. #define tan(x) __tg_real_complex(tan, (x))
  254. #define tanh(x) __tg_real_complex(tanh, (x))
  255. #define tgamma(x) __tg_real(tgamma, (x))
  256. #define trunc(x) __tg_real(trunc, (x))
  257. #endif