Skip to contentSkip to conversation

target/s390x: Fix the floating-point multiply-and-add NaN rules

PATCH In review Ilya Leoshkevich 2 participants 5 messages Oct 23, 2024 – Nov 15, 2024

lore
Share & tools
lore
#0 lore raw

cover target/s390x: Fix the floating-point multiply-and-add NaN rules

Hi,

Peter reported on IRC that FMA NaN handling was likely broken on s390x. Patch 1 of this series fixes the issue, patch 2 adds a test. For the sake of readability, the test sacrifices the 90-character line length rule.

Best regards,
Ilya

Ilya Leoshkevich (2):
target/s390x: Fix the floating-point multiply-and-add NaN rules tests/tcg/s390x: Add the floating-point multiply-and-add test

 fpu/softfloat-specialize.c.inc    |  19 +++
 target/s390x/tcg/fpu_helper.c     |   8 +-
 target/s390x/tcg/vec_fpu_helper.c |  12 +-
 tests/tcg/s390x/Makefile.target   |   5 +-
 tests/tcg/s390x/float.h           | 104 +++++++++++++
 tests/tcg/s390x/fma.c             | 233 ++++++++++++++++++++++++++++++
 tests/tcg/s390x/vfminmax.c        | 223 ++++++++++------------------
 7 files changed, 449 insertions(+), 155 deletions(-)
 create mode 100644 tests/tcg/s390x/float.h
 create mode 100644 tests/tcg/s390x/fma.c
Signature · 1 line
2.47.0
#3 lore raw

PATCH 2/2 tests/tcg/s390x: Add the floating-point multiply-and-add test

Add a test to prevent regressions.
Share some useful pieces with the vfminmax test. Remove the duplicates from the floating point class values.

---
 tests/tcg/s390x/Makefile.target |   5 +-
 tests/tcg/s390x/float.h         | 104 ++++++++++++++
 tests/tcg/s390x/fma.c           | 233 ++++++++++++++++++++++++++++++++
 tests/tcg/s390x/vfminmax.c      | 223 +++++++++++-------------------
 4 files changed, 420 insertions(+), 145 deletions(-)
 create mode 100644 tests/tcg/s390x/float.h
 create mode 100644 tests/tcg/s390x/fma.c
tests/tcg/s390x/Makefile.target +4 -1
index 2dab4f45822..da5fe71a407 100644
@@ -74,8 +74,11 @@ $(Z13_TESTS): CFLAGS+=-march=z13 -O2
 TESTS+=$(Z13_TESTS)
 
 ifneq ($(CROSS_CC_HAS_Z14),)
-Z14_TESTS=vfminmax
+Z14_TESTS=fma vfminmax
+fma: float.h
+fma: LDFLAGS+=-lm
 vfminmax: LDFLAGS+=-lm
+vfminmax: float.h
 $(Z14_TESTS): CFLAGS+=-march=z14 -O2
 TESTS+=$(Z14_TESTS)
 endif
tests/tcg/s390x/float.h new file +104 -0

104 lines not on this page · raw message

tests/tcg/s390x/fma.c new file +233 -0

233 lines not on this page · raw message

tests/tcg/s390x/vfminmax.c +79 -144

278 lines not on this page · raw message

Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/2 target/s390x: Fix the floating-point multiply-and-add NaN rules

In reply to Ilya Leoshkevich

Order the helper arguments to match the Principles of Operation. Implement the "Results: MULTIPLY AND ADD" table in pickNaNMulAdd().

---
 fpu/softfloat-specialize.c.inc    | 19 +++++++++++++++++++
 target/s390x/tcg/fpu_helper.c     |  8 ++++----
 target/s390x/tcg/vec_fpu_helper.c | 12 ++++++------
 3 files changed, 29 insertions(+), 10 deletions(-)
fpu/softfloat-specialize.c.inc +19 -0
index 4e279b9bc40..62197373c93 100644
@@ -635,6 +635,25 @@ static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
         float_raise(float_flag_invalid | float_flag_invalid_imz, status);
     }
     return 3; /* default NaN */
