Use O_CLOEXEC in tzfile handling
[glibc.git] / math / tgmath.h
blobeefdd26c9c513568d56a2bb54151a86e8d21e3d3
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 # if __GNUC_PREREQ (3, 1)
52 # define __floating_type(type) \
53 (__builtin_classify_type ((type) 0) == 8 \
54 || (__builtin_classify_type ((type) 0) == 9 \
55 && __builtin_classify_type (__real__ ((type) 0)) == 8))
56 # else
57 # define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
58 # endif
60 /* The tgmath real type for T, where E is 0 if T is an integer type and
61 1 for a floating type. */
62 # define __tgmath_real_type_sub(T, E) \
63 __typeof__ (*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
64 : (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
66 /* The tgmath real type of EXPR. */
67 # define __tgmath_real_type(expr) \
68 __tgmath_real_type_sub (__typeof__ ((__typeof__ (expr)) 0), \
69 __floating_type (__typeof__ (expr)))
72 /* We have two kinds of generic macros: to support functions which are
73 only defined on real valued parameters and those which are defined
74 for complex functions as well. */
75 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
76 (__extension__ ((sizeof (Val) == sizeof (double) \
77 || __builtin_classify_type (Val) != 8) \
78 ? (__tgmath_real_type (Val)) Fct (Val) \
79 : (sizeof (Val) == sizeof (float)) \
80 ? (__tgmath_real_type (Val)) Fct##f (Val) \
81 : (__tgmath_real_type (Val)) __tgml(Fct) (Val)))
83 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, RetType, Fct) \
84 (__extension__ ((sizeof (Val) == sizeof (double) \
85 || __builtin_classify_type (Val) != 8) \
86 ? (RetType) Fct (Val) \
87 : (sizeof (Val) == sizeof (float)) \
88 ? (RetType) Fct##f (Val) \
89 : (RetType) __tgml(Fct) (Val)))
91 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
92 (__extension__ ((sizeof (Val1) == sizeof (double) \
93 || __builtin_classify_type (Val1) != 8) \
94 ? (__tgmath_real_type (Val1)) Fct (Val1, Val2) \
95 : (sizeof (Val1) == sizeof (float)) \
96 ? (__tgmath_real_type (Val1)) Fct##f (Val1, Val2) \
97 : (__tgmath_real_type (Val1)) __tgml(Fct) (Val1, Val2)))
99 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
100 (__extension__ (((sizeof (Val1) > sizeof (double) \
101 || sizeof (Val2) > sizeof (double)) \
102 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
103 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
104 + (__tgmath_real_type (Val2)) 0)) \
105 __tgml(Fct) (Val1, Val2) \
106 : (sizeof (Val1) == sizeof (double) \
107 || sizeof (Val2) == sizeof (double) \
108 || __builtin_classify_type (Val1) != 8 \
109 || __builtin_classify_type (Val2) != 8) \
110 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
111 + (__tgmath_real_type (Val2)) 0)) \
112 Fct (Val1, Val2) \
113 : (__typeof ((__tgmath_real_type (Val1)) 0 \
114 + (__tgmath_real_type (Val2)) 0)) \
115 Fct##f (Val1, Val2)))
117 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
118 (__extension__ (((sizeof (Val1) > sizeof (double) \
119 || sizeof (Val2) > sizeof (double)) \
120 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
121 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
122 + (__tgmath_real_type (Val2)) 0)) \
123 __tgml(Fct) (Val1, Val2, Val3) \
124 : (sizeof (Val1) == sizeof (double) \
125 || sizeof (Val2) == sizeof (double) \
126 || __builtin_classify_type (Val1) != 8 \
127 || __builtin_classify_type (Val2) != 8) \
128 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
129 + (__tgmath_real_type (Val2)) 0)) \
130 Fct (Val1, Val2, Val3) \
131 : (__typeof ((__tgmath_real_type (Val1)) 0 \
132 + (__tgmath_real_type (Val2)) 0)) \
133 Fct##f (Val1, Val2, Val3)))
135 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
136 (__extension__ (((sizeof (Val1) > sizeof (double) \
137 || sizeof (Val2) > sizeof (double) \
138 || sizeof (Val3) > sizeof (double)) \
139 && __builtin_classify_type ((Val1) + (Val2) + (Val3)) \
140 == 8) \
141 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
142 + (__tgmath_real_type (Val2)) 0 \
143 + (__tgmath_real_type (Val3)) 0)) \
144 __tgml(Fct) (Val1, Val2, Val3) \
145 : (sizeof (Val1) == sizeof (double) \
146 || sizeof (Val2) == sizeof (double) \
147 || sizeof (Val3) == sizeof (double) \
148 || __builtin_classify_type (Val1) != 8 \
149 || __builtin_classify_type (Val2) != 8 \
150 || __builtin_classify_type (Val3) != 8) \
151 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
152 + (__tgmath_real_type (Val2)) 0 \
153 + (__tgmath_real_type (Val3)) 0)) \
154 Fct (Val1, Val2, Val3) \
155 : (__typeof ((__tgmath_real_type (Val1)) 0 \
156 + (__tgmath_real_type (Val2)) 0 \
157 + (__tgmath_real_type (Val3)) 0)) \
158 Fct##f (Val1, Val2, Val3)))
160 /* XXX This definition has to be changed as soon as the compiler understands
161 the imaginary keyword. */
162 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
163 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
164 || __builtin_classify_type (__real__ (Val)) != 8) \
165 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
166 ? (__tgmath_real_type (Val)) Fct (Val) \
167 : (__tgmath_real_type (Val)) Cfct (Val)) \
168 : (sizeof (__real__ (Val)) == sizeof (float)) \
169 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
170 ? (__tgmath_real_type (Val)) Fct##f (Val) \
171 : (__tgmath_real_type (Val)) Cfct##f (Val)) \
172 : ((sizeof (__real__ (Val)) == sizeof (Val)) \
173 ? (__tgmath_real_type (Val)) __tgml(Fct) (Val) \
174 : (__tgmath_real_type (Val)) __tgml(Cfct) (Val))))
176 # define __TGMATH_UNARY_IMAG(Val, Cfct) \
177 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
178 || __builtin_classify_type (__real__ (Val)) != 8) \
179 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
180 + _Complex_I)) Cfct (Val) \
181 : (sizeof (__real__ (Val)) == sizeof (float)) \
182 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
183 + _Complex_I)) Cfct##f (Val) \
184 : (__typeof__ ((__tgmath_real_type (Val)) 0 \
185 + _Complex_I)) __tgml(Cfct) (Val)))
187 /* XXX This definition has to be changed as soon as the compiler understands
188 the imaginary keyword. */
189 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
190 (__extension__ ((sizeof (__real__ (Val)) == sizeof (double) \
191 || __builtin_classify_type (__real__ (Val)) != 8) \
192 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
193 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
194 Fct (Val) \
195 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
196 Cfct (Val)) \
197 : (sizeof (__real__ (Val)) == sizeof (float)) \
198 ? ((sizeof (__real__ (Val)) == sizeof (Val)) \
199 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
200 Fct##f (Val) \
201 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
202 Cfct##f (Val)) \
203 : ((sizeof (__real__ (Val)) == sizeof (Val)) \
204 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
205 __tgml(Fct) (Val) \
206 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
207 __tgml(Cfct) (Val))))
209 /* XXX This definition has to be changed as soon as the compiler understands
210 the imaginary keyword. */
211 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
212 (__extension__ (((sizeof (__real__ (Val1)) > sizeof (double) \
213 || sizeof (__real__ (Val2)) > sizeof (double)) \
214 && __builtin_classify_type (__real__ (Val1) \
215 + __real__ (Val2)) == 8) \
216 ? ((sizeof (__real__ (Val1)) == sizeof (Val1) \
217 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
218 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
219 + (__tgmath_real_type (Val2)) 0)) \
220 __tgml(Fct) (Val1, Val2) \
221 : (__typeof ((__tgmath_real_type (Val1)) 0 \
222 + (__tgmath_real_type (Val2)) 0)) \
223 __tgml(Cfct) (Val1, Val2)) \
224 : (sizeof (__real__ (Val1)) == sizeof (double) \
225 || sizeof (__real__ (Val2)) == sizeof (double) \
226 || __builtin_classify_type (__real__ (Val1)) != 8 \
227 || __builtin_classify_type (__real__ (Val2)) != 8) \
228 ? ((sizeof (__real__ (Val1)) == sizeof (Val1) \
229 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
230 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
231 + (__tgmath_real_type (Val2)) 0)) \
232 Fct (Val1, Val2) \
233 : (__typeof ((__tgmath_real_type (Val1)) 0 \
234 + (__tgmath_real_type (Val2)) 0)) \
235 Cfct (Val1, Val2)) \
236 : ((sizeof (__real__ (Val1)) == sizeof (Val1) \
237 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
238 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
239 + (__tgmath_real_type (Val2)) 0)) \
240 Fct##f (Val1, Val2) \
241 : (__typeof ((__tgmath_real_type (Val1)) 0 \
242 + (__tgmath_real_type (Val2)) 0)) \
243 Cfct##f (Val1, Val2))))
244 #else
245 # error "Unsupported compiler; you cannot use <tgmath.h>"
246 #endif
249 /* Unary functions defined for real and complex values. */
252 /* Trigonometric functions. */
254 /* Arc cosine of X. */
255 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
256 /* Arc sine of X. */
257 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
258 /* Arc tangent of X. */
259 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
260 /* Arc tangent of Y/X. */
261 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
263 /* Cosine of X. */
264 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
265 /* Sine of X. */
266 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
267 /* Tangent of X. */
268 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
271 /* Hyperbolic functions. */
273 /* Hyperbolic arc cosine of X. */
274 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
275 /* Hyperbolic arc sine of X. */
276 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
277 /* Hyperbolic arc tangent of X. */
278 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
280 /* Hyperbolic cosine of X. */
281 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
282 /* Hyperbolic sine of X. */
283 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
284 /* Hyperbolic tangent of X. */
285 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
288 /* Exponential and logarithmic functions. */
290 /* Exponential function of X. */
291 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
293 /* Break VALUE into a normalized fraction and an integral power of 2. */
294 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
296 /* X times (two to the EXP power). */
297 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
299 /* Natural logarithm of X. */
300 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
302 /* Base-ten logarithm of X. */
303 #ifdef __USE_GNU
304 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
305 #else
306 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
307 #endif
309 /* Return exp(X) - 1. */
310 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
312 /* Return log(1 + X). */
313 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
315 /* Return the base 2 signed integral exponent of X. */
316 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
318 /* Compute base-2 exponential of X. */
319 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
321 /* Compute base-2 logarithm of X. */
322 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
325 /* Power functions. */
327 /* Return X to the Y power. */
328 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
330 /* Return the square root of X. */
331 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
333 /* Return `sqrt(X*X + Y*Y)'. */
334 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
336 /* Return the cube root of X. */
337 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
340 /* Nearest integer, absolute value, and remainder functions. */
342 /* Smallest integral value not less than X. */
343 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
345 /* Absolute value of X. */
346 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, fabs, cabs)
348 /* Largest integer not greater than X. */
349 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
351 /* Floating-point modulo remainder of X/Y. */
352 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
354 /* Round X to integral valuein floating-point format using current
355 rounding direction, but do not raise inexact exception. */
356 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
358 /* Round X to nearest integral value, rounding halfway cases away from
359 zero. */
360 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
362 /* Round X to the integral value in floating-point format nearest but
363 not larger in magnitude. */
364 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
366 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
367 and magnitude congruent `mod 2^n' to the magnitude of the integral
368 quotient x/y, with n >= 3. */
369 #define remquo(Val1, Val2, Val3) \
370 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
372 /* Round X to nearest integral value according to current rounding
373 direction. */
374 #define lrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lrint)
375 #define llrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llrint)
377 /* Round X to nearest integral value, rounding halfway cases away from
378 zero. */
379 #define lround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long int, lround)
380 #define llround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, long long int, llround)
383 /* Return X with its signed changed to Y's. */
384 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
386 /* Error and gamma functions. */
387 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
388 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
389 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
390 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
393 /* Return the integer nearest X in the direction of the
394 prevailing rounding mode. */
395 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
397 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
398 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
399 #define nexttoward(Val1, Val2) \
400 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
402 /* Return the remainder of integer divison X / Y with infinite precision. */
403 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
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)
417 /* Return the binary exponent of X, which must be nonzero. */
418 #define ilogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, int, ilogb)
421 /* Return positive difference between X and Y. */
422 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
424 /* Return maximum numeric value from X and Y. */
425 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
427 /* Return minimum numeric value from X and Y. */
428 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
431 /* Multiply-add function computed as a ternary operation. */
432 #define fma(Val1, Val2, Val3) \
433 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
436 /* Absolute value, conjugates, and projection. */
438 /* Argument value of Z. */
439 #define carg(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, carg, carg)
441 /* Complex conjugate of Z. */
442 #define conj(Val) __TGMATH_UNARY_IMAG (Val, conj)
444 /* Projection of Z onto the Riemann sphere. */
445 #define cproj(Val) __TGMATH_UNARY_IMAG (Val, cproj)
448 /* Decomposing complex values. */
450 /* Imaginary part of Z. */
451 #define cimag(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, cimag, cimag)
453 /* Real part of Z. */
454 #define creal(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, creal, creal)
456 #endif /* tgmath.h */