Skip to contentSkip to conversation

target/mips: Adjusting the results when dividing by zero in MSA instructions

PATCH In review Mateja Marjanovic 3 participants 7 messages Apr 2, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover target/mips: Adjusting the results when dividing by zero in MSA instructions

From: Mateja Marjanovic <[email protected]>

The behaviour when executing DIV_<U|S>.<B|H|W|D> and MOD_<U|S>.<B|H|W|D> differs on a referent hardware (FPGA MIPS 64 r6, little endian) and on QEMU, when the divisor is equal to zero. That is not a real bug, because the behaviour in that case is unpredictable (references in commit messages).

v2:
- Add consistency in DIV_U.df and DIV_S.df helper implementation
- Adjust the commit messages and the cover letter, making them clearer and more understandable
- Correct the spelling in the commit messages and in the cover letter

Mateja Marjanovic (2):
target/mips: Make the results of DIV_<U|S>.<B|H|W|D> the same as on
hardware
target/mips: Make the results of MOD_<U|S>.<B|H|W|D> the same as on
hardware

 target/mips/msa_helper.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/2 target/mips: Make the results of DIV_<U|S>.<B|H|W|D> the same as on hardware

From: Mateja Marjanovic <[email protected]>

MSA instructions DIV_<U|S>.<B|H|W|D> when dividing by zero, didn't return the same value when executed on a referent hardware (FPGA MIPS 64 r6, little endian) and when executed on QEMU, which is not a real bug, because the result when dividing by zero is UNPREDICTABLE [1] (page 141, 142).

[1] MIPS Architecture for Programmers
Volume IV-j: The MIPS64 SIMD
Architecture Module, Revision 1.12

---
 target/mips/msa_helper.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/mips/msa_helper.c +3 -2
index 655148d..8c77f12 100644
@@ -641,14 +641,15 @@ static inline int64_t msa_div_s_df(uint32_t df, int64_t arg1, int64_t arg2)
     if (arg1 == DF_MIN_INT(df) && arg2 == -1) {
         return DF_MIN_INT(df);
     }
-    return arg2 ? arg1 / arg2 : 0;
+    return arg2 ? arg1 / arg2
+                : arg1 >= 0 ? -1 : 1;
 }
 
 static inline int64_t msa_div_u_df(uint32_t df, int64_t arg1, int64_t arg2)
 {
     uint64_t u_arg1 = UNSIGNED(arg1, df);
     uint64_t u_arg2 = UNSIGNED(arg2, df);
-    return u_arg2 ? u_arg1 / u_arg2 : 0;
+    return arg2 ? u_arg1 / u_arg2 : -1;
 }
 
 static inline int64_t msa_mod_s_df(uint32_t df, int64_t arg1, int64_t arg2)
Signature · 1 line
2.7.4
Reviewed-by on v2 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] target/mips: Make the results of DIV_<U|S>.<B|H|W|D> the same as on hardware

Quoted 11 lines

From: Mateja Marjanovic <[email protected]>
Subject: [PATCH v2 1/2] target/mips: Make the results of DIV_<U|S>.<B|H|W|D> the same as on > hardware

From: Mateja Marjanovic <[email protected]>

MSA instructions DIV_<U|S>.<B|H|W|D> when dividing by zero, didn't return the same value when executed on a referent hardware (FPGA MIPS 64 r6, little endian) and when executed on QEMU, which is not a real bug, because the result when dividing by zero is UNPREDICTABLE [1] (page 141, 142).

Quoted 6 lines

[1] MIPS Architecture for Programmers
Volume IV-j: The MIPS64 SIMD
Architecture Module, Revision 1.12

---

Quoted 27 lines
 target/mips/msa_helper.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/mips/msa_helper.c +3 -3
index 655148d..8c77f12 100644
@@ -641,14 +641,15 @@ static inline int64_t msa_div_s_df(uint32_t df, int64_t arg1, int64_t > arg2)
     if (arg1 == DF_MIN_INT(df) && arg2 == -1) {
         return DF_MIN_INT(df);
     }
-    return arg2 ? arg1 / arg2 : 0;
+    return arg2 ? arg1 / arg2
+                : arg1 >= 0 ? -1 : 1;
 }
 
 static inline int64_t msa_div_u_df(uint32_t df, int64_t arg1, int64_t arg2)
 {
     uint64_t u_arg1 = UNSIGNED(arg1, df);
     uint64_t u_arg2 = UNSIGNED(arg2, df);
-    return u_arg2 ? u_arg1 / u_arg2 : 0;
+    return arg2 ? u_arg1 / u_arg2 : -1;
 }
 
 static inline int64_t msa_mod_s_df(uint32_t df, int64_t arg1, int64_t arg2)
