sparc smul problem
Hello!
I've been stuck on this all weekend, as I can't find where the actual problem is. I might be completely missing it, but I've tried a lot of things and can't make it work.
On SPARC, the "smul" instruction multiplies two numbers, puts the result in the result register but also puts the top 32-bits of the 64-bit result into the "Y" register.
As the attached code shows, the value of "Y" is wrong. Somehow after the multiply, the top 32 bits of the product rae all zeros. I've played around with the code generated by translate.c, and it looks like the shift and other instructions all work properly, but the 64-bit multiply the result is somehow being truncated. But I've looked at the generated code (on x86_64) and it looks like it is doing the right thing.
So anyway, I'll have to take a look at this again later, but in case anyone else wants to look at it in case I am missing anything obvious.
Thanks
Vince
! as -o smul.o smul.s ; ld -o smul smul.o
.equ SYSCALL_EXIT,1
.equ SYSCALL_WRITE,4
.equ STDOUT,1
.globl _start_start:
set 0xc22e4507,%g1 set 0x386d4380,%g4 before: smul %g4,%g1,%g2 rd %y,%g3
stop:
! g2 should be (f25fbc80) 1f2e5880
! g3 should be (00000000) f25fbc80
set 0x1f2e5880,%g5
cmp %g2,%g5
bne wrong_lower
nop
set correct_answer,%o1
ba print_lower
nopwrong_lower:
set wrong_answer,%o1
print_lower:
call write_stdout
nop
! Check to see if Y was correct
set 0xf25fbc80,%g5
cmp %g3,%g5
bne print_wrong_y
nop
set correct_y,%o1
ba print_y
nopprint_wrong_y:
set wrong_y,%o1
print_y:
call write_stdout
nop
!================================
! Exit
!================================
exit:
mov 0,%o0 ! exit value
mov SYSCALL_EXIT,%g1 ! put the exit syscall number in g1
ta 0x10 ! and exit
#================================
# WRITE_STDOUT
#================================
# %o1 has stringwrite_stdout:
set SYSCALL_WRITE,%g1 ! Write syscall in %g1 set STDOUT,%o0 ! 1 in %o0 (stdout) set 0,%o2 ! 0 (count) in %o2
str_loop1:
ldub [%o1+%o2],%l1 ! load byte cmp %l1,%g0 ! compare against zero bnz str_loop1 ! if not nul, repeat # BRANCH DELAY SLOT inc %o2 ! increment count dec %o2 ! correct count ta 0x10 ! run the syscall retl nop
!=========================================================================== .data !===========================================================================
data_region:wrong_answer: .ascii "Wrong answer\n\0"wrong_y: .ascii "Wrong Y\n\0"correct_answer: .ascii "Correct answer\n\0"correct_y: .ascii "Correct Y\n\0"
Quoted 17 lines · Vince Weaver
Hello!
I've been stuck on this all weekend, as I can't find where the actual
problem is. I might be completely missing it, but I've tried a lot of things and can't make it work.On SPARC, the "smul" instruction multiplies two numbers, puts the result in
the result register but also puts the top 32-bits of the 64-bit result into the "Y" register.As the attached code shows, the value of "Y" is wrong. Somehow after the
multiply, the top 32 bits of the product rae all zeros. I've played around with the code generated bytranslate.c, and it looks like the shift and other instructions all work properly, but the 64-bit multiply the result is somehow being truncated. But I've looked at the generated code (onx86_64) and it looks like it is doing the right thing.
Quoted 2 lines
So anyway, I'll have to take a look at this again later, but in case anyone
else wants to look at it in case I am missing anything obvious.
At least the store and load do not match:
---- 0x10084
mov_i64 tmp16,g1
mov_i64 tmp17,g4
mul_i64 tmp17,tmp16,tmp17
movi_i64 tmp18,$0x20
shr_i64 tmp16,tmp17,tmp18
st_i32 tmp16,env,$0x58
^^^^^
mov_i64 loc3,tmp17
mov_i64 g2,loc3
---- 0x10088
ld_i64 tmp0,env,$0x58
^^^^^mov_i64 g3,tmp0
Quoted 17 lines · Vince Weaver
Hello!
I've been stuck on this all weekend, as I can't find where the actual
problem is. I might be completely missing it, but I've tried a lot of things and can't make it work.On SPARC, the "smul" instruction multiplies two numbers, puts the result in
the result register but also puts the top 32-bits of the 64-bit result into the "Y" register.As the attached code shows, the value of "Y" is wrong. Somehow after the
multiply, the top 32 bits of the product rae all zeros. I've played around with the code generated bytranslate.c, and it looks like the shift and other instructions all work properly, but the 64-bit multiply the result is somehow being truncated. But I've looked at the generated code (onx86_64) and it looks like it is doing the right thing.
Quoted 2 lines
So anyway, I'll have to take a look at this again later, but in case anyone
else wants to look at it in case I am missing anything obvious.
Y loads and stores were not correct, the attached patch fixes the problem. I'm still running some tests.
target-sparc/translate.c +12 -18
@@ -713,36 +713,32 @@static inline void gen_op_mulscc (TCGv dst,TCGv src1,TCGv src2) { -TCGv r_temp, r_temp2; +TCGv r_temp;int l1; l1 =gen_new_label (); r_temp =tcg_temp_new (TCG_TYPE_TL ); - r_temp2 =tcg_temp_new (TCG_TYPE_I32 );/* old op: if (!(env->y & 1)) T1 = 0; */ tcg_gen_mov_tl (cpu_cc_src, src1); -tcg_gen_ld32u_tl (r_temp, cpu_env, offsetof(CPUSPARCState , y)); -tcg_gen_trunc_tl_i32 (r_temp2, r_temp); -tcg_gen_andi_i32 (r_temp2, r_temp2,0x1 ); +tcg_gen_ld_tl (r_temp, cpu_env, offsetof(CPUSPARCState , y)); +tcg_gen_andi_tl (r_temp, r_temp,0x1 );tcg_gen_mov_tl (cpu_cc_src2, src2); -tcg_gen_brcondi_i32 (TCG_COND_NE , r_temp2,0 , l1); +tcg_gen_brcondi_tl (TCG_COND_NE , r_temp,0 , l1);tcg_gen_movi_tl (cpu_cc_src2,0 );gen_set_label (l1);// b2 = T0 & 1; // env->y = (b2 << 31) | (env->y >> 1); -tcg_gen_trunc_tl_i32 (r_temp2, cpu_cc_src); -tcg_gen_andi_i32 (r_temp2, r_temp2,0x1 ); -tcg_gen_shli_i32 (r_temp2, r_temp2,31 ); -tcg_gen_ld_i32 (cpu_tmp32, cpu_env, offsetof(CPUSPARCState , y)); -tcg_gen_shri_i32 (cpu_tmp32, cpu_tmp32,1 ); -tcg_gen_or_i32 (cpu_tmp32, cpu_tmp32, r_temp2); -tcg_temp_free (r_temp2); -tcg_gen_st_i32 (cpu_tmp32, cpu_env, offsetof(CPUSPARCState , y)); +tcg_gen_andi_tl (r_temp, cpu_cc_src,0x1 ); +tcg_gen_shli_tl (r_temp, r_temp,31 ); +tcg_gen_ld_tl (cpu_tmp0, cpu_env, offsetof(CPUSPARCState , y)); +tcg_gen_shri_tl (cpu_tmp0, cpu_tmp0,1 ); +tcg_gen_or_tl (cpu_tmp0, cpu_tmp0, r_temp); +tcg_gen_st_tl (cpu_tmp0, cpu_env, offsetof(CPUSPARCState , y));// b1 = N ^ V; gen_mov_reg_N (cpu_tmp0, cpu_psr);
@@ -778,8 +774,7 @@tcg_gen_mul_i64 (r_temp2, r_temp, r_temp2);tcg_gen_shri_i64 (r_temp, r_temp2,32 ); -tcg_gen_trunc_i64_i32 (r_temp, r_temp); -tcg_gen_st_i32 (r_temp, cpu_env, offsetof(CPUSPARCState , y)); +tcg_gen_st_tl (r_temp, cpu_env, offsetof(CPUSPARCState , y));tcg_temp_free (r_temp);#ifdef TARGET_SPARC64 tcg_gen_mov_i64 (dst, r_temp2);
@@ -801,8 +796,7 @@tcg_gen_mul_i64 (r_temp2, r_temp, r_temp2);tcg_gen_shri_i64 (r_temp, r_temp2,32 ); -tcg_gen_trunc_i64_i32 (r_temp, r_temp); -tcg_gen_st_i32 (r_temp, cpu_env, offsetof(CPUSPARCState , y)); +tcg_gen_st_tl (r_temp, cpu_env, offsetof(CPUSPARCState , y));tcg_temp_free (r_temp);#ifdef TARGET_SPARC64 tcg_gen_mov_i64 (dst, r_temp2);
Quoted 17 lines · Vince Weaver
Hello!
I've been stuck on this all weekend, as I can't find where the actual
problem is. I might be completely missing it, but I've tried a lot of things and can't make it work.On SPARC, the "smul" instruction multiplies two numbers, puts the result in
the result register but also puts the top 32-bits of the 64-bit result into the "Y" register.As the attached code shows, the value of "Y" is wrong. Somehow after the
multiply, the top 32 bits of the product rae all zeros. I've played around with the code generated bytranslate.c, and it looks like the shift and other instructions all work properly, but the 64-bit multiply the result is somehow being truncated. But I've looked at the generated code (onx86_64) and it looks like it is doing the right thing.
Quoted 2 lines
So anyway, I'll have to take a look at this again later, but in case anyone
else wants to look at it in case I am missing anything obvious.
It looks like TCG on i386 host generates incorrect code for mulu2_i32, I think the op should be imul (unsigned) instead of mul. This patch fixes the problem.
tcg/i386/tcg-target.c +1 -1
@@ -961,7 +961,7 @@ }break ;case INDEX_op_mulu2_i32: -tcg_out_modrm (s,0xf7 ,4 , args[3 ]); +tcg_out_modrm (s,0xf7 ,5 , args[3 ]);break ;case INDEX_op_div2_i32:tcg_out_modrm (s,0xf7 ,7 , args[4 ]);
Quoted 18 lines · Blue Swirl
Quoted 17 lines · Vince Weaver
Hello!
I've been stuck on this all weekend, as I can't find where the actual
problem is. I might be completely missing it, but I've tried a lot of things and can't make it work.On SPARC, the "smul" instruction multiplies two numbers, puts the result in
the result register but also puts the top 32-bits of the 64-bit result into the "Y" register.As the attached code shows, the value of "Y" is wrong. Somehow after the
multiply, the top 32 bits of the product rae all zeros. I've played around with the code generated bytranslate.c, and it looks like the shift and other instructions all work properly, but the 64-bit multiply the result is somehow being truncated. But I've looked at the generated code (onx86_64) and it looks like it is doing the right thing.
Quoted 6 lines
Quoted 2 lines
So anyway, I'll have to take a look at this again later, but in case anyone
else wants to look at it in case I am missing anything obvious.It looks like TCG on i386 host generates incorrect code for
mulu2_i32, I think the op should be imul (unsigned) instead of mul. This patch fixes the problem.
According to the IA-32 manual, mul (F7/4) corresponds to unsigned multiplication, while imul (F7/5) corresponds to signed multiplication. Therefore the patch does not looks correct.
Signature · 4 lines
.''`. Aurelien Jarno | GPG: 1024D/F1BCDB73 : :' : Debian developer | Electrical Engineer `. `' [email protected] | [email protected] `- people.debian.org/~aurel32 | www.aurel32.net
Quoted 19 lines · Blue Swirl
Quoted 17 lines · Vince Weaver
Hello!
I've been stuck on this all weekend, as I can't find where the actual
problem is. I might be completely missing it, but I've tried a lot of things and can't make it work.On SPARC, the "smul" instruction multiplies two numbers, puts the result in
the result register but also puts the top 32-bits of the 64-bit result into the "Y" register.As the attached code shows, the value of "Y" is wrong. Somehow after the
multiply, the top 32 bits of the product rae all zeros. I've played around with the code generated bytranslate.c, and it looks like the shift and other instructions all work properly, but the 64-bit multiply the result is somehow being truncated. But I've looked at the generated code (onx86_64) and it looks like it is doing the right thing.
Quoted 7 lines
Quoted 2 lines
So anyway, I'll have to take a look at this again later, but in case anyone
else wants to look at it in case I am missing anything obvious.It looks like TCG on i386 host generates incorrect code for
mulu2_i32,
I think the op should be imul (unsigned) instead of mul. This patch fixes the problem.
Never mind, I read the manuals wrong and the bug was elsewhere.