Coverage Report

Created: 2026-03-31 06:59

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