delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/20/12:40:34

Message-Id: <199705201636.JAA01291@bluesky>
Comments: Authenticated sender is <kbaca AT bluesky DOT skygames DOT com>
From: "Kevin Baca" <kbaca AT skygames DOT com>
Organization: BlueSky Software
To: djgpp AT delorie DOT com
Date: Tue, 20 May 1997 09:46:22 +0000
MIME-Version: 1.0
Subject: Re: float & ints & triangle3d_f

> Leath Muller wrote:
> > 
> > > > Is comparing floats slower then comparing ints? i.e. if (a < b)
> > > They are much more slower.
> > 
> > This is true, but if your just checking for a < b, sub a from b, store and
> > check the sign bit. If the sign is 0 then a <= b... If you weave the code
> > properly, you can do a check in about (from memory) 5 cycles... (compared
> > to an int's 1 though)
> > 
> > Leathal.
> This is true, but the compiler won't do it for you, and I can't imagine
> writing all coparisions in assembler... so avoid it.
> There is another trick you maight find usefull. To convert float point
> to integer just add 0x59c00000 (have to be single precision fadd), store
> the result and use it as it was integer. It's only 1 clock less, but you
> have beter scheduling opportunities (can execute up to 4 integer
> instruction in a gap after fadd).

There is also another trick.  If one or both operands are positive you can 
compare their bit patterns as integers:

example:

float f1, f2;

if( *( ( int* ) &f1 ) < *( ( int* ) &f2 ) )
{
    printf( "less\n" );
}

If both operands are negative, use the oposite comparison (i.e. 
instead of the less than operator, use the greater than operator )

Either you will KNOW one or both operands are positive, or you can 
simply check the sign bit.

-Kevin

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019