Coverage Report

Created: 2025-07-23 06:32

/src/aom/av1/common/x86/jnt_convolve_avx2.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2018, 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
#include <immintrin.h>
14
15
#include "config/av1_rtcd.h"
16
17
#include "aom_dsp/aom_dsp_common.h"
18
#include "aom_dsp/aom_filter.h"
19
#include "aom_dsp/x86/convolve_avx2.h"
20
#include "aom_dsp/x86/convolve_common_intrin.h"
21
#include "aom_dsp/x86/convolve_sse4_1.h"
22
#include "aom_dsp/x86/mem_sse2.h"
23
#include "aom_dsp/x86/synonyms_avx2.h"
24
25
#include "av1/common/convolve.h"
26
27
1.36M
static inline __m256i unpack_weights_avx2(ConvolveParams *conv_params) {
28
1.36M
  const int w0 = conv_params->fwd_offset;
29
1.36M
  const int w1 = conv_params->bck_offset;
30
1.36M
  const __m256i wt0 = _mm256_set1_epi16((int16_t)w0);
31
1.36M
  const __m256i wt1 = _mm256_set1_epi16((int16_t)w1);
32
1.36M
  const __m256i wt = _mm256_unpacklo_epi16(wt0, wt1);
33
1.36M
  return wt;
34
1.36M
}
35
36
8.52M
static inline __m256i load_line2_avx2(const void *a, const void *b) {
37
8.52M
  return _mm256_permute2x128_si256(
38
8.52M
      _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)a)),
39
8.52M
      _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)b)), 0x20);
40
8.52M
}
41
42
void av1_dist_wtd_convolve_x_avx2(const uint8_t *src, int src_stride,
43
                                  uint8_t *dst0, int dst_stride0, int w, int h,
44
                                  const InterpFilterParams *filter_params_x,
45
                                  const int subpel_x_qn,
46
137k
                                  ConvolveParams *conv_params) {
47
137k
  CONV_BUF_TYPE *dst = conv_params->dst;
48
137k
  int dst_stride = conv_params->dst_stride;
49
137k
  const int bd = 8;
50
137k
  int i, j, is_horiz_4tap = 0;
51
137k
  const int bits = FILTER_BITS - conv_params->round_1;
52
137k
  const __m256i wt = unpack_weights_avx2(conv_params);
53
137k
  const int do_average = conv_params->do_average;
54
137k
  const int use_dist_wtd_comp_avg = conv_params->use_dist_wtd_comp_avg;
55
137k
  const int offset_0 =
56
137k
      bd + 2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
57
137k
  const int offset = (1 << offset_0) + (1 << (offset_0 - 1));
58
137k
  const __m256i offset_const = _mm256_set1_epi16(offset);
59
137k
  const int rounding_shift =
60
137k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
61
137k
  const __m256i rounding_const = _mm256_set1_epi16((1 << rounding_shift) >> 1);
62
63
137k
  assert(bits >= 0);
64
137k
  assert(conv_params->round_0 > 0);
65
66
137k
  const __m256i round_const =
67
137k
      _mm256_set1_epi16((1 << (conv_params->round_0 - 1)) >> 1);
68
137k
  const __m128i round_shift = _mm_cvtsi32_si128(conv_params->round_0 - 1);
69
70
137k
  __m256i filt[4], coeffs[4];
71
72
137k
  filt[0] = _mm256_load_si256((__m256i const *)filt_global_avx2);
73
137k
  filt[1] = _mm256_load_si256((__m256i const *)(filt_global_avx2 + 32));
74
75
137k
  prepare_coeffs_lowbd(filter_params_x, subpel_x_qn, coeffs);
76
77
  // Condition for checking valid horz_filt taps
78
137k
  if (!(_mm256_extract_epi32(_mm256_or_si256(coeffs[0], coeffs[3]), 0)))
79
63.5k
    is_horiz_4tap = 1;
80
81
  // horz_filt as 4 tap
82
137k
  if (is_horiz_4tap) {
83
63.5k
    const int fo_horiz = 1;
84
63.5k
    const uint8_t *const src_ptr = src - fo_horiz;
85
515k
    for (i = 0; i < h; i += 2) {
86
451k
      const uint8_t *src_data = src_ptr + i * src_stride;
87
451k
      CONV_BUF_TYPE *dst_data = dst + i * dst_stride;
88
2.41M
      for (j = 0; j < w; j += 8) {
89
1.96M
        const __m256i data =
90
1.96M
            load_line2_avx2(&src_data[j], &src_data[j + src_stride]);
91
92
1.96M
        __m256i res = convolve_lowbd_x_4tap(data, coeffs + 1, filt);
93
1.96M
        res = _mm256_sra_epi16(_mm256_add_epi16(res, round_const), round_shift);
94
1.96M
        res = _mm256_slli_epi16(res, bits);
95
96
1.96M
        const __m256i res_unsigned = _mm256_add_epi16(res, offset_const);
97
98
        // Accumulate values into the destination buffer
99
1.96M
        if (do_average) {
100
571k
          const __m256i data_ref_0 =
101
571k
              load_line2_avx2(&dst_data[j], &dst_data[j + dst_stride]);
102
571k
          const __m256i comp_avg_res =
103
571k
              comp_avg(&data_ref_0, &res_unsigned, &wt, use_dist_wtd_comp_avg);
104
105
571k
          const __m256i round_result = convolve_rounding(
106
571k
              &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
107
108
571k
          const __m256i res_8 = _mm256_packus_epi16(round_result, round_result);
109
571k
          const __m128i res_0 = _mm256_castsi256_si128(res_8);
110
571k
          const __m128i res_1 = _mm256_extracti128_si256(res_8, 1);
111
112
571k
          if (w > 4) {
113
538k
            _mm_storel_epi64((__m128i *)(&dst0[i * dst_stride0 + j]), res_0);
114
538k
            _mm_storel_epi64(
115
538k
                (__m128i *)((&dst0[i * dst_stride0 + j + dst_stride0])), res_1);
116
538k
          } else {
117
33.3k
            *(int *)(&dst0[i * dst_stride0 + j]) = _mm_cvtsi128_si32(res_0);
118
33.3k
            *(int *)(&dst0[i * dst_stride0 + j + dst_stride0]) =
119
33.3k
                _mm_cvtsi128_si32(res_1);
120
33.3k
          }
121
1.39M
        } else {
122
1.39M
          const __m128i res_0 = _mm256_castsi256_si128(res_unsigned);
123
1.39M
          _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_0);
124
125
1.39M
          const __m128i res_1 = _mm256_extracti128_si256(res_unsigned, 1);
126
1.39M
          _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + dst_stride]),
127
1.39M
                          res_1);
128
1.39M
        }
129
1.96M
      }
130
451k
    }
131
73.7k
  } else {
132
73.7k
    const int fo_horiz = filter_params_x->taps / 2 - 1;
133
73.7k
    const uint8_t *const src_ptr = src - fo_horiz;
134
135
73.7k
    filt[2] = _mm256_load_si256((__m256i const *)(filt_global_avx2 + 32 * 2));
136
73.7k
    filt[3] = _mm256_load_si256((__m256i const *)(filt_global_avx2 + 32 * 3));
137
683k
    for (i = 0; i < h; i += 2) {
138
609k
      const uint8_t *src_data = src_ptr + i * src_stride;
139
609k
      CONV_BUF_TYPE *dst_data = dst + i * dst_stride;
140
3.20M
      for (j = 0; j < w; j += 8) {
141
2.59M
        const __m256i data =
142
2.59M
            load_line2_avx2(&src_data[j], &src_data[j + src_stride]);
143
144
2.59M
        __m256i res = convolve_lowbd_x(data, coeffs, filt);
145
146
2.59M
        res = _mm256_sra_epi16(_mm256_add_epi16(res, round_const), round_shift);
147
148
2.59M
        res = _mm256_slli_epi16(res, bits);
149
150
2.59M
        const __m256i res_unsigned = _mm256_add_epi16(res, offset_const);
151
152
        // Accumulate values into the destination buffer
153
2.59M
        if (do_average) {
154
1.26M
          const __m256i data_ref_0 =
155
1.26M
              load_line2_avx2(&dst_data[j], &dst_data[j + dst_stride]);
156
1.26M
          const __m256i comp_avg_res =
157
1.26M
              comp_avg(&data_ref_0, &res_unsigned, &wt, use_dist_wtd_comp_avg);
158
159
1.26M
          const __m256i round_result = convolve_rounding(
160
1.26M
              &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
161
162
1.26M
          const __m256i res_8 = _mm256_packus_epi16(round_result, round_result);
163
1.26M
          const __m128i res_0 = _mm256_castsi256_si128(res_8);
164
1.26M
          const __m128i res_1 = _mm256_extracti128_si256(res_8, 1);
165
166
1.26M
          if (w > 4) {
167
1.26M
            _mm_storel_epi64((__m128i *)(&dst0[i * dst_stride0 + j]), res_0);
168
1.26M
            _mm_storel_epi64(
169
1.26M
                (__m128i *)((&dst0[i * dst_stride0 + j + dst_stride0])), res_1);
170
1.26M
          } else {
171
2.69k
            *(int *)(&dst0[i * dst_stride0 + j]) = _mm_cvtsi128_si32(res_0);
172
2.69k
            *(int *)(&dst0[i * dst_stride0 + j + dst_stride0]) =
173
2.69k
                _mm_cvtsi128_si32(res_1);
174
2.69k
          }
175
1.33M
        } else {
176
1.33M
          const __m128i res_0 = _mm256_castsi256_si128(res_unsigned);
177
1.33M
          _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_0);
178
179
1.33M
          const __m128i res_1 = _mm256_extracti128_si256(res_unsigned, 1);
180
1.33M
          _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + dst_stride]),
181
1.33M
                          res_1);
182
1.33M
        }
