Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libc/docs/headers/complex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Macros
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| <Func> | <Func_f> (float) | <Func> (double) | <Func_l> (long double) | <Func_f16> (float16) | <Func_f128> (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
Expand Down
14 changes: 8 additions & 6 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions libc/include/llvm-libc-macros/complex-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that we do have checks for complex float16 type in include/llvm-libc-types/cfloat16.h. Can you refactor the checks to include/llvm-libc-macros/cfloat16-macros.h similar to float16-macros.h and use it here? Thanks,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can do this, but I'm wondering how we should correctly define the check. The issue is that using _Complex Float16 works fine, but initialization using __builtin_complex is problematic. I haven't found a way to check this syntactically yet.

If we add a compiler version guard in cfloat16.h, I'm not sure we should bump the minimum Clang version to 22, since it hasn't been released yet and this may not be acceptable. If we do go this route, I think we should also kick off a build for apt.llvm.org/clang22 so we have at least one CI to test float16 support (though I'm not sure how to configure this - the last build was on 08/12).

#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
20 changes: 20 additions & 0 deletions libc/test/include/complex_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ 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);

// 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);
#endif // LIBC_TYPES_HAS_FLOAT128
}
Loading