--- old/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/FloatVector.java 2019-04-09 12:06:43.177876100 -0700 +++ new/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/FloatVector.java 2019-04-09 12:06:42.424465400 -0700 @@ -29,6 +29,7 @@ import java.nio.ByteOrder; import java.util.Objects; import java.util.function.IntUnaryOperator; +import java.util.function.Function; import java.util.concurrent.ThreadLocalRandom; import jdk.internal.misc.Unsafe; @@ -110,8 +111,10 @@ */ @ForceInline @SuppressWarnings("unchecked") - public static FloatVector zero(FloatSpecies species) { - return species.zero(); + public static FloatVector zero(Species species) { + return VectorIntrinsics.broadcastCoerced((Class) species.boxType(), float.class, species.length(), + Float.floatToIntBits(0.0f), species, + ((bits, s) -> ((FloatSpecies)s).op(i -> Float.intBitsToFloat((int)bits)))); } /** @@ -122,7 +125,7 @@ *

* This method behaves as if it returns the result of calling the * byte buffer, offset, and mask accepting - * {@link #fromByteBuffer(FloatSpecies, ByteBuffer, int, Mask) method} as follows: + * {@link #fromByteBuffer(Species, ByteBuffer, int, Mask) method} as follows: *

{@code
      * return this.fromByteBuffer(ByteBuffer.wrap(a), i, this.maskAllTrue());
      * }
@@ -136,7 +139,7 @@ */ @ForceInline @SuppressWarnings("unchecked") - public static FloatVector fromByteArray(FloatSpecies species, byte[] a, int ix) { + public static FloatVector fromByteArray(Species species, byte[] a, int ix) { Objects.requireNonNull(a); ix = VectorIntrinsics.checkIndex(ix, a.length, species.bitSize() / Byte.SIZE); return VectorIntrinsics.load((Class) species.boxType(), float.class, species.length(), @@ -158,7 +161,7 @@ *

* This method behaves as if it returns the result of calling the * byte buffer, offset, and mask accepting - * {@link #fromByteBuffer(FloatSpecies, ByteBuffer, int, Mask) method} as follows: + * {@link #fromByteBuffer(Species, ByteBuffer, int, Mask) method} as follows: *

{@code
      * return this.fromByteBuffer(ByteBuffer.wrap(a), i, m);
      * }
@@ -177,7 +180,7 @@ * {@code i >= a.length - (N * this.elementSize() / Byte.SIZE)} */ @ForceInline - public static FloatVector fromByteArray(FloatSpecies species, byte[] a, int ix, Mask m) { + public static FloatVector fromByteArray(Species species, byte[] a, int ix, Mask m) { return zero(species).blend(fromByteArray(species, a, ix), m); } @@ -197,7 +200,7 @@ */ @ForceInline @SuppressWarnings("unchecked") - public static FloatVector fromArray(FloatSpecies species, float[] a, int i){ + public static FloatVector fromArray(Species species, float[] a, int i){ Objects.requireNonNull(a); i = VectorIntrinsics.checkIndex(i, a.length, species.length()); return VectorIntrinsics.load((Class) species.boxType(), float.class, species.length(), @@ -226,7 +229,7 @@ * is set {@code i > a.length - N} */ @ForceInline - public static FloatVector fromArray(FloatSpecies species, float[] a, int i, Mask m) { + public static FloatVector fromArray(Species species, float[] a, int i, Mask m) { return zero(species).blend(fromArray(species, a, i), m); } @@ -253,20 +256,21 @@ */ @ForceInline @SuppressWarnings("unchecked") - public static FloatVector fromArray(FloatSpecies species, float[] a, int i, int[] indexMap, int j) { + public static FloatVector fromArray(Species species, float[] a, int i, int[] indexMap, int j) { Objects.requireNonNull(a); Objects.requireNonNull(indexMap); - // Index vector: vix[0:n] = k -> i + indexMap[j + i] - IntVector vix = IntVector.fromArray(species.indexSpecies(), indexMap, j).add(i); + // Index vector: vix[0:n] = k -> i + indexMap[j + k] + IntVector vix = IntVector.fromArray(IntVector.species(species.indexShape()), indexMap, j).add(i); vix = VectorIntrinsics.checkIndex(vix, a.length); return VectorIntrinsics.loadWithMap((Class) species.boxType(), float.class, species.length(), - species.indexSpecies().vectorType(), a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, vix, + IntVector.species(species.indexShape()).boxType(), a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, vix, a, i, indexMap, j, species, - (c, idx, iMap, idy, s) -> ((FloatSpecies)s).op(n -> c[idx + iMap[idy+n]])); + (float[] c, int idx, int[] iMap, int idy, Species s) -> + ((FloatSpecies)s).op(n -> c[idx + iMap[idy+n]])); } /** @@ -295,7 +299,7 @@ */ @ForceInline @SuppressWarnings("unchecked") - public static FloatVector fromArray(FloatSpecies species, float[] a, int i, Mask m, int[] indexMap, int j) { + public static FloatVector fromArray(Species species, float[] a, int i, Mask m, int[] indexMap, int j) { // @@@ This can result in out of bounds errors for unset mask lanes return zero(species).blend(fromArray(species, a, i, indexMap, j), m); } @@ -310,7 +314,7 @@ *

* This method behaves as if it returns the result of calling the * byte buffer, offset, and mask accepting - * {@link #fromByteBuffer(FloatSpecies, ByteBuffer, int, Mask)} method} as follows: + * {@link #fromByteBuffer(Species, ByteBuffer, int, Mask)} method} as follows: *

{@code
      *   return this.fromByteBuffer(b, i, this.maskAllTrue())
      * }
@@ -327,7 +331,7 @@ */ @ForceInline @SuppressWarnings("unchecked") - public static FloatVector fromByteBuffer(FloatSpecies species, ByteBuffer bb, int ix) { + public static FloatVector fromByteBuffer(Species species, ByteBuffer bb, int ix) { if (bb.order() != ByteOrder.nativeOrder()) { throw new IllegalArgumentException(); } @@ -379,11 +383,85 @@ * {@code i >= b.limit() - (N * this.elementSize() / Byte.SIZE)} */ @ForceInline - public static FloatVector fromByteBuffer(FloatSpecies species, ByteBuffer bb, int ix, Mask m) { + public static FloatVector fromByteBuffer(Species species, ByteBuffer bb, int ix, Mask m) { return zero(species).blend(fromByteBuffer(species, bb, ix), m); } /** + * Returns a vector where all lane elements are set to the primitive + * value {@code e}. + * + * @param s species of the desired vector + * @param e the value + * @return a vector of vector where all lane elements are set to + * the primitive value {@code e} + */ + @ForceInline + @SuppressWarnings("unchecked") + public static FloatVector broadcast(Species s, float e) { + return VectorIntrinsics.broadcastCoerced( + (Class) s.boxType(), float.class, s.length(), + Float.floatToIntBits(e), s, + ((bits, sp) -> ((FloatSpecies)sp).op(i -> Float.intBitsToFloat((int)bits)))); + } + + /** + * Returns a vector where each lane element is set to a given + * primitive value. + *

+ * For each vector lane, where {@code N} is the vector lane index, the + * the primitive value at index {@code N} is placed into the resulting + * vector at lane index {@code N}. + * + * @param s species of the desired vector + * @param es the given primitive values + * @return a vector where each lane element is set to a given primitive + * value + * @throws IndexOutOfBoundsException if {@code es.length < this.length()} + */ + @ForceInline + @SuppressWarnings("unchecked") + public static FloatVector scalars(Species s, float... es) { + Objects.requireNonNull(es); + int ix = VectorIntrinsics.checkIndex(0, es.length, s.length()); + return VectorIntrinsics.load((Class) s.boxType(), float.class, s.length(), + es, Unsafe.ARRAY_FLOAT_BASE_OFFSET, + es, ix, s, + (c, idx, sp) -> ((FloatSpecies)sp).op(n -> c[idx + n])); + } + + /** + * Returns a vector where the first lane element is set to the primtive + * value {@code e}, all other lane elements are set to the default + * value. + * + * @param s species of the desired vector + * @param e the value + * @return a vector where the first lane element is set to the primitive + * value {@code e} + */ + @ForceInline + public static final FloatVector single(Species s, float e) { + return zero(s).with(0, e); + } + + /** + * Returns a vector where each lane element is set to a randomly + * generated primitive value. + * + * The semantics are equivalent to calling + * {@link ThreadLocalRandom#nextFloat()} + * + * @param s species of the desired vector + * @return a vector where each lane elements is set to a randomly + * generated primitive value + */ + public static FloatVector random(Species s) { + ThreadLocalRandom r = ThreadLocalRandom.current(); + return ((FloatSpecies)s).op(i -> r.nextFloat()); + } + + /** * Returns a mask where each lane is set or unset according to given * {@code boolean} values *

@@ -397,7 +475,7 @@ * @throws IndexOutOfBoundsException if {@code bits.length < species.length()} */ @ForceInline - public static Mask maskFromValues(FloatSpecies species, boolean... bits) { + public static Mask maskFromValues(Species species, boolean... bits) { if (species.boxType() == FloatMaxVector.class) return new FloatMaxVector.FloatMaxMask(bits); switch (species.bitSize()) { @@ -410,7 +488,7 @@ } // @@@ This is a bad implementation -- makes lambdas capturing -- fix this - static Mask trueMask(FloatSpecies species) { + static Mask trueMask(Species species) { if (species.boxType() == FloatMaxVector.class) return FloatMaxVector.FloatMaxMask.TRUE_MASK; switch (species.bitSize()) { @@ -422,7 +500,7 @@ } } - static Mask falseMask(FloatSpecies species) { + static Mask falseMask(Species species) { if (species.boxType() == FloatMaxVector.class) return FloatMaxVector.FloatMaxMask.FALSE_MASK; switch (species.bitSize()) { @@ -450,7 +528,7 @@ */ @ForceInline @SuppressWarnings("unchecked") - public static Mask maskFromArray(FloatSpecies species, boolean[] bits, int ix) { + public static Mask maskFromArray(Species species, boolean[] bits, int ix) { Objects.requireNonNull(bits); ix = VectorIntrinsics.checkIndex(ix, bits.length, species.length()); return VectorIntrinsics.load((Class>) species.maskType(), int.class, species.length(), @@ -467,10 +545,10 @@ */ @ForceInline @SuppressWarnings("unchecked") - public static Mask maskAllTrue(FloatSpecies species) { + public static Mask maskAllTrue(Species species) { return VectorIntrinsics.broadcastCoerced((Class>) species.maskType(), int.class, species.length(), (int)-1, species, - ((z, s) -> trueMask((FloatSpecies)s))); + ((z, s) -> trueMask(s))); } /** @@ -481,10 +559,10 @@ */ @ForceInline @SuppressWarnings("unchecked") - public static Mask maskAllFalse(FloatSpecies species) { + public static Mask maskAllFalse(Species species) { return VectorIntrinsics.broadcastCoerced((Class>) species.maskType(), int.class, species.length(), 0, species, - ((z, s) -> falseMask((FloatSpecies)s))); + ((z, s) -> falseMask(s))); } /** @@ -512,7 +590,7 @@ * @return a shuffle of mapped indexes */ @ForceInline - public static Shuffle shuffle(FloatSpecies species, IntUnaryOperator f) { + public static Shuffle shuffle(Species species, IntUnaryOperator f) { if (species.boxType() == FloatMaxVector.class) return new FloatMaxVector.FloatMaxShuffle(f); switch (species.bitSize()) { @@ -538,7 +616,7 @@ * @return a shuffle of lane indexes */ @ForceInline - public static Shuffle shuffleIota(FloatSpecies species) { + public static Shuffle shuffleIota(Species species) { if (species.boxType() == FloatMaxVector.class) return new FloatMaxVector.FloatMaxShuffle(AbstractShuffle.IDENTITY); switch (species.bitSize()) { @@ -567,7 +645,7 @@ * {@code < species.length()} */ @ForceInline - public static Shuffle shuffleFromValues(FloatSpecies species, int... ixs) { + public static Shuffle shuffleFromValues(Species species, int... ixs) { if (species.boxType() == FloatMaxVector.class) return new FloatMaxVector.FloatMaxShuffle(ixs); switch (species.bitSize()) { @@ -595,7 +673,7 @@ * {@code i > a.length - species.length()} */ @ForceInline - public static Shuffle shuffleFromArray(FloatSpecies species, int[] ixs, int i) { + public static Shuffle shuffleFromArray(Species species, int[] ixs, int i) { if (species.boxType() == FloatMaxVector.class) return new FloatMaxVector.FloatMaxShuffle(ixs, i); switch (species.bitSize()) { @@ -607,7 +685,6 @@ } } - // Ops @Override @@ -2019,87 +2096,59 @@ // Species @Override - public abstract FloatSpecies species(); + public abstract Species species(); /** * Class representing {@link FloatVector}'s of the same {@link Vector.Shape Shape}. */ - public static abstract class FloatSpecies extends Vector.Species { + static final class FloatSpecies extends Vector.AbstractSpecies { + final Function vectorFactory; + final Function> maskFactory; + + private FloatSpecies(Vector.Shape shape, + Class boxType, + Class maskType, + Function vectorFactory, + Function> maskFactory) { + super(shape, float.class, Float.SIZE, boxType, maskType); + this.vectorFactory = vectorFactory; + this.maskFactory = maskFactory; + } + interface FOp { float apply(int i); } - abstract FloatVector op(FOp f); - - abstract FloatVector op(Mask m, FOp f); - interface FOpm { boolean apply(int i); } - abstract Mask opm(FOpm f); - - abstract IntVector.IntSpecies indexSpecies(); - - - // Factories - - @Override - public abstract FloatVector zero(); - - /** - * Returns a vector where all lane elements are set to the primitive - * value {@code e}. - * - * @param e the value - * @return a vector of vector where all lane elements are set to - * the primitive value {@code e} - */ - public abstract FloatVector broadcast(float e); - - /** - * Returns a vector where the first lane element is set to the primtive - * value {@code e}, all other lane elements are set to the default - * value. - * - * @param e the value - * @return a vector where the first lane element is set to the primitive - * value {@code e} - */ - @ForceInline - public final FloatVector single(float e) { - return zero().with(0, e); + FloatVector op(FOp f) { + float[] res = new float[length()]; + for (int i = 0; i < length(); i++) { + res[i] = f.apply(i); + } + return vectorFactory.apply(res); } - /** - * Returns a vector where each lane element is set to a randomly - * generated primitive value. - * - * The semantics are equivalent to calling - * {@code ThreadLocalRandom#nextFloat}. - * - * @return a vector where each lane elements is set to a randomly - * generated primitive value - */ - public FloatVector random() { - ThreadLocalRandom r = ThreadLocalRandom.current(); - return op(i -> r.nextFloat()); + FloatVector op(Vector.Mask o, FOp f) { + float[] res = new float[length()]; + boolean[] mbits = ((AbstractMask)o).getBits(); + for (int i = 0; i < length(); i++) { + if (mbits[i]) { + res[i] = f.apply(i); + } + } + return vectorFactory.apply(res); } - /** - * Returns a vector where each lane element is set to a given - * primitive value. - *

- * For each vector lane, where {@code N} is the vector lane index, the - * the primitive value at index {@code N} is placed into the resulting - * vector at lane index {@code N}. - * - * @param es the given primitive values - * @return a vector where each lane element is set to a given primitive - * value - * @throws IndexOutOfBoundsException if {@code es.length < this.length()} - */ - public abstract FloatVector scalars(float... es); + Vector.Mask opm(IntVector.IntSpecies.FOpm f) { + boolean[] res = new boolean[length()]; + for (int i = 0; i < length(); i++) { + res[i] = (boolean)f.apply(i); + } + return maskFactory.apply(res); + } } /** @@ -2112,8 +2161,7 @@ * * @return the preferred species for an element type of {@code float} */ - @SuppressWarnings("unchecked") - public static FloatSpecies preferredSpecies() { + private static FloatSpecies preferredSpecies() { return (FloatSpecies) Species.ofPreferred(float.class); } @@ -2124,16 +2172,41 @@ * @return a species for an element type of {@code float} and shape * @throws IllegalArgumentException if no such species exists for the shape */ - @SuppressWarnings("unchecked") - public static FloatSpecies species(Vector.Shape s) { + static FloatSpecies species(Vector.Shape s) { Objects.requireNonNull(s); switch (s) { - case S_64_BIT: return Float64Vector.SPECIES; - case S_128_BIT: return Float128Vector.SPECIES; - case S_256_BIT: return Float256Vector.SPECIES; - case S_512_BIT: return Float512Vector.SPECIES; - case S_Max_BIT: return FloatMaxVector.SPECIES; + case S_64_BIT: return (FloatSpecies) SPECIES_64; + case S_128_BIT: return (FloatSpecies) SPECIES_128; + case S_256_BIT: return (FloatSpecies) SPECIES_256; + case S_512_BIT: return (FloatSpecies) SPECIES_512; + case S_Max_BIT: return (FloatSpecies) SPECIES_MAX; default: throw new IllegalArgumentException("Bad shape: " + s); } } + + /** Species representing {@link FloatVector}s of {@link Vector.Shape#S_64_BIT Shape.S_64_BIT}. */ + public static final Species SPECIES_64 = new FloatSpecies(Shape.S_64_BIT, Float64Vector.class, Float64Vector.Float64Mask.class, + Float64Vector::new, Float64Vector.Float64Mask::new); + + /** Species representing {@link FloatVector}s of {@link Vector.Shape#S_128_BIT Shape.S_128_BIT}. */ + public static final Species SPECIES_128 = new FloatSpecies(Shape.S_128_BIT, Float128Vector.class, Float128Vector.Float128Mask.class, + Float128Vector::new, Float128Vector.Float128Mask::new); + + /** Species representing {@link FloatVector}s of {@link Vector.Shape#S_256_BIT Shape.S_256_BIT}. */ + public static final Species SPECIES_256 = new FloatSpecies(Shape.S_256_BIT, Float256Vector.class, Float256Vector.Float256Mask.class, + Float256Vector::new, Float256Vector.Float256Mask::new); + + /** Species representing {@link FloatVector}s of {@link Vector.Shape#S_512_BIT Shape.S_512_BIT}. */ + public static final Species SPECIES_512 = new FloatSpecies(Shape.S_512_BIT, Float512Vector.class, Float512Vector.Float512Mask.class, + Float512Vector::new, Float512Vector.Float512Mask::new); + + /** Species representing {@link FloatVector}s of {@link Vector.Shape#S_Max_BIT Shape.S_Max_BIT}. */ + public static final Species SPECIES_MAX = new FloatSpecies(Shape.S_Max_BIT, FloatMaxVector.class, FloatMaxVector.FloatMaxMask.class, + FloatMaxVector::new, FloatMaxVector.FloatMaxMask::new); + + /** + * Preferred species for {@link FloatVector}s. + * A preferred species is a species of maximal bit size for the platform. + */ + public static final Species SPECIES_PREFERRED = (Species) preferredSpecies(); }