Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSE2 instruction to typecast an integer register to short register and vice-versa

Tags:

x86

simd

sse

sse2

Is there any SSE2 instruction to typecast an integer register to short register and vice-versa? Please suggest.

like image 651
Andy Avatar asked May 16 '13 10:05

Andy


1 Answers

It depends on what you mean by "typecast" exactly, but if you are looking for a narrowing operation then you can use _mm_packs_epi32 (PACKSSDW) to pack two integer vectors to one short vector:

__m128i vint1, vint2;  // 2 vectors of 4 x 32 bit ints
__m128i vshort;        // 1 vector of 8 x 16 bit ints

vshort = _mm_packs_epi32 (vint1, vint2);

The reverse, widening (unpacking) operation can be achieved like this:

 vint1 = _mm_srai_epi32(_mm_unpacklo_epi16(vshort, vshort), 16); // PUNPCKLWD+PSRAD
 vint2 = _mm_srai_epi32(_mm_unpackhi_epi16(vshort, vshort), 16); // PUNPCKHWD+PSRAD

Note that there is no automatic sign extension when using SSE unpack instructions, hence the need for the arithmetic shift when signed values are being widened.

like image 155
Paul R Avatar answered Sep 22 '22 07:09

Paul R