Update powerpc-fpu libm-test-ulps.
[glibc.git] / math / tgmath.h
blob3e1fb0207a445378f5a1daa03436f77116a0a590
1 /* Copyright (C) 1997-2015 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, see
16 <http://www.gnu.org/licenses/>. */
19 * ISO C99 Standard: 7.22 Type-generic math <tgmath.h>
22 #ifndef _TGMATH_H
23 #define _TGMATH_H 1
25 /* Include the needed headers. */
26 #include <math.h>
27 #include <complex.h>
30 /* Since `complex' is currently not really implemented in most C compilers
31 and if it is implemented, the implementations differ. This makes it
32 quite difficult to write a generic implementation of this header. We
33 do not try this for now and instead concentrate only on GNU CC. Once
34 we have more information support for other compilers might follow. */
36 #if __GNUC_PREREQ (2, 7)
38 # ifdef __NO_LONG_DOUBLE_MATH
39 # define __tgml(fct) fct
40 # else
41 # define __tgml(fct) fct ## l
42 # endif
44 /* This is ugly but unless gcc gets appropriate builtins we have to do
45 something like this. Don't ask how it works. */
47 /* 1 if 'type' is a floating type, 0 if 'type' is an integer type.
48 Allows for _Bool. Expands to an integer constant expression. */
49 # if __GNUC_PREREQ (3, 1)
50 # define __floating_type(type) \
51 (__builtin_classify_type ((type) 0) == 8 \
52 || (__builtin_classify_type ((type) 0) == 9 \
53 && __builtin_classify_type (__real__ ((type) 0)) == 8))
54 # else
55 # define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
56 # endif
58 /* The tgmath real type for T, where E is 0 if T is an integer type and
59 1 for a floating type. */
60 # define __tgmath_real_type_sub(T, E) \
61 __typeof__ (*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
62 : (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
64 /* The tgmath real type of EXPR. */
65 # define __tgmath_real_type(expr) \
66 __tgmath_real_type_sub (__typeof__ ((__typeof__ (expr)) 0), \
67 __floating_type (__typeof__ (expr)))
70 /* We have two kinds of generic macros: to support functions which are
71 only defined on real valued parameters and those which are defined
72 for complex functions as well. */
73 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
74 (__extension__ ((sizeof (Val) == sizeof (double) \
75 || __builtin_classify_type (Val) != 8) \
76 ? (__tgmath_real_type (Val)) Fct (Val) \
77 : (sizeof (Val) == sizeof (float)) \
78 ? (__tgmath_real_type (Val)) Fct##f (Val) \
79 : (__tgmath_real_type (Val)) __tgml(Fct) (Val)))
81 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, RetType, Fct) \
82 (__extension__ ((sizeof (Val) == sizeof (double) \
83 || __builtin_classify_type (Val) != 8) \
84 ? (RetType) Fct (Val) \
85 : (sizeof (Val) == sizeof (float)) \
86 ? (RetType) Fct##f (Val) \
87 : (RetType) __tgml(Fct) (Val)))
89 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
90 (__extension__ ((sizeof (Val1) == sizeof (double) \
91 || __builtin_classify_type (Val1) != 8) \
92 ? (__tgmath_real_type (Val1)) Fct (Val1, Val2) \
93 : (sizeof (Val1) == sizeof (float)) \
94 ? (__tgmath_real_type (Val1)) Fct##f (Val1, Val2) \
95 : (__tgmath_real_type (Val1)) __tgml(Fct) (Val1, Val2)))
97 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
98 (__extension__ (((sizeof (Val1) > sizeof (double) \
99 || sizeof (Val2) > sizeof (double)) \
100 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
101 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
102 + (__tgmath_real_type (Val2)) 0)) \
103 __tgml(Fct) (Val1, Val2) \
104 : (sizeof (Val1) == sizeof (double) \
105 || sizeof (Val2) == sizeof (double) \
106 || __builtin_classify_type (Val1) != 8 \
107 || __builtin_classify_type (Val2) != 8) \
108 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
109 + (__tgmath_real_type (Val2)) 0)) \
110 Fct (Val1, Val2) \
111 : (__typeof ((__tgmath_real_type (Val1)) 0 \
112 + (__tgmath_real_type (Val2)) 0)) \
113 Fct##f (Val1, Val2)))
115 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
116 (__extension__ (((sizeof (Val1) > sizeof (double) \
117 || sizeof (Val2) > sizeof (double)) \
118 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
119 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
120 + (__tgmath_real_type (Val2)) 0)) \
121 __tgml(Fct) (Val1, Val2, Val3) \
122 : (sizeof (Val1) == sizeof (double) \
123 || sizeof (Val2) == sizeof (double) \
124 || __builtin_classify_type (Val1) != 8 \
125 || __builtin_classify_type (Val2) != 8) \
126 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
127 + (__tgmath_real_type (Val2)) 0)) \
128 Fct (Val1, Val2, Val3) \
129 : (__typeof ((__tgmath_real_type (Val1)) 0 \
130 + (__tgmath_real_type (Val2)) 0)) \
131 Fct##f (Val1, Val2, Val3)))
133 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
134 (__extension__ (((sizeof (Val1) > sizeof (double) \
135 || sizeof (Val2) > sizeof (double) \
136 || sizeof (Val3) > sizeof (double)) \
137 && __builtin_classify_type ((Val1) + (Val2) + (Val3)) \
138 == 8) \
139 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
140 + (__tgmath_real_type (Val2)) 0 \
141 + (__tgmath_real_type (Val3)) 0)) \
142 __tgml(Fct) (Val1, Val2, Val3) \
143 : (sizeof (Val1) == sizeof (double) \
144 || sizeof (Val2) == sizeof (double) \
145 || sizeof (Val3) == sizeof (double) \
146 || __builtin_classify_type (Val1) != 8 \
147 || __builtin_classify_type (Val2) != 8 \
148 || __builtin_classify_type (Val3) != 8) \
149 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
150 + (__tgmath_real_type (Val2)) 0 \
151 + (__tgmath_real_type (Val3)) 0)) \
152 Fct (Val1, Val2, Val3) \
153 : (__typeof ((__tgmath_real_type (Val1)) 0 \
154 + (__tgmath_real_type (Val2)) 0 \
155 + (__tgmath_real_type (Val3)) 0)) \
156 Fct##f (Val1, Val2, Val3)))
158 /* XXX This definition has to be changed as soon as the compiler understands
159 the imaginary keyword. */
160 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
161 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
162 || __builtin_classify_type (__real__ (Val)) != 8) \
163 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
164 ? (__tgmath_real_type (Val)) Fct (Val) \
165 : (__tgmath_real_type (Val)) Cfct (Val)) \
166 : (sizeof (__real__ (Val)) == sizeof (float)) \
167 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
168 ? (__tgmath_real_type (Val)) Fct##f (Val) \
169 : (__tgmath_real_type (Val)) Cfct##f (Val)) \
170 : ((sizeof (__real__ (Val)) == sizeof (Val)) \
171 ? (__tgmath_real_type (Val)) __tgml(Fct) (Val) \
172 : (__tgmath_real_type (Val)) __tgml(Cfct) (Val))))
174 # define __TGMATH_UNARY_IMAG(Val, Cfct) \
175 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
176 || __builtin_classify_type (__real__ (Val)) != 8) \
177 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
178 + _Complex_I)) Cfct (Val) \
179 : (sizeof (__real__ (Val)) == sizeof (float)) \
180 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
181 + _Complex_I)) Cfct##f (Val) \
182 : (__typeof__ ((__tgmath_real_type (Val)) 0 \
183 + _Complex_I)) __tgml(Cfct) (Val)))
185 /* XXX This definition has to be changed as soon as the compiler understands
186 the imaginary keyword. */
187 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
188 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
189 || __builtin_classify_type (__real__ (Val)) != 8) \
190 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
191 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
192 Fct (Val) \
193 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
194 Cfct (Val)) \
195 : (sizeof (__real__ (Val)) == sizeof (float)) \
196 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
197 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
198 Fct##f (Val) \
199 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
200 Cfct##f (Val)) \
201 : ((sizeof (__real__ (Val)) == sizeof (Val)) \
202 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
203 __tgml(Fct) (Val) \
204 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
205 __tgml(Cfct) (Val))))
207 /* XXX This definition has to be changed as soon as the compiler understands
208 the imaginary keyword. */
209 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
210 (__extension__ (((sizeof (__real__ (Val1)) > sizeof (double) \
211 || sizeof (__real__ (Val2)) > sizeof (double)) \
212 && __builtin_classify_type (__real__ (Val1) \
213 + __real__ (Val2)) == 8) \
214 ? ((sizeof (__real__ (Val1)) == sizeof (Val1) \
215 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
216 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
217 + (__tgmath_real_type (Val2)) 0)) \
218 __tgml(Fct) (Val1, Val2) \
219 : (__typeof ((__tgmath_real_type (Val1)) 0 \
220 + (__tgmath_real_type (Val2)) 0)) \
221 __tgml(Cfct) (Val1, Val2)) \
222 : (sizeof (__real__ (Val1)) == sizeof (double) \
223 || sizeof (__real__ (Val2)) == sizeof (double) \
224 || __builtin_classify_type (__real__ (Val1)) != 8 \
225 || __builtin_classify_type (__real__ (Val2)) != 8) \
226 ? ((sizeof (__real__ (Val1)) == sizeof (Val1) \
227 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
228 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
229 + (__tgmath_real_type (Val2)) 0)) \
230 Fct (Val1, Val2) \
231 : (__typeof ((__tgmath_real_type (Val1)) 0 \
232 + (__tgmath_real_type (Val2)) 0)) \
233 Cfct (Val1, Val2)) \
234 : ((sizeof (__real__ (Val1)) == sizeof (Val1) \
235 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
236 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
237 + (__tgmath_real_type (Val2)) 0)) \
238 Fct##f (Val1, Val2) \
239 : (__typeof ((__tgmath_real_type (Val1)) 0 \
240 + (__tgmath_real_type (Val2)) 0)) \
241 Cfct##f (Val1, Val2))))
242 #else
243 # error "Unsupported compiler; you cannot use <tgmath.h>"
244 #endif
247 /* Unary functions defined for real and complex values. */
250 /* Trigonometric functions. */
252 /* Arc cosine of X. */
253 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
254 /* Arc sine of X. */
255 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
256 /* Arc tangent of X. */
257 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
258 /* Arc tangent of Y/X. */
259 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
261 /* Cosine of X. */
262 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
263 /* Sine of X. */
264 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
265 /* Tangent of X. */
266 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
269 /* Hyperbolic functions. */
271 /* Hyperbolic arc cosine of X. */
272 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
273 /* Hyperbolic arc sine of X. */
274 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
275 /* Hyperbolic arc tangent of X. */
276 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
278 /* Hyperbolic cosine of X. */
279 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
280 /* Hyperbolic sine of X. */
281 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
282 /* Hyperbolic tangent of X. */
283 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
286 /* Exponential and logarithmic functions. */
288 /* Exponential function of X. */
289 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
291 /* Break VALUE into a normalized fraction and an integral power of 2. */
292 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
294 /* X times (two to the EXP power). */
295 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
297 /* Natural logarithm of X. */
298 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
300 /* Base-ten logarithm of X. */
301 #ifdef __USE_GNU
302 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
303 #else
304 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
305 #endif
307 /* Return exp(X) - 1. */
308 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
310 /* Return log(1 + X). */
311 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
313 /* Return the base 2 signed integral exponent of X. */
314 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
316 /* Compute base-2 exponential of X. */
317 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
319 /* Compute base-2 logarithm of X. */
320 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
323 /* Power functions. */
325 /* Return X to the Y power. */
326 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
328 /* Return the square root of X. */
329 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
331 /* Return `sqrt(X*X + Y*Y)'. */
332 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
334 /* Return the cube root of X. */
335 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
338 /* Nearest integer, absolute value, and remainder functions. */
340 /* Smallest integral value not less than X. */
341 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
343 /* Absolute value of X. */
344 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, fabs, cabs)
346 /* Largest integer not greater than X. */
347 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
349 /* Floating-point modulo remainder of X/Y. */
350 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
352 /* Round X to integral valuein floating-point format using current
353 rounding direction, but do not raise inexact exception. */
354 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
356 /* Round X to nearest integral value, rounding halfway cases away from
357 zero. */
358 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
360 /* Round X to the integral value in floating-point format nearest but
361 not larger in magnitude. */
362 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
364 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
365 and magnitude congruent `mod 2^n' to the magnitude of the integral
366 quotient x/y, with n >= 3. */
367 #define remquo(Val1, Val2, Val3) \
368 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
370 /* Round X to nearest integral value according to current rounding
371 direction. */
372 #define lrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lrint)
373 #define llrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llrint)
375 /* Round X to nearest integral value, rounding halfway cases away from
376 zero. */
377 #define lround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lround)
378 #define llround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llround)
381 /* Return X with its signed changed to Y's. */
382 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
384 /* Error and gamma functions. */
385 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
386 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
387 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
388 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
391 /* Return the integer nearest X in the direction of the
392 prevailing rounding mode. */
393 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
395 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
396 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
397 #define nexttoward(Val1, Val2) \
398 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
400 /* Return the remainder of integer divison X / Y with infinite precision. */
401 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
403 /* Return X times (2 to the Nth power). */
404 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
405 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
406 #endif
408 /* Return X times (2 to the Nth power). */
409 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
411 /* Return X times (2 to the Nth power). */
412 #define scalbln(Val1, Val2) \
413 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
415 /* Return the binary exponent of X, which must be nonzero. */
416 #define ilogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, int, ilogb)
419 /* Return positive difference between X and Y. */
420 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
422 /* Return maximum numeric value from X and Y. */
423 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
425 /* Return minimum numeric value from X and Y. */
426 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
429 /* Multiply-add function computed as a ternary operation. */
430 #define fma(Val1, Val2, Val3) \
431 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
434 /* Absolute value, conjugates, and projection. */
436 /* Argument value of Z. */
437 #define carg(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, carg, carg)
439 /* Complex conjugate of Z. */
440 #define conj(Val) __TGMATH_UNARY_IMAG (Val, conj)
442 /* Projection of Z onto the Riemann sphere. */
443 #define cproj(Val) __TGMATH_UNARY_IMAG (Val, cproj)
446 /* Decomposing complex values. */
448 /* Imaginary part of Z. */
449 #define cimag(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, cimag, cimag)
451 /* Real part of Z. */
452 #define creal(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, creal, creal)
454 #endif /* tgmath.h */