tgmath.h revision 134733
1133333Sstefanf/*- 2133333Sstefanf * Copyright (c) 2004 Stefan Farfeleder. 3133333Sstefanf * All rights reserved. 4133333Sstefanf * 5133333Sstefanf * Redistribution and use in source and binary forms, with or without 6133333Sstefanf * modification, are permitted provided that the following conditions 7133333Sstefanf * are met: 8133333Sstefanf * 1. Redistributions of source code must retain the above copyright 9133333Sstefanf * notice, this list of conditions and the following disclaimer. 10133333Sstefanf * 2. Redistributions in binary form must reproduce the above copyright 11133333Sstefanf * notice, this list of conditions and the following disclaimer in the 12133333Sstefanf * documentation and/or other materials provided with the distribution. 13133333Sstefanf * 14133333Sstefanf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15133333Sstefanf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16133333Sstefanf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17133333Sstefanf * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18133333Sstefanf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19133333Sstefanf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20133333Sstefanf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21133333Sstefanf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22133333Sstefanf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23133333Sstefanf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24133333Sstefanf * SUCH DAMAGE. 25133333Sstefanf * 26133333Sstefanf * $FreeBSD: head/include/tgmath.h 134733 2004-09-03 23:26:55Z stefanf $ 27133333Sstefanf */ 28133333Sstefanf 29133333Sstefanf#ifndef _TGMATH_H_ 30133333Sstefanf#define _TGMATH_H_ 31133333Sstefanf 32133333Sstefanf#include <complex.h> 33133333Sstefanf#include <math.h> 34133333Sstefanf 35133333Sstefanf/* 36133333Sstefanf * This implementation of <tgmath.h> requires two implementation-dependent 37133333Sstefanf * macros to be defined: 38133333Sstefanf * __tg_impl_simple(x, y, z, fn, fnf, fnl, ...) 39133333Sstefanf * Invokes fnl() if the corresponding real type of x, y or z is long 40133333Sstefanf * double, fn() if it is double or any has an integer type, and fnf() 41133333Sstefanf * otherwise. 42133333Sstefanf * __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...) 43133333Sstefanf * Invokes [c]fnl() if the corresponding real type of x, y or z is long 44133333Sstefanf * double, [c]fn() if it is double or any has an integer type, and 45133333Sstefanf * [c]fnf() otherwise. The function with the 'c' prefix is called if 46133333Sstefanf * any of x, y or z is a complex number. 47133333Sstefanf * Both macros call the chosen function with all additional arguments passed 48133333Sstefanf * to them, as given by __VA_ARGS__. 49133333Sstefanf * 50133333Sstefanf * Note that these macros cannot be implemented with C's ?: operator, 51133333Sstefanf * because the return type of the whole expression would incorrectly be long 52133333Sstefanf * double complex regardless of the argument types. 53133333Sstefanf */ 54133333Sstefanf 55133333Sstefanf#if __GNUC_PREREQ__(3, 1) 56133333Sstefanf#define __tg_type(e, t) __builtin_types_compatible_p(__typeof__(e), t) 57133333Sstefanf#define __tg_type3(e1, e2, e3, t) \ 58133333Sstefanf (__tg_type(e1, t) || __tg_type(e2, t) || __tg_type(e3, t)) 59133333Sstefanf#define __tg_type_corr(e1, e2, e3, t) \ 60133333Sstefanf (__tg_type3(e1, e2, e3, t) || __tg_type3(e1, e2, e3, t complex)) 61133333Sstefanf#define __tg_integer(e1, e2, e3) \ 62133333Sstefanf (((__typeof__(e1))1.5 == 1) || ((__typeof__(e2))1.5 == 1) || \ 63133333Sstefanf ((__typeof__(e3))1.5 == 1)) 64133333Sstefanf#define __tg_is_complex(e1, e2, e3) \ 65133333Sstefanf (__tg_type3(e1, e2, e3, float complex) || \ 66133333Sstefanf __tg_type3(e1, e2, e3, double complex) || \ 67133333Sstefanf __tg_type3(e1, e2, e3, long double complex)) 68133333Sstefanf 69133333Sstefanf#define __tg_impl_simple(x, y, z, fn, fnf, fnl, ...) \ 70133333Sstefanf __builtin_choose_expr(__tg_type_corr(x, y, z, long double), \ 71133333Sstefanf fnl(__VA_ARGS__), __builtin_choose_expr( \ 72133333Sstefanf __tg_type_corr(x, y, z, double) || __tg_integer(x, y, z),\ 73133333Sstefanf fn(__VA_ARGS__), fnf(__VA_ARGS__))) 74133333Sstefanf 75133333Sstefanf#define __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...) \ 76133333Sstefanf __builtin_choose_expr(__tg_is_complex(x, y, z), \ 77133333Sstefanf __tg_impl_simple(x, y, z, cfn, cfnf, cfnl, __VA_ARGS__), \ 78133333Sstefanf __tg_impl_simple(x, y, z, fn, fnf, fnl, __VA_ARGS__)) 79133333Sstefanf 80133333Sstefanf#else /* __GNUC__ */ 81133333Sstefanf#error "<tgmath.h> not implemented for this compiler" 82133333Sstefanf#endif /* !__GNUC__ */ 83133333Sstefanf 84133333Sstefanf/* Macros to save lots of repetition below */ 85133333Sstefanf#define __tg_simple(x, fn) \ 86133333Sstefanf __tg_impl_simple(x, x, x, fn, fn##f, fn##l, x) 87133333Sstefanf#define __tg_simple2(x, y, fn) \ 88133333Sstefanf __tg_impl_simple(x, x, y, fn, fn##f, fn##l, x, y) 89133333Sstefanf#define __tg_simplev(x, fn, ...) \ 90133333Sstefanf __tg_impl_simple(x, x, x, fn, fn##f, fn##l, __VA_ARGS__) 91133333Sstefanf#define __tg_full(x, fn) \ 92133333Sstefanf __tg_impl_full(x, x, x, fn, fn##f, fn##l, c##fn, c##fn##f, c##fn##l, x) 93133333Sstefanf 94133333Sstefanf/* 7.22#4 -- These macros expand to real or complex functions, depending on 95133333Sstefanf * the type of their arguments. */ 96133333Sstefanf#define acos(x) __tg_full(x, acos) 97133333Sstefanf#define asin(x) __tg_full(x, asin) 98133333Sstefanf#define atan(x) __tg_full(x, atan) 99133333Sstefanf#define acosh(x) __tg_full(x, acosh) 100133333Sstefanf#define asinh(x) __tg_full(x, asinh) 101133333Sstefanf#define atanh(x) __tg_full(x, atanh) 102133333Sstefanf#define cos(x) __tg_full(x, cos) 103133333Sstefanf#define sin(x) __tg_full(x, sin) 104133333Sstefanf#define tan(x) __tg_full(x, tan) 105133333Sstefanf#define cosh(x) __tg_full(x, cosh) 106133333Sstefanf#define sinh(x) __tg_full(x, sinh) 107133333Sstefanf#define tanh(x) __tg_full(x, tanh) 108133333Sstefanf#define exp(x) __tg_full(x, exp) 109133333Sstefanf#define log(x) __tg_full(x, log) 110133333Sstefanf#define pow(x, y) __tg_impl_full(x, x, y, pow, powf, powl, \ 111133333Sstefanf cpow, cpowf, cpowl, x, y) 112133333Sstefanf#define sqrt(x) __tg_full(x, sqrt) 113133333Sstefanf 114133333Sstefanf/* "The corresponding type-generic macro for fabs and cabs is fabs." */ 115133333Sstefanf#define fabs(x) __tg_impl_full(x, x, x, fabs, fabsf, fabsl, \ 116133333Sstefanf cabs, cabsf, cabsl, x) 117133333Sstefanf 118133333Sstefanf/* 7.22#5 -- These macros are only defined for arguments with real type. */ 119133333Sstefanf#define atan2(x, y) __tg_simple2(x, y, atan2) 120133333Sstefanf#define cbrt(x) __tg_simple(x, cbrt) 121133333Sstefanf#define ceil(x) __tg_simple(x, ceil) 122133333Sstefanf#define copysign(x, y) __tg_simple2(x, y, copysign) 123133333Sstefanf#define erf(x) __tg_simple(x, erf) 124133333Sstefanf#define erfc(x) __tg_simple(x, erfc) 125133333Sstefanf#define exp2(x) __tg_simple(x, exp2) 126133333Sstefanf#define expm1(x) __tg_simple(x, expm1) 127133333Sstefanf#define fdim(x, y) __tg_simple2(x, y, fdim) 128133333Sstefanf#define floor(x) __tg_simple(x, floor) 129133333Sstefanf#define fma(x, y, z) __tg_impl_simple(x, y, z, fma, fmaf, fmal, x, y, z) 130133333Sstefanf#define fmax(x, y) __tg_simple2(x, y, fmax) 131133333Sstefanf#define fmin(x, y) __tg_simple2(x, y, fmin) 132133333Sstefanf#define fmod(x, y) __tg_simple2(x, y, fmod) 133133333Sstefanf#define frexp(x, y) __tg_simplev(x, frexp, x, y) 134133333Sstefanf#define hypot(x, y) __tg_simple2(x, y, hypot) 135133333Sstefanf#define ilogb(x) __tg_simple(x, ilogb) 136133333Sstefanf#define ldexp(x, y) __tg_simplev(x, ldexp, x, y) 137133333Sstefanf#define lgamma(x) __tg_simple(x, lgamma) 138133333Sstefanf#define llrint(x) __tg_simple(x, llrint) 139133333Sstefanf#define llround(x) __tg_simple(x, llround) 140133333Sstefanf#define log10(x) __tg_simple(x, log10) 141133333Sstefanf#define log1p(x) __tg_simple(x, log1p) 142133333Sstefanf#define log2(x) __tg_simple(x, log2) 143133333Sstefanf#define logb(x) __tg_simple(x, logb) 144133333Sstefanf#define lrint(x) __tg_simple(x, lrint) 145133333Sstefanf#define lround(x) __tg_simple(x, lround) 146134733Sstefanf#define nearbyint(x) __tg_simple(x, nearbyint) 147133333Sstefanf#define nextafter(x, y) __tg_simple2(x, y, nextafter) 148133333Sstefanf#define nexttoward(x, y) __tg_simplev(x, nexttoward, x, y) 149133333Sstefanf#define remainder(x, y) __tg_simple2(x, y, remainder) 150133333Sstefanf#define remquo(x, y, z) __tg_impl_simple(x, x, y, remquo, remquof, \ 151133333Sstefanf remquol, x, y, z) 152133333Sstefanf#define rint(x) __tg_simple(x, rint) 153133333Sstefanf#define round(x) __tg_simple(x, round) 154133333Sstefanf#define scalbn(x, y) __tg_simplev(x, scalbn, x, y) 155133333Sstefanf#define scalbln(x, y) __tg_simplev(x, scalbln, x, y) 156133333Sstefanf#define tgamma(x) __tg_simple(x, tgamma) 157133333Sstefanf#define trunc(x) __tg_simple(x, trunc) 158133333Sstefanf 159133333Sstefanf/* 7.22#6 -- These macros always expand to complex functions. */ 160133333Sstefanf#define carg(x) __tg_simple(x, carg) 161133333Sstefanf#define cimag(x) __tg_simple(x, cimag) 162133333Sstefanf#define conj(x) __tg_simple(x, conj) 163133333Sstefanf#define cproj(x) __tg_simple(x, cproj) 164133333Sstefanf#define creal(x) __tg_simple(x, creal) 165133333Sstefanf 166133333Sstefanf#endif /* !_TGMATH_H_ */ 167