--

2.7.4

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] target/mips: Make the results of DIV_<U|S>.<B|H|W|D> the same as on hardware

In reply to Mateja Marjanovic

Hi Mateja,

Quoted 7 lines · Mateja Marjanovic

From: Mateja Marjanovic <[email protected]>

MSA instructions DIV_<U|S>.<B|H|W|D> when dividing by zero, didn't return the same value when executed on a referent hardware (FPGA MIPS 64 r6, little endian) and when executed on QEMU, which is not a real bug, because the result when dividing by zero is UNPREDICTABLE [1] (page 141, 142).

I'm surprised by the arch, I'd have expected a MSA floating point exception instead of UNPREDICTABLE.

So here we decide to follow the FPGA model behavior rather than the architecture...
If the community agree, my only request is to add a comment in the code that this is the "FPGA MIPS 64 r6" behavior (else while looking at this code later I'd be tempted to revert to a 0 return value instead of -1/1).

Quoted 33 lines

[1] MIPS Architecture for Programmers
Volume IV-j: The MIPS64 SIMD
Architecture Module, Revision 1.12

---
 target/mips/msa_helper.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/mips/msa_helper.c +3 -2
index 655148d..8c77f12 100644
@@ -641,14 +641,15 @@ static inline int64_t msa_div_s_df(uint32_t df, int64_t arg1, int64_t arg2)
     if (arg1 == DF_MIN_INT(df) && arg2 == -1) {
         return DF_MIN_INT(df);
     }
-    return arg2 ? arg1 / arg2 : 0;
+    return arg2 ? arg1 / arg2
+                : arg1 >= 0 ? -1 : 1;
 }
 
 static inline int64_t msa_div_u_df(uint32_t df, int64_t arg1, int64_t arg2)
 {
     uint64_t u_arg1 = UNSIGNED(arg1, df);
     uint64_t u_arg2 = UNSIGNED(arg2, df);
-    return u_arg2 ? u_arg1 / u_arg2 : 0;
+    return arg2 ? u_arg1 / u_arg2 : -1;
 }
 
 static inline int64_t msa_mod_s_df(uint32_t df, int64_t arg1, int64_t arg2)
#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] target/mips: Make the results of DIV_<U|S>.<B|H|W|D> the same as on hardware

Quoted 5 lines · Philippe Mathieu-Daudé

Hi Mateja,

Quoted 2 lines · Mateja Marjanovic

From: Mateja Marjanovic <[email protected]>

Quoted 7 lines
Quoted 5 lines

MSA instructions DIV_<U|S>.<B|H|W|D> when dividing by zero, didn't return the same value when executed on a referent hardware (FPGA MIPS 64 r6, little endian) and when executed on QEMU, which is not a real bug, because the result when dividing by zero is UNPREDICTABLE [1] (page 141, 142).

I'm surprised by the arch, I'd have expected a MSA floating point exception instead of UNPREDICTABLE.

Our architecture documents do not list any exceptions related to division by zero, for instructions DIV_<U|S>.<B|H|W|D> and MOD_<U|S>.<B|H|W|D>.

The only exceptions that are mentioned are:
Reserved Instruction Exception, MSA Disabled Exception. [1] (page 141, 142, 254, 255).

Quoted 37 lines

So here we decide to follow the FPGA model behavior rather than the architecture...
If the community agree, my only request is to add a comment in the code that this is the "FPGA MIPS 64 r6" behavior (else while looking at this code later I'd be tempted to revert to a 0 return value instead of -1/1).

Quoted 30 lines

[1] MIPS Architecture for Programmers
Volume IV-j: The MIPS64 SIMD
Architecture Module, Revision 1.12

---
  target/mips/msa_helper.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
target/mips/msa_helper.c +3 -2
index 655148d..8c77f12 100644
@@ -641,14 +641,15 @@ static inline int64_t msa_div_s_df(uint32_t df, int64_t arg1, int64_t arg2)
      if (arg1 == DF_MIN_INT(df) && arg2 == -1) {
          return DF_MIN_INT(df);
      }
-    return arg2 ? arg1 / arg2 : 0;
+    return arg2 ? arg1 / arg2
+                : arg1 >= 0 ? -1 : 1;
  }
 
  static inline int64_t msa_div_u_df(uint32_t df, int64_t arg1, int64_t arg2)
  {
      uint64_t u_arg1 = UNSIGNED(arg1, df);
      uint64_t u_arg2 = UNSIGNED(arg2, df);
-    return u_arg2 ? u_arg1 / u_arg2 : 0;
+    return arg2 ? u_arg1 / u_arg2 : -1;
  }
 
