From 6e5a3d77a3cf64924f592fa127fcc27e815ceb6b Mon Sep 17 00:00:00 2001 From: Tim Shen Date: Fri, 16 Feb 2018 01:44:36 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325311 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/APInt.cpp | 3 ++- unittests/ADT/APIntTest.cpp | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index 1ea6319acfa..48b9613b3ec 100644 --- a/lib/Support/APInt.cpp +++ b/lib/Support/APInt.cpp @@ -428,11 +428,12 @@ APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const { 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(); diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp index 05fad386064..331b3997071 100644 --- a/unittests/ADT/APIntTest.cpp +++ b/unittests/ADT/APIntTest.cpp @@ -1768,6 +1768,13 @@ TEST(APIntTest, extractBits) { i257.extractBits(128, 1).getSExtValue()); EXPECT_EQ(static_cast(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) {