/// \returns true if this APInt is non-positive.
bool isNonPositive() const { return !isStrictlyPositive(); }
- /// Determine if all bits are set.
+ /// Determine if all bits are set. This is true for zero-width values.
bool isAllOnes() const {
- if (isSingleWord()) {
- // Calculate the shift amount, handling the zero-bit wide case without UB.
- unsigned ShiftAmt =
- (APINT_BITS_PER_WORD - BitWidth) % APINT_BITS_PER_WORD;
- return U.VAL == WORDTYPE_MAX >> ShiftAmt;
- }
+ if (BitWidth == 0)
+ return true;
+ if (isSingleWord())
+ return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth);
return countTrailingOnesSlowCase() == BitWidth;
}
}
APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const {
- assert(numBits > 0 && "Can't extract zero bits");
assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
"Illegal bit extraction");
// Methods like getLowBitsSet work with zero bits.
EXPECT_EQ(0U, APInt::getLowBitsSet(0, 0).getBitWidth());
EXPECT_EQ(0U, APInt::getSplat(0, ZW).getBitWidth());
+ EXPECT_EQ(0U, APInt(4, 10).extractBits(0, 2).getBitWidth());
// Logical operators.
ZW |= ZW2;
EXPECT_EQ(0U, ZW.getLoBits(0).getBitWidth());
EXPECT_EQ(0, ZW.zext(4));
EXPECT_EQ(0U, APInt(4, 3).trunc(0).getBitWidth());
+ EXPECT_TRUE(ZW.isAllOnes());
SmallString<42> STR;
ZW.toStringUnsigned(STR);