1*05a0b428SJohn Marino /* $OpenBSD: tgmath.h,v 1.1 2011/07/08 19:28:06 martynas Exp $ */ 2*05a0b428SJohn Marino 3*05a0b428SJohn Marino /*- 4*05a0b428SJohn Marino * Copyright (c) 2004 Stefan Farfeleder. 5*05a0b428SJohn Marino * All rights reserved. 6*05a0b428SJohn Marino * 7*05a0b428SJohn Marino * Redistribution and use in source and binary forms, with or without 8*05a0b428SJohn Marino * modification, are permitted provided that the following conditions 9*05a0b428SJohn Marino * are met: 10*05a0b428SJohn Marino * 1. Redistributions of source code must retain the above copyright 11*05a0b428SJohn Marino * notice, this list of conditions and the following disclaimer. 12*05a0b428SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright 13*05a0b428SJohn Marino * notice, this list of conditions and the following disclaimer in the 14*05a0b428SJohn Marino * documentation and/or other materials provided with the distribution. 15*05a0b428SJohn Marino * 16*05a0b428SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17*05a0b428SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*05a0b428SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*05a0b428SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20*05a0b428SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*05a0b428SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*05a0b428SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*05a0b428SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*05a0b428SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*05a0b428SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*05a0b428SJohn Marino * SUCH DAMAGE. 27*05a0b428SJohn Marino * 28*05a0b428SJohn Marino * $FreeBSD: src/include/tgmath.h,v 1.5 2007/02/02 18:30:23 schweikh Exp $ 29*05a0b428SJohn Marino */ 30*05a0b428SJohn Marino 31*05a0b428SJohn Marino #ifndef _TGMATH_H_ 32*05a0b428SJohn Marino #define _TGMATH_H_ 33*05a0b428SJohn Marino 34*05a0b428SJohn Marino #include <complex.h> 35*05a0b428SJohn Marino #include <math.h> 36*05a0b428SJohn Marino 37*05a0b428SJohn Marino /* 38*05a0b428SJohn Marino * This implementation of <tgmath.h> requires two implementation-dependent 39*05a0b428SJohn Marino * macros to be defined: 40*05a0b428SJohn Marino * __tg_impl_simple(x, y, z, fn, fnf, fnl, ...) 41*05a0b428SJohn Marino * Invokes fnl() if the corresponding real type of x, y or z is long 42*05a0b428SJohn Marino * double, fn() if it is double or any has an integer type, and fnf() 43*05a0b428SJohn Marino * otherwise. 44*05a0b428SJohn Marino * __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...) 45*05a0b428SJohn Marino * Invokes [c]fnl() if the corresponding real type of x, y or z is long 46*05a0b428SJohn Marino * double, [c]fn() if it is double or any has an integer type, and 47*05a0b428SJohn Marino * [c]fnf() otherwise. The function with the 'c' prefix is called if 48*05a0b428SJohn Marino * any of x, y or z is a complex number. 49*05a0b428SJohn Marino * Both macros call the chosen function with all additional arguments passed 50*05a0b428SJohn Marino * to them, as given by __VA_ARGS__. 51*05a0b428SJohn Marino * 52*05a0b428SJohn Marino * Note that these macros cannot be implemented with C's ?: operator, 53*05a0b428SJohn Marino * because the return type of the whole expression would incorrectly be long 54*05a0b428SJohn Marino * double complex regardless of the argument types. 55*05a0b428SJohn Marino */ 56*05a0b428SJohn Marino 57*05a0b428SJohn Marino #if __GNUC_PREREQ__(3, 1) 58*05a0b428SJohn Marino #define __tg_type(e, t) __builtin_types_compatible_p(__typeof__(e), t) 59*05a0b428SJohn Marino #define __tg_type3(e1, e2, e3, t) \ 60*05a0b428SJohn Marino (__tg_type(e1, t) || __tg_type(e2, t) || __tg_type(e3, t)) 61*05a0b428SJohn Marino #define __tg_type_corr(e1, e2, e3, t) \ 62*05a0b428SJohn Marino (__tg_type3(e1, e2, e3, t) || __tg_type3(e1, e2, e3, t _Complex)) 63*05a0b428SJohn Marino #define __tg_integer(e1, e2, e3) \ 64*05a0b428SJohn Marino (((__typeof__(e1))1.5 == 1) || ((__typeof__(e2))1.5 == 1) || \ 65*05a0b428SJohn Marino ((__typeof__(e3))1.5 == 1)) 66*05a0b428SJohn Marino #define __tg_is_complex(e1, e2, e3) \ 67*05a0b428SJohn Marino (__tg_type3(e1, e2, e3, float _Complex) || \ 68*05a0b428SJohn Marino __tg_type3(e1, e2, e3, double _Complex) || \ 69*05a0b428SJohn Marino __tg_type3(e1, e2, e3, long double _Complex) || \ 70*05a0b428SJohn Marino __tg_type3(e1, e2, e3, __typeof__(_Complex_I))) 71*05a0b428SJohn Marino 72*05a0b428SJohn Marino #define __tg_impl_simple(x, y, z, fn, fnf, fnl, ...) \ 73*05a0b428SJohn Marino __builtin_choose_expr(__tg_type_corr(x, y, z, long double), \ 74*05a0b428SJohn Marino fnl(__VA_ARGS__), __builtin_choose_expr( \ 75*05a0b428SJohn Marino __tg_type_corr(x, y, z, double) || __tg_integer(x, y, z),\ 76*05a0b428SJohn Marino fn(__VA_ARGS__), fnf(__VA_ARGS__))) 77*05a0b428SJohn Marino 78*05a0b428SJohn Marino #define __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...) \ 79*05a0b428SJohn Marino __builtin_choose_expr(__tg_is_complex(x, y, z), \ 80*05a0b428SJohn Marino __tg_impl_simple(x, y, z, cfn, cfnf, cfnl, __VA_ARGS__), \ 81*05a0b428SJohn Marino __tg_impl_simple(x, y, z, fn, fnf, fnl, __VA_ARGS__)) 82*05a0b428SJohn Marino 83*05a0b428SJohn Marino #else /* __GNUC__ */ 84*05a0b428SJohn Marino #error "<tgmath.h> not implemented for this compiler" 85*05a0b428SJohn Marino #endif /* !__GNUC__ */ 86*05a0b428SJohn Marino 87*05a0b428SJohn Marino /* Macros to save lots of repetition below */ 88*05a0b428SJohn Marino #define __tg_simple(x, fn) \ 89*05a0b428SJohn Marino __tg_impl_simple(x, x, x, fn, fn##f, fn##l, x) 90*05a0b428SJohn Marino #define __tg_simple2(x, y, fn) \ 91*05a0b428SJohn Marino __tg_impl_simple(x, x, y, fn, fn##f, fn##l, x, y) 92*05a0b428SJohn Marino #define __tg_simplev(x, fn, ...) \ 93*05a0b428SJohn Marino __tg_impl_simple(x, x, x, fn, fn##f, fn##l, __VA_ARGS__) 94*05a0b428SJohn Marino #define __tg_full(x, fn) \ 95*05a0b428SJohn Marino __tg_impl_full(x, x, x, fn, fn##f, fn##l, c##fn, c##fn##f, c##fn##l, x) 96*05a0b428SJohn Marino 97*05a0b428SJohn Marino /* 7.22#4 -- These macros expand to real or complex functions, depending on 98*05a0b428SJohn Marino * the type of their arguments. */ 99*05a0b428SJohn Marino #define acos(x) __tg_full(x, acos) 100*05a0b428SJohn Marino #define asin(x) __tg_full(x, asin) 101*05a0b428SJohn Marino #define atan(x) __tg_full(x, atan) 102*05a0b428SJohn Marino #define acosh(x) __tg_full(x, acosh) 103*05a0b428SJohn Marino #define asinh(x) __tg_full(x, asinh) 104*05a0b428SJohn Marino #define atanh(x) __tg_full(x, atanh) 105*05a0b428SJohn Marino #define cos(x) __tg_full(x, cos) 106*05a0b428SJohn Marino #define sin(x) __tg_full(x, sin) 107*05a0b428SJohn Marino #define tan(x) __tg_full(x, tan) 108*05a0b428SJohn Marino #define cosh(x) __tg_full(x, cosh) 109*05a0b428SJohn Marino #define sinh(x) __tg_full(x, sinh) 110*05a0b428SJohn Marino #define tanh(x) __tg_full(x, tanh) 111*05a0b428SJohn Marino #define exp(x) __tg_full(x, exp) 112*05a0b428SJohn Marino #define log(x) __tg_full(x, log) 113*05a0b428SJohn Marino #define pow(x, y) __tg_impl_full(x, x, y, pow, powf, powl, \ 114*05a0b428SJohn Marino cpow, cpowf, cpowl, x, y) 115*05a0b428SJohn Marino #define sqrt(x) __tg_full(x, sqrt) 116*05a0b428SJohn Marino 117*05a0b428SJohn Marino /* "The corresponding type-generic macro for fabs and cabs is fabs." */ 118*05a0b428SJohn Marino #define fabs(x) __tg_impl_full(x, x, x, fabs, fabsf, fabsl, \ 119*05a0b428SJohn Marino cabs, cabsf, cabsl, x) 120*05a0b428SJohn Marino 121*05a0b428SJohn Marino /* 7.22#5 -- These macros are only defined for arguments with real type. */ 122*05a0b428SJohn Marino #define atan2(x, y) __tg_simple2(x, y, atan2) 123*05a0b428SJohn Marino #define cbrt(x) __tg_simple(x, cbrt) 124*05a0b428SJohn Marino #define ceil(x) __tg_simple(x, ceil) 125*05a0b428SJohn Marino #define copysign(x, y) __tg_simple2(x, y, copysign) 126*05a0b428SJohn Marino #define erf(x) __tg_simple(x, erf) 127*05a0b428SJohn Marino #define erfc(x) __tg_simple(x, erfc) 128*05a0b428SJohn Marino #define exp2(x) __tg_simple(x, exp2) 129*05a0b428SJohn Marino #define expm1(x) __tg_simple(x, expm1) 130*05a0b428SJohn Marino #define fdim(x, y) __tg_simple2(x, y, fdim) 131*05a0b428SJohn Marino #define floor(x) __tg_simple(x, floor) 132*05a0b428SJohn Marino #define fma(x, y, z) __tg_impl_simple(x, y, z, fma, fmaf, fmal, x, y, z) 133*05a0b428SJohn Marino #define fmax(x, y) __tg_simple2(x, y, fmax) 134*05a0b428SJohn Marino #define fmin(x, y) __tg_simple2(x, y, fmin) 135*05a0b428SJohn Marino #define fmod(x, y) __tg_simple2(x, y, fmod) 136*05a0b428SJohn Marino #define frexp(x, y) __tg_simplev(x, frexp, x, y) 137*05a0b428SJohn Marino #define hypot(x, y) __tg_simple2(x, y, hypot) 138*05a0b428SJohn Marino #define ilogb(x) __tg_simple(x, ilogb) 139*05a0b428SJohn Marino #define ldexp(x, y) __tg_simplev(x, ldexp, x, y) 140*05a0b428SJohn Marino #define lgamma(x) __tg_simple(x, lgamma) 141*05a0b428SJohn Marino #define llrint(x) __tg_simple(x, llrint) 142*05a0b428SJohn Marino #define llround(x) __tg_simple(x, llround) 143*05a0b428SJohn Marino #define log10(x) __tg_simple(x, log10) 144*05a0b428SJohn Marino #define log1p(x) __tg_simple(x, log1p) 145*05a0b428SJohn Marino #define log2(x) __tg_simple(x, log2) 146*05a0b428SJohn Marino #define logb(x) __tg_simple(x, logb) 147*05a0b428SJohn Marino #define lrint(x) __tg_simple(x, lrint) 148*05a0b428SJohn Marino #define lround(x) __tg_simple(x, lround) 149*05a0b428SJohn Marino #define nearbyint(x) __tg_simple(x, nearbyint) 150*05a0b428SJohn Marino #define nextafter(x, y) __tg_simple2(x, y, nextafter) 151*05a0b428SJohn Marino #define nexttoward(x, y) __tg_simplev(x, nexttoward, x, y) 152*05a0b428SJohn Marino #define remainder(x, y) __tg_simple2(x, y, remainder) 153*05a0b428SJohn Marino #define remquo(x, y, z) __tg_impl_simple(x, x, y, remquo, remquof, \ 154*05a0b428SJohn Marino remquol, x, y, z) 155*05a0b428SJohn Marino #define rint(x) __tg_simple(x, rint) 156*05a0b428SJohn Marino #define round(x) __tg_simple(x, round) 157*05a0b428SJohn Marino #define scalbn(x, y) __tg_simplev(x, scalbn, x, y) 158*05a0b428SJohn Marino #define scalbln(x, y) __tg_simplev(x, scalbln, x, y) 159*05a0b428SJohn Marino #define tgamma(x) __tg_simple(x, tgamma) 160*05a0b428SJohn Marino #define trunc(x) __tg_simple(x, trunc) 161*05a0b428SJohn Marino 162*05a0b428SJohn Marino /* 7.22#6 -- These macros always expand to complex functions. */ 163*05a0b428SJohn Marino #define carg(x) __tg_simple(x, carg) 164*05a0b428SJohn Marino #define cimag(x) __tg_simple(x, cimag) 165*05a0b428SJohn Marino #define conj(x) __tg_simple(x, conj) 166*05a0b428SJohn Marino #define cproj(x) __tg_simple(x, cproj) 167*05a0b428SJohn Marino #define creal(x) __tg_simple(x, creal) 168*05a0b428SJohn Marino 169*05a0b428SJohn Marino #endif /* !_TGMATH_H_ */ 170