* sysdeps/unix/sysv/linux/powerpc/powerpc64/sysdep.h
[glibc.git] / math / tgmath.h
blobf3d23f6e52fdd81df573421564c1b4bcbd5b61f4
1 /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
21 * ISO C99 Standard: 7.22 Type-generic math <tgmath.h>
24 #ifndef _TGMATH_H
25 #define _TGMATH_H 1
27 /* Include the needed headers. */
28 #include <math.h>
29 #include <complex.h>
32 /* Since `complex' is currently not really implemented in most C compilers
33 and if it is implemented, the implementations differ. This makes it
34 quite difficult to write a generic implementation of this header. We
35 do not try this for now and instead concentrate only on GNU CC. Once
36 we have more information support for other compilers might follow. */
38 #if __GNUC_PREREQ (2, 7)
40 # ifdef __NO_LONG_DOUBLE_MATH
41 # define __tgml(fct) fct
42 # else
43 # define __tgml(fct) fct ## l
44 # endif
46 /* This is ugly but unless gcc gets appropriate builtins we have to do
47 something like this. Don't ask how it works. */
49 /* 1 if 'type' is a floating type, 0 if 'type' is an integer type.
50 Allows for _Bool. Expands to an integer constant expression. */
51 # define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
53 /* The tgmath real type for T, where E is 0 if T is an integer type and
54 1 for a floating type. */
55 # define __tgmath_real_type_sub(T, E) \
56 __typeof__(*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
57 : (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
59 /* The tgmath real type of EXPR. */
60 # define __tgmath_real_type(expr) \
61 __tgmath_real_type_sub(__typeof__(expr), __floating_type(__typeof__(expr)))
64 /* We have two kinds of generic macros: to support functions which are
65 only defined on real valued parameters and those which are defined
66 for complex functions as well. */
67 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
68 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
69 if (sizeof (Val) == sizeof (double) \
70 || __builtin_classify_type (Val) != 8) \
71 __tgmres = Fct (Val); \
72 else if (sizeof (Val) == sizeof (float)) \
73 __tgmres = Fct##f (Val); \
74 else \
75 __tgmres = __tgml(Fct) (Val); \
76 __tgmres; }))
78 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, RetType, Fct) \
79 (__extension__ ({ RetType __tgmres; \
80 if (sizeof (Val) == sizeof (double) \
81 || __builtin_classify_type (Val) != 8) \
82 __tgmres = Fct (Val); \
83 else if (sizeof (Val) == sizeof (float)) \
84 __tgmres = Fct##f (Val); \
85 else \
86 __tgmres = __tgml(Fct) (Val); \
87 __tgmres; }))
89 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
90 (__extension__ ({ __tgmath_real_type (Val1) __tgmres; \
91 if (sizeof (Val1) == sizeof (double) \
92 || __builtin_classify_type (Val1) != 8) \
93 __tgmres = Fct (Val1, Val2); \
94 else if (sizeof (Val1) == sizeof (float)) \
95 __tgmres = Fct##f (Val1, Val2); \
96 else \
97 __tgmres = __tgml(Fct) (Val1, Val2); \
98 __tgmres; }))
100 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
101 (__extension__ ({ __typeof((__tgmath_real_type (Val1)) 0 \
102 + (__tgmath_real_type (Val2)) 0) __tgmres; \
103 if ((sizeof (Val1) > sizeof (double) \
104 || sizeof (Val2) > sizeof (double)) \
105 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
106 __tgmres = __tgml(Fct) (Val1, Val2); \
107 else if (sizeof (Val1) == sizeof (double) \
108 || sizeof (Val2) == sizeof (double) \
109 || __builtin_classify_type (Val1) != 8 \
110 || __builtin_classify_type (Val2) != 8) \
111 __tgmres = Fct (Val1, Val2); \
112 else \
113 __tgmres = Fct##f (Val1, Val2); \
114 __tgmres; }))
116 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
117 (__extension__ ({ __typeof((__tgmath_real_type (Val1)) 0 \
118 + (__tgmath_real_type (Val2)) 0) __tgmres; \
119 if ((sizeof (Val1) > sizeof (double) \
120 || sizeof (Val2) > sizeof (double)) \
121 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
122 __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
123 else if (sizeof (Val1) == sizeof (double) \
124 || sizeof (Val2) == sizeof (double) \
125 || __builtin_classify_type (Val1) != 8 \
126 || __builtin_classify_type (Val2) != 8) \
127 __tgmres = Fct (Val1, Val2, Val3); \
128 else \
129 __tgmres = Fct##f (Val1, Val2, Val3); \
130 __tgmres; }))
132 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
133 (__extension__ ({ __typeof((__tgmath_real_type (Val1)) 0 \
134 + (__tgmath_real_type (Val2)) 0 \
135 + (__tgmath_real_type (Val3)) 0) __tgmres; \
136 if ((sizeof (Val1) > sizeof (double) \
137 || sizeof (Val2) > sizeof (double) \
138 || sizeof (Val3) > sizeof (double)) \
139 && __builtin_classify_type ((Val1) + (Val2) \
140 + (Val3)) == 8) \
141 __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
142 else if (sizeof (Val1) == sizeof (double) \
143 || sizeof (Val2) == sizeof (double) \
144 || sizeof (Val3) == sizeof (double) \
145 || __builtin_classify_type (Val1) != 8 \
146 || __builtin_classify_type (Val2) != 8 \
147 || __builtin_classify_type (Val3) != 8) \
148 __tgmres = Fct (Val1, Val2, Val3); \
149 else \
150 __tgmres = Fct##f (Val1, Val2, Val3); \
151 __tgmres; }))
153 /* XXX This definition has to be changed as soon as the compiler understands
154 the imaginary keyword. */
155 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
156 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
157 if (sizeof (__real__ (Val)) > sizeof (double) \
158 && __builtin_classify_type (__real__ (Val)) == 8) \
160 if (sizeof (__real__ (Val)) == sizeof (Val)) \
161 __tgmres = __tgml(Fct) (Val); \
162 else \
163 __tgmres = __tgml(Cfct) (Val); \
165 else if (sizeof (__real__ (Val)) == sizeof (double) \
166 || __builtin_classify_type (__real__ (Val)) \
167 != 8) \
169 if (sizeof (__real__ (Val)) == sizeof (Val)) \
170 __tgmres = Fct (Val); \
171 else \
172 __tgmres = Cfct (Val); \
174 else \
176 if (sizeof (__real__ (Val)) == sizeof (Val)) \
177 __tgmres = Fct##f (Val); \
178 else \
179 __tgmres = Cfct##f (Val); \
181 __tgmres; }))
183 /* XXX This definition has to be changed as soon as the compiler understands
184 the imaginary keyword. */
185 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
186 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
187 if (sizeof (__real__ (Val)) > sizeof (double) \
188 && __builtin_classify_type (__real__ (Val)) == 8) \
190 if (sizeof (__real__ (Val)) == sizeof (Val)) \
191 __tgmres = __tgml(Fct) (Val); \
192 else \
193 __tgmres = __tgml(Cfct) (Val); \
195 else if (sizeof (__real__ (Val)) == sizeof (double) \
196 || __builtin_classify_type (__real__ (Val)) \
197 != 8) \
199 if (sizeof (__real__ (Val)) == sizeof (Val)) \
200 __tgmres = Fct (Val); \
201 else \
202 __tgmres = Cfct (Val); \
204 else \
206 if (sizeof (__real__ (Val)) == sizeof (Val)) \
207 __tgmres = Fct##f (Val); \
208 else \
209 __tgmres = Cfct##f (Val); \
211 __real__ __tgmres; }))
213 /* XXX This definition has to be changed as soon as the compiler understands
214 the imaginary keyword. */
215 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
216 (__extension__ ({ __typeof((__tgmath_real_type (Val1)) 0 \
217 + (__tgmath_real_type (Val2)) 0) __tgmres; \
218 if ((sizeof (__real__ (Val1)) > sizeof (double) \
219 || sizeof (__real__ (Val2)) > sizeof (double)) \
220 && __builtin_classify_type (__real__ (Val1) \
221 + __real__ (Val2)) \
222 == 8) \
224 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
225 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
226 __tgmres = __tgml(Fct) (Val1, Val2); \
227 else \
228 __tgmres = __tgml(Cfct) (Val1, Val2); \
230 else if (sizeof (__real__ (Val1)) == sizeof (double) \
231 || sizeof (__real__ (Val2)) == sizeof(double) \
232 || (__builtin_classify_type (__real__ (Val1)) \
233 != 8) \
234 || (__builtin_classify_type (__real__ (Val2)) \
235 != 8)) \
237 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
238 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
239 __tgmres = Fct (Val1, Val2); \
240 else \
241 __tgmres = Cfct (Val1, Val2); \
243 else \
245 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
246 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
247 __tgmres = Fct##f (Val1, Val2); \
248 else \
249 __tgmres = Cfct##f (Val1, Val2); \
251 __tgmres; }))
252 #else
253 # error "Unsupported compiler; you cannot use <tgmath.h>"
254 #endif
257 /* Unary functions defined for real and complex values. */
260 /* Trigonometric functions. */
262 /* Arc cosine of X. */
263 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
264 /* Arc sine of X. */
265 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
266 /* Arc tangent of X. */
267 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
268 /* Arc tangent of Y/X. */
269 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
271 /* Cosine of X. */
272 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
273 /* Sine of X. */
274 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
275 /* Tangent of X. */
276 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
279 /* Hyperbolic functions. */
281 /* Hyperbolic arc cosine of X. */
282 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
283 /* Hyperbolic arc sine of X. */
284 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
285 /* Hyperbolic arc tangent of X. */
286 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
288 /* Hyperbolic cosine of X. */
289 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
290 /* Hyperbolic sine of X. */
291 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
292 /* Hyperbolic tangent of X. */
293 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
296 /* Exponential and logarithmic functions. */
298 /* Exponential function of X. */
299 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
301 /* Break VALUE into a normalized fraction and an integral power of 2. */
302 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
304 /* X times (two to the EXP power). */
305 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
307 /* Natural logarithm of X. */
308 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
310 /* Base-ten logarithm of X. */
311 #ifdef __USE_GNU
312 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
313 #else
314 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
315 #endif
317 /* Return exp(X) - 1. */
318 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
320 /* Return log(1 + X). */
321 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
323 /* Return the base 2 signed integral exponent of X. */
324 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
326 /* Compute base-2 exponential of X. */
327 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
329 /* Compute base-2 logarithm of X. */
330 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
333 /* Power functions. */
335 /* Return X to the Y power. */
336 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
338 /* Return the square root of X. */
339 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
341 /* Return `sqrt(X*X + Y*Y)'. */
342 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
344 /* Return the cube root of X. */
345 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
348 /* Nearest integer, absolute value, and remainder functions. */
350 /* Smallest integral value not less than X. */
351 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
353 /* Absolute value of X. */
354 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, fabs, cabs)
356 /* Largest integer not greater than X. */
357 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
359 /* Floating-point modulo remainder of X/Y. */
360 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
362 /* Round X to integral valuein floating-point format using current
363 rounding direction, but do not raise inexact exception. */
364 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
366 /* Round X to nearest integral value, rounding halfway cases away from
367 zero. */
368 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
370 /* Round X to the integral value in floating-point format nearest but
371 not larger in magnitude. */
372 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
374 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
375 and magnitude congruent `mod 2^n' to the magnitude of the integral
376 quotient x/y, with n >= 3. */
377 #define remquo(Val1, Val2, Val3) \
378 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
380 /* Round X to nearest integral value according to current rounding
381 direction. */
382 #define lrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lrint)
383 #define llrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llrint)
385 /* Round X to nearest integral value, rounding halfway cases away from
386 zero. */
387 #define lround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lround)
388 #define llround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llround)
391 /* Return X with its signed changed to Y's. */
392 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
394 /* Error and gamma functions. */
395 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
396 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
397 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
398 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
401 /* Return the integer nearest X in the direction of the
402 prevailing rounding mode. */
403 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
405 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
406 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
407 #define nexttoward(Val1, Val2) \
408 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
410 /* Return the remainder of integer divison X / Y with infinite precision. */
411 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
413 /* Return X times (2 to the Nth power). */
414 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
415 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
416 #endif
418 /* Return X times (2 to the Nth power). */
419 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
421 /* Return X times (2 to the Nth power). */
422 #define scalbln(Val1, Val2) \
423 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
425 /* Return the binary exponent of X, which must be nonzero. */
426 #define ilogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, int, ilogb)
429 /* Return positive difference between X and Y. */
430 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
432 /* Return maximum numeric value from X and Y. */
433 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
435 /* Return minimum numeric value from X and Y. */
436 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
439 /* Multiply-add function computed as a ternary operation. */
440 #define fma(Val1, Val2, Val3) \
441 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
444 /* Absolute value, conjugates, and projection. */
446 /* Argument value of Z. */
447 #define carg(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, carg, carg)
449 /* Complex conjugate of Z. */
450 #define conj(Val) __TGMATH_UNARY_REAL_IMAG (Val, conj, conj)
452 /* Projection of Z onto the Riemann sphere. */
453 #define cproj(Val) __TGMATH_UNARY_REAL_IMAG (Val, cproj, cproj)
456 /* Decomposing complex values. */
458 /* Imaginary part of Z. */
459 #define cimag(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, cimag, cimag)
461 /* Real part of Z. */
462 #define creal(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, creal, creal)
464 #endif /* tgmath.h */