3
votes

I need to broadcast one arbitrary element of __m128 vector. For example the second element:

__m128 a = {a0, a1, a2, a3};
__m128 b = {a1, a1, a1, a1};

I know that there are intrinsics _mm_set1_ps(float) and _mm_broadcast_ss(float*). But these intrinsics can load value from common use registers of memory. Is exist any way to set scalar value from another vector register?

2

2 Answers

5
votes

You can just use _mm_shuffle_ps like this:

b = _mm_shuffle_ps(a, a, _MM_SHUFFLE(1,1,1,1));
4
votes

I think you have to see to _mm_shuffle_epi32(). Its using will be easy with next helper function:

#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;
}