问题描述
如果您正在编写一些将由另一个程序运行的SIMD代码,是否总是有利于摆脱分支以提高性能?我听说甚至做额外的操作只是为了避免 if / else
语句等仍然快得多。
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?
推荐答案
大多数SIMD架构都有特殊的指令,可以有条件地根据掩码向量选择元素。掩码向量通常是SIMD比较指令的结果。所以是的,很容易摆脱你在上面的例子中的那种分支。
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.
这篇关于与SIMD的偏好总是摆脱分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!