Skip to contentSkip to conversation

target/riscv: Fix shift count overflow

PATCH Superseded demin.han 3 participants 3 messages Feb 25, 2024 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

demin.han

author
#0 lore raw

PATCH v2 1/1 target/riscv: Fix shift count overflow

The result of (8 - 3 - vlmul) is negtive when vlmul >= 6, and results in wrong vill.

---
 target/riscv/vector_helper.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
target/riscv/vector_helper.c +2 -3
index 84cec73eb2..fe56c007d5 100644
@@ -44,6 +44,7 @@ target_ulong HELPER(vsetvl)(CPURISCVState *env, target_ulong s1,
     target_ulong reserved = s2 &
                             MAKE_64BIT_MASK(R_VTYPE_RESERVED_SHIFT,
                                             xlen - 1 - R_VTYPE_RESERVED_SHIFT);
+    uint16_t vlen = cpu->cfg.vlenb << 3;
     int8_t lmul;
 
     if (vlmul & 4) {
@@ -53,10 +54,8 @@ target_ulong HELPER(vsetvl)(CPURISCVState *env, target_ulong s1,
          * VLEN * LMUL >= SEW
          * VLEN >> (8 - lmul) >= sew
          * (vlenb << 3) >> (8 - lmul) >= sew
-         * vlenb >> (8 - 3 - lmul) >= sew
          */
-        if (vlmul == 4 ||
-            cpu->cfg.vlenb >> (8 - 3 - vlmul) < sew) {
+        if (vlmul == 4 || (vlen >> (8 - vlmul)) < sew) {
             vill = true;
         }
     }
Signature · 1 line
2.43.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 5 lines · demin.han

The result of (8 - 3 - vlmul) is negtive when vlmul >= 6, and results in wrong vill.

---

Quoted 27 lines
  target/riscv/vector_helper.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
target/riscv/vector_helper.c +2 -3
index 84cec73eb2..fe56c007d5 100644
@@ -44,6 +44,7 @@ target_ulong HELPER(vsetvl)(CPURISCVState *env, target_ulong s1,
      target_ulong reserved = s2 &
                              MAKE_64BIT_MASK(R_VTYPE_RESERVED_SHIFT,
                                              xlen - 1 - R_VTYPE_RESERVED_SHIFT);
+    uint16_t vlen = cpu->cfg.vlenb << 3;
      int8_t lmul;
 
      if (vlmul & 4) {
@@ -53,10 +54,8 @@ target_ulong HELPER(vsetvl)(CPURISCVState *env, target_ulong s1,
           * VLEN * LMUL >= SEW
           * VLEN >> (8 - lmul) >= sew
           * (vlenb << 3) >> (8 - lmul) >= sew
-         * vlenb >> (8 - 3 - lmul) >= sew
           */
-        if (vlmul == 4 ||
-            cpu->cfg.vlenb >> (8 - 3 - vlmul) < sew) {
+        if (vlmul == 4 || (vlen >> (8 - vlmul)) < sew) {
              vill = true;
          }
      }
#2 lore raw
In reply to demin.han
Quoted 1 line · demin.han

The result of (8 - 3 - vlmul) is negtive when vlmul >= 6,

Typo "negative".

Quoted 31 lines

and results in wrong vill.

---
  target/riscv/vector_helper.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
target/riscv/vector_helper.c +2 -3
index 84cec73eb2..fe56c007d5 100644
@@ -44,6 +44,7 @@ target_ulong HELPER(vsetvl)(CPURISCVState *env, target_ulong s1,
      target_ulong reserved = s2 &
                              MAKE_64BIT_MASK(R_VTYPE_RESERVED_SHIFT,
                                              xlen - 1 - R_VTYPE_RESERVED_SHIFT);
+    uint16_t vlen = cpu->cfg.vlenb << 3;
      int8_t lmul;
 
      if (vlmul & 4) {
@@ -53,10 +54,8 @@ target_ulong HELPER(vsetvl)(CPURISCVState *env, target_ulong s1,
           * VLEN * LMUL >= SEW
           * VLEN >> (8 - lmul) >= sew
           * (vlenb << 3) >> (8 - lmul) >= sew
-         * vlenb >> (8 - 3 - lmul) >= sew
           */
-        if (vlmul == 4 ||
-            cpu->cfg.vlenb >> (8 - 3 - vlmul) < sew) {
+        if (vlmul == 4 || (vlen >> (8 - vlmul)) < sew) {
              vill = true;
          }
      }