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

via libc-commits libc-commits at lists.llvm.org
Wed Apr 22 19:46:08 PDT 2026


llvmbot 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/193650.diff


8 Files Affected:

- (modified) libc/src/__support/math/fdim.h (+3-1) 
- (modified) libc/src/__support/math/fdimbf16.h (+1-1) 
- (modified) libc/src/__support/math/fdimf.h (+3-1) 
- (modified) libc/src/__support/math/fdimf128.h (+1-1) 
- (modified) libc/src/__support/math/fdimf16.h (+3-1) 
- (modified) libc/src/__support/math/fdiml.h (+1-1) 
- (modified) libc/test/shared/CMakeLists.txt (+6) 
- (modified) libc/test/shared/shared_math_constexpr_test.cpp (+6-1) 


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

``````````

</details>


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


More information about the libc-commits mailing list