[PATCH] D140636: [Support] Fix what I think is an off by 1 bug in UnsignedDivisionByConstantInfo.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 24 15:00:30 PST 2022


craig.topper updated this revision to Diff 485209.
craig.topper added a comment.

Add a comment and assert to describe what NC is supposed to be.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140636/new/

https://reviews.llvm.org/D140636

Files:
  llvm/lib/Support/DivisionByConstantInfo.cpp


Index: llvm/lib/Support/DivisionByConstantInfo.cpp
===================================================================
--- llvm/lib/Support/DivisionByConstantInfo.cpp
+++ llvm/lib/Support/DivisionByConstantInfo.cpp
@@ -74,7 +74,9 @@
   APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
   APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth());
 
-  APInt NC = AllOnes - (AllOnes - D).urem(D);
+  // Calculate NC, the largest dividend such that NC.urem(D) == D-1.
+  APInt NC = AllOnes - (AllOnes + 1 - D).urem(D);
+  assert(NC.urem(D) == D - 1 && "Unexpected NC value");
   unsigned P = D.getBitWidth() - 1; // initialize P
   APInt Q1, R1, Q2, R2;
   // initialize Q1 = 2P/NC; R1 = rem(2P,NC)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140636.485209.patch
Type: text/x-patch
Size: 711 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221224/700317d1/attachment.bin>


More information about the llvm-commits mailing list