Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
#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
9 changes: 9 additions & 0 deletions libc/test/include/complex_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading