1 /*- 2 * Copyright (c) 2008 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Matt Thomas <matt@3am-software.com> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #ifndef _TGMATH_H_ 31 #define _TGMATH_H_ 32 33 #include <math.h> 34 #include <complex.h> 35 36 /* 37 * C99 Type-generic math (7.22) 38 */ 39 #ifdef __GNUC__ 40 #define __TG_CHOOSE(p, a, b) __builtin_choose_expr((p), (a), (b)) 41 #define __TG_IS_EQUIV_TYPE_P(v, t) \ 42 __builtin_types_compatible_p(__typeof__(v), t) 43 #else 44 #error how does this compler do type-generic macros? 45 #endif 46 47 #define __TG_IS_FCOMPLEX_P(t) __TG_IS_EQUIV_TYPE_P(t, float complex) 48 #define __TG_IS_DCOMPLEX_P(t) __TG_IS_EQUIV_TYPE_P(t, double complex) 49 #define __TG_IS_LCOMPLEX_P(t) __TG_IS_EQUIV_TYPE_P(t, long double complex) 50 #define __TG_IS_FLOAT_P(t) __TG_IS_EQUIV_TYPE_P(t, float) 51 #define __TG_IS_LDOUBLE_P(t) __TG_IS_EQUIV_TYPE_P(t, long double) 52 #define __TG_IS_FREAL_P(t) (__TG_IS_FLOAT_P(t) || __TG_IS_FCOMPLEX_P(t)) 53 #define __TG_IS_LREAL_P(t) (__TG_IS_LDOUBLE_P(t) || __TG_IS_LCOMPLEX_P(t)) 54 55 #define __TG_IS_COMPLEX_P(t) \ 56 (__TG_IS_FCOMPLEX_P(t) \ 57 || __TG_IS_DCOMPLEX_P(t) \ 58 || __TG_IS_LCOMPLEX_P(t)) 59 60 #define __TG_GFN1(fn, a, ftype, ltype) \ 61 __TG_CHOOSE(__TG_IS_##ftype##_P(a), \ 62 fn##f(a), \ 63 __TG_CHOOSE(__TG_IS_##ltype##_P(a), \ 64 fn##l(a), \ 65 fn(a))) 66 67 #define __TG_GFN1x(fn, a, b, ftype, ltype) \ 68 __TG_CHOOSE(__TG_IS_##ftype##_P(a), \ 69 fn##f((a), (b)), \ 70 __TG_CHOOSE(__TG_IS_##ltype##_P(a), \ 71 fn##l((a), (b)), \ 72 fn((a), (b)))) 73 74 #define __TG_GFN2(fn, a, b, ftype, ltype) \ 75 __TG_CHOOSE(__TG_IS_##ftype##_P(a) \ 76 && __TG_IS_##ftype##_P(b), \ 77 fn##f((a), (b)), \ 78 __TG_CHOOSE(__TG_IS_##ltype##_P(a) \ 79 || __TG_IS_##ltype##_P(b), \ 80 fn##l((a), (b)), \ 81 fn((a), (b)))) 82 83 #define __TG_GFN2x(fn, a, b, c, ftype, ltype) \ 84 __TG_CHOOSE(__TG_IS_##ftype##_P(a) \ 85 && __TG_IS_##ftype##_P(b), \ 86 fn##f((a), (b), (c)), \ 87 __TG_CHOOSE(__TG_IS_##ltype##_P(a) \ 88 || __TG_IS_##ltype##_P(b), \ 89 fn##l((a), (b), (c)), \ 90 fn((a), (b), (c)))) 91 92 #define __TG_GFN3(fn, a, b, c, ftype, ltype) \ 93 __TG_CHOOSE(__TG_IS_##ftype##_P(a) \ 94 && __TG_IS_##ftype##_P(b) \ 95 && __TG_IS_##ftype##_P(c), \ 96 fn##f((a), (b), (c)), \ 97 __TG_CHOOSE(__TG_IS_##ltype##_P(a) \ 98 || __TG_IS_##ltype##_P(b) \ 99 || __TG_IS_##ltype##_P(c), \ 100 fn##l((a), (b), (c)), \ 101 fn((a), (b), (c)))) 102 103 104 #define __TG_CFN1(cfn, a) __TG_GFN1(cfn, a, FREAL, LREAL) 105 #define __TG_CFN2(cfn, a, b) __TG_GFN2(cfn, a, b, FREAL, LREAL) 106 107 #define __TG_FN1(fn, a) __TG_GFN1(fn, a, FLOAT, LDOUBLE) 108 #define __TG_FN1x(fn, a, b) __TG_GFN1x(fn, a, b, FLOAT, LDOUBLE) 109 #define __TG_FN2(fn, a, b) __TG_GFN2(fn, a, b, FLOAT, LDOUBLE) 110 #define __TG_FN2x(fn, a, b, c) __TG_GFN2x(fn, a, b, c, FLOAT, LDOUBLE) 111 #define __TG_FN3(fn, a, b, c) __TG_GFN3(fn, a, b, c, FLOAT, LDOUBLE) 112 113 #define __TG_COMPLEX(a, fn) \ 114 __TG_CHOOSE(__TG_IS_COMPLEX_P(a), \ 115 __TG_CFN1(c##fn, (a)), \ 116 __TG_FN1(fn, (a))) 117 118 #define __TG_COMPLEX1(a, cfn, fn) \ 119 __TG_CHOOSE(__TG_IS_COMPLEX_P(a), \ 120 __TG_CFN1(cfn, (a)), \ 121 __TG_FN1(fn, (a))) 122 123 #define __TG_COMPLEX2(a, b, fn) \ 124 __TG_CHOOSE(__TG_IS_COMPLEX_P(a) \ 125 || __TG_IS_COMPLEX_P(b), \ 126 __TG_CFN2(c##fn, (a), (b)), \ 127 __TG_FN2(fn, (a), (b))) 128 129 #define acos(a) __TG_COMPLEX((a), acos) 130 #define asin(a) __TG_COMPLEX((a), asin) 131 #define atan(a) __TG_COMPLEX((a), atan) 132 #define acosh(a) __TG_COMPLEX((a), acosh) 133 #define asinh(a) __TG_COMPLEX((a), asinh) 134 #define atanh(a) __TG_COMPLEX((a), atanh) 135 #define cos(a) __TG_COMPLEX((a), cos) 136 #define sin(a) __TG_COMPLEX((a), sin) 137 #define tan(a) __TG_COMPLEX((a), tan) 138 #define cosh(a) __TG_COMPLEX((a), cosh) 139 #define sinh(a) __TG_COMPLEX((a), sinh) 140 #define tanh(a) __TG_COMPLEX((a), tanh) 141 #define exp(a) __TG_COMPLEX((a), exp) 142 #define log(a) __TG_COMPLEX((a), log) 143 #define pow(a,b) __TG_COMPLEX2((a), (b), pow) 144 #define sqrt(a) __TG_COMPLEX((a), sqrt) 145 #define fabs(a) __TG_COMPLEX1((a), cabs, fabs) 146 147 #define atan2(a,b) __TG_FN2(atan2, (a), (b)) 148 #define cbrt(a) __TG_FN1(cbrt, (a)) 149 #define ceil(a) __TG_FN1(ceil, (a)) 150 #define copysign(a,b) __TG_FN2(copysign, (a), (b)) 151 #define erf(a) __TG_FN1(erf, (a)) 152 #define erfc(a) __TG_FN1(erfc, (a)) 153 #define exp2(a) __TG_FN1(exp2, (a)) 154 #define expm1(a) __TG_FN1(expm1, (a)) 155 #define fdim(a,b) __TG_FN2(fdim, (a), (b)) 156 #define floor(a) __TG_FN1(floor, (a)) 157 #define fma(a,b,c) __TG_FN3(fma, (a), (b), (c)) 158 #define fmax(a,b) __TG_FN2(fmax, (a), (b)) 159 #define fmin(a,b) __TG_FN2(fmin, (a), (b)) 160 #define fmod(a,b) __TG_FN2(fmod, (a), (b)) 161 #define frexp(a,b) __TG_FN1x(frexp, (a), (b)) 162 #define hypot(a,b) __TG_FN2(hypot, (a), (b)) 163 #define ilogb(a) __TG_FN1(ilogb, (a)) 164 #define ldexp(a,b) __TG_FN1x(ldexp, (a), (b)) 165 #define lgamma(a) __TG_FN1(lgamma, (a)) 166 #define llrint(a) __TG_FN1(llrint, (a)) 167 #define llround(a) __TG_FN1(llround, (a)) 168 #define log10(a) __TG_FN1(log10, (a)) 169 #define log1p(a) __TG_FN1(log1p, (a)) 170 #define log2(a) __TG_FN1(log2, (a)) 171 #define logb(a) __TG_FN1(logb, (a)) 172 #define lrint(a) __TG_FN1(lrint, (a)) 173 #define lround(a) __TG_FN1(lround, (a)) 174 #define nearbyint(a) __TG_FN1(nearbyint, (a)) 175 #define nextafter(a,b) __TG_FN2(nextafter, (a), (b)) 176 #define nexttoward(a,b) __TG_FN2(nexttoward, (a), (b)) 177 #define remainder(a,b) __TG_FN2(remainder, (a), (b)) 178 #define remquo(a,b,c) __TG_FN2x(remquo, (a), (b), (c)) 179 #define rint(a) __TG_FN1(rint, (a)) 180 #define round(a) __TG_FN1(round, (a)) 181 #define scalbn(a,b) __TG_FN1x(scalbn, (a), (b)) 182 #define scalb1n(a,b) __TG_FN1x(scalb1n, (a), (b)) 183 #define tgamma(a) __TG_FN1(tgamma, (a)) 184 #define trunc(a) __TG_FN1(trunc, (a)) 185 186 #define carg(a) __TG_CFN1(carg, (a)) 187 #define cimag(a) __TG_CFN1(cimag, (a)) 188 #define conj(a) __TG_CFN1(conj, (a)) 189 #define cproj(a) __TG_CFN1(cproj, (a)) 190 #define creal(a) __TG_CFN1(creal, (a)) 191 192 #endif /* !_TGMATH_H_ */ 193