What's the fastest/cleanest way to find the bit offset of the first unset bit in an array of arbitrary length?
Assume the prototype for your function is something like this size_t first_unset_bit(char unsigned const *buf, size_t bit_count, size_t start_bit);
and that it may be called several times in quick succession on the same buffer. If you can give a better prototype, please justify.
If you use assembly any assembly, please provide a x86 sample that will run on a core2 or later. I will award the answer to the solution that provides the best combination of speed and beauty.
Update0
Here's my naive implementation. I have no idea if it's actually right yet, it's not yet used in a live system.
static size_t first_unset_bit(char unsigned const *buf, size_t bit_count, size_t start_bit)
{
for (; start_bit < bit_count; ++start_bit)
{
size_t buf_index = start_bit / CHAR_BIT;
int bit_index = start_bit % CHAR_BIT;
if (!((buf[buf_index] >> bit_index) & 1))
return start_bit;
}
return -1;
}