我需要广播__m128 vector 的任意元素。
例如第二个元素:
__m128 a = {a0, a1, a2, a3};
__m128 b = {a1, a1, a1, a1};
我知道有内在函数_mm_set1_ps(float)和_mm_broadcast_ss(float *)。
但是这些内在函数可以从内存的常用寄存器中加载值。
是否可以通过其他 vector 寄存器设置标量值?
最佳答案
我认为您必须查看_mm_shuffle_epi32()。使用下一个帮助器功能,将很容易使用它:
#include <emmintrin.h>
template <int index> inline __m128 Broadcast(const __m128 & a)
{
return _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(a), index * 0x55));
}
int main()
{
__m128 a = {a0, a1, a2, a3};
__m128 b = Broadcast<1>(a);
return 0;
}