From 44803e26aeea86a93f71f9f8e2b2b3a4546d652e Mon Sep 17 00:00:00 2001 From: Theodore Dubois Date: Sun, 12 Jan 2020 22:50:57 -0800 Subject: [PATCH] Don't try to round float80s when no precision is lost Fixes the ceil() function. Previously, ceil(3) was 4. --- emu/float80.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/emu/float80.c b/emu/float80.c index b5969655e1..d159b63285 100644 --- a/emu/float80.c +++ b/emu/float80.c @@ -50,6 +50,10 @@ static uint128_t u128_shift_right_round(uint128_t i, int shift, int sign) { uint64_t rest = i & ~(-1ul << (shift - 1)); i >>= shift; + // if all the bits shifted out were zeroes, we're done + if (guard == 0 && rest == 0) + return i; + switch (f80_rounding_mode) { case round_up: if (!sign) @@ -61,12 +65,12 @@ static uint128_t u128_shift_right_round(uint128_t i, int shift, int sign) { break; case round_to_nearest: // if guard bit is not set, no need to round up - if (guard) { - if (rest != 0) - i++; // round up - else if (i & 1) - i++; // round to nearest even - } + if (!guard) + break; + if (rest != 0) + i++; // round up + else if (i & 1) + i++; // round to nearest even break; } return i;