Update.
[glibc.git] / math / tgmath.h
blob0c02aaa6e79a812491ccc940ee0d8518fb8e2a05
1 /* Copyright (C) 1997, 1998, 1999, 2000, 2001 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 # 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_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
78 (__extension__ ({ __tgmath_real_type (Val1) __tgmres; \
79 if (sizeof (Val1) == sizeof (double) \
80 || __builtin_classify_type (Val1) != 8) \
81 __tgmres = Fct (Val1, Val2); \
82 else if (sizeof (Val1) == sizeof (float)) \
83 __tgmres = Fct##f (Val1, Val2); \
84 else \
85 __tgmres = __tgml(Fct) (Val1, Val2); \
86 __tgmres; }))
88 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
89 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
90 if ((sizeof (Val1) > sizeof (double) \
91 || sizeof (Val2) > sizeof (double)) \
92 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
93 __tgmres = __tgml(Fct) (Val1, Val2); \
94 else if (sizeof (Val1) == sizeof (double) \
95 || sizeof (Val2) == sizeof (double) \
96 || __builtin_classify_type ((Val1) \
97 + (Val2)) != 8) \
98 __tgmres = Fct (Val1, Val2); \
99 else \
100 __tgmres = Fct##f (Val1, Val2); \
101 __tgmres; }))
103 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
104 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
105 if ((sizeof (Val1) > sizeof (double) \
106 || sizeof (Val2) > sizeof (double)) \
107 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
108 __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
109 else if (sizeof (Val1) == sizeof (double) \
110 || sizeof (Val2) == sizeof (double) \
111 || __builtin_classify_type ((Val1) \
112 + (Val2)) != 8) \
113 __tgmres = Fct (Val1, Val2, Val3); \
114 else \
115 __tgmres = Fct##f (Val1, Val2, Val3); \
116 __tgmres; }))
118 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
119 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2) + (Val3)) __tgmres;\
120 if ((sizeof (Val1) > sizeof (double) \
121 || sizeof (Val2) > sizeof (double) \
122 || sizeof (Val3) > sizeof (double)) \
123 && __builtin_classify_type ((Val1) + (Val2) \
124 + (Val3)) == 8) \
125 __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
126 else if (sizeof (Val1) == sizeof (double) \
127 || sizeof (Val2) == sizeof (double) \
128 || sizeof (Val3) == sizeof (double) \
129 || __builtin_classify_type ((Val1) + (Val2) \
130 + (Val3)) != 8) \
131 __tgmres = Fct (Val1, Val2, Val3); \
132 else \
133 __tgmres = Fct##f (Val1, Val2, Val3); \
134 __tgmres; }))
136 /* XXX This definition has to be changed as soon as the compiler understands
137 the imaginary keyword. */
138 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
139 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
140 if (sizeof (__real__ (Val)) > sizeof (double) \
141 && __builtin_classify_type (__real__ (Val)) == 8) \
143 if (sizeof (__real__ (Val)) == sizeof (Val)) \
144 __tgmres = __tgml(Fct) (Val); \
145 else \
146 __tgmres = __tgml(Cfct) (Val); \
148 else if (sizeof (__real__ (Val)) == sizeof (double) \
149 || __builtin_classify_type (__real__ (Val)) \
150 != 8) \
152 if (sizeof (__real__ (Val)) == sizeof (Val)) \
153 __tgmres = Fct (Val); \
154 else \
155 __tgmres = Cfct (Val); \
157 else \
159 if (sizeof (__real__ (Val)) == sizeof (Val)) \
160 __tgmres = Fct##f (Val); \
161 else \
162 __tgmres = Cfct##f (Val); \
164 __tgmres; }))
166 /* XXX This definition has to be changed as soon as the compiler understands
167 the imaginary keyword. */
168 # define __TGMATH_UNARY_IMAG_ONLY(Val, Fct) \
169 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
170 if (sizeof (Val) == sizeof (__complex__ double) \
171 || __builtin_classify_type (__real__ (Val)) != 8) \
172 __tgmres = Fct (Val); \
173 else if (sizeof (Val) == sizeof (__complex__ float)) \
174 __tgmres = Fct##f (Val); \
175 else \
176 __tgmres = __tgml(Fct) (Val); \
177 __tgmres; }))
179 /* XXX This definition has to be changed as soon as the compiler understands
180 the imaginary keyword. */
181 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
182 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
183 if ((sizeof (__real__ (Val1)) > sizeof (double) \
184 || sizeof (__real__ (Val2)) > sizeof (double)) \
185 && __builtin_classify_type (__real__ (Val1) \
186 + __real__ (Val2)) \
187 == 8) \
189 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
190 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
191 __tgmres = __tgml(Fct) (Val1, Val2); \
192 else \
193 __tgmres = __tgml(Cfct) (Val1, Val2); \
195 else if (sizeof (__real__ (Val1)) == sizeof (double) \
196 || sizeof (__real__ (Val2)) == sizeof(double) \
197 || __builtin_classify_type (__real__ (Val1) \
198 + __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_ONLY (Val, lrint)
347 #define llrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, llrint)
349 /* Round X to nearest integral value, rounding halfway cases away from
350 zero. */
351 #define lround(Val) __TGMATH_UNARY_REAL_ONLY (Val, lround)
352 #define llround(Val) __TGMATH_UNARY_REAL_ONLY (Val, 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_ONLY (Val, 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_IMAG_ONLY (Val, carg)
413 /* Complex conjugate of Z. */
414 #define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj)
416 /* Projection of Z onto the Riemann sphere. */
417 #define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj)
420 /* Decomposing complex values. */
422 /* Imaginary part of Z. */
423 #define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag)
425 /* Real part of Z. */
426 #define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal)
428 #endif /* tgmath.h */