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

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Tue Dec 17 09:16:17 PST 2024


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

>From ac1966c67ca5526f774d953e785484d66916e184 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 12 Dec 2024 22:15:04 +0530
Subject: [PATCH 1/8] implement cproj and add tests

---
 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/docs/headers/complex.rst             |   2 +-
 libc/hdrgen/yaml/complex.yaml             |  32 ++++++
 libc/src/complex/CMakeLists.txt           |   6 +
 libc/src/complex/cproj.h                  |  20 ++++
 libc/src/complex/cprojf.h                 |  20 ++++
 libc/src/complex/cprojf128.h              |  27 +++++
 libc/src/complex/cprojf16.h               |  27 +++++
 libc/src/complex/cprojl.h                 |  20 ++++
 libc/src/complex/generic/CMakeLists.txt   |  74 ++++++++++++
 libc/src/complex/generic/cproj.cpp        |  26 +++++
 libc/src/complex/generic/cprojf.cpp       |  26 +++++
 libc/src/complex/generic/cprojf128.cpp    |  30 +++++
 libc/src/complex/generic/cprojf16.cpp     |  30 +++++
 libc/src/complex/generic/cprojl.cpp       |  26 +++++
 libc/test/src/complex/CMakeLists.txt      |  60 ++++++++++
 libc/test/src/complex/CprojTest.h         | 133 ++++++++++++++++++++++
 libc/test/src/complex/cproj_test.cpp      |  13 +++
 libc/test/src/complex/cprojf128_test.cpp  |  17 +++
 libc/test/src/complex/cprojf16_test.cpp   |  17 +++
 libc/test/src/complex/cprojf_test.cpp     |  13 +++
 libc/test/src/complex/cprojl_test.cpp     |  13 +++
 25 files changed, 648 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/complex/cproj.h
 create mode 100644 libc/src/complex/cprojf.h
 create mode 100644 libc/src/complex/cprojf128.h
 create mode 100644 libc/src/complex/cprojf16.h
 create mode 100644 libc/src/complex/cprojl.h
 create mode 100644 libc/src/complex/generic/cproj.cpp
 create mode 100644 libc/src/complex/generic/cprojf.cpp
 create mode 100644 libc/src/complex/generic/cprojf128.cpp
 create mode 100644 libc/src/complex/generic/cprojf16.cpp
 create mode 100644 libc/src/complex/generic/cprojl.cpp
 create mode 100644 libc/test/src/complex/CprojTest.h
 create mode 100644 libc/test/src/complex/cproj_test.cpp
 create mode 100644 libc/test/src/complex/cprojf128_test.cpp
 create mode 100644 libc/test/src/complex/cprojf16_test.cpp
 create mode 100644 libc/test/src/complex/cprojf_test.cpp
 create mode 100644 libc/test/src/complex/cprojl_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index c2179938b40d18..b949e4b4f67ba0 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -370,6 +370,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.conj
     libc.src.complex.conjf
     libc.src.complex.conjl
+    libc.src.complex.cproj
+    libc.src.complex.cprojf
+    libc.src.complex.cprojl
 
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
@@ -622,6 +625,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     # libc.src.complex.crealf16
     # libc.src.complex.cimagf16
     # libc.src.complex.conjf16
+    # libc.src.complex.cprojf16
     
     # math.h C23 _Float16 entrypoints
     libc.src.math.canonicalizef16
@@ -728,6 +732,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     libc.src.complex.crealf128
     libc.src.complex.cimagf128
     libc.src.complex.conjf128
+    libc.src.complex.cprojf128
 
     # 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 f5e98277273961..83f4dfaaa2d0f2 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -212,6 +212,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.conj
     libc.src.complex.conjf
     libc.src.complex.conjl
+    libc.src.complex.cproj
+    libc.src.complex.cprojf
+    libc.src.complex.cprojl
     
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index e2df6aca38bf9d..19980f79e7be81 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -367,6 +367,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.conj
     libc.src.complex.conjf
     libc.src.complex.conjl
+    libc.src.complex.cproj
+    libc.src.complex.cprojf
+    libc.src.complex.cprojl
 
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
@@ -623,6 +626,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     libc.src.complex.crealf128
     libc.src.complex.cimagf128
     libc.src.complex.conjf128
+    libc.src.complex.cprojf128
     
     # 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 68d0501d3d3587..c5a2797d607ef7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -370,6 +370,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.conj
     libc.src.complex.conjf
     libc.src.complex.conjl
+    libc.src.complex.cproj
+    libc.src.complex.cprojf
+    libc.src.complex.cprojl
     
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
@@ -627,6 +630,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.complex.crealf16
     libc.src.complex.cimagf16
     libc.src.complex.conjf16
+    libc.src.complex.cprojf16
 
     # math.h C23 _Float16 entrypoints
     libc.src.math.canonicalizef16
@@ -737,6 +741,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     # libc.src.complex.crealf128
     # libc.src.complex.cimagf128
     # libc.src.complex.conjf128
+    # libc.src.complex.cprojf128
     
     # math.h C23 _Float128 entrypoints
     libc.src.math.canonicalizef128
diff --git a/libc/docs/headers/complex.rst b/libc/docs/headers/complex.rst
index b6a340543fad1f..272cf00c883bc4 100644
--- a/libc/docs/headers/complex.rst
+++ b/libc/docs/headers/complex.rst
@@ -59,7 +59,7 @@ Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | conj      | |check|          | |check|         | |check|                | |check|              | |check|                | 7.3.9.4                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| cproj     |                  |                 |                        |                      |                        | 7.3.9.5                | N/A                        |
+| cproj     | |check|          | |check|         | |check|                | |check|              | |check|                | 7.3.9.5                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | creal     | |check|          | |check|         | |check|                | |check|              | |check|                | 7.3.9.6                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/hdrgen/yaml/complex.yaml b/libc/hdrgen/yaml/complex.yaml
index be0d3c9ae59b47..cd81de7dd9e204 100644
--- a/libc/hdrgen/yaml/complex.yaml
+++ b/libc/hdrgen/yaml/complex.yaml
@@ -103,3 +103,35 @@ functions:
     arguments:
       - type: cfloat128
     guard: LIBC_TYPES_HAS_CFLOAT128
