1 /* Copyright (C) 1997 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 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.
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.
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. */
20 * ISO C 9X Standard: 7.9 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 defined __GNUC__ && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
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) \
43 (__extension__ (sizeof (__real__ (Val)) == sizeof (double) \
45 : (sizeof (__real__ (Val)) == sizeof (long double) \
49 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
50 (__extension__ (sizeof (__real__ (Val1)) == sizeof (double) \
52 : (sizeof (__real__ (Val1)) == sizeof (long double) \
53 ? Fct##l (Val1, Val2) \
54 : Fct##f (Val1, Val2))))
56 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
57 (__extension__ (sizeof (Val1) == sizeof (double) \
58 || sizeof (Val2) == sizeof (double) \
60 : (sizeof (Val1) == sizeof (long double) \
61 || sizeof (Val2) == sizeof (long double) \
62 ? Fct##l (Val1, Val2) \
63 : Fct##f (Val1, Val2))))
65 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
66 (__extension__ (sizeof (Val1) == sizeof (double) \
67 || sizeof (Val2) == sizeof (double) \
68 ? Fct (Val1, Val2, Val3) \
69 : (sizeof (Val1) == sizeof (long double) \
70 || sizeof (Val2) == sizeof (long double) \
71 ? Fct##l (Val1, Val2, Val3) \
72 : Fct##f (Val1, Val2, Val3))))
74 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
75 (__extension__ (sizeof (__real__ (Val)) == sizeof (double) \
76 ? (sizeof (__real__ (Val)) == sizeof (Val) \
79 : (sizeof (__real__ (Val)) == sizeof (long double) \
80 ? (sizeof (__real__ (Val)) == sizeof (Val) \
83 : (sizeof (__real__ (Val)) == sizeof (Val) \
87 # define __TGMATH_UNARY_IMAG_ONLY(Val, Fct) \
88 (__extension__ (sizeof (Val) == sizeof (__complex__ double) \
90 : (sizeof (Val) == sizeof (__complex__ long double) \
94 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
95 (__extension__ (sizeof (__real__ (Val1)) == sizeof (double) \
96 || sizeof (__real__ (Val2)) == sizeof (double) \
97 ? (sizeof (__real__ (Val1)) == sizeof (Val1) \
98 && sizeof (__real__ (Val2)) == sizeof (Val2) \
100 : Cfct (Val1, Val2)) \
101 : (sizeof (__real__ (Val1)) == sizeof (long double) \
102 || sizeof (__real__ (Val2)) == sizeof (long double) \
103 ? (sizeof (__real__ (Val1)) == sizeof (Val1) \
104 && sizeof (__real__ (Val2)) == sizeof (Val2) \
105 ? Fct##l (Val1, Val2) \
106 : Cfct##l (Val1, Val2)) \
107 : (sizeof (__real__ (Val1)) == sizeof (Val1) \
108 && sizeof (__real__ (Val2)) == sizeof (Val2) \
109 ? Fct##f (Val1, Val2) \
110 : Cfct##f (Val1, Val2)))))
112 # error "Unsupported compiler; you cannot use <tgmath.h>"
116 /* Unary functions defined for real and complex values. */
119 /* Trigonometric functions. */
121 /* Arc cosine of X. */
122 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
124 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
125 /* Arc tangent of X. */
126 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
127 /* Arc tangent of Y/X. */
128 #define atan2(Val) __TGMATH_UNARY_REAL_ONLY (Val, atan2)
131 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
133 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
135 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
138 /* Hyperbolic functions. */
140 /* Hyperbolic arc cosine of X. */
141 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
142 /* Hyperbolic arc sine of X. */
143 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
144 /* Hyperbolic arc tangent of X. */
145 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
147 /* Hyperbolic cosine of X. */
148 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
149 /* Hyperbolic sine of X. */
150 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
151 /* Hyperbolic tangent of X. */
152 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
155 /* Exponential and logarithmic functions. */
157 /* Exponential function of X. */
158 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
160 /* Break VALUE into a normalized fraction and an integral power of 2. */
161 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
163 /* X times (two to the EXP power). */
164 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
166 /* Natural logarithm of X. */
167 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
169 /* Base-ten logarithm of X. */
171 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
173 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
176 /* Return exp(X) - 1. */
177 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
179 /* Return log(1 + X). */
180 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
182 /* Return the base 2 signed integral exponent of X. */
183 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
185 /* Compute base-2 exponential of X. */
186 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
188 /* Compute base-2 logarithm of X. */
189 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
192 /* Power functions. */
194 /* Return X to the Y power. */
195 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
197 /* Return the square root of X. */
198 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
200 /* Return `sqrt(X*X + Y*Y)'. */
201 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
203 /* Return the cube root of X. */
204 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
207 /* Nearest integer, absolute value, and remainder functions. */
209 /* Smallest integral value not less than X. */
210 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
212 /* Absolute value of X. */
213 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs)
215 /* Largest integer not greater than X. */
216 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
218 /* Floating-point modulo remainder of X/Y. */
219 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
221 /* Round X to integral valuein floating-point format using current
222 rounding direction, but do not raise inexact exception. */
223 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
225 /* Round X to nearest integral value, rounding halfway cases away from
227 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
229 /* Round X to the integral value in floating-point format nearest but
230 not larger in magnitude. */
231 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
233 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
234 and magnitude congruent `mod 2^n' to the magnitude of the integral
235 quotient x/y, with n >= 3. */
236 #define remquo(Val1, Val2, Val3) \
237 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
239 /* Round X to nearest integral value according to current rounding
241 #define lrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, lrint)
242 #define llrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, llrint)
244 /* Round X to nearest integral value, rounding halfway cases away from
246 #define lround(Val) __TGMATH_UNARY_REAL_ONLY (Val, lround)
247 #define llround(Val) __TGMATH_UNARY_REAL_ONLY (Val, llround)
250 /* Return X with its signed changed to Y's. */
251 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
253 /* Error and gamma functions. */
254 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
255 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
256 #define gamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, gamma)
257 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
260 /* Return the integer nearest X in the direction of the
261 prevailing rounding mode. */
262 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
264 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
265 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
266 #define nextafterx(Val1, Val2) \
267 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nextafterx)
269 /* Return the remainder of integer divison X / Y with infinite precision. */
270 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
272 /* Return X times (2 to the Nth power). */
273 #define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
275 /* Return X times (2 to the Nth power). */
276 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
278 /* Return X times (2 to the Nth power). */
279 #define scalbln(Val1, Val2) \
280 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
282 /* Return the binary exponent of X, which must be nonzero. */
283 #define ilogb(Val) __TGMATH_UNARY_REAL_ONLY (Val, ilogb)
286 /* Return positive difference between X and Y. */
287 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
289 /* Return maximum numeric value from X and Y. */
290 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
292 /* Return minimum numeric value from X and Y. */
293 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
296 /* Absolute value, conjugates, and projection. */
298 /* Argument value of Z. */
299 #define carg(Val) __TGMATH_UNARY_IMAG_ONLY (Val, carg)
301 /* Complex conjugate of Z. */
302 #define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj)
304 /* Projection of Z onto the Riemann sphere. */
305 #define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj)
308 /* Decomposing complex values. */
310 /* Imaginary part of Z. */
311 #define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag)
313 /* Real part of Z. */
314 #define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal)
316 #endif /* tgmath.h */