1133333Sstefanf/*- 2133333Sstefanf * Copyright (c) 2004 Stefan Farfeleder. 3133333Sstefanf * All rights reserved. 4133333Sstefanf * 5236326Stheraven * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org> 6236326Stheraven * All rights reserved. 7236326Stheraven * 8133333Sstefanf * Redistribution and use in source and binary forms, with or without 9133333Sstefanf * modification, are permitted provided that the following conditions 10133333Sstefanf * are met: 11133333Sstefanf * 1. Redistributions of source code must retain the above copyright 12133333Sstefanf * notice, this list of conditions and the following disclaimer. 13133333Sstefanf * 2. Redistributions in binary form must reproduce the above copyright 14133333Sstefanf * notice, this list of conditions and the following disclaimer in the 15133333Sstefanf * documentation and/or other materials provided with the distribution. 16133333Sstefanf * 17133333Sstefanf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18133333Sstefanf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19133333Sstefanf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20133333Sstefanf * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21133333Sstefanf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22133333Sstefanf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23133333Sstefanf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24133333Sstefanf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25133333Sstefanf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26133333Sstefanf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27133333Sstefanf * SUCH DAMAGE. 28133333Sstefanf * 29133333Sstefanf * $FreeBSD$ 30133333Sstefanf */ 31133333Sstefanf 32133333Sstefanf#ifndef _TGMATH_H_ 33133333Sstefanf#define _TGMATH_H_ 34133333Sstefanf 35133333Sstefanf#include <complex.h> 36133333Sstefanf#include <math.h> 37133333Sstefanf 38133333Sstefanf/* 39236326Stheraven * This implementation of <tgmath.h> uses the two following macros, 40236326Stheraven * which are based on the macros described in C11 proposal N1404: 41236326Stheraven * __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) 42133333Sstefanf * Invokes fnl() if the corresponding real type of x, y or z is long 43133333Sstefanf * double, fn() if it is double or any has an integer type, and fnf() 44133333Sstefanf * otherwise. 45236326Stheraven * __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) 46236326Stheraven * Invokes [c]fnl() if the corresponding real type of x or y is long 47133333Sstefanf * double, [c]fn() if it is double or any has an integer type, and 48133333Sstefanf * [c]fnf() otherwise. The function with the 'c' prefix is called if 49236326Stheraven * any of x or y is a complex number. 50133333Sstefanf * Both macros call the chosen function with all additional arguments passed 51133333Sstefanf * to them, as given by __VA_ARGS__. 52133333Sstefanf * 53133333Sstefanf * Note that these macros cannot be implemented with C's ?: operator, 54133333Sstefanf * because the return type of the whole expression would incorrectly be long 55133333Sstefanf * double complex regardless of the argument types. 56236326Stheraven * 57236326Stheraven * The structure of the C11 implementation of these macros can in 58236326Stheraven * principle be reused for non-C11 compilers, but due to an integer 59236326Stheraven * promotion bug for complex types in GCC 4.2, simply let non-C11 60236326Stheraven * compilers use an inefficient yet reliable version. 61133333Sstefanf */ 62133333Sstefanf 63236326Stheraven#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L 64236326Stheraven#define __tg_generic(x, cfnl, cfn, cfnf, fnl, fn, fnf) \ 65236326Stheraven _Generic(x, \ 66236326Stheraven long double _Complex: cfnl, \ 67236326Stheraven double _Complex: cfn, \ 68236326Stheraven float _Complex: cfnf, \ 69236326Stheraven long double: fnl, \ 70236326Stheraven default: fn, \ 71236326Stheraven float: fnf \ 72236326Stheraven ) 73236326Stheraven#define __tg_type(x) \ 74236326Stheraven __tg_generic(x, (long double _Complex)0, (double _Complex)0, \ 75236326Stheraven (float _Complex)0, (long double)0, (double)0, (float)0) 76236326Stheraven#define __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) \ 77236326Stheraven __tg_generic( \ 78236326Stheraven __tg_type(x) + __tg_type(y) + __tg_type(z), \ 79236326Stheraven fnl, fn, fnf, fnl, fn, fnf)(__VA_ARGS__) 80236326Stheraven#define __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) \ 81236326Stheraven __tg_generic( \ 82236326Stheraven __tg_type(x) + __tg_type(y), \ 83236326Stheraven cfnl, cfn, cfnf, fnl, fn, fnf)(__VA_ARGS__) 84236326Stheraven#elif defined(__generic) 85236326Stheraven#define __tg_generic_simple(x, fnl, fn, fnf) \ 86236326Stheraven __generic(x, long double _Complex, fnl, \ 87236326Stheraven __generic(x, double _Complex, fn, \ 88236326Stheraven __generic(x, float _Complex, fnf, \ 89236326Stheraven __generic(x, long double, fnl, \ 90236326Stheraven __generic(x, float, fnf, fn))))) 91236326Stheraven#define __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) \ 92236326Stheraven __tg_generic_simple(x, \ 93236326Stheraven __tg_generic_simple(y, \ 94236326Stheraven __tg_generic_simple(z, fnl, fnl, fnl), \ 95236326Stheraven __tg_generic_simple(z, fnl, fnl, fnl), \ 96236326Stheraven __tg_generic_simple(z, fnl, fnl, fnl)), \ 97236326Stheraven __tg_generic_simple(y, \ 98236326Stheraven __tg_generic_simple(z, fnl, fnl, fnl), \ 99236326Stheraven __tg_generic_simple(z, fnl, fn , fn ), \ 100236326Stheraven __tg_generic_simple(z, fnl, fn , fn )), \ 101236326Stheraven __tg_generic_simple(y, \ 102236326Stheraven __tg_generic_simple(z, fnl, fnl, fnl), \ 103236326Stheraven __tg_generic_simple(z, fnl, fn , fn ), \ 104236326Stheraven __tg_generic_simple(z, fnl, fn , fnf)))(__VA_ARGS__) 105236326Stheraven#define __tg_generic_full(x, cfnl, cfn, cfnf, fnl, fn, fnf) \ 106236326Stheraven __generic(x, long double _Complex, cfnl, \ 107236326Stheraven __generic(x, double _Complex, cfn, \ 108236326Stheraven __generic(x, float _Complex, cfnf, \ 109236326Stheraven __generic(x, long double, fnl, \ 110236326Stheraven __generic(x, float, fnf, fn))))) 111236326Stheraven#define __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) \ 112236326Stheraven __tg_generic_full(x, \ 113236326Stheraven __tg_generic_full(y, cfnl, cfnl, cfnl, cfnl, cfnl, cfnl), \ 114236326Stheraven __tg_generic_full(y, cfnl, cfn , cfn , cfnl, cfn , cfn ), \ 115236326Stheraven __tg_generic_full(y, cfnl, cfn , cfnf, cfnl, cfn , cfnf), \ 116236326Stheraven __tg_generic_full(y, cfnl, cfnl, cfnl, fnl , fnl , fnl ), \ 117236326Stheraven __tg_generic_full(y, cfnl, cfn , cfn , fnl , fn , fn ), \ 118236326Stheraven __tg_generic_full(y, cfnl, cfn , cfnf, fnl , fn , fnf )) \ 119236326Stheraven (__VA_ARGS__) 120236326Stheraven#else 121133333Sstefanf#error "<tgmath.h> not implemented for this compiler" 122236326Stheraven#endif 123133333Sstefanf 124133333Sstefanf/* Macros to save lots of repetition below */ 125133333Sstefanf#define __tg_simple(x, fn) \ 126236326Stheraven __tg_impl_simple(x, x, x, fn##l, fn, fn##f, x) 127133333Sstefanf#define __tg_simple2(x, y, fn) \ 128236326Stheraven __tg_impl_simple(x, x, y, fn##l, fn, fn##f, x, y) 129236326Stheraven#define __tg_simple3(x, y, z, fn) \ 130236326Stheraven __tg_impl_simple(x, y, z, fn##l, fn, fn##f, x, y, z) 131133333Sstefanf#define __tg_simplev(x, fn, ...) \ 132236326Stheraven __tg_impl_simple(x, x, x, fn##l, fn, fn##f, __VA_ARGS__) 133133333Sstefanf#define __tg_full(x, fn) \ 134236326Stheraven __tg_impl_full(x, x, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x) 135236326Stheraven#define __tg_full2(x, y, fn) \ 136236326Stheraven __tg_impl_full(x, y, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x, y) 137133333Sstefanf 138133333Sstefanf/* 7.22#4 -- These macros expand to real or complex functions, depending on 139133333Sstefanf * the type of their arguments. */ 140133333Sstefanf#define acos(x) __tg_full(x, acos) 141133333Sstefanf#define asin(x) __tg_full(x, asin) 142133333Sstefanf#define atan(x) __tg_full(x, atan) 143133333Sstefanf#define acosh(x) __tg_full(x, acosh) 144133333Sstefanf#define asinh(x) __tg_full(x, asinh) 145133333Sstefanf#define atanh(x) __tg_full(x, atanh) 146133333Sstefanf#define cos(x) __tg_full(x, cos) 147133333Sstefanf#define sin(x) __tg_full(x, sin) 148133333Sstefanf#define tan(x) __tg_full(x, tan) 149133333Sstefanf#define cosh(x) __tg_full(x, cosh) 150133333Sstefanf#define sinh(x) __tg_full(x, sinh) 151133333Sstefanf#define tanh(x) __tg_full(x, tanh) 152133333Sstefanf#define exp(x) __tg_full(x, exp) 153133333Sstefanf#define log(x) __tg_full(x, log) 154236326Stheraven#define pow(x, y) __tg_full2(x, y, pow) 155133333Sstefanf#define sqrt(x) __tg_full(x, sqrt) 156133333Sstefanf 157133333Sstefanf/* "The corresponding type-generic macro for fabs and cabs is fabs." */ 158236326Stheraven#define fabs(x) __tg_impl_full(x, x, cabsl, cabs, cabsf, \ 159236326Stheraven fabsl, fabs, fabsf, x) 160133333Sstefanf 161133333Sstefanf/* 7.22#5 -- These macros are only defined for arguments with real type. */ 162133333Sstefanf#define atan2(x, y) __tg_simple2(x, y, atan2) 163133333Sstefanf#define cbrt(x) __tg_simple(x, cbrt) 164133333Sstefanf#define ceil(x) __tg_simple(x, ceil) 165133333Sstefanf#define copysign(x, y) __tg_simple2(x, y, copysign) 166133333Sstefanf#define erf(x) __tg_simple(x, erf) 167133333Sstefanf#define erfc(x) __tg_simple(x, erfc) 168133333Sstefanf#define exp2(x) __tg_simple(x, exp2) 169133333Sstefanf#define expm1(x) __tg_simple(x, expm1) 170133333Sstefanf#define fdim(x, y) __tg_simple2(x, y, fdim) 171133333Sstefanf#define floor(x) __tg_simple(x, floor) 172236326Stheraven#define fma(x, y, z) __tg_simple3(x, y, z, fma) 173133333Sstefanf#define fmax(x, y) __tg_simple2(x, y, fmax) 174133333Sstefanf#define fmin(x, y) __tg_simple2(x, y, fmin) 175133333Sstefanf#define fmod(x, y) __tg_simple2(x, y, fmod) 176133333Sstefanf#define frexp(x, y) __tg_simplev(x, frexp, x, y) 177133333Sstefanf#define hypot(x, y) __tg_simple2(x, y, hypot) 178133333Sstefanf#define ilogb(x) __tg_simple(x, ilogb) 179133333Sstefanf#define ldexp(x, y) __tg_simplev(x, ldexp, x, y) 180133333Sstefanf#define lgamma(x) __tg_simple(x, lgamma) 181133333Sstefanf#define llrint(x) __tg_simple(x, llrint) 182133333Sstefanf#define llround(x) __tg_simple(x, llround) 183133333Sstefanf#define log10(x) __tg_simple(x, log10) 184133333Sstefanf#define log1p(x) __tg_simple(x, log1p) 185133333Sstefanf#define log2(x) __tg_simple(x, log2) 186133333Sstefanf#define logb(x) __tg_simple(x, logb) 187133333Sstefanf#define lrint(x) __tg_simple(x, lrint) 188133333Sstefanf#define lround(x) __tg_simple(x, lround) 189134733Sstefanf#define nearbyint(x) __tg_simple(x, nearbyint) 190133333Sstefanf#define nextafter(x, y) __tg_simple2(x, y, nextafter) 191133333Sstefanf#define nexttoward(x, y) __tg_simplev(x, nexttoward, x, y) 192133333Sstefanf#define remainder(x, y) __tg_simple2(x, y, remainder) 193236326Stheraven#define remquo(x, y, z) __tg_impl_simple(x, x, y, remquol, remquo, \ 194236326Stheraven remquof, x, y, z) 195133333Sstefanf#define rint(x) __tg_simple(x, rint) 196133333Sstefanf#define round(x) __tg_simple(x, round) 197133333Sstefanf#define scalbn(x, y) __tg_simplev(x, scalbn, x, y) 198133333Sstefanf#define scalbln(x, y) __tg_simplev(x, scalbln, x, y) 199133333Sstefanf#define tgamma(x) __tg_simple(x, tgamma) 200133333Sstefanf#define trunc(x) __tg_simple(x, trunc) 201133333Sstefanf 202133333Sstefanf/* 7.22#6 -- These macros always expand to complex functions. */ 203133333Sstefanf#define carg(x) __tg_simple(x, carg) 204133333Sstefanf#define cimag(x) __tg_simple(x, cimag) 205133333Sstefanf#define conj(x) __tg_simple(x, conj) 206133333Sstefanf#define cproj(x) __tg_simple(x, cproj) 207133333Sstefanf#define creal(x) __tg_simple(x, creal) 208133333Sstefanf 209133333Sstefanf#endif /* !_TGMATH_H_ */ 210