News:

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

Proper way to test Real8 for 0

Started by ecube, April 14, 2009, 03:14:49 AM

Previous topic - Next topic

ecube

.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

NightWare

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

@@:
;not zero

never forget the boole's operators  :wink

raymond

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
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

ecube

Thanks Raymond,
i'm suprised your degree is in Chemistry as your math knowledge is very impressive.  :U