183
2.59M
      }
184
609k
    }
185
73.7k
  }
186
137k
}
187
188
void av1_dist_wtd_convolve_y_avx2(const uint8_t *src, int src_stride,
189
                                  uint8_t *dst0, int dst_stride0, int w, int h,
190
                                  const InterpFilterParams *filter_params_y,
191
                                  const int subpel_y_qn,
192
76.6k
                                  ConvolveParams *conv_params) {
193
76.6k
  CONV_BUF_TYPE *dst = conv_params->dst;
194
76.6k
  int dst_stride = conv_params->dst_stride;
195
76.6k
  const int bd = 8;
196
76.6k
  int i, j, is_vert_4tap = 0;
197
  // +1 to compensate for dividing the filter coeffs by 2
198
76.6k
  const int left_shift = FILTER_BITS - conv_params->round_0 + 1;
199
76.6k
  const __m256i round_const =
200
76.6k
      _mm256_set1_epi32((1 << conv_params->round_1) >> 1);
201
76.6k
  const __m128i round_shift = _mm_cvtsi32_si128(conv_params->round_1);
202
76.6k
  const __m256i wt = unpack_weights_avx2(conv_params);
203
76.6k
  const int do_average = conv_params->do_average;
204
76.6k
  const int use_dist_wtd_comp_avg = conv_params->use_dist_wtd_comp_avg;
205
76.6k
  const int offset_0 =
206
76.6k
      bd + 2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
207
76.6k
  const int offset = (1 << offset_0) + (1 << (offset_0 - 1));
208
76.6k
  const __m256i offset_const = _mm256_set1_epi16(offset);
209
76.6k
  const int offset_1 = (1 << (bd + FILTER_BITS - 2));
210
76.6k
  const __m256i offset_const_1 = _mm256_set1_epi16(offset_1);
211
76.6k
  const __m256i offset_const_2 = _mm256_set1_epi16((1 << offset_0));
212
76.6k
  const int rounding_shift =
213
76.6k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
214
76.6k
  const __m256i rounding_const = _mm256_set1_epi16((1 << rounding_shift) >> 1);
215
76.6k
  const __m256i zero = _mm256_setzero_si256();
216
76.6k
  __m256i coeffs[4], s[8];
217
218
76.6k
  assert((FILTER_BITS - conv_params->round_0) >= 0);
219
220
76.6k
  prepare_coeffs_lowbd(filter_params_y, subpel_y_qn, coeffs);
221
222
  // Condition for checking valid vert_filt taps
223
76.6k
  if (!(_mm256_extract_epi32(_mm256_or_si256(coeffs[0], coeffs[3]), 0)))
224
40.0k
    is_vert_4tap = 1;
225
226
76.6k
  if (is_vert_4tap) {
227
40.0k
    const int fo_vert = 1;
228
40.0k
    const uint8_t *const src_ptr = src - fo_vert * src_stride;
229
81.9k
    for (j = 0; j < w; j += 16) {
230
41.9k
      const uint8_t *data = &src_ptr[j];
231
41.9k
      __m256i src4;
232
      // Load lines a and b. Line a to lower 128, line b to upper 128
233
41.9k
      {
234
41.9k
        __m256i src_ab[4];
235
41.9k
        __m256i src_a[5];
236
41.9k
        src_a[0] = _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)data));
237
209k
        for (int kk = 0; kk < 4; ++kk) {
238
167k
          data += src_stride;
239
167k
          src_a[kk + 1] =
240
167k
              _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)data));
241
167k
          src_ab[kk] =
242
167k
              _mm256_permute2x128_si256(src_a[kk], src_a[kk + 1], 0x20);
243
167k
        }
244
41.9k
        src4 = src_a[4];
245
41.9k
        s[0] = _mm256_unpacklo_epi8(src_ab[0], src_ab[1]);
246
41.9k
        s[1] = _mm256_unpacklo_epi8(src_ab[2], src_ab[3]);
247
248
41.9k
        s[3] = _mm256_unpackhi_epi8(src_ab[0], src_ab[1]);
249
41.9k
        s[4] = _mm256_unpackhi_epi8(src_ab[2], src_ab[3]);
250
41.9k
      }
251
252
239k
      for (i = 0; i < h; i += 2) {
253
197k
        data = &src_ptr[(i + 5) * src_stride + j];
254
197k
        const __m256i src5 =
255
197k
            _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)data));
256
197k
        const __m256i src_45a = _mm256_permute2x128_si256(src4, src5, 0x20);
257
258
197k
        src4 = _mm256_castsi128_si256(
259
197k
            _mm_loadu_si128((__m128i *)(data + src_stride)));
260
197k
        const __m256i src_56a = _mm256_permute2x128_si256(src5, src4, 0x20);
261
262
197k
        s[2] = _mm256_unpacklo_epi8(src_45a, src_56a);
263
197k
        s[5] = _mm256_unpackhi_epi8(src_45a, src_56a);
264
265
197k
        __m256i res_lo = convolve_lowbd_4tap(s, coeffs + 1);
266
267
197k
        res_lo = _mm256_add_epi16(res_lo, offset_const_1);
268
269
197k
        const __m256i res_lo_0_32b = _mm256_unpacklo_epi16(res_lo, zero);
270
197k
        const __m256i res_lo_0_shift =
271
197k
            _mm256_slli_epi32(res_lo_0_32b, left_shift);
272
197k
        const __m256i res_lo_0_round = _mm256_sra_epi32(
273
197k
            _mm256_add_epi32(res_lo_0_shift, round_const), round_shift);
274
275
197k
        const __m256i res_lo_1_32b = _mm256_unpackhi_epi16(res_lo, zero);
276
197k
        const __m256i res_lo_1_shift =
277
197k
            _mm256_slli_epi32(res_lo_1_32b, left_shift);
278
197k
        const __m256i res_lo_1_round = _mm256_sra_epi32(
279
197k
            _mm256_add_epi32(res_lo_1_shift, round_const), round_shift);
280
281
197k
        const __m256i res_lo_round =
282
197k
            _mm256_packs_epi32(res_lo_0_round, res_lo_1_round);
283
284
197k
        const __m256i res_lo_unsigned =
285
197k
            _mm256_add_epi16(res_lo_round, offset_const_2);
286
287
197k
        if (w - j < 16) {
288
109k
          if (do_average) {
289
74.5k
            const __m256i data_ref_0 =
290
74.5k
                load_line2_avx2(&dst[i * dst_stride + j],
291
74.5k
                                &dst[i * dst_stride + j + dst_stride]);
292
74.5k
            const __m256i comp_avg_res = comp_avg(&data_ref_0, &res_lo_unsigned,
293
74.5k
                                                  &wt, use_dist_wtd_comp_avg);
294
295
74.5k
            const __m256i round_result = convolve_rounding(
296
74.5k
                &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
297
298
74.5k
            const __m256i res_8 =
299
74.5k
                _mm256_packus_epi16(round_result, round_result);
300
74.5k
            const __m128i res_0 = _mm256_castsi256_si128(res_8);
301
74.5k
            const __m128i res_1 = _mm256_extracti128_si256(res_8, 1);
302
303
74.5k
            if (w - j > 4) {
304
40.4k
              _mm_storel_epi64((__m128i *)(&dst0[i * dst_stride0 + j]), res_0);
305
40.4k
              _mm_storel_epi64(
306
40.4k
                  (__m128i *)((&dst0[i * dst_stride0 + j + dst_stride0])),
307
40.4k
                  res_1);
308
40.4k
            } else {
309
34.1k
              *(int *)(&dst0[i * dst_stride0 + j]) = _mm_cvtsi128_si32(res_0);
310
34.1k
              *(int *)(&dst0[i * dst_stride0 + j + dst_stride0]) =
311
34.1k
                  _mm_cvtsi128_si32(res_1);
312
34.1k
            }
313
74.5k
          } else {
314
35.2k
            const __m128i res_0 = _mm256_castsi256_si128(res_lo_unsigned);
315
35.2k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_0);
316
317
35.2k
            const __m128i res_1 = _mm256_extracti128_si256(res_lo_unsigned, 1);
318
35.2k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + dst_stride]),
319
35.2k
                            res_1);
