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