Is the preference with SIMD to always get rid of b

2019-02-25 22:59发布

If you are writing some SIMD code that will be run by another program, is it always favorable to get rid of branching to increase performance? I heard that even doing extra operations just to avoid if/else statements, etc is still much faster.

I ask this because I have some branching that I do that's basically like this:

//  axis; x=0, y=1, z=2

float p, q;
if (axis == 0)
{
    p = point.y;
    q = point.z;
}
else if (axis == 1)
{
    p = point.x;
    q = point.z;
}
else if (axis == 2)
{
    p = point.x;
    q = point.y;
}

Can I avoid this kind of branching with some clever trick?

1条回答
神经病院院长
2楼-- · 2019-02-25 23:19

Most SIMD architectures have special instructions which let you conditionally select elements based on a mask vector. The mask vector is typically the result of a SIMD compare instruction. So yes, it's pretty easy to get rid of the kind of branches that you have in your example above.

Whether you actually need to get rid of any given branch though will depend on various factors such as the predictability of the branch, the nature (statistics) of the data, and how much code is executed conditionally. As a rule of thumb, branchless is good, but as with most rules there are exceptions.

查看更多
登录 后发表回答