< prev index next >

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

Print this page
rev 55589 : Species-phase2
rev 55591 : XxxSpecies made package private
rev 55592 : Capitalized Species names


  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have
  23  * questions.
  24  */
  25 package jdk.incubator.vector;
  26 
  27 import java.nio.ByteBuffer;
  28 import java.nio.ByteOrder;
  29 import java.nio.ShortBuffer;
  30 import java.nio.ReadOnlyBufferException;
  31 import java.util.Arrays;
  32 import java.util.Objects;
  33 import java.util.function.IntUnaryOperator;
  34 
  35 import jdk.internal.misc.Unsafe;
  36 import jdk.internal.vm.annotation.ForceInline;
  37 import static jdk.incubator.vector.VectorIntrinsics.*;
  38 
  39 @SuppressWarnings("cast")
  40 final class ShortMaxVector extends ShortVector {
  41     static final ShortMaxSpecies SPECIES = new ShortMaxSpecies();
  42 
  43     static final ShortMaxVector ZERO = new ShortMaxVector();
  44 
  45     static final int LENGTH = SPECIES.length();
  46 
  47     private final short[] vec; // Don't access directly, use getElements() instead.
  48 
  49     private short[] getElements() {
  50         return VectorIntrinsics.maybeRebox(this).vec;
  51     }
  52 
  53     ShortMaxVector() {
  54         vec = new short[SPECIES.length()];
  55     }
  56 
  57     ShortMaxVector(short[] v) {
  58         vec = v;
  59     }
  60 
  61     @Override


 138 
 139     @Override
 140     short rOp(short v, FBinOp f) {
 141         short[] vec = getElements();
 142         for (int i = 0; i < length(); i++) {
 143             v = f.apply(i, v, vec[i]);
 144         }
 145         return v;
 146     }
 147 
 148     @Override
 149     @ForceInline
 150     public <F> Vector<F> cast(Species<F> s) {
 151         Objects.requireNonNull(s);
 152         if (s.length() != LENGTH)
 153             throw new IllegalArgumentException("Vector length this species length differ");
 154 
 155         return VectorIntrinsics.cast(
 156             ShortMaxVector.class,
 157             short.class, LENGTH,
 158             s.vectorType(),
 159             s.elementType(), LENGTH,
 160             this, s,
 161             (species, vector) -> vector.castDefault(species)
 162         );
 163     }
 164 
 165     @SuppressWarnings("unchecked")
 166     @ForceInline
 167     private <F> Vector<F> castDefault(Species<F> s) {
 168         int limit = s.length();
 169 
 170         Class<?> stype = s.elementType();
 171         if (stype == byte.class) {
 172             byte[] a = new byte[limit];
 173             for (int i = 0; i < limit; i++) {
 174                 a[i] = (byte) this.get(i);
 175             }
 176             return (Vector) ByteVector.fromArray((ByteVector.ByteSpecies) s, a, 0);
 177         } else if (stype == short.class) {
 178             short[] a = new short[limit];
 179             for (int i = 0; i < limit; i++) {
 180                 a[i] = (short) this.get(i);
 181             }
 182             return (Vector) ShortVector.fromArray((ShortVector.ShortSpecies) s, a, 0);
 183         } else if (stype == int.class) {
 184             int[] a = new int[limit];
 185             for (int i = 0; i < limit; i++) {
 186                 a[i] = (int) this.get(i);
 187             }
 188             return (Vector) IntVector.fromArray((IntVector.IntSpecies) s, a, 0);
 189         } else if (stype == long.class) {
 190             long[] a = new long[limit];
 191             for (int i = 0; i < limit; i++) {
 192                 a[i] = (long) this.get(i);
 193             }
 194             return (Vector) LongVector.fromArray((LongVector.LongSpecies) s, a, 0);
 195         } else if (stype == float.class) {
 196             float[] a = new float[limit];
 197             for (int i = 0; i < limit; i++) {
 198                 a[i] = (float) this.get(i);
 199             }
 200             return (Vector) FloatVector.fromArray((FloatVector.FloatSpecies) s, a, 0);
 201         } else if (stype == double.class) {
 202             double[] a = new double[limit];
 203             for (int i = 0; i < limit; i++) {
 204                 a[i] = (double) this.get(i);
 205             }
 206             return (Vector) DoubleVector.fromArray((DoubleVector.DoubleSpecies) s, a, 0);
 207         } else {
 208             throw new UnsupportedOperationException("Bad lane type for casting.");
 209         }
 210     }
 211 
 212     @Override
 213     @ForceInline
 214     @SuppressWarnings("unchecked")
 215     public <F> Vector<F> reinterpret(Species<F> s) {
 216         Objects.requireNonNull(s);
 217 
 218         if(s.elementType().equals(short.class)) {
 219             return (Vector<F>) reshape((Species<Short>)s);
 220         }
 221         if(s.bitSize() == bitSize()) {
 222             return reinterpretType(s);
 223         }
 224 
 225         return defaultReinterpret(s);
 226     }


 276                 (species, vector) -> vector.defaultReinterpret(species)
 277             );
 278         } else if (stype == double.class) {
 279             return VectorIntrinsics.reinterpret(
 280                 ShortMaxVector.class,
 281                 short.class, LENGTH,
 282                 DoubleMaxVector.class,
 283                 double.class, DoubleMaxVector.LENGTH,
 284                 this, s,
 285                 (species, vector) -> vector.defaultReinterpret(species)
 286             );
 287         } else {
 288             throw new UnsupportedOperationException("Bad lane type for casting.");
 289         }
 290     }
 291 
 292     @Override
 293     @ForceInline
 294     public ShortVector reshape(Species<Short> s) {
 295         Objects.requireNonNull(s);
 296         if (s.bitSize() == 64 && (s instanceof Short64Vector.Short64Species)) {
 297             Short64Vector.Short64Species ts = (Short64Vector.Short64Species)s;
 298             return VectorIntrinsics.reinterpret(
 299                 ShortMaxVector.class,
 300                 short.class, LENGTH,
 301                 Short64Vector.class,
 302                 short.class, Short64Vector.LENGTH,
 303                 this, ts,
 304                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 305             );
 306         } else if (s.bitSize() == 128 && (s instanceof Short128Vector.Short128Species)) {
 307             Short128Vector.Short128Species ts = (Short128Vector.Short128Species)s;
 308             return VectorIntrinsics.reinterpret(
 309                 ShortMaxVector.class,
 310                 short.class, LENGTH,
 311                 Short128Vector.class,
 312                 short.class, Short128Vector.LENGTH,
 313                 this, ts,
 314                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 315             );
 316         } else if (s.bitSize() == 256 && (s instanceof Short256Vector.Short256Species)) {
 317             Short256Vector.Short256Species ts = (Short256Vector.Short256Species)s;
 318             return VectorIntrinsics.reinterpret(
 319                 ShortMaxVector.class,
 320                 short.class, LENGTH,
 321                 Short256Vector.class,
 322                 short.class, Short256Vector.LENGTH,
 323                 this, ts,
 324                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 325             );
 326         } else if (s.bitSize() == 512 && (s instanceof Short512Vector.Short512Species)) {
 327             Short512Vector.Short512Species ts = (Short512Vector.Short512Species)s;
 328             return VectorIntrinsics.reinterpret(
 329                 ShortMaxVector.class,
 330                 short.class, LENGTH,
 331                 Short512Vector.class,
 332                 short.class, Short512Vector.LENGTH,
 333                 this, ts,
 334                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 335             );
 336         } else if ((s.bitSize() > 0) && (s.bitSize() <= 2048)
 337                 && (s.bitSize() % 128 == 0) && (s instanceof ShortMaxVector.ShortMaxSpecies)) {
 338             ShortMaxVector.ShortMaxSpecies ts = (ShortMaxVector.ShortMaxSpecies)s;
 339             return VectorIntrinsics.reinterpret(
 340                 ShortMaxVector.class,
 341                 short.class, LENGTH,
 342                 ShortMaxVector.class,
 343                 short.class, ShortMaxVector.LENGTH,
 344                 this, ts,
 345                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 346             );
 347         } else {
 348             throw new InternalError("Unimplemented size");
 349         }
 350     }
 351 
 352     // Binary operations with scalars
 353 
 354     @Override
 355     @ForceInline
 356     public ShortVector add(short o) {
 357         return add(SPECIES.broadcast(o));
 358     }
 359 
 360     @Override
 361     @ForceInline
 362     public ShortVector add(short o, Mask<Short> m) {
 363         return add(SPECIES.broadcast(o), m);
 364     }
 365 
 366     @Override
 367     @ForceInline
 368     public ShortVector sub(short o) {
 369         return sub(SPECIES.broadcast(o));
 370     }
 371 
 372     @Override
 373     @ForceInline
 374     public ShortVector sub(short o, Mask<Short> m) {
 375         return sub(SPECIES.broadcast(o), m);
 376     }
 377 
 378     @Override
 379     @ForceInline
 380     public ShortVector mul(short o) {
 381         return mul(SPECIES.broadcast(o));
 382     }
 383 
 384     @Override
 385     @ForceInline
 386     public ShortVector mul(short o, Mask<Short> m) {
 387         return mul(SPECIES.broadcast(o), m);
 388     }
 389 
 390     @Override
 391     @ForceInline
 392     public ShortVector min(short o) {
 393         return min(SPECIES.broadcast(o));
 394     }
 395 
 396     @Override
 397     @ForceInline
 398     public ShortVector max(short o) {
 399         return max(SPECIES.broadcast(o));
 400     }
 401 
 402     @Override
 403     @ForceInline
 404     public Mask<Short> equal(short o) {
 405         return equal(SPECIES.broadcast(o));
 406     }
 407 
 408     @Override
 409     @ForceInline
 410     public Mask<Short> notEqual(short o) {
 411         return notEqual(SPECIES.broadcast(o));
 412     }
 413 
 414     @Override
 415     @ForceInline
 416     public Mask<Short> lessThan(short o) {
 417         return lessThan(SPECIES.broadcast(o));
 418     }
 419 
 420     @Override
 421     @ForceInline
 422     public Mask<Short> lessThanEq(short o) {
 423         return lessThanEq(SPECIES.broadcast(o));
 424     }
 425 
 426     @Override
 427     @ForceInline
 428     public Mask<Short> greaterThan(short o) {
 429         return greaterThan(SPECIES.broadcast(o));
 430     }
 431 
 432     @Override
 433     @ForceInline
 434     public Mask<Short> greaterThanEq(short o) {
 435         return greaterThanEq(SPECIES.broadcast(o));
 436     }
 437 
 438     @Override
 439     @ForceInline
 440     public ShortVector blend(short o, Mask<Short> m) {
 441         return blend(SPECIES.broadcast(o), m);
 442     }
 443 
 444 
 445     @Override
 446     @ForceInline
 447     public ShortVector and(short o) {
 448         return and(SPECIES.broadcast(o));
 449     }
 450 
 451     @Override
 452     @ForceInline
 453     public ShortVector and(short o, Mask<Short> m) {
 454         return and(SPECIES.broadcast(o), m);
 455     }
 456 
 457     @Override
 458     @ForceInline
 459     public ShortVector or(short o) {
 460         return or(SPECIES.broadcast(o));
 461     }
 462 
 463     @Override
 464     @ForceInline
 465     public ShortVector or(short o, Mask<Short> m) {
 466         return or(SPECIES.broadcast(o), m);
 467     }
 468 
 469     @Override
 470     @ForceInline
 471     public ShortVector xor(short o) {
 472         return xor(SPECIES.broadcast(o));
 473     }
 474 
 475     @Override
 476     @ForceInline
 477     public ShortVector xor(short o, Mask<Short> m) {
 478         return xor(SPECIES.broadcast(o), m);
 479     }
 480 
 481     @Override
 482     @ForceInline
 483     public ShortMaxVector neg() {
 484         return (ShortMaxVector)zero(SPECIES).sub(this);
 485     }
 486 
 487     // Unary operations
 488 
 489     @ForceInline
 490     @Override
 491     public ShortMaxVector neg(Mask<Short> m) {
 492         return blend(neg(), m);
 493     }
 494 
 495     @Override
 496     @ForceInline
 497     public ShortMaxVector abs() {
 498         return VectorIntrinsics.unaryOp(


 713     @ForceInline
 714     public short addAll() {
 715         return (short) VectorIntrinsics.reductionCoerced(
 716             VECTOR_OP_ADD, ShortMaxVector.class, short.class, LENGTH,
 717             this,
 718             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a + b)));
 719     }
 720 
 721     @Override
 722     @ForceInline
 723     public short andAll() {
 724         return (short) VectorIntrinsics.reductionCoerced(
 725             VECTOR_OP_AND, ShortMaxVector.class, short.class, LENGTH,
 726             this,
 727             v -> (long) v.rOp((short) -1, (i, a, b) -> (short) (a & b)));
 728     }
 729 
 730     @Override
 731     @ForceInline
 732     public short andAll(Mask<Short> m) {
 733         return SPECIES.broadcast((short) -1).blend(this, m).andAll();
 734     }
 735 
 736     @Override
 737     @ForceInline
 738     public short minAll() {
 739         return (short) VectorIntrinsics.reductionCoerced(
 740             VECTOR_OP_MIN, ShortMaxVector.class, short.class, LENGTH,
 741             this,
 742             v -> (long) v.rOp(Short.MAX_VALUE , (i, a, b) -> (short) Math.min(a, b)));
 743     }
 744 
 745     @Override
 746     @ForceInline
 747     public short maxAll() {
 748         return (short) VectorIntrinsics.reductionCoerced(
 749             VECTOR_OP_MAX, ShortMaxVector.class, short.class, LENGTH,
 750             this,
 751             v -> (long) v.rOp(Short.MIN_VALUE , (i, a, b) -> (short) Math.max(a, b)));
 752     }
 753 


 755     @ForceInline
 756     public short mulAll() {
 757         return (short) VectorIntrinsics.reductionCoerced(
 758             VECTOR_OP_MUL, ShortMaxVector.class, short.class, LENGTH,
 759             this,
 760             v -> (long) v.rOp((short) 1, (i, a, b) -> (short) (a * b)));
 761     }
 762 
 763     @Override
 764     @ForceInline
 765     public short orAll() {
 766         return (short) VectorIntrinsics.reductionCoerced(
 767             VECTOR_OP_OR, ShortMaxVector.class, short.class, LENGTH,
 768             this,
 769             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a | b)));
 770     }
 771 
 772     @Override
 773     @ForceInline
 774     public short orAll(Mask<Short> m) {
 775         return SPECIES.broadcast((short) 0).blend(this, m).orAll();
 776     }
 777 
 778     @Override
 779     @ForceInline
 780     public short xorAll() {
 781         return (short) VectorIntrinsics.reductionCoerced(
 782             VECTOR_OP_XOR, ShortMaxVector.class, short.class, LENGTH,
 783             this,
 784             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a ^ b)));
 785     }
 786 
 787     @Override
 788     @ForceInline
 789     public short xorAll(Mask<Short> m) {
 790         return SPECIES.broadcast((short) 0).blend(this, m).xorAll();
 791     }
 792 
 793 
 794     @Override
 795     @ForceInline
 796     public short addAll(Mask<Short> m) {
 797         return SPECIES.broadcast((short) 0).blend(this, m).addAll();
 798     }
 799 
 800 
 801     @Override
 802     @ForceInline
 803     public short mulAll(Mask<Short> m) {
 804         return SPECIES.broadcast((short) 1).blend(this, m).mulAll();
 805     }
 806 
 807     @Override
 808     @ForceInline
 809     public short minAll(Mask<Short> m) {
 810         return SPECIES.broadcast(Short.MAX_VALUE).blend(this, m).minAll();
 811     }
 812 
 813     @Override
 814     @ForceInline
 815     public short maxAll(Mask<Short> m) {
 816         return SPECIES.broadcast(Short.MIN_VALUE).blend(this, m).maxAll();
 817     }
 818 
 819     @Override
 820     @ForceInline
 821     public Shuffle<Short> toShuffle() {
 822         short[] a = toArray();
 823         int[] sa = new int[a.length];
 824         for (int i = 0; i < a.length; i++) {
 825             sa[i] = (int) a[i];
 826         }
 827         return ShortVector.shuffleFromArray(SPECIES, sa, 0);
 828     }
 829 
 830     // Memory operations
 831 
 832     private static final int ARRAY_SHIFT         = 31 - Integer.numberOfLeadingZeros(Unsafe.ARRAY_SHORT_INDEX_SCALE);
 833     private static final int BOOLEAN_ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(Unsafe.ARRAY_BOOLEAN_INDEX_SCALE);
 834 
 835     @Override
 836     @ForceInline


