Skip to contentSkip to conversation

softfloat: FloatRelation cleanups

PATCH In review Richard Henderson 2 participants 11 messages Apr 1, 2022 – Apr 27, 2022

lore
Share & tools
lore
#0 lore raw

cover softfloat: FloatRelation cleanups

Make consistent use of FloatRelation throughout the implementation of the float compare functions.

I don't yet know if this actually solves Coverity issues, but they all look a bit cleaner, I think.

r~

Richard Henderson (3):
softfloat: Fix declaration of partsN_compare
softfloat: Use FloatRelation within partsN_compare
softfloat: Use FloatRelation for fracN_cmp

 fpu/softfloat.c           | 20 +++++++++++---------
 fpu/softfloat-parts.c.inc | 11 +++++++----
 2 files changed, 18 insertions(+), 13 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/3 softfloat: Fix declaration of partsN_compare

The declaration used 'int', while the definition used 'FloatRelation'. This should have resulted in a compiler error, but mysteriously didn't.

---
 fpu/softfloat.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fpu/softfloat.c +4 -4
index 7f524d4377..7e62fcf414 100644
@@ -874,10 +874,10 @@ static FloatParts128 *parts128_minmax(FloatParts128 *a, FloatParts128 *b,
 #define parts_minmax(A, B, S, F) \
     PARTS_GENERIC_64_128(minmax, A)(A, B, S, F)
 
-static int parts64_compare(FloatParts64 *a, FloatParts64 *b,
-                           float_status *s, bool q);
-static int parts128_compare(FloatParts128 *a, FloatParts128 *b,
-                            float_status *s, bool q);
+static FloatRelation parts64_compare(FloatParts64 *a, FloatParts64 *b,
+                                     float_status *s, bool q);
+static FloatRelation parts128_compare(FloatParts128 *a, FloatParts128 *b,
+                                      float_status *s, bool q);
 
 #define parts_compare(A, B, S, Q) \
     PARTS_GENERIC_64_128(compare, A)(A, B, S, Q)
Signature · 1 line
2.25.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] softfloat: Fix declaration of partsN_compare

Quoted 8 lines · Richard Henderson

The declaration used 'int', while the definition used 'FloatRelation'. This should have resulted in a compiler error, but mysteriously didn't.

---
 fpu/softfloat.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

thanks
-- PMM

#3 lore raw

Re: [PATCH 1/3] softfloat: Fix declaration of partsN_compare

In reply to Richard Henderson
Quoted 2 lines · Richard Henderson

The declaration used 'int', while the definition used 'FloatRelation'. This should have resulted in a compiler error, but mysteriously didn't.

Bah, of course there's no error -- this is C not C++.

The enumeration has values -1 ... 2, which means that the enumeration is compatible with an implementation defined signed integer type, which for our set of hosts will be 'int'. So, no conflict.

I'll edit the commit message.

r~

#4 lore raw

Re: [PATCH 1/3] softfloat: Fix declaration of partsN_compare

Quoted 10 lines · Richard Henderson
Quoted 2 lines · Richard Henderson

The declaration used 'int', while the definition used 'FloatRelation'. This should have resulted in a compiler error, but mysteriously didn't.

Bah, of course there's no error -- this is C not C++.

The enumeration has values -1 ... 2, which means that the enumeration is compatible with an implementation defined signed integer type, which for our set of hosts will be 'int'. So, no conflict.

The types are compatible, but it's weird that the compiler doesn't warn that the prototype and definition are different types: it seems like the kind of "technically valid but usually a bug" that a warning would be nice for.

-- PMM

#6 lore raw

PATCH 2/3 softfloat: Use FloatRelation within partsN_compare

In reply to Richard Henderson

As the return type is FloatRelation, it's clearer to use the type for 'cmp' within the function.

---
 fpu/softfloat-parts.c.inc | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
fpu/softfloat-parts.c.inc +7 -4
index db3e1f393d..bbeadaa189 100644
@@ -1327,16 +1327,19 @@ static FloatRelation partsN(compare)(FloatPartsN *a, FloatPartsN *b,
                                      float_status *s, bool is_quiet)
 {
     int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
-    int cmp;
 
     if (likely(ab_mask == float_cmask_normal)) {
+        FloatRelation cmp;
+
         if (a->sign != b->sign) {
             goto a_sign;
         }
-        if (a->exp != b->exp) {
-            cmp = a->exp < b->exp ? -1 : 1;
-        } else {
+        if (a->exp == b->exp) {
             cmp = frac_cmp(a, b);
+        } else if (a->exp < b->exp) {
+            cmp = float_relation_less;
+        } else {
+            cmp = float_relation_greater;
         }
         if (a->sign) {
             cmp = -cmp;
Signature · 1 line
2.25.1
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] softfloat: Use FloatRelation within partsN_compare

Quoted 8 lines · Richard Henderson

As the return type is FloatRelation, it's clearer to use the type for 'cmp' within the function.

---
 fpu/softfloat-parts.c.inc | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

thanks
-- PMM

#8 lore raw

PATCH 3/3 softfloat: Use FloatRelation for fracN_cmp

In reply to Richard Henderson

Since the caller, partsN_compare, is now exclusively using FloatRelation, it's clearer to use it here too.

---
 fpu/softfloat.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
fpu/softfloat.c +7 -5
index 7e62fcf414..9f2d4e7a29 100644
@@ -957,21 +957,23 @@ static void frac128_allones(FloatParts128 *a)
 
 #define frac_allones(A)  FRAC_GENERIC_64_128(allones, A)(A)
 
-static int frac64_cmp(FloatParts64 *a, FloatParts64 *b)
+static FloatRelation frac64_cmp(FloatParts64 *a, FloatParts64 *b)
 {
-    return a->frac == b->frac ? 0 : a->frac < b->frac ? -1 : 1;
+    return (a->frac == b->frac ? float_relation_equal
+            : a->frac < b->frac ? float_relation_less
+            : float_relation_greater);
 }
 
-static int frac128_cmp(FloatParts128 *a, FloatParts128 *b)
+static FloatRelation frac128_cmp(FloatParts128 *a, FloatParts128 *b)
 {
     uint64_t ta = a->frac_hi, tb = b->frac_hi;
     if (ta == tb) {
         ta = a->frac_lo, tb = b->frac_lo;
         if (ta == tb) {
-            return 0;
+            return float_relation_equal;
         }
     }
-    return ta < tb ? -1 : 1;
+    return ta < tb ? float_relation_less : float_relation_greater;
 }
 
 #define frac_cmp(A, B)  FRAC_GENERIC_64_128(cmp, A)(A, B)
Signature · 1 line
2.25.1
#10 lore raw
In reply to Richard Henderson
Quoted 8 lines · Richard Henderson

Make consistent use of FloatRelation throughout the implementation of the float compare functions.

I don't yet know if this actually solves Coverity issues, but they all look a bit cleaner, I think.

r~

Quoted 9 lines

Richard Henderson (3):
softfloat: Fix declaration of partsN_compare
softfloat: Use FloatRelation within partsN_compare
softfloat: Use FloatRelation for fracN_cmp

  fpu/softfloat.c           | 20 +++++++++++---------
  fpu/softfloat-parts.c.inc | 11 +++++++----
  2 files changed, 18 insertions(+), 13 deletions(-)

Queuing to tcg-next.

r~