1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8283894
* @key randomness
* @summary To test various transforms added for bit COMPRESS_BITS and EXPAND_BITS operations
* @requires vm.compiler2.enabled
* @requires (((os.arch=="x86" | os.arch=="amd64" | os.arch=="x86_64") &
* (vm.cpu.features ~= ".*bmi2.*" & vm.cpu.features ~= ".*bmi1.*" &
* vm.cpu.features ~= ".*sse2.*")) |
* (os.arch=="aarch64" & vm.cpu.features ~= ".*svebitperm.*"))
* @library /test/lib /
* @run driver compiler.intrinsics.TestBitShuffleOpers
*/
package compiler.intrinsics;
import java.util.concurrent.Callable;
import compiler.lib.ir_framework.*;
import jdk.test.lib.Utils;
import java.util.Random;
public class TestBitShuffleOpers {
int [] ri;
int [] ai;
int [] bi;
long [] rl;
long [] al;
long [] bl;
//===================== Compress Bits Transforms ================
@Test
@IR(counts = {IRNode.RSHIFT_I, " > 0 ", IRNode.AND_I, " > 0"})
public void test1(int[] ri, int[] ai, int[] bi) {
for (int i = 0; i < ri.length; i++) {
ri[i] = Integer.compress(ai[i], 1 << bi[i]);
}
}
@Run(test = {"test1"}, mode = RunMode.STANDALONE)
public void kernel_test1() {
for (int i = 0; i < 5000; i++) {
test1(ri, ai, bi);
}
}
@Test
@IR(counts = {IRNode.URSHIFT_I, " > 0 "})
public void test2(int[] ri, int[] ai, int[] bi) {
for (int i = 0; i < ri.length; i++) {
ri[i] = Integer.compress(ai[i], -1 << bi[i]);
}
}
@Run(test = {"test2"}, mode = RunMode.STANDALONE)
public void kernel_test2() {
for (int i = 0; i < 5000; i++) {
test2(ri, ai, bi);
}
}
@Test
@IR(counts = {IRNode.COMPRESS_BITS, " > 0 ", IRNode.AND_I , " > 0 "})
public void test3(int[] ri, int[] ai, int[] bi) {
for (int i = 0; i < ri.length; i++) {
ri[i] = Integer.compress(Integer.expand(ai[i], bi[i]), bi[i]);
}
}
@Run(test = {"test3"}, mode = RunMode.STANDALONE)
public void kernel_test3() {
for (int i = 0; i < 5000; i++) {
test3(ri, ai, bi);
}
}
@Test
@IR(counts = {IRNode.RSHIFT_L, " > 0 ", IRNode.AND_L, " > 0"})
public void test4(long[] rl, long[] al, long[] bl) {
for (int i = 0; i < rl.length; i++) {
rl[i] = Long.compress(al[i], 1L << bl[i]);
}
}
@Run(test = {"test4"}, mode = RunMode.STANDALONE)
public void kernel_test4() {
for (int i = 0; i < 5000; i++) {
test4(rl, al, bl);
}
}
@Test
@IR(counts = {IRNode.URSHIFT_L, " > 0 "})
public void test5(long[] rl, long[] al, long[] bl) {
for (int i = 0; i < rl.length; i++) {
rl[i] = Long.compress(al[i], -1L << bl[i]);
}
}
@Run(test = {"test5"}, mode = RunMode.STANDALONE)
public void kernel_test5() {
for (int i = 0; i < 5000; i++) {
test5(rl, al, bl);
}
}
@Test
@IR(counts = {IRNode.COMPRESS_BITS, " > 0 ", IRNode.AND_L , " > 0 "})
public void test6(long[] rl, long[] al, long[] bl) {
for (int i = 0; i < rl.length; i++) {
rl[i] = Long.compress(Long.expand(al[i], bl[i]), bl[i]);
}
}
@Run(test = {"test6"}, mode = RunMode.STANDALONE)
public void kernel_test6() {
for (int i = 0; i < 5000; i++) {
test6(rl, al, bl);
}
}
//===================== Expand Bits Transforms ================
@Test
@IR(counts = {IRNode.LSHIFT_I, " > 0 ", IRNode.AND_I, " > 0"})
public void test7(int[] ri, int[] ai, int[] bi) {
for (int i = 0; i < ri.length; i++) {
ri[i] = Integer.expand(ai[i], 1 << bi[i]);
}
}
@Run(test = {"test7"}, mode = RunMode.STANDALONE)
public void kernel_test7() {
for (int i = 0; i < 5000; i++) {
test7(ri, ai, bi);
}
}
@Test
@IR(counts = {IRNode.LSHIFT_I, " > 0 "})
public void test8(int[] ri, int[] ai, int[] bi) {
for (int i = 0; i < ri.length; i++) {
ri[i] = Integer.expand(ai[i], -1 << bi[i]);
}
}
@Run(test = {"test8"}, mode = RunMode.STANDALONE)
public void kernel_test8() {
for (int i = 0; i < 5000; i++) {
test8(ri, ai, bi);
}
}
@Test
@IR(counts = {IRNode.AND_I , " > 0 "})
public void test9(int[] ri, int[] ai, int[] bi) {
for (int i = 0; i < ri.length; i++) {
ri[i] = Integer.expand(Integer.compress(ai[i], bi[i]), bi[i]);
}
}
@Run(test = {"test9"}, mode = RunMode.STANDALONE)
public void kernel_test9() {
for (int i = 0; i < 5000; i++) {
test9(ri, ai, bi);
}
}
@Test
@IR(counts = {IRNode.LSHIFT_L, " > 0 ", IRNode.AND_L, " > 0"})
public void test10(long[] rl, long[] al, long[] bl) {
for (int i = 0; i < rl.length; i++) {
rl[i] = Long.expand(al[i], 1L << bl[i]);
}
}
@Run(test = {"test10"}, mode = RunMode.STANDALONE)
public void kernel_test10() {
for (int i = 0; i < 5000; i++) {
test10(rl, al, bl);
}
}
@Test
@IR(counts = {IRNode.LSHIFT_L, " > 0 "})
public void test11(long[] rl, long[] al, long[] bl) {
for (int i = 0; i < rl.length; i++) {
rl[i] = Long.expand(al[i], -1L << bl[i]);
}
}
@Run(test = {"test11"}, mode = RunMode.STANDALONE)
public void kernel_test11() {
for (int i = 0; i < 5000; i++) {
test11(rl, al, bl);
}
}
@Test
@IR(counts = {IRNode.AND_L , " > 0 "})
public void test12(long[] rl, long[] al, long[] bl) {
for (int i = 0; i < rl.length; i++) {
rl[i] = Long.expand(Long.compress(al[i], bl[i]), bl[i]);
}
}
@Run(test = {"test12"}, mode = RunMode.STANDALONE)
public void kernel_test12() {
for (int i = 0; i < 5000; i++) {
test12(rl, al, bl);
}
}
// ================ Compress/ExpandBits Vanilla ================= //
@Test
@IR(counts = {IRNode.COMPRESS_BITS, " > 0 "})
public void test13(int[] ri, int[] ai, int[] bi) {
for (int i = 0; i < ri.length; i++) {
ri[i] = Integer.compress(ai[i], bi[i]);
}
}
@Run(test = {"test13"}, mode = RunMode.STANDALONE)
public void kernel_test13() {
for (int i = 0; i < 5000; i++) {
test13(ri, ai, bi);
}
verifyCompressInts(ri, ai, bi);
}
@Test
@IR(counts = {IRNode.COMPRESS_BITS, " > 0 "})
public void test14(long[] rl, long[] al, long[] bl) {
for (int i = 0; i < rl.length; i++) {
rl[i] = Long.compress(al[i], bl[i]);
}
}
@Run(test = {"test14"}, mode = RunMode.STANDALONE)
public void kernel_test14() {
for (int i = 0; i < 5000; i++) {
test14(rl, al, bl);
}
verifyCompressLongs(rl, al, bl);
}
@Test
@IR(counts = {IRNode.EXPAND_BITS, " > 0 "})
public void test15(int[] ri, int[] ai, int[] bi) {
for (int i = 0; i < ri.length; i++) {
ri[i] = Integer.expand(ai[i], bi[i]);
}
}
@Run(test = {"test15"}, mode = RunMode.STANDALONE)
public void kernel_test15() {
for (int i = 0; i < 5000; i++) {
test15(ri, ai, bi);
}
verifyExpandInts(ri, ai, bi);
}
@Test
@IR(counts = {IRNode.EXPAND_BITS, " > 0 "})
public void test16(long[] rl, long[] al, long[] bl) {
for (int i = 0; i < rl.length; i++) {
rl[i] = Long.expand(al[i], bl[i]);
}
}
@Run(test = {"test16"}, mode = RunMode.STANDALONE)
public void kernel_test16() {
for (int i = 0; i < 5000; i++) {
test16(rl, al, bl);
}
verifyExpandLongs(rl, al, bl);
}
@Test
public void test17() {
int resI = 0;
long resL = 0L;
for (int i = 0; i < 5000; i++) {
resI = Integer.expand(-1, -1);
verifyExpandInt(resI, -1, -1);
resI = Integer.compress(-1, -1);
verifyCompressInt(resI, -1, -1);
resI = Integer.expand(ai[i&(SIZE-1)], -1);
verifyExpandInt(resI, ai[i&(SIZE-1)], -1);
resI = Integer.expand(ai[i&(SIZE-1)], -2);
verifyExpandInt(resI, ai[i&(SIZE-1)], -2);
resI = Integer.expand(ai[i&(SIZE-1)], 5);
verifyExpandInt(resI, ai[i&(SIZE-1)], 5);
resI = Integer.compress(ai[i&(SIZE-1)], -1);
verifyCompressInt(resI, ai[i&(SIZE-1)], -1);
resI = Integer.compress(ai[i&(SIZE-1)], -2);
verifyCompressInt(resI, ai[i&(SIZE-1)], -2);
resI = Integer.compress(ai[i&(SIZE-1)], 5);
verifyCompressInt(resI, ai[i&(SIZE-1)], 5);
resI = Integer.expand(ai[i&(SIZE-1)], bi[i&(SIZE-1)] & ~(0x10000000));
verifyExpandInt(resI, ai[i&(SIZE-1)], bi[i&(SIZE-1)] & ~(0x10000000));
resI = Integer.expand(ai[i&(SIZE-1)], bi[i&(SIZE-1)] | (0x10000000));
verifyExpandInt(resI, ai[i&(SIZE-1)], bi[i&(SIZE-1)] | (0x10000000));
resI = Integer.compress(ai[i&(SIZE-1)], bi[i&(SIZE-1)] & ~(0x10000000));
verifyCompressInt(resI, ai[i&(SIZE-1)], bi[i&(SIZE-1)] & ~(0x10000000));
resI = Integer.compress(ai[i&(SIZE-1)], bi[i&(SIZE-1)] | (0x10000000));
verifyCompressInt(resI, ai[i&(SIZE-1)], bi[i&(SIZE-1)] | (0x10000000));
resI = Integer.compress(0x12123434, 0xFF00FF00);
verifyCompressInt(resI, 0x12123434, 0xFF00FF00);
resI = Integer.expand(0x12123434, 0xFF00FF00);
verifyExpandInt(resI, 0x12123434, 0xFF00FF00);
resL = Long.expand(-1L, -1L);
verifyExpandLong(resL, -1L, -1L);
resL = Long.compress(-1L, -1L);
verifyCompressLong(resL, -1L, -1L);
resL = Long.compress(0x1212343412123434L, 0xFF00FF00FF00FF00L);
verifyCompressLong(resL, 0x1212343412123434L, 0xFF00FF00FF00FF00L);
resL = Long.expand(0x1212343412123434L, 0xFF00FF00FF00FF00L);
verifyExpandLong(resL, 0x1212343412123434L, 0xFF00FF00FF00FF00L);
resL = Long.expand(al[i&(SIZE-1)], -1);
verifyExpandLong(resL, al[i&(SIZE-1)], -1);
resL = Long.expand(al[i&(SIZE-1)], -2);
verifyExpandLong(resL, al[i&(SIZE-1)], -2);
resL = Long.expand(al[i&(SIZE-1)], 5);
verifyExpandLong(resL, al[i&(SIZE-1)], 5);
resL = Long.compress(al[i&(SIZE-1)], -1);
verifyCompressLong(resL, al[i&(SIZE-1)], -1);
resL = Long.compress(al[i&(SIZE-1)], -2);
verifyCompressLong(resL, al[i&(SIZE-1)], -2);
resL = Long.compress(al[i&(SIZE-1)], 5);
verifyCompressLong(resL, al[i&(SIZE-1)], 5);
resL = Long.expand(al[i&(SIZE-1)], bl[i&(SIZE-1)] & ~(0x10000000));
verifyExpandLong(resL, al[i&(SIZE-1)], bl[i&(SIZE-1)] & ~(0x10000000));
resL = Long.expand(al[i&(SIZE-1)], bl[i&(SIZE-1)] | (0x10000000));
verifyExpandLong(resL, al[i&(SIZE-1)], bl[i&(SIZE-1)] | (0x10000000));
resL = Long.compress(al[i&(SIZE-1)], bl[i&(SIZE-1)] & ~(0x10000000));
verifyCompressLong(resL, al[i&(SIZE-1)], bl[i&(SIZE-1)] & ~(0x10000000));
resL = Long.compress(al[i&(SIZE-1)], bl[i&(SIZE-1)] | (0x10000000));
verifyCompressLong(resL, al[i&(SIZE-1)], bl[i&(SIZE-1)] | (0x10000000));
}
}
private static final Random R = Utils.getRandomInstance();
static int[] fillIntRandom(Callable<int[]> factory) {
try {
int[] arr = factory.call();
for (int i = 0; i < arr.length; i++) {
arr[i] = R.nextInt();
}
return arr;
} catch (Exception e) {
throw new InternalError(e);
}
}
static long[] fillLongRandom(Callable<long[]> factory) {
try {
long[] arr = factory.call();
for (int i = 0; i < arr.length; i++) {
arr[i] = R.nextLong();
}
return arr;
} catch (Exception e) {
throw new InternalError(e);
}
}
static void verifyExpandInt(int actual, int src, int mask) {
int exp = 0;
for(int j = 0, k = 0; j < Integer.SIZE; j++) {
if ((mask & 0x1) == 1) {
exp |= (src & 0x1) << j;
src >>= 1;
}
mask >>= 1;
}
if (actual != exp) {
throw new Error("expand_int: src = " + src + " mask = " + mask +
" acutal = " + actual + " expected = " + exp);
}
}
static void verifyExpandInts(int [] actual_res, int [] inp_arr, int [] mask_arr) {
assert inp_arr.length == mask_arr.length && inp_arr.length == actual_res.length;
for(int i = 0; i < actual_res.length; i++) {
verifyExpandInt(actual_res[i], inp_arr[i], mask_arr[i]);
}
}
static void verifyExpandLong(long actual, long src, long mask) {
long exp = 0;
for(int j = 0, k = 0; j < Long.SIZE; j++) {
if ((mask & 0x1) == 1) {
exp |= (src & 0x1) << j;
src >>= 1;
}
mask >>= 1;
}
if (actual != exp) {
throw new Error("expand_long: src = " + src + " mask = " + mask +
" acutal = " + actual + " expected = " + exp);
}
}
static void verifyExpandLongs(long [] actual_res, long [] inp_arr, long [] mask_arr) {
assert inp_arr.length == mask_arr.length && inp_arr.length == actual_res.length;
for(int i = 0; i < actual_res.length; i++) {
verifyExpandLong(actual_res[i], inp_arr[i], mask_arr[i]);
}
}
static void verifyCompressInt(int actual, int src, int mask) {
int exp = 0;
for(int j = 0, k = 0; j < Integer.SIZE; j++) {
if ((mask & 0x1) == 1) {
exp |= (src & 0x1) << k++;
}
mask >>= 1;
src >>= 1;
}
if (actual != exp) {
throw new Error("compress_int: src = " + src + " mask = " + mask +
" acutal = " + actual + " expected = " + exp);
}
}
static void verifyCompressInts(int [] actual_res, int [] inp_arr, int [] mask_arr) {
assert inp_arr.length == mask_arr.length && inp_arr.length == actual_res.length;
for(int i = 0; i < actual_res.length; i++) {
verifyCompressInt(actual_res[i], inp_arr[i], mask_arr[i]);
}
}
static void verifyCompressLong(long actual, long src, long mask) {
long exp = 0;
for(int j = 0, k = 0; j < Long.SIZE; j++) {
if ((mask & 0x1) == 1) {
exp |= (src & 0x1) << k++;
}
mask >>= 1;
src >>= 1;
}
if (actual != exp) {
throw new Error("compress_long: src = " + src + " mask = " + mask +
" acutal = " + actual + " expected = " + exp);
}
}
static void verifyCompressLongs(long [] actual_res, long [] inp_arr, long [] mask_arr) {
assert inp_arr.length == mask_arr.length && inp_arr.length == actual_res.length;
for(int i = 0; i < actual_res.length; i++) {
verifyCompressLong(actual_res[i], inp_arr[i], mask_arr[i]);
}
}
// ===================================================== //
static final int SIZE = 512;
public TestBitShuffleOpers() {
ri = new int[SIZE];
ai = fillIntRandom(()-> new int[SIZE]);
bi = fillIntRandom(()-> new int[SIZE]);
rl = new long[SIZE];
al = fillLongRandom(() -> new long[SIZE]);
bl = fillLongRandom(() -> new long[SIZE]);
}
public static void main(String[] args) {
TestFramework.runWithFlags("-XX:-TieredCompilation",
"-XX:CompileThresholdScaling=0.3");
}
}
|