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-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
76   /// CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<Cons0>>]      loop:none
77   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
78   /// CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
79   /// CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
80   /// CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
81   /// CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Load2>>] loop:<<Loop>> outer_loop:none
82   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
sadShort2Int(short[] s1, short[] s2)83   private static int sadShort2Int(short[] s1, short[] s2) {
84     int min_length = Math.min(s1.length, s2.length);
85     int sad = 0;
86     for (int i = 0; i < min_length; i++) {
87       sad += Math.abs(s1[i] - s2[i]);
88     }
89     return sad;
90   }
91 
92   /// CHECK-START: int SimdSadShort.sadShort2IntAlt(short[], short[]) loop_optimization (before)
93   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
94   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
95   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
96   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
97   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
98   /// CHECK-DAG: <<Get2:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
99   /// CHECK-DAG: <<Sub:i\d+>>    Sub [<<Get2>>,<<Get1>>]        loop:<<Loop>>      outer_loop:none
100   /// CHECK-DAG: <<Intrin:i\d+>> Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
101   /// CHECK-DAG:                 Add [<<Phi2>>,<<Intrin>>]      loop:<<Loop>>      outer_loop:none
102   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
103   //
104   /// CHECK-START-ARM64: int SimdSadShort.sadShort2IntAlt(short[], short[]) loop_optimization (after)
105   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
106   /// CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
107   /// CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<Cons0>>]      loop:none
108   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
109   /// CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
110   /// CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
111   /// CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
112   /// CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load2>>,<<Load1>>] loop:<<Loop>> outer_loop:none
113   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
sadShort2IntAlt(short[] s1, short[] s2)114   private static int sadShort2IntAlt(short[] s1, short[] s2) {
115     int min_length = Math.min(s1.length, s2.length);
116     int sad = 0;
117     for (int i = 0; i < min_length; i++) {
118       short s = s1[i];
119       short p = s2[i];
120       sad += s >= p ? s - p : p - s;
121     }
122     return sad;
123   }
124 
125   /// CHECK-START: int SimdSadShort.sadShort2IntAlt2(short[], short[]) loop_optimization (before)
126   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
127   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
128   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
129   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
130   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
131   /// CHECK-DAG: <<Get2:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
132   /// CHECK-DAG: <<Sub:i\d+>>    Sub [<<Get1>>,<<Get2>>]        loop:<<Loop>>      outer_loop:none
133   /// CHECK-DAG: <<Intrin:i\d+>> Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
134   /// CHECK-DAG:                 Add [<<Phi2>>,<<Intrin>>]      loop:<<Loop>>      outer_loop:none
135   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
136   //
137   /// CHECK-START-ARM64: int SimdSadShort.sadShort2IntAlt2(short[], short[]) loop_optimization (after)
138   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
139   /// CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
140   /// CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<Cons0>>]      loop:none
141   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
142   /// CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
143   /// CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
144   /// CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
145   /// CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Load2>>] loop:<<Loop>> outer_loop:none
146   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
sadShort2IntAlt2(short[] s1, short[] s2)147   private static int sadShort2IntAlt2(short[] s1, short[] s2) {
148     int min_length = Math.min(s1.length, s2.length);
149     int sad = 0;
150     for (int i = 0; i < min_length; i++) {
151       short s = s1[i];
152       short p = s2[i];
153       int x = s - p;
154       if (x < 0) x = -x;
155       sad += x;
156     }
157     return sad;
158   }
159 
160   /// CHECK-START: int SimdSadShort.sadShort2IntConstant1(short[]) loop_optimization (before)
161   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
162   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
163   /// CHECK-DAG: <<Cons:i\d+>>   IntConstant -7                 loop:none
164   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
165   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
166   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
167   /// CHECK-DAG: <<Add:i\d+>>    Add [<<Get1>>,<<Cons>>]        loop:<<Loop>>      outer_loop:none
168   /// CHECK-DAG: <<Intrin:i\d+>> Abs [<<Add>>]                  loop:<<Loop>>      outer_loop:none
169   /// CHECK-DAG:                 Add [<<Phi2>>,<<Intrin>>]      loop:<<Loop>>      outer_loop:none
170   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
171   //
172   /// CHECK-START-ARM64: int SimdSadShort.sadShort2IntConstant1(short[]) loop_optimization (after)
173   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
174   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
175   /// CHECK-DAG: <<Cons:i\d+>>   IntConstant 7                  loop:none
176   /// CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
177   /// CHECK-DAG: <<Rep:d\d+>>    VecReplicateScalar [<<Cons>>]  loop:none
178   /// CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<Cons0>>]      loop:none
179   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
180   /// CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
181   /// CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
182   /// CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Rep>>] loop:<<Loop>> outer_loop:none
183   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
sadShort2IntConstant1(short[] s)184   private static int sadShort2IntConstant1(short[] s) {
185     int sad = 0;
186     for (int i = 0; i < s.length; i++) {
187       sad += Math.abs(s[i] - 7);  // s[i] + -7
188     }
189     return sad;
190   }
191 
192   /// CHECK-START: int SimdSadShort.sadShort2IntConstant2(short[]) loop_optimization (before)
193   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
194   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
195   /// CHECK-DAG: <<Cons:i\d+>>   IntConstant 7                  loop:none
196   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
197   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
198   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
199   /// CHECK-DAG: <<Sub:i\d+>>    Sub [<<Get1>>,<<Cons>>]        loop:<<Loop>>      outer_loop:none
200   /// CHECK-DAG: <<Intrin:i\d+>> Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
201   /// CHECK-DAG:                 Add [<<Phi2>>,<<Intrin>>]      loop:<<Loop>>      outer_loop:none
202   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
203   //
204   /// CHECK-START-ARM64: int SimdSadShort.sadShort2IntConstant2(short[]) loop_optimization (after)
205   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
206   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
207   /// CHECK-DAG: <<Cons:i\d+>>   IntConstant 7                  loop:none
208   /// CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
209   /// CHECK-DAG: <<Rep:d\d+>>    VecReplicateScalar [<<Cons>>]  loop:none
210   /// CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<Cons0>>]      loop:none
211   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
212   /// CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
213   /// CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
214   /// CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Rep>>] loop:<<Loop>> outer_loop:none
215   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
sadShort2IntConstant2(short[] s)216   private static int sadShort2IntConstant2(short[] s) {
217     int sad = 0;
218     for (int i = 0; i < s.length; i++) {
219       sad += Math.abs(s[i] - $inline$seven());  // s[i] - 7
220     }
221     return sad;
222   }
223 
224   /// CHECK-START: int SimdSadShort.sadShort2IntConstant3(short[]) loop_optimization (before)
225   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
226   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
227   /// CHECK-DAG: <<Cons:i\d+>>   IntConstant 7                  loop:none
228   /// CHECK-DAG: <<Phi2:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
229   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop>>      outer_loop:none
230   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
231   /// CHECK-DAG: <<Add:i\d+>>    Add [<<Get1>>,<<Cons>>]        loop:<<Loop>>      outer_loop:none
232   /// CHECK-DAG: <<Intrin:i\d+>> Abs [<<Add>>]                  loop:<<Loop>>      outer_loop:none
233   /// CHECK-DAG:                 Add [<<Phi2>>,<<Intrin>>]      loop:<<Loop>>      outer_loop:none
234   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
235   //
236   /// CHECK-START-ARM64: int SimdSadShort.sadShort2IntConstant3(short[]) loop_optimization (after)
237   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
238   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
239   /// CHECK-DAG: <<Cons:i\d+>>   IntConstant -7                 loop:none
240   /// CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
241   /// CHECK-DAG: <<Rep:d\d+>>    VecReplicateScalar [<<Cons>>]  loop:none
242   /// CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<Cons0>>]      loop:none
243   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
244   /// CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
245   /// CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
246   /// CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Rep>>] loop:<<Loop>> outer_loop:none
247   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
sadShort2IntConstant3(short[] s)248   private static int sadShort2IntConstant3(short[] s) {
249     int sad = 0;
250     for (int i = 0; i < s.length; i++) {
251       sad += Math.abs(s[i] + $inline$seven());  // hidden s[i] - (-7)
252     }
253     return sad;
254   }
255 
256   /// CHECK-START: long SimdSadShort.sadShort2Long(short[], short[]) loop_optimization (before)
257   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
258   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
259   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 0                 loop:none
260   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
261   /// CHECK-DAG: <<Phi2:j\d+>>   Phi [<<ConsL>>,{{j\d+}}]       loop:<<Loop>>      outer_loop:none
262   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
263   /// CHECK-DAG: <<Get2:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
264   /// CHECK-DAG: <<Cnv1:j\d+>>   TypeConversion [<<Get1>>]      loop:<<Loop>>      outer_loop:none
265   /// CHECK-DAG: <<Cnv2:j\d+>>   TypeConversion [<<Get2>>]      loop:<<Loop>>      outer_loop:none
266   /// CHECK-DAG: <<Sub:j\d+>>    Sub [<<Cnv1>>,<<Cnv2>>]        loop:<<Loop>>      outer_loop:none
267   /// CHECK-DAG: <<Intrin:j\d+>> Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
268   /// CHECK-DAG:                 Add [<<Phi2>>,<<Intrin>>]      loop:<<Loop>>      outer_loop:none
269   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
270   //
271   /// CHECK-START-ARM64: long SimdSadShort.sadShort2Long(short[], short[]) loop_optimization (after)
272   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
273   /// CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
274   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 0                 loop:none
275   /// CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<ConsL>>]      loop:none
276   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
277   /// CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
278   /// CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
279   /// CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
280   /// CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Load2>>] loop:<<Loop>> outer_loop:none
281   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
sadShort2Long(short[] s1, short[] s2)282   private static long sadShort2Long(short[] s1, short[] s2) {
283     int min_length = Math.min(s1.length, s2.length);
284     long sad = 0;
285     for (int i = 0; i < min_length; i++) {
286       long x = s1[i];
287       long y = s2[i];
288       sad += Math.abs(x - y);
289     }
290     return sad;
291   }
292 
293   /// CHECK-START: long SimdSadShort.sadShort2LongAt1(short[], short[]) loop_optimization (before)
294   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
295   /// CHECK-DAG: <<Cons1:i\d+>>  IntConstant 1                  loop:none
296   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 1                 loop:none
297   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
298   /// CHECK-DAG: <<Phi2:j\d+>>   Phi [<<ConsL>>,{{j\d+}}]       loop:<<Loop>>      outer_loop:none
299   /// CHECK-DAG: <<Get1:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
300   /// CHECK-DAG: <<Get2:s\d+>>   ArrayGet [{{l\d+}},<<Phi1>>]   loop:<<Loop>>      outer_loop:none
301   /// CHECK-DAG: <<Cnv1:j\d+>>   TypeConversion [<<Get1>>]      loop:<<Loop>>      outer_loop:none
302   /// CHECK-DAG: <<Cnv2:j\d+>>   TypeConversion [<<Get2>>]      loop:<<Loop>>      outer_loop:none
303   /// CHECK-DAG: <<Sub:j\d+>>    Sub [<<Cnv1>>,<<Cnv2>>]        loop:<<Loop>>      outer_loop:none
304   /// CHECK-DAG: <<Intrin:j\d+>> Abs [<<Sub>>]                  loop:<<Loop>>      outer_loop:none
305   /// CHECK-DAG:                 Add [<<Phi2>>,<<Intrin>>]      loop:<<Loop>>      outer_loop:none
306   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons1>>]       loop:<<Loop>>      outer_loop:none
307   //
308   /// CHECK-START-ARM64: long SimdSadShort.sadShort2LongAt1(short[], short[]) loop_optimization (after)
309   /// CHECK-DAG: <<Cons0:i\d+>>  IntConstant 0                  loop:none
310   /// CHECK-DAG: <<Cons8:i\d+>>  IntConstant 8                  loop:none
311   /// CHECK-DAG: <<ConsL:j\d+>>  LongConstant 1                 loop:none
312   /// CHECK-DAG: <<Set:d\d+>>    VecSetScalars [<<ConsL>>]      loop:none
313   /// CHECK-DAG: <<Phi1:i\d+>>   Phi [<<Cons0>>,{{i\d+}}]       loop:<<Loop:B\d+>> outer_loop:none
314   /// CHECK-DAG: <<Phi2:d\d+>>   Phi [<<Set>>,{{d\d+}}]         loop:<<Loop>>      outer_loop:none
315   /// CHECK-DAG: <<Load1:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
316   /// CHECK-DAG: <<Load2:d\d+>>  VecLoad [{{l\d+}},<<Phi1>>]    loop:<<Loop>>      outer_loop:none
317   /// CHECK-DAG: <<SAD:d\d+>>    VecSADAccumulate [<<Phi2>>,<<Load1>>,<<Load2>>] loop:<<Loop>> outer_loop:none
318   /// CHECK-DAG:                 Add [<<Phi1>>,<<Cons8>>]       loop:<<Loop>>      outer_loop:none
sadShort2LongAt1(short[] s1, short[] s2)319   private static long sadShort2LongAt1(short[] s1, short[] s2) {
320     int min_length = Math.min(s1.length, s2.length);
321     long sad = 1;  // starts at 1
322     for (int i = 0; i < min_length; i++) {
323       long x = s1[i];
324       long y = s2[i];
325       sad += Math.abs(x - y);
326     }
327     return sad;
328   }
329 
main()330   public static void main() {
331     // Cross-test the two most extreme values individually.
332     short[] s1 = { 0, -32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
333     short[] s2 = { 0,  32767, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
334     expectEquals(-1, sadShort2Short(s1, s2));
335     expectEquals(-1, sadShort2Short(s2, s1));
336     expectEquals(-1, sadShort2ShortAlt(s1, s2));
337     expectEquals(-1, sadShort2ShortAlt(s2, s1));
338     expectEquals(-1, sadShort2ShortAlt2(s1, s2));
339     expectEquals(-1, sadShort2ShortAlt2(s2, s1));
340     expectEquals(65535, sadShort2Int(s1, s2));
341     expectEquals(65535, sadShort2Int(s2, s1));
342     expectEquals(65535, sadShort2IntAlt(s1, s2));
343     expectEquals(65535, sadShort2IntAlt(s2, s1));
344     expectEquals(65535, sadShort2IntAlt2(s1, s2));
345     expectEquals(65535, sadShort2IntAlt2(s2, s1));
346     expectEquals(32880, sadShort2IntConstant1(s1));
347     expectEquals(32880, sadShort2IntConstant2(s1));
348     expectEquals(32866, sadShort2IntConstant3(s1));
349     expectEquals(65535L, sadShort2Long(s1, s2));
350     expectEquals(65535L, sadShort2Long(s2, s1));
351     expectEquals(65536L, sadShort2LongAt1(s1, s2));
352     expectEquals(65536L, sadShort2LongAt1(s2, s1));
353 
354     // Use cross-values to test all cases.
355     short[] interesting = {
356       (short) 0x0000,
357       (short) 0x0001,
358       (short) 0x0002,
359       (short) 0x1234,
360       (short) 0x8000,
361       (short) 0x8001,
362       (short) 0x7fff,
363       (short) 0xffff
364     };
365     int n = interesting.length;
366     int m = n * n + 1;
367     s1 = new short[m];
368     s2 = new short[m];
369     int k = 0;
370     for (int i = 0; i < n; i++) {
371       for (int j = 0; j < n; j++) {
372         s1[k] = interesting[i];
373         s2[k] = interesting[j];
374         k++;
375       }
376     }
377     s1[k] = 10;
378     s2[k] = 2;
379     expectEquals(-18932, sadShort2Short(s1, s2));
380     expectEquals(-18932, sadShort2ShortAlt(s1, s2));
381     expectEquals(-18932, sadShort2ShortAlt2(s1, s2));
382     expectEquals(1291788, sadShort2Int(s1, s2));
383     expectEquals(1291788, sadShort2IntAlt(s1, s2));
384     expectEquals(1291788, sadShort2IntAlt2(s1, s2));
385     expectEquals(823907, sadShort2IntConstant1(s1));
386     expectEquals(823907, sadShort2IntConstant2(s1));
387     expectEquals(823953, sadShort2IntConstant3(s1));
388     expectEquals(1291788L, sadShort2Long(s1, s2));
389     expectEquals(1291789L, sadShort2LongAt1(s1, s2));
390 
391     System.out.println("SimdSadShort passed");
392   }
393 
expectEquals(int expected, int result)394   private static void expectEquals(int expected, int result) {
395     if (expected != result) {
396       throw new Error("Expected: " + expected + ", found: " + result);
397     }
398   }
399 
expectEquals(long expected, long result)400   private static void expectEquals(long expected, long result) {
401     if (expected != result) {
402       throw new Error("Expected: " + expected + ", found: " + result);
403     }
404   }
405 }
406