News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

128-bit integer arithmetics

Started by msoftprogramming, May 24, 2011, 11:29:57 AM

Previous topic - Next topic

msoftprogramming

Hello everyone,
just a very simple question. I'd like to use XMM 128-bit registries to do some VERY simple 128-bit integer math on them.
In particular, I'd need to:
set XMM registries to some 128 bit int value
compare two 128-bit integer and jump somewhere if they are equal
multiply 128 bit XMM registry value by 3
divide 128 bit XMM registry value by 2
add 1 to 128 XMM registry value

is that possible?
Thank you very much!

Matteo Monti
Msoft Programming

dedndave

might be easier to divide by 2, add that to the original, then increment
Y = 1.5X + 1

jj2007

Quote from: msoftprogramming on May 24, 2011, 11:29:57 AM
compare two 128-bit integer and jump somewhere if they are equal

Ciao Matteo,

Here is a snippet that compares two 128-bit memory vars:
include \masm32\include\masm32rt.inc
.686
.xmm

IsEqual128 MACRO arg1, arg2
movups xmm0, oword ptr arg1
movups xmm1, oword ptr arg2
psubd xmm0, xmm1
xorps xmm1, xmm1
pcmpeqb xmm0, xmm1
pmovmskb eax, xmm0
cwde
inc eax
EXITM <Zero?>
ENDM

.data
x128A REAL8 123456789.0, 123456789.1
x128B REAL8 123456789.0, 123456789.1
x128C REAL8 123456789.1, 123456789.1

.code
AppName db "Masm32:", 0

start:
.if IsEqual128(x128A, x128B)
MsgBox 0, "A=B", "Hi", MB_OK
.else
MsgBox 0, "A and B are different", "Hi", MB_OK
.endif
.if IsEqual128(x128A, x128C)
MsgBox 0, "A=C", "Hi", MB_OK
.else
MsgBox 0, "A and C are different", "Hi", MB_OK
.endif
exit

end start


This is for REAL8 vars, but it works for integers, too; try this:

x128A dq 1234567890, 1234567891
x128B dq 1234567890, 1234567891
x128C dq 1234567891, 1234567891

qWord

hi,
Quote from: msoftprogramming on May 24, 2011, 11:29:57 AMI'd like to use XMM 128-bit registries to do some VERY simple 128-bit integer math on them.
you have obvious miss understood what SSEx is for: it is designed for processing vectorized data (SIMD). There is no nature support for 128Bit integers - 64Bit integers are the maximum.
FPU in a trice: SmplMath
It's that simple!

msoftprogramming

... all right! I think I should find out something to understand the language a bit better.. could you tell me where can I find something like a tutorial explaining everything from the beginning? Something about every registry and how does each work and all the operation I can perform with them? Thank you very much.
Anyway, jj2007, I tried to run your code, but.. it always says that numbers are different, even if i change the values...

Thank you again!

Matteo

jj2007

Quote from: msoftprogramming on May 24, 2011, 02:35:02 PM
Anyway, jj2007, I tried to run your code, but.. it always says that numbers are different, even if i change the values...

That's odd - here it works, and it should work, as it simply tests two packed quadwords for equality (which is indeed a 128:128-bit comparison, although a very simple one).

Can anybody confirm Matteo's finding?

qWord

jj's code works for me, but it could be done a bit easier:
.data
align 16
data1 QWORD -123,-123
data2 QWORD -123,-123
.code

movdqa xmm0,OWORD ptr data1
pcmpeqb xmm0,OWORD ptr data2
pmovmskb eax,xmm0
.if eax == 0ffffh
MsgBox 0,"equal",0,0
.endif
FPU in a trice: SmplMath
It's that simple!

jj2007

Yes, that's right, I forgot the pcmpeqb compares all bytes to their counterparts...
Nonetheless I would stick with movups - assuming 16-bit alignment is kind of bug-prone :wink

