I'm very new to SSE - coding: And i want to store the result of _m128i[4] with int32 type to one _m128i with int8 type. (The values of _m128i[j]._i32[k] are all between (-127 and + 127 )
I think in pseudo-code this is the following:
result._i8 = {
vec1._i8[0], vec1._i8[4], vec1._i8[8], vec1._i8[12],
vec2._i8[0], vec2._i8[4], vec2._i8[8], vec2._i8[12],
vec3._i8[0], vec3._i8[4], vec3._i8[8], vec3._i8[12],
vec4._i8[0], vec4._i8[4], vec4._i8[8], vec4._i8[12]};
The only way i found is this messy shuffling.
__m128i mmResult, mmResult0_3, mmResult4_7, mmResult8_11, mmResult12_15;
//some calculation ...
__m128i mmShuffler0_3 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1,12, 8, 4, 0);
__m128i mmShuffler4_7 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 12, 8, 4, 0, -1, -1, -1, -1);
__m128i mmShuffler8_11 = _mm_set_epi8(-1, -1, -1, -1, 12, 8, 4, 0, -1, -1, -1, -1, -1, -1, -1, -1);
__m128i mmShuffler12_15 = _mm_set_epi8(12, 8, 4, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
mmResult0_3 = _mm_shuffle_epi8(mmResult0_3, mmShuffler0_3);
mmResult4_7 = _mm_shuffle_epi8(mmResult4_7, mmShuffler4_7);
mmResult8_11 = _mm_shuffle_epi8(mmResult8_11, mmShuffler8_11);
mmResult12_15 = _mm_shuffle_epi8(mmResult12_15, mmShuffler12_15);
mmResult = _mm_or_si128(_mm_or_si128(mmResult0_3, mmResult4_7), _mm_or_si128(mmResult8_11, mmResult12_15));
Is there a of doing this more beautiful? Is there a way of doing this faster?
You can just use packing operations to reduce your 32 bit values to 8 bits, e.g. assuming you have 4 vectors of 32 bit
int
elements,v0
...v3
:Example:
Compile and test: