Skip to content

Commit caf5fb1

Browse files
authored
[libc] Implement CMPLX for clang < 12 (#157096)
Fixes #156344 (comment)
1 parent 80d4e24 commit caf5fb1

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

libc/include/llvm-libc-macros/complex-macros.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,26 @@
2222

2323
// TODO: Add imaginary macros once GCC or Clang support _Imaginary builtin-type.
2424

25-
#define CMPLX(x, y) __builtin_complex((double)(x), (double)(y))
26-
#define CMPLXF(x, y) __builtin_complex((float)(x), (float)(y))
27-
#define CMPLXL(x, y) __builtin_complex((long double)(x), (long double)(y))
25+
#if __has_builtin(__builtin_complex)
26+
#define __CMPLX(r, i, t) (__builtin_complex((t)(r), (t)(i)))
27+
#else
28+
#define __CMPLX(r, i, t) ((_Complex t){(t)(r), (t)(i)})
29+
#endif
30+
31+
#define CMPLX(r, i) __CMPLX(r, i, double)
32+
#define CMPLXF(r, i) __CMPLX(r, i, float)
33+
#define CMPLXL(r, i) __CMPLX(r, i, long double)
2834

2935
#ifdef LIBC_TYPES_HAS_CFLOAT16
3036
#if !defined(__clang__) || (__clang_major__ >= 22 && __clang_minor__ > 0)
31-
#define CMPLXF16(x, y) __builtin_complex((_Float16)(x), (_Float16)(y))
37+
#define CMPLXF16(r, i) __CMPLX(r, i, _Float16)
3238
#else
33-
#define CMPLXF16(x, y) \
34-
((complex _Float16)(__builtin_complex((float)(x), (float)(y))))
39+
#define CMPLXF16(r, i) ((complex _Float16)(__CMPLX(r, i, float)))
3540
#endif
3641
#endif // LIBC_TYPES_HAS_CFLOAT16
3742

3843
#ifdef LIBC_TYPES_HAS_CFLOAT128
39-
#define CMPLXF128(x, y) __builtin_complex((float128)(x), (float128)(y))
44+
#define CMPLXF128(r, i) __CMPLX(r, i, float128)
4045
#endif // LIBC_TYPES_HAS_CFLOAT128
4146

4247
#endif // __STDC_NO_COMPLEX__

0 commit comments

Comments
 (0)