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