Most efficient way to check if all __m128i components are 0 [using lt;= SSE4.1 intrinsics](检查所有 __m128i 组件是否为 0 的最有效方法 [使用 lt;= SSE4.1 内在函数])
问题描述
我正在使用 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.
这篇关于检查所有 __m128i 组件是否为 0 的最有效方法 [使用 <= SSE4.1 内在函数]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查所有 __m128i 组件是否为 0 的最有效方法 [使用 <= SSE4.1 内在函数]


基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01