Skip to content

Commit b4baabb

Browse files
authored
[NFC] Address compiler warnings: Fix C4146 compiler warnings in APInt.cpp (microsoft#7556)
Addresses microsoft#7555 All but one are simple updates to use std::numeric_limits<T>. One case converts to use ~ operator and includes a comment with additional context.
1 parent b78ac50 commit b4baabb

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

lib/Support/APInt.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ inline static unsigned getDigit(char cdigit, uint8_t radix) {
7070
if (r < radix)
7171
return r;
7272

73-
return -1U;
73+
return std::numeric_limits<unsigned>::max();
7474
}
7575

7676

@@ -79,7 +79,7 @@ void APInt::initSlowCase(unsigned numBits, uint64_t val, bool isSigned) {
7979
pVal[0] = val;
8080
if (isSigned && int64_t(val) < 0)
8181
for (unsigned i = 1; i < getNumWords(); ++i)
82-
pVal[i] = -1ULL;
82+
pVal[i] = std::numeric_limits<uint64_t>::max();
8383
}
8484

8585
void APInt::initSlowCase(const APInt& that) {
@@ -735,7 +735,7 @@ unsigned APInt::countLeadingOnes() const {
735735
unsigned Count = llvm::countLeadingOnes(pVal[i] << shift);
736736
if (Count == highWordBits) {
737737
for (i--; i >= 0; --i) {
738-
if (pVal[i] == -1ULL)
738+
if (pVal[i] == std::numeric_limits<uint64_t>::max())
739739
Count += APINT_BITS_PER_WORD;
740740
else {
741741
Count += llvm::countLeadingOnes(pVal[i]);
@@ -761,7 +761,8 @@ unsigned APInt::countTrailingZeros() const {
761761
unsigned APInt::countTrailingOnesSlowCase() const {
762762
unsigned Count = 0;
763763
unsigned i = 0;
764-
for (; i < getNumWords() && pVal[i] == -1ULL; ++i)
764+
for (; i < getNumWords() && pVal[i] == std::numeric_limits<uint64_t>::max();
765+
++i)
765766
Count += APINT_BITS_PER_WORD;
766767
if (i < getNumWords())
767768
Count += llvm::countTrailingOnes(pVal[i]);
@@ -1070,7 +1071,7 @@ APInt APInt::ashr(unsigned shiftAmt) const {
10701071
// issues in the algorithm below.
10711072
if (shiftAmt == BitWidth) {
10721073
if (isNegative())
1073-
return APInt(BitWidth, -1ULL, true);
1074+
return APInt(BitWidth, std::numeric_limits<uint64_t>::max(), true);
10741075
else
10751076
return APInt(BitWidth, 0);
10761077
}
@@ -1123,7 +1124,8 @@ APInt APInt::ashr(unsigned shiftAmt) const {
11231124
}
11241125

11251126
// Remaining words are 0 or -1, just assign them.
1126-
uint64_t fillValue = (isNegative() ? -1ULL : 0);
1127+
uint64_t fillValue =
1128+
(isNegative() ? std::numeric_limits<uint64_t>::max() : 0);
11271129
for (unsigned i = breakWord+1; i < getNumWords(); ++i)
11281130
val[i] = fillValue;
11291131
APInt Result(val, BitWidth);
@@ -2192,7 +2194,18 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
21922194
N = I;
21932195
} else {
21942196
Str.push_back('-');
2195-
N = -(uint64_t)I;
2197+
// In this else block, all values of I must be less than 0.
2198+
//
2199+
// Because values are stored in 2's complement and I is a signed
2200+
// integer, the expression -I is equivalent to (~I + 1) for all values
2201+
// of I, except INT64_MIN, where -I is undefined behavior in C++ due to
2202+
// overflow.
2203+
//
2204+
// However, (~I + 1) is still well-defined even when I == INT64_MIN, and
2205+
// it evaluates to the same bit pattern as INT64_MIN. Because N is
2206+
// unsigned, assigning N = ~I + 1 preserves the exact bit pattern
2207+
// and correctly represents the 2's complement value of -I.
2208+
N = (~I + 1);
21962209
}
21972210
}
21982211

@@ -2408,7 +2421,7 @@ APInt::tcLSB(const integerPart *parts, unsigned int n)
24082421
}
24092422
}
24102423

2411-
return -1U;
2424+
return std::numeric_limits<unsigned int>::max();
24122425
}
24132426

24142427
/* Returns the bit number of the most significant set bit of a number.
@@ -2428,7 +2441,7 @@ APInt::tcMSB(const integerPart *parts, unsigned int n)
24282441
}
24292442
} while (n);
24302443

2431-
return -1U;
2444+
return std::numeric_limits<unsigned int>::max();
24322445
}
24332446

24342447
/* Copy the bit vector of width srcBITS from SRC, starting at bit

0 commit comments

Comments
 (0)