320
35.2k
          }
321
109k
        } else {
322
87.9k
          __m256i res_hi = convolve_lowbd_4tap(s + 3, coeffs + 1);
323
324
87.9k
          res_hi = _mm256_add_epi16(res_hi, offset_const_1);
325
326
87.9k
          const __m256i res_hi_0_32b = _mm256_unpacklo_epi16(res_hi, zero);
327
87.9k
          const __m256i res_hi_0_shift =
328
87.9k
              _mm256_slli_epi32(res_hi_0_32b, left_shift);
329
87.9k
          const __m256i res_hi_0_round = _mm256_sra_epi32(
330
87.9k
              _mm256_add_epi32(res_hi_0_shift, round_const), round_shift);
331
332
87.9k
          const __m256i res_hi_1_32b = _mm256_unpackhi_epi16(res_hi, zero);
333
87.9k
          const __m256i res_hi_1_shift =
334
87.9k
              _mm256_slli_epi32(res_hi_1_32b, left_shift);
335
87.9k
          const __m256i res_hi_1_round = _mm256_sra_epi32(
336
87.9k
              _mm256_add_epi32(res_hi_1_shift, round_const), round_shift);
337
338
87.9k
          const __m256i res_hi_round =
339
87.9k
              _mm256_packs_epi32(res_hi_0_round, res_hi_1_round);
340
341
87.9k
          const __m256i res_hi_unsigned =
342
87.9k
              _mm256_add_epi16(res_hi_round, offset_const_2);
343
344
87.9k
          if (do_average) {
345
33.9k
            const __m256i data_ref_0_lo =
346
33.9k
                load_line2_avx2(&dst[i * dst_stride + j],
347
33.9k
                                &dst[i * dst_stride + j + dst_stride]);
348
349
33.9k
            const __m256i data_ref_0_hi =
350
33.9k
                load_line2_avx2(&dst[i * dst_stride + j + 8],
351
33.9k
                                &dst[i * dst_stride + j + 8 + dst_stride]);
352
353
33.9k
            const __m256i comp_avg_res_lo = comp_avg(
354
33.9k
                &data_ref_0_lo, &res_lo_unsigned, &wt, use_dist_wtd_comp_avg);
355
356
33.9k
            const __m256i comp_avg_res_hi = comp_avg(
357
33.9k
                &data_ref_0_hi, &res_hi_unsigned, &wt, use_dist_wtd_comp_avg);
358
359
33.9k
            const __m256i round_result_lo =
360
33.9k
                convolve_rounding(&comp_avg_res_lo, &offset_const,
361
33.9k
                                  &rounding_const, rounding_shift);
362
363
33.9k
            const __m256i round_result_hi =
364
33.9k
                convolve_rounding(&comp_avg_res_hi, &offset_const,
365
33.9k
                                  &rounding_const, rounding_shift);
366
367
33.9k
            const __m256i res_8 =
368
33.9k
                _mm256_packus_epi16(round_result_lo, round_result_hi);
369
33.9k
            const __m128i res_0 = _mm256_castsi256_si128(res_8);
370
33.9k
            const __m128i res_1 = _mm256_extracti128_si256(res_8, 1);
371
372
33.9k
            _mm_store_si128((__m128i *)(&dst0[i * dst_stride0 + j]), res_0);
373
33.9k
            _mm_store_si128(
374
33.9k
                (__m128i *)((&dst0[i * dst_stride0 + j + dst_stride0])), res_1);
375
376
54.0k
          } else {
377
54.0k
            const __m128i res_lo_0 = _mm256_castsi256_si128(res_lo_unsigned);
378
54.0k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_lo_0);
379
380
54.0k
            const __m128i res_lo_1 =
381
54.0k
                _mm256_extracti128_si256(res_lo_unsigned, 1);
382
54.0k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + dst_stride]),
383
54.0k
                            res_lo_1);
384
385
54.0k
            const __m128i res_hi_0 = _mm256_castsi256_si128(res_hi_unsigned);
386
54.0k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + 8]),
387
54.0k
                            res_hi_0);
388
389
54.0k
            const __m128i res_hi_1 =
390
54.0k
                _mm256_extracti128_si256(res_hi_unsigned, 1);
391
54.0k
            _mm_store_si128(
392
54.0k
                (__m128i *)(&dst[i * dst_stride + j + 8 + dst_stride]),
393
54.0k
                res_hi_1);
394
54.0k
          }
395
87.9k
        }
396
197k
        s[0] = s[1];
397
197k
        s[1] = s[2];
398
399
197k
        s[3] = s[4];
400
197k
        s[4] = s[5];
401
197k
      }
402
41.9k
    }
403
40.0k
  } else {
404
36.6k
    const int fo_vert = filter_params_y->taps / 2 - 1;
405
36.6k
    const uint8_t *const src_ptr = src - fo_vert * src_stride;
406
85.0k
    for (j = 0; j < w; j += 16) {
407
48.4k
      const uint8_t *data = &src_ptr[j];
408
48.4k
      __m256i src6;
409
      // Load lines a and b. Line a to lower 128, line b to upper 128
410
48.4k
      {
411
48.4k
        __m256i src_ab[7];
412
48.4k
        __m256i src_a[7];
413
48.4k
        src_a[0] = _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)data));
414
339k
        for (int kk = 0; kk < 6; ++kk) {
415
290k
          data += src_stride;
416
290k
          src_a[kk + 1] =
417
290k
              _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)data));
418
290k
          src_ab[kk] =
419
290k
              _mm256_permute2x128_si256(src_a[kk], src_a[kk + 1], 0x20);
420
290k
        }
421
48.4k
        src6 = src_a[6];
422
48.4k
        s[0] = _mm256_unpacklo_epi8(src_ab[0], src_ab[1]);
423
48.4k
        s[1] = _mm256_unpacklo_epi8(src_ab[2], src_ab[3]);
424
48.4k
        s[2] = _mm256_unpacklo_epi8(src_ab[4], src_ab[5]);
425
48.4k
        s[4] = _mm256_unpackhi_epi8(src_ab[0], src_ab[1]);
426
48.4k
        s[5] = _mm256_unpackhi_epi8(src_ab[2], src_ab[3]);
427
48.4k
        s[6] = _mm256_unpackhi_epi8(src_ab[4], src_ab[5]);
428
48.4k
      }
429
430
555k
      for (i = 0; i < h; i += 2) {
431
507k
        data = &src_ptr[(i + 7) * src_stride + j];
432
507k
        const __m256i src7 =
433
507k
            _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)data));
434
507k
        const __m256i src_67a = _mm256_permute2x128_si256(src6, src7, 0x20);
435
436
507k
        src6 = _mm256_castsi128_si256(
437
507k
            _mm_loadu_si128((__m128i *)(data + src_stride)));
438
507k
        const __m256i src_78a = _mm256_permute2x128_si256(src7, src6, 0x20);
439
440
507k
        s[3] = _mm256_unpacklo_epi8(src_67a, src_78a);
441
507k
        s[7] = _mm256_unpackhi_epi8(src_67a, src_78a);
442
443
507k
        __m256i res_lo = convolve_lowbd(s, coeffs);
444
445
507k
        res_lo = _mm256_add_epi16(res_lo, offset_const_1);
446
447
507k
        const __m256i res_lo_0_32b = _mm256_unpacklo_epi16(res_lo, zero);
448
507k
        const __m256i res_lo_0_shift =
449
507k
            _mm256_slli_epi32(res_lo_0_32b, left_shift);
450
507k
        const __m256i res_lo_0_round = _mm256_sra_epi32(
451
507k
            _mm256_add_epi32(res_lo_0_shift, round_const), round_shift);
452
453
507k
        const __m256i res_lo_1_32b = _mm256_unpackhi_epi16(res_lo, zero);
454
507k
        const __m256i res_lo_1_shift =