+  - name: cproj
+    standards:
+      - stdc
+    return_type: _Complex double
+    arguments:
+      - type: _Complex double
+  - name: cprojf
+    standards:
+      - stdc
+    return_type: _Complex float
+    arguments:
+      - type: _Complex float
+  - name: cprojl
+    standards:
+      - stdc
+    return_type: _Complex long double
+    arguments:
+      - type: _Complex long double
+  - name: cprojf16
+    standards:
+      - stdc
+    return_type: cfloat16
+    arguments:
+      - type: cfloat16
+    guard: LIBC_TYPES_HAS_CFLOAT16
+  - name: cprojf128
+    standards:
+      - stdc
+    return_type: cfloat128
+    arguments:
+      - type: cfloat128
+    guard: LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/CMakeLists.txt b/libc/src/complex/CMakeLists.txt
index 7306e2fe925e3a..bc66a5445d7274 100644
--- a/libc/src/complex/CMakeLists.txt
+++ b/libc/src/complex/CMakeLists.txt
@@ -30,3 +30,9 @@ add_complex_entrypoint_object(conjf)
 add_complex_entrypoint_object(conjl)
 add_complex_entrypoint_object(conjf16)
 add_complex_entrypoint_object(conjf128)
+
+add_complex_entrypoint_object(cproj)
+add_complex_entrypoint_object(cprojf)
+add_complex_entrypoint_object(cprojl)
+add_complex_entrypoint_object(cprojf16)
+add_complex_entrypoint_object(cprojf128)
diff --git a/libc/src/complex/cproj.h b/libc/src/complex/cproj.h
new file mode 100644
index 00000000000000..62d41bceec3e30
--- /dev/null
+++ b/libc/src/complex/cproj.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cproj -------------------------*- 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_CPROJ_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJ_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex double cproj(_Complex double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJ_H
diff --git a/libc/src/complex/cprojf.h b/libc/src/complex/cprojf.h
new file mode 100644
index 00000000000000..76124f91177763
--- /dev/null
+++ b/libc/src/complex/cprojf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cprojf ------------------------*- 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_CPROJF_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJF_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex float cprojf(_Complex float x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJF_H
diff --git a/libc/src/complex/cprojf128.h b/libc/src/complex/cprojf128.h
new file mode 100644
index 00000000000000..8de090b2c49b34
--- /dev/null
+++ b/libc/src/complex/cprojf128.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for cprojf128 ---------------------*- 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_CPROJF128_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJF128_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+cfloat128 cprojf128(cfloat128 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJF128_H
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/cprojf16.h b/libc/src/complex/cprojf16.h
new file mode 100644
index 00000000000000..e744011eb10c34
--- /dev/null
+++ b/libc/src/complex/cprojf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for cprojf16 ----------------------*- 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_CPROJF16_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJF16_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+cfloat16 cprojf16(cfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJF16_H
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/cprojl.h b/libc/src/complex/cprojl.h
new file mode 100644
index 00000000000000..ecc8dce8f85354
--- /dev/null
+++ b/libc/src/complex/cprojl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cprojl ------------------------*- 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_CPROJL_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex long double cprojl(_Complex long double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJL_H
diff --git a/libc/src/complex/generic/CMakeLists.txt b/libc/src/complex/generic/CMakeLists.txt
index cc14f89122eddd..139b3ed97af1bb 100644
--- a/libc/src/complex/generic/CMakeLists.txt
+++ b/libc/src/complex/generic/CMakeLists.txt
@@ -1,3 +1,77 @@
+add_entrypoint_object(
+  cproj
+  SRCS
+    cproj.cpp
+  HDRS
+    ../cproj.h
+  COMPILE_OPTIONS
+    ${libc_opt_high_flag}
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
+    libc.src.__support.FPUtil.basic_operations
+)
+
+add_entrypoint_object(
+  cprojf
+  SRCS
+    cprojf.cpp
+  HDRS
+    ../cprojf.h
+  COMPILE_OPTIONS
+    ${libc_opt_high_flag}
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
+    libc.src.__support.FPUtil.basic_operations
+)
+
+add_entrypoint_object(
+  cprojl
+  SRCS
+    cprojl.cpp
+  HDRS
+    ../cprojl.h
+  COMPILE_OPTIONS
+    ${libc_opt_high_flag}
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
+    libc.src.__support.FPUtil.basic_operations
+)
+
+add_entrypoint_object(
+  cprojf16
+  SRCS
+    cprojf16.cpp
+  HDRS
+    ../cprojf16.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
+    libc.src.__support.FPUtil.basic_operations
+)
+
+add_entrypoint_object(
+  cprojf128
+  SRCS
+    cprojf128.cpp
+  HDRS
+    ../cprojf128.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
+    libc.src.__support.FPUtil.basic_operations
+)
+
 add_entrypoint_object(
   conj
   SRCS
diff --git a/libc/src/complex/generic/cproj.cpp b/libc/src/complex/generic/cproj.cpp
new file mode 100644
index 00000000000000..857ff45587c40e
--- /dev/null
+++ b/libc/src/complex/generic/cproj.cpp
@@ -0,0 +1,26 @@
+//===-- Implementation of cproj 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/cproj.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex double, cproj, (_Complex double x)) {
+  Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
+  if(fputil::FPBits<double>(x_c.real).is_inf() || fputil::FPBits<double>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<_Complex double>(Complex<double>{(fputil::FPBits<double>::inf(Sign::POS).get_val()), (x_c.imag > 0 ? 0.0 : -0.0)});
+  } else {
+    return x;
+  }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojf.cpp b/libc/src/complex/generic/cprojf.cpp
new file mode 100644
index 00000000000000..edda389f74ac75
--- /dev/null
+++ b/libc/src/complex/generic/cprojf.cpp
@@ -0,0 +1,26 @@
+//===-- Implementation of cprojf 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/cprojf.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex float, cprojf, (_Complex float x)) {
+  Complex<float> x_c = cpp::bit_cast<Complex<float>>(x);
+  if(fputil::FPBits<float>(x_c.real).is_inf() || fputil::FPBits<float>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<_Complex float>(Complex<float>{(fputil::FPBits<float>::inf(Sign::POS).get_val()), (float)(x_c.imag > 0 ? 0.0 : -0.0)});
+  } else {
+    return x;
+  }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojf128.cpp b/libc/src/complex/generic/cprojf128.cpp
new file mode 100644
index 00000000000000..30fb1a1d7bd011
--- /dev/null
+++ b/libc/src/complex/generic/cprojf128.cpp
@@ -0,0 +1,30 @@
+//===-- Implementation of cprojf128 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/cprojf128.h"
+#if defined(LIBC_TYPES_HAS_CFLOAT128)
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(cfloat128, cprojf128, (cfloat128 x)) {
+  Complex<float128> x_c = cpp::bit_cast<Complex<float128>>(x);
+  if(fputil::FPBits<float128>(x_c.real).is_inf() || fputil::FPBits<float128>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<cfloat128>(Complex<float128>{(fputil::FPBits<float128>::inf(Sign::POS).get_val()), (float128)(x_c.imag > 0 ? 0.0 : -0.0)});
+  } else {
+    return x;
+  }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/generic/cprojf16.cpp b/libc/src/complex/generic/cprojf16.cpp
new file mode 100644
index 00000000000000..88bc502555c0cf
--- /dev/null
+++ b/libc/src/complex/generic/cprojf16.cpp
@@ -0,0 +1,30 @@
+//===-- Implementation of cprojf16 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/cprojf16.h"
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(cfloat16, cprojf16, (cfloat16 x)) {
+  Complex<float16> x_c = cpp::bit_cast<Complex<float16>>(x);
+  if(fputil::FPBits<float16>(x_c.real).is_inf() || fputil::FPBits<float16>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<cfloat16>(Complex<float16>{(fputil::FPBits<float16>::inf(Sign::POS).get_val()), (float16)(x_c.imag > 0 ? 0.0 : -0.0)});
+  } else {
+    return x;
+  }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/generic/cprojl.cpp b/libc/src/complex/generic/cprojl.cpp
new file mode 100644
index 00000000000000..25fbb5e2b060cc
--- /dev/null
+++ b/libc/src/complex/generic/cprojl.cpp
@@ -0,0 +1,26 @@
+//===-- Implementation of cprojl 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/cprojl.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex long double, cprojl, (_Complex long double x)) {
+  Complex<long double> x_c = cpp::bit_cast<Complex<long double>>(x);
+  if(fputil::FPBits<long double>(x_c.real).is_inf() || fputil::FPBits<long double>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<_Complex long double>(Complex<long double>{(fputil::FPBits<long double>::inf(Sign::POS).get_val()), (long double)(x_c.imag > 0 ? 0.0 : -0.0)});
+  } else {
+    return x;
+  }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/complex/CMakeLists.txt b/libc/test/src/complex/CMakeLists.txt
index 0c668d9e1e8b9c..d6b62e4686a223 100644
--- a/libc/test/src/complex/CMakeLists.txt
+++ b/libc/test/src/complex/CMakeLists.txt
@@ -60,6 +60,66 @@ add_libc_test(
     LibcFPTestHelpers
 )
 
+add_libc_test(
+  cproj_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cproj_test.cpp
+  DEPENDS
+    libc.src.complex.cproj
+  LINK_LIBRARIES
+      LibcFPTestHelpers
+)
+
+add_libc_test(
+  cprojf_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cprojf_test.cpp
+  DEPENDS
+    libc.src.complex.cprojf
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  cprojl_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cprojl_test.cpp
+  DEPENDS
+    libc.src.complex.cprojl
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  cprojf16_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cprojf16_test.cpp
+  DEPENDS
+    libc.src.complex.cprojf16
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  cprojf128_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cprojf128_test.cpp
+  DEPENDS
+    libc.src.complex.cprojf128
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
 add_libc_test(
   creal_test
   SUITE
diff --git a/libc/test/src/complex/CprojTest.h b/libc/test/src/complex/CprojTest.h
new file mode 100644
index 00000000000000..3fa9bbbbd9e9e1
--- /dev/null
+++ b/libc/test/src/complex/CprojTest.h
@@ -0,0 +1,133 @@
+//===-- Utility class to test different flavors of cproj --------*- 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_CPROJTEST_H
+#define LLVM_LIBC_TEST_SRC_COMPLEX_CPROJTEST_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 CprojTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+  DECLARE_SPECIAL_CONSTANTS(FPT)
+
+public:
+  typedef CFPT (*CprojFunc)(CFPT);
+
+  void testSpecialNumbers(CprojFunc func) {
+    EXPECT_CFP_EQ(func(CFPT(inf + 9024.2442i)), CFPT(inf + 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(inf - 9024.2442i)), CFPT(inf - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(neg_inf + 8923.124i)), CFPT(inf + 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(neg_inf - 8923.124i)), CFPT(inf - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(9024.2442 + inf * 1.0i)), CFPT(inf + 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(9024.2442 + neg_inf * 1.0i)), CFPT(inf - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(inf + neg_inf * 1.0i)), CFPT(inf - 0.0i)); 
+    EXPECT_CFP_EQ(func(CFPT(inf + inf * 1.0i)), CFPT(inf + 0.0i)); 
+    EXPECT_CFP_EQ(func(CFPT(neg_inf + neg_inf * 1.0i)), CFPT(inf - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(neg_inf + inf * 1.0i)), CFPT(inf + 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(neg_inf + inf * 1.0i)), CFPT(inf + 0.0i));
+    // EXPECT_CFP_EQ(func(CFPT(inf + aNaN * 1.0i)), CFPT(inf + 0.0i));
+    // EXPECT_CFP_EQ(func(CFPT(inf + neg_aNaN * 1.0i)), CFPT(inf - 0.0i));
+
+    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(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));
+    EXPECT_CFP_EQ(func(CFPT(-0.0 + 0.0i)), CFPT(-0.0 + 0.0i));
+  }
+
+  void testRoundedNumbers(CprojFunc 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_CPROJ_TESTS(U, T, func)                                            \
+  using LlvmLibcCprojTest = CprojTest<U, T>;                                    \
+  TEST_F(LlvmLibcCprojTest, SpecialNumbers) { testSpecialNumbers(&func); }      \
+  TEST_F(LlvmLibcCprojTest, RoundedNumbers) { testRoundedNumbers(&func); }
+
+#endif // LLVM_LIBC_TEST_SRC_COMPLEX_CPROJTEST_H
diff --git a/libc/test/src/complex/cproj_test.cpp b/libc/test/src/complex/cproj_test.cpp
new file mode 100644
index 00000000000000..83e5760f9ca823
--- /dev/null
+++ b/libc/test/src/complex/cproj_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for cproj -----------------------------------------------===//
+//
+// 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 "CprojTest.h"
+
+#include "src/complex/cproj.h"
+
+LIST_CPROJ_TESTS(_Complex double, double, LIBC_NAMESPACE::cproj)
diff --git a/libc/test/src/complex/cprojf128_test.cpp b/libc/test/src/complex/cprojf128_test.cpp
new file mode 100644
index 00000000000000..75708122260d66
--- /dev/null
+++ b/libc/test/src/complex/cprojf128_test.cpp
@@ -0,0 +1,17 @@
+//===-- Unittests for cprojf128 -------------------------------------------===//
+//
+// 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 "CprojTest.h"
+
+#include "src/complex/cprojf128.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT128)
+
+LIST_CPROJ_TESTS(cfloat128, float128, LIBC_NAMESPACE::cprojf128)
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/test/src/complex/cprojf16_test.cpp b/libc/test/src/complex/cprojf16_test.cpp
new file mode 100644
index 00000000000000..e3d336ce1a2f11
--- /dev/null
+++ b/libc/test/src/complex/cprojf16_test.cpp
@@ -0,0 +1,17 @@
+//===-- Unittests for cprojf16 ---------------------------------------------===//
+//
+// 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 "CprojTest.h"
+
+#include "src/complex/cprojf16.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+
+LIST_CPROJ_TESTS(cfloat16, float16, LIBC_NAMESPACE::cprojf16)
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/test/src/complex/cprojf_test.cpp b/libc/test/src/complex/cprojf_test.cpp
new file mode 100644
index 00000000000000..7123ed4e28d4b8
--- /dev/null
+++ b/libc/test/src/complex/cprojf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for cprojf ----------------------------------------------===//
+//
+// 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 "CprojTest.h"
+
+#include "src/complex/cprojf.h"
+
+LIST_CPROJ_TESTS(_Complex float, float, LIBC_NAMESPACE::cprojf)
diff --git a/libc/test/src/complex/cprojl_test.cpp b/libc/test/src/complex/cprojl_test.cpp
new file mode 100644
index 00000000000000..0858bf460188d5
--- /dev/null
+++ b/libc/test/src/complex/cprojl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for cprojl ----------------------------------------------===//
+//
+// 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 "CprojTest.h"
+
+#include "src/complex/cprojl.h"
+
+LIST_CPROJ_TESTS(_Complex long double, long double, LIBC_NAMESPACE::cprojl)

>From 2ff0a44b63417a48b946636ab58de2fbf5757ebd Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 12 Dec 2024 22:16:31 +0530
Subject: [PATCH 2/8] clang fmt

---
 libc/src/complex/generic/cproj.cpp      |  9 ++++++---
 libc/src/complex/generic/cprojf.cpp     |  9 ++++++---
 libc/src/complex/generic/cprojf128.cpp  |  9 ++++++---
 libc/src/complex/generic/cprojf16.cpp   |  9 ++++++---
 libc/src/complex/generic/cprojl.cpp     |  9 ++++++---
 libc/test/src/complex/CprojTest.h       | 14 +++++++-------
 libc/test/src/complex/cprojf16_test.cpp |  2 +-
 7 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/libc/src/complex/generic/cproj.cpp b/libc/src/complex/generic/cproj.cpp
index 857ff45587c40e..d8175a84b612c4 100644
--- a/libc/src/complex/generic/cproj.cpp
+++ b/libc/src/complex/generic/cproj.cpp
@@ -8,16 +8,19 @@
 
 #include "src/complex/cproj.h"
 #include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(_Complex double, cproj, (_Complex double x)) {
   Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
-  if(fputil::FPBits<double>(x_c.real).is_inf() || fputil::FPBits<double>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<_Complex double>(Complex<double>{(fputil::FPBits<double>::inf(Sign::POS).get_val()), (x_c.imag > 0 ? 0.0 : -0.0)});
+  if (fputil::FPBits<double>(x_c.real).is_inf() ||
+      fputil::FPBits<double>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<_Complex double>(
+        Complex<double>{(fputil::FPBits<double>::inf(Sign::POS).get_val()),
+                        (x_c.imag > 0 ? 0.0 : -0.0)});
   } else {
     return x;
   }
diff --git a/libc/src/complex/generic/cprojf.cpp b/libc/src/complex/generic/cprojf.cpp
index edda389f74ac75..d733731959324c 100644
--- a/libc/src/complex/generic/cprojf.cpp
+++ b/libc/src/complex/generic/cprojf.cpp
@@ -8,16 +8,19 @@
 
 #include "src/complex/cprojf.h"
 #include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(_Complex float, cprojf, (_Complex float x)) {
   Complex<float> x_c = cpp::bit_cast<Complex<float>>(x);
-  if(fputil::FPBits<float>(x_c.real).is_inf() || fputil::FPBits<float>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<_Complex float>(Complex<float>{(fputil::FPBits<float>::inf(Sign::POS).get_val()), (float)(x_c.imag > 0 ? 0.0 : -0.0)});
+  if (fputil::FPBits<float>(x_c.real).is_inf() ||
+      fputil::FPBits<float>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<_Complex float>(
+        Complex<float>{(fputil::FPBits<float>::inf(Sign::POS).get_val()),
+                       (float)(x_c.imag > 0 ? 0.0 : -0.0)});
   } else {
     return x;
   }
diff --git a/libc/src/complex/generic/cprojf128.cpp b/libc/src/complex/generic/cprojf128.cpp
index 30fb1a1d7bd011..57b76da7abc354 100644
--- a/libc/src/complex/generic/cprojf128.cpp
+++ b/libc/src/complex/generic/cprojf128.cpp
@@ -10,16 +10,19 @@
 #if defined(LIBC_TYPES_HAS_CFLOAT128)
 
 #include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(cfloat128, cprojf128, (cfloat128 x)) {
   Complex<float128> x_c = cpp::bit_cast<Complex<float128>>(x);
-  if(fputil::FPBits<float128>(x_c.real).is_inf() || fputil::FPBits<float128>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<cfloat128>(Complex<float128>{(fputil::FPBits<float128>::inf(Sign::POS).get_val()), (float128)(x_c.imag > 0 ? 0.0 : -0.0)});
+  if (fputil::FPBits<float128>(x_c.real).is_inf() ||
+      fputil::FPBits<float128>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<cfloat128>(
+        Complex<float128>{(fputil::FPBits<float128>::inf(Sign::POS).get_val()),
+                          (float128)(x_c.imag > 0 ? 0.0 : -0.0)});
   } else {
     return x;
   }
diff --git a/libc/src/complex/generic/cprojf16.cpp b/libc/src/complex/generic/cprojf16.cpp
index 88bc502555c0cf..4cabd65c768d63 100644
--- a/libc/src/complex/generic/cprojf16.cpp
+++ b/libc/src/complex/generic/cprojf16.cpp
@@ -10,16 +10,19 @@
 #if defined(LIBC_TYPES_HAS_CFLOAT16)
 
 #include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(cfloat16, cprojf16, (cfloat16 x)) {
   Complex<float16> x_c = cpp::bit_cast<Complex<float16>>(x);
-  if(fputil::FPBits<float16>(x_c.real).is_inf() || fputil::FPBits<float16>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<cfloat16>(Complex<float16>{(fputil::FPBits<float16>::inf(Sign::POS).get_val()), (float16)(x_c.imag > 0 ? 0.0 : -0.0)});
+  if (fputil::FPBits<float16>(x_c.real).is_inf() ||
+      fputil::FPBits<float16>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<cfloat16>(
+        Complex<float16>{(fputil::FPBits<float16>::inf(Sign::POS).get_val()),
+                         (float16)(x_c.imag > 0 ? 0.0 : -0.0)});
   } else {
     return x;
   }
diff --git a/libc/src/complex/generic/cprojl.cpp b/libc/src/complex/generic/cprojl.cpp
index 25fbb5e2b060cc..57d2516b23b600 100644
--- a/libc/src/complex/generic/cprojl.cpp
+++ b/libc/src/complex/generic/cprojl.cpp
@@ -8,16 +8,19 @@
 
 #include "src/complex/cprojl.h"
 #include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(_Complex long double, cprojl, (_Complex long double x)) {
   Complex<long double> x_c = cpp::bit_cast<Complex<long double>>(x);
-  if(fputil::FPBits<long double>(x_c.real).is_inf() || fputil::FPBits<long double>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<_Complex long double>(Complex<long double>{(fputil::FPBits<long double>::inf(Sign::POS).get_val()), (long double)(x_c.imag > 0 ? 0.0 : -0.0)});
+  if (fputil::FPBits<long double>(x_c.real).is_inf() ||
+      fputil::FPBits<long double>(x_c.imag).is_inf()) {
+    return cpp::bit_cast<_Complex long double>(Complex<long double>{
+        (fputil::FPBits<long double>::inf(Sign::POS).get_val()),
+        (long double)(x_c.imag > 0 ? 0.0 : -0.0)});
   } else {
     return x;
   }
diff --git a/libc/test/src/complex/CprojTest.h b/libc/test/src/complex/CprojTest.h
index 3fa9bbbbd9e9e1..6eb2a5c391832c 100644
--- a/libc/test/src/complex/CprojTest.h
+++ b/libc/test/src/complex/CprojTest.h
@@ -30,8 +30,8 @@ class CprojTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_CFP_EQ(func(CFPT(neg_inf - 8923.124i)), CFPT(inf - 0.0i));
     EXPECT_CFP_EQ(func(CFPT(9024.2442 + inf * 1.0i)), CFPT(inf + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(9024.2442 + neg_inf * 1.0i)), CFPT(inf - 0.0i));
-    EXPECT_CFP_EQ(func(CFPT(inf + neg_inf * 1.0i)), CFPT(inf - 0.0i)); 
-    EXPECT_CFP_EQ(func(CFPT(inf + inf * 1.0i)), CFPT(inf + 0.0i)); 
+    EXPECT_CFP_EQ(func(CFPT(inf + neg_inf * 1.0i)), CFPT(inf - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(inf + inf * 1.0i)), CFPT(inf + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(neg_inf + neg_inf * 1.0i)), CFPT(inf - 0.0i));
     EXPECT_CFP_EQ(func(CFPT(neg_inf + inf * 1.0i)), CFPT(inf + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(neg_inf + inf * 1.0i)), CFPT(inf + 0.0i));
@@ -63,9 +63,9 @@ class CprojTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     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));
+    //   CFPT(9024.2442 + inf * 1.0i));
     // EXPECT_CFP_EQ(func(CFPT(8923.124 + neg_inf * 1.0i)),
-                //   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)),
@@ -125,9 +125,9 @@ class CprojTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   }
 };
 
-#define LIST_CPROJ_TESTS(U, T, func)                                            \
-  using LlvmLibcCprojTest = CprojTest<U, T>;                                    \
-  TEST_F(LlvmLibcCprojTest, SpecialNumbers) { testSpecialNumbers(&func); }      \
+#define LIST_CPROJ_TESTS(U, T, func)                                           \
+  using LlvmLibcCprojTest = CprojTest<U, T>;                                   \
+  TEST_F(LlvmLibcCprojTest, SpecialNumbers) { testSpecialNumbers(&func); }     \
   TEST_F(LlvmLibcCprojTest, RoundedNumbers) { testRoundedNumbers(&func); }
 
 #endif // LLVM_LIBC_TEST_SRC_COMPLEX_CPROJTEST_H
diff --git a/libc/test/src/complex/cprojf16_test.cpp b/libc/test/src/complex/cprojf16_test.cpp
index e3d336ce1a2f11..628cec0dc5d960 100644
--- a/libc/test/src/complex/cprojf16_test.cpp
+++ b/libc/test/src/complex/cprojf16_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for cprojf16 ---------------------------------------------===//
+//===-- Unittests for cprojf16 --------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

>From 481f8af31dcc4dad56bd233572a02888e4c70db0 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 12 Dec 2024 22:29:23 +0530
Subject: [PATCH 3/8] add special tests

---
 libc/test/src/complex/CprojTest.h | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/libc/test/src/complex/CprojTest.h b/libc/test/src/complex/CprojTest.h
index 6eb2a5c391832c..7fa0b38349b554 100644
--- a/libc/test/src/complex/CprojTest.h
+++ b/libc/test/src/complex/CprojTest.h
@@ -35,8 +35,11 @@ class CprojTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_CFP_EQ(func(CFPT(neg_inf + neg_inf * 1.0i)), CFPT(inf - 0.0i));
     EXPECT_CFP_EQ(func(CFPT(neg_inf + inf * 1.0i)), CFPT(inf + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(neg_inf + inf * 1.0i)), CFPT(inf + 0.0i));
-    // EXPECT_CFP_EQ(func(CFPT(inf + aNaN * 1.0i)), CFPT(inf + 0.0i));
-    // EXPECT_CFP_EQ(func(CFPT(inf + neg_aNaN * 1.0i)), CFPT(inf - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(aNaN + inf * 1.0i)), CFPT(inf + 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(aNaN + neg_inf * 1.0i)), CFPT(inf - 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(90.24 + inf * 1.0i)), CFPT(inf + 0.0i));
+    EXPECT_CFP_EQ(func(CFPT(89.12 + neg_inf * 1.0i)), CFPT(inf - 0.0i));
+
 
     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));
@@ -62,10 +65,6 @@ class CprojTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                   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)),

>From 7cc53ea1dc8596ff6b1003bc946d3f8fbdde63ad Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 12 Dec 2024 22:29:41 +0530
Subject: [PATCH 4/8] fmt

---
 libc/test/src/complex/CprojTest.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/test/src/complex/CprojTest.h b/libc/test/src/complex/CprojTest.h
index 7fa0b38349b554..4e2f6cc58a5aed 100644
--- a/libc/test/src/complex/CprojTest.h
+++ b/libc/test/src/complex/CprojTest.h
@@ -40,7 +40,6 @@ class CprojTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_CFP_EQ(func(CFPT(90.24 + inf * 1.0i)), CFPT(inf + 0.0i));
     EXPECT_CFP_EQ(func(CFPT(89.12 + neg_inf * 1.0i)), CFPT(inf - 0.0i));
 
-
     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));

>From 4409ad2a00d4d3f36f50d441391355f9cdce2f1f Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 17 Dec 2024 15:19:52 +0530
Subject: [PATCH 5/8] templated cproj func

---
 libc/src/__support/CMakeLists.txt       |  3 +++
 libc/src/__support/complex_type.h       | 14 ++++++++++++++
 libc/src/complex/generic/CMakeLists.txt | 15 ---------------
 libc/src/complex/generic/conj.cpp       |  1 -
 libc/src/complex/generic/conjf.cpp      |  1 -
 libc/src/complex/generic/conjf128.cpp   |  1 -
 libc/src/complex/generic/conjf16.cpp    |  1 -
 libc/src/complex/generic/conjl.cpp      |  1 -
 libc/src/complex/generic/cproj.cpp      | 12 +-----------
 libc/src/complex/generic/cprojf.cpp     | 12 +-----------
 libc/src/complex/generic/cprojf128.cpp  | 12 +-----------
 libc/src/complex/generic/cprojf16.cpp   | 12 +-----------
 libc/src/complex/generic/cprojl.cpp     | 12 +-----------
 13 files changed, 22 insertions(+), 75 deletions(-)

diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 8f85740f70a06e..6c65bcbb01bfe6 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -235,6 +235,9 @@ add_header_library(
   complex_type
   HDRS
     complex_type.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.FPUtil.fp_bits
 )
 
 add_header_library(
diff --git a/libc/src/__support/complex_type.h b/libc/src/__support/complex_type.h
index a6207d38d0eb57..3ffde8a49ffeef 100644
--- a/libc/src/__support/complex_type.h
+++ b/libc/src/__support/complex_type.h
@@ -12,6 +12,8 @@
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/complex_types.h"
 #include "src/__support/macros/properties/types.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/FPBits.h"
 
 namespace LIBC_NAMESPACE_DECL {
 template <typename T> struct Complex {
@@ -77,5 +79,17 @@ template <typename T> LIBC_INLINE constexpr T conjugate(T c) {
   return cpp::bit_cast<T>(c_c);
 }
 
+template <typename T> LIBC_INLINE constexpr T project(T c) {
+  Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
+  if (fputil::FPBits<make_real_t<T>>(c_c.real).is_inf() ||
+      fputil::FPBits<make_real_t<T>>(c_c.imag).is_inf()) {
+    return cpp::bit_cast<T>(
+        Complex<make_real_t<T>>{(fputil::FPBits<make_real_t<T>>::inf(Sign::POS).get_val()),
+                        static_cast<make_real_t<T>>(c_c.imag > 0 ? 0.0 : -0.0)});
+  } else {
+    return c;
+  }
+}
+
 } // namespace LIBC_NAMESPACE_DECL
 #endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
diff --git a/libc/src/complex/generic/CMakeLists.txt b/libc/src/complex/generic/CMakeLists.txt
index 139b3ed97af1bb..3dae6f8a614953 100644
--- a/libc/src/complex/generic/CMakeLists.txt
+++ b/libc/src/complex/generic/CMakeLists.txt
@@ -7,9 +7,7 @@ add_entrypoint_object(
   COMPILE_OPTIONS
     ${libc_opt_high_flag}
   DEPENDS
-    libc.src.__support.CPP.bit
     libc.src.__support.complex_type
-    libc.src.__support.FPUtil.basic_operations
 )
 
 add_entrypoint_object(
@@ -21,9 +19,7 @@ add_entrypoint_object(
   COMPILE_OPTIONS
     ${libc_opt_high_flag}
   DEPENDS
-    libc.src.__support.CPP.bit
     libc.src.__support.complex_type
-    libc.src.__support.FPUtil.basic_operations
 )
 
 add_entrypoint_object(
@@ -35,9 +31,7 @@ add_entrypoint_object(
   COMPILE_OPTIONS
     ${libc_opt_high_flag}
   DEPENDS
-    libc.src.__support.CPP.bit
     libc.src.__support.complex_type
-    libc.src.__support.FPUtil.basic_operations
 )
 
 add_entrypoint_object(
@@ -49,11 +43,9 @@ add_entrypoint_object(
   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
-    libc.src.__support.FPUtil.basic_operations
 )
 
 add_entrypoint_object(
@@ -65,11 +57,9 @@ add_entrypoint_object(
   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
-    libc.src.__support.FPUtil.basic_operations
 )
 
 add_entrypoint_object(
@@ -81,7 +71,6 @@ add_entrypoint_object(
   COMPILE_OPTIONS
     ${libc_opt_high_flag}
   DEPENDS
-    libc.src.__support.CPP.bit
     libc.src.__support.complex_type
 )
 
@@ -94,7 +83,6 @@ add_entrypoint_object(
   COMPILE_OPTIONS
     ${libc_opt_high_flag}
   DEPENDS
-    libc.src.__support.CPP.bit
     libc.src.__support.complex_type
 )
 
@@ -107,7 +95,6 @@ add_entrypoint_object(
   COMPILE_OPTIONS
     ${libc_opt_high_flag}
   DEPENDS
-    libc.src.__support.CPP.bit
     libc.src.__support.complex_type
 )
 
@@ -120,7 +107,6 @@ add_entrypoint_object(
   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
@@ -135,7 +121,6 @@ add_entrypoint_object(
   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
diff --git a/libc/src/complex/generic/conj.cpp b/libc/src/complex/generic/conj.cpp
index 1a93bc25dc3c49..cbcd480d6efa5d 100644
--- a/libc/src/complex/generic/conj.cpp
+++ b/libc/src/complex/generic/conj.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/conj.h"
-#include "src/__support/CPP/bit.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
 
diff --git a/libc/src/complex/generic/conjf.cpp b/libc/src/complex/generic/conjf.cpp
index 33cb34340a04e6..a1af3d78ebc6a4 100644
--- a/libc/src/complex/generic/conjf.cpp
+++ b/libc/src/complex/generic/conjf.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/conjf.h"
-#include "src/__support/CPP/bit.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
 
diff --git a/libc/src/complex/generic/conjf128.cpp b/libc/src/complex/generic/conjf128.cpp
index 4e35b3d5a97b72..c65b54849f52ec 100644
--- a/libc/src/complex/generic/conjf128.cpp
+++ b/libc/src/complex/generic/conjf128.cpp
@@ -9,7 +9,6 @@
 #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"
 
diff --git a/libc/src/complex/generic/conjf16.cpp b/libc/src/complex/generic/conjf16.cpp
index 2564fe252027ac..dac11e27b30a29 100644
--- a/libc/src/complex/generic/conjf16.cpp
+++ b/libc/src/complex/generic/conjf16.cpp
@@ -9,7 +9,6 @@
 #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"
 
diff --git a/libc/src/complex/generic/conjl.cpp b/libc/src/complex/generic/conjl.cpp
index dc071ab1ec51bc..8298ede6fa38f3 100644
--- a/libc/src/complex/generic/conjl.cpp
+++ b/libc/src/complex/generic/conjl.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/conjl.h"
-#include "src/__support/CPP/bit.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
 
diff --git a/libc/src/complex/generic/cproj.cpp b/libc/src/complex/generic/cproj.cpp
index d8175a84b612c4..d5e8c3ff3d9ec7 100644
--- a/libc/src/complex/generic/cproj.cpp
+++ b/libc/src/complex/generic/cproj.cpp
@@ -7,23 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/cproj.h"
-#include "src/__support/CPP/bit.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(_Complex double, cproj, (_Complex double x)) {
-  Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
-  if (fputil::FPBits<double>(x_c.real).is_inf() ||
-      fputil::FPBits<double>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<_Complex double>(
-        Complex<double>{(fputil::FPBits<double>::inf(Sign::POS).get_val()),
-                        (x_c.imag > 0 ? 0.0 : -0.0)});
-  } else {
-    return x;
-  }
+  return project<_Complex double>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojf.cpp b/libc/src/complex/generic/cprojf.cpp
index d733731959324c..d0235f6bfef7e6 100644
--- a/libc/src/complex/generic/cprojf.cpp
+++ b/libc/src/complex/generic/cprojf.cpp
@@ -7,23 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/cprojf.h"
-#include "src/__support/CPP/bit.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(_Complex float, cprojf, (_Complex float x)) {
-  Complex<float> x_c = cpp::bit_cast<Complex<float>>(x);
-  if (fputil::FPBits<float>(x_c.real).is_inf() ||
-      fputil::FPBits<float>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<_Complex float>(
-        Complex<float>{(fputil::FPBits<float>::inf(Sign::POS).get_val()),
-                       (float)(x_c.imag > 0 ? 0.0 : -0.0)});
-  } else {
-    return x;
-  }
+  return project<_Complex float>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojf128.cpp b/libc/src/complex/generic/cprojf128.cpp
index 57b76da7abc354..97134b5523a564 100644
--- a/libc/src/complex/generic/cprojf128.cpp
+++ b/libc/src/complex/generic/cprojf128.cpp
@@ -9,23 +9,13 @@
 #include "src/complex/cprojf128.h"
 #if defined(LIBC_TYPES_HAS_CFLOAT128)
 
-#include "src/__support/CPP/bit.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(cfloat128, cprojf128, (cfloat128 x)) {
-  Complex<float128> x_c = cpp::bit_cast<Complex<float128>>(x);
-  if (fputil::FPBits<float128>(x_c.real).is_inf() ||
-      fputil::FPBits<float128>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<cfloat128>(
-        Complex<float128>{(fputil::FPBits<float128>::inf(Sign::POS).get_val()),
-                          (float128)(x_c.imag > 0 ? 0.0 : -0.0)});
-  } else {
-    return x;
-  }
+  return project<cfloat128>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojf16.cpp b/libc/src/complex/generic/cprojf16.cpp
index 4cabd65c768d63..bd0425ffb5fe57 100644
--- a/libc/src/complex/generic/cprojf16.cpp
+++ b/libc/src/complex/generic/cprojf16.cpp
@@ -9,23 +9,13 @@
 #include "src/complex/cprojf16.h"
 #if defined(LIBC_TYPES_HAS_CFLOAT16)
 
-#include "src/__support/CPP/bit.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(cfloat16, cprojf16, (cfloat16 x)) {
-  Complex<float16> x_c = cpp::bit_cast<Complex<float16>>(x);
-  if (fputil::FPBits<float16>(x_c.real).is_inf() ||
-      fputil::FPBits<float16>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<cfloat16>(
-        Complex<float16>{(fputil::FPBits<float16>::inf(Sign::POS).get_val()),
-                         (float16)(x_c.imag > 0 ? 0.0 : -0.0)});
-  } else {
-    return x;
-  }
+  return project<cfloat16>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojl.cpp b/libc/src/complex/generic/cprojl.cpp
index 57d2516b23b600..34deeb63b16d01 100644
--- a/libc/src/complex/generic/cprojl.cpp
+++ b/libc/src/complex/generic/cprojl.cpp
@@ -7,23 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/cprojl.h"
-#include "src/__support/CPP/bit.h"
-#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(_Complex long double, cprojl, (_Complex long double x)) {
-  Complex<long double> x_c = cpp::bit_cast<Complex<long double>>(x);
-  if (fputil::FPBits<long double>(x_c.real).is_inf() ||
-      fputil::FPBits<long double>(x_c.imag).is_inf()) {
-    return cpp::bit_cast<_Complex long double>(Complex<long double>{
-        (fputil::FPBits<long double>::inf(Sign::POS).get_val()),
-        (long double)(x_c.imag > 0 ? 0.0 : -0.0)});
-  } else {
-    return x;
-  }
+  return project<_Complex long double>(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL

>From 248ee3699e69e128335280ee63c1e994c9dd5958 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 17 Dec 2024 15:20:24 +0530
Subject: [PATCH 6/8] clang-fmt

---
 libc/src/__support/complex_type.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libc/src/__support/complex_type.h b/libc/src/__support/complex_type.h
index 3ffde8a49ffeef..66d6a190a2396b 100644
--- a/libc/src/__support/complex_type.h
+++ b/libc/src/__support/complex_type.h
@@ -9,11 +9,11 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
 #define LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
 
+#include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/complex_types.h"
 #include "src/__support/macros/properties/types.h"
-#include "src/__support/CPP/bit.h"
-#include "src/__support/FPUtil/FPBits.h"
 
 namespace LIBC_NAMESPACE_DECL {
 template <typename T> struct Complex {
@@ -83,9 +83,9 @@ template <typename T> LIBC_INLINE constexpr T project(T c) {
   Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
   if (fputil::FPBits<make_real_t<T>>(c_c.real).is_inf() ||
       fputil::FPBits<make_real_t<T>>(c_c.imag).is_inf()) {
-    return cpp::bit_cast<T>(
-        Complex<make_real_t<T>>{(fputil::FPBits<make_real_t<T>>::inf(Sign::POS).get_val()),
-                        static_cast<make_real_t<T>>(c_c.imag > 0 ? 0.0 : -0.0)});
+    return cpp::bit_cast<T>(Complex<make_real_t<T>>{
+        (fputil::FPBits<make_real_t<T>>::inf(Sign::POS).get_val()),
+        static_cast<make_real_t<T>>(c_c.imag > 0 ? 0.0 : -0.0)});
   } else {
     return c;
   }

>From 648c338e8d4bf73f6902fa74d515c58ea8d12d25 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 17 Dec 2024 15:28:54 +0530
Subject: [PATCH 7/8] reduced some deps

---
 libc/src/__support/CMakeLists.txt | 2 ++
 libc/src/complex/conjf128.h       | 1 -
 libc/src/complex/conjf16.h        | 1 -
 libc/src/complex/cprojf128.h      | 1 -
 libc/src/complex/cprojf16.h       | 1 -
 5 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 6c65bcbb01bfe6..03d83704ad31f2 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -238,6 +238,8 @@ add_header_library(
   DEPENDS
     libc.src.__support.CPP.bit
     libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.macros.properties.types
+    libc.src.__support.macros.properties.complex_types
 )
 
 add_header_library(
diff --git a/libc/src/complex/conjf128.h b/libc/src/complex/conjf128.h
index 587c979d315efb..c1ae0b03d067af 100644
--- a/libc/src/complex/conjf128.h
+++ b/libc/src/complex/conjf128.h
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/__support/macros/properties/complex_types.h"
-#include "src/__support/macros/properties/types.h"
 
 #if defined(LIBC_TYPES_HAS_CFLOAT128)
 
diff --git a/libc/src/complex/conjf16.h b/libc/src/complex/conjf16.h
index b15c5b3f61f4a0..685ac8ac5c8584 100644
--- a/libc/src/complex/conjf16.h
+++ b/libc/src/complex/conjf16.h
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/__support/macros/properties/complex_types.h"
-#include "src/__support/macros/properties/types.h"
 
 #if defined(LIBC_TYPES_HAS_CFLOAT16)
 
diff --git a/libc/src/complex/cprojf128.h b/libc/src/complex/cprojf128.h
index 8de090b2c49b34..5f7fe992ef30b6 100644
--- a/libc/src/complex/cprojf128.h
+++ b/libc/src/complex/cprojf128.h
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/__support/macros/properties/complex_types.h"
-#include "src/__support/macros/properties/types.h"
 
 #if defined(LIBC_TYPES_HAS_CFLOAT128)
 
diff --git a/libc/src/complex/cprojf16.h b/libc/src/complex/cprojf16.h
index e744011eb10c34..8cce5f0bcef2bc 100644
--- a/libc/src/complex/cprojf16.h
+++ b/libc/src/complex/cprojf16.h
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/__support/macros/properties/complex_types.h"
-#include "src/__support/macros/properties/types.h"
 
 #if defined(LIBC_TYPES_HAS_CFLOAT16)
 

>From f159f34cab6998e005a1c61147c07849dea48c09 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 17 Dec 2024 22:45:56 +0530
Subject: [PATCH 8/8] nit: refactor

---
 libc/src/__support/complex_type.h | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/libc/src/__support/complex_type.h b/libc/src/__support/complex_type.h
index 66d6a190a2396b..f96cb5506d1f99 100644
--- a/libc/src/__support/complex_type.h
+++ b/libc/src/__support/complex_type.h
@@ -80,12 +80,13 @@ template <typename T> LIBC_INLINE constexpr T conjugate(T c) {
 }
 
 template <typename T> LIBC_INLINE constexpr T project(T c) {
-  Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
-  if (fputil::FPBits<make_real_t<T>>(c_c.real).is_inf() ||
-      fputil::FPBits<make_real_t<T>>(c_c.imag).is_inf()) {
-    return cpp::bit_cast<T>(Complex<make_real_t<T>>{
-        (fputil::FPBits<make_real_t<T>>::inf(Sign::POS).get_val()),
-        static_cast<make_real_t<T>>(c_c.imag > 0 ? 0.0 : -0.0)});
+  using real_t = make_real_t<T>;
+  Complex<real_t> c_c = cpp::bit_cast<Complex<real_t>>(c);
+  if (fputil::FPBits<real_t>(c_c.real).is_inf() ||
+      fputil::FPBits<real_t>(c_c.imag).is_inf()) {
+    return cpp::bit_cast<T>(Complex<real_t>{
+        (fputil::FPBits<real_t>::inf(Sign::POS).get_val()),
+        static_cast<real_t>(c_c.imag > 0 ? 0.0 : -0.0)});
   } else {
     return c;
   }



More information about the libc-commits mailing list