summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorYaron Keren <yaron.keren@gmail.com>2015-03-26 19:45:19 +0000
committerYaron Keren <yaron.keren@gmail.com>2015-03-26 19:45:19 +0000
commit61ac65ddec9becc4b6773077c69f0f54b942e445 (patch)
treef1dee8a2ef269445044c0b5e2059d28937e1f2c9 /unittests
parent784545fba0e1fcbb5e12cde76d13d91fd0972323 (diff)
Fix rare case where APInt divide algorithm applied un-needed transformation.
APInt uses Knuth's D algorithm for long division. In rare cases the implementation applied a transformation that was not needed. Added unit tests for long division. KnuthDiv() procedure is fully covered. There is a case in APInt::divide() that I believe is never used (marked with a comment) as all users of divide() handle trivial cases earlier. Patch by Pawel Bylica! http://reviews.llvm.org/D8448 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233312 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/ADT/APIntTest.cpp200
1 files changed, 200 insertions, 0 deletions
diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp
index 96fa0ddbeae..acdc1ecbbde 100644
--- a/unittests/ADT/APIntTest.cpp
+++ b/unittests/ADT/APIntTest.cpp
@@ -209,6 +209,206 @@ TEST(APIntTest, i1) {
}
}
+TEST(APIntTest, divrem_big1) {
+ // Tests KnuthDiv rare step D6
+ APInt a{256, "1ffffffffffffffff", 16};
+ APInt b{256, "1ffffffffffffffff", 16};
+ APInt c{256, 0};
+
+ auto p = a * b + c;
+ auto q = p.udiv(a);
+ auto r = p.urem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ APInt::udivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.udiv(b);
+ r = p.urem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ APInt::udivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(a);
+ r = p.srem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ APInt::sdivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(b);
+ r = p.srem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ APInt::sdivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+}
+
+TEST(APIntTest, divrem_big2) {
+ // Tests KnuthDiv rare step D6
+ APInt a{1024, "111111ffffffffffffffff"
+ "ffffffffffffffffffffffffffffffff"
+ "fffffffffffffffffffffffffffffccf"
+ "ffffffffffffffffffffffffffffff00", 16};
+ APInt b{1024, "112233ceff"
+ "cecece000000ffffffffffffffffffff"
+ "ffffffffffffffffffffffffffffffff"
+ "ffffffffffffffffffffffffffffffff"
+ "ffffffffffffffffffffffffffffff33", 16};
+ APInt c{1024, 7919};
+
+ auto p = a * b + c;
+ auto q = p.udiv(a);
+ auto r = p.urem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ APInt::udivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.udiv(b);
+ r = p.urem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ APInt::udivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(a);
+ r = p.srem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ APInt::sdivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(b);
+ r = p.srem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ APInt::sdivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+}
+
+TEST(APIntTest, divrem_big3) {
+ // Tests KnuthDiv case without shift
+ APInt a{256, "ffffffffffffff0000000", 16};
+ APInt b{256, "80000001ffffffffffffffff", 16};
+ APInt c{256, 4219};
+
+ auto p = a * b + c;
+ auto q = p.udiv(a);
+ auto r = p.urem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ APInt::udivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.udiv(b);
+ r = p.urem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ APInt::udivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(a);
+ r = p.srem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ APInt::sdivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(b);
+ r = p.srem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ APInt::sdivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+}
+
+TEST(APIntTest, divrem_big4) {
+ // Tests heap allocation in divide() enfoced by huge numbers
+ auto a = APInt{4096, 1}.shl(2000);
+ auto b = APInt{4096, 5}.shl(2001);
+ auto c = APInt{4096, 4219*13};
+
+ auto p = a * b + c;
+ auto q = p.udiv(a);
+ auto r = p.urem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = APInt{1024, 0}; // test non-single word APInt conversion in divide()
+ r = APInt{1024, 0};
+ APInt::udivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.udiv(b);
+ r = p.urem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ q = APInt{1024, 0};
+ r = APInt{1024, 0};
+ APInt::udivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(a);
+ r = p.srem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = APInt{1024, 0};
+ r = APInt{1024, 0};
+ APInt::sdivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(b);
+ r = p.srem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ q = APInt{1024, 0};
+ r = APInt{1024, 0};
+ APInt::sdivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+}
+
+TEST(APIntTest, divrem_big5) {
+ // Tests one word divisor case of divide()
+ auto a = APInt{1024, 19}.shl(811);
+ auto b = APInt{1024, 4356013}; // one word
+ auto c = APInt{1024, 1};
+
+ auto p = a * b + c;
+ auto q = p.udiv(a);
+ auto r = p.urem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ APInt::udivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.udiv(b);
+ r = p.urem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ APInt::udivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(a);
+ r = p.srem(a);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ APInt::sdivrem(p, a, q, r);
+ EXPECT_EQ(q, b);
+ EXPECT_EQ(r, c);
+ q = p.sdiv(b);
+ r = p.srem(b);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+ APInt::sdivrem(p, b, q, r);
+ EXPECT_EQ(q, a);
+ EXPECT_EQ(r, c);
+}
+
TEST(APIntTest, fromString) {
EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2));
EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2));