455
507k
            _mm256_slli_epi32(res_lo_1_32b, left_shift);
456
507k
        const __m256i res_lo_1_round = _mm256_sra_epi32(
457
507k
            _mm256_add_epi32(res_lo_1_shift, round_const), round_shift);
458
459
507k
        const __m256i res_lo_round =
460
507k
            _mm256_packs_epi32(res_lo_0_round, res_lo_1_round);
461
462
507k
        const __m256i res_lo_unsigned =
463
507k
            _mm256_add_epi16(res_lo_round, offset_const_2);
464
465
507k
        if (w - j < 16) {
466
97.4k
          if (do_average) {
467
44.8k
            const __m256i data_ref_0 =
468
44.8k
                load_line2_avx2(&dst[i * dst_stride + j],
469
44.8k
                                &dst[i * dst_stride + j + dst_stride]);
470
44.8k
            const __m256i comp_avg_res = comp_avg(&data_ref_0, &res_lo_unsigned,
471
44.8k
                                                  &wt, use_dist_wtd_comp_avg);
472
473
44.8k
            const __m256i round_result = convolve_rounding(
474
44.8k
                &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
475
476
44.8k
            const __m256i res_8 =
477
44.8k
                _mm256_packus_epi16(round_result, round_result);
478
44.8k
            const __m128i res_0 = _mm256_castsi256_si128(res_8);
479
44.8k
            const __m128i res_1 = _mm256_extracti128_si256(res_8, 1);
480
481
44.8k
            if (w - j > 4) {
482
35.0k
              _mm_storel_epi64((__m128i *)(&dst0[i * dst_stride0 + j]), res_0);
483
35.0k
              _mm_storel_epi64(
484
35.0k
                  (__m128i *)((&dst0[i * dst_stride0 + j + dst_stride0])),
485
35.0k
                  res_1);
486
35.0k
            } else {
487
9.87k
              *(int *)(&dst0[i * dst_stride0 + j]) = _mm_cvtsi128_si32(res_0);
488
9.87k
              *(int *)(&dst0[i * dst_stride0 + j + dst_stride0]) =
489
9.87k
                  _mm_cvtsi128_si32(res_1);
490
9.87k
            }
491
52.6k
          } else {
492
52.6k
            const __m128i res_0 = _mm256_castsi256_si128(res_lo_unsigned);
493
52.6k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_0);
494
495
52.6k
            const __m128i res_1 = _mm256_extracti128_si256(res_lo_unsigned, 1);
496
52.6k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + dst_stride]),
497
52.6k
                            res_1);
498
52.6k
          }
499
409k
        } else {
500
409k
          __m256i res_hi = convolve_lowbd(s + 4, coeffs);
501
502
409k
          res_hi = _mm256_add_epi16(res_hi, offset_const_1);
503
504
409k
          const __m256i res_hi_0_32b = _mm256_unpacklo_epi16(res_hi, zero);
505
409k
          const __m256i res_hi_0_shift =
506
409k
              _mm256_slli_epi32(res_hi_0_32b, left_shift);
507
409k
          const __m256i res_hi_0_round = _mm256_sra_epi32(
508
409k
              _mm256_add_epi32(res_hi_0_shift, round_const), round_shift);
509
510
409k
          const __m256i res_hi_1_32b = _mm256_unpackhi_epi16(res_hi, zero);
511
409k
          const __m256i res_hi_1_shift =
512
409k
              _mm256_slli_epi32(res_hi_1_32b, left_shift);
513
409k
          const __m256i res_hi_1_round = _mm256_sra_epi32(
514
409k
              _mm256_add_epi32(res_hi_1_shift, round_const), round_shift);
515
516
409k
          const __m256i res_hi_round =
517
409k
              _mm256_packs_epi32(res_hi_0_round, res_hi_1_round);
518
519
409k
          const __m256i res_hi_unsigned =
520
409k
              _mm256_add_epi16(res_hi_round, offset_const_2);
521
522
409k
          if (do_average) {
523
176k
            const __m256i data_ref_0_lo =
524
176k
                load_line2_avx2(&dst[i * dst_stride + j],
525
176k
                                &dst[i * dst_stride + j + dst_stride]);
526
527
176k
            const __m256i data_ref_0_hi =
528
176k
                load_line2_avx2(&dst[i * dst_stride + j + 8],
529
176k
                                &dst[i * dst_stride + j + 8 + dst_stride]);
530
531
176k
            const __m256i comp_avg_res_lo = comp_avg(
532
176k
                &data_ref_0_lo, &res_lo_unsigned, &wt, use_dist_wtd_comp_avg);
533
534
176k
            const __m256i comp_avg_res_hi = comp_avg(
535
176k
                &data_ref_0_hi, &res_hi_unsigned, &wt, use_dist_wtd_comp_avg);
536
537
176k
            const __m256i round_result_lo =
538
176k
                convolve_rounding(&comp_avg_res_lo, &offset_const,
539
176k
                                  &rounding_const, rounding_shift);
540
541
176k
            const __m256i round_result_hi =
542
176k
                convolve_rounding(&comp_avg_res_hi, &offset_const,
543
176k
                                  &rounding_const, rounding_shift);
544
545
176k
            const __m256i res_8 =
546
176k
                _mm256_packus_epi16(round_result_lo, round_result_hi);
547
176k
            const __m128i res_0 = _mm256_castsi256_si128(res_8);
548
176k
            const __m128i res_1 = _mm256_extracti128_si256(res_8, 1);
549
550
176k
            _mm_store_si128((__m128i *)(&dst0[i * dst_stride0 + j]), res_0);
551
176k
            _mm_store_si128(
552
176k
                (__m128i *)((&dst0[i * dst_stride0 + j + dst_stride0])), res_1);
553
554
233k
          } else {
555
233k
            const __m128i res_lo_0 = _mm256_castsi256_si128(res_lo_unsigned);
556
233k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_lo_0);
557
558
233k
            const __m128i res_lo_1 =
559
233k
                _mm256_extracti128_si256(res_lo_unsigned, 1);
560
233k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + dst_stride]),
561
233k
                            res_lo_1);
562
563
233k
            const __m128i res_hi_0 = _mm256_castsi256_si128(res_hi_unsigned);
564
233k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + 8]),
565
233k
                            res_hi_0);
566
567
233k
            const __m128i res_hi_1 =
568
233k
                _mm256_extracti128_si256(res_hi_unsigned, 1);
569
233k
            _mm_store_si128(
570
233k
                (__m128i *)(&dst[i * dst_stride + j + 8 + dst_stride]),
571
233k
                res_hi_1);
572
233k
          }
573
409k
        }
574
507k
        s[0] = s[1];
575
507k
        s[1] = s[2];
576
507k
        s[2] = s[3];
577
578
507k
        s[4] = s[5];
579
507k
        s[5] = s[6];
580
507k
        s[6] = s[7];
581
507k
      }
582
48.4k
    }
583
36.6k
  }
