gpt4 book ai didi

c++ - SSE 内在函数 - _mm_and_ps 奇怪的行为

转载 作者:搜寻专家 更新时间:2023-10-31 00:53:40 26 4
gpt4 key购买 nike

下面这段代码:

__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)

Computes the bitwise AND of the four SP FP values of a and b.

R0 R1 R2 R3

a0 & b0 a1 & b1 a2 & b2 a3 & b3

最佳答案

documentation我发现说:

Computes the bitwise AND of the four single-precision, floating-point values of a and b.

(我的重点)

2 和 4 将具有相同的尾数(0,加上隐含的前导 1 位),指数分别为 128 和 129。这些的按位与是零尾数和 128 的指数 (== 2.0)。


编辑

如果你想对非负整数进行按位与运算,你可以添加一个偏移量。如果您使用偏移量 8388608 (== 1<<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);

关于c++ - SSE 内在函数 - _mm_and_ps 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com