[libc-commits] [libc] [libc][math] Qualify fdim functions to constexpr (PR #193650)

Kiriti Ponduri via libc-commits libc-commits at lists.llvm.org
Wed Apr 22 19:53:23 PDT 2026


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

>From d55f175d220e63c82795a34c730ffb7559944209 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti624 at gmail.com>
Date: Thu, 23 Apr 2026 08:14:32 +0530
Subject: [PATCH 1/2] [libc][math] Qualify fdim functions to constexpr

Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
---
 libc/src/__support/math/fdim.h                  | 4 +++-
 libc/src/__support/math/fdimbf16.h              | 2 +-
 libc/src/__support/math/fdimf.h                 | 4 +++-
 libc/src/__support/math/fdimf128.h              | 2 +-
 libc/src/__support/math/fdimf16.h               | 4 +++-
 libc/src/__support/math/fdiml.h                 | 2 +-
 libc/test/shared/CMakeLists.txt                 | 6 ++++++
 libc/test/shared/shared_math_constexpr_test.cpp | 7 ++++++-
 8 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/libc/src/__support/math/fdim.h b/libc/src/__support/math/fdim.h
index 6b6def814a28a..6d3c5189e189c 100644
--- a/libc/src/__support/math/fdim.h
+++ b/libc/src/__support/math/fdim.h
@@ -15,7 +15,9 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE double fdim(double x, double y) { return fputil::fdim(x, y); }
+LIBC_INLINE LIBC_CONSTEXPR double fdim(double x, double y) {
+  return fputil::fdim(x, y);
+}
 
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/fdimbf16.h b/libc/src/__support/math/fdimbf16.h
index 473a326b8c114..265f771648a17 100644
--- a/libc/src/__support/math/fdimbf16.h
+++ b/libc/src/__support/math/fdimbf16.h
@@ -16,7 +16,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE bfloat16 fdimbf16(bfloat16 x, bfloat16 y) {
+LIBC_INLINE constexpr bfloat16 fdimbf16(bfloat16 x, bfloat16 y) {
   return fputil::fdim(x, y);
 }
 
diff --git a/libc/src/__support/math/fdimf.h b/libc/src/__support/math/fdimf.h
index d837b5200de38..c8779a6b0ffdb 100644
--- a/libc/src/__support/math/fdimf.h
+++ b/libc/src/__support/math/fdimf.h
@@ -15,7 +15,9 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE float fdimf(float x, float y) { return fputil::fdim(x, y); }
+LIBC_INLINE LIBC_CONSTEXPR float fdimf(float x, float y) {
+  return fputil::fdim(x, y);
+}
 
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/fdimf128.h b/libc/src/__support/math/fdimf128.h
index 02e4b821f2a4b..2537070633257 100644
--- a/libc/src/__support/math/fdimf128.h
+++ b/libc/src/__support/math/fdimf128.h
@@ -19,7 +19,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE float128 fdimf128(float128 x, float128 y) {
+LIBC_INLINE constexpr float128 fdimf128(float128 x, float128 y) {
   return fputil::fdim(x, y);
 }
 
diff --git a/libc/src/__support/math/fdimf16.h b/libc/src/__support/math/fdimf16.h
index b92ae6eae8c59..eb2be87dbd54d 100644
--- a/libc/src/__support/math/fdimf16.h
+++ b/libc/src/__support/math/fdimf16.h
@@ -19,7 +19,9 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE float16 fdimf16(float16 x, float16 y) { return fputil::fdim(x, y); }
+LIBC_INLINE LIBC_CONSTEXPR float16 fdimf16(float16 x, float16 y) {
+  return fputil::fdim(x, y);
+}
 
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/fdiml.h b/libc/src/__support/math/fdiml.h
index bc765951d4a4d..285c61910577e 100644
--- a/libc/src/__support/math/fdiml.h
+++ b/libc/src/__support/math/fdiml.h
@@ -15,7 +15,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE long double fdiml(long double x, long double y) {
+LIBC_INLINE constexpr long double fdiml(long double x, long double y) {
   return fputil::fdim(x, y);
 }
 
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 8c233e8326b77..bf26fee553425 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -290,6 +290,12 @@ add_fp_unittest(
     libc.src.__support.math.copysignf128
     libc.src.__support.math.copysignf16
     libc.src.__support.math.copysignl
+    libc.src.__support.math.fdim
+    libc.src.__support.math.fdimbf16
+    libc.src.__support.math.fdimf
+    libc.src.__support.math.fdimf16
+    libc.src.__support.math.fdimf128
+    libc.src.__support.math.fdiml
     libc.src.__support.math.floor
     libc.src.__support.math.floorbf16
     libc.src.__support.math.floorf
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 5e69d981d0473..66b81b7e33b73 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -17,6 +17,7 @@
 
 static_assert(0.0 == LIBC_NAMESPACE::shared::ceil(0.0));
 static_assert(0.0 == LIBC_NAMESPACE::shared::copysign(0.0, 0.0));
+static_assert(2.0 == LIBC_NAMESPACE::shared::fdim(3.0, 1.0));
 static_assert(1.0 == LIBC_NAMESPACE::shared::floor(1.2));
 static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0));
 
@@ -26,6 +27,7 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0));
 
 static_assert(0.0f == LIBC_NAMESPACE::shared::ceilf(0.0f));
 static_assert(0.0f == LIBC_NAMESPACE::shared::copysignf(0.0f, 0.0f));
+static_assert(2.0f == LIBC_NAMESPACE::shared::fdimf(3.0f, 1.0f));
 static_assert(0.0f == LIBC_NAMESPACE::shared::floorf(0.0f));
 
 //===----------------------------------------------------------------------===//
@@ -63,7 +65,8 @@ static_assert(float128(0.0) == LIBC_NAMESPACE::shared::ceilf128(float128(0.0)));
 static_assert(float128(0.0) ==
               LIBC_NAMESPACE::shared::copysignf128(float128(0.0),
                                                    float128(0.0)));
-
+static_assert(float128(2.0) ==
+              LIBC_NAMESPACE::shared::fdimf128(float128(3.0),float128(1.0)));
 static_assert(float128(0.0) ==
               LIBC_NAMESPACE::shared::floorf128(float128(0.0)));
 
@@ -78,6 +81,8 @@ static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::ceilbf16(bfloat16(0.0)));
 static_assert(bfloat16(0.0) ==
               LIBC_NAMESPACE::shared::copysignbf16(bfloat16(0.0),
                                                    bfloat16(0.0)));
