The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ecube on April 14, 2009, 03:14:49 AM

Title: Proper way to test Real8 for 0
Post by: ecube on April 14, 2009, 03:14:49 AM
.data
Results REAL8 ?

.code
mov ecx, dword ptr [Results]
mov edx, dword ptr [Results+4]
test ecx,edx
jnz @F
;zero

@@:
;not zero


it seems to work ok but I just want to make sure
Title: Re: Proper way to test Real8 for 0
Post by: NightWare on April 14, 2009, 03:24:40 AM
.code
mov edx, dword ptr [Results]
or edx, dword ptr [Results+4]
jnz @F
;zero

@@:
;not zero

never forget the boole's operators  :wink
Title: Re: Proper way to test Real8 for 0
Post by: raymond on April 14, 2009, 04:30:20 AM
Quote from: E^cube on April 14, 2009, 03:14:49 AM
.data
Results REAL8 ?

.code
mov ecx, dword ptr [Results]
mov edx, dword ptr [Results+4]
test ecx,edx
jnz @F
;zero

@@:
;not zero


it seems to work ok but I just want to make sure

I don't know what data you used to mention that it works. I do agree that it would work with many REAL8 floats but it would yield the wrong result with some of them. The simplest example is 1.0 which would have the following representation:
3FF0000000000000h

Effectively, all integers below +/-200000h (+/-2097152) would yield the wrong result.
Remember that the "test" performs an AND operation, modifying the flag register without modifying the source nor the destination.

NightWare's  proposed code would yield the correct result 100% of the time with one exception, i.e. -0 which has the following representation: 8000000000000000h and is a valid number. If you want to cover that rare case, you would then have to add in the non-zero case:

and  edx,7FFFFFFFh
jz   zero_case


Or, more simply:

mov edx, dword ptr [Results+4]
and  edx,7FFFFFFFh   ;get rid of the sign bit
or edx, dword ptr [Results]
jnz @F
;zero

@@:
;not zero


If you would consider numbers smaller than approximately 10-308 as equivalent to ZERO, you could simplify the test without even using a register:

test dword ptr Results[4],7FFFFFFFh
jnz  @F
;zero

@@:
;not zero
Title: Re: Proper way to test Real8 for 0
Post by: ecube on April 14, 2009, 04:45:04 AM
Thanks Raymond,
i'm suprised your degree is in Chemistry as your math knowledge is very impressive.  :U