tgmath.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /* cast to double when x is integral, otherwise use typeof(x) */
  23. #define __RETCAST(x) (__typeof__(*( \
  24. 0 ? (__typeof__(0 ? (double *)0 : (void *)__IS_FP(x)))0 : \
  25. (__typeof__(0 ? (__typeof__(x) *)0 : (void *)!__IS_FP(x)))0 )))
  26. /* 2 args case, consider complex types (for cpow) */
  27. #define __RETCAST_2(x, y) (__typeof__(*( \
  28. 0 ? (__typeof__(0 ? (double *)0 : \
  29. (void *)!((!__IS_FP(x) || !__IS_FP(y)) && __FLT((x)+(y)+1.0f))))0 : \
  30. 0 ? (__typeof__(0 ? (double complex *)0 : \
  31. (void *)!((!__IS_FP(x) || !__IS_FP(y)) && __FLTCX((x)+(y)))))0 : \
  32. (__typeof__(0 ? (__typeof__((x)+(y)) *)0 : \
  33. (void *)((!__IS_FP(x) || !__IS_FP(y)) && (__FLT((x)+(y)+1.0f) || __FLTCX((x)+(y))))))0 )))
  34. /* 3 args case, don't consider complex types (fma only) */
  35. #define __RETCAST_3(x, y, z) (__typeof__(*( \
  36. 0 ? (__typeof__(0 ? (double *)0 : \
  37. (void *)!((!__IS_FP(x) || !__IS_FP(y) || !__IS_FP(z)) && __FLT((x)+(y)+(z)+1.0f))))0 : \
  38. (__typeof__(0 ? (__typeof__((x)+(y)) *)0 : \
  39. (void *)((!__IS_FP(x) || !__IS_FP(y) || !__IS_FP(z)) && __FLT((x)+(y)+(z)+1.0f))))0 )))
  40. /* drop complex from the type of x */
  41. #define __TO_REAL(x) *( \
  42. 0 ? (__typeof__(0 ? (double *)0 : (void *)!__DBLCX(x)))0 : \
  43. 0 ? (__typeof__(0 ? (float *)0 : (void *)!__FLTCX(x)))0 : \
  44. 0 ? (__typeof__(0 ? (long double *)0 : (void *)!__LDBLCX(x)))0 : \
  45. (__typeof__(0 ? (__typeof__(x) *)0 : (void *)__IS_CX(x)))0 )
  46. #else
  47. #define __RETCAST(x)
  48. #define __RETCAST_2(x, y)
  49. #define __RETCAST_3(x, y, z)
  50. #endif
  51. /* function selection */
  52. #define __tg_real(fun, x) (__RETCAST(x)( \
  53. __FLT(x) ? fun ## f (x) : \
  54. __LDBL(x) ? fun ## l (x) : \
  55. fun(x) ))
  56. #define __tg_real_2_1(fun, x, y) (__RETCAST(x)( \
  57. __FLT(x) ? fun ## f (x, y) : \
  58. __LDBL(x) ? fun ## l (x, y) : \
  59. fun(x, y) ))
  60. #define __tg_real_2(fun, x, y) (__RETCAST_2(x, y)( \
  61. __FLT(x) && __FLT(y) ? fun ## f (x, y) : \
  62. __LDBL((x)+(y)) ? fun ## l (x, y) : \
  63. fun(x, y) ))
  64. #define __tg_complex(fun, x) (__RETCAST((x)+I)( \
  65. __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \
  66. __LDBLCX((x)+I) ? fun ## l (x) : \
  67. fun(x) ))
  68. #define __tg_complex_retreal(fun, x) (__RETCAST(__TO_REAL(x))( \
  69. __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \
  70. __LDBLCX((x)+I) ? fun ## l (x) : \
  71. fun(x) ))
  72. #define __tg_real_complex(fun, x) (__RETCAST(x)( \
  73. __FLTCX(x) ? c ## fun ## f (x) : \
  74. __DBLCX(x) ? c ## fun (x) : \
  75. __LDBLCX(x) ? c ## fun ## l (x) : \
  76. __FLT(x) ? fun ## f (x) : \
  77. __LDBL(x) ? fun ## l (x) : \
  78. fun(x) ))
  79. /* special cases */
  80. #define __tg_real_remquo(x, y, z) (__RETCAST_2(x, y)( \
  81. __FLT(x) && __FLT(y) ? remquof(x, y, z) : \
  82. __LDBL((x)+(y)) ? remquol(x, y, z) : \
  83. remquo(x, y, z) ))
  84. #define __tg_real_fma(x, y, z) (__RETCAST_3(x, y, z)( \
  85. __FLT(x) && __FLT(y) && __FLT(z) ? fmaf(x, y, z) : \
  86. __LDBL((x)+(y)+(z)) ? fmal(x, y, z) : \
  87. fma(x, y, z) ))
  88. #define __tg_real_complex_pow(x, y) (__RETCAST_2(x, y)( \
  89. __FLTCX((x)+(y)) && __IS_FP(x) && __IS_FP(y) ? cpowf(x, y) : \
  90. __FLTCX((x)+(y)) ? cpow(x, y) : \
  91. __DBLCX((x)+(y)) ? cpow(x, y) : \
  92. __LDBLCX((x)+(y)) ? cpowl(x, y) : \
  93. __FLT(x) && __FLT(y) ? powf(x, y) : \
  94. __LDBL((x)+(y)) ? powl(x, y) : \
  95. pow(x, y) ))
  96. #define __tg_real_complex_fabs(x) (__RETCAST(__TO_REAL(x))( \
  97. __FLTCX(x) ? cabsf(x) : \
  98. __DBLCX(x) ? cabs(x) : \
  99. __LDBLCX(x) ? cabsl(x) : \
  100. __FLT(x) ? fabsf(x) : \
  101. __LDBL(x) ? fabsl(x) : \
  102. fabs(x) ))
  103. /* suppress any macros in math.h or complex.h */
  104. #undef acos
  105. #undef acosh
  106. #undef asin
  107. #undef asinh
  108. #undef atan
  109. #undef atan2
  110. #undef atanh
  111. #undef carg
  112. #undef cbrt
  113. #undef ceil
  114. #undef cimag
  115. #undef conj
  116. #undef copysign
  117. #undef cos
  118. #undef cosh
  119. #undef cproj
  120. #undef creal
  121. #undef erf
  122. #undef erfc
  123. #undef exp
  124. #undef exp2
  125. #undef expm1
  126. #undef fabs
  127. #undef fdim
  128. #undef floor
  129. #undef fma
  130. #undef fmax
  131. #undef fmin
  132. #undef fmod
  133. #undef frexp
  134. #undef hypot
  135. #undef ilogb
  136. #undef ldexp
  137. #undef lgamma
  138. #undef llrint
  139. #undef llround
  140. #undef log
  141. #undef log10
  142. #undef log1p
  143. #undef log2
  144. #undef logb
  145. #undef lrint
  146. #undef lround
  147. #undef nearbyint
  148. #undef nextafter
  149. #undef nexttoward
  150. #undef pow
  151. #undef remainder
  152. #undef remquo
  153. #undef rint
  154. #undef round
  155. #undef scalbln
  156. #undef scalbn
  157. #undef sin
  158. #undef sinh
  159. #undef sqrt
  160. #undef tan
  161. #undef tanh
  162. #undef tgamma
  163. #undef trunc
  164. /* tg functions */
  165. #define acos(x) __tg_real_complex(acos, (x))
  166. #define acosh(x) __tg_real_complex(acosh, (x))
  167. #define asin(x) __tg_real_complex(asin, (x))
  168. #define asinh(x) __tg_real_complex(asinh, (x))
  169. #define atan(x) __tg_real_complex(atan, (x))
  170. #define atan2(x,y) __tg_real_2(atan2, (x), (y))
  171. #define atanh(x) __tg_real_complex(atanh, (x))
  172. #define carg(x) __tg_complex_retreal(carg, (x))
  173. #define cbrt(x) __tg_real(cbrt, (x))
  174. #define ceil(x) __tg_real(ceil, (x))
  175. #define cimag(x) __tg_complex_retreal(cimag, (x))
  176. #define conj(x) __tg_complex(conj, (x))
  177. #define copysign(x,y) __tg_real_2(copysign, (x), (y))
  178. #define cos(x) __tg_real_complex(cos, (x))
  179. #define cosh(x) __tg_real_complex(cosh, (x))
  180. #define cproj(x) __tg_complex(cproj, (x))
  181. #define creal(x) __tg_complex_retreal(creal, (x))
  182. #define erf(x) __tg_real(erf, (x))
  183. #define erfc(x) __tg_real(erfc, (x))
  184. #define exp(x) __tg_real_complex(exp, (x))
  185. #define exp2(x) __tg_real(exp2, (x))
  186. #define expm1(x) __tg_real(expm1, (x))
  187. #define fabs(x) __tg_real_complex_fabs(x)
  188. #define fdim(x,y) __tg_real_2(fdim, (x), (y))
  189. #define floor(x) __tg_real(floor, (x))
  190. #define fma(x,y,z) __tg_real_fma((x), (y), (z))
  191. #define fmax(x,y) __tg_real_2(fmax, (x), (y))
  192. #define fmin(x,y) __tg_real_2(fmin, (x), (y))
  193. #define fmod(x,y) __tg_real_2(fmod, (x), (y))
  194. #define frexp(x,y) __tg_real_2_1(frexp, (x), (y))
  195. #define hypot(x,y) __tg_real_2(hypot, (x), (y))
  196. #define ilogb(x) __tg_real(ilogb, (x))
  197. #define ldexp(x,y) __tg_real_2_1(ldexp, (x), (y))
  198. #define lgamma(x) __tg_real(lgamma, (x))
  199. #define llrint(x) __tg_real(llrint, (x))
  200. #define llround(x) __tg_real(llround, (x))
  201. #define log(x) __tg_real_complex(log, (x))
  202. #define log10(x) __tg_real(log10, (x))
  203. #define log1p(x) __tg_real(log1p, (x))
  204. #define log2(x) __tg_real(log2, (x))
  205. #define logb(x) __tg_real(logb, (x))
  206. #define lrint(x) __tg_real(lrint, (x))
  207. #define lround(x) __tg_real(lround, (x))
  208. #define nearbyint(x) __tg_real(nearbyint, (x))
  209. #define nextafter(x,y) __tg_real_2(nextafter, (x), (y))
  210. #define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y))
  211. #define pow(x,y) __tg_real_complex_pow((x), (y))
  212. #define remainder(x,y) __tg_real_2(remainder, (x), (y))
  213. #define remquo(x,y,z) __tg_real_remquo((x), (y), (z))
  214. #define rint(x) __tg_real(rint, (x))
  215. #define round(x) __tg_real(round, (x))
  216. #define scalbln(x,y) __tg_real_2_1(scalbln, (x), (y))
  217. #define scalbn(x,y) __tg_real_2_1(scalbn, (x), (y))
  218. #define sin(x) __tg_real_complex(sin, (x))
  219. #define sinh(x) __tg_real_complex(sinh, (x))
  220. #define sqrt(x) __tg_real_complex(sqrt, (x))
  221. #define tan(x) __tg_real_complex(tan, (x))
  222. #define tanh(x) __tg_real_complex(tanh, (x))
  223. #define tgamma(x) __tg_real(tgamma, (x))
  224. #define trunc(x) __tg_real(trunc, (x))
  225. #endif