[PATCH]: transform nearbyint->rint & rint/lrint->round/lround

Richard Guenther richard.guenther@gmail.com
Sun Mar 11 18:50:00 GMT 2007


On 3/11/07, Kaveh R. GHAZI <ghazi@caip.rutgers.edu> wrote:
> On Sat, 10 Mar 2007, Richard Guenther wrote:
>
> > On 3/10/07, Kaveh R. GHAZI <ghazi@caip.rutgers.edu> wrote:
> > > This patch adds transformations of the form:
> > >
> > > nearbyint -> rint (when -fno-trapping-math)
> > > rint -> round (when -fno-rounding-math)
> > > lrint -> lround (also when -fno-rounding-math)
> >
> > round (0.5) is 1.0 while rint (0.5) is 0.0 - i.e. the round-to-nearest rounding
> > direction rounds to even while round rounds 0.5 away from zero.
> > So the latter two transformations are not valid.  Also lrint can be
> > implemented more efficient than lround for this reason.
> > Richard.
>
> Oh my, how embarrassing!  (And I have a sense of deja vu here.)
>
> Pray tell, what is the algorithm to implement round to nearest even?
> E.g. real_round does:
>
>   do_add (r, x, &dconsthalf, x->sign);
>   do_fix_trunc (r, r);
>
> Is there something similarly simple for a hypothetical real_rint?

Sure, it's just r + 2**prec - 2**prec with intermediate rounding as
specified by IEEE.  And adjusting it for negative r.  Take a look at
config/i386/i386.c:ix86_expand_rint for what we do to expand rint
to a SSE computation sequence.

Richard.



More information about the Gcc-patches mailing list