diff options
author | Simon Pilgrim <llvm-dev@redking.me.uk> | 2017-02-24 17:46:18 +0000 |
---|---|---|
committer | Simon Pilgrim <llvm-dev@redking.me.uk> | 2017-02-24 17:46:18 +0000 |
commit | bd9fb2ae959dc2bc0a2a6a309b56ea239d41797e (patch) | |
tree | f6b76d75fb74cca216bc2f37094c6d97c0ba4d13 /llvm/unittests/ADT | |
parent | 24a1bedf765fd2dab8384073c9ac930563fa0110 (diff) | |
download | bcm5719-llvm-bd9fb2ae959dc2bc0a2a6a309b56ea239d41797e.tar.gz bcm5719-llvm-bd9fb2ae959dc2bc0a2a6a309b56ea239d41797e.zip |
[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
Diffstat (limited to 'llvm/unittests/ADT')
-rw-r--r-- | llvm/unittests/ADT/APIntTest.cpp | 12 |
1 files changed, 12 insertions, 0 deletions
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 |