SSE比较本质-如何从比较中获得1或0? [英] SSE Comparison Intrinsics - How to get 1 or 0 from a comparison?
问题描述
我正在尝试使用SSE内在函数编写等价的if语句。
I am trying to write the equivalent of an if statement with SSE intrinsics.
我正在使用 __ m128 _mm_cmplt_ps(__ m128 a,__m128 b )
进行比较< b,如果比较分别为true或false,则返回 0xffffffff
或 0x0
。我想将这些值转换为1和0。为此,实现逻辑和 __ m128 _mm_and_ps(__ m128 c,__m128 d)
是否正确? ,其中 c
是转换的结果, d
是例如 0xffffffff
?
I am using __m128 _mm_cmplt_ps(__m128 a, __m128 b)
to do the comparison a < b, and this returns 0xffffffff
or 0x0
if the comparison was respectively true or false. I would like to convert these values into 1 and 0. In order to do this, is it correct to implement the logical "and" __m128 _mm_and_ps(__m128 c , __m128 d)
, where c
is the result of the conversion and d
is, e.g., 0xffffffff
?
谢谢您的关注。
推荐答案
您正在比较要获得 1
或 0
,然后乘以另一个数字。基本上就是这样:
You're comparing to get a 1
or a 0
, then multiplying by another number. That is essentially this:
c = (a < b) * d;
与此相同:
c = 0;
if (a < b)
c = d;
这也称为有条件移动。
如果这就是您想要的,那么您就不需要 0
或 1
。
If that's what you want, then you don't need the 0
or the 1
. Just AND the result of the compare directly with the number you will multiply with.
__m128 c = _mm_cmplt_ps(a,b);
c = _mm_and_ps(c,d);
之所以有用,是因为比较结果全部返回了 0
或所有 1
。因此,将它们与结果进行与运算将使其为零,或完全保留该结果。
This works because the comparison returns either all 0
's or all 1
's. So ANDing them with the result will either zero it, or keep it entirely.
这是有意设计的。不需要乘法。
It was intentionally designed that way. There's no need for a multiplication.
这篇关于SSE比较本质-如何从比较中获得1或0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!