< prev index next >

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/ByteVector.java

Print this page




  94     interface FUnCon {
  95         void apply(int i, byte a);
  96     }
  97 
  98     abstract void forEach(FUnCon f);
  99 
 100     abstract void forEach(VectorMask<Byte> m, FUnCon f);
 101 
 102     // Static factories
 103 
 104     /**
 105      * Returns a vector where all lane elements are set to the default
 106      * primitive value.
 107      *
 108      * @param species species of desired vector
 109      * @return a zero vector of given species
 110      */
 111     @ForceInline
 112     @SuppressWarnings("unchecked")
 113     public static ByteVector zero(VectorSpecies<Byte> species) {
 114         return VectorIntrinsics.broadcastCoerced((Class<ByteVector>) species.boxType(), byte.class, species.length(),
 115                                                  0, species,
 116                                                  ((bits, s) -> ((ByteSpecies)s).op(i -> (byte)bits)));
 117     }
 118 
 119     /**
 120      * Loads a vector from a byte array starting at an offset.
 121      * <p>
 122      * Bytes are composed into primitive lane elements according to the
 123      * native byte order of the underlying platform
 124      * <p>
 125      * This method behaves as if it returns the result of calling the
 126      * byte buffer, offset, and mask accepting
 127      * {@link #fromByteBuffer(VectorSpecies, ByteBuffer, int, VectorMask) method} as follows:
 128      * <pre>{@code
 129      * return fromByteBuffer(species, ByteBuffer.wrap(a), offset, VectorMask.allTrue());
 130      * }</pre>
 131      *
 132      * @param species species of desired vector
 133      * @param a the byte array
 134      * @param offset the offset into the array
 135      * @return a vector loaded from a byte array
 136      * @throws IndexOutOfBoundsException if {@code i < 0} or
 137      * {@code offset > a.length - (species.length() * species.elementSize() / Byte.SIZE)}
 138      */
 139     @ForceInline
 140     @SuppressWarnings("unchecked")
 141     public static ByteVector fromByteArray(VectorSpecies<Byte> species, byte[] a, int offset) {
 142         Objects.requireNonNull(a);
 143         offset = VectorIntrinsics.checkIndex(offset, a.length, species.bitSize() / Byte.SIZE);
 144         return VectorIntrinsics.load((Class<ByteVector>) species.boxType(), byte.class, species.length(),
 145                                      a, ((long) offset) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
 146                                      a, offset, species,
 147                                      (c, idx, s) -> {
 148                                          ByteBuffer bbc = ByteBuffer.wrap(c, idx, a.length - idx).order(ByteOrder.nativeOrder());
 149                                          ByteBuffer tb = bbc;
 150                                          return ((ByteSpecies)s).op(i -> tb.get());
 151                                      });
 152     }
 153 
 154     /**
 155      * Loads a vector from a byte array starting at an offset and using a
 156      * mask.
 157      * <p>
 158      * Bytes are composed into primitive lane elements according to the
 159      * native byte order of the underlying platform.
 160      * <p>
 161      * This method behaves as if it returns the result of calling the
 162      * byte buffer, offset, and mask accepting
 163      * {@link #fromByteBuffer(VectorSpecies, ByteBuffer, int, VectorMask) method} as follows:
 164      * <pre>{@code


 182 
 183     /**
 184      * Loads a vector from an array starting at offset.
 185      * <p>
 186      * For each vector lane, where {@code N} is the vector lane index, the
 187      * array element at index {@code offset + N} is placed into the
 188      * resulting vector at lane index {@code N}.
 189      *
 190      * @param species species of desired vector
 191      * @param a the array
 192      * @param offset the offset into the array
 193      * @return the vector loaded from an array
 194      * @throws IndexOutOfBoundsException if {@code offset < 0}, or
 195      * {@code offset > a.length - species.length()}
 196      */
 197     @ForceInline
 198     @SuppressWarnings("unchecked")
 199     public static ByteVector fromArray(VectorSpecies<Byte> species, byte[] a, int offset){
 200         Objects.requireNonNull(a);
 201         offset = VectorIntrinsics.checkIndex(offset, a.length, species.length());
 202         return VectorIntrinsics.load((Class<ByteVector>) species.boxType(), byte.class, species.length(),
 203                                      a, (((long) offset) << ARRAY_SHIFT) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
 204                                      a, offset, species,
 205                                      (c, idx, s) -> ((ByteSpecies)s).op(n -> c[idx + n]));
 206     }
 207 
 208 
 209     /**
 210      * Loads a vector from an array starting at offset and using a mask.
 211      * <p>
 212      * For each vector lane, where {@code N} is the vector lane index,
 213      * if the mask lane at index {@code N} is set then the array element at
 214      * index {@code offset + N} is placed into the resulting vector at lane index
 215      * {@code N}, otherwise the default element value is placed into the
 216      * resulting vector at lane index {@code N}.
 217      *
 218      * @param species species of desired vector
 219      * @param a the array
 220      * @param offset the offset into the array
 221      * @param m the mask
 222      * @return the vector loaded from an array


 295      *   return fromByteBuffer(b, offset, VectorMask.allTrue())
 296      * }</pre>
 297      *
 298      * @param species species of desired vector
 299      * @param bb the byte buffer
 300      * @param offset the offset into the byte buffer
 301      * @return a vector loaded from a byte buffer
 302      * @throws IndexOutOfBoundsException if the offset is {@code < 0},
 303      * or {@code > b.limit()},
 304      * or if there are fewer than
 305      * {@code species.length() * species.elementSize() / Byte.SIZE} bytes
 306      * remaining in the byte buffer from the given offset
 307      */
 308     @ForceInline
 309     @SuppressWarnings("unchecked")
 310     public static ByteVector fromByteBuffer(VectorSpecies<Byte> species, ByteBuffer bb, int offset) {
 311         if (bb.order() != ByteOrder.nativeOrder()) {
 312             throw new IllegalArgumentException();
 313         }
 314         offset = VectorIntrinsics.checkIndex(offset, bb.limit(), species.bitSize() / Byte.SIZE);
 315         return VectorIntrinsics.load((Class<ByteVector>) species.boxType(), byte.class, species.length(),
 316                                      U.getReference(bb, BYTE_BUFFER_HB), U.getLong(bb, BUFFER_ADDRESS) + offset,
 317                                      bb, offset, species,
 318                                      (c, idx, s) -> {
 319                                          ByteBuffer bbc = c.duplicate().position(idx).order(ByteOrder.nativeOrder());
 320                                          ByteBuffer tb = bbc;
 321                                          return ((ByteSpecies)s).op(i -> tb.get());
 322                                      });
 323     }
 324 
 325     /**
 326      * Loads a vector from a {@link ByteBuffer byte buffer} starting at an
 327      * offset into the byte buffer and using a mask.
 328      * <p>
 329      * This method behaves as if the byte buffer is viewed as a primitive
 330      * {@link java.nio.Buffer buffer} for the primitive element type,
 331      * according to the native byte order of the underlying platform, and
 332      * the returned vector is loaded with a mask from a primitive array
 333      * obtained from the primitive buffer.
 334      * The following pseudocode expresses the behaviour, where
 335      * {@code EBuffer} is the primitive buffer type, {@code e} is the


 359      * {@code offset >= b.limit() - (N * species.elementSize() / Byte.SIZE)}
 360      */
 361     @ForceInline
 362     public static ByteVector fromByteBuffer(VectorSpecies<Byte> species, ByteBuffer bb, int offset, VectorMask<Byte> m) {
 363         return zero(species).blend(fromByteBuffer(species, bb, offset), m);
 364     }
 365 
 366     /**
 367      * Returns a vector where all lane elements are set to the primitive
 368      * value {@code e}.
 369      *
 370      * @param species species of the desired vector
 371      * @param e the value
 372      * @return a vector of vector where all lane elements are set to
 373      * the primitive value {@code e}
 374      */
 375     @ForceInline
 376     @SuppressWarnings("unchecked")
 377     public static ByteVector broadcast(VectorSpecies<Byte> species, byte e) {
 378         return VectorIntrinsics.broadcastCoerced(
 379             (Class<ByteVector>) species.boxType(), byte.class, species.length(),
 380             e, species,
 381             ((bits, sp) -> ((ByteSpecies)sp).op(i -> (byte)bits)));
 382     }
 383 
 384     /**
 385      * Returns a vector where each lane element is set to given
 386      * primitive values.
 387      * <p>
 388      * For each vector lane, where {@code N} is the vector lane index, the
 389      * the primitive value at index {@code N} is placed into the resulting
 390      * vector at lane index {@code N}.
 391      *
 392      * @param species species of the desired vector
 393      * @param es the given primitive values
 394      * @return a vector where each lane element is set to given primitive
 395      * values
 396      * @throws IndexOutOfBoundsException if {@code es.length < species.length()}
 397      */
 398     @ForceInline
 399     @SuppressWarnings("unchecked")
 400     public static ByteVector scalars(VectorSpecies<Byte> species, byte... es) {
 401         Objects.requireNonNull(es);
 402         int ix = VectorIntrinsics.checkIndex(0, es.length, species.length());
 403         return VectorIntrinsics.load((Class<ByteVector>) species.boxType(), byte.class, species.length(),
 404                                      es, Unsafe.ARRAY_BYTE_BASE_OFFSET,
 405                                      es, ix, species,
 406                                      (c, idx, sp) -> ((ByteSpecies)sp).op(n -> c[idx + n]));
 407     }
 408 
 409     /**
 410      * Returns a vector where the first lane element is set to the primtive
 411      * value {@code e}, all other lane elements are set to the default
 412      * value.
 413      *
 414      * @param species species of the desired vector
 415      * @param e the value
 416      * @return a vector where the first lane element is set to the primitive
 417      * value {@code e}
 418      */
 419     @ForceInline
 420     public static final ByteVector single(VectorSpecies<Byte> species, byte e) {
 421         return zero(species).with(0, e);
 422     }
 423 


 761     @Override
 762     public abstract ByteVector rearrange(Vector<Byte> v,
 763                                                       VectorShuffle<Byte> s, VectorMask<Byte> m);
 764 
 765     /**
 766      * {@inheritDoc}
 767      */
 768     @Override
 769     public abstract ByteVector rearrange(VectorShuffle<Byte> m);
 770 
 771     /**
 772      * {@inheritDoc}
 773      */
 774     @Override
 775     public abstract ByteVector reshape(VectorSpecies<Byte> s);
 776 
 777     /**
 778      * {@inheritDoc}
 779      */
 780     @Override
 781     public abstract ByteVector rotateEL(int i);
 782 
 783     /**
 784      * {@inheritDoc}
 785      */
 786     @Override
 787     public abstract ByteVector rotateER(int i);
 788 
 789     /**
 790      * {@inheritDoc}
 791      */
 792     @Override
 793     public abstract ByteVector shiftEL(int i);
 794 
 795     /**
 796      * {@inheritDoc}
 797      */
 798     @Override
 799     public abstract ByteVector shiftER(int i);
 800 
 801 
 802 
 803     /**
 804      * Bitwise ANDs this vector with an input vector.
 805      * <p>
 806      * This is a lane-wise binary operation which applies the primitive bitwise AND
 807      * operation ({@code &}) to each lane.
 808      *
 809      * @param v the input vector
 810      * @return the bitwise AND of this vector with the input vector
 811      */
 812     public abstract ByteVector and(Vector<Byte> v);
 813 
 814     /**
 815      * Bitwise ANDs this vector with the broadcast of an input scalar.
 816      * <p>
 817      * This is a lane-wise binary operation which applies the primitive bitwise AND
 818      * operation ({@code &}) to each lane.
 819      *


 959      * @return the bitwise NOT of this vector
 960      */
 961     public abstract ByteVector not();
 962 
 963     /**
 964      * Bitwise NOTs this vector, selecting lane elements controlled by a mask.
 965      * <p>
 966      * This is a lane-wise unary operation which applies the primitive bitwise NOT
 967      * operation ({@code ~}) to each lane.
 968      *
 969      * @param m the mask controlling lane selection
 970      * @return the bitwise NOT of this vector
 971      */
 972     public abstract ByteVector not(VectorMask<Byte> m);
 973 
 974     /**
 975      * Logically left shifts this vector by the broadcast of an input scalar.
 976      * <p>
 977      * This is a lane-wise binary operation which applies the primitive logical left shift
 978      * operation ({@code <<}) to each lane to left shift the
 979      * element by shift value as specified by the input scalar. Only the 3
 980      * lowest-order bits of shift value are used. It is as if the shift value
 981      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
 982      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
 983      *
 984      * @param s the input scalar; the number of the bits to left shift
 985      * @return the result of logically left shifting left this vector by the
 986      * broadcast of an input scalar
 987      */
 988     public abstract ByteVector shiftL(int s);
 989 
 990     /**
 991      * Logically left shifts this vector by the broadcast of an input scalar,
 992      * selecting lane elements controlled by a mask.
 993      * <p>
 994      * This is a lane-wise binary operation which applies the primitive logical left shift
 995      * operation ({@code <<}) to each lane to left shift the
 996      * element by shift value as specified by the input scalar. Only the 3
 997      * lowest-order bits of shift value are used. It is as if the shift value
 998      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
 999      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1000      *
1001      * @param s the input scalar; the number of the bits to left shift
1002      * @param m the mask controlling lane selection
1003      * @return the result of logically left shifting left this vector by the
1004      * broadcast of an input scalar
1005      */
1006     public abstract ByteVector shiftL(int s, VectorMask<Byte> m);
1007 



































