1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @modules jdk.incubator.vector 27 * @run testng/othervm -ea -esa ByteMaxVectorTests 28 */ 29 30 import jdk.incubator.vector.Vector.Shape; 31 import jdk.incubator.vector.Vector.Species; 32 import jdk.incubator.vector.Vector; 33 34 import jdk.incubator.vector.ByteVector; 35 36 import org.testng.Assert; 37 import org.testng.annotations.DataProvider; 38 import org.testng.annotations.Test; 39 40 import java.lang.Integer; 41 import java.util.List; 42 import java.util.function.BiFunction; 43 import java.util.function.IntFunction; 44 import java.util.stream.Collectors; 45 import java.util.stream.Stream; 46 47 @Test 48 public class ByteMaxVectorTests extends AbstractVectorTest { 49 50 static final Species<Byte> SPECIES = 51 ByteVector.SPECIES_MAX; 52 53 static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100); 54 55 static Shape getMaxBit() { 56 return Shape.S_Max_BIT; 57 } 58 59 interface FUnOp { 60 byte apply(byte a); 61 } 62 63 static void assertArraysEquals(byte[] a, byte[] r, FUnOp f) { 64 int i = 0; 65 try { 66 for (; i < a.length; i++) { 67 Assert.assertEquals(r[i], f.apply(a[i])); 68 } 69 } catch (AssertionError e) { 70 Assert.assertEquals(r[i], f.apply(a[i]), "at index #" + i + ", input = " + a[i]); 71 } 72 } 73 74 static void assertArraysEquals(byte[] a, byte[] r, boolean[] mask, FUnOp f) { 75 int i = 0; 76 try { 77 for (; i < a.length; i++) { 78 Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? f.apply(a[i]) : a[i]); 79 } 80 } catch (AssertionError e) { 81 Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? f.apply(a[i]) : a[i], "at index #" + i + ", input = " + a[i] + ", mask = " + mask[i % SPECIES.length()]); 82 } 83 } 84 85 interface FReductionOp { 86 byte apply(byte[] a, int idx); 87 } 88 89 interface FReductionAllOp { 90 byte apply(byte[] a); 91 } 92 93 static void assertReductionArraysEquals(byte[] a, byte[] b, byte c, 94 FReductionOp f, FReductionAllOp fa) { 95 int i = 0; 96 try { 97 Assert.assertEquals(c, fa.apply(a)); 98 for (; i < a.length; i += SPECIES.length()) { 99 Assert.assertEquals(b[i], f.apply(a, i)); 100 } 101 } catch (AssertionError e) { 102 Assert.assertEquals(c, fa.apply(a), "Final result is incorrect!"); 103 Assert.assertEquals(b[i], f.apply(a, i), "at index #" + i); 104 } 105 } 106 107 interface FBoolReductionOp { 108 boolean apply(boolean[] a, int idx); 109 } 110 111 static void assertReductionBoolArraysEquals(boolean[] a, boolean[] b, FBoolReductionOp f) { 112 int i = 0; 113 try { 114 for (; i < a.length; i += SPECIES.length()) { 115 Assert.assertEquals(f.apply(a, i), b[i]); 116 } 117 } catch (AssertionError e) { 118 Assert.assertEquals(f.apply(a, i), b[i], "at index #" + i); 119 } 120 } 121 122 static void assertInsertArraysEquals(byte[] a, byte[] b, byte element, int index) { 123 int i = 0; 124 try { 125 for (; i < a.length; i += 1) { 126 if(i%SPECIES.length() == index) { 127 Assert.assertEquals(b[i], element); 128 } else { 129 Assert.assertEquals(b[i], a[i]); 130 } 131 } 132 } catch (AssertionError e) { 133 if (i%SPECIES.length() == index) { 134 Assert.assertEquals(b[i], element, "at index #" + i); 135 } else { 136 Assert.assertEquals(b[i], a[i], "at index #" + i); 137 } 138 } 139 } 140 141 static void assertRearrangeArraysEquals(byte[] a, byte[] r, int[] order, int vector_len) { 142 int i = 0, j = 0; 143 try { 144 for (; i < a.length; i += vector_len) { 145 for (j = 0; j < vector_len; j++) { 146 Assert.assertEquals(r[i+j], a[i+order[i+j]]); 147 } 148 } 149 } catch (AssertionError e) { 150 int idx = i + j; 151 Assert.assertEquals(r[i+j], a[i+order[i+j]], "at index #" + idx + ", input = " + a[i+order[i+j]]); 152 } 153 } 154 155 interface FBinOp { 156 byte apply(byte a, byte b); 157 } 158 159 interface FBinMaskOp { 160 byte apply(byte a, byte b, boolean m); 161 162 static FBinMaskOp lift(FBinOp f) { 163 return (a, b, m) -> m ? f.apply(a, b) : a; 164 } 165 } 166 167 static void assertArraysEquals(byte[] a, byte[] b, byte[] r, FBinOp f) { 168 int i = 0; 169 try { 170 for (; i < a.length; i++) { 171 Assert.assertEquals(r[i], f.apply(a[i], b[i])); 172 } 173 } catch (AssertionError e) { 174 Assert.assertEquals(f.apply(a[i], b[i]), r[i], "(" + a[i] + ", " + b[i] + ") at index #" + i); 175 } 176 } 177 178 static void assertArraysEquals(byte[] a, byte[] b, byte[] r, boolean[] mask, FBinOp f) { 179 assertArraysEquals(a, b, r, mask, FBinMaskOp.lift(f)); 180 } 181 182 static void assertArraysEquals(byte[] a, byte[] b, byte[] r, boolean[] mask, FBinMaskOp f) { 183 int i = 0; 184 try { 185 for (; i < a.length; i++) { 186 Assert.assertEquals(r[i], f.apply(a[i], b[i], mask[i % SPECIES.length()])); 187 } 188 } catch (AssertionError err) { 189 Assert.assertEquals(r[i], f.apply(a[i], b[i], mask[i % SPECIES.length()]), "at index #" + i + ", input1 = " + a[i] + ", input2 = " + b[i] + ", mask = " + mask[i % SPECIES.length()]); 190 } 191 } 192 193 static void assertShiftArraysEquals(byte[] a, byte[] b, byte[] r, FBinOp f) { 194 int i = 0; 195 int j = 0; 196 try { 197 for (; j < a.length; j += SPECIES.length()) { 198 for (i = 0; i < SPECIES.length(); i++) { 199 Assert.assertEquals(f.apply(a[i+j], b[j]), r[i+j]); 200 } 201 } 202 } catch (AssertionError e) { 203 Assert.assertEquals(f.apply(a[i+j], b[j]), r[i+j], "at index #" + i + ", " + j); 204 } 205 } 206 207 static void assertShiftArraysEquals(byte[] a, byte[] b, byte[] r, boolean[] mask, FBinOp f) { 208 assertShiftArraysEquals(a, b, r, mask, FBinMaskOp.lift(f)); 209 } 210 211 static void assertShiftArraysEquals(byte[] a, byte[] b, byte[] r, boolean[] mask, FBinMaskOp f) { 212 int i = 0; 213 int j = 0; 214 try { 215 for (; j < a.length; j += SPECIES.length()) { 216 for (i = 0; i < SPECIES.length(); i++) { 217 Assert.assertEquals(r[i+j], f.apply(a[i+j], b[j], mask[i])); 218 } 219 } 220 } catch (AssertionError err) { 221 Assert.assertEquals(r[i+j], f.apply(a[i+j], b[j], mask[i]), "at index #" + i + ", input1 = " + a[i+j] + ", input2 = " + b[j] + ", mask = " + mask[i]); 222 } 223 } 224 225 226 interface FBinArrayOp { 227 byte apply(byte[] a, int b); 228 } 229 230 static void assertArraysEquals(byte[] a, byte[] r, FBinArrayOp f) { 231 int i = 0; 232 try { 233 for (; i < a.length; i++) { 234 Assert.assertEquals(f.apply(a, i), r[i]); 235 } 236 } catch (AssertionError e) { 237 Assert.assertEquals(f.apply(a,i), r[i], "at index #" + i); 238 } 239 } 240 241 static final List<IntFunction<byte[]>> BYTE_GENERATORS = List.of( 242 withToString("byte[-i * 5]", (int s) -> { 243 return fill(s * 1000, 244 i -> (byte)(-i * 5)); 245 }), 246 withToString("byte[i * 5]", (int s) -> { 247 return fill(s * 1000, 248 i -> (byte)(i * 5)); 249 }), 250 withToString("byte[i + 1]", (int s) -> { 251 return fill(s * 1000, 252 i -> (((byte)(i + 1) == 0) ? 1 : (byte)(i + 1))); 253 }), 254 withToString("byte[cornerCaseValue(i)]", (int s) -> { 255 return fill(s * 1000, 256 i -> cornerCaseValue(i)); 257 }) 258 ); 259 260 // Create combinations of pairs 261 // @@@ Might be sensitive to order e.g. div by 0 262 static final List<List<IntFunction<byte[]>>> BYTE_GENERATOR_PAIRS = 263 Stream.of(BYTE_GENERATORS.get(0)). 264 flatMap(fa -> BYTE_GENERATORS.stream().skip(1).map(fb -> List.of(fa, fb))). 265 collect(Collectors.toList()); 266 267 @DataProvider 268 public Object[][] boolUnaryOpProvider() { 269 return BOOL_ARRAY_GENERATORS.stream(). 270 map(f -> new Object[]{f}). 271 toArray(Object[][]::new); 272 } 273 274 275 @DataProvider 276 public Object[][] byteBinaryOpProvider() { 277 return BYTE_GENERATOR_PAIRS.stream().map(List::toArray). 278 toArray(Object[][]::new); 279 } 280 281 @DataProvider 282 public Object[][] byteIndexedOpProvider() { 283 return BYTE_GENERATOR_PAIRS.stream().map(List::toArray). 284 toArray(Object[][]::new); 285 } 286 287 @DataProvider 288 public Object[][] byteBinaryOpMaskProvider() { 289 return BOOLEAN_MASK_GENERATORS.stream(). 290 flatMap(fm -> BYTE_GENERATOR_PAIRS.stream().map(lfa -> { 291 return Stream.concat(lfa.stream(), Stream.of(fm)).toArray(); 292 })). 293 toArray(Object[][]::new); 294 } 295 296 297 @DataProvider 298 public Object[][] byteUnaryOpProvider() { 299 return BYTE_GENERATORS.stream(). 300 map(f -> new Object[]{f}). 301 toArray(Object[][]::new); 302 } 303 304 @DataProvider 305 public Object[][] byteUnaryOpMaskProvider() { 306 return BOOLEAN_MASK_GENERATORS.stream(). 307 flatMap(fm -> BYTE_GENERATORS.stream().map(fa -> { 308 return new Object[] {fa, fm}; 309 })). 310 toArray(Object[][]::new); 311 } 312 313 @DataProvider 314 public Object[][] byteUnaryOpShuffleProvider() { 315 return INT_SHUFFLE_GENERATORS.stream(). 316 flatMap(fs -> BYTE_GENERATORS.stream().map(fa -> { 317 return new Object[] {fa, fs}; 318 })). 319 toArray(Object[][]::new); 320 } 321 322 @DataProvider 323 public Object[][] byteUnaryOpIndexProvider() { 324 return INT_INDEX_GENERATORS.stream(). 325 flatMap(fs -> BYTE_GENERATORS.stream().map(fa -> { 326 return new Object[] {fa, fs}; 327 })). 328 toArray(Object[][]::new); 329 } 330 331 332 static final List<IntFunction<byte[]>> BYTE_COMPARE_GENERATORS = List.of( 333 withToString("byte[i]", (int s) -> { 334 return fill(s * 1000, 335 i -> (byte)i); 336 }), 337 withToString("byte[i + 1]", (int s) -> { 338 return fill(s * 1000, 339 i -> (byte)(i + 1)); 340 }), 341 withToString("byte[i - 2]", (int s) -> { 342 return fill(s * 1000, 343 i -> (byte)(i - 2)); 344 }), 345 withToString("byte[zigZag(i)]", (int s) -> { 346 return fill(s * 1000, 347 i -> i%3 == 0 ? (byte)i : (i%3 == 1 ? (byte)(i + 1) : (byte)(i - 2))); 348 }), 349 withToString("byte[cornerCaseValue(i)]", (int s) -> { 350 return fill(s * 1000, 351 i -> cornerCaseValue(i)); 352 }) 353 ); 354 355 static final List<List<IntFunction<byte[]>>> BYTE_COMPARE_GENERATOR_PAIRS = 356 BYTE_COMPARE_GENERATORS.stream(). 357 flatMap(fa -> BYTE_COMPARE_GENERATORS.stream().map(fb -> List.of(fa, fb))). 358 collect(Collectors.toList()); 359 360 @DataProvider 361 public Object[][] byteCompareOpProvider() { 362 return BYTE_COMPARE_GENERATOR_PAIRS.stream().map(List::toArray). 363 toArray(Object[][]::new); 364 } 365 366 interface ToByteF { 367 byte apply(int i); 368 } 369 370 static byte[] fill(int s , ToByteF f) { 371 return fill(new byte[s], f); 372 } 373 374 static byte[] fill(byte[] a, ToByteF f) { 375 for (int i = 0; i < a.length; i++) { 376 a[i] = f.apply(i); 377 } 378 return a; 379 } 380 381 static byte cornerCaseValue(int i) { 382 switch(i % 5) { 383 case 0: 384 return Byte.MAX_VALUE; 385 case 1: 386 return Byte.MIN_VALUE; 387 case 2: 388 return Byte.MIN_VALUE; 389 case 3: 390 return Byte.MAX_VALUE; 391 default: 392 return (byte)0; 393 } 394 } 395 static byte get(byte[] a, int i) { 396 return (byte) a[i]; 397 } 398 399 static final IntFunction<byte[]> fr = (vl) -> { 400 int length = 1000 * vl; 401 return new byte[length]; 402 }; 403 404 static final IntFunction<boolean[]> fmr = (vl) -> { 405 int length = 1000 * vl; 406 return new boolean[length]; 407 }; 408 static byte add(byte a, byte b) { 409 return (byte)(a + b); 410 } 411 412 @Test(dataProvider = "byteBinaryOpProvider") 413 static void addByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 414 byte[] a = fa.apply(SPECIES.length()); 415 byte[] b = fb.apply(SPECIES.length()); 416 byte[] r = fr.apply(SPECIES.length()); 417 418 for (int ic = 0; ic < INVOC_COUNT; ic++) { 419 for (int i = 0; i < a.length; i += SPECIES.length()) { 420 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 421 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 422 av.add(bv).intoArray(r, i); 423 } 424 } 425 426 assertArraysEquals(a, b, r, ByteMaxVectorTests::add); 427 } 428 429 @Test(dataProvider = "byteBinaryOpMaskProvider") 430 static void addByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 431 IntFunction<boolean[]> fm) { 432 byte[] a = fa.apply(SPECIES.length()); 433 byte[] b = fb.apply(SPECIES.length()); 434 byte[] r = fr.apply(SPECIES.length()); 435 boolean[] mask = fm.apply(SPECIES.length()); 436 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 437 438 for (int ic = 0; ic < INVOC_COUNT; ic++) { 439 for (int i = 0; i < a.length; i += SPECIES.length()) { 440 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 441 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 442 av.add(bv, vmask).intoArray(r, i); 443 } 444 } 445 446 assertArraysEquals(a, b, r, mask, ByteMaxVectorTests::add); 447 } 448 static byte sub(byte a, byte b) { 449 return (byte)(a - b); 450 } 451 452 @Test(dataProvider = "byteBinaryOpProvider") 453 static void subByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 454 byte[] a = fa.apply(SPECIES.length()); 455 byte[] b = fb.apply(SPECIES.length()); 456 byte[] r = fr.apply(SPECIES.length()); 457 458 for (int ic = 0; ic < INVOC_COUNT; ic++) { 459 for (int i = 0; i < a.length; i += SPECIES.length()) { 460 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 461 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 462 av.sub(bv).intoArray(r, i); 463 } 464 } 465 466 assertArraysEquals(a, b, r, ByteMaxVectorTests::sub); 467 } 468 469 @Test(dataProvider = "byteBinaryOpMaskProvider") 470 static void subByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 471 IntFunction<boolean[]> fm) { 472 byte[] a = fa.apply(SPECIES.length()); 473 byte[] b = fb.apply(SPECIES.length()); 474 byte[] r = fr.apply(SPECIES.length()); 475 boolean[] mask = fm.apply(SPECIES.length()); 476 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 477 478 for (int ic = 0; ic < INVOC_COUNT; ic++) { 479 for (int i = 0; i < a.length; i += SPECIES.length()) { 480 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 481 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 482 av.sub(bv, vmask).intoArray(r, i); 483 } 484 } 485 486 assertArraysEquals(a, b, r, mask, ByteMaxVectorTests::sub); 487 } 488 489 490 static byte mul(byte a, byte b) { 491 return (byte)(a * b); 492 } 493 494 @Test(dataProvider = "byteBinaryOpProvider") 495 static void mulByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 496 byte[] a = fa.apply(SPECIES.length()); 497 byte[] b = fb.apply(SPECIES.length()); 498 byte[] r = fr.apply(SPECIES.length()); 499 500 for (int ic = 0; ic < INVOC_COUNT; ic++) { 501 for (int i = 0; i < a.length; i += SPECIES.length()) { 502 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 503 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 504 av.mul(bv).intoArray(r, i); 505 } 506 } 507 508 assertArraysEquals(a, b, r, ByteMaxVectorTests::mul); 509 } 510 511 @Test(dataProvider = "byteBinaryOpMaskProvider") 512 static void mulByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 513 IntFunction<boolean[]> fm) { 514 byte[] a = fa.apply(SPECIES.length()); 515 byte[] b = fb.apply(SPECIES.length()); 516 byte[] r = fr.apply(SPECIES.length()); 517 boolean[] mask = fm.apply(SPECIES.length()); 518 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 519 520 for (int ic = 0; ic < INVOC_COUNT; ic++) { 521 for (int i = 0; i < a.length; i += SPECIES.length()) { 522 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 523 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 524 av.mul(bv, vmask).intoArray(r, i); 525 } 526 } 527 528 assertArraysEquals(a, b, r, mask, ByteMaxVectorTests::mul); 529 } 530 531 static byte and(byte a, byte b) { 532 return (byte)(a & b); 533 } 534 535 @Test(dataProvider = "byteBinaryOpProvider") 536 static void andByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 537 byte[] a = fa.apply(SPECIES.length()); 538 byte[] b = fb.apply(SPECIES.length()); 539 byte[] r = fr.apply(SPECIES.length()); 540 541 for (int ic = 0; ic < INVOC_COUNT; ic++) { 542 for (int i = 0; i < a.length; i += SPECIES.length()) { 543 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 544 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 545 av.and(bv).intoArray(r, i); 546 } 547 } 548 549 assertArraysEquals(a, b, r, ByteMaxVectorTests::and); 550 } 551 552 553 554 @Test(dataProvider = "byteBinaryOpMaskProvider") 555 static void andByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 556 IntFunction<boolean[]> fm) { 557 byte[] a = fa.apply(SPECIES.length()); 558 byte[] b = fb.apply(SPECIES.length()); 559 byte[] r = fr.apply(SPECIES.length()); 560 boolean[] mask = fm.apply(SPECIES.length()); 561 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 562 563 for (int ic = 0; ic < INVOC_COUNT; ic++) { 564 for (int i = 0; i < a.length; i += SPECIES.length()) { 565 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 566 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 567 av.and(bv, vmask).intoArray(r, i); 568 } 569 } 570 571 assertArraysEquals(a, b, r, mask, ByteMaxVectorTests::and); 572 } 573 574 575 static byte or(byte a, byte b) { 576 return (byte)(a | b); 577 } 578 579 @Test(dataProvider = "byteBinaryOpProvider") 580 static void orByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 581 byte[] a = fa.apply(SPECIES.length()); 582 byte[] b = fb.apply(SPECIES.length()); 583 byte[] r = fr.apply(SPECIES.length()); 584 585 for (int ic = 0; ic < INVOC_COUNT; ic++) { 586 for (int i = 0; i < a.length; i += SPECIES.length()) { 587 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 588 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 589 av.or(bv).intoArray(r, i); 590 } 591 } 592 593 assertArraysEquals(a, b, r, ByteMaxVectorTests::or); 594 } 595 596 597 598 @Test(dataProvider = "byteBinaryOpMaskProvider") 599 static void orByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 600 IntFunction<boolean[]> fm) { 601 byte[] a = fa.apply(SPECIES.length()); 602 byte[] b = fb.apply(SPECIES.length()); 603 byte[] r = fr.apply(SPECIES.length()); 604 boolean[] mask = fm.apply(SPECIES.length()); 605 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 606 607 for (int ic = 0; ic < INVOC_COUNT; ic++) { 608 for (int i = 0; i < a.length; i += SPECIES.length()) { 609 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 610 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 611 av.or(bv, vmask).intoArray(r, i); 612 } 613 } 614 615 assertArraysEquals(a, b, r, mask, ByteMaxVectorTests::or); 616 } 617 618 619 static byte xor(byte a, byte b) { 620 return (byte)(a ^ b); 621 } 622 623 @Test(dataProvider = "byteBinaryOpProvider") 624 static void xorByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 625 byte[] a = fa.apply(SPECIES.length()); 626 byte[] b = fb.apply(SPECIES.length()); 627 byte[] r = fr.apply(SPECIES.length()); 628 629 for (int ic = 0; ic < INVOC_COUNT; ic++) { 630 for (int i = 0; i < a.length; i += SPECIES.length()) { 631 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 632 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 633 av.xor(bv).intoArray(r, i); 634 } 635 } 636 637 assertArraysEquals(a, b, r, ByteMaxVectorTests::xor); 638 } 639 640 641 642 @Test(dataProvider = "byteBinaryOpMaskProvider") 643 static void xorByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 644 IntFunction<boolean[]> fm) { 645 byte[] a = fa.apply(SPECIES.length()); 646 byte[] b = fb.apply(SPECIES.length()); 647 byte[] r = fr.apply(SPECIES.length()); 648 boolean[] mask = fm.apply(SPECIES.length()); 649 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 650 651 for (int ic = 0; ic < INVOC_COUNT; ic++) { 652 for (int i = 0; i < a.length; i += SPECIES.length()) { 653 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 654 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 655 av.xor(bv, vmask).intoArray(r, i); 656 } 657 } 658 659 assertArraysEquals(a, b, r, mask, ByteMaxVectorTests::xor); 660 } 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 static byte aShiftR_unary(byte a, byte b) { 676 return (byte)((a >> (b & 7))); 677 } 678 679 @Test(dataProvider = "byteBinaryOpProvider") 680 static void aShiftRByteMaxVectorTestsShift(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 681 byte[] a = fa.apply(SPECIES.length()); 682 byte[] b = fb.apply(SPECIES.length()); 683 byte[] r = fr.apply(SPECIES.length()); 684 685 for (int ic = 0; ic < INVOC_COUNT; ic++) { 686 for (int i = 0; i < a.length; i += SPECIES.length()) { 687 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 688 av.aShiftR((int)b[i]).intoArray(r, i); 689 } 690 } 691 692 assertShiftArraysEquals(a, b, r, ByteMaxVectorTests::aShiftR_unary); 693 } 694 695 696 697 @Test(dataProvider = "byteBinaryOpMaskProvider") 698 static void aShiftRByteMaxVectorTestsShift(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 699 IntFunction<boolean[]> fm) { 700 byte[] a = fa.apply(SPECIES.length()); 701 byte[] b = fb.apply(SPECIES.length()); 702 byte[] r = fr.apply(SPECIES.length()); 703 boolean[] mask = fm.apply(SPECIES.length()); 704 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 705 706 for (int ic = 0; ic < INVOC_COUNT; ic++) { 707 for (int i = 0; i < a.length; i += SPECIES.length()) { 708 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 709 av.aShiftR((int)b[i], vmask).intoArray(r, i); 710 } 711 } 712 713 assertShiftArraysEquals(a, b, r, mask, ByteMaxVectorTests::aShiftR_unary); 714 } 715 716 717 static byte shiftL_unary(byte a, byte b) { 718 return (byte)((a << (b & 7))); 719 } 720 721 @Test(dataProvider = "byteBinaryOpProvider") 722 static void shiftLByteMaxVectorTestsShift(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 723 byte[] a = fa.apply(SPECIES.length()); 724 byte[] b = fb.apply(SPECIES.length()); 725 byte[] r = fr.apply(SPECIES.length()); 726 727 for (int ic = 0; ic < INVOC_COUNT; ic++) { 728 for (int i = 0; i < a.length; i += SPECIES.length()) { 729 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 730 av.shiftL((int)b[i]).intoArray(r, i); 731 } 732 } 733 734 assertShiftArraysEquals(a, b, r, ByteMaxVectorTests::shiftL_unary); 735 } 736 737 738 739 @Test(dataProvider = "byteBinaryOpMaskProvider") 740 static void shiftLByteMaxVectorTestsShift(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 741 IntFunction<boolean[]> fm) { 742 byte[] a = fa.apply(SPECIES.length()); 743 byte[] b = fb.apply(SPECIES.length()); 744 byte[] r = fr.apply(SPECIES.length()); 745 boolean[] mask = fm.apply(SPECIES.length()); 746 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 747 748 for (int ic = 0; ic < INVOC_COUNT; ic++) { 749 for (int i = 0; i < a.length; i += SPECIES.length()) { 750 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 751 av.shiftL((int)b[i], vmask).intoArray(r, i); 752 } 753 } 754 755 assertShiftArraysEquals(a, b, r, mask, ByteMaxVectorTests::shiftL_unary); 756 } 757 758 759 static byte shiftR_unary(byte a, byte b) { 760 return (byte)(((a & 0xFF) >>> (b & 7))); 761 } 762 763 @Test(dataProvider = "byteBinaryOpProvider") 764 static void shiftRByteMaxVectorTestsShift(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 765 byte[] a = fa.apply(SPECIES.length()); 766 byte[] b = fb.apply(SPECIES.length()); 767 byte[] r = fr.apply(SPECIES.length()); 768 769 for (int ic = 0; ic < INVOC_COUNT; ic++) { 770 for (int i = 0; i < a.length; i += SPECIES.length()) { 771 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 772 av.shiftR((int)b[i]).intoArray(r, i); 773 } 774 } 775 776 assertShiftArraysEquals(a, b, r, ByteMaxVectorTests::shiftR_unary); 777 } 778 779 780 781 @Test(dataProvider = "byteBinaryOpMaskProvider") 782 static void shiftRByteMaxVectorTestsShift(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 783 IntFunction<boolean[]> fm) { 784 byte[] a = fa.apply(SPECIES.length()); 785 byte[] b = fb.apply(SPECIES.length()); 786 byte[] r = fr.apply(SPECIES.length()); 787 boolean[] mask = fm.apply(SPECIES.length()); 788 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 789 790 for (int ic = 0; ic < INVOC_COUNT; ic++) { 791 for (int i = 0; i < a.length; i += SPECIES.length()) { 792 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 793 av.shiftR((int)b[i], vmask).intoArray(r, i); 794 } 795 } 796 797 assertShiftArraysEquals(a, b, r, mask, ByteMaxVectorTests::shiftR_unary); 798 } 799 800 801 802 803 804 805 806 static byte max(byte a, byte b) { 807 return (byte)(Math.max(a, b)); 808 } 809 810 @Test(dataProvider = "byteBinaryOpProvider") 811 static void maxByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 812 byte[] a = fa.apply(SPECIES.length()); 813 byte[] b = fb.apply(SPECIES.length()); 814 byte[] r = fr.apply(SPECIES.length()); 815 816 for (int ic = 0; ic < INVOC_COUNT; ic++) { 817 for (int i = 0; i < a.length; i += SPECIES.length()) { 818 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 819 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 820 av.max(bv).intoArray(r, i); 821 } 822 } 823 824 assertArraysEquals(a, b, r, ByteMaxVectorTests::max); 825 } 826 static byte min(byte a, byte b) { 827 return (byte)(Math.min(a, b)); 828 } 829 830 @Test(dataProvider = "byteBinaryOpProvider") 831 static void minByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 832 byte[] a = fa.apply(SPECIES.length()); 833 byte[] b = fb.apply(SPECIES.length()); 834 byte[] r = fr.apply(SPECIES.length()); 835 836 for (int ic = 0; ic < INVOC_COUNT; ic++) { 837 for (int i = 0; i < a.length; i += SPECIES.length()) { 838 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 839 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 840 av.min(bv).intoArray(r, i); 841 } 842 } 843 844 assertArraysEquals(a, b, r, ByteMaxVectorTests::min); 845 } 846 847 static byte andAll(byte[] a, int idx) { 848 byte res = -1; 849 for (int i = idx; i < (idx + SPECIES.length()); i++) { 850 res &= a[i]; 851 } 852 853 return res; 854 } 855 856 static byte andAll(byte[] a) { 857 byte res = -1; 858 for (int i = 0; i < a.length; i += SPECIES.length()) { 859 byte tmp = -1; 860 for (int j = 0; j < SPECIES.length(); j++) { 861 tmp &= a[i + j]; 862 } 863 res &= tmp; 864 } 865 866 return res; 867 } 868 869 870 @Test(dataProvider = "byteUnaryOpProvider") 871 static void andAllByteMaxVectorTests(IntFunction<byte[]> fa) { 872 byte[] a = fa.apply(SPECIES.length()); 873 byte[] r = fr.apply(SPECIES.length()); 874 byte ra = -1; 875 876 for (int ic = 0; ic < INVOC_COUNT; ic++) { 877 for (int i = 0; i < a.length; i += SPECIES.length()) { 878 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 879 r[i] = av.andAll(); 880 } 881 } 882 883 for (int ic = 0; ic < INVOC_COUNT; ic++) { 884 ra = -1; 885 for (int i = 0; i < a.length; i += SPECIES.length()) { 886 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 887 ra &= av.andAll(); 888 } 889 } 890 891 assertReductionArraysEquals(a, r, ra, ByteMaxVectorTests::andAll, ByteMaxVectorTests::andAll); 892 } 893 894 895 static byte orAll(byte[] a, int idx) { 896 byte res = 0; 897 for (int i = idx; i < (idx + SPECIES.length()); i++) { 898 res |= a[i]; 899 } 900 901 return res; 902 } 903 904 static byte orAll(byte[] a) { 905 byte res = 0; 906 for (int i = 0; i < a.length; i += SPECIES.length()) { 907 byte tmp = 0; 908 for (int j = 0; j < SPECIES.length(); j++) { 909 tmp |= a[i + j]; 910 } 911 res |= tmp; 912 } 913 914 return res; 915 } 916 917 918 @Test(dataProvider = "byteUnaryOpProvider") 919 static void orAllByteMaxVectorTests(IntFunction<byte[]> fa) { 920 byte[] a = fa.apply(SPECIES.length()); 921 byte[] r = fr.apply(SPECIES.length()); 922 byte ra = 0; 923 924 for (int ic = 0; ic < INVOC_COUNT; ic++) { 925 for (int i = 0; i < a.length; i += SPECIES.length()) { 926 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 927 r[i] = av.orAll(); 928 } 929 } 930 931 for (int ic = 0; ic < INVOC_COUNT; ic++) { 932 ra = 0; 933 for (int i = 0; i < a.length; i += SPECIES.length()) { 934 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 935 ra |= av.orAll(); 936 } 937 } 938 939 assertReductionArraysEquals(a, r, ra, ByteMaxVectorTests::orAll, ByteMaxVectorTests::orAll); 940 } 941 942 943 static byte xorAll(byte[] a, int idx) { 944 byte res = 0; 945 for (int i = idx; i < (idx + SPECIES.length()); i++) { 946 res ^= a[i]; 947 } 948 949 return res; 950 } 951 952 static byte xorAll(byte[] a) { 953 byte res = 0; 954 for (int i = 0; i < a.length; i += SPECIES.length()) { 955 byte tmp = 0; 956 for (int j = 0; j < SPECIES.length(); j++) { 957 tmp ^= a[i + j]; 958 } 959 res ^= tmp; 960 } 961 962 return res; 963 } 964 965 966 @Test(dataProvider = "byteUnaryOpProvider") 967 static void xorAllByteMaxVectorTests(IntFunction<byte[]> fa) { 968 byte[] a = fa.apply(SPECIES.length()); 969 byte[] r = fr.apply(SPECIES.length()); 970 byte ra = 0; 971 972 for (int ic = 0; ic < INVOC_COUNT; ic++) { 973 for (int i = 0; i < a.length; i += SPECIES.length()) { 974 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 975 r[i] = av.xorAll(); 976 } 977 } 978 979 for (int ic = 0; ic < INVOC_COUNT; ic++) { 980 ra = 0; 981 for (int i = 0; i < a.length; i += SPECIES.length()) { 982 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 983 ra ^= av.xorAll(); 984 } 985 } 986 987 assertReductionArraysEquals(a, r, ra, ByteMaxVectorTests::xorAll, ByteMaxVectorTests::xorAll); 988 } 989 990 static byte addAll(byte[] a, int idx) { 991 byte res = 0; 992 for (int i = idx; i < (idx + SPECIES.length()); i++) { 993 res += a[i]; 994 } 995 996 return res; 997 } 998 999 static byte addAll(byte[] a) { 1000 byte res = 0; 1001 for (int i = 0; i < a.length; i += SPECIES.length()) { 1002 byte tmp = 0; 1003 for (int j = 0; j < SPECIES.length(); j++) { 1004 tmp += a[i + j]; 1005 } 1006 res += tmp; 1007 } 1008 1009 return res; 1010 } 1011 @Test(dataProvider = "byteUnaryOpProvider") 1012 static void addAllByteMaxVectorTests(IntFunction<byte[]> fa) { 1013 byte[] a = fa.apply(SPECIES.length()); 1014 byte[] r = fr.apply(SPECIES.length()); 1015 byte ra = 0; 1016 1017 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1018 for (int i = 0; i < a.length; i += SPECIES.length()) { 1019 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1020 r[i] = av.addAll(); 1021 } 1022 } 1023 1024 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1025 ra = 0; 1026 for (int i = 0; i < a.length; i += SPECIES.length()) { 1027 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1028 ra += av.addAll(); 1029 } 1030 } 1031 1032 assertReductionArraysEquals(a, r, ra, ByteMaxVectorTests::addAll, ByteMaxVectorTests::addAll); 1033 } 1034 static byte mulAll(byte[] a, int idx) { 1035 byte res = 1; 1036 for (int i = idx; i < (idx + SPECIES.length()); i++) { 1037 res *= a[i]; 1038 } 1039 1040 return res; 1041 } 1042 1043 static byte mulAll(byte[] a) { 1044 byte res = 1; 1045 for (int i = 0; i < a.length; i += SPECIES.length()) { 1046 byte tmp = 1; 1047 for (int j = 0; j < SPECIES.length(); j++) { 1048 tmp *= a[i + j]; 1049 } 1050 res *= tmp; 1051 } 1052 1053 return res; 1054 } 1055 @Test(dataProvider = "byteUnaryOpProvider") 1056 static void mulAllByteMaxVectorTests(IntFunction<byte[]> fa) { 1057 byte[] a = fa.apply(SPECIES.length()); 1058 byte[] r = fr.apply(SPECIES.length()); 1059 byte ra = 1; 1060 1061 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1062 for (int i = 0; i < a.length; i += SPECIES.length()) { 1063 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1064 r[i] = av.mulAll(); 1065 } 1066 } 1067 1068 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1069 ra = 1; 1070 for (int i = 0; i < a.length; i += SPECIES.length()) { 1071 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1072 ra *= av.mulAll(); 1073 } 1074 } 1075 1076 assertReductionArraysEquals(a, r, ra, ByteMaxVectorTests::mulAll, ByteMaxVectorTests::mulAll); 1077 } 1078 static byte minAll(byte[] a, int idx) { 1079 byte res = Byte.MAX_VALUE; 1080 for (int i = idx; i < (idx + SPECIES.length()); i++) { 1081 res = (byte)Math.min(res, a[i]); 1082 } 1083 1084 return res; 1085 } 1086 1087 static byte minAll(byte[] a) { 1088 byte res = Byte.MAX_VALUE; 1089 for (int i = 0; i < a.length; i++) { 1090 res = (byte)Math.min(res, a[i]); 1091 } 1092 1093 return res; 1094 } 1095 @Test(dataProvider = "byteUnaryOpProvider") 1096 static void minAllByteMaxVectorTests(IntFunction<byte[]> fa) { 1097 byte[] a = fa.apply(SPECIES.length()); 1098 byte[] r = fr.apply(SPECIES.length()); 1099 byte ra = Byte.MAX_VALUE; 1100 1101 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1102 for (int i = 0; i < a.length; i += SPECIES.length()) { 1103 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1104 r[i] = av.minAll(); 1105 } 1106 } 1107 1108 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1109 ra = Byte.MAX_VALUE; 1110 for (int i = 0; i < a.length; i += SPECIES.length()) { 1111 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1112 ra = (byte)Math.min(ra, av.minAll()); 1113 } 1114 } 1115 1116 assertReductionArraysEquals(a, r, ra, ByteMaxVectorTests::minAll, ByteMaxVectorTests::minAll); 1117 } 1118 static byte maxAll(byte[] a, int idx) { 1119 byte res = Byte.MIN_VALUE; 1120 for (int i = idx; i < (idx + SPECIES.length()); i++) { 1121 res = (byte)Math.max(res, a[i]); 1122 } 1123 1124 return res; 1125 } 1126 1127 static byte maxAll(byte[] a) { 1128 byte res = Byte.MIN_VALUE; 1129 for (int i = 0; i < a.length; i++) { 1130 res = (byte)Math.max(res, a[i]); 1131 } 1132 1133 return res; 1134 } 1135 @Test(dataProvider = "byteUnaryOpProvider") 1136 static void maxAllByteMaxVectorTests(IntFunction<byte[]> fa) { 1137 byte[] a = fa.apply(SPECIES.length()); 1138 byte[] r = fr.apply(SPECIES.length()); 1139 byte ra = Byte.MIN_VALUE; 1140 1141 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1142 for (int i = 0; i < a.length; i += SPECIES.length()) { 1143 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1144 r[i] = av.maxAll(); 1145 } 1146 } 1147 1148 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1149 ra = Byte.MIN_VALUE; 1150 for (int i = 0; i < a.length; i += SPECIES.length()) { 1151 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1152 ra = (byte)Math.max(ra, av.maxAll()); 1153 } 1154 } 1155 1156 assertReductionArraysEquals(a, r, ra, ByteMaxVectorTests::maxAll, ByteMaxVectorTests::maxAll); 1157 } 1158 1159 static boolean anyTrue(boolean[] a, int idx) { 1160 boolean res = false; 1161 for (int i = idx; i < (idx + SPECIES.length()); i++) { 1162 res |= a[i]; 1163 } 1164 1165 return res; 1166 } 1167 1168 1169 @Test(dataProvider = "boolUnaryOpProvider") 1170 static void anyTrueByteMaxVectorTests(IntFunction<boolean[]> fm) { 1171 boolean[] mask = fm.apply(SPECIES.length()); 1172 boolean[] r = fmr.apply(SPECIES.length()); 1173 1174 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1175 for (int i = 0; i < mask.length; i += SPECIES.length()) { 1176 Vector.Mask<Byte> vmask = ByteVector.maskFromArray(SPECIES, mask, i); 1177 r[i] = vmask.anyTrue(); 1178 } 1179 } 1180 1181 assertReductionBoolArraysEquals(mask, r, ByteMaxVectorTests::anyTrue); 1182 } 1183 1184 1185 static boolean allTrue(boolean[] a, int idx) { 1186 boolean res = true; 1187 for (int i = idx; i < (idx + SPECIES.length()); i++) { 1188 res &= a[i]; 1189 } 1190 1191 return res; 1192 } 1193 1194 1195 @Test(dataProvider = "boolUnaryOpProvider") 1196 static void allTrueByteMaxVectorTests(IntFunction<boolean[]> fm) { 1197 boolean[] mask = fm.apply(SPECIES.length()); 1198 boolean[] r = fmr.apply(SPECIES.length()); 1199 1200 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1201 for (int i = 0; i < mask.length; i += SPECIES.length()) { 1202 Vector.Mask<Byte> vmask = ByteVector.maskFromArray(SPECIES, mask, i); 1203 r[i] = vmask.allTrue(); 1204 } 1205 } 1206 1207 assertReductionBoolArraysEquals(mask, r, ByteMaxVectorTests::allTrue); 1208 } 1209 1210 1211 @Test(dataProvider = "byteUnaryOpProvider") 1212 static void withByteMaxVectorTests(IntFunction<byte []> fa) { 1213 byte[] a = fa.apply(SPECIES.length()); 1214 byte[] r = fr.apply(SPECIES.length()); 1215 1216 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1217 for (int i = 0; i < a.length; i += SPECIES.length()) { 1218 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1219 av.with(0, (byte)4).intoArray(r, i); 1220 } 1221 } 1222 1223 assertInsertArraysEquals(a, r, (byte)4, 0); 1224 } 1225 1226 @Test(dataProvider = "byteCompareOpProvider") 1227 static void lessThanByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 1228 byte[] a = fa.apply(SPECIES.length()); 1229 byte[] b = fb.apply(SPECIES.length()); 1230 1231 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1232 for (int i = 0; i < a.length; i += SPECIES.length()) { 1233 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1234 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 1235 Vector.Mask<Byte> mv = av.lessThan(bv); 1236 1237 // Check results as part of computation. 1238 for (int j = 0; j < SPECIES.length(); j++) { 1239 Assert.assertEquals(mv.getElement(j), a[i + j] < b[i + j]); 1240 } 1241 } 1242 } 1243 } 1244 1245 1246 @Test(dataProvider = "byteCompareOpProvider") 1247 static void greaterThanByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 1248 byte[] a = fa.apply(SPECIES.length()); 1249 byte[] b = fb.apply(SPECIES.length()); 1250 1251 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1252 for (int i = 0; i < a.length; i += SPECIES.length()) { 1253 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1254 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 1255 Vector.Mask<Byte> mv = av.greaterThan(bv); 1256 1257 // Check results as part of computation. 1258 for (int j = 0; j < SPECIES.length(); j++) { 1259 Assert.assertEquals(mv.getElement(j), a[i + j] > b[i + j]); 1260 } 1261 } 1262 } 1263 } 1264 1265 1266 @Test(dataProvider = "byteCompareOpProvider") 1267 static void equalByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 1268 byte[] a = fa.apply(SPECIES.length()); 1269 byte[] b = fb.apply(SPECIES.length()); 1270 1271 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1272 for (int i = 0; i < a.length; i += SPECIES.length()) { 1273 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1274 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 1275 Vector.Mask<Byte> mv = av.equal(bv); 1276 1277 // Check results as part of computation. 1278 for (int j = 0; j < SPECIES.length(); j++) { 1279 Assert.assertEquals(mv.getElement(j), a[i + j] == b[i + j]); 1280 } 1281 } 1282 } 1283 } 1284 1285 1286 @Test(dataProvider = "byteCompareOpProvider") 1287 static void notEqualByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 1288 byte[] a = fa.apply(SPECIES.length()); 1289 byte[] b = fb.apply(SPECIES.length()); 1290 1291 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1292 for (int i = 0; i < a.length; i += SPECIES.length()) { 1293 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1294 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 1295 Vector.Mask<Byte> mv = av.notEqual(bv); 1296 1297 // Check results as part of computation. 1298 for (int j = 0; j < SPECIES.length(); j++) { 1299 Assert.assertEquals(mv.getElement(j), a[i + j] != b[i + j]); 1300 } 1301 } 1302 } 1303 } 1304 1305 1306 @Test(dataProvider = "byteCompareOpProvider") 1307 static void lessThanEqByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 1308 byte[] a = fa.apply(SPECIES.length()); 1309 byte[] b = fb.apply(SPECIES.length()); 1310 1311 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1312 for (int i = 0; i < a.length; i += SPECIES.length()) { 1313 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1314 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 1315 Vector.Mask<Byte> mv = av.lessThanEq(bv); 1316 1317 // Check results as part of computation. 1318 for (int j = 0; j < SPECIES.length(); j++) { 1319 Assert.assertEquals(mv.getElement(j), a[i + j] <= b[i + j]); 1320 } 1321 } 1322 } 1323 } 1324 1325 1326 @Test(dataProvider = "byteCompareOpProvider") 1327 static void greaterThanEqByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) { 1328 byte[] a = fa.apply(SPECIES.length()); 1329 byte[] b = fb.apply(SPECIES.length()); 1330 1331 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1332 for (int i = 0; i < a.length; i += SPECIES.length()) { 1333 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1334 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 1335 Vector.Mask<Byte> mv = av.greaterThanEq(bv); 1336 1337 // Check results as part of computation. 1338 for (int j = 0; j < SPECIES.length(); j++) { 1339 Assert.assertEquals(mv.getElement(j), a[i + j] >= b[i + j]); 1340 } 1341 } 1342 } 1343 } 1344 1345 1346 static byte blend(byte a, byte b, boolean mask) { 1347 return mask ? b : a; 1348 } 1349 1350 @Test(dataProvider = "byteBinaryOpMaskProvider") 1351 static void blendByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb, 1352 IntFunction<boolean[]> fm) { 1353 byte[] a = fa.apply(SPECIES.length()); 1354 byte[] b = fb.apply(SPECIES.length()); 1355 byte[] r = fr.apply(SPECIES.length()); 1356 boolean[] mask = fm.apply(SPECIES.length()); 1357 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 1358 1359 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1360 for (int i = 0; i < a.length; i += SPECIES.length()) { 1361 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1362 ByteVector bv = ByteVector.fromArray(SPECIES, b, i); 1363 av.blend(bv, vmask).intoArray(r, i); 1364 } 1365 } 1366 1367 assertArraysEquals(a, b, r, mask, ByteMaxVectorTests::blend); 1368 } 1369 1370 @Test(dataProvider = "byteUnaryOpShuffleProvider") 1371 static void RearrangeByteMaxVectorTests(IntFunction<byte[]> fa, 1372 BiFunction<Integer,Integer,int[]> fs) { 1373 byte[] a = fa.apply(SPECIES.length()); 1374 int[] order = fs.apply(a.length, SPECIES.length()); 1375 byte[] r = fr.apply(SPECIES.length()); 1376 1377 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1378 for (int i = 0; i < a.length; i += SPECIES.length()) { 1379 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1380 av.rearrange(ByteVector.shuffleFromArray(SPECIES, order, i)).intoArray(r, i); 1381 } 1382 } 1383 1384 assertRearrangeArraysEquals(a, r, order, SPECIES.length()); 1385 } 1386 1387 1388 1389 1390 @Test(dataProvider = "byteUnaryOpProvider") 1391 static void getByteMaxVectorTests(IntFunction<byte[]> fa) { 1392 byte[] a = fa.apply(SPECIES.length()); 1393 byte[] r = fr.apply(SPECIES.length()); 1394 1395 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1396 for (int i = 0; i < a.length; i += SPECIES.length()) { 1397 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1398 int num_lanes = SPECIES.length(); 1399 // Manually unroll because full unroll happens after intrinsification. 1400 // Unroll is needed because get intrinsic requires for index to be a known constant. 1401 if (num_lanes == 1) { 1402 r[i]=av.get(0); 1403 } else if (num_lanes == 2) { 1404 r[i]=av.get(0); 1405 r[i+1]=av.get(1); 1406 } else if (num_lanes == 4) { 1407 r[i]=av.get(0); 1408 r[i+1]=av.get(1); 1409 r[i+2]=av.get(2); 1410 r[i+3]=av.get(3); 1411 } else if (num_lanes == 8) { 1412 r[i]=av.get(0); 1413 r[i+1]=av.get(1); 1414 r[i+2]=av.get(2); 1415 r[i+3]=av.get(3); 1416 r[i+4]=av.get(4); 1417 r[i+5]=av.get(5); 1418 r[i+6]=av.get(6); 1419 r[i+7]=av.get(7); 1420 } else if (num_lanes == 16) { 1421 r[i]=av.get(0); 1422 r[i+1]=av.get(1); 1423 r[i+2]=av.get(2); 1424 r[i+3]=av.get(3); 1425 r[i+4]=av.get(4); 1426 r[i+5]=av.get(5); 1427 r[i+6]=av.get(6); 1428 r[i+7]=av.get(7); 1429 r[i+8]=av.get(8); 1430 r[i+9]=av.get(9); 1431 r[i+10]=av.get(10); 1432 r[i+11]=av.get(11); 1433 r[i+12]=av.get(12); 1434 r[i+13]=av.get(13); 1435 r[i+14]=av.get(14); 1436 r[i+15]=av.get(15); 1437 } else if (num_lanes == 32) { 1438 r[i]=av.get(0); 1439 r[i+1]=av.get(1); 1440 r[i+2]=av.get(2); 1441 r[i+3]=av.get(3); 1442 r[i+4]=av.get(4); 1443 r[i+5]=av.get(5); 1444 r[i+6]=av.get(6); 1445 r[i+7]=av.get(7); 1446 r[i+8]=av.get(8); 1447 r[i+9]=av.get(9); 1448 r[i+10]=av.get(10); 1449 r[i+11]=av.get(11); 1450 r[i+12]=av.get(12); 1451 r[i+13]=av.get(13); 1452 r[i+14]=av.get(14); 1453 r[i+15]=av.get(15); 1454 r[i+16]=av.get(16); 1455 r[i+17]=av.get(17); 1456 r[i+18]=av.get(18); 1457 r[i+19]=av.get(19); 1458 r[i+20]=av.get(20); 1459 r[i+21]=av.get(21); 1460 r[i+22]=av.get(22); 1461 r[i+23]=av.get(23); 1462 r[i+24]=av.get(24); 1463 r[i+25]=av.get(25); 1464 r[i+26]=av.get(26); 1465 r[i+27]=av.get(27); 1466 r[i+28]=av.get(28); 1467 r[i+29]=av.get(29); 1468 r[i+30]=av.get(30); 1469 r[i+31]=av.get(31); 1470 } else if (num_lanes == 64) { 1471 r[i]=av.get(0); 1472 r[i+1]=av.get(1); 1473 r[i+2]=av.get(2); 1474 r[i+3]=av.get(3); 1475 r[i+4]=av.get(4); 1476 r[i+5]=av.get(5); 1477 r[i+6]=av.get(6); 1478 r[i+7]=av.get(7); 1479 r[i+8]=av.get(8); 1480 r[i+9]=av.get(9); 1481 r[i+10]=av.get(10); 1482 r[i+11]=av.get(11); 1483 r[i+12]=av.get(12); 1484 r[i+13]=av.get(13); 1485 r[i+14]=av.get(14); 1486 r[i+15]=av.get(15); 1487 r[i+16]=av.get(16); 1488 r[i+17]=av.get(17); 1489 r[i+18]=av.get(18); 1490 r[i+19]=av.get(19); 1491 r[i+20]=av.get(20); 1492 r[i+21]=av.get(21); 1493 r[i+22]=av.get(22); 1494 r[i+23]=av.get(23); 1495 r[i+24]=av.get(24); 1496 r[i+25]=av.get(25); 1497 r[i+26]=av.get(26); 1498 r[i+27]=av.get(27); 1499 r[i+28]=av.get(28); 1500 r[i+29]=av.get(29); 1501 r[i+30]=av.get(30); 1502 r[i+31]=av.get(31); 1503 r[i+32]=av.get(32); 1504 r[i+33]=av.get(33); 1505 r[i+34]=av.get(34); 1506 r[i+35]=av.get(35); 1507 r[i+36]=av.get(36); 1508 r[i+37]=av.get(37); 1509 r[i+38]=av.get(38); 1510 r[i+39]=av.get(39); 1511 r[i+40]=av.get(40); 1512 r[i+41]=av.get(41); 1513 r[i+42]=av.get(42); 1514 r[i+43]=av.get(43); 1515 r[i+44]=av.get(44); 1516 r[i+45]=av.get(45); 1517 r[i+46]=av.get(46); 1518 r[i+47]=av.get(47); 1519 r[i+48]=av.get(48); 1520 r[i+49]=av.get(49); 1521 r[i+50]=av.get(50); 1522 r[i+51]=av.get(51); 1523 r[i+52]=av.get(52); 1524 r[i+53]=av.get(53); 1525 r[i+54]=av.get(54); 1526 r[i+55]=av.get(55); 1527 r[i+56]=av.get(56); 1528 r[i+57]=av.get(57); 1529 r[i+58]=av.get(58); 1530 r[i+59]=av.get(59); 1531 r[i+60]=av.get(60); 1532 r[i+61]=av.get(61); 1533 r[i+62]=av.get(62); 1534 r[i+63]=av.get(63); 1535 } else { 1536 for (int j = 0; j < SPECIES.length(); j++) { 1537 r[i+j]=av.get(j); 1538 } 1539 } 1540 } 1541 } 1542 1543 assertArraysEquals(a, r, ByteMaxVectorTests::get); 1544 } 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 static byte neg(byte a) { 1568 return (byte)(-((byte)a)); 1569 } 1570 1571 @Test(dataProvider = "byteUnaryOpProvider") 1572 static void negByteMaxVectorTests(IntFunction<byte[]> fa) { 1573 byte[] a = fa.apply(SPECIES.length()); 1574 byte[] r = fr.apply(SPECIES.length()); 1575 1576 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1577 for (int i = 0; i < a.length; i += SPECIES.length()) { 1578 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1579 av.neg().intoArray(r, i); 1580 } 1581 } 1582 1583 assertArraysEquals(a, r, ByteMaxVectorTests::neg); 1584 } 1585 1586 @Test(dataProvider = "byteUnaryOpMaskProvider") 1587 static void negMaskedByteMaxVectorTests(IntFunction<byte[]> fa, 1588 IntFunction<boolean[]> fm) { 1589 byte[] a = fa.apply(SPECIES.length()); 1590 byte[] r = fr.apply(SPECIES.length()); 1591 boolean[] mask = fm.apply(SPECIES.length()); 1592 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 1593 1594 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1595 for (int i = 0; i < a.length; i += SPECIES.length()) { 1596 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1597 av.neg(vmask).intoArray(r, i); 1598 } 1599 } 1600 1601 assertArraysEquals(a, r, mask, ByteMaxVectorTests::neg); 1602 } 1603 1604 static byte abs(byte a) { 1605 return (byte)(Math.abs((byte)a)); 1606 } 1607 1608 @Test(dataProvider = "byteUnaryOpProvider") 1609 static void absByteMaxVectorTests(IntFunction<byte[]> fa) { 1610 byte[] a = fa.apply(SPECIES.length()); 1611 byte[] r = fr.apply(SPECIES.length()); 1612 1613 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1614 for (int i = 0; i < a.length; i += SPECIES.length()) { 1615 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1616 av.abs().intoArray(r, i); 1617 } 1618 } 1619 1620 assertArraysEquals(a, r, ByteMaxVectorTests::abs); 1621 } 1622 1623 @Test(dataProvider = "byteUnaryOpMaskProvider") 1624 static void absMaskedByteMaxVectorTests(IntFunction<byte[]> fa, 1625 IntFunction<boolean[]> fm) { 1626 byte[] a = fa.apply(SPECIES.length()); 1627 byte[] r = fr.apply(SPECIES.length()); 1628 boolean[] mask = fm.apply(SPECIES.length()); 1629 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 1630 1631 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1632 for (int i = 0; i < a.length; i += SPECIES.length()) { 1633 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1634 av.abs(vmask).intoArray(r, i); 1635 } 1636 } 1637 1638 assertArraysEquals(a, r, mask, ByteMaxVectorTests::abs); 1639 } 1640 1641 1642 static byte not(byte a) { 1643 return (byte)(~((byte)a)); 1644 } 1645 1646 1647 1648 @Test(dataProvider = "byteUnaryOpProvider") 1649 static void notByteMaxVectorTests(IntFunction<byte[]> fa) { 1650 byte[] a = fa.apply(SPECIES.length()); 1651 byte[] r = fr.apply(SPECIES.length()); 1652 1653 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1654 for (int i = 0; i < a.length; i += SPECIES.length()) { 1655 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1656 av.not().intoArray(r, i); 1657 } 1658 } 1659 1660 assertArraysEquals(a, r, ByteMaxVectorTests::not); 1661 } 1662 1663 1664 1665 @Test(dataProvider = "byteUnaryOpMaskProvider") 1666 static void notMaskedByteMaxVectorTests(IntFunction<byte[]> fa, 1667 IntFunction<boolean[]> fm) { 1668 byte[] a = fa.apply(SPECIES.length()); 1669 byte[] r = fr.apply(SPECIES.length()); 1670 boolean[] mask = fm.apply(SPECIES.length()); 1671 Vector.Mask<Byte> vmask = ByteVector.maskFromValues(SPECIES, mask); 1672 1673 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1674 for (int i = 0; i < a.length; i += SPECIES.length()) { 1675 ByteVector av = ByteVector.fromArray(SPECIES, a, i); 1676 av.not(vmask).intoArray(r, i); 1677 } 1678 } 1679 1680 assertArraysEquals(a, r, mask, ByteMaxVectorTests::not); 1681 } 1682 1683 1684 1685 1686 1687 1688 } 1689