95 interface FUnCon {
96 void apply(int i, short a);
97 }
98
99 abstract void forEach(FUnCon f);
100
101 abstract void forEach(VectorMask<Short> m, FUnCon f);
102
103 // Static factories
104
105 /**
106 * Returns a vector where all lane elements are set to the default
107 * primitive value.
108 *
109 * @param species species of desired vector
110 * @return a zero vector of given species
111 */
112 @ForceInline
113 @SuppressWarnings("unchecked")
114 public static ShortVector zero(VectorSpecies<Short> species) {
115 return VectorIntrinsics.broadcastCoerced((Class<ShortVector>) species.boxType(), short.class, species.length(),
116 0, species,
117 ((bits, s) -> ((ShortSpecies)s).op(i -> (short)bits)));
118 }
119
120 /**
121 * Loads a vector from a byte array starting at an offset.
122 * <p>
123 * Bytes are composed into primitive lane elements according to the
124 * native byte order of the underlying platform
125 * <p>
126 * This method behaves as if it returns the result of calling the
127 * byte buffer, offset, and mask accepting
128 * {@link #fromByteBuffer(VectorSpecies, ByteBuffer, int, VectorMask) method} as follows:
129 * <pre>{@code
130 * return fromByteBuffer(species, ByteBuffer.wrap(a), offset, VectorMask.allTrue());
131 * }</pre>
132 *
133 * @param species species of desired vector
134 * @param a the byte array
135 * @param offset the offset into the array
136 * @return a vector loaded from a byte array
137 * @throws IndexOutOfBoundsException if {@code i < 0} or
138 * {@code offset > a.length - (species.length() * species.elementSize() / Byte.SIZE)}
139 */
140 @ForceInline
141 @SuppressWarnings("unchecked")
142 public static ShortVector fromByteArray(VectorSpecies<Short> species, byte[] a, int offset) {
143 Objects.requireNonNull(a);
144 offset = VectorIntrinsics.checkIndex(offset, a.length, species.bitSize() / Byte.SIZE);
145 return VectorIntrinsics.load((Class<ShortVector>) species.boxType(), short.class, species.length(),
146 a, ((long) offset) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
147 a, offset, species,
148 (c, idx, s) -> {
149 ByteBuffer bbc = ByteBuffer.wrap(c, idx, a.length - idx).order(ByteOrder.nativeOrder());
150 ShortBuffer tb = bbc.asShortBuffer();
151 return ((ShortSpecies)s).op(i -> tb.get());
152 });
153 }
154
155 /**
156 * Loads a vector from a byte array starting at an offset and using a
157 * mask.
158 * <p>
159 * Bytes are composed into primitive lane elements according to the
160 * native byte order of the underlying platform.
161 * <p>
162 * This method behaves as if it returns the result of calling the
163 * byte buffer, offset, and mask accepting
164 * {@link #fromByteBuffer(VectorSpecies, ByteBuffer, int, VectorMask) method} as follows:
165 * <pre>{@code
183
184 /**
185 * Loads a vector from an array starting at offset.
186 * <p>
187 * For each vector lane, where {@code N} is the vector lane index, the
188 * array element at index {@code offset + N} is placed into the
189 * resulting vector at lane index {@code N}.
190 *
191 * @param species species of desired vector
192 * @param a the array
193 * @param offset the offset into the array
194 * @return the vector loaded from an array
195 * @throws IndexOutOfBoundsException if {@code offset < 0}, or
196 * {@code offset > a.length - species.length()}
197 */
198 @ForceInline
199 @SuppressWarnings("unchecked")
200 public static ShortVector fromArray(VectorSpecies<Short> species, short[] a, int offset){
201 Objects.requireNonNull(a);
202 offset = VectorIntrinsics.checkIndex(offset, a.length, species.length());
203 return VectorIntrinsics.load((Class<ShortVector>) species.boxType(), short.class, species.length(),
204 a, (((long) offset) << ARRAY_SHIFT) + Unsafe.ARRAY_SHORT_BASE_OFFSET,
205 a, offset, species,
206 (c, idx, s) -> ((ShortSpecies)s).op(n -> c[idx + n]));
207 }
208
209
210 /**
211 * Loads a vector from an array starting at offset and using a mask.
212 * <p>
213 * For each vector lane, where {@code N} is the vector lane index,
214 * if the mask lane at index {@code N} is set then the array element at
215 * index {@code offset + N} is placed into the resulting vector at lane index
216 * {@code N}, otherwise the default element value is placed into the
217 * resulting vector at lane index {@code N}.
218 *
219 * @param species species of desired vector
220 * @param a the array
221 * @param offset the offset into the array
222 * @param m the mask
223 * @return the vector loaded from an array
296 * return fromByteBuffer(b, offset, VectorMask.allTrue())
297 * }</pre>
298 *
299 * @param species species of desired vector
300 * @param bb the byte buffer
301 * @param offset the offset into the byte buffer
302 * @return a vector loaded from a byte buffer
303 * @throws IndexOutOfBoundsException if the offset is {@code < 0},
304 * or {@code > b.limit()},
305 * or if there are fewer than
306 * {@code species.length() * species.elementSize() / Byte.SIZE} bytes
307 * remaining in the byte buffer from the given offset
308 */
309 @ForceInline
310 @SuppressWarnings("unchecked")
311 public static ShortVector fromByteBuffer(VectorSpecies<Short> species, ByteBuffer bb, int offset) {
312 if (bb.order() != ByteOrder.nativeOrder()) {
313 throw new IllegalArgumentException();
314 }
315 offset = VectorIntrinsics.checkIndex(offset, bb.limit(), species.bitSize() / Byte.SIZE);
316 return VectorIntrinsics.load((Class<ShortVector>) species.boxType(), short.class, species.length(),
317 U.getReference(bb, BYTE_BUFFER_HB), U.getLong(bb, BUFFER_ADDRESS) + offset,
318 bb, offset, species,
319 (c, idx, s) -> {
320 ByteBuffer bbc = c.duplicate().position(idx).order(ByteOrder.nativeOrder());
321 ShortBuffer tb = bbc.asShortBuffer();
322 return ((ShortSpecies)s).op(i -> tb.get());
323 });
324 }
325
326 /**
327 * Loads a vector from a {@link ByteBuffer byte buffer} starting at an
328 * offset into the byte buffer and using a mask.
329 * <p>
330 * This method behaves as if the byte buffer is viewed as a primitive
331 * {@link java.nio.Buffer buffer} for the primitive element type,
332 * according to the native byte order of the underlying platform, and
333 * the returned vector is loaded with a mask from a primitive array
334 * obtained from the primitive buffer.
335 * The following pseudocode expresses the behaviour, where
336 * {@code EBuffer} is the primitive buffer type, {@code e} is the
360 * {@code offset >= b.limit() - (N * species.elementSize() / Byte.SIZE)}
361 */
362 @ForceInline
363 public static ShortVector fromByteBuffer(VectorSpecies<Short> species, ByteBuffer bb, int offset, VectorMask<Short> m) {
364 return zero(species).blend(fromByteBuffer(species, bb, offset), m);
365 }
366
367 /**
368 * Returns a vector where all lane elements are set to the primitive
369 * value {@code e}.
370 *
371 * @param species species of the desired vector
372 * @param e the value
373 * @return a vector of vector where all lane elements are set to
374 * the primitive value {@code e}
375 */
376 @ForceInline
377 @SuppressWarnings("unchecked")
378 public static ShortVector broadcast(VectorSpecies<Short> species, short e) {
379 return VectorIntrinsics.broadcastCoerced(
380 (Class<ShortVector>) species.boxType(), short.class, species.length(),
381 e, species,
382 ((bits, sp) -> ((ShortSpecies)sp).op(i -> (short)bits)));
383 }
384
385 /**
386 * Returns a vector where each lane element is set to given
387 * primitive values.
388 * <p>
389 * For each vector lane, where {@code N} is the vector lane index, the
390 * the primitive value at index {@code N} is placed into the resulting
391 * vector at lane index {@code N}.
392 *
393 * @param species species of the desired vector
394 * @param es the given primitive values
395 * @return a vector where each lane element is set to given primitive
396 * values
397 * @throws IndexOutOfBoundsException if {@code es.length < species.length()}
398 */
399 @ForceInline
400 @SuppressWarnings("unchecked")
401 public static ShortVector scalars(VectorSpecies<Short> species, short... es) {
402 Objects.requireNonNull(es);
403 int ix = VectorIntrinsics.checkIndex(0, es.length, species.length());
404 return VectorIntrinsics.load((Class<ShortVector>) species.boxType(), short.class, species.length(),
405 es, Unsafe.ARRAY_SHORT_BASE_OFFSET,
406 es, ix, species,
407 (c, idx, sp) -> ((ShortSpecies)sp).op(n -> c[idx + n]));
408 }
409
410 /**
411 * Returns a vector where the first lane element is set to the primtive
412 * value {@code e}, all other lane elements are set to the default
413 * value.
414 *
415 * @param species species of the desired vector
416 * @param e the value
417 * @return a vector where the first lane element is set to the primitive
418 * value {@code e}
419 */
420 @ForceInline
421 public static final ShortVector single(VectorSpecies<Short> species, short e) {
422 return zero(species).with(0, e);
423 }
424
762 @Override
763 public abstract ShortVector rearrange(Vector<Short> v,
764 VectorShuffle<Short> s, VectorMask<Short> m);
765
766 /**
767 * {@inheritDoc}
768 */
769 @Override
770 public abstract ShortVector rearrange(VectorShuffle<Short> m);
771
772 /**
773 * {@inheritDoc}
774 */
775 @Override
776 public abstract ShortVector reshape(VectorSpecies<Short> s);
777
778 /**
779 * {@inheritDoc}
780 */
781 @Override
782 public abstract ShortVector rotateEL(int i);
783
784 /**
785 * {@inheritDoc}
786 */
787 @Override
788 public abstract ShortVector rotateER(int i);
789
790 /**
791 * {@inheritDoc}
792 */
793 @Override
794 public abstract ShortVector shiftEL(int i);
795
796 /**
797 * {@inheritDoc}
798 */
799 @Override
800 public abstract ShortVector shiftER(int i);
801
802
803
804 /**
805 * Bitwise ANDs this vector with an input vector.
806 * <p>
807 * This is a lane-wise binary operation which applies the primitive bitwise AND
808 * operation ({@code &}) to each lane.
809 *
810 * @param v the input vector
811 * @return the bitwise AND of this vector with the input vector
812 */
813 public abstract ShortVector and(Vector<Short> v);
814
815 /**
816 * Bitwise ANDs this vector with the broadcast of an input scalar.
817 * <p>
818 * This is a lane-wise binary operation which applies the primitive bitwise AND
819 * operation ({@code &}) to each lane.
820 *
960 * @return the bitwise NOT of this vector
961 */
962 public abstract ShortVector not();
963
964 /**
965 * Bitwise NOTs this vector, selecting lane elements controlled by a mask.
966 * <p>
967 * This is a lane-wise unary operation which applies the primitive bitwise NOT
968 * operation ({@code ~}) to each lane.
969 *
970 * @param m the mask controlling lane selection
971 * @return the bitwise NOT of this vector
972 */
973 public abstract ShortVector not(VectorMask<Short> m);
974
975 /**
976 * Logically left shifts this vector by the broadcast of an input scalar.
977 * <p>
978 * This is a lane-wise binary operation which applies the primitive logical left shift
979 * operation ({@code <<}) to each lane to left shift the
980 * element by shift value as specified by the input scalar. Only the 4
981 * lowest-order bits of shift value are used. It is as if the shift value
982 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
983 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
984 *
985 * @param s the input scalar; the number of the bits to left shift
986 * @return the result of logically left shifting left this vector by the
987 * broadcast of an input scalar
988 */
989 public abstract ShortVector shiftL(int s);
990
991 /**
992 * Logically left shifts this vector by the broadcast of an input scalar,
993 * selecting lane elements controlled by a mask.
994 * <p>
995 * This is a lane-wise binary operation which applies the primitive logical left shift
996 * operation ({@code <<}) to each lane to left shift the
997 * element by shift value as specified by the input scalar. Only the 4
998 * lowest-order bits of shift value are used. It is as if the shift value
999 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1000 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1001 *
1002 * @param s the input scalar; the number of the bits to left shift
1003 * @param m the mask controlling lane selection
1004 * @return the result of logically left shifting left this vector by the
1005 * broadcast of an input scalar
1006 */
1007 public abstract ShortVector shiftL(int s, VectorMask<Short> m);
1008
1009
1010 // logical, or unsigned, shift right
1011
1012 /**
1013 * Logically right shifts (or unsigned right shifts) this vector by the
1014 * broadcast of an input scalar.
1015 * <p>
1016 * This is a lane-wise binary operation which applies the primitive logical right shift
1017 * operation ({@code >>>}) to each lane to logically right shift the
1018 * element by shift value as specified by the input scalar. Only the 4
1019 * lowest-order bits of shift value are used. It is as if the shift value
1020 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1021 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1022 *
1023 * @param s the input scalar; the number of the bits to right shift
1024 * @return the result of logically right shifting this vector by the
1025 * broadcast of an input scalar
1026 */
1027 public abstract ShortVector shiftR(int s);
1028
1029 /**
1030 * Logically right shifts (or unsigned right shifts) this vector by the
1031 * broadcast of an input scalar, selecting lane elements controlled by a
1032 * mask.
1033 * <p>
1034 * This is a lane-wise binary operation which applies the primitive logical right shift
1035 * operation ({@code >>>}) to each lane to logically right shift the
1036 * element by shift value as specified by the input scalar. Only the 4
1037 * lowest-order bits of shift value are used. It is as if the shift value
1038 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1039 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1040 *
1041 * @param s the input scalar; the number of the bits to right shift
1042 * @param m the mask controlling lane selection
1043 * @return the result of logically right shifting this vector by the
1044 * broadcast of an input scalar
1045 */
1046 public abstract ShortVector shiftR(int s, VectorMask<Short> m);
1047
1048
1049 /**
1050 * Arithmetically right shifts (or signed right shifts) this vector by the
1051 * broadcast of an input scalar.
1052 * <p>
1053 * This is a lane-wise binary operation which applies the primitive arithmetic right
1054 * shift operation ({@code >>}) to each lane to arithmetically
1055 * right shift the element by shift value as specified by the input scalar.
1056 * Only the 4 lowest-order bits of shift value are used. It is as if the shift
1057 * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1058 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1059 *
1060 * @param s the input scalar; the number of the bits to right shift
1061 * @return the result of arithmetically right shifting this vector by the
1062 * broadcast of an input scalar
1063 */
1064 public abstract ShortVector aShiftR(int s);
1065
1066 /**
1067 * Arithmetically right shifts (or signed right shifts) this vector by the
1068 * broadcast of an input scalar, selecting lane elements controlled by a
1069 * mask.
1070 * <p>
1071 * This is a lane-wise binary operation which applies the primitive arithmetic right
1072 * shift operation ({@code >>}) to each lane to arithmetically
1073 * right shift the element by shift value as specified by the input scalar.
1074 * Only the 4 lowest-order bits of shift value are used. It is as if the shift
1075 * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1076 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1077 *
1078 * @param s the input scalar; the number of the bits to right shift
1079 * @param m the mask controlling lane selection
1080 * @return the result of arithmetically right shifting this vector by the
1081 * broadcast of an input scalar
1082 */
1083 public abstract ShortVector aShiftR(int s, VectorMask<Short> m);
1084
1085
1086 /**
1087 * {@inheritDoc}
1088 */
1089 @Override
1090 public abstract void intoByteArray(byte[] a, int ix);
1091
1092 /**
1093 * {@inheritDoc}
1094 */
1095 @Override
1096 public abstract void intoByteArray(byte[] a, int ix, VectorMask<Short> m);
1097
1098 /**
1099 * {@inheritDoc}
1100 */
1101 @Override
1102 public abstract void intoByteBuffer(ByteBuffer bb, int ix);
1103
1104 /**
1409 * {@code < 0} or {@code >= a.length}
1410 */
1411 public void intoArray(short[] a, int a_offset, VectorMask<Short> m, int[] indexMap, int i_offset) {
1412 forEach(m, (n, e) -> a[a_offset + indexMap[i_offset + n]] = e);
1413 }
1414 // Species
1415
1416 /**
1417 * {@inheritDoc}
1418 */
1419 @Override
1420 public abstract VectorSpecies<Short> species();
1421
1422 /**
1423 * Class representing {@link ShortVector}'s of the same {@link VectorShape VectorShape}.
1424 */
1425 static final class ShortSpecies extends AbstractSpecies<Short> {
1426 final Function<short[], ShortVector> vectorFactory;
1427
1428 private ShortSpecies(VectorShape shape,
1429 Class<?> boxType,
1430 Class<?> maskType,
1431 Function<short[], ShortVector> vectorFactory,
1432 Function<boolean[], VectorMask<Short>> maskFactory,
1433 Function<IntUnaryOperator, VectorShuffle<Short>> shuffleFromArrayFactory,
1434 fShuffleFromArray<Short> shuffleFromOpFactory) {
1435 super(shape, short.class, Short.SIZE, boxType, maskType, maskFactory,
1436 shuffleFromArrayFactory, shuffleFromOpFactory);
1437 this.vectorFactory = vectorFactory;
1438 }
1439
1440 interface FOp {
1441 short apply(int i);
1442 }
1443
1444 ShortVector op(FOp f) {
1445 short[] res = new short[length()];
1446 for (int i = 0; i < length(); i++) {
1447 res[i] = f.apply(i);
1448 }
1449 return vectorFactory.apply(res);
1450 }
1451
1452 ShortVector op(VectorMask<Short> o, FOp f) {
1453 short[] res = new short[length()];
1454 boolean[] mbits = ((AbstractMask<Short>)o).getBits();
1455 for (int i = 0; i < length(); i++) {
|
95 interface FUnCon {
96 void apply(int i, short a);
97 }
98
99 abstract void forEach(FUnCon f);
100
101 abstract void forEach(VectorMask<Short> m, FUnCon f);
102
103 // Static factories
104
105 /**
106 * Returns a vector where all lane elements are set to the default
107 * primitive value.
108 *
109 * @param species species of desired vector
110 * @return a zero vector of given species
111 */
112 @ForceInline
113 @SuppressWarnings("unchecked")
114 public static ShortVector zero(VectorSpecies<Short> species) {
115 return VectorIntrinsics.broadcastCoerced((Class<ShortVector>) species.vectorType(), short.class, species.length(),
116 0, species,
117 ((bits, s) -> ((ShortSpecies)s).op(i -> (short)bits)));
118 }
119
120 /**
121 * Loads a vector from a byte array starting at an offset.
122 * <p>
123 * Bytes are composed into primitive lane elements according to the
124 * native byte order of the underlying platform
125 * <p>
126 * This method behaves as if it returns the result of calling the
127 * byte buffer, offset, and mask accepting
128 * {@link #fromByteBuffer(VectorSpecies, ByteBuffer, int, VectorMask) method} as follows:
129 * <pre>{@code
130 * return fromByteBuffer(species, ByteBuffer.wrap(a), offset, VectorMask.allTrue());
131 * }</pre>
132 *
133 * @param species species of desired vector
134 * @param a the byte array
135 * @param offset the offset into the array
136 * @return a vector loaded from a byte array
137 * @throws IndexOutOfBoundsException if {@code i < 0} or
138 * {@code offset > a.length - (species.length() * species.elementSize() / Byte.SIZE)}
139 */
140 @ForceInline
141 @SuppressWarnings("unchecked")
142 public static ShortVector fromByteArray(VectorSpecies<Short> species, byte[] a, int offset) {
143 Objects.requireNonNull(a);
144 offset = VectorIntrinsics.checkIndex(offset, a.length, species.bitSize() / Byte.SIZE);
145 return VectorIntrinsics.load((Class<ShortVector>) species.vectorType(), short.class, species.length(),
146 a, ((long) offset) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
147 a, offset, species,
148 (c, idx, s) -> {
149 ByteBuffer bbc = ByteBuffer.wrap(c, idx, a.length - idx).order(ByteOrder.nativeOrder());
150 ShortBuffer tb = bbc.asShortBuffer();
151 return ((ShortSpecies)s).op(i -> tb.get());
152 });
153 }
154
155 /**
156 * Loads a vector from a byte array starting at an offset and using a
157 * mask.
158 * <p>
159 * Bytes are composed into primitive lane elements according to the
160 * native byte order of the underlying platform.
161 * <p>
162 * This method behaves as if it returns the result of calling the
163 * byte buffer, offset, and mask accepting
164 * {@link #fromByteBuffer(VectorSpecies, ByteBuffer, int, VectorMask) method} as follows:
165 * <pre>{@code
183
184 /**
185 * Loads a vector from an array starting at offset.
186 * <p>
187 * For each vector lane, where {@code N} is the vector lane index, the
188 * array element at index {@code offset + N} is placed into the
189 * resulting vector at lane index {@code N}.
190 *
191 * @param species species of desired vector
192 * @param a the array
193 * @param offset the offset into the array
194 * @return the vector loaded from an array
195 * @throws IndexOutOfBoundsException if {@code offset < 0}, or
196 * {@code offset > a.length - species.length()}
197 */
198 @ForceInline
199 @SuppressWarnings("unchecked")
200 public static ShortVector fromArray(VectorSpecies<Short> species, short[] a, int offset){
201 Objects.requireNonNull(a);
202 offset = VectorIntrinsics.checkIndex(offset, a.length, species.length());
203 return VectorIntrinsics.load((Class<ShortVector>) species.vectorType(), short.class, species.length(),
204 a, (((long) offset) << ARRAY_SHIFT) + Unsafe.ARRAY_SHORT_BASE_OFFSET,
205 a, offset, species,
206 (c, idx, s) -> ((ShortSpecies)s).op(n -> c[idx + n]));
207 }
208
209
210 /**
211 * Loads a vector from an array starting at offset and using a mask.
212 * <p>
213 * For each vector lane, where {@code N} is the vector lane index,
214 * if the mask lane at index {@code N} is set then the array element at
215 * index {@code offset + N} is placed into the resulting vector at lane index
216 * {@code N}, otherwise the default element value is placed into the
217 * resulting vector at lane index {@code N}.
218 *
219 * @param species species of desired vector
220 * @param a the array
221 * @param offset the offset into the array
222 * @param m the mask
223 * @return the vector loaded from an array
296 * return fromByteBuffer(b, offset, VectorMask.allTrue())
297 * }</pre>
298 *
299 * @param species species of desired vector
300 * @param bb the byte buffer
301 * @param offset the offset into the byte buffer
302 * @return a vector loaded from a byte buffer
303 * @throws IndexOutOfBoundsException if the offset is {@code < 0},
304 * or {@code > b.limit()},
305 * or if there are fewer than
306 * {@code species.length() * species.elementSize() / Byte.SIZE} bytes
307 * remaining in the byte buffer from the given offset
308 */
309 @ForceInline
310 @SuppressWarnings("unchecked")
311 public static ShortVector fromByteBuffer(VectorSpecies<Short> species, ByteBuffer bb, int offset) {
312 if (bb.order() != ByteOrder.nativeOrder()) {
313 throw new IllegalArgumentException();
314 }
315 offset = VectorIntrinsics.checkIndex(offset, bb.limit(), species.bitSize() / Byte.SIZE);
316 return VectorIntrinsics.load((Class<ShortVector>) species.vectorType(), short.class, species.length(),
317 U.getReference(bb, BYTE_BUFFER_HB), U.getLong(bb, BUFFER_ADDRESS) + offset,
318 bb, offset, species,
319 (c, idx, s) -> {
320 ByteBuffer bbc = c.duplicate().position(idx).order(ByteOrder.nativeOrder());
321 ShortBuffer tb = bbc.asShortBuffer();
322 return ((ShortSpecies)s).op(i -> tb.get());
323 });
324 }
325
326 /**
327 * Loads a vector from a {@link ByteBuffer byte buffer} starting at an
328 * offset into the byte buffer and using a mask.
329 * <p>
330 * This method behaves as if the byte buffer is viewed as a primitive
331 * {@link java.nio.Buffer buffer} for the primitive element type,
332 * according to the native byte order of the underlying platform, and
333 * the returned vector is loaded with a mask from a primitive array
334 * obtained from the primitive buffer.
335 * The following pseudocode expresses the behaviour, where
336 * {@code EBuffer} is the primitive buffer type, {@code e} is the
360 * {@code offset >= b.limit() - (N * species.elementSize() / Byte.SIZE)}
361 */
362 @ForceInline
363 public static ShortVector fromByteBuffer(VectorSpecies<Short> species, ByteBuffer bb, int offset, VectorMask<Short> m) {
364 return zero(species).blend(fromByteBuffer(species, bb, offset), m);
365 }
366
367 /**
368 * Returns a vector where all lane elements are set to the primitive
369 * value {@code e}.
370 *
371 * @param species species of the desired vector
372 * @param e the value
373 * @return a vector of vector where all lane elements are set to
374 * the primitive value {@code e}
375 */
376 @ForceInline
377 @SuppressWarnings("unchecked")
378 public static ShortVector broadcast(VectorSpecies<Short> species, short e) {
379 return VectorIntrinsics.broadcastCoerced(
380 (Class<ShortVector>) species.vectorType(), short.class, species.length(),
381 e, species,
382 ((bits, sp) -> ((ShortSpecies)sp).op(i -> (short)bits)));
383 }
384
385 /**
386 * Returns a vector where each lane element is set to given
387 * primitive values.
388 * <p>
389 * For each vector lane, where {@code N} is the vector lane index, the
390 * the primitive value at index {@code N} is placed into the resulting
391 * vector at lane index {@code N}.
392 *
393 * @param species species of the desired vector
394 * @param es the given primitive values
395 * @return a vector where each lane element is set to given primitive
396 * values
397 * @throws IndexOutOfBoundsException if {@code es.length < species.length()}
398 */
399 @ForceInline
400 @SuppressWarnings("unchecked")
401 public static ShortVector scalars(VectorSpecies<Short> species, short... es) {
402 Objects.requireNonNull(es);
403 int ix = VectorIntrinsics.checkIndex(0, es.length, species.length());
404 return VectorIntrinsics.load((Class<ShortVector>) species.vectorType(), short.class, species.length(),
405 es, Unsafe.ARRAY_SHORT_BASE_OFFSET,
406 es, ix, species,
407 (c, idx, sp) -> ((ShortSpecies)sp).op(n -> c[idx + n]));
408 }
409
410 /**
411 * Returns a vector where the first lane element is set to the primtive
412 * value {@code e}, all other lane elements are set to the default
413 * value.
414 *
415 * @param species species of the desired vector
416 * @param e the value
417 * @return a vector where the first lane element is set to the primitive
418 * value {@code e}
419 */
420 @ForceInline
421 public static final ShortVector single(VectorSpecies<Short> species, short e) {
422 return zero(species).with(0, e);
423 }
424
762 @Override
763 public abstract ShortVector rearrange(Vector<Short> v,
764 VectorShuffle<Short> s, VectorMask<Short> m);
765
766 /**
767 * {@inheritDoc}
768 */
769 @Override
770 public abstract ShortVector rearrange(VectorShuffle<Short> m);
771
772 /**
773 * {@inheritDoc}
774 */
775 @Override
776 public abstract ShortVector reshape(VectorSpecies<Short> s);
777
778 /**
779 * {@inheritDoc}
780 */
781 @Override
782 public abstract ShortVector rotateLanesLeft(int i);
783
784 /**
785 * {@inheritDoc}
786 */
787 @Override
788 public abstract ShortVector rotateLanesRight(int i);
789
790 /**
791 * {@inheritDoc}
792 */
793 @Override
794 public abstract ShortVector shiftLanesLeft(int i);
795
796 /**
797 * {@inheritDoc}
798 */
799 @Override
800 public abstract ShortVector shiftLanesRight(int i);
801
802
803
804 /**
805 * Bitwise ANDs this vector with an input vector.
806 * <p>
807 * This is a lane-wise binary operation which applies the primitive bitwise AND
808 * operation ({@code &}) to each lane.
809 *
810 * @param v the input vector
811 * @return the bitwise AND of this vector with the input vector
812 */
813 public abstract ShortVector and(Vector<Short> v);
814
815 /**
816 * Bitwise ANDs this vector with the broadcast of an input scalar.
817 * <p>
818 * This is a lane-wise binary operation which applies the primitive bitwise AND
819 * operation ({@code &}) to each lane.
820 *
960 * @return the bitwise NOT of this vector
961 */
962 public abstract ShortVector not();
963
964 /**
965 * Bitwise NOTs this vector, selecting lane elements controlled by a mask.
966 * <p>
967 * This is a lane-wise unary operation which applies the primitive bitwise NOT
968 * operation ({@code ~}) to each lane.
969 *
970 * @param m the mask controlling lane selection
971 * @return the bitwise NOT of this vector
972 */
973 public abstract ShortVector not(VectorMask<Short> m);
974
975 /**
976 * Logically left shifts this vector by the broadcast of an input scalar.
977 * <p>
978 * This is a lane-wise binary operation which applies the primitive logical left shift
979 * operation ({@code <<}) to each lane to left shift the
980 * element by shift value as specified by the input scalar.
981 * Only the 4 lowest-order bits of shift value are used. It is as if the shift value
982 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
983 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
984 *
985 * @param s the input scalar; the number of the bits to left shift
986 * @return the result of logically left shifting left this vector by the
987 * broadcast of an input scalar
988 */
989 public abstract ShortVector shiftLeft(int s);
990
991 /**
992 * Logically left shifts this vector by the broadcast of an input scalar,
993 * selecting lane elements controlled by a mask.
994 * <p>
995 * This is a lane-wise binary operation which applies the primitive logical left shift
996 * operation ({@code <<}) to each lane to left shift the
997 * element by shift value as specified by the input scalar.
998 * Only the 4 lowest-order bits of shift value are used. It is as if the shift value
999 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1000 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1001 *
1002 * @param s the input scalar; the number of the bits to left shift
1003 * @param m the mask controlling lane selection
1004 * @return the result of logically left shifting left this vector by the
1005 * broadcast of an input scalar
1006 */
1007 public abstract ShortVector shiftLeft(int s, VectorMask<Short> m);
1008
1009 /**
1010 * Logically left shifts this vector by an input vector.
1011 * <p>
1012 * This is a lane-wise binary operation which applies the primitive logical left shift
1013 * operation ({@code <<}) to each lane. For each lane of this vector, the
1014 * shift value is the corresponding lane of input vector.
1015 * Only the 4 lowest-order bits of shift value are used. It is as if the shift value
1016 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1017 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1018 *
1019 * @param v the input vector
1020 * @return the result of logically left shifting this vector by the input
1021 * vector
1022 */
1023 public abstract ShortVector shiftLeft(Vector<Short> v);
1024
1025 /**
1026 * Logically left shifts this vector by an input vector, selecting lane
1027 * elements controlled by a mask.
1028 * <p>
1029 * This is a lane-wise binary operation which applies the primitive logical left shift
1030 * operation ({@code <<}) to each lane. For each lane of this vector, the
1031 * shift value is the corresponding lane of input vector.
1032 * Only the 4 lowest-order bits of shift value are used. It is as if the shift value
1033 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1034 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1035 *
1036 * @param v the input vector
1037 * @param m the mask controlling lane selection
1038 * @return the result of logically left shifting this vector by the input
1039 * vector
1040 */
1041 public ShortVector shiftLeft(Vector<Short> v, VectorMask<Short> m) {
1042 return blend(shiftLeft(v), m);
1043 }
1044
1045 // logical, or unsigned, shift right
1046
1047 /**
1048 * Logically right shifts (or unsigned right shifts) this vector by the
1049 * broadcast of an input scalar.
1050 * <p>
1051 * This is a lane-wise binary operation which applies the primitive logical right shift
1052 * operation ({@code >>>}) to each lane to logically right shift the
1053 * element by shift value as specified by the input scalar.
1054 * Only the 4 lowest-order bits of shift value are used. It is as if the shift value
1055 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1056 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1057 *
1058 * @param s the input scalar; the number of the bits to right shift
1059 * @return the result of logically right shifting this vector by the
1060 * broadcast of an input scalar
1061 */
1062 public abstract ShortVector shiftRight(int s);
1063
1064 /**
1065 * Logically right shifts (or unsigned right shifts) this vector by the
1066 * broadcast of an input scalar, selecting lane elements controlled by a
1067 * mask.
1068 * <p>
1069 * This is a lane-wise binary operation which applies the primitive logical right shift
1070 * operation ({@code >>}) to each lane to logically right shift the
1071 * element by shift value as specified by the input scalar.
1072 * Only the 4 lowest-order bits of shift value are used. It is as if the shift value
1073 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1074 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1075 *
1076 * @param s the input scalar; the number of the bits to right shift
1077 * @param m the mask controlling lane selection
1078 * @return the result of logically right shifting this vector by the
1079 * broadcast of an input scalar
1080 */
1081 public abstract ShortVector shiftRight(int s, VectorMask<Short> m);
1082
1083 /**
1084 * Logically right shifts (or unsigned right shifts) this vector by an
1085 * input vector.
1086 * <p>
1087 * This is a lane-wise binary operation which applies the primitive logical right shift
1088 * operation ({@code >>>}) to each lane. For each lane of this vector, the
1089 * shift value is the corresponding lane of input vector.
1090 * Only the 4 lowest-order bits of shift value are used. It is as if the shift value
1091 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1092 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1093 *
1094 * @param v the input vector
1095 * @return the result of logically right shifting this vector by the
1096 * input vector
1097 */
1098 public abstract ShortVector shiftRight(Vector<Short> v);
1099
1100 /**
1101 * Logically right shifts (or unsigned right shifts) this vector by an
1102 * input vector, selecting lane elements controlled by a mask.
1103 * <p>
1104 * This is a lane-wise binary operation which applies the primitive logical right shift
1105 * operation ({@code >>>}) to each lane. For each lane of this vector, the
1106 * shift value is the corresponding lane of input vector.
1107 * Only the 4 lowest-order bits of shift value are used. It is as if the shift value
1108 * were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1109 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1110 *
1111 * @param v the input vector
1112 * @param m the mask controlling lane selection
1113 * @return the result of logically right shifting this vector by the
1114 * input vector
1115 */
1116 public ShortVector shiftRight(Vector<Short> v, VectorMask<Short> m) {
1117 return blend(shiftRight(v), m);
1118 }
1119
1120 /**
1121 * Arithmetically right shifts (or signed right shifts) this vector by the
1122 * broadcast of an input scalar.
1123 * <p>
1124 * This is a lane-wise binary operation which applies the primitive arithmetic right
1125 * shift operation ({@code >>}) to each lane to arithmetically
1126 * right shift the element by shift value as specified by the input scalar.
1127 * Only the 4 lowest-order bits of shift value are used. It is as if the shift
1128 * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1129 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1130 *
1131 * @param s the input scalar; the number of the bits to right shift
1132 * @return the result of arithmetically right shifting this vector by the
1133 * broadcast of an input scalar
1134 */
1135 public abstract ShortVector shiftArithmeticRight(int s);
1136
1137 /**
1138 * Arithmetically right shifts (or signed right shifts) this vector by the
1139 * broadcast of an input scalar, selecting lane elements controlled by a
1140 * mask.
1141 * <p>
1142 * This is a lane-wise binary operation which applies the primitive arithmetic right
1143 * shift operation ({@code >>}) to each lane to arithmetically
1144 * right shift the element by shift value as specified by the input scalar.
1145 * Only the 4 lowest-order bits of shift value are used. It is as if the shift
1146 * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1147 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1148 *
1149 * @param s the input scalar; the number of the bits to right shift
1150 * @param m the mask controlling lane selection
1151 * @return the result of arithmetically right shifting this vector by the
1152 * broadcast of an input scalar
1153 */
1154 public abstract ShortVector shiftArithmeticRight(int s, VectorMask<Short> m);
1155
1156 /**
1157 * Arithmetically right shifts (or signed right shifts) this vector by an
1158 * input vector.
1159 * <p>
1160 * This is a lane-wise binary operation which applies the primitive arithmetic right
1161 * shift operation ({@code >>}) to each lane. For each lane of this vector, the
1162 * shift value is the corresponding lane of input vector.
1163 * Only the 4 lowest-order bits of shift value are used. It is as if the shift
1164 * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1165 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1166 *
1167 * @param v the input vector
1168 * @return the result of arithmetically right shifting this vector by the
1169 * input vector
1170 */
1171 public abstract ShortVector shiftArithmeticRight(Vector<Short> v);
1172
1173 /**
1174 * Arithmetically right shifts (or signed right shifts) this vector by an
1175 * input vector, selecting lane elements controlled by a mask.
1176 * <p>
1177 * This is a lane-wise binary operation which applies the primitive arithmetic right
1178 * shift operation ({@code >>}) to each lane. For each lane of this vector, the
1179 * shift value is the corresponding lane of input vector.
1180 * Only the 4 lowest-order bits of shift value are used. It is as if the shift
1181 * value were subjected to a bitwise logical AND operator ({@code &}) with the mask value 0xF.
1182 * The shift distance actually used is therefore always in the range 0 to 15, inclusive.
1183 *
1184 * @param v the input vector
1185 * @param m the mask controlling lane selection
1186 * @return the result of arithmetically right shifting this vector by the
1187 * input vector
1188 */
1189 public ShortVector shiftArithmeticRight(Vector<Short> v, VectorMask<Short> m) {
1190 return blend(shiftArithmeticRight(v), m);
1191 }
1192
1193 /**
1194 * Rotates left this vector by the broadcast of an input scalar.
1195 * <p>
1196 * This is a lane-wise binary operation which produces the result of rotating left the two's
1197 * complement binary representation of each lane of first operand (this vector) by input scalar.
1198 * Rotation by any multiple of 16 is a no-op, so only the 4 lowest-order bits of input value are used.
1199 * It is as if the input value were subjected to a bitwise logical
1200 * AND operator ({@code &}) with the mask value 0x15.
1201 *
1202 * @param s the input scalar; the number of the bits to rotate left
1203 * @return the result of rotating left this vector by the broadcast of an
1204 * input scalar
1205 */
1206 @ForceInline
1207 public final ShortVector rotateLeft(int s) {
1208 return shiftLeft(s).or(shiftRight(-s));
1209 }
1210
1211 /**
1212 * Rotates left this vector by the broadcast of an input scalar, selecting
1213 * lane elements controlled by a mask.
1214 * <p>
1215 * This is a lane-wise binary operation which produces the result of rotating left the two's
1216 * complement binary representation of each lane of first operand (this vector) by input scalar.
1217 * Rotation by any multiple of 16 is a no-op, so only the 4 lowest-order bits of input value are used.
1218 * It is as if the input value were subjected to a bitwise logical
1219 * AND operator ({@code &}) with the mask value 0x15.
1220 *
1221 * @param s the input scalar; the number of the bits to rotate left
1222 * @param m the mask controlling lane selection
1223 * @return the result of rotating left this vector by the broadcast of an
1224 * input scalar
1225 */
1226 @ForceInline
1227 public final ShortVector rotateLeft(int s, VectorMask<Short> m) {
1228 return shiftLeft(s, m).or(shiftRight(-s, m), m);
1229 }
1230
1231 /**
1232 * Rotates right this vector by the broadcast of an input scalar.
1233 * <p>
1234 * This is a lane-wise binary operation which produces the result of rotating right the two's
1235 * complement binary representation of each lane of first operand (this vector) by input scalar.
1236 * Rotation by any multiple of 16 is a no-op, so only the 4 lowest-order bits of input value are used.
1237 * It is as if the input value were subjected to a bitwise logical
1238 * AND operator ({@code &}) with the mask value 0x15.
1239 *
1240 * @param s the input scalar; the number of the bits to rotate right
1241 * @return the result of rotating right this vector by the broadcast of an
1242 * input scalar
1243 */
1244 @ForceInline
1245 public final ShortVector rotateRight(int s) {
1246 return shiftRight(s).or(shiftLeft(-s));
1247 }
1248
1249 /**
1250 * Rotates right this vector by the broadcast of an input scalar, selecting
1251 * lane elements controlled by a mask.
1252 * <p>
1253 * This is a lane-wise binary operation which produces the result of rotating right the two's
1254 * complement binary representation of each lane of first operand (this vector) by input scalar.
1255 * Rotation by any multiple of 16 is a no-op, so only the 4 lowest-order bits of input value are used.
1256 * It is as if the input value were subjected to a bitwise logical
1257 * AND operator ({@code &}) with the mask value 0x15.
1258 *
1259 * @param s the input scalar; the number of the bits to rotate right
1260 * @param m the mask controlling lane selection
1261 * @return the result of rotating right this vector by the broadcast of an
1262 * input scalar
1263 */
1264 @ForceInline
1265 public final ShortVector rotateRight(int s, VectorMask<Short> m) {
1266 return shiftRight(s, m).or(shiftLeft(-s, m), m);
1267 }
1268
1269 /**
1270 * {@inheritDoc}
1271 */
1272 @Override
1273 public abstract void intoByteArray(byte[] a, int ix);
1274
1275 /**
1276 * {@inheritDoc}
1277 */
1278 @Override
1279 public abstract void intoByteArray(byte[] a, int ix, VectorMask<Short> m);
1280
1281 /**
1282 * {@inheritDoc}
1283 */
1284 @Override
1285 public abstract void intoByteBuffer(ByteBuffer bb, int ix);
1286
1287 /**
1592 * {@code < 0} or {@code >= a.length}
1593 */
1594 public void intoArray(short[] a, int a_offset, VectorMask<Short> m, int[] indexMap, int i_offset) {
1595 forEach(m, (n, e) -> a[a_offset + indexMap[i_offset + n]] = e);
1596 }
1597 // Species
1598
1599 /**
1600 * {@inheritDoc}
1601 */
1602 @Override
1603 public abstract VectorSpecies<Short> species();
1604
1605 /**
1606 * Class representing {@link ShortVector}'s of the same {@link VectorShape VectorShape}.
1607 */
1608 static final class ShortSpecies extends AbstractSpecies<Short> {
1609 final Function<short[], ShortVector> vectorFactory;
1610
1611 private ShortSpecies(VectorShape shape,
1612 Class<?> vectorType,
1613 Class<?> maskType,
1614 Function<short[], ShortVector> vectorFactory,
1615 Function<boolean[], VectorMask<Short>> maskFactory,
1616 Function<IntUnaryOperator, VectorShuffle<Short>> shuffleFromArrayFactory,
1617 fShuffleFromArray<Short> shuffleFromOpFactory) {
1618 super(shape, short.class, Short.SIZE, vectorType, maskType, maskFactory,
1619 shuffleFromArrayFactory, shuffleFromOpFactory);
1620 this.vectorFactory = vectorFactory;
1621 }
1622
1623 interface FOp {
1624 short apply(int i);
1625 }
1626
1627 ShortVector op(FOp f) {
1628 short[] res = new short[length()];
1629 for (int i = 0; i < length(); i++) {
1630 res[i] = f.apply(i);
1631 }
1632 return vectorFactory.apply(res);
1633 }
1634
1635 ShortVector op(VectorMask<Short> o, FOp f) {
1636 short[] res = new short[length()];
1637 boolean[] mbits = ((AbstractMask<Short>)o).getBits();
1638 for (int i = 0; i < length(); i++) {
|