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