[APInt] Merge the multiword code from lshrInPlace and tcShiftRight into a single implementation
This merges the two different multiword shift right implementations into a single version located in tcShiftRight. lshrInPlace now calls tcShiftRight for the multiword case.
I retained the memmove fast path from lshrInPlace and used a memset for the zeroing. The for loop is basically tcShiftRight's implementation with the zeroing and the intra-shift of 0 removed.
Differential Revision: https://reviews.llvm.org/D32114
llvm-svn: 300503
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index d24d580..0bd309d 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -37,11 +37,6 @@
EXPECT_EQ(neg_one, neg_one.ashr(7));
}
-TEST(APIntTest, i64_LogicalRightShiftNegative) {
- const APInt neg_one(128, static_cast<uint64_t>(-1), true);
- EXPECT_EQ(0, neg_one.lshr(257));
-}
-
TEST(APIntTest, i128_NegativeCount) {
APInt Minus3(128, static_cast<uint64_t>(-3), true);
EXPECT_EQ(126u, Minus3.countLeadingOnes());
@@ -1996,4 +1991,37 @@
EXPECT_EQ(C, HugePrime);
}
+TEST(APIntTest, LogicalRightShift) {
+ APInt i256(APInt::getHighBitsSet(256, 2));
+
+ i256.lshrInPlace(1);
+ EXPECT_EQ(1U, i256.countLeadingZeros());
+ EXPECT_EQ(253U, i256.countTrailingZeros());
+ EXPECT_EQ(2U, i256.countPopulation());
+
+ i256.lshrInPlace(62);
+ EXPECT_EQ(63U, i256.countLeadingZeros());
+ EXPECT_EQ(191U, i256.countTrailingZeros());
+ EXPECT_EQ(2U, i256.countPopulation());
+
+ i256.lshrInPlace(65);
+ EXPECT_EQ(128U, i256.countLeadingZeros());
+ EXPECT_EQ(126U, i256.countTrailingZeros());
+ EXPECT_EQ(2U, i256.countPopulation());
+
+ i256.lshrInPlace(64);
+ EXPECT_EQ(192U, i256.countLeadingZeros());
+ EXPECT_EQ(62U, i256.countTrailingZeros());
+ EXPECT_EQ(2U, i256.countPopulation());
+
+ i256.lshrInPlace(63);
+ EXPECT_EQ(255U, i256.countLeadingZeros());
+ EXPECT_EQ(0U, i256.countTrailingZeros());
+ EXPECT_EQ(1U, i256.countPopulation());
+
+ // Ensure we handle large shifts of multi-word.
+ const APInt neg_one(128, static_cast<uint64_t>(-1), true);
+ EXPECT_EQ(0, neg_one.lshr(257));
+}
+
} // end anonymous namespace