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     @ForceInline
 849     public Short128Vector rearrange(Shuffle<Short, Shapes.S128Bit> o1) {
 850     Objects.requireNonNull(o1);
 851     Short128Shuffle s =  (Short128Shuffle)o1;
 852 
 853         return VectorIntrinsics.rearrangeOp(
 854             Short128Vector.class, Short128Shuffle.class, short.class, LENGTH,
 855             this, s,
 856             (v1, s_) -> v1.uOp((i, a) -> {
 857             short[] vec = this.getElements();
 858             int ei = s_.getElement(i);
 859             return vec[ei];
 860         }));
 861     }
 862 
 863     @Override
 864     @ForceInline
 865     public Short128Vector blend(Vector<Short, Shapes.S128Bit> o1, Mask<Short, Shapes.S128Bit> o2) {
 866         Objects.requireNonNull(o1);
 867         Objects.requireNonNull(o2);
 868         Short128Vector v = (Short128Vector)o1;
 869         Short128Mask   m = (Short128Mask)o2;
 870 
 871         return VectorIntrinsics.blend(
 872             Short128Vector.class, Short128Mask.class, short.class, LENGTH,
 873             this, v, m,
 874             (v1, v2, m_) -> v1.bOp(v2, (i, a, b) -> m_.getElement(i) ? b : a));
 875     }
 876 
 877     // Accessors
 878 
 879     @Override
 880     public short get(int i) {
 881         if (i < 0 || i >= LENGTH) {
 882             throw new IllegalArgumentException("Index " + i + " must be zero or positive, and less than " + LENGTH);
 883         }
 884         return (short) VectorIntrinsics.extract(
 885                                 Short128Vector.class, short.class, LENGTH,
 886                                 this, i,
 887                                 (vec, ix) -> {
 888                                     short[] vecarr = vec.getElements();
 889                                     return (long)vecarr[ix];
 890                                 });
 891     }
 892 
 893     @Override
 894     public Short128Vector with(int i, short e) {
 895         if (i < 0 || i >= LENGTH) {
 896             throw new IllegalArgumentException("Index " + i + " must be zero or positive, and less than " + LENGTH);
 897         }
 898         return VectorIntrinsics.insert(
 899                                 Short128Vector.class, short.class, LENGTH,
 900                                 this, i, (long)e,
 901                                 (v, ix, bits) -> {
 902                                     short[] res = v.getElements().clone();
 903                                     res[ix] = (short)bits;
 904                                     return new Short128Vector(res);
 905                                 });
 906     }
 907 
 908     // Mask
 909 
 910     static final class Short128Mask extends AbstractMask<Short, Shapes.S128Bit> {
 911         static final Short128Mask TRUE_MASK = new Short128Mask(true);
 912         static final Short128Mask FALSE_MASK = new Short128Mask(false);
 913 
 914         // FIXME: was temporarily put here to simplify rematerialization support in the JVM
 915         private final boolean[] bits; // Don't access directly, use getBits() instead.
 916 
 917         public Short128Mask(boolean[] bits) {
 918             this(bits, 0);
 919         }
 920 
 921         public Short128Mask(boolean[] bits, int offset) {
 922             boolean[] a = new boolean[species().length()];
 923             for (int i = 0; i < a.length; i++) {
 924                 a[i] = bits[offset + i];
 925             }
 926             this.bits = a;
 927         }
 928 
 929         public Short128Mask(boolean val) {
 930             boolean[] bits = new boolean[species().length()];
 931             Arrays.fill(bits, val);
 932             this.bits = bits;
 933         }
 934 
 935         boolean[] getBits() {
 936             return VectorIntrinsics.maybeRebox(this).bits;
 937         }
 938 
 939         @Override
 940         Short128Mask uOp(MUnOp f) {
 941             boolean[] res = new boolean[species().length()];
 942             boolean[] bits = getBits();
 943             for (int i = 0; i < species().length(); i++) {
 944                 res[i] = f.apply(i, bits[i]);
 945             }
 946             return new Short128Mask(res);
 947         }
 948 
 949         @Override
 950         Short128Mask bOp(Mask<Short, Shapes.S128Bit> o, MBinOp f) {
 951             boolean[] res = new boolean[species().length()];
 952             boolean[] bits = getBits();
 953             boolean[] mbits = ((Short128Mask)o).getBits();
 954             for (int i = 0; i < species().length(); i++) {
 955                 res[i] = f.apply(i, bits[i], mbits[i]);
 956             }
 957             return new Short128Mask(res);
 958         }
 959 
 960         @Override
 961         public Short128Species species() {
 962             return SPECIES;
 963         }
 964 
 965         @Override
 966         public Short128Vector toVector() {
 967             short[] res = new short[species().length()];
 968             boolean[] bits = getBits();
 969             for (int i = 0; i < species().length(); i++) {
 970                 // -1 will result in the most significant bit being set in
 971                 // addition to some or all other bits
 972                 res[i] = (short) (bits[i] ? -1 : 0);
 973             }
 974             return new Short128Vector(res);
 975         }
 976 
 977         // Unary operations
 978 
 979         @Override
 980         @ForceInline
 981         public Short128Mask not() {
 982             return (Short128Mask) VectorIntrinsics.unaryOp(
 983                                              VECTOR_OP_NOT, Short128Mask.class, short.class, LENGTH,
 984                                              this,
 985                                              (m1) -> m1.uOp((i, a) -> !a));
 986         }
 987 
 988         // Binary operations
 989 
 990         @Override
 991         @ForceInline
 992         public Short128Mask and(Mask<Short,Shapes.S128Bit> o) {
 993             Objects.requireNonNull(o);
 994             Short128Mask m = (Short128Mask)o;
 995             return VectorIntrinsics.binaryOp(VECTOR_OP_AND, Short128Mask.class, short.class, LENGTH,
 996                                              this, m,
 997                                              (m1, m2) -> m1.bOp(m2, (i, a, b) -> a & b));
 998         }
 999 
1000         @Override
1001         @ForceInline
1002         public Short128Mask or(Mask<Short,Shapes.S128Bit> o) {
1003             Objects.requireNonNull(o);
1004             Short128Mask m = (Short128Mask)o;
1005             return VectorIntrinsics.binaryOp(VECTOR_OP_OR, Short128Mask.class, short.class, LENGTH,
1006                                              this, m,
1007                                              (m1, m2) -> m1.bOp(m2, (i, a, b) -> a | b));
1008         }
1009 
1010         // Reductions
1011 
1012         @Override
1013         @ForceInline
1014         public boolean anyTrue() {
1015             return VectorIntrinsics.test(COND_notZero, Short128Mask.class, short.class, LENGTH,
1016                                          this, this,
1017                                          (m1, m2) -> super.anyTrue());
1018         }
1019 
1020         @Override
1021         @ForceInline
1022         public boolean allTrue() {
1023             return VectorIntrinsics.test(COND_carrySet, Short128Mask.class, short.class, LENGTH,
1024                                          this, species().maskAllTrue(),
1025                                          (m1, m2) -> super.allTrue());
1026         }
1027     }
1028 
1029     // Shuffle
1030 
1031     static final class Short128Shuffle extends AbstractShuffle<Short, Shapes.S128Bit> {
1032         Short128Shuffle(byte[] reorder) {
1033             super(reorder);
1034         }
1035 
1036         public Short128Shuffle(int[] reorder) {
1037             super(reorder);
1038         }
1039 
1040         public Short128Shuffle(int[] reorder, int i) {
1041             super(reorder, i);
1042         }
1043 
1044         public Short128Shuffle(IntUnaryOperator f) {
1045             super(f);
1046         }
1047 
1048         @Override
1049         public Short128Species species() {
1050             return SPECIES;
1051         }
1052 
1053         @Override
1054         public Short128Vector toVector() {
1055             short[] va = new short[SPECIES.length()];
1056             for (int i = 0; i < va.length; i++) {
1057               va[i] = (short) getElement(i);
1058             }
1059             return species().fromArray(va, 0);
1060         }
1061 
1062         @Override
1063         public Short128Shuffle rearrange(Vector.Shuffle<Short, Shapes.S128Bit> o) {
1064             Short128Shuffle s = (Short128Shuffle) o;
1065             byte[] r = new byte[reorder.length];
1066             for (int i = 0; i < reorder.length; i++) {
1067                 r[i] = reorder[s.reorder[i]];
1068             }
1069             return new Short128Shuffle(r);
1070         }
1071     }
1072 
1073     // Species
1074 
1075     @Override
1076     public Short128Species species() {
1077         return SPECIES;
1078     }
1079 
1080     static final class Short128Species extends ShortSpecies<Shapes.S128Bit> {
1081         static final int BIT_SIZE = Shapes.S_128_BIT.bitSize();
1082 
1083         static final int LENGTH = BIT_SIZE / Short.SIZE;
1084 
1085         @Override
1086         public String toString() {
1087            StringBuilder sb = new StringBuilder("Shape[");
1088            sb.append(bitSize()).append(" bits, ");
1089            sb.append(length()).append(" ").append(short.class.getSimpleName()).append("s x ");
1090            sb.append(elementSize()).append(" bits");
1091            sb.append("]");
1092            return sb.toString();
1093         }
1094 
1095         @Override
1096         @ForceInline
1097         public int bitSize() {
1098             return BIT_SIZE;
1099         }
1100 
1101         @Override
1102         @ForceInline
1103         public int length() {
1104             return LENGTH;
1105         }
1106 
1107         @Override
1108         @ForceInline
1109         public Class<Short> elementType() {
1110             return short.class;
1111         }
1112 
1113         @Override
1114         @ForceInline
1115         public int elementSize() {
1116             return Short.SIZE;
1117         }
1118 
1119         @Override
1120         @ForceInline
1121         public Shapes.S128Bit shape() {
1122             return Shapes.S_128_BIT;
1123         }
1124 
1125         @Override
1126         Short128Vector op(FOp f) {
1127             short[] res = new short[length()];
1128             for (int i = 0; i < length(); i++) {
1129                 res[i] = f.apply(i);
1130             }
1131             return new Short128Vector(res);
1132         }
1133 
1134         @Override
1135         Short128Vector op(Mask<Short, Shapes.S128Bit> o, FOp f) {
1136             short[] res = new short[length()];
1137             boolean[] mbits = ((Short128Mask)o).getBits();
1138             for (int i = 0; i < length(); i++) {
1139                 if (mbits[i]) {
1140                     res[i] = f.apply(i);
1141                 }
1142             }
1143             return new Short128Vector(res);
1144         }
1145 
1146         // Factories
1147 
1148         @Override
1149         public Short128Mask maskFromValues(boolean... bits) {
1150             return new Short128Mask(bits);
1151         }
1152 
1153         @Override
1154         public Short128Mask maskFromArray(boolean[] bits, int i) {
1155             return new Short128Mask(bits, i);
1156         }
1157 
1158         @Override
1159         public Short128Shuffle shuffle(IntUnaryOperator f) {
1160             return new Short128Shuffle(f);
1161         }
1162 
1163         @Override
1164         public Short128Shuffle shuffleIota() {
1165             return new Short128Shuffle(AbstractShuffle.IDENTITY);
1166         }
1167 
1168         @Override
1169         public Short128Shuffle shuffleFromValues(int... ixs) {
1170             return new Short128Shuffle(ixs);
1171         }
1172 
1173         @Override
1174         public Short128Shuffle shuffleFromArray(int[] ixs, int i) {
1175             return new Short128Shuffle(ixs, i);
1176         }
1177 
1178         @Override
1179         @ForceInline
1180         public Short128Vector zero() {
1181             return VectorIntrinsics.broadcastCoerced(Short128Vector.class, short.class, LENGTH,
1182                                                      0,
1183                                                      (z -> ZERO));
1184         }
1185 
1186         @Override
1187         @ForceInline
1188         public Short128Vector broadcast(short e) {
1189             return VectorIntrinsics.broadcastCoerced(
1190                 Short128Vector.class, short.class, LENGTH,
1191                 e,
1192                 ((long bits) -> SPECIES.op(i -> (short)bits)));
1193         }
1194 
1195         @Override
1196         @ForceInline
1197         public Short128Mask maskAllTrue() {
1198             return VectorIntrinsics.broadcastCoerced(Short128Mask.class, short.class, LENGTH,
1199                                                      (short)-1,
1200                                                      (z -> Short128Mask.TRUE_MASK));
1201         }
1202 
1203         @Override
1204         @ForceInline
1205         public Short128Mask maskAllFalse() {
1206             return VectorIntrinsics.broadcastCoerced(Short128Mask.class, short.class, LENGTH,
1207                                                      0,
1208                                                      (z -> Short128Mask.FALSE_MASK));
1209         }
1210 
1211         @Override
1212         @ForceInline
1213         public Short128Vector scalars(short... es) {
1214             Objects.requireNonNull(es);
1215             int ix = VectorIntrinsics.checkIndex(0, es.length, LENGTH);
1216             return VectorIntrinsics.load(Short128Vector.class, short.class, LENGTH,
1217                                          es, Unsafe.ARRAY_SHORT_BASE_OFFSET,
1218                                          es, ix,
1219                                          (c, idx) -> op(n -> c[idx + n]));
1220         }
1221 
1222         @Override
1223         @ForceInline
1224         public Short128Vector fromArray(short[] a, int ix) {
1225             Objects.requireNonNull(a);
1226             ix = VectorIntrinsics.checkIndex(ix, a.length, LENGTH);
1227             return VectorIntrinsics.load(Short128Vector.class, short.class, LENGTH,
1228                                          a, (((long) ix) << ARRAY_SHIFT) + Unsafe.ARRAY_SHORT_BASE_OFFSET,
1229                                          a, ix,
1230                                          (c, idx) -> op(n -> c[idx + n]));
1231         }
1232 
1233         @Override
1234         @ForceInline
1235         public Short128Vector fromArray(short[] a, int ax, Mask<Short, Shapes.S128Bit> m) {
1236             // @@@ This can result in out of bounds errors for unset mask lanes
1237             return zero().blend(fromArray(a, ax), m);
1238         }
1239 
1240         @Override
1241         @ForceInline
1242         public Short128Vector fromByteArray(byte[] a, int ix) {
1243             // @@@ Endianess
1244             Objects.requireNonNull(a);
1245             ix = VectorIntrinsics.checkIndex(ix, a.length, bitSize() / Byte.SIZE);
1246             return VectorIntrinsics.load(Short128Vector.class, short.class, LENGTH,
1247                                          a, ((long) ix) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
1248                                          a, ix,
1249                                          (c, idx) -> {
1250                                              ByteBuffer bbc = ByteBuffer.wrap(c, idx, a.length - idx).order(ByteOrder.nativeOrder());
1251                                              ShortBuffer tb = bbc.asShortBuffer();
1252                                              return op(i -> tb.get());
1253                                          });
1254         }
1255 
1256         @Override
1257         @ForceInline
1258         public Short128Vector fromByteArray(byte[] a, int ix, Mask<Short, Shapes.S128Bit> m) {
1259             // @@@ This can result in out of bounds errors for unset mask lanes
1260             return zero().blend(fromByteArray(a, ix), m);
1261         }
1262 
1263         @Override
1264         @ForceInline
1265         public Short128Vector fromByteBuffer(ByteBuffer bb, int ix) {
1266             // @@@ Endianess
1267             if (bb.order() != ByteOrder.nativeOrder()) {
1268                 throw new IllegalArgumentException();
1269             }
1270             ix = VectorIntrinsics.checkIndex(ix, bb.limit(), bitSize() / Byte.SIZE);
1271             return VectorIntrinsics.load(Short128Vector.class, short.class, LENGTH,
1272                                          U.getObject(bb, BYTE_BUFFER_HB), U.getLong(bb, BUFFER_ADDRESS) + ix,
1273                                          bb, ix,
1274                                          (c, idx) -> {
1275                                              ByteBuffer bbc = c.duplicate().position(idx).order(ByteOrder.nativeOrder());
1276                                              ShortBuffer tb = bbc.asShortBuffer();
1277                                              return op(i -> tb.get());
1278                                          });
1279         }
1280 
1281         @Override
1282         @ForceInline
1283         public Short128Vector fromByteBuffer(ByteBuffer bb, int ix, Mask<Short, Shapes.S128Bit> m) {
1284             // @@@ This can result in out of bounds errors for unset mask lanes
1285             return zero().blend(fromByteBuffer(bb, ix), m);
1286         }
1287 
1288         @Override
1289         @ForceInline
1290         @SuppressWarnings("unchecked")
1291         public <F, T extends Shape> Short128Vector cast(Vector<F, T> o) {
1292             if (o.length() != LENGTH)
1293                 throw new IllegalArgumentException("Vector length this species length differ");
1294 
1295             return VectorIntrinsics.cast(
1296                 o.getClass(),
1297                 o.elementType(), LENGTH,
1298                 short.class, LENGTH,
1299                 o, this,
1300                 (s, v) -> s.castDefault(v)
1301             );
1302         }
1303 
1304         @SuppressWarnings("unchecked")
1305         @ForceInline
1306         private <F, T extends Shape> Short128Vector castDefault(Vector<F, T> v) {
1307             // Allocate array of required size
1308             int limit = length();
1309             short[] a = new short[limit];
1310 
1311             Class<?> vtype = v.species().elementType();
1312             if (vtype == byte.class) {
1313                 ByteVector<T> tv = (ByteVector<T>)v;
1314                 for (int i = 0; i < limit; i++) {
1315                     a[i] = (short) tv.get(i);
1316                 }
1317             } else if (vtype == short.class) {
1318                 ShortVector<T> tv = (ShortVector<T>)v;
1319                 for (int i = 0; i < limit; i++) {
1320                     a[i] = (short) tv.get(i);
1321                 }
1322             } else if (vtype == int.class) {
1323                 IntVector<T> tv = (IntVector<T>)v;
1324                 for (int i = 0; i < limit; i++) {
1325                     a[i] = (short) tv.get(i);
1326                 }
1327             } else if (vtype == long.class){
1328                 LongVector<T> tv = (LongVector<T>)v;
1329                 for (int i = 0; i < limit; i++) {
1330                     a[i] = (short) tv.get(i);
1331                 }
1332             } else if (vtype == float.class){
1333                 FloatVector<T> tv = (FloatVector<T>)v;
1334                 for (int i = 0; i < limit; i++) {
1335                     a[i] = (short) tv.get(i);
1336                 }
1337             } else if (vtype == double.class){
1338                 DoubleVector<T> tv = (DoubleVector<T>)v;
1339                 for (int i = 0; i < limit; i++) {
1340                     a[i] = (short) tv.get(i);
1341                 }
1342             } else {
1343                 throw new UnsupportedOperationException("Bad lane type for casting.");
1344             }
1345 
1346             return scalars(a);
1347         }
1348 
1349         @Override
1350         @ForceInline
1351         public <E, S extends Shape> Short128Mask cast(Mask<E, S> m) {
1352             if (m.length() != LENGTH)
1353                 throw new IllegalArgumentException("Mask length this species length differ");
1354             return new Short128Mask(m.toArray());
1355         }
1356 
1357         @Override
1358         @ForceInline
1359         public <E, S extends Shape> Short128Shuffle cast(Shuffle<E, S> s) {
1360             if (s.length() != LENGTH)
1361                 throw new IllegalArgumentException("Shuffle length this species length differ");
1362             return new Short128Shuffle(s.toArray());
1363         }
1364 
1365         @Override
1366         @ForceInline
1367         @SuppressWarnings("unchecked")
1368         public <F> Short128Vector rebracket(Vector<F, Shapes.S128Bit> o) {
1369             Objects.requireNonNull(o);
1370             if (o.elementType() == byte.class) {
1371                 Byte128Vector so = (Byte128Vector)o;
1372                 return VectorIntrinsics.reinterpret(
1373                     Byte128Vector.class,
1374                     byte.class, so.length(),
1375                     short.class, LENGTH,
1376                     so, this,
1377                     (s, v) -> (Short128Vector) s.reshape(v)
1378                 );
1379             } else if (o.elementType() == short.class) {
1380                 Short128Vector so = (Short128Vector)o;
1381                 return VectorIntrinsics.reinterpret(
1382                     Short128Vector.class,
1383                     short.class, so.length(),
1384                     short.class, LENGTH,
1385                     so, this,
1386                     (s, v) -> (Short128Vector) s.reshape(v)
1387                 );
1388             } else if (o.elementType() == int.class) {
1389                 Int128Vector so = (Int128Vector)o;
1390                 return VectorIntrinsics.reinterpret(
1391                     Int128Vector.class,
1392                     int.class, so.length(),
1393                     short.class, LENGTH,
1394                     so, this,
1395                     (s, v) -> (Short128Vector) s.reshape(v)
1396                 );
1397             } else if (o.elementType() == long.class) {
1398                 Long128Vector so = (Long128Vector)o;
1399                 return VectorIntrinsics.reinterpret(
1400                     Long128Vector.class,
1401                     long.class, so.length(),
1402                     short.class, LENGTH,
1403                     so, this,
1404                     (s, v) -> (Short128Vector) s.reshape(v)
1405                 );
1406             } else if (o.elementType() == float.class) {
1407                 Float128Vector so = (Float128Vector)o;
1408                 return VectorIntrinsics.reinterpret(
1409                     Float128Vector.class,
1410                     float.class, so.length(),
1411                     short.class, LENGTH,
1412                     so, this,
1413                     (s, v) -> (Short128Vector) s.reshape(v)
1414                 );
1415             } else if (o.elementType() == double.class) {
1416                 Double128Vector so = (Double128Vector)o;
1417                 return VectorIntrinsics.reinterpret(
1418                     Double128Vector.class,
1419                     double.class, so.length(),
1420                     short.class, LENGTH,
1421                     so, this,
1422                     (s, v) -> (Short128Vector) s.reshape(v)
1423                 );
1424             } else {
1425                 throw new InternalError("Unimplemented type");
1426             }
1427         }
1428 
1429         @Override
1430         @ForceInline
1431         @SuppressWarnings("unchecked")
1432         public <T extends Shape> Short128Vector resize(Vector<Short, T> o) {
1433             Objects.requireNonNull(o);
1434             if (o.bitSize() == 64) {
1435                 Short64Vector so = (Short64Vector)o;
1436                 return VectorIntrinsics.reinterpret(
1437                     Short64Vector.class,
1438                     short.class, so.length(),
1439                     short.class, LENGTH,
1440                     so, this,
1441                     (s, v) -> (Short128Vector) s.reshape(v)
1442                 );
1443             } else if (o.bitSize() == 128) {
1444                 Short128Vector so = (Short128Vector)o;
1445                 return VectorIntrinsics.reinterpret(
1446                     Short128Vector.class,
1447                     short.class, so.length(),
1448                     short.class, LENGTH,
1449                     so, this,
1450                     (s, v) -> (Short128Vector) s.reshape(v)
1451                 );
1452             } else if (o.bitSize() == 256) {
1453                 Short256Vector so = (Short256Vector)o;
1454                 return VectorIntrinsics.reinterpret(
1455                     Short256Vector.class,
1456                     short.class, so.length(),
1457                     short.class, LENGTH,
1458                     so, this,
1459                     (s, v) -> (Short128Vector) s.reshape(v)
1460                 );
1461             } else if (o.bitSize() == 512) {
1462                 Short512Vector so = (Short512Vector)o;
1463                 return VectorIntrinsics.reinterpret(
1464                     Short512Vector.class,
1465                     short.class, so.length(),
1466                     short.class, LENGTH,
1467                     so, this,
1468                     (s, v) -> (Short128Vector) s.reshape(v)
1469                 );
1470             } else {
1471                 throw new InternalError("Unimplemented size");
1472             }
1473         }
1474     }
1475 }