The MASM Forum

General => The Campus => Topic started by: Farabi on December 17, 2012, 05:11:07 PM

Title: MMX Question
Post by: Farabi on December 17, 2012, 05:11:07 PM
I had 4 data of bytes, I want to convert it to a 4 data of words. How to do that using MMX? All data are integers.
Title: Re: MMX Question
Post by: qWord on December 17, 2012, 05:53:09 PM
hi,
MMX is outdated and should no longer used - use SSEx instead. For you question, see the unpack instructions:
mov eax,04030201h
movd xmm0,eax
pxor xmm1,xmm1
punpcklbw xmm0,xmm1
movq QWORD ptr ...,xmm0 ; store 4 WORDs

note that you could process 8 bytes in one XMM register. Also, it works only for unsigned integers.
(For an array of DWORDs, there are much better implementation possible.)
Title: Re: MMX Question
Post by: Farabi on December 17, 2012, 06:16:02 PM
 :t Thanks qWord
Title: Re: MMX Question
Post by: frktons on December 24, 2012, 01:46:54 PM
Quote from: qWord on December 17, 2012, 05:53:09 PM
hi,
MMX is outdated and should no longer used - use SSEx instead. For you question, see the unpack instructions:
mov eax,04030201h
movd xmm0,eax
pxor xmm1,xmm1
punpcklbw xmm0,xmm1
movq QWORD ptr ...,xmm0 ; store 4 WORDs

note that you could process 8 bytes in one XMM register. Also, it works only for unsigned integers.
(For an array of DWORDs, there are much better implementation possible.)

With these instructions you are creating 8 words in a 16 byte register.
If you need only 4 words, you can also use MMX registers with the same
instructions, if I correctly remember.

This is the transformation of qWord code:

mov eax,04030201h
movd mm0,eax
pxor mm1,mm1
punpcklbw mm0,mm1
movq QWORD ptr ...,mm0 ; store 4 WORDs
Title: Re: MMX Question
Post by: frktons on December 24, 2012, 02:44:48 PM
This is how Intel shows the functioning of PUNPCKLBW.
Title: Re: MMX Question
Post by: Farabi on December 24, 2012, 02:50:27 PM
Quote from: frktons on December 24, 2012, 02:44:48 PM
This is how Intel shows the functioning of PUNPCKLBW.

Im still figuring out what this instruction used for in real life.
Title: Re: MMX Question
Post by: frktons on December 24, 2012, 02:59:08 PM
Quote from: Farabi on December 17, 2012, 05:11:07 PM
I had 4 data of bytes, I want to convert it to a 4 data of words. How to do that using MMX? All data are integers.

Quote from: Farabi on December 24, 2012, 02:50:27 PM
Im still figuring out what this instruction used for in real life.

Why do you need to convert 4 bytes into 4 words? That's why they created PUNPCKLBW.
Title: Re: MMX Question
Post by: Farabi on December 24, 2012, 03:02:45 PM
Quote from: frktons on December 24, 2012, 02:59:08 PM
Quote from: Farabi on December 17, 2012, 05:11:07 PM
I had 4 data of bytes, I want to convert it to a 4 data of words. How to do that using MMX? All data are integers.

Quote from: Farabi on December 24, 2012, 02:50:27 PM
Im still figuring out what this instruction used for in real life.

Why do you need to convert 4 bytes into 4 words? That's why they created PUNPCKLBW.

I need that to make the substraction easy, sometime I need to multiply the byte data by 100 and then divide it by 200. Using byte data type it wont fit.
Title: Re: MMX Question
Post by: frktons on December 24, 2012, 03:11:12 PM
Quote from: Farabi on December 24, 2012, 03:02:45 PM

I need that to make the substraction easy, sometime I need to multiply the byte data by 100 and then divide it by 200. Using byte data type it wont fit.

And then you can use PUNPCKLBW to have 4/8 single bytes moved
into 4/8 words in one instruction. Isn't it useful?
Title: Re: MMX Question
Post by: dedndave on December 24, 2012, 03:11:53 PM
shr al,1
or, if you are nit-picking
shr al,1
adc al,0


:P
Title: Re: MMX Question
Post by: Farabi on December 24, 2012, 03:12:12 PM
Quote from: frktons on December 24, 2012, 03:11:12 PM
Quote from: Farabi on December 24, 2012, 03:02:45 PM

I need that to make the substraction easy, sometime I need to multiply the byte data by 100 and then divide it by 200. Using byte data type it wont fit.

And then you can use PUNPCKLBW to have 4/8 single bytes moved
into 4/8 words in one instruction. Isn't it useful?

Yeah it should be usefull if I understand it right.
Title: Re: MMX Question
Post by: dedndave on December 24, 2012, 03:14:17 PM
if you use PCMGTB first, you could use it to sign-extend bytes to words   :P
Title: Re: MMX Question
Post by: frktons on December 24, 2012, 03:21:01 PM
It is not too difficult to understand:

mov eax,04030201h
movd mm0,eax
pxor mm1,mm1
punpcklbw mm0,mm1
movq QWORD ptr ...,mm0 ; store 4 WORDs


1) you move your 4 bytes into a register (EAX)
2) you move EAX to an MMX register
3) you put zero to another MMX register
4) you intermix bytes from dest MMX and src MMX
5) you have 4 words into the MMX dest register
Title: Re: MMX Question
Post by: frktons on December 24, 2012, 03:29:09 PM
Have a look at Intel Manuals:

http://www.intel.it/content/www/it/it/search.html?keyword=developer%CA%B9s+manual
Title: Re: MMX Question
Post by: Farabi on December 25, 2012, 10:02:31 PM
Quote from: frktons on December 24, 2012, 02:44:48 PM
This is how Intel shows the functioning of PUNPCKLBW.

O I get it. If I want to fill the high order of the word fill it on the src, if the other wise, fill it on the dest.
Title: Re: MMX Question
Post by: frktons on December 26, 2012, 09:14:49 AM
Quote from: Farabi on December 25, 2012, 10:02:31 PM
O I get it. If I want to fill the high order of the word fill it
on the src, if the other wise, fill it on the dest.

The more you'll use them, the more you'll get on their working
mechanism.