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