Update.
[glibc.git] / math / tgmath.h
blob168b262e68c7d032db26b1ccecc2ec72fc15d327
1 /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 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 # ifdef __NO_LONG_DOUBLE_MATH
40 # define __tgml(fct) fct
41 # else
42 # define __tgml(fct) fct ## l
43 # endif
45 /* This is ugly but unless gcc gets appropriate builtins we have to do
46 something like this. Don't ask how it works. */
48 /* 1 if 'type' is a floating type, 0 if 'type' is an integer type.
49 Allows for _Bool. Expands to an integer constant expression. */
50 # define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
52 /* The tgmath real type for T, where E is 0 if T is an integer type and
53 1 for a floating type. */
54 # define __tgmath_real_type_sub(T, E) \
55 __typeof__(*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
56 : (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
58 /* The tgmath real type of EXPR. */
59 # define __tgmath_real_type(expr) \
60 __tgmath_real_type_sub(__typeof__(expr), __floating_type(__typeof__(expr)))
63 /* We have two kinds of generic macros: to support functions which are
64 only defined on real valued parameters and those which are defined
65 for complex functions as well. */
66 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
67 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
68 if (sizeof (Val) == sizeof (double) \
69 || __builtin_classify_type (Val) != 8) \
70 __tgmres = Fct (Val); \
71 else if (sizeof (Val) == sizeof (float)) \
72 __tgmres = Fct##f (Val); \
73 else \
74 __tgmres = __tgml(Fct) (Val); \
75 __tgmres; }))
77 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, RetType, Fct) \
78 (__extension__ ({ RetType __tgmres; \
79 if (sizeof (Val) == sizeof (double) \
80 || __builtin_classify_type (Val) != 8) \
81 __tgmres = Fct (Val); \
82 else if (sizeof (Val) == sizeof (float)) \
83 __tgmres = Fct##f (Val); \
84 else \
85 __tgmres = __tgml(Fct) (Val); \
86 __tgmres; }))
88 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
89 (__extension__ ({ __tgmath_real_type (Val1) __tgmres; \
90 if (sizeof (Val1) == sizeof (double) \
91 || __builtin_classify_type (Val1) != 8) \
92 __tgmres = Fct (Val1, Val2); \
93 else if (sizeof (Val1) == sizeof (float)) \
94 __tgmres = Fct##f (Val1, Val2); \
95 else \
96 __tgmres = __tgml(Fct) (Val1, Val2); \
97 __tgmres; }))
99 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
100 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
101 if ((sizeof (Val1) > sizeof (double) \
102 || sizeof (Val2) > sizeof (double)) \
103 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
104 __tgmres = __tgml(Fct) (Val1, Val2); \
105 else if (sizeof (Val1) == sizeof (double) \
106 || sizeof (Val2) == sizeof (double) \
107 || __builtin_classify_type (Val1) != 8 \
108 || __builtin_classify_type (Val2) != 8) \
109 __tgmres = Fct (Val1, Val2); \
110 else \
111 __tgmres = Fct##f (Val1, Val2); \
112 __tgmres; }))
114 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
115 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
116 if ((sizeof (Val1) > sizeof (double) \
117 || sizeof (Val2) > sizeof (double)) \
118 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
119 __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
120 else if (sizeof (Val1) == sizeof (double) \
121 || sizeof (Val2) == sizeof (double) \
122 || __builtin_classify_type (Val1) != 8 \
123 || __builtin_classify_type (Val2) != 8) \
124 __tgmres = Fct (Val1, Val2, Val3); \
125 else \
126 __tgmres = Fct##f (Val1, Val2, Val3); \
127 __tgmres; }))
129 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
130 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2) + (Val3)) __tgmres;\
131 if ((sizeof (Val1) > sizeof (double) \
132 || sizeof (Val2) > sizeof (double) \
133 || sizeof (Val3) > sizeof (double)) \
134 && __builtin_classify_type ((Val1) + (Val2) \
135 + (Val3)) == 8) \
136 __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
137 else if (sizeof (Val1) == sizeof (double) \
138 || sizeof (Val2) == sizeof (double) \
139 || sizeof (Val3) == sizeof (double) \
140 || __builtin_classify_type (Val1) != 8 \
141 || __builtin_classify_type (Val2) != 8 \
142 || __builtin_classify_type (Val3) != 8) \
143 __tgmres = Fct (Val1, Val2, Val3); \
144 else \
145 __tgmres = Fct##f (Val1, Val2, Val3); \
146 __tgmres; }))
148 /* XXX This definition has to be changed as soon as the compiler understands
149 the imaginary keyword. */
150 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
151 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
152 if (sizeof (__real__ (Val)) > sizeof (double) \
153 && __builtin_classify_type (__real__ (Val)) == 8) \
155 if (sizeof (__real__ (Val)) == sizeof (Val)) \
156 __tgmres = __tgml(Fct) (Val); \
157 else \
158 __tgmres = __tgml(Cfct) (Val); \
160 else if (sizeof (__real__ (Val)) == sizeof (double) \
161 || __builtin_classify_type (__real__ (Val)) \
162 != 8) \
164 if (sizeof (__real__ (Val)) == sizeof (Val)) \
165 __tgmres = Fct (Val); \
166 else \
167 __tgmres = Cfct (Val); \
169 else \
171 if (sizeof (__real__ (Val)) == sizeof (Val)) \
172 __tgmres = Fct##f (Val); \
173 else \
174 __tgmres = Cfct##f (Val); \
176 __tgmres; }))
178 /* XXX This definition has to be changed as soon as the compiler understands
179 the imaginary keyword. */
180 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
181 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
182 if ((sizeof (__real__ (Val1)) > sizeof (double) \
183 || sizeof (__real__ (Val2)) > sizeof (double)) \
184 && __builtin_classify_type (__real__ (Val1) \
185 + __real__ (Val2)) \
186 == 8) \
188 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
189 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
190 __tgmres = __tgml(Fct) (Val1, Val2); \
191 else \
192 __tgmres = __tgml(Cfct) (Val1, Val2); \
194 else if (sizeof (__real__ (Val1)) == sizeof (double) \
195 || sizeof (__real__ (Val2)) == sizeof(double) \
196 || (__builtin_classify_type (__real__ (Val1)) \
197 != 8) \
198 || (__builtin_classify_type (__real__ (Val2)) \
199 != 8)) \
201 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
202 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
203 __tgmres = Fct (Val1, Val2); \
204 else \
205 __tgmres = Cfct (Val1, Val2); \
207 else \
209 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
210 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
211 __tgmres = Fct##f (Val1, Val2); \
212 else \
213 __tgmres = Cfct##f (Val1, Val2); \
215 __tgmres; }))
216 #else
217 # error "Unsupported compiler; you cannot use <tgmath.h>"
218 #endif
221 /* Unary functions defined for real and complex values. */
224 /* Trigonometric functions. */
226 /* Arc cosine of X. */
227 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
228 /* Arc sine of X. */
229 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
230 /* Arc tangent of X. */
231 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
232 /* Arc tangent of Y/X. */
233 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
235 /* Cosine of X. */
236 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
237 /* Sine of X. */
238 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
239 /* Tangent of X. */
240 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
243 /* Hyperbolic functions. */
245 /* Hyperbolic arc cosine of X. */
246 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
247 /* Hyperbolic arc sine of X. */
248 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
249 /* Hyperbolic arc tangent of X. */
250 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
252 /* Hyperbolic cosine of X. */
253 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
254 /* Hyperbolic sine of X. */
255 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
256 /* Hyperbolic tangent of X. */
257 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
260 /* Exponential and logarithmic functions. */
262 /* Exponential function of X. */
263 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
265 /* Break VALUE into a normalized fraction and an integral power of 2. */
266 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
268 /* X times (two to the EXP power). */
269 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
271 /* Natural logarithm of X. */
272 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
274 /* Base-ten logarithm of X. */
275 #ifdef __USE_GNU
276 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
277 #else
278 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
279 #endif
281 /* Return exp(X) - 1. */
282 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
284 /* Return log(1 + X). */
285 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
287 /* Return the base 2 signed integral exponent of X. */
288 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
290 /* Compute base-2 exponential of X. */
291 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
293 /* Compute base-2 logarithm of X. */
294 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
297 /* Power functions. */
299 /* Return X to the Y power. */
300 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
302 /* Return the square root of X. */
303 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
305 /* Return `sqrt(X*X + Y*Y)'. */
306 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
308 /* Return the cube root of X. */
309 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
312 /* Nearest integer, absolute value, and remainder functions. */
314 /* Smallest integral value not less than X. */
315 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
317 /* Absolute value of X. */
318 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs)
320 /* Largest integer not greater than X. */
321 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
323 /* Floating-point modulo remainder of X/Y. */
324 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
326 /* Round X to integral valuein floating-point format using current
327 rounding direction, but do not raise inexact exception. */
328 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
330 /* Round X to nearest integral value, rounding halfway cases away from
331 zero. */
332 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
334 /* Round X to the integral value in floating-point format nearest but
335 not larger in magnitude. */
336 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
338 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
339 and magnitude congruent `mod 2^n' to the magnitude of the integral
340 quotient x/y, with n >= 3. */
341 #define remquo(Val1, Val2, Val3) \
342 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
344 /* Round X to nearest integral value according to current rounding
345 direction. */
346 #define lrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lrint)
347 #define llrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llrint)
349 /* Round X to nearest integral value, rounding halfway cases away from
350 zero. */
351 #define lround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lround)
352 #define llround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llround)
355 /* Return X with its signed changed to Y's. */
356 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
358 /* Error and gamma functions. */
359 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
360 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
361 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
362 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
365 /* Return the integer nearest X in the direction of the
366 prevailing rounding mode. */
367 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
369 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
370 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
371 #define nexttoward(Val1, Val2) \
372 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
374 /* Return the remainder of integer divison X / Y with infinite precision. */
375 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
377 /* Return X times (2 to the Nth power). */
378 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
379 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
380 #endif
382 /* Return X times (2 to the Nth power). */
383 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
385 /* Return X times (2 to the Nth power). */
386 #define scalbln(Val1, Val2) \
387 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
389 /* Return the binary exponent of X, which must be nonzero. */
390 #define ilogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, int, ilogb)
393 /* Return positive difference between X and Y. */
394 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
396 /* Return maximum numeric value from X and Y. */
397 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
399 /* Return minimum numeric value from X and Y. */
400 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
403 /* Multiply-add function computed as a ternary operation. */
404 #define fma(Val1, Val2, Val3) \
405 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
408 /* Absolute value, conjugates, and projection. */
410 /* Argument value of Z. */
411 #define carg(Val) __TGMATH_UNARY_REAL_IMAG (Val, carg, carg)
413 /* Complex conjugate of Z. */
414 #define conj(Val) __TGMATH_UNARY_REAL_IMAG (Val, conj, conj)
416 /* Projection of Z onto the Riemann sphere. */
417 #define cproj(Val) __TGMATH_UNARY_REAL_IMAG (Val, cproj, cproj)
420 /* Decomposing complex values. */
422 /* Imaginary part of Z. */
423 #define cimag(Val) __TGMATH_UNARY_REAL_IMAG (Val, cimag, cimag)
425 /* Real part of Z. */
426 #define creal(Val) __TGMATH_UNARY_REAL_IMAG (Val, creal, creal)
428 #endif /* tgmath.h */