最有效的方式检查所有__m128i组件是否[使用SSE内在] [英] Most efficient way to check if all __m128i components are 0 [using SSE intrinsics]
问题描述
我使用SSE内在函数确定一个矩形(由四个 int32
值定义)是否已更改:
I am using SSE intrinsics to determine if a rectangle (defined by four int32
values) has changed:
__m128i oldRect; // contains old left, top, right, bottom packed to 128 bits
__m128i newRect; // contains new left, top, right, bottom packed to 128 bits
__m128i xor = _mm_xor_si128(oldRect, newRect);
此时,生成的 xor
如果矩形没有改变,将全部为零。
At this point, the resulting xor
value will be all zeros if the rectangle hasn't changed. What is then the most efficient way of determining that?
目前我正在这样做:
if (xor.m128i_u64[0] | xor.m128i_u64[1])
{
// rectangle changed
}
但我假设有一个更聪明的方式(可能使用我还没有找到的一些SSE指令)。
But I assume there's a smarter way (possibly using some SSE instruction that I haven't found yet).
我在x64上定位SSE4.1,我在Visual Studio 2013中编写C ++。
I am targeting SSE4.1 on x64 and I am coding C++ in Visual Studio 2013.
推荐答案
可以通过 _mm_testz_si128 内在(SSE4.1)使用PTEST功能,如下所示:
You can use the PTEST instuction via the _mm_testz_si128 intrinsic (SSE4.1), like this:
#include "smmintrin.h" // SSE4.1 header
if (!_mm_testz_si128(xor, xor))
{
// rectangle has changed
}
注意,如果两个参数的位 AND
为零, _mm_testz_si128
会返回1。
Note that _mm_testz_si128
returns 1 if the bitwise AND
of the two arguments is zero.
这篇关于最有效的方式检查所有__m128i组件是否[使用SSE内在]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!