Avoid exponent overflow as well as checking that we don't
lose information with opposing scaling. Use it in partsN(scalbn)
and partsN(round_to_int_normal).
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
fpu/softfloat.c | 17 +++++++++++++++++
fpu/softfloat-parts.c.inc | 5 ++---
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/fpu/softfloat.c b/fpu/softfloat.c
index a762f4b43a..722bf5ecda 100644
--- a/fpu/softfloat.c
+++ b/fpu/softfloat.c
@@ -601,6 +601,23 @@ static float128 QEMU_FLATTEN float128_pack_raw(const FloatParts128 *p)
*----------------------------------------------------------------------------*/
#include "softfloat-specialize.c.inc"
+static int32_t exp_scalbn(int32_t exp, int32_t scale)
+{
+ /*
+ * Catch chains of scaling which lose information.
+ * In particular, if the exponent has been saturated,
+ * do not allow it to become unsaturated.
+ */
+ if (unlikely(exp == INT32_MAX)) {
+ assert(scale >= 0);
+ } else if (unlikely(exp == INT32_MIN)) {
+ assert(scale <= 0);
+ } else {
+ exp = sadd32_saturate(exp, scale);
+ }
+ return exp;
+}
+
/*
* Helper functions for softfloat-parts.c.inc, per-size operations.
*/
diff --git a/fpu/softfloat-parts.c.inc b/fpu/softfloat-parts.c.inc
index 914e588ec8..0c617beef7 100644
--- a/fpu/softfloat-parts.c.inc
+++ b/fpu/softfloat-parts.c.inc
@@ -1108,8 +1108,7 @@ static bool partsN(round_to_int_normal)(FloatPartsN *a, FloatRoundMode rmode,
uint64_t frac_lsb, frac_lsbm1, rnd_even_mask, rnd_mask, inc;
int shift_adj;
- scale = MIN(MAX(scale, -0x10000), 0x10000);
- a->exp += scale;
+ a->exp = exp_scalbn(a->exp, scale);
if (a->exp < 0) {
bool one;
@@ -1633,7 +1632,7 @@ FloatPartsN partsN(scalbn)(const FloatPartsN *a, int n, float_status *s)
case float_class_normal:
{
FloatPartsN r = *a;
- r.exp += MIN(MAX(n, -0x10000), 0x10000);
+ r.exp = exp_scalbn(r.exp, n);
return r;
}
default:
--
2.43.0