[libc-commits] [libc] [libc][complex] Implement different flavors of the `conj` function (PR #118671)

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Fri Dec 6 22:57:14 PST 2024


https://github.com/Sh0g0-1758 updated https://github.com/llvm/llvm-project/pull/118671

>From 1a591f1c8525ecdf0b09927375354f60a56de006 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Wed, 4 Dec 2024 22:55:00 +0530
Subject: [PATCH 1/7] Implement conj

---
 libc/config/linux/aarch64/entrypoints.txt |  5 ++
 libc/config/linux/arm/entrypoints.txt     |  3 +
 libc/config/linux/riscv/entrypoints.txt   |  4 +
 libc/config/linux/x86_64/entrypoints.txt  |  5 ++
 libc/src/complex/CMakeLists.txt           |  6 ++
 libc/src/complex/conj.h                   | 20 +++++
 libc/src/complex/conjf.h                  | 20 +++++
 libc/src/complex/conjf128.h               | 27 +++++++
 libc/src/complex/conjf16.h                | 27 +++++++
 libc/src/complex/conjl.h                  | 20 +++++
 libc/src/complex/generic/CMakeLists.txt   | 69 +++++++++++++++++
 libc/src/complex/generic/conj.cpp         | 21 ++++++
 libc/src/complex/generic/conjf.cpp        | 21 ++++++
 libc/src/complex/generic/conjf128.cpp     | 25 +++++++
 libc/src/complex/generic/conjf16.cpp      | 25 +++++++
 libc/src/complex/generic/conjl.cpp        | 21 ++++++
 libc/test/src/complex/CImagTest.h         |  7 +-
 libc/test/src/complex/CMakeLists.txt      | 60 +++++++++++++++
 libc/test/src/complex/CRealTest.h         |  6 +-
 libc/test/src/complex/ConjTest.h          | 90 +++++++++++++++++++++++
 libc/test/src/complex/conj_test.cpp       | 13 ++++
 libc/test/src/complex/conjf128_test.cpp   | 17 +++++
 libc/test/src/complex/conjf16_test.cpp    | 17 +++++
 libc/test/src/complex/conjf_test.cpp      | 13 ++++
 libc/test/src/complex/conjl_test.cpp      | 13 ++++
 25 files changed, 550 insertions(+), 5 deletions(-)
 create mode 100644 libc/src/complex/conj.h
 create mode 100644 libc/src/complex/conjf.h
 create mode 100644 libc/src/complex/conjf128.h
 create mode 100644 libc/src/complex/conjf16.h
 create mode 100644 libc/src/complex/conjl.h
 create mode 100644 libc/src/complex/generic/conj.cpp
 create mode 100644 libc/src/complex/generic/conjf.cpp
 create mode 100644 libc/src/complex/generic/conjf128.cpp
 create mode 100644 libc/src/complex/generic/conjf16.cpp
 create mode 100644 libc/src/complex/generic/conjl.cpp
 create mode 100644 libc/test/src/complex/ConjTest.h
 create mode 100644 libc/test/src/complex/conj_test.cpp
 create mode 100644 libc/test/src/complex/conjf128_test.cpp
 create mode 100644 libc/test/src/complex/conjf16_test.cpp
 create mode 100644 libc/test/src/complex/conjf_test.cpp
 create mode 100644 libc/test/src/complex/conjl_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index aa0b8ba0490e98..7b855e14a9e8eb 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -366,6 +366,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.cimag
     libc.src.complex.cimagf
     libc.src.complex.cimagl
+    libc.src.complex.conj
+    libc.src.complex.conjf
+    libc.src.complex.conjl
 
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
@@ -617,6 +620,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     # complex.h C23 _Complex _Float16 entrypoints
     # libc.src.complex.crealf16
     # libc.src.complex.cimagf16
+    # libc.src.complex.conjf16
     
     # math.h C23 _Float16 entrypoints
     libc.src.math.canonicalizef16
@@ -722,6 +726,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     # complex.h C23 _Complex _Float128 entrypoints
     libc.src.complex.crealf128
     libc.src.complex.cimagf128
+    libc.src.complex.conjf128
 
     # math.h C23 _Float128 entrypoints
     libc.src.math.canonicalizef128
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 31d81de06fb6b0..babdf92bb56764 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -207,6 +207,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.cimag
     libc.src.complex.cimagf
     libc.src.complex.cimagl
+    libc.src.complex.conj
+    libc.src.complex.conjf
+    libc.src.complex.conjl
     
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index fedc5002d8c246..c7db77fe8875a2 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -365,6 +365,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.cimag
     libc.src.complex.cimagf
     libc.src.complex.cimagl
+    libc.src.complex.conj
+    libc.src.complex.conjf
+    libc.src.complex.conjl
 
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
@@ -620,6 +623,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     # complex.h C23 _Complex _Float128 entrypoints
     libc.src.complex.crealf128
     libc.src.complex.cimagf128
+    libc.src.complex.conjf128
     
     # math.h C23 _Float128 entrypoints
     libc.src.math.canonicalizef128
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5e9cc71279ab16..69ac5652fba2f3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -366,6 +366,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.cimag
     libc.src.complex.cimagf
     libc.src.complex.cimagl
+    libc.src.complex.conj
+    libc.src.complex.conjf
+    libc.src.complex.conjl
     
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
@@ -622,6 +625,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     # complex.h C23 _Complex _Float16 entrypoints
     libc.src.complex.crealf16
     libc.src.complex.cimagf16
+    libc.src.complex.conjf16
 
     # math.h C23 _Float16 entrypoints
     libc.src.math.canonicalizef16
@@ -731,6 +735,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     # complex.h C23 _Complex _Float128 entrypoints
     # libc.src.complex.crealf128
     # libc.src.complex.cimagf128
+    # libc.src.complex.conjf128
     
     # math.h C23 _Float128 entrypoints
     libc.src.math.canonicalizef128
diff --git a/libc/src/complex/CMakeLists.txt b/libc/src/complex/CMakeLists.txt
index 289cce5455af78..7306e2fe925e3a 100644
--- a/libc/src/complex/CMakeLists.txt
+++ b/libc/src/complex/CMakeLists.txt
@@ -24,3 +24,9 @@ add_complex_entrypoint_object(cimagf)
 add_complex_entrypoint_object(cimagl)
 add_complex_entrypoint_object(cimagf16)
 add_complex_entrypoint_object(cimagf128)
+
+add_complex_entrypoint_object(conj)
+add_complex_entrypoint_object(conjf)
+add_complex_entrypoint_object(conjl)
+add_complex_entrypoint_object(conjf16)
+add_complex_entrypoint_object(conjf128)
diff --git a/libc/src/complex/conj.h b/libc/src/complex/conj.h
new file mode 100644
index 00000000000000..2ff82d27586202
--- /dev/null
+++ b/libc/src/complex/conj.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for conj --------------------------*- 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_CONJ_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJ_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex double conj(_Complex double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJ_H
diff --git a/libc/src/complex/conjf.h b/libc/src/complex/conjf.h
new file mode 100644
index 00000000000000..6b3bd612e5bf7d
--- /dev/null
+++ b/libc/src/complex/conjf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for conjf -------------------------*- 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_CONJF_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJF_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex float conjf(_Complex float x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJF_H
diff --git a/libc/src/complex/conjf128.h b/libc/src/complex/conjf128.h
new file mode 100644
index 00000000000000..587c979d315efb
--- /dev/null
+++ b/libc/src/complex/conjf128.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for conjf128 ----------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/macros/properties/complex_types.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT128)
+
+#ifndef LLVM_LIBC_SRC_COMPLEX_CONJF128_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJF128_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+cfloat128 conjf128(cfloat128 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJF128_H
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/conjf16.h b/libc/src/complex/conjf16.h
new file mode 100644
index 00000000000000..b15c5b3f61f4a0
--- /dev/null
+++ b/libc/src/complex/conjf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for conjf16 -----------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/macros/properties/complex_types.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+
+#ifndef LLVM_LIBC_SRC_COMPLEX_CONJF16_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJF16_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+cfloat16 conjf16(cfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJF16_H
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/conjl.h b/libc/src/complex/conjl.h
new file mode 100644
index 00000000000000..aec640f9433ac6
--- /dev/null
+++ b/libc/src/complex/conjl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for conjl -------------------------*- 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_CONJL_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex long double conjl(_Complex long double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJL_H
diff --git a/libc/src/complex/generic/CMakeLists.txt b/libc/src/complex/generic/CMakeLists.txt
index a3da781c602378..cc14f89122eddd 100644
--- a/libc/src/complex/generic/CMakeLists.txt
+++ b/libc/src/complex/generic/CMakeLists.txt
@@ -1,3 +1,72 @@
+add_entrypoint_object(
+  conj
+  SRCS
+    conj.cpp
+  HDRS
+    ../conj.h
+  COMPILE_OPTIONS
+    ${libc_opt_high_flag}
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
+)
+
+add_entrypoint_object(
+  conjf
+  SRCS
+    conjf.cpp
+  HDRS
+    ../conjf.h
+  COMPILE_OPTIONS
+    ${libc_opt_high_flag}
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
+)
+
+add_entrypoint_object(
+  conjl
+  SRCS
+    conjl.cpp
+  HDRS
+    ../conjl.h
+  COMPILE_OPTIONS
+    ${libc_opt_high_flag}
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
+)
+
+add_entrypoint_object(
+  conjf16
+  SRCS
+    conjf16.cpp
+  HDRS
+    ../conjf16.h
+  COMPILE_OPTIONS
+    ${libc_opt_high_flag}
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
+    libc.src.__support.macros.properties.types
+    libc.src.__support.macros.properties.complex_types
+)
+
+add_entrypoint_object(
+  conjf128
+  SRCS
+    conjf128.cpp
+  HDRS
+    ../conjf128.h
+  COMPILE_OPTIONS
+    ${libc_opt_high_flag}
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
+    libc.src.__support.macros.properties.types
+    libc.src.__support.macros.properties.complex_types
+)
+
 add_entrypoint_object(
   creal
   SRCS
diff --git a/libc/src/complex/generic/conj.cpp b/libc/src/complex/generic/conj.cpp
new file mode 100644
index 00000000000000..0d1376af34e9a0
--- /dev/null
+++ b/libc/src/complex/generic/conj.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of conj 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/conj.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex double, conj, (_Complex double x)) {
+  Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
+  return (x_c.real - x_c.imag * 1.0i);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjf.cpp b/libc/src/complex/generic/conjf.cpp
new file mode 100644
index 00000000000000..1d83a043f970de
--- /dev/null
+++ b/libc/src/complex/generic/conjf.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of conjf 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/conjf.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex float, conjf, (_Complex float x)) {
+  Complex<float> x_c = cpp::bit_cast<Complex<float>>(x);
+  return (x_c.real - x_c.imag * (_Complex float)1.0i);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjf128.cpp b/libc/src/complex/generic/conjf128.cpp
new file mode 100644
index 00000000000000..03114d331480b1
--- /dev/null
+++ b/libc/src/complex/generic/conjf128.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of conjf128 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/conjf128.h"
+#if defined(LIBC_TYPES_HAS_CFLOAT128)
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(cfloat128, conjf128, (cfloat128 x)) {
+  Complex<float128> x_c = cpp::bit_cast<Complex<float128>>(x);
+  return (x_c.real - x_c.imag * (cfloat128)1.0i);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/generic/conjf16.cpp b/libc/src/complex/generic/conjf16.cpp
new file mode 100644
index 00000000000000..4c1ed0e60c1f2f
--- /dev/null
+++ b/libc/src/complex/generic/conjf16.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of conjf16 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/conjf16.h"
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(cfloat16, conjf16, (cfloat16 x)) {
+  Complex<float16> x_c = cpp::bit_cast<Complex<float16>>(x);
+  return (x_c.real - x_c.imag * (cfloat16)1.0i);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/generic/conjl.cpp b/libc/src/complex/generic/conjl.cpp
new file mode 100644
index 00000000000000..9e49217551c0d8
--- /dev/null
+++ b/libc/src/complex/generic/conjl.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of conjl 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/conjl.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex long double, conjl, (_Complex long double x)) {
+  Complex<long double> x_c = cpp::bit_cast<Complex<long double>>(x);
+  return (x_c.real - x_c.imag * (_Complex long double)1.0i);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/complex/CImagTest.h b/libc/test/src/complex/CImagTest.h
index 6d2f9350026691..31236f7bf9a0d5 100644
--- a/libc/test/src/complex/CImagTest.h
+++ b/libc/test/src/complex/CImagTest.h
@@ -38,9 +38,10 @@ class CImagTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                  neg_min_denormal);
     EXPECT_FP_EQ(func(CFPT(1241.112 + max_denormal * 1.0i)), max_denormal);
     EXPECT_FP_EQ(func(CFPT(121.121 + zero * 1.0i)), zero);
-    EXPECT_FP_EQ(func(CFPT(neg_zero + zero * 1.0i)), zero);
-    EXPECT_FP_EQ(func(CFPT(neg_zero + neg_zero * 1.0i)), neg_zero);
-    EXPECT_FP_EQ(func(CFPT(zero + neg_zero * 1.0i)), neg_zero);
+    EXPECT_FP_EQ(func(CFPT(0.0 + 0.0i)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(-0.0 + 0.0i)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(0.0 - 0.0i)), -0.0);
+    EXPECT_FP_EQ(func(CFPT(-0.0 - 0.0i)), -0.0);
   }
 
   void testRoundedNumbers(CImagFunc func) {
diff --git a/libc/test/src/complex/CMakeLists.txt b/libc/test/src/complex/CMakeLists.txt
index 8f49e6d79e1796..0c668d9e1e8b9c 100644
--- a/libc/test/src/complex/CMakeLists.txt
+++ b/libc/test/src/complex/CMakeLists.txt
@@ -1,5 +1,65 @@
 add_custom_target(libc-complex-unittests)
 
+add_libc_test(
+  conj_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    conj_test.cpp
+  DEPENDS
+    libc.src.complex.conj
+  LINK_LIBRARIES
+      LibcFPTestHelpers
+)
+
+add_libc_test(
+  conjf_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    conjf_test.cpp
+  DEPENDS
+    libc.src.complex.conjf
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  conjl_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    conjl_test.cpp
+  DEPENDS
+    libc.src.complex.conjl
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  conjf16_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    conjf16_test.cpp
+  DEPENDS
+    libc.src.complex.conjf16
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  conjf128_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    conjf128_test.cpp
+  DEPENDS
+    libc.src.complex.conjf128
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
 add_libc_test(
   creal_test
   SUITE
diff --git a/libc/test/src/complex/CRealTest.h b/libc/test/src/complex/CRealTest.h
index a25555b506e225..9f1aed0fddfee1 100644
--- a/libc/test/src/complex/CRealTest.h
+++ b/libc/test/src/complex/CRealTest.h
@@ -37,8 +37,10 @@ class CRealTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(func(CFPT(neg_min_denormal + 781.134i)), neg_min_denormal);
     EXPECT_FP_EQ(func(CFPT(max_denormal + 1241.112i)), max_denormal);
     EXPECT_FP_EQ(func(CFPT(zero + 121.121i)), zero);
-    EXPECT_FP_EQ(func(CFPT(neg_zero + neg_zero * 1.0i)), neg_zero);
-    EXPECT_FP_EQ(func(CFPT(neg_zero + zero * 1.0i)), zero);
+    EXPECT_FP_EQ(func(CFPT(0.0 + 0.0i)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(-0.0 + 0.0i)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(0.0 - 0.0i)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(-0.0 - 0.0i)), -0.0);
   }
 
   void testRoundedNumbers(CRealFunc func) {
diff --git a/libc/test/src/complex/ConjTest.h b/libc/test/src/complex/ConjTest.h
new file mode 100644
index 00000000000000..6362316151eb92
--- /dev/null
+++ b/libc/test/src/complex/ConjTest.h
@@ -0,0 +1,90 @@
+//===-- Utility class to test different flavors of conj ---------*- 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_CONJTEST_H
+#define LLVM_LIBC_TEST_SRC_COMPLEX_CONJTEST_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 ConjTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+  DECLARE_SPECIAL_CONSTANTS(FPT)
+
+public:
+  typedef CFPT (*ConjFunc)(CFPT);
+
+void testSpecialNumbers(ConjFunc func) {
+    EXPECT_CFP_EQ(func(CFPT(aNaN + 67.123i)), CFPT(aNaN - 67.123i));
+    EXPECT_CFP_EQ(func(CFPT(neg_aNaN + 78.319i)), CFPT(neg_aNaN - 78.319i));
+    EXPECT_CFP_EQ(func(CFPT(sNaN + 7813.131i)), CFPT(sNaN - 7813.131i));
+    EXPECT_CFP_EQ(func(CFPT(neg_sNaN + 7824.152i)), CFPT(neg_sNaN - 7824.152i));
+    EXPECT_CFP_EQ(func(CFPT(inf + 9024.2442i)), CFPT(inf - 9024.2442i));
+    EXPECT_CFP_EQ(func(CFPT(neg_inf + 8923.124i)), CFPT(neg_inf - 8923.124i));
+    EXPECT_CFP_EQ(func(CFPT(min_normal + 782.124i)), CFPT(min_normal - 782.124i));
+    EXPECT_CFP_EQ(func(CFPT(max_normal + 2141.2352i)), CFPT(max_normal - 2141.2352i));
+    EXPECT_CFP_EQ(func(CFPT(neg_max_normal + 341.134i)), CFPT(neg_max_normal - 341.134i));
+    EXPECT_CFP_EQ(func(CFPT(min_denormal + 781.142i)), CFPT(min_denormal - 781.142i));
+    EXPECT_CFP_EQ(func(CFPT(neg_min_denormal + 781.134i)), CFPT(neg_min_denormal - 781.134i));
+    EXPECT_CFP_EQ(func(CFPT(max_denormal + 1241.112i)), CFPT(max_denormal - 1241.112i));
+    EXPECT_CFP_EQ(func(CFPT(zero + 121.121i)), CFPT(zero - 121.121i));
+    EXPECT_CFP_EQ(func(CFPT(67.123 + aNaN * 1.0i)), CFPT(67.123 - aNaN * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(78.319 + neg_aNaN * 1.0i)), CFPT(78.319 - neg_aNaN * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(7813.131 + sNaN * 1.0i)), CFPT(7813.131 - sNaN * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(7824.152 + neg_sNaN * 1.0i)), CFPT(7824.152 - neg_sNaN * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(9024.2442 + inf * 1.0i)), CFPT(9024.2442 - inf * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(8923.124 + neg_inf * 1.0i)), CFPT(8923.124 - neg_inf * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(782.124 + min_normal * 1.0i)), CFPT(782.124 - min_normal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(2141.2352 + max_normal * 1.0i)), CFPT(2141.2352 - max_normal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(341.134 + neg_max_normal * 1.0i)), CFPT(341.134 - neg_max_normal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(781.142 + min_denormal * 1.0i)), CFPT(781.142 - min_denormal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(781.134 + neg_min_denormal * 1.0i)), CFPT(781.134 - neg_min_denormal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(1241.112 + max_denormal * 1.0i)), CFPT(1241.112 - max_denormal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(121.121 + zero * 1.0i)), CFPT(121.121 - zero * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(0.0 - 0.0i)), CFPT(0.0 + 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(0.0 + 0.0i)), CFPT(0.0 - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(-0.0 - 0.0i)), CFPT(-0.0 + 0.0i));
+    // This test passes because -0.0 + 0.0i is actually
+    // CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = CMPLX(-0.0 + 0.0, 0.0) = 0.0 + 0.0i
+    EXPECT_CFP_EQ(func(CFPT(-0.0 + 0.0i)), CFPT(0.0 - 0.0i));
+
+}
+
+  void testRoundedNumbers(ConjFunc func) {
+    EXPECT_CFP_EQ(func((CFPT)(4523.1413 + 12413.1414i)), CFPT(4523.1413 - 12413.1414i));
+    EXPECT_CFP_EQ(func((CFPT)(-4523.1413 + 12413.1414i)), CFPT(-4523.1413 - 12413.1414i));
+    EXPECT_CFP_EQ(func((CFPT)(4523.1413 - 12413.1414i)), CFPT(4523.1413 + 12413.1414i));
+    EXPECT_CFP_EQ(func((CFPT)(-4523.1413 - 12413.1414i)), CFPT(-4523.1413 + 12413.1414i));
+
+    EXPECT_CFP_EQ(func((CFPT)(3210.5678 + 9876.5432i)), CFPT(3210.5678 - 9876.5432i));
+    EXPECT_CFP_EQ(func((CFPT)(-3210.5678 + 9876.5432i)), CFPT(-3210.5678 - 9876.5432i));
+    EXPECT_CFP_EQ(func((CFPT)(3210.5678 - 9876.5432i)), CFPT(3210.5678 + 9876.5432i));
+    EXPECT_CFP_EQ(func((CFPT)(-3210.5678 - 9876.5432i)), CFPT(-3210.5678 + 9876.5432i));
+
+    EXPECT_CFP_EQ(func((CFPT)(1234.4321 + 4321.1234i)), CFPT(1234.4321 - 4321.1234i));
+    EXPECT_CFP_EQ(func((CFPT)(-1234.4321 + 4321.1234i)), CFPT(-1234.4321 - 4321.1234i));
+    EXPECT_CFP_EQ(func((CFPT)(1234.4321 - 4321.1234i)), CFPT(1234.4321 + 4321.1234i));
+    EXPECT_CFP_EQ(func((CFPT)(-1234.4321 - 4321.1234i)), CFPT(-1234.4321 + 4321.1234i));
+
+    EXPECT_CFP_EQ(func((CFPT)(6789.1234 + 8765.6789i)), CFPT(6789.1234 - 8765.6789i));
+    EXPECT_CFP_EQ(func((CFPT)(-6789.1234 + 8765.6789i)), CFPT(-6789.1234 - 8765.6789i));
+    EXPECT_CFP_EQ(func((CFPT)(6789.1234 - 8765.6789i)), CFPT(6789.1234 + 8765.6789i));
+    EXPECT_CFP_EQ(func((CFPT)(-6789.1234 - 8765.6789i)), CFPT(-6789.1234 + 8765.6789i));
+  }
+};
+
+#define LIST_CONJ_TESTS(U, T, func)                                            \
+  using LlvmLibcConjTest = ConjTest<U, T>;                                     \
+  TEST_F(LlvmLibcConjTest, SpecialNumbers) { testSpecialNumbers(&func); }      \
+  TEST_F(LlvmLibcConjTest, RoundedNumbers) { testRoundedNumbers(&func); }
+
+#endif // LLVM_LIBC_TEST_SRC_COMPLEX_CONJTEST_H
diff --git a/libc/test/src/complex/conj_test.cpp b/libc/test/src/complex/conj_test.cpp
new file mode 100644
index 00000000000000..97445fa0650889
--- /dev/null
+++ b/libc/test/src/complex/conj_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for conj ------------------------------------------------===//
+//
+// 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 "ConjTest.h"
+
+#include "src/complex/conj.h"
+
+LIST_CONJ_TESTS(_Complex double, double, LIBC_NAMESPACE::conj)
diff --git a/libc/test/src/complex/conjf128_test.cpp b/libc/test/src/complex/conjf128_test.cpp
new file mode 100644
index 00000000000000..0059f550f6642a
--- /dev/null
+++ b/libc/test/src/complex/conjf128_test.cpp
@@ -0,0 +1,17 @@
+//===-- Unittests for conjf128 ---------------------------------------------===//
+//
+// 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 "ConjTest.h"
+
+#include "src/complex/conjf128.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT128)
+
+LIST_CONJ_TESTS(cfloat128, float128, LIBC_NAMESPACE::conjf128)
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/test/src/complex/conjf16_test.cpp b/libc/test/src/complex/conjf16_test.cpp
new file mode 100644
index 00000000000000..0de9f448e86812
--- /dev/null
+++ b/libc/test/src/complex/conjf16_test.cpp
@@ -0,0 +1,17 @@
+//===-- Unittests for conjf16 ---------------------------------------------===//
+//
+// 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 "ConjTest.h"
+
+#include "src/complex/conjf16.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+
+LIST_CONJ_TESTS(cfloat16, float16, LIBC_NAMESPACE::conjf16)
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/test/src/complex/conjf_test.cpp b/libc/test/src/complex/conjf_test.cpp
new file mode 100644
index 00000000000000..34b00b37996c30
--- /dev/null
+++ b/libc/test/src/complex/conjf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for conjf -----------------------------------------------===//
+//
+// 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 "ConjTest.h"
+
+#include "src/complex/conjf.h"
+
+LIST_CONJ_TESTS(_Complex float, float, LIBC_NAMESPACE::conjf)
diff --git a/libc/test/src/complex/conjl_test.cpp b/libc/test/src/complex/conjl_test.cpp
new file mode 100644
index 00000000000000..ec299f96315472
--- /dev/null
+++ b/libc/test/src/complex/conjl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for conjl -----------------------------------------------===//
+//
+// 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 "ConjTest.h"
+
+#include "src/complex/conjl.h"
+
+LIST_CONJ_TESTS(_Complex long double, long double, LIBC_NAMESPACE::conjl)

>From 25725a02afd1baebe96873b18ecd19c0c8d9b6e9 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Wed, 4 Dec 2024 22:55:18 +0530
Subject: [PATCH 2/7] fmt

---
 libc/test/src/complex/ConjTest.h        | 107 ++++++++++++++++--------
 libc/test/src/complex/conjf128_test.cpp |   3 +-
 2 files changed, 72 insertions(+), 38 deletions(-)

diff --git a/libc/test/src/complex/ConjTest.h b/libc/test/src/complex/ConjTest.h
index 6362316151eb92..2257f44909e6b9 100644
--- a/libc/test/src/complex/ConjTest.h
+++ b/libc/test/src/complex/ConjTest.h
@@ -23,62 +23,95 @@ class ConjTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 public:
   typedef CFPT (*ConjFunc)(CFPT);
 
-void testSpecialNumbers(ConjFunc func) {
+  void testSpecialNumbers(ConjFunc func) {
     EXPECT_CFP_EQ(func(CFPT(aNaN + 67.123i)), CFPT(aNaN - 67.123i));
     EXPECT_CFP_EQ(func(CFPT(neg_aNaN + 78.319i)), CFPT(neg_aNaN - 78.319i));
     EXPECT_CFP_EQ(func(CFPT(sNaN + 7813.131i)), CFPT(sNaN - 7813.131i));
     EXPECT_CFP_EQ(func(CFPT(neg_sNaN + 7824.152i)), CFPT(neg_sNaN - 7824.152i));
     EXPECT_CFP_EQ(func(CFPT(inf + 9024.2442i)), CFPT(inf - 9024.2442i));
     EXPECT_CFP_EQ(func(CFPT(neg_inf + 8923.124i)), CFPT(neg_inf - 8923.124i));
-    EXPECT_CFP_EQ(func(CFPT(min_normal + 782.124i)), CFPT(min_normal - 782.124i));
-    EXPECT_CFP_EQ(func(CFPT(max_normal + 2141.2352i)), CFPT(max_normal - 2141.2352i));
-    EXPECT_CFP_EQ(func(CFPT(neg_max_normal + 341.134i)), CFPT(neg_max_normal - 341.134i));
-    EXPECT_CFP_EQ(func(CFPT(min_denormal + 781.142i)), CFPT(min_denormal - 781.142i));
-    EXPECT_CFP_EQ(func(CFPT(neg_min_denormal + 781.134i)), CFPT(neg_min_denormal - 781.134i));
-    EXPECT_CFP_EQ(func(CFPT(max_denormal + 1241.112i)), CFPT(max_denormal - 1241.112i));
+    EXPECT_CFP_EQ(func(CFPT(min_normal + 782.124i)),
+                  CFPT(min_normal - 782.124i));
+    EXPECT_CFP_EQ(func(CFPT(max_normal + 2141.2352i)),
+                  CFPT(max_normal - 2141.2352i));
+    EXPECT_CFP_EQ(func(CFPT(neg_max_normal + 341.134i)),
+                  CFPT(neg_max_normal - 341.134i));
+    EXPECT_CFP_EQ(func(CFPT(min_denormal + 781.142i)),
+                  CFPT(min_denormal - 781.142i));
+    EXPECT_CFP_EQ(func(CFPT(neg_min_denormal + 781.134i)),
+                  CFPT(neg_min_denormal - 781.134i));
+    EXPECT_CFP_EQ(func(CFPT(max_denormal + 1241.112i)),
+                  CFPT(max_denormal - 1241.112i));
     EXPECT_CFP_EQ(func(CFPT(zero + 121.121i)), CFPT(zero - 121.121i));
     EXPECT_CFP_EQ(func(CFPT(67.123 + aNaN * 1.0i)), CFPT(67.123 - aNaN * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(78.319 + neg_aNaN * 1.0i)), CFPT(78.319 - neg_aNaN * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(7813.131 + sNaN * 1.0i)), CFPT(7813.131 - sNaN * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(7824.152 + neg_sNaN * 1.0i)), CFPT(7824.152 - neg_sNaN * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(9024.2442 + inf * 1.0i)), CFPT(9024.2442 - inf * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(8923.124 + neg_inf * 1.0i)), CFPT(8923.124 - neg_inf * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(782.124 + min_normal * 1.0i)), CFPT(782.124 - min_normal * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(2141.2352 + max_normal * 1.0i)), CFPT(2141.2352 - max_normal * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(341.134 + neg_max_normal * 1.0i)), CFPT(341.134 - neg_max_normal * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(781.142 + min_denormal * 1.0i)), CFPT(781.142 - min_denormal * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(781.134 + neg_min_denormal * 1.0i)), CFPT(781.134 - neg_min_denormal * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(1241.112 + max_denormal * 1.0i)), CFPT(1241.112 - max_denormal * 1.0i));
-    EXPECT_CFP_EQ(func(CFPT(121.121 + zero * 1.0i)), CFPT(121.121 - zero * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(78.319 + neg_aNaN * 1.0i)),
+                  CFPT(78.319 - neg_aNaN * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(7813.131 + sNaN * 1.0i)),
+                  CFPT(7813.131 - sNaN * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(7824.152 + neg_sNaN * 1.0i)),
+                  CFPT(7824.152 - neg_sNaN * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(9024.2442 + inf * 1.0i)),
+                  CFPT(9024.2442 - inf * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(8923.124 + neg_inf * 1.0i)),
+                  CFPT(8923.124 - neg_inf * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(782.124 + min_normal * 1.0i)),
+                  CFPT(782.124 - min_normal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(2141.2352 + max_normal * 1.0i)),
+                  CFPT(2141.2352 - max_normal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(341.134 + neg_max_normal * 1.0i)),
+                  CFPT(341.134 - neg_max_normal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(781.142 + min_denormal * 1.0i)),
+                  CFPT(781.142 - min_denormal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(781.134 + neg_min_denormal * 1.0i)),
+                  CFPT(781.134 - neg_min_denormal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(1241.112 + max_denormal * 1.0i)),
+                  CFPT(1241.112 - max_denormal * 1.0i));
+    EXPECT_CFP_EQ(func(CFPT(121.121 + zero * 1.0i)),
+                  CFPT(121.121 - zero * 1.0i));
     EXPECT_CFP_EQ(func(CFPT(0.0 - 0.0i)), CFPT(0.0 + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(0.0 + 0.0i)), CFPT(0.0 - 0.0i));
     EXPECT_CFP_EQ(func(CFPT(-0.0 - 0.0i)), CFPT(-0.0 + 0.0i));
     // This test passes because -0.0 + 0.0i is actually
     // CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = CMPLX(-0.0 + 0.0, 0.0) = 0.0 + 0.0i
     EXPECT_CFP_EQ(func(CFPT(-0.0 + 0.0i)), CFPT(0.0 - 0.0i));
-
-}
+  }
 
   void testRoundedNumbers(ConjFunc func) {
-    EXPECT_CFP_EQ(func((CFPT)(4523.1413 + 12413.1414i)), CFPT(4523.1413 - 12413.1414i));
-    EXPECT_CFP_EQ(func((CFPT)(-4523.1413 + 12413.1414i)), CFPT(-4523.1413 - 12413.1414i));
-    EXPECT_CFP_EQ(func((CFPT)(4523.1413 - 12413.1414i)), CFPT(4523.1413 + 12413.1414i));
-    EXPECT_CFP_EQ(func((CFPT)(-4523.1413 - 12413.1414i)), CFPT(-4523.1413 + 12413.1414i));
+    EXPECT_CFP_EQ(func((CFPT)(4523.1413 + 12413.1414i)),
+                  CFPT(4523.1413 - 12413.1414i));
+    EXPECT_CFP_EQ(func((CFPT)(-4523.1413 + 12413.1414i)),
+                  CFPT(-4523.1413 - 12413.1414i));
+    EXPECT_CFP_EQ(func((CFPT)(4523.1413 - 12413.1414i)),
+                  CFPT(4523.1413 + 12413.1414i));
+    EXPECT_CFP_EQ(func((CFPT)(-4523.1413 - 12413.1414i)),
+                  CFPT(-4523.1413 + 12413.1414i));
 
-    EXPECT_CFP_EQ(func((CFPT)(3210.5678 + 9876.5432i)), CFPT(3210.5678 - 9876.5432i));
-    EXPECT_CFP_EQ(func((CFPT)(-3210.5678 + 9876.5432i)), CFPT(-3210.5678 - 9876.5432i));
-    EXPECT_CFP_EQ(func((CFPT)(3210.5678 - 9876.5432i)), CFPT(3210.5678 + 9876.5432i));
-    EXPECT_CFP_EQ(func((CFPT)(-3210.5678 - 9876.5432i)), CFPT(-3210.5678 + 9876.5432i));
+    EXPECT_CFP_EQ(func((CFPT)(3210.5678 + 9876.5432i)),
+                  CFPT(3210.5678 - 9876.5432i));
+    EXPECT_CFP_EQ(func((CFPT)(-3210.5678 + 9876.5432i)),
+                  CFPT(-3210.5678 - 9876.5432i));
+    EXPECT_CFP_EQ(func((CFPT)(3210.5678 - 9876.5432i)),
+                  CFPT(3210.5678 + 9876.5432i));
+    EXPECT_CFP_EQ(func((CFPT)(-3210.5678 - 9876.5432i)),
+                  CFPT(-3210.5678 + 9876.5432i));
 
-    EXPECT_CFP_EQ(func((CFPT)(1234.4321 + 4321.1234i)), CFPT(1234.4321 - 4321.1234i));
-    EXPECT_CFP_EQ(func((CFPT)(-1234.4321 + 4321.1234i)), CFPT(-1234.4321 - 4321.1234i));
-    EXPECT_CFP_EQ(func((CFPT)(1234.4321 - 4321.1234i)), CFPT(1234.4321 + 4321.1234i));
-    EXPECT_CFP_EQ(func((CFPT)(-1234.4321 - 4321.1234i)), CFPT(-1234.4321 + 4321.1234i));
+    EXPECT_CFP_EQ(func((CFPT)(1234.4321 + 4321.1234i)),
+                  CFPT(1234.4321 - 4321.1234i));
+    EXPECT_CFP_EQ(func((CFPT)(-1234.4321 + 4321.1234i)),
+                  CFPT(-1234.4321 - 4321.1234i));
+    EXPECT_CFP_EQ(func((CFPT)(1234.4321 - 4321.1234i)),
+                  CFPT(1234.4321 + 4321.1234i));
+    EXPECT_CFP_EQ(func((CFPT)(-1234.4321 - 4321.1234i)),
+                  CFPT(-1234.4321 + 4321.1234i));
 
-    EXPECT_CFP_EQ(func((CFPT)(6789.1234 + 8765.6789i)), CFPT(6789.1234 - 8765.6789i));
-    EXPECT_CFP_EQ(func((CFPT)(-6789.1234 + 8765.6789i)), CFPT(-6789.1234 - 8765.6789i));
-    EXPECT_CFP_EQ(func((CFPT)(6789.1234 - 8765.6789i)), CFPT(6789.1234 + 8765.6789i));
-    EXPECT_CFP_EQ(func((CFPT)(-6789.1234 - 8765.6789i)), CFPT(-6789.1234 + 8765.6789i));
+    EXPECT_CFP_EQ(func((CFPT)(6789.1234 + 8765.6789i)),
+                  CFPT(6789.1234 - 8765.6789i));
+    EXPECT_CFP_EQ(func((CFPT)(-6789.1234 + 8765.6789i)),
+                  CFPT(-6789.1234 - 8765.6789i));
+    EXPECT_CFP_EQ(func((CFPT)(6789.1234 - 8765.6789i)),
+                  CFPT(6789.1234 + 8765.6789i));
+    EXPECT_CFP_EQ(func((CFPT)(-6789.1234 - 8765.6789i)),
+                  CFPT(-6789.1234 + 8765.6789i));
   }
 };
 
diff --git a/libc/test/src/complex/conjf128_test.cpp b/libc/test/src/complex/conjf128_test.cpp
index 0059f550f6642a..00cbaf32e69f59 100644
--- a/libc/test/src/complex/conjf128_test.cpp
+++ b/libc/test/src/complex/conjf128_test.cpp
@@ -1,4 +1,5 @@
-//===-- Unittests for conjf128 ---------------------------------------------===//
+//===-- Unittests for conjf128
+//---------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

>From 99bde32cc44e752de2e61d2d8d65c148a2c2b11f Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Wed, 4 Dec 2024 22:58:22 +0530
Subject: [PATCH 3/7] doc

---
 libc/docs/complex.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/docs/complex.rst b/libc/docs/complex.rst
index 1e74f16b60e660..3e375d5491640f 100644
--- a/libc/docs/complex.rst
+++ b/libc/docs/complex.rst
@@ -57,7 +57,7 @@ Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | cimag     | |check|          | |check|         | |check|                | |check|              | |check|                | 7.3.9.2                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| conj      |                  |                 |                        |                      |                        | 7.3.9.4                | N/A                        |
+| conj      | |check|          | |check|         | |check|                | |check|              | |check|                | 7.3.9.4                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | cproj     |                  |                 |                        |                      |                        | 7.3.9.5                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

>From dce8941a29bedbef6aa39a5f20a6ef63414e5f66 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sat, 7 Dec 2024 11:38:03 +0530
Subject: [PATCH 4/7] Add template function and a few tests

---
 libc/src/__support/complex_type.h     | 8 ++++++++
 libc/src/complex/generic/conj.cpp     | 3 +--
 libc/src/complex/generic/conjf.cpp    | 3 +--
 libc/src/complex/generic/conjf128.cpp | 3 +--
 libc/src/complex/generic/conjf16.cpp  | 3 +--
 libc/src/complex/generic/conjl.cpp    | 3 +--
 libc/test/src/complex/CImagTest.h     | 4 ++++
 libc/test/src/complex/CRealTest.h     | 4 ++++
 libc/test/src/complex/ConjTest.h      | 9 ++++++++-
 9 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/libc/src/__support/complex_type.h b/libc/src/__support/complex_type.h
index d6b5eec8fd6b24..b14c4ed0ef8f78 100644
--- a/libc/src/__support/complex_type.h
+++ b/libc/src/__support/complex_type.h
@@ -16,5 +16,13 @@ template <typename T> struct Complex {
   T real;
   T imag;
 };
+
+template <typename T, typename U>
+T conjugate(T c) {
+  Complex<U> c_c = cpp::bit_cast<Complex<U>>(c);
+  c_c.imag = -c_c.imag;
+  return cpp::bit_cast<T>(c_c);
+}
+
 } // namespace LIBC_NAMESPACE_DECL
 #endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
diff --git a/libc/src/complex/generic/conj.cpp b/libc/src/complex/generic/conj.cpp
index 0d1376af34e9a0..0b830a2d90f7dc 100644
--- a/libc/src/complex/generic/conj.cpp
+++ b/libc/src/complex/generic/conj.cpp
@@ -14,8 +14,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(_Complex double, conj, (_Complex double x)) {
-  Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
-  return (x_c.real - x_c.imag * 1.0i);
+  return conjugate<_Complex double, double>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjf.cpp b/libc/src/complex/generic/conjf.cpp
index 1d83a043f970de..bb15b6b19db55a 100644
--- a/libc/src/complex/generic/conjf.cpp
+++ b/libc/src/complex/generic/conjf.cpp
@@ -14,8 +14,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(_Complex float, conjf, (_Complex float x)) {
-  Complex<float> x_c = cpp::bit_cast<Complex<float>>(x);
-  return (x_c.real - x_c.imag * (_Complex float)1.0i);
+  return conjugate<_Complex float, float>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjf128.cpp b/libc/src/complex/generic/conjf128.cpp
index 03114d331480b1..ab68d146e3dc14 100644
--- a/libc/src/complex/generic/conjf128.cpp
+++ b/libc/src/complex/generic/conjf128.cpp
@@ -16,8 +16,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(cfloat128, conjf128, (cfloat128 x)) {
-  Complex<float128> x_c = cpp::bit_cast<Complex<float128>>(x);
-  return (x_c.real - x_c.imag * (cfloat128)1.0i);
+  return conjugate<cfloat128, float128>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjf16.cpp b/libc/src/complex/generic/conjf16.cpp
index 4c1ed0e60c1f2f..fc5e9a03eb4868 100644
--- a/libc/src/complex/generic/conjf16.cpp
+++ b/libc/src/complex/generic/conjf16.cpp
@@ -16,8 +16,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(cfloat16, conjf16, (cfloat16 x)) {
-  Complex<float16> x_c = cpp::bit_cast<Complex<float16>>(x);
-  return (x_c.real - x_c.imag * (cfloat16)1.0i);
+  return conjugate<cfloat16, float16>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjl.cpp b/libc/src/complex/generic/conjl.cpp
index 9e49217551c0d8..037fb306327736 100644
--- a/libc/src/complex/generic/conjl.cpp
+++ b/libc/src/complex/generic/conjl.cpp
@@ -14,8 +14,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(_Complex long double, conjl, (_Complex long double x)) {
-  Complex<long double> x_c = cpp::bit_cast<Complex<long double>>(x);
-  return (x_c.real - x_c.imag * (_Complex long double)1.0i);
+  return conjugate<_Complex long double, long double>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/complex/CImagTest.h b/libc/test/src/complex/CImagTest.h
index 31236f7bf9a0d5..408460d97dfc61 100644
--- a/libc/test/src/complex/CImagTest.h
+++ b/libc/test/src/complex/CImagTest.h
@@ -42,6 +42,10 @@ class CImagTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(func(CFPT(-0.0 + 0.0i)), 0.0);
     EXPECT_FP_EQ(func(CFPT(0.0 - 0.0i)), -0.0);
     EXPECT_FP_EQ(func(CFPT(-0.0 - 0.0i)), -0.0);
+    EXPECT_FP_EQ(func(CFPT(0.0)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(-0.0)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(0.0i)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(-0.0i)), -0.0);
   }
 
   void testRoundedNumbers(CImagFunc func) {
diff --git a/libc/test/src/complex/CRealTest.h b/libc/test/src/complex/CRealTest.h
index 9f1aed0fddfee1..80eafc9975f4c8 100644
--- a/libc/test/src/complex/CRealTest.h
+++ b/libc/test/src/complex/CRealTest.h
@@ -41,6 +41,10 @@ class CRealTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(func(CFPT(-0.0 + 0.0i)), 0.0);
     EXPECT_FP_EQ(func(CFPT(0.0 - 0.0i)), 0.0);
     EXPECT_FP_EQ(func(CFPT(-0.0 - 0.0i)), -0.0);
+    EXPECT_FP_EQ(func(CFPT(0.0)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(-0.0)), -0.0);
+    EXPECT_FP_EQ(func(CFPT(0.0i)), 0.0);
+    EXPECT_FP_EQ(func(CFPT(-0.0i)), -0.0);
   }
 
   void testRoundedNumbers(CRealFunc func) {
diff --git a/libc/test/src/complex/ConjTest.h b/libc/test/src/complex/ConjTest.h
index 2257f44909e6b9..9d3c625cb986a7 100644
--- a/libc/test/src/complex/ConjTest.h
+++ b/libc/test/src/complex/ConjTest.h
@@ -70,10 +70,17 @@ class ConjTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                   CFPT(121.121 - zero * 1.0i));
     EXPECT_CFP_EQ(func(CFPT(0.0 - 0.0i)), CFPT(0.0 + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(0.0 + 0.0i)), CFPT(0.0 - 0.0i));
-    EXPECT_CFP_EQ(func(CFPT(-0.0 - 0.0i)), CFPT(-0.0 + 0.0i));
+    // This test passes because the conjugate of -0.0 - 0.0i is -0.0 + 0.0i
+    // but -0.0 + 0.0i is actually CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = 0.0 + 0.0i
+    // so to represent -0.0 + 0.0i, we just write -0.0
+    EXPECT_CFP_EQ(func(CFPT(-0.0 - 0.0i)), CFPT(-0.0));
     // This test passes because -0.0 + 0.0i is actually
     // CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = CMPLX(-0.0 + 0.0, 0.0) = 0.0 + 0.0i
     EXPECT_CFP_EQ(func(CFPT(-0.0 + 0.0i)), CFPT(0.0 - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(0.0)), CFPT(0.0 - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(-0.0)), CFPT(-0.0 - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(0.0i)), CFPT(0.0 - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(-0.0i)), CFPT(-0.0));
   }
 
   void testRoundedNumbers(ConjFunc func) {

>From cf9892788a3278b58ea836be6ae64717dad58d79 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sat, 7 Dec 2024 11:38:30 +0530
Subject: [PATCH 5/7] fmt

---
 libc/src/__support/complex_type.h | 3 +--
 libc/test/src/complex/ConjTest.h  | 4 ++--
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/libc/src/__support/complex_type.h b/libc/src/__support/complex_type.h
index b14c4ed0ef8f78..34bdf6723110da 100644
--- a/libc/src/__support/complex_type.h
+++ b/libc/src/__support/complex_type.h
@@ -17,8 +17,7 @@ template <typename T> struct Complex {
   T imag;
 };
 
-template <typename T, typename U>
-T conjugate(T c) {
+template <typename T, typename U> T conjugate(T c) {
   Complex<U> c_c = cpp::bit_cast<Complex<U>>(c);
   c_c.imag = -c_c.imag;
   return cpp::bit_cast<T>(c_c);
diff --git a/libc/test/src/complex/ConjTest.h b/libc/test/src/complex/ConjTest.h
index 9d3c625cb986a7..0f372bfd0a191d 100644
--- a/libc/test/src/complex/ConjTest.h
+++ b/libc/test/src/complex/ConjTest.h
@@ -71,8 +71,8 @@ class ConjTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_CFP_EQ(func(CFPT(0.0 - 0.0i)), CFPT(0.0 + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(0.0 + 0.0i)), CFPT(0.0 - 0.0i));
     // This test passes because the conjugate of -0.0 - 0.0i is -0.0 + 0.0i
-    // but -0.0 + 0.0i is actually CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = 0.0 + 0.0i
-    // so to represent -0.0 + 0.0i, we just write -0.0
+    // but -0.0 + 0.0i is actually CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = 0.0 +
+    // 0.0i so to represent -0.0 + 0.0i, we just write -0.0
     EXPECT_CFP_EQ(func(CFPT(-0.0 - 0.0i)), CFPT(-0.0));
     // This test passes because -0.0 + 0.0i is actually
     // CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = CMPLX(-0.0 + 0.0, 0.0) = 0.0 + 0.0i

>From 979e9c0b710f8ae34c4a898229fafcfd767eff15 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sat, 7 Dec 2024 11:41:18 +0530
Subject: [PATCH 6/7] nit

---
 libc/test/src/complex/ConjTest.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/libc/test/src/complex/ConjTest.h b/libc/test/src/complex/ConjTest.h
index 0f372bfd0a191d..db25246a5efc36 100644
--- a/libc/test/src/complex/ConjTest.h
+++ b/libc/test/src/complex/ConjTest.h
@@ -70,9 +70,10 @@ class ConjTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                   CFPT(121.121 - zero * 1.0i));
     EXPECT_CFP_EQ(func(CFPT(0.0 - 0.0i)), CFPT(0.0 + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(0.0 + 0.0i)), CFPT(0.0 - 0.0i));
-    // This test passes because the conjugate of -0.0 - 0.0i is -0.0 + 0.0i
-    // but -0.0 + 0.0i is actually CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = 0.0 +
-    // 0.0i so to represent -0.0 + 0.0i, we just write -0.0
+    // This test passes because the conjugate of -0.0 - 0.0i is CMPLX(-0.0, 0.0)
+    // which cannot be represented as -0.0 + 0.0i because -0.0 + 0.0i is actually
+    // CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = 0.0 + 0.0i so to represent 
+    // CMPLX(-0.0, 0.0), we use -0.0
     EXPECT_CFP_EQ(func(CFPT(-0.0 - 0.0i)), CFPT(-0.0));
     // This test passes because -0.0 + 0.0i is actually
     // CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = CMPLX(-0.0 + 0.0, 0.0) = 0.0 + 0.0i

>From 7008416850977156c09c5948a61386833fa25f7d Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sat, 7 Dec 2024 11:41:37 +0530
Subject: [PATCH 7/7] fmt

---
 libc/test/src/complex/ConjTest.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/test/src/complex/ConjTest.h b/libc/test/src/complex/ConjTest.h
index db25246a5efc36..da4fb4fd137c8f 100644
--- a/libc/test/src/complex/ConjTest.h
+++ b/libc/test/src/complex/ConjTest.h
@@ -71,8 +71,8 @@ class ConjTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_CFP_EQ(func(CFPT(0.0 - 0.0i)), CFPT(0.0 + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(0.0 + 0.0i)), CFPT(0.0 - 0.0i));
     // This test passes because the conjugate of -0.0 - 0.0i is CMPLX(-0.0, 0.0)
-    // which cannot be represented as -0.0 + 0.0i because -0.0 + 0.0i is actually
-    // CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = 0.0 + 0.0i so to represent 
+    // which cannot be represented as -0.0 + 0.0i because -0.0 + 0.0i is
+    // actually CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = 0.0 + 0.0i so to represent
     // CMPLX(-0.0, 0.0), we use -0.0
     EXPECT_CFP_EQ(func(CFPT(-0.0 - 0.0i)), CFPT(-0.0));
     // This test passes because -0.0 + 0.0i is actually



More information about the libc-commits mailing list