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