diff options
Diffstat (limited to 'unittests/ADT/APIntTest.cpp')
-rw-r--r-- | unittests/ADT/APIntTest.cpp | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp index 3b7ac5b..acdc1ec 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)); @@ -678,6 +878,46 @@ TEST(APIntTest, nearestLogBase2) { EXPECT_EQ(A9.nearestLogBase2(), UINT32_MAX); } +TEST(APIntTest, IsSplat) { + APInt A(32, 0x01010101); + EXPECT_FALSE(A.isSplat(1)); + EXPECT_FALSE(A.isSplat(2)); + EXPECT_FALSE(A.isSplat(4)); + EXPECT_TRUE(A.isSplat(8)); + EXPECT_TRUE(A.isSplat(16)); + EXPECT_TRUE(A.isSplat(32)); + + APInt B(24, 0xAAAAAA); + EXPECT_FALSE(B.isSplat(1)); + EXPECT_TRUE(B.isSplat(2)); + EXPECT_TRUE(B.isSplat(4)); + EXPECT_TRUE(B.isSplat(8)); + EXPECT_TRUE(B.isSplat(24)); + + APInt C(24, 0xABAAAB); + EXPECT_FALSE(C.isSplat(1)); + EXPECT_FALSE(C.isSplat(2)); + EXPECT_FALSE(C.isSplat(4)); + EXPECT_FALSE(C.isSplat(8)); + EXPECT_TRUE(C.isSplat(24)); + + APInt D(32, 0xABBAABBA); + EXPECT_FALSE(D.isSplat(1)); + EXPECT_FALSE(D.isSplat(2)); + EXPECT_FALSE(D.isSplat(4)); + EXPECT_FALSE(D.isSplat(8)); + EXPECT_TRUE(D.isSplat(16)); + EXPECT_TRUE(D.isSplat(32)); + + APInt E(32, 0); + EXPECT_TRUE(E.isSplat(1)); + EXPECT_TRUE(E.isSplat(2)); + EXPECT_TRUE(E.isSplat(4)); + EXPECT_TRUE(E.isSplat(8)); + EXPECT_TRUE(E.isSplat(16)); + EXPECT_TRUE(E.isSplat(32)); +} + #if defined(__clang__) // Disable the pragma warning from versions of Clang without -Wself-move #pragma clang diagnostic push |