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 HaddAltChar {
24 
25   private static final int N = 64 * 1024;
26   private static final int M = N + 31;
27 
28   static char[] sB1 = new char[M];
29   static char[] sB2 = new char[M];
30   static char[] sBo = new char[M];
31 
32   /// CHECK-START: void HaddAltChar.halving_add_unsigned(char[], char[], char[]) 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:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
36   /// CHECK-DAG: <<Get2:c\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:c\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: void HaddAltChar.halving_add_unsigned(char[], char[], char[]) 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:Uint16 rounded:false loop:<<Loop>> outer_loop:none
46   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
47   //
48   /// CHECK-START-ARM64: void HaddAltChar.halving_add_unsigned(char[], char[], char[]) loop_optimization (after)
49   /// CHECK-IF:     hasIsaFeature("sve") and os.environ.get('ART_FORCE_TRY_PREDICATED_SIMD') == 'true'
50   //
51   //      HalvingAdd idiom is not supported for SVE.
52   ///     CHECK-NOT: VecHalvingAdd
53   //
54   /// CHECK-ELSE:
55   //
56   ///     CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
57   ///     CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
58   ///     CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
59   ///     CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
60   //
61   /// CHECK-FI:
halving_add_unsigned(char[] b1, char[] b2, char[] bo)62   private static void halving_add_unsigned(char[] b1, char[] b2, char[] bo) {
63     int min_length = Math.min(bo.length, Math.min(b1.length, b2.length));
64     for (int i = 0; i < min_length; i++) {
65       bo[i] = (char) ((b1[i] + b2[i]) >>> 1);
66     }
67   }
68 
69   /// CHECK-START: void HaddAltChar.halving_add_also_unsigned(char[], char[], char[]) instruction_simplifier (before)
70   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
71   /// CHECK-DAG: <<IMAX:i\d+>> IntConstant 65535                   loop:none
72   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
73   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
74   /// CHECK-DAG: <<Get2:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
75   /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<IMAX>>]             loop:<<Loop>>      outer_loop:none
76   /// CHECK-DAG: <<And2:i\d+>> And [<<IMAX>>,<<Get2>>]             loop:<<Loop>>      outer_loop:none
77   /// CHECK-DAG: <<Add:i\d+>>  Add [<<And1>>,<<And2>>]             loop:<<Loop>>      outer_loop:none
78   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
79   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<UShr>>]           loop:<<Loop>>      outer_loop:none
80   /// CHECK-DAG:               ArraySet [{{l\d+}},{{i\d+}},<<Cnv>>] loop:<<Loop>>      outer_loop:none
81   //
82   /// CHECK-START: void HaddAltChar.halving_add_also_unsigned(char[], char[], char[]) loop_optimization (before)
83   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
84   /// CHECK-DAG: <<IMAX:i\d+>> IntConstant 65535                   loop:none
85   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
86   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
87   /// CHECK-DAG: <<Get2:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
88   /// CHECK-DAG: <<Add:i\d+>>  Add [<<Get1>>,<<Get2>>]             loop:<<Loop>>      outer_loop:none
89   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
90   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<UShr>>]           loop:<<Loop>>      outer_loop:none
91   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
92   //
93   /// CHECK-START-ARM: void HaddAltChar.halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after)
94   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
95   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
96   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
97   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
98   //
99   /// CHECK-START-ARM64: void HaddAltChar.halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after)
100   /// CHECK-IF:     hasIsaFeature("sve") and os.environ.get('ART_FORCE_TRY_PREDICATED_SIMD') == 'true'
101   //
102   //      HalvingAdd idiom is not supported for SVE.
103   ///     CHECK-NOT: VecHalvingAdd
104   //
105   /// CHECK-ELSE:
106   //
107   ///     CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
108   ///     CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
109   ///     CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
110   ///     CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
111   //
112   /// CHECK-FI:
113   //
114   // Note: HAnd has no impact (already a zero extension).
115   //
halving_add_also_unsigned(char[] b1, char[] b2, char[] bo)116   private static void halving_add_also_unsigned(char[] b1, char[] b2, char[] bo) {
117     int min_length = Math.min(bo.length, Math.min(b1.length, b2.length));
118     for (int i = 0; i < min_length; i++) {
119       bo[i] = (char) (((b1[i] & 0xffff) + (b2[i] & 0xffff)) >>> 1);
120     }
121   }
122 
123   /// CHECK-START: void HaddAltChar.rounding_halving_add_unsigned(char[], char[], char[]) 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:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
127   /// CHECK-DAG: <<Get2:c\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: <<UShr:i\d+>> UShr [<<Add2>>,<<I1>>]              loop:<<Loop>>      outer_loop:none
131   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<UShr>>]           loop:<<Loop>>      outer_loop:none
132   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
133   //
134   /// CHECK-START-ARM: void HaddAltChar.rounding_halving_add_unsigned(char[], char[], char[]) 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:Uint16 rounded:true loop:<<Loop>> outer_loop:none
138   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
139   //
140   /// CHECK-START-ARM64: void HaddAltChar.rounding_halving_add_unsigned(char[], char[], char[]) loop_optimization (after)
141   /// CHECK-IF:     hasIsaFeature("sve") and os.environ.get('ART_FORCE_TRY_PREDICATED_SIMD') == 'true'
142   //
143   //      HalvingAdd idiom is not supported for SVE.
144   ///     CHECK-NOT: VecHalvingAdd
145   //
146   /// CHECK-ELSE:
147   //
148   ///     CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
149   ///     CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
150   ///     CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:true loop:<<Loop>> outer_loop:none
151   ///     CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
152   //
153   /// CHECK-FI:
rounding_halving_add_unsigned(char[] b1, char[] b2, char[] bo)154   private static void rounding_halving_add_unsigned(char[] b1, char[] b2, char[] bo) {
155     int min_length = Math.min(bo.length, Math.min(b1.length, b2.length));
156     for (int i = 0; i < min_length; i++) {
157       bo[i] = (char) ((b1[i] + b2[i] + 1) >>> 1);
158     }
159   }
160 
161   /// CHECK-START: void HaddAltChar.rounding_halving_add_also_unsigned(char[], char[], char[]) instruction_simplifier (before)
162   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
163   /// CHECK-DAG: <<IMAX:i\d+>> IntConstant 65535                   loop:none
164   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
165   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
166   /// CHECK-DAG: <<Get2:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
167   /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<IMAX>>]             loop:<<Loop>>      outer_loop:none
168   /// CHECK-DAG: <<And2:i\d+>> And [<<IMAX>>,<<Get2>>]             loop:<<Loop>>      outer_loop:none
169   /// CHECK-DAG: <<Add1:i\d+>> Add [<<And1>>,<<And2>>]             loop:<<Loop>>      outer_loop:none
170   /// CHECK-DAG: <<Add2:i\d+>> Add [<<Add1>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
171   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add2>>,<<I1>>]              loop:<<Loop>>      outer_loop:none
172   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<UShr>>]           loop:<<Loop>>      outer_loop:none
173   /// CHECK-DAG:               ArraySet [{{l\d+}},{{i\d+}},<<Cnv>>] loop:<<Loop>>      outer_loop:none
174   //
175   /// CHECK-START: void HaddAltChar.rounding_halving_add_also_unsigned(char[], char[], char[]) loop_optimization (before)
176   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
177   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
178   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
179   /// CHECK-DAG: <<Get2:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
180   /// CHECK-DAG: <<Add1:i\d+>> Add [<<Get1>>,<<Get2>>]             loop:<<Loop>>      outer_loop:none
181   /// CHECK-DAG: <<Add2:i\d+>> Add [<<Add1>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
182   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add2>>,<<I1>>]              loop:<<Loop>>      outer_loop:none
183   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<UShr>>]           loop:<<Loop>>      outer_loop:none
184   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
185   //
186   /// CHECK-START-ARM: void HaddAltChar.rounding_halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after)
187   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
188   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
189   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:true loop:<<Loop>> outer_loop:none
190   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
191   //
192   //
193   /// CHECK-START-ARM64: void HaddAltChar.rounding_halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after)
194   /// CHECK-IF:     hasIsaFeature("sve") and os.environ.get('ART_FORCE_TRY_PREDICATED_SIMD') == 'true'
195   //
196   //      HalvingAdd idiom is not supported for SVE.
197   ///     CHECK-NOT: VecHalvingAdd
198   //
199   /// CHECK-ELSE:
200   //
201   ///     CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
202   ///     CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
203   ///     CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:true loop:<<Loop>> outer_loop:none
204   ///     CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
205   //
206   /// CHECK-FI:
207   //
208   // Note: HAnd has no impact (already a zero extension).
209   //
rounding_halving_add_also_unsigned(char[] b1, char[] b2, char[] bo)210   private static void rounding_halving_add_also_unsigned(char[] b1, char[] b2, char[] bo) {
211     int min_length = Math.min(bo.length, Math.min(b1.length, b2.length));
212     for (int i = 0; i < min_length; i++) {
213       bo[i] = (char) (((b1[i] & 0xffff) + (b2[i] & 0xffff) + 1) >>> 1);
214     }
215   }
216 
217   /// CHECK-START: void HaddAltChar.halving_add_unsigned_constant(char[], char[]) loop_optimization (before)
218   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
219   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                   loop:none
220   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
221   /// CHECK-DAG: <<Get:c\d+>>  ArrayGet                            loop:<<Loop>>      outer_loop:none
222   /// CHECK-DAG: <<Add:i\d+>>  Add [<<Get>>,<<UMAX>>]              loop:<<Loop>>      outer_loop:none
223   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
224   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<UShr>>]           loop:<<Loop>>      outer_loop:none
225   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
226   //
227   /// CHECK-START-ARM: void HaddAltChar.halving_add_unsigned_constant(char[], char[]) loop_optimization (after)
228   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                     loop:none
229   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>]         loop:none
230   /// CHECK-DAG: <<Get:d\d+>>  VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
231   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get>>,<<Repl>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
232   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
233   //
234   /// CHECK-START-ARM64: void HaddAltChar.halving_add_unsigned_constant(char[], char[]) loop_optimization (after)
235   /// CHECK-IF:     hasIsaFeature("sve") and os.environ.get('ART_FORCE_TRY_PREDICATED_SIMD') == 'true'
236   //
237   //      HalvingAdd idiom is not supported for SVE.
238   ///     CHECK-NOT: VecHalvingAdd
239   //
240   /// CHECK-ELSE:
241   //
242   ///     CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                     loop:none
243   ///     CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>]         loop:none
244   ///     CHECK-DAG: <<Get:d\d+>>  VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
245   ///     CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get>>,<<Repl>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
246   ///     CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
247   //
248   /// CHECK-FI:
halving_add_unsigned_constant(char[] b1, char[] bo)249   private static void halving_add_unsigned_constant(char[] b1, char[] bo) {
250     int min_length = Math.min(bo.length, b1.length);
251     for (int i = 0; i < min_length; i++) {
252       bo[i] = (char) ((b1[i] + 0xffff) >>> 1);
253     }
254   }
255 
256   /// CHECK-START: void HaddAltChar.halving_add_also_unsigned_constant(char[], char[]) instruction_simplifier (before)
257   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
258   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                   loop:none
259   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
260   /// CHECK-DAG: <<Get:c\d+>>  ArrayGet                            loop:<<Loop>>      outer_loop:none
261   /// CHECK-DAG: <<And:i\d+>>  And [<<Get>>,<<UMAX>>]              loop:<<Loop>>      outer_loop:none
262   /// CHECK-DAG: <<Add:i\d+>>  Add [<<And>>,<<UMAX>>]              loop:<<Loop>>      outer_loop:none
263   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
264   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<UShr>>]           loop:<<Loop>>      outer_loop:none
265   /// CHECK-DAG:               ArraySet [{{l\d+}},{{i\d+}},<<Cnv>>] loop:<<Loop>>      outer_loop:none
266   //
267   /// CHECK-START: void HaddAltChar.halving_add_also_unsigned_constant(char[], char[]) loop_optimization (before)
268   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
269   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                   loop:none
270   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
271   /// CHECK-DAG: <<Get:c\d+>>  ArrayGet                            loop:<<Loop>>      outer_loop:none
272   /// CHECK-DAG: <<Add:i\d+>>  Add [<<Get>>,<<UMAX>>]              loop:<<Loop>>      outer_loop:none
273   /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Add>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
274   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<UShr>>]           loop:<<Loop>>      outer_loop:none
275   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
276   //
277   /// CHECK-START-ARM: void HaddAltChar.halving_add_also_unsigned_constant(char[], char[]) loop_optimization (after)
278   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                     loop:none
279   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>]         loop:none
280   /// CHECK-DAG: <<Get:d\d+>>  VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
281   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get>>,<<Repl>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
282   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
283   //
284   /// CHECK-START-ARM64: void HaddAltChar.halving_add_also_unsigned_constant(char[], char[]) loop_optimization (after)
285   /// CHECK-IF:     hasIsaFeature("sve") and os.environ.get('ART_FORCE_TRY_PREDICATED_SIMD') == 'true'
286   //
287   //      HalvingAdd idiom is not supported for SVE.
288   ///     CHECK-NOT: VecHalvingAdd
289   //
290   /// CHECK-ELSE:
291   //
292   ///     CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                     loop:none
293   ///     CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>]         loop:none
294   ///     CHECK-DAG: <<Get:d\d+>>  VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
295   ///     CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get>>,<<Repl>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
296   ///     CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
297   //
298   /// CHECK-FI:
299   //
300   // Note: HAnd has no impact (already a zero extension).
301   //
halving_add_also_unsigned_constant(char[] b1, char[] bo)302   private static void halving_add_also_unsigned_constant(char[] b1, char[] bo) {
303     int min_length = Math.min(bo.length, b1.length);
304     for (int i = 0; i < min_length; i++) {
305       bo[i] = (char) (((b1[i] & 0xffff) + 0xffff) >>> 1);
306     }
307   }
308 
main()309   public static void main() {
310     // Some interesting values.
311     char[] interesting = {
312       (char) 0x0000,
313       (char) 0x0001,
314       (char) 0x0002,
315       (char) 0x1234,
316       (char) 0x8000,
317       (char) 0x8001,
318       (char) 0x7fff,
319       (char) 0xffff
320     };
321     // Initialize cross-values to test all cases, and also
322     // set up some extra values to exercise the cleanup loop.
323     for (int i = 0; i < M; i++) {
324       sB1[i] = (char) i;
325       sB2[i] = interesting[i & 7];
326     }
327 
328     // Test halving add idioms. Note that the expected result is computed
329     // with the arithmetic >> to demonstrate the computed narrower result
330     // does not depend on the wider >> or >>>.
331     halving_add_unsigned(sB1, sB2, sBo);
332     for (int i = 0; i < M; i++) {
333       char e = (char) ((sB1[i] + sB2[i]) >> 1);
334       expectEquals(e, sBo[i]);
335     }
336     halving_add_also_unsigned(sB1, sB2, sBo);
337     for (int i = 0; i < M; i++) {
338       char e = (char) ((sB1[i] + sB2[i]) >> 1);
339       expectEquals(e, sBo[i]);
340     }
341     rounding_halving_add_unsigned(sB1, sB2, sBo);
342     for (int i = 0; i < M; i++) {
343       char e = (char) ((sB1[i] + sB2[i] + 1) >> 1);
344       expectEquals(e, sBo[i]);
345     }
346     rounding_halving_add_also_unsigned(sB1, sB2, sBo);
347     for (int i = 0; i < M; i++) {
348       char e = (char) ((sB1[i] + sB2[i] + 1) >> 1);
349       expectEquals(e, sBo[i]);
350     }
351     halving_add_unsigned_constant(sB1, sBo);
352     for (int i = 0; i < M; i++) {
353       char e = (char) ((sB1[i] + 0xffff) >> 1);
354       expectEquals(e, sBo[i]);
355     }
356     halving_add_also_unsigned_constant(sB1, sBo);
357     for (int i = 0; i < M; i++) {
358       char e = (char) ((sB1[i] + 0xffff) >> 1);
359       expectEquals(e, sBo[i]);
360     }
361 
362     System.out.println("HaddAltChar passed");
363   }
364 
expectEquals(int expected, int result)365   private static void expectEquals(int expected, int result) {
366     if (expected != result) {
367       throw new Error("Expected: " + expected + ", found: " + result);
368     }
369   }
370 }
371