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 内在函数]
基础教程推荐
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- c++ STL设置差异 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 提升 ASIO 流缓冲 2021-01-01
