• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 public class ShiftsTest {
18 
expectEquals(int expected, int result)19   public static void expectEquals(int expected, int result) {
20     if (expected != result) {
21       throw new Error("Expected: " + expected + ", found: " + result);
22     }
23   }
24 
expectEquals(long expected, long result)25   public static void expectEquals(long expected, long result) {
26     if (expected != result) {
27       throw new Error("Expected: " + expected + ", found: " + result);
28     }
29   }
30 
main()31   public static void main() {
32     testShlInt();
33     testShlLong();
34     testShrInt();
35     testShrLong();
36     testUShrInt();
37     testUShrLong();
38   }
39 
testShlInt()40   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));
45 
46     expectEquals(7, $opt$ShlInt(7, 0));
47     expectEquals(14, $opt$ShlInt(7, 1));
48     expectEquals(0, $opt$ShlInt(0, 30));
49 
50     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));
54 
55     // Only the 5 lower bits should be used for shifting (& 0x1f).
56     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
59 
60     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
64 
65     expectEquals(Integer.MIN_VALUE, $opt$ShlInt(7, Integer.MAX_VALUE));
66     expectEquals(7, $opt$ShlInt(7, Integer.MIN_VALUE));
67   }
68 
testShlLong()69   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));
74 
75     expectEquals(7L, $opt$ShlLong(7L, 0));
76     expectEquals(14L, $opt$ShlLong(7L, 1));
77     expectEquals(0L, $opt$ShlLong(0L, 30));
78 
79     expectEquals(1073741824L, $opt$ShlLong(1L, 30));
80     expectEquals(2147483648L, $opt$ShlLong(1L, 31));
81     expectEquals(2147483648L, $opt$ShlLong(1073741824L, 1));
82 
83     // Long shifts can use up to 6 lower bits.
84     expectEquals(4294967296L, $opt$ShlLong(1L, 32));
85     expectEquals(60129542144L, $opt$ShlLong(7L, 33));
86     expectEquals(Long.MIN_VALUE, $opt$ShlLong(1L, 63));  // overflow
87 
88     // Only the 6 lower bits should be used for shifting (& 0x3f).
89     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
92 
93     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
97 
98     expectEquals(Long.MIN_VALUE, $opt$ShlLong(7L, Integer.MAX_VALUE));
99     expectEquals(7L, $opt$ShlLong(7L, Integer.MIN_VALUE));
100 
101     // Exercise some special cases handled by backends/simplifier.
102     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));
107   }
108 
testShrInt()109   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));
114 
115     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));
120 
121     expectEquals(0, $opt$ShrInt(Integer.MAX_VALUE, 31));
122     expectEquals(-1, $opt$ShrInt(Integer.MIN_VALUE, 31));
123 
124     // Only the 5 lower bits should be used for shifting (& 0x1f).
125     expectEquals(7, $opt$ShrInt(7, 32));  // 32 & 0x1f = 0
126     expectEquals(3, $opt$ShrInt(7, 33));  // 33 & 0x1f = 1
127 
128     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
132 
133     expectEquals(0, $opt$ShrInt(7, Integer.MAX_VALUE));
134     expectEquals(7, $opt$ShrInt(7, Integer.MIN_VALUE));
135   }
136 
testShrLong()137   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));
142 
143     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));
148 
149     expectEquals(1L, $opt$ShrLong(1073741824L, 30));
150     expectEquals(1L, $opt$ShrLong(2147483648L, 31));
151     expectEquals(1073741824L, $opt$ShrLong(2147483648L, 1));
152 
153     // Long shifts can use up to 6 lower bits.
154     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));
158 
159     // Only the 6 lower bits should be used for shifting (& 0x3f).
160     expectEquals(7L, $opt$ShrLong(7L, 64));  // 64 & 0x3f = 0
161     expectEquals(3L, $opt$ShrLong(7L, 65));  // 65 & 0x3f = 1
162 
163     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
168 
169     expectEquals(0L, $opt$ShrLong(7L, Integer.MAX_VALUE));
170     expectEquals(7L, $opt$ShrLong(7L, Integer.MIN_VALUE));
171   }
172 
testUShrInt()173   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));
178 
179     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));
184 
185     expectEquals(0, $opt$UShrInt(Integer.MAX_VALUE, 31));
186     expectEquals(1, $opt$UShrInt(Integer.MIN_VALUE, 31));
187 
188     // Only the 5 lower bits should be used for shifting (& 0x1f).
189     expectEquals(7, $opt$UShrInt(7, 32));  // 32 & 0x1f = 0
190     expectEquals(3, $opt$UShrInt(7, 33));  // 33 & 0x1f = 1
191 
192     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
196 
197     expectEquals(0, $opt$UShrInt(7, Integer.MAX_VALUE));
198     expectEquals(7, $opt$UShrInt(7, Integer.MIN_VALUE));
199   }
200 
testUShrLong()201   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));
206 
207     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));
212 
213     expectEquals(1L, $opt$UShrLong(1073741824L, 30));
214     expectEquals(1L, $opt$UShrLong(2147483648L, 31));
215     expectEquals(1073741824L, $opt$UShrLong(2147483648L, 1));
216 
217     // Long shifts can use use up to 6 lower bits.
218     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));
222 
223     // Only the 6 lower bits should be used for shifting (& 0x3f).
224     expectEquals(7L, $opt$UShrLong(7L, 64));  // 64 & 0x3f = 0
225     expectEquals(3L, $opt$UShrLong(7L, 65));  // 65 & 0x3f = 1
226 
227     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
232 
233     expectEquals(0L, $opt$UShrLong(7L, Integer.MAX_VALUE));
234     expectEquals(7L, $opt$UShrLong(7L, Integer.MIN_VALUE));
235   }
236 
237 
$opt$ShlInt(int value, int distance)238   static int $opt$ShlInt(int value, int distance) {
239     return value << distance;
240   }
241 
$opt$ShlLong(long value, int distance)242   static long $opt$ShlLong(long value, int distance) {
243     return value << distance;
244   }
245 
$opt$ShrInt(int value, int distance)246   static int $opt$ShrInt(int value, int distance) {
247     return value >> distance;
248   }
249 
$opt$ShrLong(long value, int distance)250   static long $opt$ShrLong(long value, int distance) {
251     return value >> distance;
252   }
253 
$opt$UShrInt(int value, int distance)254   static int $opt$UShrInt(int value, int distance) {
255     return value >>> distance;
256   }
257 
$opt$UShrLong(long value, int distance)258   static long $opt$UShrLong(long value, int distance) {
259     return value >>> distance;
260   }
261 
$opt$ShlIntConst2(int value)262   static int $opt$ShlIntConst2(int value) {
263     return value << 2;
264   }
265 
$opt$ShlLongConst2(long value)266   static long $opt$ShlLongConst2(long value) {
267     return value << 2;
268   }
269 
$opt$ShrIntConst2(int value)270   static int $opt$ShrIntConst2(int value) {
271     return value >> 2;
272   }
273 
$opt$ShrLongConst2(long value)274   static long $opt$ShrLongConst2(long value) {
275     return value >> 2;
276   }
277 
$opt$UShrIntConst2(int value)278   static int $opt$UShrIntConst2(int value) {
279     return value >>> 2;
280   }
281 
$opt$UShrLongConst2(long value)282   static long $opt$UShrLongConst2(long value) {
283     return value >>> 2;
284   }
285 
$opt$ShlIntConst0(int value)286   static int $opt$ShlIntConst0(int value) {
287     return value << 0;
288   }
289 
$opt$ShlLongConst0(long value)290   static long $opt$ShlLongConst0(long value) {
291     return value << 0;
292   }
293 
$opt$ShrIntConst0(int value)294   static int $opt$ShrIntConst0(int value) {
295     return value >> 0;
296   }
297 
$opt$ShrLongConst0(long value)298   static long $opt$ShrLongConst0(long value) {
299     return value >> 0;
300   }
301 
$opt$UShrIntConst0(int value)302   static int $opt$UShrIntConst0(int value) {
303     return value >>> 0;
304   }
305 
$opt$UShrLongConst0(long value)306   static long $opt$UShrLongConst0(long value) {
307     return value >>> 0;
308   }
309 
$opt$ShlLongConst1(long value)310   static long $opt$ShlLongConst1(long value) {
311     return value << 1;
312   }
313 
$opt$ShlLongConst32(long value)314   static long $opt$ShlLongConst32(long value) {
315     return value << 32;
316   }
317 
$opt$ShlLongConst33(long value)318   static long $opt$ShlLongConst33(long value) {
319     return value << 33;
320   }
321 
$opt$ShlLongConst34(long value)322   static long $opt$ShlLongConst34(long value) {
323     return value << 34;
324   }
325 
$opt$ShlLongConst35(long value)326   static long $opt$ShlLongConst35(long value) {
327     return value << 35;
328   }
329 
330 }
331