< prev index next >

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

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


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


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


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


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


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


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

























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


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

























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


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


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


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

 296             return VectorIntrinsics.reinterpret(
 297                 Byte128Vector.class,
 298                 byte.class, LENGTH,
 299                 Byte64Vector.class,
 300                 byte.class, Byte64Vector.LENGTH,
 301                 this, s,
 302                 (species, vector) -> (ByteVector) vector.defaultReinterpret(species)
 303             );
 304         } else if (s.bitSize() == 128 && (s.boxType() == Byte128Vector.class)) {

 305             return VectorIntrinsics.reinterpret(
 306                 Byte128Vector.class,
 307                 byte.class, LENGTH,
 308                 Byte128Vector.class,
 309                 byte.class, Byte128Vector.LENGTH,
 310                 this, s,
 311                 (species, vector) -> (ByteVector) vector.defaultReinterpret(species)
 312             );
 313         } else if (s.bitSize() == 256 && (s.boxType() == Byte256Vector.class)) {

 314             return VectorIntrinsics.reinterpret(
 315                 Byte128Vector.class,
 316                 byte.class, LENGTH,
 317                 Byte256Vector.class,
 318                 byte.class, Byte256Vector.LENGTH,
 319                 this, s,
 320                 (species, vector) -> (ByteVector) vector.defaultReinterpret(species)
 321             );
 322         } else if (s.bitSize() == 512 && (s.boxType() == Byte512Vector.class)) {

 323             return VectorIntrinsics.reinterpret(
 324                 Byte128Vector.class,
 325                 byte.class, LENGTH,
 326                 Byte512Vector.class,
 327                 byte.class, Byte512Vector.LENGTH,
 328                 this, s,
 329                 (species, vector) -> (ByteVector) vector.defaultReinterpret(species)
 330             );
 331         } else if ((s.bitSize() > 0) && (s.bitSize() <= 2048)
 332                 && (s.bitSize() % 128 == 0) && (s.boxType() == ByteMaxVector.class)) {

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


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


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


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


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













































































































































1361     }
1362 }
< prev index next >