News:

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

Is Negative

Started by herge, May 20, 2008, 02:04:45 PM

Previous topic - Next topic

herge

 Hi All:


  push eax
  and eax,80000000h ; is high bit on?
  cmp eax,80000000h
  pop eax
  jnz @f
  neg eax ; is Negative so flip it!
@@:



Is there any easy way to test if a signed Number
is negative. Or is they a jmp on a conditon
to do it?
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

evlncrn8

test eax, 080000000h possibly?

Jimg

or eax,eax
jns @f
neg eax
@@:

herge


Hi Jimg:

It works for me!

Thank you.
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

jj2007

For unsigned integers such as the well-known eax:

IsNeg   EQU 80000000h   ; eax>= means: eax is negative
IsNegW   EQU 8000h   ; GetKeystate needs a WORD

  .if eax>=IsNeg
   ; negative
  .endif

  .if eax<IsNeg
   ; positive
  .endif

Rockoon

What about

input in eax

mov ebx, eax
sar eax, 31
add ebx, eax
xor ebx, eax

output in ebx

no branching, but more operations.. so profile.
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

herge


Hi Rockon:

Yes that works as well.

Thank you!
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

drizz

some more :)
Abs macro __rm:req
.repeat
neg __rm
.until !sign?
endm

AbsEAX macro
cdq
xor eax,edx
sub eax,edx
endm
The truth cannot be learned ... it can only be recognized.

Rockoon

Quote from: drizz on May 21, 2008, 06:10:49 PM
some more :)

AbsEAX macro
cdq
xor eax,edx
sub eax,edx
endm


Very humbling. This one is clearly superior to all of the others posted thus far. No benchmrking required. Even uses eax as much as possible for shorter instruction lengths.

When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

herge


Hi drizz:

The AbsEAX macro works.
But I don't need 64 bits yet.

Thanks.
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

hutch--

 :bg

Like it. Compliments drizz.  :U


AbsEAX macro
cdq
xor eax,edx
sub eax,edx
endm
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MichaelW

#11
The attachment is a quick test of the code here, based on the assumption that the goal is to convert the number in eax to its absolute value. The cycle counts in the results below are for a total of 200 conversions, of alternating positive and negative values, running on a P3. Considering the call overhead, even the CRT function is surprisingly fast.

0  0  0  0  0  0

1  1  1  1  1  1

2147483647  2147483647  2147483647  2147483647  2147483647  2147483647

1  1  1  1  1  1

2147483647  2147483647  2147483647  2147483647  2147483647  2147483647

2147483648  2147483648  2147483648  2147483648  2147483648  2147483648

804 cycles
432 cycles
426 cycles
510 cycles
406 cycles
1605 cycles




[attachment deleted by admin]
eschew obfuscation

Rockoon

Quote from: herge on May 21, 2008, 07:44:46 PM

Hi drizz:

The AbsEAX macro works.
But I don't need 64 bits yet.

Thanks.


It doesnt do 64-bit values.

It just uses the property of the cdq instruction of sign extending all the way through the edx register, creating an all 1's mask in edx if eax is negative, or an all 0's mask if eax is positive.

Negation in twos complement:

take the NOT of the value, and then add 1.

or

subtract 1 from the value, then take the NOT


The mask can be used for both NOTing and adding/subtracting 1, conditionally, based on the state of eax. (an all 1's mask is equivilent to the value '-1', and xoring with the all 1's mask is equivilent to a NOT)

My methodology is the same, 'cept I wasnt exploiting the CDQ instruction (instead I was making a copy of the input and then doing an arithmetic shift by 31 to create the mask) .. I think i've been spending too much time in HLL's

When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

Rockoon

Quote from: MichaelW on May 22, 2008, 04:37:45 AM
The cycle counts in the results below are for a total of 200 conversions, of alternating positive and negative values, running on a P3.

Alternating signs isnt a very good test of anything usefull.

The best way to test things like this is to make a list of typical inputs, then shuffle them all, then perform your test on each item in the list in the now randomly ordered sequence. Anything else biases in favor of branching versions because branch predictors love patterns, patterns that arent typical in the real world usage of such a function.
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

hutch--

This is what I got with Michaels test. I added the macro name for each so I knew what was what.


0  0  0  0  0  0

1  1  1  1  1  1

2147483647  2147483647  2147483647  2147483647  2147483647  2147483647

1  1  1  1  1  1

2147483647  2147483647  2147483647  2147483647  2147483647  2147483647

2147483648  2147483648  2147483648  2147483648  2147483648  2147483648

761 cycles abs0
364 cycles abs1
403 cycles abs2
395 cycles abs3
455 cycles abs4
3601 cycles crt_abs

Press any key to exit...


ow all  wonder is if there would be a time difference with the TEST/Jxx code if the jump was taken or not.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php