1008 
1009     // logical, or unsigned, shift right
1010 
1011      /**
1012      * Logically right shifts (or unsigned right shifts) this vector by the
1013      * broadcast of an input scalar.
1014      * <p>
1015      * This is a lane-wise binary operation which applies the primitive logical right shift
1016      * operation ({@code >>>}) to each lane to logically right shift the
1017      * element by shift value as specified by the input scalar. Only the 3
1018      * lowest-order bits of shift value are used. It is as if the shift value
1019      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1020      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1021      *
1022      * @param s the input scalar; the number of the bits to right shift
1023      * @return the result of logically right shifting this vector by the
1024      * broadcast of an input scalar
1025      */
1026     public abstract ByteVector shiftR(int s);
1027 
1028      /**
1029      * Logically right shifts (or unsigned right shifts) this vector by the
1030      * broadcast of an input scalar, selecting lane elements controlled by a
1031      * mask.
1032      * <p>
1033      * This is a lane-wise binary operation which applies the primitive logical right shift
1034      * operation ({@code >>}) to each lane to logically right shift the
1035      * element by shift value as specified by the input scalar. Only the 3
1036      * lowest-order bits of shift value are used. It is as if the shift value
1037      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1038      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1039      *
1040      * @param s the input scalar; the number of the bits to right shift
1041      * @param m the mask controlling lane selection
1042      * @return the result of logically right shifting this vector by the
1043      * broadcast of an input scalar
1044      */
1045     public abstract ByteVector shiftR(int s, VectorMask<Byte> m);

















