From 9d90c60b78b00bc1576ab97648ce3aa0ccd16a9f Mon Sep 17 00:00:00 2001 From: nsz Date: Mon, 5 Mar 2012 18:46:14 +0100 Subject: [PATCH] new tgmath.h: c99, but return types are wrong --- include/tgmath.h | 233 ++++++++++++++++++++++++++--------------------- 1 file changed, 130 insertions(+), 103 deletions(-) diff --git a/include/tgmath.h b/include/tgmath.h index 75f8aa6..08e2028 100644 --- a/include/tgmath.h +++ b/include/tgmath.h @@ -1,112 +1,139 @@ #ifndef _TGMATH_H #define _TGMATH_H -// C11 only -// See C11 6.5.1.1 for generic selections -// See C11 7.25 for tgmath.h +/* +tgmath.h is broken: return type is wrong +(always the highest conversion rank type: long double [complex]) +(imaginary type is not supported) +*/ #include #include -#define __tg_real(__fun, x) _Generic(x, \ - float: __fun ## f, \ - long double: __fun ## l, \ - default: __fun) -#define __tg_real_2(__fun, x, y) _Generic(x, \ - float: __tg_real(__fun, y), \ - long double: __fun ## l, \ - default: _Generic(y, \ - long double: __fun ## l, \ - default: __fun)) -#define __tg_real_3(__fun, x, y, z) _Generic(x, \ - float: __tg_real_2(__fun, y, z), \ - long double: __fun ## l, \ - default: _Generic(y, \ - long double: __fun ## l, \ - default: _Generic(z, \ - long double: __fun ## l, \ - default: __fun))) -// FIXME: imaginary type -#define __tg_complex(__fun, x) _Generic(x, \ - float: __fun ## f, \ - long double: __fun ## l, \ - float complex: __fun ## f, \ - long double complex: __fun ## l, \ - default: __fun) -#define __tg_real_complex(__fun, x) _Generic(x, \ - float: __fun ## f, \ - long double: __fun ## l, \ - float complex: c ## __fun ## f, \ - double complex: c ## __fun, \ - long double complex: c ## __fun ## l, \ - default: __fun) -#define __tg_real_complex_fabs(x) _Generic(x, \ - float: fabsf, \ - long double: fabsl, \ - float complex: cabsf, \ - double complex: cabs, \ - long double complex: cabsl, \ - default: fabs) - -#define acos(x) __tg_real_complex(acos, (x))(x) -#define acosh(x) __tg_real_complex(acosh, (x))(x) -#define asin(x) __tg_real_complex(asin, (x))(x) -#define asinh(x) __tg_real_complex(asinh, (x))(x) -#define atan(x) __tg_real_complex(atan, (x))(x) -#define atan2(x) __tg_real_2(atan2, (x), (y))((x), (y)) -#define atanh(x) __tg_real_complex(atanh, (x))(x) -#define carg(x) __tg_complex(carg, (x))(x) -#define cbrt(x) __tg_real(cbrt, (x))(x) -#define ceil(x) __tg_real(ceil, (x))(x) -#define cimag(x) __tg_complex(cimag, (x))(x) -#define conj(x) __tg_complex(conj, (x))(x) -#define copysign(x,y) __tg_real_2(copysign, (x), (y))((x), (y)) -#define cos(x) __tg_real_complex(cos, (x))(x) -#define cosh(x) __tg_real_complex(cosh, (x))(x) -#define cproj(x) __tg_complex(cproj, (x))(x) -#define creal(x) __tg_complex(creal, (x))(x) -#define erf(x) __tg_real(erf, (x))(x) -#define erfc(x) __tg_real(erfc, (x))(x) -#define exp(x) __tg_real_complex(exp, (x))(x) -#define exp2(x) __tg_real(exp2, (x))(x) -#define expm1(x) __tg_real(expm1, (x))(x) -#define fabs(x) __tg_real_complex_fabs(x)(x) -#define fdim(x) __tg_real(fdim, (x))(x) -#define floor(x) __tg_real(floor, (x))(x) -#define fma(x,y,z) __tg_real_3(fma, (x), (y), (z))((x), (y), (z)) -#define fmax(x,y) __tg_real_2(fmax, (x), (y))((x), (y)) -#define fmin(x,y) __tg_real_2(fmin, (x), (y))((x), (y)) -#define fmod(x,y) __tg_real_2(fmod, (x), (y))((x), (y)) -#define frexp(x,y) __tg_real(frexp, (x))((x), (y)) -#define hypot(x,y) __tg_real_2(hypot, (x), (y))((x), (y)) -#define ilogb(x) __tg_real(ilogb, (x))(x) -#define ldexp(x,y) __tg_real(ldexp, (x))((x), (y)) -#define lgamma(x) __tg_real(lgamma, (x))(x) -#define llrint(x) __tg_real(llrint, (x))(x) -#define llround(x) __tg_real(llround, (x))(x) -#define log(x) __tg_real_complex(log, (x))(x) -#define log10(x) __tg_real(log10, (x))(x) -#define log1p(x) __tg_real(log1p, (x))(x) -#define log2(x) __tg_real(log2, (x))(x) -#define logb(x) __tg_real(logb, (x))(x) -#define lrint(x) __tg_real(lrint, (x))(x) -#define lround(x) __tg_real(lround, (x))(x) -#define nearbyint(x) __tg_real(nearbyint, (x))(x) -#define nextafter(x,y) __tg_real_2(nextafter, (x), (y)((x), (y)) -#define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y))((x), (y)) -#define pow(x) __tg_real_complex(pow, (x))(x) -#define remainder(x,y) __tg_real_2(remainder, (x), (y))((x), (y)) -#define remquo(x,y,z) __tg_real_2(remquo, (x), (y))((x), (y), (z)) -#define rint(x) __tg_real(rint, (x))(x) -#define round(x) __tg_real(round, (x))(x) -#define scalbln(x,y) __tg_real(scalbln, (x))((x), (y)) -#define scalbn(x, y) __tg_real(scalbn, (x))((x), (y)) -#define sin(x) __tg_real_complex(sin, (x))(x) -#define sinh(x) __tg_real_complex(sinh, (x))(x) -#define sqrt(x) __tg_real_complex(sqrt, (x))(x) -#define tan(x) __tg_real_complex(tan, (x))(x) -#define tanh(x) __tg_real_complex(tanh, (x))(x) -#define tgamma(x) __tg_real(tgamma, (x))(x) -#define trunc(x) __tg_real(trunc, (x))(x) +#define __IS_FP(x) !!((1?1:(x))/2) +#define __IS_CX(x) (__IS_FP(x) && sizeof((x)) == sizeof((x)+I)) +#define __IS_REAL(x) (__IS_FP(x) && 2*sizeof((x)) == sizeof((x)+I)) + +#define __FLT(x) (__IS_REAL((x)) && sizeof((x)) == sizeof(float)) +#define __LDBL(x) (__IS_REAL((x)) && sizeof((x)) == sizeof(long double)) + +#define __FLTCX(x) (__IS_CX(x) && sizeof((x)) == sizeof(float complex)) +#define __DBLCX(x) (__IS_CX(x) && sizeof((x)) == sizeof(double complex)) +#define __LDBLCX(x) (__IS_CX(x) && sizeof((x)) == sizeof(long double complex)) + +#define __tg_real(__fun, x) ( \ + __FLT(x) ? __fun ## f (x) : \ + __LDBL(x) ? __fun ## l (x) : \ + __fun(x) ) + +#define __tg_real_2_1(__fun, x, y) ( \ + __FLT(x) ? __fun ## f (x, y) : \ + __LDBL(x) ? __fun ## l (x, y) : \ + __fun(x, y) ) + +#define __tg_real_2(__fun, x, y) ( \ + __FLT(x) && __FLT(y) ? __fun ## f (x, y) : \ + __LDBL(x) || __LDBL(y) ? __fun ## l (x, y) : \ + __fun(x, y) ) + +#define __tg_complex(__fun, x) ( \ + __FLTCX(x) || __FLT(x) ? __fun ## f (x) : \ + __LDBLCX(x) || __LDBL(x) ? __fun ## l (x) : \ + __fun(x) ) + +#define __tg_real_complex(__fun, x) ( \ + __LDBLCX(x) ? c ## __fun ## l (x) : \ + __DBLCX(x) ? c ## __fun (x) : \ + __FLTCX(x) ? c ## __fun ## f (x) : \ + __FLT(x) ? __fun ## f (x) : \ + __LDBL(x) ? __fun ## l (x) : \ + __fun(x) ) + +/* special cases */ + +#define __tg_real_remquo(x, y, z) ( \ + __FLT(x) && __FLT(y) ? remquof(x, y, z) : \ + __LDBL(x) || __LDBL(y) ? remquol(x, y, z) : \ + remquo(x, y, z) ) + +#define __tg_real_fma(x, y, z) ( \ + __FLT(x) && __FLT(y) && __FLT(z) ? fmaf(x, y, z) : \ + __LDBL(x) || __LDBL(y) || __LDBL(z) ? fmal(x, y, z) : \ + fma(x, y, z) ) + +#define __tg_real_complex_pow(x, y) ( \ + __LDBLCX(x) || __LDBLCX(y) ? cpowl(x, y) : \ + __DBLCX(x) || __DBLCX(y) ? cpow(x, y) : \ + __FLTCX(x) || __FLTCX(y) ? cpowf(x, y) : \ + __tg_real_2(pow, x, y) ) + +#define __tg_real_complex_fabs(__fun, x) ( \ + __LDBLCX(x) ? cabsl(x) : \ + __DBLCX(x) ? cabs(x) : \ + __FLTCX(x) ? cabsf(x) : \ + __tg_real(fabs, x) ) + +/* tg functions */ + +#define acos(x) __tg_real_complex(acos, (x)) +#define acosh(x) __tg_real_complex(acosh, (x)) +#define asin(x) __tg_real_complex(asin, (x)) +#define asinh(x) __tg_real_complex(asinh, (x)) +#define atan(x) __tg_real_complex(atan, (x)) +#define atan2(x) __tg_real_2(atan2, (x), (y)) +#define atanh(x) __tg_real_complex(atanh, (x)) +#define carg(x) __tg_complex(carg, (x)) +#define cbrt(x) __tg_real(cbrt, (x)) +#define ceil(x) __tg_real(ceil, (x)) +#define cimag(x) __tg_complex(cimag, (x)) +#define conj(x) __tg_complex(conj, (x)) +#define copysign(x,y) __tg_real_2(copysign, (x), (y)) +#define cos(x) __tg_real_complex(cos, (x)) +#define cosh(x) __tg_real_complex(cosh, (x)) +#define cproj(x) __tg_complex(cproj, (x)) +#define creal(x) __tg_complex(creal, (x)) +#define erf(x) __tg_real(erf, (x)) +#define erfc(x) __tg_real(erfc, (x)) +#define exp(x) __tg_real_complex(exp, (x)) +#define exp2(x) __tg_real(exp2, (x)) +#define expm1(x) __tg_real(expm1, (x)) +#define fabs(x) __tg_real_complex_fabs(x) +#define fdim(x) __tg_real(fdim, (x)) +#define floor(x) __tg_real(floor, (x)) +#define fma(x,y,z) __tg_real_fma((x), (y), (z)) +#define fmax(x,y) __tg_real_2(fmax, (x), (y)) +#define fmin(x,y) __tg_real_2(fmin, (x), (y)) +#define fmod(x,y) __tg_real_2(fmod, (x), (y)) +#define frexp(x,y) __tg_real_2_1(frexp, (x), (y)) +#define hypot(x,y) __tg_real_2(hypot, (x), (y)) +#define ilogb(x) __tg_real(ilogb, (x)) +#define ldexp(x,y) __tg_real_2_1(ldexp, (x), (y)) +#define lgamma(x) __tg_real(lgamma, (x)) +#define llrint(x) __tg_real(llrint, (x)) +#define llround(x) __tg_real(llround, (x)) +#define log(x) __tg_real_complex(log, (x)) +#define log10(x) __tg_real(log10, (x)) +#define log1p(x) __tg_real(log1p, (x)) +#define log2(x) __tg_real(log2, (x)) +#define logb(x) __tg_real(logb, (x)) +#define lrint(x) __tg_real(lrint, (x)) +#define lround(x) __tg_real(lround, (x)) +#define nearbyint(x) __tg_real(nearbyint, (x)) +#define nextafter(x,y) __tg_real_2(nextafter, (x), (y) +#define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y)) +#define pow(x,y) __tg_real_complex_pow((x), (y)) +#define remainder(x,y) __tg_real_2(remainder, (x), (y)) +#define remquo(x,y,z) __tg_real_remquo((x), (y), (z)) +#define rint(x) __tg_real(rint, (x)) +#define round(x) __tg_real(round, (x)) +#define scalbln(x,y) __tg_real_2_1(scalbln, (x), (y)) +#define scalbn(x,y) __tg_real_2_1(scalbn, (x), (y)) +#define sin(x) __tg_real_complex(sin, (x)) +#define sinh(x) __tg_real_complex(sinh, (x)) +#define sqrt(x) __tg_real_complex(sqrt, (x)) +#define tan(x) __tg_real_complex(tan, (x)) +#define tanh(x) __tg_real_complex(tanh, (x)) +#define tgamma(x) __tg_real(tgamma, (x)) +#define trunc(x) __tg_real(trunc, (x)) #endif -- 2.20.1