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

Reid Spencer reid at x10sys.com
Tue Feb 27 13:59:47 PST 2007



Changes in directory llvm/lib/Support:

APInt.cpp updated: 1.55 -> 1.56
---
Log message:

Implement countLeadingOnes() and getMinSignedBits(). This helps to minimize
the bit width of negative numbers by computing the minimum bit width for a
negative value. E.g. 0x1800000000000000 could be just 0x8000000000000000


---
Diffs of the changes:  (+34 -1)

 APInt.cpp |   35 ++++++++++++++++++++++++++++++++++-
 1 files changed, 34 insertions(+), 1 deletion(-)


Index: llvm/lib/Support/APInt.cpp
diff -u llvm/lib/Support/APInt.cpp:1.55 llvm/lib/Support/APInt.cpp:1.56
--- llvm/lib/Support/APInt.cpp:1.55	Tue Feb 27 14:24:31 2007
+++ llvm/lib/Support/APInt.cpp	Tue Feb 27 15:59:26 2007
@@ -702,6 +702,38 @@
   return Count;
 }
 
+static uint32_t countLeadingOnes_64(uint64_t V, uint32_t skip) {
+  uint32_t Count = 0;
+  if (skip)
+    V <<= skip;
+  while (V && (V & (1ULL << 63))) {
+    Count++;
+    V <<= 1;
+  }
+  return Count;
+}
+
+uint32_t APInt::countLeadingOnes() const {
+  if (isSingleWord())
+    return countLeadingOnes_64(VAL, APINT_BITS_PER_WORD - BitWidth);
+
+  uint32_t highWordBits = BitWidth % APINT_BITS_PER_WORD;
+  uint32_t shift = (highWordBits == 0 ? 0 : APINT_BITS_PER_WORD - highWordBits);
+  int i = getNumWords() - 1;
+  uint32_t Count = countLeadingOnes_64(pVal[i], shift);
+  if (Count == highWordBits) {
+    for (i--; i >= 0; --i) {
+      if (pVal[i] == -1ULL)
+        Count += APINT_BITS_PER_WORD;
+      else {
+        Count += countLeadingOnes_64(pVal[i], 0);
+        break;
+      }
+    }
+  }
+  return Count;
+}
+
 uint32_t APInt::countTrailingZeros() const {
   if (isSingleWord())
     return CountTrailingZeros_64(VAL);
@@ -1701,6 +1733,7 @@
   else for (unsigned i = getNumWords(); i > 0; i--) {
     cerr << pVal[i-1] << " ";
   }
-  cerr << " (" << this->toString(10) << ")\n" << std::setbase(10);
+  cerr << " U(" << this->toString(10) << ") S(" << this->toStringSigned(10)
+       << ")\n" << std::setbase(10);
 }
 #endif






More information about the llvm-commits mailing list