Quoted 1 line

target/mips/msa_helper.c static inline int64_t msa_div_s_df(uint32_t df, int64_t arg1, int64_t arg2) · line 655

Quoted 1 line
target/mips/msa_helper.c +0 -0
  static inline int64_t msa_mod_s_df(uint32_t df, int64_t arg1, int64_t arg2)

Thanks,
Mateja

#5 lore raw

PATCH v2 2/2 target/mips: Make the results of MOD_<U|S>.<B|H|W|D> the same as on hardware

In reply to Mateja Marjanovic

From: Mateja Marjanovic <[email protected]>

MSA instructions MOD_<U|S>.<B|H|W|D> when dividing by zero, didn't return the same value when executed on a referent hardware (FPGA MIPS 64 r6, little endian) and when executed on QEMU, which is not a real bug, because the result when dividing by zero is UNPREDICTABLE [1] (page 255, 256).

[1] MIPS Architecture for Programmers
Volume IV-j: The MIPS64 SIMD
Architecture Module, Revision 1.12

---
 target/mips/msa_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/mips/msa_helper.c +2 -2
index 8c77f12..6693ba6 100644
@@ -657,14 +657,14 @@ static inline int64_t msa_mod_s_df(uint32_t df, int64_t arg1, int64_t arg2)
     if (arg1 == DF_MIN_INT(df) && arg2 == -1) {
         return 0;
     }
-    return arg2 ? arg1 % arg2 : 0;
+    return arg2 ? arg1 % arg2 : arg1;
 }
 
 static inline int64_t msa_mod_u_df(uint32_t df, int64_t arg1, int64_t arg2)
 {
     uint64_t u_arg1 = UNSIGNED(arg1, df);
     uint64_t u_arg2 = UNSIGNED(arg2, df);
-    return u_arg2 ? u_arg1 % u_arg2 : 0;
+    return u_arg2 ? u_arg1 % u_arg2 : u_arg1;
 }
 
 #define SIGNED_EVEN(a, df) \
Signature · 1 line
2.7.4
Reviewed-by on v2 2/2
#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target/mips: Make the results of MOD_<U|S>.<B|H|W|D> the same as on hardware

Quoted 11 lines

From: Mateja Marjanovic <[email protected]>
Subject: [PATCH v2 2/2] target/mips: Make the results of MOD_<U|S>.<B|H|W|D> the same as on hardware

From: Mateja Marjanovic <[email protected]>

MSA instructions MOD_<U|S>.<B|H|W|D> when dividing by zero, didn't return the same value when executed on a referent hardware (FPGA MIPS 64 r6, little endian) and when executed on QEMU, which is not a real bug, because the result when dividing by zero is UNPREDICTABLE [1] (page 255, 256).

Quoted 6 lines

[1] MIPS Architecture for Programmers
Volume IV-j: The MIPS64 SIMD
Architecture Module, Revision 1.12

---

Quoted 26 lines
 target/mips/msa_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/mips/msa_helper.c +2 -3
index 8c77f12..6693ba6 100644
@@ -657,14 +657,14 @@ static inline int64_t msa_mod_s_df(uint32_t df, int64_t arg1, int64_t > arg2)
     if (arg1 == DF_MIN_INT(df) && arg2 == -1) {
         return 0;
     }
-    return arg2 ? arg1 % arg2 : 0;
+    return arg2 ? arg1 % arg2 : arg1;
 }
 
 static inline int64_t msa_mod_u_df(uint32_t df, int64_t arg1, int64_t arg2)
 {
     uint64_t u_arg1 = UNSIGNED(arg1, df);
     uint64_t u_arg2 = UNSIGNED(arg2, df);
-    return u_arg2 ? u_arg1 % u_arg2 : 0;
+    return u_arg2 ? u_arg1 % u_arg2 : u_arg1;
 }
 
 #define SIGNED_EVEN(a, df) \
--

2.7.4