Beautiful branchless binary search
probablydance.com
Beautiful branchless binary search
1–10 of 198 posts
Re: Beautiful branchless binary search
#2Edit: Everybody in the comments is focusing on performance. For performance sensitive code the point is not that it's branchless, the point is that it is fast, that some branchless code is faster than branching code is an implementation detail. During encryption the point is that it certain codepaths MUST be branchless.
Re: Beautiful branchless binary search
#3Nice algorithm. I don't agree that it is branchless however. Cmov is a branching instruction for sure. And it doesn't really matter whether a branch can be well predicted. The reason you mainly want branchless code is for security reasons where a timing sidechannel could reveal information. Calling this algorithm branchless devalues the term into something meaningless. Edit: Everybody in the comments is focusing on p…
Erm, that likely depends on the industry you're in?
In HPC, branchless algorithms are often needed to use wide architectures (SIMD / GPUs) to their full capacity...
Re: Beautiful branchless binary search
#4Nice algorithm. I don't agree that it is branchless however. Cmov is a branching instruction for sure. And it doesn't really matter whether a branch can be well predicted. The reason you mainly want branchless code is for security reasons where a timing sidechannel could reveal information. Calling this algorithm branchless devalues the term into something meaningless. Edit: Everybody in the comments is focusing on p…
Re: Beautiful branchless binary search
#5Re: Beautiful branchless binary search
#6Nice algorithm. I don't agree that it is branchless however. Cmov is a branching instruction for sure. And it doesn't really matter whether a branch can be well predicted. The reason you mainly want branchless code is for security reasons where a timing sidechannel could reveal information. Calling this algorithm branchless devalues the term into something meaningless. Edit: Everybody in the comments is focusing on p…
Re: Beautiful branchless binary search
#7Nice algorithm. I don't agree that it is branchless however. Cmov is a branching instruction for sure. And it doesn't really matter whether a branch can be well predicted. The reason you mainly want branchless code is for security reasons where a timing sidechannel could reveal information. Calling this algorithm branchless devalues the term into something meaningless. Edit: Everybody in the comments is focusing on p…
That just isn’t true.
Re: Beautiful branchless binary search
#8Trusting the C++ compiler to emit a specific instruction for guaranteed performance - cute, but not realistic.
Branches can be unpredictable and slow, no matter what instructions, (or tables, or whatever) are used to introduce the branch at question.
It is predictable nature of branching in this algo that makes the difference. Which makes me wonder...
EDIT: the cmov vs explicit branching story is a bit more complicated than just branch vs no branch.
Re: Beautiful branchless binary search
#9Nice algorithm. I don't agree that it is branchless however. Cmov is a branching instruction for sure. And it doesn't really matter whether a branch can be well predicted. The reason you mainly want branchless code is for security reasons where a timing sidechannel could reveal information. Calling this algorithm branchless devalues the term into something meaningless. Edit: Everybody in the comments is focusing on p…
> And it doesn't really matter whether a branch can be well predicted.
I'm not so sure this is true. See https://lemire.me/blog/2019/10/15/mispredicted-branches-can-... as an example.
A properly predicted branch can be _faster_ than a cmov instruction, however the important point is avoiding the branch mispredict. The fact that we're using cmov is beside the point. We could achieve a similar effect by performing a subtraction and doing bitwise arithmetic to extract the sign bit (which, in effect, performs a comparison).