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