Skip to content

Commit b6a9f46

Browse files
petar-jovanovicaurel32
authored andcommitted
target-mips: fix mipsdsp_mul_q31_q31
Multiplication of two fractional word elements is not correct when sign extension/promotion is needed. This change fixes it by adding correct casts from unsigned to signed values. In addition, the tests (dpaq_sa_l_w.c and dpsq_sa_l_w.c) have been extended to trigger the current issue. Signed-off-by: Petar Jovanovic <[email protected]> Signed-off-by: Aurelien Jarno <[email protected]>
1 parent f05d4d9 commit b6a9f46

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

target-mips/dsp_helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ static inline int64_t mipsdsp_mul_q31_q31(int32_t ac, uint32_t a, uint32_t b,
583583
temp = (0x01ull << 63) - 1;
584584
set_DSPControl_overflow_flag(1, 16 + ac, env);
585585
} else {
586-
temp = ((uint64_t)a * (uint64_t)b) << 1;
586+
temp = ((int64_t)(int32_t)a * (int32_t)b) << 1;
587587
}
588588

589589
return temp;

tests/tcg/mips/mips32-dsp/dpaq_sa_l_w.c

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int main()
1414
resultdsp = 0x01;
1515
__asm
1616
("mthi %0, $ac1\n\t"
17-
"mtlo %0, $ac1\n\t"
17+
"mtlo %1, $ac1\n\t"
1818
"dpaq_sa.l.w $ac1, %3, %4\n\t"
1919
"mfhi %0, $ac1\n\t"
2020
"mflo %1, $ac1\n\t"
@@ -27,8 +27,8 @@ int main()
2727
assert(ach == resulth);
2828
assert(acl == resultl);
2929

30-
ach = 0x12;
31-
acl = 0x48;
30+
ach = 0x00000012;
31+
acl = 0x00000048;
3232
rs = 0x80000000;
3333
rt = 0x80000000;
3434

@@ -37,7 +37,7 @@ int main()
3737
resultdsp = 0x01;
3838
__asm
3939
("mthi %0, $ac1\n\t"
40-
"mtlo %0, $ac1\n\t"
40+
"mtlo %1, $ac1\n\t"
4141
"dpaq_sa.l.w $ac1, %3, %4\n\t"
4242
"mfhi %0, $ac1\n\t"
4343
"mflo %1, $ac1\n\t"
@@ -51,16 +51,64 @@ int main()
5151
assert(acl == resultl);
5252

5353
ach = 0x741532A0;
54-
acl = 0xfceabb08;
54+
acl = 0xFCEABB08;
5555
rs = 0x80000000;
5656
rt = 0x80000000;
5757

58-
resulth = 0x7fffffff;
59-
resultl = 0xffffffff;
58+
resulth = 0x7FFFFFFF;
59+
resultl = 0xFFFFFFFF;
6060
resultdsp = 0x01;
6161
__asm
6262
("mthi %0, $ac1\n\t"
63-
"mtlo %0, $ac1\n\t"
63+
"mtlo %1, $ac1\n\t"
64+
"dpaq_sa.l.w $ac1, %3, %4\n\t"
65+
"mfhi %0, $ac1\n\t"
66+
"mflo %1, $ac1\n\t"
67+
"rddsp %2\n\t"
68+
: "+r"(ach), "+r"(acl), "=r"(dsp)
69+
: "r"(rs), "r"(rt)
70+
);
71+
dsp = (dsp >> 17) & 0x01;
72+
assert(dsp == resultdsp);
73+
assert(ach == resulth);
74+
assert(acl == resultl);
75+
76+
ach = 0;
77+
acl = 0;
78+
rs = 0xC0000000;
79+
rt = 0x7FFFFFFF;
80+
81+
resulth = 0xC0000000;
82+
resultl = 0x80000000;
83+
resultdsp = 0;
84+
__asm
85+
("wrdsp $0\n\t"
86+
"mthi %0, $ac1\n\t"
87+
"mtlo %1, $ac1\n\t"
88+
"dpaq_sa.l.w $ac1, %3, %4\n\t"
89+
"mfhi %0, $ac1\n\t"
90+
"mflo %1, $ac1\n\t"
91+
"rddsp %2\n\t"
92+
: "+r"(ach), "+r"(acl), "=r"(dsp)
93+
: "r"(rs), "r"(rt)
94+
);
95+
dsp = (dsp >> 17) & 0x01;
96+
assert(dsp == resultdsp);
97+
assert(ach == resulth);
98+
assert(acl == resultl);
99+
100+
ach = 0x20000000;
101+
acl = 0;
102+
rs = 0xE0000000;
103+
rt = 0x7FFFFFFF;
104+
105+
resulth = 0;
106+
resultl = 0x40000000;
107+
resultdsp = 0;
108+
__asm
109+
("wrdsp $0\n\t"
110+
"mthi %0, $ac1\n\t"
111+
"mtlo %1, $ac1\n\t"
64112
"dpaq_sa.l.w $ac1, %3, %4\n\t"
65113
"mfhi %0, $ac1\n\t"
66114
"mflo %1, $ac1\n\t"

tests/tcg/mips/mips32-dsp/dpsq_sa_l_w.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ int main()
99

1010
rs = 0xBC0123AD;
1111
rt = 0x01643721;
12-
resulth = 0xfdf4cbe0;
13-
resultl = 0xd138776b;
12+
resulth = 0x00BD3A22;
13+
resultl = 0xD138776B;
1414
resultdsp = 0x00;
1515
__asm
1616
("mthi %0, $ac1\n\t"

0 commit comments

Comments
 (0)