tgmath.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /* tg functions */
  104. #define acos(x) __tg_real_complex(acos, (x))
  105. #define acosh(x) __tg_real_complex(acosh, (x))
  106. #define asin(x) __tg_real_complex(asin, (x))
  107. #define asinh(x) __tg_real_complex(asinh, (x))
  108. #define atan(x) __tg_real_complex(atan, (x))
  109. #define atan2(x,y) __tg_real_2(atan2, (x), (y))
  110. #define atanh(x) __tg_real_complex(atanh, (x))
  111. #define carg(x) __tg_complex_retreal(carg, (x))
  112. #define cbrt(x) __tg_real(cbrt, (x))
  113. #define ceil(x) __tg_real(ceil, (x))
  114. #define cimag(x) __tg_complex_retreal(cimag, (x))
  115. #define conj(x) __tg_complex(conj, (x))
  116. #define copysign(x,y) __tg_real_2(copysign, (x), (y))
  117. #define cos(x) __tg_real_complex(cos, (x))
  118. #define cosh(x) __tg_real_complex(cosh, (x))
  119. #define cproj(x) __tg_complex(cproj, (x))
  120. #define creal(x) __tg_complex_retreal(creal, (x))
  121. #define erf(x) __tg_real(erf, (x))
  122. #define erfc(x) __tg_real(erfc, (x))
  123. #define exp(x) __tg_real_complex(exp, (x))
  124. #define exp2(x) __tg_real(exp2, (x))
  125. #define expm1(x) __tg_real(expm1, (x))
  126. #define fabs(x) __tg_real_complex_fabs(x)
  127. #define fdim(x,y) __tg_real_2(fdim, (x), (y))
  128. #define floor(x) __tg_real(floor, (x))
  129. #define fma(x,y,z) __tg_real_fma((x), (y), (z))
  130. #define fmax(x,y) __tg_real_2(fmax, (x), (y))
  131. #define fmin(x,y) __tg_real_2(fmin, (x), (y))
  132. #define fmod(x,y) __tg_real_2(fmod, (x), (y))
  133. #define frexp(x,y) __tg_real_2_1(frexp, (x), (y))
  134. #define hypot(x,y) __tg_real_2(hypot, (x), (y))
  135. #define ilogb(x) __tg_real(ilogb, (x))
  136. #define ldexp(x,y) __tg_real_2_1(ldexp, (x), (y))
  137. #define lgamma(x) __tg_real(lgamma, (x))
  138. #define llrint(x) __tg_real(llrint, (x))
  139. #define llround(x) __tg_real(llround, (x))
  140. #define log(x) __tg_real_complex(log, (x))
  141. #define log10(x) __tg_real(log10, (x))
  142. #define log1p(x) __tg_real(log1p, (x))
  143. #define log2(x) __tg_real(log2, (x))
  144. #define logb(x) __tg_real(logb, (x))
  145. #define lrint(x) __tg_real(lrint, (x))
  146. #define lround(x) __tg_real(lround, (x))
  147. #define nearbyint(x) __tg_real(nearbyint, (x))
  148. #define nextafter(x,y) __tg_real_2(nextafter, (x), (y)
  149. #define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y))
  150. #define pow(x,y) __tg_real_complex_pow((x), (y))
  151. #define remainder(x,y) __tg_real_2(remainder, (x), (y))
  152. #define remquo(x,y,z) __tg_real_remquo((x), (y), (z))
  153. #define rint(x) __tg_real(rint, (x))
  154. #define round(x) __tg_real(round, (x))
  155. #define scalbln(x,y) __tg_real_2_1(scalbln, (x), (y))
  156. #define scalbn(x,y) __tg_real_2_1(scalbn, (x), (y))
  157. #define sin(x) __tg_real_complex(sin, (x))
  158. #define sinh(x) __tg_real_complex(sinh, (x))
  159. #define sqrt(x) __tg_real_complex(sqrt, (x))
  160. #define tan(x) __tg_real_complex(tan, (x))
  161. #define tanh(x) __tg_real_complex(tanh, (x))
  162. #define tgamma(x) __tg_real(tgamma, (x))
  163. #define trunc(x) __tg_real(trunc, (x))
  164. #endif