[libc-commits] [libc] [libc][math] Move CR/RN expf to float_eval_cr_rn namespace (PR #237614)
Leandro Lacerda via libc-commits
libc-commits at lists.llvm.org
Sat Oct 31 12:26:38 PDT 2026
https://github.com/leandrolcampos updated https://github.com/llvm/llvm-project/pull/237614
>From a11bf971295dcae307a2e80c66eb712cdc1bf229 Mon Sep 17 00:00:00 2001
From: Leandro Augusto Lacerda Campos <leandrolcampos at yahoo.com.br>
Date: Sat, 31 Oct 2026 15:59:36 -0300
Subject: [PATCH 1/3] Add missing CMake target for exp_exceptions
Signed-off-by: Leandro Augusto Lacerda Campos <leandrolcampos at yahoo.com.br>
---
libc/src/__support/math/CMakeLists.txt | 2 ++
libc/src/__support/math/check/CMakeLists.txt | 10 ++++++++++
2 files changed, 12 insertions(+)
create mode 100644 libc/src/__support/math/check/CMakeLists.txt
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index da3a3fcb3dd366..bf87c405cc0814 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1,3 +1,5 @@
+add_subdirectory(check)
+
add_header_library(
acos
HDRS
diff --git a/libc/src/__support/math/check/CMakeLists.txt b/libc/src/__support/math/check/CMakeLists.txt
new file mode 100644
index 00000000000000..31d901f900a8a5
--- /dev/null
+++ b/libc/src/__support/math/check/CMakeLists.txt
@@ -0,0 +1,10 @@
+add_header_library(
+ exp_exceptions
+ HDRS
+ exp_exceptions.h
+ DEPENDS
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
>From 594f884bdc572df6f69296bc329f5dcc83006f61 Mon Sep 17 00:00:00 2001
From: Leandro Augusto Lacerda Campos <leandrolcampos at yahoo.com.br>
Date: Sat, 31 Oct 2026 16:06:32 -0300
Subject: [PATCH 2/3] Move correctly rounded expf to float_eval_cr_rn namespace
Signed-off-by: Leandro Augusto Lacerda Campos <leandrolcampos at yahoo.com.br>
---
libc/src/__support/math/expf.h | 10 +++++-----
libc/src/__support/math/expf_float_eval.h | 12 ++++++++----
libc/src/__support/math/expf_float_utils.h | 8 ++++----
libc/test/src/math/exhaustive/expf_test.cpp | 7 ++++---
libc/test/src/math/expf_test.cpp | 2 +-
libc/test/src/math/smoke/expf_test.cpp | 2 +-
6 files changed, 23 insertions(+), 18 deletions(-)
diff --git a/libc/src/__support/math/expf.h b/libc/src/__support/math/expf.h
index d53d19cfc78475..c60450c1f6f558 100644
--- a/libc/src/__support/math/expf.h
+++ b/libc/src/__support/math/expf.h
@@ -27,33 +27,33 @@
defined(LIBC_MATH_HAS_NO_EXCEPT) && defined(LIBC_MATH_HAS_NO_ERRNO)
#include "src/__support/math/expf_float_eval.h"
-#define LIBC_MATH_EXPF_IMPL float_eval::cr_expf_rn
+#define LIBC_MATH_EXPF_IMPL float_eval_cr_rn
#elif defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) && \
(defined(LIBC_MATH_HAS_SMALL_TABLES) || \
defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT))
#include "src/__support/math/expf_float_eval.h"
-#define LIBC_MATH_EXPF_IMPL float_eval::expf
+#define LIBC_MATH_EXPF_IMPL float_eval
#elif !defined(LIBC_TARGET_CPU_HAS_FPU_DOUBLE) && \
defined(LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY) && \
defined(LIBC_MATH_HAS_NO_EXCEPT) && defined(LIBC_MATH_HAS_NO_ERRNO)
#include "src/__support/math/expf_integer_eval.h"
-#define LIBC_MATH_EXPF_IMPL integer_eval::expf
+#define LIBC_MATH_EXPF_IMPL integer_eval
#else
#include "src/__support/math/expf_double_eval.h"
-#define LIBC_MATH_EXPF_IMPL double_eval::expf
+#define LIBC_MATH_EXPF_IMPL double_eval
#endif
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float expf(float x) { return LIBC_MATH_EXPF_IMPL(x); }
+using LIBC_MATH_EXPF_IMPL::expf;
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/expf_float_eval.h b/libc/src/__support/math/expf_float_eval.h
index a9f49751ac2495..5df8f80ff15540 100644
--- a/libc/src/__support/math/expf_float_eval.h
+++ b/libc/src/__support/math/expf_float_eval.h
@@ -123,10 +123,14 @@ LIBC_INLINE float expf(float x) {
return exp2f_eval(u, k);
}
+} // namespace float_eval
+
+namespace float_eval_cr_rn {
+
// Designed for GPU performance. Correctly rounded only in round-to-nearest,
// ties-to-even mode. Requires single-precision FMA. Does not support errno
// or floating-point exception reporting.
-LIBC_INLINE float cr_expf_rn(float x) {
+LIBC_INLINE float expf(float x) {
using FPBits = fputil::FPBits<float>;
using FPBounds = math::check::exp_internal::Bounds<float>;
@@ -179,10 +183,10 @@ LIBC_INLINE float cr_expf_rn(float x) {
u.lo += u_lo.lo;
// Polynomial approximation: e^u
- fputil::FloatFloat exp_u = cr_expf_rn_internal::exp_eval(u);
+ fputil::FloatFloat exp_u = expf_internal::exp_eval(u);
// Reconstruction: 2^k * e^u
- float res = cr_expf_rn_internal::scale_result(exp_u, k, produces_subnormal);
+ float res = expf_internal::scale_result(exp_u, k, produces_subnormal);
// Special cases
constexpr float INF = FPBits::inf().get_val();
@@ -194,7 +198,7 @@ LIBC_INLINE float cr_expf_rn(float x) {
return res;
}
-} // namespace float_eval
+} // namespace float_eval_cr_rn
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/expf_float_utils.h b/libc/src/__support/math/expf_float_utils.h
index d65abda27af380..ac6371225a20ac 100644
--- a/libc/src/__support/math/expf_float_utils.h
+++ b/libc/src/__support/math/expf_float_utils.h
@@ -23,9 +23,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-namespace float_eval {
+namespace float_eval_cr_rn {
-namespace cr_expf_rn_internal {
+namespace expf_internal {
using fputil::FloatFloat;
@@ -140,9 +140,9 @@ LIBC_INLINE float scale_result(const FloatFloat &exp_u, int k,
return FPBits((res_u & res_mask) + exponent_adj).get_val();
}
-} // namespace cr_expf_rn_internal
+} // namespace expf_internal
-} // namespace float_eval
+} // namespace float_eval_cr_rn
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/exhaustive/expf_test.cpp b/libc/test/src/math/exhaustive/expf_test.cpp
index c299eaedee9d84..c547358a39d088 100644
--- a/libc/test/src/math/exhaustive/expf_test.cpp
+++ b/libc/test/src/math/exhaustive/expf_test.cpp
@@ -58,13 +58,14 @@ TEST_F(LlvmLibcExpfFloatExhaustiveTest, NegativeRange) {
// Float-eval implementation correctly rounded only in round-to-nearest,
// ties-to-even mode: tested against the double-eval version in that mode
// with a 0 ULP bound.
-static float cr_expf_rn(float x) {
- return LIBC_NAMESPACE::math::float_eval::cr_expf_rn(x);
+static float expf_float_eval_cr_rn(float x) {
+ return LIBC_NAMESPACE::math::float_eval_cr_rn::expf(x);
}
using LlvmLibcExpfFloatCrRnExhaustiveTest =
LlvmLibcUnaryOpAgainstBaselineExhaustiveMathTest<
- float, LIBC_NAMESPACE::math::double_eval::expf, cr_expf_rn, 0>;
+ float, LIBC_NAMESPACE::math::double_eval::expf,
+ expf_float_eval_cr_rn, 0>;
TEST_F(LlvmLibcExpfFloatCrRnExhaustiveTest, PositiveRange) {
test_full_range(mpfr::RoundingMode::Nearest, POS_START, POS_STOP);
diff --git a/libc/test/src/math/expf_test.cpp b/libc/test/src/math/expf_test.cpp
index e9a2d1dd230929..56ee5fe9f3b834 100644
--- a/libc/test/src/math/expf_test.cpp
+++ b/libc/test/src/math/expf_test.cpp
@@ -182,7 +182,7 @@ LIST_EXPF_TESTS(FloatEval, LIBC_NAMESPACE::math::float_eval::expf,
/*ulp_tolerance=*/1.5, /*all_rounding=*/false,
/*check_exception_and_errno=*/true, /*check_errno=*/true)
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-LIST_EXPF_TESTS(FloatEvalCrRn, LIBC_NAMESPACE::math::float_eval::cr_expf_rn,
+LIST_EXPF_TESTS(FloatEvalCrRn, LIBC_NAMESPACE::math::float_eval_cr_rn::expf,
/*ulp_tolerance=*/TOLERANCE + 0.5, /*all_rounding=*/false,
/*check_exception_and_errno=*/false, /*check_errno=*/false)
#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
diff --git a/libc/test/src/math/smoke/expf_test.cpp b/libc/test/src/math/smoke/expf_test.cpp
index 49361ed716d942..85a40eb4e81b13 100644
--- a/libc/test/src/math/smoke/expf_test.cpp
+++ b/libc/test/src/math/smoke/expf_test.cpp
@@ -122,7 +122,7 @@ LIST_EXPF_TESTS(FloatEval, LIBC_NAMESPACE::math::float_eval::expf,
/*check_snan_invalid=*/false,
/*check_exception_and_errno=*/true, /*check_errno=*/true)
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-LIST_EXPF_TESTS(FloatEvalCrRn, LIBC_NAMESPACE::math::float_eval::cr_expf_rn,
+LIST_EXPF_TESTS(FloatEvalCrRn, LIBC_NAMESPACE::math::float_eval_cr_rn::expf,
/*check_snan_invalid=*/false,
/*check_exception_and_errno=*/false, /*check_errno=*/false)
#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
>From 05e049458268d2d6ddc402aa25c4b6b7326df453 Mon Sep 17 00:00:00 2001
From: Leandro Augusto Lacerda Campos <leandrolcampos at yahoo.com.br>
Date: Sat, 31 Oct 2026 16:26:20 -0300
Subject: [PATCH 3/3] Fix formatting in expf exhaustive test
Signed-off-by: Leandro Augusto Lacerda Campos <leandrolcampos at yahoo.com.br>
---
libc/test/src/math/exhaustive/expf_test.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/test/src/math/exhaustive/expf_test.cpp b/libc/test/src/math/exhaustive/expf_test.cpp
index c547358a39d088..04bfaaf1b5a1e8 100644
--- a/libc/test/src/math/exhaustive/expf_test.cpp
+++ b/libc/test/src/math/exhaustive/expf_test.cpp
@@ -64,8 +64,8 @@ static float expf_float_eval_cr_rn(float x) {
using LlvmLibcExpfFloatCrRnExhaustiveTest =
LlvmLibcUnaryOpAgainstBaselineExhaustiveMathTest<
- float, LIBC_NAMESPACE::math::double_eval::expf,
- expf_float_eval_cr_rn, 0>;
+ float, LIBC_NAMESPACE::math::double_eval::expf, expf_float_eval_cr_rn,
+ 0>;
TEST_F(LlvmLibcExpfFloatCrRnExhaustiveTest, PositiveRange) {
test_full_range(mpfr::RoundingMode::Nearest, POS_START, POS_STOP);
More information about the libc-commits
mailing list