[linux-audio-dev] Traps in floating point code
Pete Bessman
ninjadroid at gazuga.net
Wed Jun 30 19:25:53 UTC 2004
At Wed, 30 Jun 2004 16:37:59 +0100 (BST),
Mike Rawes wrote:
>
> --- Pete Bessman <ninjadroid at gazuga.net> wrote:
> > At Mon, 28 Jun 2004 19:56:50 +1000,
> > Erik de Castro Lopo wrote:
> > >
> > > double fractional = 0.0, increment = 0.1;
> > > int integer = 0;
> > >
> > > for (;;)
> > > {
> > > /* Bunch of other code. */
> > >
> > > fractional += increment ;
> > > integer += lrint (floor (fractional));
> > > fractional -= floor (fractional);
> > > }
> >
> > I don't understand this lrint (floor ()) stuff. floor() returns a
> > truncated integer value, so what's the point of calling lrint() on
> > that?
>
> The lrint converts the float value to an int (a lot) faster than a
> simple cast.
Ah. Now I get to find all the places in my code that can benefit from
that. If you assign a float value into an int variable, is that an
implicit cast?
> The floor is to get around the rounding rule used by the FPU when the float
> value is exactly halfway. It rounds to even numbers, at least it does on an
> AthlonXP:
Good to know, thanks for the tip. Although, just using lrintf(val)
would be wrong anyway because it would round up if fmod(val, 1.0) >
0.5, right?
-Pete
www.gazuga.net
More information about the Linux-audio-dev
mailing list