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