forked from OSchip/llvm-project
Fix UB in APInt::ashr
i64 -1, whose sign bit is the 0th one, can't be left shifted without invoking UB. https://reviews.llvm.org/D23362 llvm-svn: 278280
This commit is contained in:
parent
9ef6b6b4f4
commit
851b79dc4d
|
@ -1042,11 +1042,7 @@ APInt APInt::ashr(unsigned shiftAmt) const {
|
||||||
if (isSingleWord()) {
|
if (isSingleWord()) {
|
||||||
if (shiftAmt == BitWidth)
|
if (shiftAmt == BitWidth)
|
||||||
return APInt(BitWidth, 0); // undefined
|
return APInt(BitWidth, 0); // undefined
|
||||||
else {
|
return APInt(BitWidth, SignExtend64(VAL, BitWidth) >> shiftAmt);
|
||||||
unsigned SignBit = APINT_BITS_PER_WORD - BitWidth;
|
|
||||||
return APInt(BitWidth,
|
|
||||||
(((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If all the bits were shifted out, the result is, technically, undefined.
|
// If all the bits were shifted out, the result is, technically, undefined.
|
||||||
|
|
|
@ -32,6 +32,11 @@ TEST(APIntTest, ShiftLeftByZero) {
|
||||||
EXPECT_FALSE(Shl[1]);
|
EXPECT_FALSE(Shl[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(APIntTest, i64_ArithmeticRightShiftNegative) {
|
||||||
|
const APInt neg_one(64, static_cast<uint64_t>(-1), true);
|
||||||
|
EXPECT_EQ(neg_one, neg_one.ashr(7));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(APIntTest, i128_NegativeCount) {
|
TEST(APIntTest, i128_NegativeCount) {
|
||||||
APInt Minus3(128, static_cast<uint64_t>(-3), true);
|
APInt Minus3(128, static_cast<uint64_t>(-3), true);
|
||||||
EXPECT_EQ(126u, Minus3.countLeadingOnes());
|
EXPECT_EQ(126u, Minus3.countLeadingOnes());
|
||||||
|
|
Loading…
Reference in New Issue