Author Topic: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords  (Read 49109 times)

frktons

  • Member
  • ***
  • Posts: 491
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #75 on: November 29, 2012, 04:54:25 AM »
What do you want to compare? FP or integer value?

xmm integer packed data is what I'd like to compare.

I want to know, for example, if xmm0 is equal to xmm1.

qWord

  • Member
  • *****
  • Posts: 1476
  • The base type of a type is the type itself
    • SmplMath macros
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #76 on: November 29, 2012, 05:00:07 AM »
What do you want to compare? FP or integer value?

xmm integer packed data is what I'd like to compare.

I want to know, for example, if xmm0 is equal to xmm1.
so, you are rigth with PCMPEQxx + PMOVMSKB.
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

  • Member
  • *****
  • Posts: 11176
  • Assembler is fun ;-)
    • MasmBasic
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #77 on: November 29, 2012, 05:02:03 AM »
What do you want to compare? FP or integer value?

Test for equality only, so FP or INT won't make a difference. Although I wonder how CMPNEQPS aka CMPPS xmmDest, xmmSrc, 4 handles exotic cases (NaN vs 0 etc). In any case, PCMPEQxx is the right choice, as qWord already wrote.


qWord

  • Member
  • *****
  • Posts: 1476
  • The base type of a type is the type itself
    • SmplMath macros
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #78 on: November 29, 2012, 05:07:05 AM »
Although I wonder how CMPNEQPS aka CMPPS xmmDest, xmmSrc, 4 handles exotic cases (NaN vs 0 etc).
can't wok because there are N possibilities to represent a NaN, whereas CMPxxPS returns true for all pairs of NaNs.
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

  • Member
  • *****
  • Posts: 11176
  • Assembler is fun ;-)
    • MasmBasic
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #79 on: November 29, 2012, 05:13:34 AM »
can't wok because there are N possibilities to represent a NaN, whereas CMPxxPS returns true for all pairs of NaNs.

Grazie, good to know :t

qWord

  • Member
  • *****
  • Posts: 1476
  • The base type of a type is the type itself
    • SmplMath macros
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #80 on: November 29, 2012, 05:15:49 AM »
can't wok because there are N possibilities to represent a NaN, whereas CMPxxPS returns true for all pairs of NaNs.

Grazie, good to know :t
ups..., that applies for the unordered compare, for CMPEQPS it allways return false.
MREAL macros - when you need floating point arithmetic while assembling!

frktons

  • Member
  • ***
  • Posts: 491
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #81 on: November 29, 2012, 05:24:12 AM »
I'm glad to see everybody agreed eventually.  :t

Now let's go further.

How do I compare for greater than?
 :P

Same registers, same data type.

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #82 on: November 29, 2012, 06:18:43 AM »
reverse the operands ?

qWord

  • Member
  • *****
  • Posts: 1476
  • The base type of a type is the type itself
    • SmplMath macros
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #83 on: November 29, 2012, 06:20:50 AM »
Now let's go further.

How do I compare for greater than?
 :P

Same registers, same data type.
PCMPGTD  ::)
MREAL macros - when you need floating point arithmetic while assembling!

frktons

  • Member
  • ***
  • Posts: 491
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #84 on: November 29, 2012, 06:22:44 AM »
Now let's go further.

How do I compare for greater than?
 :P

Same registers, same data type.
PCMPGTD  ::)

Thanks qWord, maybe this time I'll take less time to get the info.
Well it looks quite simple to manage:
Quote
If a data element in the destination operand is greater
than the corresponding date element in the source operand, the corresponding data
element in the destination operand is set to all 1s; otherwise, it is set to all 0s.

nidud

  • Member
  • *****
  • Posts: 2054
    • https://github.com/nidud/asmc
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #85 on: November 29, 2012, 08:32:39 AM »
To conclude:

pcmpeqb compares 16 bytes, pcmpeqd compares 4 doublewords, result in xmm0 is the same.
pcmpgtb compares 16 bytes, pcmpgtd compares 4 doublewords, result in xmm0 is not the same.

Code: [Select]
pcmpeqb xmm0,xmm1 ; xmm0 to FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFh if equal
pmovmskb eax,xmm0 ; eax to 0000FFFFh
cmp ax,0FFFFh
je is_equal

pcmpgtb xmm0,xmm1 ; xmm0 to FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFh if greater
pmovmskb eax,xmm0 ; eax to 0000FFFFh
cmp ax,8000h ; ?
jle is_great

