Fixed compare64 implementation arithmetic
authorLubomir Nerad <lubomir.nerad@oracle.com>
Wed, 06 Feb 2013 12:46:35 +0100
brancharithmetic
changeset 6807ffb635a5c4f
parent 679 ee595aae8353
child 688 fd47ec497fb2
Fixed compare64 implementation
emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
     1.1 --- a/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js	Tue Feb 05 19:14:39 2013 +0100
     1.2 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js	Wed Feb 06 12:46:35 2013 +0100
     1.3 @@ -188,10 +188,10 @@
     1.4  };
     1.5  
     1.6  Number.prototype.compare64 = function(x) {
     1.7 -    if (this.hi == x.hi) {
     1.8 -        return (this == x) ? 0 : ((this < x) ? -1 : 1);
     1.9 +    if (this.high32() === x.high32()) {
    1.10 +        return (this < x) ? -1 : ((this > x) ? 1 : 0);
    1.11      }
    1.12 -    return (this.hi < x.hi) ? -1 : 1;
    1.13 +    return (this.high32() < x.high32()) ? -1 : 1;
    1.14  };
    1.15  
    1.16  Number.prototype.neg64 = function() {
     2.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Tue Feb 05 19:14:39 2013 +0100
     2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Wed Feb 06 12:46:35 2013 +0100
     2.3 @@ -649,7 +649,47 @@
     2.4              new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
     2.5          );
     2.6      }
     2.7 -    
     2.8 +
     2.9 +    @Test public void longCompareSameNumbers() throws Exception {
    2.10 +        assertExec("Long compare same numbers",
    2.11 +            Numbers.class, "compareL__I_3B_3BI",
    2.12 +            0.0,
    2.13 +            new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
    2.14 +            new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
    2.15 +            0
    2.16 +        );
    2.17 +    }
    2.18 +
    2.19 +    @Test public void longComparePositiveNumbers() throws Exception {
    2.20 +        assertExec("Long compare positive numbers",
    2.21 +            Numbers.class, "compareL__I_3B_3BI",
    2.22 +            -1.0,
    2.23 +            new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x20, (byte)0x00, (byte)0x00 },
    2.24 +            new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x10, (byte)0x00, (byte)0x00, (byte)0x00 },
    2.25 +            0
    2.26 +        );
    2.27 +    }
    2.28 +
    2.29 +    @Test public void longCompareNegativeNumbers() throws Exception {
    2.30 +        assertExec("Long compare negative numbers",
    2.31 +            Numbers.class, "compareL__I_3B_3BI",
    2.32 +            1.0,
    2.33 +            new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
    2.34 +            new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
    2.35 +            0
    2.36 +        );
    2.37 +    }
    2.38 +
    2.39 +    @Test public void longCompareMixedNumbers() throws Exception {
    2.40 +        assertExec("Long compare mixed numbers",
    2.41 +            Numbers.class, "compareL__I_3B_3BI",
    2.42 +            -1.0,
    2.43 +            new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
    2.44 +            new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
    2.45 +            0
    2.46 +        );
    2.47 +    }
    2.48 +
    2.49      private static CharSequence codeSeq;
    2.50      private static Invocable code;
    2.51  
     3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Tue Feb 05 19:14:39 2013 +0100
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Wed Feb 06 12:46:35 2013 +0100
     3.3 @@ -159,4 +159,44 @@
     3.4          DataInputStream disY = new DataInputStream(isY);
     3.5          return (disX.readLong() ^ disY.readLong());
     3.6      }
     3.7 +
     3.8 +    public static int compareL(byte[] arrX, byte[] arrY,
     3.9 +                               int zero) throws IOException {
    3.10 +        ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
    3.11 +        DataInputStream disX = new DataInputStream(isX);
    3.12 +        ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
    3.13 +        DataInputStream disY = new DataInputStream(isY);
    3.14 +        final long x = disX.readLong();
    3.15 +        final long y = disY.readLong();
    3.16 +
    3.17 +        final int xyResult = compareL(x, y, zero);
    3.18 +        final int yxResult = compareL(y, x, zero);
    3.19 +
    3.20 +        return ((xyResult + yxResult) == 0) ? xyResult : -2;
    3.21 +    }
    3.22 +
    3.23 +    private static int compareL(long x, long y, int zero) {
    3.24 +        int result = -2;
    3.25 +        int trueCount = 0;
    3.26 +
    3.27 +        x += zero;
    3.28 +        if (x == y) {
    3.29 +            result = 0;
    3.30 +            ++trueCount;
    3.31 +        }
    3.32 +
    3.33 +        x += zero;
    3.34 +        if (x < y) {
    3.35 +            result = -1;
    3.36 +            ++trueCount;
    3.37 +        }
    3.38 +
    3.39 +        x += zero;
    3.40 +        if (x > y) {
    3.41 +            result = 1;
    3.42 +            ++trueCount;
    3.43 +        }
    3.44 +
    3.45 +        return (trueCount == 1) ? result : -2;
    3.46 +    }
    3.47  }