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