target/sh4: Fix ADDV/SUBV opcodes
v4 cover target/sh4: Fix ADDV/SUBV opcodes
Pass 'build-legacy' CI job which runs check-tcg-sh4.
Since v3:
- Fixed copy/paste mistake (Paul)
- Tests print values on failure
- Added rth R-b tags
Since v2:
- Add tests (Paul)
- Rename TCGv variables as in manual
Philippe Mathieu-Daudé (4):
target/sh4: Fix ADDV opcode
target/sh4: Fix SUBV opcode
target/sh4: Rename TCGv variables as manual for ADDV opcode target/sh4: Rename TCGv variables as manual for SUBV opcode
target/sh4/translate.c | 32 ++++++++++++++++++++------------ tests/tcg/sh4/test-addv.c | 27 +++++++++++++++++++++++++++ tests/tcg/sh4/test-subv.c | 30 ++++++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 6 ++++++ 4 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 tests/tcg/sh4/test-addv.c create mode 100644 tests/tcg/sh4/test-subv.c
Signature · 1 line
2.41.0
PATCH v4 1/4 target/sh4: Fix ADDV opcode
The documentation says:
ADDV Rm, Rn Rn + Rm -> Rn, overflow -> T
But QEMU implementation was:
ADDV Rm, Rn Rn + Rm -> Rm, overflow -> T
Fix by filling the correct Rm register.
Add tests provided by Paul Cercueil.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2317
--- target/sh4/translate.c | 2 +- tests/tcg/sh4/test-addv.c | 27 +++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 3 +++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/sh4/test-addv.c
target/sh4/translate.c +1 -1
@@ -714,7 +714,7 @@ static void _decode_opc(DisasContext * ctx)tcg_gen_xor_i32 (t2,REG (B7_4 ),REG (B11_8 ));tcg_gen_andc_i32 (cpu_sr_t, t1, t2);tcg_gen_shri_i32 (cpu_sr_t, cpu_sr_t,31 ); -tcg_gen_mov_i32 (REG (B7_4 ), t0); +tcg_gen_mov_i32 (REG (B11_8 ), t0); }return ;case 0x2009 :/* and Rm,Rn */
tests/tcg/sh4/test-addv.c new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> + +static void addv (const int a,const int b,const int res,const int carry) +{ +int o = a, c; + + asmvolatile ("addv %2,%0\n" +"movt %1\n" + :"+r" (o),"=r" (c) :"r" (b) :); + +if (c != carry || aw != res) { +printf ("ADDV %d, %d = %d/%d [T = %d/%d]\n" , a, b, o, res, c, carry); +abort (); + } +} + +int main (void ) +{ +addv (INT_MAX ,1 ,INT_MIN ,1 ); +addv (INT_MAX -1 ,1 ,INT_MAX ,0 ); + +return 0 ; +}
tests/tcg/sh4/Makefile.target +3 -0
@@ -17,3 +17,6 @@ TESTS += test-macl test-macw: CFLAGS += -O -g TESTS += test-macw + +test-addv: CFLAGS += -O -g +TESTS += test-addv
Signature · 1 line
2.41.0
Re: [PATCH v4 1/4] target/sh4: Fix ADDV opcode
Quoted 86 lines · Philippe Mathieu-Daudé
The documentation says:
ADDV Rm, Rn Rn + Rm -> Rn, overflow -> T
But QEMU implementation was:
ADDV Rm, Rn Rn + Rm -> Rm, overflow -> T
Fix by filling the correct Rm register.
Add tests provided by Paul Cercueil.
Fixes ad8d25a11f ("target-sh4: implement addv and subv using TCG")Reported-by Paul Cercueil <[email protected]>Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2317
Reviewed-by Richard Henderson <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/sh4/translate.c | 2 +- tests/tcg/sh4/test-addv.c | 27 +++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 3 +++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/sh4/test-addv.ctarget/sh4/translate.c +1
-1@@ -714,7 +714,7 @@ static void _decode_opc(DisasContext * ctx)tcg_gen_xor_i32 (t2,REG (B7_4 ),REG (B11_8 ));tcg_gen_andc_i32 (cpu_sr_t, t1, t2);tcg_gen_shri_i32 (cpu_sr_t, cpu_sr_t,31 ); -tcg_gen_mov_i32 (REG (B7_4 ), t0); +tcg_gen_mov_i32 (REG (B11_8 ), t0); }return ;case 0x2009 :/* and Rm,Rn */ tests/tcg/sh4/test-addv.c new file +27
-0@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> + +static void addv (const int a,const int b,const int res,const int carry) +{ +int o = a, c; + + asmvolatile ("addv %2,%0\n" +"movt %1\n" + :"+r" (o),"=r" (c) :"r" (b) :); + +if (c != carry || aw != res) { +printf ("ADDV %d, %d = %d/%d [T = %d/%d]\n" , a, b, o, res, c, carry); +abort (); + } +} + +int main (void ) +{ +addv (INT_MAX ,1 ,INT_MIN ,1 ); +addv (INT_MAX -1 ,1 ,INT_MAX ,0 ); + +return 0 ; +}tests/tcg/sh4/Makefile.target +3
-0@@ -17,3 +17,6 @@ TESTS += test-macl test-macw: CFLAGS += -O -g TESTS += test-macw + +test-addv: CFLAGS += -O -g +TESTS += test-addvSignature · 1 line
2.41.0
Signature · 1 line
Yosinori Sato
PATCH v4 2/4 target/sh4: Fix SUBV opcode
In reply to Philippe Mathieu-DaudéThe documentation says:
SUBV Rm, Rn Rn - Rm -> Rn, underflow -> T
The overflow / underflow can be calculated as:
T = ((Rn ^ Rm) & (Result ^ Rn)) >> 31
However we were using the incorrect:
T = ((Rn ^ Rm) & (Result ^ Rm)) >> 31
Fix by using the Rn register instead of Rm.
Add tests provided by Paul Cercueil.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2318
--- target/sh4/translate.c | 2 +- tests/tcg/sh4/test-subv.c | 30 ++++++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 3 +++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/sh4/test-subv.c
target/sh4/translate.c +1 -1
@@ -933,7 +933,7 @@ static void _decode_opc(DisasContext * ctx) t0 =tcg_temp_new ();tcg_gen_sub_i32 (t0,REG (B11_8 ),REG (B7_4 )); t1 =tcg_temp_new (); -tcg_gen_xor_i32 (t1, t0,REG (B7_4 )); +tcg_gen_xor_i32 (t1, t0,REG (B11_8 )); t2 =tcg_temp_new ();tcg_gen_xor_i32 (t2,REG (B11_8 ),REG (B7_4 ));tcg_gen_and_i32 (t1, t1, t2);
tests/tcg/sh4/test-subv.c new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> + +static void subv (const int a,const int b,const int res,const int carry) +{ +int o = a, c; + + asmvolatile ("subv %2,%0\n" +"movt %1\n" + :"+r" (o),"=r" (c) :"r" (b) :); + +if (c != carry || o != res) { +printf ("SUBV %d, %d = %d/%d [T = %d/%d]\n" , a, b, o, res, c, carry); +abort (); + } +} + +int main (void ) +{ +subv (INT_MIN ,1 ,INT_MAX ,1 ); +subv (INT_MAX ,-1 ,INT_MIN ,1 ); +subv (INT_MAX ,1 ,INT_MAX -1 ,0 ); +subv (0 ,1 ,-1 ,0 ); +subv (-1 ,-1 ,0 ,0 ); + +return 0 ; +}
tests/tcg/sh4/Makefile.target +3 -0
@@ -20,3 +20,6 @@ TESTS += test-macw test-addv: CFLAGS += -O -g TESTS += test-addv + +test-subv: CFLAGS += -O -g +TESTS += test-subv
Signature · 1 line
2.41.0
Re: [PATCH v4 2/4] target/sh4: Fix SUBV opcode
Quoted 94 lines · Philippe Mathieu-Daudé
The documentation says:
SUBV Rm, Rn Rn - Rm -> Rn, underflow -> T
The overflow / underflow can be calculated as:
T = ((Rn ^ Rm) & (Result ^ Rn)) >> 31
However we were using the incorrect:
T = ((Rn ^ Rm) & (Result ^ Rm)) >> 31
Fix by using the Rn register instead of Rm.
Add tests provided by Paul Cercueil.
Fixes ad8d25a11f ("target-sh4: implement addv and subv using TCG")Reported-by Paul Cercueil <[email protected]>Suggested-by Paul Cercueil <[email protected]>Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2318
Reviewed-by Richard Henderson <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/sh4/translate.c | 2 +- tests/tcg/sh4/test-subv.c | 30 ++++++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 3 +++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/sh4/test-subv.ctarget/sh4/translate.c +1
-1@@ -933,7 +933,7 @@ static void _decode_opc(DisasContext * ctx) t0 =tcg_temp_new ();tcg_gen_sub_i32 (t0,REG (B11_8 ),REG (B7_4 )); t1 =tcg_temp_new (); -tcg_gen_xor_i32 (t1, t0,REG (B7_4 )); +tcg_gen_xor_i32 (t1, t0,REG (B11_8 )); t2 =tcg_temp_new ();tcg_gen_xor_i32 (t2,REG (B11_8 ),REG (B7_4 ));tcg_gen_and_i32 (t1, t1, t2);tests/tcg/sh4/test-subv.c new file +30
-0@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> + +static void subv (const int a,const int b,const int res,const int carry) +{ +int o = a, c; + + asmvolatile ("subv %2,%0\n" +"movt %1\n" + :"+r" (o),"=r" (c) :"r" (b) :); + +if (c != carry || o != res) { +printf ("SUBV %d, %d = %d/%d [T = %d/%d]\n" , a, b, o, res, c, carry); +abort (); + } +} + +int main (void ) +{ +subv (INT_MIN ,1 ,INT_MAX ,1 ); +subv (INT_MAX ,-1 ,INT_MIN ,1 ); +subv (INT_MAX ,1 ,INT_MAX -1 ,0 ); +subv (0 ,1 ,-1 ,0 ); +subv (-1 ,-1 ,0 ,0 ); + +return 0 ; +}tests/tcg/sh4/Makefile.target +3
-0@@ -20,3 +20,6 @@ TESTS += test-macw test-addv: CFLAGS += -O -g TESTS += test-addv + +test-subv: CFLAGS += -O -g +TESTS += test-subvSignature · 1 line
2.41.0
Signature · 1 line
Yosinori Sato
PATCH v4 3/4 target/sh4: Rename TCGv variables as manual for ADDV opcode
In reply to Philippe Mathieu-DaudéTo easily compare with the SH4 manual, rename:
REG(B11_8) -> Rn
REG(B7_4) -> Rm
t0 -> result
Mention how overflow is calculated.
--- target/sh4/translate.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
target/sh4/translate.c +10 -6
@@ -705,16 +705,20 @@ static void _decode_opc(DisasContext * ctx)return ;case 0x300f :/* addv Rm,Rn */ { -TCGv t0, t1, t2; - t0 =tcg_temp_new (); -tcg_gen_add_i32 (t0,REG (B7_4 ),REG (B11_8 )); +TCGv Rn =REG (B11_8 ); +TCGv Rm =REG (B7_4 ); +TCGv result, t1, t2; + + result =tcg_temp_new (); t1 =tcg_temp_new (); -tcg_gen_xor_i32 (t1, t0,REG (B11_8 )); t2 =tcg_temp_new (); -tcg_gen_xor_i32 (t2,REG (B7_4 ),REG (B11_8 )); +tcg_gen_add_i32 (result, Rm, Rn); +/* T = ((Rn ^ Rm) & (Result ^ Rn)) >> 31 */ +tcg_gen_xor_i32 (t1, result, Rn); +tcg_gen_xor_i32 (t2, Rm, Rn);tcg_gen_andc_i32 (cpu_sr_t, t1, t2);tcg_gen_shri_i32 (cpu_sr_t, cpu_sr_t,31 ); -tcg_gen_mov_i32 (REG (B11_8 ), t0); +tcg_gen_mov_i32 (Rn, result); }return ;case 0x2009 :/* and Rm,Rn */
Signature · 1 line
2.41.0
Re: [PATCH v4 3/4] target/sh4: Rename TCGv variables as manual for ADDV opcode
Quoted 49 lines · Philippe Mathieu-Daudé
To easily compare with the SH4 manual, rename:
REG(B11_8)-> Rn
REG(B7_4)-> Rm
t0 -> resultMention how overflow is calculated.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Richard Henderson <[email protected]>--- target/sh4/translate.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)target/sh4/translate.c +10
-6@@ -705,16 +705,20 @@ static void _decode_opc(DisasContext * ctx)return ;case 0x300f :/* addv Rm,Rn */ { -TCGv t0, t1, t2; - t0 =tcg_temp_new (); -tcg_gen_add_i32 (t0,REG (B7_4 ),REG (B11_8 )); +TCGv Rn =REG (B11_8 ); +TCGv Rm =REG (B7_4 ); +TCGv result, t1, t2; + + result =tcg_temp_new (); t1 =tcg_temp_new (); -tcg_gen_xor_i32 (t1, t0,REG (B11_8 )); t2 =tcg_temp_new (); -tcg_gen_xor_i32 (t2,REG (B7_4 ),REG (B11_8 )); +tcg_gen_add_i32 (result, Rm, Rn); +/* T = ((Rn ^ Rm) & (Result ^ Rn)) >> 31 */ +tcg_gen_xor_i32 (t1, result, Rn); +tcg_gen_xor_i32 (t2, Rm, Rn);tcg_gen_andc_i32 (cpu_sr_t, t1, t2);tcg_gen_shri_i32 (cpu_sr_t, cpu_sr_t,31 ); -tcg_gen_mov_i32 (REG (B11_8 ), t0); +tcg_gen_mov_i32 (Rn, result); }return ;case 0x2009 :/* and Rm,Rn */ Signature · 1 line
2.41.0
Signature · 1 line
Yosinori Sato
PATCH v4 4/4 target/sh4: Rename TCGv variables as manual for SUBV opcode
In reply to Philippe Mathieu-DaudéTo easily compare with the SH4 manual, rename:
REG(B11_8) -> Rn
REG(B7_4) -> Rm
t0 -> result
Mention how underflow is calculated.
--- target/sh4/translate.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
target/sh4/translate.c +10 -6
@@ -933,16 +933,20 @@ static void _decode_opc(DisasContext * ctx)return ;case 0x300b :/* subv Rm,Rn */ { -TCGv t0, t1, t2; - t0 =tcg_temp_new (); -tcg_gen_sub_i32 (t0,REG (B11_8 ),REG (B7_4 )); +TCGv Rn =REG (B11_8 ); +TCGv Rm =REG (B7_4 ); +TCGv result, t1, t2; + + result =tcg_temp_new (); t1 =tcg_temp_new (); -tcg_gen_xor_i32 (t1, t0,REG (B11_8 )); t2 =tcg_temp_new (); -tcg_gen_xor_i32 (t2,REG (B11_8 ),REG (B7_4 )); +tcg_gen_sub_i32 (result, Rn, Rm); +/* T = ((Rn ^ Rm) & (Result ^ Rn)) >> 31 */ +tcg_gen_xor_i32 (t1, result, Rn); +tcg_gen_xor_i32 (t2, Rn, Rm);tcg_gen_and_i32 (t1, t1, t2);tcg_gen_shri_i32 (cpu_sr_t, t1,31 ); -tcg_gen_mov_i32 (REG (B11_8 ), t0); +tcg_gen_mov_i32 (Rn, result); }return ;case 0x2008 :/* tst Rm,Rn */
Signature · 1 line
2.41.0
Re: [PATCH v4 4/4] target/sh4: Rename TCGv variables as manual for SUBV opcode
Quoted 49 lines · Philippe Mathieu-Daudé
To easily compare with the SH4 manual, rename:
REG(B11_8)-> Rn
REG(B7_4)-> Rm
t0 -> resultMention how underflow is calculated.
Reviewed-by Richard Henderson <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/sh4/translate.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)target/sh4/translate.c +10
-6@@ -933,16 +933,20 @@ static void _decode_opc(DisasContext * ctx)return ;case 0x300b :/* subv Rm,Rn */ { -TCGv t0, t1, t2; - t0 =tcg_temp_new (); -tcg_gen_sub_i32 (t0,REG (B11_8 ),REG (B7_4 )); +TCGv Rn =REG (B11_8 ); +TCGv Rm =REG (B7_4 ); +TCGv result, t1, t2; + + result =tcg_temp_new (); t1 =tcg_temp_new (); -tcg_gen_xor_i32 (t1, t0,REG (B11_8 )); t2 =tcg_temp_new (); -tcg_gen_xor_i32 (t2,REG (B11_8 ),REG (B7_4 )); +tcg_gen_sub_i32 (result, Rn, Rm); +/* T = ((Rn ^ Rm) & (Result ^ Rn)) >> 31 */ +tcg_gen_xor_i32 (t1, result, Rn); +tcg_gen_xor_i32 (t2, Rn, Rm);tcg_gen_and_i32 (t1, t1, t2);tcg_gen_shri_i32 (cpu_sr_t, t1,31 ); -tcg_gen_mov_i32 (REG (B11_8 ), t0); +tcg_gen_mov_i32 (Rn, result); }return ;case 0x2008 :/* tst Rm,Rn */ Signature · 1 line
2.41.0
Signature · 1 line
Yosinori Sato
Quoted 5 lines · Philippe Mathieu-Daudé
Philippe Mathieu-Daudé (4):
target/sh4: Fix ADDV opcode
target/sh4: Fix SUBV opcode
target/sh4: Rename TCGv variables as manual for ADDV opcode target/sh4: Rename TCGv variables as manual for SUBV opcode
Series queued, thanks.