1046 



















1047 
1048     /**
1049      * Arithmetically right shifts (or signed right shifts) this vector by the
1050      * broadcast of an input scalar.
1051      * <p>
1052      * This is a lane-wise binary operation which applies the primitive arithmetic right
1053      * shift operation ({@code >>}) to each lane to arithmetically
1054      * right shift the element by shift value as specified by the input scalar.
1055      * Only the 3 lowest-order bits of shift value are used. It is as if the shift
1056      * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1057      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1058      *
1059      * @param s the input scalar; the number of the bits to right shift
1060      * @return the result of arithmetically right shifting this vector by the
1061      * broadcast of an input scalar
1062      */
1063     public abstract ByteVector aShiftR(int s);
1064 
1065     /**
1066      * Arithmetically right shifts (or signed right shifts) this vector by the
1067      * broadcast of an input scalar, selecting lane elements controlled by a
1068      * mask.
1069      * <p>
1070      * This is a lane-wise binary operation which applies the primitive arithmetic right
1071      * shift operation ({@code >>}) to each lane to arithmetically
1072      * right shift the element by shift value as specified by the input scalar.
1073      * Only the 3 lowest-order bits of shift value are used. It is as if the shift
1074      * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1075      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1076      *
1077      * @param s the input scalar; the number of the bits to right shift
1078      * @param m the mask controlling lane selection
1079      * @return the result of arithmetically right shifting this vector by the
1080      * broadcast of an input scalar
1081      */
1082     public abstract ByteVector aShiftR(int s, VectorMask<Byte> m);
1083 
















































































































