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: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
30   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
31   //
32   /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.and(boolean) loop_optimization (after)
33   /// CHECK-DAG: VecLoad  loop:<<Loop:B\d+>> outer_loop:none
34   /// CHECK-DAG: VecAnd   loop:<<Loop>>      outer_loop:none
35   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
and(boolean x)36   static void and(boolean x) {
37     for (int i = 0; i < 128; i++)
38       a[i] &= x;  // NOTE: bitwise and, not the common &&
39   }
40 
41   /// CHECK-START: void Main.or(boolean) loop_optimization (before)
42   /// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
43   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
44   //
45   /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.or(boolean) loop_optimization (after)
46   /// CHECK-DAG: VecLoad  loop:<<Loop:B\d+>> outer_loop:none
47   /// CHECK-DAG: VecOr    loop:<<Loop>>      outer_loop:none
48   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
or(boolean x)49   static void or(boolean x) {
50     for (int i = 0; i < 128; i++)
51       a[i] |= x;  // NOTE: bitwise or, not the common ||
52   }
53 
54   /// CHECK-START: void Main.xor(boolean) loop_optimization (before)
55   /// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
56   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
57   //
58   /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.xor(boolean) loop_optimization (after)
59   /// CHECK-DAG: VecLoad  loop:<<Loop:B\d+>> outer_loop:none
60   /// CHECK-DAG: VecXor   loop:<<Loop>>      outer_loop:none
61   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
xor(boolean x)62   static void xor(boolean x) {
63     for (int i = 0; i < 128; i++)
64       a[i] ^= x;  // NOTE: bitwise xor
65   }
66 
67   /// CHECK-START: void Main.not() loop_optimization (before)
68   /// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
69   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
70   //
71   /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.not() loop_optimization (after)
72   /// CHECK-DAG: VecLoad  loop:<<Loop:B\d+>> outer_loop:none
73   /// CHECK-DAG: VecNot   loop:<<Loop>>      outer_loop:none
74   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
not()75   static void not() {
76     for (int i = 0; i < 128; i++)
77       a[i] = !a[i];
78   }
79 
80   //
81   // Test Driver.
82   //
83 
main(String[] args)84   public static void main(String[] args) {
85     // Set up.
86     a = new boolean[128];
87     for (int i = 0; i < 128; i++) {
88       a[i] = (i & 1) == 0;
89     }
90     // Arithmetic operations.
91     and(true);
92     for (int i = 0; i < 128; i++) {
93       expectEquals((i & 1) == 0, a[i], "and-true");
94     }
95     xor(true);
96     for (int i = 0; i < 128; i++) {
97       expectEquals((i & 1) != 0, a[i], "xor-true");
98     }
99     xor(false);
100     for (int i = 0; i < 128; i++) {
101       expectEquals((i & 1) != 0, a[i], "xor-false");
102     }
103     not();
104     for (int i = 0; i < 128; i++) {
105       expectEquals((i & 1) == 0, a[i], "not");
106     }
107     or(true);
108     for (int i = 0; i < 128; i++) {
109       expectEquals(true, a[i], "or-true");
110     }
111     and(false);
112     for (int i = 0; i < 128; i++) {
113       expectEquals(false, a[i], "and-false");
114     }
115     or(false);
116     for (int i = 0; i < 128; i++) {
117       expectEquals(false, a[i], "or-false");
118     }
119     // Done.
120     System.out.println("passed");
121   }
122 
expectEquals(boolean expected, boolean result, String action)123   private static void expectEquals(boolean expected, boolean result, String action) {
124     if (expected != result) {
125       throw new Error("Expected: " + expected + ", found: " + result + " for " + action);
126     }
127   }
128 }
129