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