Skip to content

[libc][complex] Implement different flavors of the cproj function #119722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 17, 2024

Conversation

Sh0g0-1758
Copy link
Member

Refer section 7.3.9.5 of ISO/IEC 9899:2023

@llvmbot llvmbot added the libc label Dec 12, 2024
@Sh0g0-1758 Sh0g0-1758 requested a review from lntue December 12, 2024 17:04
@llvmbot
Copy link
Member

llvmbot commented Dec 12, 2024

@llvm/pr-subscribers-libc

Author: Shourya Goel (Sh0g0-1758)

Changes

Refer section 7.3.9.5 of ISO/IEC 9899:2023


Patch is 30.98 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/119722.diff

25 Files Affected:

  • (modified) libc/config/linux/aarch64/entrypoints.txt (+5)
  • (modified) libc/config/linux/arm/entrypoints.txt (+3)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+4)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+5)
  • (modified) libc/docs/headers/complex.rst (+1-1)
  • (modified) libc/hdrgen/yaml/complex.yaml (+32)
  • (modified) libc/src/complex/CMakeLists.txt (+6)
  • (added) libc/src/complex/cproj.h (+20)
  • (added) libc/src/complex/cprojf.h (+20)
  • (added) libc/src/complex/cprojf128.h (+27)
  • (added) libc/src/complex/cprojf16.h (+27)
  • (added) libc/src/complex/cprojl.h (+20)
  • (modified) libc/src/complex/generic/CMakeLists.txt (+74)
  • (added) libc/src/complex/generic/cproj.cpp (+29)
  • (added) libc/src/complex/generic/cprojf.cpp (+29)
  • (added) libc/src/complex/generic/cprojf128.cpp (+33)
  • (added) libc/src/complex/generic/cprojf16.cpp (+33)
  • (added) libc/src/complex/generic/cprojl.cpp (+29)
  • (modified) libc/test/src/complex/CMakeLists.txt (+60)
  • (added) libc/test/src/complex/CprojTest.h (+131)
  • (added) libc/test/src/complex/cproj_test.cpp (+13)
  • (added) libc/test/src/complex/cprojf128_test.cpp (+17)
  • (added) libc/test/src/complex/cprojf16_test.cpp (+17)
  • (added) libc/test/src/complex/cprojf_test.cpp (+13)
  • (added) libc/test/src/complex/cprojl_test.cpp (+13)
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..d8175a84b612c4
--- /dev/null
+++ b/libc/src/complex/generic/cproj.cpp
@@ -0,0 +1,29 @@
+//===-- 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/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;
+  }
+}
+
+} // 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..d733731959324c
--- /dev/null
+++ b/libc/src/complex/generic/cprojf.cpp
@@ -0,0 +1,29 @@
+//===-- 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/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;
+  }
+}
+
+} // 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..57b76da7abc354
--- /dev/null
+++ b/libc/src/complex/generic/cprojf128.cpp
@@ -0,0 +1,33 @@
+//===-- 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/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;
+  }
+}
+
+} // 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..4cabd65c768d63
--- /dev/null
+++ b/libc/src/complex/generic/cprojf16.cpp
@@ -0,0 +1,33 @@
+//===-- 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/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;
+  }
+}
+
+} // 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..57d2516b23b600
--- /dev/null
+++ b/libc/src/complex/generic/cprojl.cpp
@@ -0,0 +1,29 @@
+//===-- 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/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;
+  }
+}
+
+} // 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_tes...
[truncated]

@Sh0g0-1758 Sh0g0-1758 self-assigned this Dec 12, 2024
@Sh0g0-1758 Sh0g0-1758 requested review from SchrodingerZhu, overmighty and nickdesaulniers and removed request for overmighty December 12, 2024 17:15
@Sh0g0-1758 Sh0g0-1758 merged commit c98e79d into llvm:main Dec 17, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants