vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
author Martin Soch <Martin.Soch@oracle.com>
Thu, 31 Jan 2013 20:19:38 +0100
brancharithmetic
changeset 627 4c2b92281cdc
parent 620 189f695d0b02
child 628 e606853325f1
permissions -rw-r--r--
Added binary OR for Long + tests for AND and OR
     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 addL(byte[] arrX, byte[] arrY) throws IOException {
    76         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
    77         DataInputStream disX = new DataInputStream(isX);
    78         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
    79         DataInputStream disY = new DataInputStream(isY);
    80         return (disX.readLong() + disY.readLong());
    81     }
    82     
    83     public static long subL(byte[] arrX, byte[] arrY) throws IOException {
    84         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
    85         DataInputStream disX = new DataInputStream(isX);
    86         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
    87         DataInputStream disY = new DataInputStream(isY);
    88         return (disX.readLong() - disY.readLong());
    89     }
    90     
    91     public static long mulL(long x, long y) {
    92         return (x * y);
    93     }
    94     
    95     public static long divL(long x, long y) {
    96         return (x / y);
    97     }
    98     
    99     public static long modL(long x, long y) {
   100         return (x % y);
   101     }
   102     
   103     public static long shlL(byte[] arrValue, int nBits) throws IOException {
   104         ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue);
   105         DataInputStream disValue = new DataInputStream(isValue);
   106         return (disValue.readLong() << nBits);
   107     }
   108     
   109     public static long shrL(byte[] arrValue, int nBits) throws IOException {
   110         ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue);
   111         DataInputStream disValue = new DataInputStream(isValue);
   112         return (disValue.readLong() >> nBits);
   113     }
   114     
   115     public static long andL(byte[] arrX, byte[] arrY) throws IOException {
   116         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
   117         DataInputStream disX = new DataInputStream(isX);
   118         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
   119         DataInputStream disY = new DataInputStream(isY);
   120         return (disX.readLong() & disY.readLong());
   121     }
   122     
   123     public static long orL(byte[] arrX, byte[] arrY) throws IOException {
   124         ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
   125         DataInputStream disX = new DataInputStream(isX);
   126         ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
   127         DataInputStream disY = new DataInputStream(isY);
   128         return (disX.readLong() | disY.readLong());
   129     }
   130 }