From 8f02fce3ed7d99db5ce80c88e22bbc1cce7c0c71 Mon Sep 17 00:00:00 2001 From: nsz Date: Sat, 10 Mar 2012 17:27:47 +0100 Subject: [PATCH] tgmath.h return type fixes, non-float argument fixes --- include/tgmath.h | 81 +++++++++++++++++++++++++++++++----------------- test/tgmath/t.c | 9 ++++-- 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/include/tgmath.h b/include/tgmath.h index e859252..9034ef7 100644 --- a/include/tgmath.h +++ b/include/tgmath.h @@ -2,10 +2,12 @@ #define _TGMATH_H /* -wrong when sizeof(double) == sizeof(long double) -with __GNUC__ the return type is broken in case of mixed float and int arguments -without __GNUC__ the return type is always long double [complex] -(imaginary type is not supported) +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 @@ -16,17 +18,31 @@ without __GNUC__ the return type is always long double [complex] #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 __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)) +#define __LDBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(long double complex) && sizeof(long double) != sizeof(double)) #ifdef __GNUC__ -/* typeof(x) if fp else double (using ?: with null pointers) */ +/* 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 : \ @@ -35,6 +51,8 @@ without __GNUC__ the return type is always long double [complex] (__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)( \ @@ -47,52 +65,57 @@ without __GNUC__ the return type is always long double [complex] __LDBL(x) ? fun ## l (x, y) : \ fun(x, y) )) -#define __tg_real_2(fun, x, y) (__RETCAST((x)+(y))( \ - __FLT(x) && __FLT(y) ? fun ## f (x, y) : \ - __LDBL(x) || __LDBL(y) ? fun ## l (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) || __FLT(x) ? fun ## f (x) : \ - __LDBLCX(x) || __LDBL(x) ? fun ## l (x) : \ + __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) || __FLT(x) ? fun ## f (x) : \ - __LDBLCX(x) || __LDBL(x) ? fun ## l (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)( \ - __LDBLCX(x) ? c ## fun ## l (x) : \ - __DBLCX(x) ? c ## fun (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((x)+(y))( \ - __FLT(x) && __FLT(y) ? remquof(x, y, z) : \ - __LDBL(x) || __LDBL(y) ? remquol(x, y, z) : \ +#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((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) || __LDBL(y) || __LDBL(z) ? fmal(x, y, z) : \ + __LDBL((x)+(y)+(z)) ? fmal(x, y, z) : \ fma(x, y, z) )) -#define __tg_real_complex_pow(x, y) (__RETCAST((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_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))( \ - __LDBLCX(x) ? cabsl(x) : \ - __DBLCX(x) ? cabs(x) : \ __FLTCX(x) ? cabsf(x) : \ - __tg_real(fabs, x) )) + __DBLCX(x) ? cabs(x) : \ + __LDBLCX(x) ? cabsl(x) : \ + __FLT(x) ? fabsf(x) : \ + __LDBL(x) ? fabsl(x) : \ + fabs(x) )) /* tg functions */ diff --git a/test/tgmath/t.c b/test/tgmath/t.c index 836add5..b8c35ca 100644 --- a/test/tgmath/t.c +++ b/test/tgmath/t.c @@ -19,8 +19,11 @@ int main() printf("f %2u %La\n", sizeof sin(1.0f), f); printf("l %2u %La\n", sizeof sin(1.0l), l); - printf("dc %2u %La %La\n", sizeof sin(1.0+0*I), creall(dc), cimagl(dc)); - printf("fc %2u %La %La\n", sizeof sin(1.0f+0*I), creall(fc), cimagl(fc)); - printf("lc %2u %La %La\n", sizeof sin(1.0l+0*I), creall(lc), cimagl(lc)); + printf("dc %2u %La %La\n", sizeof sin(1.0+0*I), creal(dc), cimag(dc)); + printf("fc %2u %La %La\n", sizeof sin(1.0f+0*I), creal(fc), cimag(fc)); + printf("lc %2u %La %La\n", sizeof sin(1.0l+0*I), creal(lc), cimag(lc)); + + printf("sizeof pow(I,1) = %2u\n", sizeof pow(I,1)); + printf("sizeof pow(I,1.0f) = %2u\n", sizeof pow(I,1.0f)); return 0; } -- 2.20.1