Coverage Report

Created: 2026-02-14 07:00

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