Converting floor to rint
Jan Hubicka
jh@suse.cz
Thu Nov 7 01:44:00 GMT 2002
> Jan Hubicka <jh@suse.cz> writes:
>
> | > Jan Hubicka <jh@suse.cz> writes:
> | >
> | > | On the related note. How bad would you consider converting
> | > | floor(x) into rint(x-0.5) in the fast-math mode?
> | >
> | > If you think you do want to do that transformation, then I would
> | > prefer this
> | >
> | > floor(x) -> nearbyint(x-0.5)
> | >
> | > | That transformation would do a miracles for i386, where rint is faster
> | > | than floor by quite a lot and should suffice for 3D application in
> | > | reliablility. I can imagine it to fail only for very large numbers...
> | >
> | > Not really. floor(1) == 1 and rint(1 - 0.5) maybe be 0 or 1 depending
> | > on the current rounding mode.
> | Hmm, is rint really expected to be dependent on the rouding mode?
> | Man page claims:
> | The nearbyint functions round their argument to an integer value
> | in floating point format, using the current
> | rounding direction and without raising the inexact
> | exception.
> |
> | The rint functions do the same, but will raise the
> | inexact exception when the result differs in value
> | from the argument.
>
> The C definition says:
>
> 7.12.9.3 The nearbyint functions
>
> Synopsis
>
> [#1]
>
> #include <math.h>
> double nearbyint(double x);
> float nearbyintf(float x);
> long double nearbyintl(long double x);
>
> Description
>
> [#2] The nearbyint functions round their argument to an
> integer value in floating-point format, using the current
> rounding direction and without raising the ``inexact''
> floating-point exception.
>
> Returns
>
> [#3] The nearbyint functions return the rounded integer
> value.
>
> 7.12.9.4 The rint functions
>
> Synopsis
>
> [#1]
>
> #include <math.h>
> double rint(double x);
> float rintf(float x);
> long double rintl(long double x);
>
> Description
>
> [#2] The rint functions differ from the nearbyint functions
> (7.12.9.3) only in that the rint functions may raise the
> ``inexact'' floating-point exception if the result differs
> in value from the argument.
I see, that is quite different than what I have in manpage. I will
check the standard for other differences
>
> Returns
>
> [#3] The rint functions return the rounded integer value.
>
> | I can builtinize nearbyint too if it makes sense.
>
> Do we have framework to deal correctly with current rounding mode?
> I not, I would say, leave it as is until we have the appropriate
> machinery.
RTL truncating expressions are underfined for nonintegral FP values, so
we don't have problems with the compiler, only we can't fold at compile
time for non-integral constats, since the results depends on runtime
conditions.
>
> | I am bit confused by
> | rint. Does it imply that rint will raise exception for any non-integral
> | arugment?
>
> And depending on the rounding mode.
Bad :(
I will check whether frndint will do the exception. If not, I guess the
current glibc rint builtin is wrong and it really should be rint
builtin. The other functions (floor and friends) are not throwing
inexact I guess, but we can safely mask this out of rounding mode of
course.
Honza
>
> -- Gaby
More information about the Gcc-patches
mailing list