diff options
Diffstat (limited to 'include/tgmath.h')
-rw-r--r-- | include/tgmath.h | 70 |
1 files changed, 44 insertions, 26 deletions
diff --git a/include/tgmath.h b/include/tgmath.h index 832b052b..e41ccac9 100644 --- a/include/tgmath.h +++ b/include/tgmath.h @@ -13,7 +13,7 @@ sizeof(double) == sizeof(long double) #include <math.h> #include <complex.h> -#define __IS_FP(x) !!((1?1:(x))/2) +#define __IS_FP(x) (sizeof((x)+1ULL) == sizeof((x)+1.0f)) #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)) @@ -27,34 +27,52 @@ sizeof(double) == sizeof(long double) /* return type */ #ifdef __GNUC__ +/* +the result must be casted to the right type +(otherwise the result type is determined by the conversion +rules applied to all the function return types so it is long +double or long double complex except for integral functions) + +this cannot be done in c99, so the typeof gcc extension is +used and that the type of ?: depends on wether an operand is +a null pointer constant or not +(in c11 _Generic can be used) + +the c arguments below must be integer constant expressions +so they can be in null pointer constants +(__IS_FP above was carefully chosen this way) +*/ +/* if c then t else void */ +#define __type1(c,t) __typeof__(*(0?(t*)0:(void*)!(c))) +/* if c then t1 else t2 */ +#define __type2(c,t1,t2) __typeof__(*(0?(__type1(c,t1)*)0:(__type1(!(c),t2)*)0)) /* cast to double when x is integral, otherwise use typeof(x) */ -#define __RETCAST(x) (__typeof__(*( \ - 0 ? (__typeof__(0 ? (double *)0 : (void *)__IS_FP(x)))0 : \ - (__typeof__(0 ? (__typeof__(x) *)0 : (void *)!__IS_FP(x)))0 ))) -/* 2 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 ))) -/* 3 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 ))) +#define __RETCAST(x) ( \ + __type2(__IS_FP(x), __typeof__(x), double)) +/* 2 args case, should work for complex types (cpow) */ +#define __RETCAST_2(x, y) ( \ + __type2(__IS_FP(x) && __IS_FP(y), \ + __typeof__((x)+(y)), \ + __typeof__((x)+(y)+1.0))) +/* 3 args case (fma only) */ +#define __RETCAST_3(x, y, z) ( \ + __type2(__IS_FP(x) && __IS_FP(y) && __IS_FP(z), \ + __typeof__((x)+(y)+(z)), \ + __typeof__((x)+(y)+(z)+1.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 ) +/* TODO: wrong when sizeof(long double)==sizeof(double) */ +#define __RETCAST_REAL(x) ( \ + __type2(__IS_FP(x) && sizeof((x)+I) == sizeof(float complex), float, \ + __type2(sizeof((x)+1.0+I) == sizeof(double complex), double, \ + long double))) +/* add complex to the type of x */ +#define __RETCAST_CX(x) (__typeof__(__RETCAST(x)0+I)) #else #define __RETCAST(x) #define __RETCAST_2(x, y) #define __RETCAST_3(x, y, z) +#define __RETCAST_REAL(x) +#define __RETCAST_CX(x) #endif /* function selection */ @@ -76,12 +94,12 @@ sizeof(double) == sizeof(long double) __LDBL((x)+(y)) ? fun ## l (x, y) : \ fun(x, y) )) -#define __tg_complex(fun, x) (__RETCAST((x)+I)( \ +#define __tg_complex(fun, x) (__RETCAST_CX(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))( \ +#define __tg_complex_retreal(fun, x) (__RETCAST_REAL(x)( \ __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \ __LDBLCX((x)+I) ? fun ## l (x) : \ fun(x) )) @@ -115,7 +133,7 @@ sizeof(double) == sizeof(long double) __LDBL((x)+(y)) ? powl(x, y) : \ pow(x, y) )) -#define __tg_real_complex_fabs(x) (__RETCAST(__TO_REAL(x))( \ +#define __tg_real_complex_fabs(x) (__RETCAST_REAL(x)( \ __FLTCX(x) ? cabsf(x) : \ __DBLCX(x) ? cabs(x) : \ __LDBLCX(x) ? cabsl(x) : \ |