jaroslav@114: /** jaroslav@114: * Back 2 Browser Bytecode Translator jaroslav@114: * Copyright (C) 2012 Jaroslav Tulach jaroslav@114: * jaroslav@114: * This program is free software: you can redistribute it and/or modify jaroslav@114: * it under the terms of the GNU General Public License as published by jaroslav@114: * the Free Software Foundation, version 2 of the License. jaroslav@114: * jaroslav@114: * This program is distributed in the hope that it will be useful, jaroslav@114: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@114: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@114: * GNU General Public License for more details. jaroslav@114: * jaroslav@114: * You should have received a copy of the GNU General Public License jaroslav@114: * along with this program. Look for COPYING file in the top folder. jaroslav@114: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@114: */ jaroslav@114: package org.apidesign.vm4brwsr; jaroslav@114: jaroslav@181: import java.io.ByteArrayInputStream; jaroslav@181: import java.io.DataInputStream; jaroslav@181: import java.io.IOException; jaroslav@181: jaroslav@114: /** jaroslav@114: * jaroslav@114: * @author Jaroslav Tulach jaroslav@114: */ jaroslav@114: public class Numbers { jaroslav@114: private static Double autoboxDbl() { jaroslav@114: return 3.3; jaroslav@114: } jaroslav@114: public static String autoboxDblToString() { jaroslav@114: return autoboxDbl().toString().toString(); jaroslav@114: } jaroslav@178: public static int rem(int a, int b) { jaroslav@178: return a % b; jaroslav@178: } jaroslav@181: jaroslav@181: static float deserFloat() throws IOException { jaroslav@181: byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83}; jaroslav@181: ByteArrayInputStream is = new ByteArrayInputStream(arr); jaroslav@181: DataInputStream dis = new DataInputStream(is); jaroslav@181: float r = dis.readFloat(); jaroslav@181: return r; jaroslav@181: } jaroslav@185: static double deserDouble() throws IOException { jaroslav@185: byte[] arr = {(byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0}; jaroslav@185: ByteArrayInputStream is = new ByteArrayInputStream(arr); jaroslav@185: DataInputStream dis = new DataInputStream(is); jaroslav@185: return dis.readDouble(); jaroslav@185: } jaroslav@185: static long deserLong(byte[] arr) throws IOException { jaroslav@185: ByteArrayInputStream is = new ByteArrayInputStream(arr); jaroslav@185: DataInputStream dis = new DataInputStream(is); jaroslav@185: return dis.readLong(); jaroslav@185: } jaroslav@181: static int deserInt() throws IOException { jaroslav@181: byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83}; jaroslav@181: ByteArrayInputStream is = new ByteArrayInputStream(arr); jaroslav@181: DataInputStream dis = new DataInputStream(is); jaroslav@181: return dis.readInt(); jaroslav@181: } jaroslav@186: jaroslav@186: static String intToString() { jaroslav@186: return new Integer(5).toString().toString(); jaroslav@186: } jaroslav@187: static String floatToString() { jaroslav@187: return new Float(7.0).toString().toString(); jaroslav@187: } Martin@615: Martin@615: public static long conversionL() { Martin@615: return Long.MAX_VALUE; Martin@615: } Martin@615: Martin@630: public static long negL(byte[] arrValue) throws IOException { Martin@630: ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue); Martin@630: DataInputStream disValue = new DataInputStream(isValue); Martin@630: return (-disValue.readLong()); Martin@630: } Martin@630: Martin@616: public static long addL(byte[] arrX, byte[] arrY) throws IOException { Martin@616: ByteArrayInputStream isX = new ByteArrayInputStream(arrX); Martin@616: DataInputStream disX = new DataInputStream(isX); Martin@616: ByteArrayInputStream isY = new ByteArrayInputStream(arrY); Martin@616: DataInputStream disY = new DataInputStream(isY); Martin@616: return (disX.readLong() + disY.readLong()); Martin@615: } Martin@615: Martin@620: public static long subL(byte[] arrX, byte[] arrY) throws IOException { Martin@620: ByteArrayInputStream isX = new ByteArrayInputStream(arrX); Martin@620: DataInputStream disX = new DataInputStream(isX); Martin@620: ByteArrayInputStream isY = new ByteArrayInputStream(arrY); Martin@620: DataInputStream disY = new DataInputStream(isY); Martin@620: return (disX.readLong() - disY.readLong()); Martin@615: } Martin@615: Martin@657: public static long mulL(byte[] arrX, byte[] arrY) throws IOException { Martin@657: ByteArrayInputStream isX = new ByteArrayInputStream(arrX); Martin@657: DataInputStream disX = new DataInputStream(isX); Martin@657: ByteArrayInputStream isY = new ByteArrayInputStream(arrY); Martin@657: DataInputStream disY = new DataInputStream(isY); Martin@657: return (disX.readLong() * disY.readLong()); Martin@615: } Martin@615: lubomir@676: public static long divL(byte[] arrX, byte[] arrY) throws IOException { lubomir@676: ByteArrayInputStream isX = new ByteArrayInputStream(arrX); lubomir@676: DataInputStream disX = new DataInputStream(isX); lubomir@676: ByteArrayInputStream isY = new ByteArrayInputStream(arrY); lubomir@676: DataInputStream disY = new DataInputStream(isY); lubomir@676: return (disX.readLong() / disY.readLong()); Martin@615: } Martin@615: lubomir@676: public static long modL(byte[] arrX, byte[] arrY) throws IOException { lubomir@676: ByteArrayInputStream isX = new ByteArrayInputStream(arrX); lubomir@676: DataInputStream disX = new DataInputStream(isX); lubomir@676: ByteArrayInputStream isY = new ByteArrayInputStream(arrY); lubomir@676: DataInputStream disY = new DataInputStream(isY); lubomir@676: return (disX.readLong() % disY.readLong()); Martin@615: } Martin@617: Martin@617: public static long shlL(byte[] arrValue, int nBits) throws IOException { Martin@617: ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue); Martin@617: DataInputStream disValue = new DataInputStream(isValue); Martin@617: return (disValue.readLong() << nBits); Martin@617: } Martin@617: Martin@617: public static long shrL(byte[] arrValue, int nBits) throws IOException { Martin@617: ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue); Martin@617: DataInputStream disValue = new DataInputStream(isValue); Martin@617: return (disValue.readLong() >> nBits); Martin@617: } Martin@627: Martin@629: public static long ushrL(byte[] arrValue, int nBits) throws IOException { Martin@629: ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue); Martin@629: DataInputStream disValue = new DataInputStream(isValue); Martin@629: return (disValue.readLong() >>> nBits); Martin@629: } Martin@629: Martin@627: public static long andL(byte[] arrX, byte[] arrY) throws IOException { Martin@627: ByteArrayInputStream isX = new ByteArrayInputStream(arrX); Martin@627: DataInputStream disX = new DataInputStream(isX); Martin@627: ByteArrayInputStream isY = new ByteArrayInputStream(arrY); Martin@627: DataInputStream disY = new DataInputStream(isY); Martin@627: return (disX.readLong() & disY.readLong()); Martin@627: } Martin@627: Martin@627: public static long orL(byte[] arrX, byte[] arrY) throws IOException { Martin@627: ByteArrayInputStream isX = new ByteArrayInputStream(arrX); Martin@627: DataInputStream disX = new DataInputStream(isX); Martin@627: ByteArrayInputStream isY = new ByteArrayInputStream(arrY); Martin@627: DataInputStream disY = new DataInputStream(isY); Martin@627: return (disX.readLong() | disY.readLong()); Martin@627: } Martin@628: Martin@628: public static long xorL(byte[] arrX, byte[] arrY) throws IOException { Martin@628: ByteArrayInputStream isX = new ByteArrayInputStream(arrX); Martin@628: DataInputStream disX = new DataInputStream(isX); Martin@628: ByteArrayInputStream isY = new ByteArrayInputStream(arrY); Martin@628: DataInputStream disY = new DataInputStream(isY); Martin@628: return (disX.readLong() ^ disY.readLong()); Martin@628: } lubomir@680: lubomir@680: public static int compareL(byte[] arrX, byte[] arrY, lubomir@680: int zero) throws IOException { lubomir@680: ByteArrayInputStream isX = new ByteArrayInputStream(arrX); lubomir@680: DataInputStream disX = new DataInputStream(isX); lubomir@680: ByteArrayInputStream isY = new ByteArrayInputStream(arrY); lubomir@680: DataInputStream disY = new DataInputStream(isY); lubomir@680: final long x = disX.readLong(); lubomir@680: final long y = disY.readLong(); lubomir@680: lubomir@680: final int xyResult = compareL(x, y, zero); lubomir@680: final int yxResult = compareL(y, x, zero); lubomir@680: lubomir@680: return ((xyResult + yxResult) == 0) ? xyResult : -2; lubomir@680: } lubomir@680: lubomir@680: private static int compareL(long x, long y, int zero) { lubomir@680: int result = -2; lubomir@680: int trueCount = 0; lubomir@680: lubomir@680: x += zero; lubomir@680: if (x == y) { lubomir@680: result = 0; lubomir@680: ++trueCount; lubomir@680: } lubomir@680: lubomir@680: x += zero; lubomir@680: if (x < y) { lubomir@680: result = -1; lubomir@680: ++trueCount; lubomir@680: } lubomir@680: lubomir@680: x += zero; lubomir@680: if (x > y) { lubomir@680: result = 1; lubomir@680: ++trueCount; lubomir@680: } lubomir@680: lubomir@680: return (trueCount == 1) ? result : -2; lubomir@680: } jaroslav@114: }