[llvm] [APInt] Added APInt::clearBits() method (PR #137098)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu May 22 10:23:04 PDT 2025


================
@@ -336,6 +336,33 @@ void APInt::setBitsSlowCase(unsigned loBit, unsigned hiBit) {
     U.pVal[word] = WORDTYPE_MAX;
 }
 
+void APInt::clearBitsSlowCase(unsigned LoBit, unsigned HiBit) {
+  unsigned LoWord = whichWord(LoBit);
+  unsigned HiWord = whichWord(HiBit);
+
+  // Create an initial mask for the low word with ones below loBit.
+  uint64_t LoMask = ~(WORDTYPE_MAX << whichBit(LoBit));
+
+  // If HiBit is not aligned, we need a high mask.
+  unsigned HiShiftAmt = whichBit(HiBit);
+  if (HiShiftAmt != 0) {
+    // Create a high mask with ones above HiBit.
+    uint64_t HiMask = ~(WORDTYPE_MAX >> (APINT_BITS_PER_WORD - HiShiftAmt));
+    // If LoWord and HiWord are equal, then we combine the masks. Otherwise,
+    // set the bits in HiWord.
+    if (HiWord == LoWord)
+      LoMask &= HiMask;
----------------
topperc wrote:

I agree it should be `LoMask |= HiMask`

https://github.com/llvm/llvm-project/pull/137098


More information about the llvm-commits mailing list