[libc-commits] [libc] [libc][mathvec] Vectorize small-range float32 log1pf with scalar fall (PR #246555)

via libc-commits libc-commits at lists.llvm.org
Thu Nov 26 21:16:01 PST 2026


https://github.com/sriramshastry created https://github.com/llvm/llvm-project/pull/246555

## Summary

Optimizes `mathvec::log1pf` for single-precision (`float`) SIMD vectors using a vectorized double-precision polynomial fast path for normal small-magnitude inputs while preserving exact `math::log1pf` behavior for out-of-range and hard-to-round lanes, and adds unit test coverage for mixed small and fallback inputs.

## Changes

- `libc/src/__support/mathvec/log1pf.h`:
  - Compute `abs_bits` via bit-cast and sign-bit mask and test range membership `[MIN_NORMAL_BITS, MAX_SMALL_BITS]` (`[0x0080'0000U, 0x3c80'0000U]`).
  - Early-out to scalar `math::log1pf` per lane when no lanes are in range (`cpp::none_of(in_range)`).
  - Exclude hard-to-round float32 bit patterns (`0x3540'0003U`, `0x3710'001bU`, `0xb53f'fffdU`, `0xb70f'ffe5U`, `0xbb0e'c8c4U`) from the fast path, evaluate the polynomial approximation in `internal::eval_small_poly`, and fall back to scalar `math::log1pf` for non-fast lanes.
- `libc/test/src/mathvec/log1pf_test.cpp`:
  - Add `LlvmLibcVecLog1pfTest.MixedSmallAndFallbackInputs` covering boundary values and mixed lanes combining small inputs with scalar-fallback inputs.

## Verification

- Built and ran `log1pf` unit and exhaustive tests (`PASS` across all variants).
- Verified `0` ULP mismatches (`max_ulp=0`, `non_zero_ulp=0`) across `67,372,048` float32 inputs (subnormals, small-range boundaries, hard-to-round/special values, and full-range strided sweep) against `math::log1pf`.
- Benchmarked on x86_64 AVX2+FMA (31 interleaved runs per case):
  - `all small`: ~2.89× median speedup (`0.545 ns` vs `1.575 ns`)
  - `one fallback lane`: ~1.70× median speedup (`0.927 ns` vs `1.580 ns`)
  - `all outside`: within ~3.8% of scalar baseline (`2.231 ns` vs `2.149 ns` median)

>From 45de270fc391e2e19670eddb56f34603c006b731 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 small-range float32 log1pf with
 scalar fallback

Add a SIMD fast path for mathvec::log1pf on normal float32 inputs in

[0x00800000, 0x3c800000] (excluding hard-to-round bit patterns), evaluate

the polynomial approximation across lanes with scalar fallback, and add

mixed small/fallback unit tests.

Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
 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