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 ShortMaxVectorTests 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.ShortVector; 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 ShortMaxVectorTests extends AbstractVectorTest { 49 50 static final Species<Short> SPECIES = 51 ShortVector.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 short apply(short a); 61 } 62 63 static void assertArraysEquals(short[] a, short[] 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(short[] a, short[] 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 short apply(short[] a, int idx); 87 } 88 89 interface FReductionAllOp { 90 short apply(short[] a); 91 } 92 93 static void assertReductionArraysEquals(short[] a, short[] b, short 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(short[] a, short[] b, short 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(short[] a, short[] 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 short apply(short a, short b); 157 } 158 159 interface FBinMaskOp { 160 short apply(short a, short 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(short[] a, short[] b, short[] 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(short[] a, short[] b, short[] r, boolean[] mask, FBinOp f) { 179 assertArraysEquals(a, b, r, mask, FBinMaskOp.lift(f)); 180 } 181 182 static void assertArraysEquals(short[] a, short[] b, short[] 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(short[] a, short[] b, short[] 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(short[] a, short[] b, short[] r, boolean[] mask, FBinOp f) { 208 assertShiftArraysEquals(a, b, r, mask, FBinMaskOp.lift(f)); 209 } 210 211 static void assertShiftArraysEquals(short[] a, short[] b, short[] 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 short apply(short[] a, int b); 228 } 229 230 static void assertArraysEquals(short[] a, short[] 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<short[]>> SHORT_GENERATORS = List.of( 242 withToString("short[-i * 5]", (int s) -> { 243 return fill(s * 1000, 244 i -> (short)(-i * 5)); 245 }), 246 withToString("short[i * 5]", (int s) -> { 247 return fill(s * 1000, 248 i -> (short)(i * 5)); 249 }), 250 withToString("short[i + 1]", (int s) -> { 251 return fill(s * 1000, 252 i -> (((short)(i + 1) == 0) ? 1 : (short)(i + 1))); 253 }), 254 withToString("short[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<short[]>>> SHORT_GENERATOR_PAIRS = 263 Stream.of(SHORT_GENERATORS.get(0)). 264 flatMap(fa -> SHORT_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[][] shortBinaryOpProvider() { 277 return SHORT_GENERATOR_PAIRS.stream().map(List::toArray). 278 toArray(Object[][]::new); 279 } 280 281 @DataProvider 282 public Object[][] shortIndexedOpProvider() { 283 return SHORT_GENERATOR_PAIRS.stream().map(List::toArray). 284 toArray(Object[][]::new); 285 } 286 287 @DataProvider 288 public Object[][] shortBinaryOpMaskProvider() { 289 return BOOLEAN_MASK_GENERATORS.stream(). 290 flatMap(fm -> SHORT_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[][] shortUnaryOpProvider() { 299 return SHORT_GENERATORS.stream(). 300 map(f -> new Object[]{f}). 301 toArray(Object[][]::new); 302 } 303 304 @DataProvider 305 public Object[][] shortUnaryOpMaskProvider() { 306 return BOOLEAN_MASK_GENERATORS.stream(). 307 flatMap(fm -> SHORT_GENERATORS.stream().map(fa -> { 308 return new Object[] {fa, fm}; 309 })). 310 toArray(Object[][]::new); 311 } 312 313 @DataProvider 314 public Object[][] shortUnaryOpShuffleProvider() { 315 return INT_SHUFFLE_GENERATORS.stream(). 316 flatMap(fs -> SHORT_GENERATORS.stream().map(fa -> { 317 return new Object[] {fa, fs}; 318 })). 319 toArray(Object[][]::new); 320 } 321 322 @DataProvider 323 public Object[][] shortUnaryOpIndexProvider() { 324 return INT_INDEX_GENERATORS.stream(). 325 flatMap(fs -> SHORT_GENERATORS.stream().map(fa -> { 326 return new Object[] {fa, fs}; 327 })). 328 toArray(Object[][]::new); 329 } 330 331 332 static final List<IntFunction<short[]>> SHORT_COMPARE_GENERATORS = List.of( 333 withToString("short[i]", (int s) -> { 334 return fill(s * 1000, 335 i -> (short)i); 336 }), 337 withToString("short[i + 1]", (int s) -> { 338 return fill(s * 1000, 339 i -> (short)(i + 1)); 340 }), 341 withToString("short[i - 2]", (int s) -> { 342 return fill(s * 1000, 343 i -> (short)(i - 2)); 344 }), 345 withToString("short[zigZag(i)]", (int s) -> { 346 return fill(s * 1000, 347 i -> i%3 == 0 ? (short)i : (i%3 == 1 ? (short)(i + 1) : (short)(i - 2))); 348 }), 349 withToString("short[cornerCaseValue(i)]", (int s) -> { 350 return fill(s * 1000, 351 i -> cornerCaseValue(i)); 352 }) 353 ); 354 355 static final List<List<IntFunction<short[]>>> SHORT_COMPARE_GENERATOR_PAIRS = 356 SHORT_COMPARE_GENERATORS.stream(). 357 flatMap(fa -> SHORT_COMPARE_GENERATORS.stream().map(fb -> List.of(fa, fb))). 358 collect(Collectors.toList()); 359 360 @DataProvider 361 public Object[][] shortCompareOpProvider() { 362 return SHORT_COMPARE_GENERATOR_PAIRS.stream().map(List::toArray). 363 toArray(Object[][]::new); 364 } 365 366 interface ToShortF { 367 short apply(int i); 368 } 369 370 static short[] fill(int s , ToShortF f) { 371 return fill(new short[s], f); 372 } 373 374 static short[] fill(short[] a, ToShortF f) { 375 for (int i = 0; i < a.length; i++) { 376 a[i] = f.apply(i); 377 } 378 return a; 379 } 380 381 static short cornerCaseValue(int i) { 382 switch(i % 5) { 383 case 0: 384 return Short.MAX_VALUE; 385 case 1: 386 return Short.MIN_VALUE; 387 case 2: 388 return Short.MIN_VALUE; 389 case 3: 390 return Short.MAX_VALUE; 391 default: 392 return (short)0; 393 } 394 } 395 static short get(short[] a, int i) { 396 return (short) a[i]; 397 } 398 399 static final IntFunction<short[]> fr = (vl) -> { 400 int length = 1000 * vl; 401 return new short[length]; 402 }; 403 404 static final IntFunction<boolean[]> fmr = (vl) -> { 405 int length = 1000 * vl; 406 return new boolean[length]; 407 }; 408 static short add(short a, short b) { 409 return (short)(a + b); 410 } 411 412 @Test(dataProvider = "shortBinaryOpProvider") 413 static void addShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 414 short[] a = fa.apply(SPECIES.length()); 415 short[] b = fb.apply(SPECIES.length()); 416 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 421 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 422 av.add(bv).intoArray(r, i); 423 } 424 } 425 426 assertArraysEquals(a, b, r, ShortMaxVectorTests::add); 427 } 428 429 @Test(dataProvider = "shortBinaryOpMaskProvider") 430 static void addShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb, 431 IntFunction<boolean[]> fm) { 432 short[] a = fa.apply(SPECIES.length()); 433 short[] b = fb.apply(SPECIES.length()); 434 short[] r = fr.apply(SPECIES.length()); 435 boolean[] mask = fm.apply(SPECIES.length()); 436 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 441 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 442 av.add(bv, vmask).intoArray(r, i); 443 } 444 } 445 446 assertArraysEquals(a, b, r, mask, ShortMaxVectorTests::add); 447 } 448 static short sub(short a, short b) { 449 return (short)(a - b); 450 } 451 452 @Test(dataProvider = "shortBinaryOpProvider") 453 static void subShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 454 short[] a = fa.apply(SPECIES.length()); 455 short[] b = fb.apply(SPECIES.length()); 456 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 461 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 462 av.sub(bv).intoArray(r, i); 463 } 464 } 465 466 assertArraysEquals(a, b, r, ShortMaxVectorTests::sub); 467 } 468 469 @Test(dataProvider = "shortBinaryOpMaskProvider") 470 static void subShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb, 471 IntFunction<boolean[]> fm) { 472 short[] a = fa.apply(SPECIES.length()); 473 short[] b = fb.apply(SPECIES.length()); 474 short[] r = fr.apply(SPECIES.length()); 475 boolean[] mask = fm.apply(SPECIES.length()); 476 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 481 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 482 av.sub(bv, vmask).intoArray(r, i); 483 } 484 } 485 486 assertArraysEquals(a, b, r, mask, ShortMaxVectorTests::sub); 487 } 488 489 490 static short mul(short a, short b) { 491 return (short)(a * b); 492 } 493 494 @Test(dataProvider = "shortBinaryOpProvider") 495 static void mulShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 496 short[] a = fa.apply(SPECIES.length()); 497 short[] b = fb.apply(SPECIES.length()); 498 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 503 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 504 av.mul(bv).intoArray(r, i); 505 } 506 } 507 508 assertArraysEquals(a, b, r, ShortMaxVectorTests::mul); 509 } 510 511 @Test(dataProvider = "shortBinaryOpMaskProvider") 512 static void mulShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb, 513 IntFunction<boolean[]> fm) { 514 short[] a = fa.apply(SPECIES.length()); 515 short[] b = fb.apply(SPECIES.length()); 516 short[] r = fr.apply(SPECIES.length()); 517 boolean[] mask = fm.apply(SPECIES.length()); 518 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 523 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 524 av.mul(bv, vmask).intoArray(r, i); 525 } 526 } 527 528 assertArraysEquals(a, b, r, mask, ShortMaxVectorTests::mul); 529 } 530 531 static short and(short a, short b) { 532 return (short)(a & b); 533 } 534 535 @Test(dataProvider = "shortBinaryOpProvider") 536 static void andShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 537 short[] a = fa.apply(SPECIES.length()); 538 short[] b = fb.apply(SPECIES.length()); 539 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 544 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 545 av.and(bv).intoArray(r, i); 546 } 547 } 548 549 assertArraysEquals(a, b, r, ShortMaxVectorTests::and); 550 } 551 552 553 554 @Test(dataProvider = "shortBinaryOpMaskProvider") 555 static void andShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb, 556 IntFunction<boolean[]> fm) { 557 short[] a = fa.apply(SPECIES.length()); 558 short[] b = fb.apply(SPECIES.length()); 559 short[] r = fr.apply(SPECIES.length()); 560 boolean[] mask = fm.apply(SPECIES.length()); 561 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 566 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 567 av.and(bv, vmask).intoArray(r, i); 568 } 569 } 570 571 assertArraysEquals(a, b, r, mask, ShortMaxVectorTests::and); 572 } 573 574 575 static short or(short a, short b) { 576 return (short)(a | b); 577 } 578 579 @Test(dataProvider = "shortBinaryOpProvider") 580 static void orShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 581 short[] a = fa.apply(SPECIES.length()); 582 short[] b = fb.apply(SPECIES.length()); 583 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 588 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 589 av.or(bv).intoArray(r, i); 590 } 591 } 592 593 assertArraysEquals(a, b, r, ShortMaxVectorTests::or); 594 } 595 596 597 598 @Test(dataProvider = "shortBinaryOpMaskProvider") 599 static void orShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb, 600 IntFunction<boolean[]> fm) { 601 short[] a = fa.apply(SPECIES.length()); 602 short[] b = fb.apply(SPECIES.length()); 603 short[] r = fr.apply(SPECIES.length()); 604 boolean[] mask = fm.apply(SPECIES.length()); 605 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 610 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 611 av.or(bv, vmask).intoArray(r, i); 612 } 613 } 614 615 assertArraysEquals(a, b, r, mask, ShortMaxVectorTests::or); 616 } 617 618 619 static short xor(short a, short b) { 620 return (short)(a ^ b); 621 } 622 623 @Test(dataProvider = "shortBinaryOpProvider") 624 static void xorShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 625 short[] a = fa.apply(SPECIES.length()); 626 short[] b = fb.apply(SPECIES.length()); 627 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 632 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 633 av.xor(bv).intoArray(r, i); 634 } 635 } 636 637 assertArraysEquals(a, b, r, ShortMaxVectorTests::xor); 638 } 639 640 641 642 @Test(dataProvider = "shortBinaryOpMaskProvider") 643 static void xorShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb, 644 IntFunction<boolean[]> fm) { 645 short[] a = fa.apply(SPECIES.length()); 646 short[] b = fb.apply(SPECIES.length()); 647 short[] r = fr.apply(SPECIES.length()); 648 boolean[] mask = fm.apply(SPECIES.length()); 649 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 654 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 655 av.xor(bv, vmask).intoArray(r, i); 656 } 657 } 658 659 assertArraysEquals(a, b, r, mask, ShortMaxVectorTests::xor); 660 } 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 static short aShiftR_unary(short a, short b) { 682 return (short)((a >> (b & 15))); 683 } 684 685 @Test(dataProvider = "shortBinaryOpProvider") 686 static void aShiftRShortMaxVectorTestsShift(IntFunction<short[]> fa, IntFunction<short[]> fb) { 687 short[] a = fa.apply(SPECIES.length()); 688 short[] b = fb.apply(SPECIES.length()); 689 short[] r = fr.apply(SPECIES.length()); 690 691 for (int ic = 0; ic < INVOC_COUNT; ic++) { 692 for (int i = 0; i < a.length; i += SPECIES.length()) { 693 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 694 av.aShiftR((int)b[i]).intoArray(r, i); 695 } 696 } 697 698 assertShiftArraysEquals(a, b, r, ShortMaxVectorTests::aShiftR_unary); 699 } 700 701 702 703 @Test(dataProvider = "shortBinaryOpMaskProvider") 704 static void aShiftRShortMaxVectorTestsShift(IntFunction<short[]> fa, IntFunction<short[]> fb, 705 IntFunction<boolean[]> fm) { 706 short[] a = fa.apply(SPECIES.length()); 707 short[] b = fb.apply(SPECIES.length()); 708 short[] r = fr.apply(SPECIES.length()); 709 boolean[] mask = fm.apply(SPECIES.length()); 710 Vector.Mask<Short> vmask = ShortVector.maskFromValues(SPECIES, mask); 711 712 for (int ic = 0; ic < INVOC_COUNT; ic++) { 713 for (int i = 0; i < a.length; i += SPECIES.length()) { 714 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 715 av.aShiftR((int)b[i], vmask).intoArray(r, i); 716 } 717 } 718 719 assertShiftArraysEquals(a, b, r, mask, ShortMaxVectorTests::aShiftR_unary); 720 } 721 722 723 static short shiftL_unary(short a, short b) { 724 return (short)((a << (b & 15))); 725 } 726 727 @Test(dataProvider = "shortBinaryOpProvider") 728 static void shiftLShortMaxVectorTestsShift(IntFunction<short[]> fa, IntFunction<short[]> fb) { 729 short[] a = fa.apply(SPECIES.length()); 730 short[] b = fb.apply(SPECIES.length()); 731 short[] r = fr.apply(SPECIES.length()); 732 733 for (int ic = 0; ic < INVOC_COUNT; ic++) { 734 for (int i = 0; i < a.length; i += SPECIES.length()) { 735 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 736 av.shiftL((int)b[i]).intoArray(r, i); 737 } 738 } 739 740 assertShiftArraysEquals(a, b, r, ShortMaxVectorTests::shiftL_unary); 741 } 742 743 744 745 @Test(dataProvider = "shortBinaryOpMaskProvider") 746 static void shiftLShortMaxVectorTestsShift(IntFunction<short[]> fa, IntFunction<short[]> fb, 747 IntFunction<boolean[]> fm) { 748 short[] a = fa.apply(SPECIES.length()); 749 short[] b = fb.apply(SPECIES.length()); 750 short[] r = fr.apply(SPECIES.length()); 751 boolean[] mask = fm.apply(SPECIES.length()); 752 Vector.Mask<Short> vmask = ShortVector.maskFromValues(SPECIES, mask); 753 754 for (int ic = 0; ic < INVOC_COUNT; ic++) { 755 for (int i = 0; i < a.length; i += SPECIES.length()) { 756 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 757 av.shiftL((int)b[i], vmask).intoArray(r, i); 758 } 759 } 760 761 assertShiftArraysEquals(a, b, r, mask, ShortMaxVectorTests::shiftL_unary); 762 } 763 764 765 static short shiftR_unary(short a, short b) { 766 return (short)(((a & 0xFFFF) >>> (b & 15))); 767 } 768 769 @Test(dataProvider = "shortBinaryOpProvider") 770 static void shiftRShortMaxVectorTestsShift(IntFunction<short[]> fa, IntFunction<short[]> fb) { 771 short[] a = fa.apply(SPECIES.length()); 772 short[] b = fb.apply(SPECIES.length()); 773 short[] r = fr.apply(SPECIES.length()); 774 775 for (int ic = 0; ic < INVOC_COUNT; ic++) { 776 for (int i = 0; i < a.length; i += SPECIES.length()) { 777 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 778 av.shiftR((int)b[i]).intoArray(r, i); 779 } 780 } 781 782 assertShiftArraysEquals(a, b, r, ShortMaxVectorTests::shiftR_unary); 783 } 784 785 786 787 @Test(dataProvider = "shortBinaryOpMaskProvider") 788 static void shiftRShortMaxVectorTestsShift(IntFunction<short[]> fa, IntFunction<short[]> fb, 789 IntFunction<boolean[]> fm) { 790 short[] a = fa.apply(SPECIES.length()); 791 short[] b = fb.apply(SPECIES.length()); 792 short[] r = fr.apply(SPECIES.length()); 793 boolean[] mask = fm.apply(SPECIES.length()); 794 Vector.Mask<Short> vmask = ShortVector.maskFromValues(SPECIES, mask); 795 796 for (int ic = 0; ic < INVOC_COUNT; ic++) { 797 for (int i = 0; i < a.length; i += SPECIES.length()) { 798 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 799 av.shiftR((int)b[i], vmask).intoArray(r, i); 800 } 801 } 802 803 assertShiftArraysEquals(a, b, r, mask, ShortMaxVectorTests::shiftR_unary); 804 } 805 806 static short max(short a, short b) { 807 return (short)(Math.max(a, b)); 808 } 809 810 @Test(dataProvider = "shortBinaryOpProvider") 811 static void maxShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 812 short[] a = fa.apply(SPECIES.length()); 813 short[] b = fb.apply(SPECIES.length()); 814 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 819 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 820 av.max(bv).intoArray(r, i); 821 } 822 } 823 824 assertArraysEquals(a, b, r, ShortMaxVectorTests::max); 825 } 826 static short min(short a, short b) { 827 return (short)(Math.min(a, b)); 828 } 829 830 @Test(dataProvider = "shortBinaryOpProvider") 831 static void minShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 832 short[] a = fa.apply(SPECIES.length()); 833 short[] b = fb.apply(SPECIES.length()); 834 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 839 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 840 av.min(bv).intoArray(r, i); 841 } 842 } 843 844 assertArraysEquals(a, b, r, ShortMaxVectorTests::min); 845 } 846 847 static short andAll(short[] a, int idx) { 848 short 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 short andAll(short[] a) { 857 short res = -1; 858 for (int i = 0; i < a.length; i += SPECIES.length()) { 859 short 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 = "shortUnaryOpProvider") 871 static void andAllShortMaxVectorTests(IntFunction<short[]> fa) { 872 short[] a = fa.apply(SPECIES.length()); 873 short[] r = fr.apply(SPECIES.length()); 874 short ra = -1; 875 876 for (int ic = 0; ic < INVOC_COUNT; ic++) { 877 for (int i = 0; i < a.length; i += SPECIES.length()) { 878 ShortVector av = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 887 ra &= av.andAll(); 888 } 889 } 890 891 assertReductionArraysEquals(a, r, ra, ShortMaxVectorTests::andAll, ShortMaxVectorTests::andAll); 892 } 893 894 895 static short orAll(short[] a, int idx) { 896 short 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 short orAll(short[] a) { 905 short res = 0; 906 for (int i = 0; i < a.length; i += SPECIES.length()) { 907 short 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 = "shortUnaryOpProvider") 919 static void orAllShortMaxVectorTests(IntFunction<short[]> fa) { 920 short[] a = fa.apply(SPECIES.length()); 921 short[] r = fr.apply(SPECIES.length()); 922 short ra = 0; 923 924 for (int ic = 0; ic < INVOC_COUNT; ic++) { 925 for (int i = 0; i < a.length; i += SPECIES.length()) { 926 ShortVector av = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 935 ra |= av.orAll(); 936 } 937 } 938 939 assertReductionArraysEquals(a, r, ra, ShortMaxVectorTests::orAll, ShortMaxVectorTests::orAll); 940 } 941 942 943 static short xorAll(short[] a, int idx) { 944 short 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 short xorAll(short[] a) { 953 short res = 0; 954 for (int i = 0; i < a.length; i += SPECIES.length()) { 955 short 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 = "shortUnaryOpProvider") 967 static void xorAllShortMaxVectorTests(IntFunction<short[]> fa) { 968 short[] a = fa.apply(SPECIES.length()); 969 short[] r = fr.apply(SPECIES.length()); 970 short ra = 0; 971 972 for (int ic = 0; ic < INVOC_COUNT; ic++) { 973 for (int i = 0; i < a.length; i += SPECIES.length()) { 974 ShortVector av = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 983 ra ^= av.xorAll(); 984 } 985 } 986 987 assertReductionArraysEquals(a, r, ra, ShortMaxVectorTests::xorAll, ShortMaxVectorTests::xorAll); 988 } 989 990 static short addAll(short[] a, int idx) { 991 short 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 short addAll(short[] a) { 1000 short res = 0; 1001 for (int i = 0; i < a.length; i += SPECIES.length()) { 1002 short 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 = "shortUnaryOpProvider") 1012 static void addAllShortMaxVectorTests(IntFunction<short[]> fa) { 1013 short[] a = fa.apply(SPECIES.length()); 1014 short[] r = fr.apply(SPECIES.length()); 1015 short ra = 0; 1016 1017 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1018 for (int i = 0; i < a.length; i += SPECIES.length()) { 1019 ShortVector av = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1028 ra += av.addAll(); 1029 } 1030 } 1031 1032 assertReductionArraysEquals(a, r, ra, ShortMaxVectorTests::addAll, ShortMaxVectorTests::addAll); 1033 } 1034 static short mulAll(short[] a, int idx) { 1035 short 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 short mulAll(short[] a) { 1044 short res = 1; 1045 for (int i = 0; i < a.length; i += SPECIES.length()) { 1046 short 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 = "shortUnaryOpProvider") 1056 static void mulAllShortMaxVectorTests(IntFunction<short[]> fa) { 1057 short[] a = fa.apply(SPECIES.length()); 1058 short[] r = fr.apply(SPECIES.length()); 1059 short ra = 1; 1060 1061 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1062 for (int i = 0; i < a.length; i += SPECIES.length()) { 1063 ShortVector av = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1072 ra *= av.mulAll(); 1073 } 1074 } 1075 1076 assertReductionArraysEquals(a, r, ra, ShortMaxVectorTests::mulAll, ShortMaxVectorTests::mulAll); 1077 } 1078 static short minAll(short[] a, int idx) { 1079 short res = Short.MAX_VALUE; 1080 for (int i = idx; i < (idx + SPECIES.length()); i++) { 1081 res = (short)Math.min(res, a[i]); 1082 } 1083 1084 return res; 1085 } 1086 1087 static short minAll(short[] a) { 1088 short res = Short.MAX_VALUE; 1089 for (int i = 0; i < a.length; i++) { 1090 res = (short)Math.min(res, a[i]); 1091 } 1092 1093 return res; 1094 } 1095 @Test(dataProvider = "shortUnaryOpProvider") 1096 static void minAllShortMaxVectorTests(IntFunction<short[]> fa) { 1097 short[] a = fa.apply(SPECIES.length()); 1098 short[] r = fr.apply(SPECIES.length()); 1099 short ra = Short.MAX_VALUE; 1100 1101 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1102 for (int i = 0; i < a.length; i += SPECIES.length()) { 1103 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1104 r[i] = av.minAll(); 1105 } 1106 } 1107 1108 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1109 ra = Short.MAX_VALUE; 1110 for (int i = 0; i < a.length; i += SPECIES.length()) { 1111 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1112 ra = (short)Math.min(ra, av.minAll()); 1113 } 1114 } 1115 1116 assertReductionArraysEquals(a, r, ra, ShortMaxVectorTests::minAll, ShortMaxVectorTests::minAll); 1117 } 1118 static short maxAll(short[] a, int idx) { 1119 short res = Short.MIN_VALUE; 1120 for (int i = idx; i < (idx + SPECIES.length()); i++) { 1121 res = (short)Math.max(res, a[i]); 1122 } 1123 1124 return res; 1125 } 1126 1127 static short maxAll(short[] a) { 1128 short res = Short.MIN_VALUE; 1129 for (int i = 0; i < a.length; i++) { 1130 res = (short)Math.max(res, a[i]); 1131 } 1132 1133 return res; 1134 } 1135 @Test(dataProvider = "shortUnaryOpProvider") 1136 static void maxAllShortMaxVectorTests(IntFunction<short[]> fa) { 1137 short[] a = fa.apply(SPECIES.length()); 1138 short[] r = fr.apply(SPECIES.length()); 1139 short ra = Short.MIN_VALUE; 1140 1141 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1142 for (int i = 0; i < a.length; i += SPECIES.length()) { 1143 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1144 r[i] = av.maxAll(); 1145 } 1146 } 1147 1148 for (int ic = 0; ic < INVOC_COUNT; ic++) { 1149 ra = Short.MIN_VALUE; 1150 for (int i = 0; i < a.length; i += SPECIES.length()) { 1151 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1152 ra = (short)Math.max(ra, av.maxAll()); 1153 } 1154 } 1155 1156 assertReductionArraysEquals(a, r, ra, ShortMaxVectorTests::maxAll, ShortMaxVectorTests::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 anyTrueShortMaxVectorTests(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<Short> vmask = ShortVector.maskFromArray(SPECIES, mask, i); 1177 r[i] = vmask.anyTrue(); 1178 } 1179 } 1180 1181 assertReductionBoolArraysEquals(mask, r, ShortMaxVectorTests::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 allTrueShortMaxVectorTests(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<Short> vmask = ShortVector.maskFromArray(SPECIES, mask, i); 1203 r[i] = vmask.allTrue(); 1204 } 1205 } 1206 1207 assertReductionBoolArraysEquals(mask, r, ShortMaxVectorTests::allTrue); 1208 } 1209 1210 1211 @Test(dataProvider = "shortUnaryOpProvider") 1212 static void withShortMaxVectorTests(IntFunction<short []> fa) { 1213 short[] a = fa.apply(SPECIES.length()); 1214 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1219 av.with(0, (short)4).intoArray(r, i); 1220 } 1221 } 1222 1223 assertInsertArraysEquals(a, r, (short)4, 0); 1224 } 1225 1226 @Test(dataProvider = "shortCompareOpProvider") 1227 static void lessThanShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 1228 short[] a = fa.apply(SPECIES.length()); 1229 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1234 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 1235 Vector.Mask<Short> 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 = "shortCompareOpProvider") 1247 static void greaterThanShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 1248 short[] a = fa.apply(SPECIES.length()); 1249 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1254 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 1255 Vector.Mask<Short> 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 = "shortCompareOpProvider") 1267 static void equalShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 1268 short[] a = fa.apply(SPECIES.length()); 1269 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1274 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 1275 Vector.Mask<Short> 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 = "shortCompareOpProvider") 1287 static void notEqualShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 1288 short[] a = fa.apply(SPECIES.length()); 1289 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1294 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 1295 Vector.Mask<Short> 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 = "shortCompareOpProvider") 1307 static void lessThanEqShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 1308 short[] a = fa.apply(SPECIES.length()); 1309 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1314 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 1315 Vector.Mask<Short> 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 = "shortCompareOpProvider") 1327 static void greaterThanEqShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) { 1328 short[] a = fa.apply(SPECIES.length()); 1329 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1334 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 1335 Vector.Mask<Short> 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 short blend(short a, short b, boolean mask) { 1347 return mask ? b : a; 1348 } 1349 1350 @Test(dataProvider = "shortBinaryOpMaskProvider") 1351 static void blendShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb, 1352 IntFunction<boolean[]> fm) { 1353 short[] a = fa.apply(SPECIES.length()); 1354 short[] b = fb.apply(SPECIES.length()); 1355 short[] r = fr.apply(SPECIES.length()); 1356 boolean[] mask = fm.apply(SPECIES.length()); 1357 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1362 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 1363 av.blend(bv, vmask).intoArray(r, i); 1364 } 1365 } 1366 1367 assertArraysEquals(a, b, r, mask, ShortMaxVectorTests::blend); 1368 } 1369 1370 @Test(dataProvider = "shortUnaryOpShuffleProvider") 1371 static void RearrangeShortMaxVectorTests(IntFunction<short[]> fa, 1372 BiFunction<Integer,Integer,int[]> fs) { 1373 short[] a = fa.apply(SPECIES.length()); 1374 int[] order = fs.apply(a.length, SPECIES.length()); 1375 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1380 av.rearrange(ShortVector.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 = "shortUnaryOpProvider") 1391 static void getShortMaxVectorTests(IntFunction<short[]> fa) { 1392 short[] a = fa.apply(SPECIES.length()); 1393 short[] 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 ShortVector av = ShortVector.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, ShortMaxVectorTests::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 short neg(short a) { 1568 return (short)(-((short)a)); 1569 } 1570 1571 @Test(dataProvider = "shortUnaryOpProvider") 1572 static void negShortMaxVectorTests(IntFunction<short[]> fa) { 1573 short[] a = fa.apply(SPECIES.length()); 1574 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1579 av.neg().intoArray(r, i); 1580 } 1581 } 1582 1583 assertArraysEquals(a, r, ShortMaxVectorTests::neg); 1584 } 1585 1586 @Test(dataProvider = "shortUnaryOpMaskProvider") 1587 static void negMaskedShortMaxVectorTests(IntFunction<short[]> fa, 1588 IntFunction<boolean[]> fm) { 1589 short[] a = fa.apply(SPECIES.length()); 1590 short[] r = fr.apply(SPECIES.length()); 1591 boolean[] mask = fm.apply(SPECIES.length()); 1592 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1597 av.neg(vmask).intoArray(r, i); 1598 } 1599 } 1600 1601 assertArraysEquals(a, r, mask, ShortMaxVectorTests::neg); 1602 } 1603 1604 static short abs(short a) { 1605 return (short)(Math.abs((short)a)); 1606 } 1607 1608 @Test(dataProvider = "shortUnaryOpProvider") 1609 static void absShortMaxVectorTests(IntFunction<short[]> fa) { 1610 short[] a = fa.apply(SPECIES.length()); 1611 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1616 av.abs().intoArray(r, i); 1617 } 1618 } 1619 1620 assertArraysEquals(a, r, ShortMaxVectorTests::abs); 1621 } 1622 1623 @Test(dataProvider = "shortUnaryOpMaskProvider") 1624 static void absMaskedShortMaxVectorTests(IntFunction<short[]> fa, 1625 IntFunction<boolean[]> fm) { 1626 short[] a = fa.apply(SPECIES.length()); 1627 short[] r = fr.apply(SPECIES.length()); 1628 boolean[] mask = fm.apply(SPECIES.length()); 1629 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1634 av.abs(vmask).intoArray(r, i); 1635 } 1636 } 1637 1638 assertArraysEquals(a, r, mask, ShortMaxVectorTests::abs); 1639 } 1640 1641 1642 static short not(short a) { 1643 return (short)(~((short)a)); 1644 } 1645 1646 1647 1648 @Test(dataProvider = "shortUnaryOpProvider") 1649 static void notShortMaxVectorTests(IntFunction<short[]> fa) { 1650 short[] a = fa.apply(SPECIES.length()); 1651 short[] 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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1656 av.not().intoArray(r, i); 1657 } 1658 } 1659 1660 assertArraysEquals(a, r, ShortMaxVectorTests::not); 1661 } 1662 1663 1664 1665 @Test(dataProvider = "shortUnaryOpMaskProvider") 1666 static void notMaskedShortMaxVectorTests(IntFunction<short[]> fa, 1667 IntFunction<boolean[]> fm) { 1668 short[] a = fa.apply(SPECIES.length()); 1669 short[] r = fr.apply(SPECIES.length()); 1670 boolean[] mask = fm.apply(SPECIES.length()); 1671 Vector.Mask<Short> vmask = ShortVector.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 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 1676 av.not(vmask).intoArray(r, i); 1677 } 1678 } 1679 1680 assertArraysEquals(a, r, mask, ShortMaxVectorTests::not); 1681 } 1682 1683 1684 1685 1686 1687 1688 } 1689