Skip to contentSkip to conversation

Fix overflow conditions for MIPS add / subtract

PATCH In review Stefan Weil 4 participants 5 messages Apr 13, 2006 – Apr 28, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix overflow conditions for MIPS add / subtract

Hi,

I had problems with MIPS system emulation (AR7 based DSL router) which were caused by wrong overflow exceptions.

With the patch given below emulation works. See this link for first results: http://forum.openwrt.org/viewtopic.php?id=4381

In user mode emulation, the MIPS emulation currently ignores exceptions. So the bug might have an effect on emulation speed but not on functionality for user mode emulation.

Regards
Stefan Weil

PS. Please include this and also my last MIPS patch in CVS HEAD.

Index: target-mips/op.c
===================================================================
RCS file: /sources/qemu/qemu/target-mips/op.c,v
retrieving revision 1.5

op.c 13 Apr 2006 18:38:19 -0000 renamed from op.c 5 Dec 2005 19:59:36 -0000 1.5 +4 -2
diff -u -b -B -r1.5 op.c
@@ -206,7 +206,8 @@
 
    tmp = T0;
    T0 += T1;
-    if ((T0 >> 31) ^ (T1 >> 31) ^ (tmp >> 31)) {
+    if (((tmp ^ T1 ^ (-1)) & (T0 ^ T1)) >> 31) {
+       /* operands of same sign, result different sign */
        CALL_FROM_TB1(do_raise_exception_direct, EXCP_OVERFLOW);
    }
    RETURN();
@@ -224,7 +225,8 @@
 
    tmp = T0;
    T0 = (int32_t)T0 - (int32_t)T1;
-    if (!((T0 >> 31) ^ (T1 >> 31) ^ (tmp >> 31))) {
+    if (((tmp ^ T1) & (tmp ^ T0)) >> 31) {
+       /* operands of different sign, first operand and result different sign */
        CALL_FROM_TB1(do_raise_exception_direct, EXCP_OVERFLOW);
    }
    RETURN();
#1 lore raw

op.c 13 Apr 2006 18:38:19 -0000 line 212

Quoted 5 lines · Stefan Weil
op.c 13 Apr 2006 18:38:19 -0000 renamed from op.c 5 Dec 2005 19:59:36 -0000 1.5 +2 -1
-    if ((T0 >> 31) ^ (T1 >> 31) ^ (tmp >> 31)) {
+    if (((tmp ^ T1 ^ (-1)) & (T0 ^ T1)) >> 31) {
+       /* operands of same sign, result different sign */
        CALL_FROM_TB1(do_raise_exception_direct, EXCP_OVERFLOW);
    }

I see this went in, but - huh? The math doesn't make sense.

T0 ^ T1 -> operands of different sign
tmp ^ T1 ^ (-1) -> result has same sign as T1

Which is a "who cares" case. This is addition, it can't overflow if the operands have the same sign.

Quoted 6 lines
op.c 13 Apr 2006 18:38:19 -0000 renamed from op.c 5 Dec 2005 19:59:36 -0000 1.5 +2 -1
-    if (!((T0 >> 31) ^ (T1 >> 31) ^ (tmp >> 31))) {
+    if (((tmp ^ T1) & (tmp ^ T0)) >> 31) {
+       /* operands of different sign, first operand and result 

different sign */

        CALL_FROM_TB1(do_raise_exception_direct, EXCP_OVERFLOW);
    }

tmp ^ T1 -> result and T1 of different sign
tmp ^ T0 -> result and T0 of different sign

Which implies that the operands have the same sign. Again, this case can't overflow.

I haven't tested the patched qemu, but I did test the expressions themselves in standalone code, and they definitely do not detect overflow.

Signature · 2 lines
Daniel Jacobowitz
CodeSourcery
#3 lore raw
Quoted 10 lines · Dirk Behme
Quoted 3 lines · Daniel Jacobowitz

I haven't tested the patched qemu, but I did test the expressions themselves in standalone code, and they definitely do not detect overflow.

Maybe you can test Ralf's alternative proposal

http://lists.gnu.org/archive/html/qemu-devel/2006-02/msg00154.html

as well?

Using 64-bit math for this would be awful for performance. My original checks were wrong; we just need to use a correct fix... Lightly tested, but I think this is right for add:

- if ((T0 >> 31) ^ (T1 >> 31) ^ (tmp >> 31)) {
+ if (~(T0 ^ T1) & (T0 ^ tmp) & 0x80000000) {

And this for sub:

- if (!((T0 >> 31) ^ (T1 >> 31) ^ (tmp >> 31))) {
+ if ((T0 ^ T1) & (T0 ^ tmp) & 0x80000000) {

Signature · 2 lines
Daniel Jacobowitz
CodeSourcery
#4 lore raw
In reply to Daniel Jacobowitz
Quoted 11 lines

op.c 13 Apr 2006 18:38:19 -0000 line 212

Quoted 6 lines
op.c 13 Apr 2006 18:38:19 -0000 renamed from op.c 5 Dec 2005 19:59:36 -0000 1.5 +2 -1
-    if ((T0 >> 31) ^ (T1 >> 31) ^ (tmp >> 31)) {
+    if (((tmp ^ T1 ^ (-1)) & (T0 ^ T1)) >> 31) {
+       /* operands of same sign, result different sign */
        CALL_FROM_TB1(do_raise_exception_direct, EXCP_OVERFLOW);
    }

I see this went in, but - huh? The math doesn't make sense.

T0 ^ T1 -> operands of different sign
tmp ^ T1 ^ (-1) -> result has same sign as T1

The definitive reference for all this bit twiddling magic and much more besides is an excellent book, "Hacker's Delight", by Hank Warren. It has loads of stuff about integer overflow and whatnot.

J