views:

163

answers:

1

I have two packed quadword integers in xmm0 and I need to add them together and store the result in a memory location. I can guarantee that the value of the each integer is less than 2^15. Right now, I'm doing the following:

int temp;
....   

   movdq2q mm0, xmm0
   psrldq xmm0, 8
   movdq2q mm1, xmm0
   paddq mm0,mm1
   movd temp, mm0

Is there a better way to do this?

+2  A: 

First off, why are you using quadwords to represent values that would fit in a 16-bit format? Leaving that aside, a couple solutions:

pshufd xmm1, xmm0, EEh
paddq  xmm0, xmm1
movd   temp, xmm0

or

movdqa xmm1, xmm0
psrldq xmm1, 8
paddq  xmm0, xmm1
movd   temp, xmm0

or

movhlps xmm1, xmm0
paddq   xmm0, xmm1
movd    temp, xmm0

Note that you don't actually need to use paddq, you can get away with one of the narrower adds if you prefer.

edit summing four double quadwords -- what you have is pretty much fine. Given that you know that all the data in them fits into the low doubleword of each slot, you could try something like:

shufps  xmm0, xmm2, 88h
shufps  xmm4, xmm6, 88h
paddd   xmm0, xmm4
psrlq   xmm1, xmm0, 32
paddd   xmm0, xmm1
movhlps xmm1, xmm0
paddd   xmm0, xmm0
movd    temp, xmm0

which may or may not prove to be faster.

As for EMMS, it's just another instruction. After any code that touches the MMX registers, before any code that uses the x87 floating-point instructions you need to have emms.

Stephen Canon
@Stephen: The previous operations need the double quadwords to simultaneously work on 128 bytes of information. After that, a sequence of summations results in the final result with the aforementioned upperbound.
Jacob
*shrug*, fair enough. Anyway, any of the sequences I put up should work for you, and avoid the legacy mmx usage.
Stephen Canon
Thanks! It actually messed up the rest of my code such that all the floats were reduced to -1.#IND!
Jacob
Yeah, if you use the MMX registers, you need to make sure to do an `EMMS` before any code that uses x87 instructions.
Stephen Canon