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  * Tests for zero vectorization.
19  */
20 public class Main {
21 
22   /// CHECK-START: void Main.zeroz(boolean[]) loop_optimization (before)
23   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
24   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
25   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Zero>>] loop:<<Loop>>      outer_loop:none
26   //
27   /// CHECK-START-ARM64: void Main.zeroz(boolean[]) loop_optimization (after)
28   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
29   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<Zero>>]        loop:none
30   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
31   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Repl>>] loop:<<Loop>>      outer_loop:none
zeroz(boolean[] x)32   private static void zeroz(boolean[] x) {
33     for (int i = 0; i < x.length; i++) {
34       x[i] = false;
35     }
36   }
37 
38   /// CHECK-START: void Main.zerob(byte[]) loop_optimization (before)
39   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
40   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
41   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Zero>>] loop:<<Loop>>      outer_loop:none
42   //
43   /// CHECK-START-ARM64: void Main.zerob(byte[]) loop_optimization (after)
44   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
45   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<Zero>>]        loop:none
46   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
47   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Repl>>] loop:<<Loop>>      outer_loop:none
zerob(byte[] x)48   private static void zerob(byte[] x) {
49     for (int i = 0; i < x.length; i++) {
50       x[i] = 0;
51     }
52   }
53 
54   /// CHECK-START: void Main.zeroc(char[]) loop_optimization (before)
55   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
56   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
57   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Zero>>] loop:<<Loop>>      outer_loop:none
58   //
59   /// CHECK-START-ARM64: void Main.zeroc(char[]) loop_optimization (after)
60   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
61   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<Zero>>]        loop:none
62   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
63   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Repl>>] loop:<<Loop>>      outer_loop:none
zeroc(char[] x)64   private static void zeroc(char[] x) {
65     for (int i = 0; i < x.length; i++) {
66       x[i] = 0;
67     }
68   }
69 
70   /// CHECK-START: void Main.zeros(short[]) loop_optimization (before)
71   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
72   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
73   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Zero>>] loop:<<Loop>>      outer_loop:none
74   //
75   /// CHECK-START-ARM64: void Main.zeros(short[]) loop_optimization (after)
76   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
77   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<Zero>>]        loop:none
78   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
79   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Repl>>] loop:<<Loop>>      outer_loop:none
zeros(short[] x)80   private static void zeros(short[] x) {
81     for (int i = 0; i < x.length; i++) {
82       x[i] = 0;
83     }
84   }
85 
86   /// CHECK-START: void Main.zeroi(int[]) loop_optimization (before)
87   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
88   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
89   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Zero>>] loop:<<Loop>>      outer_loop:none
90   //
91   /// CHECK-START-ARM64: void Main.zeroi(int[]) loop_optimization (after)
92   /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0                        loop:none
93   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<Zero>>]        loop:none
94   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
95   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Repl>>] loop:<<Loop>>      outer_loop:none
zeroi(int[] x)96   private static void zeroi(int[] x) {
97     for (int i = 0; i < x.length; i++) {
98       x[i] = 0;
99     }
100   }
101 
102   /// CHECK-START: void Main.zerol(long[]) loop_optimization (before)
103   /// CHECK-DAG: <<Zero:j\d+>> LongConstant 0                       loop:none
104   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
105   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Zero>>] loop:<<Loop>>      outer_loop:none
106   //
107   /// CHECK-START-ARM64: void Main.zerol(long[]) loop_optimization (after)
108   /// CHECK-DAG: <<Zero:j\d+>> LongConstant 0                       loop:none
109   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<Zero>>]        loop:none
110   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
111   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Repl>>] loop:<<Loop>>      outer_loop:none
zerol(long[] x)112   private static void zerol(long[] x) {
113     for (int i = 0; i < x.length; i++) {
114       x[i] = 0;
115     }
116   }
117 
118   /// CHECK-START: void Main.zerof(float[]) loop_optimization (before)
119   /// CHECK-DAG: <<Zero:f\d+>> FloatConstant 0                      loop:none
120   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
121   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Zero>>] loop:<<Loop>>      outer_loop:none
122   //
123   /// CHECK-START-ARM64: void Main.zerof(float[]) loop_optimization (after)
124   /// CHECK-DAG: <<Zero:f\d+>> FloatConstant 0                      loop:none
125   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<Zero>>]        loop:none
126   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
127   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Repl>>] loop:<<Loop>>      outer_loop:none
zerof(float[] x)128   private static void zerof(float[] x) {
129     for (int i = 0; i < x.length; i++) {
130       x[i] = 0;
131     }
132   }
133 
134   /// CHECK-START: void Main.zerod(double[]) loop_optimization (before)
135   /// CHECK-DAG: <<Zero:d\d+>> DoubleConstant 0                     loop:none
136   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
137   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Zero>>] loop:<<Loop>>      outer_loop:none
138   //
139   /// CHECK-START-ARM64: void Main.zerod(double[]) loop_optimization (after)
140   /// CHECK-DAG: <<Zero:d\d+>> DoubleConstant 0                     loop:none
141   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<Zero>>]        loop:none
142   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                  loop:<<Loop:B\d+>> outer_loop:none
143   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Repl>>] loop:<<Loop>>      outer_loop:none
zerod(double[] x)144   private static void zerod(double[] x) {
145     for (int i = 0; i < x.length; i++) {
146       x[i] = 0;
147     }
148   }
149 
main(String[] args)150   public static void main(String[] args) {
151     int total = 1111;
152 
153     boolean[] xz = new boolean[total];
154     byte[]    xb = new byte[total];
155     char[]    xc = new char[total];
156     short[]   xs = new short[total];
157     int[]     xi = new int[total];
158     long[]    xl = new long[total];
159     float[]   xf = new float[total];
160     double[]  xd = new double[total];
161 
162     for (int i = 0; i < total; i++) {
163       xz[i] = true;
164       xb[i] = 1;
165       xc[i] = 1;
166       xs[i] = 1;
167       xi[i] = 1;
168       xl[i] = 1;
169       xf[i] = 1;
170       xd[i] = 1;
171     }
172 
173     for (int i = 0; i < total; i++) {
174       expectEquals(true, xz[i]);
175       expectEquals(1, xb[i]);
176       expectEquals(1, xc[i]);
177       expectEquals(1, xs[i]);
178       expectEquals(1, xi[i]);
179       expectEquals(1, xl[i]);
180       expectEquals(1, xf[i]);
181       expectEquals(1, xd[i]);
182     }
183 
184     zeroz(xz);
185     zerob(xb);
186     zeroc(xc);
187     zeros(xs);
188     zeroi(xi);
189     zerol(xl);
190     zerof(xf);
191     zerod(xd);
192 
193     for (int i = 0; i < total; i++) {
194       expectEquals(false, xz[i]);
195       expectEquals(0, xb[i]);
196       expectEquals(0, xc[i]);
197       expectEquals(0, xs[i]);
198       expectEquals(0, xi[i]);
199       expectEquals(0, xl[i]);
200       expectEquals(0, xf[i]);
201       expectEquals(0, xd[i]);
202     }
203 
204     System.out.println("passed");
205   }
206 
expectEquals(boolean expected, boolean result)207   private static void expectEquals(boolean expected, boolean result) {
208     if (expected != result) {
209       throw new Error("Expected: " + expected + ", found: " + result);
210     }
211   }
212 
expectEquals(int expected, int result)213   private static void expectEquals(int expected, int result) {
214     if (expected != result) {
215       throw new Error("Expected: " + expected + ", found: " + result);
216     }
217   }
218 
expectEquals(long expected, long result)219   private static void expectEquals(long expected, long result) {
220     if (expected != result) {
221       throw new Error("Expected: " + expected + ", found: " + result);
222     }
223   }
224 
expectEquals(float expected, float result)225   private static void expectEquals(float expected, float result) {
226     if (expected != result) {
227       throw new Error("Expected: " + expected + ", found: " + result);
228     }
229   }
230 
expectEquals(double expected, double result)231   private static void expectEquals(double expected, double result) {
232     if (expected != result) {
233       throw new Error("Expected: " + expected + ", found: " + result);
234     }
235   }
236 }
237