1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @modules jdk.incubator.vector 27 #if[MaxBit] 28 * @run testng/othervm --add-opens jdk.incubator.vector/jdk.incubator.vector=ALL-UNNAMED 29 * $vectorteststype$ 30 #else[MaxBit] 31 * @run testng $vectorteststype$ 32 #end[MaxBit] 33 * 34 */ 35 36 import jdk.incubator.vector.Vector.Shape; 37 import jdk.incubator.vector.Vector.Species; 38 import jdk.incubator.vector.Vector; 39 40 import jdk.incubator.vector.$Type$Vector; 41 42 import org.testng.Assert; 43 import org.testng.annotations.DataProvider; 44 import org.testng.annotations.Test; 45 46 #if[MaxBit] 47 import java.lang.invoke.MethodHandles; 48 import java.lang.invoke.VarHandle; 49 #end[MaxBit] 50 import java.nio.ByteBuffer; 51 #if[!byte] 52 import java.nio.$Type$Buffer; 53 #end[!byte] 54 import java.nio.ByteOrder; 55 import java.util.List; 56 import java.util.function.IntFunction; 57 58 @Test 59 public class $vectorteststype$ extends AbstractVectorTest { 60 #if[MaxBit] 61 static final Species<$Wideboxtype$> SPECIES = 62 $Type$Vector.SPECIES_MAX; 63 #else[MaxBit] 64 static final Species<$Wideboxtype$> SPECIES = 65 $Type$Vector.SPECIES_$bits$; 66 #end[MaxBit] 67 68 static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 10); 69 70 #if[MaxBit] 71 static Shape getMaxBit() { 72 return Shape.S_Max_BIT; 73 } 74 75 #end[MaxBit] 76 static void assertArraysEquals($type$[] a, $type$[] r, boolean[] mask) { 77 int i = 0; 78 try { 79 for (; i < a.length; i++) { 80 Assert.assertEquals(mask[i % SPECIES.length()] ? a[i] : ($type$) 0, r[i]); 81 } 82 } catch (AssertionError e) { 83 Assert.assertEquals(mask[i % SPECIES.length()] ? a[i] : ($type$) 0, r[i], "at index #" + i); 84 } 85 } 86 87 static void assertArraysEquals($type$[] a, $type$[] r, int[] im) { 88 int i = 0; 89 try { 90 for (; i < a.length; i++) { 91 Assert.assertEquals(a[im[i]], r[i]); 92 } 93 } catch (AssertionError e) { 94 Assert.assertEquals(a[im[i]], r[i], "at index #" + i); 95 } 96 } 97 98 static void assertArraysEquals($type$[] a, $type$[] r, int[] im, boolean[] mask) { 99 int i = 0; 100 try { 101 for (; i < a.length; i++) { 102 Assert.assertEquals(mask[i % SPECIES.length()] ? a[im[i]] : ($type$) 0, r[i]); 103 } 104 } catch (AssertionError e) { 105 Assert.assertEquals(mask[i % SPECIES.length()] ? a[im[i]] : ($type$) 0, r[i], "at index #" + i); 106 } 107 } 108 109 static final List<IntFunction<$type$[]>> $TYPE$_GENERATORS = List.of( 110 withToString("$type$[i * 5]", (int s) -> { 111 return fill(s * 1000, 112 i -> ($type$)(i * 5)); 113 }), 114 withToString("$type$[i + 1]", (int s) -> { 115 return fill(s * 1000, 116 i -> ((($type$)(i + 1) == 0) ? 1 : ($type$)(i + 1))); 117 }) 118 ); 119 120 @DataProvider 121 public Object[][] $type$Provider() { 122 return $TYPE$_GENERATORS.stream(). 123 map(f -> new Object[]{f}). 124 toArray(Object[][]::new); 125 } 126 127 @DataProvider 128 public Object[][] $type$MaskProvider() { 129 return BOOLEAN_MASK_GENERATORS.stream(). 130 flatMap(fm -> $TYPE$_GENERATORS.stream().map(fa -> { 131 return new Object[] {fa, fm}; 132 })). 133 toArray(Object[][]::new); 134 } 135 136 @DataProvider 137 public Object[][] $type$IndexMapProvider() { 138 return INDEX_GENERATORS.stream(). 139 flatMap(fim -> $TYPE$_GENERATORS.stream().map(fa -> { 140 return new Object[] {fa, fim}; 141 })). 142 toArray(Object[][]::new); 143 } 144 145 @DataProvider 146 public Object[][] $type$IndexMapMaskProvider() { 147 return BOOLEAN_MASK_GENERATORS.stream(). 148 flatMap(fm -> INDEX_GENERATORS.stream(). 149 flatMap(fim -> $TYPE$_GENERATORS.stream().map(fa -> { 150 return new Object[] {fa, fim, fm}; 151 }))). 152 toArray(Object[][]::new); 153 } 154 155 @DataProvider 156 public Object[][] $type$ByteBufferProvider() { 157 return $TYPE$_GENERATORS.stream(). 158 flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().map(fb -> { 159 return new Object[]{fa, fb}; 160 })). 161 toArray(Object[][]::new); 162 } 163 164 @DataProvider 165 public Object[][] $type$ByteBufferMaskProvider() { 166 return BOOLEAN_MASK_GENERATORS.stream(). 167 flatMap(fm -> $TYPE$_GENERATORS.stream(). 168 flatMap(fa -> BYTE_BUFFER_GENERATORS.stream().map(fb -> { 169 return new Object[]{fa, fb, fm}; 170 }))). 171 toArray(Object[][]::new); 172 } 173 174 static ByteBuffer toBuffer($type$[] a, IntFunction<ByteBuffer> fb) { 175 ByteBuffer bb = fb.apply(a.length * SPECIES.elementSize() / 8); 176 for ($type$ v : a) { 177 bb.{#if[byte]?put(v):put$Type$(v)}; 178 } 179 return bb.clear(); 180 } 181 182 static $type$[] bufferToArray(ByteBuffer bb) { 183 $Type$Buffer db = bb{#if[byte]?;:.as$Type$Buffer();} 184 $type$[] d = new $type$[db.capacity()]; 185 db.get(d); 186 return d; 187 } 188 189 interface To$Type$F { 190 $type$ apply(int i); 191 } 192 193 static $type$[] fill(int s , To$Type$F f) { 194 return fill(new $type$[s], f); 195 } 196 197 static $type$[] fill($type$[] a, To$Type$F f) { 198 for (int i = 0; i < a.length; i++) { 199 a[i] = f.apply(i); 200 } 201 return a; 202 } 203 204 @Test(dataProvider = "$type$Provider") 205 static void loadStoreArray(IntFunction<$type$[]> fa) { 206 $type$[] a = fa.apply(SPECIES.length()); 207 $type$[] r = new $type$[a.length]; 208 209 for (int ic = 0; ic < INVOC_COUNT; ic++) { 210 for (int i = 0; i < a.length; i += SPECIES.length()) { 211 $abstractvectortype$ av = $abstractvectortype$.fromArray(SPECIES, a, i); 212 av.intoArray(r, i); 213 } 214 } 215 Assert.assertEquals(a, r); 216 } 217 218 @Test(dataProvider = "$type$MaskProvider") 219 static void loadStoreMaskArray(IntFunction<$type$[]> fa, 220 IntFunction<boolean[]> fm) { 221 $type$[] a = fa.apply(SPECIES.length()); 222 $type$[] r = new $type$[a.length]; 223 boolean[] mask = fm.apply(SPECIES.length()); 224 Vector.Mask<$Boxtype$> vmask = $abstractvectortype$.maskFromValues(SPECIES, mask); 225 226 for (int ic = 0; ic < INVOC_COUNT; ic++) { 227 for (int i = 0; i < a.length; i += SPECIES.length()) { 228 $abstractvectortype$ av = $abstractvectortype$.fromArray(SPECIES, a, i, vmask); 229 av.intoArray(r, i); 230 } 231 } 232 assertArraysEquals(a, r, mask); 233 234 r = new $type$[a.length]; 235 for (int ic = 0; ic < INVOC_COUNT; ic++) { 236 for (int i = 0; i < a.length; i += SPECIES.length()) { 237 $abstractvectortype$ av = $abstractvectortype$.fromArray(SPECIES, a, i); 238 av.intoArray(r, i, vmask); 239 } 240 } 241 242 assertArraysEquals(a, r, mask); 243 } 244 245 246 @Test(dataProvider = "$type$ByteBufferProvider") 247 static void loadStoreByteBuffer(IntFunction<$type$[]> fa, 248 IntFunction<ByteBuffer> fb) { 249 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb); 250 ByteBuffer r = fb.apply(a.limit()); 251 252 int l = a.limit(); 253 int s = SPECIES.length() * SPECIES.elementSize() / 8; 254 255 for (int ic = 0; ic < INVOC_COUNT; ic++) { 256 for (int i = 0; i < l; i += s) { 257 $abstractvectortype$ av = $abstractvectortype$.fromByteBuffer(SPECIES, a, i); 258 av.intoByteBuffer(r, i); 259 } 260 } 261 Assert.assertEquals(a.position(), 0, "Input buffer position changed"); 262 Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); 263 Assert.assertEquals(r.position(), 0, "Result buffer position changed"); 264 Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); 265 Assert.assertEquals(a, r, "Buffers not equal"); 266 } 267 268 @Test(dataProvider = "$type$ByteBufferProvider") 269 static void loadReadOnlyStoreByteBuffer(IntFunction<$type$[]> fa, 270 IntFunction<ByteBuffer> fb) { 271 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb); 272 a = a.asReadOnlyBuffer().order(a.order()); 273 ByteBuffer r = fb.apply(a.limit()); 274 275 int l = a.limit(); 276 int s = SPECIES.length() * SPECIES.elementSize() / 8; 277 278 for (int ic = 0; ic < INVOC_COUNT; ic++) { 279 for (int i = 0; i < l; i += s) { 280 $abstractvectortype$ av = $abstractvectortype$.fromByteBuffer(SPECIES, a, i); 281 av.intoByteBuffer(r, i); 282 } 283 } 284 Assert.assertEquals(a.position(), 0, "Input buffer position changed"); 285 Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); 286 Assert.assertEquals(r.position(), 0, "Result buffer position changed"); 287 Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); 288 Assert.assertEquals(a, r, "Buffers not equal"); 289 } 290 291 @Test(dataProvider = "$type$ByteBufferMaskProvider") 292 static void loadStoreByteBufferMask(IntFunction<$type$[]> fa, 293 IntFunction<ByteBuffer> fb, 294 IntFunction<boolean[]> fm) { 295 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb); 296 ByteBuffer r = fb.apply(a.limit()); 297 boolean[] mask = fm.apply(SPECIES.length()); 298 Vector.Mask<$Boxtype$> vmask = $abstractvectortype$.maskFromValues(SPECIES, mask); 299 300 int l = a.limit(); 301 int s = SPECIES.length() * SPECIES.elementSize() / 8; 302 303 for (int ic = 0; ic < INVOC_COUNT; ic++) { 304 for (int i = 0; i < l; i += s) { 305 $abstractvectortype$ av = $abstractvectortype$.fromByteBuffer(SPECIES, a, i, vmask); 306 av.intoByteBuffer(r, i); 307 } 308 } 309 Assert.assertEquals(a.position(), 0, "Input buffer position changed"); 310 Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); 311 Assert.assertEquals(r.position(), 0, "Result buffer position changed"); 312 Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); 313 assertArraysEquals(bufferToArray(a), bufferToArray(r), mask); 314 315 a = toBuffer(fa.apply(SPECIES.length()), fb); 316 r = fb.apply(a.limit()); 317 for (int ic = 0; ic < INVOC_COUNT; ic++) { 318 for (int i = 0; i < l; i += s) { 319 $abstractvectortype$ av = $abstractvectortype$.fromByteBuffer(SPECIES, a, i); 320 av.intoByteBuffer(r, i, vmask); 321 } 322 } 323 Assert.assertEquals(a.position(), 0, "Input buffer position changed"); 324 Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); 325 Assert.assertEquals(r.position(), 0, "Result buffer position changed"); 326 Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); 327 assertArraysEquals(bufferToArray(a), bufferToArray(r), mask); 328 } 329 330 @Test(dataProvider = "$type$ByteBufferMaskProvider") 331 static void loadReadOnlyStoreByteBufferMask(IntFunction<$type$[]> fa, 332 IntFunction<ByteBuffer> fb, 333 IntFunction<boolean[]> fm) { 334 ByteBuffer a = toBuffer(fa.apply(SPECIES.length()), fb); 335 a = a.asReadOnlyBuffer().order(a.order()); 336 ByteBuffer r = fb.apply(a.limit()); 337 boolean[] mask = fm.apply(SPECIES.length()); 338 Vector.Mask<$Boxtype$> vmask = $abstractvectortype$.maskFromValues(SPECIES, mask); 339 340 int l = a.limit(); 341 int s = SPECIES.length() * SPECIES.elementSize() / 8; 342 343 for (int ic = 0; ic < INVOC_COUNT; ic++) { 344 for (int i = 0; i < l; i += s) { 345 $abstractvectortype$ av = $abstractvectortype$.fromByteBuffer(SPECIES, a, i, vmask); 346 av.intoByteBuffer(r, i); 347 } 348 } 349 Assert.assertEquals(a.position(), 0, "Input buffer position changed"); 350 Assert.assertEquals(a.limit(), l, "Input buffer limit changed"); 351 Assert.assertEquals(r.position(), 0, "Result buffer position changed"); 352 Assert.assertEquals(r.limit(), l, "Result buffer limit changed"); 353 assertArraysEquals(bufferToArray(a), bufferToArray(r), mask); 354 } 355 }