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