[libcxx-commits] [libcxx] [libc++][math] Add `constexpr` to `copysign` functions (PR #216285)

Lucas Mellone via libcxx-commits libcxx-commits at lists.llvm.org
Sat Aug 15 08:58:06 PDT 2026


https://github.com/lknknm updated https://github.com/llvm/llvm-project/pull/216285

>From 714be78ad730429ed9591a77d040b9def148f0e7 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Fri, 14 Aug 2026 11:35:19 +0200
Subject: [PATCH 1/2] constexpr cxx23 copysign

---
 libcxx/include/__math/copysign.h              | 20 ++++++++++++++++---
 libcxx/include/cmath                          |  6 +++---
 .../c.math/constexpr-cxx23-clang.pass.cpp     | 10 +++++-----
 3 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/libcxx/include/__math/copysign.h b/libcxx/include/__math/copysign.h
index 4c297cb0894e9..ea67c8d8704e3 100644
--- a/libcxx/include/__math/copysign.h
+++ b/libcxx/include/__math/copysign.h
@@ -24,19 +24,33 @@ namespace __math {
 
 // copysign
 
-[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float copysign(float __x, float __y) _NOEXCEPT {
+[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI float
+copysign(float __x, float __y) _NOEXCEPT {
   return ::__builtin_copysignf(__x, __y);
 }
 
-[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double copysign(long double __x, long double __y) _NOEXCEPT {
+[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI long double
+copysign(long double __x, long double __y) _NOEXCEPT {
   return ::__builtin_copysignl(__x, __y);
 }
 
 template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
-[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __promote_t<_A1, _A2> copysign(_A1 __x, _A2 __y) _NOEXCEPT {
+[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI __promote_t<_A1, _A2>
+copysign(_A1 __x, _A2 __y) _NOEXCEPT {
   return ::__builtin_copysign(__x, __y);
 }
 
+#ifdef _LIBCPP_PREFERRED_OVERLOAD
+[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_PREFERRED_OVERLOAD float copysignf(float __x, float __y) _NOEXCEPT {
+  return __builtin_copysignf(__x, __y);
+}
+[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _LIBCPP_PREFERRED_OVERLOAD long double
+copysignl(long double __x, long double __y) _NOEXCEPT {
+  return __builtin_copysignl(__x, __y);
+}
+#endif
+
 } // namespace __math
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/cmath b/libcxx/include/cmath
index c4f4030c2049b..8cc48912b46bb 100644
--- a/libcxx/include/cmath
+++ b/libcxx/include/cmath
@@ -424,8 +424,8 @@ using ::atanhf _LIBCPP_USING_IF_EXISTS;
 using ::cbrt _LIBCPP_USING_IF_EXISTS;
 using ::cbrtf _LIBCPP_USING_IF_EXISTS;
 
-using ::copysign _LIBCPP_USING_IF_EXISTS;
-using ::copysignf _LIBCPP_USING_IF_EXISTS;
+using std::__math::copysign _LIBCPP_USING_IF_EXISTS;
+using std::__math::copysignf _LIBCPP_USING_IF_EXISTS;
 
 using ::erf _LIBCPP_USING_IF_EXISTS;
 using ::erff _LIBCPP_USING_IF_EXISTS;
@@ -518,7 +518,7 @@ using ::asinhl _LIBCPP_USING_IF_EXISTS;
 using ::atanhl _LIBCPP_USING_IF_EXISTS;
 using ::cbrtl _LIBCPP_USING_IF_EXISTS;
 
-using ::copysignl _LIBCPP_USING_IF_EXISTS;
+using std::__math::copysignl _LIBCPP_USING_IF_EXISTS;
 
 using ::erfl _LIBCPP_USING_IF_EXISTS;
 using ::erfcl _LIBCPP_USING_IF_EXISTS;
diff --git a/libcxx/test/libcxx/numerics/c.math/constexpr-cxx23-clang.pass.cpp b/libcxx/test/libcxx/numerics/c.math/constexpr-cxx23-clang.pass.cpp
index beca4907284ad..3b8b418884d3d 100644
--- a/libcxx/test/libcxx/numerics/c.math/constexpr-cxx23-clang.pass.cpp
+++ b/libcxx/test/libcxx/numerics/c.math/constexpr-cxx23-clang.pass.cpp
@@ -161,11 +161,11 @@ int main(int, char**) {
   ASSERT_NOT_CONSTEXPR_CXX23(std::remquof(0.5f, 1.0f, &DummyInt) == 0.5f);
   ASSERT_NOT_CONSTEXPR_CXX23(std::remquol(0.5L, 1.0L, &DummyInt) == 0.5L);
 
-  ASSERT_NOT_CONSTEXPR_CXX23(std::copysign(1.0f, 1.0f) == 1.0f);
-  ASSERT_NOT_CONSTEXPR_CXX23(std::copysign(1.0, 1.0) == 1.0);
-  ASSERT_NOT_CONSTEXPR_CXX23(std::copysign(1.0L, 1.0L) == 1.0L);
-  ASSERT_NOT_CONSTEXPR_CXX23(std::copysignf(1.0f, 1.0f) == 1.0f);
-  ASSERT_NOT_CONSTEXPR_CXX23(std::copysignl(1.0L, 1.0L) == 1.0L);
+  ASSERT_CONSTEXPR_CXX23(std::copysign(1.0f, 1.0f) == 1.0f);
+  ASSERT_CONSTEXPR_CXX23(std::copysign(1.0, 1.0) == 1.0);
+  ASSERT_CONSTEXPR_CXX23(std::copysign(1.0L, 1.0L) == 1.0L);
+  ASSERT_CONSTEXPR_CXX23(std::copysignf(1.0f, 1.0f) == 1.0f);
+  ASSERT_CONSTEXPR_CXX23(std::copysignl(1.0L, 1.0L) == 1.0L);
 
   ASSERT_NOT_CONSTEXPR_CXX23(std::nextafter(0.0f, 0.0f) == 0.0f);
   ASSERT_NOT_CONSTEXPR_CXX23(std::nextafter(0.0, 0.0) == 0.0);

>From 9b8bf93665db2c2e1bd1c2e973b2a4d24cf7984e Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Sat, 15 Aug 2026 17:57:51 +0200
Subject: [PATCH 2/2] revert few changes

---
 libcxx/include/__math/copysign.h | 11 -----------
 libcxx/include/cmath             |  6 +++---
 2 files changed, 3 insertions(+), 14 deletions(-)

diff --git a/libcxx/include/__math/copysign.h b/libcxx/include/__math/copysign.h
index ea67c8d8704e3..7e3b5db2d9887 100644
--- a/libcxx/include/__math/copysign.h
+++ b/libcxx/include/__math/copysign.h
@@ -40,17 +40,6 @@ copysign(_A1 __x, _A2 __y) _NOEXCEPT {
   return ::__builtin_copysign(__x, __y);
 }
 
-#ifdef _LIBCPP_PREFERRED_OVERLOAD
-[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_PREFERRED_OVERLOAD float copysignf(float __x, float __y) _NOEXCEPT {
-  return __builtin_copysignf(__x, __y);
-}
-[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _LIBCPP_PREFERRED_OVERLOAD long double
-copysignl(long double __x, long double __y) _NOEXCEPT {
-  return __builtin_copysignl(__x, __y);
-}
-#endif
-
 } // namespace __math
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/cmath b/libcxx/include/cmath
index 8cc48912b46bb..c4f4030c2049b 100644
--- a/libcxx/include/cmath
+++ b/libcxx/include/cmath
@@ -424,8 +424,8 @@ using ::atanhf _LIBCPP_USING_IF_EXISTS;
 using ::cbrt _LIBCPP_USING_IF_EXISTS;
 using ::cbrtf _LIBCPP_USING_IF_EXISTS;
 
-using std::__math::copysign _LIBCPP_USING_IF_EXISTS;
-using std::__math::copysignf _LIBCPP_USING_IF_EXISTS;
+using ::copysign _LIBCPP_USING_IF_EXISTS;
+using ::copysignf _LIBCPP_USING_IF_EXISTS;
 
 using ::erf _LIBCPP_USING_IF_EXISTS;
 using ::erff _LIBCPP_USING_IF_EXISTS;
@@ -518,7 +518,7 @@ using ::asinhl _LIBCPP_USING_IF_EXISTS;
 using ::atanhl _LIBCPP_USING_IF_EXISTS;
 using ::cbrtl _LIBCPP_USING_IF_EXISTS;
 
-using std::__math::copysignl _LIBCPP_USING_IF_EXISTS;
+using ::copysignl _LIBCPP_USING_IF_EXISTS;
 
 using ::erfl _LIBCPP_USING_IF_EXISTS;
 using ::erfcl _LIBCPP_USING_IF_EXISTS;



More information about the libcxx-commits mailing list