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

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon May 15 09:47:48 PDT 2017


~Craig

On Mon, May 15, 2017 at 9:35 AM, David Blaikie <dblaikie at gmail.com> wrote:

>
>
> On Wed, May 10, 2017 at 1:14 PM Craig Topper via llvm-commits <
> llvm-commits at lists.llvm.org> wrote:
>
>> Author: ctopper
>> Date: Wed May 10 15:01:38 2017
>> New Revision: 302716
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=302716&view=rev
>> Log:
>> [APInt] Add negate helper method to implement twos complement. Use it to
>> shorten code.
>>
>> Modified:
>>     llvm/trunk/include/llvm/ADT/APInt.h
>>     llvm/trunk/lib/Support/APInt.cpp
>>
>> Modified: llvm/trunk/include/llvm/ADT/APInt.h
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/
>> llvm/ADT/APInt.h?rev=302716&r1=302715&r2=302716&view=diff
>> ============================================================
>> ==================
>> --- llvm/trunk/include/llvm/ADT/APInt.h (original)
>> +++ llvm/trunk/include/llvm/ADT/APInt.h Wed May 10 15:01:38 2017
>> @@ -1437,6 +1437,12 @@ public:
>>    /// as "bitPosition".
>>    void flipBit(unsigned bitPosition);
>>
>> +  /// Negate this APInt in place.
>> +  void negate() {
>> +    flipAllBits();
>> +    ++(*this);
>>
>
> If you like, you can drop the () here ^ and write: ++*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_ostre
>>  }
>>
>>  inline APInt operator-(APInt v) {
>> -  v.flipAllBits();
>> -  ++v;
>> +  v.negate();
>>    return v;
>>  }
>>
>>
>> Modified: llvm/trunk/lib/Support/APInt.cpp
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/
>> Support/APInt.cpp?rev=302716&r1=302715&r2=302716&view=diff
>> ============================================================
>> ==================
>> --- llvm/trunk/lib/Support/APInt.cpp (original)
>> +++ llvm/trunk/lib/Support/APInt.cpp Wed May 10 15:01:38 2017
>> @@ -1846,10 +1846,8 @@ void APInt::fromString(unsigned numbits,
>>      *this += digit;
>>    }
>>    // If its negative, put it in two's complement form
>> -  if (isNeg) {
>> -    --(*this);
>> -    this->flipAllBits();
>> -  }
>> +  if (isNeg)
>> +    this->negate();
>>
>
> Any particular reason for the "this->" qualification?
>

Nope, I suspect I just pasted "negate" over the original "flipAllBits"
without much thought.


>
>
>>  }
>>
>>  void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
>> @@ -1927,8 +1925,7 @@ void APInt::toString(SmallVectorImpl<cha
>>      // 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('-');
>>    }
>>
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170515/4f6d09b6/attachment.html>


More information about the llvm-commits mailing list