Skip to main content
GameDev.net gamedev.net
🔒 Locked

[MMX Asm] PSHUFW explanation?

Started by Mindowindow Jan 3, 2007 at 12:41 AM 1 replies 4.7k views
Original Post
Mindowindow
Mindowindow
MMX Documentation:
Quote:
PSHUFW mmreg1, mmreg2, imm8 PSHUFW mmreg, mem64, imm8 ... The PSHUFW instruction selects from the four words of the source operand (an MMX register or a 64-bit memory location) in one of 256 possible ways as defined by an immediate byte.
I don't understand how the immediate byte parameter works. By experimentation I've found that the nybble hexidecimal masks 4, C, E ...maybe others? are indentity masks when working with two 32bit values in the 64bit MMX registers, and for example 0x4C, 0xEC swaps the values. These masks don't make much masking sense to me though. 0xC = 0b1100 makes sense in a way, but the others don't. Can someone explain how you map your desired shuffle to this byte and vice versa?
Nytegard
Nytegard
I probably misunderstand the question, but PSHUFW works as follows

The immediate is 8 bits.

Bits 76 54 32 10

Bits 1 & 0 describe word 0.
Bits 3 & 2 describe word 1.
Bits 5 & 6 describe word 2.
Bits 7 & 6 describe word 3.

Your mmerg (64 bits) is set up as 3210.

Thus, if you had

short A[4];short B[4] = (1, 2, 3, 4};__asm{movq MM1, B;pshufw MM0, MM1, 0x1B;movq A, MM0;}


A should be {4, 3, 2, 1} now, as 0x1B = 0b00 01 10 11

MM0[bits 0-15] = MM1(start address = Immediate Byte [bits 1 & 0] * 16) [bits 0 - 15]
MM0[bits 16-31] = MM1(start address = Immediate Byte [bits 3 & 2] * 16) [bits 0 - 15]
MM0[bits 32-47] = MM1(start address = Immediate Byte [bits 5 & 4] * 16) [bits 0 - 15]
MM0[bits 48-63] = MM1(start address = Immediate Byte [bits 7 & 6] * 16) [bits 0 - 15]

It's kind of late, so hopefully I understood the question correctly, and hopefully you understand my response.

*Edit*

Thus if you wanted to swap 2 32 bit values, it would be

pshufw MM0, MM1, 0x4E, as that would be 01 00 11 10

So
MM0 [bits 48-63] would be MM1[bits 16-31]
MM0 [bits 32-47] would be MM1[bits 0-15]
MM0 [bits 16-31] would be MM1[bits 48-63]
MM0 [bits 0-15] would be MM1[bits 32-47]

[Edited by - Nytegard on January 3, 2007 2:56:02 AM]
Mindowindow
Mindowindow
Thanks for the explination, I understand it a whole lot better now. I think I was thinking too much in 32bit divisions.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.