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