584
76.6k
}
585
586
void av1_dist_wtd_convolve_2d_avx2(const uint8_t *src, int src_stride,
587
                                   uint8_t *dst0, int dst_stride0, int w, int h,
588
                                   const InterpFilterParams *filter_params_x,
589
                                   const InterpFilterParams *filter_params_y,
590
                                   const int subpel_x_qn, const int subpel_y_qn,
591
231k
                                   ConvolveParams *conv_params) {
592
231k
  CONV_BUF_TYPE *dst = conv_params->dst;
593
231k
  int dst_stride = conv_params->dst_stride;
594
231k
  const int bd = 8;
595
596
231k
  DECLARE_ALIGNED(32, int16_t, im_block[(MAX_SB_SIZE + MAX_FILTER_TAP) * 8]);
597
598
231k
  int im_stride = 8;
599
231k
  int i, is_horiz_4tap = 0, is_vert_4tap = 0;
600
231k
  const __m256i wt = unpack_weights_avx2(conv_params);
601
231k
  const int do_average = conv_params->do_average;
602
231k
  const int use_dist_wtd_comp_avg = conv_params->use_dist_wtd_comp_avg;
603
231k
  const int offset_0 =
604
231k
      bd + 2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
605
231k
  const int offset = (1 << offset_0) + (1 << (offset_0 - 1));
606
231k
  const __m256i offset_const = _mm256_set1_epi16(offset);
607
231k
  const int rounding_shift =
608
231k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
609
231k
  const __m256i rounding_const = _mm256_set1_epi16((1 << rounding_shift) >> 1);
610
611
231k
  assert(conv_params->round_0 > 0);
612
613
231k
  const __m256i round_const_h = _mm256_set1_epi16(
614
231k
      ((1 << (conv_params->round_0 - 1)) >> 1) + (1 << (bd + FILTER_BITS - 2)));
615
231k
  const __m128i round_shift_h = _mm_cvtsi32_si128(conv_params->round_0 - 1);
616
617
231k
  const __m256i round_const_v = _mm256_set1_epi32(
618
231k
      ((1 << conv_params->round_1) >> 1) -
619
231k
      (1 << (bd + 2 * FILTER_BITS - conv_params->round_0 - 1)));
620
231k
  const __m128i round_shift_v = _mm_cvtsi32_si128(conv_params->round_1);
621
622
231k
  __m256i filt[4], coeffs_x[4], coeffs_y[4];
623
624
231k
  filt[0] = _mm256_load_si256((__m256i const *)filt_global_avx2);
625
231k
  filt[1] = _mm256_load_si256((__m256i const *)(filt_global_avx2 + 32));
626
627
231k
  prepare_coeffs_lowbd(filter_params_x, subpel_x_qn, coeffs_x);
628
231k
  prepare_coeffs(filter_params_y, subpel_y_qn, coeffs_y);
629
630
  // Condition for checking valid horz_filt taps
631
231k
  if (!(_mm256_extract_epi32(_mm256_or_si256(coeffs_x[0], coeffs_x[3]), 0)))
632
121k
    is_horiz_4tap = 1;
633
634
  // Condition for checking valid vert_filt taps
635
231k
  if (!(_mm256_extract_epi32(_mm256_or_si256(coeffs_y[0], coeffs_y[3]), 0)))
636
130k
    is_vert_4tap = 1;
637
638
231k
  if (is_horiz_4tap) {
639
121k
    int im_h = h + filter_params_y->taps - 1;
640
121k
    const int fo_vert = filter_params_y->taps / 2 - 1;
641
121k
    const int fo_horiz = 1;
642
121k
    const uint8_t *const src_ptr = src - fo_vert * src_stride - fo_horiz;
643
268k
    for (int j = 0; j < w; j += 8) {
644
      /* Horizontal filter */
645
147k
      const uint8_t *src_h = src_ptr + j;
646
1.66M
      for (i = 0; i < im_h; i += 2) {
647
1.52M
        __m256i data =
648
1.52M
            _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)src_h));
649
1.52M
        if (i + 1 < im_h)
650
1.37M
          data = _mm256_inserti128_si256(
651
1.52M
              data, _mm_loadu_si128((__m128i *)(src_h + src_stride)), 1);
652
1.52M
        src_h += (src_stride << 1);
653
1.52M
        __m256i res = convolve_lowbd_x_4tap(data, coeffs_x + 1, filt);
654
655
1.52M
        res = _mm256_sra_epi16(_mm256_add_epi16(res, round_const_h),
656
1.52M
                               round_shift_h);
657
658
1.52M
        _mm256_store_si256((__m256i *)&im_block[i * im_stride], res);
659
1.52M
      }
660
147k
      DIST_WTD_CONVOLVE_VERTICAL_FILTER_8TAP;
661
147k
    }
662
121k
  } else if (is_vert_4tap) {
663
29.3k
    int im_h = h + 3;
664
29.3k
    const int fo_vert = 1;
665
29.3k
    const int fo_horiz = filter_params_x->taps / 2 - 1;
666
29.3k
    const uint8_t *const src_ptr = src - fo_vert * src_stride - fo_horiz;
667
668
29.3k
    filt[2] = _mm256_load_si256((__m256i const *)(filt_global_avx2 + 32 * 2));
669
29.3k
    filt[3] = _mm256_load_si256((__m256i const *)(filt_global_avx2 + 32 * 3));
670
671
69.6k
    for (int j = 0; j < w; j += 8) {
672
      /* Horizontal filter */
673
40.3k
      const uint8_t *src_h = src_ptr + j;
674
40.3k
      DIST_WTD_CONVOLVE_HORIZONTAL_FILTER_8TAP;
675
676
      /* Vertical filter */
677
40.3k
      __m256i s[6];
678
40.3k
      __m256i s0 = _mm256_loadu_si256((__m256i *)(im_block + 0 * im_stride));
679
40.3k
      __m256i s1 = _mm256_loadu_si256((__m256i *)(im_block + 1 * im_stride));
680
40.3k
      __m256i s2 = _mm256_loadu_si256((__m256i *)(im_block + 2 * im_stride));
681
40.3k
      __m256i s3 = _mm256_loadu_si256((__m256i *)(im_block + 3 * im_stride));
682
683
40.3k
      s[0] = _mm256_unpacklo_epi16(s0, s1);
684
40.3k
      s[1] = _mm256_unpacklo_epi16(s2, s3);
685
686
40.3k
      s[3] = _mm256_unpackhi_epi16(s0, s1);
687
40.3k
      s[4] = _mm256_unpackhi_epi16(s2, s3);
688
689
170k
      for (i = 0; i < h; i += 2) {
690
130k
        const int16_t *data = &im_block[i * im_stride];
691
692
130k
        const __m256i s4 =
693
130k
            _mm256_loadu_si256((__m256i *)(data + 4 * im_stride));
694
130k
        const __m256i s5 =
695
130k
            _mm256_loadu_si256((__m256i *)(data + 5 * im_stride));
696
697
130k
        s[2] = _mm256_unpacklo_epi16(s4, s5);
698
130k
        s[5] = _mm256_unpackhi_epi16(s4, s5);
699
700
130k
        const __m256i res_a = convolve_4tap(s, coeffs_y + 1);
701
130k
        const __m256i res_a_round = _mm256_sra_epi32(
702
130k
            _mm256_add_epi32(res_a, round_const_v), round_shift_v);
703
704
130k
        if (w - j > 4) {
705
130k
          const __m256i res_b = convolve_4tap(s + 3, coeffs_y + 1);
706
130k
          const __m256i res_b_round = _mm256_sra_epi32(
707
130k
              _mm256_add_epi32(res_b, round_const_v), round_shift_v);
708
130k
          const __m256i res_16b = _mm256_packs_epi32(res_a_round, res_b_round);
709
130k
          const __m256i res_unsigned = _mm256_add_epi16(res_16b, offset_const);
710
711
130k
          if (do_average) {
712
51.0k
            const __m256i data_ref_0 =
713
51.0k
                load_line2_avx2(&dst[i * dst_stride + j],
714
51.0k
                                &dst[i * dst_stride + j + dst_stride]);
715
51.0k
            const __m256i comp_avg_res = comp_avg(&data_ref_0, &res_unsigned,
716
51.0k
                                                  &wt, use_dist_wtd_comp_avg);
717
718
51.0k
            const __m256i round_result = convolve_rounding(
719
51.0k
                &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
720
721
51.0k
            const __m256i res_8 =
722
51.0k
                _mm256_packus_epi16(round_result, round_result);
723
51.0k
            const __m128i res_0 = _mm256_castsi256_si128(res_8);
724
51.0k
            const __m128i res_1 = _mm256_extracti128_si256(res_8, 1);
725
726
51.0k
            _mm_storel_epi64((__m128i *)(&dst0[i * dst_stride0 + j]), res_0);
727
51.0k
            _mm_storel_epi64(
728
51.0k
                (__m128i *)((&dst0[i * dst_stride0 + j + dst_stride0])), res_1);
729
79.4k
          } else {
730
79.4k
            const __m128i res_0 = _mm256_castsi256_si128(res_unsigned);
731
79.4k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_0);
732
733
79.4k
            const __m128i res_1 = _mm256_extracti128_si256(res_unsigned, 1);
734
79.4k
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + dst_stride]),
735
79.4k
                            res_1);
736
79.4k
          }
737
130k
        } else {
738
0
          const __m256i res_16b = _mm256_packs_epi32(res_a_round, res_a_round);
739
0
          const __m256i res_unsigned = _mm256_add_epi16(res_16b, offset_const);
740
741
0
          if (do_average) {
742
0
            const __m256i data_ref_0 =
743
0
                load_line2_avx2(&dst[i * dst_stride + j],
744
0
                                &dst[i * dst_stride + j + dst_stride]);
745
746
0
            const __m256i comp_avg_res = comp_avg(&data_ref_0, &res_unsigned,
747
0
                                                  &wt, use_dist_wtd_comp_avg);
748
749
0
            const __m256i round_result = convolve_rounding(
750
0
                &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
751
752
0
            const __m256i res_8 =
753
0
                _mm256_packus_epi16(round_result, round_result);
754
0
            const __m128i res_0 = _mm256_castsi256_si128(res_8);
755
0
            const __m128i res_1 = _mm256_extracti128_si256(res_8, 1);
756
757
0
            *(int *)(&dst0[i * dst_stride0 + j]) = _mm_cvtsi128_si32(res_0);
758
0
            *(int *)(&dst0[i * dst_stride0 + j + dst_stride0]) =
759
0
                _mm_cvtsi128_si32(res_1);
760
761
0
          } else {
762
0
            const __m128i res_0 = _mm256_castsi256_si128(res_unsigned);
763
0
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_0);
764
765
0
            const __m128i res_1 = _mm256_extracti128_si256(res_unsigned, 1);
766
0
            _mm_store_si128((__m128i *)(&dst[i * dst_stride + j + dst_stride]),
767
0
                            res_1);
768
0
          }
769
0
        }
