Update.
[glibc.git] / math / tgmath.h
blob50d4d428ee084d52c013f23aaebf2ee79b6d50b4
1 /* Copyright (C) 1997, 1998, 1999, 2000 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 /* This is ugly but unless gcc gets appropriate builtins we have to do
40 something like this. Don't ask how it works. */
42 /* 1 if 'type' is a floating type, 0 if 'type' is an integer type.
43 Allows for _Bool. Expands to an integer constant expression. */
44 #define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
46 /* The tgmath real type for T, where E is 0 if T is an integer type and
47 1 for a floating type. */
48 #define __tgmath_real_type_sub(T, E) \
49 __typeof__(*(0 ? (__typeof__(0 ? (double *)0 : (void *)(E)))0 \
50 : (__typeof__(0 ? (T *)0 : (void *)(!(E))))0))
52 /* The tgmath real type of EXPR. */
53 #define __tgmath_real_type(expr) \
54 __tgmath_real_type_sub(__typeof__(expr), __floating_type(__typeof__(expr)))
57 /* We have two kinds of generic macros: to support functions which are
58 only defined on real valued parameters and those which are defined
59 for complex functions as well. */
60 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
61 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
62 if (sizeof (Val) == sizeof (double) \
63 || __builtin_classify_type (Val) != 8) \
64 __tgmres = Fct (Val); \
65 else if (sizeof (Val) == sizeof (float)) \
66 __tgmres = Fct##f (Val); \
67 else \
68 __tgmres = Fct##l (Val); \
69 __tgmres; }))
71 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
72 (__extension__ ({ __tgmath_real_type (Val1) __tgmres; \
73 if (sizeof (Val1) == sizeof (double) \
74 || __builtin_classify_type (Val) != 8) \
75 __tgmres = Fct (Val1, Val2); \
76 else if (sizeof (Val1) == sizeof (float)) \
77 __tgmres = Fct##f (Val1, Val2); \
78 else \
79 __tgmres = Fct##l (Val1, Val2); \
80 __tgmres; }))
82 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
83 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
84 if ((sizeof (Val1) > sizeof (double) \
85 || sizeof (Val2) > sizeof (double)) \
86 && __builtin_classify_type (Val) == 8) \
87 __tgmres = Fct##l (Val1, Val2); \
88 else if (sizeof (Val1) == sizeof (double) \
89 || sizeof (Val2) == sizeof (double) \
90 || __builtin_classify_type (Val) != 8) \
91 __tgmres = Fct (Val1, Val2); \
92 else \
93 __tgmres = Fct (Val1, Val2); \
94 __tgmres; }))
96 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
97 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
98 if ((sizeof (Val1) > sizeof (double) \
99 || sizeof (Val2) > sizeof (double)) \
100 && __builtin_classify_type (Val) == 8) \
101 __tgmres = Fct##l (Val1, Val2, Val3); \
102 else if (sizeof (Val1) == sizeof (double) \
103 || sizeof (Val2) == sizeof (double) \
104 || __builtin_classify_type (Val) != 8) \
105 __tgmres = Fct (Val1, Val2, Val3); \
106 else \
107 __tgmres = Fct (Val1, Val2, Val3); \
108 __tgmres; }))
110 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
111 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2) + (Val3)) __tgmres;\
112 if ((sizeof (Val1) > sizeof (double) \
113 || sizeof (Val2) > sizeof (double) \
114 || sizeof (Val3) > sizeof (double)) \
115 && __builtin_classify_type (Val) == 8) \
116 __tgmres = Fct##l (Val1, Val2, Val3); \
117 else if (sizeof (Val1) == sizeof (double) \
118 || sizeof (Val2) == sizeof (double) \
119 || sizeof (Val3) == sizeof (double) \
120 || __builtin_classify_type (Val) != 8) \
121 __tgmres = Fct (Val1, Val2, Val3); \
122 else \
123 __tgmres = Fct (Val1, Val2, Val3); \
124 __tgmres; }))
126 /* XXX This definition has to be changed as soon as the compiler understands
127 the imaginary keyword. */
128 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
129 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
130 if (sizeof (__real__ (Val)) > sizeof (double) \
131 && __builtin_classify_type (__real__ (Val)) == 8) \
133 if (sizeof (__real__ (Val)) == sizeof (Val)) \
134 __tgmres = Fct##l (Val); \
135 else \
136 __tgmres = Cfct##l (Val); \
138 else if (sizeof (__real__ (Val)) == sizeof (double) \
139 || (__builtin_classify_type (__real__ (Val)) \
140 != 8) \
142 if (sizeof (__real__ (Val)) == sizeof (Val)) \
143 __tgmres = Fct (Val); \
144 else \
145 __tgmres = Cfct (Val); \
147 else \
149 if (sizeof (__real__ (Val)) == sizeof (Val)) \
150 __tgmres = Fct##f (Val); \
151 else \
152 __tgmres = Cfct##f (Val); \
154 __tgmres; }))
156 /* XXX This definition has to be changed as soon as the compiler understands
157 the imaginary keyword. */
158 # define __TGMATH_UNARY_IMAG_ONLY(Val, Fct) \
159 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
160 if (sizeof (Val) == sizeof (__complex__ double) \
161 || __builtin_classify_type (__real__ (Val)) != 8) \
162 __tgmres = Fct (Val); \
163 else if (sizeof (Val) == sizeof (__complex__ float)) \
164 __tgmres = Fct##f (Val); \
165 else \
166 __tgmres = Fct##l (Val); \
167 __tgmres; }))
169 /* XXX This definition has to be changed as soon as the compiler understands
170 the imaginary keyword. */
171 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
172 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
173 if ((sizeof (__real__ (Val1)) > sizeof (double) \
174 || sizeof (__real__ (Val2)) > sizeof (double)) \
175 && __builtin_classify_type (__real__ (Val)) == 8) \
177 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
178 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
179 __tgmres = Fct##l (Val1, Val2); \
180 else \
181 __tgmres = Cfct##l (Val1, Val2); \
183 else if (sizeof (__real__ (Val1)) == sizeof (double) \
184 || sizeof (__real__ (Val2)) == sizeof(double) \
185 || (__builtin_classify_type (__real__ (Val)) \
186 != 8) \
188 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
189 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
190 __tgmres = Fct (Val1, Val2); \
191 else \
192 __tgmres = Cfct (Val1, Val2); \
194 else \
196 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
197 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
198 __tgmres = Fct##f (Val1, Val2); \
199 else \
200 __tgmres = Cfct##f (Val1, Val2); \
202 __tgmres; }))
203 #else
204 # error "Unsupported compiler; you cannot use <tgmath.h>"
205 #endif
208 /* Unary functions defined for real and complex values. */
211 /* Trigonometric functions. */
213 /* Arc cosine of X. */
214 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
215 /* Arc sine of X. */
216 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
217 /* Arc tangent of X. */
218 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
219 /* Arc tangent of Y/X. */
220 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
222 /* Cosine of X. */
223 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
224 /* Sine of X. */
225 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
226 /* Tangent of X. */
227 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
230 /* Hyperbolic functions. */
232 /* Hyperbolic arc cosine of X. */
233 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
234 /* Hyperbolic arc sine of X. */
235 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
236 /* Hyperbolic arc tangent of X. */
237 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
239 /* Hyperbolic cosine of X. */
240 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
241 /* Hyperbolic sine of X. */
242 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
243 /* Hyperbolic tangent of X. */
244 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
247 /* Exponential and logarithmic functions. */
249 /* Exponential function of X. */
250 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
252 /* Break VALUE into a normalized fraction and an integral power of 2. */
253 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
255 /* X times (two to the EXP power). */
256 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
258 /* Natural logarithm of X. */
259 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
261 /* Base-ten logarithm of X. */
262 #ifdef __USE_GNU
263 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
264 #else
265 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
266 #endif
268 /* Return exp(X) - 1. */
269 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
271 /* Return log(1 + X). */
272 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
274 /* Return the base 2 signed integral exponent of X. */
275 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
277 /* Compute base-2 exponential of X. */
278 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
280 /* Compute base-2 logarithm of X. */
281 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
284 /* Power functions. */
286 /* Return X to the Y power. */
287 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
289 /* Return the square root of X. */
290 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
292 /* Return `sqrt(X*X + Y*Y)'. */
293 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
295 /* Return the cube root of X. */
296 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
299 /* Nearest integer, absolute value, and remainder functions. */
301 /* Smallest integral value not less than X. */
302 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
304 /* Absolute value of X. */
305 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs)
307 /* Largest integer not greater than X. */
308 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
310 /* Floating-point modulo remainder of X/Y. */
311 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
313 /* Round X to integral valuein floating-point format using current
314 rounding direction, but do not raise inexact exception. */
315 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
317 /* Round X to nearest integral value, rounding halfway cases away from
318 zero. */
319 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
321 /* Round X to the integral value in floating-point format nearest but
322 not larger in magnitude. */
323 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
325 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
326 and magnitude congruent `mod 2^n' to the magnitude of the integral
327 quotient x/y, with n >= 3. */
328 #define remquo(Val1, Val2, Val3) \
329 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
331 /* Round X to nearest integral value according to current rounding
332 direction. */
333 #define lrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, lrint)
334 #define llrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, llrint)
336 /* Round X to nearest integral value, rounding halfway cases away from
337 zero. */
338 #define lround(Val) __TGMATH_UNARY_REAL_ONLY (Val, lround)
339 #define llround(Val) __TGMATH_UNARY_REAL_ONLY (Val, llround)
342 /* Return X with its signed changed to Y's. */
343 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
345 /* Error and gamma functions. */
346 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
347 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
348 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
349 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
352 /* Return the integer nearest X in the direction of the
353 prevailing rounding mode. */
354 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
356 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
357 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
358 #define nexttoward(Val1, Val2) \
359 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
361 /* Return the remainder of integer divison X / Y with infinite precision. */
362 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
364 /* Return X times (2 to the Nth power). */
365 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
366 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
367 #endif
369 /* Return X times (2 to the Nth power). */
370 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
372 /* Return X times (2 to the Nth power). */
373 #define scalbln(Val1, Val2) \
374 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
376 /* Return the binary exponent of X, which must be nonzero. */
377 #define ilogb(Val) __TGMATH_UNARY_REAL_ONLY (Val, ilogb)
380 /* Return positive difference between X and Y. */
381 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
383 /* Return maximum numeric value from X and Y. */
384 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
386 /* Return minimum numeric value from X and Y. */
387 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
390 /* Multiply-add function computed as a ternary operation. */
391 #define fma(Vat1, Val2, Val3) \
392 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
395 /* Absolute value, conjugates, and projection. */
397 /* Argument value of Z. */
398 #define carg(Val) __TGMATH_UNARY_IMAG_ONLY (Val, carg)
400 /* Complex conjugate of Z. */
401 #define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj)
403 /* Projection of Z onto the Riemann sphere. */
404 #define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj)
407 /* Decomposing complex values. */
409 /* Imaginary part of Z. */
410 #define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag)
412 /* Real part of Z. */
413 #define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal)
415 #endif /* tgmath.h */