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 内在函数]


- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 从python回调到c++的选项 2022-11-16