Sometimes a loop where the CPU spends most of the time has some branch prediction miss (misprediction) very often (near .5 probability.) I\'ve seen a few techniques on very isol
The generalization of the example you give is "replace conditional evaluation with math"; conditional-branch avoidance largely boils down to that.
What's going on with replacing &&
with &
is that, since &&
is short-circuit, it constitutes conditional evaluation in and of itself. &
gets you the same logical results if both sides are either 0 or 1, and isn't short-circuit. Same applies to ||
and |
except you don't need to make sure the sides are constrained to 0 or 1 (again, for logic purposes only, i.e. you're using the result only Booleanly).