Skip to contentSkip to conversation

softfloat fixes

PATCH In review Richard Henderson 3 participants 7 messages Apr 17, 2018

lore
Share & tools
lore
#0 lore raw

cover softfloat fixes

An alternate fix for Bastian's division problem and another fix for a scalbn problem exposed during SVE testing. Again, the SVE issue should be visible with normal NEON insns too.

r~

Richard Henderson (2):
fpu: Check for inf/x before x/0
fpu: Bound increment for scalbn

 fpu/softfloat.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH 1/2 fpu: Check for inf/x before x/0

The re-factoring of div_floats changed the order of checking meaning an operation like -inf/0 erroneously raises the divbyzero flag. IEEE-754 (2008) specifies this should only occur for operations on finite operands.

We fix this by moving the check on the dividend being Inf/0 to before the divisor is zero check.

Cc Bastian Koppelmann <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Peter Maydell <[email protected]>
---
 fpu/softfloat.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fpu/softfloat.c +8 -3
index fb8663f59e..ba6e654050 100644
@@ -1146,15 +1146,20 @@ static FloatParts div_floats(FloatParts a, FloatParts b, float_status *s)
         a.cls = float_class_dnan;
         return a;
     }
-    /* Div 0 => Inf */
+    /* Inf / x */
+    if (a.cls == float_class_inf) {
+        a.sign = sign;
+        return a;
+    }
+    /* x / 0 => Inf */
     if (b.cls == float_class_zero) {
         s->float_exception_flags |= float_flag_divbyzero;
         a.cls = float_class_inf;
         a.sign = sign;
         return a;
     }
-    /* Inf / x or 0 / x */
-    if (a.cls == float_class_inf || a.cls == float_class_zero) {
+    /* 0 / x */
+    if (a.cls == float_class_zero) {
         a.sign = sign;
         return a;
     }
Signature · 1 line
2.14.3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] fpu: Check for inf/x before x/0

Quoted 8 lines · Richard Henderson

The re-factoring of div_floats changed the order of checking meaning an operation like -inf/0 erroneously raises the divbyzero flag. IEEE-754 (2008) specifies this should only occur for operations on finite operands.

We fix this by moving the check on the dividend being Inf/0 to before the divisor is zero check.

I've applied Alex's fix to master for this, since it seems more straightforward.

thanks
-- PMM

#3 lore raw

PATCH 2/2 fpu: Bound increment for scalbn

In reply to Richard Henderson

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.

---
 fpu/softfloat.c | 6 ++++++
 1 file changed, 6 insertions(+)
fpu/softfloat.c +6 -0
index ba6e654050..a589f328c9 100644
@@ -1883,6 +1883,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;
Signature · 1 line
2.14.3
Reviewed-by on v1 2/2
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] fpu: Bound increment for scalbn

Quoted 20 lines · Richard Henderson

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.

---
 fpu/softfloat.c | 6 ++++++
 1 file changed, 6 insertions(+)
fpu/softfloat.c +0 -0
index ba6e654050..a589f328c9 100644
@@ -1883,6 +1883,12 @@ static FloatParts scalbn_decomposed(FloatParts a, int n, float_status *s)
         return return_nan(a, s);
     }
     if (a.cls == float_class_normal) {

fpu/softfloat.c static FloatParts scalbn_decomposed(FloatParts a, int n, float_status *s)

Quoted 10 lines
fpu/softfloat.c +6 -1
+        /* 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;
--

thanks
-- PMM

Reviewed-by Tested-by on v1 2/2
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] fpu: Bound increment for scalbn

In reply to Richard Henderson
Quoted 19 lines · Richard Henderson

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.

---
 fpu/softfloat.c | 6 ++++++
 1 file changed, 6 insertions(+)
fpu/softfloat.c +0 -0
index ba6e654050..a589f328c9 100644
@@ -1883,6 +1883,12 @@ static FloatParts scalbn_decomposed(FloatParts a, int n, float_status *s)
         return return_nan(a, s);
     }

fpu/softfloat.c static FloatParts scalbn_decomposed(FloatParts a, int n, float_status *s) · line 1894

Quoted 10 lines
fpu/softfloat.c +6 -0
     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;

(risu FWIW although it obviously didn't catch this failure ;-)

--
Alex Bennée

#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] fpu: Bound increment for scalbn

Quoted 36 lines · Alex Bennée
Quoted 19 lines · Richard Henderson

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.

---
 fpu/softfloat.c | 6 ++++++
 1 file changed, 6 insertions(+)
fpu/softfloat.c +0 -0
index ba6e654050..a589f328c9 100644
@@ -1883,6 +1883,12 @@ static FloatParts scalbn_decomposed(FloatParts a, int n, float_status *s)
         return return_nan(a, s);
     }

fpu/softfloat.c static FloatParts scalbn_decomposed(FloatParts a, int n, float_status *s) · line 1894

Quoted 10 lines
fpu/softfloat.c +6 -0
     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;
Quoted 1 line

(risu FWIW although it obviously didn't catch this failure ;-)

Thanks; applied this patch to master.

-- PMM