[libc-commits] [libc] [libc] Implement `CMPLX` for clang < 12 (PR #157096)
Connector Switch via libc-commits
libc-commits at lists.llvm.org
Fri Sep 5 06:18:04 PDT 2025
https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/157096
>From ffb7b6cafcf957c84b0f94718c1a973283a49c4a Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Fri, 5 Sep 2025 20:51:27 +0800
Subject: [PATCH 1/2] buildin complex only available for clang >= 12
---
.../include/llvm-libc-macros/complex-macros.h | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
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__
>From d553af70bcd01a0417e9e2c10dba93bdb7872a57 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Fri, 5 Sep 2025 21:06:49 +0800
Subject: [PATCH 2/2] address review comments
---
libc/include/llvm-libc-macros/complex-macros.h | 2 +-
libc/test/CMakeLists.txt | 12 ++++++------
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/libc/include/llvm-libc-macros/complex-macros.h b/libc/include/llvm-libc-macros/complex-macros.h
index 5ea272a2e7482..e7b0edf96142d 100644
--- a/libc/include/llvm-libc-macros/complex-macros.h
+++ b/libc/include/llvm-libc-macros/complex-macros.h
@@ -22,7 +22,7 @@
// TODO: Add imaginary macros once GCC or Clang support _Imaginary builtin-type.
-#if !defined(__clang__) || (__clang_major__ >= 12)
+#if __has_builtin(__builtin_complex)
#define __CMPLX(r, i, t) (__builtin_complex((t)(r), (t)(i)))
#else
#define __CMPLX(r, i, t) ((_Complex t){(t)(r), (t)(i)})
diff --git a/libc/test/CMakeLists.txt b/libc/test/CMakeLists.txt
index 011ad6aeb34b7..74438946d5417 100644
--- a/libc/test/CMakeLists.txt
+++ b/libc/test/CMakeLists.txt
@@ -18,13 +18,13 @@ if(LIBC_TARGET_OS_IS_GPU)
endif()
endif()
-add_subdirectory(src)
-add_subdirectory(utils)
-add_subdirectory(shared)
+# add_subdirectory(src)
+# add_subdirectory(utils)
+# add_subdirectory(shared)
-if(NOT LLVM_LIBC_FULL_BUILD)
- return()
-endif()
+# if(NOT LLVM_LIBC_FULL_BUILD)
+# return()
+# endif()
add_subdirectory(include)
More information about the libc-commits
mailing list