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 HaddChar {
21 
22   private static final int N = 64 * 1024;
23   private static final int M = N + 31;
24 
25   static char[] sB1 = new char[M];
26   static char[] sB2 = new char[M];
27   static char[] sBo = new char[M];
28 
29   /// CHECK-START: void HaddChar.halving_add_unsigned(char[], char[], char[]) 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:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
33   /// CHECK-DAG: <<Get2:c\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:c\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: void HaddChar.halving_add_unsigned(char[], char[], char[]) 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:Uint16 rounded:false loop:<<Loop>> outer_loop:none
43   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
44   //
45   /// CHECK-START-ARM64: void HaddChar.halving_add_unsigned(char[], char[], char[]) loop_optimization (after)
46   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
47   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
48   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
49   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
halving_add_unsigned(char[] b1, char[] b2, char[] bo)50   private static void halving_add_unsigned(char[] b1, char[] b2, char[] bo) {
51     int min_length = Math.min(bo.length, Math.min(b1.length, b2.length));
52     for (int i = 0; i < min_length; i++) {
53       bo[i] = (char) ((b1[i] + b2[i]) >> 1);
54     }
55   }
56 
57   /// CHECK-START: void HaddChar.halving_add_also_unsigned(char[], char[], char[]) instruction_simplifier (before)
58   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
59   /// CHECK-DAG: <<IMAX:i\d+>> IntConstant 65535                   loop:none
60   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
61   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
62   /// CHECK-DAG: <<Get2:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
63   /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<IMAX>>]             loop:<<Loop>>      outer_loop:none
64   /// CHECK-DAG: <<And2:i\d+>> And [<<IMAX>>,<<Get2>>]             loop:<<Loop>>      outer_loop:none
65   /// CHECK-DAG: <<Add:i\d+>>  Add [<<And1>>,<<And2>>]             loop:<<Loop>>      outer_loop:none
66   /// CHECK-DAG: <<Shr:i\d+>>  Shr [<<Add>>,<<I1>>]                loop:<<Loop>>      outer_loop:none
67   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<Shr>>]            loop:<<Loop>>      outer_loop:none
68   /// CHECK-DAG:               ArraySet [{{l\d+}},{{i\d+}},<<Cnv>>] loop:<<Loop>>      outer_loop:none
69   //
70   /// CHECK-START: void HaddChar.halving_add_also_unsigned(char[], char[], char[]) loop_optimization (before)
71   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       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: <<Add:i\d+>>  Add [<<Get1>>,<<Get2>>]             loop:<<Loop>>      outer_loop:none
76   /// CHECK-DAG: <<Shr:i\d+>>  Shr [<<Add>>,<<I1>>]                loop:<<Loop>>      outer_loop:none
77   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<Shr>>]            loop:<<Loop>>      outer_loop:none
78   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
79   //
80   /// CHECK-START-ARM: void HaddChar.halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after)
81   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
82   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
83   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
84   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
85   //
86   /// CHECK-START-ARM64: void HaddChar.halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after)
87   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
88   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
89   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
90   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
91   //
92   // Note: HAnd has no impact (already a zero extension).
93   //
halving_add_also_unsigned(char[] b1, char[] b2, char[] bo)94   private static void halving_add_also_unsigned(char[] b1, char[] b2, char[] bo) {
95     int min_length = Math.min(bo.length, Math.min(b1.length, b2.length));
96     for (int i = 0; i < min_length; i++) {
97       bo[i] = (char) (((b1[i] & 0xffff) + (b2[i] & 0xffff)) >> 1);
98     }
99   }
100 
101   /// CHECK-START: void HaddChar.rounding_halving_add_unsigned(char[], char[], char[]) loop_optimization (before)
102   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
103   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
104   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
105   /// CHECK-DAG: <<Get2:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
106   /// CHECK-DAG: <<Add1:i\d+>> Add [<<Get1>>,<<Get2>>]             loop:<<Loop>>      outer_loop:none
107   /// CHECK-DAG: <<Add2:i\d+>> Add [<<Add1>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
108   /// CHECK-DAG: <<Shr:i\d+>>  Shr [<<Add2>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
109   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<Shr>>]            loop:<<Loop>>      outer_loop:none
110   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
111   //
112   /// CHECK-START-ARM: void HaddChar.rounding_halving_add_unsigned(char[], char[], char[]) loop_optimization (after)
113   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
114   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
115   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:true loop:<<Loop>> outer_loop:none
116   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
117   //
118   /// CHECK-START-ARM64: void HaddChar.rounding_halving_add_unsigned(char[], char[], char[]) loop_optimization (after)
119   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
120   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
121   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:true loop:<<Loop>> outer_loop:none
122   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
rounding_halving_add_unsigned(char[] b1, char[] b2, char[] bo)123   private static void rounding_halving_add_unsigned(char[] b1, char[] b2, char[] bo) {
124     int min_length = Math.min(bo.length, Math.min(b1.length, b2.length));
125     for (int i = 0; i < min_length; i++) {
126       bo[i] = (char) ((b1[i] + b2[i] + 1) >> 1);
127     }
128   }
129 
130   /// CHECK-START: void HaddChar.rounding_halving_add_also_unsigned(char[], char[], char[]) instruction_simplifier (before)
131   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
132   /// CHECK-DAG: <<IMAX:i\d+>> IntConstant 65535                   loop:none
133   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
134   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
135   /// CHECK-DAG: <<Get2:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
136   /// CHECK-DAG: <<And1:i\d+>> And [<<Get1>>,<<IMAX>>]             loop:<<Loop>>      outer_loop:none
137   /// CHECK-DAG: <<And2:i\d+>> And [<<IMAX>>,<<Get2>>]             loop:<<Loop>>      outer_loop:none
138   /// CHECK-DAG: <<Add1:i\d+>> Add [<<And1>>,<<And2>>]             loop:<<Loop>>      outer_loop:none
139   /// CHECK-DAG: <<Add2:i\d+>> Add [<<Add1>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
140   /// CHECK-DAG: <<Shr:i\d+>>  Shr [<<Add2>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
141   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<Shr>>]            loop:<<Loop>>      outer_loop:none
142   /// CHECK-DAG:               ArraySet [{{l\d+}},{{i\d+}},<<Cnv>>] loop:<<Loop>>      outer_loop:none
143   //
144   /// CHECK-START: void HaddChar.rounding_halving_add_also_unsigned(char[], char[], char[]) loop_optimization (before)
145   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
146   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
147   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
148   /// CHECK-DAG: <<Get2:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
149   /// CHECK-DAG: <<Add1:i\d+>> Add [<<Get1>>,<<Get2>>]             loop:<<Loop>>      outer_loop:none
150   /// CHECK-DAG: <<Add2:i\d+>> Add [<<Add1>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
151   /// CHECK-DAG: <<Shr:i\d+>>  Shr [<<Add2>>,<<I1>>]               loop:<<Loop>>      outer_loop:none
152   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<Shr>>]            loop:<<Loop>>      outer_loop:none
153   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
154   //
155   /// CHECK-START-ARM: void HaddChar.rounding_halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after)
156   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
157   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
158   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:true loop:<<Loop>> outer_loop:none
159   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
160   //
161   /// CHECK-START-ARM64: void HaddChar.rounding_halving_add_also_unsigned(char[], char[], char[]) loop_optimization (after)
162   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
163   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                               loop:<<Loop>>      outer_loop:none
164   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get1>>,<<Get2>>] packed_type:Uint16 rounded:true loop:<<Loop>> outer_loop:none
165   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
166   //
167   // Note: HAnd has no impact (already a zero extension).
168   //
rounding_halving_add_also_unsigned(char[] b1, char[] b2, char[] bo)169   private static void rounding_halving_add_also_unsigned(char[] b1, char[] b2, char[] bo) {
170     int min_length = Math.min(bo.length, Math.min(b1.length, b2.length));
171     for (int i = 0; i < min_length; i++) {
172       bo[i] = (char) (((b1[i] & 0xffff) + (b2[i] & 0xffff) + 1) >> 1);
173     }
174   }
175 
176   /// CHECK-START: void HaddChar.halving_add_unsigned_constant(char[], char[]) loop_optimization (before)
177   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
178   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                   loop:none
179   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
180   /// CHECK-DAG: <<Get:c\d+>>  ArrayGet                            loop:<<Loop>>      outer_loop:none
181   /// CHECK-DAG: <<Add:i\d+>>  Add [<<Get>>,<<UMAX>>]              loop:<<Loop>>      outer_loop:none
182   /// CHECK-DAG: <<Shr:i\d+>>  Shr [<<Add>>,<<I1>>]                loop:<<Loop>>      outer_loop:none
183   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<Shr>>]            loop:<<Loop>>      outer_loop:none
184   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
185   //
186   /// CHECK-START-ARM: void HaddChar.halving_add_unsigned_constant(char[], char[]) loop_optimization (after)
187   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                     loop:none
188   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>]         loop:none
189   /// CHECK-DAG: <<Get:d\d+>>  VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
190   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get>>,<<Repl>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
191   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
192   //
193   /// CHECK-START-ARM64: void HaddChar.halving_add_unsigned_constant(char[], char[]) loop_optimization (after)
194   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                     loop:none
195   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>]         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:Uint16 rounded:false loop:<<Loop>> outer_loop:none
halving_add_unsigned_constant(char[] b1, char[] bo)198   private static void halving_add_unsigned_constant(char[] b1, char[] bo) {
199     int min_length = Math.min(bo.length, b1.length);
200     for (int i = 0; i < min_length; i++) {
201       bo[i] = (char) ((b1[i] + 0xffff) >> 1);
202     }
203   }
204 
205   /// CHECK-START: void HaddChar.halving_add_also_unsigned_constant(char[], char[]) instruction_simplifier (before)
206   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
207   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                   loop:none
208   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
209   /// CHECK-DAG: <<Get:c\d+>>  ArrayGet                            loop:<<Loop>>      outer_loop:none
210   /// CHECK-DAG: <<And:i\d+>>  And [<<Get>>,<<UMAX>>]              loop:<<Loop>>      outer_loop:none
211   /// CHECK-DAG: <<Add:i\d+>>  Add [<<And>>,<<UMAX>>]              loop:<<Loop>>      outer_loop:none
212   /// CHECK-DAG: <<Shr:i\d+>>  Shr [<<Add>>,<<I1>>]                loop:<<Loop>>      outer_loop:none
213   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<Shr>>]            loop:<<Loop>>      outer_loop:none
214   /// CHECK-DAG:               ArraySet [{{l\d+}},{{i\d+}},<<Cnv>>] loop:<<Loop>>      outer_loop:none
215   //
216   /// CHECK-START: void HaddChar.halving_add_also_unsigned_constant(char[], char[]) loop_optimization (before)
217   /// CHECK-DAG: <<I1:i\d+>>   IntConstant 1                       loop:none
218   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                   loop:none
219   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
220   /// CHECK-DAG: <<Get:c\d+>>  ArrayGet                            loop:<<Loop>>      outer_loop:none
221   /// CHECK-DAG: <<Add:i\d+>>  Add [<<Get>>,<<UMAX>>]              loop:<<Loop>>      outer_loop:none
222   /// CHECK-DAG: <<Shr:i\d+>>  Shr [<<Add>>,<<I1>>]                loop:<<Loop>>      outer_loop:none
223   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<Shr>>]            loop:<<Loop>>      outer_loop:none
224   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
225   //
226   /// CHECK-START-ARM: void HaddChar.halving_add_also_unsigned_constant(char[], char[]) loop_optimization (after)
227   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                     loop:none
228   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>]         loop:none
229   /// CHECK-DAG: <<Get:d\d+>>  VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
230   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get>>,<<Repl>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
231   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
232   //
233   /// CHECK-START-ARM64: void HaddChar.halving_add_also_unsigned_constant(char[], char[]) loop_optimization (after)
234   /// CHECK-DAG: <<UMAX:i\d+>> IntConstant 65535                     loop:none
235   /// CHECK-DAG: <<Repl:d\d+>> VecReplicateScalar [<<UMAX>>]         loop:none
236   /// CHECK-DAG: <<Get:d\d+>>  VecLoad                               loop:<<Loop:B\d+>> outer_loop:none
237   /// CHECK-DAG: <<HAdd:d\d+>> VecHalvingAdd [<<Get>>,<<Repl>>] packed_type:Uint16 rounded:false loop:<<Loop>> outer_loop:none
238   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<HAdd>>] loop:<<Loop>>      outer_loop:none
239   //
240   // Note: HAnd has no impact (already a zero extension).
241   //
halving_add_also_unsigned_constant(char[] b1, char[] bo)242   private static void halving_add_also_unsigned_constant(char[] b1, char[] bo) {
243     int min_length = Math.min(bo.length, b1.length);
244     for (int i = 0; i < min_length; i++) {
245       bo[i] = (char) (((b1[i] & 0xffff) + 0xffff) >> 1);
246     }
247   }
248 
main()249   public static void main() {
250     // Some interesting values.
251     char[] interesting = {
252       (char) 0x0000,
253       (char) 0x0001,
254       (char) 0x0002,
255       (char) 0x1234,
256       (char) 0x8000,
257       (char) 0x8001,
258       (char) 0x7fff,
259       (char) 0xffff
260     };
261     // Initialize cross-values to test all cases, and also
262     // set up some extra values to exercise the cleanup loop.
263     for (int i = 0; i < M; i++) {
264       sB1[i] = (char) i;
265       sB2[i] = interesting[i & 7];
266     }
267 
268     // Test halving add idioms.
269     halving_add_unsigned(sB1, sB2, sBo);
270     for (int i = 0; i < M; i++) {
271       char e = (char) ((sB1[i] + sB2[i]) >> 1);
272       expectEquals(e, sBo[i]);
273     }
274     halving_add_also_unsigned(sB1, sB2, sBo);
275     for (int i = 0; i < M; i++) {
276       char e = (char) ((sB1[i] + sB2[i]) >> 1);
277       expectEquals(e, sBo[i]);
278     }
279     rounding_halving_add_unsigned(sB1, sB2, sBo);
280     for (int i = 0; i < M; i++) {
281       char e = (char) ((sB1[i] + sB2[i] + 1) >> 1);
282       expectEquals(e, sBo[i]);
283     }
284     rounding_halving_add_also_unsigned(sB1, sB2, sBo);
285     for (int i = 0; i < M; i++) {
286       char e = (char) ((sB1[i] + sB2[i] + 1) >> 1);
287       expectEquals(e, sBo[i]);
288     }
289     halving_add_unsigned_constant(sB1, sBo);
290     for (int i = 0; i < M; i++) {
291       char e = (char) ((sB1[i] + 0xffff) >> 1);
292       expectEquals(e, sBo[i]);
293     }
294     halving_add_also_unsigned_constant(sB1, sBo);
295     for (int i = 0; i < M; i++) {
296       char e = (char) ((sB1[i] + 0xffff) >> 1);
297       expectEquals(e, sBo[i]);
298     }
299 
300     System.out.println("HaddChar passed");
301   }
302 
expectEquals(int expected, int result)303   private static void expectEquals(int expected, int result) {
304     if (expected != result) {
305       throw new Error("Expected: " + expected + ", found: " + result);
306     }
307   }
308 }
309