What techniques to avoid conditional branching do you know?

前端 未结 9 524
迷失自我
迷失自我 2021-02-03 23:47

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

9条回答
  •  无人共我
    2021-02-04 00:34

    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).

提交回复
热议问题