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-{ARM,ARM64,MIPS64}: void Main.halving_add_signed(byte[], byte[], byte[]) 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:Int8 rounded:false loop:<<Loop>> outer_loop:none
46   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
halving_add_signed(byte[] b1, byte[] b2, byte[] bo)47   private static void halving_add_signed(byte[] b1, byte[] b2, byte[] 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] = (byte) ((b1[i] + b2[i]) >>> 1);
51     }
52   }
53 
54   /// CHECK-START: void Main.halving_add_unsigned(byte[], byte[], byte[]) instruction_simplifier (before)
55   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
56   /// CHECK-DAG: <<I255:i\d+>> IntConstant 255                     loop:none
57   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
58   /// CHECK-DAG: <<Get1:b\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
59   /// CHECK-DAG: <<Get2:b\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
60   /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<I255>>]             loop:<<Loop>>      outer_loop:none
61   /// CHECK-DAG: <<And2:i\d+>> And [<<Get2>>,<<I255>>]             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:b\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(byte[], byte[], byte[]) 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:a\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
71   /// CHECK-DAG: <<Get2:a\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:b\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(byte[], byte[], byte[]) 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:Uint8 rounded:false loop:<<Loop>> outer_loop:none
81   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
halving_add_unsigned(byte[] b1, byte[] b2, byte[] bo)82   private static void halving_add_unsigned(byte[] b1, byte[] b2, byte[] 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] = (byte) (((b1[i] & 0xff) + (b2[i] & 0xff)) >>> 1);
86     }
87   }
88 
89   /// CHECK-START: void Main.rounding_halving_add_signed(byte[], byte[], byte[]) 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:b\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
93   /// CHECK-DAG: <<Get2:b\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:b\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(byte[], byte[], byte[]) 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:Int8 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(byte[] b1, byte[] b2, byte[] bo)105   private static void rounding_halving_add_signed(byte[] b1, byte[] b2, byte[] 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] = (byte) ((b1[i] + b2[i] + 1) >>> 1);
109     }
110   }
111 
112   /// CHECK-START: void Main.rounding_halving_add_unsigned(byte[], byte[], byte[]) instruction_simplifier (before)
113   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
114   /// CHECK-DAG: <<I255:i\d+>> IntConstant 255                     loop:none
115   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
116   /// CHECK-DAG: <<Get1:b\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
117   /// CHECK-DAG: <<Get2:b\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
118   /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<I255>>]             loop:<<Loop>>      outer_loop:none
119   /// CHECK-DAG: <<And2:i\d+>> And [<<Get2>>,<<I255>>]             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:b\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(byte[], byte[], byte[]) 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:a\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
130   /// CHECK-DAG: <<Get2:a\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:b\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(byte[], byte[], byte[]) 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:Uint8 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(byte[] b1, byte[] b2, byte[] bo)142   private static void rounding_halving_add_unsigned(byte[] b1, byte[] b2, byte[] 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] = (byte) (((b1[i] & 0xff) + (b2[i] & 0xff) + 1) >>> 1);
146     }
147   }
148 
149   /// CHECK-START: void Main.halving_add_signed_constant(byte[], byte[]) loop_optimization (before)
150   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
151   /// CHECK-DAG: <<I127:i\d+>> IntConstant 127                     loop:none
152   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
153   /// CHECK-DAG: <<Get:b\d+>>  ArrayGet                            loop:<<Loop>>      outer_loop:none
154   /// CHECK-DAG: <<Add:i\d+>>  Add [<<Get>>,<<I127>>]              loop:<<Loop>>      outer_loop:none
155   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
156   /// CHECK-DAG: <<Cnv:b\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(byte[], byte[]) loop_optimization (after)
160   /// CHECK-DAG: <<I127:i\d+>> IntConstant 127                       loop:none
161   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<I127>>]         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:Int8 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(byte[] b1, byte[] bo)165   private static void halving_add_signed_constant(byte[] b1, byte[] bo) {
166     int min_length = Math.min(bo.length, b1.length);
167     for (int i = 0; i < min_length; i++) {
168       bo[i] = (byte) ((b1[i] + 0x7f) >>> 1);
169     }
170   }
171 
172   /// CHECK-START: void Main.halving_add_unsigned_constant(byte[], byte[]) instruction_simplifier (before)
173   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
174   /// CHECK-DAG: <<I255:i\d+>> IntConstant 255                     loop:none
175   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
176   /// CHECK-DAG: <<Get:b\d+>>  ArrayGet                            loop:<<Loop>>      outer_loop:none
177   /// CHECK-DAG: <<And:i\d+>>  And [<<Get>>,<<I255>>]              loop:<<Loop>>      outer_loop:none
178   /// CHECK-DAG: <<Add:i\d+>>  Add [<<And>>,<<I255>>]              loop:<<Loop>>      outer_loop:none
179   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
180   /// CHECK-DAG: <<Cnv:b\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(byte[], byte[]) loop_optimization (before)
184   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
185   /// CHECK-DAG: <<I255:i\d+>> IntConstant 255                     loop:none
186   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
187   /// CHECK-DAG: <<Get:a\d+>>  ArrayGet                            loop:<<Loop>>      outer_loop:none
188   /// CHECK-DAG: <<Add:i\d+>>  Add [<<Get>>,<<I255>>]              loop:<<Loop>>      outer_loop:none
189   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
190   /// CHECK-DAG: <<Cnv:b\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(byte[], byte[]) loop_optimization (after)
194   /// CHECK-DAG: <<I255:i\d+>> IntConstant 255                       loop:none
195   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<I255>>]         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:Uint8 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(byte[] b1, byte[] bo)199   private static void halving_add_unsigned_constant(byte[] b1, byte[] bo) {
200     int min_length = Math.min(bo.length, b1.length);
201     for (int i = 0; i < min_length; i++) {
202       bo[i] = (byte) (((b1[i] & 0xff) + 0xff) >>> 1);
203     }
204   }
205 
main(String[] args)206   public static void main(String[] args) {
207     // Initialize cross-values to test all cases, and also
208     // set up some extra values to exercise the cleanup loop.
209     int k = 0;
210     for (int i = 0; i < N; i++) {
211       for (int j = 0; j < N; j++) {
212         sB1[k] = (byte) i;
213         sB2[k] = (byte) j;
214         k++;
215       }
216     }
217     for (int i = 0; i < 15; i++) {
218       sB1[k] = (byte) i;
219       sB2[k] = 100;
220       k++;
221     }
222     expectEquals(k, M);
223 
224     // Test halving add idioms. Note that the expected result is computed
225     // with the arithmetic >> to demonstrate the computed narrower result
226     // does not depend on the wider >> or >>>.
227     halving_add_signed(sB1, sB2, sBo);
228     for (int i = 0; i < M; i++) {
229       byte e = (byte) ((sB1[i] + sB2[i]) >> 1);
230       expectEquals(e, sBo[i]);
231     }
232     halving_add_unsigned(sB1, sB2, sBo);
233     for (int i = 0; i < M; i++) {
234       byte e = (byte) (((sB1[i] & 0xff) + (sB2[i] & 0xff)) >> 1);
235       expectEquals(e, sBo[i]);
236     }
237     rounding_halving_add_signed(sB1, sB2, sBo);
238     for (int i = 0; i < M; i++) {
239       byte e = (byte) ((sB1[i] + sB2[i] + 1) >> 1);
240       expectEquals(e, sBo[i]);
241     }
242     rounding_halving_add_unsigned(sB1, sB2, sBo);
243     for (int i = 0; i < M; i++) {
244       byte e = (byte) (((sB1[i] & 0xff) + (sB2[i] & 0xff) + 1) >> 1);
245       expectEquals(e, sBo[i]);
246     }
247     halving_add_signed_constant(sB1, sBo);
248     for (int i = 0; i < M; i++) {
249       byte e = (byte) ((sB1[i] + 0x7f) >> 1);
250       expectEquals(e, sBo[i]);
251     }
252     halving_add_unsigned_constant(sB1, sBo);
253     for (int i = 0; i < M; i++) {
254       byte e = (byte) (((sB1[i] & 0xff) + 0xff) >> 1);
255       expectEquals(e, sBo[i]);
256     }
257 
258     System.out.println("passed");
259   }
260 
expectEquals(int expected, int result)261   private static void expectEquals(int expected, int result) {
262     if (expected != result) {
263       throw new Error("Expected: " + expected + ", found: " + result);
264     }
265   }
266 }
267