QuoteIsEqual128 MACRO arg1, arg2
   movups xmm0, oword ptr arg1
   movups xmm1, oword ptr arg2
   pcmpeqb xmm0, xmm1
   pmovmskb eax, xmm0
   cwde
   inc eax
   EXITM <Zero?>
ENDM

raymond

Quote.if IsEqual128(x128A, x128B)

Looking at the IsEqual128 macro, it is obvious that you need memory locations as arguments. As coded, would the x128A and x128B arguments always be interpreted as offsets with ALL assemblers or could some assemblers interpret them as actual values?
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

lingo

"There is no nature support for 128Bit integers - 64Bit integers are the maximum."

Take a look of AVX coz AVX adds new register-state through the 256-bit wide YMM register-file, so explicit operating system support is required to properly save & restore AVX's new registers between context switches.

I have Intel Sandy Bridge processor, Windows 7-64bit SP1, MASM from VS2010-SP1 and have no problem with the new instructions. See my replay #4  here :U

qWord

I know about AVX,
but AFAIKS there is no nature support for arithmetic on 128Bit integers !(?)
FPU in a trice: SmplMath
It's that simple!

jj2007

Quote from: raymond on May 25, 2011, 01:20:16 AMLooking at the IsEqual128 macro, it is obvious that you need memory locations as arguments.

Well, not really:
movups xmm3, oword ptr x128B
.if IsEqual128(x128A, xmm3)


QuoteAs coded, would the x128A and x128B arguments always be interpreted as offsets with ALL assemblers or could some assemblers interpret them as actual values?

Most of the code posted in the Forum can be interpreted correctly only by ml.exe and jwasm.exe ...

Quote from: lingo on May 25, 2011, 01:35:50 AM"There is no nature support for 128Bit integers

Except for the supernatural IsEqual128 macro, of course :bg

hutch--

This seems to run and the results appear to work but no garrantees.  :bg You know, Eenie, meanie (blue), miney and MOE.


IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    cmp128 PROTO :DWORD,:DWORD

    .data
      item1 oword 0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFh
      item2 oword 0
      item3 oword 1
      item4 oword 0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEh
      item5 oword 0000000000000000FFFFFFFFFFFFFFFFh
      item6 oword 0FFFFFFFFFFFFFFFF0000000000000000h

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    invoke cmp128,ADDR item2,ADDR item2
    print str$(eax),13,10

    invoke cmp128,ADDR item4,ADDR item1
    print str$(eax),13,10

    invoke cmp128,ADDR item3,ADDR item2
    print str$(eax),13,10

    invoke cmp128,ADDR item5,ADDR item6
    print str$(eax),13,10

    invoke cmp128,ADDR item6,ADDR item5
    print str$(eax),13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

cmp128 proc num1:DWORD,num2:DWORD

    mov ecx, num1
    mov edx, num2

    mov eax, [ecx+12]
    cmp eax, [edx+12]
    jb lessthan
    ja greater

    mov eax, [ecx+8]
    cmp eax, [edx+8]
    jb lessthan
    ja greater

    mov eax, [ecx+4]
    cmp eax, [edx+4]
    jb lessthan
    ja greater

    mov eax, [ecx]
    cmp eax, [edx]
    jb lessthan
    ja greater

  equal:
    xor eax, eax
    ret

  greater:
    mov eax, 1
    ret

  lessthan:
    or eax, -1
    ret

cmp128 endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

sinsi

Jumpin' Jiminy!


cmp128 proc num1:DWORD,num2:DWORD

    mov ecx, num1
    mov edx, num2

    mov eax, [ecx+12]
    sub eax, [edx+12]
    jnz above_or_below

    mov eax, [ecx+8]
    sub eax, [edx+8]
    jnz above_or_below

    mov eax, [ecx+4]
    sub eax, [edx+4]
    jnz above_or_below

    mov eax, [ecx]
    sub eax, [edx]
    jz done
   
above_or_below:
    sbb eax,eax
    jnz done
    add eax,1

done:
    ret

cmp128 endp

Unsigned I assume.
Light travels faster than sound, that's why some people seem bright until you hear them.

hutch--

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php