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. Note that this class provides a mere 19 * functional test, not a precise numerical verifier. 20 */ 21 public class Main { 22 23 static float[] a; 24 25 // 26 // Arithmetic operations. 27 // 28 29 /// CHECK-START: void Main.add(float) loop_optimization (before) 30 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 31 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 32 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 33 // 34 /// CHECK-START-ARM64: void Main.add(float) loop_optimization (after) 35 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 36 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 37 /// CHECK-DAG: VecAdd loop:<<Loop>> outer_loop:none 38 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none add(float x)39 static void add(float x) { 40 for (int i = 0; i < 128; i++) 41 a[i] += x; 42 } 43 44 /// CHECK-START: void Main.sub(float) loop_optimization (before) 45 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 46 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 47 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 48 // 49 /// CHECK-START-ARM64: void Main.sub(float) loop_optimization (after) 50 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 51 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 52 /// CHECK-DAG: VecSub loop:<<Loop>> outer_loop:none 53 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none sub(float x)54 static void sub(float x) { 55 for (int i = 0; i < 128; i++) 56 a[i] -= x; 57 } 58 59 /// CHECK-START: void Main.mul(float) loop_optimization (before) 60 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 61 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 62 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 63 // 64 /// CHECK-START-ARM64: void Main.mul(float) loop_optimization (after) 65 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 66 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 67 /// CHECK-DAG: VecMul loop:<<Loop>> outer_loop:none 68 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none mul(float x)69 static void mul(float x) { 70 for (int i = 0; i < 128; i++) 71 a[i] *= x; 72 } 73 74 /// CHECK-START: void Main.div(float) loop_optimization (before) 75 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 76 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 77 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 78 // 79 /// CHECK-START-ARM64: void Main.div(float) loop_optimization (after) 80 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 81 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 82 /// CHECK-DAG: VecDiv loop:<<Loop>> outer_loop:none 83 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none div(float x)84 static void div(float x) { 85 for (int i = 0; i < 128; i++) 86 a[i] /= x; 87 } 88 89 /// CHECK-START: void Main.neg() loop_optimization (before) 90 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 91 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 92 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 93 // 94 /// CHECK-START-ARM64: void Main.neg() loop_optimization (after) 95 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 96 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 97 /// CHECK-DAG: VecNeg loop:<<Loop>> outer_loop:none 98 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none neg()99 static void neg() { 100 for (int i = 0; i < 128; i++) 101 a[i] = -a[i]; 102 } 103 104 /// CHECK-START: void Main.abs() loop_optimization (before) 105 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 106 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 107 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 108 // 109 /// CHECK-START-ARM64: void Main.abs() loop_optimization (after) 110 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 111 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 112 /// CHECK-DAG: VecAbs loop:<<Loop>> outer_loop:none 113 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none abs()114 static void abs() { 115 for (int i = 0; i < 128; i++) 116 a[i] = Math.abs(a[i]); 117 } 118 119 /// CHECK-START: void Main.conv(int[]) loop_optimization (before) 120 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 121 /// CHECK-DAG: ArrayGet loop:<<Loop>> outer_loop:none 122 /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none 123 // 124 /// CHECK-START-ARM64: void Main.conv(int[]) loop_optimization (after) 125 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none 126 /// CHECK-DAG: VecLoad loop:<<Loop>> outer_loop:none 127 /// CHECK-DAG: VecCnv loop:<<Loop>> outer_loop:none 128 /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none conv(int[] b)129 static void conv(int[] b) { 130 for (int i = 0; i < 128; i++) 131 a[i] = b[i]; 132 } 133 134 // 135 // Loop bounds. 136 // 137 bounds()138 static void bounds() { 139 for (int i = 1; i < 127; i++) 140 a[i] += 11; 141 } 142 143 // 144 // Test Driver. 145 // 146 main(String[] args)147 public static void main(String[] args) { 148 // Set up. 149 a = new float[128]; 150 for (int i = 0; i < 128; i++) { 151 a[i] = i; 152 } 153 // Arithmetic operations. 154 add(2.0f); 155 for (int i = 0; i < 128; i++) { 156 expectEquals(i + 2, a[i], "add"); 157 } 158 sub(2.0f); 159 for (int i = 0; i < 128; i++) { 160 expectEquals(i, a[i], "sub"); 161 } 162 mul(2.0f); 163 for (int i = 0; i < 128; i++) { 164 expectEquals(i + i, a[i], "mul"); 165 } 166 div(2.0f); 167 for (int i = 0; i < 128; i++) { 168 expectEquals(i, a[i], "div"); 169 } 170 neg(); 171 for (int i = 0; i < 128; i++) { 172 expectEquals(-i, a[i], "neg"); 173 } 174 // Loop bounds. 175 bounds(); 176 expectEquals(0, a[0], "bounds0"); 177 for (int i = 1; i < 127; i++) { 178 expectEquals(11 - i, a[i], "bounds"); 179 } 180 expectEquals(-127, a[127], "bounds127"); 181 // Abs. 182 abs(); 183 expectEquals(0, a[0], "abs0"); 184 for (int i = 1; i <= 11; i++) { 185 expectEquals(11 - i, a[i], "abs_lo"); 186 } 187 for (int i = 12; i < 127; i++) { 188 expectEquals(i - 11, a[i], "abs_hi"); 189 } 190 expectEquals(127, a[127], "abs127"); 191 // Conversion. 192 int[] b = new int[128]; 193 for (int i = 0; i < 128; i++) { 194 b[i] = 1000 * i; 195 } 196 conv(b); 197 for (int i = 1; i < 127; i++) { 198 expectEquals(1000.0f * i, a[i], "conv"); 199 } 200 // Done. 201 System.out.println("passed"); 202 } 203 expectEquals(float expected, float result, String action)204 private static void expectEquals(float expected, float result, String action) { 205 if (expected != result) { 206 throw new Error("Expected: " + expected + ", found: " + result + " for " + action); 207 } 208 } 209 } 210