From 91c28f61f43ba029166772e8ac25808ea3c3dc98 Mon Sep 17 00:00:00 2001 From: nsz Date: Tue, 20 Mar 2012 22:49:19 +0100 Subject: nearbyint optimization (only clear inexact when necessary) old code saved/restored the fenv (the new code is only as slow as that when inexact is not set before the call, but some other flag is set and the rounding is inexact, which is rare) before: bench_nearbyint_exact 5000000 N 261 ns/op bench_nearbyint_inexact_set 5000000 N 262 ns/op bench_nearbyint_inexact_unset 5000000 N 261 ns/op after: bench_nearbyint_exact 10000000 N 94.99 ns/op bench_nearbyint_inexact_set 25000000 N 65.81 ns/op bench_nearbyint_inexact_unset 10000000 N 94.97 ns/op --- src/math/nearbyintf.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/math/nearbyintf.c') diff --git a/src/math/nearbyintf.c b/src/math/nearbyintf.c index 07df8f54..39c3d73b 100644 --- a/src/math/nearbyintf.c +++ b/src/math/nearbyintf.c @@ -1,11 +1,17 @@ #include #include -float nearbyintf(float x) { - fenv_t e; +float nearbyintf(float x) +{ +#ifdef FE_INEXACT + int e; - fegetenv(&e); + e = fetestexcept(FE_INEXACT); +#endif x = rintf(x); - fesetenv(&e); +#ifdef FE_INEXACT + if (!e) + feclearexcept(FE_INEXACT); +#endif return x; } -- cgit v1.2.1