[libc-commits] [libc] [llvm] [libc][math] Refactor copysign family to header-only (PR #182137)

Mohamed Emad via libc-commits libc-commits at lists.llvm.org
Wed Feb 18 12:54:01 PST 2026


https://github.com/hulxv created https://github.com/llvm/llvm-project/pull/182137

Refactors the copysign math family to be header-only.

Closes https://github.com/llvm/llvm-project/issues/182136

Target Functions:
  - copysign
  - copysignbf16
  - copysignf
  - copysignf128
  - copysignf16
  - copysignl

>From b8d2e6925a484e01130420632ddac4d371cf41c5 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 18 Feb 2026 22:53:46 +0200
Subject: [PATCH] [libc][math] Refactor copysign family to header-only

Refactored functions:
  - copysign
  - copysignbf16
  - copysignf
  - copysignf128
  - copysignf16
  - copysignl
---
 libc/shared/math.h                            |  6 ++
 libc/shared/math/copysign.h                   | 22 +++++
 libc/shared/math/copysignbf16.h               | 22 +++++
 libc/shared/math/copysignf.h                  | 22 +++++
 libc/shared/math/copysignf128.h               | 28 ++++++
 libc/shared/math/copysignf16.h                | 28 ++++++
 libc/shared/math/copysignl.h                  | 22 +++++
 libc/src/__support/math/CMakeLists.txt        | 51 ++++++++++
 libc/src/__support/math/copysign.h            | 30 ++++++
 libc/src/__support/math/copysignbf16.h        | 27 ++++++
 libc/src/__support/math/copysignf.h           | 30 ++++++
 libc/src/__support/math/copysignf128.h        | 32 +++++++
 libc/src/__support/math/copysignf16.h         | 36 ++++++++
 libc/src/__support/math/copysignl.h           | 26 ++++++
 libc/src/math/generic/CMakeLists.txt          | 26 ++----
 libc/src/math/generic/copysign.cpp            | 10 +-
 libc/src/math/generic/copysignbf16.cpp        |  7 +-
 libc/src/math/generic/copysignf.cpp           | 10 +-
 libc/src/math/generic/copysignf128.cpp        |  6 +-
 libc/src/math/generic/copysignf16.cpp         | 10 +-
 libc/src/math/generic/copysignl.cpp           |  6 +-
 libc/test/shared/CMakeLists.txt               |  6 ++
 libc/test/shared/shared_math_test.cpp         | 14 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 92 ++++++++++++++++++-
 24 files changed, 507 insertions(+), 62 deletions(-)
 create mode 100644 libc/shared/math/copysign.h
 create mode 100644 libc/shared/math/copysignbf16.h
 create mode 100644 libc/shared/math/copysignf.h
 create mode 100644 libc/shared/math/copysignf128.h
 create mode 100644 libc/shared/math/copysignf16.h
 create mode 100644 libc/shared/math/copysignl.h
 create mode 100644 libc/src/__support/math/copysign.h
 create mode 100644 libc/src/__support/math/copysignbf16.h
 create mode 100644 libc/src/__support/math/copysignf.h
 create mode 100644 libc/src/__support/math/copysignf128.h
 create mode 100644 libc/src/__support/math/copysignf16.h
 create mode 100644 libc/src/__support/math/copysignl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8a5aca82c6ec3..0b9d15a144649 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -45,6 +45,12 @@
 #include "math/canonicalizel.h"
 #include "math/cbrt.h"
 #include "math/cbrtf.h"
+#include "math/copysign.h"
+#include "math/copysignbf16.h"
+#include "math/copysignf.h"
+#include "math/copysignf128.h"
+#include "math/copysignf16.h"
+#include "math/copysignl.h"
 #include "math/cos.h"
 #include "math/cosf.h"
 #include "math/cosf16.h"
diff --git a/libc/shared/math/copysign.h b/libc/shared/math/copysign.h
new file mode 100644
index 0000000000000..b64ba513f9dce
--- /dev/null
+++ b/libc/shared/math/copysign.h
@@ -0,0 +1,22 @@
+//===-- Shared copysign function --------------------------------*- 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_SHARED_MATH_COPYSIGN_H
+#define LLVM_LIBC_SHARED_MATH_COPYSIGN_H
+
+#include "src/__support/math/copysign.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::copysign;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COPYSIGN_H
diff --git a/libc/shared/math/copysignbf16.h b/libc/shared/math/copysignbf16.h
new file mode 100644
index 0000000000000..3bb24da81f19e
--- /dev/null
+++ b/libc/shared/math/copysignbf16.h
@@ -0,0 +1,22 @@
+//===-- Shared copysignbf16 function ----------------------------*- 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_SHARED_MATH_COPYSIGNBF16_H
+#define LLVM_LIBC_SHARED_MATH_COPYSIGNBF16_H
+
+#include "src/__support/math/copysignbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::copysignbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNBF16_H
diff --git a/libc/shared/math/copysignf.h b/libc/shared/math/copysignf.h
new file mode 100644
index 0000000000000..5551f51ed705f
--- /dev/null
+++ b/libc/shared/math/copysignf.h
@@ -0,0 +1,22 @@
+//===-- Shared copysignf function -------------------------------*- 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_SHARED_MATH_COPYSIGNF_H
+#define LLVM_LIBC_SHARED_MATH_COPYSIGNF_H
+
+#include "src/__support/math/copysignf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::copysignf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNF_H
diff --git a/libc/shared/math/copysignf128.h b/libc/shared/math/copysignf128.h
new file mode 100644
index 0000000000000..59e2f68319120
--- /dev/null
+++ b/libc/shared/math/copysignf128.h
@@ -0,0 +1,28 @@
+//===-- Shared copysignf128 function ----------------------------*- 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_SHARED_MATH_COPYSIGNF128_H
+#define LLVM_LIBC_SHARED_MATH_COPYSIGNF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/copysignf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::copysignf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNF128_H
diff --git a/libc/shared/math/copysignf16.h b/libc/shared/math/copysignf16.h
new file mode 100644
index 0000000000000..bd66d8da6488a
--- /dev/null
+++ b/libc/shared/math/copysignf16.h
@@ -0,0 +1,28 @@
+//===-- Shared copysignf16 function -----------------------------*- 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_SHARED_MATH_COPYSIGNF16_H
+#define LLVM_LIBC_SHARED_MATH_COPYSIGNF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/copysignf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::copysignf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNF16_H
diff --git a/libc/shared/math/copysignl.h b/libc/shared/math/copysignl.h
new file mode 100644
index 0000000000000..5bb0907eaf173
--- /dev/null
+++ b/libc/shared/math/copysignl.h
@@ -0,0 +1,22 @@
+//===-- Shared copysignl function -------------------------------*- 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_SHARED_MATH_COPYSIGNL_H
+#define LLVM_LIBC_SHARED_MATH_COPYSIGNL_H
+
+#include "src/__support/math/copysignl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::copysignl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e21fe8ef0ab93..1970a48271302 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -484,6 +484,57 @@ add_header_library(
     libc.src.__support.macros.config
     libc.src.__support.number_pair
 )
