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