1 /*
2  * Copyright (C) 2015 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 Main {
18 
assertIntEquals(int expected, int actual)19   public static void assertIntEquals(int expected, int actual) {
20     if (expected != actual) {
21       throw new Error("Expected: " + expected + ", found: " + actual);
22     }
23   }
24 
assertLongEquals(long expected, long actual)25   public static void assertLongEquals(long expected, long actual) {
26     if (expected != actual) {
27       throw new Error("Expected: " + expected + ", found: " + actual);
28     }
29   }
30 
main(String args[])31   public static void main(String args[]) throws Exception {
32     test_Integer_right_v_csubv();
33     test_Long_right_v_csubv();
34 
35     test_Integer_right_constant_v();
36     test_Long_right_constant_v();
37 
38     test_Integer_left_csubv_v();
39     test_Long_left_csubv_v();
40 
41     test_Integer_right_v_negv();
42     test_Long_right_v_negv();
43 
44     test_Integer_left_negv_v();
45     test_Long_left_negv_v();
46 
47     test_Integer_left_constant_v();
48     test_Long_left_constant_v();
49   }
50 
51   public static boolean doThrow = false;
52 
$noinline$rotate_int_right_reg_v_csubv(int value, int distance)53   public static int $noinline$rotate_int_right_reg_v_csubv(int value, int distance) {
54     if (doThrow) {
55       throw new Error();
56     }
57     return (value >>> distance) | (value << (32 - distance));
58   }
59 
test_Integer_right_v_csubv()60   public static void test_Integer_right_v_csubv() throws Exception {
61     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x11, 0), 0x11);
62 
63     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x11, 1), 0x80000008);
64     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x11, Integer.SIZE - 1), 0x22);
65     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x11, Integer.SIZE), 0x11);
66     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x11, Integer.SIZE + 1), 0x80000008);
67 
68     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x11, -1), 0x22);
69     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x11, -(Integer.SIZE - 1)), 0x80000008);
70     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x11, -Integer.SIZE), 0x11);
71     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x11, -(Integer.SIZE + 1)), 0x22);
72 
73     assertIntEquals($noinline$rotate_int_right_reg_v_csubv(0x80000000, 1), 0x40000000);
74   }
75 
$noinline$rotate_long_right_reg_v_csubv(long value, int distance)76   public static long $noinline$rotate_long_right_reg_v_csubv(long value, int distance) {
77     if (doThrow) {
78       throw new Error();
79     }
80     return (value >>> distance) | (value << (64 - distance));
81   }
82 
test_Long_right_v_csubv()83   public static void test_Long_right_v_csubv() throws Exception {
84     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x11, 0), 0x11);
85 
86     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x11, 1), 0x8000000000000008L);
87     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x11, Long.SIZE - 1), 0x22);
88     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x11, Long.SIZE), 0x11);
89     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x11, Long.SIZE + 1), 0x8000000000000008L);
90 
91     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x11, -1), 0x22);
92     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x11, -(Long.SIZE - 1)), 0x8000000000000008L);
93     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x11, -Long.SIZE), 0x11);
94     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x11, -(Long.SIZE + 1)), 0x22);
95 
96     assertLongEquals($noinline$rotate_long_right_reg_v_csubv(0x8000000000000000L, 1), 0x4000000000000000L);
97   }
98 
$noinline$rotate_int_left_reg_csubv_v(int value, int distance)99   public static int $noinline$rotate_int_left_reg_csubv_v(int value, int distance) {
100     if (doThrow) {
101       throw new Error();
102     }
103     return (value >>> (32 - distance)) | (value << distance);
104   }
105 
test_Integer_left_csubv_v()106   public static void test_Integer_left_csubv_v() throws Exception {
107     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0x11, 0), 0x11);
108 
109     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0x11, 1), 0x22);
110     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0x11, Integer.SIZE - 1), 0x80000008);
111     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0x11, Integer.SIZE), 0x11);
112     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0x11, Integer.SIZE + 1), 0x22);
113 
114     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0x11, -1), 0x80000008);
115     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0x11, -(Integer.SIZE - 1)), 0x22);
116     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0x11, -Integer.SIZE), 0x11);
117     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0x11, -(Integer.SIZE + 1)), 0x80000008);
118 
119     assertIntEquals($noinline$rotate_int_left_reg_csubv_v(0xC0000000, 1), 0x80000001);
120   }
121 
$noinline$rotate_long_left_reg_csubv_v(long value, int distance)122   public static long $noinline$rotate_long_left_reg_csubv_v(long value, int distance) {
123     if (doThrow) {
124       throw new Error();
125     }
126     return (value >>> (64 - distance)) | (value << distance);
127   }
128 
test_Long_left_csubv_v()129   public static void test_Long_left_csubv_v() throws Exception {
130     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0x11, 0), 0x11);
131 
132     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0x11, 1), 0x22);
133     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0x11, Long.SIZE - 1), 0x8000000000000008L);
134     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0x11, Long.SIZE), 0x11);
135     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0x11, Long.SIZE + 1), 0x22);
136 
137     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0x11, -1), 0x8000000000000008L);
138     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0x11, -(Long.SIZE - 1)), 0x22);
139     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0x11, -Long.SIZE), 0x11);
140     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0x11, -(Long.SIZE + 1)), 0x8000000000000008L);
141 
142     assertLongEquals($noinline$rotate_long_left_reg_csubv_v(0xC000000000000000L, 1), 0x8000000000000001L);
143   }
144 
$noinline$rotate_int_right_reg_v_negv(int value, int distance)145   public static int $noinline$rotate_int_right_reg_v_negv(int value, int distance) {
146     if (doThrow) {
147       throw new Error();
148     }
149     return (value >>> distance) | (value << -distance);
150   }
151 
test_Integer_right_v_negv()152   public static void test_Integer_right_v_negv() throws Exception {
153     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x11, 0), 0x11);
154 
155     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x11, 1), 0x80000008);
156     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x11, Integer.SIZE - 1), 0x22);
157     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x11, Integer.SIZE), 0x11);
158     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x11, Integer.SIZE + 1), 0x80000008);
159 
160     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x11, -1), 0x22);
161     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x11, -(Integer.SIZE - 1)), 0x80000008);
162     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x11, -Integer.SIZE), 0x11);
163     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x11, -(Integer.SIZE + 1)), 0x22);
164 
165     assertIntEquals($noinline$rotate_int_right_reg_v_negv(0x80000000, 1), 0x40000000);
166   }
167 
$noinline$rotate_long_right_reg_v_negv(long value, int distance)168   public static long $noinline$rotate_long_right_reg_v_negv(long value, int distance) {
169     if (doThrow) {
170       throw new Error();
171     }
172     return (value >>> distance) | (value << -distance);
173   }
174 
test_Long_right_v_negv()175   public static void test_Long_right_v_negv() throws Exception {
176     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x11, 0), 0x11);
177 
178     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x11, 1), 0x8000000000000008L);
179     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x11, Long.SIZE - 1), 0x22);
180     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x11, Long.SIZE), 0x11);
181     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x11, Long.SIZE + 1), 0x8000000000000008L);
182 
183     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x11, -1), 0x22);
184     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x11, -(Long.SIZE - 1)), 0x8000000000000008L);
185     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x11, -Long.SIZE), 0x11);
186     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x11, -(Long.SIZE + 1)), 0x22);
187 
188     assertLongEquals($noinline$rotate_long_right_reg_v_negv(0x8000000000000000L, 1), 0x4000000000000000L);
189   }
190 
$noinline$rotate_int_left_reg_negv_v(int value, int distance)191   public static int $noinline$rotate_int_left_reg_negv_v(int value, int distance) {
192     if (doThrow) {
193       throw new Error();
194     }
195     return (value >>> -distance) | (value << distance);
196   }
197 
test_Integer_left_negv_v()198   public static void test_Integer_left_negv_v() throws Exception {
199     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0x11, 0), 0x11);
200 
201     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0x11, 1), 0x22);
202     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0x11, Integer.SIZE - 1), 0x80000008);
203     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0x11, Integer.SIZE), 0x11);
204     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0x11, Integer.SIZE + 1), 0x22);
205 
206     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0x11, -1), 0x80000008);
207     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0x11, -(Integer.SIZE - 1)), 0x22);
208     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0x11, -Integer.SIZE), 0x11);
209     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0x11, -(Integer.SIZE + 1)), 0x80000008);
210 
211     assertIntEquals($noinline$rotate_int_left_reg_negv_v(0xC0000000, 1), 0x80000001);
212   }
213 
$noinline$rotate_long_left_reg_negv_v(long value, int distance)214   public static long $noinline$rotate_long_left_reg_negv_v(long value, int distance) {
215     if (doThrow) {
216       throw new Error();
217     }
218     return (value >>> -distance) | (value << distance);
219   }
220 
test_Long_left_negv_v()221   public static void test_Long_left_negv_v() throws Exception {
222     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0x11, 0), 0x11);
223 
224     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0x11, 1), 0x22);
225     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0x11, Long.SIZE - 1), 0x8000000000000008L);
226     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0x11, Long.SIZE), 0x11);
227     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0x11, Long.SIZE + 1), 0x22);
228 
229     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0x11, -1), 0x8000000000000008L);
230     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0x11, -(Long.SIZE - 1)), 0x22);
231     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0x11, -Long.SIZE), 0x11);
232     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0x11, -(Long.SIZE + 1)), 0x8000000000000008L);
233 
234     assertLongEquals($noinline$rotate_long_left_reg_negv_v(0xC000000000000000L, 1), 0x8000000000000001L);
235   }
236 
$noinline$rotate_int_right_constant_0(int value)237   public static int $noinline$rotate_int_right_constant_0(int value) {
238     if (doThrow) {
239       throw new Error();
240     }
241     return (value >>> 0) | (value << 0);
242   }
243 
$noinline$rotate_int_right_constant_1(int value)244   public static int $noinline$rotate_int_right_constant_1(int value) {
245     if (doThrow) {
246       throw new Error();
247     }
248     return (value >>> 1) | (value << -1);
249   }
250 
$noinline$rotate_int_right_constant_m1(int value)251   public static int $noinline$rotate_int_right_constant_m1(int value) {
252     if (doThrow) {
253       throw new Error();
254     }
255     return (value >>> -1) | (value << 1);
256   }
257 
$noinline$rotate_int_right_constant_16(int value)258   public static int $noinline$rotate_int_right_constant_16(int value) {
259     if (doThrow) {
260       throw new Error();
261     }
262     return (value >>> 16) | (value << -16);
263   }
264 
test_Integer_right_constant_v()265   public static void test_Integer_right_constant_v() throws Exception {
266     assertIntEquals($noinline$rotate_int_right_constant_0(0x11), 0x11);
267     assertIntEquals($noinline$rotate_int_right_constant_1(0x11), 0x80000008);
268     assertIntEquals($noinline$rotate_int_right_constant_m1(0x11), 0x22);
269     assertIntEquals($noinline$rotate_int_right_constant_16(0x11), 0x110000);
270   }
271 
$noinline$rotate_long_right_constant_0(long value)272   public static long $noinline$rotate_long_right_constant_0(long value) {
273     if (doThrow) {
274       throw new Error();
275     }
276     return (value >>> 0) | (value << 0);
277   }
278 
$noinline$rotate_long_right_constant_1(long value)279   public static long $noinline$rotate_long_right_constant_1(long value) {
280     if (doThrow) {
281       throw new Error();
282     }
283     return (value >>> 1) | (value << -1);
284   }
285 
$noinline$rotate_long_right_constant_m1(long value)286   public static long $noinline$rotate_long_right_constant_m1(long value) {
287     if (doThrow) {
288       throw new Error();
289     }
290     return (value >>> -1) | (value << 1);
291   }
292 
$noinline$rotate_long_right_constant_16(long value)293   public static long $noinline$rotate_long_right_constant_16(long value) {
294     if (doThrow) {
295       throw new Error();
296     }
297     return (value >>> 16) | (value << -16);
298   }
299 
$noinline$rotate_long_right_constant_32(long value)300   public static long $noinline$rotate_long_right_constant_32(long value) {
301     if (doThrow) {
302       throw new Error();
303     }
304     return (value >>> 32) | (value << -32);
305   }
306 
$noinline$rotate_long_right_constant_48(long value)307   public static long $noinline$rotate_long_right_constant_48(long value) {
308     if (doThrow) {
309       throw new Error();
310     }
311     return (value >>> 48) | (value << -48);
312   }
313 
$noinline$rotate_long_right_constant_64(long value)314   public static long $noinline$rotate_long_right_constant_64(long value) {
315     if (doThrow) {
316       throw new Error();
317     }
318     return (value >>> 64) | (value << -64);
319   }
320 
test_Long_right_constant_v()321   public static void test_Long_right_constant_v() throws Exception {
322     assertLongEquals($noinline$rotate_long_right_constant_0(0x11), 0x11);
323     assertLongEquals($noinline$rotate_long_right_constant_1(0x11), 0x8000000000000008L);
324     assertLongEquals($noinline$rotate_long_right_constant_m1(0x11), 0x22);
325     assertLongEquals($noinline$rotate_long_right_constant_16(0x11), 0x11000000000000L);
326     assertLongEquals($noinline$rotate_long_right_constant_32(0x11), 0x1100000000L);
327     assertLongEquals($noinline$rotate_long_right_constant_48(0x11), 0x110000L);
328   }
329 
$noinline$rotate_int_left_constant_0(int value)330   public static int $noinline$rotate_int_left_constant_0(int value) {
331     if (doThrow) {
332       throw new Error();
333     }
334     return (value << 0) | (value >>> 0);
335   }
336 
$noinline$rotate_int_left_constant_1(int value)337   public static int $noinline$rotate_int_left_constant_1(int value) {
338     if (doThrow) {
339       throw new Error();
340     }
341     return (value << 1) | (value >>> -1);
342   }
343 
$noinline$rotate_int_left_constant_m1(int value)344   public static int $noinline$rotate_int_left_constant_m1(int value) {
345     if (doThrow) {
346       throw new Error();
347     }
348     return (value << -1) | (value >>> 1);
349   }
350 
$noinline$rotate_int_left_constant_16(int value)351   public static int $noinline$rotate_int_left_constant_16(int value) {
352     if (doThrow) {
353       throw new Error();
354     }
355     return (value << 16) | (value >>> -16);
356   }
357 
test_Integer_left_constant_v()358   public static void test_Integer_left_constant_v() throws Exception {
359     assertIntEquals($noinline$rotate_int_left_constant_0(0x11), 0x11);
360     assertIntEquals($noinline$rotate_int_left_constant_1(0x11), 0x22);
361     assertIntEquals($noinline$rotate_int_left_constant_m1(0x11), 0x80000008);
362     assertIntEquals($noinline$rotate_int_left_constant_16(0x11), 0x110000);
363   }
364 
$noinline$rotate_long_left_constant_0(long value)365   public static long $noinline$rotate_long_left_constant_0(long value) {
366     if (doThrow) {
367       throw new Error();
368     }
369     return (value << 0) | (value >>> 0);
370   }
371 
$noinline$rotate_long_left_constant_1(long value)372   public static long $noinline$rotate_long_left_constant_1(long value) {
373     if (doThrow) {
374       throw new Error();
375     }
376     return (value << 1) | (value >>> -1);
377   }
378 
$noinline$rotate_long_left_constant_m1(long value)379   public static long $noinline$rotate_long_left_constant_m1(long value) {
380     if (doThrow) {
381       throw new Error();
382     }
383     return (value << -1) | (value >>> 1);
384   }
385 
$noinline$rotate_long_left_constant_16(long value)386   public static long $noinline$rotate_long_left_constant_16(long value) {
387     if (doThrow) {
388       throw new Error();
389     }
390     return (value << 16) | (value >>> -16);
391   }
392 
$noinline$rotate_long_left_constant_32(long value)393   public static long $noinline$rotate_long_left_constant_32(long value) {
394     if (doThrow) {
395       throw new Error();
396     }
397     return (value << 32) | (value >>> -32);
398   }
399 
$noinline$rotate_long_left_constant_48(long value)400   public static long $noinline$rotate_long_left_constant_48(long value) {
401     if (doThrow) {
402       throw new Error();
403     }
404     return (value << 48) | (value >>> -48);
405   }
406 
$noinline$rotate_long_left_constant_64(long value)407   public static long $noinline$rotate_long_left_constant_64(long value) {
408     if (doThrow) {
409       throw new Error();
410     }
411     return (value << 64) | (value >>> -64);
412   }
413 
test_Long_left_constant_v()414   public static void test_Long_left_constant_v() throws Exception {
415     assertLongEquals($noinline$rotate_long_left_constant_0(0x11), 0x11);
416     assertLongEquals($noinline$rotate_long_left_constant_1(0x11), 0x22);
417     assertLongEquals($noinline$rotate_long_left_constant_m1(0x11), 0x8000000000000008L);
418     assertLongEquals($noinline$rotate_long_left_constant_16(0x11), 0x110000L);
419     assertLongEquals($noinline$rotate_long_left_constant_32(0x11), 0x1100000000L);
420     assertLongEquals($noinline$rotate_long_left_constant_48(0x11), 0x11000000000000L);
421   }
422 
423 }
424