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

StackOverflow https://stackoverflow.com/questions/16585639

  •  29-05-2022
  •  | 
  •  

Question

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top