1084 
1085     /**
1086      * {@inheritDoc}
1087      */
1088     @Override
1089     public abstract void intoByteArray(byte[] a, int ix);
1090 
1091     /**
1092      * {@inheritDoc}
1093      */
1094     @Override
1095     public abstract void intoByteArray(byte[] a, int ix, VectorMask<Byte> m);
1096 
1097     /**
1098      * {@inheritDoc}
1099      */
1100     @Override
1101     public abstract void intoByteBuffer(ByteBuffer bb, int ix);
1102 
1103     /**


1408      * {@code < 0} or {@code >= a.length}
1409      */
1410     public void intoArray(byte[] a, int a_offset, VectorMask<Byte> m, int[] indexMap, int i_offset) {
1411         forEach(m, (n, e) -> a[a_offset + indexMap[i_offset + n]] = e);
1412     }
1413     // Species
1414 
1415     /**
1416      * {@inheritDoc}
1417      */
1418     @Override
1419     public abstract VectorSpecies<Byte> species();
1420 
1421     /**
1422      * Class representing {@link ByteVector}'s of the same {@link VectorShape VectorShape}.
1423      */
1424     static final class ByteSpecies extends AbstractSpecies<Byte> {
1425         final Function<byte[], ByteVector> vectorFactory;
1426 
1427         private ByteSpecies(VectorShape shape,
1428                           Class<?> boxType,
1429                           Class<?> maskType,
1430                           Function<byte[], ByteVector> vectorFactory,
1431                           Function<boolean[], VectorMask<Byte>> maskFactory,
1432                           Function<IntUnaryOperator, VectorShuffle<Byte>> shuffleFromArrayFactory,
1433                           fShuffleFromArray<Byte> shuffleFromOpFactory) {
1434             super(shape, byte.class, Byte.SIZE, boxType, maskType, maskFactory,
1435                   shuffleFromArrayFactory, shuffleFromOpFactory);
1436             this.vectorFactory = vectorFactory;
1437         }
1438 
1439         interface FOp {
1440             byte apply(int i);
1441         }
1442 
1443         ByteVector op(FOp f) {
1444             byte[] res = new byte[length()];
1445             for (int i = 0; i < length(); i++) {
1446                 res[i] = f.apply(i);
1447             }
1448             return vectorFactory.apply(res);
1449         }
1450 
1451         ByteVector op(VectorMask<Byte> o, FOp f) {
1452             byte[] res = new byte[length()];
1453             boolean[] mbits = ((AbstractMask<Byte>)o).getBits();
1454             for (int i = 0; i < length(); i++) {




  94     interface FUnCon {
  95         void apply(int i, byte a);
  96     }
  97 
  98     abstract void forEach(FUnCon f);
  99 
 100     abstract void forEach(VectorMask<Byte> m, FUnCon f);
 101 
 102     // Static factories
 103 
 104     /**
 105      * Returns a vector where all lane elements are set to the default
 106      * primitive value.
 107      *
 108      * @param species species of desired vector
 109      * @return a zero vector of given species
 110      */
 111     @ForceInline
 112     @SuppressWarnings("unchecked")
 113     public static ByteVector zero(VectorSpecies<Byte> species) {
 114         return VectorIntrinsics.broadcastCoerced((Class<ByteVector>) species.vectorType(), byte.class, species.length(),
 115                                                  0, species,
 116                                                  ((bits, s) -> ((ByteSpecies)s).op(i -> (byte)bits)));
 117     }
 118 
 119     /**
 120      * Loads a vector from a byte array starting at an offset.
 121      * <p>
 122      * Bytes are composed into primitive lane elements according to the
 123      * native byte order of the underlying platform
 124      * <p>
 125      * This method behaves as if it returns the result of calling the
 126      * byte buffer, offset, and mask accepting
 127      * {@link #fromByteBuffer(VectorSpecies, ByteBuffer, int, VectorMask) method} as follows:
 128      * <pre>{@code
 129      * return fromByteBuffer(species, ByteBuffer.wrap(a), offset, VectorMask.allTrue());
 130      * }</pre>
 131      *
 132      * @param species species of desired vector
 133      * @param a the byte array
 134      * @param offset the offset into the array
 135      * @return a vector loaded from a byte array
 136      * @throws IndexOutOfBoundsException if {@code i < 0} or
 137      * {@code offset > a.length - (species.length() * species.elementSize() / Byte.SIZE)}
 138      */
 139     @ForceInline
 140     @SuppressWarnings("unchecked")
 141     public static ByteVector fromByteArray(VectorSpecies<Byte> species, byte[] a, int offset) {
 142         Objects.requireNonNull(a);
 143         offset = VectorIntrinsics.checkIndex(offset, a.length, species.bitSize() / Byte.SIZE);
 144         return VectorIntrinsics.load((Class<ByteVector>) species.vectorType(), byte.class, species.length(),
 145                                      a, ((long) offset) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
 146                                      a, offset, species,
 147                                      (c, idx, s) -> {
 148                                          ByteBuffer bbc = ByteBuffer.wrap(c, idx, a.length - idx).order(ByteOrder.nativeOrder());
 149                                          ByteBuffer tb = bbc;
 150                                          return ((ByteSpecies)s).op(i -> tb.get());
 151                                      });
 152     }
 153 
 154     /**
 155      * Loads a vector from a byte array starting at an offset and using a
 156      * mask.
 157      * <p>
 158      * Bytes are composed into primitive lane elements according to the
 159      * native byte order of the underlying platform.
 160      * <p>
 161      * This method behaves as if it returns the result of calling the
 162      * byte buffer, offset, and mask accepting
 163      * {@link #fromByteBuffer(VectorSpecies, ByteBuffer, int, VectorMask) method} as follows:
 164      * <pre>{@code


 182 
 183     /**
 184      * Loads a vector from an array starting at offset.
 185      * <p>
 186      * For each vector lane, where {@code N} is the vector lane index, the
 187      * array element at index {@code offset + N} is placed into the
 188      * resulting vector at lane index {@code N}.
 189      *
 190      * @param species species of desired vector
 191      * @param a the array
 192      * @param offset the offset into the array
 193      * @return the vector loaded from an array
 194      * @throws IndexOutOfBoundsException if {@code offset < 0}, or
 195      * {@code offset > a.length - species.length()}
 196      */
 197     @ForceInline
 198     @SuppressWarnings("unchecked")
 199     public static ByteVector fromArray(VectorSpecies<Byte> species, byte[] a, int offset){
 200         Objects.requireNonNull(a);
 201         offset = VectorIntrinsics.checkIndex(offset, a.length, species.length());
 202         return VectorIntrinsics.load((Class<ByteVector>) species.vectorType(), byte.class, species.length(),
 203                                      a, (((long) offset) << ARRAY_SHIFT) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
 204                                      a, offset, species,
 205                                      (c, idx, s) -> ((ByteSpecies)s).op(n -> c[idx + n]));
 206     }
 207 
 208 
 209     /**
 210      * Loads a vector from an array starting at offset and using a mask.
 211      * <p>
 212      * For each vector lane, where {@code N} is the vector lane index,
 213      * if the mask lane at index {@code N} is set then the array element at
 214      * index {@code offset + N} is placed into the resulting vector at lane index
 215      * {@code N}, otherwise the default element value is placed into the
 216      * resulting vector at lane index {@code N}.
 217      *
 218      * @param species species of desired vector
 219      * @param a the array
 220      * @param offset the offset into the array
 221      * @param m the mask
 222      * @return the vector loaded from an array


 295      *   return fromByteBuffer(b, offset, VectorMask.allTrue())
 296      * }</pre>
 297      *
 298      * @param species species of desired vector
 299      * @param bb the byte buffer
 300      * @param offset the offset into the byte buffer
 301      * @return a vector loaded from a byte buffer
 302      * @throws IndexOutOfBoundsException if the offset is {@code < 0},
 303      * or {@code > b.limit()},
 304      * or if there are fewer than
 305      * {@code species.length() * species.elementSize() / Byte.SIZE} bytes
 306      * remaining in the byte buffer from the given offset
 307      */
 308     @ForceInline
 309     @SuppressWarnings("unchecked")
 310     public static ByteVector fromByteBuffer(VectorSpecies<Byte> species, ByteBuffer bb, int offset) {
 311         if (bb.order() != ByteOrder.nativeOrder()) {
 312             throw new IllegalArgumentException();
 313         }
 314         offset = VectorIntrinsics.checkIndex(offset, bb.limit(), species.bitSize() / Byte.SIZE);
 315         return VectorIntrinsics.load((Class<ByteVector>) species.vectorType(), byte.class, species.length(),
 316                                      U.getReference(bb, BYTE_BUFFER_HB), U.getLong(bb, BUFFER_ADDRESS) + offset,
 317                                      bb, offset, species,
 318                                      (c, idx, s) -> {
 319                                          ByteBuffer bbc = c.duplicate().position(idx).order(ByteOrder.nativeOrder());
 320                                          ByteBuffer tb = bbc;
 321                                          return ((ByteSpecies)s).op(i -> tb.get());
 322                                      });
 323     }
 324 
 325     /**
 326      * Loads a vector from a {@link ByteBuffer byte buffer} starting at an
 327      * offset into the byte buffer and using a mask.
 328      * <p>
 329      * This method behaves as if the byte buffer is viewed as a primitive
 330      * {@link java.nio.Buffer buffer} for the primitive element type,
 331      * according to the native byte order of the underlying platform, and
 332      * the returned vector is loaded with a mask from a primitive array
 333      * obtained from the primitive buffer.
 334      * The following pseudocode expresses the behaviour, where
 335      * {@code EBuffer} is the primitive buffer type, {@code e} is the


 359      * {@code offset >= b.limit() - (N * species.elementSize() / Byte.SIZE)}
 360      */
 361     @ForceInline
 362     public static ByteVector fromByteBuffer(VectorSpecies<Byte> species, ByteBuffer bb, int offset, VectorMask<Byte> m) {
 363         return zero(species).blend(fromByteBuffer(species, bb, offset), m);
 364     }
 365 
 366     /**
 367      * Returns a vector where all lane elements are set to the primitive
 368      * value {@code e}.
 369      *
 370      * @param species species of the desired vector
 371      * @param e the value
 372      * @return a vector of vector where all lane elements are set to
 373      * the primitive value {@code e}
 374      */
 375     @ForceInline
 376     @SuppressWarnings("unchecked")
 377     public static ByteVector broadcast(VectorSpecies<Byte> species, byte e) {
 378         return VectorIntrinsics.broadcastCoerced(
 379             (Class<ByteVector>) species.vectorType(), byte.class, species.length(),
 380             e, species,
 381             ((bits, sp) -> ((ByteSpecies)sp).op(i -> (byte)bits)));
 382     }
 383 
 384     /**
 385      * Returns a vector where each lane element is set to given
 386      * primitive values.
 387      * <p>
 388      * For each vector lane, where {@code N} is the vector lane index, the
 389      * the primitive value at index {@code N} is placed into the resulting
 390      * vector at lane index {@code N}.
 391      *
 392      * @param species species of the desired vector
 393      * @param es the given primitive values
 394      * @return a vector where each lane element is set to given primitive
 395      * values
 396      * @throws IndexOutOfBoundsException if {@code es.length < species.length()}
 397      */
 398     @ForceInline
 399     @SuppressWarnings("unchecked")
 400     public static ByteVector scalars(VectorSpecies<Byte> species, byte... es) {
 401         Objects.requireNonNull(es);
 402         int ix = VectorIntrinsics.checkIndex(0, es.length, species.length());
 403         return VectorIntrinsics.load((Class<ByteVector>) species.vectorType(), byte.class, species.length(),
 404                                      es, Unsafe.ARRAY_BYTE_BASE_OFFSET,
 405                                      es, ix, species,
 406                                      (c, idx, sp) -> ((ByteSpecies)sp).op(n -> c[idx + n]));
 407     }
 408 
 409     /**
 410      * Returns a vector where the first lane element is set to the primtive
 411      * value {@code e}, all other lane elements are set to the default
 412      * value.
 413      *
 414      * @param species species of the desired vector
 415      * @param e the value
 416      * @return a vector where the first lane element is set to the primitive
 417      * value {@code e}
 418      */
 419     @ForceInline
 420     public static final ByteVector single(VectorSpecies<Byte> species, byte e) {
 421         return zero(species).with(0, e);
 422     }
 423 


 761     @Override
 762     public abstract ByteVector rearrange(Vector<Byte> v,
 763                                                       VectorShuffle<Byte> s, VectorMask<Byte> m);
 764 
 765     /**
 766      * {@inheritDoc}
 767      */
 768     @Override
 769     public abstract ByteVector rearrange(VectorShuffle<Byte> m);
 770 
 771     /**
 772      * {@inheritDoc}
 773      */
 774     @Override
 775     public abstract ByteVector reshape(VectorSpecies<Byte> s);
 776 
 777     /**
 778      * {@inheritDoc}
 779      */
 780     @Override
 781     public abstract ByteVector rotateLanesLeft(int i);
 782 
 783     /**
 784      * {@inheritDoc}
 785      */
 786     @Override
 787     public abstract ByteVector rotateLanesRight(int i);
 788 
 789     /**
 790      * {@inheritDoc}
 791      */
 792     @Override
 793     public abstract ByteVector shiftLanesLeft(int i);
 794 
 795     /**
 796      * {@inheritDoc}
 797      */
 798     @Override
 799     public abstract ByteVector shiftLanesRight(int i);
 800 
 801 
 802 
 803     /**
 804      * Bitwise ANDs this vector with an input vector.
 805      * <p>
 806      * This is a lane-wise binary operation which applies the primitive bitwise AND
 807      * operation ({@code &}) to each lane.
 808      *
 809      * @param v the input vector
 810      * @return the bitwise AND of this vector with the input vector
 811      */
 812     public abstract ByteVector and(Vector<Byte> v);
 813 
 814     /**
 815      * Bitwise ANDs this vector with the broadcast of an input scalar.
 816      * <p>
 817      * This is a lane-wise binary operation which applies the primitive bitwise AND
 818      * operation ({@code &}) to each lane.
 819      *


 959      * @return the bitwise NOT of this vector
 960      */
 961     public abstract ByteVector not();
 962 
 963     /**
 964      * Bitwise NOTs this vector, selecting lane elements controlled by a mask.
 965      * <p>
 966      * This is a lane-wise unary operation which applies the primitive bitwise NOT
 967      * operation ({@code ~}) to each lane.
 968      *
 969      * @param m the mask controlling lane selection
 970      * @return the bitwise NOT of this vector
 971      */
 972     public abstract ByteVector not(VectorMask<Byte> m);
 973 
 974     /**
 975      * Logically left shifts this vector by the broadcast of an input scalar.
 976      * <p>
 977      * This is a lane-wise binary operation which applies the primitive logical left shift
 978      * operation ({@code <<}) to each lane to left shift the
 979      * element by shift value as specified by the input scalar.
 980      * Only the 3 lowest-order bits of shift value are used. It is as if the shift value
 981      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
 982      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
 983      *
 984      * @param s the input scalar; the number of the bits to left shift
 985      * @return the result of logically left shifting left this vector by the
 986      * broadcast of an input scalar
 987      */
 988     public abstract ByteVector shiftLeft(int s);
 989 
 990     /**
 991      * Logically left shifts this vector by the broadcast of an input scalar,
 992      * selecting lane elements controlled by a mask.
 993      * <p>
 994      * This is a lane-wise binary operation which applies the primitive logical left shift
 995      * operation ({@code <<}) to each lane to left shift the
 996      * element by shift value as specified by the input scalar.
 997      * Only the 3 lowest-order bits of shift value are used. It is as if the shift value
 998      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
 999      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1000      *
1001      * @param s the input scalar; the number of the bits to left shift
1002      * @param m the mask controlling lane selection
1003      * @return the result of logically left shifting left this vector by the
1004      * broadcast of an input scalar
1005      */
1006     public abstract ByteVector shiftLeft(int s, VectorMask<Byte> m);
1007 
1008     /**
1009      * Logically left shifts this vector by an input vector.
1010      * <p>
1011      * This is a lane-wise binary operation which applies the primitive logical left shift
1012      * operation ({@code <<}) to each lane. For each lane of this vector, the
1013      * shift value is the corresponding lane of input vector.
1014      * Only the 3 lowest-order bits of shift value are used. It is as if the shift value
1015      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1016      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1017      *
1018      * @param v the input vector
1019      * @return the result of logically left shifting this vector by the input
1020      * vector
1021      */
1022     public abstract ByteVector shiftLeft(Vector<Byte> v);
1023 
1024     /**
1025      * Logically left shifts this vector by an input vector, selecting lane
1026      * elements controlled by a mask.
1027      * <p>
1028      * This is a lane-wise binary operation which applies the primitive logical left shift
1029      * operation ({@code <<}) to each lane. For each lane of this vector, the
1030      * shift value is the corresponding lane of input vector.
1031      * Only the 3 lowest-order bits of shift value are used. It is as if the shift value
1032      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1033      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1034      *
1035      * @param v the input vector
1036      * @param m the mask controlling lane selection
1037      * @return the result of logically left shifting this vector by the input
1038      * vector
1039      */
1040     public ByteVector shiftLeft(Vector<Byte> v, VectorMask<Byte> m) {
1041         return blend(shiftLeft(v), m);
1042     }
1043 
1044     // logical, or unsigned, shift right
1045 
1046      /**
1047      * Logically right shifts (or unsigned right shifts) this vector by the
1048      * broadcast of an input scalar.
1049      * <p>
1050      * This is a lane-wise binary operation which applies the primitive logical right shift
1051      * operation ({@code >>>}) to each lane to logically right shift the
1052      * element by shift value as specified by the input scalar.
1053      * Only the 3 lowest-order bits of shift value are used. It is as if the shift value
1054      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1055      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1056      *
1057      * @param s the input scalar; the number of the bits to right shift
1058      * @return the result of logically right shifting this vector by the
1059      * broadcast of an input scalar
1060      */
1061     public abstract ByteVector shiftRight(int s);
1062 
1063      /**
1064      * Logically right shifts (or unsigned right shifts) this vector by the
1065      * broadcast of an input scalar, selecting lane elements controlled by a
1066      * mask.
1067      * <p>
1068      * This is a lane-wise binary operation which applies the primitive logical right shift
1069      * operation ({@code >>}) to each lane to logically right shift the
1070      * element by shift value as specified by the input scalar.
1071      * Only the 3 lowest-order bits of shift value are used. It is as if the shift value
1072      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1073      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1074      *
1075      * @param s the input scalar; the number of the bits to right shift
1076      * @param m the mask controlling lane selection
1077      * @return the result of logically right shifting this vector by the
1078      * broadcast of an input scalar
1079      */
1080     public abstract ByteVector shiftRight(int s, VectorMask<Byte> m);
1081 
1082     /**
1083      * Logically right shifts (or unsigned right shifts) this vector by an
1084      * input vector.
1085      * <p>
1086      * This is a lane-wise binary operation which applies the primitive logical right shift
1087      * operation ({@code >>>}) to each lane. For each lane of this vector, the
1088      * shift value is the corresponding lane of input vector.
1089      * Only the 3 lowest-order bits of shift value are used. It is as if the shift value
1090      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1091      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1092      *
1093      * @param v the input vector
1094      * @return the result of logically right shifting this vector by the
1095      * input vector
1096      */
1097     public abstract ByteVector shiftRight(Vector<Byte> v);
1098 
1099     /**
1100      * Logically right shifts (or unsigned right shifts) this vector by an
1101      * input vector, selecting lane elements controlled by a mask.
1102      * <p>
1103      * This is a lane-wise binary operation which applies the primitive logical right shift
1104      * operation ({@code >>>}) to each lane. For each lane of this vector, the
1105      * shift value is the corresponding lane of input vector.
1106      * Only the 3 lowest-order bits of shift value are used. It is as if the shift value
1107      * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1108      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1109      *
1110      * @param v the input vector
1111      * @param m the mask controlling lane selection
1112      * @return the result of logically right shifting this vector by the
1113      * input vector
1114      */
1115     public ByteVector shiftRight(Vector<Byte> v, VectorMask<Byte> m) {
1116         return blend(shiftRight(v), m);
1117     }
1118 
1119     /**
1120      * Arithmetically right shifts (or signed right shifts) this vector by the
1121      * broadcast of an input scalar.
1122      * <p>
1123      * This is a lane-wise binary operation which applies the primitive arithmetic right
1124      * shift operation ({@code >>}) to each lane to arithmetically
1125      * right shift the element by shift value as specified by the input scalar.
1126      * Only the 3 lowest-order bits of shift value are used. It is as if the shift
1127      * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1128      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1129      *
1130      * @param s the input scalar; the number of the bits to right shift
1131      * @return the result of arithmetically right shifting this vector by the
1132      * broadcast of an input scalar
1133      */
1134     public abstract ByteVector shiftArithmeticRight(int s);
1135 
1136     /**
1137      * Arithmetically right shifts (or signed right shifts) this vector by the
1138      * broadcast of an input scalar, selecting lane elements controlled by a
1139      * mask.
1140      * <p>
1141      * This is a lane-wise binary operation which applies the primitive arithmetic right
1142      * shift operation ({@code >>}) to each lane to arithmetically
1143      * right shift the element by shift value as specified by the input scalar.
1144      * Only the 3 lowest-order bits of shift value are used. It is as if the shift
1145      * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1146      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1147      *
1148      * @param s the input scalar; the number of the bits to right shift
1149      * @param m the mask controlling lane selection
1150      * @return the result of arithmetically right shifting this vector by the
1151      * broadcast of an input scalar
1152      */
1153     public abstract ByteVector shiftArithmeticRight(int s, VectorMask<Byte> m);
1154 
1155     /**
1156      * Arithmetically right shifts (or signed right shifts) this vector by an
1157      * input vector.
1158      * <p>
1159      * This is a lane-wise binary operation which applies the primitive arithmetic right
1160      * shift operation ({@code >>}) to each lane. For each lane of this vector, the
1161      * shift value is the corresponding lane of input vector.
1162      * Only the 3 lowest-order bits of shift value are used. It is as if the shift
1163      * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1164      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1165      *
1166      * @param v the input vector
1167      * @return the result of arithmetically right shifting this vector by the
1168      * input vector
1169      */
1170     public abstract ByteVector shiftArithmeticRight(Vector<Byte> v);
1171 
1172     /**
1173      * Arithmetically right shifts (or signed right shifts) this vector by an
1174      * input vector, selecting lane elements controlled by a mask.
1175      * <p>
1176      * This is a lane-wise binary operation which applies the primitive arithmetic right
1177      * shift operation ({@code >>}) to each lane. For each lane of this vector, the
1178      * shift value is the corresponding lane of input vector.
1179      * Only the 3 lowest-order bits of shift value are used. It is as if the shift
1180      * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0x7.
1181      * The shift distance actually used is therefore always in the range 0 to 7, inclusive.
1182      *
1183      * @param v the input vector
1184      * @param m the mask controlling lane selection
1185      * @return the result of arithmetically right shifting this vector by the
1186      * input vector
1187      */
1188     public ByteVector shiftArithmeticRight(Vector<Byte> v, VectorMask<Byte> m) {
1189         return blend(shiftArithmeticRight(v), m);
1190     }
1191 
1192     /**
1193      * Rotates left this vector by the broadcast of an input scalar.
1194      * <p>
1195      * This is a lane-wise binary operation which produces the result of rotating left the two's
1196      * complement binary representation of each lane of first operand (this vector) by input scalar.
1197      * Rotation by any multiple of 8 is a no-op, so only the 3 lowest-order bits of input value are used.
1198      * It is as if the input value were subjected to a bitwise logical
1199      * AND operator ({@code &}) with the mask value 0x7.
1200      *
1201      * @param s the input scalar; the number of the bits to rotate left
1202      * @return the result of rotating left this vector by the broadcast of an
1203      * input scalar
1204      */
1205     @ForceInline
1206     public final ByteVector rotateLeft(int s) {
1207         return shiftLeft(s).or(shiftRight(-s));
1208     }
1209 
1210     /**
1211      * Rotates left this vector by the broadcast of an input scalar, selecting
1212      * lane elements controlled by a mask.
1213      * <p>
1214      * This is a lane-wise binary operation which produces the result of rotating left the two's
1215      * complement binary representation of each lane of first operand (this vector) by input scalar.
1216      * Rotation by any multiple of 8 is a no-op, so only the 3 lowest-order bits of input value are used.
1217      * It is as if the input value were subjected to a bitwise logical
1218      * AND operator ({@code &}) with the mask value 0x7.
1219      *
1220      * @param s the input scalar; the number of the bits to rotate left
1221      * @param m the mask controlling lane selection
1222      * @return the result of rotating left this vector by the broadcast of an
1223      * input scalar
1224      */
1225     @ForceInline
1226     public final ByteVector rotateLeft(int s, VectorMask<Byte> m) {
1227         return shiftLeft(s, m).or(shiftRight(-s, m), m);
1228     }
1229 
1230     /**
1231      * Rotates right this vector by the broadcast of an input scalar.
1232      * <p>
1233      * This is a lane-wise binary operation which produces the result of rotating right the two's
1234      * complement binary representation of each lane of first operand (this vector) by input scalar.
1235      * Rotation by any multiple of 8 is a no-op, so only the 3 lowest-order bits of input value are used.
1236      * It is as if the input value were subjected to a bitwise logical
1237      * AND operator ({@code &}) with the mask value 0x7.
1238      *
1239      * @param s the input scalar; the number of the bits to rotate right
1240      * @return the result of rotating right this vector by the broadcast of an
1241      * input scalar
1242      */
1243     @ForceInline
1244     public final ByteVector rotateRight(int s) {
1245         return shiftRight(s).or(shiftLeft(-s));
1246     }
1247 
1248     /**
1249      * Rotates right this vector by the broadcast of an input scalar, selecting
1250      * lane elements controlled by a mask.
1251      * <p>
1252      * This is a lane-wise binary operation which produces the result of rotating right the two's
1253      * complement binary representation of each lane of first operand (this vector) by input scalar.
1254      * Rotation by any multiple of 8 is a no-op, so only the 3 lowest-order bits of input value are used.
1255      * It is as if the input value were subjected to a bitwise logical
1256      * AND operator ({@code &}) with the mask value 0x7.
1257      *
1258      * @param s the input scalar; the number of the bits to rotate right
1259      * @param m the mask controlling lane selection
1260      * @return the result of rotating right this vector by the broadcast of an
1261      * input scalar
1262      */
1263     @ForceInline
1264     public final ByteVector rotateRight(int s, VectorMask<Byte> m) {
1265         return shiftRight(s, m).or(shiftLeft(-s, m), m);
1266     }
1267 
1268     /**
1269      * {@inheritDoc}
1270      */
1271     @Override
1272     public abstract void intoByteArray(byte[] a, int ix);
1273 
1274     /**
1275      * {@inheritDoc}
1276      */
1277     @Override
1278     public abstract void intoByteArray(byte[] a, int ix, VectorMask<Byte> m);
1279 
1280     /**
1281      * {@inheritDoc}
1282      */
1283     @Override
1284     public abstract void intoByteBuffer(ByteBuffer bb, int ix);
1285 
1286     /**


1591      * {@code < 0} or {@code >= a.length}
1592      */
1593     public void intoArray(byte[] a, int a_offset, VectorMask<Byte> m, int[] indexMap, int i_offset) {
1594         forEach(m, (n, e) -> a[a_offset + indexMap[i_offset + n]] = e);
1595     }
1596     // Species
1597 
1598     /**
1599      * {@inheritDoc}
1600      */
1601     @Override
1602     public abstract VectorSpecies<Byte> species();
1603 
1604     /**
1605      * Class representing {@link ByteVector}'s of the same {@link VectorShape VectorShape}.
1606      */
1607     static final class ByteSpecies extends AbstractSpecies<Byte> {
1608         final Function<byte[], ByteVector> vectorFactory;
1609 
1610         private ByteSpecies(VectorShape shape,
1611                           Class<?> vectorType,
1612                           Class<?> maskType,
1613                           Function<byte[], ByteVector> vectorFactory,
1614                           Function<boolean[], VectorMask<Byte>> maskFactory,
1615                           Function<IntUnaryOperator, VectorShuffle<Byte>> shuffleFromArrayFactory,
1616                           fShuffleFromArray<Byte> shuffleFromOpFactory) {
1617             super(shape, byte.class, Byte.SIZE, vectorType, maskType, maskFactory,
1618                   shuffleFromArrayFactory, shuffleFromOpFactory);
1619             this.vectorFactory = vectorFactory;
1620         }
1621 
1622         interface FOp {
1623             byte apply(int i);
1624         }
1625 
1626         ByteVector op(FOp f) {
1627             byte[] res = new byte[length()];
1628             for (int i = 0; i < length(); i++) {
1629                 res[i] = f.apply(i);
1630             }
1631             return vectorFactory.apply(res);
1632         }
1633 
1634         ByteVector op(VectorMask<Byte> o, FOp f) {
1635             byte[] res = new byte[length()];
1636             boolean[] mbits = ((AbstractMask<Byte>)o).getBits();
1637             for (int i = 0; i < length(); i++) {


< prev index next >