检查所有 __m128i 组件是否为 0 的最有效方法 [使用 <= SSE4.1 内在函数]

2022-01-14 00:00:00 simd integer c++ intrinsics sse

我正在使用 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.

问题与 __m128i 变量是否为零?,因为它指定了在 SSE-2 和更早的处理器上"(尽管安东尼奥确实添加了一个答案为了完整性",在发布和回答这个问题后的某个时间解决了 4.1).p>

The question is not quite the same as Is an __m128i variable zero?, as that specifies "on SSE-2-and-earlier processors" (although Antonio did add an answer "for completeness" that addresses 4.1 some time after this question was posted and answered).

推荐答案

您可以通过 _mm_testz_si128 内在(SSE4.1),像这样:

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.

相关文章