• 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 SAD (sum of absolute differences).
19  *
20  * Special case, char array that is first casted to short, forcing sign extension.
21  */
22 public class SimdSadShort2 {
23 
24   // TODO: lower precision still coming, b/64091002
25 
sadCastedChar2Short(char[] s1, char[] s2)26   private static short sadCastedChar2Short(char[] s1, char[] s2) {
27     int min_length = Math.min(s1.length, s2.length);
28     short sad = 0;
29     for (int i = 0; i < min_length; i++) {
30       sad += Math.abs(((short) s1[i]) - ((short) s2[i]));
31     }
32     return sad;
33   }
34 
sadCastedChar2ShortAlt(char[] s1, char[] s2)35   private static short sadCastedChar2ShortAlt(char[] s1, char[] s2) {
36     int min_length = Math.min(s1.length, s2.length);
37     short sad = 0;
38     for (int i = 0; i < min_length; i++) {
39       short s = (short) s1[i];
40       short p = (short) s2[i];
41       sad += s >= p ? s - p : p - s;
42     }
43     return sad;
44   }
45 
sadCastedChar2ShortAlt2(char[] s1, char[] s2)46   private static short sadCastedChar2ShortAlt2(char[] s1, char[] s2) {
47     int min_length = Math.min(s1.length, s2.length);
48     short sad = 0;
49     for (int i = 0; i < min_length; i++) {
50       short s = (short) s1[i];
51       short p = (short) s2[i];
52       int x = s - p;
53       if (x < 0) x = -x;
54       sad += x;
55     }
56     return sad;
57   }
58 
59   /// CHECK-START: int SimdSadShort2.sadCastedChar2Int(char[], char[]) instruction_simplifier (before)
60   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
61   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
62   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
63   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
64   /// CHECK-DAG: <<BC1:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>     outer_loop:none
65   /// CHECK-DAG: <<BC2:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>     outer_loop:none
66   /// CHECK-DAG: <<Get1:c\d+>>   ArrayGet [{{l\d+}},<<BC1>>]    loop:<<Loop>>      outer_loop:none
67   /// CHECK-DAG: <<Get2:c\d+>>   ArrayGet [{{l\d+}},<<BC2>>]    loop:<<Loop>>      outer_loop:none
68   /// CHECK-DAG: <<Cnv1:s\d+>>   TypeConversion [<<Get1>>]      loop:<<Loop>>      outer_loop:none
69   /// CHECK-DAG: <<Cnv2:s\d+>>   TypeConversion [<<Get2>>]      loop:<<Loop>>      outer_loop:none
70   /// CHECK-DAG: <<Sub:i\d+>>    Sub [<<Cnv1>>,<<Cnv2>>]        loop:<<Loop>>      outer_loop:none
71   /// CHECK-DAG: <<Abs:i\d+>>    Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
72   /// CHECK-DAG:                 Add [<<Phi2>>,<<Abs>>]         loop:<<Loop>>      outer_loop:none
73   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
74   //
75   /// CHECK-START: int SimdSadShort2.sadCastedChar2Int(char[], char[]) loop_optimization (before)
76   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
77   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
78   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
79   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
80   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
81   /// CHECK-DAG: <<Get2:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
82   /// CHECK-DAG: <<Sub:i\d+>>    Sub [<<Get1>>,<<Get2>>]        loop:<<Loop>>      outer_loop:none
83   /// CHECK-DAG: <<Abs:i\d+>>    Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
84   /// CHECK-DAG:                 Add [<<Phi2>>,<<Abs>>]         loop:<<Loop>>      outer_loop:none
85   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
86   //
87   /// CHECK-START-ARM64: int SimdSadShort2.sadCastedChar2Int(char[], char[]) loop_optimization (after)
88   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
89   /// CHECK-IF:     hasIsaFeature("sve")
90   //
91   //      SAD idiom is not supported for SVE.
92   ///     CHECK-NOT: VecSADAccumulate
93   //
94   /// CHECK-ELSE:
95   //
96   ///     CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
97   ///     CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<Cons0>>]      loop:none
98   ///     CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
99   ///     CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
100   ///     CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
101   ///     CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
102   ///     CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Load2>>] loop:<<Loop>> outer_loop:none
103   ///     CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
104   //
105   /// CHECK-FI:
sadCastedChar2Int(char[] s1, char[] s2)106   private static int sadCastedChar2Int(char[] s1, char[] s2) {
107     int min_length = Math.min(s1.length, s2.length);
108     int sad = 0;
109     for (int i = 0; i < min_length; i++) {
110       sad += Math.abs(((short) s1[i]) - ((short) s2[i]));
111     }
112     return sad;
113   }
114 
115   /// CHECK-START: int SimdSadShort2.sadCastedChar2IntAlt(char[], char[]) instruction_simplifier (before)
116   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
117   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
118   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
119   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
120   /// CHECK-DAG: <<BC1:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>      outer_loop:none
121   /// CHECK-DAG: <<BC2:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>      outer_loop:none
122   /// CHECK-DAG: <<Get1:c\d+>>   ArrayGet [{{l\d+}},<<BC1>>]    loop:<<Loop>>      outer_loop:none
123   /// CHECK-DAG: <<Get2:c\d+>>   ArrayGet [{{l\d+}},<<BC2>>]    loop:<<Loop>>      outer_loop:none
124   /// CHECK-DAG: <<Cnv1:s\d+>>   TypeConversion [<<Get1>>]      loop:<<Loop>>      outer_loop:none
125   /// CHECK-DAG: <<Cnv2:s\d+>>   TypeConversion [<<Get2>>]      loop:<<Loop>>      outer_loop:none
126   /// CHECK-DAG: <<Sub1:i\d+>>   Sub [<<Cnv2>>,<<Cnv1>>]        loop:<<Loop>>      outer_loop:none
127   /// CHECK-DAG: <<Sub2:i\d+>>   Sub [<<Cnv1>>,<<Cnv2>>]        loop:<<Loop>>      outer_loop:none
128   /// CHECK-DAG: <<Phi3:i\d+>>   Phi [<<Sub2>>,<<Sub1>>]        loop:<<Loop>>      outer_loop:none
129   /// CHECK-DAG:                 Add [<<Phi2>>,<<Phi3>>]        loop:<<Loop>>      outer_loop:none
130   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
131   //
132   /// CHECK-START: int SimdSadShort2.sadCastedChar2IntAlt(char[], char[]) loop_optimization (before)
133   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
134   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
135   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
136   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
137   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
138   /// CHECK-DAG: <<Get2:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
139   /// CHECK-DAG: <<Sub:i\d+>>    Sub [<<Get2>>,<<Get1>>]        loop:<<Loop>>      outer_loop:none
140   /// CHECK-DAG: <<Intrin:i\d+>> Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
141   /// CHECK-DAG:                 Add [<<Phi2>>,<<Intrin>>]      loop:<<Loop>>      outer_loop:none
142   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
143   //
144   /// CHECK-START-ARM64: int SimdSadShort2.sadCastedChar2IntAlt(char[], char[]) loop_optimization (after)
145   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
146   /// CHECK-IF:     hasIsaFeature("sve")
147   //
148   //      SAD idiom is not supported for SVE.
149   ///     CHECK-NOT: VecSADAccumulate
150   //
151   /// CHECK-ELSE:
152   //
153   ///     CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
154   ///     CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<Cons0>>]      loop:none
155   ///     CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
156   ///     CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
157   ///     CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
158   ///     CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
159   ///     CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load2>>,<<Load1>>] loop:<<Loop>> outer_loop:none
160   ///     CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
161   //
162   /// CHECK-FI:
sadCastedChar2IntAlt(char[] s1, char[] s2)163   private static int sadCastedChar2IntAlt(char[] s1, char[] s2) {
164     int min_length = Math.min(s1.length, s2.length);
165     int sad = 0;
166     for (int i = 0; i < min_length; i++) {
167       short s = (short) s1[i];
168       short p = (short) s2[i];
169       sad += s >= p ? s - p : p - s;
170     }
171     return sad;
172   }
173 
174   /// CHECK-START: int SimdSadShort2.sadCastedChar2IntAlt2(char[], char[]) instruction_simplifier (before)
175   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
176   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
177   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
178   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
179   /// CHECK-DAG: <<BC1:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>      outer_loop:none
180   /// CHECK-DAG: <<BC2:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>      outer_loop:none
181   /// CHECK-DAG: <<Get1:c\d+>>   ArrayGet [{{l\d+}},<<BC1>>]    loop:<<Loop>>      outer_loop:none
182   /// CHECK-DAG: <<Get2:c\d+>>   ArrayGet [{{l\d+}},<<BC2>>]    loop:<<Loop>>      outer_loop:none
183   /// CHECK-DAG: <<Cnv1:s\d+>>   TypeConversion [<<Get1>>]      loop:<<Loop>>      outer_loop:none
184   /// CHECK-DAG: <<Cnv2:s\d+>>   TypeConversion [<<Get2>>]      loop:<<Loop>>      outer_loop:none
185   /// CHECK-DAG: <<Sub:i\d+>>    Sub [<<Cnv1>>,<<Cnv2>>]        loop:<<Loop>>      outer_loop:none
186   /// CHECK-DAG: <<Neg:i\d+>>    Neg [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
187   /// CHECK-DAG: <<Phi3:i\d+>>   Phi [<<Sub>>,<<Neg>>]          loop:<<Loop>>      outer_loop:none
188   /// CHECK-DAG:                 Add [<<Phi2>>,<<Phi3>>]        loop:<<Loop>>      outer_loop:none
189   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
190   //
191   /// CHECK-START: int SimdSadShort2.sadCastedChar2IntAlt2(char[], char[]) loop_optimization (before)
192   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
193   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
194   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
195   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
196   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
197   /// CHECK-DAG: <<Get2:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
198   /// CHECK-DAG: <<Sub:i\d+>>    Sub [<<Get1>>,<<Get2>>]        loop:<<Loop>>      outer_loop:none
199   /// CHECK-DAG: <<Intrin:i\d+>> Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
200   /// CHECK-DAG:                 Add [<<Phi2>>,<<Intrin>>]      loop:<<Loop>>      outer_loop:none
201   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
202   //
203   /// CHECK-START-ARM64: int SimdSadShort2.sadCastedChar2IntAlt2(char[], char[]) loop_optimization (after)
204   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
205   /// CHECK-IF:     hasIsaFeature("sve")
206   //
207   //      SAD idiom is not supported for SVE.
208   ///     CHECK-NOT: VecSADAccumulate
209   //
210   /// CHECK-ELSE:
211   //
212   ///     CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
213   ///     CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<Cons0>>]      loop:none
214   ///     CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
215   ///     CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
216   ///     CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
217   ///     CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
218   ///     CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Load2>>] loop:<<Loop>> outer_loop:none
219   ///     CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
220   //
221   /// CHECK-FI:
sadCastedChar2IntAlt2(char[] s1, char[] s2)222   private static int sadCastedChar2IntAlt2(char[] s1, char[] s2) {
223     int min_length = Math.min(s1.length, s2.length);
224     int sad = 0;
225     for (int i = 0; i < min_length; i++) {
226       short s = (short) s1[i];
227       short p = (short) s2[i];
228       int x = s - p;
229       if (x < 0) x = -x;
230       sad += x;
231     }
232     return sad;
233   }
234 
235   /// CHECK-START: long SimdSadShort2.sadCastedChar2Long(char[], char[]) instruction_simplifier (before)
236   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
237   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
238   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 0                 loop:none
239   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
240   /// CHECK-DAG: <<Phi2:j\d+>>   Phi [<<ConsL>>,{{j\d+}}]       loop:<<Loop>>      outer_loop:none
241   /// CHECK-DAG: <<BC1:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>     outer_loop:none
242   /// CHECK-DAG: <<BC2:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>     outer_loop:none
243   /// CHECK-DAG: <<Get1:c\d+>>   ArrayGet [{{l\d+}},<<BC1>>]    loop:<<Loop>>      outer_loop:none
244   /// CHECK-DAG: <<Get2:c\d+>>   ArrayGet [{{l\d+}},<<BC2>>]    loop:<<Loop>>      outer_loop:none
245   /// CHECK-DAG: <<Cnv1:s\d+>>   TypeConversion [<<Get1>>]      loop:<<Loop>>      outer_loop:none
246   /// CHECK-DAG: <<Cnv2:s\d+>>   TypeConversion [<<Get2>>]      loop:<<Loop>>      outer_loop:none
247   /// CHECK-DAG: <<Cnv3:j\d+>>   TypeConversion [<<Cnv1>>]      loop:<<Loop>>      outer_loop:none
248   /// CHECK-DAG: <<Cnv4:j\d+>>   TypeConversion [<<Cnv2>>]      loop:<<Loop>>      outer_loop:none
249   /// CHECK-DAG: <<Sub:j\d+>>    Sub [<<Cnv3>>,<<Cnv4>>]        loop:<<Loop>>      outer_loop:none
250   /// CHECK-DAG: <<Abs:j\d+>>    Abs [<<Sub>>]                  loop:<<Loop>> outer_loop:none
251   /// CHECK-DAG:                 Add [<<Phi2>>,<<Abs>>]         loop:<<Loop>>      outer_loop:none
252   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
253   //
254   /// CHECK-START: long SimdSadShort2.sadCastedChar2Long(char[], char[]) loop_optimization (before)
255   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
256   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
257   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 0                 loop:none
258   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
259   /// CHECK-DAG: <<Phi2:j\d+>>   Phi [<<ConsL>>,{{j\d+}}]       loop:<<Loop>>      outer_loop:none
260   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
261   /// CHECK-DAG: <<Get2:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
262   /// CHECK-DAG: <<Cnv1:j\d+>>   TypeConversion [<<Get1>>]      loop:<<Loop>>      outer_loop:none
263   /// CHECK-DAG: <<Cnv2:j\d+>>   TypeConversion [<<Get2>>]      loop:<<Loop>>      outer_loop:none
264   /// CHECK-DAG: <<Sub:j\d+>>    Sub [<<Cnv1>>,<<Cnv2>>]        loop:<<Loop>>      outer_loop:none
265   /// CHECK-DAG: <<Abs:j\d+>>    Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
266   /// CHECK-DAG:                 Add [<<Phi2>>,<<Abs>>]         loop:<<Loop>>      outer_loop:none
267   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
268   //
269   /// CHECK-START-ARM64: long SimdSadShort2.sadCastedChar2Long(char[], char[]) loop_optimization (after)
270   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
271   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 0                 loop:none
272   /// CHECK-IF:     hasIsaFeature("sve")
273   //
274   //      SAD idiom is not supported for SVE.
275   ///     CHECK-NOT: VecSADAccumulate
276   //
277   /// CHECK-ELSE:
278   //
279   ///     CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
280   ///     CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<ConsL>>]      loop:none
281   ///     CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
282   ///     CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
283   ///     CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
284   ///     CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
285   ///     CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Load2>>] loop:<<Loop>> outer_loop:none
286   ///     CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
287   //
288   /// CHECK-FI:
sadCastedChar2Long(char[] s1, char[] s2)289   private static long sadCastedChar2Long(char[] s1, char[] s2) {
290     int min_length = Math.min(s1.length, s2.length);
291     long sad = 0;
292     for (int i = 0; i < min_length; i++) {
293       long x = (short) s1[i];
294       long y = (short) s2[i];
295       sad += Math.abs(x - y);
296     }
297     return sad;
298   }
299 
300   /// CHECK-START: long SimdSadShort2.sadCastedChar2LongAt1(char[], char[]) instruction_simplifier (before)
301   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
302   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
303   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 1                 loop:none
304   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
305   /// CHECK-DAG: <<Phi2:j\d+>>   Phi [<<ConsL>>,{{j\d+}}]       loop:<<Loop>>      outer_loop:none
306   /// CHECK-DAG: <<BC1:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>     outer_loop:none
307   /// CHECK-DAG: <<BC2:i\d+>>    BoundsCheck [<<Phi1>>,{{i\d+}}] loop:<<Loop>>     outer_loop:none
308   /// CHECK-DAG: <<Get1:c\d+>>   ArrayGet [{{l\d+}},<<BC1>>]    loop:<<Loop>>      outer_loop:none
309   /// CHECK-DAG: <<Get2:c\d+>>   ArrayGet [{{l\d+}},<<BC2>>]    loop:<<Loop>>      outer_loop:none
310   /// CHECK-DAG: <<Cnv1:s\d+>>   TypeConversion [<<Get1>>]      loop:<<Loop>>      outer_loop:none
311   /// CHECK-DAG: <<Cnv2:s\d+>>   TypeConversion [<<Get2>>]      loop:<<Loop>>      outer_loop:none
312   /// CHECK-DAG: <<Cnv3:j\d+>>   TypeConversion [<<Cnv1>>]      loop:<<Loop>>      outer_loop:none
313   /// CHECK-DAG: <<Cnv4:j\d+>>   TypeConversion [<<Cnv2>>]      loop:<<Loop>>      outer_loop:none
314   /// CHECK-DAG: <<Sub:j\d+>>    Sub [<<Cnv3>>,<<Cnv4>>]        loop:<<Loop>>      outer_loop:none
315   /// CHECK-DAG: <<Abs:j\d+>>    Abs [<<Sub>>]                  loop:<<Loop>> outer_loop:none
316   /// CHECK-DAG:                 Add [<<Phi2>>,<<Abs>>]         loop:<<Loop>>      outer_loop:none
317   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
318   //
319   /// CHECK-START: long SimdSadShort2.sadCastedChar2LongAt1(char[], char[]) loop_optimization (before)
320   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
321   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
322   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 1                 loop:none
323   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
324   /// CHECK-DAG: <<Phi2:j\d+>>   Phi [<<ConsL>>,{{j\d+}}]       loop:<<Loop>>      outer_loop:none
325   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
326   /// CHECK-DAG: <<Get2:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
327   /// CHECK-DAG: <<Cnv1:j\d+>>   TypeConversion [<<Get1>>]      loop:<<Loop>>      outer_loop:none
328   /// CHECK-DAG: <<Cnv2:j\d+>>   TypeConversion [<<Get2>>]      loop:<<Loop>>      outer_loop:none
329   /// CHECK-DAG: <<Sub:j\d+>>    Sub [<<Cnv1>>,<<Cnv2>>]        loop:<<Loop>>      outer_loop:none
330   /// CHECK-DAG: <<Abs:j\d+>>    Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
331   /// CHECK-DAG:                 Add [<<Phi2>>,<<Abs>>]         loop:<<Loop>>      outer_loop:none
332   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
333   //
334   /// CHECK-START-ARM64: long SimdSadShort2.sadCastedChar2LongAt1(char[], char[]) loop_optimization (after)
335   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
336   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 1                 loop:none
337   /// CHECK-IF:     hasIsaFeature("sve")
338   //
339   //      SAD idiom is not supported for SVE.
340   ///     CHECK-NOT: VecSADAccumulate
341   //
342   /// CHECK-ELSE:
343   //
344   ///     CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
345   ///     CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<ConsL>>]      loop:none
346   ///     CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
347   ///     CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
348   ///     CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
349   ///     CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
350   ///     CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Load2>>] loop:<<Loop>> outer_loop:none
351   ///     CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
352   //
353   /// CHECK-FI:
sadCastedChar2LongAt1(char[] s1, char[] s2)354   private static long sadCastedChar2LongAt1(char[] s1, char[] s2) {
355     int min_length = Math.min(s1.length, s2.length);
356     long sad = 1;  // starts at 1
357     for (int i = 0; i < min_length; i++) {
358       long x = (short) s1[i];
359       long y = (short) s2[i];
360       sad += Math.abs(x - y);
361     }
362     return sad;
363   }
364 
main()365   public static void main() {
366     // Cross-test the two most extreme values individually.
367     char[] s1 = { 0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
368     char[] s2 = { 0, 0x7fff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
369     expectEquals(-1, sadCastedChar2Short(s1, s2));
370     expectEquals(-1, sadCastedChar2Short(s2, s1));
371     expectEquals(-1, sadCastedChar2ShortAlt(s1, s2));
372     expectEquals(-1, sadCastedChar2ShortAlt(s2, s1));
373     expectEquals(-1, sadCastedChar2ShortAlt2(s1, s2));
374     expectEquals(-1, sadCastedChar2ShortAlt2(s2, s1));
375     expectEquals(65535, sadCastedChar2Int(s1, s2));
376     expectEquals(65535, sadCastedChar2Int(s2, s1));
377     expectEquals(65535, sadCastedChar2IntAlt(s1, s2));
378     expectEquals(65535, sadCastedChar2IntAlt(s2, s1));
379     expectEquals(65535, sadCastedChar2IntAlt2(s1, s2));
380     expectEquals(65535, sadCastedChar2IntAlt2(s2, s1));
381     expectEquals(65535L, sadCastedChar2Long(s1, s2));
382     expectEquals(65535L, sadCastedChar2Long(s2, s1));
383     expectEquals(65536L, sadCastedChar2LongAt1(s1, s2));
384     expectEquals(65536L, sadCastedChar2LongAt1(s2, s1));
385 
386     // Use cross-values to test all cases.
387     char[] interesting = {
388       (char) 0x0000,
389       (char) 0x0001,
390       (char) 0x0002,
391       (char) 0x1234,
392       (char) 0x8000,
393       (char) 0x8001,
394       (char) 0x7fff,
395       (char) 0xffff
396     };
397     int n = interesting.length;
398     int m = n * n + 1;
399     s1 = new char[m];
400     s2 = new char[m];
401     int k = 0;
402     for (int i = 0; i < n; i++) {
403       for (int j = 0; j < n; j++) {
404         s1[k] = interesting[i];
405         s2[k] = interesting[j];
406         k++;
407       }
408     }
409     s1[k] = 10;
410     s2[k] = 2;
411     expectEquals(-18932, sadCastedChar2Short(s1, s2));
412     expectEquals(-18932, sadCastedChar2ShortAlt(s1, s2));
413     expectEquals(-18932, sadCastedChar2ShortAlt2(s1, s2));
414     expectEquals(1291788, sadCastedChar2Int(s1, s2));
415     expectEquals(1291788, sadCastedChar2IntAlt(s1, s2));
416     expectEquals(1291788, sadCastedChar2IntAlt2(s1, s2));
417     expectEquals(1291788L, sadCastedChar2Long(s1, s2));
418     expectEquals(1291789L, sadCastedChar2LongAt1(s1, s2));
419 
420     System.out.println("SimdSadShort2 passed");
421   }
422 
expectEquals(int expected, int result)423   private static void expectEquals(int expected, int result) {
424     if (expected != result) {
425       throw new Error("Expected: " + expected + ", found: " + result);
426     }
427   }
428 
expectEquals(long expected, long result)429   private static void expectEquals(long expected, long result) {
430     if (expected != result) {
431       throw new Error("Expected: " + expected + ", found: " + result);
432     }
433   }
434 }
435