[llvm-commits] CVS: llvm/lib/Support/APInt.cpp

Reid Spencer reid at x10sys.com
Sat Feb 17 22:39:59 PST 2007



Changes in directory llvm/lib/Support:

APInt.cpp updated: 1.17 -> 1.18
---
Log message:

Make add_1 exit early if carry is 0.
Fix line breaks and 80 cols violation.
Simplify operator^= since bitwidths must be the same.


---
Diffs of the changes:  (+23 -36)

 APInt.cpp |   59 +++++++++++++++++++++++------------------------------------
 1 files changed, 23 insertions(+), 36 deletions(-)


Index: llvm/lib/Support/APInt.cpp
diff -u llvm/lib/Support/APInt.cpp:1.17 llvm/lib/Support/APInt.cpp:1.18
--- llvm/lib/Support/APInt.cpp:1.17	Sat Feb 17 18:44:22 2007
+++ llvm/lib/Support/APInt.cpp	Sun Feb 18 00:39:42 2007
@@ -139,13 +139,16 @@
 /// returns the carry.
 /// @returns the carry of the addition.
 static uint64_t add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
-  uint64_t carry = y;
-
   for (unsigned i = 0; i < len; ++i) {
-    dest[i] = carry + x[i];
-    carry = (dest[i] < carry) ? 1 : 0;
+    dest[i] = y + x[i];
+    if (dest[i] < y)
+      y = 1;
+    else {
+      y = 0;
+      break;
+    }
   }
-  return carry;
+  return y;
 }
 
 /// @brief Prefix increment operator. Increments the APInt by one.
@@ -161,20 +164,17 @@
 /// sub_1 - This function subtracts the integer array x[] by
 /// integer y and returns the borrow-out carry.
 static uint64_t sub_1(uint64_t x[], unsigned len, uint64_t y) {
-  uint64_t cy = y;
-
   for (unsigned i = 0; i < len; ++i) {
     uint64_t X = x[i];
-    x[i] -= cy;
-    if (cy > X) 
-      cy = 1;
+    x[i] -= y;
+    if (y > X) 
+      y = 1;
     else {
-      cy = 0;
+      y = 0;
       break;
     }
   }
-
-  return cy;
+  return y;
 }
 
 /// @brief Prefix decrement operator. Decrements the APInt by one.
@@ -188,10 +188,8 @@
 
 /// add - This function adds the integer array x[] by integer array
 /// y[] and returns the carry.
-static uint64_t add(uint64_t dest[], uint64_t x[],
-                    uint64_t y[], unsigned len) {
+static uint64_t add(uint64_t dest[], uint64_t x[], uint64_t y[], unsigned len) {
   unsigned carry = 0;
-  
   for (unsigned i = 0; i< len; ++i) {
     carry += x[i];
     dest[i] = carry + y[i];
@@ -223,8 +221,7 @@
 
 /// sub - This function subtracts the integer array x[] by
 /// integer array y[], and returns the borrow-out carry.
-static uint64_t sub(uint64_t dest[], uint64_t x[],
-                    uint64_t y[], unsigned len) {
+static uint64_t sub(uint64_t dest[], uint64_t x[], uint64_t y[], unsigned len) {
   // Carry indicator.
   uint64_t cy = 0;
   
@@ -252,7 +249,8 @@
     else {
       if (RHS.getNumWords() < getNumWords()) { 
         uint64_t carry = sub(pVal, pVal, RHS.pVal, RHS.getNumWords());
-        sub_1(pVal + RHS.getNumWords(), getNumWords() - RHS.getNumWords(), carry); 
+        sub_1(pVal + RHS.getNumWords(), getNumWords() - RHS.getNumWords(), 
+              carry); 
       }
       else
         sub(pVal, pVal, RHS.pVal, getNumWords());
@@ -410,23 +408,12 @@
 APInt& APInt::operator^=(const APInt& RHS) {
   assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
   if (isSingleWord()) {
-    if (RHS.isSingleWord()) VAL ^= RHS.VAL;
-    else VAL ^= RHS.pVal[0];
-  } else {
-    if (RHS.isSingleWord()) {
-      for (unsigned i = 0; i < getNumWords(); ++i)
-        pVal[i] ^= RHS.VAL;
-    } else {
-      unsigned minwords = getNumWords() < RHS.getNumWords() ? 
-                          getNumWords() : RHS.getNumWords();
-      for (unsigned i = 0; i < minwords; ++i)
-        pVal[i] ^= RHS.pVal[i];
-      if (getNumWords() > minwords)
-        for (unsigned i = minwords; i < getNumWords(); ++i)
-          pVal[i] ^= 0;
-    }
-  }
-  clearUnusedBits();
+    VAL ^= RHS.VAL;
+    return *this;
+  } 
+  unsigned numWords = getNumWords();
+  for (unsigned i = 0; i < numWords; ++i)
+      pVal[i] ^= RHS.pVal[i];
   return *this;
 }
 






More information about the llvm-commits mailing list