[vector] Small patch to rationalize species/shape factories
Brian Goetz
brian.goetz at oracle.com
Tue Jan 29 21:41:51 UTC 2019
Please find attached a small patch to rationalize species/shape factory
methods. It compiles and tests cleanly against the last checkin for
which the tests pass. This is the patch for only the ungenerated
sources (for ease of review.)
It moves the species factories to Species.of() / Species.ofPreferred(),
and the Shape factory to Shape.forBitSize().
-------------- next part --------------
# HG changeset patch
# Parent d3b456d84b4ed3b6f4a15a09f9e26433b0d91bd5
diff -r d3b456d84b4e -r 8ab9aad380d0 src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Vector.java
--- a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Vector.java Wed Jan 09 22:09:11 2019 +0100
+++ b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Vector.java Tue Jan 29 16:15:44 2019 -0500
@@ -74,9 +74,9 @@
* factory called a {@link Species}. A Species has an
* element type and shape and creates Vector values of the same element type
* and shape.
- * A species can be {@link #species(Class, Shape)} obtained} given an element
+ * A species can be {@link Species#of(Class, Shape)} obtained} given an element
* type and shape, or a preferred species can be
- * {@link #preferredSpecies obtained} given just an element type where the most
+ * {@link Species#ofPreferred(Class)} obtained} given just an element type where the most
* optimal shape is selected for the current platform. It is recommended that
* Species instances be held in {@code static final} fields for optimal creation
* and usage of Vector values by the runtime compiler.
@@ -908,6 +908,32 @@
int length(Species<?> s) {
return bitSize() / s.elementSize();
}
+
+ /**
+ * Finds appropriate shape depending on bitsize.
+ *
+ * @param bitSize the size in bits
+ * @return the shape corresponding to bitsize
+ * @see #bitSize
+ */
+ public static Shape forBitSize(int bitSize) {
+ switch (bitSize) {
+ case 64:
+ return Shape.S_64_BIT;
+ case 128:
+ return Shape.S_128_BIT;
+ case 256:
+ return Shape.S_256_BIT;
+ case 512:
+ return Shape.S_512_BIT;
+ default:
+ if ((bitSize > 0) && (bitSize <= 2048) && (bitSize % 128 == 0)) {
+ return Shape.S_Max_BIT;
+ } else {
+ throw new IllegalArgumentException("Bad vector bit size: " + bitSize);
+ }
+ }
+ }
}
@@ -962,6 +988,65 @@
// Factory
/**
+ * Finds a species for an element type and shape.
+ *
+ * @param c the element type
+ * @param s the shape
+ * @param <E> the boxed element type
+ * @return a species for an element type and shape
+ * @throws IllegalArgumentException if no such species exists for the
+ * element type and/or shape
+ */
+ @SuppressWarnings("unchecked")
+ public static <E> Vector.Species<E> of(Class<E> c, Shape s) {
+ if (c == float.class) {
+ return (Vector.Species<E>) FloatVector.species(s);
+ }
+ else if (c == double.class) {
+ return (Vector.Species<E>) DoubleVector.species(s);
+ }
+ else if (c == byte.class) {
+ return (Vector.Species<E>) ByteVector.species(s);
+ }
+ else if (c == short.class) {
+ return (Vector.Species<E>) ShortVector.species(s);
+ }
+ else if (c == int.class) {
+ return (Vector.Species<E>) IntVector.species(s);
+ }
+ else if (c == long.class) {
+ return (Vector.Species<E>) LongVector.species(s);
+ }
+ else {
+ throw new IllegalArgumentException("Bad vector element type: " + c.getName());
+ }
+ }
+
+ /**
+ * Finds a preferred species for an element type.
+ * <p>
+ * A preferred species is a species chosen by the platform that has a
+ * shape of maximal bit size. A preferred species for different element
+ * types will have the same shape, and therefore vectors created from
+ * such species will be shape compatible.
+ *
+ * @param c the element type
+ * @param <E> the boxed element type
+ * @return a preferred species for an element type
+ * @throws IllegalArgumentException if no such species exists for the
+ * element type
+ */
+ @SuppressWarnings("unchecked")
+ public static <E> Vector.Species<E> ofPreferred(Class<E> c) {
+ Unsafe u = Unsafe.getUnsafe();
+
+ int vectorLength = u.getMaxVectorSize(c);
+ int vectorBitSize = bitSizeForVectorLength(c, vectorLength);
+ Shape s = Shape.forBitSize(vectorBitSize);
+ return Species.of(c, s);
+ }
+
+ /**
* Returns a vector where all lane elements are set to the default
* primitive value.
*
@@ -1726,30 +1811,6 @@
}
/**
- * Finds a preferred species for an element type.
- * <p>
- * A preferred species is a species chosen by the platform that has a
- * shape of maximal bit size. A preferred species for different element
- * types will have the same shape, and therefore vectors created from
- * such species will be shape compatible.
- *
- * @param c the element type
- * @param <E> the boxed element type
- * @return a preferred species for an element type
- * @throws IllegalArgumentException if no such species exists for the
- * element type
- */
- @SuppressWarnings("unchecked")
- public static <E> Vector.Species<E> preferredSpecies(Class<E> c) {
- Unsafe u = Unsafe.getUnsafe();
-
- int vectorLength = u.getMaxVectorSize(c);
- int vectorBitSize = bitSizeForVectorLength(c, vectorLength);
- Shape s = shapeForVectorBitSize(vectorBitSize);
- return species(c, s);
- }
-
- /**
* Find bit size based on element type and number of elements.
*
* @param c the element type
@@ -1779,65 +1840,4 @@
throw new IllegalArgumentException("Bad vector type: " + c.getName());
}
}
-
- /**
- * Finds appropriate shape depending on bitsize.
- *
- * @param bitSize the size in bits
- * @return the shape corresponding to bitsize
- * @see #bitSize
- */
- public static Shape shapeForVectorBitSize(int bitSize) {
- switch (bitSize) {
- case 64:
- return Shape.S_64_BIT;
- case 128:
- return Shape.S_128_BIT;
- case 256:
- return Shape.S_256_BIT;
- case 512:
- return Shape.S_512_BIT;
- default:
- if ((bitSize > 0) && (bitSize <= 2048) && (bitSize % 128 == 0)) {
- return Shape.S_Max_BIT;
- } else {
- throw new IllegalArgumentException("Bad vector bit size: " + bitSize);
- }
- }
- }
-
- /**
- * Finds a species for an element type and shape.
- *
- * @param c the element type
- * @param s the shape
- * @param <E> the boxed element type
- * @return a species for an element type and shape
- * @throws IllegalArgumentException if no such species exists for the
- * element type and/or shape
- */
- @SuppressWarnings("unchecked")
- public static <E> Vector.Species<E> species(Class<E> c, Shape s) {
- if (c == float.class) {
- return (Vector.Species<E>) FloatVector.species(s);
- }
- else if (c == double.class) {
- return (Vector.Species<E>) DoubleVector.species(s);
- }
- else if (c == byte.class) {
- return (Vector.Species<E>) ByteVector.species(s);
- }
- else if (c == short.class) {
- return (Vector.Species<E>) ShortVector.species(s);
- }
- else if (c == int.class) {
- return (Vector.Species<E>) IntVector.species(s);
- }
- else if (c == long.class) {
- return (Vector.Species<E>) LongVector.species(s);
- }
- else {
- throw new IllegalArgumentException("Bad vector element type: " + c.getName());
- }
- }
}
diff -r d3b456d84b4e -r 8ab9aad380d0 src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template
--- a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template Wed Jan 09 22:09:11 2019 +0100
+++ b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template Tue Jan 29 16:15:44 2019 -0500
@@ -2240,7 +2240,7 @@
*/
@SuppressWarnings("unchecked")
public static $Type$Species preferredSpecies() {
- return ($Type$Species) Vector.preferredSpecies($type$.class);
+ return ($Type$Species) Species.ofPreferred($type$.class);
}
/**
diff -r d3b456d84b4e -r 8ab9aad380d0 src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-VectorBits.java.template
--- a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-VectorBits.java.template Wed Jan 09 22:09:11 2019 +0100
+++ b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-VectorBits.java.template Tue Jan 29 16:15:44 2019 -0500
@@ -52,8 +52,8 @@
private static final IntVector.IntSpecies INDEX_SPEC;
static {
int bitSize = Vector.bitSizeForVectorLength(int.class, LENGTH);
- Vector.Shape shape = Vector.shapeForVectorBitSize(bitSize);
- INDEX_SPEC = (IntVector.IntSpecies) Vector.species(int.class, shape);
+ Vector.Shape shape = Shape.forBitSize(bitSize);
+ INDEX_SPEC = (IntVector.IntSpecies) Species.of(int.class, shape);
}
#end[!longOrDouble64]
diff -r d3b456d84b4e -r 8ab9aad380d0 test/jdk/jdk/incubator/vector/PreferredSpeciesTest.java
--- a/test/jdk/jdk/incubator/vector/PreferredSpeciesTest.java Wed Jan 09 22:09:11 2019 +0100
+++ b/test/jdk/jdk/incubator/vector/PreferredSpeciesTest.java Tue Jan 29 16:15:44 2019 -0500
@@ -51,7 +51,7 @@
@Test(dataProvider = "classesProvider")
void testVectorLength(Class<?> c) {
Vector.Species<?> species =
- Vector.preferredSpecies(c);
+ Vector.Species.ofPreferred(c);
Assert.assertEquals(species.length(), U.getMaxVectorSize(c));
}
More information about the panama-dev
mailing list