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 halving-add idiomatic vectorization. 19 * 20 * Alternative version expressed with logical shift right 21 * in the higher precision (has no impact on idiom). 22 */ 23 public class HaddAltChar { 24 25 private static final int N = 64 * 1024; 26 private static final int M = N + 31; 27 28 static char[] sB1 = new char[M]; 29 static char[] sB2 = new char[M]; 30 static char[] sBo = new char[M]; 31 32 /// CHECK-START: void HaddAltChar.halving_add_unsigned(char[], char[], char[]) loop_optimization (before) 33 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 34 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 35 /// CHECK-DAG: <<Get1:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 36 /// CHECK-DAG: <<Get2:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 37 /// CHECK-DAG: <<Add:i\d+>> Add [<<Get1>>,<<Get2>>] loop:<<Loop>> outer_loop:none 38 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>] loop:<<Loop>> outer_loop:none 39 /// CHECK-DAG: <<Cnv:c\d+>> TypeConversion [<<UShr>>] loop:<<Loop>> outer_loop:none 40 /// CHECK-DAG: ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>> outer_loop:none 41 // 42 /// CHECK-START-{ARM,ARM64}: void HaddAltChar.halving_add_unsigned(char[], char[], char[]) loop_optimization (after) 43 /// CHECK-DAG: <<Get1:d\d+>> VecLoad loop:<<Loop:B\d+>> outer_loop:none 44 /// CHECK-DAG: <<Get2:d\d+>> VecLoad loop:<<Loop>> outer_loop:none 45 /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none 46 /// CHECK-DAG: VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>> outer_loop:none halving_add_unsigned(char[] b1, char[] b2, char[] bo)47 private static void halving_add_unsigned(char[] b1, char[] b2, char[] bo) { 48 int min_length = Math.min(bo.length, Math.min(b1.length, b2.length)); 49 for (int i = 0; i < min_length; i++) { 50 bo[i] = (char) ((b1[i] + b2[i]) >>> 1); 51 } 52 } 53 54 /// CHECK-START: void HaddAltChar.halving_add_also_unsigned(char[], char[], char[]) instruction_simplifier (before) 55 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 56 /// CHECK-DAG: <<IMAX:i\d+>> IntConstant 65535 loop:none 57 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 58 /// CHECK-DAG: <<Get1:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 59 /// CHECK-DAG: <<Get2:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 60 /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<IMAX>>] loop:<<Loop>> outer_loop:none 61 /// CHECK-DAG: <<And2:i\d+>> And [<<IMAX>>,<<Get2>>] loop:<<Loop>> outer_loop:none 62 /// CHECK-DAG: <<Add:i\d+>> Add [<<And1>>,<<And2>>] loop:<<Loop>> outer_loop:none 63 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>] loop:<<Loop>> outer_loop:none 64 /// CHECK-DAG: <<Cnv:c\d+>> TypeConversion [<<UShr>>] loop:<<Loop>> outer_loop:none 65 /// CHECK-DAG: ArraySet [{{l\d+}},{{i\d+}},<<Cnv>>] loop:<<Loop>> outer_loop:none 66 // 67 /// CHECK-START: void HaddAltChar.halving_add_also_unsigned(char[], char[], char[]) loop_optimization (before) 68 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 69 /// CHECK-DAG: <<IMAX:i\d+>> IntConstant 65535 loop:none 70 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 71 /// CHECK-DAG: <<Get1:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 72 /// CHECK-DAG: <<Get2:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 73 /// CHECK-DAG: <<Add:i\d+>> Add [<<Get1>>,<<Get2>>] loop:<<Loop>> outer_loop:none 74 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>] loop:<<Loop>> outer_loop:none 75 /// CHECK-DAG: <<Cnv:c\d+>> TypeConversion [<<UShr>>] loop:<<Loop>> outer_loop:none 76 /// CHECK-DAG: ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>> outer_loop:none 77 // 78 /// CHECK-START-{ARM,ARM64}: void HaddAltChar.halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after) 79 /// CHECK-DAG: <<Get1:d\d+>> VecLoad loop:<<Loop:B\d+>> outer_loop:none 80 /// CHECK-DAG: <<Get2:d\d+>> VecLoad loop:<<Loop>> outer_loop:none 81 /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none 82 /// CHECK-DAG: VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>> outer_loop:none 83 // 84 // Note: HAnd has no impact (already a zero extension). 85 // halving_add_also_unsigned(char[] b1, char[] b2, char[] bo)86 private static void halving_add_also_unsigned(char[] b1, char[] b2, char[] bo) { 87 int min_length = Math.min(bo.length, Math.min(b1.length, b2.length)); 88 for (int i = 0; i < min_length; i++) { 89 bo[i] = (char) (((b1[i] & 0xffff) + (b2[i] & 0xffff)) >>> 1); 90 } 91 } 92 93 /// CHECK-START: void HaddAltChar.rounding_halving_add_unsigned(char[], char[], char[]) loop_optimization (before) 94 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 95 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 96 /// CHECK-DAG: <<Get1:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 97 /// CHECK-DAG: <<Get2:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 98 /// CHECK-DAG: <<Add1:i\d+>> Add [<<Get1>>,<<Get2>>] loop:<<Loop>> outer_loop:none 99 /// CHECK-DAG: <<Add2:i\d+>> Add [<<Add1>>,<<I1>>] loop:<<Loop>> outer_loop:none 100 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add2>>,<<I1>>] loop:<<Loop>> outer_loop:none 101 /// CHECK-DAG: <<Cnv:c\d+>> TypeConversion [<<UShr>>] loop:<<Loop>> outer_loop:none 102 /// CHECK-DAG: ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>> outer_loop:none 103 // 104 /// CHECK-START-{ARM,ARM64}: void HaddAltChar.rounding_halving_add_unsigned(char[], char[], char[]) loop_optimization (after) 105 /// CHECK-DAG: <<Get1:d\d+>> VecLoad loop:<<Loop:B\d+>> outer_loop:none 106 /// CHECK-DAG: <<Get2:d\d+>> VecLoad loop:<<Loop>> outer_loop:none 107 /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:true loop:<<Loop>> outer_loop:none 108 /// CHECK-DAG: VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>> outer_loop:none rounding_halving_add_unsigned(char[] b1, char[] b2, char[] bo)109 private static void rounding_halving_add_unsigned(char[] b1, char[] b2, char[] bo) { 110 int min_length = Math.min(bo.length, Math.min(b1.length, b2.length)); 111 for (int i = 0; i < min_length; i++) { 112 bo[i] = (char) ((b1[i] + b2[i] + 1) >>> 1); 113 } 114 } 115 116 /// CHECK-START: void HaddAltChar.rounding_halving_add_also_unsigned(char[], char[], char[]) instruction_simplifier (before) 117 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 118 /// CHECK-DAG: <<IMAX:i\d+>> IntConstant 65535 loop:none 119 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 120 /// CHECK-DAG: <<Get1:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 121 /// CHECK-DAG: <<Get2:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 122 /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<IMAX>>] loop:<<Loop>> outer_loop:none 123 /// CHECK-DAG: <<And2:i\d+>> And [<<IMAX>>,<<Get2>>] loop:<<Loop>> outer_loop:none 124 /// CHECK-DAG: <<Add1:i\d+>> Add [<<And1>>,<<And2>>] loop:<<Loop>> outer_loop:none 125 /// CHECK-DAG: <<Add2:i\d+>> Add [<<Add1>>,<<I1>>] loop:<<Loop>> outer_loop:none 126 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add2>>,<<I1>>] loop:<<Loop>> outer_loop:none 127 /// CHECK-DAG: <<Cnv:c\d+>> TypeConversion [<<UShr>>] loop:<<Loop>> outer_loop:none 128 /// CHECK-DAG: ArraySet [{{l\d+}},{{i\d+}},<<Cnv>>] loop:<<Loop>> outer_loop:none 129 // 130 /// CHECK-START: void HaddAltChar.rounding_halving_add_also_unsigned(char[], char[], char[]) loop_optimization (before) 131 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 132 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 133 /// CHECK-DAG: <<Get1:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 134 /// CHECK-DAG: <<Get2:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 135 /// CHECK-DAG: <<Add1:i\d+>> Add [<<Get1>>,<<Get2>>] loop:<<Loop>> outer_loop:none 136 /// CHECK-DAG: <<Add2:i\d+>> Add [<<Add1>>,<<I1>>] loop:<<Loop>> outer_loop:none 137 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add2>>,<<I1>>] loop:<<Loop>> outer_loop:none 138 /// CHECK-DAG: <<Cnv:c\d+>> TypeConversion [<<UShr>>] loop:<<Loop>> outer_loop:none 139 /// CHECK-DAG: ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>> outer_loop:none 140 // 141 /// CHECK-START-{ARM,ARM64}: void HaddAltChar.rounding_halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after) 142 /// CHECK-DAG: <<Get1:d\d+>> VecLoad loop:<<Loop:B\d+>> outer_loop:none 143 /// CHECK-DAG: <<Get2:d\d+>> VecLoad loop:<<Loop>> outer_loop:none 144 /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:true loop:<<Loop>> outer_loop:none 145 /// CHECK-DAG: VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>> outer_loop:none 146 // 147 // Note: HAnd has no impact (already a zero extension). 148 // rounding_halving_add_also_unsigned(char[] b1, char[] b2, char[] bo)149 private static void rounding_halving_add_also_unsigned(char[] b1, char[] b2, char[] bo) { 150 int min_length = Math.min(bo.length, Math.min(b1.length, b2.length)); 151 for (int i = 0; i < min_length; i++) { 152 bo[i] = (char) (((b1[i] & 0xffff) + (b2[i] & 0xffff) + 1) >>> 1); 153 } 154 } 155 156 /// CHECK-START: void HaddAltChar.halving_add_unsigned_constant(char[], char[]) loop_optimization (before) 157 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 158 /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535 loop:none 159 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 160 /// CHECK-DAG: <<Get:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 161 /// CHECK-DAG: <<Add:i\d+>> Add [<<Get>>,<<UMAX>>] loop:<<Loop>> outer_loop:none 162 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>] loop:<<Loop>> outer_loop:none 163 /// CHECK-DAG: <<Cnv:c\d+>> TypeConversion [<<UShr>>] loop:<<Loop>> outer_loop:none 164 /// CHECK-DAG: ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>> outer_loop:none 165 // 166 /// CHECK-START-{ARM,ARM64}: void HaddAltChar.halving_add_unsigned_constant(char[], char[]) loop_optimization (after) 167 /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535 loop:none 168 /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>] loop:none 169 /// CHECK-DAG: <<Get:d\d+>> VecLoad loop:<<Loop:B\d+>> outer_loop:none 170 /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get>>,<<Repl>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none 171 /// CHECK-DAG: VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>> outer_loop:none halving_add_unsigned_constant(char[] b1, char[] bo)172 private static void halving_add_unsigned_constant(char[] b1, char[] bo) { 173 int min_length = Math.min(bo.length, b1.length); 174 for (int i = 0; i < min_length; i++) { 175 bo[i] = (char) ((b1[i] + 0xffff) >>> 1); 176 } 177 } 178 179 /// CHECK-START: void HaddAltChar.halving_add_also_unsigned_constant(char[], char[]) instruction_simplifier (before) 180 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 181 /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535 loop:none 182 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 183 /// CHECK-DAG: <<Get:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 184 /// CHECK-DAG: <<And:i\d+>> And [<<Get>>,<<UMAX>>] loop:<<Loop>> outer_loop:none 185 /// CHECK-DAG: <<Add:i\d+>> Add [<<And>>,<<UMAX>>] loop:<<Loop>> outer_loop:none 186 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>] loop:<<Loop>> outer_loop:none 187 /// CHECK-DAG: <<Cnv:c\d+>> TypeConversion [<<UShr>>] loop:<<Loop>> outer_loop:none 188 /// CHECK-DAG: ArraySet [{{l\d+}},{{i\d+}},<<Cnv>>] loop:<<Loop>> outer_loop:none 189 // 190 /// CHECK-START: void HaddAltChar.halving_add_also_unsigned_constant(char[], char[]) loop_optimization (before) 191 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 192 /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535 loop:none 193 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 194 /// CHECK-DAG: <<Get:c\d+>> ArrayGet loop:<<Loop>> outer_loop:none 195 /// CHECK-DAG: <<Add:i\d+>> Add [<<Get>>,<<UMAX>>] loop:<<Loop>> outer_loop:none 196 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>] loop:<<Loop>> outer_loop:none 197 /// CHECK-DAG: <<Cnv:c\d+>> TypeConversion [<<UShr>>] loop:<<Loop>> outer_loop:none 198 /// CHECK-DAG: ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>> outer_loop:none 199 // 200 /// CHECK-START-{ARM,ARM64}: void HaddAltChar.halving_add_also_unsigned_constant(char[], char[]) loop_optimization (after) 201 /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535 loop:none 202 /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>] loop:none 203 /// CHECK-DAG: <<Get:d\d+>> VecLoad loop:<<Loop:B\d+>> outer_loop:none 204 /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get>>,<<Repl>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none 205 /// CHECK-DAG: VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>> outer_loop:none 206 // 207 // Note: HAnd has no impact (already a zero extension). 208 // halving_add_also_unsigned_constant(char[] b1, char[] bo)209 private static void halving_add_also_unsigned_constant(char[] b1, char[] bo) { 210 int min_length = Math.min(bo.length, b1.length); 211 for (int i = 0; i < min_length; i++) { 212 bo[i] = (char) (((b1[i] & 0xffff) + 0xffff) >>> 1); 213 } 214 } 215 main()216 public static void main() { 217 // Some interesting values. 218 char[] interesting = { 219 (char) 0x0000, 220 (char) 0x0001, 221 (char) 0x0002, 222 (char) 0x1234, 223 (char) 0x8000, 224 (char) 0x8001, 225 (char) 0x7fff, 226 (char) 0xffff 227 }; 228 // Initialize cross-values to test all cases, and also 229 // set up some extra values to exercise the cleanup loop. 230 for (int i = 0; i < M; i++) { 231 sB1[i] = (char) i; 232 sB2[i] = interesting[i & 7]; 233 } 234 235 // Test halving add idioms. Note that the expected result is computed 236 // with the arithmetic >> to demonstrate the computed narrower result 237 // does not depend on the wider >> or >>>. 238 halving_add_unsigned(sB1, sB2, sBo); 239 for (int i = 0; i < M; i++) { 240 char e = (char) ((sB1[i] + sB2[i]) >> 1); 241 expectEquals(e, sBo[i]); 242 } 243 halving_add_also_unsigned(sB1, sB2, sBo); 244 for (int i = 0; i < M; i++) { 245 char e = (char) ((sB1[i] + sB2[i]) >> 1); 246 expectEquals(e, sBo[i]); 247 } 248 rounding_halving_add_unsigned(sB1, sB2, sBo); 249 for (int i = 0; i < M; i++) { 250 char e = (char) ((sB1[i] + sB2[i] + 1) >> 1); 251 expectEquals(e, sBo[i]); 252 } 253 rounding_halving_add_also_unsigned(sB1, sB2, sBo); 254 for (int i = 0; i < M; i++) { 255 char e = (char) ((sB1[i] + sB2[i] + 1) >> 1); 256 expectEquals(e, sBo[i]); 257 } 258 halving_add_unsigned_constant(sB1, sBo); 259 for (int i = 0; i < M; i++) { 260 char e = (char) ((sB1[i] + 0xffff) >> 1); 261 expectEquals(e, sBo[i]); 262 } 263 halving_add_also_unsigned_constant(sB1, sBo); 264 for (int i = 0; i < M; i++) { 265 char e = (char) ((sB1[i] + 0xffff) >> 1); 266 expectEquals(e, sBo[i]); 267 } 268 269 System.out.println("HaddAltChar passed"); 270 } 271 expectEquals(int expected, int result)272 private static void expectEquals(int expected, int result) { 273 if (expected != result) { 274 throw new Error("Expected: " + expected + ", found: " + result); 275 } 276 } 277 } 278