770
130k
        s[0] = s[1];
771
130k
        s[1] = s[2];
772
130k
        s[3] = s[4];
773
130k
        s[4] = s[5];
774
130k
      }
775
40.3k
    }
776
81.2k
  } else {
777
81.2k
    int im_h = h + filter_params_y->taps - 1;
778
81.2k
    const int fo_vert = filter_params_y->taps / 2 - 1;
779
81.2k
    const int fo_horiz = filter_params_x->taps / 2 - 1;
780
81.2k
    const uint8_t *const src_ptr = src - fo_vert * src_stride - fo_horiz;
781
782
81.2k
    filt[2] = _mm256_load_si256((__m256i const *)(filt_global_avx2 + 32 * 2));
783
81.2k
    filt[3] = _mm256_load_si256((__m256i const *)(filt_global_avx2 + 32 * 3));
784
785
270k
    for (int j = 0; j < w; j += 8) {
786
      /* Horizontal filter */
787
189k
      const uint8_t *src_h = src_ptr + j;
788
189k
      DIST_WTD_CONVOLVE_HORIZONTAL_FILTER_8TAP;
789
790
189k
      DIST_WTD_CONVOLVE_VERTICAL_FILTER_8TAP;
791
189k
    }
792
81.2k
  }
793
231k
}
794
795
#define DO_NO_AVG_2D_COPY_4X16(r0, c0, r1, c1, r2, c2, r3, c3)          \
796
29.3M
  do {                                                                  \
797
29.3M
    src_0 = _mm256_cvtepu8_epi16(                                       \
798
29.3M
        _mm_loadu_si128((__m128i *)(&src[r0 * src_stride + c0])));      \
799
29.3M
    src_1 = _mm256_cvtepu8_epi16(                                       \
800
29.3M
        _mm_loadu_si128((__m128i *)(&src[r1 * src_stride + c1])));      \
801
29.3M
    src_2 = _mm256_cvtepu8_epi16(                                       \
802
29.3M
        _mm_loadu_si128((__m128i *)(&src[r2 * src_stride + c2])));      \
803
29.3M
    src_3 = _mm256_cvtepu8_epi16(                                       \
804
29.3M
        _mm_loadu_si128((__m128i *)(&src[r3 * src_stride + c3])));      \
805
29.3M
                                                                        \
806
29.3M
    src_0 = _mm256_slli_epi16(src_0, LEFT_SHIFT);                       \
807
29.3M
    src_1 = _mm256_slli_epi16(src_1, LEFT_SHIFT);                       \
808
29.3M
    src_2 = _mm256_slli_epi16(src_2, LEFT_SHIFT);                       \
809
29.3M
    src_3 = _mm256_slli_epi16(src_3, LEFT_SHIFT);                       \
810
29.3M
                                                                        \
811
29.3M
    src_0 = _mm256_add_epi16(src_0, offset_const);                      \
812
29.3M
    src_1 = _mm256_add_epi16(src_1, offset_const);                      \
813
29.3M
    src_2 = _mm256_add_epi16(src_2, offset_const);                      \
814
29.3M
    src_3 = _mm256_add_epi16(src_3, offset_const);                      \
815
29.3M
                                                                        \
816
29.3M
    _mm256_store_si256((__m256i *)(&dst[r0 * dst_stride + c0]), src_0); \
817
29.3M
    _mm256_store_si256((__m256i *)(&dst[r1 * dst_stride + c1]), src_1); \
818
29.3M
    _mm256_store_si256((__m256i *)(&dst[r2 * dst_stride + c2]), src_2); \
819
29.3M
    _mm256_store_si256((__m256i *)(&dst[r3 * dst_stride + c3]), src_3); \
820
29.3M
  } while (0)
