[libc-commits] [libc] 8f7818a - [libc][math] Speed up log2f subnormal normalization (#223718)

via libc-commits libc-commits at lists.llvm.org
Sat Oct 10 12:20:41 PDT 2026


Author: sriramshastry
Date: 2026-10-10T15:20:36-04:00
New Revision: 8f7818a8d9273992f08341da1f8d0b7366dd7772

URL: https://github.com/llvm/llvm-project/commit/8f7818a8d9273992f08341da1f8d0b7366dd7772
DIFF: https://github.com/llvm/llvm-project/commit/8f7818a8d9273992f08341da1f8d0b7366dd7772.diff

LOG: [libc][math] Speed up log2f subnormal normalization (#223718)

## Summary

Normalize positive subnormal inputs in `log2f` by converting the integer
significand to float exactly and accounting for the `2^-149` scale.
This replaces the existing floating-point multiplication by `2^23`.

Replacement prepared on main at
`681b57195f495c346c1d15229334bb0ffcbba957`.
The remaining diff changes only `libc/src/__support/math/log2f.h`.

The `log10f` table relocation is already covered upstream by #229173,
merged as `636bedc3b3c00266a95f139c4b79cb8b940c1a36`. It moved `LOG10_R`
and `LOG10_2` to namespace-scope inline constexpr storage. This update
preserves that implementation and drops our redundant table change,
leaving only the `log2f` subnormal optimization.

## Validation

- Clang 18 generic and native x86-64 builds passed 33,954,500
differential
comparisons each: 67,909,000 records total, with no mismatches in result
  bits, errno, or floating-point exception flags.
- Each build checks every positive subnormal in all four rounding modes,
plus explicit boundary/special values and deterministic sampled inputs.
A subset checks preservation of pre-existing errno and exception state.
- Fresh LLVM libc log2f/log10f smoke suites: 8/8 tests passed.

These are comparisons with the matching upstream implementation, not an
independent MPFR accuracy proof or cross-platform certification.

## Historical Performance

The earlier PR reported 49.68 ns saved for the log2f denormal range.
That measurement predates this rebase; it has not been rerun on this
base.
No new performance claim is made for this update. The earlier log10f
timings no longer describe a change in this PR.

Signed-off-by: sriramshastry <sriramshastry at gmail.com>

Added: 
    

Modified: 
    libc/src/__support/math/log2f.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/math/log2f.h b/libc/src/__support/math/log2f.h
index 138dd9fa29e03..5e7fb902ef996 100644
--- a/libc/src/__support/math/log2f.h
+++ b/libc/src/__support/math/log2f.h
@@ -25,8 +25,8 @@
 
 // Step 1 - Range reduction:
 //   For x = 2^m * 1.mant, log2(x) = m + log2(1.m)
-//   If x is denormal, we normalize it by multiplying x by 2^23 and subtracting
-//   m by 23.
+//   If x is denormal, convert its integer significand to float exactly and
+//   account for the 2^-149 subnormal scale in m.
 
 // Step 2 - Another range reduction:
 //   To compute log(1.mant), let f be the highest 8 bits including the hidden
@@ -91,8 +91,8 @@ LIBC_INLINE float log2f(float x) {
       return x;
     }
     // Normalize denormal inputs.
-    xbits = FPBits(xbits.get_val() * 0x1.0p23f);
-    m -= 23;
+    xbits = FPBits(static_cast<float>(static_cast<int32_t>(x_u)));
+    m -= 149;
   }
 
   m += xbits.get_biased_exponent();


        


More information about the libc-commits mailing list