[libc-commits] [libc] [libc][stdfix] Fix idiv for MIN / -EPS (PR #241529)

Robin Bisht via libc-commits libc-commits at lists.llvm.org
Thu Nov 12 18:02:54 PST 2026


https://github.com/robinbishtt updated https://github.com/llvm/llvm-project/pull/241529

>From 3adce2edcd0bb49331692d0aca9bffbeb6fd5e06 Mon Sep 17 00:00:00 2001
From: robinbishtt <robinbisht49 at gmail.com>
Date: Wed, 11 Nov 2026 11:11:28 +0530
Subject: [PATCH 1/2] [libc][stdfix] Fix idiv for MIN / -EPS

fixed_point::idiv divided in CompType, which is as wide as the storage.
For x = MIN and y = -EPS the quotient is 2^(TOTAL_LEN - 1): it does not
fit in CompType, but it fits in the return type of idivr (int) and
idivlr (long). idivr returned -32768 instead of 32768 and idivlr raised
SIGFPE.
---
 libc/src/__support/fixed_point/fx_bits.h | 7 ++++++-
 libc/test/src/stdfix/IdivFxTest.h        | 6 ++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/fixed_point/fx_bits.h b/libc/src/__support/fixed_point/fx_bits.h
index eb7af96aa7c99..15e4929c43b1c 100644
--- a/libc/src/__support/fixed_point/fx_bits.h
+++ b/libc/src/__support/fixed_point/fx_bits.h
@@ -222,7 +222,12 @@ idiv(T x, T y) {
 
   // If an integer result of one of these functions overflows, the behavior is
   // undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16
-  CompType result = x_comp / y_comp;
+  using WideType = cpp::conditional_t<
+      (sizeof(CompType) < sizeof(int64_t)),
+      cpp::conditional_t<cpp::is_signed_v<CompType>, int64_t, uint64_t>,
+      cpp::conditional_t<cpp::is_signed_v<CompType>, Int128, UInt128>>;
+  WideType result =
+      static_cast<WideType>(x_comp) / static_cast<WideType>(y_comp);
 
   return static_cast<XType>(result);
 }
diff --git a/libc/test/src/stdfix/IdivFxTest.h b/libc/test/src/stdfix/IdivFxTest.h
index 14abf75d846d4..4a3f7a566f2dc 100644
--- a/libc/test/src/stdfix/IdivFxTest.h
+++ b/libc/test/src/stdfix/IdivFxTest.h
@@ -31,6 +31,7 @@ class IdivFxTest : public LIBC_NAMESPACE::testing::Test {
   static constexpr T min = FXRep::MIN();
   static constexpr T one_half = FXRep::ONE_HALF();
   static constexpr T one_fourth = FXRep::ONE_FOURTH();
+  static constexpr T eps = FXRep::EPS();
 
 public:
   typedef XType (*IdivFxFunc)(T, T);
@@ -67,6 +68,11 @@ class IdivFxTest : public LIBC_NAMESPACE::testing::Test {
         EXPECT_EQ(func(2.25, -3.0), static_cast<XType>(0));
       }
     }
+
+    if constexpr (is_signed && (sizeof(XType) > sizeof(T))) {
+      EXPECT_EQ(func(min, -eps), static_cast<XType>(1)
+                                     << (FXRep::TOTAL_LEN - 1));
+    }
   }
 
   void testInvalidNumbers([[maybe_unused]] IdivFxFunc func) {

>From 05e1a9cf7200fae116f21cd23d07ed4d723856c8 Mon Sep 17 00:00:00 2001
From: Robin Bisht <robinbisht49 at gmail.com>
Date: Fri, 13 Nov 2026 07:32:45 +0530
Subject: [PATCH 2/2] [libc][stdfix] Handle idiv MIN / -EPS with an early
 return

Address review: MIN / -EPS is the only input pair that overflows CompType, so
return its result directly instead of dividing in a wider type.
---
 libc/src/__support/fixed_point/fx_bits.h | 15 +++++++++------
 libc/test/src/stdfix/IdivFxTest.h        | 11 +++++++++--
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/libc/src/__support/fixed_point/fx_bits.h b/libc/src/__support/fixed_point/fx_bits.h
index 15e4929c43b1c..8e3446781aea6 100644
--- a/libc/src/__support/fixed_point/fx_bits.h
+++ b/libc/src/__support/fixed_point/fx_bits.h
@@ -217,17 +217,20 @@ idiv(T x, T y) {
   // behavior is undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16
   LIBC_CRASH_ON_VALUE(y, FXRep::ZERO());
 
+  // MIN / -EPS == 2^VALUE_LEN does not fit in CompType (the division would
+  // truncate, or trap with SIGFPE), but it fits in XType if XType is wider
+  // than CompType. This is the only input pair that overflows CompType.
+  if constexpr (FXRep::SIGN_LEN > 0 && sizeof(XType) > sizeof(CompType)) {
+    if (LIBC_UNLIKELY(x == FXRep::MIN() && y == -FXRep::EPS()))
+      return static_cast<XType>(1) << FXRep::VALUE_LEN;
+  }
+
   CompType x_comp = static_cast<CompType>(FXBits(x).get_bits());
   CompType y_comp = static_cast<CompType>(FXBits(y).get_bits());
 
   // If an integer result of one of these functions overflows, the behavior is
   // undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16
-  using WideType = cpp::conditional_t<
-      (sizeof(CompType) < sizeof(int64_t)),
-      cpp::conditional_t<cpp::is_signed_v<CompType>, int64_t, uint64_t>,
-      cpp::conditional_t<cpp::is_signed_v<CompType>, Int128, UInt128>>;
-  WideType result =
-      static_cast<WideType>(x_comp) / static_cast<WideType>(y_comp);
+  CompType result = x_comp / y_comp;
 
   return static_cast<XType>(result);
 }
diff --git a/libc/test/src/stdfix/IdivFxTest.h b/libc/test/src/stdfix/IdivFxTest.h
index 4a3f7a566f2dc..4326c130ad2f8 100644
--- a/libc/test/src/stdfix/IdivFxTest.h
+++ b/libc/test/src/stdfix/IdivFxTest.h
@@ -69,9 +69,16 @@ class IdivFxTest : public LIBC_NAMESPACE::testing::Test {
       }
     }
 
+    // MIN / -EPS == 2^VALUE_LEN does not fit in the storage type of T, but it
+    // is representable (and thus well defined) if XType is wider than T. The
+    // neighbouring inputs must keep working.
     if constexpr (is_signed && (sizeof(XType) > sizeof(T))) {
-      EXPECT_EQ(func(min, -eps), static_cast<XType>(1)
-                                     << (FXRep::TOTAL_LEN - 1));
+      constexpr XType two_pow_value_len = static_cast<XType>(1)
+                                          << FXRep::VALUE_LEN;
+      EXPECT_EQ(func(min, -eps), two_pow_value_len);
+      EXPECT_EQ(func(min + eps, -eps), two_pow_value_len - 1);
+      EXPECT_EQ(func(min, -eps - eps), two_pow_value_len / 2);
+      EXPECT_EQ(func(max, -eps), -(two_pow_value_len - 1));
     }
   }
 



More information about the libc-commits mailing list