< prev index next >

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template

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


 181     @ForceInline
 182     public static $abstractvectortype$ fromByteArray($Type$Species species, byte[] a, int ix, Mask<$Boxtype$> m) {
 183         return zero(species).blend(fromByteArray(species, a, ix), m);
 184     }
 185 
 186     /**
 187      * Loads a vector from an array starting at offset.
 188      * <p>
 189      * For each vector lane, where {@code N} is the vector lane index, the
 190      * array element at index {@code i + N} is placed into the
 191      * resulting vector at lane index {@code N}.
 192      *
 193      * @param species species of desired vector
 194      * @param a the array
 195      * @param i the offset into the array
 196      * @return the vector loaded from an array
 197      * @throws IndexOutOfBoundsException if {@code i < 0}, or
 198      * {@code i > a.length - this.length()}
 199      */
 200     @ForceInline
 201     @SuppressWarnings("unchecked")
 202     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i){
 203         Objects.requireNonNull(a);
 204         i = VectorIntrinsics.checkIndex(i, a.length, species.length());






 205         return VectorIntrinsics.load((Class<$abstractvectortype$>) species.boxType(), $type$.class, species.length(),
 206                                      a, (((long) i) << ARRAY_SHIFT) + Unsafe.ARRAY_$TYPE$_BASE_OFFSET,
 207                                      a, i, species,
 208                                      (c, idx, s) -> (($Type$Species)s).op(n -> c[idx + n]));
 209     }
 210 
 211 
 212     /**
 213      * Loads a vector from an array starting at offset and using a mask.
 214      * <p>
 215      * For each vector lane, where {@code N} is the vector lane index,
 216      * if the mask lane at index {@code N} is set then the array element at
 217      * index {@code i + N} is placed into the resulting vector at lane index
 218      * {@code N}, otherwise the default element value is placed into the
 219      * resulting vector at lane index {@code N}.
 220      *
 221      * @param species species of desired vector
 222      * @param a the array
 223      * @param i the offset into the array
 224      * @param m the mask
 225      * @return the vector loaded from an array
 226      * @throws IndexOutOfBoundsException if {@code i < 0}, or
 227      * for any vector lane index {@code N} where the mask at lane {@code N}
 228      * is set {@code i > a.length - N}
 229      */
 230     @ForceInline
 231     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, Mask<$Boxtype$> m) {
 232         return zero(species).blend(fromArray(species, a, i), m);





 233     }
 234 
 235     /**
 236      * Loads a vector from an array using indexes obtained from an index
 237      * map.
 238      * <p>
 239      * For each vector lane, where {@code N} is the vector lane index, the
 240      * array element at index {@code i + indexMap[j + N]} is placed into the
 241      * resulting vector at lane index {@code N}.
 242      *
 243      * @param species species of desired vector
 244      * @param a the array
 245      * @param i the offset into the array, may be negative if relative
 246      * indexes in the index map compensate to produce a value within the
 247      * array bounds
 248      * @param indexMap the index map
 249      * @param j the offset into the index map
 250      * @return the vector loaded from an array
 251      * @throws IndexOutOfBoundsException if {@code j < 0}, or
 252      * {@code j > indexMap.length - this.length()},
 253      * or for any vector lane index {@code N} the result of
 254      * {@code i + indexMap[j + N]} is {@code < 0} or {@code >= a.length}
 255      */
 256 #if[byteOrShort]
 257     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, int[] indexMap, int j) {
 258         return species.op(n -> a[i + indexMap[j + n]]);
 259     }
 260 #else[byteOrShort]
 261     @ForceInline
 262     @SuppressWarnings("unchecked")
 263     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, int[] indexMap, int j) {
 264         Objects.requireNonNull(a);
 265         Objects.requireNonNull(indexMap);
 266 
 267 #if[longOrDouble]
 268         if (species.length() == 1) {
 269           return $abstractvectortype$.fromArray(species, a, i + indexMap[j]);
 270         }
 271 #end[longOrDouble]
 272 

 273         // Index vector: vix[0:n] = k -> i + indexMap[j + i]
 274         IntVector vix = IntVector.fromArray(species.indexSpecies(), indexMap, j).add(i);
 275 
 276         vix = VectorIntrinsics.checkIndex(vix, a.length);
 277 






 278         return VectorIntrinsics.loadWithMap((Class<$abstractvectortype$>) species.boxType(), $type$.class, species.length(),
 279                                             species.indexSpecies().vectorType(), a, Unsafe.ARRAY_$TYPE$_BASE_OFFSET, vix,
 280                                             a, i, indexMap, j, species,
 281                                            (c, idx, iMap, idy, s) -> (($Type$Species)s).op(n -> c[idx + iMap[idy+n]]));
 282         }
 283 
 284 #end[byteOrShort]

 285     /**
 286      * Loads a vector from an array using indexes obtained from an index
 287      * map and using a mask.
 288      * <p>
 289      * For each vector lane, where {@code N} is the vector lane index,
 290      * if the mask lane at index {@code N} is set then the array element at
 291      * index {@code i + indexMap[j + N]} is placed into the resulting vector
 292      * at lane index {@code N}.
 293      *
 294      * @param species species of desired vector
 295      * @param a the array
 296      * @param i the offset into the array, may be negative if relative
 297      * indexes in the index map compensate to produce a value within the
 298      * array bounds
 299      * @param m the mask
 300      * @param indexMap the index map
 301      * @param j the offset into the index map
 302      * @return the vector loaded from an array
 303      * @throws IndexOutOfBoundsException if {@code j < 0}, or
 304      * {@code j > indexMap.length - this.length()},
 305      * or for any vector lane index {@code N} where the mask at lane
 306      * {@code N} is set the result of {@code i + indexMap[j + N]} is
 307      * {@code < 0} or {@code >= a.length}
 308      */
 309 #if[byteOrShort]
 310     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, Mask<$Boxtype$> m, int[] indexMap, int j) {
 311         return species.op(m, n -> a[i + indexMap[j + n]]);
 312     }
 313 #else[byteOrShort]
 314     @ForceInline
 315     @SuppressWarnings("unchecked")
 316     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, Mask<$Boxtype$> m, int[] indexMap, int j) {
 317         // @@@ This can result in out of bounds errors for unset mask lanes
 318         return zero(species).blend(fromArray(species, a, i, indexMap, j), m);















 319     }
 320 
 321 #end[byteOrShort]
 322 
 323     /**
 324      * Loads a vector from a {@link ByteBuffer byte buffer} starting at an
 325      * offset into the byte buffer.
 326      * <p>
 327      * Bytes are composed into primitive lane elements according to the
 328      * native byte order of the underlying platform.
 329      * <p>
 330      * This method behaves as if it returns the result of calling the
 331      * byte buffer, offset, and mask accepting
 332      * {@link #fromByteBuffer($Type$Species, ByteBuffer, int, Mask)} method} as follows:
 333      * <pre>{@code
 334      *   return this.fromByteBuffer(b, i, this.maskAllTrue())
 335      * }</pre>
 336      *
 337      * @param species species of desired vector
 338      * @param bb the byte buffer




 181     @ForceInline
 182     public static $abstractvectortype$ fromByteArray($Type$Species species, byte[] a, int ix, Mask<$Boxtype$> m) {
 183         return zero(species).blend(fromByteArray(species, a, ix), m);
 184     }
 185 
 186     /**
 187      * Loads a vector from an array starting at offset.
 188      * <p>
 189      * For each vector lane, where {@code N} is the vector lane index, the
 190      * array element at index {@code i + N} is placed into the
 191      * resulting vector at lane index {@code N}.
 192      *
 193      * @param species species of desired vector
 194      * @param a the array
 195      * @param i the offset into the array
 196      * @return the vector loaded from an array
 197      * @throws IndexOutOfBoundsException if {@code i < 0}, or
 198      * {@code i > a.length - this.length()}
 199      */
 200     @ForceInline
 201     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i) {

 202         Objects.requireNonNull(a);
 203         i = VectorIntrinsics.checkIndex(i, a.length, species.length());
 204         return fromArrayWithoutCheck(species, a, i);
 205     }
 206 
 207     @ForceInline
 208     @SuppressWarnings("unchecked")
 209     static $abstractvectortype$ fromArrayWithoutCheck($Type$Species species, $type$[] a, int i) {
 210         return VectorIntrinsics.load((Class<$abstractvectortype$>) species.boxType(), $type$.class, species.length(),
 211                                      a, (((long) i) << ARRAY_SHIFT) + Unsafe.ARRAY_$TYPE$_BASE_OFFSET,
 212                                      a, i, species,
 213                                      (c, idx, s) -> (($Type$Species)s).op(n -> c[idx + n]));
 214     }
 215 

 216     /**
 217      * Loads a vector from an array starting at offset and using a mask.
 218      * <p>
 219      * For each vector lane, where {@code N} is the vector lane index,
 220      * if the mask lane at index {@code N} is set then the array element at
 221      * index {@code i + N} is placed into the resulting vector at lane index
 222      * {@code N}, otherwise the default element value is placed into the
 223      * resulting vector at lane index {@code N}.
 224      *
 225      * @param species species of desired vector
 226      * @param a the array
 227      * @param i the offset into the array
 228      * @param m the mask
 229      * @return the vector loaded from an array
 230      * @throws IndexOutOfBoundsException if {@code i < 0}, or
 231      * for any vector lane index {@code N} where the mask at lane {@code N}
 232      * is set {@code i > a.length - N}
 233      */
 234     @ForceInline
 235     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, Mask<$Boxtype$> m) {
 236         Objects.requireNonNull(a);
 237         if (i + species.length() <= a.length) {
 238             return zero(species).blend(fromArrayWithoutCheck(species, a, i), m);
 239         } else {
 240             return species.op(m, n -> a[i + n]);
 241         }
 242     }
 243 
 244     /**
 245      * Loads a vector from an array using indexes obtained from an index
 246      * map.
 247      * <p>
 248      * For each vector lane, where {@code N} is the vector lane index, the
 249      * array element at index {@code i + indexMap[j + N]} is placed into the
 250      * resulting vector at lane index {@code N}.
 251      *
 252      * @param species species of desired vector
 253      * @param a the array
 254      * @param i the offset into the array, may be negative if relative
 255      * indexes in the index map compensate to produce a value within the
 256      * array bounds
 257      * @param indexMap the index map
 258      * @param j the offset into the index map
 259      * @return the vector loaded from an array
 260      * @throws IndexOutOfBoundsException if {@code j < 0}, or
 261      * {@code j > indexMap.length - this.length()},
 262      * or for any vector lane index {@code N} the result of
 263      * {@code i + indexMap[j + N]} is {@code < 0} or {@code >= a.length}
 264      */
 265 #if[byteOrShort]
 266     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, int[] indexMap, int j) {
 267         return species.op(n -> a[i + indexMap[j + n]]);
 268     }
 269 #else[byteOrShort]
 270     @ForceInline

 271     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, int[] indexMap, int j) {
 272         Objects.requireNonNull(a);
 273         Objects.requireNonNull(indexMap);
 274 
 275 #if[longOrDouble]
 276         if (species.length() == 1) {
 277           return $abstractvectortype$.fromArray(species, a, i + indexMap[j]);
 278         }

 279 
 280 #end[longOrDouble]
 281         // Index vector: vix[0:n] = k -> i + indexMap[j + i]
 282         IntVector vix = IntVector.fromArray(species.indexSpecies(), indexMap, j).add(i);
 283 
 284         vix = VectorIntrinsics.checkIndex(vix, a.length);
 285 
 286         return fromArrayWithoutCheck(species, a, i, indexMap, j, vix);
 287     }
 288 
 289     @ForceInline
 290     @SuppressWarnings("unchecked")
 291     static $abstractvectortype$ fromArrayWithoutCheck($Type$Species species, $type$[] a, int i, int[] indexMap, int j, IntVector vix) {
 292         return VectorIntrinsics.loadWithMap((Class<$abstractvectortype$>) species.boxType(), $type$.class, species.length(),
 293                                             species.indexSpecies().vectorType(), a, Unsafe.ARRAY_$TYPE$_BASE_OFFSET, vix,
 294                                             a, i, indexMap, j, species,
 295                                             (c, idx, iMap, idy, s) -> (($Type$Species)s).op(n -> c[idx + iMap[idy+n]]));
 296     }

 297 #end[byteOrShort]
 298 
 299     /**
 300      * Loads a vector from an array using indexes obtained from an index
 301      * map and using a mask.
 302      * <p>
 303      * For each vector lane, where {@code N} is the vector lane index,
 304      * if the mask lane at index {@code N} is set then the array element at
 305      * index {@code i + indexMap[j + N]} is placed into the resulting vector
 306      * at lane index {@code N}.
 307      *
 308      * @param species species of desired vector
 309      * @param a the array
 310      * @param i the offset into the array, may be negative if relative
 311      * indexes in the index map compensate to produce a value within the
 312      * array bounds
 313      * @param m the mask
 314      * @param indexMap the index map
 315      * @param j the offset into the index map
 316      * @return the vector loaded from an array
 317      * @throws IndexOutOfBoundsException if {@code j < 0}, or
 318      * {@code j > indexMap.length - this.length()},
 319      * or for any vector lane index {@code N} where the mask at lane
 320      * {@code N} is set the result of {@code i + indexMap[j + N]} is
 321      * {@code < 0} or {@code >= a.length}
 322      */
 323 #if[byteOrShort]
 324     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, Mask<$Boxtype$> m, int[] indexMap, int j) {
 325         return species.op(m, n -> a[i + indexMap[j + n]]);
 326     }
 327 #else[byteOrShort]
 328     @ForceInline

 329     public static $abstractvectortype$ fromArray($Type$Species species, $type$[] a, int i, Mask<$Boxtype$> m, int[] indexMap, int j) {
 330         Objects.requireNonNull(a);
 331         Objects.requireNonNull(indexMap);
 332 
 333 #if[longOrDouble]
 334         if (species.length() == 1) {
 335             return fromArray(species, a, i + indexMap[j], m);
 336         }
 337 
 338 #end[longOrDouble]
 339         // Index vector: vix[0:n] = k -> i + indexMap[j + i]
 340         IntVector vix = IntVector.fromArray(species.indexSpecies(), indexMap, j).add(i);
 341 
 342         if (vix.lessThan(0).anyTrue() || vix.greaterThanEq(a.length).anyTrue()) {
 343             return species.op(m, n -> a[i + indexMap[j+n]]);
 344         } else {
 345             return zero(species).blend(fromArrayWithoutCheck(species, a, i, indexMap, j, vix), m);
 346         }
 347     }
 348 
 349 #end[byteOrShort]
 350 
 351     /**
 352      * Loads a vector from a {@link ByteBuffer byte buffer} starting at an
 353      * offset into the byte buffer.
 354      * <p>
 355      * Bytes are composed into primitive lane elements according to the
 356      * native byte order of the underlying platform.
 357      * <p>
 358      * This method behaves as if it returns the result of calling the
 359      * byte buffer, offset, and mask accepting
 360      * {@link #fromByteBuffer($Type$Species, ByteBuffer, int, Mask)} method} as follows:
 361      * <pre>{@code
 362      *   return this.fromByteBuffer(b, i, this.maskAllTrue())
 363      * }</pre>
 364      *
 365      * @param species species of desired vector
 366      * @param bb the byte buffer


< prev index next >