blob: d8667c63c5efadb6c3bc852c836ca97fc9ebe95b [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
55 // othe nly 5 lower bits should be used for shifting (& 0x1f).
56 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));
100 }
101
102 private static void shrInt() {
103 expectEquals(3, $opt$ShrConst2(12));
104 expectEquals(12, $opt$ShrConst0(12));
105 expectEquals(-3, $opt$Shr(-12, 2));
106 expectEquals(1, $opt$Shr(32, 5));
107
108 expectEquals(7, $opt$Shr(7, 0));
109 expectEquals(3, $opt$Shr(7, 1));
110 expectEquals(0, $opt$Shr(0, 30));
111 expectEquals(0, $opt$Shr(1, 30));
112 expectEquals(-1, $opt$Shr(-1, 30));
113
114 expectEquals(0, $opt$Shr(Integer.MAX_VALUE, 31));
115 expectEquals(-1, $opt$Shr(Integer.MIN_VALUE, 31));
116
117 // Only the 5 lower bits should be used for shifting (& 0x1f).
118 expectEquals(7, $opt$Shr(7, 32)); // 32 & 0x1f = 0
119 expectEquals(3, $opt$Shr(7, 33)); // 33 & 0x1f = 1
120
121 expectEquals(0, $opt$Shr(1, -1)); // -1 & 0x1f = 31
122 expectEquals(3, $opt$Shr(7, -31)); // -31 & 0x1f = 1
123 expectEquals(7, $opt$Shr(7, -32)); // -32 & 0x1f = 0
124 expectEquals(-4, $opt$Shr(Integer.MIN_VALUE, -3)); // -3 & 0x1f = 29
125
126 expectEquals(0, $opt$Shr(7, Integer.MAX_VALUE));
127 expectEquals(7, $opt$Shr(7, Integer.MIN_VALUE));
128 }
129
130 private static void shrLong() {
131 expectEquals(3L, $opt$ShrConst2(12L));
132 expectEquals(12L, $opt$ShrConst0(12L));
133 expectEquals(-3L, $opt$Shr(-12L, 2L));
134 expectEquals(1, $opt$Shr(32, 5));
135
136 expectEquals(7L, $opt$Shr(7L, 0L));
137 expectEquals(3L, $opt$Shr(7L, 1L));
138 expectEquals(0L, $opt$Shr(0L, 30L));
139 expectEquals(0L, $opt$Shr(1L, 30L));
140 expectEquals(-1L, $opt$Shr(-1L, 30L));
141
142
143 expectEquals(1L, $opt$Shr(1073741824L, 30L));
144 expectEquals(1L, $opt$Shr(2147483648L, 31L));
145 expectEquals(1073741824L, $opt$Shr(2147483648L, 1L));
146
147 // Long shifts can use up to 6 lower bits.
148 expectEquals(1L, $opt$Shr(4294967296L, 32L));
149 expectEquals(7L, $opt$Shr(60129542144L, 33L));
150 expectEquals(0L, $opt$Shr(Long.MAX_VALUE, 63L));
151 expectEquals(-1L, $opt$Shr(Long.MIN_VALUE, 63L));
152
153 // Only the 6 lower bits should be used for shifting (& 0x3f).
154 expectEquals(7L, $opt$Shr(7L, 64L)); // 64 & 0x3f = 0
155 expectEquals(3L, $opt$Shr(7L, 65L)); // 65 & 0x3f = 1
156
157 expectEquals(-1L, $opt$Shr(Long.MIN_VALUE, -1L)); // -1 & 0x3f = 63
158 expectEquals(3L, $opt$Shr(7L, -63L)); // -63 & 0x3f = 1
159 expectEquals(7L, $opt$Shr(7L, -64L)); // -64 & 0x3f = 0
160 expectEquals(1L, $opt$Shr(2305843009213693952L, -3L)); // -3 & 0x3f = 61
161 expectEquals(-4L, $opt$Shr(Integer.MIN_VALUE, -3)); // -3 & 0x1f = 29
162
163 expectEquals(0L, $opt$Shr(7L, Long.MAX_VALUE));
164 expectEquals(7L, $opt$Shr(7L, Long.MIN_VALUE));
165 }
166
167 private static void ushrInt() {
168 expectEquals(3, $opt$UShrConst2(12));
169 expectEquals(12, $opt$UShrConst0(12));
170 expectEquals(1073741821, $opt$UShr(-12, 2));
171 expectEquals(1, $opt$UShr(32, 5));
172
173 expectEquals(7, $opt$UShr(7, 0));
174 expectEquals(3, $opt$UShr(7, 1));
175 expectEquals(0, $opt$UShr(0, 30));
176 expectEquals(0, $opt$UShr(1, 30));
177 expectEquals(3, $opt$UShr(-1, 30));
178
179 expectEquals(0, $opt$UShr(Integer.MAX_VALUE, 31));
180 expectEquals(1, $opt$UShr(Integer.MIN_VALUE, 31));
181
182 // Only the 5 lower bits should be used for shifting (& 0x1f).
183 expectEquals(7, $opt$UShr(7, 32)); // 32 & 0x1f = 0
184 expectEquals(3, $opt$UShr(7, 33)); // 33 & 0x1f = 1
185
186 expectEquals(0, $opt$UShr(1, -1)); // -1 & 0x1f = 31
187 expectEquals(3, $opt$UShr(7, -31)); // -31 & 0x1f = 1
188 expectEquals(7, $opt$UShr(7, -32)); // -32 & 0x1f = 0
189 expectEquals(4, $opt$UShr(Integer.MIN_VALUE, -3)); // -3 & 0x1f = 29
190
191 expectEquals(0, $opt$UShr(7, Integer.MAX_VALUE));
192 expectEquals(7, $opt$UShr(7, Integer.MIN_VALUE));
193 }
194
195 private static void ushrLong() {
196 expectEquals(3L, $opt$UShrConst2(12L));
197 expectEquals(12L, $opt$UShrConst0(12L));
198 expectEquals(4611686018427387901L, $opt$UShr(-12L, 2L));
199 expectEquals(1, $opt$UShr(32, 5));
200
201 expectEquals(7L, $opt$UShr(7L, 0L));
202 expectEquals(3L, $opt$UShr(7L, 1L));
203 expectEquals(0L, $opt$UShr(0L, 30L));
204 expectEquals(0L, $opt$UShr(1L, 30L));
205 expectEquals(17179869183L, $opt$UShr(-1L, 30L));
206
207
208 expectEquals(1L, $opt$UShr(1073741824L, 30L));
209 expectEquals(1L, $opt$UShr(2147483648L, 31L));
210 expectEquals(1073741824L, $opt$UShr(2147483648L, 1L));
211
Calin Juravle8c3961a2014-11-24 16:36:44 +0000212 // Long shifts can use use up to 6 lower bits.
Calin Juravle9aec02f2014-11-18 23:06:35 +0000213 expectEquals(1L, $opt$UShr(4294967296L, 32L));
214 expectEquals(7L, $opt$UShr(60129542144L, 33L));
215 expectEquals(0L, $opt$UShr(Long.MAX_VALUE, 63L));
216 expectEquals(1L, $opt$UShr(Long.MIN_VALUE, 63L));
217
218 // Only the 6 lower bits should be used for shifting (& 0x3f).
219 expectEquals(7L, $opt$UShr(7L, 64L)); // 64 & 0x3f = 0
220 expectEquals(3L, $opt$UShr(7L, 65L)); // 65 & 0x3f = 1
221
222 expectEquals(1L, $opt$UShr(Long.MIN_VALUE, -1L)); // -1 & 0x3f = 63
223 expectEquals(3L, $opt$UShr(7L, -63L)); // -63 & 0x3f = 1
224 expectEquals(7L, $opt$UShr(7L, -64L)); // -64 & 0x3f = 0
225 expectEquals(1L, $opt$UShr(2305843009213693952L, -3L)); // -3 & 0x3f = 61
226 expectEquals(4L, $opt$UShr(Long.MIN_VALUE, -3L)); // -3 & 0x3f = 61
227
228 expectEquals(0L, $opt$UShr(7L, Long.MAX_VALUE));
229 expectEquals(7L, $opt$UShr(7L, Long.MIN_VALUE));
230 }
231
232 static int $opt$Shl(int a, int b) {
233 return a << b;
234 }
235
236 static long $opt$Shl(long a, long b) {
237 return a << b;
238 }
239
240 static int $opt$Shr(int a, int b) {
241 return a >> b;
242 }
243
244 static long $opt$Shr(long a, long b) {
245 return a >> b;
246 }
247
248 static int $opt$UShr(int a, int b) {
249 return a >>> b;
250 }
251
252 static long $opt$UShr(long a, long b) {
253 return a >>> b;
254 }
255
256 static int $opt$ShlConst2(int a) {
257 return a << 2;
258 }
259
260 static long $opt$ShlConst2(long a) {
261 return a << 2L;
262 }
263
264 static int $opt$ShrConst2(int a) {
265 return a >> 2;
266 }
267
268 static long $opt$ShrConst2(long a) {
269 return a >> 2L;
270 }
271
272 static int $opt$UShrConst2(int a) {
273 return a >>> 2;
274 }
275
276 static long $opt$UShrConst2(long a) {
277 return a >>> 2L;
278 }
279
280 static int $opt$ShlConst0(int a) {
281 return a << 0;
282 }
283
284 static long $opt$ShlConst0(long a) {
285 return a << 0L;
286 }
287
288 static int $opt$ShrConst0(int a) {
289 return a >> 0;
290 }
291
292 static long $opt$ShrConst0(long a) {
293 return a >> 0L;
294 }
295
296 static int $opt$UShrConst0(int a) {
297 return a >>> 0;
298 }
299
300 static long $opt$UShrConst0(long a) {
301 return a >>> 0L;
302 }
303
304}
305