by jcbages · 4y ago · 0 comments
A well predicted branch costs 0. This is the best case and why branching code can sometimes be faster.
An instruction that needs a previous instruction to compute has some pipeline stalls. This is slower than the above and why branchless code can be slower.
A mispredicted branch costs a full pipeline flush. The worst case. This is why we avoid branches but we shouldn't do so blindly since it might not be common in a given program.
So it depends. I discourage removing branches in general unless you have benchmarks to back it up.