New file.
[glibc.git] / math / tgmath.h
blob286ea56df099634941a5bb3fd92a6da38de718fe
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 /* 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 (Val1) != 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 ((Val1) + (Val2)) == 8) \
87 __tgmres = Fct##l (Val1, Val2); \
88 else if (sizeof (Val1) == sizeof (double) \
89 || sizeof (Val2) == sizeof (double) \
90 || __builtin_classify_type ((Val1) \
91 + (Val2)) != 8) \
92 __tgmres = Fct (Val1, Val2); \
93 else \
94 __tgmres = Fct##f (Val1, Val2); \
95 __tgmres; }))
97 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
98 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
99 if ((sizeof (Val1) > sizeof (double) \
100 || sizeof (Val2) > sizeof (double)) \
101 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
102 __tgmres = Fct##l (Val1, Val2, Val3); \
103 else if (sizeof (Val1) == sizeof (double) \
104 || sizeof (Val2) == sizeof (double) \
105 || __builtin_classify_type ((Val1) \
106 + (Val2)) != 8) \
107 __tgmres = Fct (Val1, Val2, Val3); \
108 else \
109 __tgmres = Fct##f (Val1, Val2, Val3); \
110 __tgmres; }))
112 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
113 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2) + (Val3)) __tgmres;\
114 if ((sizeof (Val1) > sizeof (double) \
115 || sizeof (Val2) > sizeof (double) \
116 || sizeof (Val3) > sizeof (double)) \
117 && __builtin_classify_type ((Val1) + (Val2) \
118 + (Val3)) == 8) \
119 __tgmres = Fct##l (Val1, Val2, Val3); \
120 else 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 = Fct (Val1, Val2, Val3); \
126 else \
127 __tgmres = Fct##f (Val1, Val2, Val3); \
128 __tgmres; }))
130 /* XXX This definition has to be changed as soon as the compiler understands
131 the imaginary keyword. */
132 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
133 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
134 if (sizeof (__real__ (Val)) > sizeof (double) \
135 && __builtin_classify_type (__real__ (Val)) == 8) \
137 if (sizeof (__real__ (Val)) == sizeof (Val)) \
138 __tgmres = Fct##l (Val); \
139 else \
140 __tgmres = Cfct##l (Val); \
142 else if (sizeof (__real__ (Val)) == sizeof (double) \
143 || __builtin_classify_type (__real__ (Val)) \
144 != 8) \
146 if (sizeof (__real__ (Val)) == sizeof (Val)) \
147 __tgmres = Fct (Val); \
148 else \
149 __tgmres = Cfct (Val); \
151 else \
153 if (sizeof (__real__ (Val)) == sizeof (Val)) \
154 __tgmres = Fct##f (Val); \
155 else \
156 __tgmres = Cfct##f (Val); \
158 __tgmres; }))
160 /* XXX This definition has to be changed as soon as the compiler understands
161 the imaginary keyword. */
162 # define __TGMATH_UNARY_IMAG_ONLY(Val, Fct) \
163 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
164 if (sizeof (Val) == sizeof (__complex__ double) \
165 || __builtin_classify_type (__real__ (Val)) != 8) \
166 __tgmres = Fct (Val); \
167 else if (sizeof (Val) == sizeof (__complex__ float)) \
168 __tgmres = Fct##f (Val); \
169 else \
170 __tgmres = Fct##l (Val); \
171 __tgmres; }))
173 /* XXX This definition has to be changed as soon as the compiler understands
174 the imaginary keyword. */
175 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
176 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
177 if ((sizeof (__real__ (Val1)) > sizeof (double) \
178 || sizeof (__real__ (Val2)) > sizeof (double)) \
179 && __builtin_classify_type (__real__ (Val1) \
180 + __real__ (Val2)) \
181 == 8) \
183 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
184 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
185 __tgmres = Fct##l (Val1, Val2); \
186 else \
187 __tgmres = Cfct##l (Val1, Val2); \
189 else if (sizeof (__real__ (Val1)) == sizeof (double) \
190 || sizeof (__real__ (Val2)) == sizeof(double) \
191 || __builtin_classify_type (__real__ (Val1) \
192 + __real__ (Val2))\
193 != 8) \
195 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
196 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
197 __tgmres = Fct (Val1, Val2); \
198 else \
199 __tgmres = Cfct (Val1, Val2); \
201 else \
203 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
204 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
205 __tgmres = Fct##f (Val1, Val2); \
206 else \
207 __tgmres = Cfct##f (Val1, Val2); \
209 __tgmres; }))
210 #else
211 # error "Unsupported compiler; you cannot use <tgmath.h>"
212 #endif
215 /* Unary functions defined for real and complex values. */
218 /* Trigonometric functions. */
220 /* Arc cosine of X. */
221 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
222 /* Arc sine of X. */
223 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
224 /* Arc tangent of X. */
225 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
226 /* Arc tangent of Y/X. */
227 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
229 /* Cosine of X. */
230 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
231 /* Sine of X. */
232 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
233 /* Tangent of X. */
234 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
237 /* Hyperbolic functions. */
239 /* Hyperbolic arc cosine of X. */
240 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
241 /* Hyperbolic arc sine of X. */
242 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
243 /* Hyperbolic arc tangent of X. */
244 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
246 /* Hyperbolic cosine of X. */
247 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
248 /* Hyperbolic sine of X. */
249 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
250 /* Hyperbolic tangent of X. */
251 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
254 /* Exponential and logarithmic functions. */
256 /* Exponential function of X. */
257 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
259 /* Break VALUE into a normalized fraction and an integral power of 2. */
260 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
262 /* X times (two to the EXP power). */
263 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
265 /* Natural logarithm of X. */
266 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
268 /* Base-ten logarithm of X. */
269 #ifdef __USE_GNU
270 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
271 #else
272 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
273 #endif
275 /* Return exp(X) - 1. */
276 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
278 /* Return log(1 + X). */
279 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
281 /* Return the base 2 signed integral exponent of X. */
282 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
284 /* Compute base-2 exponential of X. */
285 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
287 /* Compute base-2 logarithm of X. */
288 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
291 /* Power functions. */
293 /* Return X to the Y power. */
294 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
296 /* Return the square root of X. */
297 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
299 /* Return `sqrt(X*X + Y*Y)'. */
300 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
302 /* Return the cube root of X. */
303 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
306 /* Nearest integer, absolute value, and remainder functions. */
308 /* Smallest integral value not less than X. */
309 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
311 /* Absolute value of X. */
312 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs)
314 /* Largest integer not greater than X. */
315 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
317 /* Floating-point modulo remainder of X/Y. */
318 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
320 /* Round X to integral valuein floating-point format using current
321 rounding direction, but do not raise inexact exception. */
322 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
324 /* Round X to nearest integral value, rounding halfway cases away from
325 zero. */
326 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
328 /* Round X to the integral value in floating-point format nearest but
329 not larger in magnitude. */
330 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
332 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
333 and magnitude congruent `mod 2^n' to the magnitude of the integral
334 quotient x/y, with n >= 3. */
335 #define remquo(Val1, Val2, Val3) \
336 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
338 /* Round X to nearest integral value according to current rounding
339 direction. */
340 #define lrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, lrint)
341 #define llrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, llrint)
343 /* Round X to nearest integral value, rounding halfway cases away from
344 zero. */
345 #define lround(Val) __TGMATH_UNARY_REAL_ONLY (Val, lround)
346 #define llround(Val) __TGMATH_UNARY_REAL_ONLY (Val, llround)
349 /* Return X with its signed changed to Y's. */
350 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
352 /* Error and gamma functions. */
353 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
354 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
355 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
356 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
359 /* Return the integer nearest X in the direction of the
360 prevailing rounding mode. */
361 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
363 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
364 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
365 #define nexttoward(Val1, Val2) \
366 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
368 /* Return the remainder of integer divison X / Y with infinite precision. */
369 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
371 /* Return X times (2 to the Nth power). */
372 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
373 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
374 #endif
376 /* Return X times (2 to the Nth power). */
377 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
379 /* Return X times (2 to the Nth power). */
380 #define scalbln(Val1, Val2) \
381 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
383 /* Return the binary exponent of X, which must be nonzero. */
384 #define ilogb(Val) __TGMATH_UNARY_REAL_ONLY (Val, ilogb)
387 /* Return positive difference between X and Y. */
388 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
390 /* Return maximum numeric value from X and Y. */
391 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
393 /* Return minimum numeric value from X and Y. */
394 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
397 /* Multiply-add function computed as a ternary operation. */
398 #define fma(Val1, Val2, Val3) \
399 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
402 /* Absolute value, conjugates, and projection. */
404 /* Argument value of Z. */
405 #define carg(Val) __TGMATH_UNARY_IMAG_ONLY (Val, carg)
407 /* Complex conjugate of Z. */
408 #define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj)
410 /* Projection of Z onto the Riemann sphere. */
411 #define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj)
414 /* Decomposing complex values. */
416 /* Imaginary part of Z. */
417 #define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag)
419 /* Real part of Z. */
420 #define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal)
422 #endif /* tgmath.h */