package jdk.incubator.vector;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.IntUnaryOperator;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.vector.VectorSupport;
import static jdk.internal.vm.vector.VectorSupport.*;
import static jdk.incubator.vector.VectorOperators.*;
@SuppressWarnings("cast")
final class Int256Vector extends IntVector {
static final IntSpecies VSPECIES =
(IntSpecies) IntVector.SPECIES_256;
static final VectorShape VSHAPE =
VSPECIES.vectorShape();
static final Class<Int256Vector> VCLASS = Int256Vector.class;
static final int VSIZE = VSPECIES.vectorBitSize();
static final int VLENGTH = VSPECIES.laneCount();
static final Class<Integer> ETYPE = int.class;
Int256Vector(int[] v) {
super(v);
}
Int256Vector(Object v) {
this((int[]) v);
}
static final Int256Vector ZERO = new Int256Vector(new int[VLENGTH]);
static final Int256Vector IOTA = new Int256Vector(VSPECIES.iotaArray());
static {
VSPECIES.dummyVector();
VSPECIES.withLanes(LaneType.BYTE);
}
@ForceInline
final @Override
public IntSpecies vspecies() {
return VSPECIES;
}
@ForceInline
@Override
public final Class<Integer> elementType() { return int.class; }
@ForceInline
@Override
public final int elementSize() { return Integer.SIZE; }
@ForceInline
@Override
public final VectorShape shape() { return VSHAPE; }
@ForceInline
@Override
public final int length() { return VLENGTH; }
@ForceInline
@Override
public final int bitSize() { return VSIZE; }
@ForceInline
@Override
public final int byteSize() { return VSIZE / Byte.SIZE; }
@ForceInline
final @Override
int[] vec() {
return (int[])getPayload();
}
@Override
@ForceInline
public final Int256Vector broadcast(int e) {
return (Int256Vector) super.broadcastTemplate(e);
}
@Override
@ForceInline
public final Int256Vector broadcast(long e) {
return (Int256Vector) super.broadcastTemplate(e);
}
@Override
@ForceInline
Int256Mask maskFromArray(boolean[] bits) {
return new Int256Mask(bits);
}
@Override
@ForceInline
Int256Shuffle iotaShuffle() { return Int256Shuffle.IOTA; }
@ForceInline
Int256Shuffle iotaShuffle(int start, int step, boolean wrap) {
if (wrap) {
return (Int256Shuffle)VectorSupport.shuffleIota(ETYPE, Int256Shuffle.class, VSPECIES, VLENGTH, start, step, 1,
(l, lstart, lstep, s) -> s.shuffleFromOp(i -> (VectorIntrinsics.wrapToRange(i*lstep + lstart, l))));
} else {
return (Int256Shuffle)VectorSupport.shuffleIota(ETYPE, Int256Shuffle.class, VSPECIES, VLENGTH, start, step, 0,
(l, lstart, lstep, s) -> s.shuffleFromOp(i -> (i*lstep + lstart)));
}
}
@Override
@ForceInline
Int256Shuffle shuffleFromBytes(byte[] reorder) { return new Int256Shuffle(reorder); }
@Override
@ForceInline
Int256Shuffle shuffleFromArray(int[] indexes, int i) { return new Int256Shuffle(indexes, i); }
@Override
@ForceInline
Int256Shuffle shuffleFromOp(IntUnaryOperator fn) { return new Int256Shuffle(fn); }
@ForceInline
final @Override
Int256Vector vectorFactory(int[] vec) {
return new Int256Vector(vec);
}
@ForceInline
final @Override
Byte256Vector asByteVectorRaw() {
return (Byte256Vector) super.asByteVectorRawTemplate();
}
@ForceInline
final @Override
AbstractVector<?> asVectorRaw(LaneType laneType) {
return super.asVectorRawTemplate(laneType);
}
@ForceInline
final @Override
Int256Vector uOp(FUnOp f) {
return (Int256Vector) super.uOpTemplate(f);
}
@ForceInline
final @Override
Int256Vector uOp(VectorMask<Integer> m, FUnOp f) {
return (Int256Vector)
super.uOpTemplate((Int256Mask)m, f);
}
@ForceInline
final @Override
Int256Vector bOp(Vector<Integer> v, FBinOp f) {
return (Int256Vector) super.bOpTemplate((Int256Vector)v, f);
}
@ForceInline
final @Override
Int256Vector bOp(Vector<Integer> v,
VectorMask<Integer> m, FBinOp f) {
return (Int256Vector)
super.bOpTemplate((Int256Vector)v, (Int256Mask)m,
f);
}
@ForceInline
final @Override
Int256Vector tOp(Vector<Integer> v1, Vector<Integer> v2, FTriOp f) {
return (Int256Vector)
super.tOpTemplate((Int256Vector)v1, (Int256Vector)v2,
f);
}
@ForceInline
final @Override
Int256Vector tOp(Vector<Integer> v1, Vector<Integer> v2,
VectorMask<Integer> m, FTriOp f) {
return (Int256Vector)
super.tOpTemplate((Int256Vector)v1, (Int256Vector)v2,
(Int256Mask)m, f);
}
@ForceInline
final @Override
int rOp(int v, FBinOp f) {
return super.rOpTemplate(v, f);
}
@Override
@ForceInline
public final <F>
Vector<F> convertShape(VectorOperators.Conversion<Integer,F> conv,
VectorSpecies<F> rsp, int part) {
return super.convertShapeTemplate(conv, rsp, part);
}
@Override
@ForceInline
public final <F>
Vector<F> reinterpretShape(VectorSpecies<F> toSpecies, int part) {
return super.reinterpretShapeTemplate(toSpecies, part);
}
@Override
@ForceInline
public Int256Vector lanewise(Unary op) {
return (Int256Vector) super.lanewiseTemplate(op);
}
@Override
@ForceInline
public Int256Vector lanewise(Binary op, Vector<Integer> v) {
return (Int256Vector) super.lanewiseTemplate(op, v);
}
@Override
@ForceInline Int256Vector
lanewiseShift(VectorOperators.Binary op, int e) {
return (Int256Vector) super.lanewiseShiftTemplate(op, e);
}
@Override
@ForceInline
public final
Int256Vector
lanewise(VectorOperators.Ternary op, Vector<Integer> v1, Vector<Integer> v2) {
return (Int256Vector) super.lanewiseTemplate(op, v1, v2);
}
@Override
@ForceInline
public final
Int256Vector addIndex(int scale) {
return (Int256Vector) super.addIndexTemplate(scale);
}
@Override
@ForceInline
public final int reduceLanes(VectorOperators.Associative op) {
return super.reduceLanesTemplate(op);
}
@Override
@ForceInline
public final int reduceLanes(VectorOperators.Associative op,
VectorMask<Integer> m) {
return super.reduceLanesTemplate(op, m);
}
@Override
@ForceInline
public final long reduceLanesToLong(VectorOperators.Associative op) {
return (long) super.reduceLanesTemplate(op);
}
@Override
@ForceInline
public final long reduceLanesToLong(VectorOperators.Associative op,
VectorMask<Integer> m) {
return (long) super.reduceLanesTemplate(op, m);
}
@Override
@ForceInline
public VectorShuffle<Integer> toShuffle() {
int[] a = toArray();
int[] sa = new int[a.length];
for (int i = 0; i < a.length; i++) {
sa[i] = (int) a[i];
}
return VectorShuffle.fromArray(VSPECIES, sa, 0);
}
@Override
@ForceInline
public final Int256Mask test(Test op) {
return super.testTemplate(Int256Mask.class, op);
}
@Override
@ForceInline
public final Int256Mask compare(Comparison op, Vector<Integer> v) {
return super.compareTemplate(Int256Mask.class, op, v);
}
@Override
@ForceInline
public final Int256Mask compare(Comparison op, int s) {
return super.compareTemplate(Int256Mask.class, op, s);
}
@Override
@ForceInline
public final Int256Mask compare(Comparison op, long s) {
return super.compareTemplate(Int256Mask.class, op, s);
}
@Override
@ForceInline
public Int256Vector blend(Vector<Integer> v, VectorMask<Integer> m) {
return (Int256Vector)
super.blendTemplate(Int256Mask.class,
(Int256Vector) v,
(Int256Mask) m);
}
@Override
@ForceInline
public Int256Vector slice(int origin, Vector<Integer> v) {
return (Int256Vector) super.sliceTemplate(origin, v);
}
@Override
@ForceInline
public Int256Vector slice(int origin) {
if ((origin < 0) || (origin >= VLENGTH)) {
throw new ArrayIndexOutOfBoundsException("Index " + origin + " out of bounds for vector length " + VLENGTH);
} else {
Int256Shuffle Iota = iotaShuffle();
VectorMask<Integer> BlendMask = Iota.toVector().compare(VectorOperators.LT, (broadcast((int)(VLENGTH-origin))));
Iota = iotaShuffle(origin, 1, true);
return ZERO.blend(this.rearrange(Iota), BlendMask);
}
}
@Override
@ForceInline
public Int256Vector unslice(int origin, Vector<Integer> w, int part) {
return (Int256Vector) super.unsliceTemplate(origin, w, part);
}
@Override
@ForceInline
public Int256Vector unslice(int origin, Vector<Integer> w, int part, VectorMask<Integer> m) {
return (Int256Vector)
super.unsliceTemplate(Int256Mask.class,
origin, w, part,
(Int256Mask) m);
}
@Override
@ForceInline
public Int256Vector unslice(int origin) {
if ((origin < 0) || (origin >= VLENGTH)) {
throw new ArrayIndexOutOfBoundsException("Index " + origin + " out of bounds for vector length " + VLENGTH);
} else {
Int256Shuffle Iota = iotaShuffle();
VectorMask<Integer> BlendMask = Iota.toVector().compare(VectorOperators.GE, (broadcast((int)(origin))));
Iota = iotaShuffle(-origin, 1, true);
return ZERO.blend(this.rearrange(Iota), BlendMask);
}
}
@Override
@ForceInline
public Int256Vector rearrange(VectorShuffle<Integer> s) {
return (Int256Vector)
super.rearrangeTemplate(Int256Shuffle.class,
(Int256Shuffle) s);
}
@Override
@ForceInline
public Int256Vector rearrange(VectorShuffle<Integer> shuffle,
VectorMask<Integer> m) {
return (Int256Vector)
super.rearrangeTemplate(Int256Shuffle.class,
(Int256Shuffle) shuffle,
(Int256Mask) m);
}
@Override
@ForceInline
public Int256Vector rearrange(VectorShuffle<Integer> s,
Vector<Integer> v) {
return (Int256Vector)
super.rearrangeTemplate(Int256Shuffle.class,
(Int256Shuffle) s,
(Int256Vector) v);
}
@Override
@ForceInline
public Int256Vector selectFrom(Vector<Integer> v) {
return (Int256Vector)
super.selectFromTemplate((Int256Vector) v);
}
@Override
@ForceInline
public Int256Vector selectFrom(Vector<Integer> v,
VectorMask<Integer> m) {
return (Int256Vector)
super.selectFromTemplate((Int256Vector) v,
(Int256Mask) m);
}
@ForceInline
@Override
public int lane(int i) {
switch(i) {
case 0: return laneHelper(0);
case 1: return laneHelper(1);
case 2: return laneHelper(2);
case 3: return laneHelper(3);
case 4: return laneHelper(4);
case 5: return laneHelper(5);
case 6: return laneHelper(6);
case 7: return laneHelper(7);
default: throw new IllegalArgumentException("Index " + i + " must be zero or positive, and less than " + VLENGTH);
}
}
public int laneHelper(int i) {
return (int) VectorSupport.extract(
VCLASS, ETYPE, VLENGTH,
this, i,
(vec, ix) -> {
int[] vecarr = vec.vec();
return (long)vecarr[ix];
});
}
@ForceInline
@Override
public Int256Vector withLane(int i, int e) {
switch (i) {
case 0: return withLaneHelper(0, e);
case 1: return withLaneHelper(1, e);
case 2: return withLaneHelper(2, e);
case 3: return withLaneHelper(3, e);
case 4: return withLaneHelper(4, e);
case 5: return withLaneHelper(5, e);
case 6: return withLaneHelper(6, e);
case 7: return withLaneHelper(7, e);
default: throw new IllegalArgumentException("Index " + i + " must be zero or positive, and less than " + VLENGTH);
}
}
public Int256Vector withLaneHelper(int i, int e) {
return VectorSupport.insert(
VCLASS, ETYPE, VLENGTH,
this, i, (long)e,
(v, ix, bits) -> {
int[] res = v.vec().clone();
res[ix] = (int)bits;
return v.vectorFactory(res);
});
}
static final class Int256Mask extends AbstractMask<Integer> {
static final int VLENGTH = VSPECIES.laneCount();
static final Class<Integer> ETYPE = int.class;
Int256Mask(boolean[] bits) {
this(bits, 0);
}
Int256Mask(boolean[] bits, int offset) {
super(prepare(bits, offset));
}
Int256Mask(boolean val) {
super(prepare(val));
}
private static boolean[] prepare(boolean[] bits, int offset) {
boolean[] newBits = new boolean[VSPECIES.laneCount()];
for (int i = 0; i < newBits.length; i++) {
newBits[i] = bits[offset + i];
}
return newBits;
}
private static boolean[] prepare(boolean val) {
boolean[] bits = new boolean[VSPECIES.laneCount()];
Arrays.fill(bits, val);
return bits;
}
@ForceInline
final @Override
public IntSpecies vspecies() {
return VSPECIES;
}
@ForceInline
boolean[] getBits() {
return (boolean[])getPayload();
}
@Override
Int256Mask uOp(MUnOp f) {
boolean[] res = new boolean[vspecies().laneCount()];
boolean[] bits = getBits();
for (int i = 0; i < res.length; i++) {
res[i] = f.apply(i, bits[i]);
}
return new Int256Mask(res);
}
@Override
Int256Mask bOp(VectorMask<Integer> m, MBinOp f) {
boolean[] res = new boolean[vspecies().laneCount()];
boolean[] bits = getBits();
boolean[] mbits = ((Int256Mask)m).getBits();
for (int i = 0; i < res.length; i++) {
res[i] = f.apply(i, bits[i], mbits[i]);
}
return new Int256Mask(res);
}
@ForceInline
@Override
public final
Int256Vector toVector() {
return (Int256Vector) super.toVectorTemplate();
}
@Override
@ForceInline
public <E> VectorMask<E> cast(VectorSpecies<E> s) {
AbstractSpecies<E> species = (AbstractSpecies<E>) s;
if (length() != species.laneCount())
throw new IllegalArgumentException("VectorMask length and species length differ");
boolean[] maskArray = toArray();
switch (species.laneType.switchKey) {
case LaneType.SK_BYTE:
return new Byte256Vector.Byte256Mask(maskArray).check(species);
case LaneType.SK_SHORT:
return new Short256Vector.Short256Mask(maskArray).check(species);
case LaneType.SK_INT:
return new Int256Vector.Int256Mask(maskArray).check(species);
case LaneType.SK_LONG:
return new Long256Vector.Long256Mask(maskArray).check(species);
case LaneType.SK_FLOAT:
return new Float256Vector.Float256Mask(maskArray).check(species);
case LaneType.SK_DOUBLE:
return new Double256Vector.Double256Mask(maskArray).check(species);
}
throw new AssertionError(species);
}
@Override
@ForceInline
public Int256Mask not() {
return xor(maskAll(true));
}
@Override
@ForceInline
public Int256Mask and(VectorMask<Integer> mask) {
Objects.requireNonNull(mask);
Int256Mask m = (Int256Mask)mask;
return VectorSupport.binaryOp(VECTOR_OP_AND, Int256Mask.class, int.class, VLENGTH,
this, m,
(m1, m2) -> m1.bOp(m2, (i, a, b) -> a & b));
}
@Override
@ForceInline
public Int256Mask or(VectorMask<Integer> mask) {
Objects.requireNonNull(mask);
Int256Mask m = (Int256Mask)mask;
return VectorSupport.binaryOp(VECTOR_OP_OR, Int256Mask.class, int.class, VLENGTH,
this, m,
(m1, m2) -> m1.bOp(m2, (i, a, b) -> a | b));
}
@ForceInline
Int256Mask xor(VectorMask<Integer> mask) {
Objects.requireNonNull(mask);
Int256Mask m = (Int256Mask)mask;
return VectorSupport.binaryOp(VECTOR_OP_XOR, Int256Mask.class, int.class, VLENGTH,
this, m,
(m1, m2) -> m1.bOp(m2, (i, a, b) -> a ^ b));
}
@Override
@ForceInline
public boolean anyTrue() {
return VectorSupport.test(BT_ne, Int256Mask.class, int.class, VLENGTH,
this, vspecies().maskAll(true),
(m, __) -> anyTrueHelper(((Int256Mask)m).getBits()));
}
@Override
@ForceInline
public boolean allTrue() {
return VectorSupport.test(BT_overflow, Int256Mask.class, int.class, VLENGTH,
this, vspecies().maskAll(true),
(m, __) -> allTrueHelper(((Int256Mask)m).getBits()));
}
@ForceInline
static Int256Mask maskAll(boolean bit) {
return VectorSupport.broadcastCoerced(Int256Mask.class, int.class, VLENGTH,
(bit ? -1 : 0), null,
(v, __) -> (v != 0 ? TRUE_MASK : FALSE_MASK));
}
private static final Int256Mask TRUE_MASK = new Int256Mask(true);
private static final Int256Mask FALSE_MASK = new Int256Mask(false);
}
static final class Int256Shuffle extends AbstractShuffle<Integer> {
static final int VLENGTH = VSPECIES.laneCount();
static final Class<Integer> ETYPE = int.class;
Int256Shuffle(byte[] reorder) {
super(VLENGTH, reorder);
}
public Int256Shuffle(int[] reorder) {
super(VLENGTH, reorder);
}
public Int256Shuffle(int[] reorder, int i) {
super(VLENGTH, reorder, i);
}
public Int256Shuffle(IntUnaryOperator fn) {
super(VLENGTH, fn);
}
@Override
public IntSpecies vspecies() {
return VSPECIES;
}
static {
assert(VLENGTH < Byte.MAX_VALUE);
assert(Byte.MIN_VALUE <= -VLENGTH);
}
static final Int256Shuffle IOTA = new Int256Shuffle(IDENTITY);
@Override
@ForceInline
public Int256Vector toVector() {
return VectorSupport.shuffleToVector(VCLASS, ETYPE, Int256Shuffle.class, this, VLENGTH,
(s) -> ((Int256Vector)(((AbstractShuffle<Integer>)(s)).toVectorTemplate())));
}
@Override
@ForceInline
public <F> VectorShuffle<F> cast(VectorSpecies<F> s) {
AbstractSpecies<F> species = (AbstractSpecies<F>) s;
if (length() != species.laneCount())
throw new IllegalArgumentException("VectorShuffle length and species length differ");
int[] shuffleArray = toArray();
switch (species.laneType.switchKey) {
case LaneType.SK_BYTE:
return new Byte256Vector.Byte256Shuffle(shuffleArray).check(species);
case LaneType.SK_SHORT:
return new Short256Vector.Short256Shuffle(shuffleArray).check(species);
case LaneType.SK_INT:
return new Int256Vector.Int256Shuffle(shuffleArray).check(species);
case LaneType.SK_LONG:
return new Long256Vector.Long256Shuffle(shuffleArray).check(species);
case LaneType.SK_FLOAT:
return new Float256Vector.Float256Shuffle(shuffleArray).check(species);
case LaneType.SK_DOUBLE:
return new Double256Vector.Double256Shuffle(shuffleArray).check(species);
}
throw new AssertionError(species);
}
@ForceInline
@Override
public Int256Shuffle rearrange(VectorShuffle<Integer> shuffle) {
Int256Shuffle s = (Int256Shuffle) shuffle;
byte[] reorder1 = reorder();
byte[] reorder2 = s.reorder();
byte[] r = new byte[reorder1.length];
for (int i = 0; i < reorder1.length; i++) {
int ssi = reorder2[i];
r[i] = reorder1[ssi];
}
return new Int256Shuffle(r);
}
}
@ForceInline
@Override
final
IntVector fromArray0(int[] a, int offset) {
return super.fromArray0Template(a, offset);
}
@ForceInline
@Override
final
IntVector fromByteArray0(byte[] a, int offset) {
return super.fromByteArray0Template(a, offset);
}
@ForceInline
@Override
final
IntVector fromByteBuffer0(ByteBuffer bb, int offset) {
return super.fromByteBuffer0Template(bb, offset);
}
@ForceInline
@Override
final
void intoArray0(int[] a, int offset) {
super.intoArray0Template(a, offset);
}
@ForceInline
@Override
final
void intoByteArray0(byte[] a, int offset) {
super.intoByteArray0Template(a, offset);
}
}