float to int conversions

Richard Dobson RWD at cableinet.co.uk
Wed Jun 10 04:32:07 EDT 1998


Yes, it's fast, but it does not work correctly for negative numbers - to comply
with expected C semantics, the int cast must truncate positive numbers
downwards, and negative numbers upwards, so that -0.5 truncates to 0.0. This
routine truncates -1.0 to -2.0!

It maybe that the Intel code is the fastest compliant way of doing things, on
the principle that if there was a faster way of doing it, presumably they would
use it. ~;-)

Richard Dobson



Chris Townsend wrote:
> 
> I can't believe I forgot the most obvious method to do truncatation (thanks
> Chris W.).  Just subtract 1/2.  Here's the updated code to do truncation.
> It should only take 1 or 2 cycles more, and we can mix and match rounding
> and truncation with little overhead.  This seems like an almost ideal
> solution to me.
> 
> float OneHalfOffset=0.5;
> 
> // Truncate instead of round
> __inline int truncate(float fval)
> {
>         int result;
>         _asm{
>                 fld     fval
>                 fsub   OneHalfOffset
>                 fistp   result
>                 mov     eax,result
>         }
> }
> 
> Chris
>






More information about the music-dsp mailing list