+#elif defined(TARGET_S390X)
+    if (infzero) {
+        float_raise(float_flag_invalid | float_flag_invalid_imz, status);
+        return 3;
+    }
+
+    if (is_snan(a_cls)) {
+        return 0;
+    } else if (is_snan(b_cls)) {
+        return 1;
+    } else if (is_snan(c_cls)) {
+        return 2;
+    } else if (is_qnan(a_cls)) {
+        return 0;
+    } else if (is_qnan(b_cls)) {
+        return 1;
+    } else {
+        return 2;
+    }
 #elif defined(TARGET_SPARC)
     /* For (inf,0,nan) return c. */
     if (infzero) {
target/s390x/tcg/fpu_helper.c +4 -4
index d8bd5748faf..5041c139627 100644
@@ -780,7 +780,7 @@ uint32_t HELPER(kxb)(CPUS390XState *env, Int128 a, Int128 b)
 uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
                       uint64_t f2, uint64_t f3)
 {
-    float32 ret = float32_muladd(f2, f3, f1, 0, &env->fpu_status);
+    float32 ret = float32_muladd(f3, f2, f1, 0, &env->fpu_status);
     handle_exceptions(env, false, GETPC());
     return ret;
 }
@@ -789,7 +789,7 @@ uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
 uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
                       uint64_t f2, uint64_t f3)
 {
-    float64 ret = float64_muladd(f2, f3, f1, 0, &env->fpu_status);
+    float64 ret = float64_muladd(f3, f2, f1, 0, &env->fpu_status);
     handle_exceptions(env, false, GETPC());
     return ret;
 }
@@ -798,7 +798,7 @@ uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
 uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
                       uint64_t f2, uint64_t f3)
 {
-    float32 ret = float32_muladd(f2, f3, f1, float_muladd_negate_c,
+    float32 ret = float32_muladd(f3, f2, f1, float_muladd_negate_c,
                                  &env->fpu_status);
     handle_exceptions(env, false, GETPC());
     return ret;
@@ -808,7 +808,7 @@ uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
 uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
                       uint64_t f2, uint64_t f3)
 {
-    float64 ret = float64_muladd(f2, f3, f1, float_muladd_negate_c,
+    float64 ret = float64_muladd(f3, f2, f1, float_muladd_negate_c,
                                  &env->fpu_status);
     handle_exceptions(env, false, GETPC());
     return ret;
target/s390x/tcg/vec_fpu_helper.c +6 -6
index 75cf605b9f4..1bbaa82fe8a 100644
@@ -621,8 +621,8 @@ static void vfma32(S390Vector *v1, const S390Vector *v2, const S390Vector *v3,
     int i;
 
     for (i = 0; i < 4; i++) {
-        const float32 a = s390_vec_read_float32(v2, i);
-        const float32 b = s390_vec_read_float32(v3, i);
+        const float32 a = s390_vec_read_float32(v3, i);
+        const float32 b = s390_vec_read_float32(v2, i);
         const float32 c = s390_vec_read_float32(v4, i);
         float32 ret = float32_muladd(a, b, c, flags, &env->fpu_status);
 
@@ -645,8 +645,8 @@ static void vfma64(S390Vector *v1, const S390Vector *v2, const S390Vector *v3,
     int i;
 
     for (i = 0; i < 2; i++) {
-        const float64 a = s390_vec_read_float64(v2, i);
-        const float64 b = s390_vec_read_float64(v3, i);
+        const float64 a = s390_vec_read_float64(v3, i);
+        const float64 b = s390_vec_read_float64(v2, i);
         const float64 c = s390_vec_read_float64(v4, i);
         const float64 ret = float64_muladd(a, b, c, flags, &env->fpu_status);
 
@@ -664,8 +664,8 @@ static void vfma128(S390Vector *v1, const S390Vector *v2, const S390Vector *v3,
                     const S390Vector *v4, CPUS390XState *env, bool s, int flags,
                     uintptr_t retaddr)
 {
-    const float128 a = s390_vec_read_float128(v2);
-    const float128 b = s390_vec_read_float128(v3);
+    const float128 a = s390_vec_read_float128(v3);
+    const float128 b = s390_vec_read_float128(v2);
     const float128 c = s390_vec_read_float128(v4);
     uint8_t vxc, vec_exc = 0;
     float128 ret;
Signature · 1 line
2.47.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] tests/tcg/s390x: Add the floating-point multiply-and-add test

In reply to Ilya Leoshkevich
Quoted 6 lines · Ilya Leoshkevich

Add a test to prevent regressions.
Share some useful pieces with the vfminmax test. Remove the duplicates from the floating point class values.

Quoted 8 lines
---
 tests/tcg/s390x/Makefile.target |   5 +-
 tests/tcg/s390x/float.h         | 104 ++++++++++++++
 tests/tcg/s390x/fma.c           | 233 ++++++++++++++++++++++++++++++++
 tests/tcg/s390x/vfminmax.c      | 223 +++++++++++-------------------
 4 files changed, 420 insertions(+), 145 deletions(-)
 create mode 100644 tests/tcg/s390x/float.h
 create mode 100644 tests/tcg/s390x/fma.c

I haven't looked at this very closely, but it's only test code, so

thanks
-- PMM