diff --git a/include/tgmath.h b/include/tgmath.h index 9de0afe4248b..9302302658fb 100644 --- a/include/tgmath.h +++ b/include/tgmath.h @@ -53,19 +53,23 @@ * Note that these macros cannot be implemented with C's ?: operator, * because the return type of the whole expression would incorrectly be long * double complex regardless of the argument types. + * + * The structure of the C11 implementation of these macros can in + * principle be reused for non-C11 compilers, but due to an integer + * promotion bug for complex types in GCC 4.2, simply let non-C11 + * compilers use an inefficient yet reliable version. */ -#ifndef __generic -#error " not implemented for this compiler" -#endif - -#if 0 /* XXX: Much shorter and faster to compile, but broken with GCC 4.2. */ +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L #define __tg_generic(x, cfnl, cfn, cfnf, fnl, fn, fnf) \ - __generic(x, long double _Complex, cfnl, \ - __generic(x, double _Complex, cfn, \ - __generic(x, float _Complex, cfnf, \ - __generic(x, long double, fnl, \ - __generic(x, float, fnf, fn))))) + _Generic(x, \ + long double _Complex: cfnl, \ + double _Complex: cfn, \ + float _Complex: cfnf, \ + long double: fnl, \ + default: fn, \ + float: fnf \ + ) #define __tg_type(x) \ __tg_generic(x, (long double _Complex)0, (double _Complex)0, \ (float _Complex)0, (long double)0, (double)0, (float)0) @@ -77,7 +81,7 @@ __tg_generic( \ __tg_type(x) + __tg_type(y), \ cfnl, cfn, cfnf, fnl, fn, fnf)(__VA_ARGS__) -#else +#elif defined(__generic) #define __tg_generic_simple(x, fnl, fn, fnf) \ __generic(x, long double _Complex, fnl, \ __generic(x, double _Complex, fn, \ @@ -113,6 +117,8 @@ __tg_generic_full(y, cfnl, cfn , cfn , fnl , fn , fn ), \ __tg_generic_full(y, cfnl, cfn , cfnf, fnl , fn , fnf )) \ (__VA_ARGS__) +#else +#error " not implemented for this compiler" #endif /* Macros to save lots of repetition below */