How can I multiply two signed 32-bit integers using SSE2 instruction set?
Asked
Active
Viewed 399 times
-4
-
In what? x86 assembly? Compiler intrinsics? – Mysticial May 16 '13 at 17:49
-
The full 64-bit result or just the lowest dword? – harold May 16 '13 at 17:51
-
for x86 assembly and also need full 64-bit rsult – user2003619 May 16 '13 at 18:06
1 Answers
0
pmuldq
is, unfortunately, an SSE4.1 instruction, but SSE2 has pmuludq
and using the "convert unsigned higher order product to signed" algorithm (found in Hacker's Delight chapter 8.3) the high dword can be fixed. The low dword is, of course, already correct.
Here's my attempt at it in asm, I did not test this at all.
movdqa xmm2, xmm0
psrad xmm0, 31
movdqa xmm3, xmm1
psrad xmm1, 31
pand xmm2, xmm0
pand xmm3, xmm1
paddd xmm2, xmm3
pmuludq xmm0, xmm1
pshufd xmm2, xmm2, 0xB1
psubd xmm0, xmm2
The dwords other than the ones it's multiplying must be zero at the beginning. It looks a little different than in Hacker's Delight, because I rearranged the last bit of the algorithm to p = p - (t1 + t2)
(saves a shuffle).

harold
- 61,398
- 6
- 86
- 164