X-Git-Url: http://nsz.repo.hu/git/?p=libm;a=blobdiff_plain;f=include%2Ftgmath.h;h=9034ef7f4537c973a8fa2264f2704e0650653ca0;hp=169601a59bd6c1d63cf05338c2ca58b40934ec94;hb=8f02fce3ed7d99db5ce80c88e22bbc1cce7c0c71;hpb=fc04675a09587031d5daebcb877b73976890b71d diff --git a/include/tgmath.h b/include/tgmath.h index 169601a..9034ef7 100644 --- a/include/tgmath.h +++ b/include/tgmath.h @@ -1,105 +1,183 @@ #ifndef _TGMATH_H #define _TGMATH_H -// C11 only -// See C11 6.5.1.1 for generic selections -// See C11 7.25 for tgmath.h +/* +the return types are only set correctly whit gcc (__GNUC__) +otherwise they are long double or long double complex + +the long double version of a function is never chosen when +sizeof(double) == sizeof(long double) +(but the return type is set correctly) +*/ #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 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) && sizeof(long double) != sizeof(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) && sizeof(long double) != sizeof(double)) + +#ifdef __GNUC__ +/* arg type if it's floating-point else double (using ?: with null pointers) */ +#define __RETCAST(x) (__typeof__(*( \ + 0 ? (__typeof__(0 ? (double *)0 : (void *)__IS_FP(x)))0 : \ + (__typeof__(0 ? (__typeof__(x) *)0 : (void *)!__IS_FP(x)))0 ))) +/* two args case, consider complex types (for cpow) */ +#define __RETCAST_2(x, y) (__typeof__(*( \ + 0 ? (__typeof__(0 ? (double *)0 : \ + (void *)!((!__IS_FP(x) || !__IS_FP(y)) && __FLT((x)+(y)+1.0f))))0 : \ + 0 ? (__typeof__(0 ? (double complex *)0 : \ + (void *)!((!__IS_FP(x) || !__IS_FP(y)) && __FLTCX((x)+(y)))))0 : \ + (__typeof__(0 ? (__typeof__((x)+(y)) *)0 : \ + (void *)((!__IS_FP(x) || !__IS_FP(y)) && (__FLT((x)+(y)+1.0f) || __FLTCX((x)+(y))))))0 ))) +/* three args case, don't consider complex types (fma only) */ +#define __RETCAST_3(x, y, z) (__typeof__(*( \ + 0 ? (__typeof__(0 ? (double *)0 : \ + (void *)!((!__IS_FP(x) || !__IS_FP(y) || !__IS_FP(z)) && __FLT((x)+(y)+(z)+1.0f))))0 : \ + (__typeof__(0 ? (__typeof__((x)+(y)) *)0 : \ + (void *)((!__IS_FP(x) || !__IS_FP(y) || !__IS_FP(z)) && __FLT((x)+(y)+(z)+1.0f))))0 ))) +/* drop complex from the type of x */ +#define __TO_REAL(x) *( \ + 0 ? (__typeof__(0 ? (double *)0 : (void *)!__DBLCX(x)))0 : \ + 0 ? (__typeof__(0 ? (float *)0 : (void *)!__FLTCX(x)))0 : \ + 0 ? (__typeof__(0 ? (long double *)0 : (void *)!__LDBLCX(x)))0 : \ + (__typeof__(0 ? (__typeof__(x) *)0 : (void *)__IS_CX(x)))0 ) +#else +#define __RETCAST(x) +#define __RETCAST_2(x, y) +#define __RETCAST_3(x, y, z) +#endif + +#define __tg_real(fun, x) (__RETCAST(x)( \ + __FLT(x) ? fun ## f (x) : \ + __LDBL(x) ? fun ## l (x) : \ + fun(x) )) + +#define __tg_real_2_1(fun, x, y) (__RETCAST(x)( \ + __FLT(x) ? fun ## f (x, y) : \ + __LDBL(x) ? fun ## l (x, y) : \ + fun(x, y) )) + +#define __tg_real_2(fun, x, y) (__RETCAST_2(x, y)( \ + __FLT(x) && __FLT(y) ? fun ## f (x, y) : \ + __LDBL((x)+(y)) ? fun ## l (x, y) : \ + fun(x, y) )) + +#define __tg_complex(fun, x) (__RETCAST((x)+I)( \ + __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \ + __LDBLCX((x)+I) ? fun ## l (x) : \ + fun(x) )) + +#define __tg_complex_retreal(fun, x) (__RETCAST(__TO_REAL(x))( \ + __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \ + __LDBLCX((x)+I) ? fun ## l (x) : \ + fun(x) )) + +#define __tg_real_complex(fun, x) (__RETCAST(x)( \ + __FLTCX(x) ? c ## fun ## f (x) : \ + __DBLCX(x) ? c ## fun (x) : \ + __LDBLCX(x) ? c ## fun ## l (x) : \ + __FLT(x) ? fun ## f (x) : \ + __LDBL(x) ? fun ## l (x) : \ + fun(x) )) + +/* special cases */ + +#define __tg_real_remquo(x, y, z) (__RETCAST_2(x, y)( \ + __FLT(x) && __FLT(y) ? remquof(x, y, z) : \ + __LDBL((x)+(y)) ? remquol(x, y, z) : \ + remquo(x, y, z) )) + +#define __tg_real_fma(x, y, z) (__RETCAST_3(x, y, z)( \ + __FLT(x) && __FLT(y) && __FLT(z) ? fmaf(x, y, z) : \ + __LDBL((x)+(y)+(z)) ? fmal(x, y, z) : \ + fma(x, y, z) )) + +#define __tg_real_complex_pow(x, y) (__RETCAST_2(x, y)( \ + __FLTCX((x)+(y)) && __IS_FP(x) && __IS_FP(y) ? cpowf(x, y) : \ + __FLTCX((x)+(y)) ? cpow(x, y) : \ + __DBLCX((x)+(y)) ? cpow(x, y) : \ + __LDBLCX((x)+(y)) ? cpowl(x, y) : \ + __FLT(x) && __FLT(y) ? powf(x, y) : \ + __LDBL((x)+(y)) ? powl(x, y) : \ + pow(x, y) )) + +#define __tg_real_complex_fabs(x) (__RETCAST(__TO_REAL(x))( \ + __FLTCX(x) ? cabsf(x) : \ + __DBLCX(x) ? cabs(x) : \ + __LDBLCX(x) ? cabsl(x) : \ + __FLT(x) ? fabsf(x) : \ + __LDBL(x) ? fabsl(x) : \ + 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,y) __tg_real_2(atan2, (x), (y)) +#define atanh(x) __tg_real_complex(atanh, (x)) +#define carg(x) __tg_complex_retreal(carg, (x)) +#define cbrt(x) __tg_real(cbrt, (x)) +#define ceil(x) __tg_real(ceil, (x)) +#define cimag(x) __tg_complex_retreal(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_retreal(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,y) __tg_real_2(fdim, (x), (y)) +#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