vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Wed, 06 Feb 2013 12:46:35 +0100
brancharithmetic
changeset 680 7ffb635a5c4f
parent 676 eecf6077ec4e
child 698 ff57af563cb8
permissions -rw-r--r--
Fixed compare64 implementation
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.ByteArrayInputStream;
    21 import java.io.DataInputStream;
    22 import java.io.IOException;
    23 
    24 /**
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 public class Numbers {
    29     private static Double autoboxDbl() {
    30         return 3.3;
    31     }
    32     public static String autoboxDblToString() {
    33         return autoboxDbl().toString().toString();
    34     }
    35     public static int rem(int a, int b) {
    36         return a % b;
    37     }
    38 
    39     static float deserFloat() throws IOException {
    40         byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
    41         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    42         DataInputStream dis = new DataInputStream(is);
    43         float r = dis.readFloat();
    44         return r;
    45     }
    46     static double deserDouble() throws IOException {
    47         byte[] arr = {(byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
    48         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    49         DataInputStream dis = new DataInputStream(is);
    50         return dis.readDouble();
    51     }
    52     static long deserLong(byte[] arr) throws IOException {
    53         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    54         DataInputStream dis = new DataInputStream(is);
    55         return dis.readLong();
    56     }
    57     static int deserInt() throws IOException {
    58         byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
    59         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    60         DataInputStream dis = new DataInputStream(is);
    61         return dis.readInt();
    62     }
    63 
    64     static String intToString() {
    65         return new Integer(5).toString().toString();
    66     }
    67     static String floatToString() {
    68         return new Float(7.0).toString().toString();
    69     }
    70     
    71     public static long conversionL() {
    72         return Long.MAX_VALUE;
    73     }
    74     
    75     public static long negL(byte[] arrValue) throws IOException {
    76         ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue);
    77         DataInputStream disValue = new DataInputStream(isValue);
    78         return (-disValue.readLong());
    79     }
    80     
    81     public static long addL(byte[] arrX, byte[] arrY) throws IOException {
    82         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
    83         DataInputStream disX = new DataInputStream(isX);
    84         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
    85         DataInputStream disY = new DataInputStream(isY);
    86         return (disX.readLong() + disY.readLong());
    87     }
    88     
    89     public static long subL(byte[] arrX, byte[] arrY) throws IOException {
    90         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
    91         DataInputStream disX = new DataInputStream(isX);
    92         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
    93         DataInputStream disY = new DataInputStream(isY);
    94         return (disX.readLong() - disY.readLong());
    95     }
    96     
    97     public static long mulL(byte[] arrX, byte[] arrY) throws IOException {
    98         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
    99         DataInputStream disX = new DataInputStream(isX);
   100         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
   101         DataInputStream disY = new DataInputStream(isY);
   102         return (disX.readLong() * disY.readLong());
   103     }
   104     
   105     public static long divL(byte[] arrX, byte[] arrY) throws IOException {
   106         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
   107         DataInputStream disX = new DataInputStream(isX);
   108         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
   109         DataInputStream disY = new DataInputStream(isY);
   110         return (disX.readLong() / disY.readLong());
   111     }
   112     
   113     public static long modL(byte[] arrX, byte[] arrY) throws IOException {
   114         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
   115         DataInputStream disX = new DataInputStream(isX);
   116         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
   117         DataInputStream disY = new DataInputStream(isY);
   118         return (disX.readLong() % disY.readLong());
   119     }
   120     
   121     public static long shlL(byte[] arrValue, int nBits) throws IOException {
   122         ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue);
   123         DataInputStream disValue = new DataInputStream(isValue);
   124         return (disValue.readLong() << nBits);
   125     }
   126     
   127     public static long shrL(byte[] arrValue, int nBits) throws IOException {
   128         ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue);
   129         DataInputStream disValue = new DataInputStream(isValue);
   130         return (disValue.readLong() >> nBits);
   131     }
   132     
   133     public static long ushrL(byte[] arrValue, int nBits) throws IOException {
   134         ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue);
   135         DataInputStream disValue = new DataInputStream(isValue);
   136         return (disValue.readLong() >>> nBits);
   137     }
   138     
   139     public static long andL(byte[] arrX, byte[] arrY) throws IOException {
   140         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
   141         DataInputStream disX = new DataInputStream(isX);
   142         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
   143         DataInputStream disY = new DataInputStream(isY);
   144         return (disX.readLong() & disY.readLong());
   145     }
   146     
   147     public static long orL(byte[] arrX, byte[] arrY) throws IOException {
   148         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
   149         DataInputStream disX = new DataInputStream(isX);
   150         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
   151         DataInputStream disY = new DataInputStream(isY);
   152         return (disX.readLong() | disY.readLong());
   153     }
   154     
   155     public static long xorL(byte[] arrX, byte[] arrY) throws IOException {
   156         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
   157         DataInputStream disX = new DataInputStream(isX);
   158         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
   159         DataInputStream disY = new DataInputStream(isY);
   160         return (disX.readLong() ^ disY.readLong());
   161     }
   162 
   163     public static int compareL(byte[] arrX, byte[] arrY,
   164                                int zero) throws IOException {
   165         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
   166         DataInputStream disX = new DataInputStream(isX);
   167         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
   168         DataInputStream disY = new DataInputStream(isY);
   169         final long x = disX.readLong();
   170         final long y = disY.readLong();
   171 
   172         final int xyResult = compareL(x, y, zero);
   173         final int yxResult = compareL(y, x, zero);
   174 
   175         return ((xyResult + yxResult) == 0) ? xyResult : -2;
   176     }
   177 
   178     private static int compareL(long x, long y, int zero) {
   179         int result = -2;
   180         int trueCount = 0;
   181 
   182         x += zero;
   183         if (x == y) {
   184             result = 0;
   185             ++trueCount;
   186         }
   187 
   188         x += zero;
   189         if (x < y) {
   190             result = -1;
   191             ++trueCount;
   192         }
   193 
   194         x += zero;
   195         if (x > y) {
   196             result = 1;
   197             ++trueCount;
   198         }
   199 
   200         return (trueCount == 1) ? result : -2;
   201     }
   202 }