[PATCH] D70156: [APInt] Fix tests that had wrong assumption about sdivs with negative quotient.

Tim Shen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 12 18:26:57 PST 2019


timshen created this revision.
timshen added a reviewer: sanjoy.
Herald added subscribers: sanjoy.google, dexonsmith, bixia.
Herald added a project: LLVM.
timshen added a comment.

This fixes wrong tests introduced by D48498 <https://reviews.llvm.org/D48498>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D70156

Files:
  llvm/unittests/ADT/APIntTest.cpp


Index: llvm/unittests/ADT/APIntTest.cpp
===================================================================
--- llvm/unittests/ADT/APIntTest.cpp
+++ llvm/unittests/ADT/APIntTest.cpp
@@ -2570,31 +2570,36 @@
       EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::TOWARD_ZERO));
     }
 
-    for (uint64_t Bi = -128; Bi <= 127; Bi++) {
+    for (int64_t Bi = -128; Bi <= 127; Bi++) {
       if (Bi == 0)
         continue;
 
       APInt B(8, Bi);
+      APInt QuoTowardZero = A.sdiv(B);
       {
         APInt Quo = APIntOps::RoundingSDiv(A, B, APInt::Rounding::UP);
-        auto Prod = Quo.sext(16) * B.sext(16);
-        EXPECT_TRUE(Prod.uge(A));
-        if (Prod.ugt(A)) {
-          EXPECT_TRUE(((Quo - 1).sext(16) * B.sext(16)).ult(A));
+        if (A.srem(B).isNullValue()) {
+          EXPECT_TRUE(Quo == QuoTowardZero);
+        } else if (A.isNegative() !=
+                   B.isNegative()) { // if the math quotient is negative.
+          EXPECT_TRUE(Quo == QuoTowardZero);
+        } else {
+          EXPECT_TRUE(Quo == QuoTowardZero + 1);
         }
       }
       {
         APInt Quo = APIntOps::RoundingSDiv(A, B, APInt::Rounding::DOWN);
-        auto Prod = Quo.sext(16) * B.sext(16);
-        EXPECT_TRUE(Prod.ule(A));
-        if (Prod.ult(A)) {
-          EXPECT_TRUE(((Quo + 1).sext(16) * B.sext(16)).ugt(A));
+        if (A.srem(B).isNullValue()) {
+          EXPECT_TRUE(Quo == QuoTowardZero);
+        } else if (A.isNegative() !=
+                   B.isNegative()) { // if the math quotient is negative.
+          EXPECT_TRUE(Quo == QuoTowardZero - 1);
+        } else {
+          EXPECT_TRUE(Quo == QuoTowardZero);
         }
       }
-      {
-        APInt Quo = A.sdiv(B);
-        EXPECT_EQ(Quo, APIntOps::RoundingSDiv(A, B, APInt::Rounding::TOWARD_ZERO));
-      }
+      EXPECT_EQ(QuoTowardZero,
+                APIntOps::RoundingSDiv(A, B, APInt::Rounding::TOWARD_ZERO));
     }
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70156.228994.patch
Type: text/x-patch
Size: 1944 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191113/fd5e7336/attachment-0001.bin>


More information about the llvm-commits mailing list