Extracting ints and shorts from a structure using AVX?

I have a structure that contains a concatenation between various items and an AVX type to load all bytes into one load. My code looks like this:

#include <immintrin.h>

union S{
    struct{
        int32_t a;
        int32_t b;
        int16_t c;
        int16_t d;
    };

    __m128i x;
}

      

I would like to use the AVX register to load data together and then separately extract the four members into local variables int32_t

and int16_t

.

How should I do it? I'm not sure how can I separate the data from each other when retrieving from the AVX register?

EDIT: Looking for answers in terms of GCC builtins.

EDIT2: updated the code to change the structure using union.

+3


source to share


1 answer


You can extract 16-bit elements from __m128i

with _mm_extract_epi16

(requires SSE2):

int16_t v = _mm_extract_epi16 (v, 4);  // extract element 4

      

For 32-bit elements use _mm_extract_epi32

(requires SSE4.1)

int32_t v = _mm_extract_epi32 (v, 0);  // extract element 0

      

See: Intel Intrinsics Guide




Assuming your structure is declared as:

union S{
    struct{
        int32_t a;
        int32_t b;
        int16_t c;
        int16_t d;
    };

    __m128i x;
}

      

then you will extract the elements a, b, c, d like this:

S s = { { 1, 2, 3, 4 } };

__m128i v = _mm_loadu_si128((__m128i *)&s);

int32_t a = _mm_extract_epi32 (v, 0);
int32_t b = _mm_extract_epi32 (v, 1);
int16_t c = _mm_extract_epi16 (v, 4);
int16_t d = _mm_extract_epi16 (v, 5);

      

+6


source







All Articles