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