821
822
234M
#define LEFT_SHIFT (2 * FILTER_BITS - 3 - 7)
823
static inline void av1_dist_wtd_convolve_2d_no_avg_copy_avx2(
824
    const uint8_t *src, int src_stride, CONV_BUF_TYPE *dst, int dst_stride,
825
516k
    int w, int h, const __m256i offset_const) {
826
516k
  int i = h;
827
516k
  if (w >= 16) {
828
351k
    __m256i src_0, src_1, src_2, src_3;
829
351k
    if (w == 128) {
830
7.63M
      do {
831
7.63M
        DO_NO_AVG_2D_COPY_4X16(0, 0, 0, 16, 0, 32, 0, 48);
832
7.63M
        DO_NO_AVG_2D_COPY_4X16(0, 64, 0, 80, 0, 96, 0, 112);
833
7.63M
        src += 1 * src_stride;
834
7.63M
        dst += 1 * dst_stride;
835
7.63M
        i -= 1;
836
7.63M
      } while (i);
837
291k
    } else if (w == 64) {
838
11.6M
      do {
839
11.6M
        DO_NO_AVG_2D_COPY_4X16(0, 0, 0, 16, 0, 32, 0, 48);
840
11.6M
        src += 1 * src_stride;
841
11.6M
        dst += 1 * dst_stride;
842
11.6M
        i -= 1;
843
11.6M
      } while (i);
844
153k
    } else if (w == 32) {
845
2.13M
      do {
846
2.13M
        DO_NO_AVG_2D_COPY_4X16(0, 0, 1, 0, 0, 16, 1, 16);
847
2.13M
        src += 2 * src_stride;
848
2.13M
        dst += 2 * dst_stride;
849
2.13M
        i -= 2;
850
2.13M
      } while (i);
851
80.4k
    } else if (w == 16) {
852
318k
      do {
853
318k
        DO_NO_AVG_2D_COPY_4X16(0, 0, 1, 0, 2, 0, 3, 0);
854
318k
        src += 4 * src_stride;
855
318k
        dst += 4 * dst_stride;
856
318k
        i -= 4;
857
318k
      } while (i);
858
58.4k
    }
859
351k
  } else {
860
164k
    const __m256i zero = _mm256_setzero_si256();
861
438k
    do {
862
438k
      const __m128i src_row_0 =
863
438k
          _mm_loadl_epi64((__m128i *)(&src[0 * src_stride]));
864
438k
      const __m128i src_row_1 =
865
438k
          _mm_loadl_epi64((__m128i *)(&src[1 * src_stride]));
866
438k
      const __m128i src_row_2 =
867
438k
          _mm_loadl_epi64((__m128i *)(&src[2 * src_stride]));
868
438k
      const __m128i src_row_3 =
869
438k
          _mm_loadl_epi64((__m128i *)(&src[3 * src_stride]));
870
871
438k
      __m256i src_10 = _mm256_insertf128_si256(
872
438k
          _mm256_castsi128_si256(src_row_0), src_row_1, 1);
873
438k
      __m256i src_32 = _mm256_insertf128_si256(
874
438k
          _mm256_castsi128_si256(src_row_2), src_row_3, 1);
875
876
438k
      src_10 = _mm256_unpacklo_epi8(src_10, zero);
877
438k
      src_32 = _mm256_unpacklo_epi8(src_32, zero);
878
879
438k
      src_10 = _mm256_slli_epi16(src_10, LEFT_SHIFT);
880
438k
      src_32 = _mm256_slli_epi16(src_32, LEFT_SHIFT);
881
882
438k
      src_10 = _mm256_add_epi16(src_10, offset_const);
883
438k
      src_32 = _mm256_add_epi16(src_32, offset_const);
884
885
      // Accumulate values into the destination buffer
886
438k
      _mm_store_si128((__m128i *)(&dst[0 * dst_stride]),
887
438k
                      _mm256_castsi256_si128(src_10));
888
438k
      _mm_store_si128((__m128i *)(&dst[1 * dst_stride]),
889
438k
                      _mm256_extracti128_si256(src_10, 1));
890
438k
      _mm_store_si128((__m128i *)(&dst[2 * dst_stride]),
891
438k
                      _mm256_castsi256_si128(src_32));
892
438k
      _mm_store_si128((__m128i *)(&dst[3 * dst_stride]),
893
438k
                      _mm256_extracti128_si256(src_32, 1));
894
895
438k
      src += 4 * src_stride;
896
438k
      dst += 4 * dst_stride;
897
438k
      i -= 4;
898
438k
    } while (i);
899
164k
  }
900
516k
}
901
902
#define DO_AVG_2D_COPY_4X16(USE_DIST_WEIGHTED, r0, c0, r1, c1, r2, c2, r3, c3) \
903
28.8M
  do {                                                                         \
904
28.8M
    src_0 = _mm256_cvtepu8_epi16(                                              \
905
28.8M
        _mm_loadu_si128((__m128i *)(&src[r0 * src_stride + c0])));             \
906
28.8M
    src_1 = _mm256_cvtepu8_epi16(                                              \
907
28.8M
        _mm_loadu_si128((__m128i *)(&src[r1 * src_stride + c1])));             \
908
28.8M
    src_2 = _mm256_cvtepu8_epi16(                                              \
909
28.8M
        _mm_loadu_si128((__m128i *)(&src[r2 * src_stride + c2])));             \
910
28.8M
    src_3 = _mm256_cvtepu8_epi16(                                              \
911
28.8M
        _mm_loadu_si128((__m128i *)(&src[r3 * src_stride + c3])));             \
912
28.8M
                                                                               \
913
28.8M
    src_0 = _mm256_slli_epi16(src_0, LEFT_SHIFT);                              \
914
28.8M
    src_1 = _mm256_slli_epi16(src_1, LEFT_SHIFT);                              \
915
28.8M
    src_2 = _mm256_slli_epi16(src_2, LEFT_SHIFT);                              \
916
28.8M
    src_3 = _mm256_slli_epi16(src_3, LEFT_SHIFT);                              \
917
28.8M
    src_0 = _mm256_add_epi16(src_0, offset_const);                             \
918
28.8M
    src_1 = _mm256_add_epi16(src_1, offset_const);                             \
919
28.8M
    src_2 = _mm256_add_epi16(src_2, offset_const);                             \
920
28.8M
    src_3 = _mm256_add_epi16(src_3, offset_const);                             \
921
28.8M
                                                                               \
922
28.8M
    ref_0 = _mm256_loadu_si256((__m256i *)(&dst[r0 * dst_stride + c0]));       \
923
28.8M
    ref_1 = _mm256_loadu_si256((__m256i *)(&dst[r1 * dst_stride + c1]));       \
924
28.8M
    ref_2 = _mm256_loadu_si256((__m256i *)(&dst[r2 * dst_stride + c2]));       \
925
28.8M
    ref_3 = _mm256_loadu_si256((__m256i *)(&dst[r3 * dst_stride + c3]));       \
926
28.8M
                                                                               \
927
28.8M
    res_0 = comp_avg(&ref_0, &src_0, &wt, USE_DIST_WEIGHTED);                  \
928
28.8M
    res_1 = comp_avg(&ref_1, &src_1, &wt, USE_DIST_WEIGHTED);                  \
929
28.8M
    res_2 = comp_avg(&ref_2, &src_2, &wt, USE_DIST_WEIGHTED);                  \
930
28.8M
    res_3 = comp_avg(&ref_3, &src_3, &wt, USE_DIST_WEIGHTED);                  \
931
28.8M
                                                                               \
932
28.8M
    res_0 = convolve_rounding(&res_0, &offset_const, &rounding_const,          \
933
28.8M
                              rounding_shift);                                 \
934
28.8M
    res_1 = convolve_rounding(&res_1, &offset_const, &rounding_const,          \
935
28.8M
                              rounding_shift);                                 \
936
28.8M
    res_2 = convolve_rounding(&res_2, &offset_const, &rounding_const,          \
937
28.8M
                              rounding_shift);                                 \
938
28.8M
    res_3 = convolve_rounding(&res_3, &offset_const, &rounding_const,          \
939
28.8M
                              rounding_shift);                                 \
940
28.8M
                                                                               \
941
28.8M
    res_10 = _mm256_packus_epi16(res_0, res_1);                                \
942
28.8M
    res_32 = _mm256_packus_epi16(res_2, res_3);                                \
943
28.8M
    res_10 = _mm256_permute4x64_epi64(res_10, 0xD8);                           \
944
28.8M
    res_32 = _mm256_permute4x64_epi64(res_32, 0xD8);                           \
945
28.8M
                                                                               \
946
28.8M
    _mm_store_si128((__m128i *)(&dst0[r0 * dst_stride0 + c0]),                 \
947
28.8M
                    _mm256_castsi256_si128(res_10));                           \
948
28.8M
    _mm_store_si128((__m128i *)(&dst0[r1 * dst_stride0 + c1]),                 \
949
28.8M
                    _mm256_extracti128_si256(res_10, 1));                      \
950
28.8M
    _mm_store_si128((__m128i *)(&dst0[r2 * dst_stride0 + c2]),                 \
951
28.8M
                    _mm256_castsi256_si128(res_32));                           \
952
28.8M
    _mm_store_si128((__m128i *)(&dst0[r3 * dst_stride0 + c3]),                 \
953
28.8M
                    _mm256_extracti128_si256(res_32, 1));                      \
954
28.8M
  } while (0)
955
956
#define DO_AVG_2D_COPY(USE_DIST_WEIGHTED)                                     \
957
399k
  int i = h;                                                                  \
