[libc-commits] [libc] [libc][mathvec] Vectorize small-range float32 log1pf with scalar fallback (PR #246555)
via libc-commits
libc-commits at lists.llvm.org
Fri Nov 27 00:26:11 PST 2026
https://github.com/sriramshastry updated https://github.com/llvm/llvm-project/pull/246555
>From 098d557b693ecf899e896a803206f71448d0ad46 Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Fri, 27 Nov 2026 05:08:49 +0000
Subject: [PATCH] [libc][mathvec] Vectorize the small-input log1pf path
The vector entry point calls scalar log1pf for every lane. Evaluate the
existing double-precision polynomial with SIMD for normal inputs with
2^-126 <= |x| <= 2^-6. Keep scalar evaluation for hard-to-round and
out-of-range lanes, and add tests for vectors that mix both paths.
---
libc/src/__support/mathvec/log1pf.h | 55 ++++++++++++++++++++++++++-
libc/test/src/mathvec/log1pf_test.cpp | 20 ++++++++++
2 files changed, 74 insertions(+), 1 deletion(-)
diff --git a/libc/src/__support/mathvec/log1pf.h b/libc/src/__support/mathvec/log1pf.h
index f58db55d46c729..1d565eb10ab5c4 100644
--- a/libc/src/__support/mathvec/log1pf.h
+++ b/libc/src/__support/mathvec/log1pf.h
@@ -24,9 +24,62 @@ namespace LIBC_NAMESPACE_DECL {
namespace mathvec {
+namespace internal {
+
+template <size_t N, typename Mask>
+LIBC_INLINE cpp::simd<float, N> eval_small_poly(cpp::simd<float, N> x,
+ Mask fast) {
+ using Double = cpp::simd<double, N>;
+ using math::double_eval::log1pf_internal::COEFFS;
+
+ cpp::simd<float, N> poly_x = fast ? x : cpp::simd<float, N>(0.0f);
+ Double xd = cpp::simd_cast<double>(poly_x);
+ Double xsq = xd * xd;
+ Double c0 = cpp::multiply_add(xd, Double(COEFFS[1]), Double(COEFFS[0]));
+ Double c1 = cpp::multiply_add(xd, Double(COEFFS[3]), Double(COEFFS[2]));
+ Double c2 = cpp::multiply_add(xd, Double(COEFFS[5]), Double(COEFFS[4]));
+ Double x4 = xsq * xsq;
+ Double d0 = cpp::multiply_add(xsq, c1, c0);
+ Double d1 = cpp::multiply_add(xsq, Double(COEFFS[6]), c2);
+ Double d2 = cpp::multiply_add(x4, d1, d0);
+ return cpp::simd_cast<float>(cpp::multiply_add(xsq, d2, xd));
+}
+
+} // namespace internal
+
template <size_t N>
LIBC_INLINE cpp::simd<float, N> log1pf(cpp::simd<float, N> x) {
- return cpp::map(x, [](float a) { return math::log1pf(a); });
+ using UInt = cpp::simd<uint32_t, N>;
+
+ constexpr uint32_t SIGN_MASK = 0x7fff'ffffU;
+ constexpr uint32_t MIN_NORMAL_BITS = 0x0080'0000U;
+ constexpr uint32_t MAX_SMALL_BITS = 0x3c80'0000U;
+ constexpr uint32_t RANGE_SPAN = MAX_SMALL_BITS - MIN_NORMAL_BITS;
+
+ UInt bits = cpp::bit_cast<UInt>(x);
+ UInt abs_bits = bits & SIGN_MASK;
+ auto in_range = (abs_bits - MIN_NORMAL_BITS) <= RANGE_SPAN;
+
+ if (cpp::none_of(in_range)) {
+ cpp::simd<float, N> out;
+ for (size_t i = 0; i < N; ++i)
+ out[i] = math::log1pf(x[i]);
+ return out;
+ }
+
+ auto hard = (bits == 0x3540'0003U) || (bits == 0x3710'001bU) ||
+ (bits == 0xb53f'fffdU) || (bits == 0xb70f'ffe5U) ||
+ (bits == 0xbb0e'c8c4U);
+ auto fast = in_range && !hard;
+ auto result = internal::eval_small_poly(x, fast);
+
+ if (!cpp::all_of(fast)) {
+ for (size_t i = 0; i < N; ++i) {
+ if (!fast[i])
+ result[i] = math::log1pf(x[i]);
+ }
+ }
+ return result;
}
} // namespace mathvec
diff --git a/libc/test/src/mathvec/log1pf_test.cpp b/libc/test/src/mathvec/log1pf_test.cpp
index c250fc6b7f87f8..0c82c4d0d20075 100644
--- a/libc/test/src/mathvec/log1pf_test.cpp
+++ b/libc/test/src/mathvec/log1pf_test.cpp
@@ -84,6 +84,26 @@ TEST_F(LlvmLibcVecLog1pfTest, TrickyInputs) {
}
}
+TEST_F(LlvmLibcVecLog1pfTest, MixedSmallAndFallbackInputs) {
+ constexpr float small = 0x1p-8f;
+ constexpr float cutoff = 0x1p-6f;
+ constexpr float above_cutoff = 0x1.000002p-6f;
+ constexpr float hard = 0x1.800006p-21f;
+
+ // Distinct small inputs, including the SIMD path's upper boundary.
+ EXPECT_SIMD_EQ(wrap_ref<Log1pfOp>(small, -cutoff),
+ wrap_vector<Log1pfOp>(small, -cutoff));
+
+ // Mix a small input with values that force the scalar fallback.
+ constexpr float fallback_inputs[] = {above_cutoff, hard, -0.0f, -1.0f};
+ for (float other : fallback_inputs) {
+ EXPECT_SIMD_EQ(wrap_ref<Log1pfOp>(small, other),
+ wrap_vector<Log1pfOp>(small, other));
+ EXPECT_SIMD_EQ(wrap_ref<Log1pfOp>(other, small),
+ wrap_vector<Log1pfOp>(other, small));
+ }
+}
+
TEST_F(LlvmLibcVecLog1pfTest, InFloatRange) {
TEST_MATHVEC_FLOAT_RANGE(Log1pfOp);
}
More information about the libc-commits
mailing list