1*e4b17023SJohn Marino /* Copyright (C) 2004, 2005, 2009 Free Software Foundation, Inc. 2*e4b17023SJohn Marino Contributed by Apple, Inc. 3*e4b17023SJohn Marino 4*e4b17023SJohn Marino This file is part of GCC. 5*e4b17023SJohn Marino 6*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify 7*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by 8*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option) 9*e4b17023SJohn Marino any later version. 10*e4b17023SJohn Marino 11*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, 12*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of 13*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*e4b17023SJohn Marino GNU General Public License for more details. 15*e4b17023SJohn Marino 16*e4b17023SJohn Marino Under Section 7 of GPL version 3, you are granted additional 17*e4b17023SJohn Marino permissions described in the GCC Runtime Library Exception, version 18*e4b17023SJohn Marino 3.1, as published by the Free Software Foundation. 19*e4b17023SJohn Marino 20*e4b17023SJohn Marino You should have received a copy of the GNU General Public License and 21*e4b17023SJohn Marino a copy of the GCC Runtime Library Exception along with this program; 22*e4b17023SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */ 24*e4b17023SJohn Marino 25*e4b17023SJohn Marino /* 26*e4b17023SJohn Marino * ISO C Standard: 7.22 Type-generic math <tgmath.h> 27*e4b17023SJohn Marino */ 28*e4b17023SJohn Marino 29*e4b17023SJohn Marino #ifndef _TGMATH_H 30*e4b17023SJohn Marino #define _TGMATH_H 31*e4b17023SJohn Marino 32*e4b17023SJohn Marino #include <math.h> 33*e4b17023SJohn Marino 34*e4b17023SJohn Marino #ifndef __cplusplus 35*e4b17023SJohn Marino #include <complex.h> 36*e4b17023SJohn Marino 37*e4b17023SJohn Marino /* Naming convention: generic macros are defining using 38*e4b17023SJohn Marino __TGMATH_CPLX*, __TGMATH_REAL*, and __TGMATH_CPLX_ONLY. _CPLX 39*e4b17023SJohn Marino means the generic argument(s) may be real or complex, _REAL means 40*e4b17023SJohn Marino real only, _CPLX means complex only. If there is no suffix, we are 41*e4b17023SJohn Marino defining a function of one generic argument. If the suffix is _n 42*e4b17023SJohn Marino it is a function of n generic arguments. If the suffix is _m_n it 43*e4b17023SJohn Marino is a function of n arguments, the first m of which are generic. We 44*e4b17023SJohn Marino only define these macros for values of n and/or m that are needed. */ 45*e4b17023SJohn Marino 46*e4b17023SJohn Marino /* The general rules for generic macros are given in 7.22 paragraphs 1 and 2. 47*e4b17023SJohn Marino If any generic parameter is complex, we use a complex version. Otherwise 48*e4b17023SJohn Marino we use a real version. If the real part of any generic parameter is long 49*e4b17023SJohn Marino double, we use the long double version. Otherwise if the real part of any 50*e4b17023SJohn Marino generic parameter is double or of integer type, we use the double version. 51*e4b17023SJohn Marino Otherwise we use the float version. */ 52*e4b17023SJohn Marino 53*e4b17023SJohn Marino #define __tg_cplx(expr) \ 54*e4b17023SJohn Marino __builtin_classify_type(expr) == 9 55*e4b17023SJohn Marino 56*e4b17023SJohn Marino #define __tg_ldbl(expr) \ 57*e4b17023SJohn Marino __builtin_types_compatible_p(__typeof__(expr), long double) 58*e4b17023SJohn Marino 59*e4b17023SJohn Marino #define __tg_dbl(expr) \ 60*e4b17023SJohn Marino (__builtin_types_compatible_p(__typeof__(expr), double) \ 61*e4b17023SJohn Marino || __builtin_classify_type(expr) == 1) 62*e4b17023SJohn Marino 63*e4b17023SJohn Marino #define __tg_choose(x,f,d,l) \ 64*e4b17023SJohn Marino __builtin_choose_expr(__tg_ldbl(x), l, \ 65*e4b17023SJohn Marino __builtin_choose_expr(__tg_dbl(x), d, \ 66*e4b17023SJohn Marino f)) 67*e4b17023SJohn Marino 68*e4b17023SJohn Marino #define __tg_choose_2(x,y,f,d,l) \ 69*e4b17023SJohn Marino __builtin_choose_expr(__tg_ldbl(x) || __tg_ldbl(y), l, \ 70*e4b17023SJohn Marino __builtin_choose_expr(__tg_dbl(x) || __tg_dbl(y), d, \ 71*e4b17023SJohn Marino f)) 72*e4b17023SJohn Marino 73*e4b17023SJohn Marino #define __tg_choose_3(x,y,z,f,d,l) \ 74*e4b17023SJohn Marino __builtin_choose_expr(__tg_ldbl(x) || __tg_ldbl(y) || __tg_ldbl(z), l, \ 75*e4b17023SJohn Marino __builtin_choose_expr(__tg_dbl(x) || __tg_dbl(y) \ 76*e4b17023SJohn Marino || __tg_dbl(z), d, \ 77*e4b17023SJohn Marino f)) 78*e4b17023SJohn Marino 79*e4b17023SJohn Marino #define __TGMATH_CPLX(z,R,C) \ 80*e4b17023SJohn Marino __builtin_choose_expr (__tg_cplx(z), \ 81*e4b17023SJohn Marino __tg_choose (__real__(z), C##f(z), (C)(z), C##l(z)), \ 82*e4b17023SJohn Marino __tg_choose (z, R##f(z), (R)(z), R##l(z))) 83*e4b17023SJohn Marino 84*e4b17023SJohn Marino #define __TGMATH_CPLX_2(z1,z2,R,C) \ 85*e4b17023SJohn Marino __builtin_choose_expr (__tg_cplx(z1) || __tg_cplx(z2), \ 86*e4b17023SJohn Marino __tg_choose_2 (__real__(z1), __real__(z2), \ 87*e4b17023SJohn Marino C##f(z1,z2), (C)(z1,z2), C##l(z1,z2)), \ 88*e4b17023SJohn Marino __tg_choose_2 (z1, z2, \ 89*e4b17023SJohn Marino R##f(z1,z2), (R)(z1,z2), R##l(z1,z2))) 90*e4b17023SJohn Marino 91*e4b17023SJohn Marino #define __TGMATH_REAL(x,R) \ 92*e4b17023SJohn Marino __tg_choose (x, R##f(x), (R)(x), R##l(x)) 93*e4b17023SJohn Marino #define __TGMATH_REAL_2(x,y,R) \ 94*e4b17023SJohn Marino __tg_choose_2 (x, y, R##f(x,y), (R)(x,y), R##l(x,y)) 95*e4b17023SJohn Marino #define __TGMATH_REAL_3(x,y,z,R) \ 96*e4b17023SJohn Marino __tg_choose_3 (x, y, z, R##f(x,y,z), (R)(x,y,z), R##l(x,y,z)) 97*e4b17023SJohn Marino #define __TGMATH_REAL_1_2(x,y,R) \ 98*e4b17023SJohn Marino __tg_choose (x, R##f(x,y), (R)(x,y), R##l(x,y)) 99*e4b17023SJohn Marino #define __TGMATH_REAL_2_3(x,y,z,R) \ 100*e4b17023SJohn Marino __tg_choose_2 (x, y, R##f(x,y,z), (R)(x,y,z), R##l(x,y,z)) 101*e4b17023SJohn Marino #define __TGMATH_CPLX_ONLY(z,C) \ 102*e4b17023SJohn Marino __tg_choose (__real__(z), C##f(z), (C)(z), C##l(z)) 103*e4b17023SJohn Marino 104*e4b17023SJohn Marino /* Functions defined in both <math.h> and <complex.h> (7.22p4) */ 105*e4b17023SJohn Marino #define acos(z) __TGMATH_CPLX(z, acos, cacos) 106*e4b17023SJohn Marino #define asin(z) __TGMATH_CPLX(z, asin, casin) 107*e4b17023SJohn Marino #define atan(z) __TGMATH_CPLX(z, atan, catan) 108*e4b17023SJohn Marino #define acosh(z) __TGMATH_CPLX(z, acosh, cacosh) 109*e4b17023SJohn Marino #define asinh(z) __TGMATH_CPLX(z, asinh, casinh) 110*e4b17023SJohn Marino #define atanh(z) __TGMATH_CPLX(z, atanh, catanh) 111*e4b17023SJohn Marino #define cos(z) __TGMATH_CPLX(z, cos, ccos) 112*e4b17023SJohn Marino #define sin(z) __TGMATH_CPLX(z, sin, csin) 113*e4b17023SJohn Marino #define tan(z) __TGMATH_CPLX(z, tan, ctan) 114*e4b17023SJohn Marino #define cosh(z) __TGMATH_CPLX(z, cosh, ccosh) 115*e4b17023SJohn Marino #define sinh(z) __TGMATH_CPLX(z, sinh, csinh) 116*e4b17023SJohn Marino #define tanh(z) __TGMATH_CPLX(z, tanh, ctanh) 117*e4b17023SJohn Marino #define exp(z) __TGMATH_CPLX(z, exp, cexp) 118*e4b17023SJohn Marino #define log(z) __TGMATH_CPLX(z, log, clog) 119*e4b17023SJohn Marino #define pow(z1,z2) __TGMATH_CPLX_2(z1, z2, pow, cpow) 120*e4b17023SJohn Marino #define sqrt(z) __TGMATH_CPLX(z, sqrt, csqrt) 121*e4b17023SJohn Marino #define fabs(z) __TGMATH_CPLX(z, fabs, cabs) 122*e4b17023SJohn Marino 123*e4b17023SJohn Marino /* Functions defined in <math.h> only (7.22p5) */ 124*e4b17023SJohn Marino #define atan2(x,y) __TGMATH_REAL_2(x, y, atan2) 125*e4b17023SJohn Marino #define cbrt(x) __TGMATH_REAL(x, cbrt) 126*e4b17023SJohn Marino #define ceil(x) __TGMATH_REAL(x, ceil) 127*e4b17023SJohn Marino #define copysign(x,y) __TGMATH_REAL_2(x, y, copysign) 128*e4b17023SJohn Marino #define erf(x) __TGMATH_REAL(x, erf) 129*e4b17023SJohn Marino #define erfc(x) __TGMATH_REAL(x, erfc) 130*e4b17023SJohn Marino #define exp2(x) __TGMATH_REAL(x, exp2) 131*e4b17023SJohn Marino #define expm1(x) __TGMATH_REAL(x, expm1) 132*e4b17023SJohn Marino #define fdim(x,y) __TGMATH_REAL_2(x, y, fdim) 133*e4b17023SJohn Marino #define floor(x) __TGMATH_REAL(x, floor) 134*e4b17023SJohn Marino #define fma(x,y,z) __TGMATH_REAL_3(x, y, z, fma) 135*e4b17023SJohn Marino #define fmax(x,y) __TGMATH_REAL_2(x, y, fmax) 136*e4b17023SJohn Marino #define fmin(x,y) __TGMATH_REAL_2(x, y, fmin) 137*e4b17023SJohn Marino #define fmod(x,y) __TGMATH_REAL_2(x, y, fmod) 138*e4b17023SJohn Marino #define frexp(x,y) __TGMATH_REAL_1_2(x, y, frexp) 139*e4b17023SJohn Marino #define hypot(x,y) __TGMATH_REAL_2(x, y, hypot) 140*e4b17023SJohn Marino #define ilogb(x) __TGMATH_REAL(x, ilogb) 141*e4b17023SJohn Marino #define ldexp(x,y) __TGMATH_REAL_1_2(x, y, ldexp) 142*e4b17023SJohn Marino #define lgamma(x) __TGMATH_REAL(x, lgamma) 143*e4b17023SJohn Marino #define llrint(x) __TGMATH_REAL(x, llrint) 144*e4b17023SJohn Marino #define llround(x) __TGMATH_REAL(x, llround) 145*e4b17023SJohn Marino #define log10(x) __TGMATH_REAL(x, log10) 146*e4b17023SJohn Marino #define log1p(x) __TGMATH_REAL(x, log1p) 147*e4b17023SJohn Marino #define log2(x) __TGMATH_REAL(x, log2) 148*e4b17023SJohn Marino #define logb(x) __TGMATH_REAL(x, logb) 149*e4b17023SJohn Marino #define lrint(x) __TGMATH_REAL(x, lrint) 150*e4b17023SJohn Marino #define lround(x) __TGMATH_REAL(x, lround) 151*e4b17023SJohn Marino #define nearbyint(x) __TGMATH_REAL(x, nearbyint) 152*e4b17023SJohn Marino #define nextafter(x,y) __TGMATH_REAL_2(x, y, nextafter) 153*e4b17023SJohn Marino #define nexttoward(x,y) __TGMATH_REAL_1_2(x, y, nexttoward) 154*e4b17023SJohn Marino #define remainder(x,y) __TGMATH_REAL_2(x, y, remainder) 155*e4b17023SJohn Marino #define remquo(x,y,z) __TGMATH_REAL_2_3(x, y, z, remquo) 156*e4b17023SJohn Marino #define rint(x) __TGMATH_REAL(x, rint) 157*e4b17023SJohn Marino #define round(x) __TGMATH_REAL(x, round) 158*e4b17023SJohn Marino #define scalbn(x,y) __TGMATH_REAL_1_2(x, y, scalbn) 159*e4b17023SJohn Marino #define scalbln(x,y) __TGMATH_REAL_1_2(x, y, scalbln) 160*e4b17023SJohn Marino #define tgamma(x) __TGMATH_REAL(x, tgamma) 161*e4b17023SJohn Marino #define trunc(x) __TGMATH_REAL(x, trunc) 162*e4b17023SJohn Marino 163*e4b17023SJohn Marino /* Functions defined in <complex.h> only (7.22p6) */ 164*e4b17023SJohn Marino #define carg(z) __TGMATH_CPLX_ONLY(z, carg) 165*e4b17023SJohn Marino #define cimag(z) __TGMATH_CPLX_ONLY(z, cimag) 166*e4b17023SJohn Marino #define conj(z) __TGMATH_CPLX_ONLY(z, conj) 167*e4b17023SJohn Marino #define cproj(z) __TGMATH_CPLX_ONLY(z, cproj) 168*e4b17023SJohn Marino #define creal(z) __TGMATH_CPLX_ONLY(z, creal) 169*e4b17023SJohn Marino 170*e4b17023SJohn Marino #endif /* __cplusplus */ 171*e4b17023SJohn Marino #endif /* _TGMATH_H */ 172