[libc-commits] [libc] [libc][math] Qualify floor functions to constexpr (PR #192791)
Kiriti Ponduri via libc-commits
libc-commits at lists.llvm.org
Sat Apr 18 13:31:25 PDT 2026
https://github.com/udaykiriti updated https://github.com/llvm/llvm-project/pull/192791
>From 31292d94f9e5c74598d4f15f3fac9ee3a134b9f8 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti624 at gmail.com>
Date: Sat, 18 Apr 2026 19:59:13 +0530
Subject: [PATCH] [libc][math] Qualify floor functions to constexpr
Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
[libc][math] Ordering, formatting, removing duplicate test cases.
Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
[IR] Add icmp like matcher (NFC) (#192746)
matches icmp and trunc nuw x to i1 (icmp ne x,0)
[libc][math] Cmake depends for floor-constexp
Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
---
libc/src/__support/math/floor.h | 5 +++--
libc/src/__support/math/floorbf16.h | 4 +++-
libc/src/__support/math/floorf.h | 5 +++--
libc/src/__support/math/floorf128.h | 4 +++-
libc/src/__support/math/floorf16.h | 5 +++--
libc/src/__support/math/floorl.h | 4 +++-
libc/test/shared/CMakeLists.txt | 6 ++++++
libc/test/shared/shared_math_constexpr_test.cpp | 11 ++++++++++-
8 files changed, 34 insertions(+), 10 deletions(-)
diff --git a/libc/src/__support/math/floor.h b/libc/src/__support/math/floor.h
index 2f7ac5841087f..01177b0701f7d 100644
--- a/libc/src/__support/math/floor.h
+++ b/libc/src/__support/math/floor.h
@@ -15,8 +15,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double floor(double x) {
-#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
+LIBC_INLINE LIBC_CONSTEXPR double floor(double x) {
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
+ !defined(LIBC_HAS_CONSTANT_EVALUATION)
return __builtin_floor(x);
#else
return fputil::floor(x);
diff --git a/libc/src/__support/math/floorbf16.h b/libc/src/__support/math/floorbf16.h
index 72ed6cc3f2b97..7e4373c2ca741 100644
--- a/libc/src/__support/math/floorbf16.h
+++ b/libc/src/__support/math/floorbf16.h
@@ -16,7 +16,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 floorbf16(bfloat16 x) { return fputil::floor(x); }
+LIBC_INLINE constexpr bfloat16 floorbf16(bfloat16 x) {
+ return fputil::floor(x);
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/floorf.h b/libc/src/__support/math/floorf.h
index 693b17441245b..10e298b8c8d46 100644
--- a/libc/src/__support/math/floorf.h
+++ b/libc/src/__support/math/floorf.h
@@ -15,8 +15,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float floorf(float x) {
-#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
+LIBC_INLINE LIBC_CONSTEXPR float floorf(float x) {
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
+ !defined(LIBC_HAS_CONSTANT_EVALUATION)
return __builtin_floorf(x);
#else
return fputil::floor(x);
diff --git a/libc/src/__support/math/floorf128.h b/libc/src/__support/math/floorf128.h
index b2ef990b6d2b7..85f92ee77fc69 100644
--- a/libc/src/__support/math/floorf128.h
+++ b/libc/src/__support/math/floorf128.h
@@ -19,7 +19,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float128 floorf128(float128 x) { return fputil::floor(x); }
+LIBC_INLINE constexpr float128 floorf128(float128 x) {
+ return fputil::floor(x);
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/floorf16.h b/libc/src/__support/math/floorf16.h
index 82015b6a9f597..80489530a45c7 100644
--- a/libc/src/__support/math/floorf16.h
+++ b/libc/src/__support/math/floorf16.h
@@ -21,9 +21,10 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 floorf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR float16 floorf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS)
+ defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
+ !defined(LIBC_HAS_CONSTANT_EVALUATION)
return fputil::cast<float16>(__builtin_floorf(x));
#else
return fputil::floor(x);
diff --git a/libc/src/__support/math/floorl.h b/libc/src/__support/math/floorl.h
index d5977b784da7f..a43ebabb528e5 100644
--- a/libc/src/__support/math/floorl.h
+++ b/libc/src/__support/math/floorl.h
@@ -15,7 +15,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE long double floorl(long double x) { return fputil::floor(x); }
+LIBC_INLINE constexpr long double floorl(long double x) {
+ return fputil::floor(x);
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 3289321f25dd4..d31b5a7839738 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -289,6 +289,12 @@ add_fp_unittest(
libc.src.__support.math.copysignf128
libc.src.__support.math.copysignf16
libc.src.__support.math.copysignl
+ libc.src.__support.math.floor
+ libc.src.__support.math.floorbf16
+ libc.src.__support.math.floorf
+ libc.src.__support.math.floorf128
+ libc.src.__support.math.floorf16
+ libc.src.__support.math.floorl
libc.src.__support.math.log
)
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 6fb1318e5a4ea..1a013396bcf5e 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -16,8 +16,9 @@
//===----------------------------------------------------------------------===//
static_assert(0.0 == LIBC_NAMESPACE::shared::ceil(0.0));
-static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0));
static_assert(0.0 == LIBC_NAMESPACE::shared::copysign(0.0, 0.0));
+static_assert(1.0 == LIBC_NAMESPACE::shared::floor(1.2));
+static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0));
//===----------------------------------------------------------------------===//
// Float Tests
@@ -25,6 +26,7 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::copysign(0.0, 0.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(0.0f == LIBC_NAMESPACE::shared::floorf(0.0f));
//===----------------------------------------------------------------------===//
// Float16 Tests
@@ -34,6 +36,7 @@ static_assert(0.0f == LIBC_NAMESPACE::shared::copysignf(0.0f, 0.0f));
static_assert(0.0f16 == LIBC_NAMESPACE::shared::ceilf16(0.0f16));
static_assert(0.0f16 == LIBC_NAMESPACE::shared::copysignf16(0.0f16, 0.0f16));
+static_assert(3.0f16 == LIBC_NAMESPACE::shared::floorf16(3.7f16));
#endif // LIBC_TYPES_HAS_FLOAT16
@@ -46,6 +49,7 @@ static_assert(0.0f16 == LIBC_NAMESPACE::shared::copysignf16(0.0f16, 0.0f16));
static_assert(0.0L == LIBC_NAMESPACE::shared::ceill(0.0L));
static_assert(0.0L == LIBC_NAMESPACE::shared::copysignl(0.0L, 0.0L));
+static_assert(0.0L == LIBC_NAMESPACE::shared::floorl(0.0L));
#endif
@@ -60,6 +64,9 @@ static_assert(float128(0.0) ==
LIBC_NAMESPACE::shared::copysignf128(float128(0.0),
float128(0.0)));
+static_assert(float128(0.0) ==
+ LIBC_NAMESPACE::shared::floorf128(float128(0.0)));
+
#endif // LIBC_TYPES_HAS_FLOAT128
//===----------------------------------------------------------------------===//
@@ -71,5 +78,7 @@ 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(0.0f) ==
+ LIBC_NAMESPACE::shared::floorbf16(bfloat16(0.0f)));
TEST(LlvmLibcSharedMathTest, ConstantEvaluation) {}
More information about the libc-commits
mailing list