958
399k
  if (w >= 16) {                                                              \
959
315k
    __m256i src_0, src_1, src_2, src_3;                                       \
960
315k
    __m256i ref_0, ref_1, ref_2, ref_3;                                       \
961
315k
    __m256i res_0, res_1, res_2, res_3;                                       \
962
315k
    __m256i res_10, res_32;                                                   \
963
315k
    if (w == 128) {                                                           \
964
7.59M
      do {                                                                    \
965
7.59M
        DO_AVG_2D_COPY_4X16(USE_DIST_WEIGHTED, 0, 0, 0, 16, 0, 32, 0, 48);    \
966
7.59M
        DO_AVG_2D_COPY_4X16(USE_DIST_WEIGHTED, 0, 64, 0, 80, 0, 96, 0, 112);  \
967
7.59M
        i -= 1;                                                               \
968
7.59M
        src += 1 * src_stride;                                                \
969
7.59M
        dst += 1 * dst_stride;                                                \
970
7.59M
        dst0 += 1 * dst_stride0;                                              \
971
7.59M
      } while (i);                                                            \
972
256k
    } else if (w == 64) {                                                     \
973
11.5M
      do {                                                                    \
974
11.5M
        DO_AVG_2D_COPY_4X16(USE_DIST_WEIGHTED, 0, 0, 0, 16, 0, 32, 0, 48);    \
975
11.5M
                                                                              \
976
11.5M
        i -= 1;                                                               \
977
11.5M
        src += 1 * src_stride;                                                \
978
11.5M
        dst += 1 * dst_stride;                                                \
979
11.5M
        dst0 += 1 * dst_stride0;                                              \
980
11.5M
      } while (i);                                                            \
981
151k
    } else if (w == 32) {                                                     \
982
2.04M
      do {                                                                    \
983
2.04M
        DO_AVG_2D_COPY_4X16(USE_DIST_WEIGHTED, 0, 0, 1, 0, 0, 16, 1, 16);     \
984
2.04M
                                                                              \
985
2.04M
        i -= 2;                                                               \
986
2.04M
        src += 2 * src_stride;                                                \
987
2.04M
        dst += 2 * dst_stride;                                                \
988
2.04M
        dst0 += 2 * dst_stride0;                                              \
989
2.04M
      } while (i);                                                            \
990
73.0k
    } else {                                                                  \
991
31.6k
      assert(w == 16);                                                        \
992
123k
      do {                                                                    \
993
123k
        DO_AVG_2D_COPY_4X16(USE_DIST_WEIGHTED, 0, 0, 1, 0, 2, 0, 3, 0);       \
994
123k
                                                                              \
995
123k
        i -= 4;                                                               \
996
123k
        src += 4 * src_stride;                                                \
997
123k
        dst += 4 * dst_stride;                                                \
998
123k
        dst0 += 4 * dst_stride0;                                              \
999
123k
      } while (i);                                                            \
1000
31.8k
    }                                                                         \
1001
315k
  } else if (w == 8) {                                                        \
1002
130k
    do {                                                                      \
1003
130k
      const __m128i src_0 =                                                   \
1004
130k
          _mm_loadl_epi64((__m128i *)(&src[0 * src_stride]));                 \
1005
130k
      const __m128i src_1 =                                                   \
1006
130k
          _mm_loadl_epi64((__m128i *)(&src[1 * src_stride]));                 \
1007
130k
      const __m128i src_2 =                                                   \
1008
130k
          _mm_loadl_epi64((__m128i *)(&src[2 * src_stride]));                 \
1009
130k
      const __m128i src_3 =                                                   \
1010
130k
          _mm_loadl_epi64((__m128i *)(&src[3 * src_stride]));                 \
1011
130k
      __m256i src_10 =                                                        \
1012
130k
          _mm256_insertf128_si256(_mm256_castsi128_si256(src_0), src_1, 1);   \
1013
130k
      __m256i src_32 =                                                        \
1014
130k
          _mm256_insertf128_si256(_mm256_castsi128_si256(src_2), src_3, 1);   \
1015
130k
                                                                              \
1016
130k
      src_10 = _mm256_unpacklo_epi8(src_10, zero);                            \
1017
130k
      src_32 = _mm256_unpacklo_epi8(src_32, zero);                            \
1018
130k
                                                                              \
1019
130k
      src_10 = _mm256_slli_epi16(src_10, LEFT_SHIFT);                         \
1020
130k
      src_32 = _mm256_slli_epi16(src_32, LEFT_SHIFT);                         \
1021
130k
                                                                              \
1022
130k
      src_10 = _mm256_add_epi16(src_10, offset_const);                        \
1023
130k
      src_32 = _mm256_add_epi16(src_32, offset_const);                        \
1024
130k
                                                                              \
1025
130k
      const __m256i ref_10 =                                                  \
1026
130k
          load_line2_avx2(&dst[0 * dst_stride], &dst[1 * dst_stride]);        \
1027
130k
      const __m256i ref_32 =                                                  \
1028
130k
          load_line2_avx2(&dst[2 * dst_stride], &dst[3 * dst_stride]);        \
1029
130k
      __m256i res_10 = comp_avg(&ref_10, &src_10, &wt, USE_DIST_WEIGHTED);    \
1030
130k
      __m256i res_32 = comp_avg(&ref_32, &src_32, &wt, USE_DIST_WEIGHTED);    \
1031
130k
                                                                              \
1032
130k
      res_10 = convolve_rounding(&res_10, &offset_const, &rounding_const,     \
1033
130k
                                 rounding_shift);                             \
1034
130k
      res_32 = convolve_rounding(&res_32, &offset_const, &rounding_const,     \
1035
130k
                                 rounding_shift);                             \
1036
130k
                                                                              \
1037
130k
      __m256i res = _mm256_packus_epi16(res_10, res_32);                      \
1038
130k
      const __m128i res_20 = _mm256_castsi256_si128(res);                     \
1039
130k
      const __m128i res_31 = _mm256_extracti128_si256(res, 1);                \
1040
130k
                                                                              \
1041
130k
      _mm_storel_epi64((__m128i *)(&dst0[0 * dst_stride0]), res_20);          \
1042
130k
      _mm_storel_epi64((__m128i *)((&dst0[1 * dst_stride0])), res_31);        \
1043
130k
      _mm_storeh_epi64((__m128i *)(&dst0[2 * dst_stride0]), res_20);          \
1044
130k
      _mm_storeh_epi64((__m128i *)((&dst0[3 * dst_stride0])), res_31);        \
1045
130k
      i -= 4;                                                                 \
1046
130k
      src += 4 * src_stride;                                                  \
1047
130k
      dst += 4 * dst_stride;                                                  \
1048
130k
      dst0 += 4 * dst_stride0;                                                \
1049
130k
    } while (i);                                                              \
1050
50.4k
  } else {                                                                    \
1051
33.8k
    assert(w == 4);                                                           \
1052
50.3k
    do {                                                                      \
1053
50.3k
      __m256i src_3210_8bit =                                                 \
1054
50.3k
          _mm256_setr_epi32(loadu_int32(src + 0 * src_stride),                \
1055
50.3k
                            loadu_int32(src + 1 * src_stride), 0, 0,          \
1056
50.3k
                            loadu_int32(src + 2 * src_stride),                \
1057
50.3k
                            loadu_int32(src + 3 * src_stride), 0, 0);         \
1058
50.3k
                                                                              \
1059
50.3k
      __m256i src_3210 = _mm256_unpacklo_epi8(src_3210_8bit, zero);           \
1060
50.3k
      src_3210 = _mm256_slli_epi16(src_3210, LEFT_SHIFT);                     \
1061
50.3k
      src_3210 = _mm256_add_epi16(src_3210, offset_const);                    \
1062
50.3k
                                                                              \
1063
50.3k
      __m256i ref_3210 =                                                      \
1064
50.3k
          _mm256_setr_epi64x(*(int64_t *)(dst + 0 * dst_stride),              \
1065
50.3k
                             *(int64_t *)(dst + 1 * dst_stride),              \
1066
50.3k
                             *(int64_t *)(dst + 2 * dst_stride),              \
1067
50.3k
                             *(int64_t *)(dst + 3 * dst_stride));             \
1068
50.3k
      __m256i res_3210 =                                                      \
1069
50.3k
          comp_avg(&ref_3210, &src_3210, &wt, USE_DIST_WEIGHTED);             \
1070
50.3k
                                                                              \
1071
50.3k
      res_3210 = convolve_rounding(&res_3210, &offset_const, &rounding_const, \
1072
50.3k
                                   rounding_shift);                           \
1073
50.3k
                                                                              \
1074
50.3k
      res_3210 = _mm256_packus_epi16(res_3210, res_3210);                     \
1075
50.3k
      const __m128i res_10 = _mm256_castsi256_si128(res_3210);                \
1076
50.3k
      const __m128i res_32 = _mm256_extracti128_si256(res_3210, 1);           \
1077
50.3k
                                                                              \
1078
50.3k
      *(int *)(&dst0[0 * dst_stride0]) = _mm_cvtsi128_si32(res_10);           \
1079
50.3k
      *(int *)(&dst0[2 * dst_stride0]) = _mm_cvtsi128_si32(res_32);           \
1080
50.3k
      *(int *)(&dst0[1 * dst_stride0]) = _mm_extract_epi32(res_10, 1);        \
1081
50.3k
      *(int *)(&dst0[3 * dst_stride0]) = _mm_extract_epi32(res_32, 1);        \
1082
50.3k
      i -= 4;                                                                 \
1083
50.3k
      src += 4 * src_stride;                                                  \
1084
50.3k
      dst += 4 * dst_stride;                                                  \
1085
50.3k
      dst0 += 4 * dst_stride0;                                                \
1086
50.3k
    } while (i);                                                              \
1087
33.8k
  }
1088
1089
void av1_dist_wtd_convolve_2d_copy_avx2(const uint8_t *src, int src_stride,
1090
                                        uint8_t *dst0, int dst_stride0, int w,
1091
915k
                                        int h, ConvolveParams *conv_params) {
1092
915k
  const int bd = 8;
1093
915k
  CONV_BUF_TYPE *dst = conv_params->dst;
1094
915k
  int dst_stride = conv_params->dst_stride;
1095
915k
  assert(conv_params->round_0 == 3);
1096
915k
  assert(conv_params->round_1 == 7);
1097
915k
  assert(w % 4 == 0);
1098
915k
  assert(h % 4 == 0);
1099
1100
915k
  const int do_average = conv_params->do_average;
1101
915k
  const int use_dist_wtd_comp_avg = conv_params->use_dist_wtd_comp_avg;
1102
915k
  const __m256i wt = unpack_weights_avx2(conv_params);
1103
915k
  const __m256i zero = _mm256_setzero_si256();
1104
1105
915k
  const int offset_0 =
1106
915k
      bd + 2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
1107
915k
  const int offset = (1 << offset_0) + (1 << (offset_0 - 1));
1108
915k
  const __m256i offset_const = _mm256_set1_epi16(offset);
1109
915k
  const int rounding_shift =
1110
915k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
1111
915k
  const __m256i rounding_const = _mm256_set1_epi16((1 << rounding_shift) >> 1);
1112
1113
915k
  if (do_average) {
1114
399k
    if (use_dist_wtd_comp_avg) {
1115
61.3k
      DO_AVG_2D_COPY(1)
1116
338k
    } else {
1117
338k
      DO_AVG_2D_COPY(0)
1118
338k
    }
1119
515k
  } else {
1120
515k
    av1_dist_wtd_convolve_2d_no_avg_copy_avx2(src, src_stride, dst, dst_stride,
1121
515k
                                              w, h, offset_const);
1122
515k
  }
1123
915k
}
1124
#undef LEFT_SHIFT