Coverage Report

Created: 2025-11-16 07:09

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