2

Does anyone know how to make sign extension from 16 bits words to 32 bits words using MMX registers? I would like to get two 32 bits sign extended words from two 16 bits words stored in a MMX register. No SSE4 instructions allowed.

Regards

LooPer
  • 1,459
  • 2
  • 15
  • 24

1 Answers1

5

You can just do a left shift (PSLLD) followed by an arithmetic right shift (PSRAD), e.g. using intrinsics:

v = _mm_srai_pi32(_mm_slli_pi32(v, 16), 16);

(This is assuming that you already have the 16 bit values in the low halves of each 32 bit word.)

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • +1 And if they aren't in the correct position you probably want something like `_mm_unpacklo_pi16(v, ZERO)` (`PUNPCKLWD`) first. Note Paul R's answer is using the compiler intrinsics of MSVC, GCC has something [similar](http://gcc.gnu.org/onlinedocs/gcc/X86-Built_002din-Functions.html). – user786653 Jul 27 '11 at 16:48
  • Thanks - I've edited the question to clarify the use of intrinsics in the example. Note that the MMX/SSE intrinsics are pretty much identical for MSVC, gcc, ICC, etc. – Paul R Jul 27 '11 at 17:45
  • What I am looking for is doing shl(mult(var1,var2),1) where 'mult' multiply var1 and var2 and 'shl' shift left aritmetically multiplication result. Result must be saturated, i.e.: mult(-32768,-32768)=2147483647. I though about making mult(sing_extesion(var1), shl(sign_extension(var2))) but I've just discovered no MMX mult() saturation version exists. Do you know any other way to get it? – LooPer Jul 27 '11 at 18:01
  • @LooPer: maybe you should post a new question explaining what it is that you *really* want to do ? – Paul R Jul 27 '11 at 18:54