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.ByteOrder;
  29 import java.nio.ShortBuffer;
  30 import java.nio.ReadOnlyBufferException;
  31 import java.util.Arrays;
  32 import java.util.Objects;
  33 import java.util.function.IntUnaryOperator;
  34 
  35 import jdk.internal.misc.Unsafe;
  36 import jdk.internal.vm.annotation.ForceInline;
  37 import static jdk.incubator.vector.VectorIntrinsics.*;
  38 
  39 @SuppressWarnings("cast")
  40 final class Short128Vector extends ShortVector<Shapes.S128Bit> {
  41     static final Short128Species SPECIES = new Short128Species();
  42 
  43     static final Short128Vector ZERO = new Short128Vector();
  44 
  45     static final int LENGTH = SPECIES.length();
  46 
  47     private final short[] vec; // Don't access directly, use getElements() instead.
  48 
  49     private short[] getElements() {
  50         return VectorIntrinsics.maybeRebox(this).vec;
  51     }
  52 
  53     Short128Vector() {
  54         vec = new short[SPECIES.length()];
  55     }
  56 
  57     Short128Vector(short[] v) {
  58         vec = v;
  59     }
  60 
  61     @Override
  62     public int length() { return LENGTH; }
  63 
  64     // Unary operator
  65 
  66     @Override
  67     Short128Vector uOp(FUnOp f) {
  68         short[] vec = getElements();
  69         short[] res = new short[length()];
  70         for (int i = 0; i < length(); i++) {
  71             res[i] = f.apply(i, vec[i]);
  72         }
  73         return new Short128Vector(res);
  74     }
  75 
  76     @Override
  77     Short128Vector uOp(Mask<Short, Shapes.S128Bit> o, FUnOp f) {
  78         short[] vec = getElements();
  79         short[] res = new short[length()];
  80         boolean[] mbits = ((Short128Mask)o).getBits();
  81         for (int i = 0; i < length(); i++) {
  82             res[i] = mbits[i] ? f.apply(i, vec[i]) : vec[i];
  83         }
  84         return new Short128Vector(res);
  85     }
  86 
  87     // Binary operator
  88 
  89     @Override
  90     Short128Vector bOp(Vector<Short, Shapes.S128Bit> o, FBinOp f) {
  91         short[] res = new short[length()];
  92         short[] vec1 = this.getElements();
  93         short[] vec2 = ((Short128Vector)o).getElements();
  94         for (int i = 0; i < length(); i++) {
  95             res[i] = f.apply(i, vec1[i], vec2[i]);
  96         }
  97         return new Short128Vector(res);
  98     }
  99 
 100     @Override
 101     Short128Vector bOp(Vector<Short, Shapes.S128Bit> o1, Mask<Short, Shapes.S128Bit> o2, FBinOp f) {
 102         short[] res = new short[length()];
 103         short[] vec1 = this.getElements();
 104         short[] vec2 = ((Short128Vector)o1).getElements();
 105         boolean[] mbits = ((Short128Mask)o2).getBits();
 106         for (int i = 0; i < length(); i++) {
 107             res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i]) : vec1[i];
 108         }
 109         return new Short128Vector(res);
 110     }
 111 
 112     // Trinary operator
 113 
 114     @Override
 115     Short128Vector tOp(Vector<Short, Shapes.S128Bit> o1, Vector<Short, Shapes.S128Bit> o2, FTriOp f) {
 116         short[] res = new short[length()];
 117         short[] vec1 = this.getElements();
 118         short[] vec2 = ((Short128Vector)o1).getElements();
 119         short[] vec3 = ((Short128Vector)o2).getElements();
 120         for (int i = 0; i < length(); i++) {
 121             res[i] = f.apply(i, vec1[i], vec2[i], vec3[i]);
 122         }
 123         return new Short128Vector(res);
 124     }
 125 
 126     @Override
 127     Short128Vector tOp(Vector<Short, Shapes.S128Bit> o1, Vector<Short, Shapes.S128Bit> o2, Mask<Short, Shapes.S128Bit> o3, FTriOp f) {
 128         short[] res = new short[length()];
 129         short[] vec1 = getElements();
 130         short[] vec2 = ((Short128Vector)o1).getElements();
 131         short[] vec3 = ((Short128Vector)o2).getElements();
 132         boolean[] mbits = ((Short128Mask)o3).getBits();
 133         for (int i = 0; i < length(); i++) {
 134             res[i] = mbits[i] ? f.apply(i, vec1[i], vec2[i], vec3[i]) : vec1[i];
 135         }
 136         return new Short128Vector(res);
 137     }
 138 
 139     @Override
 140     short rOp(short v, FBinOp f) {
 141         short[] vec = getElements();
 142         for (int i = 0; i < length(); i++) {
 143             v = f.apply(i, v, vec[i]);
 144         }
 145         return v;
 146     }
 147 
 148     // Binary operations with scalars
 149 
 150     @Override
 151     @ForceInline
 152     public ShortVector<Shapes.S128Bit> add(short o) {
 153         return add(SPECIES.broadcast(o));
 154     }
 155 
 156     @Override
 157     @ForceInline
 158     public ShortVector<Shapes.S128Bit> add(short o, Mask<Short,Shapes.S128Bit> m) {
 159         return add(SPECIES.broadcast(o), m);
 160     }
 161 
 162     @Override
 163     @ForceInline
 164     public ShortVector<Shapes.S128Bit> sub(short o) {
 165         return sub(SPECIES.broadcast(o));
 166     }
 167 
 168     @Override
 169     @ForceInline
 170     public ShortVector<Shapes.S128Bit> sub(short o, Mask<Short,Shapes.S128Bit> m) {
 171         return sub(SPECIES.broadcast(o), m);
 172     }
 173 
 174     @Override
 175     @ForceInline
 176     public ShortVector<Shapes.S128Bit> mul(short o) {
 177         return mul(SPECIES.broadcast(o));
 178     }
 179 
 180     @Override
 181     @ForceInline
 182     public ShortVector<Shapes.S128Bit> mul(short o, Mask<Short,Shapes.S128Bit> m) {
 183         return mul(SPECIES.broadcast(o), m);
 184     }
 185 
 186     @Override
 187     @ForceInline
 188     public ShortVector<Shapes.S128Bit> min(short o) {
 189         return min(SPECIES.broadcast(o));
 190     }
 191 
 192     @Override
 193     @ForceInline
 194     public ShortVector<Shapes.S128Bit> max(short o) {
 195         return max(SPECIES.broadcast(o));
 196     }
 197 
 198     @Override
 199     @ForceInline
 200     public Mask<Short, Shapes.S128Bit> equal(short o) {
 201         return equal(SPECIES.broadcast(o));
 202     }
 203 
 204     @Override
 205     @ForceInline
 206     public Mask<Short, Shapes.S128Bit> notEqual(short o) {
 207         return notEqual(SPECIES.broadcast(o));
 208     }
 209 
 210     @Override
 211     @ForceInline
 212     public Mask<Short, Shapes.S128Bit> lessThan(short o) {
 213         return lessThan(SPECIES.broadcast(o));
 214     }
 215 
 216     @Override
 217     @ForceInline
 218     public Mask<Short, Shapes.S128Bit> lessThanEq(short o) {
 219         return lessThanEq(SPECIES.broadcast(o));
 220     }
 221 
 222     @Override
 223     @ForceInline
 224     public Mask<Short, Shapes.S128Bit> greaterThan(short o) {
 225         return greaterThan(SPECIES.broadcast(o));
 226     }
 227 
 228     @Override
 229     @ForceInline
 230     public Mask<Short, Shapes.S128Bit> greaterThanEq(short o) {
 231         return greaterThanEq(SPECIES.broadcast(o));
 232     }
 233 
 234     @Override
 235     @ForceInline
 236     public ShortVector<Shapes.S128Bit> blend(short o, Mask<Short,Shapes.S128Bit> m) {
 237         return blend(SPECIES.broadcast(o), m);
 238     }
 239 
 240 
 241     @Override
 242     @ForceInline
 243     public ShortVector<Shapes.S128Bit> and(short o) {
 244         return and(SPECIES.broadcast(o));
 245     }
 246 
 247     @Override
 248     @ForceInline
 249     public ShortVector<Shapes.S128Bit> and(short o, Mask<Short,Shapes.S128Bit> m) {
 250         return and(SPECIES.broadcast(o), m);
 251     }
 252 
 253     @Override
 254     @ForceInline
 255     public ShortVector<Shapes.S128Bit> or(short o) {
 256         return or(SPECIES.broadcast(o));
 257     }
 258 
 259     @Override
 260     @ForceInline
 261     public ShortVector<Shapes.S128Bit> or(short o, Mask<Short,Shapes.S128Bit> m) {
 262         return or(SPECIES.broadcast(o), m);
 263     }
 264 
 265     @Override
 266     @ForceInline
 267     public ShortVector<Shapes.S128Bit> xor(short o) {
 268         return xor(SPECIES.broadcast(o));
 269     }
 270 
 271     @Override
 272     @ForceInline
 273     public ShortVector<Shapes.S128Bit> xor(short o, Mask<Short,Shapes.S128Bit> m) {
 274         return xor(SPECIES.broadcast(o), m);
 275     }
 276 
 277     @Override
 278     @ForceInline
 279     public Short128Vector neg() {
 280         return SPECIES.zero().sub(this);
 281     }
 282 
 283     // Unary operations
 284 
 285     @ForceInline
 286     @Override
 287     public Short128Vector neg(Mask<Short, Shapes.S128Bit> m) {
 288         return blend(neg(), m);
 289     }
 290 
 291     @Override
 292     @ForceInline
 293     public Short128Vector abs() {
 294         return VectorIntrinsics.unaryOp(
 295             VECTOR_OP_ABS, Short128Vector.class, short.class, LENGTH,
 296             this,
 297             v1 -> v1.uOp((i, a) -> (short) Math.abs(a)));
 298     }
 299 
 300     @ForceInline
 301     @Override
 302     public Short128Vector abs(Mask<Short, Shapes.S128Bit> m) {
 303         return blend(abs(), m);
 304     }
 305 
 306 
 307     @Override
 308     @ForceInline
 309     public Short128Vector not() {
 310         return VectorIntrinsics.unaryOp(
 311             VECTOR_OP_NOT, Short128Vector.class, short.class, LENGTH,
 312             this,
 313             v1 -> v1.uOp((i, a) -> (short) ~a));
 314     }
 315 
 316     @ForceInline
 317     @Override
 318     public Short128Vector not(Mask<Short, Shapes.S128Bit> m) {
 319         return blend(not(), m);
 320     }
 321     // Binary operations
 322 
 323     @Override
 324     @ForceInline
 325     public Short128Vector add(Vector<Short,Shapes.S128Bit> o) {
 326         Objects.requireNonNull(o);
 327         Short128Vector v = (Short128Vector)o;
 328         return VectorIntrinsics.binaryOp(
 329             VECTOR_OP_ADD, Short128Vector.class, short.class, LENGTH,
 330             this, v,
 331             (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a + b)));
 332     }
 333 
 334     @Override
 335     @ForceInline
 336     public Short128Vector add(Vector<Short,Shapes.S128Bit> v, Mask<Short, Shapes.S128Bit> m) {
 337         return blend(add(v), m);
 338     }
 339 
 340     @Override
 341     @ForceInline
 342     public Short128Vector sub(Vector<Short,Shapes.S128Bit> o) {
 343         Objects.requireNonNull(o);
 344         Short128Vector v = (Short128Vector)o;
 345         return VectorIntrinsics.binaryOp(
 346             VECTOR_OP_SUB, Short128Vector.class, short.class, LENGTH,
 347             this, v,
 348             (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a - b)));
 349     }
 350 
 351     @Override
 352     @ForceInline
 353     public Short128Vector sub(Vector<Short,Shapes.S128Bit> v, Mask<Short, Shapes.S128Bit> m) {
 354         return blend(sub(v), m);
 355     }
 356 
 357     @Override
 358     @ForceInline
 359     public Short128Vector mul(Vector<Short,Shapes.S128Bit> o) {
 360         Objects.requireNonNull(o);
 361         Short128Vector v = (Short128Vector)o;
 362         return VectorIntrinsics.binaryOp(
 363             VECTOR_OP_MUL, Short128Vector.class, short.class, LENGTH,
 364             this, v,
 365             (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a * b)));
 366     }
 367 
 368     @Override
 369     @ForceInline
 370     public Short128Vector mul(Vector<Short,Shapes.S128Bit> v, Mask<Short, Shapes.S128Bit> m) {
 371         return blend(mul(v), m);
 372     }
 373 
 374     @Override
 375     @ForceInline
 376     public Short128Vector min(Vector<Short,Shapes.S128Bit> o) {
 377         Objects.requireNonNull(o);
 378         Short128Vector v = (Short128Vector)o;
 379         return (Short128Vector) VectorIntrinsics.binaryOp(
 380             VECTOR_OP_MIN, Short128Vector.class, short.class, LENGTH,
 381             this, v,
 382             (v1, v2) -> ((Short128Vector)v1).bOp(v2, (i, a, b) -> (short) ((a < b) ? a : b)));
 383     }
 384 
 385     @Override
 386     @ForceInline
 387     public Short128Vector max(Vector<Short,Shapes.S128Bit> o) {
 388         Objects.requireNonNull(o);
 389         Short128Vector v = (Short128Vector)o;
 390         return VectorIntrinsics.binaryOp(
 391             VECTOR_OP_MAX, Short128Vector.class, short.class, LENGTH,
 392             this, v,
 393             (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short) ((a > b) ? a : b)));
 394         }
 395 
 396     @Override
 397     @ForceInline
 398     public Short128Vector and(Vector<Short,Shapes.S128Bit> o) {
 399         Objects.requireNonNull(o);
 400         Short128Vector v = (Short128Vector)o;
 401         return VectorIntrinsics.binaryOp(
 402             VECTOR_OP_AND, Short128Vector.class, short.class, LENGTH,
 403             this, v,
 404             (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a & b)));
 405     }
 406 
 407     @Override
 408     @ForceInline
 409     public Short128Vector or(Vector<Short,Shapes.S128Bit> o) {
 410         Objects.requireNonNull(o);
 411         Short128Vector v = (Short128Vector)o;
 412         return VectorIntrinsics.binaryOp(
 413             VECTOR_OP_OR, Short128Vector.class, short.class, LENGTH,
 414             this, v,
 415             (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a | b)));
 416     }
 417 
 418     @Override
 419     @ForceInline
 420     public Short128Vector xor(Vector<Short,Shapes.S128Bit> o) {
 421         Objects.requireNonNull(o);
 422         Short128Vector v = (Short128Vector)o;
 423         return VectorIntrinsics.binaryOp(
 424             VECTOR_OP_XOR, Short128Vector.class, short.class, LENGTH,
 425             this, v,
 426             (v1, v2) -> v1.bOp(v2, (i, a, b) -> (short)(a ^ b)));
 427     }
 428 
 429     @Override
 430     @ForceInline
 431     public Short128Vector and(Vector<Short,Shapes.S128Bit> v, Mask<Short, Shapes.S128Bit> m) {
 432         return blend(and(v), m);
 433     }
 434 
 435     @Override
 436     @ForceInline
 437     public Short128Vector or(Vector<Short,Shapes.S128Bit> v, Mask<Short, Shapes.S128Bit> m) {
 438         return blend(or(v), m);
 439     }
 440 
 441     @Override
 442     @ForceInline
 443     public Short128Vector xor(Vector<Short,Shapes.S128Bit> v, Mask<Short, Shapes.S128Bit> m) {
 444         return blend(xor(v), m);
 445     }
 446 
 447     // Ternary operations
 448 
 449 
 450     // Type specific horizontal reductions
 451 
 452     @Override
 453     @ForceInline
 454     public short addAll() {
 455         return (short) VectorIntrinsics.reductionCoerced(
 456             VECTOR_OP_ADD, Short128Vector.class, short.class, LENGTH,
 457             this,
 458             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a + b)));
 459     }
 460 
 461     @Override
 462     @ForceInline
 463     public short andAll() {
 464         return (short) VectorIntrinsics.reductionCoerced(
 465             VECTOR_OP_AND, Short128Vector.class, short.class, LENGTH,
 466             this,
 467             v -> (long) v.rOp((short) -1, (i, a, b) -> (short) (a & b)));
 468     }
 469 
 470     @Override
 471     @ForceInline
 472     public short andAll(Mask<Short, Shapes.S128Bit> m) {
 473         return blend(SPECIES.broadcast((short) -1), m).andAll();
 474     }
 475 
 476     @Override
 477     @ForceInline
 478     public short minAll() {
 479         return (short) VectorIntrinsics.reductionCoerced(
 480             VECTOR_OP_MIN, Short128Vector.class, short.class, LENGTH,
 481             this,
 482             v -> (long) v.rOp(Short.MAX_VALUE , (i, a, b) -> (short) ((a < b) ? a : b)));
 483     }
 484 
 485     @Override
 486     @ForceInline
 487     public short maxAll() {
 488         return (short) VectorIntrinsics.reductionCoerced(
 489             VECTOR_OP_MAX, Short128Vector.class, short.class, LENGTH,
 490             this,
 491             v -> (long) v.rOp(Short.MIN_VALUE , (i, a, b) -> (short) ((a > b) ? a : b)));
 492     }
 493 
 494     @Override
 495     @ForceInline
 496     public short mulAll() {
 497         return (short) VectorIntrinsics.reductionCoerced(
 498             VECTOR_OP_MUL, Short128Vector.class, short.class, LENGTH,
 499             this,
 500             v -> (long) v.rOp((short) 1, (i, a, b) -> (short) (a * b)));
 501     }
 502 
 503     @Override
 504     @ForceInline
 505     public short subAll() {
 506         return (short) VectorIntrinsics.reductionCoerced(
 507             VECTOR_OP_SUB, Short128Vector.class, short.class, LENGTH,
 508             this,
 509             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a - b)));
 510     }
 511 
 512     @Override
 513     @ForceInline
 514     public short orAll() {
 515         return (short) VectorIntrinsics.reductionCoerced(
 516             VECTOR_OP_OR, Short128Vector.class, short.class, LENGTH,
 517             this,
 518             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a | b)));
 519     }
 520 
 521     @Override
 522     @ForceInline
 523     public short orAll(Mask<Short, Shapes.S128Bit> m) {
 524         return blend(SPECIES.broadcast((short) 0), m).orAll();
 525     }
 526 
 527     @Override
 528     @ForceInline
 529     public short xorAll() {
 530         return (short) VectorIntrinsics.reductionCoerced(
 531             VECTOR_OP_XOR, Short128Vector.class, short.class, LENGTH,
 532             this,
 533             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a ^ b)));
 534     }
 535 
 536     @Override
 537     @ForceInline
 538     public short xorAll(Mask<Short, Shapes.S128Bit> m) {
 539         return blend(SPECIES.broadcast((short) 0), m).xorAll();
 540     }
 541 
 542 
 543     @Override
 544     @ForceInline
 545     public short addAll(Mask<Short, Shapes.S128Bit> m) {
 546         return blend(SPECIES.broadcast((short) 0), m).addAll();
 547     }
 548 
 549     @Override
 550     @ForceInline
 551     public short subAll(Mask<Short, Shapes.S128Bit> m) {
 552         return blend(SPECIES.broadcast((short) 0), m).subAll();
 553     }
 554 
 555     @Override
 556     @ForceInline
 557     public short mulAll(Mask<Short, Shapes.S128Bit> m) {
 558         return blend(SPECIES.broadcast((short) 1), m).mulAll();
 559     }
 560 
 561     @Override
 562     @ForceInline
 563     public short minAll(Mask<Short, Shapes.S128Bit> m) {
 564         return blend(SPECIES.broadcast(Short.MAX_VALUE), m).minAll();
 565     }
 566 
 567     @Override
 568     @ForceInline
 569     public short maxAll(Mask<Short, Shapes.S128Bit> m) {
 570         return blend(SPECIES.broadcast(Short.MIN_VALUE), m).maxAll();
 571     }
 572 
 573     @Override
 574     @ForceInline
 575     public Shuffle<Short, Shapes.S128Bit> toShuffle() {
 576         short[] a = toArray();
 577         int[] sa = new int[a.length];
 578         for (int i = 0; i < a.length; i++) {
 579             sa[i] = (int) a[i];
 580         }
 581         return SPECIES.shuffleFromArray(sa, 0);
 582     }
 583 
 584     // Memory operations
 585 
 586     private static final int ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(Unsafe.ARRAY_SHORT_INDEX_SCALE);
 587 
 588     @Override
 589     @ForceInline
 590     public void intoArray(short[] a, int ix) {
 591         Objects.requireNonNull(a);
 592         ix = VectorIntrinsics.checkIndex(ix, a.length, LENGTH);
 593         VectorIntrinsics.store(Short128Vector.class, short.class, LENGTH,
 594                                a, (((long) ix) << ARRAY_SHIFT) + Unsafe.ARRAY_SHORT_BASE_OFFSET,
 595                                this,
 596                                a, ix,
 597                                (arr, idx, v) -> v.forEach((i, e) -> arr[idx + i] = e));
 598     }
 599 
 600     @Override
 601     @ForceInline
 602     public final void intoArray(short[] a, int ax, Mask<Short, Shapes.S128Bit> m) {
 603         // @@@ This can result in out of bounds errors for unset mask lanes
 604         Short128Vector oldVal = SPECIES.fromArray(a, ax);
 605         Short128Vector newVal = oldVal.blend(this, m);
 606         newVal.intoArray(a, ax);
 607     }
 608 
 609     @Override
 610     @ForceInline
 611     public void intoByteArray(byte[] a, int ix) {
 612         // @@@ Endianess
 613         Objects.requireNonNull(a);
 614         ix = VectorIntrinsics.checkIndex(ix, a.length, bitSize() / Byte.SIZE);
 615         VectorIntrinsics.store(Short128Vector.class, short.class, LENGTH,
 616                                a, ((long) ix) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
 617                                this,
 618                                a, ix,
 619                                (c, idx, v) -> {
 620                                    ByteBuffer bbc = ByteBuffer.wrap(c, idx, c.length - idx).order(ByteOrder.nativeOrder());
 621                                    ShortBuffer tb = bbc.asShortBuffer();
 622                                    v.forEach((i, e) -> tb.put(e));
 623                                });
 624     }
 625 
 626     @Override
 627     @ForceInline
 628     public final void intoByteArray(byte[] a, int ix, Mask<Short, Shapes.S128Bit> m) {
 629         // @@@ This can result in out of bounds errors for unset mask lanes
 630         Short128Vector oldVal = SPECIES.fromByteArray(a, ix);
 631         Short128Vector newVal = oldVal.blend(this, m);
 632         newVal.intoByteArray(a, ix);
 633     }
 634 
 635     @Override
 636     @ForceInline
 637     public void intoByteBuffer(ByteBuffer bb, int ix) {
 638         // @@@ Endianess
 639         if (bb.order() != ByteOrder.nativeOrder()) {
 640             throw new IllegalArgumentException();
 641         }
 642         if (bb.isReadOnly()) {
 643             throw new ReadOnlyBufferException();
 644         }
 645         ix = VectorIntrinsics.checkIndex(ix, bb.limit(), bitSize() / Byte.SIZE);
 646         VectorIntrinsics.store(Short128Vector.class, short.class, LENGTH,
 647                                U.getObject(bb, BYTE_BUFFER_HB), ix + U.getLong(bb, BUFFER_ADDRESS),
 648                                this,
 649                                bb, ix,
 650                                (c, idx, v) -> {
 651                                    ByteBuffer bbc = c.duplicate().position(idx).order(ByteOrder.nativeOrder());
 652                                    ShortBuffer tb = bbc.asShortBuffer();
 653                                    v.forEach((i, e) -> tb.put(e));
 654                                });
 655     }
 656 
 657     @Override
 658     @ForceInline
 659     public void intoByteBuffer(ByteBuffer bb, int ix, Mask<Short, Shapes.S128Bit> m) {
 660         // @@@ This can result in out of bounds errors for unset mask lanes
 661         Short128Vector oldVal = SPECIES.fromByteBuffer(bb, ix);
 662         Short128Vector newVal = oldVal.blend(this, m);
 663         newVal.intoByteBuffer(bb, ix);
 664     }
 665 
 666     //
 667 
 668     @Override
 669     public String toString() {
 670         return Arrays.toString(getElements());
 671     }
 672 
 673     @Override
 674     public boolean equals(Object o) {
 675         if (this == o) return true;
 676         if (o == null || this.getClass() != o.getClass()) return false;
 677 
 678         // @@@ Use equal op
 679         Short128Vector that = (Short128Vector) o;
 680         return Arrays.equals(this.getElements(), that.getElements());
 681     }
 682 
 683     @Override
 684     public int hashCode() {
 685         return Arrays.hashCode(vec);
 686     }
 687 
 688     // Binary test
 689 
 690     @Override
 691     Short128Mask bTest(Vector<Short, Shapes.S128Bit> o, FBinTest f) {
 692         short[] vec1 = getElements();
 693         short[] vec2 = ((Short128Vector)o).getElements();
 694         boolean[] bits = new boolean[length()];
 695         for (int i = 0; i < length(); i++){
 696             bits[i] = f.apply(i, vec1[i], vec2[i]);
 697         }
 698         return new Short128Mask(bits);
 699     }
 700 
 701     // Comparisons
 702 
 703     @Override
 704     @ForceInline
 705     public Short128Mask equal(Vector<Short, Shapes.S128Bit> o) {
 706         Objects.requireNonNull(o);
 707         Short128Vector v = (Short128Vector)o;
 708 
 709         return VectorIntrinsics.compare(
 710             BT_eq, Short128Vector.class, Short128Mask.class, short.class, LENGTH,
 711             this, v,
 712             (v1, v2) -> v1.bTest(v2, (i, a, b) -> a == b));
 713     }
 714 
 715     @Override
 716     @ForceInline
 717     public Short128Mask notEqual(Vector<Short, Shapes.S128Bit> o) {
 718         Objects.requireNonNull(o);
 719         Short128Vector v = (Short128Vector)o;
 720 
 721         return VectorIntrinsics.compare(
 722             BT_ne, Short128Vector.class, Short128Mask.class, short.class, LENGTH,
 723             this, v,
 724             (v1, v2) -> v1.bTest(v2, (i, a, b) -> a != b));
 725     }
 726 
 727     @Override
 728     @ForceInline
 729     public Short128Mask lessThan(Vector<Short, Shapes.S128Bit> o) {
 730         Objects.requireNonNull(o);
 731         Short128Vector v = (Short128Vector)o;
 732 
 733         return VectorIntrinsics.compare(
 734             BT_lt, Short128Vector.class, Short128Mask.class, short.class, LENGTH,
 735             this, v,
 736             (v1, v2) -> v1.bTest(v2, (i, a, b) -> a < b));
 737     }
 738 
 739     @Override
 740     @ForceInline
 741     public Short128Mask lessThanEq(Vector<Short, Shapes.S128Bit> o) {
 742         Objects.requireNonNull(o);
 743         Short128Vector v = (Short128Vector)o;
 744 
 745         return VectorIntrinsics.compare(
 746             BT_le, Short128Vector.class, Short128Mask.class, short.class, LENGTH,
 747             this, v,
 748             (v1, v2) -> v1.bTest(v2, (i, a, b) -> a <= b));
 749     }
 750 
 751     @Override
 752     @ForceInline
 753     public Short128Mask greaterThan(Vector<Short, Shapes.S128Bit> o) {
 754         Objects.requireNonNull(o);
 755         Short128Vector v = (Short128Vector)o;
 756 
 757         return (Short128Mask) VectorIntrinsics.compare(
 758             BT_gt, Short128Vector.class, Short128Mask.class, short.class, LENGTH,
 759             this, v,
 760             (v1, v2) -> v1.bTest(v2, (i, a, b) -> a > b));
 761     }
 762 
 763     @Override
 764     @ForceInline
 765     public Short128Mask greaterThanEq(Vector<Short, Shapes.S128Bit> o) {
 766         Objects.requireNonNull(o);
 767         Short128Vector v = (Short128Vector)o;
 768 
 769         return VectorIntrinsics.compare(
 770             BT_ge, Short128Vector.class, Short128Mask.class, short.class, LENGTH,
 771             this, v,
 772             (v1, v2) -> v1.bTest(v2, (i, a, b) -> a >= b));
 773     }
 774 
 775     // Foreach
 776 
 777     @Override
 778     void forEach(FUnCon f) {
 779         short[] vec = getElements();
 780         for (int i = 0; i < length(); i++) {
 781             f.apply(i, vec[i]);
 782         }
 783     }
 784 
 785     @Override
 786     void forEach(Mask<Short, Shapes.S128Bit> o, FUnCon f) {
 787         boolean[] mbits = ((Short128Mask)o).getBits();
 788         forEach((i, a) -> {
 789             if (mbits[i]) { f.apply(i, a); }
 790         });
 791     }
 792 
 793 
 794 
 795     @Override
 796     public Short128Vector rotateEL(int j) {
 797         short[] vec = getElements();
 798         short[] res = new short[length()];
 799         for (int i = 0; i < length(); i++){
 800             res[(j + i) % length()] = vec[i];
 801         }
 802         return new Short128Vector(res);
 803     }
 804 
 805     @Override
 806     public Short128Vector rotateER(int j) {
 807         short[] vec = getElements();
 808         short[] res = new short[length()];
 809         for (int i = 0; i < length(); i++){
 810             int z = i - j;
 811             if(j < 0) {
 812                 res[length() + z] = vec[i];
 813             } else {
 814                 res[z] = vec[i];
 815             }
 816         }
 817         return new Short128Vector(res);
 818     }
 819 
 820     @Override
 821     public Short128Vector shiftEL(int j) {
 822         short[] vec = getElements();
 823         short[] res = new short[length()];
 824         for (int i = 0; i < length() - j; i++) {
 825             res[i] = vec[i + j];
 826         }
 827         return new Short128Vector(res);
 828     }
 829 
 830     @Override
 831     public Short128Vector shiftER(int j) {
 832         short[] vec = getElements();
 833         short[] res = new short[length()];
 834         for (int i = 0; i < length() - j; i++){
 835             res[i + j] = vec[i];
 836         }
 837         return new Short128Vector(res);
 838     }
 839 
 840     @Override
 841     @ForceInline
 842     public Short128Vector rearrange(Vector<Short, Shapes.S128Bit> v,
 843                                   Shuffle<Short, Shapes.S128Bit> s, Mask<Short, Shapes.S128Bit> m) {
 844         return this.rearrange(s).blend(v.rearrange(s), m);
 845     }
 846 
 847     @Override
 848     public Short128Vector rearrange(Shuffle<Short, Shapes.S128Bit> s) {
 849         return uOp((i, a) -> {
 850             short[] vec = this.getElements();
 851             int ei = s.getElement(i);
 852             return vec[ei];
 853         });
 854     }
 855 
 856     @Override
 857     @ForceInline
 858     public Short128Vector blend(Vector<Short, Shapes.S128Bit> o1, Mask<Short, Shapes.S128Bit> o2) {
 859         Objects.requireNonNull(o1);
 860         Objects.requireNonNull(o2);
 861         Short128Vector v = (Short128Vector)o1;
 862         Short128Mask   m = (Short128Mask)o2;
 863 
 864         return VectorIntrinsics.blend(
 865             Short128Vector.class, Short128Mask.class, short.class, LENGTH,
 866             this, v, m,
 867             (v1, v2, m_) -> v1.bOp(v2, (i, a, b) -> m_.getElement(i) ? b : a));
 868     }
 869 
 870     // Accessors
 871 
 872     @Override
 873     public short get(int i) {
 874         if (i < 0 || i >= LENGTH) {
 875             throw new IllegalArgumentException("Index " + i + " must be zero or positive, and less than " + LENGTH);
 876         }
 877         return (short) VectorIntrinsics.extract(
 878                                 Short128Vector.class, short.class, LENGTH,
 879                                 this, i,
 880                                 (vec, ix) -> {
 881                                     short[] vecarr = vec.getElements();
 882                                     return (long)vecarr[ix];
 883                                 });
 884     }
 885 
 886     @Override
 887     public Short128Vector with(int i, short e) {
 888         if (i < 0 || i >= LENGTH) {
 889             throw new IllegalArgumentException("Index " + i + " must be zero or positive, and less than " + LENGTH);
 890         }
 891         return VectorIntrinsics.insert(
 892                                 Short128Vector.class, short.class, LENGTH,
 893                                 this, i, (long)e,
 894                                 (v, ix, bits) -> {
 895                                     short[] res = v.getElements().clone();
 896                                     res[ix] = (short)bits;
 897                                     return new Short128Vector(res);
 898                                 });
 899     }
 900 
 901     // Mask
 902 
 903     static final class Short128Mask extends AbstractMask<Short, Shapes.S128Bit> {
 904         static final Short128Mask TRUE_MASK = new Short128Mask(true);
 905         static final Short128Mask FALSE_MASK = new Short128Mask(false);
 906 
 907         // FIXME: was temporarily put here to simplify rematerialization support in the JVM
 908         private final boolean[] bits; // Don't access directly, use getBits() instead.
 909 
 910         public Short128Mask(boolean[] bits) {
 911             this(bits, 0);
 912         }
 913 
 914         public Short128Mask(boolean[] bits, int offset) {
 915             boolean[] a = new boolean[species().length()];
 916             for (int i = 0; i < a.length; i++) {
 917                 a[i] = bits[offset + i];
 918             }
 919             this.bits = a;
 920         }
 921 
 922         public Short128Mask(boolean val) {
 923             boolean[] bits = new boolean[species().length()];
 924             Arrays.fill(bits, val);
 925             this.bits = bits;
 926         }
 927 
 928         boolean[] getBits() {
 929             return VectorIntrinsics.maybeRebox(this).bits;
 930         }
 931 
 932         @Override
 933         Short128Mask uOp(MUnOp f) {
 934             boolean[] res = new boolean[species().length()];
 935             boolean[] bits = getBits();
 936             for (int i = 0; i < species().length(); i++) {
 937                 res[i] = f.apply(i, bits[i]);
 938             }
 939             return new Short128Mask(res);
 940         }
 941 
 942         @Override
 943         Short128Mask bOp(Mask<Short, Shapes.S128Bit> o, MBinOp f) {
 944             boolean[] res = new boolean[species().length()];
 945             boolean[] bits = getBits();
 946             boolean[] mbits = ((Short128Mask)o).getBits();
 947             for (int i = 0; i < species().length(); i++) {
 948                 res[i] = f.apply(i, bits[i], mbits[i]);
 949             }
 950             return new Short128Mask(res);
 951         }
 952 
 953         @Override
 954         public Short128Species species() {
 955             return SPECIES;
 956         }
 957 
 958         @Override
 959         public Short128Vector toVector() {
 960             short[] res = new short[species().length()];
 961             boolean[] bits = getBits();
 962             for (int i = 0; i < species().length(); i++) {
 963                 // -1 will result in the most significant bit being set in
 964                 // addition to some or all other bits
 965                 res[i] = (short) (bits[i] ? -1 : 0);
 966             }
 967             return new Short128Vector(res);
 968         }
 969 
 970         // Unary operations
 971 
 972         @Override
 973         @ForceInline
 974         public Short128Mask not() {
 975             return (Short128Mask) VectorIntrinsics.unaryOp(
 976                                              VECTOR_OP_NOT, Short128Mask.class, short.class, LENGTH,
 977                                              this,
 978                                              (m1) -> m1.uOp((i, a) -> !a));
 979         }
 980 
 981         // Binary operations
 982 
 983         @Override
 984         @ForceInline
 985         public Short128Mask and(Mask<Short,Shapes.S128Bit> o) {
 986             Objects.requireNonNull(o);
 987             Short128Mask m = (Short128Mask)o;
 988             return VectorIntrinsics.binaryOp(VECTOR_OP_AND, Short128Mask.class, short.class, LENGTH,
 989                                              this, m,
 990                                              (m1, m2) -> m1.bOp(m2, (i, a, b) -> a & b));
 991         }
 992 
 993         @Override
 994         @ForceInline
 995         public Short128Mask or(Mask<Short,Shapes.S128Bit> o) {
 996             Objects.requireNonNull(o);
 997             Short128Mask m = (Short128Mask)o;
 998             return VectorIntrinsics.binaryOp(VECTOR_OP_OR, Short128Mask.class, short.class, LENGTH,
 999                                              this, m,
1000                                              (m1, m2) -> m1.bOp(m2, (i, a, b) -> a | b));
1001         }
1002 
1003         // Reductions
1004 
1005         @Override
1006         @ForceInline
1007         public boolean anyTrue() {
1008             return VectorIntrinsics.test(COND_notZero, Short128Mask.class, short.class, LENGTH,
1009                                          this, this,
1010                                          (m1, m2) -> super.anyTrue());
1011         }
1012 
1013         @Override
1014         @ForceInline
1015         public boolean allTrue() {
1016             return VectorIntrinsics.test(COND_carrySet, Short128Mask.class, short.class, LENGTH,
1017                                          this, species().maskAllTrue(),
1018                                          (m1, m2) -> super.allTrue());
1019         }
1020     }
1021 
1022     // Shuffle
1023 
1024     static final class Short128Shuffle extends AbstractShuffle<Short, Shapes.S128Bit> {
1025         Short128Shuffle(byte[] reorder) {
1026             super(reorder);
1027         }
1028 
1029         public Short128Shuffle(int[] reorder) {
1030             super(reorder);
1031         }
1032 
1033         public Short128Shuffle(int[] reorder, int i) {
1034             super(reorder, i);
1035         }
1036 
1037         public Short128Shuffle(IntUnaryOperator f) {
1038             super(f);
1039         }
1040 
1041         @Override
1042         public Short128Species species() {
1043             return SPECIES;
1044         }
1045 
1046         @Override
1047         public Short128Vector toVector() {
1048             short[] va = new short[SPECIES.length()];
1049             for (int i = 0; i < va.length; i++) {
1050               va[i] = (short) getElement(i);
1051             }
1052             return species().fromArray(va, 0);
1053         }
1054 
1055         @Override
1056         public Short128Shuffle rearrange(Vector.Shuffle<Short, Shapes.S128Bit> o) {
1057             Short128Shuffle s = (Short128Shuffle) o;
1058             byte[] r = new byte[reorder.length];
1059             for (int i = 0; i < reorder.length; i++) {
1060                 r[i] = reorder[s.reorder[i]];
1061             }
1062             return new Short128Shuffle(r);
1063         }
1064     }
1065 
1066     // Species
1067 
1068     @Override
1069     public Short128Species species() {
1070         return SPECIES;
1071     }
1072 
1073     static final class Short128Species extends ShortSpecies<Shapes.S128Bit> {
1074         static final int BIT_SIZE = Shapes.S_128_BIT.bitSize();
1075 
1076         static final int LENGTH = BIT_SIZE / Short.SIZE;
1077 
1078         @Override
1079         public String toString() {
1080            StringBuilder sb = new StringBuilder("Shape[");
1081            sb.append(bitSize()).append(" bits, ");
1082            sb.append(length()).append(" ").append(short.class.getSimpleName()).append("s x ");
1083            sb.append(elementSize()).append(" bits");
1084            sb.append("]");
1085            return sb.toString();
1086         }
1087 
1088         @Override
1089         @ForceInline
1090         public int bitSize() {
1091             return BIT_SIZE;
1092         }
1093 
1094         @Override
1095         @ForceInline
1096         public int length() {
1097             return LENGTH;
1098         }
1099 
1100         @Override
1101         @ForceInline
1102         public Class<Short> elementType() {
1103             return short.class;
1104         }
1105 
1106         @Override
1107         @ForceInline
1108         public int elementSize() {
1109             return Short.SIZE;
1110         }
1111 
1112         @Override
1113         @ForceInline
1114         public Shapes.S128Bit shape() {
1115             return Shapes.S_128_BIT;
1116         }
1117 
1118         @Override
1119         Short128Vector op(FOp f) {
1120             short[] res = new short[length()];
1121             for (int i = 0; i < length(); i++) {
1122                 res[i] = f.apply(i);
1123             }
1124             return new Short128Vector(res);
1125         }
1126 
1127         @Override
1128         Short128Vector op(Mask<Short, Shapes.S128Bit> o, FOp f) {
1129             short[] res = new short[length()];
1130             boolean[] mbits = ((Short128Mask)o).getBits();
1131             for (int i = 0; i < length(); i++) {
1132                 if (mbits[i]) {
1133                     res[i] = f.apply(i);
1134                 }
1135             }
1136             return new Short128Vector(res);
1137         }
1138 
1139         // Factories
1140 
1141         @Override
1142         public Short128Mask maskFromValues(boolean... bits) {
1143             return new Short128Mask(bits);
1144         }
1145 
1146         @Override
1147         public Short128Mask maskFromArray(boolean[] bits, int i) {
1148             return new Short128Mask(bits, i);
1149         }
1150 
1151         @Override
1152         public Short128Shuffle shuffle(IntUnaryOperator f) {
1153             return new Short128Shuffle(f);
1154         }
1155 
1156         @Override
1157         public Short128Shuffle shuffleIota() {
1158             return new Short128Shuffle(AbstractShuffle.IDENTITY);
1159         }
1160 
1161         @Override
1162         public Short128Shuffle shuffleFromValues(int... ixs) {
1163             return new Short128Shuffle(ixs);
1164         }
1165 
1166         @Override
1167         public Short128Shuffle shuffleFromArray(int[] ixs, int i) {
1168             return new Short128Shuffle(ixs, i);
1169         }
1170 
1171         @Override
1172         @ForceInline
1173         public Short128Vector zero() {
1174             return VectorIntrinsics.broadcastCoerced(Short128Vector.class, short.class, LENGTH,
1175                                                      0,
1176                                                      (z -> ZERO));
1177         }
1178 
1179         @Override
1180         @ForceInline
1181         public Short128Vector broadcast(short e) {
1182             return VectorIntrinsics.broadcastCoerced(
1183                 Short128Vector.class, short.class, LENGTH,
1184                 e,
1185                 ((long bits) -> SPECIES.op(i -> (short)bits)));
1186         }
1187 
1188         @Override
1189         @ForceInline
1190         public Short128Mask maskAllTrue() {
1191             return VectorIntrinsics.broadcastCoerced(Short128Mask.class, short.class, LENGTH,
1192                                                      (short)-1,
1193                                                      (z -> Short128Mask.TRUE_MASK));
1194         }
1195 
1196         @Override
1197         @ForceInline
1198         public Short128Mask maskAllFalse() {
1199             return VectorIntrinsics.broadcastCoerced(Short128Mask.class, short.class, LENGTH,
1200                                                      0,
1201                                                      (z -> Short128Mask.FALSE_MASK));
1202         }
1203 
1204         @Override
1205         @ForceInline
1206         public Short128Vector scalars(short... es) {
1207             Objects.requireNonNull(es);
1208             int ix = VectorIntrinsics.checkIndex(0, es.length, LENGTH);
1209             return VectorIntrinsics.load(Short128Vector.class, short.class, LENGTH,
1210                                          es, Unsafe.ARRAY_SHORT_BASE_OFFSET,
1211                                          es, ix,
1212                                          (c, idx) -> op(n -> c[idx + n]));
1213         }
1214 
1215         @Override
1216         @ForceInline
1217         public Short128Vector fromArray(short[] a, int ix) {
1218             Objects.requireNonNull(a);
1219             ix = VectorIntrinsics.checkIndex(ix, a.length, LENGTH);
1220             return VectorIntrinsics.load(Short128Vector.class, short.class, LENGTH,
1221                                          a, (((long) ix) << ARRAY_SHIFT) + Unsafe.ARRAY_SHORT_BASE_OFFSET,
1222                                          a, ix,
1223                                          (c, idx) -> op(n -> c[idx + n]));
1224         }
1225 
1226         @Override
1227         @ForceInline
1228         public Short128Vector fromArray(short[] a, int ax, Mask<Short, Shapes.S128Bit> m) {
1229             // @@@ This can result in out of bounds errors for unset mask lanes
1230             return zero().blend(fromArray(a, ax), m);
1231         }
1232 
1233         @Override
1234         @ForceInline
1235         public Short128Vector fromByteArray(byte[] a, int ix) {
1236             // @@@ Endianess
1237             Objects.requireNonNull(a);
1238             ix = VectorIntrinsics.checkIndex(ix, a.length, bitSize() / Byte.SIZE);
1239             return VectorIntrinsics.load(Short128Vector.class, short.class, LENGTH,
1240                                          a, ((long) ix) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
1241                                          a, ix,
1242                                          (c, idx) -> {
1243                                              ByteBuffer bbc = ByteBuffer.wrap(c, idx, a.length - idx).order(ByteOrder.nativeOrder());
1244                                              ShortBuffer tb = bbc.asShortBuffer();
1245                                              return op(i -> tb.get());
1246                                          });
1247         }
1248 
1249         @Override
1250         @ForceInline
1251         public Short128Vector fromByteArray(byte[] a, int ix, Mask<Short, Shapes.S128Bit> m) {
1252             // @@@ This can result in out of bounds errors for unset mask lanes
1253             return zero().blend(fromByteArray(a, ix), m);
1254         }
1255 
1256         @Override
1257         @ForceInline
1258         public Short128Vector fromByteBuffer(ByteBuffer bb, int ix) {
1259             // @@@ Endianess
1260             if (bb.order() != ByteOrder.nativeOrder()) {
1261                 throw new IllegalArgumentException();
1262             }
1263             ix = VectorIntrinsics.checkIndex(ix, bb.limit(), bitSize() / Byte.SIZE);
1264             return VectorIntrinsics.load(Short128Vector.class, short.class, LENGTH,
1265                                          U.getObject(bb, BYTE_BUFFER_HB), U.getLong(bb, BUFFER_ADDRESS) + ix,
1266                                          bb, ix,
1267                                          (c, idx) -> {
1268                                              ByteBuffer bbc = c.duplicate().position(idx).order(ByteOrder.nativeOrder());
1269                                              ShortBuffer tb = bbc.asShortBuffer();
1270                                              return op(i -> tb.get());
1271                                          });
1272         }
1273 
1274         @Override
1275         @ForceInline
1276         public Short128Vector fromByteBuffer(ByteBuffer bb, int ix, Mask<Short, Shapes.S128Bit> m) {
1277             // @@@ This can result in out of bounds errors for unset mask lanes
1278             return zero().blend(fromByteBuffer(bb, ix), m);
1279         }
1280 
1281         @Override
1282         @ForceInline
1283         @SuppressWarnings("unchecked")
1284         public <F, T extends Shape> Short128Vector cast(Vector<F, T> o) {
1285             if (o.length() != LENGTH)
1286                 throw new IllegalArgumentException("Vector length this species length differ");
1287 
1288             return VectorIntrinsics.cast(
1289                 o.getClass(),
1290                 o.elementType(), LENGTH,
1291                 short.class, LENGTH,
1292                 o, this,
1293                 (s, v) -> s.castDefault(v)
1294             );
1295         }
1296 
1297         @SuppressWarnings("unchecked")
1298         @ForceInline
1299         private <F, T extends Shape> Short128Vector castDefault(Vector<F, T> v) {
1300             // Allocate array of required size
1301             int limit = length();
1302             short[] a = new short[limit];
1303 
1304             Class<?> vtype = v.species().elementType();
1305             if (vtype == byte.class) {
1306                 ByteVector<T> tv = (ByteVector<T>)v;
1307                 for (int i = 0; i < limit; i++) {
1308                     a[i] = (short) tv.get(i);
1309                 }
1310             } else if (vtype == short.class) {
1311                 ShortVector<T> tv = (ShortVector<T>)v;
1312                 for (int i = 0; i < limit; i++) {
1313                     a[i] = (short) tv.get(i);
1314                 }
1315             } else if (vtype == int.class) {
1316                 IntVector<T> tv = (IntVector<T>)v;
1317                 for (int i = 0; i < limit; i++) {
1318                     a[i] = (short) tv.get(i);
1319                 }
1320             } else if (vtype == long.class){
1321                 LongVector<T> tv = (LongVector<T>)v;
1322                 for (int i = 0; i < limit; i++) {
1323                     a[i] = (short) tv.get(i);
1324                 }
1325             } else if (vtype == float.class){
1326                 FloatVector<T> tv = (FloatVector<T>)v;
1327                 for (int i = 0; i < limit; i++) {
1328                     a[i] = (short) tv.get(i);
1329                 }
1330             } else if (vtype == double.class){
1331                 DoubleVector<T> tv = (DoubleVector<T>)v;
1332                 for (int i = 0; i < limit; i++) {
1333                     a[i] = (short) tv.get(i);
1334                 }
1335             } else {
1336                 throw new UnsupportedOperationException("Bad lane type for casting.");
1337             }
1338 
1339             return scalars(a);
1340         }
1341 
1342         @Override
1343         @ForceInline
1344         public <E, S extends Shape> Short128Mask cast(Mask<E, S> m) {
1345             if (m.length() != LENGTH)
1346                 throw new IllegalArgumentException("Mask length this species length differ");
1347             return new Short128Mask(m.toArray());
1348         }
1349 
1350         @Override
1351         @ForceInline
1352         public <E, S extends Shape> Short128Shuffle cast(Shuffle<E, S> s) {
1353             if (s.length() != LENGTH)
1354                 throw new IllegalArgumentException("Shuffle length this species length differ");
1355             return new Short128Shuffle(s.toArray());
1356         }
1357 
1358         @Override
1359         @ForceInline
1360         @SuppressWarnings("unchecked")
1361         public <F> Short128Vector rebracket(Vector<F, Shapes.S128Bit> o) {
1362             Objects.requireNonNull(o);
1363             if (o.elementType() == byte.class) {
1364                 Byte128Vector so = (Byte128Vector)o;
1365                 return VectorIntrinsics.reinterpret(
1366                     Byte128Vector.class,
1367                     byte.class, so.length(),
1368                     short.class, LENGTH,
1369                     so, this,
1370                     (s, v) -> (Short128Vector) s.reshape(v)
1371                 );
1372             } else if (o.elementType() == short.class) {
1373                 Short128Vector so = (Short128Vector)o;
1374                 return VectorIntrinsics.reinterpret(
1375                     Short128Vector.class,
1376                     short.class, so.length(),
1377                     short.class, LENGTH,
1378                     so, this,
1379                     (s, v) -> (Short128Vector) s.reshape(v)
1380                 );
1381             } else if (o.elementType() == int.class) {
1382                 Int128Vector so = (Int128Vector)o;
1383                 return VectorIntrinsics.reinterpret(
1384                     Int128Vector.class,
1385                     int.class, so.length(),
1386                     short.class, LENGTH,
1387                     so, this,
1388                     (s, v) -> (Short128Vector) s.reshape(v)
1389                 );
1390             } else if (o.elementType() == long.class) {
1391                 Long128Vector so = (Long128Vector)o;
1392                 return VectorIntrinsics.reinterpret(
1393                     Long128Vector.class,
1394                     long.class, so.length(),
1395                     short.class, LENGTH,
1396                     so, this,
1397                     (s, v) -> (Short128Vector) s.reshape(v)
1398                 );
1399             } else if (o.elementType() == float.class) {
1400                 Float128Vector so = (Float128Vector)o;
1401                 return VectorIntrinsics.reinterpret(
1402                     Float128Vector.class,
1403                     float.class, so.length(),
1404                     short.class, LENGTH,
1405                     so, this,
1406                     (s, v) -> (Short128Vector) s.reshape(v)
1407                 );
1408             } else if (o.elementType() == double.class) {
1409                 Double128Vector so = (Double128Vector)o;
1410                 return VectorIntrinsics.reinterpret(
1411                     Double128Vector.class,
1412                     double.class, so.length(),
1413                     short.class, LENGTH,
1414                     so, this,
1415                     (s, v) -> (Short128Vector) s.reshape(v)
1416                 );
1417             } else {
1418                 throw new InternalError("Unimplemented type");
1419             }
1420         }
1421 
1422         @Override
1423         @ForceInline
1424         @SuppressWarnings("unchecked")
1425         public <T extends Shape> Short128Vector resize(Vector<Short, T> o) {
1426             Objects.requireNonNull(o);
1427             if (o.bitSize() == 64) {
1428                 Short64Vector so = (Short64Vector)o;
1429                 return VectorIntrinsics.reinterpret(
1430                     Short64Vector.class,
1431                     short.class, so.length(),
1432                     short.class, LENGTH,
1433                     so, this,
1434                     (s, v) -> (Short128Vector) s.reshape(v)
1435                 );
1436             } else if (o.bitSize() == 128) {
1437                 Short128Vector so = (Short128Vector)o;
1438                 return VectorIntrinsics.reinterpret(
1439                     Short128Vector.class,
1440                     short.class, so.length(),
1441                     short.class, LENGTH,
1442                     so, this,
1443                     (s, v) -> (Short128Vector) s.reshape(v)
1444                 );
1445             } else if (o.bitSize() == 256) {
1446                 Short256Vector so = (Short256Vector)o;
1447                 return VectorIntrinsics.reinterpret(
1448                     Short256Vector.class,
1449                     short.class, so.length(),
1450                     short.class, LENGTH,
1451                     so, this,
1452                     (s, v) -> (Short128Vector) s.reshape(v)
1453                 );
1454             } else if (o.bitSize() == 512) {
1455                 Short512Vector so = (Short512Vector)o;
1456                 return VectorIntrinsics.reinterpret(
1457                     Short512Vector.class,
1458                     short.class, so.length(),
1459                     short.class, LENGTH,
1460                     so, this,
1461                     (s, v) -> (Short128Vector) s.reshape(v)
1462                 );
1463             } else {
1464                 throw new InternalError("Unimplemented size");
1465             }
1466         }
1467     }
1468 }