nidud

  • Member
  • *****
  • Posts: 2054
    • https://github.com/nidud/asmc
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #86 on: November 29, 2012, 08:52:13 AM »
Hmm, maybe:
Code: [Select]
cmp ax,8000h
jae is_great
..
cmp ax,-1
jle is_great

frktons

  • Member
  • ***
  • Posts: 491
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #87 on: November 29, 2012, 09:52:49 AM »
To conclude:

pcmpeqb compares 16 bytes, pcmpeqd compares 4 doublewords, result in xmm0 is the same.
pcmpgtb compares 16 bytes, pcmpgtd compares 4 doublewords, result in xmm0 is not the same.

Code: [Select]
pcmpeqb xmm0,xmm1 ; xmm0 to FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFh if equal
pmovmskb eax,xmm0 ; eax to 0000FFFFh
cmp ax,0FFFFh
je is_equal

pcmpgtb xmm0,xmm1 ; xmm0 to FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFh if greater
pmovmskb eax,xmm0 ; eax to 0000FFFFh
cmp ax,8000h ; ?
jle is_great


I don't think so about the test for GT. So far I got:

when we use pcmpgtb
we have FF only in the bytes that are greater, not all of them,
and the same for the dword, with pcmpgtd,only the dword that are greater
are switched to FF, the remaining of them are switched to
00 if are equal or less than.

Instead of this:
Code: [Select]
pcmpgtb xmm0,xmm1 ; xmm0 to FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFh if greater
pmovmskb eax,xmm0 ; eax to 0000FFFFh
cmp ax,8000h ; ?
jle is_great

Something like:

Code: [Select]
pcmpgtd xmm0,xmm1
pmovmskb eax,xmm0
        .if bit ax, 15
            jmp IsGreater
        .endif

If bit 15 not 1 The way is longer, and we have to test
other things:

Code: [Select]
pcmpgtd xmm2,xmm3; same values in reverse order
pmovmskb ebx,xmm2
        .if bit bx, 15
            jmp IsGreater;  The second original value tested
        .endif
        .if ax == bx
            jmp AreEqual
        .elseif ax > bx
            jmp IsGreater
        .else
            jmp IsLessThan
        .endif

Not already tested, but this is the idea.
« Last Edit: November 29, 2012, 10:59:09 AM by frktons »

nidud

  • Member
  • *****
  • Posts: 2054
    • https://github.com/nidud/asmc
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #88 on: November 29, 2012, 10:59:37 AM »
we have FF only in the bytes that are greater, not all of them,
and the same for the dword, with pcmpgtd,only the dword that are greater

The upper byte should always be the same (FF,? or FFFFFFFF,? if greater)

Code: [Select]
pcmpgtd xmm2,xmm3; same values in reverse order
pmovmskb ebx,xmm2
        .if bit bx, 15
            jmp IsGreater;  The second original value tested
        .endif
        .if ax == bx
            jmp AreEqual
        .else
            jmp IsLessThan; The second original value tested
        .endif
Not already tested, but this is the idea.

assuming ax is zero, I think that is correct

same as:
Code: [Select]
test ah,80h
jnz is_great
test ax,ax
jz is_equal
jmp is_less

frktons

  • Member
  • ***
  • Posts: 491
Re: MASM FOR FUN - REBORN - #0 Extract low order bytes from dwords
« Reply #89 on: November 29, 2012, 11:07:16 AM »

The upper byte should always be the same (FF,? or FFFFFFFF,? if greater)

Yes and not. It depends if the byte or dword tested is greater, not the whole xmm register.
But if upper byte or word or dword is FF, you know the entire xmm register is greater, otherwise
you have to check other things.

Code: [Select]
pcmpgtd xmm2,xmm3; same values in reverse order
pmovmskb ebx,xmm2
        .if bit bx, 15
            jmp IsGreater;  The second original value tested
        .endif
        .if ax == bx
            jmp AreEqual
        .else
            jmp IsLessThan; The second original value tested
        .endif
Not already tested, but this is the idea.

assuming ax is zero, I think that is correct
[/quote]

I modified the code, there was a logical error, have a look.
It should work with whatever value ax and bx assume.

Quote
same as:
Code: [Select]
test ah,80h
jnz is_great
test ax,ax
jz is_equal
jmp is_less


I think you have to use 2 registers not only ax. According to my understanding
you cannot check if greater, equal or less than with a single passage. Only
if you are lucky you can find the answer in the first check, if the upper byte is FF
you can say xmm0 is greater than xmm1.