blob: 139ff70bf02be84735b3895f7c2d75720df11bef [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
Vladimir Markof66b67f2018-03-28 13:32:18 +010017public class ShiftsTest {
Calin Juravle9aec02f2014-11-18 23:06:35 +000018
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
Vladimir Markof66b67f2018-03-28 13:32:18 +010031 public static void main() {
Roland Levillain9b579662016-03-18 18:29:58 +000032 testShlInt();
33 testShlLong();
34 testShrInt();
35 testShrLong();
36 testUShrInt();
37 testUShrLong();
Calin Juravle9aec02f2014-11-18 23:06:35 +000038 }
39
Roland Levillain9b579662016-03-18 18:29:58 +000040 private static void testShlInt() {
41 expectEquals(48, $opt$ShlIntConst2(12));
42 expectEquals(12, $opt$ShlIntConst0(12));
43 expectEquals(-48, $opt$ShlInt(-12, 2));
44 expectEquals(1024, $opt$ShlInt(32, 5));
Calin Juravle9aec02f2014-11-18 23:06:35 +000045
Roland Levillain9b579662016-03-18 18:29:58 +000046 expectEquals(7, $opt$ShlInt(7, 0));
47 expectEquals(14, $opt$ShlInt(7, 1));
48 expectEquals(0, $opt$ShlInt(0, 30));
Calin Juravle9aec02f2014-11-18 23:06:35 +000049
Roland Levillain9b579662016-03-18 18:29:58 +000050 expectEquals(1073741824L, $opt$ShlInt(1, 30));
51 expectEquals(Integer.MIN_VALUE, $opt$ShlInt(1, 31)); // overflow
52 expectEquals(Integer.MIN_VALUE, $opt$ShlInt(1073741824, 1)); // overflow
53 expectEquals(1073741824, $opt$ShlInt(268435456, 2));
Calin Juravle9aec02f2014-11-18 23:06:35 +000054
Mark Mendellba56d062015-05-05 21:34:03 -040055 // Only the 5 lower bits should be used for shifting (& 0x1f).
Roland Levillain9b579662016-03-18 18:29:58 +000056 expectEquals(7, $opt$ShlInt(7, 32)); // 32 & 0x1f = 0
57 expectEquals(14, $opt$ShlInt(7, 33)); // 33 & 0x1f = 1
58 expectEquals(32, $opt$ShlInt(1, 101)); // 101 & 0x1f = 5
Calin Juravle9aec02f2014-11-18 23:06:35 +000059
Roland Levillain9b579662016-03-18 18:29:58 +000060 expectEquals(Integer.MIN_VALUE, $opt$ShlInt(1, -1)); // -1 & 0x1f = 31
61 expectEquals(14, $opt$ShlInt(7, -31)); // -31 & 0x1f = 1
62 expectEquals(7, $opt$ShlInt(7, -32)); // -32 & 0x1f = 0
63 expectEquals(-536870912, $opt$ShlInt(7, -3)); // -3 & 0x1f = 29
Calin Juravle9aec02f2014-11-18 23:06:35 +000064
Roland Levillain9b579662016-03-18 18:29:58 +000065 expectEquals(Integer.MIN_VALUE, $opt$ShlInt(7, Integer.MAX_VALUE));
66 expectEquals(7, $opt$ShlInt(7, Integer.MIN_VALUE));
Calin Juravle9aec02f2014-11-18 23:06:35 +000067 }
68
Roland Levillain9b579662016-03-18 18:29:58 +000069 private static void testShlLong() {
70 expectEquals(48L, $opt$ShlLongConst2(12L));
71 expectEquals(12L, $opt$ShlLongConst0(12L));
72 expectEquals(-48L, $opt$ShlLong(-12L, 2));
73 expectEquals(1024L, $opt$ShlLong(32L, 5));
Calin Juravle9aec02f2014-11-18 23:06:35 +000074
Roland Levillain9b579662016-03-18 18:29:58 +000075 expectEquals(7L, $opt$ShlLong(7L, 0));
76 expectEquals(14L, $opt$ShlLong(7L, 1));
77 expectEquals(0L, $opt$ShlLong(0L, 30));
Calin Juravle9aec02f2014-11-18 23:06:35 +000078
Roland Levillain9b579662016-03-18 18:29:58 +000079 expectEquals(1073741824L, $opt$ShlLong(1L, 30));
80 expectEquals(2147483648L, $opt$ShlLong(1L, 31));
81 expectEquals(2147483648L, $opt$ShlLong(1073741824L, 1));
Calin Juravle9aec02f2014-11-18 23:06:35 +000082
83 // Long shifts can use up to 6 lower bits.
Roland Levillain9b579662016-03-18 18:29:58 +000084 expectEquals(4294967296L, $opt$ShlLong(1L, 32));
85 expectEquals(60129542144L, $opt$ShlLong(7L, 33));
86 expectEquals(Long.MIN_VALUE, $opt$ShlLong(1L, 63)); // overflow
Calin Juravle9aec02f2014-11-18 23:06:35 +000087
88 // Only the 6 lower bits should be used for shifting (& 0x3f).
Roland Levillain9b579662016-03-18 18:29:58 +000089 expectEquals(7L, $opt$ShlLong(7L, 64)); // 64 & 0x3f = 0
90 expectEquals(14L, $opt$ShlLong(7L, 65)); // 65 & 0x3f = 1
91 expectEquals(137438953472L, $opt$ShlLong(1L, 101)); // 101 & 0x3f = 37
Calin Juravle9aec02f2014-11-18 23:06:35 +000092
Roland Levillain9b579662016-03-18 18:29:58 +000093 expectEquals(Long.MIN_VALUE, $opt$ShlLong(1L, -1)); // -1 & 0x3f = 63
94 expectEquals(14L, $opt$ShlLong(7L, -63)); // -63 & 0x3f = 1
95 expectEquals(7L, $opt$ShlLong(7L, -64)); // -64 & 0x3f = 0
96 expectEquals(2305843009213693952L, $opt$ShlLong(1L, -3)); // -3 & 0x3f = 61
Calin Juravle9aec02f2014-11-18 23:06:35 +000097
Roland Levillain9b579662016-03-18 18:29:58 +000098 expectEquals(Long.MIN_VALUE, $opt$ShlLong(7L, Integer.MAX_VALUE));
99 expectEquals(7L, $opt$ShlLong(7L, Integer.MIN_VALUE));
Mark Mendellba56d062015-05-05 21:34:03 -0400100
101 // Exercise some special cases handled by backends/simplifier.
Roland Levillain9b579662016-03-18 18:29:58 +0000102 expectEquals(24L, $opt$ShlLongConst1(12L));
103 expectEquals(0x2345678900000000L, $opt$ShlLongConst32(0x123456789L));
104 expectEquals(0x2490249000000000L, $opt$ShlLongConst33(0x12481248L));
105 expectEquals(0x4920492000000000L, $opt$ShlLongConst34(0x12481248L));
106 expectEquals(0x9240924000000000L, $opt$ShlLongConst35(0x12481248L));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000107 }
108
Roland Levillain9b579662016-03-18 18:29:58 +0000109 private static void testShrInt() {
110 expectEquals(3, $opt$ShrIntConst2(12));
111 expectEquals(12, $opt$ShrIntConst0(12));
112 expectEquals(-3, $opt$ShrInt(-12, 2));
113 expectEquals(1, $opt$ShrInt(32, 5));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000114
Roland Levillain9b579662016-03-18 18:29:58 +0000115 expectEquals(7, $opt$ShrInt(7, 0));
116 expectEquals(3, $opt$ShrInt(7, 1));
117 expectEquals(0, $opt$ShrInt(0, 30));
118 expectEquals(0, $opt$ShrInt(1, 30));
119 expectEquals(-1, $opt$ShrInt(-1, 30));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000120
Roland Levillain9b579662016-03-18 18:29:58 +0000121 expectEquals(0, $opt$ShrInt(Integer.MAX_VALUE, 31));
122 expectEquals(-1, $opt$ShrInt(Integer.MIN_VALUE, 31));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000123
124 // Only the 5 lower bits should be used for shifting (& 0x1f).
Roland Levillain9b579662016-03-18 18:29:58 +0000125 expectEquals(7, $opt$ShrInt(7, 32)); // 32 & 0x1f = 0
126 expectEquals(3, $opt$ShrInt(7, 33)); // 33 & 0x1f = 1
Calin Juravle9aec02f2014-11-18 23:06:35 +0000127
Roland Levillain9b579662016-03-18 18:29:58 +0000128 expectEquals(0, $opt$ShrInt(1, -1)); // -1 & 0x1f = 31
129 expectEquals(3, $opt$ShrInt(7, -31)); // -31 & 0x1f = 1
130 expectEquals(7, $opt$ShrInt(7, -32)); // -32 & 0x1f = 0
131 expectEquals(-4, $opt$ShrInt(Integer.MIN_VALUE, -3)); // -3 & 0x1f = 29
Calin Juravle9aec02f2014-11-18 23:06:35 +0000132
Roland Levillain9b579662016-03-18 18:29:58 +0000133 expectEquals(0, $opt$ShrInt(7, Integer.MAX_VALUE));
134 expectEquals(7, $opt$ShrInt(7, Integer.MIN_VALUE));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000135 }
136
Roland Levillain9b579662016-03-18 18:29:58 +0000137 private static void testShrLong() {
138 expectEquals(3L, $opt$ShrLongConst2(12L));
139 expectEquals(12L, $opt$ShrLongConst0(12L));
140 expectEquals(-3L, $opt$ShrLong(-12L, 2));
141 expectEquals(1, $opt$ShrLong(32, 5));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000142
Roland Levillain9b579662016-03-18 18:29:58 +0000143 expectEquals(7L, $opt$ShrLong(7L, 0));
144 expectEquals(3L, $opt$ShrLong(7L, 1));
145 expectEquals(0L, $opt$ShrLong(0L, 30));
146 expectEquals(0L, $opt$ShrLong(1L, 30));
147 expectEquals(-1L, $opt$ShrLong(-1L, 30));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000148
Roland Levillain9b579662016-03-18 18:29:58 +0000149 expectEquals(1L, $opt$ShrLong(1073741824L, 30));
150 expectEquals(1L, $opt$ShrLong(2147483648L, 31));
151 expectEquals(1073741824L, $opt$ShrLong(2147483648L, 1));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000152
153 // Long shifts can use up to 6 lower bits.
Roland Levillain9b579662016-03-18 18:29:58 +0000154 expectEquals(1L, $opt$ShrLong(4294967296L, 32));
155 expectEquals(7L, $opt$ShrLong(60129542144L, 33));
156 expectEquals(0L, $opt$ShrLong(Long.MAX_VALUE, 63));
157 expectEquals(-1L, $opt$ShrLong(Long.MIN_VALUE, 63));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000158
159 // Only the 6 lower bits should be used for shifting (& 0x3f).
Roland Levillain9b579662016-03-18 18:29:58 +0000160 expectEquals(7L, $opt$ShrLong(7L, 64)); // 64 & 0x3f = 0
161 expectEquals(3L, $opt$ShrLong(7L, 65)); // 65 & 0x3f = 1
Calin Juravle9aec02f2014-11-18 23:06:35 +0000162
Roland Levillain9b579662016-03-18 18:29:58 +0000163 expectEquals(-1L, $opt$ShrLong(Long.MIN_VALUE, -1)); // -1 & 0x3f = 63
164 expectEquals(3L, $opt$ShrLong(7L, -63)); // -63 & 0x3f = 1
165 expectEquals(7L, $opt$ShrLong(7L, -64)); // -64 & 0x3f = 0
166 expectEquals(1L, $opt$ShrLong(2305843009213693952L, -3)); // -3 & 0x3f = 61
167 expectEquals(-1L, $opt$ShrLong(Integer.MIN_VALUE, -3)); // -3 & 0x1f = 29
Calin Juravle9aec02f2014-11-18 23:06:35 +0000168
Roland Levillain9b579662016-03-18 18:29:58 +0000169 expectEquals(0L, $opt$ShrLong(7L, Integer.MAX_VALUE));
170 expectEquals(7L, $opt$ShrLong(7L, Integer.MIN_VALUE));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000171 }
172
Roland Levillain9b579662016-03-18 18:29:58 +0000173 private static void testUShrInt() {
174 expectEquals(3, $opt$UShrIntConst2(12));
175 expectEquals(12, $opt$UShrIntConst0(12));
176 expectEquals(1073741821, $opt$UShrInt(-12, 2));
177 expectEquals(1, $opt$UShrInt(32, 5));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000178
Roland Levillain9b579662016-03-18 18:29:58 +0000179 expectEquals(7, $opt$UShrInt(7, 0));
180 expectEquals(3, $opt$UShrInt(7, 1));
181 expectEquals(0, $opt$UShrInt(0, 30));
182 expectEquals(0, $opt$UShrInt(1, 30));
183 expectEquals(3, $opt$UShrInt(-1, 30));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000184
Roland Levillain9b579662016-03-18 18:29:58 +0000185 expectEquals(0, $opt$UShrInt(Integer.MAX_VALUE, 31));
186 expectEquals(1, $opt$UShrInt(Integer.MIN_VALUE, 31));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000187
188 // Only the 5 lower bits should be used for shifting (& 0x1f).
Roland Levillain9b579662016-03-18 18:29:58 +0000189 expectEquals(7, $opt$UShrInt(7, 32)); // 32 & 0x1f = 0
190 expectEquals(3, $opt$UShrInt(7, 33)); // 33 & 0x1f = 1
Calin Juravle9aec02f2014-11-18 23:06:35 +0000191
Roland Levillain9b579662016-03-18 18:29:58 +0000192 expectEquals(0, $opt$UShrInt(1, -1)); // -1 & 0x1f = 31
193 expectEquals(3, $opt$UShrInt(7, -31)); // -31 & 0x1f = 1
194 expectEquals(7, $opt$UShrInt(7, -32)); // -32 & 0x1f = 0
195 expectEquals(4, $opt$UShrInt(Integer.MIN_VALUE, -3)); // -3 & 0x1f = 29
Calin Juravle9aec02f2014-11-18 23:06:35 +0000196
Roland Levillain9b579662016-03-18 18:29:58 +0000197 expectEquals(0, $opt$UShrInt(7, Integer.MAX_VALUE));
198 expectEquals(7, $opt$UShrInt(7, Integer.MIN_VALUE));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000199 }
200
Roland Levillain9b579662016-03-18 18:29:58 +0000201 private static void testUShrLong() {
202 expectEquals(3L, $opt$UShrLongConst2(12L));
203 expectEquals(12L, $opt$UShrLongConst0(12L));
204 expectEquals(4611686018427387901L, $opt$UShrLong(-12L, 2));
205 expectEquals(1, $opt$UShrLong(32, 5));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000206
Roland Levillain9b579662016-03-18 18:29:58 +0000207 expectEquals(7L, $opt$UShrLong(7L, 0));
208 expectEquals(3L, $opt$UShrLong(7L, 1));
209 expectEquals(0L, $opt$UShrLong(0L, 30));
210 expectEquals(0L, $opt$UShrLong(1L, 30));
211 expectEquals(17179869183L, $opt$UShrLong(-1L, 30));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000212
Roland Levillain9b579662016-03-18 18:29:58 +0000213 expectEquals(1L, $opt$UShrLong(1073741824L, 30));
214 expectEquals(1L, $opt$UShrLong(2147483648L, 31));
215 expectEquals(1073741824L, $opt$UShrLong(2147483648L, 1));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000216
Calin Juravle8c3961a2014-11-24 16:36:44 +0000217 // Long shifts can use use up to 6 lower bits.
Roland Levillain9b579662016-03-18 18:29:58 +0000218 expectEquals(1L, $opt$UShrLong(4294967296L, 32));
219 expectEquals(7L, $opt$UShrLong(60129542144L, 33));
220 expectEquals(0L, $opt$UShrLong(Long.MAX_VALUE, 63));
221 expectEquals(1L, $opt$UShrLong(Long.MIN_VALUE, 63));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000222
223 // Only the 6 lower bits should be used for shifting (& 0x3f).
Roland Levillain9b579662016-03-18 18:29:58 +0000224 expectEquals(7L, $opt$UShrLong(7L, 64)); // 64 & 0x3f = 0
225 expectEquals(3L, $opt$UShrLong(7L, 65)); // 65 & 0x3f = 1
Calin Juravle9aec02f2014-11-18 23:06:35 +0000226
Roland Levillain9b579662016-03-18 18:29:58 +0000227 expectEquals(1L, $opt$UShrLong(Long.MIN_VALUE, -1)); // -1 & 0x3f = 63
228 expectEquals(3L, $opt$UShrLong(7L, -63)); // -63 & 0x3f = 1
229 expectEquals(7L, $opt$UShrLong(7L, -64)); // -64 & 0x3f = 0
230 expectEquals(1L, $opt$UShrLong(2305843009213693952L, -3)); // -3 & 0x3f = 61
231 expectEquals(4L, $opt$UShrLong(Long.MIN_VALUE, -3)); // -3 & 0x3f = 61
Calin Juravle9aec02f2014-11-18 23:06:35 +0000232
Roland Levillain9b579662016-03-18 18:29:58 +0000233 expectEquals(0L, $opt$UShrLong(7L, Integer.MAX_VALUE));
234 expectEquals(7L, $opt$UShrLong(7L, Integer.MIN_VALUE));
Calin Juravle9aec02f2014-11-18 23:06:35 +0000235 }
236
Roland Levillain9b579662016-03-18 18:29:58 +0000237
238 static int $opt$ShlInt(int value, int distance) {
239 return value << distance;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000240 }
241
Roland Levillain9b579662016-03-18 18:29:58 +0000242 static long $opt$ShlLong(long value, int distance) {
243 return value << distance;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000244 }
245
Roland Levillain9b579662016-03-18 18:29:58 +0000246 static int $opt$ShrInt(int value, int distance) {
247 return value >> distance;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000248 }
249
Roland Levillain9b579662016-03-18 18:29:58 +0000250 static long $opt$ShrLong(long value, int distance) {
251 return value >> distance;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000252 }
253
Roland Levillain9b579662016-03-18 18:29:58 +0000254 static int $opt$UShrInt(int value, int distance) {
255 return value >>> distance;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000256 }
257
Roland Levillain9b579662016-03-18 18:29:58 +0000258 static long $opt$UShrLong(long value, int distance) {
259 return value >>> distance;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000260 }
261
Roland Levillain9b579662016-03-18 18:29:58 +0000262 static int $opt$ShlIntConst2(int value) {
263 return value << 2;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000264 }
265
Roland Levillain9b579662016-03-18 18:29:58 +0000266 static long $opt$ShlLongConst2(long value) {
267 return value << 2;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000268 }
269
Roland Levillain9b579662016-03-18 18:29:58 +0000270 static int $opt$ShrIntConst2(int value) {
271 return value >> 2;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000272 }
273
Roland Levillain9b579662016-03-18 18:29:58 +0000274 static long $opt$ShrLongConst2(long value) {
275 return value >> 2;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000276 }
277
Roland Levillain9b579662016-03-18 18:29:58 +0000278 static int $opt$UShrIntConst2(int value) {
279 return value >>> 2;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000280 }
281
Roland Levillain9b579662016-03-18 18:29:58 +0000282 static long $opt$UShrLongConst2(long value) {
283 return value >>> 2;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000284 }
285
Roland Levillain9b579662016-03-18 18:29:58 +0000286 static int $opt$ShlIntConst0(int value) {
287 return value << 0;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000288 }
289
Roland Levillain9b579662016-03-18 18:29:58 +0000290 static long $opt$ShlLongConst0(long value) {
291 return value << 0;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000292 }
293
Roland Levillain9b579662016-03-18 18:29:58 +0000294 static int $opt$ShrIntConst0(int value) {
295 return value >> 0;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000296 }
297
Roland Levillain9b579662016-03-18 18:29:58 +0000298 static long $opt$ShrLongConst0(long value) {
299 return value >> 0;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000300 }
301
Roland Levillain9b579662016-03-18 18:29:58 +0000302 static int $opt$UShrIntConst0(int value) {
303 return value >>> 0;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000304 }
305
Roland Levillain9b579662016-03-18 18:29:58 +0000306 static long $opt$UShrLongConst0(long value) {
307 return value >>> 0;
Calin Juravle9aec02f2014-11-18 23:06:35 +0000308 }
309
Roland Levillain9b579662016-03-18 18:29:58 +0000310 static long $opt$ShlLongConst1(long value) {
311 return value << 1;
Mark Mendellba56d062015-05-05 21:34:03 -0400312 }
313
Roland Levillain9b579662016-03-18 18:29:58 +0000314 static long $opt$ShlLongConst32(long value) {
315 return value << 32;
Mark Mendellba56d062015-05-05 21:34:03 -0400316 }
317
Roland Levillain9b579662016-03-18 18:29:58 +0000318 static long $opt$ShlLongConst33(long value) {
319 return value << 33;
Mark Mendellba56d062015-05-05 21:34:03 -0400320 }
321
Roland Levillain9b579662016-03-18 18:29:58 +0000322 static long $opt$ShlLongConst34(long value) {
323 return value << 34;
Mark Mendellba56d062015-05-05 21:34:03 -0400324 }
325
Roland Levillain9b579662016-03-18 18:29:58 +0000326 static long $opt$ShlLongConst35(long value) {
327 return value << 35;
Mark Mendellba56d062015-05-05 21:34:03 -0400328 }
329
Calin Juravle9aec02f2014-11-18 23:06:35 +0000330}