[llvm] r325311 - [APInt] Fix extractBits to correctly handle Result.isSingleWord() case.

Tim Shen via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 15 17:44:36 PST 2018


Author: timshen
Date: Thu Feb 15 17:44:36 2018
New Revision: 325311

URL: http://llvm.org/viewvc/llvm-project?rev=325311&view=rev
Log:
[APInt] Fix extractBits to correctly handle Result.isSingleWord() case.

Summary: extractBits assumes that `!this->isSingleWord() implies !Result.isSingleWord()`, which may not necessarily be true. Handle both cases.

Reviewers: RKSimon

Subscribers: sanjoy, llvm-commits, hiraditya

Differential Revision: https://reviews.llvm.org/D43363

Modified:
    llvm/trunk/lib/Support/APInt.cpp
    llvm/trunk/unittests/ADT/APIntTest.cpp

Modified: llvm/trunk/lib/Support/APInt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APInt.cpp?rev=325311&r1=325310&r2=325311&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APInt.cpp (original)
+++ llvm/trunk/lib/Support/APInt.cpp Thu Feb 15 17:44:36 2018
@@ -428,11 +428,12 @@ APInt APInt::extractBits(unsigned numBit
   unsigned NumSrcWords = getNumWords();
   unsigned NumDstWords = Result.getNumWords();
 
+  uint64_t *DestPtr = Result.isSingleWord() ? &Result.U.VAL : Result.U.pVal;
   for (unsigned word = 0; word < NumDstWords; ++word) {
     uint64_t w0 = U.pVal[loWord + word];
     uint64_t w1 =
         (loWord + word + 1) < NumSrcWords ? U.pVal[loWord + word + 1] : 0;
-    Result.U.pVal[word] = (w0 >> loBit) | (w1 << (APINT_BITS_PER_WORD - loBit));
+    DestPtr[word] = (w0 >> loBit) | (w1 << (APINT_BITS_PER_WORD - loBit));
   }
 
   return Result.clearUnusedBits();

Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=325311&r1=325310&r2=325311&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Thu Feb 15 17:44:36 2018
@@ -1768,6 +1768,13 @@ TEST(APIntTest, extractBits) {
             i257.extractBits(128, 1).getSExtValue());
   EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full),
             i257.extractBits(129, 1).getSExtValue());
+
+  EXPECT_EQ(APInt(48, 0),
+            APInt(144, "281474976710655", 10).extractBits(48, 48));
+  EXPECT_EQ(APInt(48, 0x0000ffffffffffffull),
+            APInt(144, "281474976710655", 10).extractBits(48, 0));
+  EXPECT_EQ(APInt(48, 0x00007fffffffffffull),
+            APInt(144, "281474976710655", 10).extractBits(48, 1));
 }
 
 TEST(APIntTest, getLowBitsSet) {




More information about the llvm-commits mailing list