[PATCH] D69032: [APInt] add wrapping support for APInt::setBits
ChenZheng via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 18 04:26:06 PST 2019
shchenz updated this revision to Diff 229806.
shchenz added a comment.
address @lebedev.ri and @craig.topper comments.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D69032/new/
https://reviews.llvm.org/D69032
Files:
llvm/include/llvm/ADT/APInt.h
llvm/unittests/ADT/APIntTest.cpp
Index: llvm/unittests/ADT/APIntTest.cpp
===================================================================
--- llvm/unittests/ADT/APIntTest.cpp
+++ llvm/unittests/ADT/APIntTest.cpp
@@ -2044,6 +2044,22 @@
EXPECT_EQ(1u, i127hi1lo1.countTrailingZeros());
EXPECT_EQ(0u, i127hi1lo1.countTrailingOnes());
EXPECT_EQ(125u, i127hi1lo1.countPopulation());
+
+ APInt i64hi1lo1wrap = APInt::getBitsSet(64, 63, 1);
+ EXPECT_EQ(1u, i64hi1lo1wrap.countLeadingOnes());
+ EXPECT_EQ(0u, i64hi1lo1wrap.countLeadingZeros());
+ EXPECT_EQ(64u, i64hi1lo1wrap.getActiveBits());
+ EXPECT_EQ(0u, i64hi1lo1wrap.countTrailingZeros());
+ EXPECT_EQ(1u, i64hi1lo1wrap.countTrailingOnes());
+ EXPECT_EQ(2u, i64hi1lo1wrap.countPopulation());
+
+ APInt i127hi1lo1wrap = APInt::getBitsSet(127, 126, 1);
+ EXPECT_EQ(1u, i127hi1lo1wrap.countLeadingOnes());
+ EXPECT_EQ(0u, i127hi1lo1wrap.countLeadingZeros());
+ EXPECT_EQ(127u, i127hi1lo1wrap.getActiveBits());
+ EXPECT_EQ(0u, i127hi1lo1wrap.countTrailingZeros());
+ EXPECT_EQ(1u, i127hi1lo1wrap.countTrailingOnes());
+ EXPECT_EQ(2u, i127hi1lo1wrap.countPopulation());
}
TEST(APIntTest, getHighBitsSet) {
Index: llvm/include/llvm/ADT/APInt.h
===================================================================
--- llvm/include/llvm/ADT/APInt.h
+++ llvm/include/llvm/ADT/APInt.h
@@ -605,7 +605,7 @@
/// \returns An APInt value with the requested bits set.
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
APInt Res(numBits, 0);
- Res.setBits(loBit, hiBit);
+ Res.setBitsWithWrap(loBit, hiBit);
return Res;
}
@@ -1431,6 +1431,21 @@
}
/// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
+ /// This function handles "wrap" case when \p loBit > \p hiBit, and calls
+ /// setBits when \p loBit <= \p hiBit.
+ void setBitsWithWrap(unsigned loBit, unsigned hiBit) {
+ assert(hiBit <= BitWidth && "hiBit out of range");
+ assert(loBit <= BitWidth && "loBit out of range");
+ if (loBit > hiBit) {
+ setLowBits(hiBit);
+ setHighBits(BitWidth - loBit);
+ return;
+ }
+ setBits(loBit, hiBit);
+ }
+
+ /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
+ /// This function handles case when \p loBit <= \p hiBit.
void setBits(unsigned loBit, unsigned hiBit) {
assert(hiBit <= BitWidth && "hiBit out of range");
assert(loBit <= BitWidth && "loBit out of range");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69032.229806.patch
Type: text/x-patch
Size: 2433 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191118/7c293b50/attachment.bin>
More information about the llvm-commits
mailing list