Apex › Systems & OS

Code with no branches (branchless / cmov)

A branch on unpredictable or secret data costs a ~15-cycle misprediction flush and leaks the condition through timing and speculative execution. Branchless code turns the condition into a full-width mask — −(a<b) is all-1 bits or all-0 bits — and selects with arithmetic: max(a,b) = a ^ ((a^b) & mask). The same operations run every time, so it’s fast (no misprediction) and constant-time (no timing side channel); hardware exposes it as the cmov instruction. Verified in node: branchless min/max/abs/select match the branchy versions across all inputs. Why constant-time crypto never branches on secrets. Build the mask and watch it select.

More in Systems & OS