[libc-commits] [libc] [libc][math] Qualify getpayload functions to constexpr (PR #195532)
Kiriti Ponduri via libc-commits
libc-commits at lists.llvm.org
Sun May 3 08:17:48 PDT 2026
https://github.com/udaykiriti updated https://github.com/llvm/llvm-project/pull/195532
>From a0474f7caa16e0696c12f02426f69ce578185127 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti624 at gmail.com>
Date: Sun, 3 May 2026 20:04:27 +0530
Subject: [PATCH 1/2] [libc][math] Qualify getpayload functions to constexpr
Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
---
libc/src/__support/FPUtil/BasicOperations.h | 2 +-
libc/src/__support/math/getpayload.h | 2 +-
libc/src/__support/math/getpayloadbf16.h | 2 +-
libc/src/__support/math/getpayloadf.h | 4 +-
libc/src/__support/math/getpayloadf128.h | 2 +-
libc/src/__support/math/getpayloadf16.h | 2 +-
libc/src/__support/math/getpayloadl.h | 2 +-
libc/test/shared/CMakeLists.txt | 6 +++
.../shared/shared_math_constexpr_test.cpp | 37 +++++++++++++++++++
9 files changed, 52 insertions(+), 7 deletions(-)
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 2cbb1f8a04ee7..acd3e16dad59f 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -365,7 +365,7 @@ totalordermag(T x, T y) {
}
template <typename T>
-LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> getpayload(T x) {
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T> getpayload(T x) {
using FPBits = FPBits<T>;
using StorageType = typename FPBits::StorageType;
FPBits x_bits(x);
diff --git a/libc/src/__support/math/getpayload.h b/libc/src/__support/math/getpayload.h
index 5983edc827c1a..8a0b2845fd52f 100644
--- a/libc/src/__support/math/getpayload.h
+++ b/libc/src/__support/math/getpayload.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double getpayload(const double *x) {
+LIBC_INLINE constexpr double getpayload(const double *x) {
return fputil::getpayload(*x);
}
diff --git a/libc/src/__support/math/getpayloadbf16.h b/libc/src/__support/math/getpayloadbf16.h
index a2a60d8816f1b..37878f4fe7252 100644
--- a/libc/src/__support/math/getpayloadbf16.h
+++ b/libc/src/__support/math/getpayloadbf16.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 getpayloadbf16(const bfloat16 *x) {
+LIBC_INLINE constexpr bfloat16 getpayloadbf16(const bfloat16 *x) {
return fputil::getpayload(*x);
}
diff --git a/libc/src/__support/math/getpayloadf.h b/libc/src/__support/math/getpayloadf.h
index 107aab60d2fe3..6ab8d4c6e6e77 100644
--- a/libc/src/__support/math/getpayloadf.h
+++ b/libc/src/__support/math/getpayloadf.h
@@ -16,7 +16,9 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float getpayloadf(const float *x) { return fputil::getpayload(*x); }
+LIBC_INLINE constexpr float getpayloadf(const float *x) {
+ return fputil::getpayload(*x);
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/getpayloadf128.h b/libc/src/__support/math/getpayloadf128.h
index 68d21c69dc0d5..9e9231f2a89d5 100644
--- a/libc/src/__support/math/getpayloadf128.h
+++ b/libc/src/__support/math/getpayloadf128.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float128 getpayloadf128(const float128 *x) {
+LIBC_INLINE constexpr float128 getpayloadf128(const float128 *x) {
return fputil::getpayload(*x);
}
diff --git a/libc/src/__support/math/getpayloadf16.h b/libc/src/__support/math/getpayloadf16.h
index 46febea7973bb..4c38bce74c38a 100644
--- a/libc/src/__support/math/getpayloadf16.h
+++ b/libc/src/__support/math/getpayloadf16.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 getpayloadf16(const float16 *x) {
+LIBC_INLINE constexpr float16 getpayloadf16(const float16 *x) {
return fputil::getpayload(*x);
}
diff --git a/libc/src/__support/math/getpayloadl.h b/libc/src/__support/math/getpayloadl.h
index cf82f76d8e67a..0bfbc11fa993a 100644
--- a/libc/src/__support/math/getpayloadl.h
+++ b/libc/src/__support/math/getpayloadl.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE long double getpayloadl(const long double *x) {
+LIBC_INLINE constexpr long double getpayloadl(const long double *x) {
return fputil::getpayload(*x);
}
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c64dad12391e9..c1d14ea8a7f7d 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -587,6 +587,12 @@ add_fp_unittest(
libc.src.__support.math.fsub
libc.src.__support.math.fsubf128
libc.src.__support.math.fsubl
+ libc.src.__support.math.getpayload
+ libc.src.__support.math.getpayloadbf16
+ libc.src.__support.math.getpayloadf
+ libc.src.__support.math.getpayloadf128
+ libc.src.__support.math.getpayloadf16
+ libc.src.__support.math.getpayloadl
libc.src.__support.math.ldexp
libc.src.__support.math.ldexpbf16
libc.src.__support.math.ldexpl
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 9885484e854df..b8de138e36eb1 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -35,6 +35,12 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::ufromfp(0.0, 0, 32));
static_assert(0.0 == LIBC_NAMESPACE::shared::ufromfpx(0.0, 0, 32));
static_assert(0.0 == LIBC_NAMESPACE::shared::fmaximum_mag(0.0, 0.0));
static_assert(0.0 == LIBC_NAMESPACE::shared::fminimum_mag(0.0, 0.0));
+static_assert(42.0 == [] {
+ double nan = LIBC_NAMESPACE::fputil::FPBits<double>::quiet_nan(
+ LIBC_NAMESPACE::Sign::POS, 42)
+ .get_val();
+ return LIBC_NAMESPACE::shared::getpayload(&nan);
+}());
constexpr double TOTALORDER_X = 0.0;
constexpr double TOTALORDER_Y = 0.0;
@@ -90,6 +96,12 @@ static_assert(0.0f == LIBC_NAMESPACE::shared::ufromfpf(0.0f, 0, 32));
static_assert(0.0f == LIBC_NAMESPACE::shared::ufromfpxf(0.0f, 0, 32));
static_assert(0.0f == LIBC_NAMESPACE::shared::fmaximum_magf(0.0f, 0.0f));
static_assert(0.0f == LIBC_NAMESPACE::shared::fminimum_magf(0.0f, 0.0f));
+static_assert(42.0f == [] {
+ float nan = LIBC_NAMESPACE::fputil::FPBits<float>::quiet_nan(
+ LIBC_NAMESPACE::Sign::POS, 42)
+ .get_val();
+ return LIBC_NAMESPACE::shared::getpayloadf(&nan);
+}());
constexpr float TOTALORDERF_X = 0.0f;
constexpr float TOTALORDERF_Y = 0.0f;
@@ -151,6 +163,12 @@ static_assert(0.0f16 ==
LIBC_NAMESPACE::shared::fminimum_numf16(0.0f16, 0.0f16));
static_assert(0.0f16 == LIBC_NAMESPACE::shared::fromfpf16(0.0f16, 0, 32));
static_assert(0.0f16 == LIBC_NAMESPACE::shared::fromfpxf16(0.0f16, 0, 32));
+static_assert(static_cast<float16>(42.0) == [] {
+ float16 nan = LIBC_NAMESPACE::fputil::FPBits<float16>::quiet_nan(
+ LIBC_NAMESPACE::Sign::POS, 42)
+ .get_val();
+ return LIBC_NAMESPACE::shared::getpayloadf16(&nan);
+}());
static_assert(0.0f16 == LIBC_NAMESPACE::shared::ufromfpf16(0.0f16, 0, 32));
static_assert(0.0f16 == LIBC_NAMESPACE::shared::ufromfpxf16(0.0f16, 0, 32));
static_assert(0.0f16 ==
@@ -217,6 +235,10 @@ static_assert(0.0L == LIBC_NAMESPACE::shared::fminimum_numl(0.0L, 0.0L));
static_assert(0.0L == LIBC_NAMESPACE::shared::fromfpl(0.0L, 0, 32));
static_assert(0.0L == LIBC_NAMESPACE::shared::fromfpxl(0.0L, 0, 32));
static_assert(0.0L == LIBC_NAMESPACE::shared::ufromfpl(0.0L, 0, 32));
+static_assert(42.0L == [] {
+ long double nan = LIBC_NAMESPACE::fputil::FPBits<long double>::quiet_nan(LIBC_NAMESPACE::Sign::POS, 42).get_val();
+ return LIBC_NAMESPACE::shared::getpayloadl(&nan);
+}());
static_assert(0.0L == LIBC_NAMESPACE::shared::ufromfpxl(0.0L, 0, 32));
static_assert(0.0L == LIBC_NAMESPACE::shared::fmaximum_magl(0.0L, 0.0L));
static_assert(0.0L == LIBC_NAMESPACE::shared::fminimum_magl(0.0L, 0.0L));
@@ -305,6 +327,12 @@ static_assert(float128(0.0) ==
LIBC_NAMESPACE::shared::fromfpf128(float128(0.0), 0, 32));
static_assert(float128(0.0) ==
LIBC_NAMESPACE::shared::fromfpxf128(float128(0.0), 0, 32));
+static_assert(static_cast<float128>(42.0) == [] {
+ float128 nan = LIBC_NAMESPACE::fputil::FPBits<float128>::quiet_nan(
+ LIBC_NAMESPACE::Sign::POS, 42)
+ .get_val();
+ return LIBC_NAMESPACE::shared::getpayloadf128(&nan);
+}());
static_assert(float128(0.0) ==
LIBC_NAMESPACE::shared::ufromfpf128(float128(0.0), 0, 32));
static_assert(float128(0.0) ==
@@ -401,6 +429,15 @@ static_assert(bfloat16(0.0) ==
LIBC_NAMESPACE::shared::fromfpbf16(bfloat16(0.0), 0, 32));
static_assert(bfloat16(0.0) ==
LIBC_NAMESPACE::shared::fromfpxbf16(bfloat16(0.0), 0, 32));
+
+static_assert(static_cast<bfloat16>(42.0) == [] {
+ bfloat16 nan =
+ static_cast<bfloat16>(LIBC_NAMESPACE::fputil::FPBits<bfloat16>::quiet_nan(
+ LIBC_NAMESPACE::Sign::POS, 42)
+ .get_val());
+ return LIBC_NAMESPACE::shared::getpayloadbf16(&nan);
+}());
+
static_assert(bfloat16(0.0) ==
LIBC_NAMESPACE::shared::ufromfpbf16(bfloat16(0.0), 0, 32));
static_assert(bfloat16(0.0) ==
>From 4eb56571c4bc637f9523a389ae9eb9d06b8312d6 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti624 at gmail.com>
Date: Sun, 3 May 2026 20:47:19 +0530
Subject: [PATCH 2/2] formated the code
Signed-rff-by: udaykiriti <udaykiriti624 at gmail.com>
---
libc/src/__support/FPUtil/BasicOperations.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index acd3e16dad59f..e6f5f29e73726 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -365,7 +365,8 @@ totalordermag(T x, T y) {
}
template <typename T>
-LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T> getpayload(T x) {
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T>
+getpayload(T x) {
using FPBits = FPBits<T>;
using StorageType = typename FPBits::StorageType;
FPBits x_bits(x);
More information about the libc-commits
mailing list