Main.java revision 6e74fa9db63908a4ad8aeb30bd0b6ff772f9f23d
1/*
2 * Copyright (C) 2016 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  /// CHECK-START: int Main.rotateLeft32(int, int) intrinsics_recognition (after)
20  /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerRotateLeft
21  /// CHECK-DAG:                 Return [<<Result>>]
22  private static int rotateLeft32(int x, int y) {
23    return Integer.rotateLeft(x, y);
24  }
25
26  /// CHECK-START: long Main.rotateLeft64(long, int) intrinsics_recognition (after)
27  /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:LongRotateLeft
28  /// CHECK-DAG:                 Return [<<Result>>]
29  private static long rotateLeft64(long x, int y) {
30    return Long.rotateLeft(x, y);
31  }
32
33  /// CHECK-START: int Main.rotateRight32(int, int) intrinsics_recognition (after)
34  /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerRotateRight
35  /// CHECK-DAG:                 Return [<<Result>>]
36  private static int rotateRight32(int x, int y) {
37    return Integer.rotateRight(x, y);
38  }
39
40  /// CHECK-START: long Main.rotateRight64(long, int) intrinsics_recognition (after)
41  /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:LongRotateRight
42  /// CHECK-DAG:                 Return [<<Result>>]
43  private static long rotateRight64(long x, int y) {
44    return Long.rotateRight(x, y);
45  }
46
47  public static void main(String args[]) {
48    expectEquals32(0x00000001, rotateLeft32(0x00000001, 0));
49    expectEquals32(0x00000002, rotateLeft32(0x00000001, 1));
50    expectEquals32(0x80000000, rotateLeft32(0x00000001, 31));
51    expectEquals32(0x00000001, rotateLeft32(0x00000001, 32));  // overshoot
52    expectEquals32(0x00000003, rotateLeft32(0x80000001, 1));
53    expectEquals32(0x00000006, rotateLeft32(0x80000001, 2));
54    expectEquals32(0x23456781, rotateLeft32(0x12345678, 4));
55    expectEquals32(0xBCDEF09A, rotateLeft32(0x9ABCDEF0, 8));
56    for (int i = 0; i < 40; i++) {  // overshoot a bit
57      int j = i & 31;
58      expectEquals32(0x00000000, rotateLeft32(0x00000000, i));
59      expectEquals32(0xFFFFFFFF, rotateLeft32(0xFFFFFFFF, i));
60      expectEquals32(1 << j, rotateLeft32(0x00000001, i));
61      expectEquals32((0x12345678 << j) | (0x12345678 >>> -j),
62                     rotateLeft32(0x12345678, i));
63    }
64
65    expectEquals64(0x0000000000000001L, rotateLeft64(0x0000000000000001L, 0));
66    expectEquals64(0x0000000000000002L, rotateLeft64(0x0000000000000001L, 1));
67    expectEquals64(0x8000000000000000L, rotateLeft64(0x0000000000000001L, 63));
68    expectEquals64(0x0000000000000001L, rotateLeft64(0x0000000000000001L, 64));  // overshoot
69    expectEquals64(0x0000000000000003L, rotateLeft64(0x8000000000000001L, 1));
70    expectEquals64(0x0000000000000006L, rotateLeft64(0x8000000000000001L, 2));
71    expectEquals64(0x23456789ABCDEF01L, rotateLeft64(0x123456789ABCDEF0L, 4));
72    expectEquals64(0x3456789ABCDEF012L, rotateLeft64(0x123456789ABCDEF0L, 8));
73    for (int i = 0; i < 70; i++) {  // overshoot a bit
74      int j = i & 63;
75      expectEquals64(0x0000000000000000L, rotateLeft64(0x0000000000000000L, i));
76      expectEquals64(0xFFFFFFFFFFFFFFFFL, rotateLeft64(0xFFFFFFFFFFFFFFFFL, i));
77      expectEquals64(1L << j, rotateLeft64(0x0000000000000001, i));
78      expectEquals64((0x123456789ABCDEF0L << j) | (0x123456789ABCDEF0L >>> -j),
79                     rotateLeft64(0x123456789ABCDEF0L, i));
80    }
81
82    expectEquals32(0x80000000, rotateRight32(0x80000000, 0));
83    expectEquals32(0x40000000, rotateRight32(0x80000000, 1));
84    expectEquals32(0x00000001, rotateRight32(0x80000000, 31));
85    expectEquals32(0x80000000, rotateRight32(0x80000000, 32));  // overshoot
86    expectEquals32(0xC0000000, rotateRight32(0x80000001, 1));
87    expectEquals32(0x60000000, rotateRight32(0x80000001, 2));
88    expectEquals32(0x81234567, rotateRight32(0x12345678, 4));
89    expectEquals32(0xF09ABCDE, rotateRight32(0x9ABCDEF0, 8));
90    for (int i = 0; i < 40; i++) {  // overshoot a bit
91      int j = i & 31;
92      expectEquals32(0x00000000, rotateRight32(0x00000000, i));
93      expectEquals32(0xFFFFFFFF, rotateRight32(0xFFFFFFFF, i));
94      expectEquals32(0x80000000 >>> j, rotateRight32(0x80000000, i));
95      expectEquals32((0x12345678 >>> j) | (0x12345678 << -j),
96                     rotateRight32(0x12345678, i));
97    }
98
99    expectEquals64(0x8000000000000000L, rotateRight64(0x8000000000000000L, 0));
100    expectEquals64(0x4000000000000000L, rotateRight64(0x8000000000000000L, 1));
101    expectEquals64(0x0000000000000001L, rotateRight64(0x8000000000000000L, 63));
102    expectEquals64(0x8000000000000000L, rotateRight64(0x8000000000000000L, 64));  // overshoot
103    expectEquals64(0xC000000000000000L, rotateRight64(0x8000000000000001L, 1));
104    expectEquals64(0x6000000000000000L, rotateRight64(0x8000000000000001L, 2));
105    expectEquals64(0x0123456789ABCDEFL, rotateRight64(0x123456789ABCDEF0L, 4));
106    expectEquals64(0xF0123456789ABCDEL, rotateRight64(0x123456789ABCDEF0L, 8));
107    for (int i = 0; i < 70; i++) {  // overshoot a bit
108      int j = i & 63;
109      expectEquals64(0x0000000000000000L, rotateRight64(0x0000000000000000L, i));
110      expectEquals64(0xFFFFFFFFFFFFFFFFL, rotateRight64(0xFFFFFFFFFFFFFFFFL, i));
111      expectEquals64(0x8000000000000000L >>> j, rotateRight64(0x8000000000000000L, i));
112      expectEquals64((0x123456789ABCDEF0L >>> j) | (0x123456789ABCDEF0L << -j),
113                     rotateRight64(0x123456789ABCDEF0L, i));
114    }
115
116    System.out.println("passed");
117  }
118
119  private static void expectEquals32(int expected, int result) {
120    if (expected != result) {
121      throw new Error("Expected: " + expected + ", found: " + result);
122    }
123  }
124  private static void expectEquals64(long expected, long result) {
125    if (expected != result) {
126      throw new Error("Expected: " + expected + ", found: " + result);
127    }
128  }
129}
130