+add_header_library(
+  copysign
+  HDRS
+    copysign.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+add_header_library(
+  copysignbf16
+  HDRS
+    copysignbf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+add_header_library(
+  copysignf
+  HDRS
+    copysignf.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+add_header_library(
+  copysignf128
+  HDRS
+    copysignf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+add_header_library(
+  copysignf16
+  HDRS
+    copysignf16.h
+  DEPENDS
+    libc.include.llvm-libc-macros.float16_macros
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+add_header_library(
+  copysignl
+  HDRS
+    copysignl.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
 
 add_header_library(
   cos
diff --git a/libc/src/__support/math/copysign.h b/libc/src/__support/math/copysign.h
new file mode 100644
index 0000000000000..7ba974b3664cd
--- /dev/null
+++ b/libc/src/__support/math/copysign.h
@@ -0,0 +1,30 @@
+//===-- Implementation header for copysign ----------------------*- 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_MATH_COPYSIGN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGN_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double copysign(double x, double y) {
+#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
+  return __builtin_copysign(x, y);
+#else
+  return fputil::copysign(x, y);
+#endif
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGN_H
diff --git a/libc/src/__support/math/copysignbf16.h b/libc/src/__support/math/copysignbf16.h
new file mode 100644
index 0000000000000..60b72848101f1
--- /dev/null
+++ b/libc/src/__support/math/copysignbf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for copysignbf16 ------------------*- 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_MATH_COPYSIGNBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNBF16_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr bfloat16 copysignbf16(bfloat16 x, bfloat16 y) {
+  return fputil::copysign(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNBF16_H
diff --git a/libc/src/__support/math/copysignf.h b/libc/src/__support/math/copysignf.h
new file mode 100644
index 0000000000000..a8b8adb5631b1
--- /dev/null
+++ b/libc/src/__support/math/copysignf.h
@@ -0,0 +1,30 @@
+//===-- Implementation header for copysignf ---------------------*- 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_MATH_COPYSIGNF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float copysignf(float x, float y) {
+#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
+  return __builtin_copysignf(x, y);
+#else
+  return fputil::copysign(x, y);
+#endif
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF_H
diff --git a/libc/src/__support/math/copysignf128.h b/libc/src/__support/math/copysignf128.h
new file mode 100644
index 0000000000000..a95f4620572ce
--- /dev/null
+++ b/libc/src/__support/math/copysignf128.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for copysignf128 ------------------*- 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_MATH_COPYSIGNF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr float128 copysignf128(float128 x, float128 y) {
+  return fputil::copysign(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF128_H
diff --git a/libc/src/__support/math/copysignf16.h b/libc/src/__support/math/copysignf16.h
new file mode 100644
index 0000000000000..4e79c29256cba
--- /dev/null
+++ b/libc/src/__support/math/copysignf16.h
@@ -0,0 +1,36 @@
+//===-- Implementation header for copysignf16 -------------------*- 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_MATH_COPYSIGNF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float16 copysignf16(float16 x, float16 y) {
+#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
+  return __builtin_copysignf16(x, y);
+#else
+  return fputil::copysign(x, y);
+#endif
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF16_H
diff --git a/libc/src/__support/math/copysignl.h b/libc/src/__support/math/copysignl.h
new file mode 100644
index 0000000000000..3a420365d8178
--- /dev/null
+++ b/libc/src/__support/math/copysignl.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for copysignl ---------------------*- 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_MATH_COPYSIGNL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNL_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr long double copysignl(long double x, long double y) {
+  return fputil::copysign(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 57a29665318a3..6d892dbf11c61 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1517,9 +1517,7 @@ add_entrypoint_object(
   HDRS
     ../copysign.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
-  FLAGS
-    MISC_MATH_BASIC_OPS_OPT
+    libc.src.__support.math.copysign
 )
 
 add_entrypoint_object(
@@ -1529,9 +1527,7 @@ add_entrypoint_object(
   HDRS
     ../copysignf.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
-  FLAGS
-    MISC_MATH_BASIC_OPS_OPT
+    libc.src.__support.math.copysignf
 )
 
 add_entrypoint_object(
@@ -1541,7 +1537,7 @@ add_entrypoint_object(
   HDRS
     ../copysignl.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.copysignl
 )
 
 add_entrypoint_object(
@@ -1551,10 +1547,7 @@ add_entrypoint_object(
   HDRS
     ../copysignf16.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
-  FLAGS
-    MISC_MATH_BASIC_OPS_OPT
+    libc.src.__support.math.copysignf16
 )
 
 add_entrypoint_object(
@@ -1564,8 +1557,7 @@ add_entrypoint_object(
   HDRS
     ../copysignf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.copysignf128
 )
 
 add_entrypoint_object(
@@ -1575,13 +1567,7 @@ add_entrypoint_object(
   HDRS
     ../copysignbf16.h
   DEPENDS
-    libc.src.__support.common
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.manipulation_functions
-  FLAGS
-    MISC_MATH_BASIC_OPS_OPT
+    libc.src.__support.math.copysignbf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/copysign.cpp b/libc/src/math/generic/copysign.cpp
index 186bb2c5983f4..6d92b0600eeb2 100644
--- a/libc/src/math/generic/copysign.cpp
+++ b/libc/src/math/generic/copysign.cpp
@@ -7,18 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/copysign.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/copysign.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, copysign, (double x, double y)) {
-#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
-  return __builtin_copysign(x, y);
-#else
-  return fputil::copysign(x, y);
-#endif
+  return math::copysign(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/copysignbf16.cpp b/libc/src/math/generic/copysignbf16.cpp
index 48ade2b26981c..548fa42835e5f 100644
--- a/libc/src/math/generic/copysignbf16.cpp
+++ b/libc/src/math/generic/copysignbf16.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/copysignbf16.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/copysignbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, copysignbf16, (bfloat16 x, bfloat16 y)) {
-  return fputil::copysign(x, y);
+  return math::copysignbf16(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/copysignf.cpp b/libc/src/math/generic/copysignf.cpp
index c79e50b61ebda..94eeba9eaf705 100644
--- a/libc/src/math/generic/copysignf.cpp
+++ b/libc/src/math/generic/copysignf.cpp
@@ -7,18 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/copysignf.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/copysignf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, copysignf, (float x, float y)) {
-#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
-  return __builtin_copysignf(x, y);
-#else
-  return fputil::copysign(x, y);
-#endif
+  return math::copysignf(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/copysignf128.cpp b/libc/src/math/generic/copysignf128.cpp
index 9a51c8d5eb8df..9af98f0d10723 100644
--- a/libc/src/math/generic/copysignf128.cpp
+++ b/libc/src/math/generic/copysignf128.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/copysignf128.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/copysignf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float128, copysignf128, (float128 x, float128 y)) {
-  return fputil::copysign(x, y);
+  return math::copysignf128(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/copysignf16.cpp b/libc/src/math/generic/copysignf16.cpp
index 546622f049ebe..23db8faf3211a 100644
--- a/libc/src/math/generic/copysignf16.cpp
+++ b/libc/src/math/generic/copysignf16.cpp
@@ -7,18 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/copysignf16.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/copysignf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, copysignf16, (float16 x, float16 y)) {
-#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
-  return __builtin_copysignf16(x, y);
-#else
-  return fputil::copysign(x, y);
-#endif
+  return math::copysignf16(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/copysignl.cpp b/libc/src/math/generic/copysignl.cpp
index 73c47bb04ad4a..b2e6d3ed50ba9 100644
--- a/libc/src/math/generic/copysignl.cpp
+++ b/libc/src/math/generic/copysignl.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/copysignl.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/copysignl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, copysignl, (long double x, long double y)) {
-  return fputil::copysign(x, y);
+  return math::copysignl(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 53ad309af4f30..402dfe00a3659 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -41,6 +41,12 @@ add_fp_unittest(
     libc.src.__support.math.canonicalizel
     libc.src.__support.math.cbrt
     libc.src.__support.math.cbrtf
+    libc.src.__support.math.copysign
+    libc.src.__support.math.copysignbf16
+    libc.src.__support.math.copysignf
+    libc.src.__support.math.copysignf128
+    libc.src.__support.math.copysignf16
+    libc.src.__support.math.copysignl
     libc.src.__support.math.cos
     libc.src.__support.math.cosf
     libc.src.__support.math.cosf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 7f2f39ac7a285..04aa7752433a1 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -88,6 +88,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef16(&canonicalizef16_cx,
                                                        &canonicalizef16_x));
   EXPECT_FP_EQ(0x0p+0f16, canonicalizef16_cx);
+
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::copysignf16(0.0f16, 0.0f16));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT16
@@ -150,6 +152,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef(&canonicalizef_cx,
                                                      &canonicalizef_x));
   EXPECT_FP_EQ(0x0p+0f, canonicalizef_cx);
+
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::copysignf(0.0f, 0.0f));
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
@@ -187,6 +191,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalize(&canonicalize_cx,
                                                     &canonicalize_x));
   EXPECT_FP_EQ(0.0, canonicalize_cx);
+
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::copysign(0.0, 0.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {
@@ -203,6 +209,8 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizel(&canonicalizel_cx,
                                                      &canonicalizel_x));
   EXPECT_FP_EQ(0x0p+0L, canonicalizel_cx);
+
+  EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::copysignl(0.0L, 0.0L));
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
@@ -238,6 +246,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef128(&canonicalizef128_cx,
                                                         &canonicalizef128_x));
   EXPECT_FP_EQ(float128(0.0), canonicalizef128_cx);
+
+  EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::copysignf128(
+                                  float128(0.0), float128(0.0)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
@@ -257,4 +268,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
   EXPECT_FP_EQ(bfloat16(10.0),
                LIBC_NAMESPACE::shared::bf16fmaf(2.0f, 3.0f, 4.0f));
+
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::copysignbf16(
+                                  bfloat16(0.0), bfloat16(0.0)));
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index b90688af7a1d2..d9ec20974d924 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2796,6 +2796,63 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_copysign",
+    hdrs = ["src/__support/math/copysign.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_copysignbf16",
+    hdrs = ["src/__support/math/copysignbf16.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_copysignf",
+    hdrs = ["src/__support/math/copysignf.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_copysignf128",
+    hdrs = ["src/__support/math/copysignf128.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+        ":llvm_libc_types_float128",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_copysignf16",
+    hdrs = ["src/__support/math/copysignf16.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_copysignl",
+    hdrs = ["src/__support/math/copysignl.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_cos",
     hdrs = ["src/__support/math/cos.h"],
@@ -4685,15 +4742,40 @@ libc_math_function(name = "ceilf128")
 
 libc_math_function(name = "ceilf16")
 
-libc_math_function(name = "copysign")
+libc_math_function(
+    name = "copysign",
+    additional_deps = [
+        ":__support_math_copysign",
+    ],
+)
 
-libc_math_function(name = "copysignf")
+libc_math_function(
+    name = "copysignf",
+    additional_deps = [
+        ":__support_math_copysignf",
+    ],
+)
 
-libc_math_function(name = "copysignl")
+libc_math_function(
+    name = "copysignl",
+    additional_deps = [
+        ":__support_math_copysignl",
+    ],
+)
 
-libc_math_function(name = "copysignf128")
+libc_math_function(
+    name = "copysignf128",
+    additional_deps = [
+        ":__support_math_copysignf128",
+    ],
+)
 
-libc_math_function(name = "copysignf16")
+libc_math_function(
+    name = "copysignf16",
+    additional_deps = [
+        ":__support_math_copysignf16",
+    ],
+)
 
 libc_math_function(
     name = "cos",



More information about the libc-commits mailing list