tgmath.h revision 249995
1270096Strasz/*- 2270096Strasz * Copyright (c) 2004 Stefan Farfeleder. 3270096Strasz * All rights reserved. 4270096Strasz * 5270096Strasz * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org> 6270096Strasz * All rights reserved. 7270096Strasz * 8270096Strasz * Redistribution and use in source and binary forms, with or without 9270096Strasz * modification, are permitted provided that the following conditions 10270096Strasz * are met: 11270096Strasz * 1. Redistributions of source code must retain the above copyright 12270096Strasz * notice, this list of conditions and the following disclaimer. 13270096Strasz * 2. Redistributions in binary form must reproduce the above copyright 14270096Strasz * notice, this list of conditions and the following disclaimer in the 15270096Strasz * documentation and/or other materials provided with the distribution. 16270096Strasz * 17270096Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18270096Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19270096Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20270096Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21270096Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22270096Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23270096Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24270096Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25270096Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26270096Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27270096Strasz * SUCH DAMAGE. 28270096Strasz * 29270096Strasz * $FreeBSD: head/include/tgmath.h 249995 2013-04-27 21:18:34Z ed $ 30270096Strasz */ 31270897Strasz 32270897Strasz#ifndef _TGMATH_H_ 33270897Strasz#define _TGMATH_H_ 34270096Strasz 35270096Strasz#include <complex.h> 36270096Strasz#include <math.h> 37270096Strasz 38270096Strasz/* 39270096Strasz * This implementation of <tgmath.h> uses the two following macros, 40270096Strasz * which are based on the macros described in C11 proposal N1404: 41270096Strasz * __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) 42270096Strasz * Invokes fnl() if the corresponding real type of x, y or z is long 43270096Strasz * double, fn() if it is double or any has an integer type, and fnf() 44270096Strasz * otherwise. 45270096Strasz * __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) 46270096Strasz * Invokes [c]fnl() if the corresponding real type of x or y is long 47270096Strasz * double, [c]fn() if it is double or any has an integer type, and 48270096Strasz * [c]fnf() otherwise. The function with the 'c' prefix is called if 49270096Strasz * any of x or y is a complex number. 50270096Strasz * Both macros call the chosen function with all additional arguments passed 51270096Strasz * to them, as given by __VA_ARGS__. 52270096Strasz * 53270096Strasz * Note that these macros cannot be implemented with C's ?: operator, 54270096Strasz * because the return type of the whole expression would incorrectly be long 55270096Strasz * double complex regardless of the argument types. 56270096Strasz * 57270096Strasz * The structure of the C11 implementation of these macros can in 58270096Strasz * principle be reused for non-C11 compilers, but due to an integer 59270096Strasz * promotion bug for complex types in GCC 4.2, simply let non-C11 60270096Strasz * compilers use an inefficient yet reliable version. 61270096Strasz */ 62270096Strasz 63270096Strasz#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \ 64270096Strasz __has_extension(c_generic_selections) 65270096Strasz#define __tg_generic(x, cfnl, cfn, cfnf, fnl, fn, fnf) \ 66270096Strasz _Generic(x, \ 67270096Strasz long double _Complex: cfnl, \ 68270096Strasz double _Complex: cfn, \ 69270096Strasz float _Complex: cfnf, \ 70270096Strasz long double: fnl, \ 71270096Strasz default: fn, \ 72270096Strasz float: fnf \ 73270096Strasz ) 74270096Strasz#define __tg_type(x) \ 75270096Strasz __tg_generic(x, (long double _Complex)0, (double _Complex)0, \ 76270096Strasz (float _Complex)0, (long double)0, (double)0, (float)0) 77270096Strasz#define __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) \ 78270096Strasz __tg_generic( \ 79270096Strasz __tg_type(x) + __tg_type(y) + __tg_type(z), \ 80270096Strasz fnl, fn, fnf, fnl, fn, fnf)(__VA_ARGS__) 81270096Strasz#define __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) \ 82270096Strasz __tg_generic( \ 83270096Strasz __tg_type(x) + __tg_type(y), \ 84270096Strasz cfnl, cfn, cfnf, fnl, fn, fnf)(__VA_ARGS__) 85270096Strasz#elif defined(__generic) 86270096Strasz#define __tg_generic_simple(x, fnl, fn, fnf) \ 87270096Strasz __generic(x, long double _Complex, fnl, \ 88270096Strasz __generic(x, double _Complex, fn, \ 89270096Strasz __generic(x, float _Complex, fnf, \ 90270096Strasz __generic(x, long double, fnl, \ 91270096Strasz __generic(x, float, fnf, fn))))) 92270096Strasz#define __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) \ 93270096Strasz __tg_generic_simple(x, \ 94270096Strasz __tg_generic_simple(y, \ 95270096Strasz __tg_generic_simple(z, fnl, fnl, fnl), \ 96270096Strasz __tg_generic_simple(z, fnl, fnl, fnl), \ 97270096Strasz __tg_generic_simple(z, fnl, fnl, fnl)), \ 98270096Strasz __tg_generic_simple(y, \ 99270096Strasz __tg_generic_simple(z, fnl, fnl, fnl), \ 100270096Strasz __tg_generic_simple(z, fnl, fn , fn ), \ 101270096Strasz __tg_generic_simple(z, fnl, fn , fn )), \ 102270096Strasz __tg_generic_simple(y, \ 103270096Strasz __tg_generic_simple(z, fnl, fnl, fnl), \ 104270096Strasz __tg_generic_simple(z, fnl, fn , fn ), \ 105270096Strasz __tg_generic_simple(z, fnl, fn , fnf)))(__VA_ARGS__) 106270096Strasz#define __tg_generic_full(x, cfnl, cfn, cfnf, fnl, fn, fnf) \ 107270096Strasz __generic(x, long double _Complex, cfnl, \ 108270096Strasz __generic(x, double _Complex, cfn, \ 109270096Strasz __generic(x, float _Complex, cfnf, \ 110270096Strasz __generic(x, long double, fnl, \ 111270096Strasz __generic(x, float, fnf, fn))))) 112270096Strasz#define __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) \ 113270096Strasz __tg_generic_full(x, \ 114270096Strasz __tg_generic_full(y, cfnl, cfnl, cfnl, cfnl, cfnl, cfnl), \ 115270096Strasz __tg_generic_full(y, cfnl, cfn , cfn , cfnl, cfn , cfn ), \ 116270096Strasz __tg_generic_full(y, cfnl, cfn , cfnf, cfnl, cfn , cfnf), \ 117270096Strasz __tg_generic_full(y, cfnl, cfnl, cfnl, fnl , fnl , fnl ), \ 118270096Strasz __tg_generic_full(y, cfnl, cfn , cfn , fnl , fn , fn ), \ 119270096Strasz __tg_generic_full(y, cfnl, cfn , cfnf, fnl , fn , fnf )) \ 120270096Strasz (__VA_ARGS__) 121270096Strasz#else 122270096Strasz#error "<tgmath.h> not implemented for this compiler" 123270096Strasz#endif 124270096Strasz 125270096Strasz/* Macros to save lots of repetition below */ 126270096Strasz#define __tg_simple(x, fn) \ 127270096Strasz __tg_impl_simple(x, x, x, fn##l, fn, fn##f, x) 128270096Strasz#define __tg_simple2(x, y, fn) \ 129270096Strasz __tg_impl_simple(x, x, y, fn##l, fn, fn##f, x, y) 130270096Strasz#define __tg_simple3(x, y, z, fn) \ 131270096Strasz __tg_impl_simple(x, y, z, fn##l, fn, fn##f, x, y, z) 132270096Strasz#define __tg_simplev(x, fn, ...) \ 133270096Strasz __tg_impl_simple(x, x, x, fn##l, fn, fn##f, __VA_ARGS__) 134270096Strasz#define __tg_full(x, fn) \ 135270096Strasz __tg_impl_full(x, x, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x) 136270096Strasz#define __tg_full2(x, y, fn) \ 137270096Strasz __tg_impl_full(x, y, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x, y) 138270096Strasz 139270096Strasz/* 7.22#4 -- These macros expand to real or complex functions, depending on 140270096Strasz * the type of their arguments. */ 141270096Strasz#define acos(x) __tg_full(x, acos) 142270096Strasz#define asin(x) __tg_full(x, asin) 143270096Strasz#define atan(x) __tg_full(x, atan) 144283235Strasz#define acosh(x) __tg_full(x, acosh) 145283235Strasz#define asinh(x) __tg_full(x, asinh) 146270096Strasz#define atanh(x) __tg_full(x, atanh) 147270096Strasz#define cos(x) __tg_full(x, cos) 148270096Strasz#define sin(x) __tg_full(x, sin) 149270096Strasz#define tan(x) __tg_full(x, tan) 150270096Strasz#define cosh(x) __tg_full(x, cosh) 151270096Strasz#define sinh(x) __tg_full(x, sinh) 152270096Strasz#define tanh(x) __tg_full(x, tanh) 153270096Strasz#define exp(x) __tg_full(x, exp) 154270096Strasz#define log(x) __tg_full(x, log) 155270096Strasz#define pow(x, y) __tg_full2(x, y, pow) 156270096Strasz#define sqrt(x) __tg_full(x, sqrt) 157270096Strasz 158270096Strasz/* "The corresponding type-generic macro for fabs and cabs is fabs." */ 159270096Strasz#define fabs(x) __tg_impl_full(x, x, cabsl, cabs, cabsf, \ 160270096Strasz fabsl, fabs, fabsf, x) 161270096Strasz 162270096Strasz/* 7.22#5 -- These macros are only defined for arguments with real type. */ 163270096Strasz#define atan2(x, y) __tg_simple2(x, y, atan2) 164270096Strasz#define cbrt(x) __tg_simple(x, cbrt) 165270096Strasz#define ceil(x) __tg_simple(x, ceil) 166270096Strasz#define copysign(x, y) __tg_simple2(x, y, copysign) 167270096Strasz#define erf(x) __tg_simple(x, erf) 168270096Strasz#define erfc(x) __tg_simple(x, erfc) 169270096Strasz#define exp2(x) __tg_simple(x, exp2) 170270096Strasz#define expm1(x) __tg_simple(x, expm1) 171270096Strasz#define fdim(x, y) __tg_simple2(x, y, fdim) 172270096Strasz#define floor(x) __tg_simple(x, floor) 173270096Strasz#define fma(x, y, z) __tg_simple3(x, y, z, fma) 174270096Strasz#define fmax(x, y) __tg_simple2(x, y, fmax) 175270096Strasz#define fmin(x, y) __tg_simple2(x, y, fmin) 176270096Strasz#define fmod(x, y) __tg_simple2(x, y, fmod) 177270096Strasz#define frexp(x, y) __tg_simplev(x, frexp, x, y) 178283235Strasz#define hypot(x, y) __tg_simple2(x, y, hypot) 179270096Strasz#define ilogb(x) __tg_simple(x, ilogb) 180270096Strasz#define ldexp(x, y) __tg_simplev(x, ldexp, x, y) 181270096Strasz#define lgamma(x) __tg_simple(x, lgamma) 182270096Strasz#define llrint(x) __tg_simple(x, llrint) 183270096Strasz#define llround(x) __tg_simple(x, llround) 184270096Strasz#define log10(x) __tg_simple(x, log10) 185270096Strasz#define log1p(x) __tg_simple(x, log1p) 186270096Strasz#define log2(x) __tg_simple(x, log2) 187283235Strasz#define logb(x) __tg_simple(x, logb) 188270096Strasz#define lrint(x) __tg_simple(x, lrint) 189270096Strasz#define lround(x) __tg_simple(x, lround) 190270096Strasz#define nearbyint(x) __tg_simple(x, nearbyint) 191270096Strasz#define nextafter(x, y) __tg_simple2(x, y, nextafter) 192270096Strasz#define nexttoward(x, y) __tg_simplev(x, nexttoward, x, y) 193270096Strasz#define remainder(x, y) __tg_simple2(x, y, remainder) 194270096Strasz#define remquo(x, y, z) __tg_impl_simple(x, x, y, remquol, remquo, \ 195270096Strasz remquof, x, y, z) 196270096Strasz#define rint(x) __tg_simple(x, rint) 197270096Strasz#define round(x) __tg_simple(x, round) 198270096Strasz#define scalbn(x, y) __tg_simplev(x, scalbn, x, y) 199270096Strasz#define scalbln(x, y) __tg_simplev(x, scalbln, x, y) 200270096Strasz#define tgamma(x) __tg_simple(x, tgamma) 201270096Strasz#define trunc(x) __tg_simple(x, trunc) 202270096Strasz 203270096Strasz/* 7.22#6 -- These macros always expand to complex functions. */ 204270096Strasz#define carg(x) __tg_simple(x, carg) 205270096Strasz#define cimag(x) __tg_simple(x, cimag) 206270096Strasz#define conj(x) __tg_simple(x, conj) 207270096Strasz#define cproj(x) __tg_simple(x, cproj) 208270096Strasz#define creal(x) __tg_simple(x, creal) 209270096Strasz 210270096Strasz#endif /* !_TGMATH_H_ */ 211270096Strasz