1 /*
   2  * Copyright (c) 2017, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have
  23  * questions.
  24  */
  25 package jdk.incubator.vector;
  26 
  27 import java.nio.ByteBuffer;
  28 import java.nio.ShortBuffer;
  29 import java.nio.ByteOrder;
  30 import java.util.Objects;
  31 import java.util.function.IntUnaryOperator;
  32 import java.util.function.Function;
  33 import java.util.concurrent.ThreadLocalRandom;
  34 
  35 import jdk.internal.misc.Unsafe;
  36 import jdk.internal.vm.annotation.ForceInline;
  37 import static jdk.incubator.vector.VectorIntrinsics.*;
  38 
  39 
  40 /**
  41  * A specialized {@link Vector} representing an ordered immutable sequence of
  42  * {@code short} values.
  43  */
  44 @SuppressWarnings("cast")
  45 public abstract class ShortVector extends Vector<Short> {
  46 
  47     ShortVector() {}
  48 
  49     private static final int ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(Unsafe.ARRAY_SHORT_INDEX_SCALE);
  50 
  51     // Unary operator
  52 
  53     interface FUnOp {
  54         short apply(int i, short a);
  55     }
  56 
  57     abstract ShortVector uOp(FUnOp f);
  58 
  59     abstract ShortVector uOp(Mask<Short> m, FUnOp f);
  60 
  61     // Binary operator
  62 
  63     interface FBinOp {
  64         short apply(int i, short a, short b);
  65     }
  66 
  67     abstract ShortVector bOp(Vector<Short> v, FBinOp f);
  68 
  69     abstract ShortVector bOp(Vector<Short> v, Mask<Short> m, FBinOp f);
  70 
  71     // Trinary operator
  72 
  73     interface FTriOp {
  74         short apply(int i, short a, short b, short c);
  75     }
  76 
  77     abstract ShortVector tOp(Vector<Short> v1, Vector<Short> v2, FTriOp f);
  78 
  79     abstract ShortVector tOp(Vector<Short> v1, Vector<Short> v2, Mask<Short> m, FTriOp f);
  80 
  81     // Reduction operator
  82 
  83     abstract short rOp(short v, FBinOp f);
  84 
  85     // Binary test
  86 
  87     interface FBinTest {
  88         boolean apply(int i, short a, short b);
  89     }
  90 
  91     abstract Mask<Short> bTest(Vector<Short> v, FBinTest f);
  92 
  93     // Foreach
  94 
  95     interface FUnCon {
  96         void apply(int i, short a);
  97     }
  98 
  99     abstract void forEach(FUnCon f);
 100 
 101     abstract void forEach(Mask<Short> m, FUnCon f);
 102 
 103     // Static factories
 104 
 105     /**
 106      * Returns a vector where all lane elements are set to the default
 107      * primitive value.
 108      *
 109      * @param species species of desired vector
 110      * @return a zero vector of given species
 111      */
 112     @ForceInline
 113     @SuppressWarnings("unchecked")
 114     public static ShortVector zero(Species<Short> species) {
 115         return VectorIntrinsics.broadcastCoerced((Class<ShortVector>) species.boxType(), short.class, species.length(),
 116                                                  0, species,
 117                                                  ((bits, s) -> ((ShortSpecies)s).op(i -> (short)bits)));
 118     }
 119 
 120     /**
 121      * Loads a vector from a byte array starting at an offset.
 122      * <p>
 123      * Bytes are composed into primitive lane elements according to the
 124      * native byte order of the underlying platform
 125      * <p>
 126      * This method behaves as if it returns the result of calling the
 127      * byte buffer, offset, and mask accepting
 128      * {@link #fromByteBuffer(Species<Short>, ByteBuffer, int, Mask) method} as follows:
 129      * <pre>{@code
 130      * return this.fromByteBuffer(ByteBuffer.wrap(a), i, this.maskAllTrue());
 131      * }</pre>
 132      *
 133      * @param species species of desired vector
 134      * @param a the byte array
 135      * @param ix the offset into the array
 136      * @return a vector loaded from a byte array
 137      * @throws IndexOutOfBoundsException if {@code i < 0} or
 138      * {@code i > a.length - (this.length() * this.elementSize() / Byte.SIZE)}
 139      */
 140     @ForceInline
 141     @SuppressWarnings("unchecked")
 142     public static ShortVector fromByteArray(Species<Short> species, byte[] a, int ix) {
 143         Objects.requireNonNull(a);
 144         ix = VectorIntrinsics.checkIndex(ix, a.length, species.bitSize() / Byte.SIZE);
 145         return VectorIntrinsics.load((Class<ShortVector>) species.boxType(), short.class, species.length(),
 146                                      a, ((long) ix) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
 147                                      a, ix, species,
 148                                      (c, idx, s) -> {
 149                                          ByteBuffer bbc = ByteBuffer.wrap(c, idx, a.length - idx).order(ByteOrder.nativeOrder());
 150                                          ShortBuffer tb = bbc.asShortBuffer();
 151                                          return ((ShortSpecies)s).op(i -> tb.get());
 152                                      });
 153     }
 154 
 155     /**
 156      * Loads a vector from a byte array starting at an offset and using a
 157      * mask.
 158      * <p>
 159      * Bytes are composed into primitive lane elements according to the
 160      * native byte order of the underlying platform.
 161      * <p>
 162      * This method behaves as if it returns the result of calling the
 163      * byte buffer, offset, and mask accepting
 164      * {@link #fromByteBuffer(Species<Short>, ByteBuffer, int, Mask) method} as follows:
 165      * <pre>{@code
 166      * return this.fromByteBuffer(ByteBuffer.wrap(a), i, m);
 167      * }</pre>
 168      *
 169      * @param species species of desired vector
 170      * @param a the byte array
 171      * @param ix the offset into the array
 172      * @param m the mask
 173      * @return a vector loaded from a byte array
 174      * @throws IndexOutOfBoundsException if {@code i < 0} or
 175      * {@code i > a.length - (this.length() * this.elementSize() / Byte.SIZE)}
 176      * @throws IndexOutOfBoundsException if the offset is {@code < 0},
 177      * or {@code > a.length},
 178      * for any vector lane index {@code N} where the mask at lane {@code N}
 179      * is set
 180      * {@code i >= a.length - (N * this.elementSize() / Byte.SIZE)}
 181      */
 182     @ForceInline
 183     public static ShortVector fromByteArray(Species<Short> species, byte[] a, int ix, Mask<Short> m) {
 184         return zero(species).blend(fromByteArray(species, a, ix), m);
 185     }
 186 
 187     /**
 188      * Loads a vector from an array starting at offset.
 189      * <p>
 190      * For each vector lane, where {@code N} is the vector lane index, the
 191      * array element at index {@code i + N} is placed into the
 192      * resulting vector at lane index {@code N}.
 193      *
 194      * @param species species of desired vector
 195      * @param a the array
 196      * @param i the offset into the array
 197      * @return the vector loaded from an array
 198      * @throws IndexOutOfBoundsException if {@code i < 0}, or
 199      * {@code i > a.length - this.length()}
 200      */
 201     @ForceInline
 202     @SuppressWarnings("unchecked")
 203     public static ShortVector fromArray(Species<Short> species, short[] a, int i){
 204         Objects.requireNonNull(a);
 205         i = VectorIntrinsics.checkIndex(i, a.length, species.length());
 206         return VectorIntrinsics.load((Class<ShortVector>) species.boxType(), short.class, species.length(),
 207                                      a, (((long) i) << ARRAY_SHIFT) + Unsafe.ARRAY_SHORT_BASE_OFFSET,
 208                                      a, i, species,
 209                                      (c, idx, s) -> ((ShortSpecies)s).op(n -> c[idx + n]));
 210     }
 211 
 212 
 213     /**
 214      * Loads a vector from an array starting at offset and using a mask.
 215      * <p>
 216      * For each vector lane, where {@code N} is the vector lane index,
 217      * if the mask lane at index {@code N} is set then the array element at
 218      * index {@code i + N} is placed into the resulting vector at lane index
 219      * {@code N}, otherwise the default element value is placed into the
 220      * resulting vector at lane index {@code N}.
 221      *
 222      * @param species species of desired vector
 223      * @param a the array
 224      * @param i the offset into the array
 225      * @param m the mask
 226      * @return the vector loaded from an array
 227      * @throws IndexOutOfBoundsException if {@code i < 0}, or
 228      * for any vector lane index {@code N} where the mask at lane {@code N}
 229      * is set {@code i > a.length - N}
 230      */
 231     @ForceInline
 232     public static ShortVector fromArray(Species<Short> species, short[] a, int i, Mask<Short> m) {
 233         return zero(species).blend(fromArray(species, a, i), m);
 234     }
 235 
 236     /**
 237      * Loads a vector from an array using indexes obtained from an index
 238      * map.
 239      * <p>
 240      * For each vector lane, where {@code N} is the vector lane index, the
 241      * array element at index {@code i + indexMap[j + N]} is placed into the
 242      * resulting vector at lane index {@code N}.
 243      *
 244      * @param species species of desired vector
 245      * @param a the array
 246      * @param i the offset into the array, may be negative if relative
 247      * indexes in the index map compensate to produce a value within the
 248      * array bounds
 249      * @param indexMap the index map
 250      * @param j the offset into the index map
 251      * @return the vector loaded from an array
 252      * @throws IndexOutOfBoundsException if {@code j < 0}, or
 253      * {@code j > indexMap.length - this.length()},
 254      * or for any vector lane index {@code N} the result of
 255      * {@code i + indexMap[j + N]} is {@code < 0} or {@code >= a.length}
 256      */
 257     public static ShortVector fromArray(Species<Short> species, short[] a, int i, int[] indexMap, int j) {
 258         return ((ShortSpecies)species).op(n -> a[i + indexMap[j + n]]);
 259     }
 260     /**
 261      * Loads a vector from an array using indexes obtained from an index
 262      * map and using a mask.
 263      * <p>
 264      * For each vector lane, where {@code N} is the vector lane index,
 265      * if the mask lane at index {@code N} is set then the array element at
 266      * index {@code i + indexMap[j + N]} is placed into the resulting vector
 267      * at lane index {@code N}.
 268      *
 269      * @param species species of desired vector
 270      * @param a the array
 271      * @param i the offset into the array, may be negative if relative
 272      * indexes in the index map compensate to produce a value within the
 273      * array bounds
 274      * @param m the mask
 275      * @param indexMap the index map
 276      * @param j the offset into the index map
 277      * @return the vector loaded from an array
 278      * @throws IndexOutOfBoundsException if {@code j < 0}, or
 279      * {@code j > indexMap.length - this.length()},
 280      * or for any vector lane index {@code N} where the mask at lane
 281      * {@code N} is set the result of {@code i + indexMap[j + N]} is
 282      * {@code < 0} or {@code >= a.length}
 283      */
 284     public static ShortVector fromArray(Species<Short> species, short[] a, int i, Mask<Short> m, int[] indexMap, int j) {
 285         return ((ShortSpecies)species).op(m, n -> a[i + indexMap[j + n]]);
 286     }
 287 
 288     /**
 289      * Loads a vector from a {@link ByteBuffer byte buffer} starting at an
 290      * offset into the byte buffer.
 291      * <p>
 292      * Bytes are composed into primitive lane elements according to the
 293      * native byte order of the underlying platform.
 294      * <p>
 295      * This method behaves as if it returns the result of calling the
 296      * byte buffer, offset, and mask accepting
 297      * {@link #fromByteBuffer(Species<Short>, ByteBuffer, int, Mask)} method} as follows:
 298      * <pre>{@code
 299      *   return this.fromByteBuffer(b, i, this.maskAllTrue())
 300      * }</pre>
 301      *
 302      * @param species species of desired vector
 303      * @param bb the byte buffer
 304      * @param ix the offset into the byte buffer
 305      * @return a vector loaded from a byte buffer
 306      * @throws IndexOutOfBoundsException if the offset is {@code < 0},
 307      * or {@code > b.limit()},
 308      * or if there are fewer than
 309      * {@code this.length() * this.elementSize() / Byte.SIZE} bytes
 310      * remaining in the byte buffer from the given offset
 311      */
 312     @ForceInline
 313     @SuppressWarnings("unchecked")
 314     public static ShortVector fromByteBuffer(Species<Short> species, ByteBuffer bb, int ix) {
 315         if (bb.order() != ByteOrder.nativeOrder()) {
 316             throw new IllegalArgumentException();
 317         }
 318         ix = VectorIntrinsics.checkIndex(ix, bb.limit(), species.bitSize() / Byte.SIZE);
 319         return VectorIntrinsics.load((Class<ShortVector>) species.boxType(), short.class, species.length(),
 320                                      U.getReference(bb, BYTE_BUFFER_HB), U.getLong(bb, BUFFER_ADDRESS) + ix,
 321                                      bb, ix, species,
 322                                      (c, idx, s) -> {
 323                                          ByteBuffer bbc = c.duplicate().position(idx).order(ByteOrder.nativeOrder());
 324                                          ShortBuffer tb = bbc.asShortBuffer();
 325                                          return ((ShortSpecies)s).op(i -> tb.get());
 326                                      });
 327     }
 328 
 329     /**
 330      * Loads a vector from a {@link ByteBuffer byte buffer} starting at an
 331      * offset into the byte buffer and using a mask.
 332      * <p>
 333      * This method behaves as if the byte buffer is viewed as a primitive
 334      * {@link java.nio.Buffer buffer} for the primitive element type,
 335      * according to the native byte order of the underlying platform, and
 336      * the returned vector is loaded with a mask from a primitive array
 337      * obtained from the primitive buffer.
 338      * The following pseudocode expresses the behaviour, where
 339      * {@coce EBuffer} is the primitive buffer type, {@code e} is the
 340      * primitive element type, and {@code ESpecies<S>} is the primitive
 341      * species for {@code e}:
 342      * <pre>{@code
 343      * EBuffer eb = b.duplicate().
 344      *     order(ByteOrder.nativeOrder()).position(i).
 345      *     asEBuffer();
 346      * e[] es = new e[this.length()];
 347      * for (int n = 0; n < t.length; n++) {
 348      *     if (m.isSet(n))
 349      *         es[n] = eb.get(n);
 350      * }
 351      * Vector<E> r = ((ESpecies<S>)this).fromArray(es, 0, m);
 352      * }</pre>
 353      *
 354      * @param species species of desired vector
 355      * @param bb the byte buffer
 356      * @param ix the offset into the byte buffer
 357      * @param m the mask
 358      * @return a vector loaded from a byte buffer
 359      * @throws IndexOutOfBoundsException if the offset is {@code < 0},
 360      * or {@code > b.limit()},
 361      * for any vector lane index {@code N} where the mask at lane {@code N}
 362      * is set
 363      * {@code i >= b.limit() - (N * this.elementSize() / Byte.SIZE)}
 364      */
 365     @ForceInline
 366     public static ShortVector fromByteBuffer(Species<Short> species, ByteBuffer bb, int ix, Mask<Short> m) {
 367         return zero(species).blend(fromByteBuffer(species, bb, ix), m);
 368     }
 369 
 370     /**
 371      * Returns a vector where all lane elements are set to the primitive
 372      * value {@code e}.
 373      *
 374      * @param s species of the desired vector
 375      * @param e the value
 376      * @return a vector of vector where all lane elements are set to
 377      * the primitive value {@code e}
 378      */
 379     @ForceInline
 380     @SuppressWarnings("unchecked")
 381     public static ShortVector broadcast(Species<Short> s, short e) {
 382         return VectorIntrinsics.broadcastCoerced(
 383             (Class<ShortVector>) s.boxType(), short.class, s.length(),
 384             e, s,
 385             ((bits, sp) -> ((ShortSpecies)sp).op(i -> (short)bits)));
 386     }
 387 
 388     /**
 389      * Returns a vector where each lane element is set to a given
 390      * primitive value.
 391      * <p>
 392      * For each vector lane, where {@code N} is the vector lane index, the
 393      * the primitive value at index {@code N} is placed into the resulting
 394      * vector at lane index {@code N}.
 395      *
 396      * @param s species of the desired vector
 397      * @param es the given primitive values
 398      * @return a vector where each lane element is set to a given primitive
 399      * value
 400      * @throws IndexOutOfBoundsException if {@code es.length < this.length()}
 401      */
 402     @ForceInline
 403     @SuppressWarnings("unchecked")
 404     public static ShortVector scalars(Species<Short> s, short... es) {
 405         Objects.requireNonNull(es);
 406         int ix = VectorIntrinsics.checkIndex(0, es.length, s.length());
 407         return VectorIntrinsics.load((Class<ShortVector>) s.boxType(), short.class, s.length(),
 408                                      es, Unsafe.ARRAY_SHORT_BASE_OFFSET,
 409                                      es, ix, s,
 410                                      (c, idx, sp) -> ((ShortSpecies)sp).op(n -> c[idx + n]));
 411     }
 412 
 413     /**
 414      * Returns a vector where the first lane element is set to the primtive
 415      * value {@code e}, all other lane elements are set to the default
 416      * value.
 417      *
 418      * @param s species of the desired vector
 419      * @param e the value
 420      * @return a vector where the first lane element is set to the primitive
 421      * value {@code e}
 422      */
 423     @ForceInline
 424     public static final ShortVector single(Species<Short> s, short e) {
 425         return zero(s).with(0, e);
 426     }
 427 
 428     /**
 429      * Returns a vector where each lane element is set to a randomly
 430      * generated primitive value.
 431      *
 432      * The semantics are equivalent to calling
 433      * (short){@link ThreadLocalRandom#nextInt()}
 434      *
 435      * @param s species of the desired vector
 436      * @return a vector where each lane elements is set to a randomly
 437      * generated primitive value
 438      */
 439     public static ShortVector random(Species<Short> s) {
 440         ThreadLocalRandom r = ThreadLocalRandom.current();
 441         return ((ShortSpecies)s).op(i -> (short) r.nextInt());
 442     }
 443 
 444     /**
 445      * Returns a mask where each lane is set or unset according to given
 446      * {@code boolean} values
 447      * <p>
 448      * For each mask lane, where {@code N} is the mask lane index,
 449      * if the given {@code boolean} value at index {@code N} is {@code true}
 450      * then the mask lane at index {@code N} is set, otherwise it is unset.
 451      *
 452      * @param species mask species
 453      * @param bits the given {@code boolean} values
 454      * @return a mask where each lane is set or unset according to the given {@code boolean} value
 455      * @throws IndexOutOfBoundsException if {@code bits.length < species.length()}
 456      */
 457     @ForceInline
 458     public static Mask<Short> maskFromValues(Species<Short> species, boolean... bits) {
 459         if (species.boxType() == ShortMaxVector.class)
 460             return new ShortMaxVector.ShortMaxMask(bits);
 461         switch (species.bitSize()) {
 462             case 64: return new Short64Vector.Short64Mask(bits);
 463             case 128: return new Short128Vector.Short128Mask(bits);
 464             case 256: return new Short256Vector.Short256Mask(bits);
 465             case 512: return new Short512Vector.Short512Mask(bits);
 466             default: throw new IllegalArgumentException(Integer.toString(species.bitSize()));
 467         }
 468     }
 469 
 470     // @@@ This is a bad implementation -- makes lambdas capturing -- fix this
 471     static Mask<Short> trueMask(Species<Short> species) {
 472         if (species.boxType() == ShortMaxVector.class)
 473             return ShortMaxVector.ShortMaxMask.TRUE_MASK;
 474         switch (species.bitSize()) {
 475             case 64: return Short64Vector.Short64Mask.TRUE_MASK;
 476             case 128: return Short128Vector.Short128Mask.TRUE_MASK;
 477             case 256: return Short256Vector.Short256Mask.TRUE_MASK;
 478             case 512: return Short512Vector.Short512Mask.TRUE_MASK;
 479             default: throw new IllegalArgumentException(Integer.toString(species.bitSize()));
 480         }
 481     }
 482 
 483     static Mask<Short> falseMask(Species<Short> species) {
 484         if (species.boxType() == ShortMaxVector.class)
 485             return ShortMaxVector.ShortMaxMask.FALSE_MASK;
 486         switch (species.bitSize()) {
 487             case 64: return Short64Vector.Short64Mask.FALSE_MASK;
 488             case 128: return Short128Vector.Short128Mask.FALSE_MASK;
 489             case 256: return Short256Vector.Short256Mask.FALSE_MASK;
 490             case 512: return Short512Vector.Short512Mask.FALSE_MASK;
 491             default: throw new IllegalArgumentException(Integer.toString(species.bitSize()));
 492         }
 493     }
 494 
 495     /**
 496      * Loads a mask from a {@code boolean} array starting at an offset.
 497      * <p>
 498      * For each mask lane, where {@code N} is the mask lane index,
 499      * if the array element at index {@code ix + N} is {@code true} then the
 500      * mask lane at index {@code N} is set, otherwise it is unset.
 501      *
 502      * @param species mask species
 503      * @param bits the {@code boolean} array
 504      * @param ix the offset into the array
 505      * @return the mask loaded from a {@code boolean} array
 506      * @throws IndexOutOfBoundsException if {@code ix < 0}, or
 507      * {@code ix > bits.length - species.length()}
 508      */
 509     @ForceInline
 510     @SuppressWarnings("unchecked")
 511     public static Mask<Short> maskFromArray(Species<Short> species, boolean[] bits, int ix) {
 512         Objects.requireNonNull(bits);
 513         ix = VectorIntrinsics.checkIndex(ix, bits.length, species.length());
 514         return VectorIntrinsics.load((Class<Mask<Short>>) species.maskType(), short.class, species.length(),
 515                                      bits, (((long) ix) << ARRAY_SHIFT) + Unsafe.ARRAY_BOOLEAN_BASE_OFFSET,
 516                                      bits, ix, species,
 517                                      (c, idx, s) -> (Mask<Short>) ((ShortSpecies)s).opm(n -> c[idx + n]));
 518     }
 519 
 520     /**
 521      * Returns a mask where all lanes are set.
 522      *
 523      * @param species mask species
 524      * @return a mask where all lanes are set
 525      */
 526     @ForceInline
 527     @SuppressWarnings("unchecked")
 528     public static Mask<Short> maskAllTrue(Species<Short> species) {
 529         return VectorIntrinsics.broadcastCoerced((Class<Mask<Short>>) species.maskType(), short.class, species.length(),
 530                                                  (short)-1,  species,
 531                                                  ((z, s) -> trueMask(s)));
 532     }
 533 
 534     /**
 535      * Returns a mask where all lanes are unset.
 536      *
 537      * @param species mask species
 538      * @return a mask where all lanes are unset
 539      */
 540     @ForceInline
 541     @SuppressWarnings("unchecked")
 542     public static Mask<Short> maskAllFalse(Species<Short> species) {
 543         return VectorIntrinsics.broadcastCoerced((Class<Mask<Short>>) species.maskType(), short.class, species.length(),
 544                                                  0, species, 
 545                                                  ((z, s) -> falseMask(s)));
 546     }
 547 
 548     /**
 549      * Returns a shuffle of mapped indexes where each lane element is
 550      * the result of applying a mapping function to the corresponding lane
 551      * index.
 552      * <p>
 553      * Care should be taken to ensure Shuffle values produced from this
 554      * method are consumed as constants to ensure optimal generation of
 555      * code.  For example, values held in static final fields or values
 556      * held in loop constant local variables.
 557      * <p>
 558      * This method behaves as if a shuffle is created from an array of
 559      * mapped indexes as follows:
 560      * <pre>{@code
 561      *   int[] a = new int[species.length()];
 562      *   for (int i = 0; i < a.length; i++) {
 563      *       a[i] = f.applyAsInt(i);
 564      *   }
 565      *   return this.shuffleFromValues(a);
 566      * }</pre>
 567      *
 568      * @param species shuffle species
 569      * @param f the lane index mapping function
 570      * @return a shuffle of mapped indexes
 571      */
 572     @ForceInline
 573     public static Shuffle<Short> shuffle(Species<Short> species, IntUnaryOperator f) {
 574         if (species.boxType() == ShortMaxVector.class)
 575             return new ShortMaxVector.ShortMaxShuffle(f);
 576         switch (species.bitSize()) {
 577             case 64: return new Short64Vector.Short64Shuffle(f);
 578             case 128: return new Short128Vector.Short128Shuffle(f);
 579             case 256: return new Short256Vector.Short256Shuffle(f);
 580             case 512: return new Short512Vector.Short512Shuffle(f);
 581             default: throw new IllegalArgumentException(Integer.toString(species.bitSize()));
 582         }
 583     }
 584 
 585     /**
 586      * Returns a shuffle where each lane element is the value of its
 587      * corresponding lane index.
 588      * <p>
 589      * This method behaves as if a shuffle is created from an identity
 590      * index mapping function as follows:
 591      * <pre>{@code
 592      *   return this.shuffle(i -> i);
 593      * }</pre>
 594      *
 595      * @param species shuffle species
 596      * @return a shuffle of lane indexes
 597      */
 598     @ForceInline
 599     public static Shuffle<Short> shuffleIota(Species<Short> species) {
 600         if (species.boxType() == ShortMaxVector.class)
 601             return new ShortMaxVector.ShortMaxShuffle(AbstractShuffle.IDENTITY);
 602         switch (species.bitSize()) {
 603             case 64: return new Short64Vector.Short64Shuffle(AbstractShuffle.IDENTITY);
 604             case 128: return new Short128Vector.Short128Shuffle(AbstractShuffle.IDENTITY);
 605             case 256: return new Short256Vector.Short256Shuffle(AbstractShuffle.IDENTITY);
 606             case 512: return new Short512Vector.Short512Shuffle(AbstractShuffle.IDENTITY);
 607             default: throw new IllegalArgumentException(Integer.toString(species.bitSize()));
 608         }
 609     }
 610 
 611     /**
 612      * Returns a shuffle where each lane element is set to a given
 613      * {@code int} value logically AND'ed by the species length minus one.
 614      * <p>
 615      * For each shuffle lane, where {@code N} is the shuffle lane index, the
 616      * the {@code int} value at index {@code N} logically AND'ed by
 617      * {@code species.length() - 1} is placed into the resulting shuffle at
 618      * lane index {@code N}.
 619      *
 620      * @param species shuffle species
 621      * @param ixs the given {@code int} values
 622      * @return a shuffle where each lane element is set to a given
 623      * {@code int} value
 624      * @throws IndexOutOfBoundsException if the number of int values is
 625      * {@code < species.length()}
 626      */
 627     @ForceInline
 628     public static Shuffle<Short> shuffleFromValues(Species<Short> species, int... ixs) {
 629         if (species.boxType() == ShortMaxVector.class)
 630             return new ShortMaxVector.ShortMaxShuffle(ixs);
 631         switch (species.bitSize()) {
 632             case 64: return new Short64Vector.Short64Shuffle(ixs);
 633             case 128: return new Short128Vector.Short128Shuffle(ixs);
 634             case 256: return new Short256Vector.Short256Shuffle(ixs);
 635             case 512: return new Short512Vector.Short512Shuffle(ixs);
 636             default: throw new IllegalArgumentException(Integer.toString(species.bitSize()));
 637         }
 638     }
 639 
 640     /**
 641      * Loads a shuffle from an {@code int} array starting at an offset.
 642      * <p>
 643      * For each shuffle lane, where {@code N} is the shuffle lane index, the
 644      * array element at index {@code i + N} logically AND'ed by
 645      * {@code species.length() - 1} is placed into the resulting shuffle at lane
 646      * index {@code N}.
 647      *
 648      * @param species shuffle species
 649      * @param ixs the {@code int} array
 650      * @param i the offset into the array
 651      * @return a shuffle loaded from the {@code int} array
 652      * @throws IndexOutOfBoundsException if {@code i < 0}, or
 653      * {@code i > a.length - species.length()}
 654      */
 655     @ForceInline
 656     public static Shuffle<Short> shuffleFromArray(Species<Short> species, int[] ixs, int i) {
 657         if (species.boxType() == ShortMaxVector.class)
 658             return new ShortMaxVector.ShortMaxShuffle(ixs, i);
 659         switch (species.bitSize()) {
 660             case 64: return new Short64Vector.Short64Shuffle(ixs, i);
 661             case 128: return new Short128Vector.Short128Shuffle(ixs, i);
 662             case 256: return new Short256Vector.Short256Shuffle(ixs, i);
 663             case 512: return new Short512Vector.Short512Shuffle(ixs, i);
 664             default: throw new IllegalArgumentException(Integer.toString(species.bitSize()));
 665         }
 666     }
 667 
 668     // Ops
 669 
 670     @Override
 671     public abstract ShortVector add(Vector<Short> v);
 672 
 673     /**
 674      * Adds this vector to the broadcast of an input scalar.
 675      * <p>
 676      * This is a vector binary operation where the primitive addition operation
 677      * ({@code +}) is applied to lane elements.
 678      *
 679      * @param s the input scalar
 680      * @return the result of adding this vector to the broadcast of an input
 681      * scalar
 682      */
 683     public abstract ShortVector add(short s);
 684 
 685     @Override
 686     public abstract ShortVector add(Vector<Short> v, Mask<Short> m);
 687 
 688     /**
 689      * Adds this vector to broadcast of an input scalar,
 690      * selecting lane elements controlled by a mask.
 691      * <p>
 692      * This is a vector binary operation where the primitive addition operation
 693      * ({@code +}) is applied to lane elements.
 694      *
 695      * @param s the input scalar
 696      * @param m the mask controlling lane selection
 697      * @return the result of adding this vector to the broadcast of an input
 698      * scalar
 699      */
 700     public abstract ShortVector add(short s, Mask<Short> m);
 701 
 702     @Override
 703     public abstract ShortVector sub(Vector<Short> v);
 704 
 705     /**
 706      * Subtracts the broadcast of an input scalar from this vector.
 707      * <p>
 708      * This is a vector binary operation where the primitive subtraction
 709      * operation ({@code -}) is applied to lane elements.
 710      *
 711      * @param s the input scalar
 712      * @return the result of subtracting the broadcast of an input
 713      * scalar from this vector
 714      */
 715     public abstract ShortVector sub(short s);
 716 
 717     @Override
 718     public abstract ShortVector sub(Vector<Short> v, Mask<Short> m);
 719 
 720     /**
 721      * Subtracts the broadcast of an input scalar from this vector, selecting
 722      * lane elements controlled by a mask.
 723      * <p>
 724      * This is a vector binary operation where the primitive subtraction
 725      * operation ({@code -}) is applied to lane elements.
 726      *
 727      * @param s the input scalar
 728      * @param m the mask controlling lane selection
 729      * @return the result of subtracting the broadcast of an input
 730      * scalar from this vector
 731      */
 732     public abstract ShortVector sub(short s, Mask<Short> m);
 733 
 734     @Override
 735     public abstract ShortVector mul(Vector<Short> v);
 736 
 737     /**
 738      * Multiplies this vector with the broadcast of an input scalar.
 739      * <p>
 740      * This is a vector binary operation where the primitive multiplication
 741      * operation ({@code *}) is applied to lane elements.
 742      *
 743      * @param s the input scalar
 744      * @return the result of multiplying this vector with the broadcast of an
 745      * input scalar
 746      */
 747     public abstract ShortVector mul(short s);
 748 
 749     @Override
 750     public abstract ShortVector mul(Vector<Short> v, Mask<Short> m);
 751 
 752     /**
 753      * Multiplies this vector with the broadcast of an input scalar, selecting
 754      * lane elements controlled by a mask.
 755      * <p>
 756      * This is a vector binary operation where the primitive multiplication
 757      * operation ({@code *}) is applied to lane elements.
 758      *
 759      * @param s the input scalar
 760      * @param m the mask controlling lane selection
 761      * @return the result of multiplying this vector with the broadcast of an
 762      * input scalar
 763      */
 764     public abstract ShortVector mul(short s, Mask<Short> m);
 765 
 766     @Override
 767     public abstract ShortVector neg();
 768 
 769     @Override
 770     public abstract ShortVector neg(Mask<Short> m);
 771 
 772     @Override
 773     public abstract ShortVector abs();
 774 
 775     @Override
 776     public abstract ShortVector abs(Mask<Short> m);
 777 
 778     @Override
 779     public abstract ShortVector min(Vector<Short> v);
 780 
 781     @Override
 782     public abstract ShortVector min(Vector<Short> v, Mask<Short> m);
 783 
 784     /**
 785      * Returns the minimum of this vector and the broadcast of an input scalar.
 786      * <p>
 787      * This is a vector binary operation where the operation
 788      * {@code (a, b) -> Math.min(a, b)} is applied to lane elements.
 789      *
 790      * @param s the input scalar
 791      * @return the minimum of this vector and the broadcast of an input scalar
 792      */
 793     public abstract ShortVector min(short s);
 794 
 795     @Override
 796     public abstract ShortVector max(Vector<Short> v);
 797 
 798     @Override
 799     public abstract ShortVector max(Vector<Short> v, Mask<Short> m);
 800 
 801     /**
 802      * Returns the maximum of this vector and the broadcast of an input scalar.
 803      * <p>
 804      * This is a vector binary operation where the operation
 805      * {@code (a, b) -> Math.max(a, b)} is applied to lane elements.
 806      *
 807      * @param s the input scalar
 808      * @return the maximum of this vector and the broadcast of an input scalar
 809      */
 810     public abstract ShortVector max(short s);
 811 
 812     @Override
 813     public abstract Mask<Short> equal(Vector<Short> v);
 814 
 815     /**
 816      * Tests if this vector is equal to the broadcast of an input scalar.
 817      * <p>
 818      * This is a vector binary test operation where the primitive equals
 819      * operation ({@code ==}) is applied to lane elements.
 820      *
 821      * @param s the input scalar
 822      * @return the result mask of testing if this vector is equal to the
 823      * broadcast of an input scalar
 824      */
 825     public abstract Mask<Short> equal(short s);
 826 
 827     @Override
 828     public abstract Mask<Short> notEqual(Vector<Short> v);
 829 
 830     /**
 831      * Tests if this vector is not equal to the broadcast of an input scalar.
 832      * <p>
 833      * This is a vector binary test operation where the primitive not equals
 834      * operation ({@code !=}) is applied to lane elements.
 835      *
 836      * @param s the input scalar
 837      * @return the result mask of testing if this vector is not equal to the
 838      * broadcast of an input scalar
 839      */
 840     public abstract Mask<Short> notEqual(short s);
 841 
 842     @Override
 843     public abstract Mask<Short> lessThan(Vector<Short> v);
 844 
 845     /**
 846      * Tests if this vector is less than the broadcast of an input scalar.
 847      * <p>
 848      * This is a vector binary test operation where the primitive less than
 849      * operation ({@code <}) is applied to lane elements.
 850      *
 851      * @param s the input scalar
 852      * @return the mask result of testing if this vector is less than the
 853      * broadcast of an input scalar
 854      */
 855     public abstract Mask<Short> lessThan(short s);
 856 
 857     @Override
 858     public abstract Mask<Short> lessThanEq(Vector<Short> v);
 859 
 860     /**
 861      * Tests if this vector is less or equal to the broadcast of an input scalar.
 862      * <p>
 863      * This is a vector binary test operation where the primitive less than
 864      * or equal to operation ({@code <=}) is applied to lane elements.
 865      *
 866      * @param s the input scalar
 867      * @return the mask result of testing if this vector is less than or equal
 868      * to the broadcast of an input scalar
 869      */
 870     public abstract Mask<Short> lessThanEq(short s);
 871 
 872     @Override
 873     public abstract Mask<Short> greaterThan(Vector<Short> v);
 874 
 875     /**
 876      * Tests if this vector is greater than the broadcast of an input scalar.
 877      * <p>
 878      * This is a vector binary test operation where the primitive greater than
 879      * operation ({@code >}) is applied to lane elements.
 880      *
 881      * @param s the input scalar
 882      * @return the mask result of testing if this vector is greater than the
 883      * broadcast of an input scalar
 884      */
 885     public abstract Mask<Short> greaterThan(short s);
 886 
 887     @Override
 888     public abstract Mask<Short> greaterThanEq(Vector<Short> v);
 889 
 890     /**
 891      * Tests if this vector is greater than or equal to the broadcast of an
 892      * input scalar.
 893      * <p>
 894      * This is a vector binary test operation where the primitive greater than
 895      * or equal to operation ({@code >=}) is applied to lane elements.
 896      *
 897      * @param s the input scalar
 898      * @return the mask result of testing if this vector is greater than or
 899      * equal to the broadcast of an input scalar
 900      */
 901     public abstract Mask<Short> greaterThanEq(short s);
 902 
 903     @Override
 904     public abstract ShortVector blend(Vector<Short> v, Mask<Short> m);
 905 
 906     /**
 907      * Blends the lane elements of this vector with those of the broadcast of an
 908      * input scalar, selecting lanes controlled by a mask.
 909      * <p>
 910      * For each lane of the mask, at lane index {@code N}, if the mask lane
 911      * is set then the lane element at {@code N} from the input vector is
 912      * selected and placed into the resulting vector at {@code N},
 913      * otherwise the the lane element at {@code N} from this input vector is
 914      * selected and placed into the resulting vector at {@code N}.
 915      *
 916      * @param s the input scalar
 917      * @param m the mask controlling lane selection
 918      * @return the result of blending the lane elements of this vector with
 919      * those of the broadcast of an input scalar
 920      */
 921     public abstract ShortVector blend(short s, Mask<Short> m);
 922 
 923     @Override
 924     public abstract ShortVector rearrange(Vector<Short> v,
 925                                                       Shuffle<Short> s, Mask<Short> m);
 926 
 927     @Override
 928     public abstract ShortVector rearrange(Shuffle<Short> m);
 929 
 930     @Override
 931     public abstract ShortVector reshape(Species<Short> s);
 932 
 933     @Override
 934     public abstract ShortVector rotateEL(int i);
 935 
 936     @Override
 937     public abstract ShortVector rotateER(int i);
 938 
 939     @Override
 940     public abstract ShortVector shiftEL(int i);
 941 
 942     @Override
 943     public abstract ShortVector shiftER(int i);
 944 
 945 
 946 
 947     /**
 948      * Bitwise ANDs this vector with an input vector.
 949      * <p>
 950      * This is a vector binary operation where the primitive bitwise AND
 951      * operation ({@code &}) is applied to lane elements.
 952      *
 953      * @param v the input vector
 954      * @return the bitwise AND of this vector with the input vector
 955      */
 956     public abstract ShortVector and(Vector<Short> v);
 957 
 958     /**
 959      * Bitwise ANDs this vector with the broadcast of an input scalar.
 960      * <p>
 961      * This is a vector binary operation where the primitive bitwise AND
 962      * operation ({@code &}) is applied to lane elements.
 963      *
 964      * @param s the input scalar
 965      * @return the bitwise AND of this vector with the broadcast of an input
 966      * scalar
 967      */
 968     public abstract ShortVector and(short s);
 969 
 970     /**
 971      * Bitwise ANDs this vector with an input vector, selecting lane elements
 972      * controlled by a mask.
 973      * <p>
 974      * This is a vector binary operation where the primitive bitwise AND
 975      * operation ({@code &}) is applied to lane elements.
 976      *
 977      * @param v the input vector
 978      * @param m the mask controlling lane selection
 979      * @return the bitwise AND of this vector with the input vector
 980      */
 981     public abstract ShortVector and(Vector<Short> v, Mask<Short> m);
 982 
 983     /**
 984      * Bitwise ANDs this vector with the broadcast of an input scalar, selecting
 985      * lane elements controlled by a mask.
 986      * <p>
 987      * This is a vector binary operation where the primitive bitwise AND
 988      * operation ({@code &}) is applied to lane elements.
 989      *
 990      * @param s the input scalar
 991      * @param m the mask controlling lane selection
 992      * @return the bitwise AND of this vector with the broadcast of an input
 993      * scalar
 994      */
 995     public abstract ShortVector and(short s, Mask<Short> m);
 996 
 997     /**
 998      * Bitwise ORs this vector with an input vector.
 999      * <p>
1000      * This is a vector binary operation where the primitive bitwise OR
1001      * operation ({@code |}) is applied to lane elements.
1002      *
1003      * @param v the input vector
1004      * @return the bitwise OR of this vector with the input vector
1005      */
1006     public abstract ShortVector or(Vector<Short> v);
1007 
1008     /**
1009      * Bitwise ORs this vector with the broadcast of an input scalar.
1010      * <p>
1011      * This is a vector binary operation where the primitive bitwise OR
1012      * operation ({@code |}) is applied to lane elements.
1013      *
1014      * @param s the input scalar
1015      * @return the bitwise OR of this vector with the broadcast of an input
1016      * scalar
1017      */
1018     public abstract ShortVector or(short s);
1019 
1020     /**
1021      * Bitwise ORs this vector with an input vector, selecting lane elements
1022      * controlled by a mask.
1023      * <p>
1024      * This is a vector binary operation where the primitive bitwise OR
1025      * operation ({@code |}) is applied to lane elements.
1026      *
1027      * @param v the input vector
1028      * @param m the mask controlling lane selection
1029      * @return the bitwise OR of this vector with the input vector
1030      */
1031     public abstract ShortVector or(Vector<Short> v, Mask<Short> m);
1032 
1033     /**
1034      * Bitwise ORs this vector with the broadcast of an input scalar, selecting
1035      * lane elements controlled by a mask.
1036      * <p>
1037      * This is a vector binary operation where the primitive bitwise OR
1038      * operation ({@code |}) is applied to lane elements.
1039      *
1040      * @param s the input scalar
1041      * @param m the mask controlling lane selection
1042      * @return the bitwise OR of this vector with the broadcast of an input
1043      * scalar
1044      */
1045     public abstract ShortVector or(short s, Mask<Short> m);
1046 
1047     /**
1048      * Bitwise XORs this vector with an input vector.
1049      * <p>
1050      * This is a vector binary operation where the primitive bitwise XOR
1051      * operation ({@code ^}) is applied to lane elements.
1052      *
1053      * @param v the input vector
1054      * @return the bitwise XOR of this vector with the input vector
1055      */
1056     public abstract ShortVector xor(Vector<Short> v);
1057 
1058     /**
1059      * Bitwise XORs this vector with the broadcast of an input scalar.
1060      * <p>
1061      * This is a vector binary operation where the primitive bitwise XOR
1062      * operation ({@code ^}) is applied to lane elements.
1063      *
1064      * @param s the input scalar
1065      * @return the bitwise XOR of this vector with the broadcast of an input
1066      * scalar
1067      */
1068     public abstract ShortVector xor(short s);
1069 
1070     /**
1071      * Bitwise XORs this vector with an input vector, selecting lane elements
1072      * controlled by a mask.
1073      * <p>
1074      * This is a vector binary operation where the primitive bitwise XOR
1075      * operation ({@code ^}) is applied to lane elements.
1076      *
1077      * @param v the input vector
1078      * @param m the mask controlling lane selection
1079      * @return the bitwise XOR of this vector with the input vector
1080      */
1081     public abstract ShortVector xor(Vector<Short> v, Mask<Short> m);
1082 
1083     /**
1084      * Bitwise XORs this vector with the broadcast of an input scalar, selecting
1085      * lane elements controlled by a mask.
1086      * <p>
1087      * This is a vector binary operation where the primitive bitwise XOR
1088      * operation ({@code ^}) is applied to lane elements.
1089      *
1090      * @param s the input scalar
1091      * @param m the mask controlling lane selection
1092      * @return the bitwise XOR of this vector with the broadcast of an input
1093      * scalar
1094      */
1095     public abstract ShortVector xor(short s, Mask<Short> m);
1096 
1097     /**
1098      * Bitwise NOTs this vector.
1099      * <p>
1100      * This is a vector unary operation where the primitive bitwise NOT
1101      * operation ({@code ~}) is applied to lane elements.
1102      *
1103      * @return the bitwise NOT of this vector
1104      */
1105     public abstract ShortVector not();
1106 
1107     /**
1108      * Bitwise NOTs this vector, selecting lane elements controlled by a mask.
1109      * <p>
1110      * This is a vector unary operation where the primitive bitwise NOT
1111      * operation ({@code ~}) is applied to lane elements.
1112      *
1113      * @param m the mask controlling lane selection
1114      * @return the bitwise NOT of this vector
1115      */
1116     public abstract ShortVector not(Mask<Short> m);
1117 
1118     /**
1119      * Logically left shifts this vector by the broadcast of an input scalar.
1120      * <p>
1121      * This is a vector binary operation where the primitive logical left shift
1122      * operation ({@code <<}) is applied to lane elements to left shift the
1123      * element by shift value as specified by the input scalar. Only the 4
1124      * lowest-order bits of shift value are used. It is as if the shift value
1125      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1126      * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1127      *
1128      * @param s the input scalar; the number of the bits to left shift
1129      * @return the result of logically left shifting left this vector by the
1130      * broadcast of an input scalar
1131      */
1132     public abstract ShortVector shiftL(int s);
1133 
1134     /**
1135      * Logically left shifts this vector by the broadcast of an input scalar,
1136      * selecting lane elements controlled by a mask.
1137      * <p>
1138      * This is a vector binary operation where the primitive logical left shift
1139      * operation ({@code <<}) is applied to lane elements to left shift the
1140      * element by shift value as specified by the input scalar. Only the 4
1141      * lowest-order bits of shift value are used. It is as if the shift value
1142      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1143      * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1144      *
1145      * @param s the input scalar; the number of the bits to left shift
1146      * @param m the mask controlling lane selection
1147      * @return the result of logically left shifting left this vector by the
1148      * broadcast of an input scalar
1149      */
1150     public abstract ShortVector shiftL(int s, Mask<Short> m);
1151 
1152 
1153     // logical, or unsigned, shift right
1154 
1155      /**
1156      * Logically right shifts (or unsigned right shifts) this vector by the
1157      * broadcast of an input scalar.
1158      * <p>
1159      * This is a vector binary operation where the primitive logical right shift
1160      * operation ({@code >>>}) is applied to lane elements to logically right shift the
1161      * element by shift value as specified by the input scalar. Only the 4
1162      * lowest-order bits of shift value are used. It is as if the shift value
1163      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1164      * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1165      *
1166      * @param s the input scalar; the number of the bits to right shift
1167      * @return the result of logically right shifting this vector by the
1168      * broadcast of an input scalar
1169      */
1170     public abstract ShortVector shiftR(int s);
1171 
1172      /**
1173      * Logically right shifts (or unsigned right shifts) this vector by the
1174      * broadcast of an input scalar, selecting lane elements controlled by a
1175      * mask.
1176      * <p>
1177      * This is a vector binary operation where the primitive logical right shift
1178      * operation ({@code >>>}) is applied to lane elements to logically right shift the
1179      * element by shift value as specified by the input scalar. Only the 4
1180      * lowest-order bits of shift value are used. It is as if the shift value
1181      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1182      * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1183      *
1184      * @param s the input scalar; the number of the bits to right shift
1185      * @param m the mask controlling lane selection
1186      * @return the result of logically right shifting this vector by the
1187      * broadcast of an input scalar
1188      */
1189     public abstract ShortVector shiftR(int s, Mask<Short> m);
1190 
1191 
1192     /**
1193      * Arithmetically right shifts (or signed right shifts) this vector by the
1194      * broadcast of an input scalar.
1195      * <p>
1196      * This is a vector binary operation where the primitive arithmetic right
1197      * shift operation ({@code >>}) is applied to lane elements  to arithmetically
1198      * right shift the element by shift value as specified by the input scalar.
1199      * Only the 4 lowest-order bits of shift value are used. It is as if the shift
1200      * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1201      * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1202      *
1203      * @param s the input scalar; the number of the bits to right shift
1204      * @return the result of arithmetically right shifting this vector by the
1205      * broadcast of an input scalar
1206      */
1207     public abstract ShortVector aShiftR(int s);
1208 
1209     /**
1210      * Arithmetically right shifts (or signed right shifts) this vector by the
1211      * broadcast of an input scalar, selecting lane elements controlled by a
1212      * mask.
1213      * <p>
1214      * This is a vector binary operation where the primitive arithmetic right
1215      * shift operation ({@code >>}) is applied to lane elements  to arithmetically
1216      * right shift the element by shift value as specified by the input scalar.
1217      * Only the 4 lowest-order bits of shift value are used. It is as if the shift
1218      * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1219      * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1220      *
1221      * @param s the input scalar; the number of the bits to right shift
1222      * @param m the mask controlling lane selection
1223      * @return the result of arithmetically right shifting this vector by the
1224      * broadcast of an input scalar
1225      */
1226     public abstract ShortVector aShiftR(int s, Mask<Short> m);
1227 
1228 
1229     @Override
1230     public abstract void intoByteArray(byte[] a, int ix);
1231 
1232     @Override
1233     public abstract void intoByteArray(byte[] a, int ix, Mask<Short> m);
1234 
1235     @Override
1236     public abstract void intoByteBuffer(ByteBuffer bb, int ix);
1237 
1238     @Override
1239     public abstract void intoByteBuffer(ByteBuffer bb, int ix, Mask<Short> m);
1240 
1241 
1242     // Type specific horizontal reductions
1243     /**
1244      * Adds all lane elements of this vector.
1245      * <p>
1246      * This is an associative vector reduction operation where the addition
1247      * operation ({@code +}) is applied to lane elements,
1248      * and the identity value is {@code 0}.
1249      *
1250      * @return the addition of all the lane elements of this vector
1251      */
1252     public abstract short addAll();
1253 
1254     /**
1255      * Adds all lane elements of this vector, selecting lane elements
1256      * controlled by a mask.
1257      * <p>
1258      * This is an associative vector reduction operation where the addition
1259      * operation ({@code +}) is applied to lane elements,
1260      * and the identity value is {@code 0}.
1261      *
1262      * @param m the mask controlling lane selection
1263      * @return the addition of the selected lane elements of this vector
1264      */
1265     public abstract short addAll(Mask<Short> m);
1266 
1267     /**
1268      * Multiplies all lane elements of this vector.
1269      * <p>
1270      * This is an associative vector reduction operation where the
1271      * multiplication operation ({@code *}) is applied to lane elements,
1272      * and the identity value is {@code 1}.
1273      *
1274      * @return the multiplication of all the lane elements of this vector
1275      */
1276     public abstract short mulAll();
1277 
1278     /**
1279      * Multiplies all lane elements of this vector, selecting lane elements
1280      * controlled by a mask.
1281      * <p>
1282      * This is an associative vector reduction operation where the
1283      * multiplication operation ({@code *}) is applied to lane elements,
1284      * and the identity value is {@code 1}.
1285      *
1286      * @param m the mask controlling lane selection
1287      * @return the multiplication of all the lane elements of this vector
1288      */
1289     public abstract short mulAll(Mask<Short> m);
1290 
1291     /**
1292      * Returns the minimum lane element of this vector.
1293      * <p>
1294      * This is an associative vector reduction operation where the operation
1295      * {@code (a, b) -> Math.min(a, b)} is applied to lane elements,
1296      * and the identity value is
1297      * {@link Short#MAX_VALUE}.
1298      *
1299      * @return the minimum lane element of this vector
1300      */
1301     public abstract short minAll();
1302 
1303     /**
1304      * Returns the minimum lane element of this vector, selecting lane elements
1305      * controlled by a mask.
1306      * <p>
1307      * This is an associative vector reduction operation where the operation
1308      * {@code (a, b) -> Math.min(a, b)} is applied to lane elements,
1309      * and the identity value is
1310      * {@link Short#MAX_VALUE}.
1311      *
1312      * @param m the mask controlling lane selection
1313      * @return the minimum lane element of this vector
1314      */
1315     public abstract short minAll(Mask<Short> m);
1316 
1317     /**
1318      * Returns the maximum lane element of this vector.
1319      * <p>
1320      * This is an associative vector reduction operation where the operation
1321      * {@code (a, b) -> Math.max(a, b)} is applied to lane elements,
1322      * and the identity value is
1323      * {@link Short#MIN_VALUE}.
1324      *
1325      * @return the maximum lane element of this vector
1326      */
1327     public abstract short maxAll();
1328 
1329     /**
1330      * Returns the maximum lane element of this vector, selecting lane elements
1331      * controlled by a mask.
1332      * <p>
1333      * This is an associative vector reduction operation where the operation
1334      * {@code (a, b) -> Math.max(a, b)} is applied to lane elements,
1335      * and the identity value is
1336      * {@link Short#MIN_VALUE}.
1337      *
1338      * @param m the mask controlling lane selection
1339      * @return the maximum lane element of this vector
1340      */
1341     public abstract short maxAll(Mask<Short> m);
1342 
1343     /**
1344      * Logically ORs all lane elements of this vector.
1345      * <p>
1346      * This is an associative vector reduction operation where the logical OR
1347      * operation ({@code |}) is applied to lane elements,
1348      * and the identity value is {@code 0}.
1349      *
1350      * @return the logical OR all the lane elements of this vector
1351      */
1352     public abstract short orAll();
1353 
1354     /**
1355      * Logically ORs all lane elements of this vector, selecting lane elements
1356      * controlled by a mask.
1357      * <p>
1358      * This is an associative vector reduction operation where the logical OR
1359      * operation ({@code |}) is applied to lane elements,
1360      * and the identity value is {@code 0}.
1361      *
1362      * @param m the mask controlling lane selection
1363      * @return the logical OR all the lane elements of this vector
1364      */
1365     public abstract short orAll(Mask<Short> m);
1366 
1367     /**
1368      * Logically ANDs all lane elements of this vector.
1369      * <p>
1370      * This is an associative vector reduction operation where the logical AND
1371      * operation ({@code |}) is applied to lane elements,
1372      * and the identity value is {@code -1}.
1373      *
1374      * @return the logical AND all the lane elements of this vector
1375      */
1376     public abstract short andAll();
1377 
1378     /**
1379      * Logically ANDs all lane elements of this vector, selecting lane elements
1380      * controlled by a mask.
1381      * <p>
1382      * This is an associative vector reduction operation where the logical AND
1383      * operation ({@code |}) is applied to lane elements,
1384      * and the identity value is {@code -1}.
1385      *
1386      * @param m the mask controlling lane selection
1387      * @return the logical AND all the lane elements of this vector
1388      */
1389     public abstract short andAll(Mask<Short> m);
1390 
1391     /**
1392      * Logically XORs all lane elements of this vector.
1393      * <p>
1394      * This is an associative vector reduction operation where the logical XOR
1395      * operation ({@code ^}) is applied to lane elements,
1396      * and the identity value is {@code 0}.
1397      *
1398      * @return the logical XOR all the lane elements of this vector
1399      */
1400     public abstract short xorAll();
1401 
1402     /**
1403      * Logically XORs all lane elements of this vector, selecting lane elements
1404      * controlled by a mask.
1405      * <p>
1406      * This is an associative vector reduction operation where the logical XOR
1407      * operation ({@code ^}) is applied to lane elements,
1408      * and the identity value is {@code 0}.
1409      *
1410      * @param m the mask controlling lane selection
1411      * @return the logical XOR all the lane elements of this vector
1412      */
1413     public abstract short xorAll(Mask<Short> m);
1414 
1415     // Type specific accessors
1416 
1417     /**
1418      * Gets the lane element at lane index {@code i}
1419      *
1420      * @param i the lane index
1421      * @return the lane element at lane index {@code i}
1422      * @throws IllegalArgumentException if the index is is out of range
1423      * ({@code < 0 || >= length()})
1424      */
1425     public abstract short get(int i);
1426 
1427     /**
1428      * Replaces the lane element of this vector at lane index {@code i} with
1429      * value {@code e}.
1430      * <p>
1431      * This is a cross-lane operation and behaves as if it returns the result
1432      * of blending this vector with an input vector that is the result of
1433      * broadcasting {@code e} and a mask that has only one lane set at lane
1434      * index {@code i}.
1435      *
1436      * @param i the lane index of the lane element to be replaced
1437      * @param e the value to be placed
1438      * @return the result of replacing the lane element of this vector at lane
1439      * index {@code i} with value {@code e}.
1440      * @throws IllegalArgumentException if the index is is out of range
1441      * ({@code < 0 || >= length()})
1442      */
1443     public abstract ShortVector with(int i, short e);
1444 
1445     // Type specific extractors
1446 
1447     /**
1448      * Returns an array containing the lane elements of this vector.
1449      * <p>
1450      * This method behaves as if it {@link #intoArray(short[], int)} stores}
1451      * this vector into an allocated array and returns the array as follows:
1452      * <pre>{@code
1453      *   short[] a = new short[this.length()];
1454      *   this.intoArray(a, 0);
1455      *   return a;
1456      * }</pre>
1457      *
1458      * @return an array containing the the lane elements of this vector
1459      */
1460     @ForceInline
1461     public final short[] toArray() {
1462         short[] a = new short[species().length()];
1463         intoArray(a, 0);
1464         return a;
1465     }
1466 
1467     /**
1468      * Stores this vector into an array starting at offset.
1469      * <p>
1470      * For each vector lane, where {@code N} is the vector lane index,
1471      * the lane element at index {@code N} is stored into the array at index
1472      * {@code i + N}.
1473      *
1474      * @param a the array
1475      * @param i the offset into the array
1476      * @throws IndexOutOfBoundsException if {@code i < 0}, or
1477      * {@code i > a.length - this.length()}
1478      */
1479     public abstract void intoArray(short[] a, int i);
1480 
1481     /**
1482      * Stores this vector into an array starting at offset and using a mask.
1483      * <p>
1484      * For each vector lane, where {@code N} is the vector lane index,
1485      * if the mask lane at index {@code N} is set then the lane element at
1486      * index {@code N} is stored into the array index {@code i + N}.
1487      *
1488      * @param a the array
1489      * @param i the offset into the array
1490      * @param m the mask
1491      * @throws IndexOutOfBoundsException if {@code i < 0}, or
1492      * for any vector lane index {@code N} where the mask at lane {@code N}
1493      * is set {@code i >= a.length - N}
1494      */
1495     public abstract void intoArray(short[] a, int i, Mask<Short> m);
1496 
1497     /**
1498      * Stores this vector into an array using indexes obtained from an index
1499      * map.
1500      * <p>
1501      * For each vector lane, where {@code N} is the vector lane index, the
1502      * lane element at index {@code N} is stored into the array at index
1503      * {@code i + indexMap[j + N]}.
1504      *
1505      * @param a the array
1506      * @param i the offset into the array, may be negative if relative
1507      * indexes in the index map compensate to produce a value within the
1508      * array bounds
1509      * @param indexMap the index map
1510      * @param j the offset into the index map
1511      * @throws IndexOutOfBoundsException if {@code j < 0}, or
1512      * {@code j > indexMap.length - this.length()},
1513      * or for any vector lane index {@code N} the result of
1514      * {@code i + indexMap[j + N]} is {@code < 0} or {@code >= a.length}
1515      */
1516     public void intoArray(short[] a, int i, int[] indexMap, int j) {
1517         forEach((n, e) -> a[i + indexMap[j + n]] = e);
1518     }
1519 
1520     /**
1521      * Stores this vector into an array using indexes obtained from an index
1522      * map and using a mask.
1523      * <p>
1524      * For each vector lane, where {@code N} is the vector lane index,
1525      * if the mask lane at index {@code N} is set then the lane element at
1526      * index {@code N} is stored into the array at index
1527      * {@code i + indexMap[j + N]}.
1528      *
1529      * @param a the array
1530      * @param i the offset into the array, may be negative if relative
1531      * indexes in the index map compensate to produce a value within the
1532      * array bounds
1533      * @param m the mask
1534      * @param indexMap the index map
1535      * @param j the offset into the index map
1536      * @throws IndexOutOfBoundsException if {@code j < 0}, or
1537      * {@code j > indexMap.length - this.length()},
1538      * or for any vector lane index {@code N} where the mask at lane
1539      * {@code N} is set the result of {@code i + indexMap[j + N]} is
1540      * {@code < 0} or {@code >= a.length}
1541      */
1542     public void intoArray(short[] a, int i, Mask<Short> m, int[] indexMap, int j) {
1543         forEach(m, (n, e) -> a[i + indexMap[j + n]] = e);
1544     }
1545     // Species
1546 
1547     @Override
1548     public abstract Species<Short> species();
1549 
1550     /**
1551      * Class representing {@link ShortVector}'s of the same {@link Vector.Shape Shape}.
1552      */
1553     static final class ShortSpecies extends Vector.AbstractSpecies<Short> {
1554         final Function<short[], ShortVector> vectorFactory;
1555         final Function<boolean[], Vector.Mask<Short>> maskFactory;
1556 
1557         private ShortSpecies(Vector.Shape shape,
1558                           Class<?> boxType,
1559                           Class<?> maskType,
1560                           Function<short[], ShortVector> vectorFactory,
1561                           Function<boolean[], Vector.Mask<Short>> maskFactory) {
1562             super(shape, short.class, Short.SIZE, boxType, maskType);
1563             this.vectorFactory = vectorFactory;
1564             this.maskFactory = maskFactory;
1565         }
1566 
1567         interface FOp {
1568             short apply(int i);
1569         }
1570 
1571         interface FOpm {
1572             boolean apply(int i);
1573         }
1574 
1575         ShortVector op(FOp f) {
1576             short[] res = new short[length()];
1577             for (int i = 0; i < length(); i++) {
1578                 res[i] = f.apply(i);
1579             }
1580             return vectorFactory.apply(res);
1581         }
1582 
1583         ShortVector op(Vector.Mask<Short> o, FOp f) {
1584             short[] res = new short[length()];
1585             boolean[] mbits = ((AbstractMask<Short>)o).getBits();
1586             for (int i = 0; i < length(); i++) {
1587                 if (mbits[i]) {
1588                     res[i] = f.apply(i);
1589                 }
1590             }
1591             return vectorFactory.apply(res);
1592         }
1593 
1594         Vector.Mask<Short> opm(IntVector.IntSpecies.FOpm f) {
1595             boolean[] res = new boolean[length()];
1596             for (int i = 0; i < length(); i++) {
1597                 res[i] = (boolean)f.apply(i);
1598             }
1599             return maskFactory.apply(res);
1600         }
1601     }
1602 
1603     /**
1604      * Finds the preferred species for an element type of {@code short}.
1605      * <p>
1606      * A preferred species is a species chosen by the platform that has a
1607      * shape of maximal bit size.  A preferred species for different element
1608      * types will have the same shape, and therefore vectors, masks, and
1609      * shuffles created from such species will be shape compatible.
1610      *
1611      * @return the preferred species for an element type of {@code short}
1612      */
1613     private static ShortSpecies preferredSpecies() {
1614         return (ShortSpecies) Species.ofPreferred(short.class);
1615     }
1616 
1617     /**
1618      * Finds a species for an element type of {@code short} and shape.
1619      *
1620      * @param s the shape
1621      * @return a species for an element type of {@code short} and shape
1622      * @throws IllegalArgumentException if no such species exists for the shape
1623      */
1624     static ShortSpecies species(Vector.Shape s) {
1625         Objects.requireNonNull(s);
1626         switch (s) {
1627             case S_64_BIT: return (ShortSpecies) SPECIES_64;
1628             case S_128_BIT: return (ShortSpecies) SPECIES_128;
1629             case S_256_BIT: return (ShortSpecies) SPECIES_256;
1630             case S_512_BIT: return (ShortSpecies) SPECIES_512;
1631             case S_Max_BIT: return (ShortSpecies) SPECIES_MAX;
1632             default: throw new IllegalArgumentException("Bad shape: " + s);
1633         }
1634     }
1635 
1636     /** Species representing {@link ShortVector}s of {@link Vector.Shape#S_64_BIT Shape.S_64_BIT}. */
1637     public static final Species<Short> SPECIES_64 = new ShortSpecies(Shape.S_64_BIT, Short64Vector.class, Short64Vector.Short64Mask.class,
1638                                                                      Short64Vector::new, Short64Vector.Short64Mask::new);
1639 
1640     /** Species representing {@link ShortVector}s of {@link Vector.Shape#S_128_BIT Shape.S_128_BIT}. */
1641     public static final Species<Short> SPECIES_128 = new ShortSpecies(Shape.S_128_BIT, Short128Vector.class, Short128Vector.Short128Mask.class,
1642                                                                       Short128Vector::new, Short128Vector.Short128Mask::new);
1643 
1644     /** Species representing {@link ShortVector}s of {@link Vector.Shape#S_256_BIT Shape.S_256_BIT}. */
1645     public static final Species<Short> SPECIES_256 = new ShortSpecies(Shape.S_256_BIT, Short256Vector.class, Short256Vector.Short256Mask.class,
1646                                                                       Short256Vector::new, Short256Vector.Short256Mask::new);
1647 
1648     /** Species representing {@link ShortVector}s of {@link Vector.Shape#S_512_BIT Shape.S_512_BIT}. */
1649     public static final Species<Short> SPECIES_512 = new ShortSpecies(Shape.S_512_BIT, Short512Vector.class, Short512Vector.Short512Mask.class,
1650                                                                       Short512Vector::new, Short512Vector.Short512Mask::new);
1651 
1652     /** Species representing {@link ShortVector}s of {@link Vector.Shape#S_Max_BIT Shape.S_Max_BIT}. */
1653     public static final Species<Short> SPECIES_MAX = new ShortSpecies(Shape.S_Max_BIT, ShortMaxVector.class, ShortMaxVector.ShortMaxMask.class,
1654                                                                       ShortMaxVector::new, ShortMaxVector.ShortMaxMask::new);
1655 
1656     /**
1657      * Preferred species for {@link ShortVector}s.
1658      * A preferred species is a species of maximal bit size for the platform.
1659      */
1660     public static final Species<Short> SPECIES_PREFERRED = (Species<Short>) preferredSpecies();
1661 }