比较SSE Intrinsics中的符号位 [英] Compare the sign bit in SSE Intrinsics
问题描述
如何使用SSE内在函数创建一个掩码,该掩码指示两个打包浮点数(__m128's)的符号是否相同,例如,如果比较a和b,其中a为[1.0 -1.0 0.0 2.0],b为[1.0 1.0 1.0 1.0]我们想要的蒙版是[true false true true].
How would one create a mask using SSE intrinsics which indicates whether the signs of two packed floats (__m128's) are the same for example if comparing a and b where a is [1.0 -1.0 0.0 2.0] and b is [1.0 1.0 1.0 1.0] the desired mask we would get is [true false true true].
推荐答案
这里是一种解决方案:
const __m128i MASK = _mm_set1_epi32(0xffffffff);
__m128 a = _mm_setr_ps(1,-1,0,2);
__m128 b = _mm_setr_ps(1,1,1,1);
__m128 f = _mm_xor_ps(a,b);
__m128i i = _mm_castps_si128(f);
i = _mm_srai_epi32(i,31);
i = _mm_xor_si128(i,MASK);
f = _mm_castsi128_ps(i);
// i = (0xffffffff, 0, 0xffffffff, 0xffffffff)
// f = (0xffffffff, 0, 0xffffffff, 0xffffffff)
在此代码段中,i
和f
将具有相同的位掩码.我假设您想要它为__m128
类型,所以我添加了f = _mm_castsi128_ps(i);
将其从__m128i
转换回.
In this snippet, both i
and f
will have the same bitmask. I assume you want it in the __m128
type so I added the f = _mm_castsi128_ps(i);
to convert it back from an __m128i
.
请注意,此代码对零的符号敏感.因此0.0
和-0.0
将影响结果.
Note that this code is sensitive to the sign of the zero. So 0.0
and -0.0
will affect the results.
说明:
代码的工作方式如下:
f = _mm_xor_ps(a,b); // xor the sign bits (well all the bits actually)
i = _mm_castps_si128(f); // Convert it to an integer. There's no instruction here.
i = _mm_srai_epi32(i,31); // Arithmetic shift that sign bit into all the bits.
i = _mm_xor_si128(i,MASK); // Invert all the bits
f = _mm_castsi128_ps(i); // Convert back. Again, there's no instruction here.
这篇关于比较SSE Intrinsics中的符号位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!