This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Floating point to int casts


It really blew my mind when Geoff Keating <geoffk@geoffk.org> said:
> 
> You might consider using 'lrint' (or lrintf or lrintl or llrint...  or
> even rint) instead, which is the standard C function implemented using
> this instruction.  With current glibc versions, this produces code
> identical to your macro.

Not quite identical :-). I'm currently using this in a very tight loop
which does nothing more than convert an array of floats/doubles into
ints. Something like

    while (count)
    {   count -- ;
        output [count] = lrint (input [count]) ;
        } 

or this:

    while (count)
    {   count -- ;
        ERIKS_FLOAT_TO_INT_MACRO (output [count], input [count]) ;
        } 

Because lrint and family are implemented as inline functions which have their 
return value on the stack, the code produced looks like this:

 8048ab8:	db 5d fc             	fistpl 0xfffffffc(%ebp)
 8048abb:	8b 45 fc             	mov    0xfffffffc(%ebp),%eax
 8048abe:	89 04 91             	mov    %eax,(%ecx,%edx,4)

where the fistpl instructions puts an int on the stack, the first move loads
that value into eax and the second move places the contents of eax into the
correct place in the output array.

My macro is not an inline function, and produced the following code:

 8048b24:	db 1c 82             	fistpl (%edx,%eax,4)

ie, the fistpl instruction places the int directly into the output array.

Dispite this, I have decided to use lrint instead of my macro for reasons of
portability and standards compliance. It would however be nice if the optimiser
was smart enough to turn the first pice of code into the second pice of code.

Maybe this is what Tom Prince was talking about when he mentions moving lrint
into the compiler. Tom?

Regards,
Erik
-- 
+-----------------------------------------------------------+
  Erik de Castro Lopo  nospam@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
Spammer:     Any of you guys looking for a permanent position in Scotland?
Kaz Kylheku: No, I'm looking for a thug in Scotland who might be interested 
             in beating up off-topic Usenet spammers, on a pro bono basis.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]