/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have * questions. */ /* * @test * @modules jdk.incubator.vector * @run testng Byte512VectorLoadStoreTests * */ import jdk.incubator.vector.Vector.Shape; import jdk.incubator.vector.Vector.Species; import jdk.incubator.vector.Vector; import jdk.incubator.vector.ByteVector; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.List; import java.util.function.IntFunction; @Test public class Byte512VectorLoadStoreTests extends AbstractVectorTest { static final Species SPECIES = ByteVector.SPECIES_512; static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 10); static void assertArraysEquals(byte[] a, byte[] r, boolean[] mask) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(mask[i % SPECIES.length()] ? a[i] : (byte) 0, r[i]); } } catch (AssertionError e) { Assert.assertEquals(mask[i % SPECIES.length()] ? a[i] : (byte) 0, r[i], "at index #" + i); } } static void assertArraysEquals(byte[] a, byte[] r, int[] im) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(a[im[i]], r[i]); } } catch (AssertionError e) { Assert.assertEquals(a[im[i]], r[i], "at index #" + i); } } static void assertArraysEquals(byte[] a, byte[] r, int[] im, boolean[] mask) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(mask[i % SPECIES.length()] ? a[im[i]] : (byte) 0, r[i]); } } catch (AssertionError e) { Assert.assertEquals(mask[i % SPECIES.length()] ? a[im[i]] : (byte) 0, r[i], "at index #" + i); } } static final List> BYTE_GENERATORS = List.of( withToString("byte[i * 5]", (int s) -> { return fill(s * 1000, i -> (byte)(i * 5)); }), withToString("byte[i + 1]", (int s) -> { return fill(s * 1000, i -> (((byte)(i + 1) == 0) ? 1 : (byte)(i + 1))); }) ); @DataProvider public Object[][] byteProvider() { return BYTE_GENERATORS.stream(). map(f -> new Object[]{f}). toArray(Object[][]::new); } @DataProvider public Object[][] byteMaskProvider() { return BOOLEAN_MASK_GENERATORS.stream(). flatMap(fm -> BYTE_GENERATORS.stream().map(fa -> { return new Object[] {fa, fm}; })). toArray(Object[][]::new); } @DataProvider public Object[][] byteIndexMapProvider() { return INDEX_GENERATORS.stream(). flatMap(fim -> BYTE_GENERATORS.stream().map(fa -> { return new Object[] {fa, fim}; })). toArray(Object[][]::new); } @DataProvider public Object[][] byteIndexMapMaskProvider() { return BOOLEAN_MASK_GENERATORS.stream(). flatMap(fm -> INDEX_GENERATORS.stream(). flatMap(fim -> BYTE_GENERATORS.stream().map(fa -> { return new Object[] {fa, fim, fm}; }))). toArray(Object[][]::new); } @DataProvider public Object[][] byteByteBufferProvider() { return BYTE_GENERATORS.stream(). flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().map(fb -> { return new Object[]{fa, fb}; })). toArray(Object[][]::new); } @DataProvider public Object[][] byteByteBufferMaskProvider() { return BOOLEAN_MASK_GENERATORS.stream(). flatMap(fm -> BYTE_GENERATORS.stream(). flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().map(fb -> { return new Object[]{fa, fb, fm}; }))). toArray(Object[][]::new); } static ByteBuffer toBuffer(byte[] a, IntFunction fb) { ByteBuffer bb = fb.apply(a.length * SPECIES.elementSize() / 8); for (byte v : a) { bb.put(v); } return bb.clear(); } static byte[] bufferToArray(ByteBuffer bb) { ByteBuffer db = bb; byte[] d = new byte[db.capacity()]; db.get(d); return d; } interface ToByteF { byte apply(int i); } static byte[] fill(int s , ToByteF f) { return fill(new byte[s], f); } static byte[] fill(byte[] a, ToByteF f) { for (int i = 0; i < a.length; i++) { a[i] = f.apply(i); } return a; } @Test(dataProvider = "byteProvider") static void loadStoreArray(IntFunction fa) { byte[] a = fa.apply(SPECIES.length()); byte[] r = new byte[a.length]; for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) { ByteVector av = ByteVector.fromArray(SPECIES, a, i); av.intoArray(r, i); } } Assert.assertEquals(a, r); } @Test(dataProvider = "byteMaskProvider") static void loadStoreMaskArray(IntFunction fa, IntFunction fm) { byte[] a = fa.apply(SPECIES.length()); byte[] r = new byte[a.length]; boolean[] mask = fm.apply(SPECIES.length()); Vector.Mask vmask = ByteVector.maskFromValues(SPECIES, mask); for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) { ByteVector av = ByteVector.fromArray(SPECIES, a, i, vmask); av.intoArray(r, i); } } assertArraysEquals(a, r, mask); r = new byte[a.length]; for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) { ByteVector av = ByteVector.fromArray(SPECIES, a, i); av.intoArray(r, i, vmask); } } assertArraysEquals(a, r, mask); } @Test(dataProvider = "byteByteBufferProvider") static void loadStoreByteBuffer(IntFunction fa, IntFunction fb) { ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb); ByteBuffer r = fb.apply(a.limit()); int l = a.limit(); int s = SPECIES.length() * SPECIES.elementSize() / 8; for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < l; i += s) { ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i); av.intoByteBuffer(r, i); } } Assert.assertEquals(a.position(), 0, "Input buffer position changed"); Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); Assert.assertEquals(r.position(), 0, "Result buffer position changed"); Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); Assert.assertEquals(a, r, "Buffers not equal"); } @Test(dataProvider = "byteByteBufferProvider") static void loadReadOnlyStoreByteBuffer(IntFunction fa, IntFunction fb) { ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb); a = a.asReadOnlyBuffer().order(a.order()); ByteBuffer r = fb.apply(a.limit()); int l = a.limit(); int s = SPECIES.length() * SPECIES.elementSize() / 8; for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < l; i += s) { ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i); av.intoByteBuffer(r, i); } } Assert.assertEquals(a.position(), 0, "Input buffer position changed"); Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); Assert.assertEquals(r.position(), 0, "Result buffer position changed"); Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); Assert.assertEquals(a, r, "Buffers not equal"); } @Test(dataProvider = "byteByteBufferMaskProvider") static void loadStoreByteBufferMask(IntFunction fa, IntFunction fb, IntFunction fm) { ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb); ByteBuffer r = fb.apply(a.limit()); boolean[] mask = fm.apply(SPECIES.length()); Vector.Mask vmask = ByteVector.maskFromValues(SPECIES, mask); int l = a.limit(); int s = SPECIES.length() * SPECIES.elementSize() / 8; for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < l; i += s) { ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i, vmask); av.intoByteBuffer(r, i); } } Assert.assertEquals(a.position(), 0, "Input buffer position changed"); Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); Assert.assertEquals(r.position(), 0, "Result buffer position changed"); Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); assertArraysEquals(bufferToArray(a), bufferToArray(r), mask); a = toBuffer(fa.apply(SPECIES.length()), fb); r = fb.apply(a.limit()); for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < l; i += s) { ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i); av.intoByteBuffer(r, i, vmask); } } Assert.assertEquals(a.position(), 0, "Input buffer position changed"); Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); Assert.assertEquals(r.position(), 0, "Result buffer position changed"); Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); assertArraysEquals(bufferToArray(a), bufferToArray(r), mask); } @Test(dataProvider = "byteByteBufferMaskProvider") static void loadReadOnlyStoreByteBufferMask(IntFunction fa, IntFunction fb, IntFunction fm) { ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb); a = a.asReadOnlyBuffer().order(a.order()); ByteBuffer r = fb.apply(a.limit()); boolean[] mask = fm.apply(SPECIES.length()); Vector.Mask vmask = ByteVector.maskFromValues(SPECIES, mask); int l = a.limit(); int s = SPECIES.length() * SPECIES.elementSize() / 8; for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < l; i += s) { ByteVector av = ByteVector.fromByteBuffer(SPECIES, a, i, vmask); av.intoByteBuffer(r, i); } } Assert.assertEquals(a.position(), 0, "Input buffer position changed"); Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); Assert.assertEquals(r.position(), 0, "Result buffer position changed"); Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); assertArraysEquals(bufferToArray(a), bufferToArray(r), mask); } }