tgmath.h improved return type with gcc
[libm] / include / tgmath.h
index 6926d0f..e859252 100644 (file)
@@ -2,8 +2,8 @@
 #define _TGMATH_H
 
 /*
-tgmath.h is broken:
-with __GNUC__ the return type is wrong when all arguments are float [complex]
+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)
 */
@@ -11,12 +11,6 @@ without __GNUC__ the return type is always long double [complex]
 #include <math.h>
 #include <complex.h>
 
-#ifdef __GNUC__
-#define __RETCAST(x) (__typeof__((x)))
-#else
-#define __RETCAST(x)
-#endif
-
 #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))
@@ -28,57 +22,77 @@ without __GNUC__ the return type is always long double [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) __RETCAST((x)+1.0)( \
+#ifdef __GNUC__
+/* typeof(x) if fp 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 )))
+/* 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)
+#endif
+
+#define __tg_real(fun, x) (__RETCAST(x)( \
        __FLT(x) ? fun ## f (x) : \
        __LDBL(x) ? fun ## l (x) : \
-       fun(x) )
+       fun(x) ))
 
-#define __tg_real_2_1(fun, x, y) __RETCAST((x)+1.0)( \
+#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) )
+       fun(x, y) ))
 
-#define __tg_real_2(fun, x, y) __RETCAST((x)+(y)+1.0)( \
+#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) : \
-       fun(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) : \
+       fun(x) ))
 
-#define __tg_complex(fun, x) __RETCAST((x)+1.0+I)( \
+#define __tg_complex_retreal(fun, x) (__RETCAST(__TO_REAL(x))( \
        __FLTCX(x) || __FLT(x) ? fun ## f (x) : \
        __LDBLCX(x) || __LDBL(x) ? fun ## l (x) : \
-       fun(x) )
+       fun(x) ))
 
-#define __tg_real_complex(fun, x) __RETCAST((x)+1.0)( \
+#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) : \
        __FLT(x) ? fun ## f (x) : \
        __LDBL(x) ? fun ## l (x) : \
-       fun(x) )
+       fun(x) ))
 
 /* special cases */
 
-#define __tg_real_remquo(x, y, z) __RETCAST((x)+(y)+1.0)( \
+#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) : \
-       remquo(x, y, z) )
+       remquo(x, y, z) ))
 
-#define __tg_real_fma(x, y, z) __RETCAST((x)+(y)+(z)+1.0)( \
+#define __tg_real_fma(x, y, z) (__RETCAST((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) )
+       fma(x, y, z) ))
 
-#define __tg_real_complex_pow(x, y) __RETCAST((x)+(y)+1.0)( \
+#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) )
+       __tg_real_2(pow, x, y) ))
 
-#define __tg_real_complex_fabs(x) __RETCAST((x)+1.0)( \
+#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) )
+       __tg_real(fabs, x) ))
 
 /* tg functions */
 
@@ -89,16 +103,16 @@ without __GNUC__ the return type is always long double [complex]
 #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(carg, (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(cimag, (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(creal, (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))