Re:Beautiful branchless binary search
by secondcoming on Friday April 28, 2023 @01:12PM
(#35744198)
Most topics I've read/viewed on branchless stuff seems to boil down to doing arithmetic with bools. So your code above can be thought of as:
bool b = compare(begin[step], value);
begin += (step * b);
Re:Beautiful branchless binary search
by Paul-Craft on Friday April 28, 2023 @01:32PM
(#35744463)
Yeah, I got that. That was the part u/2102922286's comment made click for me. I guess I just don't do enough low level stuff for that to be something that automatically occurs to me.
> You can achieve the same effect as cmov by using bitwise operations and a subtraction, though it'd just be a few cycles slower--but it would be even more clear that it doesn't branch.
BTW, I hate how the author doesn't use braces with this `if` statement. Many a production bug, including one in OSX, as I recall, have occurred because someone didn't want to type a couple extra characters.