[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
Wed May 10 13:01:39 PDT 2017


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);
+  }
+
   /// 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();
 }
 
 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('-');
   }
 




More information about the llvm-commits mailing list