[libc-commits] [libc] [libc][math] Speed up log2f subnormals and log10f table access (PR #223718)

via libc-commits libc-commits at lists.llvm.org
Fri Sep 25 09:51:12 PDT 2026


================
@@ -91,7 +92,13 @@ LIBC_INLINE float log2f(float x) {
       return x;
     }
     // Normalize denormal inputs.
-    xbits = FPBits(xbits.get_val() * 0x1.0p23f);
+    int biased_exponent = 32 - cpp::countl_zero(x_u);
----------------
sriramshastry wrote:

Good point. The normalization change replaces:
```
-    xbits = FPBits(xbits.get_val() * 0x1.0p23f);
+    int biased_exponent = 32 - cpp::countl_zero(x_u);
+    uint32_t mantissa =
+        (x_u << (FPBits::FRACTION_LEN + 1 - biased_exponent)) &
+        FPBits::FRACTION_MASK;
+    uint32_t normalized_x_u =
+        (static_cast<uint32_t>(biased_exponent) << FPBits::FRACTION_LEN) |
+        mantissa;
+    xbits = FPBits(normalized_x_u);
```

It avoids a potentially expensive subnormal FP multiply, but adds a leading-zero count, a dependent shift, and integer bit reconstruction. The tradeoff may be target-dependent. Is there a specific CPU or architecture with fast subnormal multiplication that you have in mind? I can benchmark both versions there.

If an immediate scope restriction is preferred, I can gate the integer path on `LIBC_TARGET_ARCH_IS_X86_64` and retain the multiply path elsewhere. I do not want to make that architecture-wide assumption without guidance.

https://github.com/llvm/llvm-project/pull/223718


More information about the libc-commits mailing list