summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/include/llvm/ADT/APInt.h6
-rw-r--r--llvm/unittests/ADT/APIntTest.cpp30
2 files changed, 16 insertions, 20 deletions
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 0f82654a88f..e08e28c99ae 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1360,13 +1360,9 @@ public:
void setBits(unsigned loBit, unsigned hiBit) {
assert(hiBit <= BitWidth && "hiBit out of range");
assert(loBit <= BitWidth && "loBit out of range");
+ assert(loBit <= hiBit && "loBit greater than hiBit");
if (loBit == hiBit)
return;
- if (loBit > hiBit) {
- setLowBits(hiBit);
- setHighBits(BitWidth - loBit);
- return;
- }
if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) {
uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
mask <<= loBit;
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 2235f271658..bb6cf35fe9e 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -1723,21 +1723,21 @@ TEST(APIntTest, getLowBitsSet) {
}
TEST(APIntTest, getBitsSet) {
- APInt i64hi1lo1 = APInt::getBitsSet(64, 63, 1);
- EXPECT_EQ(1u, i64hi1lo1.countLeadingOnes());
- EXPECT_EQ(0u, i64hi1lo1.countLeadingZeros());
- EXPECT_EQ(64u, i64hi1lo1.getActiveBits());
- EXPECT_EQ(0u, i64hi1lo1.countTrailingZeros());
- EXPECT_EQ(1u, i64hi1lo1.countTrailingOnes());
- EXPECT_EQ(2u, i64hi1lo1.countPopulation());
-
- APInt i127hi1lo1 = APInt::getBitsSet(127, 126, 1);
- EXPECT_EQ(1u, i127hi1lo1.countLeadingOnes());
- EXPECT_EQ(0u, i127hi1lo1.countLeadingZeros());
- EXPECT_EQ(127u, i127hi1lo1.getActiveBits());
- EXPECT_EQ(0u, i127hi1lo1.countTrailingZeros());
- EXPECT_EQ(1u, i127hi1lo1.countTrailingOnes());
- EXPECT_EQ(2u, i127hi1lo1.countPopulation());
+ APInt i64hi1lo1 = APInt::getBitsSet(64, 1, 63);
+ EXPECT_EQ(0u, i64hi1lo1.countLeadingOnes());
+ EXPECT_EQ(1u, i64hi1lo1.countLeadingZeros());
+ EXPECT_EQ(63u, i64hi1lo1.getActiveBits());
+ EXPECT_EQ(1u, i64hi1lo1.countTrailingZeros());
+ EXPECT_EQ(0u, i64hi1lo1.countTrailingOnes());
+ EXPECT_EQ(62u, i64hi1lo1.countPopulation());
+
+ APInt i127hi1lo1 = APInt::getBitsSet(127, 1, 126);
+ EXPECT_EQ(0u, i127hi1lo1.countLeadingOnes());
+ EXPECT_EQ(1u, i127hi1lo1.countLeadingZeros());
+ EXPECT_EQ(126u, i127hi1lo1.getActiveBits());
+ EXPECT_EQ(1u, i127hi1lo1.countTrailingZeros());
+ EXPECT_EQ(0u, i127hi1lo1.countTrailingOnes());
+ EXPECT_EQ(125u, i127hi1lo1.countPopulation());
}
TEST(APIntTest, getHighBitsSet) {
OpenPOWER on IntegriCloud