1180             boolean[] res = new boolean[species().length()];
1181             boolean[] bits = getBits();
1182             for (int i = 0; i < species().length(); i++) {
1183                 res[i] = f.apply(i, bits[i]);
1184             }
1185             return new ShortMaxMask(res);
1186         }
1187 
1188         @Override
1189         ShortMaxMask bOp(Mask<Short> o, MBinOp f) {
1190             boolean[] res = new boolean[species().length()];
1191             boolean[] bits = getBits();
1192             boolean[] mbits = ((ShortMaxMask)o).getBits();
1193             for (int i = 0; i < species().length(); i++) {
1194                 res[i] = f.apply(i, bits[i], mbits[i]);
1195             }
1196             return new ShortMaxMask(res);
1197         }
1198 
1199         @Override
1200         public ShortMaxSpecies species() {
1201             return SPECIES;
1202         }
1203 
1204         @Override
1205         public ShortMaxVector toVector() {
1206             short[] res = new short[species().length()];
1207             boolean[] bits = getBits();
1208             for (int i = 0; i < species().length(); i++) {
1209                 // -1 will result in the most significant bit being set in
1210                 // addition to some or all other bits
1211                 res[i] = (short) (bits[i] ? -1 : 0);
1212             }
1213             return new ShortMaxVector(res);
1214         }
1215 

























