]>
Commit | Line | Data |
---|---|---|
42bd0a85 | 1 | /* Copyright (C) 1997, 1998, 1999 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 UD |
38 | |
39 | /* We have two kinds of generic macros: to support functions which are | |
40 | only defined on real valued parameters and those which are defined | |
41 | for complex functions as well. */ | |
42 | # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \ | |
48244d09 UD |
43 | (__extension__ ({ __typeof__ (Val) __tgmres; \ |
44 | if (sizeof (Val) == sizeof (double)) \ | |
45 | __tgmres = Fct (Val); \ | |
46 | else if (sizeof (Val) == sizeof (float)) \ | |
47 | __tgmres = Fct##f (Val); \ | |
48 | else \ | |
49 | __tgmres = Fct##l (Val); \ | |
50 | __tgmres; })) | |
dfd2257a UD |
51 | |
52 | # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \ | |
48244d09 UD |
53 | (__extension__ ({ __typeof__ (Val1) __tgmres; \ |
54 | if (sizeof (Val1) == sizeof (double)) \ | |
55 | __tgmres = Fct (Val1, Val2); \ | |
56 | else if (sizeof (Val1) == sizeof (float)) \ | |
57 | __tgmres = Fct##f (Val1, Val2); \ | |
58 | else \ | |
59 | __tgmres = Fct##l (Val1, Val2); \ | |
60 | __tgmres; })) | |
dfd2257a UD |
61 | |
62 | # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \ | |
48244d09 UD |
63 | (__extension__ ({ __typeof__ ((Val1) + (Val2)) __tgmres; \ |
64 | if (sizeof (Val1) > sizeof (double) \ | |
65 | || sizeof (Val2) > sizeof (double)) \ | |
66 | __tgmres = Fct##l (Val1, Val2); \ | |
67 | else if (sizeof (Val1) == sizeof (double) \ | |
68 | || sizeof (Val2) == sizeof (double)) \ | |
69 | __tgmres = Fct (Val1, Val2); \ | |
70 | else \ | |
71 | __tgmres = Fct (Val1, Val2); \ | |
72 | __tgmres; })) | |
dfd2257a UD |
73 | |
74 | # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \ | |
48244d09 UD |
75 | (__extension__ ({ __typeof__ ((Val1) + (Val2)) __tgmres; \ |
76 | if (sizeof (Val1) > sizeof (double) \ | |
77 | || sizeof (Val2) > sizeof (double)) \ | |
78 | __tgmres = Fct##l (Val1, Val2, Val3); \ | |
79 | else if (sizeof (Val1) == sizeof (double) \ | |
80 | || sizeof (Val2) == sizeof (double)) \ | |
81 | __tgmres = Fct (Val1, Val2, Val3); \ | |
82 | else \ | |
83 | __tgmres = Fct (Val1, Val2, Val3); \ | |
84 | __tgmres; })) | |
bfce746a UD |
85 | |
86 | # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \ | |
48244d09 UD |
87 | (__extension__ ({ __typeof__ ((Val1) + (Val2) + (Val3)) __tgmres; \ |
88 | if (sizeof (Val1) > sizeof (double) \ | |
89 | || sizeof (Val2) > sizeof (double) \ | |
90 | || sizeof (Val3) > sizeof (double)) \ | |
91 | __tgmres = Fct##l (Val1, Val2, Val3); \ | |
92 | else if (sizeof (Val1) == sizeof (double) \ | |
93 | || sizeof (Val2) == sizeof (double) \ | |
94 | || sizeof (Val3) == sizeof (double)) \ | |
95 | __tgmres = Fct (Val1, Val2, Val3); \ | |
96 | else \ | |
97 | __tgmres = Fct (Val1, Val2, Val3); \ | |
98 | __tgmres; })) | |
dfd2257a | 99 | |
48244d09 UD |
100 | /* XXX This definition has to be changed as soon as the compiler understands |
101 | the imaginary keyword. */ | |
dfd2257a | 102 | # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \ |
48244d09 UD |
103 | (__extension__ ({ __typeof__ (Val) __tgmres; \ |
104 | if (sizeof (__real__ (Val)) > sizeof (double)) \ | |
105 | { \ | |
106 | if (sizeof (__real__ (Val)) == sizeof (Val)) \ | |
107 | __tgmres = Fct##l (Val); \ | |
108 | else \ | |
109 | __tgmres = Cfct##l (Val); \ | |
110 | } \ | |
111 | else if (sizeof (__real__ (Val)) == sizeof (double)) \ | |
112 | { \ | |
113 | if (sizeof (__real__ (Val)) == sizeof (Val)) \ | |
114 | __tgmres = Fct (Val); \ | |
115 | else \ | |
116 | __tgmres = Cfct (Val); \ | |
117 | } \ | |
118 | else \ | |
119 | { \ | |
120 | if (sizeof (__real__ (Val)) == sizeof (Val)) \ | |
121 | __tgmres = Fct##f (Val); \ | |
122 | else \ | |
123 | __tgmres = Cfct##f (Val); \ | |
124 | } \ | |
125 | __tgmres; })) | |
dfd2257a | 126 | |
bfce746a UD |
127 | /* XXX This definition has to be changed as soon as the compiler understands |
128 | the imaginary keyword. */ | |
dfd2257a | 129 | # define __TGMATH_UNARY_IMAG_ONLY(Val, Fct) \ |
48244d09 UD |
130 | (__extension__ ({ __typeof__ (Val) __tgmres; \ |
131 | if (sizeof (Val) == sizeof (__complex__ double)) \ | |
132 | __tgmres = Fct (Val); \ | |
133 | else if (sizeof (Val) == sizeof (__complex__ float)) \ | |
134 | __tgmres = Fct##f (Val); \ | |
135 | else \ | |
136 | __tgmres = Fct##l (Val); \ | |
137 | __tgmres; })) | |
dfd2257a | 138 | |
48244d09 UD |
139 | /* XXX This definition has to be changed as soon as the compiler understands |
140 | the imaginary keyword. */ | |
dfd2257a | 141 | # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \ |
48244d09 UD |
142 | (__extension__ ({ __typeof__ ((Val1) + (Val2)) __tgmres; \ |
143 | if (sizeof (__real__ (Val1)) > sizeof (double) \ | |
144 | || sizeof (__real__ (Val2)) > sizeof (double)) \ | |
145 | { \ | |
146 | if (sizeof (__real__ (Val1)) == sizeof (Val1) \ | |
147 | && sizeof (__real__ (Val2)) == sizeof (Val2)) \ | |
148 | __tgmres = Fct##l (Val1, Val2); \ | |
149 | else \ | |
150 | __tgmres = Cfct##l (Val1, Val2); \ | |
151 | } \ | |
152 | else if (sizeof (__real__ (Val1)) == sizeof (double) \ | |
153 | || sizeof (__real__ (Val2)) == sizeof(double))\ | |
154 | { \ | |
155 | if (sizeof (__real__ (Val1)) == sizeof (Val1) \ | |
156 | && sizeof (__real__ (Val2)) == sizeof (Val2)) \ | |
157 | __tgmres = Fct (Val1, Val2); \ | |
158 | else \ | |
159 | __tgmres = Cfct (Val1, Val2); \ | |
160 | } \ | |
161 | else \ | |
162 | { \ | |
163 | if (sizeof (__real__ (Val1)) == sizeof (Val1) \ | |
164 | && sizeof (__real__ (Val2)) == sizeof (Val2)) \ | |
165 | __tgmres = Fct##f (Val1, Val2); \ | |
166 | else \ | |
167 | __tgmres = Cfct##f (Val1, Val2); \ | |
168 | } \ | |
169 | __tgmres; })) | |
dfd2257a UD |
170 | #else |
171 | # error "Unsupported compiler; you cannot use <tgmath.h>" | |
172 | #endif | |
173 | ||
174 | ||
175 | /* Unary functions defined for real and complex values. */ | |
176 | ||
177 | ||
178 | /* Trigonometric functions. */ | |
179 | ||
180 | /* Arc cosine of X. */ | |
181 | #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos) | |
182 | /* Arc sine of X. */ | |
183 | #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin) | |
184 | /* Arc tangent of X. */ | |
185 | #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan) | |
186 | /* Arc tangent of Y/X. */ | |
cfb32a6c | 187 | #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2) |
dfd2257a UD |
188 | |
189 | /* Cosine of X. */ | |
190 | #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos) | |
191 | /* Sine of X. */ | |
192 | #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin) | |
193 | /* Tangent of X. */ | |
194 | #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan) | |
195 | ||
196 | ||
197 | /* Hyperbolic functions. */ | |
198 | ||
199 | /* Hyperbolic arc cosine of X. */ | |
200 | #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh) | |
201 | /* Hyperbolic arc sine of X. */ | |
202 | #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh) | |
203 | /* Hyperbolic arc tangent of X. */ | |
204 | #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh) | |
205 | ||
206 | /* Hyperbolic cosine of X. */ | |
207 | #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh) | |
208 | /* Hyperbolic sine of X. */ | |
209 | #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh) | |
210 | /* Hyperbolic tangent of X. */ | |
211 | #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh) | |
212 | ||
213 | ||
214 | /* Exponential and logarithmic functions. */ | |
215 | ||
216 | /* Exponential function of X. */ | |
217 | #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp) | |
218 | ||
219 | /* Break VALUE into a normalized fraction and an integral power of 2. */ | |
220 | #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp) | |
221 | ||
222 | /* X times (two to the EXP power). */ | |
223 | #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp) | |
224 | ||
225 | /* Natural logarithm of X. */ | |
226 | #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog) | |
227 | ||
228 | /* Base-ten logarithm of X. */ | |
cc3fa755 UD |
229 | #ifdef __USE_GNU |
230 | # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10) | |
231 | #else | |
232 | # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10) | |
233 | #endif | |
dfd2257a UD |
234 | |
235 | /* Return exp(X) - 1. */ | |
236 | #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1) | |
237 | ||
238 | /* Return log(1 + X). */ | |
239 | #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p) | |
240 | ||
241 | /* Return the base 2 signed integral exponent of X. */ | |
242 | #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb) | |
243 | ||
244 | /* Compute base-2 exponential of X. */ | |
245 | #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2) | |
246 | ||
247 | /* Compute base-2 logarithm of X. */ | |
248 | #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2) | |
249 | ||
250 | ||
251 | /* Power functions. */ | |
252 | ||
253 | /* Return X to the Y power. */ | |
254 | #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow) | |
255 | ||
256 | /* Return the square root of X. */ | |
257 | #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt) | |
258 | ||
259 | /* Return `sqrt(X*X + Y*Y)'. */ | |
260 | #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot) | |
261 | ||
262 | /* Return the cube root of X. */ | |
263 | #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt) | |
264 | ||
265 | ||
266 | /* Nearest integer, absolute value, and remainder functions. */ | |
267 | ||
268 | /* Smallest integral value not less than X. */ | |
269 | #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil) | |
270 | ||
271 | /* Absolute value of X. */ | |
272 | #define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs) | |
273 | ||
274 | /* Largest integer not greater than X. */ | |
275 | #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor) | |
276 | ||
277 | /* Floating-point modulo remainder of X/Y. */ | |
278 | #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod) | |
279 | ||
280 | /* Round X to integral valuein floating-point format using current | |
281 | rounding direction, but do not raise inexact exception. */ | |
282 | #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint) | |
283 | ||
284 | /* Round X to nearest integral value, rounding halfway cases away from | |
285 | zero. */ | |
286 | #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round) | |
287 | ||
288 | /* Round X to the integral value in floating-point format nearest but | |
289 | not larger in magnitude. */ | |
290 | #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc) | |
291 | ||
292 | /* Compute remainder of X and Y and put in *QUO a value with sign of x/y | |
293 | and magnitude congruent `mod 2^n' to the magnitude of the integral | |
294 | quotient x/y, with n >= 3. */ | |
295 | #define remquo(Val1, Val2, Val3) \ | |
296 | __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo) | |
297 | ||
298 | /* Round X to nearest integral value according to current rounding | |
299 | direction. */ | |
300 | #define lrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, lrint) | |
301 | #define llrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, llrint) | |
302 | ||
303 | /* Round X to nearest integral value, rounding halfway cases away from | |
304 | zero. */ | |
305 | #define lround(Val) __TGMATH_UNARY_REAL_ONLY (Val, lround) | |
306 | #define llround(Val) __TGMATH_UNARY_REAL_ONLY (Val, llround) | |
307 | ||
308 | ||
309 | /* Return X with its signed changed to Y's. */ | |
310 | #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign) | |
311 | ||
312 | /* Error and gamma functions. */ | |
313 | #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf) | |
314 | #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc) | |
315 | #define gamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, gamma) | |
316 | #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma) | |
317 | ||
318 | ||
319 | /* Return the integer nearest X in the direction of the | |
320 | prevailing rounding mode. */ | |
321 | #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint) | |
322 | ||
323 | /* Return X + epsilon if X < Y, X - epsilon if X > Y. */ | |
324 | #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter) | |
42bd0a85 UD |
325 | #define nexttoward(Val1, Val2) \ |
326 | __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nexttoward) | |
dfd2257a UD |
327 | |
328 | /* Return the remainder of integer divison X / Y with infinite precision. */ | |
329 | #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder) | |
330 | ||
331 | /* Return X times (2 to the Nth power). */ | |
26644e87 | 332 | #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED |
dfd2257a | 333 | #define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb) |
26644e87 | 334 | #endif |
dfd2257a UD |
335 | |
336 | /* Return X times (2 to the Nth power). */ | |
337 | #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn) | |
338 | ||
339 | /* Return X times (2 to the Nth power). */ | |
340 | #define scalbln(Val1, Val2) \ | |
341 | __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln) | |
342 | ||
343 | /* Return the binary exponent of X, which must be nonzero. */ | |
344 | #define ilogb(Val) __TGMATH_UNARY_REAL_ONLY (Val, ilogb) | |
345 | ||
346 | ||
347 | /* Return positive difference between X and Y. */ | |
348 | #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim) | |
349 | ||
350 | /* Return maximum numeric value from X and Y. */ | |
351 | #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax) | |
352 | ||
353 | /* Return minimum numeric value from X and Y. */ | |
354 | #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin) | |
355 | ||
356 | ||
bfce746a UD |
357 | /* Multiply-add function computed as a ternary operation. */ |
358 | #define fma(Vat1, Val2, Val3) \ | |
359 | __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma) | |
360 | ||
361 | ||
dfd2257a UD |
362 | /* Absolute value, conjugates, and projection. */ |
363 | ||
364 | /* Argument value of Z. */ | |
365 | #define carg(Val) __TGMATH_UNARY_IMAG_ONLY (Val, carg) | |
366 | ||
367 | /* Complex conjugate of Z. */ | |
368 | #define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj) | |
369 | ||
370 | /* Projection of Z onto the Riemann sphere. */ | |
371 | #define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj) | |
372 | ||
373 | ||
374 | /* Decomposing complex values. */ | |
375 | ||
376 | /* Imaginary part of Z. */ | |
377 | #define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag) | |
378 | ||
379 | /* Real part of Z. */ | |
380 | #define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal) | |
381 | ||
382 | #endif /* tgmath.h */ |