Skip to content

Commit c276a35

Browse files
committed
[libc] Implement CMPLX related macros
1 parent 6aeea12 commit c276a35

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

libc/docs/headers/complex.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Macros
1010
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
1111
| <Func> | <Func_f> (float) | <Func> (double) | <Func_l> (long double) | <Func_f16> (float16) | <Func_f128> (float128) | C23 Definition Section | C23 Error Handling Section |
1212
+===========+==================+=================+========================+======================+========================+========================+============================+
13-
| CMPLX | | | | | | 7.3.9.3 | N/A |
13+
| CMPLX | |check| | |check| | |check| | |check| | |check| | 7.3.9.3 | N/A |
1414
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
1515

1616
Functions

libc/include/llvm-libc-macros/CMakeLists.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ add_macro_header(
6161
fcntl-macros.h
6262
)
6363

64-
add_macro_header(
65-
complex_macros
66-
HDR
67-
complex-macros.h
68-
)
69-
7064
add_macro_header(
7165
features_macros
7266
HDR
@@ -103,6 +97,14 @@ add_macro_header(
10397
float16-macros.h
10498
)
10599

100+
add_macro_header(
101+
complex_macros
102+
HDR
103+
complex-macros.h
104+
DEPENDS
105+
.float16_macros
106+
)
107+
106108
add_macro_header(
107109
limits_macros
108110
HDR

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef __LLVM_LIBC_MACROS_COMPLEX_MACROS_H
1010
#define __LLVM_LIBC_MACROS_COMPLEX_MACROS_H
1111

12+
#include "float16-macros.h"
13+
1214
#ifndef __STDC_NO_COMPLEX__
1315

1416
#define __STDC_VERSION_COMPLEX_H__ 202311L
@@ -19,6 +21,18 @@
1921

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

24+
#define CMPLX(x, y) __builtin_complex((double)(x), (double)(y))
25+
#define CMPLXF(x, y) __builtin_complex((float)(x), (float)(y))
26+
#define CMPLXL(x, y) __builtin_complex((long double)(x), (long double)(y))
27+
28+
#ifdef LIBC_TYPES_HAS_FLOAT16
29+
#define CMPLXF16(x, y) __builtin_complex((float16)(x), (float16)(y))
30+
#endif // LIBC_TYPES_HAS_FLOAT16
31+
32+
#ifdef LIBC_TYPES_HAS_FLOAT128
33+
#define CMPLXF128(x, y) __builtin_complex((float128)(x), (float128)(y))
34+
#endif // LIBC_TYPES_HAS_FLOAT128
35+
2236
#endif
2337

2438
#endif // __LLVM_LIBC_MACROS_COMPLEX_MACROS_H

libc/test/include/complex_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,12 @@ TEST(LlvmLibcComplexTest, VersionMacro) {
1717
TEST(LlvmLibcComplexTest, IMacro) { EXPECT_CFP_EQ(1.0fi, I); }
1818

1919
TEST(LlvmLibcComplexTest, _Complex_IMacro) { EXPECT_CFP_EQ(1.0fi, _Complex_I); }
20+
21+
TEST(LlvmLibcComplexTest, CMPLXMacro) {
22+
EXPECT_CFP_EQ(CMPLX(0, 1.0), I);
23+
EXPECT_CFP_EQ(CMPLX(1.0, 0), 1.0);
24+
EXPECT_CFP_EQ(CMPLXF(0, 1.0f), I);
25+
EXPECT_CFP_EQ(CMPLXF(1.0f, 0), 1.0f);
26+
EXPECT_CFP_EQ(CMPLXL(0, 1.0l), I);
27+
EXPECT_CFP_EQ(CMPLXL(1.0l, 0), 1.0l);
28+
}

0 commit comments

Comments
 (0)