1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * Checker test for arm and arm64 simd optimizations.
19  */
20 public class Main {
21 
expectEquals(int expected, int result)22   private static void expectEquals(int expected, int result) {
23     if (expected != result) {
24       throw new Error("Expected: " + expected + ", found: " + result);
25     }
26   }
27 
28   /// CHECK-START-ARM64: void Main.encodableConstants(byte[], short[], char[], int[], long[], float[], double[]) disassembly (after)
29   /// CHECK-DAG: <<C1:i\d+>>   IntConstant 1
30   /// CHECK-DAG: <<C2:i\d+>>   IntConstant 2
31   /// CHECK-DAG: <<C3:i\d+>>   IntConstant 3
32   /// CHECK-DAG: <<C4:i\d+>>   IntConstant 4
33   /// CHECK-DAG: <<L5:j\d+>>   LongConstant 5
34   /// CHECK-DAG: <<F2:f\d+>>   FloatConstant 2
35   /// CHECK-DAG: <<D20:d\d+>>  DoubleConstant 20
36   //
37   /// CHECK-DAG:               VecReplicateScalar [<<C1>>]
38   /// CHECK-DAG:               movi v{{[0-9]+}}.16b, #0x1
39   /// CHECK-DAG:               VecReplicateScalar [<<C2>>]
40   /// CHECK-DAG:               movi v{{[0-9]+}}.8h, #0x2, lsl #0
41   /// CHECK-DAG:               VecReplicateScalar [<<C3>>]
42   /// CHECK-DAG:               movi v{{[0-9]+}}.8h, #0x3, lsl #0
43   /// CHECK-DAG:               VecReplicateScalar [<<C4>>]
44   /// CHECK-DAG:               movi v{{[0-9]+}}.4s, #0x4, lsl #0
45   /// CHECK-DAG:               VecReplicateScalar [<<L5>>]
46   /// CHECK-DAG:               dup v{{[0-9]+}}.2d, x{{[0-9]+}}
47   /// CHECK-DAG:               VecReplicateScalar [<<F2>>]
48   /// CHECK-DAG:               fmov v{{[0-9]+}}.4s, #0x0
49   /// CHECK-DAG:               VecReplicateScalar [<<D20>>]
50   /// CHECK-DAG:               fmov v{{[0-9]+}}.2d, #0x34
encodableConstants(byte[] b, short[] s, char[] c, int[] a, long[] l, float[] f, double[] d)51   private static void encodableConstants(byte[] b, short[] s, char[] c, int[] a, long[] l, float[] f, double[] d) {
52     for (int i = 0; i < ARRAY_SIZE; i++) {
53       b[i] += 1;
54     }
55     for (int i = 0; i < ARRAY_SIZE; i++) {
56       s[i] += 2;
57     }
58     for (int i = 0; i < ARRAY_SIZE; i++) {
59       c[i] += 3;
60     }
61     for (int i = 0; i < ARRAY_SIZE; i++) {
62       a[i] += 4;
63     }
64     for (int i = 0; i < ARRAY_SIZE; i++) {
65       l[i] += 5;
66     }
67     for (int i = 0; i < ARRAY_SIZE; i++) {
68       f[i] += 2.0f;
69     }
70     for (int i = 0; i < ARRAY_SIZE; i++) {
71       d[i] += 20.0;
72     }
73   }
74 
sumArray(byte[] b, short[] s, char[] c, int[] a, long[] l, float[] f, double[] d)75   private static int sumArray(byte[] b, short[] s, char[] c, int[] a, long[] l, float[] f, double[] d) {
76     int sum = 0;
77     for (int i = 0; i < ARRAY_SIZE; i++) {
78       sum += b[i] + s[i] + c[i] + a[i] + l[i] + f[i] + d[i];
79     }
80     return sum;
81   }
82 
83   public static final int ARRAY_SIZE = 100;
84 
main(String[] args)85   public static void main(String[] args) {
86     byte[] b = new byte[ARRAY_SIZE];
87     short[] s = new short[ARRAY_SIZE];
88     char[] c = new char[ARRAY_SIZE];
89     int[] a = new int[ARRAY_SIZE];
90     long[] l = new long[ARRAY_SIZE];
91     float[] f = new float[ARRAY_SIZE];
92     double[] d = new double[ARRAY_SIZE];
93 
94     encodableConstants(b, s, c, a, l, f, d);
95     expectEquals(3700, sumArray(b, s, c, a, l, f, d));
96 
97     System.out.println("passed");
98   }
99 }
100