Coverage Report

Created: 2025-06-13 07:07

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