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 = 256; 26 private static final int M = N * N + 15; 27 28 static byte[] sB1 = new byte[M]; 29 static byte[] sB2 = new byte[M]; 30 static byte[] sBo = new byte[M]; 31 32 /// CHECK-START: void Main.halving_add_signed(byte[], byte[], byte[]) 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:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none 36 /// CHECK-DAG: <<Get2:b\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:b\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(byte[], byte[], byte[]) 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(byte[] b1, byte[] b2, byte[] 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] = (byte) ((b1[i] + b2[i]) >>> 1); 52 } 53 } 54 55 /// CHECK-START: void Main.halving_add_unsigned(byte[], byte[], byte[]) loop_optimization (before) 56 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 57 /// CHECK-DAG: <<I255:i\d+>> IntConstant 255 loop:none 58 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 59 /// CHECK-DAG: <<Get1:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none 60 /// CHECK-DAG: <<Get2:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none 61 /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<I255>>] loop:<<Loop>> outer_loop:none 62 /// CHECK-DAG: <<And2:i\d+>> And [<<Get2>>,<<I255>>] 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:b\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(byte[], byte[], byte[]) 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(byte[] b1, byte[] b2, byte[] 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] = (byte) (((b1[i] & 0xff) + (b2[i] & 0xff)) >>> 1); 78 } 79 } 80 81 /// CHECK-START: void Main.rounding_halving_add_signed(byte[], byte[], byte[]) 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:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none 85 /// CHECK-DAG: <<Get2:b\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:b\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(byte[], byte[], byte[]) 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(byte[] b1, byte[] b2, byte[] 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] = (byte) ((b1[i] + b2[i] + 1) >>> 1); 102 } 103 } 104 105 /// CHECK-START: void Main.rounding_halving_add_unsigned(byte[], byte[], byte[]) loop_optimization (before) 106 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 107 /// CHECK-DAG: <<I255:i\d+>> IntConstant 255 loop:none 108 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 109 /// CHECK-DAG: <<Get1:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none 110 /// CHECK-DAG: <<Get2:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none 111 /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<I255>>] loop:<<Loop>> outer_loop:none 112 /// CHECK-DAG: <<And2:i\d+>> And [<<Get2>>,<<I255>>] 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:b\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(byte[], byte[], byte[]) 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(byte[] b1, byte[] b2, byte[] 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] = (byte) (((b1[i] & 0xff) + (b2[i] & 0xff) + 1) >>> 1); 129 } 130 } 131 132 /// CHECK-START: void Main.halving_add_signed_constant(byte[], byte[]) loop_optimization (before) 133 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 134 /// CHECK-DAG: <<I127:i\d+>> IntConstant 127 loop:none 135 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 136 /// CHECK-DAG: <<Get:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none 137 /// CHECK-DAG: <<Add:i\d+>> Add [<<Get>>,<<I127>>] loop:<<Loop>> outer_loop:none 138 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>] loop:<<Loop>> outer_loop:none 139 /// CHECK-DAG: <<Cnv:b\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(byte[], byte[]) loop_optimization (after) 143 /// CHECK-DAG: <<I127:i\d+>> IntConstant 127 loop:none 144 /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<I127>>] 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(byte[] b1, byte[] bo) { 150 int min_length = Math.min(bo.length, b1.length); 151 for (int i = 0; i < min_length; i++) { 152 bo[i] = (byte) ((b1[i] + 0x7f) >>> 1); 153 } 154 } 155 156 /// CHECK-START: void Main.halving_add_unsigned_constant(byte[], byte[]) loop_optimization (before) 157 /// CHECK-DAG: <<I1:i\d+>> IntConstant 1 loop:none 158 /// CHECK-DAG: <<I255:i\d+>> IntConstant 255 loop:none 159 /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none 160 /// CHECK-DAG: <<Get:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none 161 /// CHECK-DAG: <<And:i\d+>> And [<<Get>>,<<I255>>] loop:<<Loop>> outer_loop:none 162 /// CHECK-DAG: <<Add:i\d+>> Add [<<And>>,<<I255>>] loop:<<Loop>> outer_loop:none 163 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>] loop:<<Loop>> outer_loop:none 164 /// CHECK-DAG: <<Cnv:b\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(byte[], byte[]) loop_optimization (after) 168 /// CHECK-DAG: <<I255:i\d+>> IntConstant 255 loop:none 169 /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<I255>>] 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(byte[] b1, byte[] bo) { 175 int min_length = Math.min(bo.length, b1.length); 176 for (int i = 0; i < min_length; i++) { 177 bo[i] = (byte) (((b1[i] & 0xff) + 0xff) >>> 1); 178 } 179 } 180 181 public static void main(String[] args) { 182 // Initialize cross-values to test all cases, and also 183 // set up some extra values to exercise the cleanup loop. 184 int k = 0; 185 for (int i = 0; i < N; i++) { 186 for (int j = 0; j < N; j++) { 187 sB1[k] = (byte) i; 188 sB2[k] = (byte) j; 189 k++; 190 } 191 } 192 for (int i = 0; i < 15; i++) { 193 sB1[k] = (byte) i; 194 sB2[k] = 100; 195 k++; 196 } 197 expectEquals(k, M); 198 199 // Test halving add idioms. Note that the expected result is computed 200 // with the arithmetic >> to demonstrate the computed narrower result 201 // does not depend on the wider >> or >>>. 202 halving_add_signed(sB1, sB2, sBo); 203 for (int i = 0; i < M; i++) { 204 byte e = (byte) ((sB1[i] + sB2[i]) >> 1); 205 expectEquals(e, sBo[i]); 206 } 207 halving_add_unsigned(sB1, sB2, sBo); 208 for (int i = 0; i < M; i++) { 209 byte e = (byte) (((sB1[i] & 0xff) + (sB2[i] & 0xff)) >> 1); 210 expectEquals(e, sBo[i]); 211 } 212 rounding_halving_add_signed(sB1, sB2, sBo); 213 for (int i = 0; i < M; i++) { 214 byte e = (byte) ((sB1[i] + sB2[i] + 1) >> 1); 215 expectEquals(e, sBo[i]); 216 } 217 rounding_halving_add_unsigned(sB1, sB2, sBo); 218 for (int i = 0; i < M; i++) { 219 byte e = (byte) (((sB1[i] & 0xff) + (sB2[i] & 0xff) + 1) >> 1); 220 expectEquals(e, sBo[i]); 221 } 222 halving_add_signed_constant(sB1, sBo); 223 for (int i = 0; i < M; i++) { 224 byte e = (byte) ((sB1[i] + 0x7f) >> 1); 225 expectEquals(e, sBo[i]); 226 } 227 halving_add_unsigned_constant(sB1, sBo); 228 for (int i = 0; i < M; i++) { 229 byte e = (byte) (((sB1[i] & 0xff) + 0xff) >> 1); 230 expectEquals(e, sBo[i]); 231 } 232 233 System.out.println("passed"); 234 } 235 236 private static void expectEquals(int expected, int result) { 237 if (expected != result) { 238 throw new Error("Expected: " + expected + ", found: " + result); 239 } 240 } 241 } 242