by tzs · 4y ago · 0 comments
I wonder how using that would compare?
You'd find the leftmost bit, invoke the func_true for that, and clear the leftmost bit.
Then loop finding the leftmost bit, invoking the func_false for the bits between the current leftmost bit and the previous leftmost bit, func_true for the leftmost bit, and then clear that bit.
I wouldn't expect it to be dramatically different, but it is trading test and clear on the off bits and a conditional branch off of the result for a loop calling func_false a predetermined number of times. One of those options might be a little faster than the other.
[1] https://en.wikipedia.org/wiki/Find_first_set#Hardware_suppor...
[2] https://en.wikipedia.org/wiki/Find_first_set#Tool_and_librar...
What I was suggesting was something like this (in pseudocode):
int previous_leftmost = 64;
while (mask != 0) {
leftmost = find_leftmost(mask);
mask ^= 1u << leftmost;
while (--previous_leftmost > leftmost)
func_false(previous_leftmost);
func_true(leftmost);
}