1. Fix a bug in fromString for the <= 64bits case
authorReid Spencer <rspencer@reidspencer.com>
Sat, 24 Feb 2007 20:19:37 +0000 (20:19 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Sat, 24 Feb 2007 20:19:37 +0000 (20:19 +0000)
2. Fix shl when shiftAmount == BitWidth.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34560 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/APInt.cpp

index 63bc5f55b3e350cc3e777663d806c221d9cc733b..7f2aa4292472d02459c0139e3a0faf68a5adf34c 100644 (file)
@@ -945,24 +945,33 @@ APInt APInt::lshr(uint32_t shiftAmt) const {
 /// Left-shift this APInt by shiftAmt.
 /// @brief Left-shift function.
 APInt APInt::shl(uint32_t shiftAmt) const {
+  assert(shiftAmt <= BitWidth && "Invalid shift amount");
   APInt API(*this);
-  if (API.isSingleWord())
-    API.VAL <<= shiftAmt;
-  else if (shiftAmt >= API.BitWidth)
-    memset(API.pVal, 0, API.getNumWords() * APINT_WORD_SIZE);
-  else {
-    if (uint32_t offset = shiftAmt / APINT_BITS_PER_WORD) {
-      for (uint32_t i = API.getNumWords() - 1; i > offset - 1; --i)
-        API.pVal[i] = API.pVal[i-offset];
-      memset(API.pVal, 0, offset * APINT_WORD_SIZE);
-    }
-    shiftAmt %= APINT_BITS_PER_WORD;
-    uint32_t i;
-    for (i = API.getNumWords() - 1; i > 0; --i)
-      API.pVal[i] = (API.pVal[i] << shiftAmt) | 
-                    (API.pVal[i-1] >> (APINT_BITS_PER_WORD - shiftAmt));
-    API.pVal[i] <<= shiftAmt;
+  if (API.isSingleWord()) {
+    if (shiftAmt == BitWidth)
+      API.VAL = 0;
+    else 
+      API.VAL <<= shiftAmt;
+    API.clearUnusedBits();
+    return API;
   }
+
+  if (shiftAmt == BitWidth) {
+    memset(API.pVal, 0, getNumWords() * APINT_WORD_SIZE);
+    return API;
+  }
+
+  if (uint32_t offset = shiftAmt / APINT_BITS_PER_WORD) {
+    for (uint32_t i = API.getNumWords() - 1; i > offset - 1; --i)
+      API.pVal[i] = API.pVal[i-offset];
+    memset(API.pVal, 0, offset * APINT_WORD_SIZE);
+  }
+  shiftAmt %= APINT_BITS_PER_WORD;
+  uint32_t i;
+  for (i = API.getNumWords() - 1; i > 0; --i)
+    API.pVal[i] = (API.pVal[i] << shiftAmt) | 
+                  (API.pVal[i-1] >> (APINT_BITS_PER_WORD - shiftAmt));
+  API.pVal[i] <<= shiftAmt;
   API.clearUnusedBits();
   return API;
 }
@@ -1423,7 +1432,10 @@ void APInt::fromString(uint32_t numbits, const char *str, uint32_t slen,
       *this *= apradix;
 
     // Add in the digit we just interpreted
-    apdigit.pVal[0] = digit;
+    if (apdigit.isSingleWord())
+      apdigit.VAL = digit;
+    else
+      apdigit.pVal[0] = digit;
     *this += apdigit;
   }
 }