From 7b771ed448487705237868f705da17b40c6bfe82 Mon Sep 17 00:00:00 2001 From: Tim Shen Date: Tue, 12 Nov 2019 18:17:04 -0800 Subject: [PATCH] [APInt] Fix tests that had wrong assumption about sdivs with negative quotient. Reviewers: sanjoy Subscribers: bixia, dexonsmith, sanjoy.google, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D70156 --- llvm/unittests/ADT/APIntTest.cpp | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index b6fee5b..6e87c70 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -2604,31 +2604,36 @@ TEST(APIntTest, RoundingSDiv) { 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_EQ(QuoTowardZero, Quo); + } else if (A.isNegative() != + B.isNegative()) { // if the math quotient is negative. + EXPECT_EQ(QuoTowardZero, Quo); + } else { + EXPECT_EQ(QuoTowardZero + 1, Quo); } } { 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_EQ(QuoTowardZero, Quo); + } else if (A.isNegative() != + B.isNegative()) { // if the math quotient is negative. + EXPECT_EQ(QuoTowardZero - 1, Quo); + } else { + EXPECT_EQ(QuoTowardZero, Quo); } } - { - 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)); } } } -- 2.7.4