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

via libc-commits libc-commits at lists.llvm.org
Sat Oct 10 06:37:27 PDT 2026


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

>From 1ac1386a0ad1f0fdb4c52c03eaba156fd87c4d91 Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Sat, 10 Oct 2026 13:36:48 +0000
Subject: [PATCH] [libc][math] Normalize log2f subnormals using integer
 conversion

Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
 libc/src/__support/math/log2f.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

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