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 17 import java.lang.reflect.Method; 18 19 public class Main { 20 21 private static Class main2; 22 23 /// CHECK-START: int Main.rotateLeftByte(byte, int) builder (after) 24 /// CHECK: <<ArgVal:b\d+>> ParameterValue 25 /// CHECK: <<ArgDist:i\d+>> ParameterValue 26 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateLeft 27 /// CHECK-DAG: Return [<<Result>>] 28 29 /// CHECK-START: int Main.rotateLeftByte(byte, int) instruction_simplifier (after) 30 /// CHECK: <<ArgVal:b\d+>> ParameterValue 31 /// CHECK: <<ArgDist:i\d+>> ParameterValue 32 /// CHECK-DAG: <<NegDist:i\d+>> Neg [<<ArgDist>>] 33 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<NegDist>>] 34 /// CHECK-DAG: Return [<<Result>>] 35 36 /// CHECK-START: int Main.rotateLeftByte(byte, int) instruction_simplifier (after) 37 /// CHECK-NOT: InvokeStaticOrDirect 38 rotateLeftByte(byte value, int distance)39 private static int rotateLeftByte(byte value, int distance) { 40 return Integer.rotateLeft(value, distance); 41 } 42 43 /// CHECK-START: int Main.rotateLeftShort(short, int) builder (after) 44 /// CHECK: <<ArgVal:s\d+>> ParameterValue 45 /// CHECK: <<ArgDist:i\d+>> ParameterValue 46 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateLeft 47 /// CHECK-DAG: Return [<<Result>>] 48 49 /// CHECK-START: int Main.rotateLeftShort(short, int) instruction_simplifier (after) 50 /// CHECK: <<ArgVal:s\d+>> ParameterValue 51 /// CHECK: <<ArgDist:i\d+>> ParameterValue 52 /// CHECK-DAG: <<NegDist:i\d+>> Neg [<<ArgDist>>] 53 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<NegDist>>] 54 /// CHECK-DAG: Return [<<Result>>] 55 56 /// CHECK-START: int Main.rotateLeftShort(short, int) instruction_simplifier (after) 57 /// CHECK-NOT: InvokeStaticOrDirect 58 rotateLeftShort(short value, int distance)59 private static int rotateLeftShort(short value, int distance) { 60 return Integer.rotateLeft(value, distance); 61 } 62 63 /// CHECK-START: int Main.rotateLeftChar(char, int) builder (after) 64 /// CHECK: <<ArgVal:c\d+>> ParameterValue 65 /// CHECK: <<ArgDist:i\d+>> ParameterValue 66 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateLeft 67 /// CHECK-DAG: Return [<<Result>>] 68 69 /// CHECK-START: int Main.rotateLeftChar(char, int) instruction_simplifier (after) 70 /// CHECK: <<ArgVal:c\d+>> ParameterValue 71 /// CHECK: <<ArgDist:i\d+>> ParameterValue 72 /// CHECK-DAG: <<NegDist:i\d+>> Neg [<<ArgDist>>] 73 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<NegDist>>] 74 /// CHECK-DAG: Return [<<Result>>] 75 76 /// CHECK-START: int Main.rotateLeftChar(char, int) instruction_simplifier (after) 77 /// CHECK-NOT: InvokeStaticOrDirect 78 rotateLeftChar(char value, int distance)79 private static int rotateLeftChar(char value, int distance) { 80 return Integer.rotateLeft(value, distance); 81 } 82 83 /// CHECK-START: int Main.rotateLeftInt(int, int) builder (after) 84 /// CHECK: <<ArgVal:i\d+>> ParameterValue 85 /// CHECK: <<ArgDist:i\d+>> ParameterValue 86 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateLeft 87 /// CHECK-DAG: Return [<<Result>>] 88 89 /// CHECK-START: int Main.rotateLeftInt(int, int) instruction_simplifier (after) 90 /// CHECK: <<ArgVal:i\d+>> ParameterValue 91 /// CHECK: <<ArgDist:i\d+>> ParameterValue 92 /// CHECK-DAG: <<NegDist:i\d+>> Neg [<<ArgDist>>] 93 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<NegDist>>] 94 /// CHECK-DAG: Return [<<Result>>] 95 96 /// CHECK-START: int Main.rotateLeftInt(int, int) instruction_simplifier (after) 97 /// CHECK-NOT: InvokeStaticOrDirect 98 rotateLeftInt(int value, int distance)99 private static int rotateLeftInt(int value, int distance) { 100 return Integer.rotateLeft(value, distance); 101 } 102 103 /// CHECK-START: long Main.rotateLeftLong(long, int) builder (after) 104 /// CHECK: <<ArgVal:j\d+>> ParameterValue 105 /// CHECK: <<ArgDist:i\d+>> ParameterValue 106 /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:LongRotateLeft 107 /// CHECK-DAG: Return [<<Result>>] 108 109 /// CHECK-START: long Main.rotateLeftLong(long, int) instruction_simplifier (after) 110 /// CHECK: <<ArgVal:j\d+>> ParameterValue 111 /// CHECK: <<ArgDist:i\d+>> ParameterValue 112 /// CHECK-DAG: <<NegDist:i\d+>> Neg [<<ArgDist>>] 113 /// CHECK-DAG: <<Result:j\d+>> Ror [<<ArgVal>>,<<NegDist>>] 114 /// CHECK-DAG: Return [<<Result>>] 115 116 /// CHECK-START: long Main.rotateLeftLong(long, int) instruction_simplifier (after) 117 /// CHECK-NOT: InvokeStaticOrDirect 118 rotateLeftLong(long value, int distance)119 private static long rotateLeftLong(long value, int distance) { 120 return Long.rotateLeft(value, distance); 121 } 122 123 /// CHECK-START: int Main.rotateRightByte(byte, int) builder (after) 124 /// CHECK: <<ArgVal:b\d+>> ParameterValue 125 /// CHECK: <<ArgDist:i\d+>> ParameterValue 126 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateRight 127 /// CHECK-DAG: Return [<<Result>>] 128 129 /// CHECK-START: int Main.rotateRightByte(byte, int) instruction_simplifier (after) 130 /// CHECK: <<ArgVal:b\d+>> ParameterValue 131 /// CHECK: <<ArgDist:i\d+>> ParameterValue 132 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<ArgDist>>] 133 /// CHECK-DAG: Return [<<Result>>] 134 135 /// CHECK-START: int Main.rotateRightByte(byte, int) instruction_simplifier (after) 136 /// CHECK-NOT: InvokeStaticOrDirect 137 rotateRightByte(byte value, int distance)138 private static int rotateRightByte(byte value, int distance) { 139 return Integer.rotateRight(value, distance); 140 } 141 142 /// CHECK-START: int Main.rotateRightShort(short, int) builder (after) 143 /// CHECK: <<ArgVal:s\d+>> ParameterValue 144 /// CHECK: <<ArgDist:i\d+>> ParameterValue 145 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateRight 146 /// CHECK-DAG: Return [<<Result>>] 147 148 /// CHECK-START: int Main.rotateRightShort(short, int) instruction_simplifier (after) 149 /// CHECK: <<ArgVal:s\d+>> ParameterValue 150 /// CHECK: <<ArgDist:i\d+>> ParameterValue 151 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<ArgDist>>] 152 /// CHECK-DAG: Return [<<Result>>] 153 154 /// CHECK-START: int Main.rotateRightShort(short, int) instruction_simplifier (after) 155 /// CHECK-NOT: InvokeStaticOrDirect 156 rotateRightShort(short value, int distance)157 private static int rotateRightShort(short value, int distance) { 158 return Integer.rotateRight(value, distance); 159 } 160 161 /// CHECK-START: int Main.rotateRightChar(char, int) builder (after) 162 /// CHECK: <<ArgVal:c\d+>> ParameterValue 163 /// CHECK: <<ArgDist:i\d+>> ParameterValue 164 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateRight 165 /// CHECK-DAG: Return [<<Result>>] 166 167 /// CHECK-START: int Main.rotateRightChar(char, int) instruction_simplifier (after) 168 /// CHECK: <<ArgVal:c\d+>> ParameterValue 169 /// CHECK: <<ArgDist:i\d+>> ParameterValue 170 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<ArgDist>>] 171 /// CHECK-DAG: Return [<<Result>>] 172 173 /// CHECK-START: int Main.rotateRightChar(char, int) instruction_simplifier (after) 174 /// CHECK-NOT: InvokeStaticOrDirect 175 rotateRightChar(char value, int distance)176 private static int rotateRightChar(char value, int distance) { 177 return Integer.rotateRight(value, distance); 178 } 179 180 /// CHECK-START: int Main.rotateRightInt(int, int) builder (after) 181 /// CHECK: <<ArgVal:i\d+>> ParameterValue 182 /// CHECK: <<ArgDist:i\d+>> ParameterValue 183 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateRight 184 /// CHECK-DAG: Return [<<Result>>] 185 186 /// CHECK-START: int Main.rotateRightInt(int, int) instruction_simplifier (after) 187 /// CHECK: <<ArgVal:i\d+>> ParameterValue 188 /// CHECK: <<ArgDist:i\d+>> ParameterValue 189 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<ArgDist>>] 190 /// CHECK-DAG: Return [<<Result>>] 191 192 /// CHECK-START: int Main.rotateRightInt(int, int) instruction_simplifier (after) 193 /// CHECK-NOT: InvokeStaticOrDirect 194 rotateRightInt(int value, int distance)195 private static int rotateRightInt(int value, int distance) { 196 return Integer.rotateRight(value, distance); 197 } 198 199 /// CHECK-START: long Main.rotateRightLong(long, int) builder (after) 200 /// CHECK: <<ArgVal:j\d+>> ParameterValue 201 /// CHECK: <<ArgDist:i\d+>> ParameterValue 202 /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:LongRotateRight 203 /// CHECK-DAG: Return [<<Result>>] 204 205 /// CHECK-START: long Main.rotateRightLong(long, int) instruction_simplifier (after) 206 /// CHECK: <<ArgVal:j\d+>> ParameterValue 207 /// CHECK: <<ArgDist:i\d+>> ParameterValue 208 /// CHECK-DAG: <<Result:j\d+>> Ror [<<ArgVal>>,<<ArgDist>>] 209 /// CHECK-DAG: Return [<<Result>>] 210 211 /// CHECK-START: long Main.rotateRightLong(long, int) instruction_simplifier (after) 212 /// CHECK-NOT: InvokeStaticOrDirect 213 rotateRightLong(long value, int distance)214 private static long rotateRightLong(long value, int distance) { 215 return Long.rotateRight(value, distance); 216 } 217 218 219 /// CHECK-START: int Main.rotateLeftIntWithByteDistance(int, byte) builder (after) 220 /// CHECK: <<ArgVal:i\d+>> ParameterValue 221 /// CHECK: <<ArgDist:b\d+>> ParameterValue 222 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateLeft 223 /// CHECK-DAG: Return [<<Result>>] 224 225 /// CHECK-START: int Main.rotateLeftIntWithByteDistance(int, byte) instruction_simplifier (after) 226 /// CHECK: <<ArgVal:i\d+>> ParameterValue 227 /// CHECK: <<ArgDist:b\d+>> ParameterValue 228 /// CHECK-DAG: <<NegDist:i\d+>> Neg [<<ArgDist>>] 229 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<NegDist>>] 230 /// CHECK-DAG: Return [<<Result>>] 231 232 /// CHECK-START: int Main.rotateLeftIntWithByteDistance(int, byte) instruction_simplifier (after) 233 /// CHECK-NOT: InvokeStaticOrDirect 234 rotateLeftIntWithByteDistance(int value, byte distance)235 private static int rotateLeftIntWithByteDistance(int value, byte distance) { 236 return Integer.rotateLeft(value, distance); 237 } 238 239 /// CHECK-START: int Main.rotateRightIntWithByteDistance(int, byte) builder (after) 240 /// CHECK: <<ArgVal:i\d+>> ParameterValue 241 /// CHECK: <<ArgDist:b\d+>> ParameterValue 242 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<ArgVal>>,<<ArgDist>>{{(,[ij]\d+)?}}] intrinsic:IntegerRotateRight 243 /// CHECK-DAG: Return [<<Result>>] 244 245 /// CHECK-START: int Main.rotateRightIntWithByteDistance(int, byte) instruction_simplifier (after) 246 /// CHECK: <<ArgVal:i\d+>> ParameterValue 247 /// CHECK: <<ArgDist:b\d+>> ParameterValue 248 /// CHECK-DAG: <<Result:i\d+>> Ror [<<ArgVal>>,<<ArgDist>>] 249 /// CHECK-DAG: Return [<<Result>>] 250 251 /// CHECK-START: int Main.rotateRightIntWithByteDistance(int, byte) instruction_simplifier (after) 252 /// CHECK-NOT: InvokeStaticOrDirect 253 rotateRightIntWithByteDistance(int value, byte distance)254 private static int rotateRightIntWithByteDistance(int value, byte distance) { 255 return Integer.rotateRight(value, distance); 256 } 257 258 testRotateLeftBoolean()259 public static void testRotateLeftBoolean() throws Exception { 260 Method rotateLeftBoolean = main2.getMethod("rotateLeftBoolean", boolean.class, int.class); 261 for (int i = 0; i < 40; i++) { // overshoot a bit 262 int j = i & 31; 263 expectEqualsInt(0, (int)rotateLeftBoolean.invoke(null, false, i)); 264 expectEqualsInt(1 << i, (int)rotateLeftBoolean.invoke(null, true, i)); 265 } 266 } 267 testRotateLeftByte()268 public static void testRotateLeftByte() { 269 expectEqualsInt(0x00000001, rotateLeftByte((byte)0x01, 0)); 270 expectEqualsInt(0x00000002, rotateLeftByte((byte)0x01, 1)); 271 expectEqualsInt(0x80000000, rotateLeftByte((byte)0x01, 31)); 272 expectEqualsInt(0x00000001, rotateLeftByte((byte)0x01, 32)); // overshoot 273 expectEqualsInt(0xFFFFFF03, rotateLeftByte((byte)0x81, 1)); 274 expectEqualsInt(0xFFFFFE07, rotateLeftByte((byte)0x81, 2)); 275 expectEqualsInt(0x00000120, rotateLeftByte((byte)0x12, 4)); 276 expectEqualsInt(0xFFFF9AFF, rotateLeftByte((byte)0x9A, 8)); 277 for (int i = 0; i < 40; i++) { // overshoot a bit 278 int j = i & 31; 279 expectEqualsInt(0x00000000, rotateLeftByte((byte)0x0000, i)); 280 expectEqualsInt(0xFFFFFFFF, rotateLeftByte((byte)0xFFFF, i)); 281 expectEqualsInt((1 << j), rotateLeftByte((byte)0x0001, i)); 282 expectEqualsInt((0x12 << j) | (0x12 >>> -j), rotateLeftByte((byte)0x12, i)); 283 } 284 } 285 testRotateLeftShort()286 public static void testRotateLeftShort() { 287 expectEqualsInt(0x00000001, rotateLeftShort((short)0x0001, 0)); 288 expectEqualsInt(0x00000002, rotateLeftShort((short)0x0001, 1)); 289 expectEqualsInt(0x80000000, rotateLeftShort((short)0x0001, 31)); 290 expectEqualsInt(0x00000001, rotateLeftShort((short)0x0001, 32)); // overshoot 291 expectEqualsInt(0xFFFF0003, rotateLeftShort((short)0x8001, 1)); 292 expectEqualsInt(0xFFFE0007, rotateLeftShort((short)0x8001, 2)); 293 expectEqualsInt(0x00012340, rotateLeftShort((short)0x1234, 4)); 294 expectEqualsInt(0xFF9ABCFF, rotateLeftShort((short)0x9ABC, 8)); 295 for (int i = 0; i < 40; i++) { // overshoot a bit 296 int j = i & 31; 297 expectEqualsInt(0x00000000, rotateLeftShort((short)0x0000, i)); 298 expectEqualsInt(0xFFFFFFFF, rotateLeftShort((short)0xFFFF, i)); 299 expectEqualsInt((1 << j), rotateLeftShort((short)0x0001, i)); 300 expectEqualsInt((0x1234 << j) | (0x1234 >>> -j), rotateLeftShort((short)0x1234, i)); 301 } 302 } 303 testRotateLeftChar()304 public static void testRotateLeftChar() { 305 expectEqualsInt(0x00000001, rotateLeftChar((char)0x0001, 0)); 306 expectEqualsInt(0x00000002, rotateLeftChar((char)0x0001, 1)); 307 expectEqualsInt(0x80000000, rotateLeftChar((char)0x0001, 31)); 308 expectEqualsInt(0x00000001, rotateLeftChar((char)0x0001, 32)); // overshoot 309 expectEqualsInt(0x00010002, rotateLeftChar((char)0x8001, 1)); 310 expectEqualsInt(0x00020004, rotateLeftChar((char)0x8001, 2)); 311 expectEqualsInt(0x00012340, rotateLeftChar((char)0x1234, 4)); 312 expectEqualsInt(0x009ABC00, rotateLeftChar((char)0x9ABC, 8)); 313 expectEqualsInt(0x00FF0000, rotateLeftChar((char)0xFF00, 8)); 314 for (int i = 0; i < 40; i++) { // overshoot a bit 315 int j = i & 31; 316 expectEqualsInt(0x00000000, rotateLeftChar((char)0x0000, i)); 317 expectEqualsInt((1 << j), rotateLeftChar((char)0x0001, i)); 318 expectEqualsInt((0x1234 << j) | (0x1234 >>> -j), rotateLeftChar((char)0x1234, i)); 319 } 320 } 321 testRotateLeftInt()322 public static void testRotateLeftInt() { 323 expectEqualsInt(0x00000001, rotateLeftInt(0x00000001, 0)); 324 expectEqualsInt(0x00000002, rotateLeftInt(0x00000001, 1)); 325 expectEqualsInt(0x80000000, rotateLeftInt(0x00000001, 31)); 326 expectEqualsInt(0x00000001, rotateLeftInt(0x00000001, 32)); // overshoot 327 expectEqualsInt(0x00000003, rotateLeftInt(0x80000001, 1)); 328 expectEqualsInt(0x00000006, rotateLeftInt(0x80000001, 2)); 329 expectEqualsInt(0x23456781, rotateLeftInt(0x12345678, 4)); 330 expectEqualsInt(0xBCDEF09A, rotateLeftInt(0x9ABCDEF0, 8)); 331 for (int i = 0; i < 40; i++) { // overshoot a bit 332 int j = i & 31; 333 expectEqualsInt(0x00000000, rotateLeftInt(0x00000000, i)); 334 expectEqualsInt(0xFFFFFFFF, rotateLeftInt(0xFFFFFFFF, i)); 335 expectEqualsInt(1 << j, rotateLeftInt(0x00000001, i)); 336 expectEqualsInt((0x12345678 << j) | (0x12345678 >>> -j), rotateLeftInt(0x12345678, i)); 337 } 338 } 339 testRotateLeftLong()340 public static void testRotateLeftLong() { 341 expectEqualsLong(0x0000000000000001L, rotateLeftLong(0x0000000000000001L, 0)); 342 expectEqualsLong(0x0000000000000002L, rotateLeftLong(0x0000000000000001L, 1)); 343 expectEqualsLong(0x8000000000000000L, rotateLeftLong(0x0000000000000001L, 63)); 344 expectEqualsLong(0x0000000000000001L, rotateLeftLong(0x0000000000000001L, 64)); // overshoot 345 expectEqualsLong(0x0000000000000003L, rotateLeftLong(0x8000000000000001L, 1)); 346 expectEqualsLong(0x0000000000000006L, rotateLeftLong(0x8000000000000001L, 2)); 347 expectEqualsLong(0x23456789ABCDEF01L, rotateLeftLong(0x123456789ABCDEF0L, 4)); 348 expectEqualsLong(0x3456789ABCDEF012L, rotateLeftLong(0x123456789ABCDEF0L, 8)); 349 for (int i = 0; i < 70; i++) { // overshoot a bit 350 int j = i & 63; 351 expectEqualsLong(0x0000000000000000L, rotateLeftLong(0x0000000000000000L, i)); 352 expectEqualsLong(0xFFFFFFFFFFFFFFFFL, rotateLeftLong(0xFFFFFFFFFFFFFFFFL, i)); 353 expectEqualsLong(1L << j, rotateLeftLong(0x0000000000000001, i)); 354 expectEqualsLong((0x123456789ABCDEF0L << j) | (0x123456789ABCDEF0L >>> -j), 355 rotateLeftLong(0x123456789ABCDEF0L, i)); 356 } 357 } 358 testRotateRightBoolean()359 public static void testRotateRightBoolean() throws Exception { 360 Method rotateRightBoolean = main2.getMethod("rotateRightBoolean", boolean.class, int.class); 361 for (int i = 0; i < 40; i++) { // overshoot a bit 362 int j = i & 31; 363 expectEqualsInt(0, (int)rotateRightBoolean.invoke(null, false, i)); 364 expectEqualsInt(1 << (32 - i), (int)rotateRightBoolean.invoke(null, true, i)); 365 } 366 } 367 testRotateRightByte()368 public static void testRotateRightByte() { 369 expectEqualsInt(0xFFFFFF80, rotateRightByte((byte)0x80, 0)); 370 expectEqualsInt(0x7FFFFFC0, rotateRightByte((byte)0x80, 1)); 371 expectEqualsInt(0xFFFFFF01, rotateRightByte((byte)0x80, 31)); 372 expectEqualsInt(0xFFFFFF80, rotateRightByte((byte)0x80, 32)); // overshoot 373 expectEqualsInt(0xFFFFFFC0, rotateRightByte((byte)0x81, 1)); 374 expectEqualsInt(0x7FFFFFE0, rotateRightByte((byte)0x81, 2)); 375 expectEqualsInt(0x20000001, rotateRightByte((byte)0x12, 4)); 376 expectEqualsInt(0x9AFFFFFF, rotateRightByte((byte)0x9A, 8)); 377 for (int i = 0; i < 40; i++) { // overshoot a bit 378 int j = i & 31; 379 expectEqualsInt(0x00000000, rotateRightByte((byte)0x00, i)); 380 expectEqualsInt(0xFFFFFFFF, rotateRightByte((byte)0xFF, i)); 381 expectEqualsInt(1 << (32 - j), rotateRightByte((byte)0x01, i)); 382 expectEqualsInt((0x12 >>> j) | (0x12 << -j), rotateRightByte((byte)0x12, i)); 383 } 384 } 385 testRotateRightShort()386 public static void testRotateRightShort() { 387 expectEqualsInt(0xFFFF8000, rotateRightShort((short)0x8000, 0)); 388 expectEqualsInt(0x7FFFC000, rotateRightShort((short)0x8000, 1)); 389 expectEqualsInt(0xFFFF0001, rotateRightShort((short)0x8000, 31)); 390 expectEqualsInt(0xFFFF8000, rotateRightShort((short)0x8000, 32)); // overshoot 391 expectEqualsInt(0xFFFFC000, rotateRightShort((short)0x8001, 1)); 392 expectEqualsInt(0x7FFFE000, rotateRightShort((short)0x8001, 2)); 393 expectEqualsInt(0x40000123, rotateRightShort((short)0x1234, 4)); 394 expectEqualsInt(0xBCFFFF9A, rotateRightShort((short)0x9ABC, 8)); 395 for (int i = 0; i < 40; i++) { // overshoot a bit 396 int j = i & 31; 397 expectEqualsInt(0x00000000, rotateRightShort((short)0x0000, i)); 398 expectEqualsInt(0xFFFFFFFF, rotateRightShort((short)0xFFFF, i)); 399 expectEqualsInt(1 << (32 - j), rotateRightShort((short)0x0001, i)); 400 expectEqualsInt((0x1234 >>> j) | (0x1234 << -j), rotateRightShort((short)0x1234, i)); 401 } 402 } 403 testRotateRightChar()404 public static void testRotateRightChar() { 405 expectEqualsInt(0x00008000, rotateRightChar((char)0x8000, 0)); 406 expectEqualsInt(0x00004000, rotateRightChar((char)0x8000, 1)); 407 expectEqualsInt(0x00010000, rotateRightChar((char)0x8000, 31)); 408 expectEqualsInt(0x00008000, rotateRightChar((char)0x8000, 32)); // overshoot 409 expectEqualsInt(0x80004000, rotateRightChar((char)0x8001, 1)); 410 expectEqualsInt(0x40002000, rotateRightChar((char)0x8001, 2)); 411 expectEqualsInt(0x40000123, rotateRightChar((char)0x1234, 4)); 412 expectEqualsInt(0xBC00009A, rotateRightChar((char)0x9ABC, 8)); 413 for (int i = 0; i < 40; i++) { // overshoot a bit 414 int j = i & 31; 415 expectEqualsInt(0x00000000, rotateRightChar((char)0x0000, i)); 416 expectEqualsInt(1 << (32 - j), rotateRightChar((char)0x0001, i)); 417 expectEqualsInt((0x1234 >>> j) | (0x1234 << -j), rotateRightChar((char)0x1234, i)); 418 } 419 } 420 testRotateRightInt()421 public static void testRotateRightInt() { 422 expectEqualsInt(0x80000000, rotateRightInt(0x80000000, 0)); 423 expectEqualsInt(0x40000000, rotateRightInt(0x80000000, 1)); 424 expectEqualsInt(0x00000001, rotateRightInt(0x80000000, 31)); 425 expectEqualsInt(0x80000000, rotateRightInt(0x80000000, 32)); // overshoot 426 expectEqualsInt(0xC0000000, rotateRightInt(0x80000001, 1)); 427 expectEqualsInt(0x60000000, rotateRightInt(0x80000001, 2)); 428 expectEqualsInt(0x81234567, rotateRightInt(0x12345678, 4)); 429 expectEqualsInt(0xF09ABCDE, rotateRightInt(0x9ABCDEF0, 8)); 430 for (int i = 0; i < 40; i++) { // overshoot a bit 431 int j = i & 31; 432 expectEqualsInt(0x00000000, rotateRightInt(0x00000000, i)); 433 expectEqualsInt(0xFFFFFFFF, rotateRightInt(0xFFFFFFFF, i)); 434 expectEqualsInt(0x80000000 >>> j, rotateRightInt(0x80000000, i)); 435 expectEqualsInt((0x12345678 >>> j) | (0x12345678 << -j), rotateRightInt(0x12345678, i)); 436 } 437 } 438 testRotateRightLong()439 public static void testRotateRightLong() { 440 expectEqualsLong(0x8000000000000000L, rotateRightLong(0x8000000000000000L, 0)); 441 expectEqualsLong(0x4000000000000000L, rotateRightLong(0x8000000000000000L, 1)); 442 expectEqualsLong(0x0000000000000001L, rotateRightLong(0x8000000000000000L, 63)); 443 expectEqualsLong(0x8000000000000000L, rotateRightLong(0x8000000000000000L, 64)); // overshoot 444 expectEqualsLong(0xC000000000000000L, rotateRightLong(0x8000000000000001L, 1)); 445 expectEqualsLong(0x6000000000000000L, rotateRightLong(0x8000000000000001L, 2)); 446 expectEqualsLong(0x0123456789ABCDEFL, rotateRightLong(0x123456789ABCDEF0L, 4)); 447 expectEqualsLong(0xF0123456789ABCDEL, rotateRightLong(0x123456789ABCDEF0L, 8)); 448 for (int i = 0; i < 70; i++) { // overshoot a bit 449 int j = i & 63; 450 expectEqualsLong(0x0000000000000000L, rotateRightLong(0x0000000000000000L, i)); 451 expectEqualsLong(0xFFFFFFFFFFFFFFFFL, rotateRightLong(0xFFFFFFFFFFFFFFFFL, i)); 452 expectEqualsLong(0x8000000000000000L >>> j, rotateRightLong(0x8000000000000000L, i)); 453 expectEqualsLong((0x123456789ABCDEF0L >>> j) | (0x123456789ABCDEF0L << -j), 454 rotateRightLong(0x123456789ABCDEF0L, i)); 455 } 456 } 457 458 testRotateLeftIntWithByteDistance()459 public static void testRotateLeftIntWithByteDistance() { 460 expectEqualsInt(0x00000001, rotateLeftIntWithByteDistance(0x00000001, (byte)0)); 461 expectEqualsInt(0x00000002, rotateLeftIntWithByteDistance(0x00000001, (byte)1)); 462 expectEqualsInt(0x80000000, rotateLeftIntWithByteDistance(0x00000001, (byte)31)); 463 expectEqualsInt(0x00000001, rotateLeftIntWithByteDistance(0x00000001, (byte)32)); // overshoot 464 expectEqualsInt(0x00000003, rotateLeftIntWithByteDistance(0x80000001, (byte)1)); 465 expectEqualsInt(0x00000006, rotateLeftIntWithByteDistance(0x80000001, (byte)2)); 466 expectEqualsInt(0x23456781, rotateLeftIntWithByteDistance(0x12345678, (byte)4)); 467 expectEqualsInt(0xBCDEF09A, rotateLeftIntWithByteDistance(0x9ABCDEF0, (byte)8)); 468 for (byte i = 0; i < 40; i++) { // overshoot a bit 469 byte j = (byte)(i & 31); 470 expectEqualsInt(0x00000000, rotateLeftIntWithByteDistance(0x00000000, i)); 471 expectEqualsInt(0xFFFFFFFF, rotateLeftIntWithByteDistance(0xFFFFFFFF, i)); 472 expectEqualsInt(1 << j, rotateLeftIntWithByteDistance(0x00000001, i)); 473 expectEqualsInt((0x12345678 << j) | (0x12345678 >>> -j), 474 rotateLeftIntWithByteDistance(0x12345678, i)); 475 } 476 } 477 testRotateRightIntWithByteDistance()478 public static void testRotateRightIntWithByteDistance() { 479 expectEqualsInt(0x80000000, rotateRightIntWithByteDistance(0x80000000, (byte)0)); 480 expectEqualsInt(0x40000000, rotateRightIntWithByteDistance(0x80000000, (byte)1)); 481 expectEqualsInt(0x00000001, rotateRightIntWithByteDistance(0x80000000, (byte)31)); 482 expectEqualsInt(0x80000000, rotateRightIntWithByteDistance(0x80000000, (byte)32)); // overshoot 483 expectEqualsInt(0xC0000000, rotateRightIntWithByteDistance(0x80000001, (byte)1)); 484 expectEqualsInt(0x60000000, rotateRightIntWithByteDistance(0x80000001, (byte)2)); 485 expectEqualsInt(0x81234567, rotateRightIntWithByteDistance(0x12345678, (byte)4)); 486 expectEqualsInt(0xF09ABCDE, rotateRightIntWithByteDistance(0x9ABCDEF0, (byte)8)); 487 for (byte i = 0; i < 40; i++) { // overshoot a bit 488 byte j = (byte)(i & 31); 489 expectEqualsInt(0x00000000, rotateRightIntWithByteDistance(0x00000000, i)); 490 expectEqualsInt(0xFFFFFFFF, rotateRightIntWithByteDistance(0xFFFFFFFF, i)); 491 expectEqualsInt(0x80000000 >>> j, rotateRightIntWithByteDistance(0x80000000, i)); 492 expectEqualsInt((0x12345678 >>> j) | (0x12345678 << -j), 493 rotateRightIntWithByteDistance(0x12345678, i)); 494 } 495 } 496 497 main(String args[])498 public static void main(String args[]) throws Exception { 499 main2 = Class.forName("Main2"); 500 501 testRotateLeftBoolean(); 502 testRotateLeftByte(); 503 testRotateLeftShort(); 504 testRotateLeftChar(); 505 testRotateLeftInt(); 506 testRotateLeftLong(); 507 508 testRotateRightBoolean(); 509 testRotateRightByte(); 510 testRotateRightShort(); 511 testRotateRightChar(); 512 testRotateRightInt(); 513 testRotateRightLong(); 514 515 // Also exercise distance values with types other than int. 516 testRotateLeftIntWithByteDistance(); 517 testRotateRightIntWithByteDistance(); 518 519 System.out.println("passed"); 520 } 521 522 expectEqualsInt(int expected, int result)523 private static void expectEqualsInt(int expected, int result) { 524 if (expected != result) { 525 throw new Error("Expected: " + expected + ", found: " + result); 526 } 527 } 528 expectEqualsLong(long expected, long result)529 private static void expectEqualsLong(long expected, long result) { 530 if (expected != result) { 531 throw new Error("Expected: " + expected + ", found: " + result); 532 } 533 } 534 } 535