+static_assert(bfloat16(2.0) ==
+              LIBC_NAMESPACE::shared::fdimbf16(bfloat16(3.0), bfloat16(1.0)));
 static_assert(bfloat16(0.0) ==
               LIBC_NAMESPACE::shared::floorbf16(bfloat16(0.0f)));
 static_assert(bfloat16(0.0) ==

>From fb25f74d371c3bab35055e5cfb767fc1d78b653f Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti624 at gmail.com>
Date: Thu, 23 Apr 2026 08:22:14 +0530
Subject: [PATCH 2/2] [libc][math] make fputil::fdim constexpr

Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
---
 libc/src/__support/FPUtil/BasicOperations.h     | 2 +-
 libc/test/shared/shared_math_constexpr_test.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index ca7be6676630a..810f13fc3052a 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -233,7 +233,7 @@ LIBC_INLINE T fminimum_mag_num(T x, T y) {
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
-LIBC_INLINE T fdim(T x, T y) {
+LIBC_INLINE constexpr T fdim(T x, T y) {
   FPBits<T> bitx(x), bity(y);
 
   if (bitx.is_nan()) {
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 66b81b7e33b73..5b8f0280b405f 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -66,7 +66,7 @@ static_assert(float128(0.0) ==
               LIBC_NAMESPACE::shared::copysignf128(float128(0.0),
                                                    float128(0.0)));
 static_assert(float128(2.0) ==
-              LIBC_NAMESPACE::shared::fdimf128(float128(3.0),float128(1.0)));
+              LIBC_NAMESPACE::shared::fdimf128(float128(3.0), float128(1.0)));
 static_assert(float128(0.0) ==
               LIBC_NAMESPACE::shared::floorf128(float128(0.0)));
 



More information about the libc-commits mailing list