• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Functional tests for SIMD vectorization.
19  */
20 public class Main {
21 
22   static boolean[] a;
23 
24   //
25   // Arithmetic operations.
26   //
27 
28   /// CHECK-START: void Main.and(boolean) loop_optimization (before)
29   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
30   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
31   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
32   //
33   /// CHECK-START-ARM: void Main.and(boolean) loop_optimization (after)
34   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
35   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
36   /// CHECK-DAG: VecAnd   loop:<<Loop>>      outer_loop:none
37   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
38   //
39   /// CHECK-START-ARM64: void Main.and(boolean) loop_optimization (after)
40   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
41   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
42   /// CHECK-DAG: VecAnd   loop:<<Loop>>      outer_loop:none
43   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
44   //
45   /// CHECK-START-MIPS64: void Main.and(boolean) loop_optimization (after)
46   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
47   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
48   /// CHECK-DAG: VecAnd   loop:<<Loop>>      outer_loop:none
49   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
and(boolean x)50   static void and(boolean x) {
51     for (int i = 0; i < 128; i++)
52       a[i] &= x;  // NOTE: bitwise and, not the common &&
53   }
54 
55   /// CHECK-START: void Main.or(boolean) loop_optimization (before)
56   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
57   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
58   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
59   //
60   /// CHECK-START-ARM: void Main.or(boolean) loop_optimization (after)
61   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
62   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
63   /// CHECK-DAG: VecOr    loop:<<Loop>>      outer_loop:none
64   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
65   //
66   /// CHECK-START-ARM64: void Main.or(boolean) loop_optimization (after)
67   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
68   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
69   /// CHECK-DAG: VecOr    loop:<<Loop>>      outer_loop:none
70   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
71   //
72   /// CHECK-START-MIPS64: void Main.or(boolean) loop_optimization (after)
73   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
74   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
75   /// CHECK-DAG: VecOr    loop:<<Loop>>      outer_loop:none
76   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
or(boolean x)77   static void or(boolean x) {
78     for (int i = 0; i < 128; i++)
79       a[i] |= x;  // NOTE: bitwise or, not the common ||
80   }
81 
82   /// CHECK-START: void Main.xor(boolean) loop_optimization (before)
83   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
84   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
85   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
86   //
87   /// CHECK-START-ARM: void Main.xor(boolean) loop_optimization (after)
88   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
89   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
90   /// CHECK-DAG: VecXor   loop:<<Loop>>      outer_loop:none
91   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
92   //
93   /// CHECK-START-ARM64: void Main.xor(boolean) loop_optimization (after)
94   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
95   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
96   /// CHECK-DAG: VecXor   loop:<<Loop>>      outer_loop:none
97   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
98   //
99   /// CHECK-START-MIPS64: void Main.xor(boolean) loop_optimization (after)
100   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
101   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
102   /// CHECK-DAG: VecXor   loop:<<Loop>>      outer_loop:none
103   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
xor(boolean x)104   static void xor(boolean x) {
105     for (int i = 0; i < 128; i++)
106       a[i] ^= x;  // NOTE: bitwise xor
107   }
108 
109   /// CHECK-START: void Main.not() loop_optimization (before)
110   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
111   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
112   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
113   //
114   /// CHECK-START-ARM: void Main.not() loop_optimization (after)
115   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
116   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
117   /// CHECK-DAG: VecNot   loop:<<Loop>>      outer_loop:none
118   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
119   //
120   /// CHECK-START-ARM64: void Main.not() loop_optimization (after)
121   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
122   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
123   /// CHECK-DAG: VecNot   loop:<<Loop>>      outer_loop:none
124   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
125   //
126   /// CHECK-START-MIPS64: void Main.not() loop_optimization (after)
127   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
128   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
129   /// CHECK-DAG: VecNot   loop:<<Loop>>      outer_loop:none
130   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
not()131   static void not() {
132     for (int i = 0; i < 128; i++)
133       a[i] = !a[i];
134   }
135 
136   //
137   // Test Driver.
138   //
139 
main(String[] args)140   public static void main(String[] args) {
141     // Set up.
142     a = new boolean[128];
143     for (int i = 0; i < 128; i++) {
144       a[i] = (i & 1) == 0;
145     }
146     // Arithmetic operations.
147     and(true);
148     for (int i = 0; i < 128; i++) {
149       expectEquals((i & 1) == 0, a[i], "and-true");
150     }
151     xor(true);
152     for (int i = 0; i < 128; i++) {
153       expectEquals((i & 1) != 0, a[i], "xor-true");
154     }
155     xor(false);
156     for (int i = 0; i < 128; i++) {
157       expectEquals((i & 1) != 0, a[i], "xor-false");
158     }
159     not();
160     for (int i = 0; i < 128; i++) {
161       expectEquals((i & 1) == 0, a[i], "not");
162     }
163     or(true);
164     for (int i = 0; i < 128; i++) {
165       expectEquals(true, a[i], "or-true");
166     }
167     and(false);
168     for (int i = 0; i < 128; i++) {
169       expectEquals(false, a[i], "and-false");
170     }
171     or(false);
172     for (int i = 0; i < 128; i++) {
173       expectEquals(false, a[i], "or-false");
174     }
175     // Done.
176     System.out.println("passed");
177   }
178 
expectEquals(boolean expected, boolean result, String action)179   private static void expectEquals(boolean expected, boolean result, String action) {
180     if (expected != result) {
181       throw new Error("Expected: " + expected + ", found: " + result + " for " + action);
182     }
183   }
184 }
185