Add cfi directives.
[glibc/history.git] / math / tgmath.h
blob4f45aaa0f2bb00dcfb96308357214394f1cf3215
1 /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2007
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__ ((__typeof__ (expr)) 0), \
62 __floating_type (__typeof__ (expr)))
65 /* We have two kinds of generic macros: to support functions which are
66 only defined on real valued parameters and those which are defined
67 for complex functions as well. */
68 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
69 (__extension__ ((sizeof (Val) == sizeof (double) \
70 || __builtin_classify_type (Val) != 8) \
71 ? (__tgmath_real_type (Val)) Fct (Val) \
72 : (sizeof (Val) == sizeof (float)) \
73 ? (__tgmath_real_type (Val)) Fct##f (Val) \
74 : (__tgmath_real_type (Val)) __tgml(Fct) (Val)))
76 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, RetType, Fct) \
77 (__extension__ ((sizeof (Val) == sizeof (double) \
78 || __builtin_classify_type (Val) != 8) \
79 ? (RetType) Fct (Val) \
80 : (sizeof (Val) == sizeof (float)) \
81 ? (RetType) Fct##f (Val) \
82 : (RetType) __tgml(Fct) (Val)))
84 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
85 (__extension__ ((sizeof (Val1) == sizeof (double) \
86 || __builtin_classify_type (Val1) != 8) \
87 ? (__tgmath_real_type (Val1)) Fct (Val1, Val2) \
88 : (sizeof (Val1) == sizeof (float)) \
89 ? (__tgmath_real_type (Val1)) Fct##f (Val1, Val2) \
90 : (__tgmath_real_type (Val1)) __tgml(Fct) (Val1, Val2)))
92 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
93 (__extension__ (((sizeof (Val1) > sizeof (double) \
94 || sizeof (Val2) > sizeof (double)) \
95 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
96 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
97 + (__tgmath_real_type (Val2)) 0)) \
98 __tgml(Fct) (Val1, Val2) \
99 : (sizeof (Val1) == sizeof (double) \
100 || sizeof (Val2) == sizeof (double) \
101 || __builtin_classify_type (Val1) != 8 \
102 || __builtin_classify_type (Val2) != 8) \
103 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
104 + (__tgmath_real_type (Val2)) 0)) \
105 Fct (Val1, Val2) \
106 : (__typeof ((__tgmath_real_type (Val1)) 0 \
107 + (__tgmath_real_type (Val2)) 0)) \
108 Fct##f (Val1, Val2)))
110 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
111 (__extension__ (((sizeof (Val1) > sizeof (double) \
112 || sizeof (Val2) > sizeof (double)) \
113 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
114 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
115 + (__tgmath_real_type (Val2)) 0)) \
116 __tgml(Fct) (Val1, Val2, Val3) \
117 : (sizeof (Val1) == sizeof (double) \
118 || sizeof (Val2) == sizeof (double) \
119 || __builtin_classify_type (Val1) != 8 \
120 || __builtin_classify_type (Val2) != 8) \
121 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
122 + (__tgmath_real_type (Val2)) 0)) \
123 Fct (Val1, Val2, Val3) \
124 : (__typeof ((__tgmath_real_type (Val1)) 0 \
125 + (__tgmath_real_type (Val2)) 0)) \
126 Fct##f (Val1, Val2, Val3)))
128 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
129 (__extension__ (((sizeof (Val1) > sizeof (double) \
130 || sizeof (Val2) > sizeof (double) \
131 || sizeof (Val3) > sizeof (double)) \
132 && __builtin_classify_type ((Val1) + (Val2) + (Val3)) \
133 == 8) \
134 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
135 + (__tgmath_real_type (Val2)) 0 \
136 + (__tgmath_real_type (Val3)) 0)) \
137 __tgml(Fct) (Val1, Val2, Val3) \
138 : (sizeof (Val1) == sizeof (double) \
139 || sizeof (Val2) == sizeof (double) \
140 || sizeof (Val3) == sizeof (double) \
141 || __builtin_classify_type (Val1) != 8 \
142 || __builtin_classify_type (Val2) != 8 \
143 || __builtin_classify_type (Val3) != 8) \
144 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
145 + (__tgmath_real_type (Val2)) 0 \
146 + (__tgmath_real_type (Val3)) 0)) \
147 Fct (Val1, Val2, Val3) \
148 : (__typeof ((__tgmath_real_type (Val1)) 0 \
149 + (__tgmath_real_type (Val2)) 0 \
150 + (__tgmath_real_type (Val3)) 0)) \
151 Fct##f (Val1, Val2, Val3)))
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__ ((sizeof (__real__ (Val)) == sizeof (double) \
157 || __builtin_classify_type (__real__ (Val)) != 8) \
158 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
159 ? (__tgmath_real_type (Val)) Fct (Val) \
160 : (__tgmath_real_type (Val)) Cfct (Val)) \
161 : (sizeof (__real__ (Val)) == sizeof (float)) \
162 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
163 ? (__tgmath_real_type (Val)) Fct##f (Val) \
164 : (__tgmath_real_type (Val)) Cfct##f (Val)) \
165 : ((sizeof (__real__ (Val)) == sizeof (Val)) \
166 ? (__tgmath_real_type (Val)) __tgml(Fct) (Val) \
167 : (__tgmath_real_type (Val)) __tgml(Cfct) (Val))))
169 # define __TGMATH_UNARY_IMAG(Val, Cfct) \
170 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
171 || __builtin_classify_type (__real__ (Val)) != 8) \
172 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
173 + _Complex_I)) Cfct (Val) \
174 : (sizeof (__real__ (Val)) == sizeof (float)) \
175 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
176 + _Complex_I)) Cfct##f (Val) \
177 : (__typeof__ ((__tgmath_real_type (Val)) 0 \
178 + _Complex_I)) __tgml(Cfct) (Val)))
180 /* XXX This definition has to be changed as soon as the compiler understands
181 the imaginary keyword. */
182 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
183 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
184 || __builtin_classify_type (__real__ (Val)) != 8) \
185 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
186 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
187 Fct (Val) \
188 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
189 Cfct (Val)) \
190 : (sizeof (__real__ (Val)) == sizeof (float)) \
191 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
192 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
193 Fct##f (Val) \
194 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
195 Cfct##f (Val)) \
196 : ((sizeof (__real__ (Val)) == sizeof (Val)) \
197 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
198 __tgml(Fct) (Val) \
199 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
200 __tgml(Cfct) (Val))))
202 /* XXX This definition has to be changed as soon as the compiler understands
203 the imaginary keyword. */
204 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
205 (__extension__ (((sizeof (__real__ (Val1)) > sizeof (double) \
206 || sizeof (__real__ (Val2)) > sizeof (double)) \
207 && __builtin_classify_type (__real__ (Val1) \
208 + __real__ (Val2)) == 8) \
209 ? ((sizeof (__real__ (Val1)) == sizeof (Val1) \
210 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
211 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
212 + (__tgmath_real_type (Val2)) 0)) \
213 __tgml(Fct) (Val1, Val2) \
214 : (__typeof ((__tgmath_real_type (Val1)) 0 \
215 + (__tgmath_real_type (Val2)) 0)) \
216 __tgml(Cfct) (Val1, Val2)) \
217 : (sizeof (__real__ (Val1)) == sizeof (double) \
218 || sizeof (__real__ (Val2)) == sizeof (double) \
219 || __builtin_classify_type (__real__ (Val1)) != 8 \
220 || __builtin_classify_type (__real__ (Val2)) != 8) \
221 ? ((sizeof (__real__ (Val1)) == sizeof (Val1) \
222 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
223 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
224 + (__tgmath_real_type (Val2)) 0)) \
225 Fct (Val1, Val2) \
226 : (__typeof ((__tgmath_real_type (Val1)) 0 \
227 + (__tgmath_real_type (Val2)) 0)) \
228 Cfct (Val1, Val2)) \
229 : ((sizeof (__real__ (Val1)) == sizeof (Val1) \
230 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
231 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
232 + (__tgmath_real_type (Val2)) 0)) \
233 Fct##f (Val1, Val2) \
234 : (__typeof ((__tgmath_real_type (Val1)) 0 \
235 + (__tgmath_real_type (Val2)) 0)) \
236 Cfct##f (Val1, Val2))))
237 #else
238 # error "Unsupported compiler; you cannot use <tgmath.h>"
239 #endif
242 /* Unary functions defined for real and complex values. */
245 /* Trigonometric functions. */
247 /* Arc cosine of X. */
248 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
249 /* Arc sine of X. */
250 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
251 /* Arc tangent of X. */
252 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
253 /* Arc tangent of Y/X. */
254 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
256 /* Cosine of X. */
257 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
258 /* Sine of X. */
259 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
260 /* Tangent of X. */
261 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
264 /* Hyperbolic functions. */
266 /* Hyperbolic arc cosine of X. */
267 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
268 /* Hyperbolic arc sine of X. */
269 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
270 /* Hyperbolic arc tangent of X. */
271 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
273 /* Hyperbolic cosine of X. */
274 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
275 /* Hyperbolic sine of X. */
276 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
277 /* Hyperbolic tangent of X. */
278 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
281 /* Exponential and logarithmic functions. */
283 /* Exponential function of X. */
284 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
286 /* Break VALUE into a normalized fraction and an integral power of 2. */
287 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
289 /* X times (two to the EXP power). */
290 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
292 /* Natural logarithm of X. */
293 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
295 /* Base-ten logarithm of X. */
296 #ifdef __USE_GNU
297 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
298 #else
299 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
300 #endif
302 /* Return exp(X) - 1. */
303 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
305 /* Return log(1 + X). */
306 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
308 /* Return the base 2 signed integral exponent of X. */
309 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
311 /* Compute base-2 exponential of X. */
312 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
314 /* Compute base-2 logarithm of X. */
315 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
318 /* Power functions. */
320 /* Return X to the Y power. */
321 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
323 /* Return the square root of X. */
324 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
326 /* Return `sqrt(X*X + Y*Y)'. */
327 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
329 /* Return the cube root of X. */
330 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
333 /* Nearest integer, absolute value, and remainder functions. */
335 /* Smallest integral value not less than X. */
336 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
338 /* Absolute value of X. */
339 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, fabs, cabs)
341 /* Largest integer not greater than X. */
342 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
344 /* Floating-point modulo remainder of X/Y. */
345 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
347 /* Round X to integral valuein floating-point format using current
348 rounding direction, but do not raise inexact exception. */
349 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
351 /* Round X to nearest integral value, rounding halfway cases away from
352 zero. */
353 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
355 /* Round X to the integral value in floating-point format nearest but
356 not larger in magnitude. */
357 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
359 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
360 and magnitude congruent `mod 2^n' to the magnitude of the integral
361 quotient x/y, with n >= 3. */
362 #define remquo(Val1, Val2, Val3) \
363 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
365 /* Round X to nearest integral value according to current rounding
366 direction. */
367 #define lrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lrint)
368 #define llrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llrint)
370 /* Round X to nearest integral value, rounding halfway cases away from
371 zero. */
372 #define lround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lround)
373 #define llround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llround)
376 /* Return X with its signed changed to Y's. */
377 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
379 /* Error and gamma functions. */
380 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
381 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
382 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
383 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
386 /* Return the integer nearest X in the direction of the
387 prevailing rounding mode. */
388 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
390 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
391 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
392 #define nexttoward(Val1, Val2) \
393 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
395 /* Return the remainder of integer divison X / Y with infinite precision. */
396 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
398 /* Return X times (2 to the Nth power). */
399 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
400 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
401 #endif
403 /* Return X times (2 to the Nth power). */
404 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
406 /* Return X times (2 to the Nth power). */
407 #define scalbln(Val1, Val2) \
408 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
410 /* Return the binary exponent of X, which must be nonzero. */
411 #define ilogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, int, ilogb)
414 /* Return positive difference between X and Y. */
415 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
417 /* Return maximum numeric value from X and Y. */
418 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
420 /* Return minimum numeric value from X and Y. */
421 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
424 /* Multiply-add function computed as a ternary operation. */
425 #define fma(Val1, Val2, Val3) \
426 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
429 /* Absolute value, conjugates, and projection. */
431 /* Argument value of Z. */
432 #define carg(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, carg, carg)
434 /* Complex conjugate of Z. */
435 #define conj(Val) __TGMATH_UNARY_IMAG (Val, conj)
437 /* Projection of Z onto the Riemann sphere. */
438 #define cproj(Val) __TGMATH_UNARY_IMAG (Val, cproj)
441 /* Decomposing complex values. */
443 /* Imaginary part of Z. */
444 #define cimag(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, cimag, cimag)
446 /* Real part of Z. */
447 #define creal(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, creal, creal)
449 #endif /* tgmath.h */