nptl: remove sysdep-cancel ASM macros, convert to C
[uclibc-ng.git] / include / tgmath.h
blob1dc3595ee96da03427e84875bf3c1e3292135941
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, see
17 <http://www.gnu.org/licenses/>. */
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 # if __GNUC_PREREQ (3, 1)
51 # define __floating_type(type) \
52 (__builtin_classify_type ((type) 0) == 8 \
53 || (__builtin_classify_type ((type) 0) == 9 \
54 && __builtin_classify_type (__real__ ((type) 0)) == 8))
55 # else
56 # define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
57 # endif
59 /* The tgmath real type for T, where E is 0 if T is an integer type and
60 1 for a floating type. */
61 # define __tgmath_real_type_sub(T, E) \
62 __typeof__ (*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
63 : (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
65 /* The tgmath real type of EXPR. */
66 # define __tgmath_real_type(expr) \
67 __tgmath_real_type_sub (__typeof__ ((__typeof__ (expr)) 0), \
68 __floating_type (__typeof__ (expr)))
71 /* We have two kinds of generic macros: to support functions which are
72 only defined on real valued parameters and those which are defined
73 for complex functions as well. */
74 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
75 (__extension__ ((sizeof (Val) == sizeof (double) \
76 || __builtin_classify_type (Val) != 8) \
77 ? (__tgmath_real_type (Val)) Fct (Val) \
78 : (sizeof (Val) == sizeof (float)) \
79 ? (__tgmath_real_type (Val)) Fct##f (Val) \
80 : (__tgmath_real_type (Val)) __tgml(Fct) (Val)))
82 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, RetType, Fct) \
83 (__extension__ ((sizeof (Val) == sizeof (double) \
84 || __builtin_classify_type (Val) != 8) \
85 ? (RetType) Fct (Val) \
86 : (sizeof (Val) == sizeof (float)) \
87 ? (RetType) Fct##f (Val) \
88 : (RetType) __tgml(Fct) (Val)))
90 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
91 (__extension__ ((sizeof (Val1) == sizeof (double) \
92 || __builtin_classify_type (Val1) != 8) \
93 ? (__tgmath_real_type (Val1)) Fct (Val1, Val2) \
94 : (sizeof (Val1) == sizeof (float)) \
95 ? (__tgmath_real_type (Val1)) Fct##f (Val1, Val2) \
96 : (__tgmath_real_type (Val1)) __tgml(Fct) (Val1, Val2)))
98 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
99 (__extension__ (((sizeof (Val1) > sizeof (double) \
100 || sizeof (Val2) > sizeof (double)) \
101 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
102 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
103 + (__tgmath_real_type (Val2)) 0)) \
104 __tgml(Fct) (Val1, Val2) \
105 : (sizeof (Val1) == sizeof (double) \
106 || sizeof (Val2) == sizeof (double) \
107 || __builtin_classify_type (Val1) != 8 \
108 || __builtin_classify_type (Val2) != 8) \
109 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
110 + (__tgmath_real_type (Val2)) 0)) \
111 Fct (Val1, Val2) \
112 : (__typeof ((__tgmath_real_type (Val1)) 0 \
113 + (__tgmath_real_type (Val2)) 0)) \
114 Fct##f (Val1, Val2)))
116 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
117 (__extension__ (((sizeof (Val1) > sizeof (double) \
118 || sizeof (Val2) > sizeof (double)) \
119 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
120 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
121 + (__tgmath_real_type (Val2)) 0)) \
122 __tgml(Fct) (Val1, Val2, Val3) \
123 : (sizeof (Val1) == sizeof (double) \
124 || sizeof (Val2) == sizeof (double) \
125 || __builtin_classify_type (Val1) != 8 \
126 || __builtin_classify_type (Val2) != 8) \
127 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
128 + (__tgmath_real_type (Val2)) 0)) \
129 Fct (Val1, Val2, Val3) \
130 : (__typeof ((__tgmath_real_type (Val1)) 0 \
131 + (__tgmath_real_type (Val2)) 0)) \
132 Fct##f (Val1, Val2, Val3)))
134 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
135 (__extension__ (((sizeof (Val1) > sizeof (double) \
136 || sizeof (Val2) > sizeof (double) \
137 || sizeof (Val3) > sizeof (double)) \
138 && __builtin_classify_type ((Val1) + (Val2) + (Val3)) \
139 == 8) \
140 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
141 + (__tgmath_real_type (Val2)) 0 \
142 + (__tgmath_real_type (Val3)) 0)) \
143 __tgml(Fct) (Val1, Val2, Val3) \
144 : (sizeof (Val1) == sizeof (double) \
145 || sizeof (Val2) == sizeof (double) \
146 || sizeof (Val3) == sizeof (double) \
147 || __builtin_classify_type (Val1) != 8 \
148 || __builtin_classify_type (Val2) != 8 \
149 || __builtin_classify_type (Val3) != 8) \
150 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
151 + (__tgmath_real_type (Val2)) 0 \
152 + (__tgmath_real_type (Val3)) 0)) \
153 Fct (Val1, Val2, Val3) \
154 : (__typeof ((__tgmath_real_type (Val1)) 0 \
155 + (__tgmath_real_type (Val2)) 0 \
156 + (__tgmath_real_type (Val3)) 0)) \
157 Fct##f (Val1, Val2, Val3)))
159 /* XXX This definition has to be changed as soon as the compiler understands
160 the imaginary keyword. */
161 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
162 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
163 || __builtin_classify_type (__real__ (Val)) != 8) \
164 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
165 ? (__tgmath_real_type (Val)) Fct (Val) \
166 : (__tgmath_real_type (Val)) Cfct (Val)) \
167 : (sizeof (__real__ (Val)) == sizeof (float)) \
168 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
169 ? (__tgmath_real_type (Val)) Fct##f (Val) \
170 : (__tgmath_real_type (Val)) Cfct##f (Val)) \
171 : ((sizeof (__real__ (Val)) == sizeof (Val)) \
172 ? (__tgmath_real_type (Val)) __tgml(Fct) (Val) \
173 : (__tgmath_real_type (Val)) __tgml(Cfct) (Val))))
175 # define __TGMATH_UNARY_IMAG(Val, Cfct) \
176 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
177 || __builtin_classify_type (__real__ (Val)) != 8) \
178 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
179 + _Complex_I)) Cfct (Val) \
180 : (sizeof (__real__ (Val)) == sizeof (float)) \
181 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
182 + _Complex_I)) Cfct##f (Val) \
183 : (__typeof__ ((__tgmath_real_type (Val)) 0 \
184 + _Complex_I)) __tgml(Cfct) (Val)))
186 /* XXX This definition has to be changed as soon as the compiler understands
187 the imaginary keyword. */
188 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
189 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
190 || __builtin_classify_type (__real__ (Val)) != 8) \
191 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
192 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
193 Fct (Val) \
194 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
195 Cfct (Val)) \
196 : (sizeof (__real__ (Val)) == sizeof (float)) \
197 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
198 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
199 Fct##f (Val) \
200 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
201 Cfct##f (Val)) \
202 : ((sizeof (__real__ (Val)) == sizeof (Val)) \
203 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
204 __tgml(Fct) (Val) \
205 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
206 __tgml(Cfct) (Val))))
208 /* XXX This definition has to be changed as soon as the compiler understands
209 the imaginary keyword. */
210 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
211 (__extension__ (((sizeof (__real__ (Val1)) > sizeof (double) \
212 || sizeof (__real__ (Val2)) > sizeof (double)) \
213 && __builtin_classify_type (__real__ (Val1) \
214 + __real__ (Val2)) == 8) \
215 ? ((sizeof (__real__ (Val1)) == sizeof (Val1) \
216 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
217 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
218 + (__tgmath_real_type (Val2)) 0)) \
219 __tgml(Fct) (Val1, Val2) \
220 : (__typeof ((__tgmath_real_type (Val1)) 0 \
221 + (__tgmath_real_type (Val2)) 0)) \
222 __tgml(Cfct) (Val1, Val2)) \
223 : (sizeof (__real__ (Val1)) == sizeof (double) \
224 || sizeof (__real__ (Val2)) == sizeof (double) \
225 || __builtin_classify_type (__real__ (Val1)) != 8 \
226 || __builtin_classify_type (__real__ (Val2)) != 8) \
227 ? ((sizeof (__real__ (Val1)) == sizeof (Val1) \
228 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
229 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
230 + (__tgmath_real_type (Val2)) 0)) \
231 Fct (Val1, Val2) \
232 : (__typeof ((__tgmath_real_type (Val1)) 0 \
233 + (__tgmath_real_type (Val2)) 0)) \
234 Cfct (Val1, Val2)) \
235 : ((sizeof (__real__ (Val1)) == sizeof (Val1) \
236 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
237 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
238 + (__tgmath_real_type (Val2)) 0)) \
239 Fct##f (Val1, Val2) \
240 : (__typeof ((__tgmath_real_type (Val1)) 0 \
241 + (__tgmath_real_type (Val2)) 0)) \
242 Cfct##f (Val1, Val2))))
243 #else
244 # error "Unsupported compiler; you cannot use <tgmath.h>"
245 #endif
248 /* Unary functions defined for real and complex values. */
251 /* Trigonometric functions. */
253 /* Arc cosine of X. */
254 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
255 /* Arc sine of X. */
256 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
257 /* Arc tangent of X. */
258 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
259 /* Arc tangent of Y/X. */
260 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
262 /* Cosine of X. */
263 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
264 /* Sine of X. */
265 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
266 /* Tangent of X. */
267 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
270 /* Hyperbolic functions. */
272 /* Hyperbolic arc cosine of X. */
273 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
274 /* Hyperbolic arc sine of X. */
275 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
276 /* Hyperbolic arc tangent of X. */
277 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
279 /* Hyperbolic cosine of X. */
280 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
281 /* Hyperbolic sine of X. */
282 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
283 /* Hyperbolic tangent of X. */
284 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
287 /* Exponential and logarithmic functions. */
289 /* Exponential function of X. */
290 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
292 /* Break VALUE into a normalized fraction and an integral power of 2. */
293 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
295 /* X times (two to the EXP power). */
296 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
298 /* Natural logarithm of X. */
299 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
301 /* Base-ten logarithm of X. */
302 #ifdef __USE_GNU
303 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
304 #else
305 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
306 #endif
308 /* Return exp(X) - 1. */
309 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
311 /* Return log(1 + X). */
312 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
314 /* Return the base 2 signed integral exponent of X. */
315 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
317 /* Compute base-2 exponential of X. */
318 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
320 /* Compute base-2 logarithm of X. */
321 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
324 /* Power functions. */
326 /* Return X to the Y power. */
327 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
329 /* Return the square root of X. */
330 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
332 /* Return `sqrt(X*X + Y*Y)'. */
333 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
335 /* Return the cube root of X. */
336 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
339 /* Nearest integer, absolute value, and remainder functions. */
341 /* Smallest integral value not less than X. */
342 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
344 /* Absolute value of X. */
345 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, fabs, cabs)
347 /* Largest integer not greater than X. */
348 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
350 /* Floating-point modulo remainder of X/Y. */
351 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
353 /* Round X to integral valuein floating-point format using current
354 rounding direction, but do not raise inexact exception. */
355 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
357 /* Round X to nearest integral value, rounding halfway cases away from
358 zero. */
359 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
361 /* Round X to the integral value in floating-point format nearest but
362 not larger in magnitude. */
363 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
365 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
366 and magnitude congruent `mod 2^n' to the magnitude of the integral
367 quotient x/y, with n >= 3. */
368 #define remquo(Val1, Val2, Val3) \
369 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
371 /* Round X to nearest integral value according to current rounding
372 direction. */
373 #define lrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lrint)
374 #define llrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llrint)
376 /* Round X to nearest integral value, rounding halfway cases away from
377 zero. */
378 #define lround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lround)
379 #define llround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llround)
382 /* Return X with its signed changed to Y's. */
383 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
385 /* Error and gamma functions. */
386 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
387 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
388 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
389 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
392 /* Return the integer nearest X in the direction of the
393 prevailing rounding mode. */
394 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
396 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
397 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
398 #define nexttoward(Val1, Val2) \
399 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
401 /* Return the remainder of integer divison X / Y with infinite precision. */
402 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
404 #ifdef __UCLIBC_SUSV3_LEGACY__
405 /* Return X times (2 to the Nth power). */
406 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
407 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
408 #endif
410 /* Return X times (2 to the Nth power). */
411 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
413 /* Return X times (2 to the Nth power). */
414 #define scalbln(Val1, Val2) \
415 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
416 #endif /* __UCLIBC_SUSV3_LEGACY__ */
418 /* Return the binary exponent of X, which must be nonzero. */
419 #define ilogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, int, ilogb)
422 /* Return positive difference between X and Y. */
423 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
425 /* Return maximum numeric value from X and Y. */
426 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
428 /* Return minimum numeric value from X and Y. */
429 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
432 /* Multiply-add function computed as a ternary operation. */
433 #define fma(Val1, Val2, Val3) \
434 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
437 /* Absolute value, conjugates, and projection. */
439 /* Argument value of Z. */
440 #define carg(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, carg, carg)
442 /* Complex conjugate of Z. */
443 #define conj(Val) __TGMATH_UNARY_IMAG (Val, conj)
445 /* Projection of Z onto the Riemann sphere. */
446 #define cproj(Val) __TGMATH_UNARY_IMAG (Val, cproj)
449 /* Decomposing complex values. */
451 /* Imaginary part of Z. */
452 #define cimag(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, cimag, cimag)
454 /* Real part of Z. */
455 #define creal(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, creal, creal)
457 #endif /* tgmath.h */