target/sh4: Fix ADDV/SUBV opcodes
v3 cover target/sh4: Fix ADDV/SUBV opcodes
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 | 23 +++++++++++++++++++++++ tests/tcg/sh4/test-subv.c | 26 ++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 6 ++++++ 4 files changed, 75 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 v3 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 | 23 +++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 3 +++ 3 files changed, 27 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 +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <assert.h> +#include <limits.h> + +static void addv (int a,int b,int res,int carry) +{ +unsigned int c; + + asmvolatile ("addv %2,%0\n" +"movt %1\n" + :"+r" (a),"=r" (c) :"r" (b) :); + +assert (c == carry && a == res); +} + +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
PATCH v3 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 | 26 ++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 3 +++ 3 files changed, 30 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 +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <assert.h> +#include <limits.h> + +static void subv (int a,int b,int res,int carry) +{ +unsigned int c; + + asmvolatile ("subv %2,%0\n" +"movt %1\n" + :"+r" (a),"=r" (c) :"r" (b) :); + +assert (c == carry && a == res); +} + +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
PATCH v3 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
PATCH v3 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_add_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 (Rm, result); }return ;case 0x2008 :/* tst Rm,Rn */
Signature · 1 line
2.41.0
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
r~