[APInt] Fix a bug in lshr by a value more than 64 bits above the bit width.

This was throwing an assert because we determined the intra-word shift amount by subtracting the size of the full word shift from the total shift amount. But we failed to account for the fact that we clipped the full word shifts by total words first. To fix this just calculate the intra-word shift as the remainder of dividing by bits per word.

llvm-svn: 300405
This commit is contained in:
Craig Topper 2017-04-16 01:03:51 +00:00
parent 909b3376ba
commit 9edfb08d93
2 changed files with 6 additions and 1 deletions

View File

@ -1188,7 +1188,7 @@ void APInt::lshrInPlace(unsigned shiftAmt) {
// Fill in first Words - ShiftFullWords by shifting.
lshrWords(pVal, pVal + ShiftFullWords, Words - ShiftFullWords,
shiftAmt - (ShiftFullWords * APINT_BITS_PER_WORD));
shiftAmt % APINT_BITS_PER_WORD);
// The remaining high words are all zero.
for (unsigned I = Words - ShiftFullWords; I != Words; ++I)

View File

@ -37,6 +37,11 @@ TEST(APIntTest, i64_ArithmeticRightShiftNegative) {
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());