1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2019, Arm Limited. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have 22 * questions. 23 */ 24 25 /* 26 * @test 27 * @modules jdk.incubator.vector 28 * @run testng/othervm --add-opens jdk.incubator.vector/jdk.incubator.vector=ALL-UNNAMED 29 * FloatMaxVectorExceptionTests 30 * 31 */ 32 33 import jdk.incubator.vector.Vector.Shape; 34 import jdk.incubator.vector.Vector; 35 import jdk.incubator.vector.FloatVector; 36 37 import org.testng.Assert; 38 import org.testng.annotations.DataProvider; 39 import org.testng.annotations.Test; 40 41 import java.util.List; 42 import java.util.function.IntFunction; 43 44 @Test 45 public class FloatMaxVectorExceptionTests extends AbstractVectorTest { 46 static final Shape S_Max_BIT = getMaxBit(); 47 48 static final FloatVector.FloatSpecies SPECIES = 49 FloatVector.species(S_Max_BIT); 50 51 static final int LOAD_STORE_ARRAY_LENGTH = SPECIES.length() / 2; 52 static final int GATHER_SCATTER_ARRAY_LENGTH = SPECIES.length(); 53 54 static Shape getMaxBit() { 55 return Shape.S_Max_BIT; 56 } 57 58 static final Vector.Mask<Float> LOAD_STORE_MASK = FloatVector.maskFromValues( 59 SPECIES, fill_boolean(SPECIES.length(), i -> (i < LOAD_STORE_ARRAY_LENGTH))); 60 61 static final Vector.Mask<Float> GATHER_SCATTER_MASK = FloatVector.maskFromValues( 62 SPECIES, fill_boolean(SPECIES.length(), i -> ((i * 2 + 1) < GATHER_SCATTER_ARRAY_LENGTH))); 63 64 static final Vector.Mask<Float> ALL_TRUE_MASK = FloatVector.maskFromValues( 65 SPECIES, fill_boolean(SPECIES.length(), i -> true)); 66 67 interface ToFloatF { 68 float apply(int i); 69 } 70 71 static float[] fill(int s , ToFloatF f) { 72 return fill(new float[s], f); 73 } 74 75 static float[] fill(float[] a, ToFloatF f) { 76 for (int i = 0; i < a.length; i++) { 77 a[i] = f.apply(i); 78 } 79 return a; 80 } 81 82 static final List<IntFunction<float[]>> FLOAT_GENERATORS = List.of( 83 withToString("float[i + 1]", (int s) -> { 84 return fill(s, 85 i -> (((float)(i + 1) == 0) ? 1 : (float)(i + 1))); 86 }) 87 ); 88 89 static final List<IntFunction<int[]>> INDEX_GENERATORS = List.of( 90 withToString("index[i * 2 + 1]", (int s) -> { 91 return fillInts(s, i -> (i * 2 + 1)); 92 }) 93 ); 94 95 @DataProvider 96 public Object[][] floatProvider() { 97 return FLOAT_GENERATORS.stream(). 98 map(f -> new Object[]{f}). 99 toArray(Object[][]::new); 100 } 101 102 @DataProvider 103 public Object[][] floatIndexMapProvider() { 104 return INDEX_GENERATORS.stream(). 105 flatMap(fim -> FLOAT_GENERATORS.stream().map(fa -> { 106 return new Object[] {fa, fim}; 107 })). 108 toArray(Object[][]::new); 109 } 110 111 @Test(dataProvider = "floatProvider") 112 static void loadStoreArrayWithMask(IntFunction<float[]> fa) { 113 float[] a = fa.apply(SPECIES.length() / 2); 114 float[] r = new float[a.length]; 115 116 // case 1: No IndexOutOfBoundsException for unset mask lanes. 117 FloatVector av = FloatVector.fromArray(SPECIES, a, 0, LOAD_STORE_MASK); 118 av.intoArray(r, 0, LOAD_STORE_MASK); 119 Assert.assertEquals(a, r); 120 121 // case 2: 122 float[] new_a = new float[a.length]; 123 av = FloatVector.fromArray(SPECIES, new_a, 0, LOAD_STORE_MASK); 124 try { 125 // Result in IndexOutOfBoundsException for intoArray(). 126 av.intoArray(r, 0, ALL_TRUE_MASK); 127 Assert.fail("Expected IndexOutOfBoundsException not thrown"); 128 } catch(IndexOutOfBoundsException e) { 129 } 130 131 // case 3: 132 try { 133 // Result in IndexOutOfBoundsException for fromArray(). 134 av = FloatVector.fromArray(SPECIES, a, 0, ALL_TRUE_MASK); 135 Assert.fail("Expected IndexOutOfBoundsException not thrown"); 136 } catch(IndexOutOfBoundsException e) { 137 } 138 } 139 140 @Test(dataProvider = "floatIndexMapProvider") 141 static void gatherScatterArrayWithMask(IntFunction<float[]> fa, 142 IntFunction<int[]> fb) { 143 float[] a = fa.apply(SPECIES.length()); 144 int[] indexMap = fb.apply(SPECIES.length()); 145 float[] r = new float[a.length]; 146 147 // case 1: No IndexOutOfBoundsException for unset mask lanes. 148 FloatVector av = FloatVector.fromArray(SPECIES, a, 0); 149 av.intoArray(r, 0, GATHER_SCATTER_MASK, indexMap, 0); 150 151 av = FloatVector.fromArray(SPECIES, a, 0, GATHER_SCATTER_MASK, indexMap, 0); 152 153 // case 2: 154 float[] r1 = new float[a.length]; 155 float[] r2 = new float[a.length]; 156 av = FloatVector.fromArray(SPECIES, a, 0); 157 try { 158 // Result in IndexOutOfBoundsException for scatter. 159 av.intoArray(r2, 0, ALL_TRUE_MASK, indexMap, 0); 160 Assert.fail("Expected IndexOutOfBoundsException not thrown"); 161 } catch(IndexOutOfBoundsException e) { 162 } 163 164 // case 3: 165 try { 166 // Result in IndexOutOfBoundsException for gather. 167 av = FloatVector.fromArray(SPECIES, a, 0, ALL_TRUE_MASK, indexMap, 0); 168 Assert.fail("Expected IndexOutOfBoundsException not thrown"); 169 } catch(IndexOutOfBoundsException e) { 170 } 171 } 172 173 @Test(dataProvider = "floatProvider") 174 static void loadStoreArray(IntFunction<float[]> fa) { 175 float[] a = fa.apply(SPECIES.length() / 2); 176 float[] r = new float[a.length]; 177 178 // case 1: 179 try { 180 // Result in IndexOutOfBoundsException for fromArray(). 181 FloatVector av = FloatVector.fromArray(SPECIES, a, 0); 182 Assert.fail("Expected IndexOutOfBoundsException not thrown"); 183 } catch(IndexOutOfBoundsException e) { 184 } 185 186 // case 2: 187 FloatVector av = FloatVector.fromArray(SPECIES, a, 0, LOAD_STORE_MASK); 188 try { 189 // Result in IndexOutOfBoundsException for intoArray(). 190 av.intoArray(r, 0); 191 Assert.fail("Expected IndexOutOfBoundsException not thrown"); 192 } catch(IndexOutOfBoundsException e) { 193 } 194 } 195 196 @Test(dataProvider = "floatIndexMapProvider") 197 static void gatherScatterArray(IntFunction<float[]> fa, 198 IntFunction<int[]> fb) { 199 float[] a = fa.apply(SPECIES.length()); 200 int[] indexMap = fb.apply(SPECIES.length()); 201 float[] r = new float[a.length]; 202 203 // case 1: 204 try { 205 // Result in IndexOutOfBoundsException for gather. 206 FloatVector av = FloatVector.fromArray(SPECIES, a, 0, indexMap, 0); 207 Assert.fail("Expected IndexOutOfBoundsException not thrown"); 208 } catch(IndexOutOfBoundsException e) { 209 } 210 211 // case 2: 212 FloatVector av = FloatVector.fromArray(SPECIES, a, 0, GATHER_SCATTER_MASK, indexMap, 0); 213 try { 214 // Result in IndexOutOfBoundsException for scatter. 215 av.intoArray(r, 0, indexMap, 0); 216 Assert.fail("Expected IndexOutOfBoundsException not thrown"); 217 } catch(IndexOutOfBoundsException e) { 218 } 219 } 220 }