forked from OSchip/llvm-project
[APInt] Add WORD_MAX constant and use it instead of UINT64_MAX. NFC
llvm-svn: 301069
This commit is contained in:
parent
c0bb21f38e
commit
5e113742e7
|
@ -78,6 +78,8 @@ public:
|
|||
APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT
|
||||
};
|
||||
|
||||
static const WordType WORD_MAX = ~WordType(0);
|
||||
|
||||
private:
|
||||
/// This union is used to store the integer value. When the
|
||||
/// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
|
||||
|
@ -144,7 +146,7 @@ private:
|
|||
return *this;
|
||||
|
||||
// Mask out the high bits.
|
||||
uint64_t mask = UINT64_MAX >> (APINT_BITS_PER_WORD - wordBits);
|
||||
uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - wordBits);
|
||||
if (isSingleWord())
|
||||
VAL &= mask;
|
||||
else
|
||||
|
@ -373,7 +375,7 @@ public:
|
|||
/// This checks to see if the value has all bits of the APInt are set or not.
|
||||
bool isAllOnesValue() const {
|
||||
if (isSingleWord())
|
||||
return VAL == UINT64_MAX >> (APINT_BITS_PER_WORD - BitWidth);
|
||||
return VAL == WORD_MAX >> (APINT_BITS_PER_WORD - BitWidth);
|
||||
return countPopulationSlowCase() == BitWidth;
|
||||
}
|
||||
|
||||
|
@ -455,7 +457,7 @@ public:
|
|||
assert(numBits != 0 && "numBits must be non-zero");
|
||||
assert(numBits <= BitWidth && "numBits out of range");
|
||||
if (isSingleWord())
|
||||
return VAL == (UINT64_MAX >> (APINT_BITS_PER_WORD - numBits));
|
||||
return VAL == (WORD_MAX >> (APINT_BITS_PER_WORD - numBits));
|
||||
unsigned Ones = countTrailingOnesSlowCase();
|
||||
return (numBits == Ones) &&
|
||||
((Ones + countLeadingZerosSlowCase()) == BitWidth);
|
||||
|
@ -519,7 +521,7 @@ public:
|
|||
///
|
||||
/// \returns the all-ones value for an APInt of the specified bit-width.
|
||||
static APInt getAllOnesValue(unsigned numBits) {
|
||||
return APInt(numBits, UINT64_MAX, true);
|
||||
return APInt(numBits, WORD_MAX, true);
|
||||
}
|
||||
|
||||
/// \brief Get the '0' value.
|
||||
|
@ -1296,7 +1298,7 @@ public:
|
|||
/// \brief Set every bit to 1.
|
||||
void setAllBits() {
|
||||
if (isSingleWord())
|
||||
VAL = UINT64_MAX;
|
||||
VAL = WORD_MAX;
|
||||
else
|
||||
// Set all the bits in all the words.
|
||||
memset(pVal, -1, getNumWords() * APINT_WORD_SIZE);
|
||||
|
@ -1326,7 +1328,7 @@ public:
|
|||
return;
|
||||
}
|
||||
if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) {
|
||||
uint64_t mask = UINT64_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
|
||||
uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
|
||||
mask <<= loBit;
|
||||
if (isSingleWord())
|
||||
VAL |= mask;
|
||||
|
@ -1368,7 +1370,7 @@ public:
|
|||
/// \brief Toggle every bit to its opposite value.
|
||||
void flipAllBits() {
|
||||
if (isSingleWord()) {
|
||||
VAL ^= UINT64_MAX;
|
||||
VAL ^= WORD_MAX;
|
||||
clearUnusedBits();
|
||||
} else {
|
||||
flipAllBitsSlowCase();
|
||||
|
@ -1663,7 +1665,7 @@ public:
|
|||
/// referencing 2 in a space where 2 does no exist.
|
||||
unsigned nearestLogBase2() const {
|
||||
// Special case when we have a bitwidth of 1. If VAL is 1, then we
|
||||
// get 0. If VAL is 0, we get UINT64_MAX which gets truncated to
|
||||
// get 0. If VAL is 0, we get WORD_MAX which gets truncated to
|
||||
// UINT32_MAX.
|
||||
if (BitWidth == 1)
|
||||
return VAL - 1;
|
||||
|
|
|
@ -81,7 +81,7 @@ void APInt::initSlowCase(uint64_t val, bool isSigned) {
|
|||
pVal[0] = val;
|
||||
if (isSigned && int64_t(val) < 0)
|
||||
for (unsigned i = 1; i < getNumWords(); ++i)
|
||||
pVal[i] = -1ULL;
|
||||
pVal[i] = WORD_MAX;
|
||||
clearUnusedBits();
|
||||
}
|
||||
|
||||
|
@ -404,13 +404,13 @@ void APInt::setBitsSlowCase(unsigned loBit, unsigned hiBit) {
|
|||
unsigned hiWord = whichWord(hiBit);
|
||||
|
||||
// Create an initial mask for the low word with zeros below loBit.
|
||||
uint64_t loMask = UINT64_MAX << whichBit(loBit);
|
||||
uint64_t loMask = WORD_MAX << whichBit(loBit);
|
||||
|
||||
// If hiBit is not aligned, we need a high mask.
|
||||
unsigned hiShiftAmt = whichBit(hiBit);
|
||||
if (hiShiftAmt != 0) {
|
||||
// Create a high mask with zeros above hiBit.
|
||||
uint64_t hiMask = UINT64_MAX >> (APINT_BITS_PER_WORD - hiShiftAmt);
|
||||
uint64_t hiMask = WORD_MAX >> (APINT_BITS_PER_WORD - hiShiftAmt);
|
||||
// If loWord and hiWord are equal, then we combine the masks. Otherwise,
|
||||
// set the bits in hiWord.
|
||||
if (hiWord == loWord)
|
||||
|
@ -423,7 +423,7 @@ void APInt::setBitsSlowCase(unsigned loBit, unsigned hiBit) {
|
|||
|
||||
// Fill any words between loWord and hiWord with all ones.
|
||||
for (unsigned word = loWord + 1; word < hiWord; ++word)
|
||||
pVal[word] = UINT64_MAX;
|
||||
pVal[word] = WORD_MAX;
|
||||
}
|
||||
|
||||
/// Set the given bit to 0 whose position is given as "bitPosition".
|
||||
|
@ -463,7 +463,7 @@ void APInt::insertBits(const APInt &subBits, unsigned bitPosition) {
|
|||
|
||||
// Single word result can be done as a direct bitmask.
|
||||
if (isSingleWord()) {
|
||||
uint64_t mask = UINT64_MAX >> (APINT_BITS_PER_WORD - subBitWidth);
|
||||
uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - subBitWidth);
|
||||
VAL &= ~(mask << bitPosition);
|
||||
VAL |= (subBits.VAL << bitPosition);
|
||||
return;
|
||||
|
@ -475,7 +475,7 @@ void APInt::insertBits(const APInt &subBits, unsigned bitPosition) {
|
|||
|
||||
// Insertion within a single word can be done as a direct bitmask.
|
||||
if (loWord == hi1Word) {
|
||||
uint64_t mask = UINT64_MAX >> (APINT_BITS_PER_WORD - subBitWidth);
|
||||
uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - subBitWidth);
|
||||
pVal[loWord] &= ~(mask << loBit);
|
||||
pVal[loWord] |= (subBits.VAL << loBit);
|
||||
return;
|
||||
|
@ -491,7 +491,7 @@ void APInt::insertBits(const APInt &subBits, unsigned bitPosition) {
|
|||
// Mask+insert remaining bits.
|
||||
unsigned remainingBits = subBitWidth % APINT_BITS_PER_WORD;
|
||||
if (remainingBits != 0) {
|
||||
uint64_t mask = UINT64_MAX >> (APINT_BITS_PER_WORD - remainingBits);
|
||||
uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - remainingBits);
|
||||
pVal[hi1Word] &= ~mask;
|
||||
pVal[hi1Word] |= subBits.getWord(subBitWidth - 1);
|
||||
}
|
||||
|
@ -658,7 +658,7 @@ unsigned APInt::countLeadingOnes() const {
|
|||
unsigned Count = llvm::countLeadingOnes(pVal[i] << shift);
|
||||
if (Count == highWordBits) {
|
||||
for (i--; i >= 0; --i) {
|
||||
if (pVal[i] == -1ULL)
|
||||
if (pVal[i] == WORD_MAX)
|
||||
Count += APINT_BITS_PER_WORD;
|
||||
else {
|
||||
Count += llvm::countLeadingOnes(pVal[i]);
|
||||
|
@ -684,7 +684,7 @@ unsigned APInt::countTrailingZeros() const {
|
|||
unsigned APInt::countTrailingOnesSlowCase() const {
|
||||
unsigned Count = 0;
|
||||
unsigned i = 0;
|
||||
for (; i < getNumWords() && pVal[i] == -1ULL; ++i)
|
||||
for (; i < getNumWords() && pVal[i] == WORD_MAX; ++i)
|
||||
Count += APINT_BITS_PER_WORD;
|
||||
if (i < getNumWords())
|
||||
Count += llvm::countTrailingOnes(pVal[i]);
|
||||
|
@ -1052,7 +1052,7 @@ APInt APInt::ashr(unsigned shiftAmt) const {
|
|||
// issues in the algorithm below.
|
||||
if (shiftAmt == BitWidth) {
|
||||
if (isNegative())
|
||||
return APInt(BitWidth, -1ULL, true);
|
||||
return APInt(BitWidth, WORD_MAX, true);
|
||||
else
|
||||
return APInt(BitWidth, 0);
|
||||
}
|
||||
|
@ -1077,7 +1077,7 @@ APInt APInt::ashr(unsigned shiftAmt) const {
|
|||
// Adjust the top significant word for sign bit fill, if negative
|
||||
if (isNegative())
|
||||
if (bitsInWord < APINT_BITS_PER_WORD)
|
||||
val[breakWord] |= ~0ULL << bitsInWord; // set high bits
|
||||
val[breakWord] |= WORD_MAX << bitsInWord; // set high bits
|
||||
} else {
|
||||
// Shift the low order words
|
||||
for (unsigned i = 0; i < breakWord; ++i) {
|
||||
|
@ -1097,15 +1097,15 @@ APInt APInt::ashr(unsigned shiftAmt) const {
|
|||
if (wordShift > bitsInWord) {
|
||||
if (breakWord > 0)
|
||||
val[breakWord-1] |=
|
||||
~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
|
||||
val[breakWord] |= ~0ULL;
|
||||
WORD_MAX << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
|
||||
val[breakWord] |= WORD_MAX;
|
||||
} else
|
||||
val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
|
||||
val[breakWord] |= WORD_MAX << (bitsInWord - wordShift);
|
||||
}
|
||||
}
|
||||
|
||||
// Remaining words are 0 or -1, just assign them.
|
||||
uint64_t fillValue = (isNegative() ? -1ULL : 0);
|
||||
uint64_t fillValue = (isNegative() ? WORD_MAX : 0);
|
||||
for (unsigned i = breakWord+1; i < getNumWords(); ++i)
|
||||
val[i] = fillValue;
|
||||
APInt Result(val, BitWidth);
|
||||
|
|
Loading…
Reference in New Issue