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 -XX:-TieredCompilation Long512VectorLoadStoreTests
28 *
29 */
30
31 // -- This file was mechanically generated: Do not edit! -- //
32
33 import jdk.incubator.vector.LongVector;
34 import jdk.incubator.vector.VectorMask;
35 import jdk.incubator.vector.VectorSpecies;
36 import jdk.incubator.vector.VectorShuffle;
37 import jdk.internal.vm.annotation.DontInline;
38 import org.testng.Assert;
39 import org.testng.annotations.DataProvider;
40 import org.testng.annotations.Test;
41
42 import java.nio.ByteBuffer;
43 import java.nio.LongBuffer;
44 import java.nio.ByteOrder;
45 import java.nio.ReadOnlyBufferException;
46 import java.util.List;
47 import java.util.function.*;
48
49 @Test
50 public class Long512VectorLoadStoreTests extends AbstractVectorLoadStoreTest {
51 static final VectorSpecies<Long> SPECIES =
52 LongVector.SPECIES_512;
53
54 static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
55
56
57 static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / 512);
58
59 static void assertArraysEquals(long[] r, long[] a, boolean[] mask) {
60 int i = 0;
61 try {
62 for (; i < a.length; i++) {
63 Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? a[i] : (long) 0);
64 }
65 } catch (AssertionError e) {
66 Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? a[i] : (long) 0, "at index #" + i);
67 }
68 }
69
70 static void assertArraysEquals(byte[] r, byte[] a, boolean[] mask) {
71 int i = 0;
72 try {
73 for (; i < a.length; i++) {
74 Assert.assertEquals(r[i], mask[(i*8/SPECIES.elementSize()) % SPECIES.length()] ? a[i] : (byte) 0);
75 }
76 } catch (AssertionError e) {
77 Assert.assertEquals(r[i], mask[(i*8/SPECIES.elementSize()) % SPECIES.length()] ? a[i] : (byte) 0, "at index #" + i);
78 }
79 }
80
81 static final List<IntFunction<long[]>> LONG_GENERATORS = List.of(
82 withToString("long[i * 5]", (int s) -> {
83 return fill(s * BUFFER_REPS,
84 i -> (long)(i * 5));
85 }),
86 withToString("long[i + 1]", (int s) -> {
87 return fill(s * BUFFER_REPS,
88 i -> (((long)(i + 1) == 0) ? 1 : (long)(i + 1)));
89 })
90 );
91
92 // Relative to array.length
93 static final List<IntFunction<Integer>> INDEX_GENERATORS = List.of(
94 withToString("-1", (int l) -> {
95 return -1;
96 }),
97 withToString("l", (int l) -> {
98 return l;
99 }),
100 withToString("l - 1", (int l) -> {
101 return l - 1;
102 }),
103 withToString("l + 1", (int l) -> {
104 return l + 1;
105 }),
106 withToString("l - speciesl + 1", (int l) -> {
107 return l - SPECIES.length() + 1;
108 }),
109 withToString("l + speciesl - 1", (int l) -> {
110 return l + SPECIES.length() - 1;
111 }),
112 withToString("l + speciesl", (int l) -> {
113 return l + SPECIES.length();
114 }),
115 withToString("l + speciesl + 1", (int l) -> {
116 return l + SPECIES.length() + 1;
117 })
118 );
119
120 // Relative to byte[] array.length or ByteBuffer.limit()
121 static final List<IntFunction<Integer>> BYTE_INDEX_GENERATORS = List.of(
122 withToString("-1", (int l) -> {
123 return -1;
124 }),
125 withToString("l", (int l) -> {
126 return l;
127 }),
128 withToString("l - 1", (int l) -> {
129 return l - 1;
130 }),
131 withToString("l + 1", (int l) -> {
132 return l + 1;
133 }),
134 withToString("l - speciesl*ebsize + 1", (int l) -> {
135 return l - SPECIES.vectorByteSize() + 1;
136 }),
137 withToString("l + speciesl*ebsize - 1", (int l) -> {
138 return l + SPECIES.vectorByteSize() - 1;
139 }),
140 withToString("l + speciesl*ebsize", (int l) -> {
171 @DataProvider
172 public Object[][] longMaskProvider() {
173 return BOOLEAN_MASK_GENERATORS.stream().
174 flatMap(fm -> LONG_GENERATORS.stream().map(fa -> {
175 return new Object[] {fa, fm};
176 })).
177 toArray(Object[][]::new);
178 }
179
180 @DataProvider
181 public Object[][] longMaskProviderForIOOBE() {
182 var f = LONG_GENERATORS.get(0);
183 return BOOLEAN_MASK_GENERATORS.stream().
184 flatMap(fm -> INDEX_GENERATORS.stream().map(fi -> {
185 return new Object[] {f, fi, fm};
186 })).
187 toArray(Object[][]::new);
188 }
189
190 @DataProvider
191 public Object[][] longByteBufferProvider() {
192 return LONG_GENERATORS.stream().
193 flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().
194 flatMap(fb -> BYTE_ORDER_VALUES.stream().map(bo -> {
195 return new Object[]{fa, fb, bo};
196 }))).
197 toArray(Object[][]::new);
198 }
199
200 @DataProvider
201 public Object[][] longByteBufferMaskProvider() {
202 return BOOLEAN_MASK_GENERATORS.stream().
203 flatMap(fm -> LONG_GENERATORS.stream().
204 flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().
205 flatMap(fb -> BYTE_ORDER_VALUES.stream().map(bo -> {
206 return new Object[]{fa, fb, fm, bo};
207 })))).
208 toArray(Object[][]::new);
209 }
210
211 @DataProvider
212 public Object[][] longByteArrayProvider() {
213 return LONG_GENERATORS.stream().
214 flatMap(fa -> BYTE_ORDER_VALUES.stream().map(bo -> {
215 return new Object[]{fa, bo};
216 })).
217 toArray(Object[][]::new);
218 }
219
220 @DataProvider
221 public Object[][] longByteArrayMaskProvider() {
222 return BOOLEAN_MASK_GENERATORS.stream().
223 flatMap(fm -> LONG_GENERATORS.stream().
224 flatMap(fa -> BYTE_ORDER_VALUES.stream().map(bo -> {
225 return new Object[]{fa, fm, bo};
226 }))).
227 toArray(Object[][]::new);
228 }
229
230 @DataProvider
231 public Object[][] longByteProviderForIOOBE() {
232 var f = LONG_GENERATORS.get(0);
233 return BYTE_INDEX_GENERATORS.stream().map(fi -> {
234 return new Object[] {f, fi};
235 }).
236 toArray(Object[][]::new);
237 }
238
239 @DataProvider
240 public Object[][] longByteMaskProviderForIOOBE() {
241 var f = LONG_GENERATORS.get(0);
242 return BOOLEAN_MASK_GENERATORS.stream().
243 flatMap(fm -> BYTE_INDEX_GENERATORS.stream().map(fi -> {
244 return new Object[] {f, fi, fm};
245 })).
246 toArray(Object[][]::new);
247 }
248
249 static ByteBuffer toBuffer(long[] a, IntFunction<ByteBuffer> fb) {
250 ByteBuffer bb = fb.apply(a.length * SPECIES.elementSize() / 8);
251 for (long v : a) {
252 bb.putLong(v);
253 }
254 return bb.clear();
255 }
256
257 static long[] bufferToArray(ByteBuffer bb) {
258 LongBuffer db = bb.asLongBuffer();
259 long[] d = new long[db.capacity()];
260 db.get(0, d);
261 return d;
262 }
263
264 static byte[] toByteArray(long[] a, IntFunction<byte[]> fb, ByteOrder bo) {
265 byte[] b = fb.apply(a.length * SPECIES.elementSize() / 8);
266 LongBuffer bb = ByteBuffer.wrap(b, 0, b.length).order(bo).asLongBuffer();
267 for (long v : a) {
268 bb.put(v);
269 }
270 return b;
271 }
272
273
274 interface ToLongF {
275 long apply(int i);
276 }
277
278 static long[] fill(int s , ToLongF f) {
279 return fill(new long[s], f);
280 }
281
282 static long[] fill(long[] a, ToLongF f) {
283 for (int i = 0; i < a.length; i++) {
284 a[i] = f.apply(i);
285 }
286 return a;
287 }
288
289 @DontInline
290 static LongVector fromArray(long[] a, int i) {
291 return LongVector.fromArray(SPECIES, a, i);
292 }
293
294 @DontInline
295 static LongVector fromArray(long[] a, int i, VectorMask<Long> m) {
296 return LongVector.fromArray(SPECIES, a, i, m);
297 }
298
299 @DontInline
300 static void intoArray(LongVector v, long[] a, int i) {
301 v.intoArray(a, i);
302 }
303
304 @DontInline
305 static void intoArray(LongVector v, long[] a, int i, VectorMask<Long> m) {
306 v.intoArray(a, i, m);
307 }
308
309 @DontInline
310 static LongVector fromByteArray(byte[] a, int i, ByteOrder bo) {
311 return LongVector.fromByteArray(SPECIES, a, i, bo);
312 }
313
314 @DontInline
315 static LongVector fromByteArray(byte[] a, int i, ByteOrder bo, VectorMask<Long> m) {
316 return LongVector.fromByteArray(SPECIES, a, i, bo, m);
317 }
318
319 @DontInline
320 static void intoByteArray(LongVector v, byte[] a, int i, ByteOrder bo) {
321 v.intoByteArray(a, i, bo);
322 }
323
324 @DontInline
325 static void intoByteArray(LongVector v, byte[] a, int i, ByteOrder bo, VectorMask<Long> m) {
326 v.intoByteArray(a, i, bo, m);
327 }
328
329 @DontInline
330 static LongVector fromByteBuffer(ByteBuffer a, int i, ByteOrder bo) {
331 return LongVector.fromByteBuffer(SPECIES, a, i, bo);
332 }
333
334 @DontInline
335 static LongVector fromByteBuffer(ByteBuffer a, int i, ByteOrder bo, VectorMask<Long> m) {
336 return LongVector.fromByteBuffer(SPECIES, a, i, bo, m);
337 }
338
339 @DontInline
340 static void intoByteBuffer(LongVector v, ByteBuffer a, int i, ByteOrder bo) {
341 v.intoByteBuffer(a, i, bo);
342 }
343
344 @DontInline
345 static void intoByteBuffer(LongVector v, ByteBuffer a, int i, ByteOrder bo, VectorMask<Long> m) {
346 v.intoByteBuffer(a, i, bo, m);
347 }
348
349
350 @Test(dataProvider = "longProvider")
351 static void loadStoreArray(IntFunction<long[]> fa) {
352 long[] a = fa.apply(SPECIES.length());
353 long[] r = new long[a.length];
354
355 for (int ic = 0; ic < INVOC_COUNT; ic++) {
356 for (int i = 0; i < a.length; i += SPECIES.length()) {
357 LongVector av = LongVector.fromArray(SPECIES, a, i);
358 av.intoArray(r, i);
359 }
360 }
361 Assert.assertEquals(r, a);
362 }
363
364 @Test(dataProvider = "longProviderForIOOBE")
365 static void loadArrayIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi) {
366 long[] a = fa.apply(SPECIES.length());
367 long[] r = new long[a.length];
368
369 for (int ic = 0; ic < INVOC_COUNT; ic++) {
500 }
501 }
502
503
504 @Test(dataProvider = "longMaskProvider")
505 static void loadStoreMask(IntFunction<long[]> fa,
506 IntFunction<boolean[]> fm) {
507 boolean[] mask = fm.apply(SPECIES.length());
508 boolean[] r = new boolean[mask.length];
509
510 for (int ic = 0; ic < INVOC_COUNT; ic++) {
511 for (int i = 0; i < mask.length; i += SPECIES.length()) {
512 VectorMask<Long> vmask = VectorMask.fromArray(SPECIES, mask, i);
513 vmask.intoArray(r, i);
514 }
515 }
516 Assert.assertEquals(r, mask);
517 }
518
519
520 @Test(dataProvider = "longByteBufferProvider")
521 static void loadStoreByteBuffer(IntFunction<long[]> fa,
522 IntFunction<ByteBuffer> fb,
523 ByteOrder bo) {
524 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb);
525 ByteBuffer r = fb.apply(a.limit());
526
527 int l = a.limit();
528 int s = SPECIES.vectorByteSize();
529
530 for (int ic = 0; ic < INVOC_COUNT; ic++) {
531 for (int i = 0; i < l; i += s) {
532 LongVector av = LongVector.fromByteBuffer(SPECIES, a, i, bo);
533 av.intoByteBuffer(r, i, bo);
534 }
535 }
536 Assert.assertEquals(a.position(), 0, "Input buffer position changed");
537 Assert.assertEquals(a.limit(), l, "Input buffer limit changed");
538 Assert.assertEquals(r.position(), 0, "Result buffer position changed");
539 Assert.assertEquals(r.limit(), l, "Result buffer limit changed");
540 Assert.assertEquals(r, a, "Buffers not equal");
541 }
542
543 @Test(dataProvider = "longByteProviderForIOOBE")
544 static void loadByteBufferIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi) {
545 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), ByteBuffer::allocateDirect);
546 ByteBuffer r = ByteBuffer.allocateDirect(a.limit());
547
548 int l = a.limit();
549 int s = SPECIES.vectorByteSize();
550
551 for (int ic = 0; ic < INVOC_COUNT; ic++) {
552 for (int i = 0; i < l; i += s) {
553 LongVector av = fromByteBuffer(a, i, ByteOrder.nativeOrder());
554 av.intoByteBuffer(r, i, ByteOrder.nativeOrder());
555 }
556 }
557
558 int index = fi.apply(a.limit());
559 boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, a.limit());
560 try {
561 fromByteBuffer(a, index, ByteOrder.nativeOrder());
562 if (shouldFail) {
563 Assert.fail("Failed to throw IndexOutOfBoundsException");
564 }
565 } catch (IndexOutOfBoundsException e) {
566 if (!shouldFail) {
567 Assert.fail("Unexpected IndexOutOfBoundsException");
568 }
569 }
570 }
571
572 @Test(dataProvider = "longByteProviderForIOOBE")
573 static void storeByteBufferIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi) {
574 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), ByteBuffer::allocateDirect);
575 ByteBuffer r = ByteBuffer.allocateDirect(a.limit());
576
577 int l = a.limit();
578 int s = SPECIES.vectorByteSize();
579
580 for (int ic = 0; ic < INVOC_COUNT; ic++) {
581 for (int i = 0; i < l; i += s) {
582 LongVector av = LongVector.fromByteBuffer(SPECIES, a, i, ByteOrder.nativeOrder());
583 intoByteBuffer(av, r, i, ByteOrder.nativeOrder());
584 }
585 }
586
587 int index = fi.apply(a.limit());
588 boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, a.limit());
589 try {
590 LongVector av = LongVector.fromByteBuffer(SPECIES, a, 0, ByteOrder.nativeOrder());
591 intoByteBuffer(av, r, index, ByteOrder.nativeOrder());
592 if (shouldFail) {
593 Assert.fail("Failed to throw IndexOutOfBoundsException");
594 }
595 } catch (IndexOutOfBoundsException e) {
596 if (!shouldFail) {
597 Assert.fail("Unexpected IndexOutOfBoundsException");
598 }
599 }
600 }
601
602
603 @Test(dataProvider = "longByteBufferMaskProvider")
604 static void loadStoreByteBufferMask(IntFunction<long[]> fa,
605 IntFunction<ByteBuffer> fb,
606 IntFunction<boolean[]> fm,
607 ByteOrder bo) {
608 long[] _a = fa.apply(SPECIES.length());
609 ByteBuffer a = toBuffer(_a, fb);
610 ByteBuffer r = fb.apply(a.limit());
611 boolean[] mask = fm.apply(SPECIES.length());
612 VectorMask<Long> vmask = VectorMask.fromValues(SPECIES, mask);
613
614 int l = a.limit();
615 int s = SPECIES.vectorByteSize();
616
617 for (int ic = 0; ic < INVOC_COUNT; ic++) {
618 for (int i = 0; i < l; i += s) {
619 LongVector av = LongVector.fromByteBuffer(SPECIES, a, i, bo, vmask);
620 av.intoByteBuffer(r, i, bo);
621 }
622 }
623 Assert.assertEquals(a.position(), 0, "Input buffer position changed");
624 Assert.assertEquals(a.limit(), l, "Input buffer limit changed");
625 Assert.assertEquals(r.position(), 0, "Result buffer position changed");
626 Assert.assertEquals(r.limit(), l, "Result buffer limit changed");
627 assertArraysEquals(bufferToArray(r), _a, mask);
628
629
630 r = fb.apply(a.limit());
631
632 for (int ic = 0; ic < INVOC_COUNT; ic++) {
633 for (int i = 0; i < l; i += s) {
634 LongVector av = LongVector.fromByteBuffer(SPECIES, a, i, bo);
635 av.intoByteBuffer(r, i, bo, vmask);
636 }
637 }
638 Assert.assertEquals(a.position(), 0, "Input buffer position changed");
639 Assert.assertEquals(a.limit(), l, "Input buffer limit changed");
640 Assert.assertEquals(r.position(), 0, "Result buffer position changed");
641 Assert.assertEquals(r.limit(), l, "Result buffer limit changed");
642 assertArraysEquals(bufferToArray(r), _a, mask);
643 }
644
645 @Test(dataProvider = "longByteMaskProviderForIOOBE")
646 static void loadByteBufferMaskIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
647 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), ByteBuffer::allocateDirect);
648 ByteBuffer r = ByteBuffer.allocateDirect(a.limit());
649 boolean[] mask = fm.apply(SPECIES.length());
650 VectorMask<Long> vmask = VectorMask.fromValues(SPECIES, mask);
651
652 int l = a.limit();
653 int s = SPECIES.vectorByteSize();
654
655 for (int ic = 0; ic < INVOC_COUNT; ic++) {
656 for (int i = 0; i < l; i += s) {
657 LongVector av = fromByteBuffer(a, i, ByteOrder.nativeOrder(), vmask);
658 av.intoByteBuffer(r, i, ByteOrder.nativeOrder());
659 }
660 }
661
662 int index = fi.apply(a.limit());
663 boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, a.limit(), SPECIES.elementSize() / 8);
664 try {
665 fromByteBuffer(a, index, ByteOrder.nativeOrder(), vmask);
666 if (shouldFail) {
667 Assert.fail("Failed to throw IndexOutOfBoundsException");
668 }
669 } catch (IndexOutOfBoundsException e) {
670 if (!shouldFail) {
671 Assert.fail("Unexpected IndexOutOfBoundsException");
672 }
673 }
674 }
675
676 @Test(dataProvider = "longByteMaskProviderForIOOBE")
677 static void storeByteBufferMaskIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
678 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), ByteBuffer::allocateDirect);
679 ByteBuffer r = ByteBuffer.allocateDirect(a.limit());
680 boolean[] mask = fm.apply(SPECIES.length());
681 VectorMask<Long> vmask = VectorMask.fromValues(SPECIES, mask);
682
683 int l = a.limit();
684 int s = SPECIES.vectorByteSize();
685
686 for (int ic = 0; ic < INVOC_COUNT; ic++) {
687 for (int i = 0; i < l; i += s) {
688 LongVector av = LongVector.fromByteBuffer(SPECIES, a, i, ByteOrder.nativeOrder());
689 intoByteBuffer(av, r, i, ByteOrder.nativeOrder(), vmask);
690 }
691 }
692
693 int index = fi.apply(a.limit());
694 boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, a.limit(), SPECIES.elementSize() / 8);
695 try {
696 LongVector av = LongVector.fromByteBuffer(SPECIES, a, 0, ByteOrder.nativeOrder());
697 intoByteBuffer(av, a, index, ByteOrder.nativeOrder(), vmask);
698 if (shouldFail) {
699 Assert.fail("Failed to throw IndexOutOfBoundsException");
700 }
701 } catch (IndexOutOfBoundsException e) {
702 if (!shouldFail) {
703 Assert.fail("Unexpected IndexOutOfBoundsException");
704 }
705 }
706 }
707
708
709 @Test(dataProvider = "longByteBufferProvider")
710 static void loadStoreReadonlyByteBuffer(IntFunction<long[]> fa,
711 IntFunction<ByteBuffer> fb,
712 ByteOrder bo) {
713 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb).asReadOnlyBuffer();
714
715 try {
716 SPECIES.zero().intoByteBuffer(a, 0, bo);
717 Assert.fail("ReadOnlyBufferException expected");
718 } catch (ReadOnlyBufferException e) {
719 }
720
721 try {
722 SPECIES.zero().intoByteBuffer(a, 0, bo, SPECIES.maskAll(true));
723 Assert.fail("ReadOnlyBufferException expected");
724 } catch (ReadOnlyBufferException e) {
725 }
726
727 try {
728 SPECIES.zero().intoByteBuffer(a, 0, bo, SPECIES.maskAll(false));
729 Assert.fail("ReadOnlyBufferException expected");
730 } catch (ReadOnlyBufferException e) {
731 }
732
733 try {
734 VectorMask<Long> m = SPECIES.shuffleFromOp(i -> i % 2 == 0 ? 1 : -1)
735 .laneIsValid();
736 SPECIES.zero().intoByteBuffer(a, 0, bo, m);
737 Assert.fail("ReadOnlyBufferException expected");
738 } catch (ReadOnlyBufferException e) {
739 }
740 }
741
742
743 @Test(dataProvider = "longByteArrayProvider")
744 static void loadStoreByteArray(IntFunction<long[]> fa,
745 ByteOrder bo) {
746 byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, bo);
747 byte[] r = new byte[a.length];
748
749 int s = SPECIES.vectorByteSize();
750 int l = a.length;
751
752 for (int ic = 0; ic < INVOC_COUNT; ic++) {
753 for (int i = 0; i < l; i += s) {
754 LongVector av = LongVector.fromByteArray(SPECIES, a, i, bo);
755 av.intoByteArray(r, i, bo);
756 }
757 }
758 Assert.assertEquals(r, a, "Byte arrays not equal");
759 }
760
761 @Test(dataProvider = "longByteProviderForIOOBE")
762 static void loadByteArrayIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi) {
763 byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, ByteOrder.nativeOrder());
764 byte[] r = new byte[a.length];
765
766 int s = SPECIES.vectorByteSize();
767 int l = a.length;
768
769 for (int ic = 0; ic < INVOC_COUNT; ic++) {
770 for (int i = 0; i < l; i += s) {
771 LongVector av = fromByteArray(a, i, ByteOrder.nativeOrder());
772 av.intoByteArray(r, i, ByteOrder.nativeOrder());
773 }
774 }
775
776 int index = fi.apply(a.length);
777 boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, a.length);
778 try {
779 fromByteArray(a, index, ByteOrder.nativeOrder());
780 if (shouldFail) {
781 Assert.fail("Failed to throw IndexOutOfBoundsException");
782 }
783 } catch (IndexOutOfBoundsException e) {
784 if (!shouldFail) {
785 Assert.fail("Unexpected IndexOutOfBoundsException");
786 }
787 }
788 }
789
790 @Test(dataProvider = "longByteProviderForIOOBE")
791 static void storeByteArrayIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi) {
792 byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, ByteOrder.nativeOrder());
793 byte[] r = new byte[a.length];
794
795 int s = SPECIES.vectorByteSize();
796 int l = a.length;
797
798 for (int ic = 0; ic < INVOC_COUNT; ic++) {
799 for (int i = 0; i < l; i += s) {
800 LongVector av = LongVector.fromByteArray(SPECIES, a, i, ByteOrder.nativeOrder());
801 intoByteArray(av, r, i, ByteOrder.nativeOrder());
802 }
803 }
804
805 int index = fi.apply(a.length);
806 boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, a.length);
807 try {
808 LongVector av = LongVector.fromByteArray(SPECIES, a, 0, ByteOrder.nativeOrder());
809 intoByteArray(av, r, index, ByteOrder.nativeOrder());
810 if (shouldFail) {
811 Assert.fail("Failed to throw IndexOutOfBoundsException");
812 }
813 } catch (IndexOutOfBoundsException e) {
814 if (!shouldFail) {
815 Assert.fail("Unexpected IndexOutOfBoundsException");
816 }
817 }
818 }
819
820
821 @Test(dataProvider = "longByteArrayMaskProvider")
822 static void loadStoreByteArrayMask(IntFunction<long[]> fa,
823 IntFunction<boolean[]> fm,
824 ByteOrder bo) {
825 byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, bo);
826 byte[] r = new byte[a.length];
827 boolean[] mask = fm.apply(SPECIES.length());
828 VectorMask<Long> vmask = VectorMask.fromValues(SPECIES, mask);
829
830 int s = SPECIES.vectorByteSize();
831 int l = a.length;
832
833 for (int ic = 0; ic < INVOC_COUNT; ic++) {
834 for (int i = 0; i < l; i += s) {
835 LongVector av = LongVector.fromByteArray(SPECIES, a, i, bo, vmask);
836 av.intoByteArray(r, i, bo);
837 }
838 }
839 assertArraysEquals(r, a, mask);
840
841
842 r = new byte[a.length];
843
844 for (int ic = 0; ic < INVOC_COUNT; ic++) {
845 for (int i = 0; i < l; i += s) {
846 LongVector av = LongVector.fromByteArray(SPECIES, a, i, bo);
847 av.intoByteArray(r, i, bo, vmask);
848 }
849 }
850 assertArraysEquals(r, a, mask);
851 }
852
853 @Test(dataProvider = "longByteMaskProviderForIOOBE")
854 static void loadByteArrayMaskIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
855 byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, ByteOrder.nativeOrder());
856 byte[] r = new byte[a.length];
857 boolean[] mask = fm.apply(SPECIES.length());
858 VectorMask<Long> vmask = VectorMask.fromValues(SPECIES, mask);
859
860 int s = SPECIES.vectorByteSize();
861 int l = a.length;
862
863 for (int ic = 0; ic < INVOC_COUNT; ic++) {
864 for (int i = 0; i < l; i += s) {
865 LongVector av = fromByteArray(a, i, ByteOrder.nativeOrder(), vmask);
866 av.intoByteArray(r, i, ByteOrder.nativeOrder());
867 }
868 }
869
870 int index = fi.apply(a.length);
871 boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, a.length, SPECIES.elementSize() / 8);
872 try {
873 fromByteArray(a, index, ByteOrder.nativeOrder(), vmask);
874 if (shouldFail) {
875 Assert.fail("Failed to throw IndexOutOfBoundsException");
876 }
877 } catch (IndexOutOfBoundsException e) {
878 if (!shouldFail) {
879 Assert.fail("Unexpected IndexOutOfBoundsException");
880 }
881 }
882 }
883
884 @Test(dataProvider = "longByteMaskProviderForIOOBE")
885 static void storeByteArrayMaskIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
886 byte[] a = toByteArray(fa.apply(SPECIES.length()), byte[]::new, ByteOrder.nativeOrder());
887 byte[] r = new byte[a.length];
888 boolean[] mask = fm.apply(SPECIES.length());
889 VectorMask<Long> vmask = VectorMask.fromValues(SPECIES, mask);
890
891 int s = SPECIES.vectorByteSize();
892 int l = a.length;
893
894 for (int ic = 0; ic < INVOC_COUNT; ic++) {
895 for (int i = 0; i < l; i += s) {
896 LongVector av = LongVector.fromByteArray(SPECIES, a, i, ByteOrder.nativeOrder());
897 intoByteArray(av, r, i, ByteOrder.nativeOrder(), vmask);
898 }
899 }
900
901 int index = fi.apply(a.length);
902 boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, a.length, SPECIES.elementSize() / 8);
903 try {
904 LongVector av = LongVector.fromByteArray(SPECIES, a, 0, ByteOrder.nativeOrder());
905 intoByteArray(av, a, index, ByteOrder.nativeOrder(), vmask);
906 if (shouldFail) {
907 Assert.fail("Failed to throw IndexOutOfBoundsException");
908 }
909 } catch (IndexOutOfBoundsException e) {
910 if (!shouldFail) {
911 Assert.fail("Unexpected IndexOutOfBoundsException");
912 }
913 }
914 }
915
916 @Test(dataProvider = "maskProvider")
917 static void loadStoreMask(IntFunction<boolean[]> fm) {
918 boolean[] a = fm.apply(SPECIES.length());
919 boolean[] r = new boolean[a.length];
920
921 for (int ic = 0; ic < INVOC_COUNT; ic++) {
922 for (int i = 0; i < a.length; i += SPECIES.length()) {
923 VectorMask<Long> vmask = SPECIES.loadMask(a, i);
924 vmask.intoArray(r, i);
925 }
926 }
927 Assert.assertEquals(r, a);
928 }
929
930 @Test
931 static void loadStoreShuffle() {
932 IntUnaryOperator fn = a -> a + 5;
933 for (int ic = 0; ic < INVOC_COUNT; ic++) {
934 var shuffle = VectorShuffle.fromOp(SPECIES, fn);
935 int [] r = shuffle.toArray();
936
937 int [] a = expectedShuffle(SPECIES.length(), fn);
938 Assert.assertEquals(r, a);
939 }
940 }
941
942
943
944
945
946 // Gather/Scatter load/store tests
947
948 static void assertGatherArraysEquals(long[] r, long[] a, int[] indexMap) {
949 int i = 0;
|
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 -XX:-TieredCompilation Long512VectorLoadStoreTests
28 *
29 */
30
31 // -- This file was mechanically generated: Do not edit! -- //
32
33 import jdk.incubator.foreign.MemorySegment;
34 import jdk.incubator.foreign.ResourceScope;
35 import jdk.incubator.foreign.ValueLayout;
36 import jdk.incubator.vector.LongVector;
37 import jdk.incubator.vector.VectorMask;
38 import jdk.incubator.vector.VectorSpecies;
39 import jdk.incubator.vector.VectorShuffle;
40 import jdk.internal.vm.annotation.DontInline;
41 import org.testng.Assert;
42 import org.testng.annotations.DataProvider;
43 import org.testng.annotations.Test;
44
45 import java.nio.ByteOrder;
46 import java.util.List;
47 import java.util.function.*;
48
49 @Test
50 public class Long512VectorLoadStoreTests extends AbstractVectorLoadStoreTest {
51 static final VectorSpecies<Long> SPECIES =
52 LongVector.SPECIES_512;
53
54 static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
55
56
57 static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / 512);
58
59 static void assertArraysEquals(long[] r, long[] a, boolean[] mask) {
60 int i = 0;
61 try {
62 for (; i < a.length; i++) {
63 Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? a[i] : (long) 0);
64 }
65 } catch (AssertionError e) {
66 Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? a[i] : (long) 0, "at index #" + i);
67 }
68 }
69
70 static final List<IntFunction<long[]>> LONG_GENERATORS = List.of(
71 withToString("long[i * 5]", (int s) -> {
72 return fill(s * BUFFER_REPS,
73 i -> (long)(i * 5));
74 }),
75 withToString("long[i + 1]", (int s) -> {
76 return fill(s * BUFFER_REPS,
77 i -> (((long)(i + 1) == 0) ? 1 : (long)(i + 1)));
78 })
79 );
80
81 // Relative to array.length
82 static final List<IntFunction<Integer>> INDEX_GENERATORS = List.of(
83 withToString("-1", (int l) -> {
84 return -1;
85 }),
86 withToString("l", (int l) -> {
87 return l;
88 }),
89 withToString("l - 1", (int l) -> {
90 return l - 1;
91 }),
92 withToString("l + 1", (int l) -> {
93 return l + 1;
94 }),
95 withToString("l - speciesl + 1", (int l) -> {
96 return l - SPECIES.length() + 1;
97 }),
98 withToString("l + speciesl - 1", (int l) -> {
99 return l + SPECIES.length() - 1;
100 }),
101 withToString("l + speciesl", (int l) -> {
102 return l + SPECIES.length();
103 }),
104 withToString("l + speciesl + 1", (int l) -> {
105 return l + SPECIES.length() + 1;
106 })
107 );
108
109 // Relative to byte[] array.length or MemorySegment.byteSize()
110 static final List<IntFunction<Integer>> BYTE_INDEX_GENERATORS = List.of(
111 withToString("-1", (int l) -> {
112 return -1;
113 }),
114 withToString("l", (int l) -> {
115 return l;
116 }),
117 withToString("l - 1", (int l) -> {
118 return l - 1;
119 }),
120 withToString("l + 1", (int l) -> {
121 return l + 1;
122 }),
123 withToString("l - speciesl*ebsize + 1", (int l) -> {
124 return l - SPECIES.vectorByteSize() + 1;
125 }),
126 withToString("l + speciesl*ebsize - 1", (int l) -> {
127 return l + SPECIES.vectorByteSize() - 1;
128 }),
129 withToString("l + speciesl*ebsize", (int l) -> {
160 @DataProvider
161 public Object[][] longMaskProvider() {
162 return BOOLEAN_MASK_GENERATORS.stream().
163 flatMap(fm -> LONG_GENERATORS.stream().map(fa -> {
164 return new Object[] {fa, fm};
165 })).
166 toArray(Object[][]::new);
167 }
168
169 @DataProvider
170 public Object[][] longMaskProviderForIOOBE() {
171 var f = LONG_GENERATORS.get(0);
172 return BOOLEAN_MASK_GENERATORS.stream().
173 flatMap(fm -> INDEX_GENERATORS.stream().map(fi -> {
174 return new Object[] {f, fi, fm};
175 })).
176 toArray(Object[][]::new);
177 }
178
179 @DataProvider
180 public Object[][] longMemorySegmentProvider() {
181 return LONG_GENERATORS.stream().
182 flatMap(fa -> MEMORY_SEGMENT_GENERATORS.stream().
183 flatMap(fb -> BYTE_ORDER_VALUES.stream().map(bo -> {
184 return new Object[]{fa, fb, bo};
185 }))).
186 toArray(Object[][]::new);
187 }
188
189 @DataProvider
190 public Object[][] longMemorySegmentMaskProvider() {
191 return BOOLEAN_MASK_GENERATORS.stream().
192 flatMap(fm -> LONG_GENERATORS.stream().
193 flatMap(fa -> MEMORY_SEGMENT_GENERATORS.stream().
194 flatMap(fb -> BYTE_ORDER_VALUES.stream().map(bo -> {
195 return new Object[]{fa, fb, fm, bo};
196 })))).
197 toArray(Object[][]::new);
198 }
199
200 @DataProvider
201 public Object[][] longByteProviderForIOOBE() {
202 var f = LONG_GENERATORS.get(0);
203 return BYTE_INDEX_GENERATORS.stream().map(fi -> {
204 return new Object[] {f, fi};
205 }).
206 toArray(Object[][]::new);
207 }
208
209 @DataProvider
210 public Object[][] longByteMaskProviderForIOOBE() {
211 var f = LONG_GENERATORS.get(0);
212 return BOOLEAN_MASK_GENERATORS.stream().
213 flatMap(fm -> BYTE_INDEX_GENERATORS.stream().map(fi -> {
214 return new Object[] {f, fi, fm};
215 })).
216 toArray(Object[][]::new);
217 }
218
219 static MemorySegment toSegment(long[] a, IntFunction<MemorySegment> fb) {
220 MemorySegment ms = fb.apply(a.length * SPECIES.elementSize() / 8);
221 for (int i = 0; i < a.length; i++) {
222 ms.set(ValueLayout.JAVA_LONG, i * SPECIES.elementSize() / 8 , a[i]);
223 }
224 return ms;
225 }
226
227 static long[] segmentToArray(MemorySegment ms) {
228 return ms.toArray(ValueLayout.JAVA_LONG);
229 }
230
231
232 interface ToLongF {
233 long apply(int i);
234 }
235
236 static long[] fill(int s , ToLongF f) {
237 return fill(new long[s], f);
238 }
239
240 static long[] fill(long[] a, ToLongF f) {
241 for (int i = 0; i < a.length; i++) {
242 a[i] = f.apply(i);
243 }
244 return a;
245 }
246
247 @DontInline
248 static LongVector fromArray(long[] a, int i) {
249 return LongVector.fromArray(SPECIES, a, i);
250 }
251
252 @DontInline
253 static LongVector fromArray(long[] a, int i, VectorMask<Long> m) {
254 return LongVector.fromArray(SPECIES, a, i, m);
255 }
256
257 @DontInline
258 static void intoArray(LongVector v, long[] a, int i) {
259 v.intoArray(a, i);
260 }
261
262 @DontInline
263 static void intoArray(LongVector v, long[] a, int i, VectorMask<Long> m) {
264 v.intoArray(a, i, m);
265 }
266
267 @DontInline
268 static LongVector fromMemorySegment(MemorySegment a, int i, ByteOrder bo) {
269 return LongVector.fromMemorySegment(SPECIES, a, i, bo);
270 }
271
272 @DontInline
273 static LongVector fromMemorySegment(MemorySegment a, int i, ByteOrder bo, VectorMask<Long> m) {
274 return LongVector.fromMemorySegment(SPECIES, a, i, bo, m);
275 }
276
277 @DontInline
278 static void intoMemorySegment(LongVector v, MemorySegment a, int i, ByteOrder bo) {
279 v.intoMemorySegment(a, i, bo);
280 }
281
282 @DontInline
283 static void intoMemorySegment(LongVector v, MemorySegment a, int i, ByteOrder bo, VectorMask<Long> m) {
284 v.intoMemorySegment(a, i, bo, m);
285 }
286
287 @Test(dataProvider = "longProvider")
288 static void loadStoreArray(IntFunction<long[]> fa) {
289 long[] a = fa.apply(SPECIES.length());
290 long[] r = new long[a.length];
291
292 for (int ic = 0; ic < INVOC_COUNT; ic++) {
293 for (int i = 0; i < a.length; i += SPECIES.length()) {
294 LongVector av = LongVector.fromArray(SPECIES, a, i);
295 av.intoArray(r, i);
296 }
297 }
298 Assert.assertEquals(r, a);
299 }
300
301 @Test(dataProvider = "longProviderForIOOBE")
302 static void loadArrayIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi) {
303 long[] a = fa.apply(SPECIES.length());
304 long[] r = new long[a.length];
305
306 for (int ic = 0; ic < INVOC_COUNT; ic++) {
437 }
438 }
439
440
441 @Test(dataProvider = "longMaskProvider")
442 static void loadStoreMask(IntFunction<long[]> fa,
443 IntFunction<boolean[]> fm) {
444 boolean[] mask = fm.apply(SPECIES.length());
445 boolean[] r = new boolean[mask.length];
446
447 for (int ic = 0; ic < INVOC_COUNT; ic++) {
448 for (int i = 0; i < mask.length; i += SPECIES.length()) {
449 VectorMask<Long> vmask = VectorMask.fromArray(SPECIES, mask, i);
450 vmask.intoArray(r, i);
451 }
452 }
453 Assert.assertEquals(r, mask);
454 }
455
456
457 @Test(dataProvider = "longMemorySegmentProvider")
458 static void loadStoreMemorySegment(IntFunction<long[]> fa,
459 IntFunction<MemorySegment> fb,
460 ByteOrder bo) {
461 MemorySegment a = toSegment(fa.apply(SPECIES.length()), fb);
462 MemorySegment r = fb.apply((int) a.byteSize());
463
464 int l = (int) a.byteSize();
465 int s = SPECIES.vectorByteSize();
466
467 for (int ic = 0; ic < INVOC_COUNT; ic++) {
468 for (int i = 0; i < l; i += s) {
469 LongVector av = LongVector.fromMemorySegment(SPECIES, a, i, bo);
470 av.intoMemorySegment(r, i, bo);
471 }
472 }
473 long m = r.mismatch(a);
474 Assert.assertEquals(m, -1, "Segments not equal");
475 }
476
477 @Test(dataProvider = "longByteProviderForIOOBE")
478 static void loadMemorySegmentIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi) {
479 MemorySegment a = toSegment(fa.apply(SPECIES.length()), i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope()));
480 MemorySegment r = MemorySegment.allocateNative(a.byteSize(), ResourceScope.newImplicitScope());
481
482 int l = (int) a.byteSize();
483 int s = SPECIES.vectorByteSize();
484
485 for (int ic = 0; ic < INVOC_COUNT; ic++) {
486 for (int i = 0; i < l; i += s) {
487 LongVector av = fromMemorySegment(a, i, ByteOrder.nativeOrder());
488 av.intoMemorySegment(r, i, ByteOrder.nativeOrder());
489 }
490 }
491
492 int index = fi.apply((int) a.byteSize());
493 boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, (int) a.byteSize());
494 try {
495 fromMemorySegment(a, index, ByteOrder.nativeOrder());
496 if (shouldFail) {
497 Assert.fail("Failed to throw IndexOutOfBoundsException");
498 }
499 } catch (IndexOutOfBoundsException e) {
500 if (!shouldFail) {
501 Assert.fail("Unexpected IndexOutOfBoundsException");
502 }
503 }
504 }
505
506 @Test(dataProvider = "longByteProviderForIOOBE")
507 static void storeMemorySegmentIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi) {
508 MemorySegment a = toSegment(fa.apply(SPECIES.length()), i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope()));
509 MemorySegment r = MemorySegment.allocateNative(a.byteSize(), ResourceScope.newImplicitScope());
510
511 int l = (int) a.byteSize();
512 int s = SPECIES.vectorByteSize();
513
514 for (int ic = 0; ic < INVOC_COUNT; ic++) {
515 for (int i = 0; i < l; i += s) {
516 LongVector av = LongVector.fromMemorySegment(SPECIES, a, i, ByteOrder.nativeOrder());
517 intoMemorySegment(av, r, i, ByteOrder.nativeOrder());
518 }
519 }
520
521 int index = fi.apply((int) a.byteSize());
522 boolean shouldFail = isIndexOutOfBounds(SPECIES.vectorByteSize(), index, (int) a.byteSize());
523 try {
524 LongVector av = LongVector.fromMemorySegment(SPECIES, a, 0, ByteOrder.nativeOrder());
525 intoMemorySegment(av, r, index, ByteOrder.nativeOrder());
526 if (shouldFail) {
527 Assert.fail("Failed to throw IndexOutOfBoundsException");
528 }
529 } catch (IndexOutOfBoundsException e) {
530 if (!shouldFail) {
531 Assert.fail("Unexpected IndexOutOfBoundsException");
532 }
533 }
534 }
535
536 @Test(dataProvider = "longMemorySegmentMaskProvider")
537 static void loadStoreMemorySegmentMask(IntFunction<long[]> fa,
538 IntFunction<MemorySegment> fb,
539 IntFunction<boolean[]> fm,
540 ByteOrder bo) {
541 long[] _a = fa.apply(SPECIES.length());
542 MemorySegment a = toSegment(_a, fb);
543 MemorySegment r = fb.apply((int) a.byteSize());
544 boolean[] mask = fm.apply(SPECIES.length());
545 VectorMask<Long> vmask = VectorMask.fromValues(SPECIES, mask);
546
547 int l = (int) a.byteSize();
548 int s = SPECIES.vectorByteSize();
549
550 for (int ic = 0; ic < INVOC_COUNT; ic++) {
551 for (int i = 0; i < l; i += s) {
552 LongVector av = LongVector.fromMemorySegment(SPECIES, a, i, bo, vmask);
553 av.intoMemorySegment(r, i, bo);
554 }
555 }
556 assertArraysEquals(segmentToArray(r), _a, mask);
557
558
559 r = fb.apply((int) a.byteSize());
560
561 for (int ic = 0; ic < INVOC_COUNT; ic++) {
562 for (int i = 0; i < l; i += s) {
563 LongVector av = LongVector.fromMemorySegment(SPECIES, a, i, bo);
564 av.intoMemorySegment(r, i, bo, vmask);
565 }
566 }
567 assertArraysEquals(segmentToArray(r), _a, mask);
568 }
569
570 @Test(dataProvider = "longByteMaskProviderForIOOBE")
571 static void loadMemorySegmentMaskIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
572 MemorySegment a = toSegment(fa.apply(SPECIES.length()), i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope()));
573 MemorySegment r = MemorySegment.allocateNative(a.byteSize(), ResourceScope.newImplicitScope());
574 boolean[] mask = fm.apply(SPECIES.length());
575 VectorMask<Long> vmask = VectorMask.fromValues(SPECIES, mask);
576
577 int l = (int) a.byteSize();
578 int s = SPECIES.vectorByteSize();
579
580 for (int ic = 0; ic < INVOC_COUNT; ic++) {
581 for (int i = 0; i < l; i += s) {
582 LongVector av = fromMemorySegment(a, i, ByteOrder.nativeOrder(), vmask);
583 av.intoMemorySegment(r, i, ByteOrder.nativeOrder());
584 }
585 }
586
587 int index = fi.apply((int) a.byteSize());
588 boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, (int) a.byteSize(), SPECIES.elementSize() / 8);
589 try {
590 fromMemorySegment(a, index, ByteOrder.nativeOrder(), vmask);
591 if (shouldFail) {
592 Assert.fail("Failed to throw IndexOutOfBoundsException");
593 }
594 } catch (IndexOutOfBoundsException e) {
595 if (!shouldFail) {
596 Assert.fail("Unexpected IndexOutOfBoundsException");
597 }
598 }
599 }
600
601 @Test(dataProvider = "longByteMaskProviderForIOOBE")
602 static void storeMemorySegmentMaskIOOBE(IntFunction<long[]> fa, IntFunction<Integer> fi, IntFunction<boolean[]> fm) {
603 MemorySegment a = toSegment(fa.apply(SPECIES.length()), i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope()));
604 MemorySegment r = MemorySegment.allocateNative(a.byteSize(), ResourceScope.newImplicitScope());
605 boolean[] mask = fm.apply(SPECIES.length());
606 VectorMask<Long> vmask = VectorMask.fromValues(SPECIES, mask);
607
608 int l = (int) a.byteSize();
609 int s = SPECIES.vectorByteSize();
610
611 for (int ic = 0; ic < INVOC_COUNT; ic++) {
612 for (int i = 0; i < l; i += s) {
613 LongVector av = LongVector.fromMemorySegment(SPECIES, a, i, ByteOrder.nativeOrder());
614 intoMemorySegment(av, r, i, ByteOrder.nativeOrder(), vmask);
615 }
616 }
617
618 int index = fi.apply((int) a.byteSize());
619 boolean shouldFail = isIndexOutOfBoundsForMask(mask, index, (int) a.byteSize(), SPECIES.elementSize() / 8);
620 try {
621 LongVector av = LongVector.fromMemorySegment(SPECIES, a, 0, ByteOrder.nativeOrder());
622 intoMemorySegment(av, a, index, ByteOrder.nativeOrder(), vmask);
623 if (shouldFail) {
624 Assert.fail("Failed to throw IndexOutOfBoundsException");
625 }
626 } catch (IndexOutOfBoundsException e) {
627 if (!shouldFail) {
628 Assert.fail("Unexpected IndexOutOfBoundsException");
629 }
630 }
631 }
632
633 @Test(dataProvider = "longMemorySegmentProvider")
634 static void loadStoreReadonlyMemorySegment(IntFunction<long[]> fa,
635 IntFunction<MemorySegment> fb,
636 ByteOrder bo) {
637 MemorySegment a = toSegment(fa.apply(SPECIES.length()), fb).asReadOnly();
638
639 Assert.assertThrows(
640 UnsupportedOperationException.class,
641 () -> SPECIES.zero().intoMemorySegment(a, 0, bo)
642 );
643
644 Assert.assertThrows(
645 UnsupportedOperationException.class,
646 () -> SPECIES.zero().intoMemorySegment(a, 0, bo, SPECIES.maskAll(true))
647 );
648
649 Assert.assertThrows(
650 UnsupportedOperationException.class,
651 () -> SPECIES.zero().intoMemorySegment(a, 0, bo, SPECIES.maskAll(false))
652 );
653
654 VectorMask<Long> m = SPECIES.shuffleFromOp(i -> i % 2 == 0 ? 1 : -1)
655 .laneIsValid();
656 Assert.assertThrows(
657 UnsupportedOperationException.class,
658 () -> SPECIES.zero().intoMemorySegment(a, 0, bo, m)
659 );
660 }
661
662
663 @Test(dataProvider = "maskProvider")
664 static void loadStoreMask(IntFunction<boolean[]> fm) {
665 boolean[] a = fm.apply(SPECIES.length());
666 boolean[] r = new boolean[a.length];
667
668 for (int ic = 0; ic < INVOC_COUNT; ic++) {
669 for (int i = 0; i < a.length; i += SPECIES.length()) {
670 VectorMask<Long> vmask = SPECIES.loadMask(a, i);
671 vmask.intoArray(r, i);
672 }
673 }
674 Assert.assertEquals(r, a);
675 }
676
677
678 @Test
679 static void loadStoreShuffle() {
680 IntUnaryOperator fn = a -> a + 5;
681 for (int ic = 0; ic < INVOC_COUNT; ic++) {
682 var shuffle = VectorShuffle.fromOp(SPECIES, fn);
683 int [] r = shuffle.toArray();
684
685 int [] a = expectedShuffle(SPECIES.length(), fn);
686 Assert.assertEquals(r, a);
687 }
688 }
689
690
691
692
693
694 // Gather/Scatter load/store tests
695
696 static void assertGatherArraysEquals(long[] r, long[] a, int[] indexMap) {
697 int i = 0;
|