/* * 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 Int256VectorLoadStoreTests * */ import jdk.incubator.vector.VectorShape; import jdk.incubator.vector.VectorSpecies; import jdk.incubator.vector.VectorMask; import jdk.incubator.vector.Vector; import jdk.incubator.vector.IntVector; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.nio.ByteBuffer; import java.nio.IntBuffer; import java.nio.ByteOrder; import java.util.List; import java.util.function.IntFunction; @Test public class Int256VectorLoadStoreTests extends AbstractVectorTest { static final VectorSpecies SPECIES = IntVector.SPECIES_256; static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 10); static void assertArraysEquals(int[] a, int[] r, boolean[] mask) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(mask[i % SPECIES.length()] ? a[i] : (int) 0, r[i]); } } catch (AssertionError e) { Assert.assertEquals(mask[i % SPECIES.length()] ? a[i] : (int) 0, r[i], "at index #" + i); } } static void assertArraysEquals(int[] a, int[] 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(int[] a, int[] r, int[] im, boolean[] mask) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(mask[i % SPECIES.length()] ? a[im[i]] : (int) 0, r[i]); } } catch (AssertionError e) { Assert.assertEquals(mask[i % SPECIES.length()] ? a[im[i]] : (int) 0, r[i], "at index #" + i); } } static final List> INT_GENERATORS = List.of( withToString("int[i * 5]", (int s) -> { return fill(s * 1000, i -> (int)(i * 5)); }), withToString("int[i + 1]", (int s) -> { return fill(s * 1000, i -> (((int)(i + 1) == 0) ? 1 : (int)(i + 1))); }) ); @DataProvider public Object[][] intProvider() { return INT_GENERATORS.stream(). map(f -> new Object[]{f}). toArray(Object[][]::new); } @DataProvider public Object[][] intMaskProvider() { return BOOLEAN_MASK_GENERATORS.stream(). flatMap(fm -> INT_GENERATORS.stream().map(fa -> { return new Object[] {fa, fm}; })). toArray(Object[][]::new); } @DataProvider public Object[][] intIndexMapProvider() { return INDEX_GENERATORS.stream(). flatMap(fim -> INT_GENERATORS.stream().map(fa -> { return new Object[] {fa, fim}; })). toArray(Object[][]::new); } @DataProvider public Object[][] intIndexMapMaskProvider() { return BOOLEAN_MASK_GENERATORS.stream(). flatMap(fm -> INDEX_GENERATORS.stream(). flatMap(fim -> INT_GENERATORS.stream().map(fa -> { return new Object[] {fa, fim, fm}; }))). toArray(Object[][]::new); } @DataProvider public Object[][] intByteBufferProvider() { return INT_GENERATORS.stream(). flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().map(fb -> { return new Object[]{fa, fb}; })). toArray(Object[][]::new); } @DataProvider public Object[][] intByteBufferMaskProvider() { return BOOLEAN_MASK_GENERATORS.stream(). flatMap(fm -> INT_GENERATORS.stream(). flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().map(fb -> { return new Object[]{fa, fb, fm}; }))). toArray(Object[][]::new); } static ByteBuffer toBuffer(int[] a, IntFunction fb) { ByteBuffer bb = fb.apply(a.length * SPECIES.elementSize() / 8); for (int v : a) { bb.putInt(v); } return bb.clear(); } static int[] bufferToArray(ByteBuffer bb) { IntBuffer db = bb.asIntBuffer(); int[] d = new int[db.capacity()]; db.get(d); return d; } interface ToIntF { int apply(int i); } static int[] fill(int s , ToIntF f) { return fill(new int[s], f); } static int[] fill(int[] a, ToIntF f) { for (int i = 0; i < a.length; i++) { a[i] = f.apply(i); } return a; } @Test(dataProvider = "intProvider") static void loadStoreArray(IntFunction fa) { int[] a = fa.apply(SPECIES.length()); int[] r = new int[a.length]; for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) { IntVector av = IntVector.fromArray(SPECIES, a, i); av.intoArray(r, i); } } Assert.assertEquals(a, r); } @Test(dataProvider = "intMaskProvider") static void loadStoreMaskArray(IntFunction fa, IntFunction fm) { int[] a = fa.apply(SPECIES.length()); int[] r = new int[a.length]; boolean[] mask = fm.apply(SPECIES.length()); VectorMask vmask = VectorMask.fromValues(SPECIES, mask); for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) { IntVector av = IntVector.fromArray(SPECIES, a, i, vmask); av.intoArray(r, i); } } assertArraysEquals(a, r, mask); r = new int[a.length]; for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) { IntVector av = IntVector.fromArray(SPECIES, a, i); av.intoArray(r, i, vmask); } } assertArraysEquals(a, r, mask); } @Test(dataProvider = "intByteBufferProvider") 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) { IntVector av = IntVector.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 = "intByteBufferProvider") 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) { IntVector av = IntVector.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 = "intByteBufferMaskProvider") 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()); VectorMask vmask = VectorMask.fromValues(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) { IntVector av = IntVector.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) { IntVector av = IntVector.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 = "intByteBufferMaskProvider") 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()); VectorMask vmask = VectorMask.fromValues(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) { IntVector av = IntVector.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); } }