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