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