fpu: Bound increment for scalbn
authorRichard Henderson <richard.henderson@linaro.org>
Tue, 17 Apr 2018 02:53:28 +0000 (16:53 -1000)
committerPeter Maydell <peter.maydell@linaro.org>
Tue, 17 Apr 2018 13:52:38 +0000 (14:52 +0100)
Without bounding the increment, we can overflow exp either here
in scalbn_decomposed or when adding the bias in round_canonical.
This can result in e.g. underflowing to 0 instead of overflowing
to infinity.

The old softfloat code did bound the increment.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Tested-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
fpu/softfloat.c

index d90d79d777f8f1285a36efddfe65e9e853e104df..70e0c40a1c0b56361673704a31588639d45cdc05 100644 (file)
@@ -1878,6 +1878,12 @@ static FloatParts scalbn_decomposed(FloatParts a, int n, float_status *s)
         return return_nan(a, s);
     }
     if (a.cls == float_class_normal) {
+        /* The largest float type (even though not supported by FloatParts)
+         * is float128, which has a 15 bit exponent.  Bounding N to 16 bits
+         * still allows rounding to infinity, without allowing overflow
+         * within the int32_t that backs FloatParts.exp.
+         */
+        n = MIN(MAX(n, -0x10000), 0x10000);
         a.exp += n;
     }
     return a;