[libc-commits] [libc] [libc][math] Qualify fadd functions to constexpr (PR #195426)
via libc-commits
libc-commits at lists.llvm.org
Sat May 2 00:22:15 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Kiriti Ponduri (udaykiriti)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/195426.diff
7 Files Affected:
- (modified) libc/src/__support/FPUtil/Hypot.h (+1-1)
- (modified) libc/src/__support/math/hypot.h (+3-1)
- (modified) libc/src/__support/math/hypotbf16.h (+1-1)
- (modified) libc/src/__support/math/hypotf.h (+1-1)
- (modified) libc/src/__support/math/hypotf16.h (+1-1)
- (modified) libc/test/shared/CMakeLists.txt (+4)
- (modified) libc/test/shared/shared_math_constexpr_test.cpp (+5)
``````````diff
diff --git a/libc/src/__support/FPUtil/Hypot.h b/libc/src/__support/FPUtil/Hypot.h
index 292140065c754..5121af5dd89fa 100644
--- a/libc/src/__support/FPUtil/Hypot.h
+++ b/libc/src/__support/FPUtil/Hypot.h
@@ -105,7 +105,7 @@ template <> struct DoubleLength<uint64_t> {
// - HYPOT(x, y) is NaN if x or y is NaN.
//
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
-LIBC_INLINE T hypot(T x, T y) {
+LIBC_INLINE constexpr T hypot(T x, T y) {
using FPBits_t = FPBits<T>;
using StorageType = typename FPBits<T>::StorageType;
using DStorageType = typename DoubleLength<StorageType>::Type;
diff --git a/libc/src/__support/math/hypot.h b/libc/src/__support/math/hypot.h
index 13d6e3fecff01..a4da1eecfd68f 100644
--- a/libc/src/__support/math/hypot.h
+++ b/libc/src/__support/math/hypot.h
@@ -16,7 +16,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double hypot(double x, double y) { return fputil::hypot(x, y); }
+LIBC_INLINE constexpr double hypot(double x, double y) {
+ return fputil::hypot(x, y);
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/hypotbf16.h b/libc/src/__support/math/hypotbf16.h
index e04013e4e3d68..ef870a9d36073 100644
--- a/libc/src/__support/math/hypotbf16.h
+++ b/libc/src/__support/math/hypotbf16.h
@@ -18,7 +18,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 hypotbf16(bfloat16 x, bfloat16 y) {
+LIBC_INLINE constexpr bfloat16 hypotbf16(bfloat16 x, bfloat16 y) {
return fputil::hypot<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/hypotf.h b/libc/src/__support/math/hypotf.h
index 5d877db190ace..0524f060e934e 100644
--- a/libc/src/__support/math/hypotf.h
+++ b/libc/src/__support/math/hypotf.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float hypotf(float x, float y) {
+LIBC_INLINE constexpr float hypotf(float x, float y) {
using DoubleBits = fputil::FPBits<double>;
using FPBits = fputil::FPBits<float>;
using fputil::DoubleDouble;
diff --git a/libc/src/__support/math/hypotf16.h b/libc/src/__support/math/hypotf16.h
index 07e8a0566b8ec..983b936c7934a 100644
--- a/libc/src/__support/math/hypotf16.h
+++ b/libc/src/__support/math/hypotf16.h
@@ -24,7 +24,7 @@ namespace math {
// For targets where conversion from float to float16 has to be
// emulated, fputil::hypot<float16> is faster
-LIBC_INLINE float16 hypotf16(float16 x, float16 y) {
+LIBC_INLINE constexpr float16 hypotf16(float16 x, float16 y) {
using FloatBits = fputil::FPBits<float>;
using FPBits = fputil::FPBits<float16>;
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 993f28d8c37c4..aeb99668e9d5b 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -514,6 +514,10 @@ add_fp_unittest(
libc.src.__support.math.fromfpxf128
libc.src.__support.math.fromfpxf16
libc.src.__support.math.fromfpxl
+ libc.src.__support.math.hypot
+ libc.src.__support.math.hypotf
+ libc.src.__support.math.hypotbf16
+ libc.src.__support.math.hypotf16
libc.src.__support.math.llogbbf16
libc.src.__support.math.log
libc.src.__support.math.logbbf16
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index f849929e54e08..5a2ca8075950e 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -31,6 +31,7 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::fmaximum_num(0.0, 0.0));
static_assert(0.0 == LIBC_NAMESPACE::shared::fminimum_num(0.0, 0.0));
static_assert(0.0 == LIBC_NAMESPACE::shared::fromfp(0.0, 0, 32));
static_assert(0.0 == LIBC_NAMESPACE::shared::fromfpx(0.0, 0, 32));
+static_assert(5.0 == LIBC_NAMESPACE::shared::hypot(3.0, 4.0));
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));
@@ -70,6 +71,7 @@ static_assert(0.0f == LIBC_NAMESPACE::shared::fmaximum_numf(0.0f, 0.0f));
static_assert(0.0f == LIBC_NAMESPACE::shared::fminimum_numf(0.0f, 0.0f));
static_assert(0.0f == LIBC_NAMESPACE::shared::fromfp(0.0f, 0, 32));
static_assert(0.0f == LIBC_NAMESPACE::shared::fromfpx(0.0f, 0, 32));
+static_assert(5.0f == LIBC_NAMESPACE::shared::hypotf(3.0f, 4.0f));
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));
@@ -110,6 +112,7 @@ 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(5.0f16 == LIBC_NAMESPACE::shared::hypotf16(3.0f16, 4.0f16));
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 ==
@@ -311,6 +314,8 @@ 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(bfloat16(5.0) ==
+ LIBC_NAMESPACE::shared::hypotbf16(bfloat16(3.0), bfloat16(4.0)));
static_assert(bfloat16(0.0) ==
LIBC_NAMESPACE::shared::ufromfpbf16(bfloat16(0.0), 0, 32));
static_assert(bfloat16(0.0) ==
``````````
</details>
https://github.com/llvm/llvm-project/pull/195426
More information about the libc-commits
mailing list