[PATCH] D31802: Add methods to find the next *unset* bits in a bit vector

Zachary Turner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 10 11:07:15 PDT 2017


zturner added inline comments.


================
Comment at: llvm/include/llvm/ADT/BitVector.h:206
+    BitWord Copy = Bits[WordPos];
+    // Mask in previous bits.
+    BitWord Mask = (1 << BitPos) - 1;
----------------
dberlin wrote:
> Remind me what endianness we use ?
> I would have expected it to simply be << some number of bits, then countleadingones.
> It looks reversed, so i'm assuming i'm thinking the wrong way around :)
> 
Yea, we had this same confusion last time.  Didn't you write this thing!  :)

If you want the 0'th bit, it's the rightmost bit.  So using an example, say `Prev` is 4, which means you've got something like XXXXXXXXXXXXYYYY where the Y's are the bits you don't care about, and the rightmost X is the bit you want to start searching from, leftwards.  So, we set all the Y bits to 1 by producing the bit pattern 10000 and then subtracting 1, and then or that in.  Then we count the number of trailing 1s.

The check below that looks if there is at least one zero-bit, and if there is it finds the last one by counting the number of trailing 1s.  But the confusion doesn't stop there, because if your word size is, say, 64 bits, but your BitVector only represents 10 bits, then then the left most 54 bits are going to be 0 even though they are out of bounds.  So we need the `Result < size()` check there as well.


https://reviews.llvm.org/D31802





More information about the llvm-commits mailing list