What techniques to avoid conditional branching do you know?

前端 未结 9 507
迷失自我
迷失自我 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:26

    I believe the most common way to avoid branching is to leverage bit parallelism in reducing the total jumps present in your code. The longer the basic blocks, the less often the pipeline is flushed.

    As someone else has mentioned, if you want to do more than unrolling loops, and providing branch hints, you're going to want to drop into assembly. Of course this should be done with utmost caution: your typical compiler can write better assembly in most cases than a human. Your best hope is to shave off rough edges, and make assumptions that the compiler cannot deduce.

    Here's an example of the following C code:

    if (b > a) b = a;
    

    In assembly without any jumps, by using bit-manipulation (and extreme commenting):

    sub eax, ebx ; = a - b
    sbb edx, edx ; = (b > a) ? 0xFFFFFFFF : 0
    and edx, eax ; = (b > a) ? a - b : 0
    add ebx, edx ; b = (b > a) ? b + (a - b) : b + 0
    

    Note that while conditional moves are immediately jumped on by assembly enthusiasts, that's only because they're easily understood and provide a higher level language concept in a convenient single instruction. They are not necessarily faster, not available on older processors, and by mapping your C code into corresponding conditional move instructions you're just doing the work of the compiler.

提交回复
热议问题