1 /*
2  * Copyright (c) 2021, Huawei Technologies Co., Ltd. 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 /**
25  * @test
26  * @bug 8260585
27  * @summary AArch64: Wrong code generated for shifting right and accumulating four unsigned short integers.
28  * @run main/othervm compiler.c2.TestShiftRightAndAccumulate
29  * @run main/othervm -Xcomp compiler.c2.TestShiftRightAndAccumulate
30  * @run main/othervm -XX:-SuperWordLoopUnrollAnalysis compiler.c2.TestShiftRightAndAccumulate
31  */
32 
33 package compiler.c2;
34 
35 import java.util.Random;
36 import java.util.Arrays;
37 
38 public class TestShiftRightAndAccumulate {
39     private static final int SMALL_LEN = 16;
40     private static final int LARGE_LEN = 1000;
41     private static final int NUM_ITERS = 200000;
42     private static final int MAX_TESTS = 10;
43     private static final int SMALL_INTS_LEN  = 3;
44     private static final int SMALL_BYTES_LEN = 80;
45 
46     private static byte[]  bytesA,  bytesB,  bytesC,  bytesD;
47     private static short[] shortsA, shortsB, shortsC, shortsD;
48     private static char[]  charsA,  charsB,  charsC;
49     private static int[]   intsA,   intsB,   intsC;
50     private static long[]  longsA,  longsB,  longsC;
51 
52     private static byte  gBytes[][];
53     private static short gShorts[][];
54     private static char  gChars[][];
55     private static int   gInts[][];
56     private static long  gLongs[][];
57 
58     private static Random r = new Random(32781);
59 
main(String args[])60     public static void main(String args[]) {
61       test_init(SMALL_LEN);
62       for (int it = 0; it < NUM_ITERS; it++) {
63           test_bytes();
64           test_shorts();
65           test_chars();
66           test_ints();
67           test_longs();
68       }
69 
70       test_init(LARGE_LEN);
71       for (int it = 0; it < NUM_ITERS; it++) {
72           test_bytes();
73           test_shorts();
74           test_chars();
75           test_ints();
76           test_longs();
77       }
78       System.out.println("Test PASSED");
79     }
80 
test_bytes()81     static void test_bytes() {
82         for (int i = 0; i < bytesC.length; i++) {
83             bytesC[i] = (byte) (bytesA[i] + (bytesB[i] >> 1));
84             bytesD[i] = (byte) (bytesA[i] + bytesB[i]);
85         }
86         assertTrue(Arrays.equals(bytesC, gBytes[0]));
87         assertTrue(Arrays.equals(bytesD, gBytes[MAX_TESTS]));
88 
89         for (int i = 0; i < bytesC.length; i++) {
90             bytesC[i] = (byte) (bytesA[i] + (bytesB[i] >> 8));
91             bytesD[i] = (byte) (bytesA[i] + bytesB[i]);
92         }
93         assertTrue(Arrays.equals(bytesC, gBytes[1]));
94         assertTrue(Arrays.equals(bytesD, gBytes[MAX_TESTS + 1]));
95 
96         for (int i = 0; i < bytesC.length; i++) {
97             bytesC[i] = (byte) (bytesA[i] + (bytesB[i] >> 13));
98             bytesD[i] = (byte) (bytesA[i] + bytesB[i]);
99         }
100         assertTrue(Arrays.equals(bytesC, gBytes[2]));
101         assertTrue(Arrays.equals(bytesD, gBytes[MAX_TESTS + 2]));
102 
103         for (int i = 0; i < bytesC.length; i++) {
104             bytesC[i] = (byte) (bytesA[i] + (bytesB[i] >> 19));
105             bytesD[i] = (byte) (bytesA[i] + bytesB[i]);
106         }
107         assertTrue(Arrays.equals(bytesC, gBytes[3]));
108         assertTrue(Arrays.equals(bytesD, gBytes[MAX_TESTS + 3]));
109 
110         for (int i = 0; i < bytesC.length; i++) {
111             bytesC[i] = (byte) (bytesA[i] + (bytesB[i] >>> 1));
112             bytesD[i] = (byte) (bytesA[i] + bytesB[i]);
113         }
114         assertTrue(Arrays.equals(bytesC, gBytes[4]));
115         assertTrue(Arrays.equals(bytesD, gBytes[MAX_TESTS + 4]));
116 
117         for (int i = 0; i < bytesC.length; i++) {
118             bytesC[i] = (byte) (bytesA[i] + (bytesB[i] >>> 8));
119             bytesD[i] = (byte) (bytesA[i] + bytesB[i]);
120         }
121         assertTrue(Arrays.equals(bytesC, gBytes[5]));
122         assertTrue(Arrays.equals(bytesD, gBytes[MAX_TESTS + 5]));
123 
124         for (int i = 0; i < bytesC.length; i++) {
125             bytesC[i] = (byte) (bytesA[i] + (bytesB[i] >>> 13));
126             bytesD[i] = (byte) (bytesA[i] + bytesB[i]);
127         }
128         assertTrue(Arrays.equals(bytesC, gBytes[6]));
129         assertTrue(Arrays.equals(bytesD, gBytes[MAX_TESTS + 6]));
130 
131         for (int i = 0; i < bytesC.length; i++) {
132             bytesC[i] = (byte) (bytesA[i] + (bytesB[i] >>> 19));
133             bytesD[i] = (byte) (bytesA[i] + bytesB[i]);
134         }
135         assertTrue(Arrays.equals(bytesC, gBytes[7]));
136         assertTrue(Arrays.equals(bytesD, gBytes[MAX_TESTS + 7]));
137     }
138 
test_shorts()139     static void test_shorts() {
140         for (int i = 0; i < shortsC.length; i++) {
141             shortsC[i] = (short) (shortsA[i] + (shortsB[i] >> 5));
142             shortsD[i] = (short) (shortsA[i] + shortsB[i]);
143         }
144         assertTrue(Arrays.equals(shortsC, gShorts[0]));
145         assertTrue(Arrays.equals(shortsD, gShorts[MAX_TESTS]));
146 
147         for (int i = 0; i < shortsC.length; i++) {
148             shortsC[i] = (short) (shortsA[i] + (shortsB[i] >> 16));
149             shortsD[i] = (short) (shortsA[i] + shortsB[i]);
150         }
151         assertTrue(Arrays.equals(shortsC, gShorts[1]));
152         assertTrue(Arrays.equals(shortsD, gShorts[MAX_TESTS + 1]));
153 
154         for (int i = 0; i < shortsC.length; i++) {
155             shortsC[i] = (short) (shortsA[i] + (shortsB[i] >> 23));
156             shortsD[i] = (short) (shortsA[i] + shortsB[i]);
157         }
158         assertTrue(Arrays.equals(shortsC, gShorts[2]));
159         assertTrue(Arrays.equals(shortsD, gShorts[MAX_TESTS + 2]));
160 
161         for (int i = 0; i < shortsC.length; i++) {
162             shortsC[i] = (short) (shortsA[i] + (shortsB[i] >> 35));
163             shortsD[i] = (short) (shortsA[i] + shortsB[i]);
164         }
165         assertTrue(Arrays.equals(shortsC, gShorts[3]));
166         assertTrue(Arrays.equals(shortsD, gShorts[MAX_TESTS + 3]));
167 
168         for (int i = 0; i < shortsC.length; i++) {
169             shortsC[i] = (short) (shortsA[i] + (shortsB[i] >>> 7));
170             shortsD[i] = (short) (shortsA[i] + shortsB[i]);
171         }
172         assertTrue(Arrays.equals(shortsC, gShorts[4]));
173         assertTrue(Arrays.equals(shortsD, gShorts[MAX_TESTS + 4]));
174 
175         for (int i = 0; i < shortsC.length; i++) {
176             shortsC[i] = (short) (shortsA[i] + (shortsB[i] >>> 16));
177             shortsD[i] = (short) (shortsA[i] + shortsB[i]);
178         }
179         assertTrue(Arrays.equals(shortsC, gShorts[5]));
180         assertTrue(Arrays.equals(shortsD, gShorts[MAX_TESTS + 5]));
181 
182         for (int i = 0; i < shortsC.length; i++) {
183             shortsC[i] = (short) (shortsA[i] + (shortsB[i] >>> 23));
184             shortsD[i] = (short) (shortsA[i] + shortsB[i]);
185         }
186         assertTrue(Arrays.equals(shortsC, gShorts[6]));
187         assertTrue(Arrays.equals(shortsD, gShorts[MAX_TESTS + 6]));
188 
189         for (int i = 0; i < shortsC.length; i++) {
190             shortsC[i] = (short) (shortsA[i] + (shortsB[i] >>> 35));
191             shortsD[i] = (short) (shortsA[i] + shortsB[i]);
192         }
193         assertTrue(Arrays.equals(shortsC, gShorts[7]));
194         assertTrue(Arrays.equals(shortsD, gShorts[MAX_TESTS + 7]));
195     }
196 
test_chars()197     static void test_chars() {
198         for (int i = 0; i < charsC.length; i++) {
199             charsC[i] = (char) (charsA[i] + (charsB[i] >>> 4));
200             charsC[i] = (char) (charsC[i] + charsA[i] + charsB[i]);
201         }
202         assertTrue(Arrays.equals(charsC, gChars[0]));
203 
204         for (int i = 0; i < charsC.length; i++) {
205             charsC[i] = (char) (charsA[i] + (charsB[i] >>> 16));
206             charsC[i] = (char) (charsC[i] + charsA[i] + charsB[i]);
207         }
208         assertTrue(Arrays.equals(charsC, gChars[1]));
209 
210         for (int i = 0; i < charsC.length; i++) {
211             charsC[i] = (char) (charsA[i] + (charsB[i] >>> 19));
212             charsC[i] = (char) (charsC[i] + charsA[i] + charsB[i]);
213         }
214         assertTrue(Arrays.equals(charsC, gChars[2]));
215 
216         for (int i = 0; i < charsC.length; i++) {
217             charsC[i] = (char) (charsA[i] + (charsB[i] >>> 35));
218             charsC[i] = (char) (charsC[i] + charsA[i] + charsB[i]);
219         }
220         assertTrue(Arrays.equals(charsC, gChars[3]));
221     }
222 
test_ints()223     static void test_ints() {
224         for (int i = 0; i < intsC.length; i++) {
225             intsC[i] = (intsA[i] + (intsB[i] >> 19));
226             intsC[i] = (intsC[i] + intsA[i] + intsB[i]);
227         }
228         assertTrue(Arrays.equals(intsC, gInts[0]));
229 
230         for (int i = 0; i < intsC.length; i++) {
231             intsC[i] = (intsA[i] + (intsB[i] >> 32));
232             intsC[i] = (intsC[i] + intsA[i] + intsB[i]);
233         }
234         assertTrue(Arrays.equals(intsC, gInts[1]));
235 
236         for (int i = 0; i < intsC.length; i++) {
237             intsC[i] = (intsA[i] + (intsB[i] >> 49));
238             intsC[i] = (intsC[i] + intsA[i] + intsB[i]);
239         }
240         assertTrue(Arrays.equals(intsC, gInts[2]));
241 
242         for (int i = 0; i < intsC.length; i++) {
243             intsC[i] = (intsA[i] + (intsB[i] >> 67));
244             intsC[i] = (intsC[i] + intsA[i] + intsB[i]);
245         }
246         assertTrue(Arrays.equals(intsC, gInts[3]));
247 
248         for (int i = 0; i < intsC.length; i++) {
249             intsC[i] = (intsA[i] + (intsB[i] >>> 19));
250             intsC[i] = (intsC[i] + intsA[i] + intsB[i]);
251         }
252         assertTrue(Arrays.equals(intsC, gInts[4]));
253 
254         for (int i = 0; i < intsC.length; i++) {
255             intsC[i] = (intsA[i] + (intsB[i] >>> 32));
256             intsC[i] = (intsC[i] + intsA[i] + intsB[i]);
257         }
258         assertTrue(Arrays.equals(intsC, gInts[5]));
259 
260         for (int i = 0; i < intsC.length; i++) {
261             intsC[i] = (intsA[i] + (intsB[i] >>> 49));
262             intsC[i] = (intsC[i] + intsA[i] + intsB[i]);
263         }
264         assertTrue(Arrays.equals(intsC, gInts[6]));
265 
266         for (int i = 0; i < intsC.length; i++) {
267             intsC[i] = (intsA[i] + (intsB[i] >>> 67));
268             intsC[i] = (intsC[i] + intsA[i] + intsB[i]);
269         }
270         assertTrue(Arrays.equals(intsC, gInts[7]));
271     }
272 
test_longs()273     static void test_longs() {
274         for (int i = 0; i < longsC.length; i++) {
275             longsC[i] = (longsA[i] + (longsB[i] >> 37));
276             longsC[i] = (longsC[i] + longsA[i] + longsB[i]);
277         }
278         assertTrue(Arrays.equals(longsC, gLongs[0]));
279 
280         for (int i = 0; i < longsC.length; i++) {
281             longsC[i] = (longsA[i] + (longsB[i] >> 64));
282             longsC[i] = (longsC[i] + longsA[i] + longsB[i]);
283         }
284         assertTrue(Arrays.equals(longsC, gLongs[1]));
285 
286         for (int i = 0; i < longsC.length; i++) {
287             longsC[i] = (longsA[i] + (longsB[i] >> 93));
288             longsC[i] = (longsC[i] + longsA[i] + longsB[i]);
289         }
290         assertTrue(Arrays.equals(longsC, gLongs[2]));
291 
292         for (int i = 0; i < longsC.length; i++) {
293             longsC[i] = (longsA[i] + (longsB[i] >> 137));
294             longsC[i] = (longsC[i] + longsA[i] + longsB[i]);
295         }
296         assertTrue(Arrays.equals(longsC, gLongs[3]));
297 
298         for (int i = 0; i < longsC.length; i++) {
299             longsC[i] = (longsA[i] + (longsB[i] >>> 37));
300             longsC[i] = (longsC[i] + longsA[i] + longsB[i]);
301         }
302         assertTrue(Arrays.equals(longsC, gLongs[4]));
303 
304         for (int i = 0; i < longsC.length; i++) {
305             longsC[i] = (longsA[i] + (longsB[i] >>> 64));
306             longsC[i] = (longsC[i] + longsA[i] + longsB[i]);
307         }
308         assertTrue(Arrays.equals(longsC, gLongs[5]));
309 
310         for (int i = 0; i < longsC.length; i++) {
311             longsC[i] = (longsA[i] + (longsB[i] >>> 93));
312             longsC[i] = (longsC[i] + longsA[i] + longsB[i]);
313         }
314         assertTrue(Arrays.equals(longsC, gLongs[6]));
315 
316         for (int i = 0; i < longsC.length; i++) {
317             longsC[i] = (longsA[i] + (longsB[i] >>> 137));
318             longsC[i] = (longsC[i] + longsA[i] + longsB[i]);
319         }
320         assertTrue(Arrays.equals(longsC, gLongs[7]));
321     }
322 
test_init(int count)323     static void test_init(int count) {
324         int countI = count == SMALL_LEN ? SMALL_INTS_LEN  : count;
325         int countB = count == SMALL_LEN ? SMALL_BYTES_LEN : count;
326 
327         bytesA  = new byte[countB];
328         shortsA = new short[count];
329         charsA  = new char[count];
330         intsA   = new int[countI];
331         longsA  = new long[count];
332 
333         bytesB  = new byte[countB];
334         shortsB = new short[count];
335         charsB  = new char[count];
336         intsB   = new int[countI];
337         longsB  = new long[count];
338 
339         bytesC  = new byte[countB];
340         shortsC = new short[count];
341         charsC  = new char[count];
342         intsC   = new int[countI];
343         longsC  = new long[count];
344 
345         bytesD  = new byte[countB];
346         shortsD = new short[count];
347 
348         gBytes  = new byte[MAX_TESTS * 2][countB];
349         gShorts = new short[MAX_TESTS * 2][count];
350         gChars  = new char[MAX_TESTS][count];
351         gInts   = new int[MAX_TESTS][countI];
352         gLongs  = new long[MAX_TESTS][count];
353 
354         for (int i = 0; i < countB; i++) {
355             bytesA[i]  = (byte) r.nextInt();
356             bytesB[i]  = (byte) r.nextInt();
357 
358             gBytes[0][i]              = (byte) (bytesA[i] + (bytesB[i] >> 1));
359             gBytes[MAX_TESTS][i]      = (byte) (bytesA[i] + bytesB[i]);
360             gBytes[1][i]              = (byte) (bytesA[i] + (bytesB[i] >> 8));
361             gBytes[MAX_TESTS + 1][i]  = (byte) (bytesA[i] + bytesB[i]);
362             gBytes[2][i]              = (byte) (bytesA[i] + (bytesB[i] >> 13));
363             gBytes[MAX_TESTS + 2][i]  = (byte) (bytesA[i] + bytesB[i]);
364             gBytes[3][i]              = (byte) (bytesA[i] + (bytesB[i] >> 19));
365             gBytes[MAX_TESTS + 3][i]  = (byte) (bytesA[i] + bytesB[i]);
366             gBytes[4][i]              = (byte) (bytesA[i] + (bytesB[i] >>> 1));
367             gBytes[MAX_TESTS + 4][i]  = (byte) (bytesA[i] + bytesB[i]);
368             gBytes[5][i]              = (byte) (bytesA[i] + (bytesB[i] >>> 8));
369             gBytes[MAX_TESTS + 5][i]  = (byte) (bytesA[i] + bytesB[i]);
370             gBytes[6][i]              = (byte) (bytesA[i] + (bytesB[i] >>> 13));
371             gBytes[MAX_TESTS + 6][i]  = (byte) (bytesA[i] + bytesB[i]);
372             gBytes[7][i]              = (byte) (bytesA[i] + (bytesB[i] >>> 19));
373             gBytes[MAX_TESTS + 7][i]  = (byte) (bytesA[i] + bytesB[i]);
374         }
375 
376         for (int i = 0; i < count; i++) {
377             shortsA[i] = (short) r.nextInt();
378             charsA[i]  = (char) r.nextInt();
379             longsA[i]  = r.nextLong();
380 
381             shortsB[i] = (short) r.nextInt();
382             charsB[i]  = (char) r.nextInt();
383             longsB[i]  = r.nextLong();
384         }
385 
386         for (int i = 0; i < count; i++) {
387             gShorts[0][i]              = (short) (shortsA[i] + (shortsB[i] >> 5));
388             gShorts[MAX_TESTS][i]      = (short) (shortsA[i] + shortsB[i]);
389             gShorts[1][i]              = (short) (shortsA[i] + (shortsB[i] >> 16));
390             gShorts[MAX_TESTS + 1][i]  = (short) (shortsA[i] + shortsB[i]);
391             gShorts[2][i]              = (short) (shortsA[i] + (shortsB[i] >> 23));
392             gShorts[MAX_TESTS + 2][i]  = (short) (shortsA[i] + shortsB[i]);
393             gShorts[3][i]              = (short) (shortsA[i] + (shortsB[i] >> 35));
394             gShorts[MAX_TESTS + 3][i]  = (short) (shortsA[i] + shortsB[i]);
395             gShorts[4][i]              = (short) (shortsA[i] + (shortsB[i] >>> 7));
396             gShorts[MAX_TESTS + 4][i]  = (short) (shortsA[i] + shortsB[i]);
397             gShorts[5][i]              = (short) (shortsA[i] + (shortsB[i] >>> 16));
398             gShorts[MAX_TESTS + 5][i]  = (short) (shortsA[i] + shortsB[i]);
399             gShorts[6][i]              = (short) (shortsA[i] + (shortsB[i] >>> 23));
400             gShorts[MAX_TESTS + 6][i]  = (short) (shortsA[i] + shortsB[i]);
401             gShorts[7][i]              = (short) (shortsA[i] + (shortsB[i] >>> 35));
402             gShorts[MAX_TESTS + 7][i]  = (short) (shortsA[i] + shortsB[i]);
403 
404             gChars[0][i]  = (char) (charsA[i] + (charsB[i] >>> 4));
405             gChars[0][i]  = (char) (gChars[0][i] + charsA[i] + charsB[i]);
406             gChars[1][i]  = (char) (charsA[i] + (charsB[i] >>> 16));
407             gChars[1][i]  = (char) (gChars[1][i] + charsA[i] + charsB[i]);
408             gChars[2][i]  = (char) (charsA[i] + (charsB[i] >>> 19));
409             gChars[2][i]  = (char) (gChars[2][i] + charsA[i] + charsB[i]);
410             gChars[3][i]  = (char) (charsA[i] + (charsB[i] >>> 35));
411             gChars[3][i]  = (char) (gChars[3][i] + charsA[i] + charsB[i]);
412 
413             gLongs[0][i]  = longsA[i] + (longsB[i] >> 37);
414             gLongs[0][i]  = gLongs[0][i] + longsA[i] + longsB[i];
415             gLongs[1][i]  = longsA[i] + (longsB[i] >> 64);
416             gLongs[1][i]  = gLongs[1][i] + longsA[i] + longsB[i];
417             gLongs[2][i]  = longsA[i] + (longsB[i] >> 93);
418             gLongs[2][i]  = gLongs[2][i] + longsA[i] + longsB[i];
419             gLongs[3][i]  = longsA[i] + (longsB[i] >> 137);
420             gLongs[3][i]  = gLongs[3][i] + longsA[i] + longsB[i];
421             gLongs[4][i]  = longsA[i] + (longsB[i] >>> 37);
422             gLongs[4][i]  = gLongs[4][i] + longsA[i] + longsB[i];
423             gLongs[5][i]  = longsA[i] + (longsB[i] >>> 64);
424             gLongs[5][i]  = gLongs[5][i] + longsA[i] + longsB[i];
425             gLongs[6][i]  = longsA[i] + (longsB[i] >>> 93);
426             gLongs[6][i]  = gLongs[6][i] + longsA[i] + longsB[i];
427             gLongs[7][i]  = longsA[i] + (longsB[i] >>> 137);
428             gLongs[7][i]  = gLongs[7][i] + longsA[i] + longsB[i];
429         }
430 
431         for (int i = 0; i < intsA.length; i++) {
432             intsA[i]     = r.nextInt();
433             intsB[i]     = r.nextInt();
434             gInts[0][i]  = intsA[i] + (intsB[i] >> 19);
435             gInts[0][i]  = gInts[0][i] + intsA[i] + intsB[i];
436             gInts[1][i]  = intsA[i] + (intsB[i] >> 32);
437             gInts[1][i]  = gInts[1][i] + intsA[i] + intsB[i];
438             gInts[2][i]  = intsA[i] + (intsB[i] >> 49);
439             gInts[2][i]  = gInts[2][i] + intsA[i] + intsB[i];
440             gInts[3][i]  = intsA[i] + (intsB[i] >> 67);
441             gInts[3][i]  = gInts[3][i] + intsA[i] + intsB[i];
442             gInts[4][i]  = intsA[i] + (intsB[i] >>> 19);
443             gInts[4][i]  = gInts[4][i] + intsA[i] + intsB[i];
444             gInts[5][i]  = intsA[i] + (intsB[i] >>> 32);
445             gInts[5][i]  = gInts[5][i] + intsA[i] + intsB[i];
446             gInts[6][i]  = intsA[i] + (intsB[i] >>> 49);
447             gInts[6][i]  = gInts[6][i] + intsA[i] + intsB[i];
448             gInts[7][i]  = intsA[i] + (intsB[i] >>> 67);
449             gInts[7][i]  = gInts[7][i] + intsA[i] + intsB[i];
450         }
451     }
452 
assertTrue(boolean okay)453     static void assertTrue(boolean okay) {
454         if (!okay) {
455             throw new RuntimeException("Test Failed");
456         }
457     }
458 }
459