From http://www.randombit.net/bitbashing/2009/10/08/integer_matrix_transpose_in_sse2.html
It is a good example of SSE for matrix transpose.
he Intel SSE2 intrinsics has a macro _MM_TRANSPOSE4_PSwhich performs a matrix transposition on a 4x4 array represented byelements in 4 SSE registers. However, it doesn’t work with integerregisters because Intel intrinsics make a distinction between integerand floating point SSE registers. Theoretically one could cast and usethe floating point operations, but it seems quite plausible that thiswill not round trip properly; for instance if one of your integervalues happens to have the same value as a 32-bit IEEE denormal.
However it is easy to do with the punpckldq, punpckhdq, punpcklqdq,and punpckhqdq instructions; code and diagrams ahoy.
If we name the 4 input registers I0, I1, I2, and I3, then label their cooresponding elements as0{0,1,2,3} and so on, then the transpose operation looks like this:
![../../../_images/sse2_transpose.png](http://www.randombit.net/bitbashing/_images/sse2_transpose.png)
When we are done, O{0,1,2,3} contains the all of the first,second, third, or fourth elements (resp) of the input vectors.
In Intel’s intrinsics (also usable in at least GNU C++ and VisualC++), this can be expressed as:
__m128i T0 = _mm_unpacklo_epi32(I0, I1);
__m128i T1 = _mm_unpacklo_epi32(I2, I3);
__m128i T2 = _mm_unpackhi_epi32(I0, I1);
__m128i T3 = _mm_unpackhi_epi32(I2, I3);
/* Assigning transposed values back into I[0-3] */
I0 = _mm_unpacklo_epi64(T0, T1);
I1 = _mm_unpackhi_epi64(T0, T1);
I2 = _mm_unpacklo_epi64(T2, T3);
I3 = _mm_unpackhi_epi64(T2, T3);
The diagram was done with latex2png,a handly little tool for generating images with LaTeX inputs.