[llvm] 0274750 - [Support][APint] Fix APInt::urem for edge case. Use `U.pVal[0] instead getZExtValue() (#189441)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 31 05:52:24 PDT 2026


Author: Max Graey
Date: 2026-03-31T14:52:17+02:00
New Revision: 027475071e73bb72f874652c90f91f4ec483d578

URL: https://github.com/llvm/llvm-project/commit/027475071e73bb72f874652c90f91f4ec483d578
DIFF: https://github.com/llvm/llvm-project/commit/027475071e73bb72f874652c90f91f4ec483d578.diff

LOG: [Support][APint] Fix APInt::urem for edge case. Use `U.pVal[0] instead getZExtValue() (#189441)

Use `U.pVal[0]` instead of `getZExtValue()` in the
`APInt::urem(uint64_t)` power-of-two fast path. `getZExtValue()`
requires the entire APInt to fit into 64 bits, but this code can be
reached for multi-word values, which may trigger assertions
(`assert(getActiveBits() <= 64 && "Too many bits for uint64_t")`) or
otherwise mis-handle wide integers.

Also add simple test for edge cases.

Improvement for https://github.com/llvm/llvm-project/pull/189245

Added: 
    

Modified: 
    llvm/lib/Support/APInt.cpp
    llvm/unittests/ADT/APIntTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index f546de5e5da22..13fd5f47f578a 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -1755,7 +1755,7 @@ uint64_t APInt::urem(uint64_t RHS) const {
     return U.pVal[0] % RHS;
   if (llvm::isPowerOf2_64(RHS))
     // X % 2^w ===> X & (2^w - 1)
-    return getZExtValue() & (RHS - 1);
+    return U.pVal[0] & (RHS - 1);
 
   // We have to compute it the hard way. Invoke the Knuth divide algorithm.
   uint64_t Remainder;

diff  --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 34efc4ff8ccf5..344f8c727dda7 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -1186,6 +1186,29 @@ TEST(APIntTest, divrem_simple) {
   EXPECT_EQ(R, APInt(65, 0));
 }
 
+TEST(APIntTest, URemUint64PowerOf2Wide) {
+  // Edge case tests for urem with power of two arguments
+  APInt V(128, "80000000000000000000000000000FFF", 16);
+
+  EXPECT_EQ(15u, V.urem(16));
+  EXPECT_EQ(255u, V.urem(256));
+  EXPECT_EQ(4095u, V.urem(4096));
+
+  EXPECT_EQ(APInt(128, 15), V.urem(APInt(128, 16)));
+  EXPECT_EQ(APInt(128, 255), V.urem(APInt(128, 256)));
+  EXPECT_EQ(APInt(128, 4095), V.urem(APInt(128, 4096)));
+
+  APInt AllOnes = APInt::getAllOnes(128);
+
+  EXPECT_EQ(63u, AllOnes.urem(64));
+  EXPECT_EQ(255u, AllOnes.urem(256));
+  EXPECT_EQ(4095u, AllOnes.urem(4096));
+
+  EXPECT_EQ(APInt(128, 63), AllOnes.urem(APInt(128, 64)));
+  EXPECT_EQ(APInt(128, 255), AllOnes.urem(APInt(128, 256)));
+  EXPECT_EQ(APInt(128, 4095), AllOnes.urem(APInt(128, 4096)));
+}
+
 TEST(APIntTest, fromString) {
   EXPECT_EQ(APInt(32, 0), APInt(32,   "0", 2));
   EXPECT_EQ(APInt(32, 1), APInt(32,   "1", 2));


        


More information about the llvm-commits mailing list