[libc-commits] [libc] [llvm] [libc][math] Add integer-only implementation for exp10f. (PR #237009)
via libc-commits
libc-commits at lists.llvm.org
Thu Nov 5 11:11:01 PST 2026
https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/237009
>From 5b8ad45d7fd123cab361d7006e20fe9b331aa39a Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Fri, 16 Oct 2026 20:41:25 +0000
Subject: [PATCH] [libc][math] Add integer-only implementation for exp10f.
- Add `src/__support/math/exp10f_integer_eval.h` using the shared `exp2_eval` and fixed-point utilities from `exp2f_integer_utils.h`.
- Select `exp10f_integer_eval` in `src/__support/math/exp10f.h` when `!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)`.
- Expose `exp10f` with explicit rounding mode parameter under `LIBC_NAMESPACE::shared::static_rounding`.
- Add smoke, unit, exhaustive, and shared math test coverage for `exp10f_integer_eval` and `static_rounding::exp10f`.
Assisted-by: Gemini is used for code size analysis, performance tuning, and test refactoring.
---
libc/shared/math/static_rounding/exp10f.h | 30 ++++
libc/shared/static_rounding_math.h | 1 +
libc/src/__support/math/CMakeLists.txt | 17 ++
libc/src/__support/math/exp10f.h | 13 +-
libc/src/__support/math/exp10f_integer_eval.h | 146 ++++++++++++++++++
libc/test/shared/shared_math_test.cpp | 31 ++++
libc/test/src/math/CMakeLists.txt | 1 +
libc/test/src/math/exhaustive/CMakeLists.txt | 3 +
libc/test/src/math/exhaustive/exp10f_test.cpp | 20 ++-
libc/test/src/math/exp10f_test.cpp | 75 ++++++---
libc/test/src/math/smoke/CMakeLists.txt | 1 +
libc/test/src/math/smoke/exp10f_test.cpp | 53 +++++--
.../llvm-project-overlay/libc/BUILD.bazel | 29 ++++
.../libc/test/src/math/BUILD.bazel | 1 +
.../libc/test/src/math/smoke/BUILD.bazel | 1 +
15 files changed, 385 insertions(+), 37 deletions(-)
create mode 100644 libc/shared/math/static_rounding/exp10f.h
create mode 100644 libc/src/__support/math/exp10f_integer_eval.h
diff --git a/libc/shared/math/static_rounding/exp10f.h b/libc/shared/math/static_rounding/exp10f.h
new file mode 100644
index 00000000000000..923e6cc67960df
--- /dev/null
+++ b/libc/shared/math/static_rounding/exp10f.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the shared statically-rounded exp10f(x) function
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_STATIC_ROUNDING_EXP10F_H
+#define LLVM_LIBC_SHARED_MATH_STATIC_ROUNDING_EXP10F_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/exp10f_integer_eval.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+namespace static_rounding {
+
+using math::static_rounding::exp10f;
+
+} // namespace static_rounding
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_STATIC_ROUNDING_EXP10F_H
diff --git a/libc/shared/static_rounding_math.h b/libc/shared/static_rounding_math.h
index 9fc00ade2be962..ee3baae081401c 100644
--- a/libc/shared/static_rounding_math.h
+++ b/libc/shared/static_rounding_math.h
@@ -15,6 +15,7 @@
#define LLVM_LIBC_SHARED_STATIC_ROUNDING_MATH_H
#include "shared/libc_common.h"
+#include "shared/math/static_rounding/exp10f.h"
#include "shared/math/static_rounding/exp2f.h"
#include "shared/math/static_rounding/expf.h"
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 7af8fba1f85a58..5efbf0044b0e64 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -4746,6 +4746,21 @@ add_header_library(
libc.src.__support.macros.properties.cpu_features
)
+add_header_library(
+ exp10f_integer_eval
+ HDRS
+ exp10f_integer_eval.h
+ DEPENDS
+ .exp2f_integer_utils
+ libc.hdr.fenv_macros
+ libc.src.__support.CPP.bit
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.frac64
+ libc.src.__support.macros.attributes
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
exp10f
HDRS
@@ -4753,9 +4768,11 @@ add_header_library(
DEPENDS
.exp10f_double_eval
.exp10f_float_eval
+ .exp10f_integer_eval
libc.src.__support.common
libc.src.__support.macros.config
libc.src.__support.macros.optimization
+ libc.src.__support.macros.properties.cpu_features
)
add_header_library(
diff --git a/libc/src/__support/math/exp10f.h b/libc/src/__support/math/exp10f.h
index 67a9d24a5a2c25..9d9517b02cb5a5 100644
--- a/libc/src/__support/math/exp10f.h
+++ b/libc/src/__support/math/exp10f.h
@@ -17,6 +17,7 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/cpu_features.h"
#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) && \
(defined(LIBC_MATH_HAS_SMALL_TABLES) || \
@@ -25,11 +26,19 @@
#include "src/__support/math/exp10f_float_eval.h"
#define LIBC_MATH_EXP10F_IMPL float_eval
-#else // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+#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/exp10f_integer_eval.h"
+#define LIBC_MATH_EXP10F_IMPL integer_eval
+
+#else
+
#include "src/__support/math/exp10f_double_eval.h"
#define LIBC_MATH_EXP10F_IMPL double_eval
-#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+#endif
namespace LIBC_NAMESPACE_DECL {
namespace math {
diff --git a/libc/src/__support/math/exp10f_integer_eval.h b/libc/src/__support/math/exp10f_integer_eval.h
new file mode 100644
index 00000000000000..5df88356639d08
--- /dev/null
+++ b/libc/src/__support/math/exp10f_integer_eval.h
@@ -0,0 +1,146 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Integer-only implementation of exp10f.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_INTEGER_EVAL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_INTEGER_EVAL_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/frac64.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/math/exp2f_integer_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+namespace static_rounding {
+
+// Constants generated by Sollya with:
+// > display = hexadecimal;
+// > round(log2(10) * 2^62, 64, RN);
+// LSB(LOG2_10) = 2^-62
+LIBC_INLINE_VAR constexpr Frac64 LOG2_10 = Frac64(0xd49a'784b'cd1b'8afe);
+
+// IEEE 754 single-precision bit representations of exact powers 10^1 .. 10^10.
+LIBC_INLINE_VAR constexpr uint32_t EXP10_POWERS[10] = {
+ 0x4120'0000U, // 10^1
+ 0x42c8'0000U, // 10^2
+ 0x447a'0000U, // 10^3
+ 0x461c'4000U, // 10^4
+ 0x47c3'5000U, // 10^5
+ 0x4974'2400U, // 10^6
+ 0x4b18'9680U, // 10^7
+ 0x4cbe'bc20U, // 10^8
+ 0x4e6e'6b28U, // 10^9
+ 0x5015'02f9U, // 10^10
+};
+
+LIBC_ALWAYS_INLINE uint32_t exp10f_bits(uint32_t x_u,
+ [[maybe_unused]] int rounding) {
+ using FPBits = fputil::FPBits<float>;
+
+ bool is_neg = (x_u >> 31) != 0;
+ uint32_t x_abs = x_u & 0x7fff'ffffU;
+ uint32_t x_e = x_abs >> FPBits::FRACTION_LEN;
+
+ // When |x| < 2^-26 (x_e <= 100) or |x| >= 32 (x_e >= 132):
+ if (LIBC_UNLIKELY(x_e - 101U >= 31U)) {
+ // |x| < 2^-26
+ if (x_e <= 100U) {
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ if (LIBC_UNLIKELY(rounding != FE_TONEAREST)) {
+ if (rounding == FE_UPWARD && static_cast<int32_t>(x_u) > 0) // x > +0.0f
+ return 0x3f80'0001U; // 0x1.000002p+0f (1.0f + 2^-23f)
+
+ if ((rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) &&
+ x_u > 0x8000'0000U) // x < -0.0f
+ return 0x3f7f'ffffU; // 0x1.fffffep-1f (1.0f - 2^-24f)
+
+ return 0x3f80'0000U; // 1.0f
+ }
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ // 10^x rounds to 1.0f (0x3f80'0000U) or 1.0f - 2^-24f (0x3f7f'ffffU) when
+ // x <= log10(1 - 2^-25) ~ -0x1.bcb7b2p-27f (0xb25e'5bd9U).
+ return 0x3f80'0000U - static_cast<uint32_t>(x_u >= 0xb25e'5bd9U);
+ }
+
+ // exp10(nan) = nan
+ if (x_abs > 0x7f80'0000U) // |x| > +inf (x is NaN)
+ return x_u | 0x0040'0000U; // quiet NaN
+
+ // x >= 0x1.344136p+5f > log10(2^128) or +inf
+ if (!is_neg && x_u >= 0x421a'209bU) {
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ if ((rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) &&
+ x_u < 0x7f80'0000U) // x < +inf
+ return 0x7f7f'ffffU; // 0x1.fffffep+127f (FLT_MAX)
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ return 0x7f80'0000U; // +inf
+ }
+
+ // x <= -0x1.693c6cp+5f < log10(2^-150) or -inf
+ if (x_u >= 0xc234'9e36U) {
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ if (rounding == FE_UPWARD && x_u < 0xff80'0000U) // x > -inf
+ return 0x0000'0001U; // 0x1.0p-149f (FLT_TRUE_MIN)
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ return 0x0000'0000U; // +0.0f
+ }
+ }
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ // Exact outputs when x = 1, 2, ..., 10 in directed rounding modes:
+ // - (x_u << 12) == 0 filters out inputs with nonzero low 20 fraction bits.
+ // - 0x3f8 is the high 12 bits of 1.0f, and 0x3f8 + 0x1a = 0x412 is 10.0f.
+ // - scaled = (8 | (d & 7)) << (d >> 3) computes 8 * x; (scaled & 7) == 0
+ // checks that the 3 fractional bits of x are zero, giving n = scaled >> 3.
+ if (rounding != FE_TONEAREST && LIBC_UNLIKELY((x_u << 12) == 0)) {
+ uint32_t d = (x_u >> 20) - 0x3f8U;
+ if (d <= 0x1aU) {
+ uint32_t scaled = (8U | (d & 7U)) << (d >> 3);
+ if ((scaled & 7U) == 0U)
+ return EXP10_POWERS[(scaled >> 3) - 1U];
+ }
+ }
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+
+ // Range reduction:
+ // k = floor(x * log2(10))
+ // x * log2(10) = k + u, with u in [0, 1)
+ // 10^x = 2^(k + u) = 2^k * 2^u
+ uint64_t u_bits = exp_b_scale(x_u, x_e, LOG2_10, 132U);
+ return exp2f_eval_bits(u_bits, is_neg, rounding);
+}
+
+// Statically rounded, no-except implementation of exp10f using integer-only
+// arithmetic.
+LIBC_ALWAYS_INLINE float exp10f(float x, [[maybe_unused]] int rounding) {
+ return cpp::bit_cast<float>(
+ exp10f_bits(cpp::bit_cast<uint32_t>(x), rounding));
+}
+
+} // namespace static_rounding
+
+namespace integer_eval {
+
+LIBC_ALWAYS_INLINE float exp10f(float x) {
+ return static_rounding::exp10f(x, FE_TONEAREST);
+}
+
+} // namespace integer_eval
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_INTEGER_EVAL_H
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 9ddce72220c8dd..844a83e02fdcb1 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -971,11 +971,17 @@ TEST(LlvmLibcSharedMathTest, StaticRounding) {
1.0f, LIBC_NAMESPACE::shared::static_rounding::expf(0.0f, FE_TONEAREST));
EXPECT_FP_EQ(
1.0f, LIBC_NAMESPACE::shared::static_rounding::exp2f(0.0f, FE_TONEAREST));
+ EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ 0.0f, FE_TONEAREST));
EXPECT_FP_EQ(
2.0f, LIBC_NAMESPACE::shared::static_rounding::exp2f(1.0f, FE_TONEAREST));
EXPECT_FP_EQ(0.5f, LIBC_NAMESPACE::shared::static_rounding::exp2f(
-1.0f, FE_TONEAREST));
+ EXPECT_FP_EQ(10.0f, LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ 1.0f, FE_TONEAREST));
+ EXPECT_FP_EQ(100.0f, LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ 2.0f, FE_TONEAREST));
EXPECT_FP_EQ(0x1.5bf0a8p+1f, LIBC_NAMESPACE::shared::static_rounding::expf(
1.0f, FE_TONEAREST));
@@ -995,6 +1001,15 @@ TEST(LlvmLibcSharedMathTest, StaticRounding) {
EXPECT_FP_EQ(0x1.6a09e6p+0f, LIBC_NAMESPACE::shared::static_rounding::exp2f(
0.5f, FE_TOWARDZERO));
+ EXPECT_FP_EQ(0x1.94c584p+1f, LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ 0.5f, FE_TONEAREST));
+ EXPECT_FP_EQ(0x1.94c584p+1f, LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ 0.5f, FE_UPWARD));
+ EXPECT_FP_EQ(0x1.94c582p+1f, LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ 0.5f, FE_DOWNWARD));
+ EXPECT_FP_EQ(0x1.94c582p+1f, LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ 0.5f, FE_TOWARDZERO));
+
int rounding_modes[] = {FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO};
for (int rm : rounding_modes) {
EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::shared::static_rounding::expf(0.0f, rm));
@@ -1004,6 +1019,14 @@ TEST(LlvmLibcSharedMathTest, StaticRounding) {
LIBC_NAMESPACE::shared::static_rounding::exp2f(0.0f, rm));
EXPECT_FP_EQ(1.0f,
LIBC_NAMESPACE::shared::static_rounding::exp2f(-0.0f, rm));
+ EXPECT_FP_EQ(1.0f,
+ LIBC_NAMESPACE::shared::static_rounding::exp10f(0.0f, rm));
+ EXPECT_FP_EQ(1.0f,
+ LIBC_NAMESPACE::shared::static_rounding::exp10f(-0.0f, rm));
+ EXPECT_FP_EQ(10.0f,
+ LIBC_NAMESPACE::shared::static_rounding::exp10f(1.0f, rm));
+ EXPECT_FP_EQ(1e10f,
+ LIBC_NAMESPACE::shared::static_rounding::exp10f(10.0f, rm));
EXPECT_TRUE(FPBits(LIBC_NAMESPACE::shared::static_rounding::expf(
FPBits::quiet_nan().get_val(), rm))
@@ -1011,6 +1034,9 @@ TEST(LlvmLibcSharedMathTest, StaticRounding) {
EXPECT_TRUE(FPBits(LIBC_NAMESPACE::shared::static_rounding::exp2f(
FPBits::quiet_nan().get_val(), rm))
.is_nan());
+ EXPECT_TRUE(FPBits(LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ FPBits::quiet_nan().get_val(), rm))
+ .is_nan());
EXPECT_FP_EQ(FPBits::inf(Sign::POS).get_val(),
LIBC_NAMESPACE::shared::static_rounding::expf(
@@ -1018,10 +1044,15 @@ TEST(LlvmLibcSharedMathTest, StaticRounding) {
EXPECT_FP_EQ(FPBits::inf(Sign::POS).get_val(),
LIBC_NAMESPACE::shared::static_rounding::exp2f(
FPBits::inf(Sign::POS).get_val(), rm));
+ EXPECT_FP_EQ(FPBits::inf(Sign::POS).get_val(),
+ LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ FPBits::inf(Sign::POS).get_val(), rm));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::static_rounding::expf(
FPBits::inf(Sign::NEG).get_val(), rm));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::static_rounding::exp2f(
FPBits::inf(Sign::NEG).get_val(), rm));
+ EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::static_rounding::exp10f(
+ FPBits::inf(Sign::NEG).get_val(), rm));
}
}
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index e87122875cfec3..67ebf4eb07ebbd 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1476,6 +1476,7 @@ add_fp_unittest(
libc.src.math.exp10f
libc.src.__support.math.exp10f_double_eval
libc.src.__support.math.exp10f_float_eval
+ libc.src.__support.math.exp10f_integer_eval
libc.src.__support.FPUtil.fp_bits
)
diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index 743779b3d51c32..a72d14d34499df 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -275,8 +275,11 @@ add_fp_unittest(
exp10f_test.cpp
DEPENDS
.exhaustive_test
+ .exhaustive_test_static_rounding
libc.src.math.exp10f
+ libc.src.__support.math.exp10f_double_eval
libc.src.__support.math.exp10f_float_eval
+ libc.src.__support.math.exp10f_integer_eval
libc.src.__support.FPUtil.fp_bits
LINK_LIBRARIES
-lpthread
diff --git a/libc/test/src/math/exhaustive/exp10f_test.cpp b/libc/test/src/math/exhaustive/exp10f_test.cpp
index 150e3d0812b657..ce94bde611578f 100644
--- a/libc/test/src/math/exhaustive/exp10f_test.cpp
+++ b/libc/test/src/math/exhaustive/exp10f_test.cpp
@@ -6,9 +6,12 @@
//
//===----------------------------------------------------------------------===//
+#include "src/__support/math/exp10f_double_eval.h"
#include "src/__support/math/exp10f_float_eval.h"
+#include "src/__support/math/exp10f_integer_eval.h"
#include "src/math/exp10f.h"
#include "test/src/math/exhaustive/exhaustive_test.h"
+#include "test/src/math/exhaustive/exhaustive_test_static_rounding.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
@@ -41,7 +44,7 @@ static float exp10f_float_eval(float x) {
using LlvmLibcExp10fFloatExhaustiveTest =
LlvmLibcUnaryOpAgainstBaselineExhaustiveMathTest<
- float, LIBC_NAMESPACE::exp10f, exp10f_float_eval, 1>;
+ float, LIBC_NAMESPACE::math::double_eval::exp10f, exp10f_float_eval, 1>;
TEST_F(LlvmLibcExp10fFloatExhaustiveTest, PositiveRange) {
test_full_range(mpfr::RoundingMode::Nearest, POS_START, POS_STOP);
@@ -50,3 +53,18 @@ TEST_F(LlvmLibcExp10fFloatExhaustiveTest, PositiveRange) {
TEST_F(LlvmLibcExp10fFloatExhaustiveTest, NegativeRange) {
test_full_range(mpfr::RoundingMode::Nearest, NEG_START, NEG_STOP);
}
+
+// Statically rounded implementation: tested against double_eval across all
+// roundings.
+using LlvmLibcExp10fStaticRoundingExhaustiveTest =
+ LlvmLibcStaticallyRoundedUnaryOpExhaustiveMathTest<
+ float, LIBC_NAMESPACE::math::double_eval::exp10f,
+ LIBC_NAMESPACE::math::static_rounding::exp10f>;
+
+TEST_F(LlvmLibcExp10fStaticRoundingExhaustiveTest, PositiveRange) {
+ test_full_range_all_roundings(POS_START, POS_STOP);
+}
+
+TEST_F(LlvmLibcExp10fStaticRoundingExhaustiveTest, NegativeRange) {
+ test_full_range_all_roundings(NEG_START, NEG_STOP);
+}
diff --git a/libc/test/src/math/exp10f_test.cpp b/libc/test/src/math/exp10f_test.cpp
index 763c61a1c66c91..fdff6a4deaf5c0 100644
--- a/libc/test/src/math/exp10f_test.cpp
+++ b/libc/test/src/math/exp10f_test.cpp
@@ -20,6 +20,7 @@
#include "src/__support/macros/optimization.h"
#include "src/__support/math/exp10f_double_eval.h"
#include "src/__support/math/exp10f_float_eval.h"
+#include "src/__support/math/exp10f_integer_eval.h"
#include "src/math/exp10f.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
@@ -29,27 +30,33 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
class Exp10fTest : public LIBC_NAMESPACE::testing::FPTest<float> {
public:
- void test_special_numbers(float (*func)(float)) {
+ void test_special_numbers(float (*func)(float), bool check_errno = true) {
EXPECT_FP_EQ(aNaN, func(aNaN));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ(inf, func(inf));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ(0.0f, func(neg_inf));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ(1.0f, func(0.0f));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ(1.0f, func(-0.0f));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
}
void test_overflow(float (*func)(float),
bool check_exception_and_errno = true) {
constexpr float VALUES[] = {
FPBits(0x7f7fffffU).get_val(),
+ FPBits(0x421a209bU).get_val(),
FPBits(0x43000000U).get_val(),
FPBits(0x43000001U).get_val(),
};
@@ -74,20 +81,27 @@ class Exp10fTest : public LIBC_NAMESPACE::testing::FPTest<float> {
EXPECT_FP_EQ(0.0f, func(FPBits(0xff7fffffU).get_val()));
}
- constexpr float VALUES[] = {
- FPBits(0xc2cffff8U).get_val(),
- FPBits(0xc2d00008U).get_val(),
+ struct TestCase {
+ float x;
+ int expected_errno;
+ } cases[] = {
+ {FPBits(0xc2349e35U).get_val(), 0},
+ {FPBits(0xc2349e36U).get_val(), ERANGE},
+ {FPBits(0xc2cffff8U).get_val(), ERANGE},
+ {FPBits(0xc2d00008U).get_val(), ERANGE},
};
- for (float x : VALUES) {
+ for (const auto &c : cases) {
+ libc_errno = 0;
if (all_rounding) {
- EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x, func(x),
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, c.x, func(c.x),
ulp_tolerance);
} else {
- EXPECT_MPFR_MATCH(mpfr::Operation::Exp10, x, func(x), ulp_tolerance);
+ EXPECT_MPFR_MATCH(mpfr::Operation::Exp10, c.x, func(c.x),
+ ulp_tolerance);
}
if (check_exception_and_errno) {
- EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_MATH_ERRNO(c.expected_errno);
}
}
}
@@ -157,25 +171,44 @@ class Exp10fTest : public LIBC_NAMESPACE::testing::FPTest<float> {
}
};
-#define LIST_EXP10F_TESTS(suffix, func, ulp_tolerance, all_rounding) \
+#define LIST_EXP10F_TESTS(suffix, func, ulp_tolerance, all_rounding, \
+ check_exception_and_errno, check_errno) \
using LlvmLibcExp10fTest##suffix = Exp10fTest; \
TEST_F(LlvmLibcExp10fTest##suffix, SpecialNumbers) { \
- test_special_numbers(&func); \
+ test_special_numbers(&func, check_errno); \
+ } \
+ TEST_F(LlvmLibcExp10fTest##suffix, Overflow) { \
+ test_overflow(&func, check_exception_and_errno); \
} \
- TEST_F(LlvmLibcExp10fTest##suffix, Overflow) { test_overflow(&func); } \
TEST_F(LlvmLibcExp10fTest##suffix, Underflow) { \
- test_underflow(&func, ulp_tolerance, all_rounding); \
+ test_underflow(&func, ulp_tolerance, all_rounding, \
+ check_exception_and_errno); \
} \
TEST_F(LlvmLibcExp10fTest##suffix, TrickyInputs) { \
test_tricky_inputs(&func, ulp_tolerance, all_rounding); \
} \
TEST_F(LlvmLibcExp10fTest##suffix, InFloatRange) { \
- test_in_range(&func, ulp_tolerance, all_rounding); \
+ test_in_range(&func, ulp_tolerance, all_rounding, check_errno); \
}
LIST_EXP10F_TESTS(Default, LIBC_NAMESPACE::exp10f, /*ulp_tolerance=*/0.5,
- /*all_rounding=*/true)
+ /*all_rounding=*/true, /*check_exception_and_errno=*/true,
+ /*check_errno=*/true)
LIST_EXP10F_TESTS(DoubleEval, LIBC_NAMESPACE::math::double_eval::exp10f,
- /*ulp_tolerance=*/0.5, /*all_rounding=*/true)
+ /*ulp_tolerance=*/0.5, /*all_rounding=*/true,
+ /*check_exception_and_errno=*/true, /*check_errno=*/true)
LIST_EXP10F_TESTS(FloatEval, LIBC_NAMESPACE::math::float_eval::exp10f,
- /*ulp_tolerance=*/1.5, /*all_rounding=*/false)
+ /*ulp_tolerance=*/1.5, /*all_rounding=*/false,
+ /*check_exception_and_errno=*/true, /*check_errno=*/true)
+LIST_EXP10F_TESTS(IntegerEval, LIBC_NAMESPACE::math::integer_eval::exp10f,
+ /*ulp_tolerance=*/0.5, /*all_rounding=*/false,
+ /*check_exception_and_errno=*/false, /*check_errno=*/false)
+
+static float exp10f_static_rounding(float x) {
+ return LIBC_NAMESPACE::math::static_rounding::exp10f(
+ x, LIBC_NAMESPACE::fputil::quick_get_round());
+}
+
+LIST_EXP10F_TESTS(StaticRounding, exp10f_static_rounding,
+ /*ulp_tolerance=*/0.5, /*all_rounding=*/true,
+ /*check_exception_and_errno=*/false, /*check_errno=*/false)
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 9bb2f0ccd5dce2..2d4ede1c9cea8b 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1613,6 +1613,7 @@ add_fp_unittest(
libc.src.math.exp10f
libc.src.__support.math.exp10f_double_eval
libc.src.__support.math.exp10f_float_eval
+ libc.src.__support.math.exp10f_integer_eval
libc.src.__support.FPUtil.fp_bits
)
diff --git a/libc/test/src/math/smoke/exp10f_test.cpp b/libc/test/src/math/smoke/exp10f_test.cpp
index 00c65dcf6acb96..eb5559dfdb5749 100644
--- a/libc/test/src/math/smoke/exp10f_test.cpp
+++ b/libc/test/src/math/smoke/exp10f_test.cpp
@@ -18,6 +18,7 @@
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/math/exp10f_double_eval.h"
#include "src/__support/math/exp10f_float_eval.h"
+#include "src/__support/math/exp10f_integer_eval.h"
#include "src/math/exp10f.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
@@ -25,28 +26,35 @@
class Exp10fTest : public LIBC_NAMESPACE::testing::FPTest<float> {
public:
void test_special_numbers(float (*func)(float),
- bool check_snan_invalid = false) {
+ bool check_snan_invalid = false,
+ bool check_errno = true) {
if (check_snan_invalid) {
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(sNaN), FE_INVALID);
} else {
EXPECT_FP_EQ(aNaN, func(sNaN));
}
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(aNaN));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(inf, func(inf));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(0.0f, func(neg_inf));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(1.0f, func(0.0f));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(1.0f, func(-0.0f));
- EXPECT_MATH_ERRNO(0);
+ if (check_errno)
+ EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(10.0f, func(1.0f));
EXPECT_FP_EQ_ALL_ROUNDING(100.0f, func(2.0f));
@@ -57,6 +65,7 @@ class Exp10fTest : public LIBC_NAMESPACE::testing::FPTest<float> {
bool check_exception_and_errno = true) {
constexpr float VALUES[] = {
FPBits(0x7f7fffffU).get_val(),
+ FPBits(0x421a209bU).get_val(),
FPBits(0x43000000U).get_val(),
FPBits(0x43000001U).get_val(),
};
@@ -97,16 +106,34 @@ class Exp10fTest : public LIBC_NAMESPACE::testing::FPTest<float> {
#define LIST_EXP10F_FTZ_DAZ_TESTS(suffix, func)
#endif // LIBC_TEST_FTZ_DAZ
-#define LIST_EXP10F_TESTS(suffix, func, check_snan_invalid) \
+#define LIST_EXP10F_TESTS(suffix, func, check_snan_invalid, \
+ check_exception_and_errno, check_errno) \
using LlvmLibcExp10fTest##suffix = Exp10fTest; \
TEST_F(LlvmLibcExp10fTest##suffix, SpecialNumbers) { \
- test_special_numbers(&func, check_snan_invalid); \
+ test_special_numbers(&func, check_snan_invalid, check_errno); \
+ } \
+ TEST_F(LlvmLibcExp10fTest##suffix, Overflow) { \
+ test_overflow(&func, check_exception_and_errno); \
} \
- TEST_F(LlvmLibcExp10fTest##suffix, Overflow) { test_overflow(&func); } \
LIST_EXP10F_FTZ_DAZ_TESTS(suffix, func)
-LIST_EXP10F_TESTS(Default, LIBC_NAMESPACE::exp10f, /*check_snan_invalid=*/true)
+LIST_EXP10F_TESTS(Default, LIBC_NAMESPACE::exp10f, /*check_snan_invalid=*/true,
+ /*check_exception_and_errno=*/true, /*check_errno=*/true)
LIST_EXP10F_TESTS(DoubleEval, LIBC_NAMESPACE::math::double_eval::exp10f,
- /*check_snan_invalid=*/false)
+ /*check_snan_invalid=*/false,
+ /*check_exception_and_errno=*/true, /*check_errno=*/true)
LIST_EXP10F_TESTS(FloatEval, LIBC_NAMESPACE::math::float_eval::exp10f,
- /*check_snan_invalid=*/false)
+ /*check_snan_invalid=*/false,
+ /*check_exception_and_errno=*/true, /*check_errno=*/true)
+LIST_EXP10F_TESTS(IntegerEval, LIBC_NAMESPACE::math::integer_eval::exp10f,
+ /*check_snan_invalid=*/false,
+ /*check_exception_and_errno=*/false, /*check_errno=*/false)
+
+static float exp10f_static_rounding(float x) {
+ return LIBC_NAMESPACE::math::static_rounding::exp10f(
+ x, LIBC_NAMESPACE::fputil::quick_get_round());
+}
+
+LIST_EXP10F_TESTS(StaticRounding, exp10f_static_rounding,
+ /*check_snan_invalid=*/false,
+ /*check_exception_and_errno=*/false, /*check_errno=*/false)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 750e73aa4ed919..b0dbe05e3b2411 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -11726,6 +11726,32 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_exp10f_integer_eval",
+ hdrs = ["src/__support/math/exp10f_integer_eval.h"],
+ deps = [
+ ":__support_cpp_bit",
+ ":__support_fputil_fp_bits",
+ ":__support_frac64",
+ ":__support_macros_attributes",
+ ":__support_macros_config",
+ ":__support_macros_optimization",
+ ":__support_math_exp2f_integer_utils",
+ ":hdr_fenv_macros",
+ ],
+)
+
+libc_support_library(
+ name = "__support_math_static_rounding_exp10f",
+ hdrs = ["shared/math/static_rounding/exp10f.h"],
+ deps = [
+ ":__support_common",
+ ":__support_macros_config",
+ ":__support_math_exp10f_integer_eval",
+ ":shared_libc_common",
+ ],
+)
+
libc_support_library(
name = "__support_math_exp10f",
hdrs = ["src/__support/math/exp10f.h"],
@@ -11733,8 +11759,10 @@ libc_support_library(
":__support_common",
":__support_macros_config",
":__support_macros_optimization",
+ ":__support_macros_properties_cpu_features",
":__support_math_exp10f_double_eval",
":__support_math_exp10f_float_eval",
+ ":__support_math_exp10f_integer_eval",
],
)
@@ -14062,6 +14090,7 @@ libc_math_function(
name = "exp10f",
additional_deps = [
":__support_math_exp10f",
+ ":__support_math_static_rounding_exp10f",
],
)
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
index 4e641c872edd2d..6a1e0fb49aaa05 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
@@ -286,6 +286,7 @@ math_mpfr_test(
deps = [
"//libc:__support_math_exp10f_double_eval",
"//libc:__support_math_exp10f_float_eval",
+ "//libc:__support_math_exp10f_integer_eval",
],
)
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
index c43d3f272cfe06..e0421aa0b7fe40 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
@@ -620,6 +620,7 @@ math_test(
deps = [
"//libc:__support_math_exp10f_double_eval",
"//libc:__support_math_exp10f_float_eval",
+ "//libc:__support_math_exp10f_integer_eval",
],
)
More information about the libc-commits
mailing list