Skip to contentSkip to conversation

target/riscv: fix vssub.vv saturation bug

PATCH In review [email protected] 3 participants 4 messages Apr 19, 2021 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: fix vssub.vv saturation bug

From: Frank Chang <[email protected]>

Doing a negate (0x0 – 0x80000000) using vssub.vv produces an incorrect result of 0x80000000 (should saturate to 0x7fffffff)

Fix this bug by treating zero as a positive number.

---
 target/riscv/vector_helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/riscv/vector_helper.c +4 -4
index a156573d281..356cef8a090 100644
@@ -2451,7 +2451,7 @@ static inline int8_t ssub8(CPURISCVState *env, int vxrm, int8_t a, int8_t b)
 {
     int8_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT8_MIN) {
-        res = a > 0 ? INT8_MAX : INT8_MIN;
+        res = a >= 0 ? INT8_MAX : INT8_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2461,7 +2461,7 @@ static inline int16_t ssub16(CPURISCVState *env, int vxrm, int16_t a, int16_t b)
 {
     int16_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT16_MIN) {
-        res = a > 0 ? INT16_MAX : INT16_MIN;
+        res = a >= 0 ? INT16_MAX : INT16_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2471,7 +2471,7 @@ static inline int32_t ssub32(CPURISCVState *env, int vxrm, int32_t a, int32_t b)
 {
     int32_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT32_MIN) {
-        res = a > 0 ? INT32_MAX : INT32_MIN;
+        res = a >= 0 ? INT32_MAX : INT32_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2481,7 +2481,7 @@ static inline int64_t ssub64(CPURISCVState *env, int vxrm, int64_t a, int64_t b)
 {
     int64_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT64_MIN) {
-        res = a > 0 ? INT64_MAX : INT64_MIN;
+        res = a >= 0 ? INT64_MAX : INT64_MIN;
         env->vxsat = 0x1;
     }
     return res;
Signature · 1 line
2.17.1
Reviewed-by Richard Henderson Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · On Mon, Apr 19, 2021 at 4:02 PM <[email protected]> wrote:

From: Frank Chang <[email protected]>

Doing a negate (0x0 – 0x80000000) using vssub.vv produces an incorrect result of 0x80000000 (should saturate to 0x7fffffff)

Fix this bug by treating zero as a positive number.

Thanks for the patch!

A similar fix is already in the riscv-to-apply.next queue

target/riscv: Fixup saturate subtract function

The overflow predication ((a - b) ^ a) & (a ^ b) & INT64_MIN is right. However, when the predication is ture and a is 0, it should return maximum.

Message-id: [email protected]
Message-Id: <[email protected]>

Alistair

Quoted 48 lines
---
 target/riscv/vector_helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/riscv/vector_helper.c +4 -5
index a156573d281..356cef8a090 100644
@@ -2451,7 +2451,7 @@ static inline int8_t ssub8(CPURISCVState *env, int vxrm, int8_t a, int8_t b)
 {
     int8_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT8_MIN) {
-        res = a > 0 ? INT8_MAX : INT8_MIN;
+        res = a >= 0 ? INT8_MAX : INT8_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2461,7 +2461,7 @@ static inline int16_t ssub16(CPURISCVState *env, int vxrm, int16_t a, int16_t b)
 {
     int16_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT16_MIN) {
-        res = a > 0 ? INT16_MAX : INT16_MIN;
+        res = a >= 0 ? INT16_MAX : INT16_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2471,7 +2471,7 @@ static inline int32_t ssub32(CPURISCVState *env, int vxrm, int32_t a, int32_t b)
 {
     int32_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT32_MIN) {
-        res = a > 0 ? INT32_MAX : INT32_MIN;
+        res = a >= 0 ? INT32_MAX : INT32_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2481,7 +2481,7 @@ static inline int64_t ssub64(CPURISCVState *env, int vxrm, int64_t a, int64_t b)
 {
     int64_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT64_MIN) {
-        res = a > 0 ? INT64_MAX : INT64_MIN;
+        res = a >= 0 ? INT64_MAX : INT64_MIN;
         env->vxsat = 0x1;
     }
     return res;
--

2.17.1

#2 lore raw

Alistair Francis <[email protected]> 於 2021年4月19日 週一 下午2:28寫道:

Quoted 21 lines · On Mon, Apr 19, 2021 at 4:02 PM <[email protected]> wrote:
Quoted 9 lines

From: Frank Chang <[email protected]>

Doing a negate (0x0 – 0x80000000) using vssub.vv produces an incorrect result of 0x80000000 (should saturate to 0x7fffffff)

Fix this bug by treating zero as a positive number.

Thanks for the patch!

A similar fix is already in the riscv-to-apply.next queue

target/riscv: Fixup saturate subtract function

The overflow predication ((a - b) ^ a) & (a ^ b) & INT64_MIN is right. However, when the predication is ture and a is 0, it should return
maximum.

Quoted 9 lines

Message-id: [email protected]
Message-Id: <[email protected]>

Alistair

Thanks, I might missed that patch.
Frank Chang

Quoted 54 lines

target/riscv/vector_helper.c static inline int8_t ssub8(CPURISCVState *env, int

Quoted 10 lines
---
 target/riscv/vector_helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/riscv/vector_helper.c +0 -0
index a156573d281..356cef8a090 100644
@@ -2451,7 +2451,7 @@ static inline int8_t ssub8(CPURISCVState *env, int

vxrm, int8_t a, int8_t b)

target/riscv/vector_helper.c static inline int16_t ssub16(CPURISCVState *env,

Quoted 9 lines
target/riscv/vector_helper.c +1 -1
 {
     int8_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT8_MIN) {
-        res = a > 0 ? INT8_MAX : INT8_MIN;
+        res = a >= 0 ? INT8_MAX : INT8_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2461,7 +2461,7 @@ static inline int16_t ssub16(CPURISCVState *env,

int vxrm, int16_t a, int16_t b)

target/riscv/vector_helper.c static inline int32_t ssub32(CPURISCVState *env,

Quoted 9 lines
target/riscv/vector_helper.c +1 -1
 {
     int16_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT16_MIN) {
-        res = a > 0 ? INT16_MAX : INT16_MIN;
+        res = a >= 0 ? INT16_MAX : INT16_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2471,7 +2471,7 @@ static inline int32_t ssub32(CPURISCVState *env,

int vxrm, int32_t a, int32_t b)

target/riscv/vector_helper.c static inline int64_t ssub64(CPURISCVState *env,

Quoted 9 lines
target/riscv/vector_helper.c +1 -1
 {
     int32_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT32_MIN) {
-        res = a > 0 ? INT32_MAX : INT32_MIN;
+        res = a >= 0 ? INT32_MAX : INT32_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2481,7 +2481,7 @@ static inline int64_t ssub64(CPURISCVState *env,

int vxrm, int64_t a, int64_t b)

Quoted 12 lines
target/riscv/vector_helper.c +1 -2
 {
     int64_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT64_MIN) {
-        res = a > 0 ? INT64_MAX : INT64_MIN;
+        res = a >= 0 ? INT64_MAX : INT64_MIN;
         env->vxsat = 0x1;
     }
     return res;
--

2.17.1

#3 lore raw
Quoted 30 lines · Frank Chang

Alistair Francis <[email protected]> 於 2021年4月19日 週一 下午2:28寫道:

Quoted 28 lines · On Mon, Apr 19, 2021 at 4:02 PM <[email protected]> wrote:
Quoted 9 lines

From: Frank Chang <[email protected]>

Doing a negate (0x0 – 0x80000000) using vssub.vv produces an incorrect result of 0x80000000 (should saturate to 0x7fffffff)

Fix this bug by treating zero as a positive number.

Thanks for the patch!

A similar fix is already in the riscv-to-apply.next queue

target/riscv: Fixup saturate subtract function

The overflow predication ((a - b) ^ a) & (a ^ b) & INT64_MIN is right. However, when the predication is ture and a is 0, it should return maximum.

Message-id: [email protected]
Message-Id: <[email protected]>

Quoted 4 lines
Quoted 1 line

Alistair

Thanks, I might missed that patch.

No worries. Thanks anyway for the fix.

Alistair

Quoted 52 lines

Frank Chang

Quoted 50 lines
Quoted 50 lines

---
 target/riscv/vector_helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/riscv/vector_helper.c +4 -5
index a156573d281..356cef8a090 100644
@@ -2451,7 +2451,7 @@ static inline int8_t ssub8(CPURISCVState *env, int vxrm, int8_t a, int8_t b)
 {
     int8_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT8_MIN) {
-        res = a > 0 ? INT8_MAX : INT8_MIN;
+        res = a >= 0 ? INT8_MAX : INT8_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2461,7 +2461,7 @@ static inline int16_t ssub16(CPURISCVState *env, int vxrm, int16_t a, int16_t b)
 {
     int16_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT16_MIN) {
-        res = a > 0 ? INT16_MAX : INT16_MIN;
+        res = a >= 0 ? INT16_MAX : INT16_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2471,7 +2471,7 @@ static inline int32_t ssub32(CPURISCVState *env, int vxrm, int32_t a, int32_t b)
 {
     int32_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT32_MIN) {
-        res = a > 0 ? INT32_MAX : INT32_MIN;
+        res = a >= 0 ? INT32_MAX : INT32_MIN;
         env->vxsat = 0x1;
     }
     return res;
@@ -2481,7 +2481,7 @@ static inline int64_t ssub64(CPURISCVState *env, int vxrm, int64_t a, int64_t b)
 {
     int64_t res = a - b;
     if ((res ^ a) & (a ^ b) & INT64_MIN) {
-        res = a > 0 ? INT64_MAX : INT64_MIN;
+        res = a >= 0 ? INT64_MAX : INT64_MIN;
         env->vxsat = 0x1;
     }
     return res;
--

2.17.1