Fix APInt::operator*= so that it computes the correct result for large integers where...
authorEli Friedman <eli.friedman@gmail.com>
Fri, 7 Oct 2011 23:40:49 +0000 (23:40 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Fri, 7 Oct 2011 23:40:49 +0000 (23:40 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141441 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/APFloat.cpp
lib/Support/APInt.cpp
unittests/ADT/APIntTest.cpp

index b32dfeb9566046d14935d9b5ae14da744c7208e4..5307829ed83b2208eb19006debb701c5a3a2c45e 100644 (file)
@@ -3455,7 +3455,7 @@ void APFloat::toString(SmallVectorImpl<char> &Str,
     //                 <= semantics->precision + e * 137 / 59
     //   (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
 
-    unsigned precision = semantics->precision + 137 * texp / 59;
+    unsigned precision = semantics->precision + (137 * texp + 136) / 59;
 
     // Multiply significand by 5^e.
     //   N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
index 6eadaafd5f206cec472eeb3cf395676188dfb775..3774c5223c46db0fed27461a0e6c2dbf59158afa 100644 (file)
@@ -386,6 +386,7 @@ APInt& APInt::operator*=(const APInt& RHS) {
   clearAllBits();
   unsigned wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords;
   memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE);
+  clearUnusedBits();
 
   // delete dest array and return
   delete[] dest;
@@ -471,7 +472,7 @@ APInt APInt::operator*(const APInt& RHS) const {
     return APInt(BitWidth, VAL * RHS.VAL);
   APInt Result(*this);
   Result *= RHS;
-  return Result.clearUnusedBits();
+  return Result;
 }
 
 APInt APInt::operator+(const APInt& RHS) const {
index 11bb5e15455b9c09d7fb0349fab443ff1720a5a6..490811deb8f93d85f2515e69c70010747f9d3d20 100644 (file)
@@ -441,4 +441,13 @@ TEST(APIntTest, StringDeath) {
 #endif
 #endif
 
+TEST(APIntTest, mul_clear) {
+  APInt ValA(65, -1ULL);
+  APInt ValB(65, 4);
+  APInt ValC(65, 0);
+  ValC = ValA * ValB;
+  ValA *= ValB;
+  EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false));
+}
+
 }