News:

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

Negating a QWORD

Started by jj2007, February 04, 2012, 02:16:42 PM

Previous topic - Next topic

dedndave

i am guessing that would be 4 cycles   :P
maybe a few more if it were in a proc

this one wants to run at 13...
Input Value: FFFFFFFF_FFFFFFFF Output Value: 00000000_00000001, No Overflow
13 13 13 13 13
Input Value: 00000000_00000000 Output Value: 00000000_00000000, No Overflow
13 13 13 13 13
Input Value: 00000000_00000001 Output Value: FFFFFFFF_FFFFFFFF, No Overflow
13 13 13 13 13
Input Value: 7FFFFFFF_FFFFFFFF Output Value: 80000000_00000001, No Overflow
13 13 13 13 13
Input Value: 80000000_00000000 Output Value: 80000000_00000000, Overflow
13 13 13 13 13
Input Value: 80000000_00000001 Output Value: 7FFFFFFF_FFFFFFFF, No Overflow
13 13 13 15 14
Pentium 4 Prescott (2005+), MMX, SSE3


if i show cpu first, it runs at 22 ???   :eek
i should make a macro instead of a proc, i guess  :P

jj2007

Just for fun :bg

NegQw   PROC    lpInt:LPVOID
; Negate a qword with overflow
; DednDave, 2-2012
   mov edx, [esp+4]
   JJ=1
   if JJ
      neg dword ptr [edx]
      .if Carry?
         not dword ptr [edx+4]
      .endif
   else
      mov eax, [edx+4]
      neg dword ptr [edx]
      not eax
      sbb eax, -1
      mov [edx+4], eax
   endif
   ret 4
NegQw   ENDP

dedndave

well.....

we both have work to do   :bg
JJ=1:
Input Value: 00000001_00000000 Output Value: 00000001_00000000, No Overflow

JJ=0:
Input Value: 00000001_00000000 Output Value: FFFFFFF1_00000000, No Overflow

should be:
Input Value: 00000001_00000000 Output Value: FFFFFFFF_00000000, No Overflow

dedndave

oops - my code works correctly
i missed a line when copy/paste
JJ=1:
Input Value: 00000001_00000000 Output Value: 00000001_00000000, No Overflow

JJ=0:
Input Value: 00000001_00000000 Output Value: FFFFFFFF_00000000, No Overflow

should be:
Input Value: 00000001_00000000 Output Value: FFFFFFFF_00000000, No Overflow

jj2007

Better?
Input Value: FFFFFFFF_FFFFFFFF Output Value: 00000000_00000001, No Overflow
3 3 3 3 3
Input Value: 00000000_00000000 Output Value: 00000000_00000000, No Overflow
4 4 4 4 4
Input Value: 00000000_00000001 Output Value: FFFFFFFF_FFFFFFFF, No Overflow
3 3 3 3 3
Input Value: 7FFFFFFF_FFFFFFFF Output Value: 80000000_00000001, No Overflow
3 3 3 3 3
Input Value: 80000000_00000000 Output Value: 80000000_00000000, Overflow
4 4 4 4 4
Input Value: 80000000_00000001 Output Value: 7FFFFFFF_FFFFFFFF, No Overflow
3 3 3 3 3
Input Value: 00000001_00000000 Output Value: FFFFFFFF_00000000, No Overflow
4 4 4 4 4
Core (2006+), MMX, SSE3

dedndave

i like it   :U

i like this even better
        mov     eax,[esp+4]
        neg dword ptr [eax]
        jnc     @F

        not dword ptr [eax+4]
        ret     4

@@:     neg dword ptr [eax+4]
        ret     4

returns the address in EAX

dedndave

it's small enough - should probably still be a macro   :P

jj2007

Quote from: dedndave on February 05, 2012, 10:08:09 PM
i like this even better

The test for equality is needed, I am afraid...

dedndave

i don't know why you say that   :red

the only way to get to that point in the code is if the low dword is zero

QuoteNEG instruction:
If the operand is zero, its sign does not change, although this clears the carry flag
Negating any other value sets the carry flag

it seems to work fine without it

jj2007

Quote from: dedndave on February 06, 2012, 01:22:53 AM
i don't know why you say that   :red

the only way to get to that point in the code is if the low dword is zero

QuoteNEG instruction:
If the operand is zero, its sign does not change, although this clears the carry flag
Negating any other value sets the carry flag

it seems to work fine without it

It does - sorry, my fault :thumbu

dedndave

we need some more of these, Jochen
we are off our tea because we haven't done it much, lately - lol

maybe we can revive my personal favorite - qword to ascii   :P

jj2007

Quote from: dedndave on February 06, 2012, 07:15:32 AM
qword to ascii   :P
qword to ascii, votsat?

Print Str$("%i\t", qword ptr [esi]), Str$("%i\t", 0-qword ptr [esi])

By the way, dq 8000000000000000h is a tricky case. It just refuses to change sign :green2

dedndave

there is a trick for that
it works for some applications - not for all
if the overflow flag is set, treat it as an unsigned value   :P