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

Liam Semeria via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 28 11:43:34 PDT 2025


================
@@ -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) {
+      uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
----------------
liamsemeria wrote:

Can you explain a bit more how that could cause an out-of-range-shift?
The maximum hiBit value and minimum lowBit value would result in a shift by 0.

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


More information about the llvm-commits mailing list