Multiplying by booleans faster than if/else

Googling that revealed a trove of hits explaining this issue. :roll_eyes:

…I need to look for some sort of reference about low level coding optimizations of this nature.

Wikipedia article on predication has a good summary of the tradeoffs. Compilers typically have cost models or heuristics for figuring out what is more efficient. The models are always bets, and don’t always win.

Another point is that for a compiler to convert if-else to branchless code, it must be able to prove that the untaken branch code won’t fault. E.g. doing x == 0 ? 0 : y % x without a branch requires hardware that can suppress the divide-by-zero fault when x==0. The x86 cmov (conditional move) instruction was notably irksome in this regard: if used to implement “if address is non-null, load from it”, it nonetheless faulted for a null address, because the address generation and checking happened unconditionally without suppression.

3 Likes

If you’re investigating how to write the highest performing code possible, then some study of assembly language and the machine architecture will be useful.

The absolute best resources for that are the web pages of Agner Fog

3 Likes

Thanks for the pointer. Great content there.