1 /*
2  * Copyright (c) 2021, Arm Limited. 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 
24 package compiler.vectorapi;
25 
26 import java.util.Random;
27 
28 import jdk.incubator.vector.ByteVector;
29 import jdk.incubator.vector.DoubleVector;
30 import jdk.incubator.vector.FloatVector;
31 import jdk.incubator.vector.IntVector;
32 import jdk.incubator.vector.LongVector;
33 import jdk.incubator.vector.ShortVector;
34 import jdk.incubator.vector.VectorSpecies;
35 
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
38 
39 /**
40  * @test
41  * @bug 8268966
42  * @summary AArch64: 'bad AD file' in some vector conversion tests
43  * @modules jdk.incubator.vector
44  * @run testng/othervm -XX:-TieredCompilation compiler.vectorapi.VectorCastShape64Test
45  */
46 
47 
48 public class VectorCastShape64Test {
49 
50     private static final VectorSpecies<Long> lspec = LongVector.SPECIES_64;
51     private static final VectorSpecies<Integer> ispec = IntVector.SPECIES_64;
52     private static final VectorSpecies<Short> sspec = ShortVector.SPECIES_64;
53     private static final VectorSpecies<Byte> bspec = ByteVector.SPECIES_64;
54     private static final VectorSpecies<Float> fspec = FloatVector.SPECIES_64;
55     private static final VectorSpecies<Double> dspec = DoubleVector.SPECIES_64;
56 
57     private static final int NUM_ITER = 50000;
58     private static final int LENGTH = 512;
59     private static int[] ia;
60     private static int[] ib;
61     private static byte[] ba;
62     private static byte[] bb;
63     private static short[] sa;
64     private static short[] sb;
65     private static long[] la;
66     private static long[] lb;
67     private static double[] da;
68     private static double[] db;
69     private static float[] fa;
70     private static float[] fb;
71 
initialize()72     private static void initialize() {
73         ia = new int[LENGTH];
74         ib = new int[LENGTH];
75         la = new long[LENGTH];
76         lb = new long[LENGTH];
77         sa = new short[LENGTH];
78         sb = new short[LENGTH];
79         ba = new byte[LENGTH];
80         bb = new byte[LENGTH];
81         fa = new float[LENGTH];
82         fb = new float[LENGTH];
83         da = new double[LENGTH];
84         db = new double[LENGTH];
85         Random r = new Random();
86         for (int i = 0; i < LENGTH; i++) {
87             ia[i] = r.nextInt();
88             la[i] = r.nextLong();
89             sa[i] = (short) r.nextInt();
90             ba[i] = (byte) r.nextInt();
91             fa[i] = r.nextFloat();
92             da[i] = r.nextDouble();
93         }
94     }
95 
96 
testDoubleToByte()97     private static void testDoubleToByte() {
98         for (int i = 0; i < dspec.loopBound(LENGTH); i += dspec.length()) {
99             DoubleVector va = DoubleVector.fromArray(dspec, da, i);
100             ByteVector vb = (ByteVector) va.castShape(bspec, 0);
101             vb.intoArray(bb, 0);
102 
103             for (int j = 0; j < Math.min(dspec.length(), bspec.length()); j++) {
104                 Assert.assertEquals(bb[j], (byte) da[j + i]);
105             }
106         }
107     }
108 
testDoubleToShort()109     private static void testDoubleToShort() {
110         for (int i = 0; i < dspec.loopBound(LENGTH); i += dspec.length()) {
111             DoubleVector va = DoubleVector.fromArray(dspec, da, i);
112             ShortVector vb = (ShortVector) va.castShape(sspec, 0);
113             vb.intoArray(sb, 0);
114 
115             for (int j = 0; j < Math.min(dspec.length(), sspec.length()); j++) {
116                 Assert.assertEquals(sb[j], (short) da[j + i]);
117             }
118         }
119     }
120 
testDoubleToInt()121     private static void testDoubleToInt() {
122         for (int i = 0; i < dspec.loopBound(LENGTH); i += dspec.length()) {
123             DoubleVector va = DoubleVector.fromArray(dspec, da, i);
124             IntVector vb = (IntVector) va.castShape(ispec, 0);
125             vb.intoArray(ib, 0);
126 
127             for (int j = 0; j < Math.min(dspec.length(), ispec.length()); j++) {
128                 Assert.assertEquals(ib[j], (int) da[j + i]);
129             }
130         }
131     }
132 
testDoubleToLong()133     private static void testDoubleToLong() {
134         for (int i = 0; i < dspec.loopBound(LENGTH); i += dspec.length()) {
135             DoubleVector va = DoubleVector.fromArray(dspec, da, i);
136             LongVector vb = (LongVector) va.castShape(lspec, 0);
137             vb.intoArray(lb, 0);
138 
139             for (int j = 0; j < Math.min(dspec.length(), lspec.length()); j++) {
140                 Assert.assertEquals(lb[j], (long) da[j + i]);
141             }
142         }
143     }
144 
testDoubleToFloat()145     private static void testDoubleToFloat() {
146         for (int i = 0; i < dspec.loopBound(LENGTH); i += dspec.length()) {
147             DoubleVector va = DoubleVector.fromArray(dspec, da, i);
148             FloatVector vb = (FloatVector) va.castShape(fspec, 0);
149             vb.intoArray(fb, 0);
150 
151             for (int j = 0; j < Math.min(dspec.length(), fspec.length()); j++) {
152                 Assert.assertEquals(fb[j], (float) da[j + i]);
153             }
154         }
155     }
156 
157 
testFloatToByte()158     private static void testFloatToByte() {
159         for (int i = 0; i < fspec.loopBound(LENGTH); i += fspec.length()) {
160             FloatVector va = FloatVector.fromArray(fspec, fa, i);
161             ByteVector vb = (ByteVector) va.castShape(bspec, 0);
162             vb.intoArray(bb, 0);
163 
164             for (int j = 0; j < Math.min(fspec.length(), bspec.length()); j++) {
165                 Assert.assertEquals(bb[j], (byte) fa[j + i]);
166             }
167         }
168     }
169 
testFloatToShort()170     private static void testFloatToShort() {
171         for (int i = 0; i < fspec.loopBound(LENGTH); i += fspec.length()) {
172             FloatVector va = FloatVector.fromArray(fspec, fa, i);
173             ShortVector vb = (ShortVector) va.castShape(sspec, 0);
174             vb.intoArray(sb, 0);
175 
176             for (int j = 0; j < Math.min(fspec.length(), sspec.length()); j++) {
177                 Assert.assertEquals(sb[j], (short) fa[j + i]);
178             }
179         }
180     }
181 
testFloatToInt()182     private static void testFloatToInt() {
183         for (int i = 0; i < fspec.loopBound(LENGTH); i += fspec.length()) {
184             FloatVector va = FloatVector.fromArray(fspec, fa, i);
185             IntVector vb = (IntVector) va.castShape(ispec, 0);
186             vb.intoArray(ib, 0);
187 
188             for (int j = 0; j < Math.min(fspec.length(), ispec.length()); j++) {
189                 Assert.assertEquals(ib[j], (int) fa[j + i]);
190             }
191         }
192     }
193 
testFloatToLong()194     private static void testFloatToLong() {
195         for (int i = 0; i < fspec.loopBound(LENGTH); i += fspec.length()) {
196             FloatVector va = FloatVector.fromArray(fspec, fa, i);
197             LongVector vb = (LongVector) va.castShape(lspec, 0);
198             vb.intoArray(lb, 0);
199 
200             for (int j = 0; j < Math.min(fspec.length(), lspec.length()); j++) {
201                 Assert.assertEquals(lb[j], (long) fa[j + i]);
202             }
203         }
204     }
205 
testFloatToDouble()206     private static void testFloatToDouble() {
207         for (int i = 0; i < fspec.loopBound(LENGTH); i += fspec.length()) {
208             FloatVector va = FloatVector.fromArray(fspec, fa, i);
209             DoubleVector vb = (DoubleVector) va.castShape(dspec, 0);
210             vb.intoArray(db, 0);
211 
212             for (int j = 0; j < Math.min(fspec.length(), dspec.length()); j++) {
213                 Assert.assertEquals(db[j], (double) fa[j + i]);
214             }
215         }
216     }
217 
218 
testIntToByte()219     private static void testIntToByte() {
220         for (int i = 0; i < ispec.loopBound(LENGTH); i += ispec.length()) {
221             IntVector va = IntVector.fromArray(ispec, ia, i);
222             ByteVector vb = (ByteVector) va.castShape(bspec, 0);
223             vb.intoArray(bb, 0);
224 
225             for (int j = 0; j < Math.min(ispec.length(), bspec.length()); j++) {
226                 Assert.assertEquals(bb[j], (byte) ia[j + i]);
227             }
228         }
229     }
230 
testIntToShort()231     private static void testIntToShort() {
232         for (int i = 0; i < ispec.loopBound(LENGTH); i += ispec.length()) {
233             IntVector va = IntVector.fromArray(ispec, ia, i);
234             ShortVector vb = (ShortVector) va.castShape(sspec, 0);
235             vb.intoArray(sb, 0);
236 
237             for (int j = 0; j < Math.min(ispec.length(), sspec.length()); j++) {
238                 Assert.assertEquals(sb[j], (short) ia[j + i]);
239             }
240         }
241     }
242 
testIntToLong()243     private static void testIntToLong() {
244         for (int i = 0; i < ispec.loopBound(LENGTH); i += ispec.length()) {
245             IntVector va = IntVector.fromArray(ispec, ia, i);
246             LongVector vb = (LongVector) va.castShape(lspec, 0);
247             vb.intoArray(lb, 0);
248 
249             for (int j = 0; j < Math.min(ispec.length(), lspec.length()); j++) {
250                 Assert.assertEquals(lb[j], (long) ia[j + i]);
251             }
252         }
253     }
254 
testIntToFloat()255     private static void testIntToFloat() {
256         for (int i = 0; i < ispec.loopBound(LENGTH); i += ispec.length()) {
257             IntVector va = IntVector.fromArray(ispec, ia, i);
258             FloatVector vb = (FloatVector) va.castShape(fspec, 0);
259             vb.intoArray(fb, 0);
260 
261             for (int j = 0; j < Math.min(ispec.length(), fspec.length()); j++) {
262                 Assert.assertEquals(fb[j], (float) ia[j + i]);
263             }
264         }
265     }
266 
testIntToDouble()267     private static void testIntToDouble() {
268         for (int i = 0; i < ispec.loopBound(LENGTH); i += ispec.length()) {
269             IntVector va = IntVector.fromArray(ispec, ia, i);
270             DoubleVector vb = (DoubleVector) va.castShape(dspec, 0);
271             vb.intoArray(db, 0);
272 
273             for (int j = 0; j < Math.min(ispec.length(), dspec.length()); j++) {
274                 Assert.assertEquals(db[j], (double) ia[j + i]);
275             }
276         }
277     }
278 
279 
testLongToByte()280     private static void testLongToByte() {
281         for (int i = 0; i < lspec.loopBound(LENGTH); i += lspec.length()) {
282             LongVector va = LongVector.fromArray(lspec, la, i);
283             ByteVector vb = (ByteVector) va.castShape(bspec, 0);
284             vb.intoArray(bb, 0);
285 
286             for (int j = 0; j < Math.min(lspec.length(), bspec.length()); j++) {
287                 Assert.assertEquals(bb[j], (byte) la[j + i]);
288             }
289         }
290     }
291 
testLongToShort()292     private static void testLongToShort() {
293         for (int i = 0; i < lspec.loopBound(LENGTH); i += lspec.length()) {
294             LongVector va = LongVector.fromArray(lspec, la, i);
295             ShortVector vb = (ShortVector) va.castShape(sspec, 0);
296             vb.intoArray(sb, 0);
297 
298             for (int j = 0; j < Math.min(lspec.length(), sspec.length()); j++) {
299                 Assert.assertEquals(sb[j], (short) la[j + i]);
300             }
301         }
302     }
303 
testLongToInt()304     private static void testLongToInt() {
305         for (int i = 0; i < lspec.loopBound(LENGTH); i += lspec.length()) {
306             LongVector va = LongVector.fromArray(lspec, la, i);
307             IntVector vb = (IntVector) va.castShape(ispec, 0);
308             vb.intoArray(ib, 0);
309 
310             for (int j = 0; j < Math.min(lspec.length(), ispec.length()); j++) {
311                 Assert.assertEquals(ib[j], (int) la[j + i]);
312             }
313         }
314     }
315 
testLongToFloat()316     private static void testLongToFloat() {
317         for (int i = 0; i < lspec.loopBound(LENGTH); i += lspec.length()) {
318             LongVector va = LongVector.fromArray(lspec, la, i);
319             FloatVector vb = (FloatVector) va.castShape(fspec, 0);
320             vb.intoArray(fb, 0);
321 
322             for (int j = 0; j < Math.min(lspec.length(), fspec.length()); j++) {
323                 Assert.assertEquals(fb[j], (float) la[j + i]);
324             }
325         }
326     }
327 
testLongToDouble()328     private static void testLongToDouble() {
329         for (int i = 0; i < lspec.loopBound(LENGTH); i += lspec.length()) {
330             LongVector va = LongVector.fromArray(lspec, la, i);
331             DoubleVector vb = (DoubleVector) va.castShape(dspec, 0);
332             vb.intoArray(db, 0);
333 
334             for (int j = 0; j < Math.min(lspec.length(), dspec.length()); j++) {
335                 Assert.assertEquals(db[j], (double) la[j + i]);
336             }
337         }
338     }
339 
340 
testShortToByte()341     private static void testShortToByte() {
342         for (int i = 0; i < sspec.loopBound(LENGTH); i += sspec.length()) {
343             ShortVector va = ShortVector.fromArray(sspec, sa, i);
344             ByteVector vb = (ByteVector) va.castShape(bspec, 0);
345             vb.intoArray(bb, 0);
346 
347             for (int j = 0; j < Math.min(sspec.length(), bspec.length()); j++) {
348                 Assert.assertEquals(bb[j], (byte) sa[j + i]);
349             }
350         }
351     }
352 
testShortToInt()353     private static void testShortToInt() {
354         for (int i = 0; i < sspec.loopBound(LENGTH); i += sspec.length()) {
355             ShortVector va = ShortVector.fromArray(sspec, sa, i);
356             IntVector vb = (IntVector) va.castShape(ispec, 0);
357             vb.intoArray(ib, 0);
358 
359             for (int j = 0; j < Math.min(sspec.length(), ispec.length()); j++) {
360                 Assert.assertEquals(ib[j], (int) sa[j + i]);
361             }
362         }
363     }
364 
testShortToLong()365     private static void testShortToLong() {
366         for (int i = 0; i < sspec.loopBound(LENGTH); i += sspec.length()) {
367             ShortVector va = ShortVector.fromArray(sspec, sa, i);
368             LongVector vb = (LongVector) va.castShape(lspec, 0);
369             vb.intoArray(lb, 0);
370 
371             for (int j = 0; j < Math.min(sspec.length(), lspec.length()); j++) {
372                 Assert.assertEquals(lb[j], (long) sa[j + i]);
373             }
374         }
375     }
376 
testShortToFloat()377     private static void testShortToFloat() {
378         for (int i = 0; i < sspec.loopBound(LENGTH); i += sspec.length()) {
379             ShortVector va = ShortVector.fromArray(sspec, sa, i);
380             FloatVector vb = (FloatVector) va.castShape(fspec, 0);
381             vb.intoArray(fb, 0);
382 
383             for (int j = 0; j < Math.min(sspec.length(), fspec.length()); j++) {
384                 Assert.assertEquals(fb[j], (float) sa[j + i]);
385             }
386         }
387     }
388 
testShortToDouble()389     private static void testShortToDouble() {
390         for (int i = 0; i < sspec.loopBound(LENGTH); i += sspec.length()) {
391             ShortVector va = ShortVector.fromArray(sspec, sa, i);
392             DoubleVector vb = (DoubleVector) va.castShape(dspec, 0);
393             vb.intoArray(db, 0);
394 
395             for (int j = 0; j < Math.min(sspec.length(), dspec.length()); j++) {
396                 Assert.assertEquals(db[j], (double) sa[j + i]);
397             }
398         }
399     }
400 
401 
testByteToShort()402     private static void testByteToShort() {
403         for (int i = 0; i < bspec.loopBound(LENGTH); i += bspec.length()) {
404             ByteVector va = ByteVector.fromArray(bspec, ba, i);
405             ShortVector vb = (ShortVector) va.castShape(sspec, 0);
406             vb.intoArray(sb, 0);
407 
408             for (int j = 0; j < Math.min(bspec.length(), sspec.length()); j++) {
409                 Assert.assertEquals(sb[j], (short) ba[j + i]);
410             }
411         }
412     }
413 
testByteToInt()414     private static void testByteToInt() {
415         for (int i = 0; i < bspec.loopBound(LENGTH); i += bspec.length()) {
416             ByteVector va = ByteVector.fromArray(bspec, ba, i);
417             IntVector vb = (IntVector) va.castShape(ispec, 0);
418             vb.intoArray(ib, 0);
419 
420             for (int j = 0; j < Math.min(bspec.length(), ispec.length()); j++) {
421                 Assert.assertEquals(ib[j], (int) ba[j + i]);
422             }
423         }
424     }
425 
testByteToLong()426     private static void testByteToLong() {
427         for (int i = 0; i < bspec.loopBound(LENGTH); i += bspec.length()) {
428             ByteVector va = ByteVector.fromArray(bspec, ba, i);
429             LongVector vb = (LongVector) va.castShape(lspec, 0);
430             vb.intoArray(lb, 0);
431 
432             for (int j = 0; j < Math.min(bspec.length(), lspec.length()); j++) {
433                 Assert.assertEquals(lb[j], (long) ba[j + i]);
434             }
435         }
436     }
437 
testByteToFloat()438     private static void testByteToFloat() {
439         for (int i = 0; i < bspec.loopBound(LENGTH); i += bspec.length()) {
440             ByteVector va = ByteVector.fromArray(bspec, ba, i);
441             FloatVector vb = (FloatVector) va.castShape(fspec, 0);
442             vb.intoArray(fb, 0);
443 
444             for (int j = 0; j < Math.min(bspec.length(), fspec.length()); j++) {
445                 Assert.assertEquals(fb[j], (float) ba[j + i]);
446             }
447         }
448     }
449 
testByteToDouble()450     private static void testByteToDouble() {
451         for (int i = 0; i < bspec.loopBound(LENGTH); i += bspec.length()) {
452             ByteVector va = ByteVector.fromArray(bspec, ba, i);
453             DoubleVector vb = (DoubleVector) va.castShape(dspec, 0);
454             vb.intoArray(db, 0);
455 
456             for (int j = 0; j < Math.min(bspec.length(), dspec.length()); j++) {
457                 Assert.assertEquals(db[j], (double) ba[j + i]);
458             }
459         }
460     }
461 
462 
463     @Test
testCastShape64()464     public void testCastShape64() {
465         initialize();
466         for (int i = 0; i < NUM_ITER; i++) {
467             testDoubleToByte();
468             testDoubleToShort();
469             testDoubleToInt();
470             testDoubleToLong();
471             testDoubleToFloat();
472 
473             testFloatToByte();
474             testFloatToShort();
475             testFloatToInt();
476             testFloatToLong();
477             testFloatToDouble();
478 
479             testLongToByte();
480             testLongToShort();
481             testLongToInt();
482             testLongToFloat();
483             testLongToDouble();
484 
485             testIntToByte();
486             testIntToShort();
487             testIntToLong();
488             testIntToFloat();
489             testIntToDouble();
490 
491             testShortToByte();
492             testShortToInt();
493             testShortToLong();
494             testShortToFloat();
495             testShortToDouble();
496 
497             testByteToShort();
498             testByteToInt();
499             testByteToLong();
500             testByteToFloat();
501             testByteToDouble();
502         }
503     }
504 }
505