fix definition of CMPLX macros in complex.h to work in constant expressions

based on patches by Jens Gustedt. these macros need to be usable in
static initializers, and the old definitions were not.

there is no portable way to provide correct definitions for these
macros unless the compiler supports pure imaginary types. a portable
definition is provided for this case even though there are presently
no compilers that can use it. gcc and compatible compilers provide a
builtin function that can be used, but clang fails to support this and
instead requires a construct which is a constraint violation and which
is only a constant expression as a clang-specific extension.

since these macros are a namespace violation in pre-C11 profiles, and
since no known pre-C11 compilers provide any way to define them
correctly anyway, the definitions have been made conditional on C11.
This commit is contained in:
Rich Felker 2014-12-17 16:34:41 -05:00
parent a414e8374d
commit 5ff2a118c6
1 changed files with 9 additions and 3 deletions

View File

@ -114,12 +114,18 @@ long double creall(long double complex);
#define cimagl(x) __CIMAG(x, long double) #define cimagl(x) __CIMAG(x, long double)
#endif #endif
#define __CMPLX(x, y, t) \ #if __STDC_VERSION__ >= 201112L
((union { _Complex t __z; t __xy[2]; }){.__xy = {(x),(y)}}.__z) #if defined(_Imaginary_I)
#define __CMPLX(x, y, t) ((t)(x) + _Imaginary_I*(t)(y)))
#elif defined(__clang__)
#define __CMPLX(x, y, t) (+(_Complex t){ (t)(x), (t)(y) })
#else
#define __CMPLX(x, y, t) (__builtin_complex((t)(x), (t)(y)))
#endif
#define CMPLX(x, y) __CMPLX(x, y, double) #define CMPLX(x, y) __CMPLX(x, y, double)
#define CMPLXF(x, y) __CMPLX(x, y, float) #define CMPLXF(x, y) __CMPLX(x, y, float)
#define CMPLXL(x, y) __CMPLX(x, y, long double) #define CMPLXL(x, y) __CMPLX(x, y, long double)
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }