blob: 86422bd8e796df71d1783c0922e3dd2d81a57da1 [file] [log] [blame]
Calin Juravle9aec02f2014-11-18 23:06:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17public class Main {
18
19 public static void expectEquals(int expected, int result) {
20 if (expected != result) {
21 throw new Error("Expected: " + expected + ", found: " + result);
22 }
23 }
24
25 public static void expectEquals(long expected, long result) {
26 if (expected != result) {
27 throw new Error("Expected: " + expected + ", found: " + result);
28 }
29 }
30
31 public static void main(String[] args) {
32 shlInt();
33 shlLong();
34 shrInt();
35 shrLong();
36 ushrInt();
37 ushrLong();
38 }
39
40 private static void shlInt() {
41 expectEquals(48, $opt$ShlConst2(12));
42 expectEquals(12, $opt$ShlConst0(12));
43 expectEquals(-48, $opt$Shl(-12, 2));
44 expectEquals(1024, $opt$Shl(32, 5));
45
46 expectEquals(7, $opt$Shl(7, 0));
47 expectEquals(14, $opt$Shl(7, 1));
48 expectEquals(0, $opt$Shl(0, 30));
49
50 expectEquals(1073741824L, $opt$Shl(1, 30));
51 expectEquals(Integer.MIN_VALUE, $opt$Shl(1, 31)); // overflow
52 expectEquals(Integer.MIN_VALUE, $opt$Shl(1073741824, 1)); // overflow
53 expectEquals(1073741824, $opt$Shl(268435456, 2));
54
Mark Mendellba56d062015-05-05 21:34:03 -040055 // Only the 5 lower bits should be used for shifting (& 0x1f).
Calin Juravle9aec02f2014-11-18 23:06:35 +000056 expectEquals(7, $opt$Shl(7, 32)); // 32 & 0x1f = 0
57 expectEquals(14, $opt$Shl(7, 33)); // 33 & 0x1f = 1
58 expectEquals(32, $opt$Shl(1, 101)); // 101 & 0x1f = 5
59
60 expectEquals(Integer.MIN_VALUE, $opt$Shl(1, -1)); // -1 & 0x1f = 31
61 expectEquals(14, $opt$Shl(7, -31)); // -31 & 0x1f = 1
62 expectEquals(7, $opt$Shl(7, -32)); // -32 & 0x1f = 0
63 expectEquals(-536870912, $opt$Shl(7, -3)); // -3 & 0x1f = 29
64
65 expectEquals(Integer.MIN_VALUE, $opt$Shl(7, Integer.MAX_VALUE));
66 expectEquals(7, $opt$Shl(7, Integer.MIN_VALUE));
67 }
68
69 private static void shlLong() {
70 expectEquals(48L, $opt$ShlConst2(12L));
71 expectEquals(12L, $opt$ShlConst0(12L));
72 expectEquals(-48L, $opt$Shl(-12L, 2L));
73 expectEquals(1024L, $opt$Shl(32L, 5L));
74
75 expectEquals(7L, $opt$Shl(7L, 0L));
76 expectEquals(14L, $opt$Shl(7L, 1L));
77 expectEquals(0L, $opt$Shl(0L, 30L));
78
79 expectEquals(1073741824L, $opt$Shl(1L, 30L));
80 expectEquals(2147483648L, $opt$Shl(1L, 31L));
81 expectEquals(2147483648L, $opt$Shl(1073741824L, 1L));
82
83 // Long shifts can use up to 6 lower bits.
84 expectEquals(4294967296L, $opt$Shl(1L, 32L));
85 expectEquals(60129542144L, $opt$Shl(7L, 33L));
86 expectEquals(Long.MIN_VALUE, $opt$Shl(1L, 63L)); // overflow
87
88 // Only the 6 lower bits should be used for shifting (& 0x3f).
89 expectEquals(7L, $opt$Shl(7L, 64L)); // 64 & 0x3f = 0
90 expectEquals(14L, $opt$Shl(7L, 65L)); // 65 & 0x3f = 1
91 expectEquals(137438953472L, $opt$Shl(1L, 101L)); // 101 & 0x3f = 37
92
93 expectEquals(Long.MIN_VALUE, $opt$Shl(1L, -1L)); // -1 & 0x3f = 63
94 expectEquals(14L, $opt$Shl(7L, -63L)); // -63 & 0x3f = 1
95 expectEquals(7L, $opt$Shl(7L, -64L)); // -64 & 0x3f = 0
96 expectEquals(2305843009213693952L, $opt$Shl(1L, -3L)); // -3 & 0x3f = 61
97
98 expectEquals(Long.MIN_VALUE, $opt$Shl(7L, Long.MAX_VALUE));
99 expectEquals(7L, $opt$Shl(7L, Long.MIN_VALUE));
Mark Mendellba56d062015-05-05 21:34:03 -0400100
101 // Exercise some special cases handled by backends/simplifier.
102 expectEquals(24L, $opt$ShlConst1(12L));
103 expectEquals(0x2345678900000000L, $opt$ShlConst32(0x123456789L));
104 expectEquals(0x2490249000000000L, $opt$ShlConst33(0x12481248L));
105 expectEquals(0x4920492000000000L, $opt$ShlConst34(0x12481248L));
106 expectEquals(0x9240924000000000L, $opt$ShlConst35(0x12481248L));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000107 }
108
109 private static void shrInt() {
110 expectEquals(3, $opt$ShrConst2(12));
111 expectEquals(12, $opt$ShrConst0(12));
112 expectEquals(-3, $opt$Shr(-12, 2));
113 expectEquals(1, $opt$Shr(32, 5));
114
115 expectEquals(7, $opt$Shr(7, 0));
116 expectEquals(3, $opt$Shr(7, 1));
117 expectEquals(0, $opt$Shr(0, 30));
118 expectEquals(0, $opt$Shr(1, 30));
119 expectEquals(-1, $opt$Shr(-1, 30));
120
121 expectEquals(0, $opt$Shr(Integer.MAX_VALUE, 31));
122 expectEquals(-1, $opt$Shr(Integer.MIN_VALUE, 31));
123
124 // Only the 5 lower bits should be used for shifting (& 0x1f).
125 expectEquals(7, $opt$Shr(7, 32)); // 32 & 0x1f = 0
126 expectEquals(3, $opt$Shr(7, 33)); // 33 & 0x1f = 1
127
128 expectEquals(0, $opt$Shr(1, -1)); // -1 & 0x1f = 31
129 expectEquals(3, $opt$Shr(7, -31)); // -31 & 0x1f = 1
130 expectEquals(7, $opt$Shr(7, -32)); // -32 & 0x1f = 0
131 expectEquals(-4, $opt$Shr(Integer.MIN_VALUE, -3)); // -3 & 0x1f = 29
132
133 expectEquals(0, $opt$Shr(7, Integer.MAX_VALUE));
134 expectEquals(7, $opt$Shr(7, Integer.MIN_VALUE));
135 }
136
137 private static void shrLong() {
138 expectEquals(3L, $opt$ShrConst2(12L));
139 expectEquals(12L, $opt$ShrConst0(12L));
140 expectEquals(-3L, $opt$Shr(-12L, 2L));
141 expectEquals(1, $opt$Shr(32, 5));
142
143 expectEquals(7L, $opt$Shr(7L, 0L));
144 expectEquals(3L, $opt$Shr(7L, 1L));
145 expectEquals(0L, $opt$Shr(0L, 30L));
146 expectEquals(0L, $opt$Shr(1L, 30L));
147 expectEquals(-1L, $opt$Shr(-1L, 30L));
148
149
150 expectEquals(1L, $opt$Shr(1073741824L, 30L));
151 expectEquals(1L, $opt$Shr(2147483648L, 31L));
152 expectEquals(1073741824L, $opt$Shr(2147483648L, 1L));
153
154 // Long shifts can use up to 6 lower bits.
155 expectEquals(1L, $opt$Shr(4294967296L, 32L));
156 expectEquals(7L, $opt$Shr(60129542144L, 33L));
157 expectEquals(0L, $opt$Shr(Long.MAX_VALUE, 63L));
158 expectEquals(-1L, $opt$Shr(Long.MIN_VALUE, 63L));
159
160 // Only the 6 lower bits should be used for shifting (& 0x3f).
161 expectEquals(7L, $opt$Shr(7L, 64L)); // 64 & 0x3f = 0
162 expectEquals(3L, $opt$Shr(7L, 65L)); // 65 & 0x3f = 1
163
164 expectEquals(-1L, $opt$Shr(Long.MIN_VALUE, -1L)); // -1 & 0x3f = 63
165 expectEquals(3L, $opt$Shr(7L, -63L)); // -63 & 0x3f = 1
166 expectEquals(7L, $opt$Shr(7L, -64L)); // -64 & 0x3f = 0
167 expectEquals(1L, $opt$Shr(2305843009213693952L, -3L)); // -3 & 0x3f = 61
168 expectEquals(-4L, $opt$Shr(Integer.MIN_VALUE, -3)); // -3 & 0x1f = 29
169
170 expectEquals(0L, $opt$Shr(7L, Long.MAX_VALUE));
171 expectEquals(7L, $opt$Shr(7L, Long.MIN_VALUE));
172 }
173
174 private static void ushrInt() {
175 expectEquals(3, $opt$UShrConst2(12));
176 expectEquals(12, $opt$UShrConst0(12));
177 expectEquals(1073741821, $opt$UShr(-12, 2));
178 expectEquals(1, $opt$UShr(32, 5));
179
180 expectEquals(7, $opt$UShr(7, 0));
181 expectEquals(3, $opt$UShr(7, 1));
182 expectEquals(0, $opt$UShr(0, 30));
183 expectEquals(0, $opt$UShr(1, 30));
184 expectEquals(3, $opt$UShr(-1, 30));
185
186 expectEquals(0, $opt$UShr(Integer.MAX_VALUE, 31));
187 expectEquals(1, $opt$UShr(Integer.MIN_VALUE, 31));
188
189 // Only the 5 lower bits should be used for shifting (& 0x1f).
190 expectEquals(7, $opt$UShr(7, 32)); // 32 & 0x1f = 0
191 expectEquals(3, $opt$UShr(7, 33)); // 33 & 0x1f = 1
192
193 expectEquals(0, $opt$UShr(1, -1)); // -1 & 0x1f = 31
194 expectEquals(3, $opt$UShr(7, -31)); // -31 & 0x1f = 1
195 expectEquals(7, $opt$UShr(7, -32)); // -32 & 0x1f = 0
196 expectEquals(4, $opt$UShr(Integer.MIN_VALUE, -3)); // -3 & 0x1f = 29
197
198 expectEquals(0, $opt$UShr(7, Integer.MAX_VALUE));
199 expectEquals(7, $opt$UShr(7, Integer.MIN_VALUE));
200 }
201
202 private static void ushrLong() {
203 expectEquals(3L, $opt$UShrConst2(12L));
204 expectEquals(12L, $opt$UShrConst0(12L));
205 expectEquals(4611686018427387901L, $opt$UShr(-12L, 2L));
206 expectEquals(1, $opt$UShr(32, 5));
207
208 expectEquals(7L, $opt$UShr(7L, 0L));
209 expectEquals(3L, $opt$UShr(7L, 1L));
210 expectEquals(0L, $opt$UShr(0L, 30L));
211 expectEquals(0L, $opt$UShr(1L, 30L));
212 expectEquals(17179869183L, $opt$UShr(-1L, 30L));
213
214
215 expectEquals(1L, $opt$UShr(1073741824L, 30L));
216 expectEquals(1L, $opt$UShr(2147483648L, 31L));
217 expectEquals(1073741824L, $opt$UShr(2147483648L, 1L));
218
Calin Juravle8c3961a2014-11-24 16:36:44 +0000219 // Long shifts can use use up to 6 lower bits.
Calin Juravle9aec02f2014-11-18 23:06:35 +0000220 expectEquals(1L, $opt$UShr(4294967296L, 32L));
221 expectEquals(7L, $opt$UShr(60129542144L, 33L));
222 expectEquals(0L, $opt$UShr(Long.MAX_VALUE, 63L));
223 expectEquals(1L, $opt$UShr(Long.MIN_VALUE, 63L));
224
225 // Only the 6 lower bits should be used for shifting (& 0x3f).
226 expectEquals(7L, $opt$UShr(7L, 64L)); // 64 & 0x3f = 0
227 expectEquals(3L, $opt$UShr(7L, 65L)); // 65 & 0x3f = 1
228
229 expectEquals(1L, $opt$UShr(Long.MIN_VALUE, -1L)); // -1 & 0x3f = 63
230 expectEquals(3L, $opt$UShr(7L, -63L)); // -63 & 0x3f = 1
231 expectEquals(7L, $opt$UShr(7L, -64L)); // -64 & 0x3f = 0
232 expectEquals(1L, $opt$UShr(2305843009213693952L, -3L)); // -3 & 0x3f = 61
233 expectEquals(4L, $opt$UShr(Long.MIN_VALUE, -3L)); // -3 & 0x3f = 61
234
235 expectEquals(0L, $opt$UShr(7L, Long.MAX_VALUE));
236 expectEquals(7L, $opt$UShr(7L, Long.MIN_VALUE));
237 }
238
239 static int $opt$Shl(int a, int b) {
240 return a << b;
241 }
242
243 static long $opt$Shl(long a, long b) {
244 return a << b;
245 }
246
247 static int $opt$Shr(int a, int b) {
248 return a >> b;
249 }
250
251 static long $opt$Shr(long a, long b) {
252 return a >> b;
253 }
254
255 static int $opt$UShr(int a, int b) {
256 return a >>> b;
257 }
258
259 static long $opt$UShr(long a, long b) {
260 return a >>> b;
261 }
262
263 static int $opt$ShlConst2(int a) {
264 return a << 2;
265 }
266
267 static long $opt$ShlConst2(long a) {
268 return a << 2L;
269 }
270
271 static int $opt$ShrConst2(int a) {
272 return a >> 2;
273 }
274
275 static long $opt$ShrConst2(long a) {
276 return a >> 2L;
277 }
278
279 static int $opt$UShrConst2(int a) {
280 return a >>> 2;
281 }
282
283 static long $opt$UShrConst2(long a) {
284 return a >>> 2L;
285 }
286
Mark Mendellba56d062015-05-05 21:34:03 -0400287 static int $opt$ShlConst0(int a) {
Calin Juravle9aec02f2014-11-18 23:06:35 +0000288 return a << 0;
289 }
290
291 static long $opt$ShlConst0(long a) {
292 return a << 0L;
293 }
294
295 static int $opt$ShrConst0(int a) {
296 return a >> 0;
297 }
298
299 static long $opt$ShrConst0(long a) {
300 return a >> 0L;
301 }
302
303 static int $opt$UShrConst0(int a) {
304 return a >>> 0;
305 }
306
307 static long $opt$UShrConst0(long a) {
308 return a >>> 0L;
309 }
310
Mark Mendellba56d062015-05-05 21:34:03 -0400311 static long $opt$ShlConst1(long a) {
312 return a << 1L;
313 }
314
315 static long $opt$ShlConst32(long a) {
316 return a << 32L;
317 }
318
319 static long $opt$ShlConst33(long a) {
320 return a << 33L;
321 }
322
323 static long $opt$ShlConst34(long a) {
324 return a << 34L;
325 }
326
327 static long $opt$ShlConst35(long a) {
328 return a << 35L;
329 }
330
Calin Juravle9aec02f2014-11-18 23:06:35 +0000331}
332