[APInt] Add negate helper method to implement twos complement. Use it to shorten code.

llvm-svn: 302716
This commit is contained in:
Craig Topper 2017-05-10 20:01:38 +00:00
parent f41f274bf8
commit ef0114c4f0
2 changed files with 10 additions and 8 deletions

View File

@ -1437,6 +1437,12 @@ public:
/// as "bitPosition".
void flipBit(unsigned bitPosition);
/// Negate this APInt in place.
void negate() {
flipAllBits();
++(*this);
}
/// Insert the bits from a smaller APInt starting at bitPosition.
void insertBits(const APInt &SubBits, unsigned bitPosition);
@ -1996,8 +2002,7 @@ inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) {
}
inline APInt operator-(APInt v) {
v.flipAllBits();
++v;
v.negate();
return v;
}

View File

@ -1846,10 +1846,8 @@ void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
*this += digit;
}
// If its negative, put it in two's complement form
if (isNeg) {
--(*this);
this->flipAllBits();
}
if (isNeg)
this->negate();
}
void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
@ -1927,8 +1925,7 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
// They want to print the signed version and it is a negative value
// Flip the bits and add one to turn it into the equivalent positive
// value and put a '-' in the result.
Tmp.flipAllBits();
++Tmp;
Tmp.negate();
Str.push_back('-');
}