3

I am a beginner in SIMD programming. I would like to process my data as follows:

Consider I have 4 simd variables (__m128i) with the data as follows:

__m128i a = {a1, a2, a3, a4}
__m128i b = {b1, b2, b3, b4}
__m128i c = {c1, c2, c3, c4}
__m128i d = {d1, d2, d3, d4}

Now I would like to initialize Xi as follows:

__m128i x1 = {a1, b1, c1, d1}
__m128i x2 = {a2, b2, c2, d2}
__m128i x3 = {a3, b3, c3, d3}
__m128i x4 = {a4, b4, c4, d4}

Can anybody suggest to me how can I do this efficiently?

0

1 Answer 1

5

I'm assuming that there's a typo in your question and that you actually want to do a 4x4 transpose. If so, then you can do a 4x4 transpose with 8 instructions, like this:

#include "emmintrin.h"

inline void Transpose_4_4(
    __m128i &v0,               // a1, a2, a3, a4 => a1, b1, c1, d1
    __m128i &v1,               // b1, b2, b3, b4 => a2, b2, c2, d2
    __m128i &v2,               // c1, c2, c3, c4 => a3, b3, c3, d3
    __m128i &v3)               // d1, d2, d3, d4 => a4, b4, c4, d4
{
    __m128i w0 = _mm_unpacklo_epi32(v0, v1);
    __m128i w1 = _mm_unpackhi_epi32(v0, v1);
    __m128i w2 = _mm_unpacklo_epi32(v2, v3);
    __m128i w3 = _mm_unpackhi_epi32(v2, v3);
    v0 = _mm_unpacklo_epi64(w0, w2);
    v1 = _mm_unpackhi_epi64(w0, w2);
    v2 = _mm_unpacklo_epi64(w1, w3);
    v3 = _mm_unpackhi_epi64(w1, w3);
}

Demo:

//
// tranpose_4_4.cpp
//

#include <stdio.h>
#include <emmintrin.h>

inline void Transpose_4_4(
    __m128i &v0,               // a1, a2, a3, a4 => a1, b1, c1, d1
    __m128i &v1,               // b1, b2, b3, b4 => a2, b2, c2, d2
    __m128i &v2,               // c1, c2, c3, c4 => a3, b3, c3, d3
    __m128i &v3)               // d1, d2, d3, d4 => a4, b4, c4, d4
{
    __m128i w0 = _mm_unpacklo_epi32(v0, v1);
    __m128i w1 = _mm_unpackhi_epi32(v0, v1);
    __m128i w2 = _mm_unpacklo_epi32(v2, v3);
    __m128i w3 = _mm_unpackhi_epi32(v2, v3);
    v0 = _mm_unpacklo_epi64(w0, w2);
    v1 = _mm_unpackhi_epi64(w0, w2);
    v2 = _mm_unpacklo_epi64(w1, w3);
    v3 = _mm_unpackhi_epi64(w1, w3);
}

int main(void)
{
    int32_t buff[4][4] __attribute__ ((aligned(16)));
    int i, j;
    int k = 0;

    // init buff
    for (i = 0; i < 4; ++i)
    {
        for (j = 0; j < 4; ++j)
        {
            buff[i][j] = k++;
        }
    }

    // print buff
    printf("\nBEFORE:\n");
    for (i = 0; i < 4; ++i)
    {
        for (j = 0; j < 4; ++j)
        {
            printf("%4d", buff[i][j]);
        }
        printf("\n");
    }

    // transpose
    Transpose_4_4(*(__m128i *)buff[0], *(__m128i *)buff[1], *(__m128i *)buff[2], *(__m128i *)buff[3]);

    // print buff
    printf("\nAFTER:\n");
    for (i = 0; i < 4; ++i)
    {
        for (j = 0; j < 4; ++j)
        {
            printf("%4d", buff[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Compile and run:

$ g++ -Wall -msse3 transpose_4_4.cpp && ./a.out 

BEFORE:
   0   1   2   3
   4   5   6   7
   8   9  10  11
  12  13  14  15

AFTER:
   0   4   8  12
   1   5   9  13
   2   6  10  14
   3   7  11  15
$ 
2
  • @sunzhuoshi: your edit was incorrect - the above code has been tested and gives the correct results.
    – Paul R
    Jul 15, 2014 at 12:49
  • 1
    yes, you're right. sorry to not to test it carefully. @Paul R
    – sunzhuoshi
    Apr 7, 2017 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.