测试两个 __m128i 变量之间的相等性 [英] Testing equality between two __m128i variables
问题描述
如果我想在两个 __m128i
变量之间进行按位相等测试,我需要使用 SSE 指令还是可以使用 ==
?如果不是,我应该使用哪个 SSE 指令?
If I want to do a bitwise equality test between two __m128i
variables, am I required to use an SSE instruction or can I use ==
? If not, which SSE instruction should I use?
推荐答案
虽然使用 _mm_movemask_epi8
是一种解决方案,但如果您的处理器采用 SSE4.1 我认为更好的解决方案是使用指令它在 FLAGS 寄存器中设置零或进位标志.这可以节省test
或 cmp
指令.
Although using _mm_movemask_epi8
is one solution, if you have a processor with SSE4.1 I think a better solution is to use an instruction which sets the zero or carry flag in the FLAGS register. This saves a test
or cmp
instruction.
要做到这一点,你可以这样做:
To do this you could do this:
if(_mm_test_all_ones(_mm_cmpeq_epi8(v1,v2))) {
//v0 == v1
}
正如 Paul R 指出的,_mm_test_all_ones
生成两条指令:pcmpeqd
和 ptest
.使用 _mm_cmpeq_epi8
总共是三个指令.这是一个更好的解决方案,它总共只使用两条指令:
as Paul R pointed out _mm_test_all_ones
generates two instructions: pcmpeqd
and ptest
. With _mm_cmpeq_epi8
that's three instructions total. Here's a better solution which only uses two instructions in total:
__m128i neq = _mm_xor_si128(v1,v2);
if(_mm_test_all_zeros(neq,neq)) {
//v0 == v1
}
这会产生
pxor %xmm1, %xmm0
ptest %xmm0, %xmm0
这篇关于测试两个 __m128i 变量之间的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!