Passing one arbitrary element of the vector __m128

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

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

      

I know there are intrinsics _mm_set1_ps (float) and _mm_broadcast_ss (float *). But these built-in functions can load the value from the shared memory registers. Is there any way to set a scalar value from another vector register?

+3


source to share


2 answers


I think you need to see _mm_shuffle_epi32 (). It's easy to use with the following 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;
}

      

+4


source


You can just use _mm_shuffle_ps

like this:



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

      

+5


source







All Articles