]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/tgmath.h
Update.
[thirdparty/glibc.git] / math / tgmath.h
CommitLineData
204e299e 1/* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
dfd2257a
UD
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
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 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19/*
63ae7b63 20 * ISO C99 Standard: 7.22 Type-generic math <tgmath.h>
dfd2257a
UD
21 */
22
23#ifndef _TGMATH_H
24#define _TGMATH_H 1
25
26/* Include the needed headers. */
27#include <math.h>
28#include <complex.h>
29
30
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. */
36
4360eafd 37#if __GNUC_PREREQ (2, 7)
dfd2257a 38
925e31d9
UD
39/* This is ugly but unless gcc gets appropriate builtins we have to do
40 something like this. Don't ask how it works. */
41
42/* 1 if 'type' is a floating type, 0 if 'type' is an integer type.
43 Allows for _Bool. Expands to an integer constant expression. */
44#define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
45
46/* The tgmath real type for T, where E is 0 if T is an integer type and
47 1 for a floating type. */
48#define __tgmath_real_type_sub(T, E) \
49 __typeof__(*(0 ? (__typeof__(0 ? (double *)0 : (void *)(E)))0 \
50 : (__typeof__(0 ? (T *)0 : (void *)(!(E))))0))
51
52/* The tgmath real type of EXPR. */
53#define __tgmath_real_type(expr) \
54 __tgmath_real_type_sub(__typeof__(expr), __floating_type(__typeof__(expr)))
55
56
dfd2257a
UD
57/* We have two kinds of generic macros: to support functions which are
58 only defined on real valued parameters and those which are defined
59 for complex functions as well. */
60# define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
925e31d9
UD
61 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
62 if (sizeof (Val) == sizeof (double) \
63 || __builtin_classify_type (Val) != 8) \
48244d09
UD
64 __tgmres = Fct (Val); \
65 else if (sizeof (Val) == sizeof (float)) \
66 __tgmres = Fct##f (Val); \
67 else \
68 __tgmres = Fct##l (Val); \
69 __tgmres; }))
dfd2257a
UD
70
71# define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
925e31d9
UD
72 (__extension__ ({ __tgmath_real_type (Val1) __tgmres; \
73 if (sizeof (Val1) == sizeof (double) \
74 || __builtin_classify_type (Val) != 8) \
48244d09
UD
75 __tgmres = Fct (Val1, Val2); \
76 else if (sizeof (Val1) == sizeof (float)) \
77 __tgmres = Fct##f (Val1, Val2); \
78 else \
79 __tgmres = Fct##l (Val1, Val2); \
80 __tgmres; }))
dfd2257a
UD
81
82# define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
925e31d9
UD
83 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
84 if ((sizeof (Val1) > sizeof (double) \
85 || sizeof (Val2) > sizeof (double)) \
86 && __builtin_classify_type (Val) == 8) \
48244d09
UD
87 __tgmres = Fct##l (Val1, Val2); \
88 else if (sizeof (Val1) == sizeof (double) \
925e31d9
UD
89 || sizeof (Val2) == sizeof (double) \
90 || __builtin_classify_type (Val) != 8) \
48244d09
UD
91 __tgmres = Fct (Val1, Val2); \
92 else \
93 __tgmres = Fct (Val1, Val2); \
94 __tgmres; }))
dfd2257a
UD
95
96# define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
925e31d9
UD
97 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
98 if ((sizeof (Val1) > sizeof (double) \
99 || sizeof (Val2) > sizeof (double)) \
100 && __builtin_classify_type (Val) == 8) \
48244d09
UD
101 __tgmres = Fct##l (Val1, Val2, Val3); \
102 else if (sizeof (Val1) == sizeof (double) \
925e31d9
UD
103 || sizeof (Val2) == sizeof (double) \
104 || __builtin_classify_type (Val) != 8) \
48244d09
UD
105 __tgmres = Fct (Val1, Val2, Val3); \
106 else \
107 __tgmres = Fct (Val1, Val2, Val3); \
108 __tgmres; }))
bfce746a
UD
109
110# define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
925e31d9
UD
111 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2) + (Val3)) __tgmres;\
112 if ((sizeof (Val1) > sizeof (double) \
113 || sizeof (Val2) > sizeof (double) \
114 || sizeof (Val3) > sizeof (double)) \
115 && __builtin_classify_type (Val) == 8) \
48244d09
UD
116 __tgmres = Fct##l (Val1, Val2, Val3); \
117 else if (sizeof (Val1) == sizeof (double) \
118 || sizeof (Val2) == sizeof (double) \
925e31d9
UD
119 || sizeof (Val3) == sizeof (double) \
120 || __builtin_classify_type (Val) != 8) \
48244d09
UD
121 __tgmres = Fct (Val1, Val2, Val3); \
122 else \
123 __tgmres = Fct (Val1, Val2, Val3); \
124 __tgmres; }))
dfd2257a 125
48244d09
UD
126/* XXX This definition has to be changed as soon as the compiler understands
127 the imaginary keyword. */
dfd2257a 128# define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
925e31d9
UD
129 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
130 if (sizeof (__real__ (Val)) > sizeof (double) \
ca8d5a5f 131 && __builtin_classify_type (__real__ (Val)) == 8) \
48244d09
UD
132 { \
133 if (sizeof (__real__ (Val)) == sizeof (Val)) \
134 __tgmres = Fct##l (Val); \
135 else \
136 __tgmres = Cfct##l (Val); \
137 } \
925e31d9 138 else if (sizeof (__real__ (Val)) == sizeof (double) \
ca8d5a5f
UD
139 || (__builtin_classify_type (__real__ (Val)) \
140 != 8) \
48244d09
UD
141 { \
142 if (sizeof (__real__ (Val)) == sizeof (Val)) \
143 __tgmres = Fct (Val); \
144 else \
145 __tgmres = Cfct (Val); \
146 } \
147 else \
148 { \
149 if (sizeof (__real__ (Val)) == sizeof (Val)) \
150 __tgmres = Fct##f (Val); \
151 else \
152 __tgmres = Cfct##f (Val); \
153 } \
154 __tgmres; }))
dfd2257a 155
bfce746a
UD
156/* XXX This definition has to be changed as soon as the compiler understands
157 the imaginary keyword. */
dfd2257a 158# define __TGMATH_UNARY_IMAG_ONLY(Val, Fct) \
925e31d9
UD
159 (__extension__ ({ __tgmath_real_type (Val) __tgmres; \
160 if (sizeof (Val) == sizeof (__complex__ double) \
ca8d5a5f 161 || __builtin_classify_type (__real__ (Val)) != 8) \
48244d09
UD
162 __tgmres = Fct (Val); \
163 else if (sizeof (Val) == sizeof (__complex__ float)) \
164 __tgmres = Fct##f (Val); \
165 else \
166 __tgmres = Fct##l (Val); \
167 __tgmres; }))
dfd2257a 168
48244d09
UD
169/* XXX This definition has to be changed as soon as the compiler understands
170 the imaginary keyword. */
dfd2257a 171# define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
925e31d9
UD
172 (__extension__ ({ __tgmath_real_type ((Val1) + (Val2)) __tgmres; \
173 if ((sizeof (__real__ (Val1)) > sizeof (double) \
174 || sizeof (__real__ (Val2)) > sizeof (double)) \
ca8d5a5f 175 && __builtin_classify_type (__real__ (Val)) == 8) \
48244d09
UD
176 { \
177 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
178 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
179 __tgmres = Fct##l (Val1, Val2); \
180 else \
181 __tgmres = Cfct##l (Val1, Val2); \
182 } \
183 else if (sizeof (__real__ (Val1)) == sizeof (double) \
925e31d9 184 || sizeof (__real__ (Val2)) == sizeof(double) \
ca8d5a5f
UD
185 || (__builtin_classify_type (__real__ (Val)) \
186 != 8) \
48244d09
UD
187 { \
188 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
189 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
190 __tgmres = Fct (Val1, Val2); \
191 else \
192 __tgmres = Cfct (Val1, Val2); \
193 } \
194 else \
195 { \
196 if (sizeof (__real__ (Val1)) == sizeof (Val1) \
197 && sizeof (__real__ (Val2)) == sizeof (Val2)) \
198 __tgmres = Fct##f (Val1, Val2); \
199 else \
200 __tgmres = Cfct##f (Val1, Val2); \
201 } \
202 __tgmres; }))
dfd2257a
UD
203#else
204# error "Unsupported compiler; you cannot use <tgmath.h>"
205#endif
206
207
208/* Unary functions defined for real and complex values. */
209
210
211/* Trigonometric functions. */
212
213/* Arc cosine of X. */
214#define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
215/* Arc sine of X. */
216#define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
217/* Arc tangent of X. */
218#define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
219/* Arc tangent of Y/X. */
cfb32a6c 220#define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
dfd2257a
UD
221
222/* Cosine of X. */
223#define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
224/* Sine of X. */
225#define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
226/* Tangent of X. */
227#define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
228
229
230/* Hyperbolic functions. */
231
232/* Hyperbolic arc cosine of X. */
233#define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
234/* Hyperbolic arc sine of X. */
235#define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
236/* Hyperbolic arc tangent of X. */
237#define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
238
239/* Hyperbolic cosine of X. */
240#define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
241/* Hyperbolic sine of X. */
242#define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
243/* Hyperbolic tangent of X. */
244#define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
245
246
247/* Exponential and logarithmic functions. */
248
249/* Exponential function of X. */
250#define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
251
252/* Break VALUE into a normalized fraction and an integral power of 2. */
253#define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
254
255/* X times (two to the EXP power). */
256#define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
257
258/* Natural logarithm of X. */
259#define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
260
261/* Base-ten logarithm of X. */
cc3fa755
UD
262#ifdef __USE_GNU
263# define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
264#else
265# define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
266#endif
dfd2257a
UD
267
268/* Return exp(X) - 1. */
269#define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
270
271/* Return log(1 + X). */
272#define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
273
274/* Return the base 2 signed integral exponent of X. */
275#define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
276
277/* Compute base-2 exponential of X. */
278#define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
279
280/* Compute base-2 logarithm of X. */
281#define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
282
283
284/* Power functions. */
285
286/* Return X to the Y power. */
287#define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
288
289/* Return the square root of X. */
290#define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
291
292/* Return `sqrt(X*X + Y*Y)'. */
293#define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
294
295/* Return the cube root of X. */
296#define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
297
298
299/* Nearest integer, absolute value, and remainder functions. */
300
301/* Smallest integral value not less than X. */
302#define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
303
304/* Absolute value of X. */
305#define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs)
306
307/* Largest integer not greater than X. */
308#define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
309
310/* Floating-point modulo remainder of X/Y. */
311#define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
312
313/* Round X to integral valuein floating-point format using current
314 rounding direction, but do not raise inexact exception. */
315#define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
316
317/* Round X to nearest integral value, rounding halfway cases away from
318 zero. */
319#define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
320
321/* Round X to the integral value in floating-point format nearest but
322 not larger in magnitude. */
323#define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
324
325/* Compute remainder of X and Y and put in *QUO a value with sign of x/y
326 and magnitude congruent `mod 2^n' to the magnitude of the integral
327 quotient x/y, with n >= 3. */
328#define remquo(Val1, Val2, Val3) \
329 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
330
331/* Round X to nearest integral value according to current rounding
332 direction. */
333#define lrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, lrint)
334#define llrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, llrint)
335
336/* Round X to nearest integral value, rounding halfway cases away from
337 zero. */
338#define lround(Val) __TGMATH_UNARY_REAL_ONLY (Val, lround)
339#define llround(Val) __TGMATH_UNARY_REAL_ONLY (Val, llround)
340
341
342/* Return X with its signed changed to Y's. */
343#define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
344
345/* Error and gamma functions. */
346#define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
347#define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
348#define gamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, gamma)
349#define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
350
351
352/* Return the integer nearest X in the direction of the
353 prevailing rounding mode. */
354#define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
355
356/* Return X + epsilon if X < Y, X - epsilon if X > Y. */
357#define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
42bd0a85
UD
358#define nexttoward(Val1, Val2) \
359 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward)
dfd2257a
UD
360
361/* Return the remainder of integer divison X / Y with infinite precision. */
362#define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
363
364/* Return X times (2 to the Nth power). */
26644e87 365#if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
204e299e 366# define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
26644e87 367#endif
dfd2257a
UD
368
369/* Return X times (2 to the Nth power). */
370#define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
371
372/* Return X times (2 to the Nth power). */
373#define scalbln(Val1, Val2) \
374 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
375
376/* Return the binary exponent of X, which must be nonzero. */
377#define ilogb(Val) __TGMATH_UNARY_REAL_ONLY (Val, ilogb)
378
379
380/* Return positive difference between X and Y. */
381#define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
382
383/* Return maximum numeric value from X and Y. */
384#define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
385
386/* Return minimum numeric value from X and Y. */
387#define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
388
389
bfce746a
UD
390/* Multiply-add function computed as a ternary operation. */
391#define fma(Vat1, Val2, Val3) \
392 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
393
394
dfd2257a
UD
395/* Absolute value, conjugates, and projection. */
396
397/* Argument value of Z. */
398#define carg(Val) __TGMATH_UNARY_IMAG_ONLY (Val, carg)
399
400/* Complex conjugate of Z. */
401#define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj)
402
403/* Projection of Z onto the Riemann sphere. */
404#define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj)
405
406
407/* Decomposing complex values. */
408
409/* Imaginary part of Z. */
410#define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag)
411
412/* Real part of Z. */
413#define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal)
414
415#endif /* tgmath.h */