1216         // Unary operations
1217 
1218         @Override
1219         @ForceInline
1220         public ShortMaxMask not() {
1221             return (ShortMaxMask) VectorIntrinsics.unaryOp(
1222                                              VECTOR_OP_NOT, ShortMaxMask.class, short.class, LENGTH,
1223                                              this,
1224                                              (m1) -> m1.uOp((i, a) -> !a));
1225         }
1226 
1227         // Binary operations
1228 
1229         @Override
1230         @ForceInline
1231         public ShortMaxMask and(Mask<Short> o) {
1232             Objects.requireNonNull(o);
1233             ShortMaxMask m = (ShortMaxMask)o;
1234             return VectorIntrinsics.binaryOp(VECTOR_OP_AND, ShortMaxMask.class, short.class, LENGTH,
1235                                              this, m,


1268     // Shuffle
1269 
1270     static final class ShortMaxShuffle extends AbstractShuffle<Short> {
1271         ShortMaxShuffle(byte[] reorder) {
1272             super(reorder);
1273         }
1274 
1275         public ShortMaxShuffle(int[] reorder) {
1276             super(reorder);
1277         }
1278 
1279         public ShortMaxShuffle(int[] reorder, int i) {
1280             super(reorder, i);
1281         }
1282 
1283         public ShortMaxShuffle(IntUnaryOperator f) {
1284             super(f);
1285         }
1286 
1287         @Override
1288         public ShortMaxSpecies species() {
1289             return SPECIES;
1290         }
1291 
1292         @Override
1293         public ShortVector toVector() {
1294             short[] va = new short[SPECIES.length()];
1295             for (int i = 0; i < va.length; i++) {
1296               va[i] = (short) getElement(i);
1297             }
1298             return ShortVector.fromArray(SPECIES, va, 0);
1299         }
1300 
1301         @Override

























1302         public ShortMaxShuffle rearrange(Vector.Shuffle<Short> o) {
1303             ShortMaxShuffle s = (ShortMaxShuffle) o;
1304             byte[] r = new byte[reorder.length];
1305             for (int i = 0; i < reorder.length; i++) {
1306                 r[i] = reorder[s.reorder[i]];
1307             }
1308             return new ShortMaxShuffle(r);
1309         }
1310     }
1311 
1312     // Species
1313 
1314     @Override
1315     public ShortMaxSpecies species() {
1316         return SPECIES;
1317     }
1318 
1319     static final class ShortMaxSpecies extends ShortSpecies {
1320         static final int BIT_SIZE = Shape.S_Max_BIT.bitSize();
1321 
1322         static final int LENGTH = BIT_SIZE / Short.SIZE;
1323 
1324         @Override
1325         public String toString() {
1326            StringBuilder sb = new StringBuilder("Shape[");
1327            sb.append(bitSize()).append(" bits, ");
1328            sb.append(length()).append(" ").append(short.class.getSimpleName()).append("s x ");
1329            sb.append(elementSize()).append(" bits");
1330            sb.append("]");
1331            return sb.toString();
1332         }
1333 
1334         @Override
1335         @ForceInline
1336         public int bitSize() {
1337             return BIT_SIZE;
1338         }
1339 
1340         @Override
1341         @ForceInline
1342         public int length() {
1343             return LENGTH;
1344         }
1345 
1346         @Override
1347         @ForceInline
1348         public Class<Short> elementType() {
1349             return short.class;
1350         }
1351 
1352         @Override
1353         @ForceInline
1354         public Class<?> boxType() {
1355             return ShortMaxVector.class;
1356         }
1357 
1358         @Override
1359         @ForceInline
1360         public Class<?> maskType() {
1361             return ShortMaxMask.class;
1362         }
1363 
1364         @Override
1365         @ForceInline
1366         public int elementSize() {
1367             return Short.SIZE;
1368         }
1369 
1370         @Override
1371         @ForceInline
1372         @SuppressWarnings("unchecked")
1373         Class<?> vectorType() {
1374             return ShortMaxVector.class;
1375         }
1376 
1377         @Override
1378         @ForceInline
1379         public Shape shape() {
1380             return Shape.S_Max_BIT;
1381         }
1382 
1383         @Override
1384         ShortMaxVector op(FOp f) {
1385             short[] res = new short[length()];
1386             for (int i = 0; i < length(); i++) {
1387                 res[i] = f.apply(i);
1388             }
1389             return new ShortMaxVector(res);
1390         }
1391 
1392         @Override
1393         ShortMaxVector op(Mask<Short> o, FOp f) {
1394             short[] res = new short[length()];
1395             boolean[] mbits = ((ShortMaxMask)o).getBits();
1396             for (int i = 0; i < length(); i++) {
1397                 if (mbits[i]) {
1398                     res[i] = f.apply(i);
1399                 }
1400             }
1401             return new ShortMaxVector(res);
1402         }
1403 
1404         @Override
1405         ShortMaxMask opm(FOpm f) {
1406             boolean[] res = new boolean[length()];
1407             for (int i = 0; i < length(); i++) {
1408                 res[i] = (boolean)f.apply(i);
1409             }
1410             return new ShortMaxMask(res);
1411         }
1412 
1413         // Factories
1414 
1415         @Override
1416         @ForceInline
1417         public ShortMaxVector zero() {
1418             return VectorIntrinsics.broadcastCoerced(ShortMaxVector.class, short.class, LENGTH,
1419                                                      0, SPECIES,
1420                                                      ((bits, s) -> ((ShortMaxSpecies)s).op(i -> (short)bits)));
1421         }
1422 
1423         @Override
1424         @ForceInline
1425         public ShortMaxVector broadcast(short e) {
1426             return VectorIntrinsics.broadcastCoerced(
1427                 ShortMaxVector.class, short.class, LENGTH,
1428                 e, SPECIES,
1429                 ((bits, s) -> ((ShortMaxSpecies)s).op(i -> (short)bits)));
1430         }
1431 
1432         @Override
1433         @ForceInline
1434         public ShortMaxVector scalars(short... es) {
1435             Objects.requireNonNull(es);
1436             int ix = VectorIntrinsics.checkIndex(0, es.length, LENGTH);
1437             return VectorIntrinsics.load(ShortMaxVector.class, short.class, LENGTH,
1438                                          es, Unsafe.ARRAY_SHORT_BASE_OFFSET,
1439                                          es, ix, SPECIES,
1440                                          (c, idx, s) -> ((ShortMaxSpecies)s).op(n -> c[idx + n]));
1441         }
1442 
1443         @Override
1444         @ForceInline
1445         public <E> ShortMaxMask cast(Mask<E> m) {
1446             if (m.length() != LENGTH)
1447                 throw new IllegalArgumentException("Mask length this species length differ");
1448             return new ShortMaxMask(m.toArray());
1449         }
1450 
1451         @Override
1452         @ForceInline
1453         public <E> ShortMaxShuffle cast(Shuffle<E> s) {
1454             if (s.length() != LENGTH)
1455                 throw new IllegalArgumentException("Shuffle length this species length differ");
1456             return new ShortMaxShuffle(s.toArray());
1457         }
1458     }
1459 }


  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have
  23  * questions.
  24  */
  25 package jdk.incubator.vector;
  26 
  27 import java.nio.ByteBuffer;
  28 import java.nio.ByteOrder;
  29 import java.nio.ShortBuffer;
  30 import java.nio.ReadOnlyBufferException;
  31 import java.util.Arrays;
  32 import java.util.Objects;
  33 import java.util.function.IntUnaryOperator;
  34 
  35 import jdk.internal.misc.Unsafe;
  36 import jdk.internal.vm.annotation.ForceInline;
  37 import static jdk.incubator.vector.VectorIntrinsics.*;
  38 
  39 @SuppressWarnings("cast")
  40 final class ShortMaxVector extends ShortVector {
  41     private static final Species<Short> SPECIES = ShortVector.SPECIES_MAX;
  42 
  43     static final ShortMaxVector ZERO = new ShortMaxVector();
  44 
  45     static final int LENGTH = SPECIES.length();
  46 
  47     private final short[] vec; // Don't access directly, use getElements() instead.
  48 
  49     private short[] getElements() {
  50         return VectorIntrinsics.maybeRebox(this).vec;
  51     }
  52 
  53     ShortMaxVector() {
  54         vec = new short[SPECIES.length()];
  55     }
  56 
  57     ShortMaxVector(short[] v) {
  58         vec = v;
  59     }
  60 
  61     @Override


 138 
 139     @Override
 140     short rOp(short v, FBinOp f) {
 141         short[] vec = getElements();
 142         for (int i = 0; i < length(); i++) {
 143             v = f.apply(i, v, vec[i]);
 144         }
 145         return v;
 146     }
 147 
 148     @Override
 149     @ForceInline
 150     public <F> Vector<F> cast(Species<F> s) {
 151         Objects.requireNonNull(s);
 152         if (s.length() != LENGTH)
 153             throw new IllegalArgumentException("Vector length this species length differ");
 154 
 155         return VectorIntrinsics.cast(
 156             ShortMaxVector.class,
 157             short.class, LENGTH,
 158             s.boxType(),
 159             s.elementType(), LENGTH,
 160             this, s,
 161             (species, vector) -> vector.castDefault(species)
 162         );
 163     }
 164 
 165     @SuppressWarnings("unchecked")
 166     @ForceInline
 167     private <F> Vector<F> castDefault(Species<F> s) {
 168         int limit = s.length();
 169 
 170         Class<?> stype = s.elementType();
 171         if (stype == byte.class) {
 172             byte[] a = new byte[limit];
 173             for (int i = 0; i < limit; i++) {
 174                 a[i] = (byte) this.get(i);
 175             }
 176             return (Vector) ByteVector.fromArray((Species<Byte>) s, a, 0);
 177         } else if (stype == short.class) {
 178             short[] a = new short[limit];
 179             for (int i = 0; i < limit; i++) {
 180                 a[i] = (short) this.get(i);
 181             }
 182             return (Vector) ShortVector.fromArray((Species<Short>) s, a, 0);
 183         } else if (stype == int.class) {
 184             int[] a = new int[limit];
 185             for (int i = 0; i < limit; i++) {
 186                 a[i] = (int) this.get(i);
 187             }
 188             return (Vector) IntVector.fromArray((Species<Integer>) s, a, 0);
 189         } else if (stype == long.class) {
 190             long[] a = new long[limit];
 191             for (int i = 0; i < limit; i++) {
 192                 a[i] = (long) this.get(i);
 193             }
 194             return (Vector) LongVector.fromArray((Species<Long>) s, a, 0);
 195         } else if (stype == float.class) {
 196             float[] a = new float[limit];
 197             for (int i = 0; i < limit; i++) {
 198                 a[i] = (float) this.get(i);
 199             }
 200             return (Vector) FloatVector.fromArray((Species<Float>) s, a, 0);
 201         } else if (stype == double.class) {
 202             double[] a = new double[limit];
 203             for (int i = 0; i < limit; i++) {
 204                 a[i] = (double) this.get(i);
 205             }
 206             return (Vector) DoubleVector.fromArray((Species<Double>) s, a, 0);
 207         } else {
 208             throw new UnsupportedOperationException("Bad lane type for casting.");
 209         }
 210     }
 211 
 212     @Override
 213     @ForceInline
 214     @SuppressWarnings("unchecked")
 215     public <F> Vector<F> reinterpret(Species<F> s) {
 216         Objects.requireNonNull(s);
 217 
 218         if(s.elementType().equals(short.class)) {
 219             return (Vector<F>) reshape((Species<Short>)s);
 220         }
 221         if(s.bitSize() == bitSize()) {
 222             return reinterpretType(s);
 223         }
 224 
 225         return defaultReinterpret(s);
 226     }


 276                 (species, vector) -> vector.defaultReinterpret(species)
 277             );
 278         } else if (stype == double.class) {
 279             return VectorIntrinsics.reinterpret(
 280                 ShortMaxVector.class,
 281                 short.class, LENGTH,
 282                 DoubleMaxVector.class,
 283                 double.class, DoubleMaxVector.LENGTH,
 284                 this, s,
 285                 (species, vector) -> vector.defaultReinterpret(species)
 286             );
 287         } else {
 288             throw new UnsupportedOperationException("Bad lane type for casting.");
 289         }
 290     }
 291 
 292     @Override
 293     @ForceInline
 294     public ShortVector reshape(Species<Short> s) {
 295         Objects.requireNonNull(s);
 296         if (s.bitSize() == 64 && (s.boxType() == Short64Vector.class)) {

 297             return VectorIntrinsics.reinterpret(
 298                 ShortMaxVector.class,
 299                 short.class, LENGTH,
 300                 Short64Vector.class,
 301                 short.class, Short64Vector.LENGTH,
 302                 this, s,
 303                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 304             );
 305         } else if (s.bitSize() == 128 && (s.boxType() == Short128Vector.class)) {

 306             return VectorIntrinsics.reinterpret(
 307                 ShortMaxVector.class,
 308                 short.class, LENGTH,
 309                 Short128Vector.class,
 310                 short.class, Short128Vector.LENGTH,
 311                 this, s,
 312                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 313             );
 314         } else if (s.bitSize() == 256 && (s.boxType() == Short256Vector.class)) {

 315             return VectorIntrinsics.reinterpret(
 316                 ShortMaxVector.class,
 317                 short.class, LENGTH,
 318                 Short256Vector.class,
 319                 short.class, Short256Vector.LENGTH,
 320                 this, s,
 321                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 322             );
 323         } else if (s.bitSize() == 512 && (s.boxType() == Short512Vector.class)) {

 324             return VectorIntrinsics.reinterpret(
 325                 ShortMaxVector.class,
 326                 short.class, LENGTH,
 327                 Short512Vector.class,
 328                 short.class, Short512Vector.LENGTH,
 329                 this, s,
 330                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 331             );
 332         } else if ((s.bitSize() > 0) && (s.bitSize() <= 2048)
 333                 && (s.bitSize() % 128 == 0) && (s.boxType() == ShortMaxVector.class)) {

 334             return VectorIntrinsics.reinterpret(
 335                 ShortMaxVector.class,
 336                 short.class, LENGTH,
 337                 ShortMaxVector.class,
 338                 short.class, ShortMaxVector.LENGTH,
 339                 this, s,
 340                 (species, vector) -> (ShortVector) vector.defaultReinterpret(species)
 341             );
 342         } else {
 343             throw new InternalError("Unimplemented size");
 344         }
 345     }
 346 
 347     // Binary operations with scalars
 348 
 349     @Override
 350     @ForceInline
 351     public ShortVector add(short o) {
 352         return add((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 353     }
 354 
 355     @Override
 356     @ForceInline
 357     public ShortVector add(short o, Mask<Short> m) {
 358         return add((ShortMaxVector)ShortVector.broadcast(SPECIES, o), m);
 359     }
 360 
 361     @Override
 362     @ForceInline
 363     public ShortVector sub(short o) {
 364         return sub((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 365     }
 366 
 367     @Override
 368     @ForceInline
 369     public ShortVector sub(short o, Mask<Short> m) {
 370         return sub((ShortMaxVector)ShortVector.broadcast(SPECIES, o), m);
 371     }
 372 
 373     @Override
 374     @ForceInline
 375     public ShortVector mul(short o) {
 376         return mul((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 377     }
 378 
 379     @Override
 380     @ForceInline
 381     public ShortVector mul(short o, Mask<Short> m) {
 382         return mul((ShortMaxVector)ShortVector.broadcast(SPECIES, o), m);
 383     }
 384 
 385     @Override
 386     @ForceInline
 387     public ShortVector min(short o) {
 388         return min((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 389     }
 390 
 391     @Override
 392     @ForceInline
 393     public ShortVector max(short o) {
 394         return max((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 395     }
 396 
 397     @Override
 398     @ForceInline
 399     public Mask<Short> equal(short o) {
 400         return equal((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 401     }
 402 
 403     @Override
 404     @ForceInline
 405     public Mask<Short> notEqual(short o) {
 406         return notEqual((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 407     }
 408 
 409     @Override
 410     @ForceInline
 411     public Mask<Short> lessThan(short o) {
 412         return lessThan((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 413     }
 414 
 415     @Override
 416     @ForceInline
 417     public Mask<Short> lessThanEq(short o) {
 418         return lessThanEq((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 419     }
 420 
 421     @Override
 422     @ForceInline
 423     public Mask<Short> greaterThan(short o) {
 424         return greaterThan((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 425     }
 426 
 427     @Override
 428     @ForceInline
 429     public Mask<Short> greaterThanEq(short o) {
 430         return greaterThanEq((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 431     }
 432 
 433     @Override
 434     @ForceInline
 435     public ShortVector blend(short o, Mask<Short> m) {
 436         return blend((ShortMaxVector)ShortVector.broadcast(SPECIES, o), m);
 437     }
 438 
 439 
 440     @Override
 441     @ForceInline
 442     public ShortVector and(short o) {
 443         return and((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 444     }
 445 
 446     @Override
 447     @ForceInline
 448     public ShortVector and(short o, Mask<Short> m) {
 449         return and((ShortMaxVector)ShortVector.broadcast(SPECIES, o), m);
 450     }
 451 
 452     @Override
 453     @ForceInline
 454     public ShortVector or(short o) {
 455         return or((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 456     }
 457 
 458     @Override
 459     @ForceInline
 460     public ShortVector or(short o, Mask<Short> m) {
 461         return or((ShortMaxVector)ShortVector.broadcast(SPECIES, o), m);
 462     }
 463 
 464     @Override
 465     @ForceInline
 466     public ShortVector xor(short o) {
 467         return xor((ShortMaxVector)ShortVector.broadcast(SPECIES, o));
 468     }
 469 
 470     @Override
 471     @ForceInline
 472     public ShortVector xor(short o, Mask<Short> m) {
 473         return xor((ShortMaxVector)ShortVector.broadcast(SPECIES, o), m);
 474     }
 475 
 476     @Override
 477     @ForceInline
 478     public ShortMaxVector neg() {
 479         return (ShortMaxVector)zero(SPECIES).sub(this);
 480     }
 481 
 482     // Unary operations
 483 
 484     @ForceInline
 485     @Override
 486     public ShortMaxVector neg(Mask<Short> m) {
 487         return blend(neg(), m);
 488     }
 489 
 490     @Override
 491     @ForceInline
 492     public ShortMaxVector abs() {
 493         return VectorIntrinsics.unaryOp(


 708     @ForceInline
 709     public short addAll() {
 710         return (short) VectorIntrinsics.reductionCoerced(
 711             VECTOR_OP_ADD, ShortMaxVector.class, short.class, LENGTH,
 712             this,
 713             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a + b)));
 714     }
 715 
 716     @Override
 717     @ForceInline
 718     public short andAll() {
 719         return (short) VectorIntrinsics.reductionCoerced(
 720             VECTOR_OP_AND, ShortMaxVector.class, short.class, LENGTH,
 721             this,
 722             v -> (long) v.rOp((short) -1, (i, a, b) -> (short) (a & b)));
 723     }
 724 
 725     @Override
 726     @ForceInline
 727     public short andAll(Mask<Short> m) {
 728         return blend((ShortMaxVector)ShortVector.broadcast(SPECIES, (short) -1), m).andAll();
 729     }
 730 
 731     @Override
 732     @ForceInline
 733     public short minAll() {
 734         return (short) VectorIntrinsics.reductionCoerced(
 735             VECTOR_OP_MIN, ShortMaxVector.class, short.class, LENGTH,
 736             this,
 737             v -> (long) v.rOp(Short.MAX_VALUE , (i, a, b) -> (short) Math.min(a, b)));
 738     }
 739 
 740     @Override
 741     @ForceInline
 742     public short maxAll() {
 743         return (short) VectorIntrinsics.reductionCoerced(
 744             VECTOR_OP_MAX, ShortMaxVector.class, short.class, LENGTH,
 745             this,
 746             v -> (long) v.rOp(Short.MIN_VALUE , (i, a, b) -> (short) Math.max(a, b)));
 747     }
 748 


 750     @ForceInline
 751     public short mulAll() {
 752         return (short) VectorIntrinsics.reductionCoerced(
 753             VECTOR_OP_MUL, ShortMaxVector.class, short.class, LENGTH,
 754             this,
 755             v -> (long) v.rOp((short) 1, (i, a, b) -> (short) (a * b)));
 756     }
 757 
 758     @Override
 759     @ForceInline
 760     public short orAll() {
 761         return (short) VectorIntrinsics.reductionCoerced(
 762             VECTOR_OP_OR, ShortMaxVector.class, short.class, LENGTH,
 763             this,
 764             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a | b)));
 765     }
 766 
 767     @Override
 768     @ForceInline
 769     public short orAll(Mask<Short> m) {
 770         return blend((ShortMaxVector)ShortVector.broadcast(SPECIES, (short) 0), m).orAll();
 771     }
 772 
 773     @Override
 774     @ForceInline
 775     public short xorAll() {
 776         return (short) VectorIntrinsics.reductionCoerced(
 777             VECTOR_OP_XOR, ShortMaxVector.class, short.class, LENGTH,
 778             this,
 779             v -> (long) v.rOp((short) 0, (i, a, b) -> (short) (a ^ b)));
 780     }
 781 
 782     @Override
 783     @ForceInline
 784     public short xorAll(Mask<Short> m) {
 785         return blend((ShortMaxVector)ShortVector.broadcast(SPECIES, (short) 0), m).xorAll();
 786     }
 787 
 788 
 789     @Override
 790     @ForceInline
 791     public short addAll(Mask<Short> m) {
 792         return blend((ShortMaxVector)ShortVector.broadcast(SPECIES, (short) 0), m).addAll();
 793     }
 794 
 795 
 796     @Override
 797     @ForceInline
 798     public short mulAll(Mask<Short> m) {
 799         return blend((ShortMaxVector)ShortVector.broadcast(SPECIES, (short) 1), m).mulAll();
 800     }
 801 
 802     @Override
 803     @ForceInline
 804     public short minAll(Mask<Short> m) {
 805         return blend((ShortMaxVector)ShortVector.broadcast(SPECIES, Short.MAX_VALUE), m).minAll();
 806     }
 807 
 808     @Override
 809     @ForceInline
 810     public short maxAll(Mask<Short> m) {
 811         return blend((ShortMaxVector)ShortVector.broadcast(SPECIES, Short.MIN_VALUE), m).maxAll();
 812     }
 813 
 814     @Override
 815     @ForceInline
 816     public Shuffle<Short> toShuffle() {
 817         short[] a = toArray();
 818         int[] sa = new int[a.length];
 819         for (int i = 0; i < a.length; i++) {
 820             sa[i] = (int) a[i];
 821         }
 822         return ShortVector.shuffleFromArray(SPECIES, sa, 0);
 823     }
 824 
 825     // Memory operations
 826 
 827     private static final int ARRAY_SHIFT         = 31 - Integer.numberOfLeadingZeros(Unsafe.ARRAY_SHORT_INDEX_SCALE);
 828     private static final int BOOLEAN_ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(Unsafe.ARRAY_BOOLEAN_INDEX_SCALE);
 829 
 830     @Override
 831     @ForceInline


1175             boolean[] res = new boolean[species().length()];
1176             boolean[] bits = getBits();
1177             for (int i = 0; i < species().length(); i++) {
1178                 res[i] = f.apply(i, bits[i]);
1179             }
1180             return new ShortMaxMask(res);
1181         }
1182 
1183         @Override
1184         ShortMaxMask bOp(Mask<Short> o, MBinOp f) {
1185             boolean[] res = new boolean[species().length()];
1186             boolean[] bits = getBits();
1187             boolean[] mbits = ((ShortMaxMask)o).getBits();
1188             for (int i = 0; i < species().length(); i++) {
1189                 res[i] = f.apply(i, bits[i], mbits[i]);
1190             }
1191             return new ShortMaxMask(res);
1192         }
1193 
1194         @Override
1195         public Species<Short> species() {
1196             return SPECIES;
1197         }
1198 
1199         @Override
1200         public ShortMaxVector toVector() {
1201             short[] res = new short[species().length()];
1202             boolean[] bits = getBits();
1203             for (int i = 0; i < species().length(); i++) {
1204                 // -1 will result in the most significant bit being set in
1205                 // addition to some or all other bits
1206                 res[i] = (short) (bits[i] ? -1 : 0);
1207             }
1208             return new ShortMaxVector(res);
1209         }
1210 
1211         @Override
1212         @ForceInline
1213         @SuppressWarnings("unchecked")
1214         public <E> Mask<E> cast(Species<E> species) {
1215             if (length() != species.length())
1216                 throw new IllegalArgumentException("Mask length and species length differ");
1217             Class<?> stype = species.elementType();
1218             boolean [] maskArray = toArray();
1219             if (stype == byte.class) {
1220                 return (Mask <E>) new ByteMaxVector.ByteMaxMask(maskArray);
1221             } else if (stype == short.class) {
1222                 return (Mask <E>) new ShortMaxVector.ShortMaxMask(maskArray);
1223             } else if (stype == int.class) {
1224                 return (Mask <E>) new IntMaxVector.IntMaxMask(maskArray);
1225             } else if (stype == long.class) {
1226                 return (Mask <E>) new LongMaxVector.LongMaxMask(maskArray);
1227             } else if (stype == float.class) {
1228                 return (Mask <E>) new FloatMaxVector.FloatMaxMask(maskArray);
1229             } else if (stype == double.class) {
1230                 return (Mask <E>) new DoubleMaxVector.DoubleMaxMask(maskArray);
1231             } else {
1232                 throw new UnsupportedOperationException("Bad lane type for casting.");
1233             }
1234         }
1235 
1236         // Unary operations
1237 
1238         @Override
1239         @ForceInline
1240         public ShortMaxMask not() {
1241             return (ShortMaxMask) VectorIntrinsics.unaryOp(
1242                                              VECTOR_OP_NOT, ShortMaxMask.class, short.class, LENGTH,
1243                                              this,
1244                                              (m1) -> m1.uOp((i, a) -> !a));
1245         }
1246 
1247         // Binary operations
1248 
1249         @Override
1250         @ForceInline
1251         public ShortMaxMask and(Mask<Short> o) {
1252             Objects.requireNonNull(o);
1253             ShortMaxMask m = (ShortMaxMask)o;
1254             return VectorIntrinsics.binaryOp(VECTOR_OP_AND, ShortMaxMask.class, short.class, LENGTH,
1255                                              this, m,


1288     // Shuffle
1289 
1290     static final class ShortMaxShuffle extends AbstractShuffle<Short> {
1291         ShortMaxShuffle(byte[] reorder) {
1292             super(reorder);
1293         }
1294 
1295         public ShortMaxShuffle(int[] reorder) {
1296             super(reorder);
1297         }
1298 
1299         public ShortMaxShuffle(int[] reorder, int i) {
1300             super(reorder, i);
1301         }
1302 
1303         public ShortMaxShuffle(IntUnaryOperator f) {
1304             super(f);
1305         }
1306 
1307         @Override
1308         public Species<Short> species() {
1309             return SPECIES;
1310         }
1311 
1312         @Override
1313         public ShortVector toVector() {
1314             short[] va = new short[SPECIES.length()];
1315             for (int i = 0; i < va.length; i++) {
1316               va[i] = (short) getElement(i);
1317             }
1318             return ShortVector.fromArray(SPECIES, va, 0);
1319         }
1320 
1321         @Override
1322         @ForceInline
1323         @SuppressWarnings("unchecked")
1324         public <F> Shuffle<F> cast(Species<F> species) {
1325             if (length() != species.length())
1326                 throw new IllegalArgumentException("Shuffle length and species length differ");
1327             Class<?> stype = species.elementType();
1328             int [] shuffleArray = toArray();
1329             if (stype == byte.class) {
1330                 return (Shuffle<F>) new ByteMaxVector.ByteMaxShuffle(shuffleArray);
1331             } else if (stype == short.class) {
1332                 return (Shuffle<F>) new ShortMaxVector.ShortMaxShuffle(shuffleArray);
1333             } else if (stype == int.class) {
1334                 return (Shuffle<F>) new IntMaxVector.IntMaxShuffle(shuffleArray);
1335             } else if (stype == long.class) {
1336                 return (Shuffle<F>) new LongMaxVector.LongMaxShuffle(shuffleArray);
1337             } else if (stype == float.class) {
1338                 return (Shuffle<F>) new FloatMaxVector.FloatMaxShuffle(shuffleArray);
1339             } else if (stype == double.class) {
1340                 return (Shuffle<F>) new DoubleMaxVector.DoubleMaxShuffle(shuffleArray);
1341             } else {
1342                 throw new UnsupportedOperationException("Bad lane type for casting.");
1343             }
1344         }
1345 
1346         @Override
1347         public ShortMaxShuffle rearrange(Vector.Shuffle<Short> o) {
1348             ShortMaxShuffle s = (ShortMaxShuffle) o;
1349             byte[] r = new byte[reorder.length];
1350             for (int i = 0; i < reorder.length; i++) {
1351                 r[i] = reorder[s.reorder[i]];
1352             }
1353             return new ShortMaxShuffle(r);
1354         }
1355     }
1356 
1357     // Species
1358 
1359     @Override
1360     public Species<Short> species() {
1361         return SPECIES;













































































































































1362     }
1363 }
< prev index next >