# HG changeset patch # User Martin Soch # Date 1355858351 -3600 # Node ID b7459b10d581687ca66a40008bf42d2ee16a0c3a # Parent f954edc4843151fdad74753c13b2033eb03bb848 Added test for + - * operations in int32 arithmetic, updated JS generator to produce code for correct int32 arithmetic. diff -r f954edc48431 -r b7459b10d581 vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Tue Dec 18 11:41:09 2012 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Tue Dec 18 20:19:11 2012 +0100 @@ -453,7 +453,7 @@ emit(out, "@1 = @2;", lmapper.setD(3), smapper.popD()); break; case opc_iadd: - emit(out, "@1 += @2;", smapper.getI(1), smapper.popI()); + emit(out, "@1 = (@1 + @2) | 0;", smapper.getI(1), smapper.popI()); break; case opc_ladd: emit(out, "@1 += @2;", smapper.getL(1), smapper.popL()); @@ -465,7 +465,7 @@ emit(out, "@1 += @2;", smapper.getD(1), smapper.popD()); break; case opc_isub: - emit(out, "@1 -= @2;", smapper.getI(1), smapper.popI()); + emit(out, "@1 = (@1 - @2) | 0;", smapper.getI(1), smapper.popI()); break; case opc_lsub: emit(out, "@1 -= @2;", smapper.getL(1), smapper.popL()); @@ -477,7 +477,7 @@ emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD()); break; case opc_imul: - emit(out, "@1 *= @2;", smapper.getI(1), smapper.popI()); + emit(out, "@1 = (((@1 * (@2 >> 16)) << 16) + @1 * (@2 & 0xFFFF)) | 0;", smapper.getI(1), smapper.popI()); break; case opc_lmul: emit(out, "@1 *= @2;", smapper.getL(1), smapper.popL()); diff -r f954edc48431 -r b7459b10d581 vm/src/test/java/org/apidesign/vm4brwsr/tck/IntegerArithmeticTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/tck/IntegerArithmeticTest.java Tue Dec 18 11:41:09 2012 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/tck/IntegerArithmeticTest.java Tue Dec 18 20:19:11 2012 +0100 @@ -17,7 +17,6 @@ */ package org.apidesign.vm4brwsr.tck; -import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.vm4brwsr.Compare; import org.apidesign.vm4brwsr.CompareVMs; import org.testng.annotations.Factory; @@ -27,30 +26,53 @@ * @author Jaroslav Tulach */ public class IntegerArithmeticTest { - //@JavaScriptBody(args="msg", body="java.lang.System.out.println(msg.toString());") - //private static native void log(String msg); - @Compare public int overflow() { - int v = Integer.MAX_VALUE + 1; - return v; + private static int add(int x, int y) { + return x + y; } - @Compare public int underflow() { - int v = Integer.MIN_VALUE - 1; - return v; + private static int sub(int x, int y) { + return x - y; } - /* @Compare public int convertToInt() { - long v = Long.MAX_VALUE / 2; - log("convert: " + v); - return (int) v; - } */ - @Compare public int addAndMaxInt() { - return Integer.MAX_VALUE + Integer.MAX_VALUE; + private static int mul(int x, int y) { + return x * y; + } + + @Compare public int addOverflow() { + return add(Integer.MAX_VALUE, 1); + } + + @Compare public int subUnderflow() { + return sub(Integer.MIN_VALUE, 1); + } + + @Compare public int addMaxIntAndMaxInt() { + return add(Integer.MAX_VALUE, Integer.MAX_VALUE); + } + + @Compare public int subMinIntAndMinInt() { + return sub(Integer.MIN_VALUE, Integer.MIN_VALUE); } @Compare public int multiplyMaxInt() { - return Integer.MAX_VALUE * Integer.MAX_VALUE; + return mul(Integer.MAX_VALUE, 2); + } + + @Compare public int multiplyMaxIntAndMaxInt() { + return mul(Integer.MAX_VALUE, Integer.MAX_VALUE); + } + + @Compare public int multiplyMinInt() { + return mul(Integer.MIN_VALUE, 2); + } + + @Compare public int multiplyMinIntAndMinInt() { + return mul(Integer.MIN_VALUE, Integer.MIN_VALUE); + } + + @Compare public int multiplyPrecision() { + return mul(119106029, 1103515245); } @Factory