From bd9fb2ae959dc2bc0a2a6a309b56ea239d41797e Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Fri, 24 Feb 2017 17:46:18 +0000 Subject: [APInt] Add APInt::extractBits() method to extract APInt subrange The current pattern for extract bits in range is typically: Mask.lshr(BitOffset).trunc(SubSizeInBits); Which can be particularly slow for large APInts (MaskSizeInBits > 64) as they require the allocation of memory for the temporary variable. This is another of the compile time issues identified in PR32037 (see also D30265). This patch adds the APInt::extractBits() helper method which avoids the temporary memory allocation. Differential Revision: https://reviews.llvm.org/D30336 llvm-svn: 296141 --- llvm/unittests/ADT/APIntTest.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'llvm/unittests/ADT') diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index 39a88cdca90..e8923b6ac01 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -1434,6 +1434,18 @@ TEST(APIntTest, isMask) { } } +TEST(APIntTest, extractBits) { + APInt i32(32, 0x1234567); + EXPECT_EQ(0x3456, i32.extractBits(16, 4)); + + APInt i256(256, -16776961 /* 0xFFFFFFFFFFFFFFFFFFFFFFFFFF0000FF */, true); + EXPECT_EQ(255, i256.extractBits(16, 0)); + EXPECT_EQ(127, i256.extractBits(16, 1)); + EXPECT_EQ(-1, i256.extractBits(32, 64).getSExtValue()); + EXPECT_EQ(-1, i256.extractBits(128, 128).getSExtValue()); + EXPECT_EQ(-8388481, i256.extractBits(128, 1).getSExtValue()); +} + #if defined(__clang__) // Disable the pragma warning from versions of Clang without -Wself-move #pragma clang diagnostic push -- cgit v1.2.3