1 /*
  2  * Copyright (c) 2021, 2022, 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 any
 21  * questions.
 22  */
 23 import org.testng.Assert;
 24 import org.testng.annotations.Test;
 25 import org.testng.annotations.DataProvider;
 26 
 27 import java.nio.ByteOrder;
 28 import java.util.Arrays;
 29 import java.util.List;
 30 import java.util.function.IntFunction;
 31 import java.util.function.IntUnaryOperator;
 32 
 33 import java.lang.foreign.MemorySegment;
 34 import jdk.incubator.vector.*;
 35 import jdk.internal.vm.annotation.ForceInline;
 36 
 37 /*
 38  * @test id=ZSinglegen
 39  * @bug 8260473
 40  * @enablePreview
 41  * @requires vm.gc.ZSinglegen
 42  * @modules jdk.incubator.vector
 43  * @modules java.base/jdk.internal.vm.annotation
 44  * @run testng/othervm -XX:CompileCommand=compileonly,jdk/incubator/vector/ByteVector.fromMemorySegment
 45  *      -XX:-TieredCompilation -XX:CICompilerCount=1 -XX:+UseZGC -XX:-ZGenerational -Xbatch -Xmx256m VectorRebracket128Test
 46  */
 47 
 48 /*
 49  * @test id=ZGenerational
 50  * @bug 8260473
 51  * @enablePreview
 52  * @requires vm.gc.ZGenerational
 53  * @modules jdk.incubator.vector
 54  * @modules java.base/jdk.internal.vm.annotation
 55  * @run testng/othervm -XX:CompileCommand=compileonly,jdk/incubator/vector/ByteVector.fromMemorySegment
 56  *      -XX:-TieredCompilation -XX:CICompilerCount=1 -XX:+UseZGC -XX:+ZGenerational -Xbatch -Xmx256m VectorRebracket128Test
 57  */
 58 
 59 @Test
 60 public class VectorRebracket128Test {
 61     static final int INVOC_COUNT = Integer.getInteger("jtreg.compiler.vectorapi.vectorrebracket128test.loop-iterations", 1000);
 62     static final int NUM_ITER = 200 * INVOC_COUNT;
 63 
 64     static final VectorSpecies<Integer> ispec128 = IntVector.SPECIES_128;
 65     static final VectorSpecies<Float> fspec128 = FloatVector.SPECIES_128;
 66     static final VectorSpecies<Long> lspec128 = LongVector.SPECIES_128;
 67     static final VectorSpecies<Double> dspec128 = DoubleVector.SPECIES_128;
 68     static final VectorSpecies<Byte> bspec128 = ByteVector.SPECIES_128;
 69     static final VectorSpecies<Short> sspec128 = ShortVector.SPECIES_128;
 70 
 71     static <T> IntFunction<T> withToString(String s, IntFunction<T> f) {
 72         return new IntFunction<T>() {
 73             @Override
 74             public T apply(int v) {
 75                 return f.apply(v);
 76             }
 77 
 78             @Override
 79             public String toString() {
 80                 return s;
 81             }
 82         };
 83     }
 84 
 85     interface ToByteF {
 86         byte apply(int i);
 87     }
 88 
 89     static byte[] fill_byte(int s , ToByteF f) {
 90         return fill_byte(new byte[s], f);
 91     }
 92 
 93     static byte[] fill_byte(byte[] a, ToByteF f) {
 94         for (int i = 0; i < a.length; i++) {
 95             a[i] = f.apply(i);
 96         }
 97         return a;
 98     }
 99 
100     static final List<IntFunction<byte[]>> BYTE_GENERATORS = List.of(
101             withToString("byte(i)", (int s) -> {
102                 return fill_byte(s, i -> (byte)(i+1));
103             })
104     );
105 
106     @DataProvider
107     public Object[][] byteUnaryOpProvider() {
108         return BYTE_GENERATORS.stream().
109                 map(f -> new Object[]{f}).
110                 toArray(Object[][]::new);
111     }
112 
113     static
114     void checkPartialResult(VectorSpecies<?> a, VectorSpecies<?> b,
115                             byte[] input, byte[] output, byte[] expected,
116                             int part, int origin) {
117         if (Arrays.equals(expected, output)) {
118             return;
119         }
120         int block;
121         block = Math.min(a.vectorByteSize(), b.vectorByteSize());
122 
123         System.out.println("input:  "+Arrays.toString(input));
124         System.out.println("Failing with "+a+"->"+b+
125                            " (reinterpret)"+
126                            ", block=" + block +
127                            ", part=" + part +
128                            ", origin=" + origin);
129         System.out.println("expect: "+Arrays.toString(expected));
130         System.out.println("output: "+Arrays.toString(output));
131         Assert.assertEquals(expected, output);
132     }
133 
134     @ForceInline
135     static <E,F>
136     void testVectorRebracket(VectorSpecies<E> a, VectorSpecies<F> b,
137                              byte[] input, byte[] output,
138                              MemorySegment msInput, MemorySegment msOutput) {
139         Vector<E> av = a.fromMemorySegment(msInput, 0, ByteOrder.nativeOrder());
140         int block;
141         assert(input.length == output.length);
142 
143         block = Math.min(a.vectorByteSize(), b.vectorByteSize());
144         if (false)
145             System.out.println("testing "+a+"->"+b+
146                     (false?" (lanewise)":" (reinterpret)")+
147                     ", block=" + block);
148         byte[] expected;
149         int origin;
150 
151         int part = 0;
152         Vector<F> bv = av.reinterpretShape(b, part);
153         bv.intoMemorySegment(msOutput, 0, ByteOrder.nativeOrder());
154         // in-place copy, no resize
155         expected = input;
156         origin = 0;
157         checkPartialResult(a, b, input, output, expected,
158                 part, origin);
159 
160     }
161 
162     @Test(dataProvider = "byteUnaryOpProvider")
163     static void testRebracket128(IntFunction<byte[]> fa) {
164         byte[] barr = fa.apply(128/Byte.SIZE);
165         byte[] bout = new byte[barr.length];
166         MemorySegment msin = MemorySegment.ofArray(barr);
167         MemorySegment msout = MemorySegment.ofArray(bout);
168         for (int i = 0; i < NUM_ITER; i++) {
169             testVectorRebracket(bspec128, bspec128, barr, bout, msin, msout);
170             testVectorRebracket(bspec128, sspec128, barr, bout, msin, msout);
171             testVectorRebracket(bspec128, ispec128, barr, bout, msin, msout);
172         }
173     }
174 
175 }