tgmath.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. /*
  16. * ISO C99 Standard: 7.22 Type-generic math <tgmath.h>
  17. */
  18. #ifndef _TGMATH_H
  19. #define _TGMATH_H 1
  20. /* Include the needed headers. */
  21. #include <math.h>
  22. #include <complex.h>
  23. /* Since `complex' is currently not really implemented in most C compilers
  24. and if it is implemented, the implementations differ. This makes it
  25. quite difficult to write a generic implementation of this header. We
  26. do not try this for now and instead concentrate only on GNU CC. Once
  27. we have more information support for other compilers might follow. */
  28. #if __GNUC_PREREQ (2, 7)
  29. # ifdef __NO_LONG_DOUBLE_MATH
  30. # define __tgml(fct) fct
  31. # else
  32. # define __tgml(fct) fct ## l
  33. # endif
  34. /* This is ugly but unless gcc gets appropriate builtins we have to do
  35. something like this. Don't ask how it works. */
  36. /* 1 if 'type' is a floating type, 0 if 'type' is an integer type.
  37. Allows for _Bool. Expands to an integer constant expression. */
  38. # define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
  39. /* The tgmath real type for T, where E is 0 if T is an integer type and
  40. 1 for a floating type. */
  41. # define __tgmath_real_type_sub(T, E) \
  42. __typeof__(*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
  43. : (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
  44. /* The tgmath real type of EXPR. */
  45. # define __tgmath_real_type(expr) \
  46. __tgmath_real_type_sub(__typeof__(expr), __floating_type(__typeof__(expr)))
  47. /* We have two kinds of generic macros: to support functions which are
  48. only defined on real valued parameters and those which are defined
  49. for complex functions as well. */
  50. # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
  51. (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
  52. if (sizeof (Val) == sizeof (double) \
  53. || __builtin_classify_type (Val) != 8) \
  54. __tgmres = Fct (Val); \
  55. else if (sizeof (Val) == sizeof (float)) \
  56. __tgmres = Fct##f (Val); \
  57. else \
  58. __tgmres = __tgml(Fct) (Val); \
  59. __tgmres; }))
  60. # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
  61. (__extension__ ({ __tgmath_real_type (Val1) __tgmres; \
  62. if (sizeof (Val1) == sizeof (double) \
  63. || __builtin_classify_type (Val1) != 8) \
  64. __tgmres = Fct (Val1, Val2); \
  65. else if (sizeof (Val1) == sizeof (float)) \
  66. __tgmres = Fct##f (Val1, Val2); \
  67. else \
  68. __tgmres = __tgml(Fct) (Val1, Val2); \
  69. __tgmres; }))
  70. # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
  71. (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
  72. if ((sizeof (Val1) > sizeof (double) \
  73. || sizeof (Val2) > sizeof (double)) \
  74. && __builtin_classify_type ((Val1) + (Val2)) == 8) \
  75. __tgmres = __tgml(Fct) (Val1, Val2); \
  76. else if (sizeof (Val1) == sizeof (double) \
  77. || sizeof (Val2) == sizeof (double) \
  78. || __builtin_classify_type (Val1) != 8 \
  79. || __builtin_classify_type (Val2) != 8) \
  80. __tgmres = Fct (Val1, Val2); \
  81. else \
  82. __tgmres = Fct##f (Val1, Val2); \
  83. __tgmres; }))
  84. # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
  85. (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
  86. if ((sizeof (Val1) > sizeof (double) \
  87. || sizeof (Val2) > sizeof (double)) \
  88. && __builtin_classify_type ((Val1) + (Val2)) == 8) \
  89. __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
  90. else if (sizeof (Val1) == sizeof (double) \
  91. || sizeof (Val2) == sizeof (double) \
  92. || __builtin_classify_type (Val1) != 8 \
  93. || __builtin_classify_type (Val2) != 8) \
  94. __tgmres = Fct (Val1, Val2, Val3); \
  95. else \
  96. __tgmres = Fct##f (Val1, Val2, Val3); \
  97. __tgmres; }))
  98. # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
  99. (__extension__ ({ __tgmath_real_type ((Val1) + (Val2) + (Val3)) __tgmres;\
  100. if ((sizeof (Val1) > sizeof (double) \
  101. || sizeof (Val2) > sizeof (double) \
  102. || sizeof (Val3) > sizeof (double)) \
  103. && __builtin_classify_type ((Val1) + (Val2) \
  104. + (Val3)) == 8) \
  105. __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
  106. else if (sizeof (Val1) == sizeof (double) \
  107. || sizeof (Val2) == sizeof (double) \
  108. || sizeof (Val3) == sizeof (double) \
  109. || __builtin_classify_type (Val1) != 8 \
  110. || __builtin_classify_type (Val2) != 8 \
  111. || __builtin_classify_type (Val3) != 8) \
  112. __tgmres = Fct (Val1, Val2, Val3); \
  113. else \
  114. __tgmres = Fct##f (Val1, Val2, Val3); \
  115. __tgmres; }))
  116. /* XXX This definition has to be changed as soon as the compiler understands
  117. the imaginary keyword. */
  118. # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
  119. (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
  120. if (sizeof (__real__ (Val)) > sizeof (double) \
  121. && __builtin_classify_type (__real__ (Val)) == 8) \
  122. { \
  123. if (sizeof (__real__ (Val)) == sizeof (Val)) \
  124. __tgmres = __tgml(Fct) (Val); \
  125. else \
  126. __tgmres = __tgml(Cfct) (Val); \
  127. } \
  128. else if (sizeof (__real__ (Val)) == sizeof (double) \
  129. || __builtin_classify_type (__real__ (Val)) \
  130. != 8) \
  131. { \
  132. if (sizeof (__real__ (Val)) == sizeof (Val)) \
  133. __tgmres = Fct (Val); \
  134. else \
  135. __tgmres = Cfct (Val); \
  136. } \
  137. else \
  138. { \
  139. if (sizeof (__real__ (Val)) == sizeof (Val)) \
  140. __tgmres = Fct##f (Val); \
  141. else \
  142. __tgmres = Cfct##f (Val); \
  143. } \
  144. __tgmres; }))
  145. /* XXX This definition has to be changed as soon as the compiler understands
  146. the imaginary keyword. */
  147. # define __TGMATH_UNARY_IMAG_ONLY(Val, Fct) \
  148. (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
  149. if (sizeof (Val) == sizeof (__complex__ double) \
  150. || __builtin_classify_type (__real__ (Val)) != 8) \
  151. __tgmres = Fct (Val); \
  152. else if (sizeof (Val) == sizeof (__complex__ float)) \
  153. __tgmres = Fct##f (Val); \
  154. else \
  155. __tgmres = __tgml(Fct) (Val); \
  156. __tgmres; }))
  157. /* XXX This definition has to be changed as soon as the compiler understands
  158. the imaginary keyword. */
  159. # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
  160. (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
  161. if ((sizeof (__real__ (Val1)) > sizeof (double) \
  162. || sizeof (__real__ (Val2)) > sizeof (double)) \
  163. && __builtin_classify_type (__real__ (Val1) \
  164. + __real__ (Val2)) \
  165. == 8) \
  166. { \
  167. if (sizeof (__real__ (Val1)) == sizeof (Val1) \
  168. && sizeof (__real__ (Val2)) == sizeof (Val2)) \
  169. __tgmres = __tgml(Fct) (Val1, Val2); \
  170. else \
  171. __tgmres = __tgml(Cfct) (Val1, Val2); \
  172. } \
  173. else if (sizeof (__real__ (Val1)) == sizeof (double) \
  174. || sizeof (__real__ (Val2)) == sizeof(double) \
  175. || (__builtin_classify_type (__real__ (Val1)) \
  176. != 8) \
  177. || (__builtin_classify_type (__real__ (Val2)) \
  178. != 8)) \
  179. { \
  180. if (sizeof (__real__ (Val1)) == sizeof (Val1) \
  181. && sizeof (__real__ (Val2)) == sizeof (Val2)) \
  182. __tgmres = Fct (Val1, Val2); \
  183. else \
  184. __tgmres = Cfct (Val1, Val2); \
  185. } \
  186. else \
  187. { \
  188. if (sizeof (__real__ (Val1)) == sizeof (Val1) \
  189. && sizeof (__real__ (Val2)) == sizeof (Val2)) \
  190. __tgmres = Fct##f (Val1, Val2); \
  191. else \
  192. __tgmres = Cfct##f (Val1, Val2); \
  193. } \
  194. __tgmres; }))
  195. #else
  196. # error "Unsupported compiler; you cannot use <tgmath.h>"
  197. #endif
  198. /* Unary functions defined for real and complex values. */
  199. /* Trigonometric functions. */
  200. /* Arc cosine of X. */
  201. #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
  202. /* Arc sine of X. */
  203. #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
  204. /* Arc tangent of X. */
  205. #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
  206. /* Arc tangent of Y/X. */
  207. #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
  208. /* Cosine of X. */
  209. #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
  210. /* Sine of X. */
  211. #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
  212. /* Tangent of X. */
  213. #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
  214. /* Hyperbolic functions. */
  215. /* Hyperbolic arc cosine of X. */
  216. #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
  217. /* Hyperbolic arc sine of X. */
  218. #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
  219. /* Hyperbolic arc tangent of X. */
  220. #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
  221. /* Hyperbolic cosine of X. */
  222. #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
  223. /* Hyperbolic sine of X. */
  224. #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
  225. /* Hyperbolic tangent of X. */
  226. #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
  227. /* Exponential and logarithmic functions. */
  228. /* Exponential function of X. */
  229. #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
  230. /* Break VALUE into a normalized fraction and an integral power of 2. */
  231. #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
  232. /* X times (two to the EXP power). */
  233. #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
  234. /* Natural logarithm of X. */
  235. #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
  236. /* Base-ten logarithm of X. */
  237. #ifdef __USE_GNU
  238. # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
  239. #else
  240. # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
  241. #endif
  242. /* Return exp(X) - 1. */
  243. #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
  244. /* Return log(1 + X). */
  245. #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
  246. /* Return the base 2 signed integral exponent of X. */
  247. #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
  248. /* Compute base-2 exponential of X. */
  249. #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
  250. /* Compute base-2 logarithm of X. */
  251. #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
  252. /* Power functions. */
  253. /* Return X to the Y power. */
  254. #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
  255. /* Return the square root of X. */
  256. #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
  257. /* Return `sqrt(X*X + Y*Y)'. */
  258. #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
  259. /* Return the cube root of X. */
  260. #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
  261. /* Nearest integer, absolute value, and remainder functions. */
  262. /* Smallest integral value not less than X. */
  263. #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
  264. /* Absolute value of X. */
  265. #define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs)
  266. /* Largest integer not greater than X. */
  267. #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
  268. /* Floating-point modulo remainder of X/Y. */
  269. #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
  270. /* Round X to integral valuein floating-point format using current
  271. rounding direction, but do not raise inexact exception. */
  272. #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
  273. /* Round X to nearest integral value, rounding halfway cases away from
  274. zero. */
  275. #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
  276. /* Round X to the integral value in floating-point format nearest but
  277. not larger in magnitude. */
  278. #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
  279. /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
  280. and magnitude congruent `mod 2^n' to the magnitude of the integral
  281. quotient x/y, with n >= 3. */
  282. #define remquo(Val1, Val2, Val3) \
  283. __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
  284. /* Round X to nearest integral value according to current rounding
  285. direction. */
  286. #define lrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, lrint)
  287. #define llrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, llrint)
  288. /* Round X to nearest integral value, rounding halfway cases away from
  289. zero. */
  290. #define lround(Val) __TGMATH_UNARY_REAL_ONLY (Val, lround)
  291. #define llround(Val) __TGMATH_UNARY_REAL_ONLY (Val, llround)
  292. /* Return X with its signed changed to Y's. */
  293. #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
  294. /* Error and gamma functions. */
  295. #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
  296. #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
  297. #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
  298. #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
  299. /* Return the integer nearest X in the direction of the
  300. prevailing rounding mode. */
  301. #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
  302. /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
  303. #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
  304. #define nexttoward(Val1, Val2) \
  305. __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
  306. /* Return the remainder of integer divison X / Y with infinite precision. */
  307. #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
  308. /* Return X times (2 to the Nth power). */
  309. #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
  310. # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
  311. #endif
  312. /* Return X times (2 to the Nth power). */
  313. #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
  314. /* Return X times (2 to the Nth power). */
  315. #define scalbln(Val1, Val2) \
  316. __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
  317. /* Return the binary exponent of X, which must be nonzero. */
  318. #define ilogb(Val) __TGMATH_UNARY_REAL_ONLY (Val, ilogb)
  319. /* Return positive difference between X and Y. */
  320. #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
  321. /* Return maximum numeric value from X and Y. */
  322. #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
  323. /* Return minimum numeric value from X and Y. */
  324. #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
  325. /* Multiply-add function computed as a ternary operation. */
  326. #define fma(Val1, Val2, Val3) \
  327. __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
  328. /* Absolute value, conjugates, and projection. */
  329. /* Argument value of Z. */
  330. #define carg(Val) __TGMATH_UNARY_IMAG_ONLY (Val, carg)
  331. /* Complex conjugate of Z. */
  332. #define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj)
  333. /* Projection of Z onto the Riemann sphere. */
  334. #define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj)
  335. /* Decomposing complex values. */
  336. /* Imaginary part of Z. */
  337. #define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag)
  338. /* Real part of Z. */
  339. #define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal)
  340. #endif /* tgmath.h */