[libc-commits] [libc] [libc][math] Qualify fadd functions to constexpr (PR #195426)

Kiriti Ponduri via libc-commits libc-commits at lists.llvm.org
Sat May 2 00:27:41 PDT 2026


https://github.com/udaykiriti updated https://github.com/llvm/llvm-project/pull/195426

>From c6ee7e2e1f294ed020870fde52985e824895ac52 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti624 at gmail.com>
Date: Sat, 2 May 2026 12:50:41 +0530
Subject: [PATCH 1/2] [libc][math] Qualify fadd functions to constexpr

Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
---
 libc/src/__support/FPUtil/Hypot.h               | 2 +-
 libc/src/__support/math/hypot.h                 | 4 +++-
 libc/src/__support/math/hypotbf16.h             | 2 +-
 libc/src/__support/math/hypotf.h                | 2 +-
 libc/src/__support/math/hypotf16.h              | 2 +-
 libc/test/shared/CMakeLists.txt                 | 4 ++++
 libc/test/shared/shared_math_constexpr_test.cpp | 5 +++++
 7 files changed, 16 insertions(+), 5 deletions(-)

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) ==

>From 5d4d61dacf3fcb9ba7254c480a6ac17996558ccd Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti624 at gmail.com>
Date: Sat, 2 May 2026 12:56:24 +0530
Subject: [PATCH 2/2] Initialized local vars

Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
---
 libc/src/__support/FPUtil/Hypot.h | 4 ++--
 libc/src/__support/math/hypotf.h  | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/src/__support/FPUtil/Hypot.h b/libc/src/__support/FPUtil/Hypot.h
index 5121af5dd89fa..ba3080d55546d 100644
--- a/libc/src/__support/FPUtil/Hypot.h
+++ b/libc/src/__support/FPUtil/Hypot.h
@@ -151,7 +151,7 @@ LIBC_INLINE constexpr T hypot(T x, T y) {
   StorageType a_mant = a_bits.get_mantissa();
   StorageType b_mant = b_bits.get_mantissa();
   DStorageType a_mant_sq, b_mant_sq;
-  bool sticky_bits;
+  bool sticky_bits = false;
 
   // Add an extra bit to simplify the final rounding bit computation.
   constexpr StorageType ONE = StorageType(1) << (FPBits_t::FRACTION_LEN + 1);
@@ -160,7 +160,7 @@ LIBC_INLINE constexpr T hypot(T x, T y) {
   b_mant <<= 1;
 
   StorageType leading_one;
-  int y_mant_width;
+  int y_mant_width = 0;
   if (a_exp != 0) {
     leading_one = ONE;
     a_mant |= ONE;
diff --git a/libc/src/__support/math/hypotf.h b/libc/src/__support/math/hypotf.h
index 0524f060e934e..286f1fa408010 100644
--- a/libc/src/__support/math/hypotf.h
+++ b/libc/src/__support/math/hypotf.h
@@ -51,7 +51,7 @@ LIBC_INLINE constexpr float hypotf(float x, float y) {
   // x^2 and y^2 are exact in double precision.
   double x_sq = xd * xd;
 
-  double sum_sq;
+  double sum_sq = 0.0;
 #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
   sum_sq = fputil::multiply_add(yd, yd, x_sq);
 #else



More information about the libc-commits mailing list