[libc-commits] [libc] [libc][stdfix] Fix lrdivi saturation for huge quotients (PR #241213)

Robin Bisht via libc-commits libc-commits at lists.llvm.org
Wed Nov 11 02:49:52 PST 2026


https://github.com/robinbishtt created https://github.com/llvm/llvm-project/pull/241213

Fixes #241209

`fixed_point::fxdivi` only did its early saturation check when
`OutRep::INTEGRAL_LEN > 0`. `long fract` has no integral bits, so it went to
the `WF <= F` branch, where the 128-bit quotient is cast to the 64-bit
`WideStorage` before it is compared with MAX/MIN. For a huge quotient the sign
of the saturated result then depended on the truncated bits, e.g.
`lrdivi(1L << 62, 3)` returned MIN instead of MAX.

Do the early saturation check for all output types `MAX_UNITS` is 1 for types
without integral bits.

Add checks to `FxDiviTest.h` for 64-bit operands a huge numerator must
saturate according to the sign of the quotient.

>From f2ea0a5e6d445bc367badedf2321d3dfa513dee6 Mon Sep 17 00:00:00 2001
From: robinbishtt <robinbisht49 at gmail.com>
Date: Wed, 11 Nov 2026 11:10:01 +0530
Subject: [PATCH] [libc][stdfix] Fix lrdivi saturation for huge quotients

fixed_point::fxdivi only ran its early saturation check when
OutRep::INTEGRAL_LEN > 0. For long fract (no integral bits) the 128-bit
quotient was cast to the 64-bit wide storage before it was compared with
MAX/MIN, so a huge quotient could saturate to the wrong sign, e.g.
lrdivi(1L << 62, 3) returned -1.0.
---
 libc/src/__support/fixed_point/fx_bits.h | 11 ++++-------
 libc/test/src/stdfix/FxDiviTest.h        |  9 +++++++++
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/libc/src/__support/fixed_point/fx_bits.h b/libc/src/__support/fixed_point/fx_bits.h
index eb7af96aa7c993..ba6a950ce386f8 100644
--- a/libc/src/__support/fixed_point/fx_bits.h
+++ b/libc/src/__support/fixed_point/fx_bits.h
@@ -275,14 +275,11 @@ fxdivi(IntType n, IntType d) {
     d_mag = d;
   }
 
-  if constexpr (OutRep::INTEGRAL_LEN > 0) {
-    constexpr UInt128 MAX_UNITS = static_cast<UInt128>(1)
-                                  << OutRep::INTEGRAL_LEN;
+  constexpr UInt128 MAX_UNITS = static_cast<UInt128>(1) << OutRep::INTEGRAL_LEN;
 
-    if (LIBC_UNLIKELY(static_cast<UInt128>(n_mag) >=
-                      MAX_UNITS * static_cast<UInt128>(d_mag)))
-      return result_is_negative ? OutRep::MIN() : OutRep::MAX();
-  }
+  if (LIBC_UNLIKELY(static_cast<UInt128>(n_mag) >=
+                    MAX_UNITS * static_cast<UInt128>(d_mag)))
+    return result_is_negative ? OutRep::MIN() : OutRep::MAX();
 
   WideFXType res;
 
diff --git a/libc/test/src/stdfix/FxDiviTest.h b/libc/test/src/stdfix/FxDiviTest.h
index 960ec341626e42..b807c81ae0f289 100644
--- a/libc/test/src/stdfix/FxDiviTest.h
+++ b/libc/test/src/stdfix/FxDiviTest.h
@@ -221,6 +221,15 @@ class FxDiviTest : public LIBC_NAMESPACE::testing::Test {
       constexpr IntType big_non_pow2 = big_pow2 + 7;
       EXPECT_TRUE(abs_diff(func(3, big_non_pow2), fx_zero) <= epsilon);
       EXPECT_EQ(func(big_non_pow2, big_non_pow2 << 1), one_half);
+
+      constexpr int digits = cpp::numeric_limits<IntType>::digits;
+      constexpr IntType huge = static_cast<IntType>(1) << (digits - 1);
+      EXPECT_EQ(func(huge, 3), fx_max);
+      if constexpr (is_signed) {
+        EXPECT_EQ(func(huge, -3), fx_min);
+        EXPECT_EQ(func(-huge, 3), fx_min);
+        EXPECT_EQ(func(-huge, -3), fx_max);
+      }
     }
   }
 



More information about the libc-commits mailing list