-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[APInt] Added APInt::clearBits() method #137098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a17519a
bbf8c16
6700a18
e85f09c
2d8fa9d
cfbf324
28cb871
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1413,6 +1413,26 @@ class [[nodiscard]] APInt { | |||||
| U.pVal[whichWord(BitPosition)] &= Mask; | ||||||
| } | ||||||
|
|
||||||
| /// Clear the bits from loBit (inclusive) to hiBit (exclusive) to 0. | ||||||
| /// This function handles case when \p loBit <= \p hiBit. | ||||||
| void clearBits(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 < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) { | ||||||
|
||||||
| if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) { | |
| if (hiBit <= APINT_BITS_PER_WORD) { |
RKSimon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LoBit + HiBit
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2520,6 +2520,62 @@ TEST(APIntTest, setAllBits) { | |
| EXPECT_EQ(128u, i128.popcount()); | ||
| } | ||
|
|
||
| TEST(APIntTest, clearBits) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip: tests might be shorter and clearer with the string‑based APInt hi = APInt::getAllOnes(64);
hi.clearBits(0, 32);
EXPECT_EQ(APInt(64, "FFFFFFFF00000000", 16), hi);There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thats very helpful thanks for the tip! |
||
| APInt i32 = APInt::getAllOnes(32); | ||
| i32.clearBits(1, 3); | ||
| EXPECT_EQ(1u, i32.countr_one()); | ||
| EXPECT_EQ(0u, i32.countr_zero()); | ||
| EXPECT_EQ(32u, i32.getActiveBits()); | ||
| EXPECT_EQ(0u, i32.countl_zero()); | ||
| EXPECT_EQ(29u, i32.countl_one()); | ||
| EXPECT_EQ(30u, i32.popcount()); | ||
|
|
||
| i32.clearBits(15, 15); | ||
| EXPECT_EQ(1u, i32.countr_one()); | ||
| EXPECT_EQ(0u, i32.countr_zero()); | ||
| EXPECT_EQ(32u, i32.getActiveBits()); | ||
| EXPECT_EQ(0u, i32.countl_zero()); | ||
| EXPECT_EQ(29u, i32.countl_one()); | ||
| EXPECT_EQ(30u, i32.popcount()); | ||
|
|
||
| i32.clearBits(28, 31); | ||
| EXPECT_EQ(1u, i32.countr_one()); | ||
| EXPECT_EQ(0u, i32.countr_zero()); | ||
| EXPECT_EQ(32u, i32.getActiveBits()); | ||
| EXPECT_EQ(0u, i32.countl_zero()); | ||
| EXPECT_EQ(1u, i32.countl_one()); | ||
| EXPECT_EQ(27u, i32.popcount()); | ||
| EXPECT_EQ(static_cast<uint64_t>((1 << 31) | ((~0u >> 4) & (~0u << 3)) | 1), | ||
| i32.getZExtValue()); | ||
|
|
||
| APInt i256 = APInt::getAllOnes(256); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please can you add an additional case of > 64 bits that isn't a multiple of 64 bits (e.g. APInt::getAllOnes(311))? This will test clearBitsSlowCase more thoroughly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I'll add that test case. |
||
| i256.clearBits(10, 250); | ||
| EXPECT_EQ(10u, i256.countr_one()); | ||
| EXPECT_EQ(0u, i256.countr_zero()); | ||
| EXPECT_EQ(256u, i256.getActiveBits()); | ||
| EXPECT_EQ(0u, i256.countl_zero()); | ||
| EXPECT_EQ(6u, i256.countl_one()); | ||
| EXPECT_EQ(16u, i256.popcount()); | ||
|
|
||
| APInt i64hi32 = APInt::getAllOnes(64); | ||
| i64hi32.clearBits(0, 32); | ||
| EXPECT_EQ(32u, i64hi32.countl_one()); | ||
| EXPECT_EQ(0u, i64hi32.countl_zero()); | ||
| EXPECT_EQ(64u, i64hi32.getActiveBits()); | ||
| EXPECT_EQ(32u, i64hi32.countr_zero()); | ||
| EXPECT_EQ(0u, i64hi32.countr_one()); | ||
| EXPECT_EQ(32u, i64hi32.popcount()); | ||
|
|
||
| i64hi32 = APInt::getAllOnes(64); | ||
| i64hi32.clearBits(32, 64); | ||
| EXPECT_EQ(32u, i64hi32.countr_one()); | ||
| EXPECT_EQ(0u, i64hi32.countr_zero()); | ||
| EXPECT_EQ(32u, i64hi32.getActiveBits()); | ||
| EXPECT_EQ(32u, i64hi32.countl_zero()); | ||
| EXPECT_EQ(0u, i64hi32.countl_one()); | ||
| EXPECT_EQ(32u, i64hi32.popcount()); | ||
| } | ||
|
|
||
| TEST(APIntTest, getLoBits) { | ||
| APInt i32(32, 0xfa); | ||
| i32.setHighBits(1); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix argument names in this comment.