-#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) )
+#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))
+
+#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) ))
+
+#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((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) (__RETCAST((x)+I)( \
+ __FLTCX(x) || __FLT(x) ? fun ## f (x) : \
+ __LDBLCX(x) || __LDBL(x) ? 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) : \
+ 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) : \
+ __FLT(x) ? fun ## f (x) : \
+ __LDBL(x) ? fun ## l (x) : \
+ fun(x) ))