[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 07:32:01 PDT 2026
https://github.com/udaykiriti created https://github.com/llvm/llvm-project/pull/192791
None
>From a9f797a65d598945be2b515d83e85b0f47c59c1f 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/src/__support/math/floor.h | 5 +++--
libc/src/__support/math/floorbf16.h | 2 +-
libc/src/__support/math/floorf.h | 5 +++--
libc/src/__support/math/floorf128.h | 2 +-
libc/src/__support/math/floorf16.h | 5 +++--
.../test/shared/shared_math_constexpr_test.cpp | 18 ++++++++++++++++++
6 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/libc/src/__support/math/floor.h b/libc/src/__support/math/floor.h
index 2f7ac5841087f..52603f6e0671a 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..a69389722361b 100644
--- a/libc/src/__support/math/floorbf16.h
+++ b/libc/src/__support/math/floorbf16.h
@@ -16,7 +16,7 @@
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..82ff9ad51de49 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..06b264d93e87a 100644
--- a/libc/src/__support/math/floorf128.h
+++ b/libc/src/__support/math/floorf128.h
@@ -19,7 +19,7 @@
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/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 6fb1318e5a4ea..c59b2fee00db7 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -19,13 +19,18 @@ 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));
+
//===----------------------------------------------------------------------===//
// Float Tests
//===----------------------------------------------------------------------===//
static_assert(0.0f == LIBC_NAMESPACE::shared::ceilf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::floorf(0.0f));
static_assert(0.0f == LIBC_NAMESPACE::shared::copysignf(0.0f, 0.0f));
+static_assert(2.0f == LIBC_NAMESPACE::shared::floorf(2.9f));
+
//===----------------------------------------------------------------------===//
// Float16 Tests
//===----------------------------------------------------------------------===//
@@ -35,6 +40,8 @@ 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
//===----------------------------------------------------------------------===//
@@ -47,6 +54,8 @@ 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 +69,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
//===----------------------------------------------------------------------===//
@@ -72,4 +84,10 @@ 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)));
+
+static_assert(bfloat16(4.0f) ==
+ LIBC_NAMESPACE::shared::floorbf16(bfloat16(4.8f)));
+
TEST(LlvmLibcSharedMathTest, ConstantEvaluation) {}
More information about the libc-commits
mailing list