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


基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07