From c276a35d6e3eaaf91626e21838153c362ae14912 Mon Sep 17 00:00:00 2001 From: c8ef Date: Mon, 1 Sep 2025 23:24:33 +0800 Subject: [PATCH 1/4] [libc] Implement CMPLX related macros --- libc/docs/headers/complex.rst | 2 +- libc/include/llvm-libc-macros/CMakeLists.txt | 14 ++++++++------ libc/include/llvm-libc-macros/complex-macros.h | 14 ++++++++++++++ libc/test/include/complex_test.cpp | 9 +++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/libc/docs/headers/complex.rst b/libc/docs/headers/complex.rst index 272cf00c883bc..7195f2695457e 100644 --- a/libc/docs/headers/complex.rst +++ b/libc/docs/headers/complex.rst @@ -10,7 +10,7 @@ Macros +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | | (float) | (double) | (long double) | (float16) | (float128) | C23 Definition Section | C23 Error Handling Section | +===========+==================+=================+========================+======================+========================+========================+============================+ -| CMPLX | | | | | | 7.3.9.3 | N/A | +| CMPLX | |check| | |check| | |check| | |check| | |check| | 7.3.9.3 | N/A | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ Functions diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt index 7aa549ddc75d9..055e11f958e02 100644 --- a/libc/include/llvm-libc-macros/CMakeLists.txt +++ b/libc/include/llvm-libc-macros/CMakeLists.txt @@ -61,12 +61,6 @@ add_macro_header( fcntl-macros.h ) -add_macro_header( - complex_macros - HDR - complex-macros.h -) - add_macro_header( features_macros HDR @@ -103,6 +97,14 @@ add_macro_header( float16-macros.h ) +add_macro_header( + complex_macros + HDR + complex-macros.h + DEPENDS + .float16_macros +) + add_macro_header( limits_macros HDR diff --git a/libc/include/llvm-libc-macros/complex-macros.h b/libc/include/llvm-libc-macros/complex-macros.h index 427c68d289e0b..fdcc9c7831dd1 100644 --- a/libc/include/llvm-libc-macros/complex-macros.h +++ b/libc/include/llvm-libc-macros/complex-macros.h @@ -9,6 +9,8 @@ #ifndef __LLVM_LIBC_MACROS_COMPLEX_MACROS_H #define __LLVM_LIBC_MACROS_COMPLEX_MACROS_H +#include "float16-macros.h" + #ifndef __STDC_NO_COMPLEX__ #define __STDC_VERSION_COMPLEX_H__ 202311L @@ -19,6 +21,18 @@ // 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)) + +#ifdef LIBC_TYPES_HAS_FLOAT16 +#define CMPLXF16(x, y) __builtin_complex((float16)(x), (float16)(y)) +#endif // LIBC_TYPES_HAS_FLOAT16 + +#ifdef LIBC_TYPES_HAS_FLOAT128 +#define CMPLXF128(x, y) __builtin_complex((float128)(x), (float128)(y)) +#endif // LIBC_TYPES_HAS_FLOAT128 + #endif #endif // __LLVM_LIBC_MACROS_COMPLEX_MACROS_H diff --git a/libc/test/include/complex_test.cpp b/libc/test/include/complex_test.cpp index da833fb527381..869d2a935986e 100644 --- a/libc/test/include/complex_test.cpp +++ b/libc/test/include/complex_test.cpp @@ -17,3 +17,12 @@ TEST(LlvmLibcComplexTest, VersionMacro) { TEST(LlvmLibcComplexTest, IMacro) { EXPECT_CFP_EQ(1.0fi, I); } TEST(LlvmLibcComplexTest, _Complex_IMacro) { EXPECT_CFP_EQ(1.0fi, _Complex_I); } + +TEST(LlvmLibcComplexTest, CMPLXMacro) { + EXPECT_CFP_EQ(CMPLX(0, 1.0), I); + EXPECT_CFP_EQ(CMPLX(1.0, 0), 1.0); + EXPECT_CFP_EQ(CMPLXF(0, 1.0f), I); + EXPECT_CFP_EQ(CMPLXF(1.0f, 0), 1.0f); + EXPECT_CFP_EQ(CMPLXL(0, 1.0l), I); + EXPECT_CFP_EQ(CMPLXL(1.0l, 0), 1.0l); +} From 65edd0ba245d51a7f67df2cd36420076f140a58d Mon Sep 17 00:00:00 2001 From: c8ef Date: Tue, 2 Sep 2025 22:14:51 +0800 Subject: [PATCH 2/4] [libc] Implement CMPLX related macros --- libc/test/include/complex_test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libc/test/include/complex_test.cpp b/libc/test/include/complex_test.cpp index 869d2a935986e..b14423dd89828 100644 --- a/libc/test/include/complex_test.cpp +++ b/libc/test/include/complex_test.cpp @@ -25,4 +25,9 @@ TEST(LlvmLibcComplexTest, CMPLXMacro) { EXPECT_CFP_EQ(CMPLXF(1.0f, 0), 1.0f); EXPECT_CFP_EQ(CMPLXL(0, 1.0l), I); EXPECT_CFP_EQ(CMPLXL(1.0l, 0), 1.0l); + +#ifdef LIBC_TYPES_HAS_FLOAT128 + EXPECT_CFP_EQ(CMPLXF128(0, 1.0), I); + EXPECT_CFP_EQ(CMPLXF128(1.0, 0), 1.0); +#endif // LIBC_TYPES_HAS_FLOAT128 } From 3904794bcfd5dec6e97943bf0f128d1051f4b533 Mon Sep 17 00:00:00 2001 From: c8ef Date: Tue, 2 Sep 2025 22:24:52 +0800 Subject: [PATCH 3/4] [libc] Implement CMPLX related macros --- libc/test/include/complex_test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libc/test/include/complex_test.cpp b/libc/test/include/complex_test.cpp index b14423dd89828..55aca9d10194f 100644 --- a/libc/test/include/complex_test.cpp +++ b/libc/test/include/complex_test.cpp @@ -26,6 +26,12 @@ TEST(LlvmLibcComplexTest, CMPLXMacro) { EXPECT_CFP_EQ(CMPLXL(0, 1.0l), I); EXPECT_CFP_EQ(CMPLXL(1.0l, 0), 1.0l); +// TODO: Uncomment these after issues#156463 is resolved. +// #ifdef LIBC_TYPES_HAS_FLOAT16 +// EXPECT_CFP_EQ(CMPLXF16(0, 1.0), I); +// EXPECT_CFP_EQ(CMPLXF16(1.0, 0), 1.0); +// #endif // LIBC_TYPES_HAS_FLOAT16 + #ifdef LIBC_TYPES_HAS_FLOAT128 EXPECT_CFP_EQ(CMPLXF128(0, 1.0), I); EXPECT_CFP_EQ(CMPLXF128(1.0, 0), 1.0); From 008cd98bc25abad09b7eae384512d319fa89b35a Mon Sep 17 00:00:00 2001 From: c8ef Date: Tue, 2 Sep 2025 22:28:39 +0800 Subject: [PATCH 4/4] [libc] Implement CMPLX related macros --- libc/test/include/complex_test.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libc/test/include/complex_test.cpp b/libc/test/include/complex_test.cpp index 55aca9d10194f..9ad67a895e2ae 100644 --- a/libc/test/include/complex_test.cpp +++ b/libc/test/include/complex_test.cpp @@ -26,11 +26,11 @@ TEST(LlvmLibcComplexTest, CMPLXMacro) { EXPECT_CFP_EQ(CMPLXL(0, 1.0l), I); EXPECT_CFP_EQ(CMPLXL(1.0l, 0), 1.0l); -// TODO: Uncomment these after issues#156463 is resolved. -// #ifdef LIBC_TYPES_HAS_FLOAT16 -// EXPECT_CFP_EQ(CMPLXF16(0, 1.0), I); -// EXPECT_CFP_EQ(CMPLXF16(1.0, 0), 1.0); -// #endif // LIBC_TYPES_HAS_FLOAT16 + // TODO: Uncomment these after issues#156463 is resolved. + // #ifdef LIBC_TYPES_HAS_FLOAT16 + // EXPECT_CFP_EQ(CMPLXF16(0, 1.0), I); + // EXPECT_CFP_EQ(CMPLXF16(1.0, 0), 1.0); + // #endif // LIBC_TYPES_HAS_FLOAT16 #ifdef LIBC_TYPES_HAS_FLOAT128 EXPECT_CFP_EQ(CMPLXF128(0, 1.0), I);