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 int[] a; 23 24 // 25 // Arithmetic operations. 26 // 27 28 /// CHECK-START: void Main.add(int) 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-ARM64: void Main.add(int) 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: VecAdd loop:<<Loop>> outer_loop:none 37 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none add(int x)38 static void add(int x) { 39 for (int i = 0; i < 128; i++) 40 a[i] += x; 41 } 42 43 /// CHECK-START: void Main.sub(int) loop_optimization (before) 44 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 45 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 46 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 47 // 48 /// CHECK-START-ARM64: void Main.sub(int) loop_optimization (after) 49 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 50 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 51 /// CHECK-DAG: VecSub loop:<<Loop>> outer_loop:none 52 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none sub(int x)53 static void sub(int x) { 54 for (int i = 0; i < 128; i++) 55 a[i] -= x; 56 } 57 58 /// CHECK-START: void Main.mul(int) loop_optimization (before) 59 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 60 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 61 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 62 // 63 /// CHECK-START-ARM64: void Main.mul(int) loop_optimization (after) 64 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 65 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 66 /// CHECK-DAG: VecMul loop:<<Loop>> outer_loop:none 67 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none mul(int x)68 static void mul(int x) { 69 for (int i = 0; i < 128; i++) 70 a[i] *= x; 71 } 72 73 /// CHECK-START: void Main.div(int) loop_optimization (before) 74 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 75 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 76 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 77 // 78 /// CHECK-START: void Main.div(int) loop_optimization (after) 79 // 80 // Not supported on any architecture. 81 // div(int x)82 static void div(int x) { 83 for (int i = 0; i < 128; i++) 84 a[i] /= x; 85 } 86 87 /// CHECK-START: void Main.neg() loop_optimization (before) 88 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 89 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 90 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 91 // 92 /// CHECK-START-ARM64: void Main.neg() loop_optimization (after) 93 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 94 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 95 /// CHECK-DAG: VecNeg loop:<<Loop>> outer_loop:none 96 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none neg()97 static void neg() { 98 for (int i = 0; i < 128; i++) 99 a[i] = -a[i]; 100 } 101 102 /// CHECK-START: void Main.not() loop_optimization (before) 103 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 104 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 105 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 106 // 107 /// CHECK-START-ARM64: void Main.not() loop_optimization (after) 108 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 109 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 110 /// CHECK-DAG: VecNot loop:<<Loop>> outer_loop:none 111 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none not()112 static void not() { 113 for (int i = 0; i < 128; i++) 114 a[i] = ~a[i]; 115 } 116 117 /// CHECK-START: void Main.shl4() loop_optimization (before) 118 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 119 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 120 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 121 // 122 /// CHECK-START-ARM64: void Main.shl4() loop_optimization (after) 123 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 124 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 125 /// CHECK-DAG: VecShl loop:<<Loop>> outer_loop:none 126 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none shl4()127 static void shl4() { 128 for (int i = 0; i < 128; i++) 129 a[i] <<= 4; 130 } 131 132 /// CHECK-START: void Main.sar2() loop_optimization (before) 133 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 134 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 135 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 136 // 137 /// CHECK-START-ARM64: void Main.sar2() loop_optimization (after) 138 // 139 // TODO: fill in when supported sar2()140 static void sar2() { 141 for (int i = 0; i < 128; i++) 142 a[i] >>= 2; 143 } 144 145 /// CHECK-START: void Main.shr2() loop_optimization (before) 146 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 147 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 148 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 149 // 150 /// CHECK-START-ARM64: void Main.shr2() loop_optimization (after) 151 // 152 // TODO: fill in when supported shr2()153 static void shr2() { 154 for (int i = 0; i < 128; i++) 155 a[i] >>>= 2; 156 } 157 158 // 159 // Shift sanity. 160 // 161 shr32()162 static void shr32() { 163 for (int i = 0; i < 128; i++) 164 a[i] >>>= 32; // 0, since & 31 165 } 166 shr33()167 static void shr33() { 168 for (int i = 0; i < 128; i++) 169 a[i] >>>= 33; // 1, since & 31 170 } 171 172 // 173 // Loop bounds. 174 // 175 bounds()176 static void bounds() { 177 for (int i = 1; i < 127; i++) 178 a[i] += 11; 179 } 180 181 // 182 // Test Driver. 183 // 184 main(String[] args)185 public static void main(String[] args) { 186 // Set up. 187 a = new int[128]; 188 for (int i = 0; i < 128; i++) { 189 a[i] = i; 190 } 191 // Arithmetic operations. 192 add(2); 193 for (int i = 0; i < 128; i++) { 194 expectEquals(i + 2, a[i], "add"); 195 } 196 sub(2); 197 for (int i = 0; i < 128; i++) { 198 expectEquals(i, a[i], "sub"); 199 } 200 mul(2); 201 for (int i = 0; i < 128; i++) { 202 expectEquals(i + i, a[i], "mul"); 203 } 204 div(2); 205 for (int i = 0; i < 128; i++) { 206 expectEquals(i, a[i], "div"); 207 } 208 neg(); 209 for (int i = 0; i < 128; i++) { 210 expectEquals(-i, a[i], "neg"); 211 } 212 // Loop bounds. 213 bounds(); 214 expectEquals(0, a[0], "bounds0"); 215 for (int i = 1; i < 127; i++) { 216 expectEquals(11 - i, a[i], "bounds"); 217 } 218 expectEquals(-127, a[127], "bounds127"); 219 // Shifts. 220 for (int i = 0; i < 128; i++) { 221 a[i] = 0xffffffff; 222 } 223 shl4(); 224 for (int i = 0; i < 128; i++) { 225 expectEquals(0xfffffff0, a[i], "shl4"); 226 } 227 sar2(); 228 for (int i = 0; i < 128; i++) { 229 expectEquals(0xfffffffc, a[i], "sar2"); 230 } 231 shr2(); 232 for (int i = 0; i < 128; i++) { 233 expectEquals(0x3fffffff, a[i], "shr2"); 234 } 235 shr32(); 236 for (int i = 0; i < 128; i++) { 237 expectEquals(0x3fffffff, a[i], "shr32"); 238 } 239 shr33(); 240 for (int i = 0; i < 128; i++) { 241 expectEquals(0x1fffffff, a[i], "shr33"); 242 } 243 not(); 244 for (int i = 0; i < 128; i++) { 245 expectEquals(0xe0000000, a[i], "not"); 246 } 247 // Done. 248 System.out.println("passed"); 249 } 250 expectEquals(int expected, int result, String action)251 private static void expectEquals(int expected, int result, String action) { 252 if (expected != result) { 253 throw new Error("Expected: " + expected + ", found: " + result + " for " + action); 254 } 255 } 256 } 257