SSE内在-_mm_and_ps奇数行为

SSE intrinsics - _mm_and_ps odd behaviour

本文关键字:ps and 内在 mm SSE      更新时间:2023-10-16

以下代码:

__m128 a   = _mm_setr_ps( 1, 2, 3, 4 );
__m128 b   = _mm_set1_ps( 2 );
__m128 res = _mm_and_ps( a, b );
cout << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << endl;
cout << b[0] << " " << b[1] << " " << b[2] << " " << b[3] << endl;
cout << res[0] << " " << res[1] << " " << res[2] << " " << res[3] << endl;
cout<<endl;
cout << ( 1 & 2 ) << " " << ( 2 & 2 ) << " " << ( 3 & 2 ) << " " << ( 4 & 2 ) << endl;

导致:

1 2 3 4
2 2 2 2
0 2 2 2
0 2 2 0

SSE操作的结果不应该是0 2 2 0,因为2 = 010, 4 = 100 => 2&4 = 0
根据文档:

__ m128 _mm_and_ps(__ m128 a,__m128 b)

计算a和b的四个SP fp值的位。

R0 R1 R2 R3

a0&amp;B0 A1&amp;B1 A2&amp;B2 A3&amp;B3

我发现的文档说:

计算a和b。

(我的重点)

2和4将具有相同的Mantissa(0,另外一个暗示的1位)和128和129的指数。位和这些是零mantissa,指数为128(== 2.0)。


编辑

如果您想做一点努力和非阴性整数,则可以添加一个偏移。如果您使用8388608的偏移(== 1&lt;&lt; 23),那么您可以按照您的期望在0..8388607上进行位置操作。

const float offset=8388608;
__m128 mm_offset = _mm_set1_ps();
__m128 a   = _mm_setr_ps( 1, 2, 3, 4 );
a =_mm_add_ps(mm_offset,a);
__m128 b   = _mm_set1_ps( 2+offset );
__m128 res = _mm_and_ps( a, b );
res = _mm_sub_ps(res,mm_offset);