Coverage Report

Created: 2023-06-07 06:31

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