[libc-commits] [libc] [libc][complex] Add cargf and carg functions to libc complex math (PR #204087)
via libc-commits
libc-commits at lists.llvm.org
Tue Jun 16 01:46:34 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: jinge90
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/204087.diff
15 Files Affected:
- (modified) libc/config/linux/aarch64/entrypoints.txt (+2)
- (modified) libc/config/linux/arm/entrypoints.txt (+2)
- (modified) libc/config/linux/riscv/entrypoints.txt (+2)
- (modified) libc/config/linux/x86_64/entrypoints.txt (+2)
- (modified) libc/include/complex.yaml (+12)
- (modified) libc/src/complex/CMakeLists.txt (+4)
- (added) libc/src/complex/carg.h (+20)
- (added) libc/src/complex/cargf.h (+20)
- (modified) libc/src/complex/generic/CMakeLists.txt (+25)
- (added) libc/src/complex/generic/carg.cpp (+22)
- (added) libc/src/complex/generic/cargf.cpp (+22)
- (added) libc/test/src/complex/CArgTest.h (+102)
- (modified) libc/test/src/complex/CMakeLists.txt (+20)
- (added) libc/test/src/complex/carg_test.cpp (+13)
- (added) libc/test/src/complex/cargf_test.cpp (+13)
``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 5cddf3dc89799..a5785620bb4c2 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -440,6 +440,8 @@ endif()
set(TARGET_LIBM_ENTRYPOINTS
# complex.h entrypoints
+ libc.src.complex.carg
+ libc.src.complex.cargf
libc.src.complex.creal
libc.src.complex.crealf
libc.src.complex.creall
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index c4ac53c4925a3..d2a75f0e1c739 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -236,6 +236,8 @@ endif()
set(TARGET_LIBM_ENTRYPOINTS
# complex.h entrypoints
+ libc.src.complex.carg
+ libc.src.complex.cargf
libc.src.complex.creal
libc.src.complex.crealf
libc.src.complex.creall
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index a57efbb8e464d..491e6df66162c 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -444,6 +444,8 @@ endif()
set(TARGET_LIBM_ENTRYPOINTS
# complex.h entrypoints
+ libc.src.complex.carg
+ libc.src.complex.cargf
libc.src.complex.creal
libc.src.complex.crealf
libc.src.complex.creall
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index ce88a6749d9dc..1ca5fab6da216 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -510,6 +510,8 @@ endif()
set(TARGET_LIBM_ENTRYPOINTS
# complex.h entrypoints
+ libc.src.complex.carg
+ libc.src.complex.cargf
libc.src.complex.creal
libc.src.complex.crealf
libc.src.complex.creall
diff --git a/libc/include/complex.yaml b/libc/include/complex.yaml
index 4db42be6082da..29799799d2e6b 100644
--- a/libc/include/complex.yaml
+++ b/libc/include/complex.yaml
@@ -21,6 +21,18 @@ types:
enums: []
objects: []
functions:
+ - name: carg
+ standards:
+ - stdc
+ return_type: double
+ arguments:
+ - type: _Complex double
+ - name: cargf
+ standards:
+ - stdc
+ return_type: float
+ arguments:
+ - type: _Complex float
- name: cimag
standards:
- stdc
diff --git a/libc/src/complex/CMakeLists.txt b/libc/src/complex/CMakeLists.txt
index bc66a5445d727..1ebc1e8ac6141 100644
--- a/libc/src/complex/CMakeLists.txt
+++ b/libc/src/complex/CMakeLists.txt
@@ -36,3 +36,7 @@ add_complex_entrypoint_object(cprojf)
add_complex_entrypoint_object(cprojl)
add_complex_entrypoint_object(cprojf16)
add_complex_entrypoint_object(cprojf128)
+
+add_complex_entrypoint_object(carg)
+add_complex_entrypoint_object(cargf)
+
diff --git a/libc/src/complex/carg.h b/libc/src/complex/carg.h
new file mode 100644
index 0000000000000..3dfaf592d38ce
--- /dev/null
+++ b/libc/src/complex/carg.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for carg --------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_COMPLEX_CARG_H
+#define LLVM_LIBC_SRC_COMPLEX_CARG_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+double carg(_Complex double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CARG_H
diff --git a/libc/src/complex/cargf.h b/libc/src/complex/cargf.h
new file mode 100644
index 0000000000000..24d3cc0684fc8
--- /dev/null
+++ b/libc/src/complex/cargf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cargf -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_COMPLEX_CARGF_H
+#define LLVM_LIBC_SRC_COMPLEX_CARGF_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float cargf(_Complex float x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CARGF_H
diff --git a/libc/src/complex/generic/CMakeLists.txt b/libc/src/complex/generic/CMakeLists.txt
index f1c21e4ed7271..f9e149c9c0230 100644
--- a/libc/src/complex/generic/CMakeLists.txt
+++ b/libc/src/complex/generic/CMakeLists.txt
@@ -1,3 +1,28 @@
+add_entrypoint_object(
+ carg
+ SRCS
+ carg.cpp
+ HDRS
+ ../carg.h
+ DEPENDS
+ libc.src.__support.CPP.bit
+ libc.src.__support.complex_type
+ libc.src.__support.math.atan2
+)
+
+add_entrypoint_object(
+ cargf
+ SRCS
+ cargf.cpp
+ HDRS
+ ../cargf.h
+ DEPENDS
+ libc.src.__support.CPP.bit
+ libc.src.__support.complex_type
+ libc.src.__support.math.atan2f
+)
+
+
add_entrypoint_object(
cproj
SRCS
diff --git a/libc/src/complex/generic/carg.cpp b/libc/src/complex/generic/carg.cpp
new file mode 100644
index 0000000000000..c0f731d55da1e
--- /dev/null
+++ b/libc/src/complex/generic/carg.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of carg function -----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/complex/carg.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/math/atan2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, carg, (_Complex double x)) {
+ Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
+ return math::atan2(x_c.imag, x_c.real);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cargf.cpp b/libc/src/complex/generic/cargf.cpp
new file mode 100644
index 0000000000000..7497192d8a147
--- /dev/null
+++ b/libc/src/complex/generic/cargf.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of cargf function ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/complex/cargf.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/math/atan2f.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, cargf, (_Complex float x)) {
+ Complex<float> x_c = cpp::bit_cast<Complex<float>>(x);
+ return math::atan2f(x_c.imag, x_c.real);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/complex/CArgTest.h b/libc/test/src/complex/CArgTest.h
new file mode 100644
index 0000000000000..890b3f280d9dc
--- /dev/null
+++ b/libc/test/src/complex/CArgTest.h
@@ -0,0 +1,102 @@
+//===-- Utility class to test different flavors of carg ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_SRC_COMPLEX_CARGTEST_H
+#define LLVM_LIBC_TEST_SRC_COMPLEX_CARGTEST_H
+
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "hdr/math_macros.h"
+
+template <typename CFPT, typename FPT>
+class CArgTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ DECLARE_SPECIAL_CONSTANTS(FPT)
+
+public:
+ typedef FPT (*CArgFunc)(CFPT);
+
+ void testZeroValues(CArgFunc func) {
+ EXPECT_FP_EQ(func(CFPT{0.0, 0.0}), zero);
+ EXPECT_FP_EQ(func(CFPT{0.0, -0.0}), neg_zero);
+ EXPECT_FP_EQ(func(CFPT{1.0, 0.0}), zero);
+ EXPECT_FP_EQ(func(CFPT{128.0, 0.0}), zero);
+ EXPECT_FP_EQ(func(CFPT{1.0, -0.0}), neg_zero);
+ }
+
+ void testInfinityValues(CArgFunc func) {
+ // carg(+inf + yi) = +0 for finite y > 0
+ EXPECT_FP_EQ(zero, func(CFPT{inf, 1.0}));
+ EXPECT_FP_EQ(zero, func(CFPT{inf, 256.0}));
+ // carg(+inf - yi) = -0 for finite y > 0
+ EXPECT_FP_EQ(neg_zero, func(CFPT{inf, -1.0}));
+ EXPECT_FP_EQ(neg_zero, func(CFPT{inf, -1024.0}));
+ // carg(+inf + 0i) = +0
+ EXPECT_FP_EQ(zero, func(CFPT{inf, 0.0}));
+ // carg(+inf - 0i) = -0
+ EXPECT_FP_EQ(neg_zero, func(CFPT{inf, -0.0}));
+
+ // carg(-inf + yi) = +pi for finite y > 0
+ EXPECT_FP_EQ(FPT(M_PI), func(CFPT{neg_inf, 1.0}));
+ EXPECT_FP_EQ(FPT(M_PI), func(CFPT{neg_inf, 64.0}));
+ // carg(-inf - yi) = -pi for finite y > 0
+ EXPECT_FP_EQ(FPT(-M_PI), func(CFPT{neg_inf,-1.0}));
+ EXPECT_FP_EQ(FPT(-M_PI), func(CFPT{neg_inf, -512.0}));
+ // carg(-inf + 0i) = +pi
+ EXPECT_FP_EQ(FPT(M_PI), func(CFPT{neg_inf, 0.0}));
+ // carg(-inf - 0i) = -pi
+ EXPECT_FP_EQ(FPT(-M_PI), func(CFPT{neg_inf, -0.0}));
+
+ // carg(x + inf*i) = +pi/2 for finite x
+ EXPECT_FP_EQ(FPT(M_PI_2), func(CFPT{1.0, inf}));
+ EXPECT_FP_EQ(FPT(M_PI_2), func(CFPT{-1.0, inf}));
+ EXPECT_FP_EQ(FPT(M_PI_2), func(CFPT{0.0, inf}));
+ EXPECT_FP_EQ(FPT(M_PI_2), func(CFPT{4.0, inf}));
+ // carg(x - inf*i) = -pi/2 for finite x
+ EXPECT_FP_EQ(FPT(-M_PI_2), func(CFPT{1.0, neg_inf}));
+ EXPECT_FP_EQ(FPT(-M_PI_2), func(CFPT{-1.0, neg_inf}));
+ EXPECT_FP_EQ(FPT(-M_PI_2), func(CFPT{0.0, neg_inf}));
+ EXPECT_FP_EQ(FPT(-M_PI_2), func(CFPT{4.0, neg_inf}));
+ }
+
+ void testNaNValues(CArgFunc func) {
+ // carg(NaN + yi) = NaN for finite y
+ EXPECT_FP_IS_NAN(func(CFPT{aNaN, 0.0}));
+ EXPECT_FP_IS_NAN(func(CFPT{aNaN, 1.0}));
+ EXPECT_FP_IS_NAN(func(CFPT{aNaN, -1.0}));
+ EXPECT_FP_IS_NAN(func(CFPT{aNaN, 512.0}));
+
+ // carg(x + NaN*i) = NaN for finite x
+ EXPECT_FP_IS_NAN(func(CFPT{0.0, aNaN}));
+ EXPECT_FP_IS_NAN(func(CFPT{1.0, aNaN}));
+ EXPECT_FP_IS_NAN(func(CFPT{-1.0, aNaN}));
+ EXPECT_FP_IS_NAN(func(CFPT{4.0, aNaN}));
+
+ // carg(NaN + NaN*i) = NaN
+ EXPECT_FP_IS_NAN(func(CFPT{aNaN, aNaN}));
+
+ // carg(+inf + NaN*i) = NaN
+ EXPECT_FP_IS_NAN(func(CFPT{inf, aNaN}));
+ // carg(-inf + NaN*i) = NaN
+ EXPECT_FP_IS_NAN(func(CFPT{neg_inf, aNaN}));
+ // carg(NaN + inf*i) = NaN
+ EXPECT_FP_IS_NAN(func(CFPT{aNaN, inf}));
+ // carg(NaN - inf*i) = NaN
+ EXPECT_FP_IS_NAN(func(CFPT{aNaN, neg_inf}));
+ }
+};
+
+#define LIST_CARG_TESTS(U, T, func) \
+ using LlvmLibcCArgTest = CArgTest<U, T>; \
+ TEST_F(LlvmLibcCArgTest, ZeroValues) { testZeroValues(&func); } \
+ TEST_F(LlvmLibcCArgTest, InfinityValues) { testInfinityValues(&func); } \
+ TEST_F(LlvmLibcCArgTest, NaNValues) { testNaNValues(&func); }
+
+#endif // LLVM_LIBC_TEST_SRC_COMPLEX_CARGTEST_H
diff --git a/libc/test/src/complex/CMakeLists.txt b/libc/test/src/complex/CMakeLists.txt
index efd1ede63eca5..fda44fa2f1422 100644
--- a/libc/test/src/complex/CMakeLists.txt
+++ b/libc/test/src/complex/CMakeLists.txt
@@ -1,5 +1,25 @@
add_custom_target(libc-complex-unittests)
+add_fp_unittest(
+ carg_test
+ SUITE
+ libc-complex-unittests
+ SRCS
+ carg_test.cpp
+ DEPENDS
+ libc.src.complex.carg
+)
+
+add_fp_unittest(
+ cargf_test
+ SUITE
+ libc-complex-unittests
+ SRCS
+ cargf_test.cpp
+ DEPENDS
+ libc.src.complex.cargf
+)
+
add_fp_unittest(
conj_test
SUITE
diff --git a/libc/test/src/complex/carg_test.cpp b/libc/test/src/complex/carg_test.cpp
new file mode 100644
index 0000000000000..f6fd1d7df5e31
--- /dev/null
+++ b/libc/test/src/complex/carg_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for carg -------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "CArgTest.h"
+
+#include "src/complex/carg.h"
+
+LIST_CARG_TESTS(_Complex double, double, LIBC_NAMESPACE::carg)
diff --git a/libc/test/src/complex/cargf_test.cpp b/libc/test/src/complex/cargf_test.cpp
new file mode 100644
index 0000000000000..1c47be9c644ae
--- /dev/null
+++ b/libc/test/src/complex/cargf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for cargf ------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "CArgTest.h"
+
+#include "src/complex/cargf.h"
+
+LIST_CARG_TESTS(_Complex float, float, LIBC_NAMESPACE::cargf)
``````````
</details>
https://github.com/llvm/llvm-project/pull/204087
More information about the libc-commits
mailing list