[libc-commits] [libc] [libc][complex] implement different flavors of `creal` and `cimag` functions (PR #113300)

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Thu Oct 31 04:30:48 PDT 2024


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

>From 566fa0fca6be71ff4ee4bd7b6e6dc2eacbbac9d6 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 22 Oct 2024 17:01:47 +0530
Subject: [PATCH 1/7] Implemented variants of creal

---
 libc/config/linux/aarch64/entrypoints.txt | 11 ++++
 libc/config/linux/arm/entrypoints.txt     |  5 ++
 libc/config/linux/riscv/entrypoints.txt   |  8 +++
 libc/config/linux/x86_64/entrypoints.txt  | 11 ++++
 libc/src/CMakeLists.txt                   |  1 +
 libc/src/complex/CMakeLists.txt           | 20 +++++++
 libc/src/complex/creal.h                  | 20 +++++++
 libc/src/complex/crealf.h                 | 20 +++++++
 libc/src/complex/crealf128.h              | 27 +++++++++
 libc/src/complex/crealf16.h               | 27 +++++++++
 libc/src/complex/creall.h                 | 20 +++++++
 libc/src/complex/generic/CMakeLists.txt   | 49 ++++++++++++++++
 libc/src/complex/generic/creal.cpp        | 21 +++++++
 libc/src/complex/generic/crealf.cpp       | 21 +++++++
 libc/src/complex/generic/crealf128.cpp    | 25 ++++++++
 libc/src/complex/generic/crealf16.cpp     | 25 ++++++++
 libc/src/complex/generic/creall.cpp       | 21 +++++++
 libc/test/src/CMakeLists.txt              |  1 +
 libc/test/src/complex/CMakeLists.txt      | 61 +++++++++++++++++++
 libc/test/src/complex/CRealTest.h         | 71 +++++++++++++++++++++++
 libc/test/src/complex/creal_test.cpp      | 13 +++++
 libc/test/src/complex/crealf128_test.cpp  | 17 ++++++
 libc/test/src/complex/crealf16_test.cpp   | 17 ++++++
 libc/test/src/complex/crealf_test.cpp     | 13 +++++
 libc/test/src/complex/creall_test.cpp     | 13 +++++
 25 files changed, 538 insertions(+)
 create mode 100644 libc/src/complex/CMakeLists.txt
 create mode 100644 libc/src/complex/creal.h
 create mode 100644 libc/src/complex/crealf.h
 create mode 100644 libc/src/complex/crealf128.h
 create mode 100644 libc/src/complex/crealf16.h
 create mode 100644 libc/src/complex/creall.h
 create mode 100644 libc/src/complex/generic/CMakeLists.txt
 create mode 100644 libc/src/complex/generic/creal.cpp
 create mode 100644 libc/src/complex/generic/crealf.cpp
 create mode 100644 libc/src/complex/generic/crealf128.cpp
 create mode 100644 libc/src/complex/generic/crealf16.cpp
 create mode 100644 libc/src/complex/generic/creall.cpp
 create mode 100644 libc/test/src/complex/CMakeLists.txt
 create mode 100644 libc/test/src/complex/CRealTest.h
 create mode 100644 libc/test/src/complex/creal_test.cpp
 create mode 100644 libc/test/src/complex/crealf128_test.cpp
 create mode 100644 libc/test/src/complex/crealf16_test.cpp
 create mode 100644 libc/test/src/complex/crealf_test.cpp
 create mode 100644 libc/test/src/complex/creall_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 71c6e874429fed..8fb60d252a99b3 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -356,6 +356,11 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
 endif()
 
 set(TARGET_LIBM_ENTRYPOINTS
+    # complex.h entrypoints
+    libc.src.complex.creal
+    libc.src.complex.crealf
+    libc.src.complex.creall
+
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
     libc.src.fenv.fedisableexcept
@@ -603,6 +608,9 @@ set(TARGET_LIBM_ENTRYPOINTS
 
 if(LIBC_TYPES_HAS_FLOAT16)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
+    # complex.h C23 _Complex _Float16 entrypoints
+    libc.src.complex.crealf16
+    
     # math.h C23 _Float16 entrypoints
     libc.src.math.canonicalizef16
     libc.src.math.ceilf16
@@ -703,6 +711,9 @@ endif()
 
 if(LIBC_TYPES_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
+    # complex.h C23 _Complex _Float128 entrypoints
+    libc.src.complex.crealf128
+
     # math.h C23 _Float128 entrypoints
     libc.src.math.canonicalizef128
     libc.src.math.ceilf128
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 2ddb7aeefe48ec..9f98a89ee85b1a 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -197,6 +197,11 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
 endif()
 
 set(TARGET_LIBM_ENTRYPOINTS
+    # complex.h entrypoints
+    libc.src.complex.creal
+    libc.src.complex.crealf
+    libc.src.complex.creall
+    
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
     libc.src.fenv.fedisableexcept
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5c09edf7cfb266..ad444ad54a028e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -355,6 +355,11 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
 endif()
 
 set(TARGET_LIBM_ENTRYPOINTS
+    # complex.h entrypoints
+    libc.src.complex.creal
+    libc.src.complex.crealf
+    libc.src.complex.creall
+
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
     libc.src.fenv.fedisableexcept
@@ -606,6 +611,9 @@ set(TARGET_LIBM_ENTRYPOINTS
 
 if(LIBC_TYPES_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
+    # complex.h C23 _Complex _Float128 entrypoints
+    libc.src.complex.crealf128
+    
     # math.h C23 _Float128 entrypoints
     libc.src.math.canonicalizef128
     libc.src.math.ceilf128
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9bc63edf06f28c..536d9d328db7e1 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -355,6 +355,11 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
 endif()
 
 set(TARGET_LIBM_ENTRYPOINTS
+    # complex.h entrypoints
+    libc.src.complex.creal
+    libc.src.complex.crealf
+    libc.src.complex.creall
+    
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
     libc.src.fenv.fedisableexcept
@@ -606,6 +611,9 @@ set(TARGET_LIBM_ENTRYPOINTS
 
 if(LIBC_TYPES_HAS_FLOAT16)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
+    # complex.h C23 _Complex _Float16 entrypoints
+    libc.src.complex.crealf16
+
     # math.h C23 _Float16 entrypoints
     libc.src.math.canonicalizef16
     libc.src.math.ceilf16
@@ -708,6 +716,9 @@ endif()
 
 if(LIBC_TYPES_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
+    # complex.h C23 _Complex _Float128 entrypoints
+    libc.src.complex.crealf128
+    
     # math.h C23 _Float128 entrypoints
     libc.src.math.canonicalizef128
     libc.src.math.ceilf128
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index d554c12fb1ec89..02c193e635362e 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -1,5 +1,6 @@
 add_subdirectory(__support)
 
+add_subdirectory(complex)
 add_subdirectory(ctype)
 add_subdirectory(dlfcn)
 add_subdirectory(errno)
diff --git a/libc/src/complex/CMakeLists.txt b/libc/src/complex/CMakeLists.txt
new file mode 100644
index 00000000000000..92851b84b38eb6
--- /dev/null
+++ b/libc/src/complex/CMakeLists.txt
@@ -0,0 +1,20 @@
+add_subdirectory(generic)
+
+function(add_complex_entrypoint_object name)
+  get_fq_target_name("generic.${name}" fq_generic_target_name)
+  if(TARGET ${fq_generic_target_name})
+    add_entrypoint_object(
+      ${name}
+      ALIAS
+      DEPENDS
+        .generic.${name}
+    )
+    return()
+  endif()
+endfunction()
+
+add_complex_entrypoint_object(creal)
+add_complex_entrypoint_object(crealf)
+add_complex_entrypoint_object(creall)
+add_complex_entrypoint_object(crealf16)
+add_complex_entrypoint_object(crealf128)
diff --git a/libc/src/complex/creal.h b/libc/src/complex/creal.h
new file mode 100644
index 00000000000000..3ec5dd049b975d
--- /dev/null
+++ b/libc/src/complex/creal.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for creal -------------------------*- 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_CREAL_H
+#define LLVM_LIBC_SRC_COMPLEX_CREAL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+double creal(_Complex double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CREAL_H
diff --git a/libc/src/complex/crealf.h b/libc/src/complex/crealf.h
new file mode 100644
index 00000000000000..79dafc1c3758d5
--- /dev/null
+++ b/libc/src/complex/crealf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for crealf ------------------------*- 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_CREALF_H
+#define LLVM_LIBC_SRC_COMPLEX_CREALF_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float crealf(_Complex float x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CREALF_H
diff --git a/libc/src/complex/crealf128.h b/libc/src/complex/crealf128.h
new file mode 100644
index 00000000000000..4922ae78cb2386
--- /dev/null
+++ b/libc/src/complex/crealf128.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for crealf128 ---------------------*- 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_CREALF128_H
+#define LLVM_LIBC_SRC_COMPLEX_CREALF128_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float128 crealf128(cfloat128 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CREALF128_H
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/crealf16.h b/libc/src/complex/crealf16.h
new file mode 100644
index 00000000000000..e6098a218d092d
--- /dev/null
+++ b/libc/src/complex/crealf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for crealf16 ----------------------*- 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_CREALF16_H
+#define LLVM_LIBC_SRC_COMPLEX_CREALF16_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 crealf16(cfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CREALF16_H
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/creall.h b/libc/src/complex/creall.h
new file mode 100644
index 00000000000000..2a8245f63d6710
--- /dev/null
+++ b/libc/src/complex/creall.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for creall ------------------------*- 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_CREALL_H
+#define LLVM_LIBC_SRC_COMPLEX_CREALL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+long double creall(_Complex long double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CREALL_H
diff --git a/libc/src/complex/generic/CMakeLists.txt b/libc/src/complex/generic/CMakeLists.txt
new file mode 100644
index 00000000000000..f7b69e245cc205
--- /dev/null
+++ b/libc/src/complex/generic/CMakeLists.txt
@@ -0,0 +1,49 @@
+add_entrypoint_object(
+  creal
+  SRCS
+    creal.cpp
+  HDRS
+    ../creal.h
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  crealf
+  SRCS
+    crealf.cpp
+  HDRS
+    ../crealf.h
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  creall
+  SRCS
+    creall.cpp
+  HDRS
+    ../creall.h
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  crealf16
+  SRCS
+    crealf16.cpp
+  HDRS
+    ../crealf16.h
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  crealf128
+  SRCS
+    crealf128.cpp
+  HDRS
+    ../crealf128.h
+  COMPILE_OPTIONS
+    -O3
+)
diff --git a/libc/src/complex/generic/creal.cpp b/libc/src/complex/generic/creal.cpp
new file mode 100644
index 00000000000000..a48d9033b68a1a
--- /dev/null
+++ b/libc/src/complex/generic/creal.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of creal 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/creal.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, creal, (_Complex double x)) {
+    double *xCmplxPtr = reinterpret_cast<double *>(&x);
+    return xCmplxPtr[0];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/crealf.cpp b/libc/src/complex/generic/crealf.cpp
new file mode 100644
index 00000000000000..a8b276a2f18fac
--- /dev/null
+++ b/libc/src/complex/generic/crealf.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of crealf 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/crealf.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, crealf, (_Complex float x)) {
+    float *xCmplxPtr = reinterpret_cast<float *>(&x);
+    return xCmplxPtr[0];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/crealf128.cpp b/libc/src/complex/generic/crealf128.cpp
new file mode 100644
index 00000000000000..c9f1e6ea3ea996
--- /dev/null
+++ b/libc/src/complex/generic/crealf128.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of crealf128 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/crealf128.h"
+#if defined(LIBC_TYPES_HAS_CFLOAT128)
+
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float128, crealf128, (cfloat128 x)) {
+    float128 *xCmplxPtr = reinterpret_cast<float128 *>(&x);
+    return xCmplxPtr[0];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/generic/crealf16.cpp b/libc/src/complex/generic/crealf16.cpp
new file mode 100644
index 00000000000000..d01ee65c9e4146
--- /dev/null
+++ b/libc/src/complex/generic/crealf16.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of crealf16 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/crealf16.h"
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, crealf16, (cfloat16 x)) {
+    float16 *xCmplxPtr = reinterpret_cast<float16 *>(&x);
+    return xCmplxPtr[0];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/generic/creall.cpp b/libc/src/complex/generic/creall.cpp
new file mode 100644
index 00000000000000..b140fbdf40cfb3
--- /dev/null
+++ b/libc/src/complex/generic/creall.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of creall 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/creall.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(long double, creall, (_Complex long double x)) {
+    long double *xCmplxPtr = reinterpret_cast<long double *>(&x);
+    return xCmplxPtr[0];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index ddc6a5c7f6965f..e121555bd60a9f 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -47,6 +47,7 @@ function(add_fp_unittest name)
 endfunction(add_fp_unittest)
 
 add_subdirectory(__support)
+add_subdirectory(complex)
 add_subdirectory(ctype)
 add_subdirectory(errno)
 add_subdirectory(fenv)
diff --git a/libc/test/src/complex/CMakeLists.txt b/libc/test/src/complex/CMakeLists.txt
new file mode 100644
index 00000000000000..51d0d2a4278b42
--- /dev/null
+++ b/libc/test/src/complex/CMakeLists.txt
@@ -0,0 +1,61 @@
+add_custom_target(libc-complex-unittests)
+
+add_libc_test(
+  creal_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    creal_test.cpp
+  DEPENDS
+    libc.src.complex.creal
+  LINK_LIBRARIES
+      LibcFPTestHelpers
+)
+
+add_libc_test(
+  crealf_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    crealf_test.cpp
+  DEPENDS
+    libc.src.complex.crealf
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  creall_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    creall_test.cpp
+  DEPENDS
+    libc.src.complex.creall
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  crealf16_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    crealf16_test.cpp
+  DEPENDS
+    libc.src.complex.crealf16
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  crealf128_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    crealf128_test.cpp
+  DEPENDS
+    libc.src.complex.crealf128
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
diff --git a/libc/test/src/complex/CRealTest.h b/libc/test/src/complex/CRealTest.h
new file mode 100644
index 00000000000000..684e7b580e6576
--- /dev/null
+++ b/libc/test/src/complex/CRealTest.h
@@ -0,0 +1,71 @@
+//===-- Utility class to test different flavors of creal --------*- 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_CREALTEST_H
+#define LLVM_LIBC_TEST_SRC_COMPLEX_CREALTEST_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 CRealTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+  DECLARE_SPECIAL_CONSTANTS(FPT)
+
+public:
+  typedef FPT (*CRealFunc)(CFPT);
+
+  void testSpecialNumbers(CRealFunc func) {
+    EXPECT_FP_EQ(func(CFPT(aNaN + 67.123i)), aNaN);
+    EXPECT_FP_EQ(func(CFPT(neg_aNaN + 78.319i)), neg_aNaN);
+    EXPECT_FP_EQ(func(CFPT(sNaN + 7813.131i)), sNaN);
+    EXPECT_FP_EQ(func(CFPT(neg_sNaN + 7824.152i)), neg_sNaN);
+    EXPECT_FP_EQ(func(CFPT(inf + 9024.2442i)), inf);
+    EXPECT_FP_EQ(func(CFPT(neg_inf + 8923.124i)), neg_inf);
+    EXPECT_FP_EQ(func(CFPT(min_normal + 782.124i)), min_normal);
+    EXPECT_FP_EQ(func(CFPT(max_normal + 2141.2352i)), max_normal);
+    EXPECT_FP_EQ(func(CFPT(neg_max_normal + 341.134i)), neg_max_normal);
+    EXPECT_FP_EQ(func(CFPT(min_denormal + 781.142i)), min_denormal);
+    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 + 12341.1321i)), neg_zero);
+  }
+
+  void testRoundedNumbers(CRealFunc func) {
+    EXPECT_FP_EQ(func((CFPT)(4523.1413 + 12413.1414i)), (FPT)(4523.1413));
+    EXPECT_FP_EQ(func((CFPT)(-4523.1413 + 12413.1414i)), (FPT)(-4523.1413));
+    EXPECT_FP_EQ(func((CFPT)(4523.1413 - 12413.1414i)), (FPT)(4523.1413));
+    EXPECT_FP_EQ(func((CFPT)(-4523.1413 - 12413.1414i)), (FPT)(-4523.1413));
+
+    EXPECT_FP_EQ(func((CFPT)(3210.5678 + 9876.5432i)), (FPT)(3210.5678));
+    EXPECT_FP_EQ(func((CFPT)(-3210.5678 + 9876.5432i)), (FPT)(-3210.5678));
+    EXPECT_FP_EQ(func((CFPT)(3210.5678 - 9876.5432i)), (FPT)(3210.5678));
+    EXPECT_FP_EQ(func((CFPT)(-3210.5678 - 9876.5432i)), (FPT)(-3210.5678));
+
+    EXPECT_FP_EQ(func((CFPT)(1234.4321 + 4321.1234i)), (FPT)(1234.4321));
+    EXPECT_FP_EQ(func((CFPT)(-1234.4321 + 4321.1234i)), (FPT)(-1234.4321));
+    EXPECT_FP_EQ(func((CFPT)(1234.4321 - 4321.1234i)), (FPT)(1234.4321));
+    EXPECT_FP_EQ(func((CFPT)(-1234.4321 - 4321.1234i)), (FPT)(-1234.4321));
+
+    EXPECT_FP_EQ(func((CFPT)(6789.1234 + 8765.6789i)), (FPT)(6789.1234));
+    EXPECT_FP_EQ(func((CFPT)(-6789.1234 + 8765.6789i)), (FPT)(-6789.1234));
+    EXPECT_FP_EQ(func((CFPT)(6789.1234 - 8765.6789i)), (FPT)(6789.1234));
+    EXPECT_FP_EQ(func((CFPT)(-6789.1234 - 8765.6789i)), (FPT)(-6789.1234));
+  }
+};
+
+#define LIST_CREAL_TESTS(U, T, func)                                           \
+  using LlvmLibcCRealTest = CRealTest<U, T>;                                   \
+  TEST_F(LlvmLibcCRealTest, SpecialNumbers) { testSpecialNumbers(&func); }     \
+  TEST_F(LlvmLibcCRealTest, RoundedNumbers) { testRoundedNumbers(&func); }
+
+#endif // LLVM_LIBC_TEST_SRC_COMPLEX_CREALTEST_H
diff --git a/libc/test/src/complex/creal_test.cpp b/libc/test/src/complex/creal_test.cpp
new file mode 100644
index 00000000000000..14dc550932d7d5
--- /dev/null
+++ b/libc/test/src/complex/creal_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for creal -----------------------------------------------===//
+//
+// 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 "CRealTest.h"
+
+#include "src/complex/creal.h"
+
+LIST_CREAL_TESTS(_Complex double, double, LIBC_NAMESPACE::creal)
diff --git a/libc/test/src/complex/crealf128_test.cpp b/libc/test/src/complex/crealf128_test.cpp
new file mode 100644
index 00000000000000..7626eeebca2783
--- /dev/null
+++ b/libc/test/src/complex/crealf128_test.cpp
@@ -0,0 +1,17 @@
+//===-- Unittests for crealf128 -------------------------------------------===//
+//
+// 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 "CRealTest.h"
+
+#include "src/complex/crealf128.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT128)
+
+LIST_CREAL_TESTS(cfloat128, float128, LIBC_NAMESPACE::crealf128)
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/test/src/complex/crealf16_test.cpp b/libc/test/src/complex/crealf16_test.cpp
new file mode 100644
index 00000000000000..97346aad615f7e
--- /dev/null
+++ b/libc/test/src/complex/crealf16_test.cpp
@@ -0,0 +1,17 @@
+//===-- Unittests for crealf16 --------------------------------------------===//
+//
+// 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 "CRealTest.h"
+
+#include "src/complex/crealf16.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+
+LIST_CREAL_TESTS(cfloat16, float16, LIBC_NAMESPACE::crealf16)
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/test/src/complex/crealf_test.cpp b/libc/test/src/complex/crealf_test.cpp
new file mode 100644
index 00000000000000..aa54f54793e2a6
--- /dev/null
+++ b/libc/test/src/complex/crealf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for crealf ----------------------------------------------===//
+//
+// 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 "CRealTest.h"
+
+#include "src/complex/crealf.h"
+
+LIST_CREAL_TESTS(_Complex float, float, LIBC_NAMESPACE::crealf)
diff --git a/libc/test/src/complex/creall_test.cpp b/libc/test/src/complex/creall_test.cpp
new file mode 100644
index 00000000000000..6985dbc2590a77
--- /dev/null
+++ b/libc/test/src/complex/creall_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for creall ----------------------------------------------===//
+//
+// 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 "CRealTest.h"
+
+#include "src/complex/creall.h"
+
+LIST_CREAL_TESTS(_Complex long double, long double, LIBC_NAMESPACE::creall)

>From 5be226fca89db62f5aa9feed7d62f9bc4b37be05 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 22 Oct 2024 17:02:27 +0530
Subject: [PATCH 2/7] ran fmt

---
 libc/src/complex/generic/creal.cpp     | 4 ++--
 libc/src/complex/generic/crealf.cpp    | 4 ++--
 libc/src/complex/generic/crealf128.cpp | 4 ++--
 libc/src/complex/generic/crealf16.cpp  | 4 ++--
 libc/src/complex/generic/creall.cpp    | 4 ++--
 5 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/libc/src/complex/generic/creal.cpp b/libc/src/complex/generic/creal.cpp
index a48d9033b68a1a..fbac499e939374 100644
--- a/libc/src/complex/generic/creal.cpp
+++ b/libc/src/complex/generic/creal.cpp
@@ -14,8 +14,8 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, creal, (_Complex double x)) {
-    double *xCmplxPtr = reinterpret_cast<double *>(&x);
-    return xCmplxPtr[0];
+  double *xCmplxPtr = reinterpret_cast<double *>(&x);
+  return xCmplxPtr[0];
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/crealf.cpp b/libc/src/complex/generic/crealf.cpp
index a8b276a2f18fac..f613e7fd566bce 100644
--- a/libc/src/complex/generic/crealf.cpp
+++ b/libc/src/complex/generic/crealf.cpp
@@ -14,8 +14,8 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, crealf, (_Complex float x)) {
-    float *xCmplxPtr = reinterpret_cast<float *>(&x);
-    return xCmplxPtr[0];
+  float *xCmplxPtr = reinterpret_cast<float *>(&x);
+  return xCmplxPtr[0];
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/crealf128.cpp b/libc/src/complex/generic/crealf128.cpp
index c9f1e6ea3ea996..df29102d259851 100644
--- a/libc/src/complex/generic/crealf128.cpp
+++ b/libc/src/complex/generic/crealf128.cpp
@@ -16,8 +16,8 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float128, crealf128, (cfloat128 x)) {
-    float128 *xCmplxPtr = reinterpret_cast<float128 *>(&x);
-    return xCmplxPtr[0];
+  float128 *xCmplxPtr = reinterpret_cast<float128 *>(&x);
+  return xCmplxPtr[0];
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/crealf16.cpp b/libc/src/complex/generic/crealf16.cpp
index d01ee65c9e4146..fcb6181c988d74 100644
--- a/libc/src/complex/generic/crealf16.cpp
+++ b/libc/src/complex/generic/crealf16.cpp
@@ -16,8 +16,8 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, crealf16, (cfloat16 x)) {
-    float16 *xCmplxPtr = reinterpret_cast<float16 *>(&x);
-    return xCmplxPtr[0];
+  float16 *xCmplxPtr = reinterpret_cast<float16 *>(&x);
+  return xCmplxPtr[0];
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/creall.cpp b/libc/src/complex/generic/creall.cpp
index b140fbdf40cfb3..e961743ba87852 100644
--- a/libc/src/complex/generic/creall.cpp
+++ b/libc/src/complex/generic/creall.cpp
@@ -14,8 +14,8 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, creall, (_Complex long double x)) {
-    long double *xCmplxPtr = reinterpret_cast<long double *>(&x);
-    return xCmplxPtr[0];
+  long double *xCmplxPtr = reinterpret_cast<long double *>(&x);
+  return xCmplxPtr[0];
 }
 
 } // namespace LIBC_NAMESPACE_DECL

>From 3914ac3ce3776bc9fa2370c3d208a5ce20b9a525 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 22 Oct 2024 21:05:42 +0530
Subject: [PATCH 3/7] update test for neg_zero

---
 libc/test/src/complex/CRealTest.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/complex/CRealTest.h b/libc/test/src/complex/CRealTest.h
index 684e7b580e6576..204a8fc529c100 100644
--- a/libc/test/src/complex/CRealTest.h
+++ b/libc/test/src/complex/CRealTest.h
@@ -37,7 +37,8 @@ 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 + 12341.1321i)), neg_zero);
+    EXPECT_FP_EQ(func(CFPT(neg_zero - zero * 1.0i)), neg_zero);
+    EXPECT_FP_EQ(func(CFPT(neg_zero + zero * 1.0i)), zero);
   }
 
   void testRoundedNumbers(CRealFunc func) {

>From 0bcfd82cf1b4d6835cfbc7ef88e7813a771af8e6 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 25 Oct 2024 16:06:20 +0530
Subject: [PATCH 4/7] implement cimag

---
 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/complex.rst                     |  4 +-
 libc/src/complex/CMakeLists.txt           |  6 ++
 libc/src/complex/cimag.h                  | 20 +++++++
 libc/src/complex/cimagf.h                 | 20 +++++++
 libc/src/complex/cimagf128.h              | 27 +++++++++
 libc/src/complex/cimagf16.h               | 27 +++++++++
 libc/src/complex/cimagl.h                 | 20 +++++++
 libc/src/complex/generic/CMakeLists.txt   | 50 ++++++++++++++++
 libc/src/complex/generic/cimag.cpp        | 21 +++++++
 libc/src/complex/generic/cimagf.cpp       | 21 +++++++
 libc/src/complex/generic/cimagf128.cpp    | 25 ++++++++
 libc/src/complex/generic/cimagf16.cpp     | 25 ++++++++
 libc/src/complex/generic/cimagl.cpp       | 21 +++++++
 libc/test/src/complex/CImagTest.h         | 73 +++++++++++++++++++++++
 libc/test/src/complex/CMakeLists.txt      | 60 +++++++++++++++++++
 libc/test/src/complex/CRealTest.h         |  2 +-
 libc/test/src/complex/cimag_test.cpp      | 13 ++++
 libc/test/src/complex/cimagf128_test.cpp  | 17 ++++++
 libc/test/src/complex/cimagf16_test.cpp   | 17 ++++++
 libc/test/src/complex/cimagf_test.cpp     | 13 ++++
 libc/test/src/complex/cimagl_test.cpp     | 13 ++++
 25 files changed, 509 insertions(+), 3 deletions(-)
 create mode 100644 libc/src/complex/cimag.h
 create mode 100644 libc/src/complex/cimagf.h
 create mode 100644 libc/src/complex/cimagf128.h
 create mode 100644 libc/src/complex/cimagf16.h
 create mode 100644 libc/src/complex/cimagl.h
 create mode 100644 libc/src/complex/generic/cimag.cpp
 create mode 100644 libc/src/complex/generic/cimagf.cpp
 create mode 100644 libc/src/complex/generic/cimagf128.cpp
 create mode 100644 libc/src/complex/generic/cimagf16.cpp
 create mode 100644 libc/src/complex/generic/cimagl.cpp
 create mode 100644 libc/test/src/complex/CImagTest.h
 create mode 100644 libc/test/src/complex/cimag_test.cpp
 create mode 100644 libc/test/src/complex/cimagf128_test.cpp
 create mode 100644 libc/test/src/complex/cimagf16_test.cpp
 create mode 100644 libc/test/src/complex/cimagf_test.cpp
 create mode 100644 libc/test/src/complex/cimagl_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8fb60d252a99b3..4e83995b24e665 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -360,6 +360,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.creal
     libc.src.complex.crealf
     libc.src.complex.creall
+    libc.src.complex.cimag
+    libc.src.complex.cimagf
+    libc.src.complex.cimagl
 
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
@@ -610,6 +613,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # complex.h C23 _Complex _Float16 entrypoints
     libc.src.complex.crealf16
+    libc.src.complex.cimagf16
     
     # math.h C23 _Float16 entrypoints
     libc.src.math.canonicalizef16
@@ -713,6 +717,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # complex.h C23 _Complex _Float128 entrypoints
     libc.src.complex.crealf128
+    libc.src.complex.cimagf128
 
     # 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 9f98a89ee85b1a..383bf8c8e52801 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -201,6 +201,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.creal
     libc.src.complex.crealf
     libc.src.complex.creall
+    libc.src.complex.cimag
+    libc.src.complex.cimagf
+    libc.src.complex.cimagl
     
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index ad444ad54a028e..c47a4662d1ac79 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -359,6 +359,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.creal
     libc.src.complex.crealf
     libc.src.complex.creall
+    libc.src.complex.cimag
+    libc.src.complex.cimagf
+    libc.src.complex.cimagl
 
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
@@ -613,6 +616,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # complex.h C23 _Complex _Float128 entrypoints
     libc.src.complex.crealf128
+    libc.src.complex.cimagf128
     
     # 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 536d9d328db7e1..140c9598a00c7d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -359,6 +359,9 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.complex.creal
     libc.src.complex.crealf
     libc.src.complex.creall
+    libc.src.complex.cimag
+    libc.src.complex.cimagf
+    libc.src.complex.cimagl
     
     # fenv.h entrypoints
     libc.src.fenv.feclearexcept
@@ -613,6 +616,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # complex.h C23 _Complex _Float16 entrypoints
     libc.src.complex.crealf16
+    libc.src.complex.cimagf16
 
     # math.h C23 _Float16 entrypoints
     libc.src.math.canonicalizef16
@@ -718,6 +722,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # complex.h C23 _Complex _Float128 entrypoints
     libc.src.complex.crealf128
+    libc.src.complex.cimagf128
     
     # math.h C23 _Float128 entrypoints
     libc.src.math.canonicalizef128
diff --git a/libc/docs/complex.rst b/libc/docs/complex.rst
index 09fbdd7c6179ef..1e74f16b60e660 100644
--- a/libc/docs/complex.rst
+++ b/libc/docs/complex.rst
@@ -55,11 +55,11 @@ Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | carg      |                  |                 |                        |                      |                        | 7.3.9.1                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| cimag     |                  |                 |                        |                      |                        | 7.3.9.2                | N/A                        |
+| cimag     | |check|          | |check|         | |check|                | |check|              | |check|                | 7.3.9.2                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | conj      |                  |                 |                        |                      |                        | 7.3.9.4                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | cproj     |                  |                 |                        |                      |                        | 7.3.9.5                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| creal     |                  |                 |                        |                      |                        | 7.3.9.6                | N/A                        |
+| creal     | |check|          | |check|         | |check|                | |check|              | |check|                | 7.3.9.6                | N/A                        |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/src/complex/CMakeLists.txt b/libc/src/complex/CMakeLists.txt
index 92851b84b38eb6..289cce5455af78 100644
--- a/libc/src/complex/CMakeLists.txt
+++ b/libc/src/complex/CMakeLists.txt
@@ -18,3 +18,9 @@ add_complex_entrypoint_object(crealf)
 add_complex_entrypoint_object(creall)
 add_complex_entrypoint_object(crealf16)
 add_complex_entrypoint_object(crealf128)
+
+add_complex_entrypoint_object(cimag)
+add_complex_entrypoint_object(cimagf)
+add_complex_entrypoint_object(cimagl)
+add_complex_entrypoint_object(cimagf16)
+add_complex_entrypoint_object(cimagf128)
diff --git a/libc/src/complex/cimag.h b/libc/src/complex/cimag.h
new file mode 100644
index 00000000000000..29c9b22a78b053
--- /dev/null
+++ b/libc/src/complex/cimag.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cimag -------------------------*- 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_CIMAG_H
+#define LLVM_LIBC_SRC_COMPLEX_CIMAG_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+double cimag(_Complex double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CIMAG_H
diff --git a/libc/src/complex/cimagf.h b/libc/src/complex/cimagf.h
new file mode 100644
index 00000000000000..577b6ca1145595
--- /dev/null
+++ b/libc/src/complex/cimagf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cimagf ------------------------*- 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_CIMAGF_H
+#define LLVM_LIBC_SRC_COMPLEX_CIMAGF_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float cimagf(_Complex float x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CIMAGF_H
diff --git a/libc/src/complex/cimagf128.h b/libc/src/complex/cimagf128.h
new file mode 100644
index 00000000000000..ab8f9ac7da58c5
--- /dev/null
+++ b/libc/src/complex/cimagf128.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for cimagf128 ---------------------*- 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_CIMAGF128_H
+#define LLVM_LIBC_SRC_COMPLEX_CIMAGF128_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float128 cimagf128(cfloat128 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CIMAGF128_H
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/cimagf16.h b/libc/src/complex/cimagf16.h
new file mode 100644
index 00000000000000..5c5de2eb1bcf2e
--- /dev/null
+++ b/libc/src/complex/cimagf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for cimagf16 ----------------------*- 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_CIMAGF16_H
+#define LLVM_LIBC_SRC_COMPLEX_CIMAGF16_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 cimagf16(cfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CIMAGF16_H
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/cimagl.h b/libc/src/complex/cimagl.h
new file mode 100644
index 00000000000000..966aee0ef3f74f
--- /dev/null
+++ b/libc/src/complex/cimagl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cimagl ------------------------*- 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_CIMAGL_H
+#define LLVM_LIBC_SRC_COMPLEX_CIMAGL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+long double cimagl(_Complex long double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CIMAGL_H
diff --git a/libc/src/complex/generic/CMakeLists.txt b/libc/src/complex/generic/CMakeLists.txt
index f7b69e245cc205..0eace99f5e82e5 100644
--- a/libc/src/complex/generic/CMakeLists.txt
+++ b/libc/src/complex/generic/CMakeLists.txt
@@ -47,3 +47,53 @@ add_entrypoint_object(
   COMPILE_OPTIONS
     -O3
 )
+
+add_entrypoint_object(
+  cimag
+  SRCS
+    cimag.cpp
+  HDRS
+    ../cimag.h
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  cimagf
+  SRCS
+    cimagf.cpp
+  HDRS
+    ../cimagf.h
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  cimagl
+  SRCS
+    cimagl.cpp
+  HDRS
+    ../cimagl.h
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  cimagf16
+  SRCS
+    cimagf16.cpp
+  HDRS
+    ../cimagf16.h
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  cimagf128
+  SRCS
+    cimagf128.cpp
+  HDRS
+    ../cimagf128.h
+  COMPILE_OPTIONS
+    -O3
+)
diff --git a/libc/src/complex/generic/cimag.cpp b/libc/src/complex/generic/cimag.cpp
new file mode 100644
index 00000000000000..e2f6a1f1fca7aa
--- /dev/null
+++ b/libc/src/complex/generic/cimag.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of cimag 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/cimag.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, cimag, (_Complex double x)) {
+  double *xCmplxPtr = reinterpret_cast<double *>(&x);
+  return xCmplxPtr[1];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cimagf.cpp b/libc/src/complex/generic/cimagf.cpp
new file mode 100644
index 00000000000000..f4c8c924a297d7
--- /dev/null
+++ b/libc/src/complex/generic/cimagf.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of cimagf 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/cimagf.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, cimagf, (_Complex float x)) {
+  float *xCmplxPtr = reinterpret_cast<float *>(&x);
+  return xCmplxPtr[1];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cimagf128.cpp b/libc/src/complex/generic/cimagf128.cpp
new file mode 100644
index 00000000000000..5b9af964ada052
--- /dev/null
+++ b/libc/src/complex/generic/cimagf128.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of cimagf128 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/cimagf128.h"
+#if defined(LIBC_TYPES_HAS_CFLOAT128)
+
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float128, cimagf128, (cfloat128 x)) {
+  float128 *xCmplxPtr = reinterpret_cast<float128 *>(&x);
+  return xCmplxPtr[1];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/generic/cimagf16.cpp b/libc/src/complex/generic/cimagf16.cpp
new file mode 100644
index 00000000000000..9de91182c51898
--- /dev/null
+++ b/libc/src/complex/generic/cimagf16.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of cimagf16 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/cimagf16.h"
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, cimagf16, (cfloat16 x)) {
+  float16 *xCmplxPtr = reinterpret_cast<float16 *>(&x);
+  return xCmplxPtr[1];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/generic/cimagl.cpp b/libc/src/complex/generic/cimagl.cpp
new file mode 100644
index 00000000000000..c28e6c0146fbb7
--- /dev/null
+++ b/libc/src/complex/generic/cimagl.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of cimagl 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/cimagl.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(long double, cimagl, (_Complex long double x)) {
+  long double *xCmplxPtr = reinterpret_cast<long double *>(&x);
+  return xCmplxPtr[1];
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/complex/CImagTest.h b/libc/test/src/complex/CImagTest.h
new file mode 100644
index 00000000000000..1a0719c22d183a
--- /dev/null
+++ b/libc/test/src/complex/CImagTest.h
@@ -0,0 +1,73 @@
+//===-- Utility class to test different flavors of cimag --------*- 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_CIMAGTEST_H
+#define LLVM_LIBC_TEST_SRC_COMPLEX_CIMAGTEST_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 CImagTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+  DECLARE_SPECIAL_CONSTANTS(FPT)
+
+public:
+  typedef FPT (*CImagFunc)(CFPT);
+
+  void testSpecialNumbers(CImagFunc func) {
+    EXPECT_FP_EQ(func(CFPT(67.123 + aNaN * 1.0i)), aNaN);
+    EXPECT_FP_EQ(func(CFPT(78.319 + neg_aNaN * 1.0i)), neg_aNaN);
+    EXPECT_FP_EQ(func(CFPT(7813.131 + sNaN * 1.0i)), sNaN);
+    EXPECT_FP_EQ(func(CFPT(7824.152 + neg_sNaN * 1.0i)), neg_sNaN);
+    EXPECT_FP_EQ(func(CFPT(9024.2442 + inf * 1.0i)), inf);
+    EXPECT_FP_EQ(func(CFPT(8923.124 + neg_inf * 1.0i)), neg_inf);
+    EXPECT_FP_EQ(func(CFPT(782.124 + min_normal * 1.0i)), min_normal);
+    EXPECT_FP_EQ(func(CFPT(2141.2352 + max_normal * 1.0i)), max_normal);
+    EXPECT_FP_EQ(func(CFPT(341.134 + neg_max_normal * 1.0i)), neg_max_normal);
+    EXPECT_FP_EQ(func(CFPT(781.142 + min_denormal * 1.0i)), min_denormal);
+    EXPECT_FP_EQ(func(CFPT(781.134 + neg_min_denormal * 1.0i)), 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);
+  }
+
+  void testRoundedNumbers(CImagFunc func) {
+    EXPECT_FP_EQ(func((CFPT)(4523.1413 + 12413.1414i)), (FPT)(12413.1414));
+    EXPECT_FP_EQ(func((CFPT)(-4523.1413 + 12413.1414i)), (FPT)(12413.1414));
+    EXPECT_FP_EQ(func((CFPT)(4523.1413 - 12413.1414i)), (FPT)(-12413.1414));
+    EXPECT_FP_EQ(func((CFPT)(-4523.1413 - 12413.1414i)), (FPT)(-12413.1414));
+
+    EXPECT_FP_EQ(func((CFPT)(3210.5678 + 9876.5432i)), (FPT)(9876.5432));
+    EXPECT_FP_EQ(func((CFPT)(-3210.5678 + 9876.5432i)), (FPT)(9876.5432));
+    EXPECT_FP_EQ(func((CFPT)(3210.5678 - 9876.5432i)), (FPT)(-9876.5432));
+    EXPECT_FP_EQ(func((CFPT)(-3210.5678 - 9876.5432i)), (FPT)(-9876.5432));
+
+    EXPECT_FP_EQ(func((CFPT)(1234.4321 + 4321.1234i)), (FPT)(4321.1234));
+    EXPECT_FP_EQ(func((CFPT)(-1234.4321 + 4321.1234i)), (FPT)(4321.1234));
+    EXPECT_FP_EQ(func((CFPT)(1234.4321 - 4321.1234i)), (FPT)(-4321.1234));
+    EXPECT_FP_EQ(func((CFPT)(-1234.4321 - 4321.1234i)), (FPT)(-4321.1234));
+
+    EXPECT_FP_EQ(func((CFPT)(6789.1234 + 8765.6789i)), (FPT)(8765.6789));
+    EXPECT_FP_EQ(func((CFPT)(-6789.1234 + 8765.6789i)), (FPT)(8765.6789));
+    EXPECT_FP_EQ(func((CFPT)(6789.1234 - 8765.6789i)), (FPT)(-8765.6789));
+    EXPECT_FP_EQ(func((CFPT)(-6789.1234 - 8765.6789i)), (FPT)(-8765.6789));
+  }
+};
+
+#define LIST_CIMAG_TESTS(U, T, func)                                           \
+  using LlvmLibcCImagTest = CImagTest<U, T>;                                   \
+  TEST_F(LlvmLibcCImagTest, SpecialNumbers) { testSpecialNumbers(&func); }     \
+  TEST_F(LlvmLibcCImagTest, RoundedNumbers) { testRoundedNumbers(&func); }
+
+#endif // LLVM_LIBC_TEST_SRC_COMPLEX_CIMAGTEST_H
diff --git a/libc/test/src/complex/CMakeLists.txt b/libc/test/src/complex/CMakeLists.txt
index 51d0d2a4278b42..8f49e6d79e1796 100644
--- a/libc/test/src/complex/CMakeLists.txt
+++ b/libc/test/src/complex/CMakeLists.txt
@@ -59,3 +59,63 @@ add_libc_test(
   LINK_LIBRARIES
     LibcFPTestHelpers
 )
+
+add_libc_test(
+  cimag_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cimag_test.cpp
+  DEPENDS
+    libc.src.complex.cimag
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  cimagf_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cimagf_test.cpp
+  DEPENDS
+    libc.src.complex.cimagf
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  cimagl_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cimagl_test.cpp
+  DEPENDS
+    libc.src.complex.cimagl
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  cimagf16_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cimagf16_test.cpp
+  DEPENDS
+    libc.src.complex.cimagf16
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
+
+add_libc_test(
+  cimagf128_test
+  SUITE
+    libc-complex-unittests
+  SRCS
+    cimagf128_test.cpp
+  DEPENDS
+    libc.src.complex.cimagf128
+  LINK_LIBRARIES
+    LibcFPTestHelpers
+)
diff --git a/libc/test/src/complex/CRealTest.h b/libc/test/src/complex/CRealTest.h
index 204a8fc529c100..a25555b506e225 100644
--- a/libc/test/src/complex/CRealTest.h
+++ b/libc/test/src/complex/CRealTest.h
@@ -37,7 +37,7 @@ 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 - zero * 1.0i)), neg_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);
   }
 
diff --git a/libc/test/src/complex/cimag_test.cpp b/libc/test/src/complex/cimag_test.cpp
new file mode 100644
index 00000000000000..706f54422b3cf8
--- /dev/null
+++ b/libc/test/src/complex/cimag_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for cimag -----------------------------------------------===//
+//
+// 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 "CImagTest.h"
+
+#include "src/complex/cimag.h"
+
+LIST_CIMAG_TESTS(_Complex double, double, LIBC_NAMESPACE::cimag)
diff --git a/libc/test/src/complex/cimagf128_test.cpp b/libc/test/src/complex/cimagf128_test.cpp
new file mode 100644
index 00000000000000..50ddc0ab061663
--- /dev/null
+++ b/libc/test/src/complex/cimagf128_test.cpp
@@ -0,0 +1,17 @@
+//===-- Unittests for cimagf128 -------------------------------------------===//
+//
+// 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 "CImagTest.h"
+
+#include "src/complex/cimagf128.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT128)
+
+LIST_CIMAG_TESTS(cfloat128, float128, LIBC_NAMESPACE::cimagf128)
+
+#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/test/src/complex/cimagf16_test.cpp b/libc/test/src/complex/cimagf16_test.cpp
new file mode 100644
index 00000000000000..65a69787ddbd66
--- /dev/null
+++ b/libc/test/src/complex/cimagf16_test.cpp
@@ -0,0 +1,17 @@
+//===-- Unittests for cimagf16 --------------------------------------------===//
+//
+// 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 "CImagTest.h"
+
+#include "src/complex/cimagf16.h"
+
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+
+LIST_CIMAG_TESTS(cfloat16, float16, LIBC_NAMESPACE::cimagf16)
+
+#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/test/src/complex/cimagf_test.cpp b/libc/test/src/complex/cimagf_test.cpp
new file mode 100644
index 00000000000000..abc9225031e134
--- /dev/null
+++ b/libc/test/src/complex/cimagf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for cimagf ----------------------------------------------===//
+//
+// 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 "CImagTest.h"
+
+#include "src/complex/cimagf.h"
+
+LIST_CIMAG_TESTS(_Complex float, float, LIBC_NAMESPACE::cimagf)
diff --git a/libc/test/src/complex/cimagl_test.cpp b/libc/test/src/complex/cimagl_test.cpp
new file mode 100644
index 00000000000000..156e4c6bc852d4
--- /dev/null
+++ b/libc/test/src/complex/cimagl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for cimagl ----------------------------------------------===//
+//
+// 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 "CImagTest.h"
+
+#include "src/complex/cimagl.h"
+
+LIST_CIMAG_TESTS(_Complex long double, long double, LIBC_NAMESPACE::cimagl)

>From 7d3521b704b7f960053171c54cb6ccd11775dd2f Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 25 Oct 2024 16:06:45 +0530
Subject: [PATCH 5/7] ran fmt

---
 libc/test/src/complex/CImagTest.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/complex/CImagTest.h b/libc/test/src/complex/CImagTest.h
index 1a0719c22d183a..6d2f9350026691 100644
--- a/libc/test/src/complex/CImagTest.h
+++ b/libc/test/src/complex/CImagTest.h
@@ -34,7 +34,8 @@ class CImagTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(func(CFPT(2141.2352 + max_normal * 1.0i)), max_normal);
     EXPECT_FP_EQ(func(CFPT(341.134 + neg_max_normal * 1.0i)), neg_max_normal);
     EXPECT_FP_EQ(func(CFPT(781.142 + min_denormal * 1.0i)), min_denormal);
-    EXPECT_FP_EQ(func(CFPT(781.134 + neg_min_denormal * 1.0i)), neg_min_denormal);
+    EXPECT_FP_EQ(func(CFPT(781.134 + neg_min_denormal * 1.0i)),
+                 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);

>From bb4f82bb7b10426a1d9906d23ff0e72c01803de6 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 31 Oct 2024 16:59:58 +0530
Subject: [PATCH 6/7] use bit_cast

---
 libc/src/__support/CMakeLists.txt       |  6 ++++
 libc/src/__support/complex_type.h       | 21 ++++++++++++++
 libc/src/complex/generic/CMakeLists.txt | 38 +++++++++++++++++++++++++
 libc/src/complex/generic/cimag.cpp      |  8 +++---
 libc/src/complex/generic/cimagf.cpp     |  8 +++---
 libc/src/complex/generic/cimagf128.cpp  |  8 +++---
 libc/src/complex/generic/cimagf16.cpp   |  8 +++---
 libc/src/complex/generic/cimagl.cpp     |  8 +++---
 libc/src/complex/generic/creal.cpp      |  8 +++---
 libc/src/complex/generic/crealf.cpp     |  8 +++---
 libc/src/complex/generic/crealf128.cpp  |  8 +++---
 libc/src/complex/generic/crealf16.cpp   |  8 +++---
 libc/src/complex/generic/creall.cpp     |  8 +++---
 13 files changed, 105 insertions(+), 40 deletions(-)
 create mode 100644 libc/src/__support/complex_type.h

diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 4785895b562b5e..cb1ce4915a0434 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -209,6 +209,12 @@ add_header_library(
     libc.src.__support.CPP.limits
 )
 
+add_header_library(
+  complex_type
+  HDRS
+    complex_type.h
+)
+
 add_header_library(
   integer_operations
   HDRS
diff --git a/libc/src/__support/complex_type.h b/libc/src/__support/complex_type.h
new file mode 100644
index 00000000000000..9e62ebf8ed0819
--- /dev/null
+++ b/libc/src/__support/complex_type.h
@@ -0,0 +1,21 @@
+//===-- complex type --------------------------------------------*- 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___SUPPORT_COMPLEX_TYPE_H
+#define LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+    template<typename T>
+    struct Complex {
+        T real;
+        T imag;
+    };
+} // 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 0eace99f5e82e5..a63871b32afbf1 100644
--- a/libc/src/complex/generic/CMakeLists.txt
+++ b/libc/src/complex/generic/CMakeLists.txt
@@ -6,6 +6,9 @@ add_entrypoint_object(
     ../creal.h
   COMPILE_OPTIONS
     -O3
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
 )
 
 add_entrypoint_object(
@@ -16,6 +19,9 @@ add_entrypoint_object(
     ../crealf.h
   COMPILE_OPTIONS
     -O3
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
 )
 
 add_entrypoint_object(
@@ -26,6 +32,9 @@ add_entrypoint_object(
     ../creall.h
   COMPILE_OPTIONS
     -O3
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
 )
 
 add_entrypoint_object(
@@ -36,6 +45,11 @@ add_entrypoint_object(
     ../crealf16.h
   COMPILE_OPTIONS
     -O3
+  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(
@@ -46,6 +60,11 @@ add_entrypoint_object(
     ../crealf128.h
   COMPILE_OPTIONS
     -O3
+  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(
@@ -56,6 +75,9 @@ add_entrypoint_object(
     ../cimag.h
   COMPILE_OPTIONS
     -O3
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
 )
 
 add_entrypoint_object(
@@ -66,6 +88,9 @@ add_entrypoint_object(
     ../cimagf.h
   COMPILE_OPTIONS
     -O3
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
 )
 
 add_entrypoint_object(
@@ -76,6 +101,9 @@ add_entrypoint_object(
     ../cimagl.h
   COMPILE_OPTIONS
     -O3
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.complex_type
 )
 
 add_entrypoint_object(
@@ -86,6 +114,11 @@ add_entrypoint_object(
     ../cimagf16.h
   COMPILE_OPTIONS
     -O3
+  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(
@@ -96,4 +129,9 @@ add_entrypoint_object(
     ../cimagf128.h
   COMPILE_OPTIONS
     -O3
+  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/cimag.cpp b/libc/src/complex/generic/cimag.cpp
index e2f6a1f1fca7aa..a7506f5c392e68 100644
--- a/libc/src/complex/generic/cimag.cpp
+++ b/libc/src/complex/generic/cimag.cpp
@@ -7,15 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/cimag.h"
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, cimag, (_Complex double x)) {
-  double *xCmplxPtr = reinterpret_cast<double *>(&x);
-  return xCmplxPtr[1];
+  Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
+  return x_c.imag;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cimagf.cpp b/libc/src/complex/generic/cimagf.cpp
index f4c8c924a297d7..436e841078f656 100644
--- a/libc/src/complex/generic/cimagf.cpp
+++ b/libc/src/complex/generic/cimagf.cpp
@@ -7,15 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/cimagf.h"
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, cimagf, (_Complex float x)) {
-  float *xCmplxPtr = reinterpret_cast<float *>(&x);
-  return xCmplxPtr[1];
+  Complex<float> x_c = cpp::bit_cast<Complex<float>>(x);
+  return x_c.imag;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cimagf128.cpp b/libc/src/complex/generic/cimagf128.cpp
index 5b9af964ada052..7574c97938d908 100644
--- a/libc/src/complex/generic/cimagf128.cpp
+++ b/libc/src/complex/generic/cimagf128.cpp
@@ -9,15 +9,15 @@
 #include "src/complex/cimagf128.h"
 #if defined(LIBC_TYPES_HAS_CFLOAT128)
 
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float128, cimagf128, (cfloat128 x)) {
-  float128 *xCmplxPtr = reinterpret_cast<float128 *>(&x);
-  return xCmplxPtr[1];
+  Complex<float128> x_c = cpp::bit_cast<Complex<float128>>(x);
+  return x_c.imag;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cimagf16.cpp b/libc/src/complex/generic/cimagf16.cpp
index 9de91182c51898..db6a5ca24f80d9 100644
--- a/libc/src/complex/generic/cimagf16.cpp
+++ b/libc/src/complex/generic/cimagf16.cpp
@@ -9,15 +9,15 @@
 #include "src/complex/cimagf16.h"
 #if defined(LIBC_TYPES_HAS_CFLOAT16)
 
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, cimagf16, (cfloat16 x)) {
-  float16 *xCmplxPtr = reinterpret_cast<float16 *>(&x);
-  return xCmplxPtr[1];
+  Complex<float16> x_c = cpp::bit_cast<Complex<float16>>(x);
+  return x_c.imag;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cimagl.cpp b/libc/src/complex/generic/cimagl.cpp
index c28e6c0146fbb7..3ed2c871d830a9 100644
--- a/libc/src/complex/generic/cimagl.cpp
+++ b/libc/src/complex/generic/cimagl.cpp
@@ -7,15 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/cimagl.h"
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, cimagl, (_Complex long double x)) {
-  long double *xCmplxPtr = reinterpret_cast<long double *>(&x);
-  return xCmplxPtr[1];
+  Complex<long double> x_c = cpp::bit_cast<Complex<long double>>(x);
+  return x_c.imag;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/creal.cpp b/libc/src/complex/generic/creal.cpp
index fbac499e939374..0c3db260a3291b 100644
--- a/libc/src/complex/generic/creal.cpp
+++ b/libc/src/complex/generic/creal.cpp
@@ -7,15 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/creal.h"
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, creal, (_Complex double x)) {
-  double *xCmplxPtr = reinterpret_cast<double *>(&x);
-  return xCmplxPtr[0];
+  Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
+  return x_c.real;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/crealf.cpp b/libc/src/complex/generic/crealf.cpp
index f613e7fd566bce..dbf8aed32fd343 100644
--- a/libc/src/complex/generic/crealf.cpp
+++ b/libc/src/complex/generic/crealf.cpp
@@ -7,15 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/crealf.h"
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, crealf, (_Complex float x)) {
-  float *xCmplxPtr = reinterpret_cast<float *>(&x);
-  return xCmplxPtr[0];
+  Complex<float> x_c = cpp::bit_cast<Complex<float>>(x);
+  return x_c.real;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/crealf128.cpp b/libc/src/complex/generic/crealf128.cpp
index df29102d259851..dc47c34b67325d 100644
--- a/libc/src/complex/generic/crealf128.cpp
+++ b/libc/src/complex/generic/crealf128.cpp
@@ -9,15 +9,15 @@
 #include "src/complex/crealf128.h"
 #if defined(LIBC_TYPES_HAS_CFLOAT128)
 
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float128, crealf128, (cfloat128 x)) {
-  float128 *xCmplxPtr = reinterpret_cast<float128 *>(&x);
-  return xCmplxPtr[0];
+  Complex<float128> x_c = cpp::bit_cast<Complex<float128>>(x);
+  return x_c.real;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/crealf16.cpp b/libc/src/complex/generic/crealf16.cpp
index fcb6181c988d74..e4d8ad2e0ea1ba 100644
--- a/libc/src/complex/generic/crealf16.cpp
+++ b/libc/src/complex/generic/crealf16.cpp
@@ -9,15 +9,15 @@
 #include "src/complex/crealf16.h"
 #if defined(LIBC_TYPES_HAS_CFLOAT16)
 
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, crealf16, (cfloat16 x)) {
-  float16 *xCmplxPtr = reinterpret_cast<float16 *>(&x);
-  return xCmplxPtr[0];
+  Complex<float16> x_c = cpp::bit_cast<Complex<float16>>(x);
+  return x_c.real;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/creall.cpp b/libc/src/complex/generic/creall.cpp
index e961743ba87852..86af965dbf96ea 100644
--- a/libc/src/complex/generic/creall.cpp
+++ b/libc/src/complex/generic/creall.cpp
@@ -7,15 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/creall.h"
-#include "src/__support/FPUtil/NearestIntegerOperations.h"
 #include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/complex_type.h"
+#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, creall, (_Complex long double x)) {
-  long double *xCmplxPtr = reinterpret_cast<long double *>(&x);
-  return xCmplxPtr[0];
+  Complex<long double> x_c = cpp::bit_cast<Complex<long double>>(x);
+  return x_c.real;
 }
 
 } // namespace LIBC_NAMESPACE_DECL

>From b7150f5d7eb9946107eac8421ea93ae681f90854 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 31 Oct 2024 17:00:27 +0530
Subject: [PATCH 7/7] fmt

---
 libc/src/__support/complex_type.h      | 9 ++++-----
 libc/src/complex/generic/cimag.cpp     | 2 +-
 libc/src/complex/generic/cimagf.cpp    | 2 +-
 libc/src/complex/generic/cimagf128.cpp | 2 +-
 libc/src/complex/generic/cimagf16.cpp  | 2 +-
 libc/src/complex/generic/cimagl.cpp    | 2 +-
 libc/src/complex/generic/creal.cpp     | 2 +-
 libc/src/complex/generic/crealf.cpp    | 2 +-
 libc/src/complex/generic/crealf128.cpp | 2 +-
 libc/src/complex/generic/crealf16.cpp  | 2 +-
 libc/src/complex/generic/creall.cpp    | 2 +-
 11 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/libc/src/__support/complex_type.h b/libc/src/__support/complex_type.h
index 9e62ebf8ed0819..d6b5eec8fd6b24 100644
--- a/libc/src/__support/complex_type.h
+++ b/libc/src/__support/complex_type.h
@@ -12,10 +12,9 @@
 #include "src/__support/macros/config.h"
 
 namespace LIBC_NAMESPACE_DECL {
-    template<typename T>
-    struct Complex {
-        T real;
-        T imag;
-    };
+template <typename T> struct Complex {
+  T real;
+  T imag;
+};
 } // namespace LIBC_NAMESPACE_DECL
 #endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
diff --git a/libc/src/complex/generic/cimag.cpp b/libc/src/complex/generic/cimag.cpp
index a7506f5c392e68..6aa5de4c2e78a8 100644
--- a/libc/src/complex/generic/cimag.cpp
+++ b/libc/src/complex/generic/cimag.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/cimag.h"
+#include "src/__support/CPP/bit.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/complex/generic/cimagf.cpp b/libc/src/complex/generic/cimagf.cpp
index 436e841078f656..f61808c8120773 100644
--- a/libc/src/complex/generic/cimagf.cpp
+++ b/libc/src/complex/generic/cimagf.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/cimagf.h"
+#include "src/__support/CPP/bit.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/complex/generic/cimagf128.cpp b/libc/src/complex/generic/cimagf128.cpp
index 7574c97938d908..c21bd7f4602cc1 100644
--- a/libc/src/complex/generic/cimagf128.cpp
+++ b/libc/src/complex/generic/cimagf128.cpp
@@ -9,9 +9,9 @@
 #include "src/complex/cimagf128.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/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/complex/generic/cimagf16.cpp b/libc/src/complex/generic/cimagf16.cpp
index db6a5ca24f80d9..361687984067b3 100644
--- a/libc/src/complex/generic/cimagf16.cpp
+++ b/libc/src/complex/generic/cimagf16.cpp
@@ -9,9 +9,9 @@
 #include "src/complex/cimagf16.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/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/complex/generic/cimagl.cpp b/libc/src/complex/generic/cimagl.cpp
index 3ed2c871d830a9..1f63ad3a957363 100644
--- a/libc/src/complex/generic/cimagl.cpp
+++ b/libc/src/complex/generic/cimagl.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/cimagl.h"
+#include "src/__support/CPP/bit.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/complex/generic/creal.cpp b/libc/src/complex/generic/creal.cpp
index 0c3db260a3291b..e97e1dac2473d8 100644
--- a/libc/src/complex/generic/creal.cpp
+++ b/libc/src/complex/generic/creal.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/creal.h"
+#include "src/__support/CPP/bit.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/complex/generic/crealf.cpp b/libc/src/complex/generic/crealf.cpp
index dbf8aed32fd343..304c743d5922aa 100644
--- a/libc/src/complex/generic/crealf.cpp
+++ b/libc/src/complex/generic/crealf.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/crealf.h"
+#include "src/__support/CPP/bit.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/complex/generic/crealf128.cpp b/libc/src/complex/generic/crealf128.cpp
index dc47c34b67325d..e72a7782160109 100644
--- a/libc/src/complex/generic/crealf128.cpp
+++ b/libc/src/complex/generic/crealf128.cpp
@@ -9,9 +9,9 @@
 #include "src/complex/crealf128.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/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/complex/generic/crealf16.cpp b/libc/src/complex/generic/crealf16.cpp
index e4d8ad2e0ea1ba..35142071f0536d 100644
--- a/libc/src/complex/generic/crealf16.cpp
+++ b/libc/src/complex/generic/crealf16.cpp
@@ -9,9 +9,9 @@
 #include "src/complex/crealf16.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/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/complex/generic/creall.cpp b/libc/src/complex/generic/creall.cpp
index 86af965dbf96ea..9d43f977cf300e 100644
--- a/libc/src/complex/generic/creall.cpp
+++ b/libc/src/complex/generic/creall.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/complex/creall.h"
+#include "src/__support/CPP/bit.h"
 #include "src/__support/common.h"
 #include "src/__support/complex_type.h"
-#include "src/__support/CPP/bit.h"
 
 namespace LIBC_NAMESPACE_DECL {
 



More information about the libc-commits mailing list