I just came across this article: Compute the minimum or maximum of two integers without branching
It starts with "[o]n some rare machines where branching is expensive...".
I used to think that branching is always expensive as it often forces the processor to clear and restart its execution pipeline (e.g. see Why is it faster to process a sorted array than an unsorted array?).
This leaves me with a couple of questions:
- Did the writer of the article get that part wrong? Or was this article maybe written in a time before branching was an issue (I can't find a date on it).
- Do modern processors have a way to complete minimal branches like the one in
(x < y) ? x : y
without performance degradation? - Or do all modern compilers simply implement this hack automatically? Specifically, what does Java do? Especially since its
Math.min(...)
function is just that ternary statement...