# HG changeset patch # User Martin Soch # Date 1359661032 -3600 # Node ID e606853325f16edff2590ee53cf60525bfb099bc # Parent 4c2b92281cdcd93cca84837955f7e9c1390a4d23 Long binary XOR implementation + tests diff -r 4c2b92281cdc -r e606853325f1 emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js --- a/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Thu Jan 31 20:19:38 2013 +0100 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Thu Jan 31 20:37:12 2013 +0100 @@ -109,6 +109,16 @@ return low; }; +Number.prototype.xor64 = function(x) { + var low = this ^ x; + low += (low < 0) ? (__m32+1) : 0; + if (this.hi || x.hi) { + var hi = this.hi ^ x.hi; + return hi.next32(low); + }; + return low; +}; + Number.prototype.shl64 = function(x) { if (x >= 32) { var hi = (this << (x - 32)) | 0; diff -r 4c2b92281cdc -r e606853325f1 vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Thu Jan 31 20:19:38 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Thu Jan 31 20:37:12 2013 +0100 @@ -594,7 +594,7 @@ emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI()); break; case opc_lxor: - emit(out, "@1 ^= @2;", smapper.getL(1), smapper.popL()); + emit(out, "@1 = @1.xor64(@2)", smapper.getL(1), smapper.popL()); break; case opc_ineg: emit(out, "@1 = -@1;", smapper.getI(0)); diff -r 4c2b92281cdc -r e606853325f1 vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Thu Jan 31 20:19:38 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Thu Jan 31 20:37:12 2013 +0100 @@ -275,6 +275,36 @@ ); } + @Test public void longXor1() throws Exception { + final long res = 0x00fa37d7763e0ca1l ^ 0xa7b3432fff00123el; + assertExec("Long binary XOR", + Numbers.class, "xorL__J_3B_3B", + Double.valueOf(res), + new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 }, + new byte[] { (byte)0xa7, (byte)0xb3, (byte)0x43, (byte)0x2f, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e } + ); + } + + @Test public void longXor2() throws Exception { + final long res = 0x00fa37d7763e0ca1l ^ 0x00000000ff00123el; + assertExec("Long binary XOR", + Numbers.class, "xorL__J_3B_3B", + Double.valueOf(res), + new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 }, + new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e } + ); + } + + @Test public void longXor3() throws Exception { + final long res = 0x00000000763e0ca1l ^ 0x00000000ff00123el; + assertExec("Long binary XOR", + Numbers.class, "xorL__J_3B_3B", + Double.valueOf(res), + new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 }, + new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e } + ); + } + private static CharSequence codeSeq; private static Invocable code; diff -r 4c2b92281cdc -r e606853325f1 vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Thu Jan 31 20:19:38 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Thu Jan 31 20:37:12 2013 +0100 @@ -127,4 +127,12 @@ DataInputStream disY = new DataInputStream(isY); return (disX.readLong() | disY.readLong()); } + + public static long xorL(byte[] arrX, byte[] arrY) throws IOException { + ByteArrayInputStream isX = new ByteArrayInputStream(arrX); + DataInputStream disX = new DataInputStream(isX); + ByteArrayInputStream isY = new ByteArrayInputStream(arrY); + DataInputStream disY = new DataInputStream(isY); + return (disX.readLong() ^ disY.readLong()); + } }