Add some comments to yr_bitmask_find_non_colliding_offset.
@@ -39,7 +39,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Finds the smaller offset within bitmask A where bitmask B can be accommodated
// without bit collisions. A collision occurs when bots bitmasks have a bit set
// to 1 at the same offset. This function assumes that the first bit in B is 1
-// and do optmizations that rely on that.
+// and do optimizations that rely on that.
//
// The function also receives a pointer to an uint64_t where the function stores
// a value that is used for speeding-up subsequent searches over the same
@@ -68,8 +68,13 @@ uint32_t yr_bitmask_find_non_colliding_offset(
{
uint32_t i, j, k;
+ // Ensure that the first bit of bitmask B is set, as this function does some
+ // optimizations that rely on that.
assert(yr_bitmask_isset(b, 0));
+ // Skip all slots that are filled with 1s. It's safe to do that because the
+ // first bit of B is 1, so we won't be able to accommodate B at any offset
+ // within such slots.
for (i = *off_a / YR_BITMASK_SLOT_BITS;
i <= len_a / YR_BITMASK_SLOT_BITS && a[i] == 0xFFFFFFFFFFFFFFFFL;
i++);
@@ -78,6 +83,7 @@ uint32_t yr_bitmask_find_non_colliding_offset(
for (; i <= len_a / YR_BITMASK_SLOT_BITS; i++)
{
+ // The slot is filled with 1s, we can safely skip it.
if (a[i] == 0xFFFFFFFFFFFFFFFFL)
continue;
