比较SSE Intrinsics中的符号位 [英] Compare the sign bit in SSE Intrinsics

查看:128
本文介绍了比较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)

在此代码段中,if将具有相同的位掩码.我假设您想要它为__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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