< prev index next >

test/jdk/jdk/incubator/vector/ByteMaxVectorLoadStoreTests.java

Print this page

   1 /*
   2  * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @modules jdk.incubator.foreign jdk.incubator.vector java.base/jdk.internal.vm.annotation
  27  * @run testng/othervm --add-opens jdk.incubator.vector/jdk.incubator.vector=ALL-UNNAMED
  28  *      -XX:-TieredCompilation ByteMaxVectorLoadStoreTests
  29  *
  30  */
  31 
  32 // -- This file was mechanically generated: Do not edit! -- //
  33 



  34 import jdk.incubator.vector.ByteVector;
  35 import jdk.incubator.vector.VectorMask;
  36 import jdk.incubator.vector.VectorShape;
  37 import jdk.incubator.vector.VectorSpecies;
  38 import jdk.incubator.vector.VectorShuffle;
  39 import jdk.internal.vm.annotation.DontInline;
  40 import org.testng.Assert;
  41 import org.testng.annotations.DataProvider;
  42 import org.testng.annotations.Test;
  43 
  44 import java.lang.invoke.MethodHandles;
  45 import java.lang.invoke.VarHandle;
  46 import java.nio.ByteBuffer;
  47 import java.nio.ByteOrder;
  48 import java.nio.ReadOnlyBufferException;
  49 import java.util.List;
  50 import java.util.function.*;
  51 
  52 @Test
  53 public class ByteMaxVectorLoadStoreTests extends AbstractVectorLoadStoreTest {
  54     static final VectorSpecies<Byte> SPECIES =
  55                 ByteVector.SPECIES_MAX;
  56 
  57     static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
  58 
  59     static VectorShape getMaxBit() {
  60         return VectorShape.S_Max_BIT;
  61     }
  62 
  63     private static final int Max = 256;  // juts so we can do N/Max
  64 
  65     static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
  66 
  67     static void assertArraysEquals(byte[] r, byte[] a, boolean[] mask) {
  68         int i = 0;
  69         try {
  70             for (; i < a.length; i++) {
  71                 Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? a[i] : (byte) 0);
  72             }
  73         } catch (AssertionError e) {
  74             Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? a[i] : (byte) 0, "at index #" + i);
  75         }
  76     }
  77 
  78 
  79     static final List<IntFunction<byte[]>> BYTE_GENERATORS = List.of(
  80             withToString("byte[i * 5]", (int s) -> {
  81                 return fill(s * BUFFER_REPS,
  82                             i -> (byte)(i * 5));
  83             }),
  84             withToString("byte[i + 1]", (int s) -> {
  85                 return fill(s * BUFFER_REPS,
  86                             i -> (((byte)(i + 1) == 0) ? 1 : (byte)(i + 1)));
  87             })
  88     );
  89 
  90     // Relative to array.length
  91     static final List<IntFunction<Integer>> INDEX_GENERATORS = List.of(
  92             withToString("-1", (int l) -> {
  93                 return -1;
  94             }),
  95             withToString("l", (int l) -> {
  96                 return l;
  97             }),
  98             withToString("l - 1", (int l) -> {
  99                 return l - 1;
 100             }),
 101             withToString("l + 1", (int l) -> {
 102                 return l + 1;
 103             }),
 104             withToString("l - speciesl + 1", (int l) -> {
 105                 return l - SPECIES.length() + 1;
 106             }),
 107             withToString("l + speciesl - 1", (int l) -> {
 108                 return l + SPECIES.length() - 1;
 109             }),
 110             withToString("l + speciesl", (int l) -> {
 111                 return l + SPECIES.length();
 112             }),
 113             withToString("l + speciesl + 1", (int l) -> {
 114                 return l + SPECIES.length() + 1;
 115             })
 116     );
 117 
 118     // Relative to byte[] array.length or ByteBuffer.limit()
 119     static final List<IntFunction<Integer>> BYTE_INDEX_GENERATORS = List.of(
 120             withToString("-1", (int l) -> {
 121                 return -1;
 122             }),
 123             withToString("l", (int l) -> {
 124                 return l;
 125             }),
 126             withToString("l - 1", (int l) -> {
 127                 return l - 1;
 128             }),
 129             withToString("l + 1", (int l) -> {
 130                 return l + 1;
 131             }),
 132             withToString("l - speciesl*ebsize + 1", (int l) -> {
 133                 return l - SPECIES.vectorByteSize() + 1;
 134             }),
 135             withToString("l + speciesl*ebsize - 1", (int l) -> {
 136                 return l + SPECIES.vectorByteSize() - 1;
 137             }),
 138             withToString("l + speciesl*ebsize", (int l) -> {

 169     @DataProvider
 170     public Object[][] byteMaskProvider() {
 171         return BOOLEAN_MASK_GENERATORS.stream().
 172                 flatMap(fm -> BYTE_GENERATORS.stream().map(fa -> {
 173                     return new Object[] {fa, fm};
 174                 })).
 175                 toArray(Object[][]::new);
 176     }
 177 
 178     @DataProvider
 179     public Object[][] byteMaskProviderForIOOBE() {
 180         var f = BYTE_GENERATORS.get(0);
 181         return BOOLEAN_MASK_GENERATORS.stream().
 182                 flatMap(fm -> INDEX_GENERATORS.stream().map(fi -> {
 183                     return new Object[] {f, fi, fm};
 184                 })).
 185                 toArray(Object[][]::new);
 186     }
 187 
 188     @DataProvider
 189     public Object[][] byteByteBufferProvider() {
 190         return BYTE_GENERATORS.stream().
 191                 flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().
 192                         flatMap(fb -> BYTE_ORDER_VALUES.stream().map(bo -> {
 193                             return new Object[]{fa, fb, bo};
 194                         }))).
 195                 toArray(Object[][]::new);
 196     }
 197 
 198     @DataProvider
 199     public Object[][] byteByteBufferMaskProvider() {
 200         return BOOLEAN_MASK_GENERATORS.stream().
 201                 flatMap(fm -> BYTE_GENERATORS.stream().
 202                         flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().
 203                                 flatMap(fb -> BYTE_ORDER_VALUES.stream().map(bo -> {
 204                             return new Object[]{fa, fb, fm, bo};
 205                         })))).
 206                 toArray(Object[][]::new);
 207     }
 208 
 209     @DataProvider
 210     public Object[][] byteByteArrayProvider() {
 211         return BYTE_GENERATORS.stream().
 212                 flatMap(fa -> BYTE_ORDER_VALUES.stream().map(bo -> {
 213                     return new Object[]{fa, bo};
 214                 })).
 215                 toArray(Object[][]::new);
 216     }
 217 
 218     @DataProvider
 219     public Object[][] byteByteArrayMaskProvider() {
 220         return BOOLEAN_MASK_GENERATORS.stream().
 221                 flatMap(fm -> BYTE_GENERATORS.stream().
 222                     flatMap(fa -> BYTE_ORDER_VALUES.stream().map(bo -> {
 223                         return new Object[]{fa, fm, bo};
 224                     }))).
 225                 toArray(Object[][]::new);
 226     }
 227 
 228     @DataProvider
 229     public Object[][] byteByteProviderForIOOBE() {
 230         var f = BYTE_GENERATORS.get(0);
 231         return BYTE_INDEX_GENERATORS.stream().map(fi -> {
 232                     return new Object[] {f, fi};
 233                 }).
 234                 toArray(Object[][]::new);
 235     }
 236 
 237     @DataProvider
 238     public Object[][] byteByteMaskProviderForIOOBE() {
 239         var f = BYTE_GENERATORS.get(0);
 240         return BOOLEAN_MASK_GENERATORS.stream().
 241                 flatMap(fm -> BYTE_INDEX_GENERATORS.stream().map(fi -> {
 242                     return new Object[] {f, fi, fm};
 243                 })).
 244                 toArray(Object[][]::new);
 245     }
 246 
 247     static ByteBuffer toBuffer(byte[] a, IntFunction<ByteBuffer> fb) {
 248         ByteBuffer bb = fb.apply(a.length * SPECIES.elementSize() / 8);
 249         for (byte v : a) {
 250             bb.put(v);
 251         }
 252         return bb.clear();
 253     }
 254 
 255     static byte[] bufferToArray(ByteBuffer bb) {
 256         ByteBuffer db = bb;
 257         byte[] d = new byte[db.capacity()];
 258         db.get(0, d);
 259         return d;
 260     }
 261 
 262     static byte[] toByteArray(byte[] a, IntFunction<byte[]> fb, ByteOrder bo) {
 263         byte[] b = fb.apply(a.length * SPECIES.elementSize() / 8);
 264         ByteBuffer bb = ByteBuffer.wrap(b, 0, b.length).order(bo);
 265         for (byte v : a) {
 266             bb.put(v);
 267         }
 268         return b;
 269     }
 270 
 271 
 272     interface ToByteF {
 273         byte apply(int i);
 274     }
 275 
 276     static byte[] fill(int s , ToByteF f) {
 277         return fill(new byte[s], f);
 278     }
 279 
 280     static byte[] fill(byte[] a, ToByteF f) {
 281         for (int i = 0; i < a.length; i++) {
 282             a[i] = f.apply(i);
 283         }
 284         return a;
 285     }
 286 
 287     @DontInline
 288     static ByteVector fromArray(byte[] a, int i) {
 289         return ByteVector.fromArray(SPECIES, a, i);
 290     }
 291 
 292     @DontInline
 293     static ByteVector fromArray(byte[] a, int i, VectorMask<Byte> m) {
 294         return ByteVector.fromArray(SPECIES, a, i, m);
 295     }
 296 
 297     @DontInline
 298     static void intoArray(ByteVector v, byte[] a, int i) {
 299         v.intoArray(a, i);
 300     }
 301 
 302     @DontInline
 303     static void intoArray(ByteVector v, byte[] a, int i, VectorMask<Byte> m) {
 304         v.intoArray(a, i, m);
 305     }
 306 
 307     @DontInline
 308     static ByteVector fromByteArray(byte[] a, int i, ByteOrder bo) {
 309         return ByteVector.fromByteArray(SPECIES, a, i, bo);
 310     }
 311 
 312     @DontInline
 313     static ByteVector fromByteArray(byte[] a, int i, ByteOrder bo, VectorMask<Byte> m) {
 314         return ByteVector.fromByteArray(SPECIES, a, i, bo, m);
 315     }
 316 
 317     @DontInline
 318     static void intoByteArray(ByteVector v, byte[] a, int i, ByteOrder bo) {
 319         v.intoByteArray(a, i, bo);
 320     }
 321 
 322     @DontInline
 323     static void intoByteArray(ByteVector v, byte[] a, int i, ByteOrder bo, VectorMask<Byte> m) {
 324         v.intoByteArray(a, i, bo, m);
 325     }
 326 
 327     @DontInline
 328     static ByteVector fromByteBuffer(ByteBuffer a, int i, ByteOrder bo) {
 329         return ByteVector.fromByteBuffer(SPECIES, a, i, bo);
 330     }
 331 
 332     @DontInline
 333     static ByteVector fromByteBuffer(ByteBuffer a, int i, ByteOrder bo, VectorMask<Byte> m) {
 334         return ByteVector.fromByteBuffer(SPECIES, a, i, bo, m);
 335     }
 336 
 337     @DontInline
 338     static void intoByteBuffer(ByteVector v, ByteBuffer a, int i, ByteOrder bo) {
 339         v.intoByteBuffer(a, i, bo);
 340     }
 341 
 342     @DontInline
 343     static void intoByteBuffer(ByteVector v, ByteBuffer a, int i, ByteOrder bo, VectorMask<Byte> m) {
 344         v.intoByteBuffer(a, i, bo, m);
 345     }
 346 
 347 
 348     @Test(dataProvider = "byteProvider")
 349     static void loadStoreArray(IntFunction<byte[]> fa) {
 350         byte[] a = fa.apply(SPECIES.length());
 351         byte[] r = new byte[a.length];
 352 
 353         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 354             for (int i = 0; i < a.length; i += SPECIES.length()) {
 355                 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
 356                 av.intoArray(r, i);
 357             }
 358         }
 359         Assert.assertEquals(r, a);
 360     }
 361 
 362     @Test(dataProvider = "byteProviderForIOOBE")
 363     static void loadArrayIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi) {
 364         byte[] a = fa.apply(SPECIES.length());
 365         byte[] r = new byte[a.length];
 366 
 367         for (int ic = 0; ic < INVOC_COUNT; ic++) {

 498         }
 499     }
 500 
 501 
 502     @Test(dataProvider = "byteMaskProvider")
 503     static void loadStoreMask(IntFunction<byte[]> fa,
 504                               IntFunction<boolean[]> fm) {
 505         boolean[] mask = fm.apply(SPECIES.length());
 506         boolean[] r = new boolean[mask.length];
 507 
 508         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 509             for (int i = 0; i < mask.length; i += SPECIES.length()) {
 510                 VectorMask<Byte> vmask = VectorMask.fromArray(SPECIES, mask, i);
 511                 vmask.intoArray(r, i);
 512             }
 513         }
 514         Assert.assertEquals(r, mask);
 515     }
 516 
 517 
 518     @Test(dataProvider = "byteByteBufferProvider")
 519     static void loadStoreByteBuffer(IntFunction<byte[]> fa,
 520                                     IntFunction<ByteBuffer> fb,
 521                                     ByteOrder bo) {
 522         ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb);
 523         ByteBuffer r = fb.apply(a.limit());
 524 
 525         int l = a.limit();
 526         int s = SPECIES.vectorByteSize();
 527 
 528         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 529             for (int i = 0; i < l; i += s) {
 530                 ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i, bo);
 531                 av.intoByteBuffer(r, i, bo);
 532             }
 533         }
 534         Assert.assertEquals(a.position(), 0, "Input buffer position changed");
 535         Assert.assertEquals(a.limit(), l, "Input buffer limit changed");
 536         Assert.assertEquals(r.position(), 0, "Result buffer position changed");
 537         Assert.assertEquals(r.limit(), l, "Result buffer limit changed");
 538         Assert.assertEquals(r, a, "Buffers not equal");
 539     }
 540 
 541     @Test(dataProvider = "byteByteProviderForIOOBE")
 542     static void loadByteBufferIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi) {
 543         ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), ByteBuffer::allocateDirect);
 544         ByteBuffer r = ByteBuffer.allocateDirect(a.limit());
 545 
 546         int l = a.limit();
 547         int s = SPECIES.vectorByteSize();
 548 
 549         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 550             for (int i = 0; i < l; i += s) {
 551                 ByteVector av = fromByteBuffer(a, i, ByteOrder.nativeOrder());
 552                 av.intoByteBuffer(r, i, ByteOrder.nativeOrder());
 553             }
 554         }
 555 
 556         int index = fi.apply(a.limit());
 557         boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, a.limit());
 558         try {
 559             fromByteBuffer(a, index, ByteOrder.nativeOrder());
 560             if (shouldFail) {
 561                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 562             }
 563         } catch (IndexOutOfBoundsException e) {
 564             if (!shouldFail) {
 565                 Assert.fail("Unexpected IndexOutOfBoundsException");
 566             }
 567         }
 568     }
 569 
 570     @Test(dataProvider = "byteByteProviderForIOOBE")
 571     static void storeByteBufferIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi) {
 572         ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), ByteBuffer::allocateDirect);
 573         ByteBuffer r = ByteBuffer.allocateDirect(a.limit());
 574 
 575         int l = a.limit();
 576         int s = SPECIES.vectorByteSize();
 577 
 578         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 579             for (int i = 0; i < l; i += s) {
 580                 ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i, ByteOrder.nativeOrder());
 581                 intoByteBuffer(av, r, i, ByteOrder.nativeOrder());
 582             }
 583         }
 584 
 585         int index = fi.apply(a.limit());
 586         boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, a.limit());
 587         try {
 588             ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, 0, ByteOrder.nativeOrder());
 589             intoByteBuffer(av, r, index, ByteOrder.nativeOrder());
 590             if (shouldFail) {
 591                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 592             }
 593         } catch (IndexOutOfBoundsException e) {
 594             if (!shouldFail) {
 595                 Assert.fail("Unexpected IndexOutOfBoundsException");
 596             }
 597         }
 598     }
 599 
 600 
 601     @Test(dataProvider = "byteByteBufferMaskProvider")
 602     static void loadStoreByteBufferMask(IntFunction<byte[]> fa,
 603                                         IntFunction<ByteBuffer> fb,
 604                                         IntFunction<boolean[]> fm,
 605                                         ByteOrder bo) {
 606         byte[] _a = fa.apply(SPECIES.length());
 607         ByteBuffer a = toBuffer(_a, fb);
 608         ByteBuffer r = fb.apply(a.limit());
 609         boolean[] mask = fm.apply(SPECIES.length());
 610         VectorMask<Byte> vmask = VectorMask.fromValues(SPECIES, mask);
 611 
 612         int l = a.limit();
 613         int s = SPECIES.vectorByteSize();
 614 
 615         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 616             for (int i = 0; i < l; i += s) {
 617                 ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i, bo, vmask);
 618                 av.intoByteBuffer(r, i, bo);
 619             }
 620         }
 621         Assert.assertEquals(a.position(), 0, "Input buffer position changed");
 622         Assert.assertEquals(a.limit(), l, "Input buffer limit changed");
 623         Assert.assertEquals(r.position(), 0, "Result buffer position changed");
 624         Assert.assertEquals(r.limit(), l, "Result buffer limit changed");
 625         assertArraysEquals(bufferToArray(r), _a, mask);
 626 
 627 
 628         r = fb.apply(a.limit());
 629 
 630         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 631             for (int i = 0; i < l; i += s) {
 632                 ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i, bo);
 633                 av.intoByteBuffer(r, i, bo, vmask);
 634             }
 635         }
 636         Assert.assertEquals(a.position(), 0, "Input buffer position changed");
 637         Assert.assertEquals(a.limit(), l, "Input buffer limit changed");
 638         Assert.assertEquals(r.position(), 0, "Result buffer position changed");
 639         Assert.assertEquals(r.limit(), l, "Result buffer limit changed");
 640         assertArraysEquals(bufferToArray(r), _a, mask);
 641     }
 642 
 643     @Test(dataProvider = "byteByteMaskProviderForIOOBE")
 644     static void loadByteBufferMaskIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
 645         ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), ByteBuffer::allocateDirect);
 646         ByteBuffer r = ByteBuffer.allocateDirect(a.limit());
 647         boolean[] mask = fm.apply(SPECIES.length());
 648         VectorMask<Byte> vmask = VectorMask.fromValues(SPECIES, mask);
 649 
 650         int l = a.limit();
 651         int s = SPECIES.vectorByteSize();
 652 
 653         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 654             for (int i = 0; i < l; i += s) {
 655                 ByteVector av = fromByteBuffer(a, i, ByteOrder.nativeOrder(), vmask);
 656                 av.intoByteBuffer(r, i, ByteOrder.nativeOrder());
 657             }
 658         }
 659 
 660         int index = fi.apply(a.limit());
 661         boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, a.limit(), SPECIES.elementSize() / 8);
 662         try {
 663             fromByteBuffer(a, index, ByteOrder.nativeOrder(), vmask);
 664             if (shouldFail) {
 665                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 666             }
 667         } catch (IndexOutOfBoundsException e) {
 668             if (!shouldFail) {
 669                 Assert.fail("Unexpected IndexOutOfBoundsException");
 670             }
 671         }
 672     }
 673 
 674     @Test(dataProvider = "byteByteMaskProviderForIOOBE")
 675     static void storeByteBufferMaskIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
 676         ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), ByteBuffer::allocateDirect);
 677         ByteBuffer r = ByteBuffer.allocateDirect(a.limit());
 678         boolean[] mask = fm.apply(SPECIES.length());
 679         VectorMask<Byte> vmask = VectorMask.fromValues(SPECIES, mask);
 680 
 681         int l = a.limit();
 682         int s = SPECIES.vectorByteSize();
 683 
 684         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 685             for (int i = 0; i < l; i += s) {
 686                 ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i, ByteOrder.nativeOrder());
 687                 intoByteBuffer(av, r, i, ByteOrder.nativeOrder(), vmask);
 688             }
 689         }
 690 
 691         int index = fi.apply(a.limit());
 692         boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, a.limit(), SPECIES.elementSize() / 8);
 693         try {
 694             ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, 0, ByteOrder.nativeOrder());
 695             intoByteBuffer(av, a, index, ByteOrder.nativeOrder(), vmask);
 696             if (shouldFail) {
 697                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 698             }
 699         } catch (IndexOutOfBoundsException e) {
 700             if (!shouldFail) {
 701                 Assert.fail("Unexpected IndexOutOfBoundsException");
 702             }
 703         }
 704     }
 705 
 706 
 707     @Test(dataProvider = "byteByteBufferProvider")
 708     static void loadStoreReadonlyByteBuffer(IntFunction<byte[]> fa,
 709                                     IntFunction<ByteBuffer> fb,
 710                                     ByteOrder bo) {
 711         ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb).asReadOnlyBuffer();
 712 
 713         try {
 714             SPECIES.zero().intoByteBuffer(a, 0, bo);
 715             Assert.fail("ReadOnlyBufferException expected");
 716         } catch (ReadOnlyBufferException e) {
 717         }
 718 
 719         try {
 720             SPECIES.zero().intoByteBuffer(a, 0, bo, SPECIES.maskAll(true));
 721             Assert.fail("ReadOnlyBufferException expected");
 722         } catch (ReadOnlyBufferException e) {
 723         }
 724 
 725         try {
 726             SPECIES.zero().intoByteBuffer(a, 0, bo, SPECIES.maskAll(false));
 727             Assert.fail("ReadOnlyBufferException expected");
 728         } catch (ReadOnlyBufferException e) {
 729         }
 730 
 731         try {
 732             VectorMask<Byte> m = SPECIES.shuffleFromOp(i -> i % 2 == 0 ? 1 : -1)
 733                     .laneIsValid();
 734             SPECIES.zero().intoByteBuffer(a, 0, bo, m);
 735             Assert.fail("ReadOnlyBufferException expected");
 736         } catch (ReadOnlyBufferException e) {
 737         }
 738     }
 739 
 740 
 741     @Test(dataProvider = "byteByteArrayProvider")
 742     static void loadStoreByteArray(IntFunction<byte[]> fa,
 743                                     ByteOrder bo) {
 744         byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, bo);
 745         byte[] r = new byte[a.length];
 746 
 747         int s = SPECIES.vectorByteSize();
 748         int l = a.length;
 749 
 750         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 751             for (int i = 0; i < l; i += s) {
 752                 ByteVector av = ByteVector.fromByteArray(SPECIES, a, i, bo);
 753                 av.intoByteArray(r, i, bo);
 754             }
 755         }
 756         Assert.assertEquals(r, a, "Byte arrays not equal");
 757     }
 758 
 759     @Test(dataProvider = "byteByteProviderForIOOBE")
 760     static void loadByteArrayIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi) {
 761         byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, ByteOrder.nativeOrder());
 762         byte[] r = new byte[a.length];
 763 
 764         int s = SPECIES.vectorByteSize();
 765         int l = a.length;
 766 
 767         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 768             for (int i = 0; i < l; i += s) {
 769                 ByteVector av = fromByteArray(a, i, ByteOrder.nativeOrder());
 770                 av.intoByteArray(r, i, ByteOrder.nativeOrder());
 771             }
 772         }
 773 
 774         int index = fi.apply(a.length);
 775         boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, a.length);
 776         try {
 777             fromByteArray(a, index, ByteOrder.nativeOrder());
 778             if (shouldFail) {
 779                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 780             }
 781         } catch (IndexOutOfBoundsException e) {
 782             if (!shouldFail) {
 783                 Assert.fail("Unexpected IndexOutOfBoundsException");
 784             }
 785         }
 786     }
 787 
 788     @Test(dataProvider = "byteByteProviderForIOOBE")
 789     static void storeByteArrayIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi) {
 790         byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, ByteOrder.nativeOrder());
 791         byte[] r = new byte[a.length];
 792 
 793         int s = SPECIES.vectorByteSize();
 794         int l = a.length;
 795 
 796         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 797             for (int i = 0; i < l; i += s) {
 798                 ByteVector av = ByteVector.fromByteArray(SPECIES, a, i, ByteOrder.nativeOrder());
 799                 intoByteArray(av, r, i, ByteOrder.nativeOrder());
 800             }
 801         }
 802 
 803         int index = fi.apply(a.length);
 804         boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, a.length);
 805         try {
 806             ByteVector av = ByteVector.fromByteArray(SPECIES, a, 0, ByteOrder.nativeOrder());
 807             intoByteArray(av, r, index, ByteOrder.nativeOrder());
 808             if (shouldFail) {
 809                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 810             }
 811         } catch (IndexOutOfBoundsException e) {
 812             if (!shouldFail) {
 813                 Assert.fail("Unexpected IndexOutOfBoundsException");
 814             }
 815         }
 816     }
 817 





 818 
 819     @Test(dataProvider = "byteByteArrayMaskProvider")
 820     static void loadStoreByteArrayMask(IntFunction<byte[]> fa,
 821                                   IntFunction<boolean[]> fm,
 822                                   ByteOrder bo) {
 823         byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, bo);
 824         byte[] r = new byte[a.length];
 825         boolean[] mask = fm.apply(SPECIES.length());
 826         VectorMask<Byte> vmask = VectorMask.fromValues(SPECIES, mask);
 827 
 828         int s = SPECIES.vectorByteSize();
 829         int l = a.length;
 830 
 831         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 832           for (int i = 0; i < l; i += s) {
 833               ByteVector av = ByteVector.fromByteArray(SPECIES, a, i, bo, vmask);
 834               av.intoByteArray(r, i, bo);
 835           }
 836         }
 837         assertArraysEquals(r, a, mask);
 838 
 839 
 840         r = new byte[a.length];
 841 
 842         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 843             for (int i = 0; i < l; i += s) {
 844                 ByteVector av = ByteVector.fromByteArray(SPECIES, a, i, bo);
 845                 av.intoByteArray(r, i, bo, vmask);
 846             }
 847         }
 848         assertArraysEquals(r, a, mask);
 849     }
 850 
 851     @Test(dataProvider = "byteByteMaskProviderForIOOBE")
 852     static void loadByteArrayMaskIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
 853         byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, ByteOrder.nativeOrder());
 854         byte[] r = new byte[a.length];
 855         boolean[] mask = fm.apply(SPECIES.length());
 856         VectorMask<Byte> vmask = VectorMask.fromValues(SPECIES, mask);
 857 
 858         int s = SPECIES.vectorByteSize();
 859         int l = a.length;


 860 
 861         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 862             for (int i = 0; i < l; i += s) {
 863                 ByteVector av = fromByteArray(a, i, ByteOrder.nativeOrder(), vmask);
 864                 av.intoByteArray(r, i, ByteOrder.nativeOrder());
 865             }
 866         }
 867 
 868         int index = fi.apply(a.length);
 869         boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, a.length, SPECIES.elementSize() / 8);
 870         try {
 871             fromByteArray(a, index, ByteOrder.nativeOrder(), vmask);
 872             if (shouldFail) {
 873                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 874             }
 875         } catch (IndexOutOfBoundsException e) {
 876             if (!shouldFail) {
 877                 Assert.fail("Unexpected IndexOutOfBoundsException");
 878             }
 879         }
 880     }
 881 
 882     @Test(dataProvider = "byteByteMaskProviderForIOOBE")
 883     static void storeByteArrayMaskIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
 884         byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, ByteOrder.nativeOrder());
 885         byte[] r = new byte[a.length];
 886         boolean[] mask = fm.apply(SPECIES.length());
 887         VectorMask<Byte> vmask = VectorMask.fromValues(SPECIES, mask);
 888 
 889         int s = SPECIES.vectorByteSize();
 890         int l = a.length;
 891 
 892         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 893             for (int i = 0; i < l; i += s) {
 894                 ByteVector av = ByteVector.fromByteArray(SPECIES, a, i, ByteOrder.nativeOrder());
 895                 intoByteArray(av, r, i, ByteOrder.nativeOrder(), vmask);
 896             }
 897         }
 898 
 899         int index = fi.apply(a.length);
 900         boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, a.length, SPECIES.elementSize() / 8);
 901         try {
 902             ByteVector av = ByteVector.fromByteArray(SPECIES, a, 0, ByteOrder.nativeOrder());
 903             intoByteArray(av, a, index, ByteOrder.nativeOrder(), vmask);
 904             if (shouldFail) {
 905                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 906             }
 907         } catch (IndexOutOfBoundsException e) {
 908             if (!shouldFail) {
 909                 Assert.fail("Unexpected IndexOutOfBoundsException");
 910             }
 911         }
 912     }
 913 
 914     @Test(dataProvider = "maskProvider")
 915     static void loadStoreMask(IntFunction<boolean[]> fm) {
 916         boolean[] a = fm.apply(SPECIES.length());
 917         boolean[] r = new boolean[a.length];
 918 
 919         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 920             for (int i = 0; i < a.length; i += SPECIES.length()) {
 921                 VectorMask<Byte> vmask = SPECIES.loadMask(a, i);
 922                 vmask.intoArray(r, i);
 923             }
 924         }
 925         Assert.assertEquals(r, a);
 926     }
 927 

 928     @Test
 929     static void loadStoreShuffle() {
 930         IntUnaryOperator fn = a -> a + 5;
 931         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 932             var shuffle = VectorShuffle.fromOp(SPECIES, fn);
 933             int [] r = shuffle.toArray();
 934 
 935             int [] a = expectedShuffle(SPECIES.length(), fn);
 936             Assert.assertEquals(r, a);
 937        }
 938     }
 939 
 940 
 941 
 942     static void assertArraysEquals(boolean[] r, byte[] a) {
 943         int i = 0;
 944         try {
 945             for (; i < a.length; i++) {
 946                 Assert.assertEquals(r[i], (a[i] & 1) == 1);
 947             }

   1 /*
   2  * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @modules jdk.incubator.foreign jdk.incubator.vector java.base/jdk.internal.vm.annotation
  27  * @run testng/othervm --add-opens jdk.incubator.vector/jdk.incubator.vector=ALL-UNNAMED
  28  *      -XX:-TieredCompilation ByteMaxVectorLoadStoreTests
  29  *
  30  */
  31 
  32 // -- This file was mechanically generated: Do not edit! -- //
  33 
  34 import jdk.incubator.foreign.MemorySegment;
  35 import jdk.incubator.foreign.ResourceScope;
  36 import jdk.incubator.foreign.ValueLayout;
  37 import jdk.incubator.vector.ByteVector;
  38 import jdk.incubator.vector.VectorMask;
  39 import jdk.incubator.vector.VectorShape;
  40 import jdk.incubator.vector.VectorSpecies;
  41 import jdk.incubator.vector.VectorShuffle;
  42 import jdk.internal.vm.annotation.DontInline;
  43 import org.testng.Assert;
  44 import org.testng.annotations.DataProvider;
  45 import org.testng.annotations.Test;
  46 



  47 import java.nio.ByteOrder;

  48 import java.util.List;
  49 import java.util.function.*;
  50 
  51 @Test
  52 public class ByteMaxVectorLoadStoreTests extends AbstractVectorLoadStoreTest {
  53     static final VectorSpecies<Byte> SPECIES =
  54                 ByteVector.SPECIES_MAX;
  55 
  56     static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
  57 
  58     static VectorShape getMaxBit() {
  59         return VectorShape.S_Max_BIT;
  60     }
  61 
  62     private static final int Max = 256;  // juts so we can do N/Max
  63 
  64     static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
  65 
  66     static void assertArraysEquals(byte[] r, byte[] a, boolean[] mask) {
  67         int i = 0;
  68         try {
  69             for (; i < a.length; i++) {
  70                 Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? a[i] : (byte) 0);
  71             }
  72         } catch (AssertionError e) {
  73             Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? a[i] : (byte) 0, "at index #" + i);
  74         }
  75     }
  76 

  77     static final List<IntFunction<byte[]>> BYTE_GENERATORS = List.of(
  78             withToString("byte[i * 5]", (int s) -> {
  79                 return fill(s * BUFFER_REPS,
  80                             i -> (byte)(i * 5));
  81             }),
  82             withToString("byte[i + 1]", (int s) -> {
  83                 return fill(s * BUFFER_REPS,
  84                             i -> (((byte)(i + 1) == 0) ? 1 : (byte)(i + 1)));
  85             })
  86     );
  87 
  88     // Relative to array.length
  89     static final List<IntFunction<Integer>> INDEX_GENERATORS = List.of(
  90             withToString("-1", (int l) -> {
  91                 return -1;
  92             }),
  93             withToString("l", (int l) -> {
  94                 return l;
  95             }),
  96             withToString("l - 1", (int l) -> {
  97                 return l - 1;
  98             }),
  99             withToString("l + 1", (int l) -> {
 100                 return l + 1;
 101             }),
 102             withToString("l - speciesl + 1", (int l) -> {
 103                 return l - SPECIES.length() + 1;
 104             }),
 105             withToString("l + speciesl - 1", (int l) -> {
 106                 return l + SPECIES.length() - 1;
 107             }),
 108             withToString("l + speciesl", (int l) -> {
 109                 return l + SPECIES.length();
 110             }),
 111             withToString("l + speciesl + 1", (int l) -> {
 112                 return l + SPECIES.length() + 1;
 113             })
 114     );
 115 
 116     // Relative to byte[] array.length or MemorySegment.byteSize()
 117     static final List<IntFunction<Integer>> BYTE_INDEX_GENERATORS = List.of(
 118             withToString("-1", (int l) -> {
 119                 return -1;
 120             }),
 121             withToString("l", (int l) -> {
 122                 return l;
 123             }),
 124             withToString("l - 1", (int l) -> {
 125                 return l - 1;
 126             }),
 127             withToString("l + 1", (int l) -> {
 128                 return l + 1;
 129             }),
 130             withToString("l - speciesl*ebsize + 1", (int l) -> {
 131                 return l - SPECIES.vectorByteSize() + 1;
 132             }),
 133             withToString("l + speciesl*ebsize - 1", (int l) -> {
 134                 return l + SPECIES.vectorByteSize() - 1;
 135             }),
 136             withToString("l + speciesl*ebsize", (int l) -> {

 167     @DataProvider
 168     public Object[][] byteMaskProvider() {
 169         return BOOLEAN_MASK_GENERATORS.stream().
 170                 flatMap(fm -> BYTE_GENERATORS.stream().map(fa -> {
 171                     return new Object[] {fa, fm};
 172                 })).
 173                 toArray(Object[][]::new);
 174     }
 175 
 176     @DataProvider
 177     public Object[][] byteMaskProviderForIOOBE() {
 178         var f = BYTE_GENERATORS.get(0);
 179         return BOOLEAN_MASK_GENERATORS.stream().
 180                 flatMap(fm -> INDEX_GENERATORS.stream().map(fi -> {
 181                     return new Object[] {f, fi, fm};
 182                 })).
 183                 toArray(Object[][]::new);
 184     }
 185 
 186     @DataProvider
 187     public Object[][] byteMemorySegmentProvider() {
 188         return BYTE_GENERATORS.stream().
 189                 flatMap(fa -> MEMORY_SEGMENT_GENERATORS.stream().
 190                         flatMap(fb -> BYTE_ORDER_VALUES.stream().map(bo -> {
 191                             return new Object[]{fa, fb, bo};
 192                         }))).
 193                 toArray(Object[][]::new);
 194     }
 195 
 196     @DataProvider
 197     public Object[][] byteMemorySegmentMaskProvider() {
 198         return BOOLEAN_MASK_GENERATORS.stream().
 199                 flatMap(fm -> BYTE_GENERATORS.stream().
 200                         flatMap(fa -> MEMORY_SEGMENT_GENERATORS.stream().
 201                                 flatMap(fb -> BYTE_ORDER_VALUES.stream().map(bo -> {
 202                             return new Object[]{fa, fb, fm, bo};
 203                         })))).
 204                 toArray(Object[][]::new);
 205     }
 206 



















 207     @DataProvider
 208     public Object[][] byteByteProviderForIOOBE() {
 209         var f = BYTE_GENERATORS.get(0);
 210         return BYTE_INDEX_GENERATORS.stream().map(fi -> {
 211                     return new Object[] {f, fi};
 212                 }).
 213                 toArray(Object[][]::new);
 214     }
 215 
 216     @DataProvider
 217     public Object[][] byteByteMaskProviderForIOOBE() {
 218         var f = BYTE_GENERATORS.get(0);
 219         return BOOLEAN_MASK_GENERATORS.stream().
 220                 flatMap(fm -> BYTE_INDEX_GENERATORS.stream().map(fi -> {
 221                     return new Object[] {f, fi, fm};
 222                 })).
 223                 toArray(Object[][]::new);
 224     }
 225 
 226     static MemorySegment toSegment(byte[] a, IntFunction<MemorySegment> fb) {
 227         MemorySegment ms = fb.apply(a.length * SPECIES.elementSize() / 8);
 228         for (int i = 0; i < a.length; i++) {
 229             ms.set(ValueLayout.JAVA_BYTE, i * SPECIES.elementSize() / 8 , a[i]);
 230         }
 231         return ms;







 232     }
 233 
 234     static byte[] segmentToArray(MemorySegment ms) {
 235         return ms.toArray(ValueLayout.JAVA_BYTE);





 236     }
 237 
 238 
 239     interface ToByteF {
 240         byte apply(int i);
 241     }
 242 
 243     static byte[] fill(int s , ToByteF f) {
 244         return fill(new byte[s], f);
 245     }
 246 
 247     static byte[] fill(byte[] a, ToByteF f) {
 248         for (int i = 0; i < a.length; i++) {
 249             a[i] = f.apply(i);
 250         }
 251         return a;
 252     }
 253 
 254     @DontInline
 255     static ByteVector fromArray(byte[] a, int i) {
 256         return ByteVector.fromArray(SPECIES, a, i);
 257     }
 258 
 259     @DontInline
 260     static ByteVector fromArray(byte[] a, int i, VectorMask<Byte> m) {
 261         return ByteVector.fromArray(SPECIES, a, i, m);
 262     }
 263 
 264     @DontInline
 265     static void intoArray(ByteVector v, byte[] a, int i) {
 266         v.intoArray(a, i);
 267     }
 268 
 269     @DontInline
 270     static void intoArray(ByteVector v, byte[] a, int i, VectorMask<Byte> m) {
 271         v.intoArray(a, i, m);
 272     }
 273 
 274     @DontInline
 275     static ByteVector fromMemorySegment(MemorySegment a, int i, ByteOrder bo) {
 276         return ByteVector.fromMemorySegment(SPECIES, a, i, bo);
 277     }
 278 
 279     @DontInline
 280     static ByteVector fromMemorySegment(MemorySegment a, int i, ByteOrder bo, VectorMask<Byte> m) {
 281         return ByteVector.fromMemorySegment(SPECIES, a, i, bo, m);
 282     }
 283 
 284     @DontInline
 285     static void intoMemorySegment(ByteVector v, MemorySegment a, int i, ByteOrder bo) {
 286         v.intoMemorySegment(a, i, bo);
 287     }
 288 
 289     @DontInline
 290     static void intoMemorySegment(ByteVector v, MemorySegment a, int i, ByteOrder bo, VectorMask<Byte> m) {
 291         v.intoMemorySegment(a, i, bo, m);
 292     }
 293 





















 294     @Test(dataProvider = "byteProvider")
 295     static void loadStoreArray(IntFunction<byte[]> fa) {
 296         byte[] a = fa.apply(SPECIES.length());
 297         byte[] r = new byte[a.length];
 298 
 299         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 300             for (int i = 0; i < a.length; i += SPECIES.length()) {
 301                 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
 302                 av.intoArray(r, i);
 303             }
 304         }
 305         Assert.assertEquals(r, a);
 306     }
 307 
 308     @Test(dataProvider = "byteProviderForIOOBE")
 309     static void loadArrayIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi) {
 310         byte[] a = fa.apply(SPECIES.length());
 311         byte[] r = new byte[a.length];
 312 
 313         for (int ic = 0; ic < INVOC_COUNT; ic++) {

 444         }
 445     }
 446 
 447 
 448     @Test(dataProvider = "byteMaskProvider")
 449     static void loadStoreMask(IntFunction<byte[]> fa,
 450                               IntFunction<boolean[]> fm) {
 451         boolean[] mask = fm.apply(SPECIES.length());
 452         boolean[] r = new boolean[mask.length];
 453 
 454         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 455             for (int i = 0; i < mask.length; i += SPECIES.length()) {
 456                 VectorMask<Byte> vmask = VectorMask.fromArray(SPECIES, mask, i);
 457                 vmask.intoArray(r, i);
 458             }
 459         }
 460         Assert.assertEquals(r, mask);
 461     }
 462 
 463 
 464     @Test(dataProvider = "byteMemorySegmentProvider")
 465     static void loadStoreMemorySegment(IntFunction<byte[]> fa,
 466                                        IntFunction<MemorySegment> fb,
 467                                        ByteOrder bo) {
 468         MemorySegment a = toSegment(fa.apply(SPECIES.length()), fb);
 469         MemorySegment r = fb.apply((int) a.byteSize());
 470 
 471         int l = (int) a.byteSize();
 472         int s = SPECIES.vectorByteSize();
 473 
 474         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 475             for (int i = 0; i < l; i += s) {
 476                 ByteVector av = ByteVector.fromMemorySegment(SPECIES, a, i, bo);
 477                 av.intoMemorySegment(r, i, bo);
 478             }
 479         }
 480         long m = r.mismatch(a);
 481         Assert.assertEquals(m, -1, "Segments not equal");



 482     }
 483 
 484     @Test(dataProvider = "byteByteProviderForIOOBE")
 485     static void loadMemorySegmentIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi) {
 486         MemorySegment a = toSegment(fa.apply(SPECIES.length()), i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope()));
 487         MemorySegment r = MemorySegment.allocateNative(a.byteSize(), ResourceScope.newImplicitScope());
 488 
 489         int l = (int) a.byteSize();
 490         int s = SPECIES.vectorByteSize();
 491 
 492         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 493             for (int i = 0; i < l; i += s) {
 494                 ByteVector av = fromMemorySegment(a, i, ByteOrder.nativeOrder());
 495                 av.intoMemorySegment(r, i, ByteOrder.nativeOrder());
 496             }
 497         }
 498 
 499         int index = fi.apply((int) a.byteSize());
 500         boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, (int) a.byteSize());
 501         try {
 502             fromMemorySegment(a, index, ByteOrder.nativeOrder());
 503             if (shouldFail) {
 504                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 505             }
 506         } catch (IndexOutOfBoundsException e) {
 507             if (!shouldFail) {
 508                 Assert.fail("Unexpected IndexOutOfBoundsException");
 509             }
 510         }
 511     }
 512 
 513     @Test(dataProvider = "byteByteProviderForIOOBE")
 514     static void storeMemorySegmentIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi) {
 515         MemorySegment a = toSegment(fa.apply(SPECIES.length()), i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope()));
 516         MemorySegment r = MemorySegment.allocateNative(a.byteSize(), ResourceScope.newImplicitScope());
 517 
 518         int l = (int) a.byteSize();
 519         int s = SPECIES.vectorByteSize();
 520 
 521         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 522             for (int i = 0; i < l; i += s) {
 523                 ByteVector av = ByteVector.fromMemorySegment(SPECIES, a, i, ByteOrder.nativeOrder());
 524                 intoMemorySegment(av, r, i, ByteOrder.nativeOrder());
 525             }
 526         }
 527 
 528         int index = fi.apply((int) a.byteSize());
 529         boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, (int) a.byteSize());
 530         try {
 531             ByteVector av = ByteVector.fromMemorySegment(SPECIES, a, 0, ByteOrder.nativeOrder());
 532             intoMemorySegment(av, r, index, ByteOrder.nativeOrder());
 533             if (shouldFail) {
 534                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 535             }
 536         } catch (IndexOutOfBoundsException e) {
 537             if (!shouldFail) {
 538                 Assert.fail("Unexpected IndexOutOfBoundsException");
 539             }
 540         }
 541     }
 542 
 543     @Test(dataProvider = "byteMemorySegmentMaskProvider")
 544     static void loadStoreMemorySegmentMask(IntFunction<byte[]> fa,
 545                                            IntFunction<MemorySegment> fb,
 546                                            IntFunction<boolean[]> fm,
 547                                            ByteOrder bo) {

 548         byte[] _a = fa.apply(SPECIES.length());
 549         MemorySegment a = toSegment(_a, fb);
 550         MemorySegment r = fb.apply((int) a.byteSize());
 551         boolean[] mask = fm.apply(SPECIES.length());
 552         VectorMask<Byte> vmask = VectorMask.fromValues(SPECIES, mask);
 553 
 554         int l = (int) a.byteSize();
 555         int s = SPECIES.vectorByteSize();
 556 
 557         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 558             for (int i = 0; i < l; i += s) {
 559                 ByteVector av = ByteVector.fromMemorySegment(SPECIES, a, i, bo, vmask);
 560                 av.intoMemorySegment(r, i, bo);
 561             }
 562         }
 563         assertArraysEquals(segmentToArray(r), _a, mask);




 564 
 565 
 566         r = fb.apply((int) a.byteSize());
 567 
 568         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 569             for (int i = 0; i < l; i += s) {
 570                 ByteVector av = ByteVector.fromMemorySegment(SPECIES, a, i, bo);
 571                 av.intoMemorySegment(r, i, bo, vmask);
 572             }
 573         }
 574         assertArraysEquals(segmentToArray(r), _a, mask);




 575     }
 576 
 577     @Test(dataProvider = "byteByteMaskProviderForIOOBE")
 578     static void loadMemorySegmentMaskIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
 579         MemorySegment a = toSegment(fa.apply(SPECIES.length()), i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope()));
 580         MemorySegment r = MemorySegment.allocateNative(a.byteSize(), ResourceScope.newImplicitScope());
 581         boolean[] mask = fm.apply(SPECIES.length());
 582         VectorMask<Byte> vmask = VectorMask.fromValues(SPECIES, mask);
 583 
 584         int l = (int) a.byteSize();
 585         int s = SPECIES.vectorByteSize();
 586 
 587         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 588             for (int i = 0; i < l; i += s) {
 589                 ByteVector av = fromMemorySegment(a, i, ByteOrder.nativeOrder(), vmask);
 590                 av.intoMemorySegment(r, i, ByteOrder.nativeOrder());
 591             }
 592         }
 593 
 594         int index = fi.apply((int) a.byteSize());
 595         boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, (int) a.byteSize(), SPECIES.elementSize() / 8);
 596         try {
 597             fromMemorySegment(a, index, ByteOrder.nativeOrder(), vmask);
 598             if (shouldFail) {
 599                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 600             }
 601         } catch (IndexOutOfBoundsException e) {
 602             if (!shouldFail) {
 603                 Assert.fail("Unexpected IndexOutOfBoundsException");
 604             }
 605         }
 606     }
 607 
 608     @Test(dataProvider = "byteByteMaskProviderForIOOBE")
 609     static void storeMemorySegmentMaskIOOBE(IntFunction<byte[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
 610         MemorySegment a = toSegment(fa.apply(SPECIES.length()), i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope()));
 611         MemorySegment r = MemorySegment.allocateNative(a.byteSize(), ResourceScope.newImplicitScope());
 612         boolean[] mask = fm.apply(SPECIES.length());
 613         VectorMask<Byte> vmask = VectorMask.fromValues(SPECIES, mask);
 614 
 615         int l = (int) a.byteSize();















































































































 616         int s = SPECIES.vectorByteSize();

 617 
 618         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 619             for (int i = 0; i < l; i += s) {
 620                 ByteVector av = ByteVector.fromMemorySegment(SPECIES, a, i, ByteOrder.nativeOrder());
 621                 intoMemorySegment(av, r, i, ByteOrder.nativeOrder(), vmask);
 622             }
 623         }
 624 
 625         int index = fi.apply((int) a.byteSize());
 626         boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, (int) a.byteSize(), SPECIES.elementSize() / 8);
 627         try {
 628             ByteVector av = ByteVector.fromMemorySegment(SPECIES, a, 0, ByteOrder.nativeOrder());
 629             intoMemorySegment(av, a, index, ByteOrder.nativeOrder(), vmask);
 630             if (shouldFail) {
 631                 Assert.fail("Failed to throw IndexOutOfBoundsException");
 632             }
 633         } catch (IndexOutOfBoundsException e) {
 634             if (!shouldFail) {
 635                 Assert.fail("Unexpected IndexOutOfBoundsException");
 636             }
 637         }
 638     }
 639 
 640     @Test(dataProvider = "byteMemorySegmentProvider")
 641     static void loadStoreReadonlyMemorySegment(IntFunction<byte[]> fa,
 642                                                IntFunction<MemorySegment> fb,
 643                                                ByteOrder bo) {
 644         MemorySegment a = toSegment(fa.apply(SPECIES.length()), fb).asReadOnly();
 645 
 646         Assert.assertThrows(
 647                 UnsupportedOperationException.class,
 648                 () -> SPECIES.zero().intoMemorySegment(a, 0, bo)
 649         );


































 650 
 651         Assert.assertThrows(
 652                 UnsupportedOperationException.class,
 653                 () -> SPECIES.zero().intoMemorySegment(a, 0, bo, SPECIES.maskAll(true))
 654         );
 655 
 656         Assert.assertThrows(
 657                 UnsupportedOperationException.class,
 658                 () -> SPECIES.zero().intoMemorySegment(a, 0, bo, SPECIES.maskAll(false))
 659         );


 660 
 661         VectorMask<Byte> m = SPECIES.shuffleFromOp(i -> i % 2 == 0 ? 1 : -1)
 662                 .laneIsValid();
 663         Assert.assertThrows(
 664                 UnsupportedOperationException.class,
 665                 () -> SPECIES.zero().intoMemorySegment(a, 0, bo, m)
 666         );






 667     }
 668 































 669 
 670     @Test(dataProvider = "maskProvider")
 671     static void loadStoreMask(IntFunction<boolean[]> fm) {
 672         boolean[] a = fm.apply(SPECIES.length());
 673         boolean[] r = new boolean[a.length];
 674 
 675         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 676             for (int i = 0; i < a.length; i += SPECIES.length()) {
 677                 VectorMask<Byte> vmask = SPECIES.loadMask(a, i);
 678                 vmask.intoArray(r, i);
 679             }
 680         }
 681         Assert.assertEquals(r, a);
 682     }
 683 
 684 
 685     @Test
 686     static void loadStoreShuffle() {
 687         IntUnaryOperator fn = a -> a + 5;
 688         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 689             var shuffle = VectorShuffle.fromOp(SPECIES, fn);
 690             int [] r = shuffle.toArray();
 691 
 692             int [] a = expectedShuffle(SPECIES.length(), fn);
 693             Assert.assertEquals(r, a);
 694        }
 695     }
 696 
 697 
 698 
 699     static void assertArraysEquals(boolean[] r, byte[] a) {
 700         int i = 0;
 701         try {
 702             for (; i < a.length; i++) {
 703                 Assert.assertEquals(r[i], (a[i] & 1) == 1);
 704             }
< prev index next >