• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <emmintrin.h>
12 
13 #include "./vpx_dsp_rtcd.h"
14 #include "vpx/vpx_integer.h"
15 #include "vpx_dsp/x86/bitdepth_conversion_sse2.h"
16 #include "vpx_ports/mem.h"
17 
vpx_minmax_8x8_sse2(const uint8_t * s,int p,const uint8_t * d,int dp,int * min,int * max)18 void vpx_minmax_8x8_sse2(const uint8_t *s, int p, const uint8_t *d, int dp,
19                          int *min, int *max) {
20   __m128i u0, s0, d0, diff, maxabsdiff, minabsdiff, negdiff, absdiff0, absdiff;
21   u0 = _mm_setzero_si128();
22   // Row 0
23   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s)), u0);
24   d0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(d)), u0);
25   diff = _mm_subs_epi16(s0, d0);
26   negdiff = _mm_subs_epi16(u0, diff);
27   absdiff0 = _mm_max_epi16(diff, negdiff);
28   // Row 1
29   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + p)), u0);
30   d0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(d + dp)), u0);
31   diff = _mm_subs_epi16(s0, d0);
32   negdiff = _mm_subs_epi16(u0, diff);
33   absdiff = _mm_max_epi16(diff, negdiff);
34   maxabsdiff = _mm_max_epi16(absdiff0, absdiff);
35   minabsdiff = _mm_min_epi16(absdiff0, absdiff);
36   // Row 2
37   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 2 * p)), u0);
38   d0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(d + 2 * dp)), u0);
39   diff = _mm_subs_epi16(s0, d0);
40   negdiff = _mm_subs_epi16(u0, diff);
41   absdiff = _mm_max_epi16(diff, negdiff);
42   maxabsdiff = _mm_max_epi16(maxabsdiff, absdiff);
43   minabsdiff = _mm_min_epi16(minabsdiff, absdiff);
44   // Row 3
45   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 3 * p)), u0);
46   d0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(d + 3 * dp)), u0);
47   diff = _mm_subs_epi16(s0, d0);
48   negdiff = _mm_subs_epi16(u0, diff);
49   absdiff = _mm_max_epi16(diff, negdiff);
50   maxabsdiff = _mm_max_epi16(maxabsdiff, absdiff);
51   minabsdiff = _mm_min_epi16(minabsdiff, absdiff);
52   // Row 4
53   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 4 * p)), u0);
54   d0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(d + 4 * dp)), u0);
55   diff = _mm_subs_epi16(s0, d0);
56   negdiff = _mm_subs_epi16(u0, diff);
57   absdiff = _mm_max_epi16(diff, negdiff);
58   maxabsdiff = _mm_max_epi16(maxabsdiff, absdiff);
59   minabsdiff = _mm_min_epi16(minabsdiff, absdiff);
60   // Row 5
61   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 5 * p)), u0);
62   d0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(d + 5 * dp)), u0);
63   diff = _mm_subs_epi16(s0, d0);
64   negdiff = _mm_subs_epi16(u0, diff);
65   absdiff = _mm_max_epi16(diff, negdiff);
66   maxabsdiff = _mm_max_epi16(maxabsdiff, absdiff);
67   minabsdiff = _mm_min_epi16(minabsdiff, absdiff);
68   // Row 6
69   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 6 * p)), u0);
70   d0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(d + 6 * dp)), u0);
71   diff = _mm_subs_epi16(s0, d0);
72   negdiff = _mm_subs_epi16(u0, diff);
73   absdiff = _mm_max_epi16(diff, negdiff);
74   maxabsdiff = _mm_max_epi16(maxabsdiff, absdiff);
75   minabsdiff = _mm_min_epi16(minabsdiff, absdiff);
76   // Row 7
77   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 7 * p)), u0);
78   d0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(d + 7 * dp)), u0);
79   diff = _mm_subs_epi16(s0, d0);
80   negdiff = _mm_subs_epi16(u0, diff);
81   absdiff = _mm_max_epi16(diff, negdiff);
82   maxabsdiff = _mm_max_epi16(maxabsdiff, absdiff);
83   minabsdiff = _mm_min_epi16(minabsdiff, absdiff);
84 
85   maxabsdiff = _mm_max_epi16(maxabsdiff, _mm_srli_si128(maxabsdiff, 8));
86   maxabsdiff = _mm_max_epi16(maxabsdiff, _mm_srli_epi64(maxabsdiff, 32));
87   maxabsdiff = _mm_max_epi16(maxabsdiff, _mm_srli_epi64(maxabsdiff, 16));
88   *max = _mm_extract_epi16(maxabsdiff, 0);
89 
90   minabsdiff = _mm_min_epi16(minabsdiff, _mm_srli_si128(minabsdiff, 8));
91   minabsdiff = _mm_min_epi16(minabsdiff, _mm_srli_epi64(minabsdiff, 32));
92   minabsdiff = _mm_min_epi16(minabsdiff, _mm_srli_epi64(minabsdiff, 16));
93   *min = _mm_extract_epi16(minabsdiff, 0);
94 }
95 
vpx_avg_8x8_sse2(const uint8_t * s,int p)96 unsigned int vpx_avg_8x8_sse2(const uint8_t *s, int p) {
97   __m128i s0, s1, u0;
98   unsigned int avg = 0;
99   u0 = _mm_setzero_si128();
100   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s)), u0);
101   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + p)), u0);
102   s0 = _mm_adds_epu16(s0, s1);
103   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 2 * p)), u0);
104   s0 = _mm_adds_epu16(s0, s1);
105   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 3 * p)), u0);
106   s0 = _mm_adds_epu16(s0, s1);
107   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 4 * p)), u0);
108   s0 = _mm_adds_epu16(s0, s1);
109   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 5 * p)), u0);
110   s0 = _mm_adds_epu16(s0, s1);
111   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 6 * p)), u0);
112   s0 = _mm_adds_epu16(s0, s1);
113   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 7 * p)), u0);
114   s0 = _mm_adds_epu16(s0, s1);
115 
116   s0 = _mm_adds_epu16(s0, _mm_srli_si128(s0, 8));
117   s0 = _mm_adds_epu16(s0, _mm_srli_epi64(s0, 32));
118   s0 = _mm_adds_epu16(s0, _mm_srli_epi64(s0, 16));
119   avg = _mm_extract_epi16(s0, 0);
120   return (avg + 32) >> 6;
121 }
122 
vpx_avg_4x4_sse2(const uint8_t * s,int p)123 unsigned int vpx_avg_4x4_sse2(const uint8_t *s, int p) {
124   __m128i s0, s1, u0;
125   unsigned int avg = 0;
126   u0 = _mm_setzero_si128();
127   s0 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s)), u0);
128   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + p)), u0);
129   s0 = _mm_adds_epu16(s0, s1);
130   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 2 * p)), u0);
131   s0 = _mm_adds_epu16(s0, s1);
132   s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i *)(s + 3 * p)), u0);
133   s0 = _mm_adds_epu16(s0, s1);
134 
135   s0 = _mm_adds_epu16(s0, _mm_srli_si128(s0, 4));
136   s0 = _mm_adds_epu16(s0, _mm_srli_epi64(s0, 16));
137   avg = _mm_extract_epi16(s0, 0);
138   return (avg + 8) >> 4;
139 }
140 
141 #if CONFIG_VP9_HIGHBITDEPTH
vpx_highbd_avg_8x8_sse2(const uint8_t * s8,int p)142 unsigned int vpx_highbd_avg_8x8_sse2(const uint8_t *s8, int p) {
143   __m128i s0, s1;
144   unsigned int avg;
145   const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
146   const __m128i zero = _mm_setzero_si128();
147   s0 = _mm_loadu_si128((const __m128i *)(s));
148   s1 = _mm_loadu_si128((const __m128i *)(s + p));
149   s0 = _mm_adds_epu16(s0, s1);
150   s1 = _mm_loadu_si128((const __m128i *)(s + 2 * p));
151   s0 = _mm_adds_epu16(s0, s1);
152   s1 = _mm_loadu_si128((const __m128i *)(s + 3 * p));
153   s0 = _mm_adds_epu16(s0, s1);
154   s1 = _mm_loadu_si128((const __m128i *)(s + 4 * p));
155   s0 = _mm_adds_epu16(s0, s1);
156   s1 = _mm_loadu_si128((const __m128i *)(s + 5 * p));
157   s0 = _mm_adds_epu16(s0, s1);
158   s1 = _mm_loadu_si128((const __m128i *)(s + 6 * p));
159   s0 = _mm_adds_epu16(s0, s1);
160   s1 = _mm_loadu_si128((const __m128i *)(s + 7 * p));
161   s0 = _mm_adds_epu16(s0, s1);
162   s1 = _mm_unpackhi_epi16(s0, zero);
163   s0 = _mm_unpacklo_epi16(s0, zero);
164   s0 = _mm_add_epi32(s0, s1);
165   s0 = _mm_add_epi32(s0, _mm_srli_si128(s0, 8));
166   s0 = _mm_add_epi32(s0, _mm_srli_si128(s0, 4));
167   avg = _mm_cvtsi128_si32(s0);
168 
169   return (avg + 32) >> 6;
170 }
171 
vpx_highbd_avg_4x4_sse2(const uint8_t * s8,int p)172 unsigned int vpx_highbd_avg_4x4_sse2(const uint8_t *s8, int p) {
173   __m128i s0, s1;
174   unsigned int avg;
175   const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
176   s0 = _mm_loadl_epi64((const __m128i *)(s));
177   s1 = _mm_loadl_epi64((const __m128i *)(s + p));
178   s0 = _mm_adds_epu16(s0, s1);
179   s1 = _mm_loadl_epi64((const __m128i *)(s + 2 * p));
180   s0 = _mm_adds_epu16(s0, s1);
181   s1 = _mm_loadl_epi64((const __m128i *)(s + 3 * p));
182   s0 = _mm_adds_epu16(s0, s1);
183   s0 = _mm_add_epi16(s0, _mm_srli_si128(s0, 4));
184   s0 = _mm_add_epi16(s0, _mm_srli_si128(s0, 2));
185   avg = _mm_extract_epi16(s0, 0);
186 
187   return (avg + 8) >> 4;
188 }
189 #endif  // CONFIG_VP9_HIGHBITDEPTH
190 
hadamard_col8_sse2(__m128i * in,int iter)191 static void hadamard_col8_sse2(__m128i *in, int iter) {
192   __m128i a0 = in[0];
193   __m128i a1 = in[1];
194   __m128i a2 = in[2];
195   __m128i a3 = in[3];
196   __m128i a4 = in[4];
197   __m128i a5 = in[5];
198   __m128i a6 = in[6];
199   __m128i a7 = in[7];
200 
201   __m128i b0 = _mm_add_epi16(a0, a1);
202   __m128i b1 = _mm_sub_epi16(a0, a1);
203   __m128i b2 = _mm_add_epi16(a2, a3);
204   __m128i b3 = _mm_sub_epi16(a2, a3);
205   __m128i b4 = _mm_add_epi16(a4, a5);
206   __m128i b5 = _mm_sub_epi16(a4, a5);
207   __m128i b6 = _mm_add_epi16(a6, a7);
208   __m128i b7 = _mm_sub_epi16(a6, a7);
209 
210   a0 = _mm_add_epi16(b0, b2);
211   a1 = _mm_add_epi16(b1, b3);
212   a2 = _mm_sub_epi16(b0, b2);
213   a3 = _mm_sub_epi16(b1, b3);
214   a4 = _mm_add_epi16(b4, b6);
215   a5 = _mm_add_epi16(b5, b7);
216   a6 = _mm_sub_epi16(b4, b6);
217   a7 = _mm_sub_epi16(b5, b7);
218 
219   if (iter == 0) {
220     b0 = _mm_add_epi16(a0, a4);
221     b7 = _mm_add_epi16(a1, a5);
222     b3 = _mm_add_epi16(a2, a6);
223     b4 = _mm_add_epi16(a3, a7);
224     b2 = _mm_sub_epi16(a0, a4);
225     b6 = _mm_sub_epi16(a1, a5);
226     b1 = _mm_sub_epi16(a2, a6);
227     b5 = _mm_sub_epi16(a3, a7);
228 
229     a0 = _mm_unpacklo_epi16(b0, b1);
230     a1 = _mm_unpacklo_epi16(b2, b3);
231     a2 = _mm_unpackhi_epi16(b0, b1);
232     a3 = _mm_unpackhi_epi16(b2, b3);
233     a4 = _mm_unpacklo_epi16(b4, b5);
234     a5 = _mm_unpacklo_epi16(b6, b7);
235     a6 = _mm_unpackhi_epi16(b4, b5);
236     a7 = _mm_unpackhi_epi16(b6, b7);
237 
238     b0 = _mm_unpacklo_epi32(a0, a1);
239     b1 = _mm_unpacklo_epi32(a4, a5);
240     b2 = _mm_unpackhi_epi32(a0, a1);
241     b3 = _mm_unpackhi_epi32(a4, a5);
242     b4 = _mm_unpacklo_epi32(a2, a3);
243     b5 = _mm_unpacklo_epi32(a6, a7);
244     b6 = _mm_unpackhi_epi32(a2, a3);
245     b7 = _mm_unpackhi_epi32(a6, a7);
246 
247     in[0] = _mm_unpacklo_epi64(b0, b1);
248     in[1] = _mm_unpackhi_epi64(b0, b1);
249     in[2] = _mm_unpacklo_epi64(b2, b3);
250     in[3] = _mm_unpackhi_epi64(b2, b3);
251     in[4] = _mm_unpacklo_epi64(b4, b5);
252     in[5] = _mm_unpackhi_epi64(b4, b5);
253     in[6] = _mm_unpacklo_epi64(b6, b7);
254     in[7] = _mm_unpackhi_epi64(b6, b7);
255   } else {
256     in[0] = _mm_add_epi16(a0, a4);
257     in[7] = _mm_add_epi16(a1, a5);
258     in[3] = _mm_add_epi16(a2, a6);
259     in[4] = _mm_add_epi16(a3, a7);
260     in[2] = _mm_sub_epi16(a0, a4);
261     in[6] = _mm_sub_epi16(a1, a5);
262     in[1] = _mm_sub_epi16(a2, a6);
263     in[5] = _mm_sub_epi16(a3, a7);
264   }
265 }
266 
hadamard_8x8_sse2(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff,int is_final)267 static INLINE void hadamard_8x8_sse2(const int16_t *src_diff,
268                                      ptrdiff_t src_stride, tran_low_t *coeff,
269                                      int is_final) {
270   __m128i src[8];
271   src[0] = _mm_load_si128((const __m128i *)src_diff);
272   src[1] = _mm_load_si128((const __m128i *)(src_diff += src_stride));
273   src[2] = _mm_load_si128((const __m128i *)(src_diff += src_stride));
274   src[3] = _mm_load_si128((const __m128i *)(src_diff += src_stride));
275   src[4] = _mm_load_si128((const __m128i *)(src_diff += src_stride));
276   src[5] = _mm_load_si128((const __m128i *)(src_diff += src_stride));
277   src[6] = _mm_load_si128((const __m128i *)(src_diff += src_stride));
278   src[7] = _mm_load_si128((const __m128i *)(src_diff += src_stride));
279 
280   hadamard_col8_sse2(src, 0);
281   hadamard_col8_sse2(src, 1);
282 
283   if (is_final) {
284     store_tran_low(src[0], coeff);
285     coeff += 8;
286     store_tran_low(src[1], coeff);
287     coeff += 8;
288     store_tran_low(src[2], coeff);
289     coeff += 8;
290     store_tran_low(src[3], coeff);
291     coeff += 8;
292     store_tran_low(src[4], coeff);
293     coeff += 8;
294     store_tran_low(src[5], coeff);
295     coeff += 8;
296     store_tran_low(src[6], coeff);
297     coeff += 8;
298     store_tran_low(src[7], coeff);
299   } else {
300     int16_t *coeff16 = (int16_t *)coeff;
301     _mm_store_si128((__m128i *)coeff16, src[0]);
302     coeff16 += 8;
303     _mm_store_si128((__m128i *)coeff16, src[1]);
304     coeff16 += 8;
305     _mm_store_si128((__m128i *)coeff16, src[2]);
306     coeff16 += 8;
307     _mm_store_si128((__m128i *)coeff16, src[3]);
308     coeff16 += 8;
309     _mm_store_si128((__m128i *)coeff16, src[4]);
310     coeff16 += 8;
311     _mm_store_si128((__m128i *)coeff16, src[5]);
312     coeff16 += 8;
313     _mm_store_si128((__m128i *)coeff16, src[6]);
314     coeff16 += 8;
315     _mm_store_si128((__m128i *)coeff16, src[7]);
316   }
317 }
318 
vpx_hadamard_8x8_sse2(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)319 void vpx_hadamard_8x8_sse2(const int16_t *src_diff, ptrdiff_t src_stride,
320                            tran_low_t *coeff) {
321   hadamard_8x8_sse2(src_diff, src_stride, coeff, 1);
322 }
323 
hadamard_16x16_sse2(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff,int is_final)324 static INLINE void hadamard_16x16_sse2(const int16_t *src_diff,
325                                        ptrdiff_t src_stride, tran_low_t *coeff,
326                                        int is_final) {
327 #if CONFIG_VP9_HIGHBITDEPTH
328   // For high bitdepths, it is unnecessary to store_tran_low
329   // (mult/unpack/store), then load_tran_low (load/pack) the same memory in the
330   // next stage.  Output to an intermediate buffer first, then store_tran_low()
331   // in the final stage.
332   DECLARE_ALIGNED(32, int16_t, temp_coeff[16 * 16]);
333   int16_t *t_coeff = temp_coeff;
334 #else
335   int16_t *t_coeff = coeff;
336 #endif
337   int16_t *coeff16 = (int16_t *)coeff;
338   int idx;
339   for (idx = 0; idx < 4; ++idx) {
340     const int16_t *src_ptr =
341         src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
342     hadamard_8x8_sse2(src_ptr, src_stride, (tran_low_t *)(t_coeff + idx * 64),
343                       0);
344   }
345 
346   for (idx = 0; idx < 64; idx += 8) {
347     __m128i coeff0 = _mm_load_si128((const __m128i *)t_coeff);
348     __m128i coeff1 = _mm_load_si128((const __m128i *)(t_coeff + 64));
349     __m128i coeff2 = _mm_load_si128((const __m128i *)(t_coeff + 128));
350     __m128i coeff3 = _mm_load_si128((const __m128i *)(t_coeff + 192));
351 
352     __m128i b0 = _mm_add_epi16(coeff0, coeff1);
353     __m128i b1 = _mm_sub_epi16(coeff0, coeff1);
354     __m128i b2 = _mm_add_epi16(coeff2, coeff3);
355     __m128i b3 = _mm_sub_epi16(coeff2, coeff3);
356 
357     b0 = _mm_srai_epi16(b0, 1);
358     b1 = _mm_srai_epi16(b1, 1);
359     b2 = _mm_srai_epi16(b2, 1);
360     b3 = _mm_srai_epi16(b3, 1);
361 
362     coeff0 = _mm_add_epi16(b0, b2);
363     coeff1 = _mm_add_epi16(b1, b3);
364     coeff2 = _mm_sub_epi16(b0, b2);
365     coeff3 = _mm_sub_epi16(b1, b3);
366 
367     if (is_final) {
368       store_tran_low(coeff0, coeff);
369       store_tran_low(coeff1, coeff + 64);
370       store_tran_low(coeff2, coeff + 128);
371       store_tran_low(coeff3, coeff + 192);
372       coeff += 8;
373     } else {
374       _mm_store_si128((__m128i *)coeff16, coeff0);
375       _mm_store_si128((__m128i *)(coeff16 + 64), coeff1);
376       _mm_store_si128((__m128i *)(coeff16 + 128), coeff2);
377       _mm_store_si128((__m128i *)(coeff16 + 192), coeff3);
378       coeff16 += 8;
379     }
380 
381     t_coeff += 8;
382   }
383 }
384 
vpx_hadamard_16x16_sse2(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)385 void vpx_hadamard_16x16_sse2(const int16_t *src_diff, ptrdiff_t src_stride,
386                              tran_low_t *coeff) {
387   hadamard_16x16_sse2(src_diff, src_stride, coeff, 1);
388 }
389 
vpx_hadamard_32x32_sse2(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)390 void vpx_hadamard_32x32_sse2(const int16_t *src_diff, ptrdiff_t src_stride,
391                              tran_low_t *coeff) {
392 #if CONFIG_VP9_HIGHBITDEPTH
393   // For high bitdepths, it is unnecessary to store_tran_low
394   // (mult/unpack/store), then load_tran_low (load/pack) the same memory in the
395   // next stage.  Output to an intermediate buffer first, then store_tran_low()
396   // in the final stage.
397   DECLARE_ALIGNED(32, int16_t, temp_coeff[32 * 32]);
398   int16_t *t_coeff = temp_coeff;
399 #else
400   int16_t *t_coeff = coeff;
401 #endif
402   int idx;
403   for (idx = 0; idx < 4; ++idx) {
404     const int16_t *src_ptr =
405         src_diff + (idx >> 1) * 16 * src_stride + (idx & 0x01) * 16;
406     hadamard_16x16_sse2(src_ptr, src_stride,
407                         (tran_low_t *)(t_coeff + idx * 256), 0);
408   }
409 
410   for (idx = 0; idx < 256; idx += 8) {
411     __m128i coeff0 = _mm_load_si128((const __m128i *)t_coeff);
412     __m128i coeff1 = _mm_load_si128((const __m128i *)(t_coeff + 256));
413     __m128i coeff2 = _mm_load_si128((const __m128i *)(t_coeff + 512));
414     __m128i coeff3 = _mm_load_si128((const __m128i *)(t_coeff + 768));
415 
416     __m128i b0 = _mm_add_epi16(coeff0, coeff1);
417     __m128i b1 = _mm_sub_epi16(coeff0, coeff1);
418     __m128i b2 = _mm_add_epi16(coeff2, coeff3);
419     __m128i b3 = _mm_sub_epi16(coeff2, coeff3);
420 
421     b0 = _mm_srai_epi16(b0, 2);
422     b1 = _mm_srai_epi16(b1, 2);
423     b2 = _mm_srai_epi16(b2, 2);
424     b3 = _mm_srai_epi16(b3, 2);
425 
426     coeff0 = _mm_add_epi16(b0, b2);
427     coeff1 = _mm_add_epi16(b1, b3);
428     store_tran_low(coeff0, coeff);
429     store_tran_low(coeff1, coeff + 256);
430 
431     coeff2 = _mm_sub_epi16(b0, b2);
432     coeff3 = _mm_sub_epi16(b1, b3);
433     store_tran_low(coeff2, coeff + 512);
434     store_tran_low(coeff3, coeff + 768);
435 
436     coeff += 8;
437     t_coeff += 8;
438   }
439 }
440 
vpx_satd_sse2(const tran_low_t * coeff,int length)441 int vpx_satd_sse2(const tran_low_t *coeff, int length) {
442   int i;
443   const __m128i zero = _mm_setzero_si128();
444   __m128i accum = zero;
445 
446   for (i = 0; i < length; i += 8) {
447     const __m128i src_line = load_tran_low(coeff);
448     const __m128i inv = _mm_sub_epi16(zero, src_line);
449     const __m128i abs = _mm_max_epi16(src_line, inv);  // abs(src_line)
450     const __m128i abs_lo = _mm_unpacklo_epi16(abs, zero);
451     const __m128i abs_hi = _mm_unpackhi_epi16(abs, zero);
452     const __m128i sum = _mm_add_epi32(abs_lo, abs_hi);
453     accum = _mm_add_epi32(accum, sum);
454     coeff += 8;
455   }
456 
457   {  // cascading summation of accum
458     __m128i hi = _mm_srli_si128(accum, 8);
459     accum = _mm_add_epi32(accum, hi);
460     hi = _mm_srli_epi64(accum, 32);
461     accum = _mm_add_epi32(accum, hi);
462   }
463 
464   return _mm_cvtsi128_si32(accum);
465 }
466 
vpx_int_pro_row_sse2(int16_t * hbuf,const uint8_t * ref,const int ref_stride,const int height)467 void vpx_int_pro_row_sse2(int16_t *hbuf, const uint8_t *ref,
468                           const int ref_stride, const int height) {
469   int idx;
470   __m128i zero = _mm_setzero_si128();
471   __m128i src_line = _mm_loadu_si128((const __m128i *)ref);
472   __m128i s0 = _mm_unpacklo_epi8(src_line, zero);
473   __m128i s1 = _mm_unpackhi_epi8(src_line, zero);
474   __m128i t0, t1;
475   int height_1 = height - 1;
476   ref += ref_stride;
477 
478   for (idx = 1; idx < height_1; idx += 2) {
479     src_line = _mm_loadu_si128((const __m128i *)ref);
480     t0 = _mm_unpacklo_epi8(src_line, zero);
481     t1 = _mm_unpackhi_epi8(src_line, zero);
482     s0 = _mm_adds_epu16(s0, t0);
483     s1 = _mm_adds_epu16(s1, t1);
484     ref += ref_stride;
485 
486     src_line = _mm_loadu_si128((const __m128i *)ref);
487     t0 = _mm_unpacklo_epi8(src_line, zero);
488     t1 = _mm_unpackhi_epi8(src_line, zero);
489     s0 = _mm_adds_epu16(s0, t0);
490     s1 = _mm_adds_epu16(s1, t1);
491     ref += ref_stride;
492   }
493 
494   src_line = _mm_loadu_si128((const __m128i *)ref);
495   t0 = _mm_unpacklo_epi8(src_line, zero);
496   t1 = _mm_unpackhi_epi8(src_line, zero);
497   s0 = _mm_adds_epu16(s0, t0);
498   s1 = _mm_adds_epu16(s1, t1);
499 
500   if (height == 64) {
501     s0 = _mm_srai_epi16(s0, 5);
502     s1 = _mm_srai_epi16(s1, 5);
503   } else if (height == 32) {
504     s0 = _mm_srai_epi16(s0, 4);
505     s1 = _mm_srai_epi16(s1, 4);
506   } else {
507     s0 = _mm_srai_epi16(s0, 3);
508     s1 = _mm_srai_epi16(s1, 3);
509   }
510 
511   _mm_storeu_si128((__m128i *)hbuf, s0);
512   hbuf += 8;
513   _mm_storeu_si128((__m128i *)hbuf, s1);
514 }
515 
vpx_int_pro_col_sse2(const uint8_t * ref,const int width)516 int16_t vpx_int_pro_col_sse2(const uint8_t *ref, const int width) {
517   __m128i zero = _mm_setzero_si128();
518   __m128i src_line = _mm_loadu_si128((const __m128i *)ref);
519   __m128i s0 = _mm_sad_epu8(src_line, zero);
520   __m128i s1;
521   int i;
522 
523   for (i = 16; i < width; i += 16) {
524     ref += 16;
525     src_line = _mm_loadu_si128((const __m128i *)ref);
526     s1 = _mm_sad_epu8(src_line, zero);
527     s0 = _mm_adds_epu16(s0, s1);
528   }
529 
530   s1 = _mm_srli_si128(s0, 8);
531   s0 = _mm_adds_epu16(s0, s1);
532 
533   return _mm_extract_epi16(s0, 0);
534 }
535 
vpx_vector_var_sse2(const int16_t * ref,const int16_t * src,const int bwl)536 int vpx_vector_var_sse2(const int16_t *ref, const int16_t *src, const int bwl) {
537   int idx;
538   int width = 4 << bwl;
539   int16_t mean;
540   __m128i v0 = _mm_loadu_si128((const __m128i *)ref);
541   __m128i v1 = _mm_load_si128((const __m128i *)src);
542   __m128i diff = _mm_subs_epi16(v0, v1);
543   __m128i sum = diff;
544   __m128i sse = _mm_madd_epi16(diff, diff);
545 
546   ref += 8;
547   src += 8;
548 
549   for (idx = 8; idx < width; idx += 8) {
550     v0 = _mm_loadu_si128((const __m128i *)ref);
551     v1 = _mm_load_si128((const __m128i *)src);
552     diff = _mm_subs_epi16(v0, v1);
553 
554     sum = _mm_add_epi16(sum, diff);
555     v0 = _mm_madd_epi16(diff, diff);
556     sse = _mm_add_epi32(sse, v0);
557 
558     ref += 8;
559     src += 8;
560   }
561 
562   v0 = _mm_srli_si128(sum, 8);
563   sum = _mm_add_epi16(sum, v0);
564   v0 = _mm_srli_epi64(sum, 32);
565   sum = _mm_add_epi16(sum, v0);
566   v0 = _mm_srli_epi32(sum, 16);
567   sum = _mm_add_epi16(sum, v0);
568 
569   v1 = _mm_srli_si128(sse, 8);
570   sse = _mm_add_epi32(sse, v1);
571   v1 = _mm_srli_epi64(sse, 32);
572   sse = _mm_add_epi32(sse, v1);
573 
574   mean = (int16_t)_mm_extract_epi16(sum, 0);
575 
576   return _mm_cvtsi128_si32(sse) - ((mean * mean) >> (bwl + 2));
577 }
578