1 /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * ISO C99 Standard: 7.22 Type-generic math <tgmath.h>
26 /* Include the needed headers. */
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
42 # define __tgml(fct) fct ## l
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 # define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
52 /* The tgmath real type for T, where E is 0 if T is an integer type and
53 1 for a floating type. */
54 # define __tgmath_real_type_sub(T, E) \
55 __typeof__(*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
56 : (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
58 /* The tgmath real type of EXPR. */
59 # define __tgmath_real_type(expr) \
60 __tgmath_real_type_sub(__typeof__(expr), __floating_type(__typeof__(expr)))
63 /* We have two kinds of generic macros: to support functions which are
64 only defined on real valued parameters and those which are defined
65 for complex functions as well. */
66 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
67 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
68 if (sizeof (Val) == sizeof (double) \
69 || __builtin_classify_type (Val) != 8) \
70 __tgmres = Fct (Val); \
71 else if (sizeof (Val) == sizeof (float)) \
72 __tgmres = Fct##f (Val); \
74 __tgmres = __tgml(Fct) (Val); \
77 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, RetType, Fct) \
78 (__extension__ ({ RetType __tgmres; \
79 if (sizeof (Val) == sizeof (double) \
80 || __builtin_classify_type (Val) != 8) \
81 __tgmres = Fct (Val); \
82 else if (sizeof (Val) == sizeof (float)) \
83 __tgmres = Fct##f (Val); \
85 __tgmres = __tgml(Fct) (Val); \
88 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
89 (__extension__ ({ __tgmath_real_type (Val1) __tgmres; \
90 if (sizeof (Val1) == sizeof (double) \
91 || __builtin_classify_type (Val1) != 8) \
92 __tgmres = Fct (Val1, Val2); \
93 else if (sizeof (Val1) == sizeof (float)) \
94 __tgmres = Fct##f (Val1, Val2); \
96 __tgmres = __tgml(Fct) (Val1, Val2); \
99 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
100 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
101 if ((sizeof (Val1) > sizeof (double) \
102 || sizeof (Val2) > sizeof (double)) \
103 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
104 __tgmres = __tgml(Fct) (Val1, Val2); \
105 else if (sizeof (Val1) == sizeof (double) \
106 || sizeof (Val2) == sizeof (double) \
107 || __builtin_classify_type (Val1) != 8 \
108 || __builtin_classify_type (Val2) != 8) \
109 __tgmres = Fct (Val1, Val2); \
111 __tgmres = Fct##f (Val1, Val2); \
114 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
115 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
116 if ((sizeof (Val1) > sizeof (double) \
117 || sizeof (Val2) > sizeof (double)) \
118 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
119 __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
120 else if (sizeof (Val1) == sizeof (double) \
121 || sizeof (Val2) == sizeof (double) \
122 || __builtin_classify_type (Val1) != 8 \
123 || __builtin_classify_type (Val2) != 8) \
124 __tgmres = Fct (Val1, Val2, Val3); \
126 __tgmres = Fct##f (Val1, Val2, Val3); \
129 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
130 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2) + (Val3)) __tgmres;\
131 if ((sizeof (Val1) > sizeof (double) \
132 || sizeof (Val2) > sizeof (double) \
133 || sizeof (Val3) > sizeof (double)) \
134 && __builtin_classify_type ((Val1) + (Val2) \
136 __tgmres = __tgml(Fct) (Val1, Val2, Val3); \
137 else if (sizeof (Val1) == sizeof (double) \
138 || sizeof (Val2) == sizeof (double) \
139 || sizeof (Val3) == sizeof (double) \
140 || __builtin_classify_type (Val1) != 8 \
141 || __builtin_classify_type (Val2) != 8 \
142 || __builtin_classify_type (Val3) != 8) \
143 __tgmres = Fct (Val1, Val2, Val3); \
145 __tgmres = Fct##f (Val1, Val2, Val3); \
148 /* XXX This definition has to be changed as soon as the compiler understands
149 the imaginary keyword. */
150 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
151 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
152 if (sizeof (__real__ (Val)) > sizeof (double) \
153 && __builtin_classify_type (__real__ (Val)) == 8) \
155 if (sizeof (__real__ (Val)) == sizeof (Val)) \
156 __tgmres = __tgml(Fct) (Val); \
158 __tgmres = __tgml(Cfct) (Val); \
160 else if (sizeof (__real__ (Val)) == sizeof (double) \
161 || __builtin_classify_type (__real__ (Val)) \
164 if (sizeof (__real__ (Val)) == sizeof (Val)) \
165 __tgmres = Fct (Val); \
167 __tgmres = Cfct (Val); \
171 if (sizeof (__real__ (Val)) == sizeof (Val)) \
172 __tgmres = Fct##f (Val); \
174 __tgmres = Cfct##f (Val); \
178 /* XXX This definition has to be changed as soon as the compiler understands
179 the imaginary keyword. */
180 # define __TGMATH_UNARY_IMAG_ONLY(Val, Fct) \
181 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
182 if (sizeof (Val) == sizeof (__complex__ double) \
183 || __builtin_classify_type (__real__ (Val)) != 8) \
184 __tgmres = Fct (Val); \
185 else if (sizeof (Val) == sizeof (__complex__ float)) \
186 __tgmres = Fct##f (Val); \
188 __tgmres = __tgml(Fct) (Val); \
191 /* XXX This definition has to be changed as soon as the compiler understands
192 the imaginary keyword. */
193 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
194 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
195 if ((sizeof (__real__ (Val1)) > sizeof (double) \
196 || sizeof (__real__ (Val2)) > sizeof (double)) \
197 && __builtin_classify_type (__real__ (Val1) \
201 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
202 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
203 __tgmres = __tgml(Fct) (Val1, Val2); \
205 __tgmres = __tgml(Cfct) (Val1, Val2); \
207 else if (sizeof (__real__ (Val1)) == sizeof (double) \
208 || sizeof (__real__ (Val2)) == sizeof(double) \
209 || (__builtin_classify_type (__real__ (Val1)) \
211 || (__builtin_classify_type (__real__ (Val2)) \
214 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
215 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
216 __tgmres = Fct (Val1, Val2); \
218 __tgmres = Cfct (Val1, Val2); \
222 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
223 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
224 __tgmres = Fct##f (Val1, Val2); \
226 __tgmres = Cfct##f (Val1, Val2); \
230 # error "Unsupported compiler; you cannot use <tgmath.h>"
234 /* Unary functions defined for real and complex values. */
237 /* Trigonometric functions. */
239 /* Arc cosine of X. */
240 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
242 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
243 /* Arc tangent of X. */
244 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
245 /* Arc tangent of Y/X. */
246 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
249 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
251 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
253 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
256 /* Hyperbolic functions. */
258 /* Hyperbolic arc cosine of X. */
259 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
260 /* Hyperbolic arc sine of X. */
261 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
262 /* Hyperbolic arc tangent of X. */
263 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
265 /* Hyperbolic cosine of X. */
266 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
267 /* Hyperbolic sine of X. */
268 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
269 /* Hyperbolic tangent of X. */
270 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
273 /* Exponential and logarithmic functions. */
275 /* Exponential function of X. */
276 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
278 /* Break VALUE into a normalized fraction and an integral power of 2. */
279 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
281 /* X times (two to the EXP power). */
282 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
284 /* Natural logarithm of X. */
285 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
287 /* Base-ten logarithm of X. */
289 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
291 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
294 /* Return exp(X) - 1. */
295 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
297 /* Return log(1 + X). */
298 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
300 /* Return the base 2 signed integral exponent of X. */
301 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
303 /* Compute base-2 exponential of X. */
304 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
306 /* Compute base-2 logarithm of X. */
307 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
310 /* Power functions. */
312 /* Return X to the Y power. */
313 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
315 /* Return the square root of X. */
316 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
318 /* Return `sqrt(X*X + Y*Y)'. */
319 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
321 /* Return the cube root of X. */
322 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
325 /* Nearest integer, absolute value, and remainder functions. */
327 /* Smallest integral value not less than X. */
328 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
330 /* Absolute value of X. */
331 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs)
333 /* Largest integer not greater than X. */
334 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
336 /* Floating-point modulo remainder of X/Y. */
337 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
339 /* Round X to integral valuein floating-point format using current
340 rounding direction, but do not raise inexact exception. */
341 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
343 /* Round X to nearest integral value, rounding halfway cases away from
345 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
347 /* Round X to the integral value in floating-point format nearest but
348 not larger in magnitude. */
349 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
351 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
352 and magnitude congruent `mod 2^n' to the magnitude of the integral
353 quotient x/y, with n >= 3. */
354 #define remquo(Val1, Val2, Val3) \
355 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
357 /* Round X to nearest integral value according to current rounding
359 #define lrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lrint)
360 #define llrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llrint)
362 /* Round X to nearest integral value, rounding halfway cases away from
364 #define lround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lround)
365 #define llround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llround)
368 /* Return X with its signed changed to Y's. */
369 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
371 /* Error and gamma functions. */
372 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
373 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
374 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
375 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
378 /* Return the integer nearest X in the direction of the
379 prevailing rounding mode. */
380 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
382 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
383 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
384 #define nexttoward(Val1, Val2) \
385 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
387 /* Return the remainder of integer divison X / Y with infinite precision. */
388 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
390 /* Return X times (2 to the Nth power). */
391 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
392 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
395 /* Return X times (2 to the Nth power). */
396 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
398 /* Return X times (2 to the Nth power). */
399 #define scalbln(Val1, Val2) \
400 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
402 /* Return the binary exponent of X, which must be nonzero. */
403 #define ilogb(Val) __TGMATH_UNARY_REAL_ONLY (Val, ilogb)
406 /* Return positive difference between X and Y. */
407 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
409 /* Return maximum numeric value from X and Y. */
410 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
412 /* Return minimum numeric value from X and Y. */
413 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
416 /* Multiply-add function computed as a ternary operation. */
417 #define fma(Val1, Val2, Val3) \
418 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
421 /* Absolute value, conjugates, and projection. */
423 /* Argument value of Z. */
424 #define carg(Val) __TGMATH_UNARY_IMAG_ONLY (Val, carg)
426 /* Complex conjugate of Z. */
427 #define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj)
429 /* Projection of Z onto the Riemann sphere. */
430 #define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj)
433 /* Decomposing complex values. */
435 /* Imaginary part of Z. */
436 #define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag)
438 /* Real part of Z. */
439 #define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal)
441 #endif /* tgmath.h */