• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <emmintrin.h>
13 
14 #include "config/av1_rtcd.h"
15 
16 #include "aom_dsp/aom_dsp_common.h"
17 #include "aom_dsp/aom_filter.h"
18 #include "aom_dsp/x86/convolve_sse2.h"
19 #include "aom_dsp/x86/convolve_common_intrin.h"
20 #include "av1/common/convolve.h"
21 
av1_convolve_2d_sr_12tap_sse2(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int w,int h,const InterpFilterParams * filter_params_x,const InterpFilterParams * filter_params_y,const int subpel_x_qn,const int subpel_y_qn,ConvolveParams * conv_params)22 void av1_convolve_2d_sr_12tap_sse2(const uint8_t *src, int src_stride,
23                                    uint8_t *dst, int dst_stride, int w, int h,
24                                    const InterpFilterParams *filter_params_x,
25                                    const InterpFilterParams *filter_params_y,
26                                    const int subpel_x_qn, const int subpel_y_qn,
27                                    ConvolveParams *conv_params) {
28   const int bd = 8;
29 
30   DECLARE_ALIGNED(16, int16_t,
31                   im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE]);
32   int im_h = h + filter_params_y->taps - 1;
33   int im_stride = w;
34   int i, j;
35   const int fo_vert = filter_params_y->taps / 2 - 1;
36   const int fo_horiz = filter_params_x->taps / 2 - 1;
37   const uint8_t *const src_ptr = src - fo_vert * src_stride - fo_horiz;
38 
39   const __m128i zero = _mm_setzero_si128();
40   const int bits =
41       FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
42   const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
43 
44   assert(conv_params->round_0 > 0);
45   __m128i coeffs[6];
46 
47   /* Horizontal filter */
48   {
49     prepare_coeffs_12tap(filter_params_x, subpel_x_qn, coeffs);
50 
51     const __m128i round_const = _mm_set1_epi32(
52         (1 << (bd + FILTER_BITS - 1)) + ((1 << conv_params->round_0) >> 1));
53     const __m128i round_shift = _mm_cvtsi32_si128(conv_params->round_0);
54 
55     for (i = 0; i < im_h; ++i) {
56       for (j = 0; j < w; j += 8) {
57         const __m128i data =
58             _mm_loadu_si128((__m128i *)&src_ptr[i * src_stride + j]);
59         const __m128i data_2 =
60             _mm_loadu_si128((__m128i *)&src_ptr[i * src_stride + (j + 4)]);
61 
62         // Filter even-index pixels
63         const __m128i src_0 = _mm_unpacklo_epi8(data, zero);
64         const __m128i res_0 = _mm_madd_epi16(src_0, coeffs[0]);
65         const __m128i src_2 = _mm_unpacklo_epi8(_mm_srli_si128(data, 2), zero);
66         const __m128i res_2 = _mm_madd_epi16(src_2, coeffs[1]);
67         const __m128i src_4 = _mm_unpacklo_epi8(data_2, zero);
68         const __m128i res_4 = _mm_madd_epi16(src_4, coeffs[2]);
69         const __m128i src_6 =
70             _mm_unpacklo_epi8(_mm_srli_si128(data_2, 2), zero);
71         const __m128i res_6 = _mm_madd_epi16(src_6, coeffs[3]);
72         const __m128i src_8 =
73             _mm_unpacklo_epi8(_mm_srli_si128(data_2, 4), zero);
74         const __m128i res_8 = _mm_madd_epi16(src_8, coeffs[4]);
75         const __m128i src_10 =
76             _mm_unpacklo_epi8(_mm_srli_si128(data_2, 6), zero);
77         const __m128i res_10 = _mm_madd_epi16(src_10, coeffs[5]);
78 
79         const __m128i res_0246 = _mm_add_epi32(_mm_add_epi32(res_0, res_4),
80                                                _mm_add_epi32(res_2, res_6));
81         __m128i res_even =
82             _mm_add_epi32(_mm_add_epi32(res_8, res_10), res_0246);
83         res_even =
84             _mm_sra_epi32(_mm_add_epi32(res_even, round_const), round_shift);
85 
86         // Filter odd-index pixels
87         const __m128i src_1 = _mm_unpacklo_epi8(_mm_srli_si128(data, 1), zero);
88         const __m128i res_1 = _mm_madd_epi16(src_1, coeffs[0]);
89         const __m128i src_3 = _mm_unpacklo_epi8(_mm_srli_si128(data, 3), zero);
90         const __m128i res_3 = _mm_madd_epi16(src_3, coeffs[1]);
91         const __m128i src_5 =
92             _mm_unpacklo_epi8(_mm_srli_si128(data_2, 1), zero);
93         const __m128i res_5 = _mm_madd_epi16(src_5, coeffs[2]);
94         const __m128i src_7 =
95             _mm_unpacklo_epi8(_mm_srli_si128(data_2, 3), zero);
96         const __m128i res_7 = _mm_madd_epi16(src_7, coeffs[3]);
97         const __m128i src_9 =
98             _mm_unpacklo_epi8(_mm_srli_si128(data_2, 5), zero);
99         const __m128i res_9 = _mm_madd_epi16(src_9, coeffs[4]);
100         const __m128i src_11 =
101             _mm_unpacklo_epi8(_mm_srli_si128(data_2, 7), zero);
102         const __m128i res_11 = _mm_madd_epi16(src_11, coeffs[5]);
103 
104         const __m128i res_1357 = _mm_add_epi32(_mm_add_epi32(res_1, res_5),
105                                                _mm_add_epi32(res_3, res_7));
106         __m128i res_odd = _mm_add_epi32(_mm_add_epi32(res_9, res_11), res_1357);
107         res_odd =
108             _mm_sra_epi32(_mm_add_epi32(res_odd, round_const), round_shift);
109 
110         // Pack in the column order 0, 2, 4, 6, 1, 3, 5, 7
111         __m128i res = _mm_packs_epi32(res_even, res_odd);
112         _mm_storeu_si128((__m128i *)&im_block[i * im_stride + j], res);
113       }
114     }
115   }
116 
117   /* Vertical filter */
118   {
119     prepare_coeffs_12tap(filter_params_y, subpel_y_qn, coeffs);
120 
121     const __m128i sum_round =
122         _mm_set1_epi32((1 << offset_bits) + ((1 << conv_params->round_1) >> 1));
123     const __m128i sum_shift = _mm_cvtsi32_si128(conv_params->round_1);
124 
125     const __m128i round_const = _mm_set1_epi32(
126         ((1 << bits) >> 1) - (1 << (offset_bits - conv_params->round_1)) -
127         ((1 << (offset_bits - conv_params->round_1)) >> 1));
128     const __m128i round_shift = _mm_cvtsi32_si128(bits);
129 
130     for (i = 0; i < h; ++i) {
131       for (j = 0; j < w; j += 8) {
132         // Filter even-index pixels
133         const int16_t *data = &im_block[i * im_stride + j];
134         const __m128i src_0 =
135             _mm_unpacklo_epi16(*(__m128i *)(data + 0 * im_stride),
136                                *(__m128i *)(data + 1 * im_stride));
137         const __m128i src_2 =
138             _mm_unpacklo_epi16(*(__m128i *)(data + 2 * im_stride),
139                                *(__m128i *)(data + 3 * im_stride));
140         const __m128i src_4 =
141             _mm_unpacklo_epi16(*(__m128i *)(data + 4 * im_stride),
142                                *(__m128i *)(data + 5 * im_stride));
143         const __m128i src_6 =
144             _mm_unpacklo_epi16(*(__m128i *)(data + 6 * im_stride),
145                                *(__m128i *)(data + 7 * im_stride));
146         const __m128i src_8 =
147             _mm_unpacklo_epi16(*(__m128i *)(data + 8 * im_stride),
148                                *(__m128i *)(data + 9 * im_stride));
149         const __m128i src_10 =
150             _mm_unpacklo_epi16(*(__m128i *)(data + 10 * im_stride),
151                                *(__m128i *)(data + 11 * im_stride));
152 
153         const __m128i res_0 = _mm_madd_epi16(src_0, coeffs[0]);
154         const __m128i res_2 = _mm_madd_epi16(src_2, coeffs[1]);
155         const __m128i res_4 = _mm_madd_epi16(src_4, coeffs[2]);
156         const __m128i res_6 = _mm_madd_epi16(src_6, coeffs[3]);
157         const __m128i res_8 = _mm_madd_epi16(src_8, coeffs[4]);
158         const __m128i res_10 = _mm_madd_epi16(src_10, coeffs[5]);
159 
160         const __m128i res_0246 = _mm_add_epi32(_mm_add_epi32(res_0, res_2),
161                                                _mm_add_epi32(res_4, res_6));
162         __m128i res_even =
163             _mm_add_epi32(_mm_add_epi32(res_8, res_10), res_0246);
164 
165         // Filter odd-index pixels
166         const __m128i src_1 =
167             _mm_unpackhi_epi16(*(__m128i *)(data + 0 * im_stride),
168                                *(__m128i *)(data + 1 * im_stride));
169         const __m128i src_3 =
170             _mm_unpackhi_epi16(*(__m128i *)(data + 2 * im_stride),
171                                *(__m128i *)(data + 3 * im_stride));
172         const __m128i src_5 =
173             _mm_unpackhi_epi16(*(__m128i *)(data + 4 * im_stride),
174                                *(__m128i *)(data + 5 * im_stride));
175         const __m128i src_7 =
176             _mm_unpackhi_epi16(*(__m128i *)(data + 6 * im_stride),
177                                *(__m128i *)(data + 7 * im_stride));
178         const __m128i src_9 =
179             _mm_unpackhi_epi16(*(__m128i *)(data + 8 * im_stride),
180                                *(__m128i *)(data + 9 * im_stride));
181         const __m128i src_11 =
182             _mm_unpackhi_epi16(*(__m128i *)(data + 10 * im_stride),
183                                *(__m128i *)(data + 11 * im_stride));
184 
185         const __m128i res_1 = _mm_madd_epi16(src_1, coeffs[0]);
186         const __m128i res_3 = _mm_madd_epi16(src_3, coeffs[1]);
187         const __m128i res_5 = _mm_madd_epi16(src_5, coeffs[2]);
188         const __m128i res_7 = _mm_madd_epi16(src_7, coeffs[3]);
189         const __m128i res_9 = _mm_madd_epi16(src_9, coeffs[4]);
190         const __m128i res_11 = _mm_madd_epi16(src_11, coeffs[5]);
191 
192         const __m128i res_1357 = _mm_add_epi32(_mm_add_epi32(res_1, res_5),
193                                                _mm_add_epi32(res_3, res_7));
194         __m128i res_odd = _mm_add_epi32(_mm_add_epi32(res_9, res_11), res_1357);
195 
196         // Rearrange pixels back into the order 0 ... 7
197         const __m128i res_lo = _mm_unpacklo_epi32(res_even, res_odd);
198         const __m128i res_hi = _mm_unpackhi_epi32(res_even, res_odd);
199 
200         __m128i res_lo_round =
201             _mm_sra_epi32(_mm_add_epi32(res_lo, sum_round), sum_shift);
202         __m128i res_hi_round =
203             _mm_sra_epi32(_mm_add_epi32(res_hi, sum_round), sum_shift);
204 
205         res_lo_round = _mm_sra_epi32(_mm_add_epi32(res_lo_round, round_const),
206                                      round_shift);
207         res_hi_round = _mm_sra_epi32(_mm_add_epi32(res_hi_round, round_const),
208                                      round_shift);
209 
210         const __m128i res16 = _mm_packs_epi32(res_lo_round, res_hi_round);
211         const __m128i res = _mm_packus_epi16(res16, res16);
212 
213         // Accumulate values into the destination buffer
214         __m128i *const p = (__m128i *)&dst[i * dst_stride + j];
215 
216         _mm_storel_epi64(p, res);
217       }
218     }
219   }
220 }
221 
av1_convolve_2d_sr_sse2(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int w,int h,const InterpFilterParams * filter_params_x,const InterpFilterParams * filter_params_y,const int subpel_x_qn,const int subpel_y_qn,ConvolveParams * conv_params)222 void av1_convolve_2d_sr_sse2(const uint8_t *src, int src_stride, uint8_t *dst,
223                              int dst_stride, int w, int h,
224                              const InterpFilterParams *filter_params_x,
225                              const InterpFilterParams *filter_params_y,
226                              const int subpel_x_qn, const int subpel_y_qn,
227                              ConvolveParams *conv_params) {
228   if (filter_params_x->taps > 8) {
229     if (w < 8) {
230       av1_convolve_2d_sr_c(src, src_stride, dst, dst_stride, w, h,
231                            filter_params_x, filter_params_y, subpel_x_qn,
232                            subpel_y_qn, conv_params);
233     } else {
234       av1_convolve_2d_sr_12tap_sse2(src, src_stride, dst, dst_stride, w, h,
235                                     filter_params_x, filter_params_y,
236                                     subpel_x_qn, subpel_y_qn, conv_params);
237     }
238   } else {
239     const int bd = 8;
240 
241     DECLARE_ALIGNED(16, int16_t,
242                     im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE]);
243     int im_h = h + filter_params_y->taps - 1;
244     int im_stride = MAX_SB_SIZE;
245     int i, j;
246     const int fo_vert = filter_params_y->taps / 2 - 1;
247     const int fo_horiz = filter_params_x->taps / 2 - 1;
248     const uint8_t *const src_ptr = src - fo_vert * src_stride - fo_horiz;
249 
250     const __m128i zero = _mm_setzero_si128();
251     const int bits =
252         FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
253     const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
254 
255     assert(conv_params->round_0 > 0);
256 
257     /* Horizontal filter */
258     {
259       const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
260           filter_params_x, subpel_x_qn & SUBPEL_MASK);
261       const __m128i coeffs_x = _mm_loadu_si128((__m128i *)x_filter);
262 
263       // coeffs 0 1 0 1 2 3 2 3
264       const __m128i tmp_0 = _mm_unpacklo_epi32(coeffs_x, coeffs_x);
265       // coeffs 4 5 4 5 6 7 6 7
266       const __m128i tmp_1 = _mm_unpackhi_epi32(coeffs_x, coeffs_x);
267 
268       // coeffs 0 1 0 1 0 1 0 1
269       const __m128i coeff_01 = _mm_unpacklo_epi64(tmp_0, tmp_0);
270       // coeffs 2 3 2 3 2 3 2 3
271       const __m128i coeff_23 = _mm_unpackhi_epi64(tmp_0, tmp_0);
272       // coeffs 4 5 4 5 4 5 4 5
273       const __m128i coeff_45 = _mm_unpacklo_epi64(tmp_1, tmp_1);
274       // coeffs 6 7 6 7 6 7 6 7
275       const __m128i coeff_67 = _mm_unpackhi_epi64(tmp_1, tmp_1);
276 
277       const __m128i round_const = _mm_set1_epi32(
278           (1 << (bd + FILTER_BITS - 1)) + ((1 << conv_params->round_0) >> 1));
279       const __m128i round_shift = _mm_cvtsi32_si128(conv_params->round_0);
280 
281       for (i = 0; i < im_h; ++i) {
282         for (j = 0; j < w; j += 8) {
283           const __m128i data =
284               _mm_loadu_si128((__m128i *)&src_ptr[i * src_stride + j]);
285 
286           // Filter even-index pixels
287           const __m128i src_0 = _mm_unpacklo_epi8(data, zero);
288           const __m128i res_0 = _mm_madd_epi16(src_0, coeff_01);
289           const __m128i src_2 =
290               _mm_unpacklo_epi8(_mm_srli_si128(data, 2), zero);
291           const __m128i res_2 = _mm_madd_epi16(src_2, coeff_23);
292           const __m128i src_4 =
293               _mm_unpacklo_epi8(_mm_srli_si128(data, 4), zero);
294           const __m128i res_4 = _mm_madd_epi16(src_4, coeff_45);
295           const __m128i src_6 =
296               _mm_unpacklo_epi8(_mm_srli_si128(data, 6), zero);
297           const __m128i res_6 = _mm_madd_epi16(src_6, coeff_67);
298 
299           __m128i res_even = _mm_add_epi32(_mm_add_epi32(res_0, res_4),
300                                            _mm_add_epi32(res_2, res_6));
301           res_even =
302               _mm_sra_epi32(_mm_add_epi32(res_even, round_const), round_shift);
303 
304           // Filter odd-index pixels
305           const __m128i src_1 =
306               _mm_unpacklo_epi8(_mm_srli_si128(data, 1), zero);
307           const __m128i res_1 = _mm_madd_epi16(src_1, coeff_01);
308           const __m128i src_3 =
309               _mm_unpacklo_epi8(_mm_srli_si128(data, 3), zero);
310           const __m128i res_3 = _mm_madd_epi16(src_3, coeff_23);
311           const __m128i src_5 =
312               _mm_unpacklo_epi8(_mm_srli_si128(data, 5), zero);
313           const __m128i res_5 = _mm_madd_epi16(src_5, coeff_45);
314           const __m128i src_7 =
315               _mm_unpacklo_epi8(_mm_srli_si128(data, 7), zero);
316           const __m128i res_7 = _mm_madd_epi16(src_7, coeff_67);
317 
318           __m128i res_odd = _mm_add_epi32(_mm_add_epi32(res_1, res_5),
319                                           _mm_add_epi32(res_3, res_7));
320           res_odd =
321               _mm_sra_epi32(_mm_add_epi32(res_odd, round_const), round_shift);
322 
323           // Pack in the column order 0, 2, 4, 6, 1, 3, 5, 7
324           __m128i res = _mm_packs_epi32(res_even, res_odd);
325           _mm_storeu_si128((__m128i *)&im_block[i * im_stride + j], res);
326         }
327       }
328     }
329 
330     /* Vertical filter */
331     {
332       const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
333           filter_params_y, subpel_y_qn & SUBPEL_MASK);
334       const __m128i coeffs_y = _mm_loadu_si128((__m128i *)y_filter);
335 
336       // coeffs 0 1 0 1 2 3 2 3
337       const __m128i tmp_0 = _mm_unpacklo_epi32(coeffs_y, coeffs_y);
338       // coeffs 4 5 4 5 6 7 6 7
339       const __m128i tmp_1 = _mm_unpackhi_epi32(coeffs_y, coeffs_y);
340 
341       // coeffs 0 1 0 1 0 1 0 1
342       const __m128i coeff_01 = _mm_unpacklo_epi64(tmp_0, tmp_0);
343       // coeffs 2 3 2 3 2 3 2 3
344       const __m128i coeff_23 = _mm_unpackhi_epi64(tmp_0, tmp_0);
345       // coeffs 4 5 4 5 4 5 4 5
346       const __m128i coeff_45 = _mm_unpacklo_epi64(tmp_1, tmp_1);
347       // coeffs 6 7 6 7 6 7 6 7
348       const __m128i coeff_67 = _mm_unpackhi_epi64(tmp_1, tmp_1);
349 
350       const __m128i sum_round = _mm_set1_epi32(
351           (1 << offset_bits) + ((1 << conv_params->round_1) >> 1));
352       const __m128i sum_shift = _mm_cvtsi32_si128(conv_params->round_1);
353 
354       const __m128i round_const = _mm_set1_epi32(
355           ((1 << bits) >> 1) - (1 << (offset_bits - conv_params->round_1)) -
356           ((1 << (offset_bits - conv_params->round_1)) >> 1));
357       const __m128i round_shift = _mm_cvtsi32_si128(bits);
358 
359       for (i = 0; i < h; ++i) {
360         for (j = 0; j < w; j += 8) {
361           // Filter even-index pixels
362           const int16_t *data = &im_block[i * im_stride + j];
363           const __m128i src_0 =
364               _mm_unpacklo_epi16(*(__m128i *)(data + 0 * im_stride),
365                                  *(__m128i *)(data + 1 * im_stride));
366           const __m128i src_2 =
367               _mm_unpacklo_epi16(*(__m128i *)(data + 2 * im_stride),
368                                  *(__m128i *)(data + 3 * im_stride));
369           const __m128i src_4 =
370               _mm_unpacklo_epi16(*(__m128i *)(data + 4 * im_stride),
371                                  *(__m128i *)(data + 5 * im_stride));
372           const __m128i src_6 =
373               _mm_unpacklo_epi16(*(__m128i *)(data + 6 * im_stride),
374                                  *(__m128i *)(data + 7 * im_stride));
375 
376           const __m128i res_0 = _mm_madd_epi16(src_0, coeff_01);
377           const __m128i res_2 = _mm_madd_epi16(src_2, coeff_23);
378           const __m128i res_4 = _mm_madd_epi16(src_4, coeff_45);
379           const __m128i res_6 = _mm_madd_epi16(src_6, coeff_67);
380 
381           const __m128i res_even = _mm_add_epi32(_mm_add_epi32(res_0, res_2),
382                                                  _mm_add_epi32(res_4, res_6));
383 
384           // Filter odd-index pixels
385           const __m128i src_1 =
386               _mm_unpackhi_epi16(*(__m128i *)(data + 0 * im_stride),
387                                  *(__m128i *)(data + 1 * im_stride));
388           const __m128i src_3 =
389               _mm_unpackhi_epi16(*(__m128i *)(data + 2 * im_stride),
390                                  *(__m128i *)(data + 3 * im_stride));
391           const __m128i src_5 =
392               _mm_unpackhi_epi16(*(__m128i *)(data + 4 * im_stride),
393                                  *(__m128i *)(data + 5 * im_stride));
394           const __m128i src_7 =
395               _mm_unpackhi_epi16(*(__m128i *)(data + 6 * im_stride),
396                                  *(__m128i *)(data + 7 * im_stride));
397 
398           const __m128i res_1 = _mm_madd_epi16(src_1, coeff_01);
399           const __m128i res_3 = _mm_madd_epi16(src_3, coeff_23);
400           const __m128i res_5 = _mm_madd_epi16(src_5, coeff_45);
401           const __m128i res_7 = _mm_madd_epi16(src_7, coeff_67);
402 
403           const __m128i res_odd = _mm_add_epi32(_mm_add_epi32(res_1, res_3),
404                                                 _mm_add_epi32(res_5, res_7));
405 
406           // Rearrange pixels back into the order 0 ... 7
407           const __m128i res_lo = _mm_unpacklo_epi32(res_even, res_odd);
408           const __m128i res_hi = _mm_unpackhi_epi32(res_even, res_odd);
409 
410           __m128i res_lo_round =
411               _mm_sra_epi32(_mm_add_epi32(res_lo, sum_round), sum_shift);
412           __m128i res_hi_round =
413               _mm_sra_epi32(_mm_add_epi32(res_hi, sum_round), sum_shift);
414 
415           res_lo_round = _mm_sra_epi32(_mm_add_epi32(res_lo_round, round_const),
416                                        round_shift);
417           res_hi_round = _mm_sra_epi32(_mm_add_epi32(res_hi_round, round_const),
418                                        round_shift);
419 
420           const __m128i res16 = _mm_packs_epi32(res_lo_round, res_hi_round);
421           const __m128i res = _mm_packus_epi16(res16, res16);
422 
423           // Accumulate values into the destination buffer
424           __m128i *const p = (__m128i *)&dst[i * dst_stride + j];
425 
426           if (w == 2) {
427             *(uint16_t *)p = (uint16_t)_mm_cvtsi128_si32(res);
428           } else if (w == 4) {
429             *(int *)p = _mm_cvtsi128_si32(res);
430           } else {
431             _mm_storel_epi64(p, res);
432           }
433         }
434       }
435     }
436   }
437 }
438 
av1_dist_wtd_convolve_2d_copy_sse2(const uint8_t * src,int src_stride,uint8_t * dst0,int dst_stride0,int w,int h,ConvolveParams * conv_params)439 void av1_dist_wtd_convolve_2d_copy_sse2(const uint8_t *src, int src_stride,
440                                         uint8_t *dst0, int dst_stride0, int w,
441                                         int h, ConvolveParams *conv_params) {
442   const int bd = 8;
443   CONV_BUF_TYPE *dst = conv_params->dst;
444   int dst_stride = conv_params->dst_stride;
445 
446   const int bits =
447       FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
448   const int do_average = conv_params->do_average;
449   const int use_dist_wtd_comp_avg = conv_params->use_dist_wtd_comp_avg;
450   const __m128i zero = _mm_setzero_si128();
451   const __m128i left_shift = _mm_cvtsi32_si128(bits);
452   int i, j;
453 
454   const int w0 = conv_params->fwd_offset;
455   const int w1 = conv_params->bck_offset;
456   const __m128i wt0 = _mm_set1_epi16(w0);
457   const __m128i wt1 = _mm_set1_epi16(w1);
458   const __m128i wt = _mm_unpacklo_epi16(wt0, wt1);
459 
460   const int offset_0 =
461       bd + 2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
462   const int offset = (1 << offset_0) + (1 << (offset_0 - 1));
463   const __m128i offset_const = _mm_set1_epi16(offset);
464   const int rounding_shift =
465       2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
466   const __m128i rounding_const = _mm_set1_epi16((1 << rounding_shift) >> 1);
467 
468   assert((w % 4) == 0);
469 
470   if (!(w % 16)) {
471     for (i = 0; i < h; ++i) {
472       for (j = 0; j < w; j += 16) {
473         const __m128i d8 = _mm_loadu_si128((__m128i *)&src[j]);
474 
475         const __m128i d16_lo = _mm_unpacklo_epi8(d8, zero);
476         const __m128i d16_hi = _mm_unpackhi_epi8(d8, zero);
477 
478         const __m128i res_lo = _mm_sll_epi16(d16_lo, left_shift);
479         const __m128i res_unsigned_lo = _mm_add_epi16(res_lo, offset_const);
480 
481         const __m128i res_hi = _mm_sll_epi16(d16_hi, left_shift);
482         const __m128i res_unsigned_hi = _mm_add_epi16(res_hi, offset_const);
483 
484         if (do_average) {
485           const __m128i data_ref_0_lo = _mm_loadu_si128((__m128i *)(&dst[j]));
486           const __m128i data_ref_0_hi =
487               _mm_loadu_si128((__m128i *)(&dst[j + 8]));
488 
489           const __m128i comp_avg_res_lo = comp_avg(
490               &data_ref_0_lo, &res_unsigned_lo, &wt, use_dist_wtd_comp_avg);
491 
492           const __m128i round_result_lo = convolve_rounding(
493               &comp_avg_res_lo, &offset_const, &rounding_const, rounding_shift);
494 
495           const __m128i comp_avg_res_hi = comp_avg(
496               &data_ref_0_hi, &res_unsigned_hi, &wt, use_dist_wtd_comp_avg);
497 
498           const __m128i round_result_hi = convolve_rounding(
499               &comp_avg_res_hi, &offset_const, &rounding_const, rounding_shift);
500 
501           const __m128i res_8 =
502               _mm_packus_epi16(round_result_lo, round_result_hi);
503 
504           _mm_store_si128((__m128i *)(&dst0[j]), res_8);
505         } else {
506           _mm_store_si128((__m128i *)(&dst[j]), res_unsigned_lo);
507           _mm_store_si128((__m128i *)(&dst[j + 8]), res_unsigned_hi);
508         }
509       }
510       src += src_stride;
511       dst += dst_stride;
512       dst0 += dst_stride0;
513     }
514   } else {
515     for (i = 0; i < h; ++i) {
516       for (j = 0; j < w; j += 8) {
517         const __m128i d8 = _mm_loadl_epi64((__m128i *)&src[j]);
518         const __m128i d16_0 = _mm_unpacklo_epi8(d8, zero);
519 
520         const __m128i res = _mm_sll_epi16(d16_0, left_shift);
521         const __m128i res_unsigned = _mm_add_epi16(res, offset_const);
522 
523         if (do_average) {
524           const __m128i data_ref_0 = _mm_loadu_si128((__m128i *)(&dst[j]));
525 
526           const __m128i comp_avg_res =
527               comp_avg(&data_ref_0, &res_unsigned, &wt, use_dist_wtd_comp_avg);
528 
529           const __m128i round_result = convolve_rounding(
530               &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
531 
532           const __m128i res_8 = _mm_packus_epi16(round_result, round_result);
533 
534           if (w > 4)
535             _mm_storel_epi64((__m128i *)(&dst0[j]), res_8);
536           else
537             *(int *)(&dst0[j]) = _mm_cvtsi128_si32(res_8);
538         } else {
539           _mm_store_si128((__m128i *)(&dst[j]), res_unsigned);
540         }
541       }
542       src += src_stride;
543       dst += dst_stride;
544       dst0 += dst_stride0;
545     }
546   }
547 }
548