/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, Arm Limited. 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 #if[MaxBit] * @run testng/othervm --add-opens jdk.incubator.vector/jdk.incubator.vector=ALL-UNNAMED * $vectorteststype$ #else[MaxBit] * @run testng $vectorteststype$ #end[MaxBit] * */ import jdk.incubator.vector.Vector.Shape; import jdk.incubator.vector.Vector; import jdk.incubator.vector.$abstractvectortype$; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.util.List; import java.util.function.IntFunction; @Test public class $vectorteststype$ extends AbstractVectorTest { #if[MaxBit] static final Shape S_Max_BIT = getMaxBit(); static final $abstractvectortype$.$Type$Species SPECIES = $Type$Vector.species(S_Max_BIT); #else[MaxBit] static final $abstractvectortype$.$Type$Species SPECIES = $Type$Vector.species(Shape.S_$bits$_BIT); #end[MaxBit] static final int LOAD_STORE_ARRAY_LENGTH = SPECIES.length() / 2; static final int GATHER_SCATTER_ARRAY_LENGTH = SPECIES.length(); #if[MaxBit] static Shape getMaxBit() { return Shape.S_Max_BIT; } #end[MaxBit] static final Vector.Mask<$Boxtype$> LOAD_STORE_MASK = $abstractvectortype$.maskFromValues( SPECIES, fill_boolean(SPECIES.length(), i -> (i < LOAD_STORE_ARRAY_LENGTH))); static final Vector.Mask<$Boxtype$> GATHER_SCATTER_MASK = $abstractvectortype$.maskFromValues( SPECIES, fill_boolean(SPECIES.length(), i -> ((i * 2 + 1) < GATHER_SCATTER_ARRAY_LENGTH))); static final Vector.Mask<$Boxtype$> ALL_TRUE_MASK = $abstractvectortype$.maskFromValues( SPECIES, fill_boolean(SPECIES.length(), i -> true)); interface To$Type$F { $type$ apply(int i); } static $type$[] fill(int s , To$Type$F f) { return fill(new $type$[s], f); } static $type$[] fill($type$[] a, To$Type$F f) { for (int i = 0; i < a.length; i++) { a[i] = f.apply(i); } return a; } static final List> $TYPE$_GENERATORS = List.of( withToString("$type$[i + 1]", (int s) -> { return fill(s, i -> ((($type$)(i + 1) == 0) ? 1 : ($type$)(i + 1))); }) ); static final List> INDEX_GENERATORS = List.of( withToString("index[i * 2 + 1]", (int s) -> { return fillInts(s, i -> (i * 2 + 1)); }) ); @DataProvider public Object[][] $type$Provider() { return $TYPE$_GENERATORS.stream(). map(f -> new Object[]{f}). toArray(Object[][]::new); } @DataProvider public Object[][] $type$IndexMapProvider() { return INDEX_GENERATORS.stream(). flatMap(fim -> $TYPE$_GENERATORS.stream().map(fa -> { return new Object[] {fa, fim}; })). toArray(Object[][]::new); } @Test(dataProvider = "$type$Provider") static void loadStoreArrayWithMask(IntFunction<$type$[]> fa) { $type$[] a = fa.apply(SPECIES.length() / 2); $type$[] r = new $type$[a.length]; // case 1: No IndexOutOfBoundsException for unset mask lanes. $abstractvectortype$ av = $abstractvectortype$.fromArray(SPECIES, a, 0, LOAD_STORE_MASK); av.intoArray(r, 0, LOAD_STORE_MASK); Assert.assertEquals(a, r); // case 2: $type$[] new_a = new $type$[a.length]; av = $abstractvectortype$.fromArray(SPECIES, new_a, 0, LOAD_STORE_MASK); try { // Result in IndexOutOfBoundsException for intoArray(). av.intoArray(r, 0, ALL_TRUE_MASK); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 3: try { // Result in IndexOutOfBoundsException for fromArray(). av = $abstractvectortype$.fromArray(SPECIES, a, 0, ALL_TRUE_MASK); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } @Test(dataProvider = "$type$IndexMapProvider") static void gatherScatterArrayWithMask(IntFunction<$type$[]> fa, IntFunction fb) { $type$[] a = fa.apply(SPECIES.length()); int[] indexMap = fb.apply(SPECIES.length()); $type$[] r = new $type$[a.length]; // case 1: No IndexOutOfBoundsException for unset mask lanes. $abstractvectortype$ av = $abstractvectortype$.fromArray(SPECIES, a, 0); av.intoArray(r, 0, GATHER_SCATTER_MASK, indexMap, 0); av = $abstractvectortype$.fromArray(SPECIES, a, 0, GATHER_SCATTER_MASK, indexMap, 0); // case 2: $type$[] r1 = new $type$[a.length]; $type$[] r2 = new $type$[a.length]; av = $abstractvectortype$.fromArray(SPECIES, a, 0); try { // Result in IndexOutOfBoundsException for scatter. av.intoArray(r2, 0, ALL_TRUE_MASK, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 3: try { // Result in IndexOutOfBoundsException for gather. av = $abstractvectortype$.fromArray(SPECIES, a, 0, ALL_TRUE_MASK, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } @Test(dataProvider = "$type$Provider") static void loadStoreArray(IntFunction<$type$[]> fa) { $type$[] a = fa.apply(SPECIES.length() / 2); $type$[] r = new $type$[a.length]; // case 1: try { // Result in IndexOutOfBoundsException for fromArray(). $abstractvectortype$ av = $abstractvectortype$.fromArray(SPECIES, a, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 2: $abstractvectortype$ av = $abstractvectortype$.fromArray(SPECIES, a, 0, LOAD_STORE_MASK); try { // Result in IndexOutOfBoundsException for intoArray(). av.intoArray(r, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } @Test(dataProvider = "$type$IndexMapProvider") static void gatherScatterArray(IntFunction<$type$[]> fa, IntFunction fb) { $type$[] a = fa.apply(SPECIES.length()); int[] indexMap = fb.apply(SPECIES.length()); $type$[] r = new $type$[a.length]; // case 1: try { // Result in IndexOutOfBoundsException for gather. $abstractvectortype$ av = $abstractvectortype$.fromArray(SPECIES, a, 0, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 2: $abstractvectortype$ av = $abstractvectortype$.fromArray(SPECIES, a, 0, GATHER_SCATTER_MASK, indexMap, 0); try { // Result in IndexOutOfBoundsException for scatter. av.intoArray(r, 0, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } }