[libc-commits] [libc] [libc][stdfix][NFC] Use cpp::numeric_limits<T>::digits in divifx (PR #239900)

Jaeyong Lee via libc-commits libc-commits at lists.llvm.org
Fri Nov 6 21:43:35 PST 2026


https://github.com/npmania created https://github.com/llvm/llvm-project/pull/239900

Use `cpp::numeric_limits<T>::digits` in `divifx` to count value bits instead of `sizeof(IntType) * 8 - cpp::is_signed_v<IntType>`.

Also compare `INTERMEDIATE_BITS` against `cpp::numeric_limits<int64_t>::digits` (63) instead of `64` for signed types, since `INTERMEDIATE_BITS` excludes the sign bit.

Follows https://github.com/llvm/llvm-project/pull/235602#issuecomment-6417638445.

>From 3f29dfdbb29bdc4d36b79f3941f21d454faf869e Mon Sep 17 00:00:00 2001
From: Jaeyong Lee <jay at glendalab.com>
Date: Sat, 7 Nov 2026 14:36:45 +0900
Subject: [PATCH] [libc][stdfix][NFC] Use cpp::numeric_limits<T>::digits in
 divifx

---
 libc/src/__support/fixed_point/fx_bits.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/libc/src/__support/fixed_point/fx_bits.h b/libc/src/__support/fixed_point/fx_bits.h
index 47798d0f6730ab..eb7af96aa7c993 100644
--- a/libc/src/__support/fixed_point/fx_bits.h
+++ b/libc/src/__support/fixed_point/fx_bits.h
@@ -390,13 +390,16 @@ divifx(IntType n, FXType d) {
 
   constexpr int F = FXRep::FRACTION_LEN;
 
-  constexpr int INTERMEDIATE_BITS =
-      sizeof(IntType) * 8 - cpp::is_signed_v<IntType> + F;
+  constexpr int INTERMEDIATE_BITS = cpp::numeric_limits<IntType>::digits + F;
 
   using WideType = cpp::conditional_t<
       cpp::is_signed_v<IntType>,
-      cpp::conditional_t<INTERMEDIATE_BITS <= 64, int64_t, Int128>,
-      cpp::conditional_t<INTERMEDIATE_BITS <= 64, uint64_t, UInt128>>;
+      cpp::conditional_t<INTERMEDIATE_BITS <=
+                             cpp::numeric_limits<int64_t>::digits,
+                         int64_t, Int128>,
+      cpp::conditional_t<INTERMEDIATE_BITS <=
+                             cpp::numeric_limits<uint64_t>::digits,
+                         uint64_t, UInt128>>;
 
   WideType scaled_n = static_cast<WideType>(n) << F;
   WideType result = scaled_n / static_cast<WideType>(d_comp);



More information about the libc-commits mailing list