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