[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 11:07:32 PDT 2026
https://github.com/udaykiriti updated https://github.com/llvm/llvm-project/pull/192791
>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 1/3] [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) {}
>From 6e559bbb0066a4758be0cef193a17909d51f7ba9 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti624 at gmail.com>
Date: Sat, 18 Apr 2026 22:33:20 +0530
Subject: [PATCH 2/3] [libc][math] Ordering, formatting, removing duplicate
test cases.
Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
---
libc/src/__support/math/floorl.h | 2 +-
libc/test/shared/shared_math_constexpr_test.cpp | 14 +++-----------
2 files changed, 4 insertions(+), 12 deletions(-)
diff --git a/libc/src/__support/math/floorl.h b/libc/src/__support/math/floorl.h
index d5977b784da7f..445582ecfa070 100644
--- a/libc/src/__support/math/floorl.h
+++ b/libc/src/__support/math/floorl.h
@@ -15,7 +15,7 @@
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/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index c59b2fee00db7..fc6bfc01043e9 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -16,20 +16,17 @@
//===----------------------------------------------------------------------===//
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
//===----------------------------------------------------------------------===//
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));
+static_assert(0.0f == LIBC_NAMESPACE::shared::floorf(0.0f));
//===----------------------------------------------------------------------===//
// Float16 Tests
@@ -39,9 +36,8 @@ static_assert(2.0f == LIBC_NAMESPACE::shared::floorf(2.9f));
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
//===----------------------------------------------------------------------===//
@@ -53,7 +49,6 @@ static_assert(3.0f16 == LIBC_NAMESPACE::shared::floorf16(3.7f16));
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
@@ -87,7 +82,4 @@ static_assert(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) {}
>From f3f56c6b8459d31e0ed4bc68eb40373bf9f0dac1 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti624 at gmail.com>
Date: Sat, 18 Apr 2026 23:33:40 +0530
Subject: [PATCH 3/3] [libc][math] Cmake depends for floor-constexp
Signed-off-by: udaykiriti <udaykiriti624 at gmail.com>
---
libc/src/__support/math/floor.h | 2 +-
libc/src/__support/math/floorbf16.h | 4 +++-
libc/src/__support/math/floorf.h | 2 +-
libc/src/__support/math/floorf128.h | 4 +++-
libc/src/__support/math/floorl.h | 4 +++-
libc/test/shared/CMakeLists.txt | 6 ++++++
libc/test/shared/shared_math_constexpr_test.cpp | 1 -
7 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/libc/src/__support/math/floor.h b/libc/src/__support/math/floor.h
index 52603f6e0671a..01177b0701f7d 100644
--- a/libc/src/__support/math/floor.h
+++ b/libc/src/__support/math/floor.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR double floor(double x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_HAS_CONSTANT_EVALUATION)
return __builtin_floor(x);
#else
diff --git a/libc/src/__support/math/floorbf16.h b/libc/src/__support/math/floorbf16.h
index a69389722361b..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 constexpr 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 82ff9ad51de49..10e298b8c8d46 100644
--- a/libc/src/__support/math/floorf.h
+++ b/libc/src/__support/math/floorf.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR float floorf(float x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_HAS_CONSTANT_EVALUATION)
return __builtin_floorf(x);
#else
diff --git a/libc/src/__support/math/floorf128.h b/libc/src/__support/math/floorf128.h
index 06b264d93e87a..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 constexpr 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/floorl.h b/libc/src/__support/math/floorl.h
index 445582ecfa070..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 constexpr 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 fc6bfc01043e9..1a013396bcf5e 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -78,7 +78,6 @@ 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)));
More information about the libc-commits
mailing list