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

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

  •  29-05-2022
  •  | 
  •  

문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top