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

via libc-commits libc-commits at lists.llvm.org
Wed Nov 11 02:50:48 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Robin Bisht (robinbishtt)

<details>
<summary>Changes</summary>

Issue #<!-- -->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.

---
Full diff: https://github.com/llvm/llvm-project/pull/241213.diff


2 Files Affected:

- (modified) libc/src/__support/fixed_point/fx_bits.h (+4-7) 
- (modified) libc/test/src/stdfix/FxDiviTest.h (+9) 


``````````diff
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);
+      }
     }
   }
 

``````````

</details>


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


More information about the libc-commits mailing list