Hacker News new | ask | show | jobs
by menaerus 1103 days ago
Inserting branches to your code can only make it slower or equally ~fast but not faster than branchless code so what you say doesn't make a lot of sense.
2 comments

Not necessarily. If the branch is predictable, it may be better than branchless, particularly if the alternative is a cmov. From Agner:

> As a rule of thumb, we can say that a conditional jump is faster than a conditional move if the code is part of a dependency chain and the prediction rate is better than 75%. A conditional jump is also preferred if we can avoid a lengthy calculation [...] when the other operand is chosen

Bounds check out of the tight loop can give the possibility of the compiler dropping bounds check and/or related branches later.
This sounds purely theoretical. And still even if compiler would be able to coalesce multiple branches into less, such branchy code still cannot be faster than the branchless one.