< prev index next >

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

Print this page
rev 55589 : 8221816: [vector] IndexOutOfBoundsException for fromArray/intoArray with unset mask lanes
Reviewed-by: duke

@@ -193,21 +193,25 @@
      * @return the vector loaded from an array
      * @throws IndexOutOfBoundsException if {@code i < 0}, or
      * {@code i > a.length - this.length()}
      */
     @ForceInline
-    @SuppressWarnings("unchecked")
-    public static ByteVector fromArray(ByteSpecies species, byte[] a, int i){
+    public static ByteVector fromArray(ByteSpecies species, byte[] a, int i) {
         Objects.requireNonNull(a);
         i = VectorIntrinsics.checkIndex(i, a.length, species.length());
+        return fromArrayWithoutCheck(species, a, i);
+    }
+
+    @ForceInline
+    @SuppressWarnings("unchecked")
+    static ByteVector fromArrayWithoutCheck(ByteSpecies species, byte[] a, int i) {
         return VectorIntrinsics.load((Class<ByteVector>) species.boxType(), byte.class, species.length(),
                                      a, (((long) i) << ARRAY_SHIFT) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
                                      a, i, species,
                                      (c, idx, s) -> ((ByteSpecies)s).op(n -> c[idx + n]));
     }
 
-
     /**
      * Loads a vector from an array starting at offset and using a mask.
      * <p>
      * For each vector lane, where {@code N} is the vector lane index,
      * if the mask lane at index {@code N} is set then the array element at

@@ -224,11 +228,16 @@
      * for any vector lane index {@code N} where the mask at lane {@code N}
      * is set {@code i > a.length - N}
      */
     @ForceInline
     public static ByteVector fromArray(ByteSpecies species, byte[] a, int i, Mask<Byte> m) {
-        return zero(species).blend(fromArray(species, a, i), m);
+        Objects.requireNonNull(a);
+        if (i + species.length() <= a.length) {
+            return zero(species).blend(fromArrayWithoutCheck(species, a, i), m);
+        } else {
+            return species.op(m, n -> a[i + n]);
+        }
     }
 
     /**
      * Loads a vector from an array using indexes obtained from an index
      * map.

@@ -251,10 +260,11 @@
      * {@code i + indexMap[j + N]} is {@code < 0} or {@code >= a.length}
      */
     public static ByteVector fromArray(ByteSpecies species, byte[] a, int i, int[] indexMap, int j) {
         return species.op(n -> a[i + indexMap[j + n]]);
     }
+
     /**
      * Loads a vector from an array using indexes obtained from an index
      * map and using a mask.
      * <p>
      * For each vector lane, where {@code N} is the vector lane index,
< prev index next >