From: Craig Topper Date: Wed, 10 May 2017 20:01:38 +0000 (+0000) Subject: [APInt] Add negate helper method to implement twos complement. Use it to shorten... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d53f653e9b4f752260d6a06ebc9bebd332400ff2;p=llvm [APInt] Add negate helper method to implement twos complement. Use it to shorten code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302716 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h index 934fe912f01..f1b039e16dc 100644 --- a/include/llvm/ADT/APInt.h +++ b/include/llvm/ADT/APInt.h @@ -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; } diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index 227cfbd2e2e..d43140b3551 100644 --- a/lib/Support/APInt.cpp +++ b/lib/Support/APInt.cpp @@ -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 &Str, unsigned Radix, @@ -1927,8 +1925,7 @@ void APInt::toString(SmallVectorImpl &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('-'); }