[libc-commits] [libc] [libc][math] Qualify fmax functions to constexpr (PR #194551)

via libc-commits libc-commits at lists.llvm.org
Mon Apr 27 23:33:41 PDT 2026


https://github.com/AnonMiraj created https://github.com/llvm/llvm-project/pull/194551

@bassiounix 

>From ff68907eeddce87f52e5827bbd72d3cf039faea9 Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Tue, 28 Apr 2026 09:30:12 +0300
Subject: [PATCH] [libc][math] Qualify fmax functions to constexpr

---
 libc/src/__support/FPUtil/BasicOperations.h     | 2 +-
 libc/src/__support/math/fmax.h                  | 4 +++-
 libc/src/__support/math/fmaxbf16.h              | 2 +-
 libc/src/__support/math/fmaxf.h                 | 4 +++-
 libc/src/__support/math/fmaxf128.h              | 2 +-
 libc/src/__support/math/fmaxf16.h               | 4 +++-
 libc/src/__support/math/fmaxl.h                 | 2 +-
 libc/test/shared/shared_math_constexpr_test.cpp | 9 +++++++++
 8 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index bbdba52e129cf..7d7688dc21a03 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -152,7 +152,7 @@ LIBC_INLINE constexpr T fmin(T x, T y) {
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
-LIBC_INLINE T fmax(T x, T y) {
+LIBC_INLINE constexpr T fmax(T x, T y) {
   FPBits<T> bitx(x), bity(y);
 
   if (bitx.is_nan())
diff --git a/libc/src/__support/math/fmax.h b/libc/src/__support/math/fmax.h
index c7b0b86b3dbc3..e3a3fc0209460 100644
--- a/libc/src/__support/math/fmax.h
+++ b/libc/src/__support/math/fmax.h
@@ -15,7 +15,9 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE double fmax(double x, double y) { return fputil::fmax(x, y); }
+LIBC_INLINE constexpr double fmax(double x, double y) {
+  return fputil::fmax(x, y);
+}
 
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/fmaxbf16.h b/libc/src/__support/math/fmaxbf16.h
index 4dd41c59a1b7e..ae337cdda0450 100644
--- a/libc/src/__support/math/fmaxbf16.h
+++ b/libc/src/__support/math/fmaxbf16.h
@@ -16,7 +16,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE bfloat16 fmaxbf16(bfloat16 x, bfloat16 y) {
+LIBC_INLINE constexpr bfloat16 fmaxbf16(bfloat16 x, bfloat16 y) {
   return fputil::fmax(x, y);
 }
 
diff --git a/libc/src/__support/math/fmaxf.h b/libc/src/__support/math/fmaxf.h
index 7137b89e392d5..862d9bf1bd062 100644
--- a/libc/src/__support/math/fmaxf.h
+++ b/libc/src/__support/math/fmaxf.h
@@ -15,7 +15,9 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE float fmaxf(float x, float y) { return fputil::fmax(x, y); }
+LIBC_INLINE float constexpr fmaxf(float x, float y) {
+  return fputil::fmax(x, y);
+}
 
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/fmaxf128.h b/libc/src/__support/math/fmaxf128.h
index ab57a0780bb65..65b7878ed0527 100644
--- a/libc/src/__support/math/fmaxf128.h
+++ b/libc/src/__support/math/fmaxf128.h
@@ -19,7 +19,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE float128 fmaxf128(float128 x, float128 y) {
+LIBC_INLINE constexpr float128 fmaxf128(float128 x, float128 y) {
   return fputil::fmax(x, y);
 }
 
diff --git a/libc/src/__support/math/fmaxf16.h b/libc/src/__support/math/fmaxf16.h
index 0a2e2ff066347..884b7032ed268 100644
--- a/libc/src/__support/math/fmaxf16.h
+++ b/libc/src/__support/math/fmaxf16.h
@@ -19,7 +19,9 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE float16 fmaxf16(float16 x, float16 y) { return fputil::fmax(x, y); }
+LIBC_INLINE constexpr float16 fmaxf16(float16 x, float16 y) {
+  return fputil::fmax(x, y);
+}
 
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/fmaxl.h b/libc/src/__support/math/fmaxl.h
index 307f5c59b25d7..41da002588f72 100644
--- a/libc/src/__support/math/fmaxl.h
+++ b/libc/src/__support/math/fmaxl.h
@@ -15,7 +15,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE long double fmaxl(long double x, long double y) {
+LIBC_INLINE constexpr long double fmaxl(long double x, long double y) {
   return fputil::fmax(x, y);
 }
 
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index a048b5f58d6ad..adc6609ec5285 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -26,6 +26,7 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0));
 static_assert(0.0 == LIBC_NAMESPACE::shared::fmaximum(0.0, 0.0));
 static_assert(0.0 == LIBC_NAMESPACE::shared::fminimum(0.0, 0.0));
 static_assert(0.0 == LIBC_NAMESPACE::shared::fmin(0.0, 0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::fmax(0.0, 0.0));
 
 //===----------------------------------------------------------------------===//
 //                       Float Tests
@@ -40,6 +41,7 @@ static_assert(0.0f == LIBC_NAMESPACE::shared::floorf(0.0f));
 static_assert(0.0f == LIBC_NAMESPACE::shared::fmaximumf(0.0f, 0.0f));
 static_assert(0.0f == LIBC_NAMESPACE::shared::fminimumf(0.0f, 0.0f));
 static_assert(0.0f == LIBC_NAMESPACE::shared::fminf(0.0f, 0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::fmaxf(0.0f, 0.0f));
 
 //===----------------------------------------------------------------------===//
 //                       Float16 Tests
@@ -55,6 +57,7 @@ static_assert(3.0f16 == LIBC_NAMESPACE::shared::floorf16(3.7f16));
 static_assert(0.0f16 == LIBC_NAMESPACE::shared::fmaximumf16(0.0f16, 0.0f16));
 static_assert(0.0f16 == LIBC_NAMESPACE::shared::fminimumf16(0.0f16, 0.0f16));
 static_assert(0.0f16 == LIBC_NAMESPACE::shared::fminf16(0.0f16, 0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::fmaxf16(0.0f16, 0.0f16));
 
 #endif // LIBC_TYPES_HAS_FLOAT16
 
@@ -78,6 +81,7 @@ static_assert(0.0L == LIBC_NAMESPACE::shared::sqrtl(0.0L));
 static_assert(0.0L == LIBC_NAMESPACE::shared::fmaximuml(0.0L, 0.0L));
 static_assert(0.0L == LIBC_NAMESPACE::shared::fminimuml(0.0L, 0.0L));
 static_assert(0.0L == LIBC_NAMESPACE::shared::fminl(0.0L, 0.0L));
+static_assert(0.0L == LIBC_NAMESPACE::shared::fmaxl(0.0L, 0.0L));
 
 #endif
 
@@ -112,6 +116,9 @@ static_assert(float128(0.0) ==
 static_assert(float128(0.0) ==
               LIBC_NAMESPACE::shared::fminf128(float128(0.0), float128(0.0)));
 
+static_assert(float128(0.0) ==
+              LIBC_NAMESPACE::shared::fmaxf128(float128(0.0), float128(0.0)));
+
 #endif // LIBC_TYPES_HAS_FLOAT128
 
 //===----------------------------------------------------------------------===//
@@ -144,5 +151,7 @@ static_assert(bfloat16(0.0) ==
                                                    bfloat16(0.0)));
 static_assert(bfloat16(0.0) ==
               LIBC_NAMESPACE::shared::fminbf16(bfloat16(0.0), bfloat16(0.0)));
+static_assert(bfloat16(0.0) ==
+              LIBC_NAMESPACE::shared::fmaxbf16(bfloat16(0.0), bfloat16(0.0)));
 
 TEST(LlvmLibcSharedMathTest, ConstantEvaluation) {}



More information about the libc-commits mailing list