[libc-commits] [libc] [libc] Implement `CMPLX` for clang < 12 (PR #157096)

via libc-commits libc-commits at lists.llvm.org
Fri Sep 5 05:53:11 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Connector Switch (c8ef)

<details>
<summary>Changes</summary>

Fixes https://github.com/llvm/llvm-project/pull/156344#issuecomment-3256837826

---
Full diff: https://github.com/llvm/llvm-project/pull/157096.diff


1 Files Affected:

- (modified) libc/include/llvm-libc-macros/complex-macros.h (+12-7) 


``````````diff
diff --git a/libc/include/llvm-libc-macros/complex-macros.h b/libc/include/llvm-libc-macros/complex-macros.h
index d98c6fdb306c8..5ea272a2e7482 100644
--- a/libc/include/llvm-libc-macros/complex-macros.h
+++ b/libc/include/llvm-libc-macros/complex-macros.h
@@ -22,21 +22,26 @@
 
 // TODO: Add imaginary macros once GCC or Clang support _Imaginary builtin-type.
 
-#define CMPLX(x, y) __builtin_complex((double)(x), (double)(y))
-#define CMPLXF(x, y) __builtin_complex((float)(x), (float)(y))
-#define CMPLXL(x, y) __builtin_complex((long double)(x), (long double)(y))
+#if !defined(__clang__) || (__clang_major__ >= 12)
+#define __CMPLX(r, i, t) (__builtin_complex((t)(r), (t)(i)))
+#else
+#define __CMPLX(r, i, t) ((_Complex t){(t)(r), (t)(i)})
+#endif
+
+#define CMPLX(r, i) __CMPLX(r, i, double)
+#define CMPLXF(r, i) __CMPLX(r, i, float)
+#define CMPLXL(r, i) __CMPLX(r, i, long double)
 
 #ifdef LIBC_TYPES_HAS_CFLOAT16
 #if !defined(__clang__) || (__clang_major__ >= 22 && __clang_minor__ > 0)
-#define CMPLXF16(x, y) __builtin_complex((_Float16)(x), (_Float16)(y))
+#define CMPLXF16(r, i) __CMPLX(r, i, _Float16)
 #else
-#define CMPLXF16(x, y)                                                         \
-  ((complex _Float16)(__builtin_complex((float)(x), (float)(y))))
+#define CMPLXF16(r, i) ((complex _Float16)(__CMPLX(r, i, float)))
 #endif
 #endif // LIBC_TYPES_HAS_CFLOAT16
 
 #ifdef LIBC_TYPES_HAS_CFLOAT128
-#define CMPLXF128(x, y) __builtin_complex((float128)(x), (float128)(y))
+#define CMPLXF128(r, i) __CMPLX(r, i, float128)
 #endif // LIBC_TYPES_HAS_CFLOAT128
 
 #endif // __STDC_NO_COMPLEX__

``````````

</details>


https://github.com/llvm/llvm-project/pull/157096


More information about the libc-commits mailing list