4 ms·
Thank you for this comment! I was sitting here, metaphorically scratching my head, trying to figure out how the hell you can call this branchless when there's
by Paul-Craft 4y ago
Thank you for this comment! I was sitting here, metaphorically scratching my head, trying to figure out how the hell you can call this branchless when there's clearly an `if` in there:
for (step /= 2; step != 0; step /= 2) {
if (compare(begin[step], value))
begin += step;
}
Your comment, especially this part, made it click for 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.
- secondcoming 4y agoMost 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);
- Paul-Craft 4y agoYeah, 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.