Coverage Report

Created: 2025-11-09 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vpx_dsp/x86/highbd_convolve_avx2.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2017 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include <immintrin.h>
12
#include "./vpx_dsp_rtcd.h"
13
#include "vpx_dsp/x86/convolve.h"
14
#include "vpx_dsp/x86/convolve_avx2.h"
15
16
// -----------------------------------------------------------------------------
17
// Copy and average
18
19
void vpx_highbd_convolve_copy_avx2(const uint16_t *src, ptrdiff_t src_stride,
20
                                   uint16_t *dst, ptrdiff_t dst_stride,
21
                                   const InterpKernel *filter, int x0_q4,
22
                                   int x_step_q4, int y0_q4, int y_step_q4,
23
842k
                                   int w, int h, int bd) {
24
842k
  (void)filter;
25
842k
  (void)x0_q4;
26
842k
  (void)x_step_q4;
27
842k
  (void)y0_q4;
28
842k
  (void)y_step_q4;
29
842k
  (void)bd;
30
31
842k
  assert(w % 4 == 0);
32
842k
  if (w > 32) {  // w = 64
33
22.0M
    do {
34
22.0M
      const __m256i p0 = _mm256_loadu_si256((const __m256i *)src);
35
22.0M
      const __m256i p1 = _mm256_loadu_si256((const __m256i *)(src + 16));
36
22.0M
      const __m256i p2 = _mm256_loadu_si256((const __m256i *)(src + 32));
37
22.0M
      const __m256i p3 = _mm256_loadu_si256((const __m256i *)(src + 48));
38
22.0M
      src += src_stride;
39
22.0M
      _mm256_storeu_si256((__m256i *)dst, p0);
40
22.0M
      _mm256_storeu_si256((__m256i *)(dst + 16), p1);
41
22.0M
      _mm256_storeu_si256((__m256i *)(dst + 32), p2);
42
22.0M
      _mm256_storeu_si256((__m256i *)(dst + 48), p3);
43
22.0M
      dst += dst_stride;
44
22.0M
      h--;
45
22.0M
    } while (h > 0);
46
425k
  } else if (w > 16) {  // w = 32
47
2.92M
    do {
48
2.92M
      const __m256i p0 = _mm256_loadu_si256((const __m256i *)src);
49
2.92M
      const __m256i p1 = _mm256_loadu_si256((const __m256i *)(src + 16));
50
2.92M
      src += src_stride;
51
2.92M
      _mm256_storeu_si256((__m256i *)dst, p0);
52
2.92M
      _mm256_storeu_si256((__m256i *)(dst + 16), p1);
53
2.92M
      dst += dst_stride;
54
2.92M
      h--;
55
2.92M
    } while (h > 0);
56
318k
  } else if (w > 8) {  // w = 16
57
75.8k
    __m256i p0, p1;
58
573k
    do {
59
573k
      p0 = _mm256_loadu_si256((const __m256i *)src);
60
573k
      src += src_stride;
61
573k
      p1 = _mm256_loadu_si256((const __m256i *)src);
62
573k
      src += src_stride;
63
64
573k
      _mm256_storeu_si256((__m256i *)dst, p0);
65
573k
      dst += dst_stride;
66
573k
      _mm256_storeu_si256((__m256i *)dst, p1);
67
573k
      dst += dst_stride;
68
573k
      h -= 2;
69
573k
    } while (h > 0);
70
242k
  } else if (w > 4) {  // w = 8
71
94.7k
    __m128i p0, p1;
72
390k
    do {
73
390k
      p0 = _mm_loadu_si128((const __m128i *)src);
74
390k
      src += src_stride;
75
390k
      p1 = _mm_loadu_si128((const __m128i *)src);
76
390k
      src += src_stride;
77
78
390k
      _mm_storeu_si128((__m128i *)dst, p0);
79
390k
      dst += dst_stride;
80
390k
      _mm_storeu_si128((__m128i *)dst, p1);
81
390k
      dst += dst_stride;
82
390k
      h -= 2;
83
390k
    } while (h > 0);
84
148k
  } else {  // w = 4
85
148k
    __m128i p0, p1;
86
343k
    do {
87
343k
      p0 = _mm_loadl_epi64((const __m128i *)src);
88
343k
      src += src_stride;
89
343k
      p1 = _mm_loadl_epi64((const __m128i *)src);
90
343k
      src += src_stride;
91
92
343k
      _mm_storel_epi64((__m128i *)dst, p0);
93
343k
      dst += dst_stride;
94
343k
      _mm_storel_epi64((__m128i *)dst, p1);
95
343k
      dst += dst_stride;
96
343k
      h -= 2;
97
343k
    } while (h > 0);
98
148k
  }
99
842k
}
100
101
void vpx_highbd_convolve_avg_avx2(const uint16_t *src, ptrdiff_t src_stride,
102
                                  uint16_t *dst, ptrdiff_t dst_stride,
103
                                  const InterpKernel *filter, int x0_q4,
104
                                  int x_step_q4, int y0_q4, int y_step_q4,
105
98.4k
                                  int w, int h, int bd) {
106
98.4k
  (void)filter;
107
98.4k
  (void)x0_q4;
108
98.4k
  (void)x_step_q4;
109
98.4k
  (void)y0_q4;
110
98.4k
  (void)y_step_q4;
111
98.4k
  (void)bd;
112
113
98.4k
  assert(w % 4 == 0);
114
98.4k
  if (w > 32) {  // w = 64
115
7.06k
    __m256i p0, p1, p2, p3, u0, u1, u2, u3;
116
413k
    do {
117
413k
      p0 = _mm256_loadu_si256((const __m256i *)src);
118
413k
      p1 = _mm256_loadu_si256((const __m256i *)(src + 16));
119
413k
      p2 = _mm256_loadu_si256((const __m256i *)(src + 32));
120
413k
      p3 = _mm256_loadu_si256((const __m256i *)(src + 48));
121
413k
      src += src_stride;
122
413k
      u0 = _mm256_loadu_si256((const __m256i *)dst);
123
413k
      u1 = _mm256_loadu_si256((const __m256i *)(dst + 16));
124
413k
      u2 = _mm256_loadu_si256((const __m256i *)(dst + 32));
125
413k
      u3 = _mm256_loadu_si256((const __m256i *)(dst + 48));
126
413k
      _mm256_storeu_si256((__m256i *)dst, _mm256_avg_epu16(p0, u0));
127
413k
      _mm256_storeu_si256((__m256i *)(dst + 16), _mm256_avg_epu16(p1, u1));
128
413k
      _mm256_storeu_si256((__m256i *)(dst + 32), _mm256_avg_epu16(p2, u2));
129
413k
      _mm256_storeu_si256((__m256i *)(dst + 48), _mm256_avg_epu16(p3, u3));
130
413k
      dst += dst_stride;
131
413k
      h--;
132
413k
    } while (h > 0);
133
91.3k
  } else if (w > 16) {  // w = 32
134
11.7k
    __m256i p0, p1, u0, u1;
135
348k
    do {
136
348k
      p0 = _mm256_loadu_si256((const __m256i *)src);
137
348k
      p1 = _mm256_loadu_si256((const __m256i *)(src + 16));
138
348k
      src += src_stride;
139
348k
      u0 = _mm256_loadu_si256((const __m256i *)dst);
140
348k
      u1 = _mm256_loadu_si256((const __m256i *)(dst + 16));
141
348k
      _mm256_storeu_si256((__m256i *)dst, _mm256_avg_epu16(p0, u0));
142
348k
      _mm256_storeu_si256((__m256i *)(dst + 16), _mm256_avg_epu16(p1, u1));
143
348k
      dst += dst_stride;
144
348k
      h--;
145
348k
    } while (h > 0);
146
79.6k
  } else if (w > 8) {  // w = 16
147
15.4k
    __m256i p0, p1, u0, u1;
148
108k
    do {
149
108k
      p0 = _mm256_loadu_si256((const __m256i *)src);
150
108k
      p1 = _mm256_loadu_si256((const __m256i *)(src + src_stride));
151
108k
      src += src_stride << 1;
152
108k
      u0 = _mm256_loadu_si256((const __m256i *)dst);
153
108k
      u1 = _mm256_loadu_si256((const __m256i *)(dst + dst_stride));
154
155
108k
      _mm256_storeu_si256((__m256i *)dst, _mm256_avg_epu16(p0, u0));
156
108k
      _mm256_storeu_si256((__m256i *)(dst + dst_stride),
157
108k
                          _mm256_avg_epu16(p1, u1));
158
108k
      dst += dst_stride << 1;
159
108k
      h -= 2;
160
108k
    } while (h > 0);
161
64.1k
  } else if (w > 4) {  // w = 8
162
21.6k
    __m128i p0, p1, u0, u1;
163
91.3k
    do {
164
91.3k
      p0 = _mm_loadu_si128((const __m128i *)src);
165
91.3k
      p1 = _mm_loadu_si128((const __m128i *)(src + src_stride));
166
91.3k
      src += src_stride << 1;
167
91.3k
      u0 = _mm_loadu_si128((const __m128i *)dst);
168
91.3k
      u1 = _mm_loadu_si128((const __m128i *)(dst + dst_stride));
169
170
91.3k
      _mm_storeu_si128((__m128i *)dst, _mm_avg_epu16(p0, u0));
171
91.3k
      _mm_storeu_si128((__m128i *)(dst + dst_stride), _mm_avg_epu16(p1, u1));
172
91.3k
      dst += dst_stride << 1;
173
91.3k
      h -= 2;
174
91.3k
    } while (h > 0);
175
42.5k
  } else {  // w = 4
176
42.5k
    __m128i p0, p1, u0, u1;
177
89.6k
    do {
178
89.6k
      p0 = _mm_loadl_epi64((const __m128i *)src);
179
89.6k
      p1 = _mm_loadl_epi64((const __m128i *)(src + src_stride));
180
89.6k
      src += src_stride << 1;
181
89.6k
      u0 = _mm_loadl_epi64((const __m128i *)dst);
182
89.6k
      u1 = _mm_loadl_epi64((const __m128i *)(dst + dst_stride));
183
184
89.6k
      _mm_storel_epi64((__m128i *)dst, _mm_avg_epu16(u0, p0));
185
89.6k
      _mm_storel_epi64((__m128i *)(dst + dst_stride), _mm_avg_epu16(u1, p1));
186
89.6k
      dst += dst_stride << 1;
187
89.6k
      h -= 2;
188
89.6k
    } while (h > 0);
189
42.5k
  }
190
98.4k
}
191
192
// -----------------------------------------------------------------------------
193
// Horizontal and vertical filtering
194
195
static const uint8_t signal_pattern_0[32] = { 0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6,
196
                                              7, 6, 7, 8, 9, 0, 1, 2, 3, 2, 3,
197
                                              4, 5, 4, 5, 6, 7, 6, 7, 8, 9 };
198
199
static const uint8_t signal_pattern_1[32] = { 4, 5, 6,  7,  6,  7,  8,  9,
200
                                              8, 9, 10, 11, 10, 11, 12, 13,
201
                                              4, 5, 6,  7,  6,  7,  8,  9,
202
                                              8, 9, 10, 11, 10, 11, 12, 13 };
203
204
static const uint8_t signal_pattern_2[32] = { 6,  7,  8,  9,  8,  9,  10, 11,
205
                                              10, 11, 12, 13, 12, 13, 14, 15,
206
                                              6,  7,  8,  9,  8,  9,  10, 11,
207
                                              10, 11, 12, 13, 12, 13, 14, 15 };
208
209
static const uint32_t signal_index[8] = { 2, 3, 4, 5, 2, 3, 4, 5 };
210
211
97.6M
#define CONV8_ROUNDING_BITS (7)
212
0
#define CONV8_ROUNDING_NUM (1 << (CONV8_ROUNDING_BITS - 1))
213
214
// -----------------------------------------------------------------------------
215
// Horizontal Filtering
216
217
23.2M
static INLINE void pack_pixels(const __m256i *s, __m256i *p /*p[4]*/) {
218
23.2M
  const __m256i idx = _mm256_loadu_si256((const __m256i *)signal_index);
219
23.2M
  const __m256i sf0 = _mm256_loadu_si256((const __m256i *)signal_pattern_0);
220
23.2M
  const __m256i sf1 = _mm256_loadu_si256((const __m256i *)signal_pattern_1);
221
23.2M
  const __m256i c = _mm256_permutevar8x32_epi32(*s, idx);
222
223
23.2M
  p[0] = _mm256_shuffle_epi8(*s, sf0);  // x0x6
224
23.2M
  p[1] = _mm256_shuffle_epi8(*s, sf1);  // x1x7
225
23.2M
  p[2] = _mm256_shuffle_epi8(c, sf0);   // x2x4
226
23.2M
  p[3] = _mm256_shuffle_epi8(c, sf1);   // x3x5
227
23.2M
}
228
229
// Note:
230
//  Shared by 8x2 and 16x1 block
231
static INLINE void pack_16_pixels(const __m256i *s0, const __m256i *s1,
232
11.5M
                                  __m256i *x /*x[8]*/) {
233
11.5M
  __m256i pp[8];
234
11.5M
  pack_pixels(s0, pp);
235
11.5M
  pack_pixels(s1, &pp[4]);
236
11.5M
  x[0] = _mm256_permute2x128_si256(pp[0], pp[4], 0x20);
237
11.5M
  x[1] = _mm256_permute2x128_si256(pp[1], pp[5], 0x20);
238
11.5M
  x[2] = _mm256_permute2x128_si256(pp[2], pp[6], 0x20);
239
11.5M
  x[3] = _mm256_permute2x128_si256(pp[3], pp[7], 0x20);
240
11.5M
  x[4] = x[2];
241
11.5M
  x[5] = x[3];
242
11.5M
  x[6] = _mm256_permute2x128_si256(pp[0], pp[4], 0x31);
243
11.5M
  x[7] = _mm256_permute2x128_si256(pp[1], pp[5], 0x31);
244
11.5M
}
245
246
124k
static INLINE void pack_8x1_pixels(const uint16_t *src, __m256i *x) {
247
124k
  __m256i pp[8];
248
124k
  __m256i s0;
249
124k
  s0 = _mm256_loadu_si256((const __m256i *)src);
250
124k
  pack_pixels(&s0, pp);
251
124k
  x[0] = _mm256_permute2x128_si256(pp[0], pp[2], 0x30);
252
124k
  x[1] = _mm256_permute2x128_si256(pp[1], pp[3], 0x30);
253
124k
  x[2] = _mm256_permute2x128_si256(pp[2], pp[0], 0x30);
254
124k
  x[3] = _mm256_permute2x128_si256(pp[3], pp[1], 0x30);
255
124k
}
256
257
static INLINE void pack_8x2_pixels(const uint16_t *src, ptrdiff_t stride,
258
1.01M
                                   __m256i *x) {
259
1.01M
  __m256i s0, s1;
260
1.01M
  s0 = _mm256_loadu_si256((const __m256i *)src);
261
1.01M
  s1 = _mm256_loadu_si256((const __m256i *)(src + stride));
262
1.01M
  pack_16_pixels(&s0, &s1, x);
263
1.01M
}
264
265
10.6M
static INLINE void pack_16x1_pixels(const uint16_t *src, __m256i *x) {
266
10.6M
  __m256i s0, s1;
267
10.6M
  s0 = _mm256_loadu_si256((const __m256i *)src);
268
10.6M
  s1 = _mm256_loadu_si256((const __m256i *)(src + 8));
269
10.6M
  pack_16_pixels(&s0, &s1, x);
270
10.6M
}
271
272
// Note:
273
//  Shared by horizontal and vertical filtering
274
817k
static INLINE void pack_filters(const int16_t *filter, __m256i *f /*f[4]*/) {
275
817k
  const __m128i h = _mm_loadu_si128((const __m128i *)filter);
276
817k
  const __m256i hh = _mm256_insertf128_si256(_mm256_castsi128_si256(h), h, 1);
277
817k
  const __m256i p0 = _mm256_set1_epi32(0x03020100);
278
817k
  const __m256i p1 = _mm256_set1_epi32(0x07060504);
279
817k
  const __m256i p2 = _mm256_set1_epi32(0x0b0a0908);
280
817k
  const __m256i p3 = _mm256_set1_epi32(0x0f0e0d0c);
281
817k
  f[0] = _mm256_shuffle_epi8(hh, p0);
282
817k
  f[1] = _mm256_shuffle_epi8(hh, p1);
283
817k
  f[2] = _mm256_shuffle_epi8(hh, p2);
284
817k
  f[3] = _mm256_shuffle_epi8(hh, p3);
285
817k
}
286
287
static INLINE void filter_8x1_pixels(const __m256i *sig /*sig[4]*/,
288
                                     const __m256i *fil /*fil[4]*/,
289
37.8M
                                     __m256i *y) {
290
37.8M
  __m256i a, a0, a1;
291
292
37.8M
  a0 = _mm256_madd_epi16(fil[0], sig[0]);
293
37.8M
  a1 = _mm256_madd_epi16(fil[3], sig[3]);
294
37.8M
  a = _mm256_add_epi32(a0, a1);
295
296
37.8M
  a0 = _mm256_madd_epi16(fil[1], sig[1]);
297
37.8M
  a1 = _mm256_madd_epi16(fil[2], sig[2]);
298
299
37.8M
  {
300
37.8M
    const __m256i min = _mm256_min_epi32(a0, a1);
301
37.8M
    a = _mm256_add_epi32(a, min);
302
37.8M
  }
303
37.8M
  {
304
37.8M
    const __m256i max = _mm256_max_epi32(a0, a1);
305
37.8M
    a = _mm256_add_epi32(a, max);
306
37.8M
  }
307
37.8M
  {
308
37.8M
    const __m256i rounding = _mm256_set1_epi32(1 << (CONV8_ROUNDING_BITS - 1));
309
37.8M
    a = _mm256_add_epi32(a, rounding);
310
37.8M
    *y = _mm256_srai_epi32(a, CONV8_ROUNDING_BITS);
311
37.8M
  }
312
37.8M
}
313
314
static INLINE void store_8x1_pixels(const __m256i *y, const __m256i *mask,
315
206k
                                    uint16_t *dst) {
316
206k
  const __m128i a0 = _mm256_castsi256_si128(*y);
317
206k
  const __m128i a1 = _mm256_extractf128_si256(*y, 1);
318
206k
  __m128i res = _mm_packus_epi32(a0, a1);
319
206k
  res = _mm_min_epi16(res, _mm256_castsi256_si128(*mask));
320
206k
  _mm_storeu_si128((__m128i *)dst, res);
321
206k
}
322
323
static INLINE void store_8x2_pixels(const __m256i *y0, const __m256i *y1,
324
                                    const __m256i *mask, uint16_t *dst,
325
1.89M
                                    ptrdiff_t pitch) {
326
1.89M
  __m256i a = _mm256_packus_epi32(*y0, *y1);
327
1.89M
  a = _mm256_min_epi16(a, *mask);
328
1.89M
  _mm_storeu_si128((__m128i *)dst, _mm256_castsi256_si128(a));
329
1.89M
  _mm_storeu_si128((__m128i *)(dst + pitch), _mm256_extractf128_si256(a, 1));
330
1.89M
}
331
332
static INLINE void store_16x1_pixels(const __m256i *y0, const __m256i *y1,
333
14.6M
                                     const __m256i *mask, uint16_t *dst) {
334
14.6M
  __m256i a = _mm256_packus_epi32(*y0, *y1);
335
14.6M
  a = _mm256_min_epi16(a, *mask);
336
14.6M
  _mm256_storeu_si256((__m256i *)dst, a);
337
14.6M
}
338
339
static void vpx_highbd_filter_block1d8_h8_avx2(
340
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
341
152k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
342
152k
  __m256i signal[8], res0, res1;
343
152k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
344
345
152k
  __m256i ff[4];
346
152k
  pack_filters(filter, ff);
347
348
152k
  src_ptr -= 3;
349
991k
  do {
350
991k
    pack_8x2_pixels(src_ptr, src_pitch, signal);
351
991k
    filter_8x1_pixels(signal, ff, &res0);
352
991k
    filter_8x1_pixels(&signal[4], ff, &res1);
353
991k
    store_8x2_pixels(&res0, &res1, &max, dst_ptr, dst_pitch);
354
991k
    height -= 2;
355
991k
    src_ptr += src_pitch << 1;
356
991k
    dst_ptr += dst_pitch << 1;
357
991k
  } while (height > 1);
358
359
152k
  if (height > 0) {
360
125k
    pack_8x1_pixels(src_ptr, signal);
361
125k
    filter_8x1_pixels(signal, ff, &res0);
362
125k
    store_8x1_pixels(&res0, &max, dst_ptr);
363
125k
  }
364
152k
}
365
366
static void vpx_highbd_filter_block1d16_h8_avx2(
367
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
368
290k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
369
290k
  __m256i signal[8], res0, res1;
370
290k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
371
372
290k
  __m256i ff[4];
373
290k
  pack_filters(filter, ff);
374
375
290k
  src_ptr -= 3;
376
10.5M
  do {
377
10.5M
    pack_16x1_pixels(src_ptr, signal);
378
10.5M
    filter_8x1_pixels(signal, ff, &res0);
379
10.5M
    filter_8x1_pixels(&signal[4], ff, &res1);
380
10.5M
    store_16x1_pixels(&res0, &res1, &max, dst_ptr);
381
10.5M
    height -= 1;
382
10.5M
    src_ptr += src_pitch;
383
10.5M
    dst_ptr += dst_pitch;
384
10.5M
  } while (height > 0);
385
290k
}
386
387
// -----------------------------------------------------------------------------
388
// 2-tap horizontal filtering
389
390
323k
static INLINE void pack_2t_filter(const int16_t *filter, __m256i *f) {
391
323k
  const __m128i h = _mm_loadu_si128((const __m128i *)filter);
392
323k
  const __m256i hh = _mm256_insertf128_si256(_mm256_castsi128_si256(h), h, 1);
393
323k
  const __m256i p = _mm256_set1_epi32(0x09080706);
394
323k
  f[0] = _mm256_shuffle_epi8(hh, p);
395
323k
}
396
397
// can be used by pack_8x2_2t_pixels() and pack_16x1_2t_pixels()
398
// the difference is s0/s1 specifies first and second rows or,
399
// first 16 samples and 8-sample shifted 16 samples
400
static INLINE void pack_16_2t_pixels(const __m256i *s0, const __m256i *s1,
401
3.83M
                                     __m256i *sig) {
402
3.83M
  const __m256i idx = _mm256_loadu_si256((const __m256i *)signal_index);
403
3.83M
  const __m256i sf2 = _mm256_loadu_si256((const __m256i *)signal_pattern_2);
404
3.83M
  __m256i x0 = _mm256_shuffle_epi8(*s0, sf2);
405
3.83M
  __m256i x1 = _mm256_shuffle_epi8(*s1, sf2);
406
3.83M
  __m256i r0 = _mm256_permutevar8x32_epi32(*s0, idx);
407
3.83M
  __m256i r1 = _mm256_permutevar8x32_epi32(*s1, idx);
408
3.83M
  r0 = _mm256_shuffle_epi8(r0, sf2);
409
3.83M
  r1 = _mm256_shuffle_epi8(r1, sf2);
410
3.83M
  sig[0] = _mm256_permute2x128_si256(x0, x1, 0x20);
411
3.83M
  sig[1] = _mm256_permute2x128_si256(r0, r1, 0x20);
412
3.83M
}
413
414
static INLINE void pack_8x2_2t_pixels(const uint16_t *src,
415
415k
                                      const ptrdiff_t pitch, __m256i *sig) {
416
415k
  const __m256i r0 = _mm256_loadu_si256((const __m256i *)src);
417
415k
  const __m256i r1 = _mm256_loadu_si256((const __m256i *)(src + pitch));
418
415k
  pack_16_2t_pixels(&r0, &r1, sig);
419
415k
}
420
421
static INLINE void pack_16x1_2t_pixels(const uint16_t *src,
422
3.41M
                                       __m256i *sig /*sig[2]*/) {
423
3.41M
  const __m256i r0 = _mm256_loadu_si256((const __m256i *)src);
424
3.41M
  const __m256i r1 = _mm256_loadu_si256((const __m256i *)(src + 8));
425
3.41M
  pack_16_2t_pixels(&r0, &r1, sig);
426
3.41M
}
427
428
static INLINE void pack_8x1_2t_pixels(const uint16_t *src,
429
81.3k
                                      __m256i *sig /*sig[2]*/) {
430
81.3k
  const __m256i idx = _mm256_loadu_si256((const __m256i *)signal_index);
431
81.3k
  const __m256i sf2 = _mm256_loadu_si256((const __m256i *)signal_pattern_2);
432
81.3k
  __m256i r0 = _mm256_loadu_si256((const __m256i *)src);
433
81.3k
  __m256i x0 = _mm256_shuffle_epi8(r0, sf2);
434
81.3k
  r0 = _mm256_permutevar8x32_epi32(r0, idx);
435
81.3k
  r0 = _mm256_shuffle_epi8(r0, sf2);
436
81.3k
  sig[0] = _mm256_permute2x128_si256(x0, r0, 0x20);
437
81.3k
}
438
439
// can be used by filter_8x2_2t_pixels() and filter_16x1_2t_pixels()
440
static INLINE void filter_16_2t_pixels(const __m256i *sig, const __m256i *f,
441
6.49M
                                       __m256i *y0, __m256i *y1) {
442
6.49M
  const __m256i rounding = _mm256_set1_epi32(1 << (CONV8_ROUNDING_BITS - 1));
443
6.49M
  __m256i x0 = _mm256_madd_epi16(sig[0], *f);
444
6.49M
  __m256i x1 = _mm256_madd_epi16(sig[1], *f);
445
6.49M
  x0 = _mm256_add_epi32(x0, rounding);
446
6.49M
  x1 = _mm256_add_epi32(x1, rounding);
447
6.49M
  *y0 = _mm256_srai_epi32(x0, CONV8_ROUNDING_BITS);
448
6.49M
  *y1 = _mm256_srai_epi32(x1, CONV8_ROUNDING_BITS);
449
6.49M
}
450
451
static INLINE void filter_8x1_2t_pixels(const __m256i *sig, const __m256i *f,
452
81.3k
                                        __m256i *y0) {
453
81.3k
  const __m256i rounding = _mm256_set1_epi32(1 << (CONV8_ROUNDING_BITS - 1));
454
81.3k
  __m256i x0 = _mm256_madd_epi16(sig[0], *f);
455
81.3k
  x0 = _mm256_add_epi32(x0, rounding);
456
81.3k
  *y0 = _mm256_srai_epi32(x0, CONV8_ROUNDING_BITS);
457
81.3k
}
458
459
static void vpx_highbd_filter_block1d8_h2_avx2(
460
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
461
95.2k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
462
95.2k
  __m256i signal[2], res0, res1;
463
95.2k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
464
465
95.2k
  __m256i ff;
466
95.2k
  pack_2t_filter(filter, &ff);
467
468
95.2k
  src_ptr -= 3;
469
389k
  do {
470
389k
    pack_8x2_2t_pixels(src_ptr, src_pitch, signal);
471
389k
    filter_16_2t_pixels(signal, &ff, &res0, &res1);
472
389k
    store_8x2_pixels(&res0, &res1, &max, dst_ptr, dst_pitch);
473
389k
    height -= 2;
474
389k
    src_ptr += src_pitch << 1;
475
389k
    dst_ptr += dst_pitch << 1;
476
389k
  } while (height > 1);
477
478
95.2k
  if (height > 0) {
479
81.3k
    pack_8x1_2t_pixels(src_ptr, signal);
480
81.3k
    filter_8x1_2t_pixels(signal, &ff, &res0);
481
81.3k
    store_8x1_pixels(&res0, &max, dst_ptr);
482
81.3k
  }
483
95.2k
}
484
485
static void vpx_highbd_filter_block1d16_h2_avx2(
486
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
487
103k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
488
103k
  __m256i signal[2], res0, res1;
489
103k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
490
491
103k
  __m256i ff;
492
103k
  pack_2t_filter(filter, &ff);
493
494
103k
  src_ptr -= 3;
495
2.87M
  do {
496
2.87M
    pack_16x1_2t_pixels(src_ptr, signal);
497
2.87M
    filter_16_2t_pixels(signal, &ff, &res0, &res1);
498
2.87M
    store_16x1_pixels(&res0, &res1, &max, dst_ptr);
499
2.87M
    height -= 1;
500
2.87M
    src_ptr += src_pitch;
501
2.87M
    dst_ptr += dst_pitch;
502
2.87M
  } while (height > 0);
503
103k
}
504
505
// -----------------------------------------------------------------------------
506
// Vertical Filtering
507
508
163k
static void pack_8x9_init(const uint16_t *src, ptrdiff_t pitch, __m256i *sig) {
509
163k
  __m256i s0 = _mm256_castsi128_si256(_mm_loadu_si128((const __m128i *)src));
510
163k
  __m256i s1 =
511
163k
      _mm256_castsi128_si256(_mm_loadu_si128((const __m128i *)(src + pitch)));
512
163k
  __m256i s2 = _mm256_castsi128_si256(
513
163k
      _mm_loadu_si128((const __m128i *)(src + 2 * pitch)));
514
163k
  __m256i s3 = _mm256_castsi128_si256(
515
163k
      _mm_loadu_si128((const __m128i *)(src + 3 * pitch)));
516
163k
  __m256i s4 = _mm256_castsi128_si256(
517
163k
      _mm_loadu_si128((const __m128i *)(src + 4 * pitch)));
518
163k
  __m256i s5 = _mm256_castsi128_si256(
519
163k
      _mm_loadu_si128((const __m128i *)(src + 5 * pitch)));
520
163k
  __m256i s6 = _mm256_castsi128_si256(
521
163k
      _mm_loadu_si128((const __m128i *)(src + 6 * pitch)));
522
523
163k
  s0 = _mm256_inserti128_si256(s0, _mm256_castsi256_si128(s1), 1);
524
163k
  s1 = _mm256_inserti128_si256(s1, _mm256_castsi256_si128(s2), 1);
525
163k
  s2 = _mm256_inserti128_si256(s2, _mm256_castsi256_si128(s3), 1);
526
163k
  s3 = _mm256_inserti128_si256(s3, _mm256_castsi256_si128(s4), 1);
527
163k
  s4 = _mm256_inserti128_si256(s4, _mm256_castsi256_si128(s5), 1);
528
163k
  s5 = _mm256_inserti128_si256(s5, _mm256_castsi256_si128(s6), 1);
529
530
163k
  sig[0] = _mm256_unpacklo_epi16(s0, s1);
531
163k
  sig[4] = _mm256_unpackhi_epi16(s0, s1);
532
163k
  sig[1] = _mm256_unpacklo_epi16(s2, s3);
533
163k
  sig[5] = _mm256_unpackhi_epi16(s2, s3);
534
163k
  sig[2] = _mm256_unpacklo_epi16(s4, s5);
535
163k
  sig[6] = _mm256_unpackhi_epi16(s4, s5);
536
163k
  sig[8] = s6;
537
163k
}
538
539
static INLINE void pack_8x9_pixels(const uint16_t *src, ptrdiff_t pitch,
540
653k
                                   __m256i *sig) {
541
  // base + 7th row
542
653k
  __m256i s0 = _mm256_castsi128_si256(
543
653k
      _mm_loadu_si128((const __m128i *)(src + 7 * pitch)));
544
  // base + 8th row
545
653k
  __m256i s1 = _mm256_castsi128_si256(
546
653k
      _mm_loadu_si128((const __m128i *)(src + 8 * pitch)));
547
653k
  __m256i s2 = _mm256_inserti128_si256(sig[8], _mm256_castsi256_si128(s0), 1);
548
653k
  __m256i s3 = _mm256_inserti128_si256(s0, _mm256_castsi256_si128(s1), 1);
549
653k
  sig[3] = _mm256_unpacklo_epi16(s2, s3);
550
653k
  sig[7] = _mm256_unpackhi_epi16(s2, s3);
551
653k
  sig[8] = s1;
552
653k
}
553
554
static INLINE void filter_8x9_pixels(const __m256i *sig, const __m256i *f,
555
653k
                                     __m256i *y0, __m256i *y1) {
556
653k
  filter_8x1_pixels(sig, f, y0);
557
653k
  filter_8x1_pixels(&sig[4], f, y1);
558
653k
}
559
560
9.58M
static INLINE void update_pixels(__m256i *sig) {
561
9.58M
  int i;
562
38.3M
  for (i = 0; i < 3; ++i) {
563
28.7M
    sig[i] = sig[i + 1];
564
28.7M
    sig[i + 4] = sig[i + 5];
565
28.7M
  }
566
9.58M
}
567
568
static void vpx_highbd_filter_block1d8_v8_avx2(
569
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
570
132k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
571
132k
  __m256i signal[9], res0, res1;
572
132k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
573
574
132k
  __m256i ff[4];
575
132k
  pack_filters(filter, ff);
576
577
132k
  pack_8x9_init(src_ptr, src_pitch, signal);
578
579
526k
  do {
580
526k
    pack_8x9_pixels(src_ptr, src_pitch, signal);
581
582
526k
    filter_8x9_pixels(signal, ff, &res0, &res1);
583
526k
    store_8x2_pixels(&res0, &res1, &max, dst_ptr, dst_pitch);
584
526k
    update_pixels(signal);
585
586
526k
    src_ptr += src_pitch << 1;
587
526k
    dst_ptr += dst_pitch << 1;
588
526k
    height -= 2;
589
526k
  } while (height > 0);
590
132k
}
591
592
272k
static void pack_16x9_init(const uint16_t *src, ptrdiff_t pitch, __m256i *sig) {
593
272k
  __m256i u0, u1, u2, u3;
594
  // load 0-6 rows
595
272k
  const __m256i s0 = _mm256_loadu_si256((const __m256i *)src);
596
272k
  const __m256i s1 = _mm256_loadu_si256((const __m256i *)(src + pitch));
597
272k
  const __m256i s2 = _mm256_loadu_si256((const __m256i *)(src + 2 * pitch));
598
272k
  const __m256i s3 = _mm256_loadu_si256((const __m256i *)(src + 3 * pitch));
599
272k
  const __m256i s4 = _mm256_loadu_si256((const __m256i *)(src + 4 * pitch));
600
272k
  const __m256i s5 = _mm256_loadu_si256((const __m256i *)(src + 5 * pitch));
601
272k
  const __m256i s6 = _mm256_loadu_si256((const __m256i *)(src + 6 * pitch));
602
603
272k
  u0 = _mm256_permute2x128_si256(s0, s1, 0x20);  // 0, 1 low
604
272k
  u1 = _mm256_permute2x128_si256(s0, s1, 0x31);  // 0, 1 high
605
606
272k
  u2 = _mm256_permute2x128_si256(s1, s2, 0x20);  // 1, 2 low
607
272k
  u3 = _mm256_permute2x128_si256(s1, s2, 0x31);  // 1, 2 high
608
609
272k
  sig[0] = _mm256_unpacklo_epi16(u0, u2);
610
272k
  sig[4] = _mm256_unpackhi_epi16(u0, u2);
611
612
272k
  sig[8] = _mm256_unpacklo_epi16(u1, u3);
613
272k
  sig[12] = _mm256_unpackhi_epi16(u1, u3);
614
615
272k
  u0 = _mm256_permute2x128_si256(s2, s3, 0x20);
616
272k
  u1 = _mm256_permute2x128_si256(s2, s3, 0x31);
617
618
272k
  u2 = _mm256_permute2x128_si256(s3, s4, 0x20);
619
272k
  u3 = _mm256_permute2x128_si256(s3, s4, 0x31);
620
621
272k
  sig[1] = _mm256_unpacklo_epi16(u0, u2);
622
272k
  sig[5] = _mm256_unpackhi_epi16(u0, u2);
623
624
272k
  sig[9] = _mm256_unpacklo_epi16(u1, u3);
625
272k
  sig[13] = _mm256_unpackhi_epi16(u1, u3);
626
627
272k
  u0 = _mm256_permute2x128_si256(s4, s5, 0x20);
628
272k
  u1 = _mm256_permute2x128_si256(s4, s5, 0x31);
629
630
272k
  u2 = _mm256_permute2x128_si256(s5, s6, 0x20);
631
272k
  u3 = _mm256_permute2x128_si256(s5, s6, 0x31);
632
633
272k
  sig[2] = _mm256_unpacklo_epi16(u0, u2);
634
272k
  sig[6] = _mm256_unpackhi_epi16(u0, u2);
635
636
272k
  sig[10] = _mm256_unpacklo_epi16(u1, u3);
637
272k
  sig[14] = _mm256_unpackhi_epi16(u1, u3);
638
639
272k
  sig[16] = s6;
640
272k
}
641
642
static void pack_16x9_pixels(const uint16_t *src, ptrdiff_t pitch,
643
4.33M
                             __m256i *sig) {
644
  // base + 7th row
645
4.33M
  const __m256i s7 = _mm256_loadu_si256((const __m256i *)(src + 7 * pitch));
646
  // base + 8th row
647
4.33M
  const __m256i s8 = _mm256_loadu_si256((const __m256i *)(src + 8 * pitch));
648
649
4.33M
  __m256i u0, u1, u2, u3;
650
4.33M
  u0 = _mm256_permute2x128_si256(sig[16], s7, 0x20);
651
4.33M
  u1 = _mm256_permute2x128_si256(sig[16], s7, 0x31);
652
653
4.33M
  u2 = _mm256_permute2x128_si256(s7, s8, 0x20);
654
4.33M
  u3 = _mm256_permute2x128_si256(s7, s8, 0x31);
655
656
4.33M
  sig[3] = _mm256_unpacklo_epi16(u0, u2);
657
4.33M
  sig[7] = _mm256_unpackhi_epi16(u0, u2);
658
659
4.33M
  sig[11] = _mm256_unpacklo_epi16(u1, u3);
660
4.33M
  sig[15] = _mm256_unpackhi_epi16(u1, u3);
661
662
4.33M
  sig[16] = s8;
663
4.33M
}
664
665
static INLINE void filter_16x9_pixels(const __m256i *sig, const __m256i *f,
666
4.47M
                                      __m256i *y0, __m256i *y1) {
667
4.47M
  __m256i res[4];
668
4.47M
  int i;
669
22.3M
  for (i = 0; i < 4; ++i) {
670
17.9M
    filter_8x1_pixels(&sig[i << 2], f, &res[i]);
671
17.9M
  }
672
673
4.47M
  {
674
4.47M
    const __m256i l0l1 = _mm256_packus_epi32(res[0], res[1]);
675
4.47M
    const __m256i h0h1 = _mm256_packus_epi32(res[2], res[3]);
676
4.47M
    *y0 = _mm256_permute2x128_si256(l0l1, h0h1, 0x20);
677
4.47M
    *y1 = _mm256_permute2x128_si256(l0l1, h0h1, 0x31);
678
4.47M
  }
679
4.47M
}
680
681
static INLINE void store_16x2_pixels(const __m256i *y0, const __m256i *y1,
682
                                     const __m256i *mask, uint16_t *dst,
683
3.75M
                                     ptrdiff_t pitch) {
684
3.75M
  __m256i p = _mm256_min_epi16(*y0, *mask);
685
3.75M
  _mm256_storeu_si256((__m256i *)dst, p);
686
3.75M
  p = _mm256_min_epi16(*y1, *mask);
687
3.75M
  _mm256_storeu_si256((__m256i *)(dst + pitch), p);
688
3.75M
}
689
690
4.32M
static void update_16x9_pixels(__m256i *sig) {
691
4.32M
  update_pixels(&sig[0]);
692
4.32M
  update_pixels(&sig[8]);
693
4.32M
}
694
695
static void vpx_highbd_filter_block1d16_v8_avx2(
696
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
697
292k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
698
292k
  __m256i signal[17], res0, res1;
699
292k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
700
701
292k
  __m256i ff[4];
702
292k
  pack_filters(filter, ff);
703
704
292k
  pack_16x9_init(src_ptr, src_pitch, signal);
705
706
4.11M
  do {
707
4.11M
    pack_16x9_pixels(src_ptr, src_pitch, signal);
708
4.11M
    filter_16x9_pixels(signal, ff, &res0, &res1);
709
4.11M
    store_16x2_pixels(&res0, &res1, &max, dst_ptr, dst_pitch);
710
4.11M
    update_16x9_pixels(signal);
711
712
4.11M
    src_ptr += src_pitch << 1;
713
4.11M
    dst_ptr += dst_pitch << 1;
714
4.11M
    height -= 2;
715
4.11M
  } while (height > 0);
716
292k
}
717
718
// -----------------------------------------------------------------------------
719
// 2-tap vertical filtering
720
721
103k
static void pack_16x2_init(const uint16_t *src, __m256i *sig) {
722
103k
  sig[2] = _mm256_loadu_si256((const __m256i *)src);
723
103k
}
724
725
static INLINE void pack_16x2_2t_pixels(const uint16_t *src, ptrdiff_t pitch,
726
2.66M
                                       __m256i *sig) {
727
  // load the next row
728
2.66M
  const __m256i u = _mm256_loadu_si256((const __m256i *)(src + pitch));
729
2.66M
  sig[0] = _mm256_unpacklo_epi16(sig[2], u);
730
2.66M
  sig[1] = _mm256_unpackhi_epi16(sig[2], u);
731
2.66M
  sig[2] = u;
732
2.66M
}
733
734
static INLINE void filter_16x2_2t_pixels(const __m256i *sig, const __m256i *f,
735
2.66M
                                         __m256i *y0, __m256i *y1) {
736
2.66M
  filter_16_2t_pixels(sig, f, y0, y1);
737
2.66M
}
738
739
static void vpx_highbd_filter_block1d16_v2_avx2(
740
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
741
68.6k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
742
68.6k
  __m256i signal[3], res0, res1;
743
68.6k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
744
68.6k
  __m256i ff;
745
746
68.6k
  pack_2t_filter(filter, &ff);
747
68.6k
  pack_16x2_init(src_ptr, signal);
748
749
1.71M
  do {
750
1.71M
    pack_16x2_2t_pixels(src_ptr, src_pitch, signal);
751
1.71M
    filter_16x2_2t_pixels(signal, &ff, &res0, &res1);
752
1.71M
    store_16x1_pixels(&res0, &res1, &max, dst_ptr);
753
754
1.71M
    src_ptr += src_pitch;
755
1.71M
    dst_ptr += dst_pitch;
756
1.71M
    height -= 1;
757
1.71M
  } while (height > 0);
758
68.6k
}
759
760
98.3k
static INLINE void pack_8x1_2t_filter(const int16_t *filter, __m128i *f) {
761
98.3k
  const __m128i h = _mm_loadu_si128((const __m128i *)filter);
762
98.3k
  const __m128i p = _mm_set1_epi32(0x09080706);
763
98.3k
  f[0] = _mm_shuffle_epi8(h, p);
764
98.3k
}
765
766
98.3k
static void pack_8x2_init(const uint16_t *src, __m128i *sig) {
767
98.3k
  sig[2] = _mm_loadu_si128((const __m128i *)src);
768
98.3k
}
769
770
static INLINE void pack_8x2_2t_pixels_ver(const uint16_t *src, ptrdiff_t pitch,
771
805k
                                          __m128i *sig) {
772
  // load the next row
773
805k
  const __m128i u = _mm_loadu_si128((const __m128i *)(src + pitch));
774
805k
  sig[0] = _mm_unpacklo_epi16(sig[2], u);
775
805k
  sig[1] = _mm_unpackhi_epi16(sig[2], u);
776
805k
  sig[2] = u;
777
805k
}
778
779
static INLINE void filter_8_2t_pixels(const __m128i *sig, const __m128i *f,
780
805k
                                      __m128i *y0, __m128i *y1) {
781
805k
  const __m128i rounding = _mm_set1_epi32(1 << (CONV8_ROUNDING_BITS - 1));
782
805k
  __m128i x0 = _mm_madd_epi16(sig[0], *f);
783
805k
  __m128i x1 = _mm_madd_epi16(sig[1], *f);
784
805k
  x0 = _mm_add_epi32(x0, rounding);
785
805k
  x1 = _mm_add_epi32(x1, rounding);
786
805k
  *y0 = _mm_srai_epi32(x0, CONV8_ROUNDING_BITS);
787
805k
  *y1 = _mm_srai_epi32(x1, CONV8_ROUNDING_BITS);
788
805k
}
789
790
static INLINE void store_8x1_2t_pixels_ver(const __m128i *y0, const __m128i *y1,
791
557k
                                           const __m128i *mask, uint16_t *dst) {
792
557k
  __m128i res = _mm_packus_epi32(*y0, *y1);
793
557k
  res = _mm_min_epi16(res, *mask);
794
557k
  _mm_storeu_si128((__m128i *)dst, res);
795
557k
}
796
797
static void vpx_highbd_filter_block1d8_v2_avx2(
798
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
799
68.2k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
800
68.2k
  __m128i signal[3], res0, res1;
801
68.2k
  const __m128i max = _mm_set1_epi16((1 << bd) - 1);
802
68.2k
  __m128i ff;
803
804
68.2k
  pack_8x1_2t_filter(filter, &ff);
805
68.2k
  pack_8x2_init(src_ptr, signal);
806
807
557k
  do {
808
557k
    pack_8x2_2t_pixels_ver(src_ptr, src_pitch, signal);
809
557k
    filter_8_2t_pixels(signal, &ff, &res0, &res1);
810
557k
    store_8x1_2t_pixels_ver(&res0, &res1, &max, dst_ptr);
811
812
557k
    src_ptr += src_pitch;
813
557k
    dst_ptr += dst_pitch;
814
557k
    height -= 1;
815
557k
  } while (height > 0);
816
68.2k
}
817
818
// Calculation with averaging the input pixels
819
820
static INLINE void store_8x1_avg_pixels(const __m256i *y0, const __m256i *mask,
821
0
                                        uint16_t *dst) {
822
0
  const __m128i a0 = _mm256_castsi256_si128(*y0);
823
0
  const __m128i a1 = _mm256_extractf128_si256(*y0, 1);
824
0
  __m128i res = _mm_packus_epi32(a0, a1);
825
0
  const __m128i pix = _mm_loadu_si128((const __m128i *)dst);
826
0
  res = _mm_min_epi16(res, _mm256_castsi256_si128(*mask));
827
0
  res = _mm_avg_epu16(res, pix);
828
0
  _mm_storeu_si128((__m128i *)dst, res);
829
0
}
830
831
static INLINE void store_8x2_avg_pixels(const __m256i *y0, const __m256i *y1,
832
                                        const __m256i *mask, uint16_t *dst,
833
187k
                                        ptrdiff_t pitch) {
834
187k
  __m256i a = _mm256_packus_epi32(*y0, *y1);
835
187k
  const __m128i pix0 = _mm_loadu_si128((const __m128i *)dst);
836
187k
  const __m128i pix1 = _mm_loadu_si128((const __m128i *)(dst + pitch));
837
187k
  const __m256i pix =
838
187k
      _mm256_insertf128_si256(_mm256_castsi128_si256(pix0), pix1, 1);
839
187k
  a = _mm256_min_epi16(a, *mask);
840
187k
  a = _mm256_avg_epu16(a, pix);
841
187k
  _mm_storeu_si128((__m128i *)dst, _mm256_castsi256_si128(a));
842
187k
  _mm_storeu_si128((__m128i *)(dst + pitch), _mm256_extractf128_si256(a, 1));
843
187k
}
844
845
static INLINE void store_16x1_avg_pixels(const __m256i *y0, const __m256i *y1,
846
2.19M
                                         const __m256i *mask, uint16_t *dst) {
847
2.19M
  __m256i a = _mm256_packus_epi32(*y0, *y1);
848
2.19M
  const __m256i pix = _mm256_loadu_si256((const __m256i *)dst);
849
2.19M
  a = _mm256_min_epi16(a, *mask);
850
2.19M
  a = _mm256_avg_epu16(a, pix);
851
2.19M
  _mm256_storeu_si256((__m256i *)dst, a);
852
2.19M
}
853
854
static INLINE void store_16x2_avg_pixels(const __m256i *y0, const __m256i *y1,
855
                                         const __m256i *mask, uint16_t *dst,
856
614k
                                         ptrdiff_t pitch) {
857
614k
  const __m256i pix0 = _mm256_loadu_si256((const __m256i *)dst);
858
614k
  const __m256i pix1 = _mm256_loadu_si256((const __m256i *)(dst + pitch));
859
614k
  __m256i p = _mm256_min_epi16(*y0, *mask);
860
614k
  p = _mm256_avg_epu16(p, pix0);
861
614k
  _mm256_storeu_si256((__m256i *)dst, p);
862
863
614k
  p = _mm256_min_epi16(*y1, *mask);
864
614k
  p = _mm256_avg_epu16(p, pix1);
865
614k
  _mm256_storeu_si256((__m256i *)(dst + pitch), p);
866
614k
}
867
868
static INLINE void store_8x1_2t_avg_pixels_ver(const __m128i *y0,
869
                                               const __m128i *y1,
870
                                               const __m128i *mask,
871
248k
                                               uint16_t *dst) {
872
248k
  __m128i res = _mm_packus_epi32(*y0, *y1);
873
248k
  const __m128i pix = _mm_loadu_si128((const __m128i *)dst);
874
248k
  res = _mm_min_epi16(res, *mask);
875
248k
  res = _mm_avg_epu16(res, pix);
876
248k
  _mm_storeu_si128((__m128i *)dst, res);
877
248k
}
878
879
static void vpx_highbd_filter_block1d8_h8_avg_avx2(
880
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
881
7.09k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
882
7.09k
  __m256i signal[8], res0, res1;
883
7.09k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
884
885
7.09k
  __m256i ff[4];
886
7.09k
  pack_filters(filter, ff);
887
888
7.09k
  src_ptr -= 3;
889
29.9k
  do {
890
29.9k
    pack_8x2_pixels(src_ptr, src_pitch, signal);
891
29.9k
    filter_8x1_pixels(signal, ff, &res0);
892
29.9k
    filter_8x1_pixels(&signal[4], ff, &res1);
893
29.9k
    store_8x2_avg_pixels(&res0, &res1, &max, dst_ptr, dst_pitch);
894
29.9k
    height -= 2;
895
29.9k
    src_ptr += src_pitch << 1;
896
29.9k
    dst_ptr += dst_pitch << 1;
897
29.9k
  } while (height > 1);
898
899
7.09k
  if (height > 0) {
900
0
    pack_8x1_pixels(src_ptr, signal);
901
0
    filter_8x1_pixels(signal, ff, &res0);
902
0
    store_8x1_avg_pixels(&res0, &max, dst_ptr);
903
0
  }
904
7.09k
}
905
906
static void vpx_highbd_filter_block1d16_h8_avg_avx2(
907
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
908
17.2k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
909
17.2k
  __m256i signal[8], res0, res1;
910
17.2k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
911
912
17.2k
  __m256i ff[4];
913
17.2k
  pack_filters(filter, ff);
914
915
17.2k
  src_ptr -= 3;
916
701k
  do {
917
701k
    pack_16x1_pixels(src_ptr, signal);
918
701k
    filter_8x1_pixels(signal, ff, &res0);
919
701k
    filter_8x1_pixels(&signal[4], ff, &res1);
920
701k
    store_16x1_avg_pixels(&res0, &res1, &max, dst_ptr);
921
701k
    height -= 1;
922
701k
    src_ptr += src_pitch;
923
701k
    dst_ptr += dst_pitch;
924
701k
  } while (height > 0);
925
17.2k
}
926
927
static void vpx_highbd_filter_block1d4_h4_avx2(
928
    const uint16_t *src_ptr, ptrdiff_t src_stride, uint16_t *dst_ptr,
929
0
    ptrdiff_t dst_stride, uint32_t height, const int16_t *kernel, int bd) {
930
  // We extract the middle four elements of the kernel into two registers in
931
  // the form
932
  // ... k[3] k[2] k[3] k[2]
933
  // ... k[5] k[4] k[5] k[4]
934
  // Then we shuffle the source into
935
  // ... s[1] s[0] s[0] s[-1]
936
  // ... s[3] s[2] s[2] s[1]
937
  // Calling multiply and add gives us half of the sum. Calling add on the two
938
  // halves gives us the output. Since avx2 allows us to use 256-bit buffer, we
939
  // can do this two rows at a time.
940
941
0
  __m256i src_reg, src_reg_shift_0, src_reg_shift_2;
942
0
  __m256i res_reg;
943
0
  __m256i idx_shift_0 =
944
0
      _mm256_setr_epi8(0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8, 9, 0, 1, 2,
945
0
                       3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8, 9);
946
0
  __m256i idx_shift_2 =
947
0
      _mm256_setr_epi8(4, 5, 6, 7, 6, 7, 8, 9, 8, 9, 10, 11, 10, 11, 12, 13, 4,
948
0
                       5, 6, 7, 6, 7, 8, 9, 8, 9, 10, 11, 10, 11, 12, 13);
949
950
0
  __m128i kernel_reg_128;  // Kernel
951
0
  __m256i kernel_reg, kernel_reg_23,
952
0
      kernel_reg_45;  // Segments of the kernel used
953
0
  const __m256i reg_round =
954
0
      _mm256_set1_epi32(CONV8_ROUNDING_NUM);  // Used for rounding
955
0
  const __m256i reg_max = _mm256_set1_epi16((1 << bd) - 1);
956
0
  const ptrdiff_t unrolled_src_stride = src_stride << 1;
957
0
  const ptrdiff_t unrolled_dst_stride = dst_stride << 1;
958
0
  int h;
959
960
  // Start one pixel before as we need tap/2 - 1 = 1 sample from the past
961
0
  src_ptr -= 1;
962
963
  // Load Kernel
964
0
  kernel_reg_128 = _mm_loadu_si128((const __m128i *)kernel);
965
0
  kernel_reg = _mm256_broadcastsi128_si256(kernel_reg_128);
966
0
  kernel_reg_23 = _mm256_shuffle_epi32(kernel_reg, 0x55);
967
0
  kernel_reg_45 = _mm256_shuffle_epi32(kernel_reg, 0xaa);
968
969
0
  for (h = height; h >= 2; h -= 2) {
970
    // Load the source
971
0
    src_reg = mm256_loadu2_si128(src_ptr, src_ptr + src_stride);
972
0
    src_reg_shift_0 = _mm256_shuffle_epi8(src_reg, idx_shift_0);
973
0
    src_reg_shift_2 = _mm256_shuffle_epi8(src_reg, idx_shift_2);
974
975
    // Get the output
976
0
    res_reg = mm256_madd_add_epi32(&src_reg_shift_0, &src_reg_shift_2,
977
0
                                   &kernel_reg_23, &kernel_reg_45);
978
979
    // Round the result
980
0
    res_reg = mm256_round_epi32(&res_reg, &reg_round, CONV8_ROUNDING_BITS);
981
982
    // Finally combine to get the final dst
983
0
    res_reg = _mm256_packus_epi32(res_reg, res_reg);
984
0
    res_reg = _mm256_min_epi16(res_reg, reg_max);
985
0
    mm256_storeu2_epi64((__m128i *)dst_ptr, (__m128i *)(dst_ptr + dst_stride),
986
0
                        &res_reg);
987
988
0
    src_ptr += unrolled_src_stride;
989
0
    dst_ptr += unrolled_dst_stride;
990
0
  }
991
992
  // Repeat for the last row if needed
993
0
  if (h > 0) {
994
    // Load the source
995
0
    src_reg = mm256_loadu2_si128(src_ptr, src_ptr + 4);
996
0
    src_reg_shift_0 = _mm256_shuffle_epi8(src_reg, idx_shift_0);
997
0
    src_reg_shift_2 = _mm256_shuffle_epi8(src_reg, idx_shift_2);
998
999
    // Get the output
1000
0
    res_reg = mm256_madd_add_epi32(&src_reg_shift_0, &src_reg_shift_2,
1001
0
                                   &kernel_reg_23, &kernel_reg_45);
1002
1003
    // Round the result
1004
0
    res_reg = mm256_round_epi32(&res_reg, &reg_round, CONV8_ROUNDING_BITS);
1005
1006
    // Finally combine to get the final dst
1007
0
    res_reg = _mm256_packus_epi32(res_reg, res_reg);
1008
0
    res_reg = _mm256_min_epi16(res_reg, reg_max);
1009
0
    _mm_storel_epi64((__m128i *)dst_ptr, _mm256_castsi256_si128(res_reg));
1010
0
  }
1011
0
}
1012
1013
static void vpx_highbd_filter_block1d8_h4_avx2(
1014
    const uint16_t *src_ptr, ptrdiff_t src_stride, uint16_t *dst_ptr,
1015
0
    ptrdiff_t dst_stride, uint32_t height, const int16_t *kernel, int bd) {
1016
  // We will extract the middle four elements of the kernel into two registers
1017
  // in the form
1018
  // ... k[3] k[2] k[3] k[2]
1019
  // ... k[5] k[4] k[5] k[4]
1020
  // Then we shuffle the source into
1021
  // ... s[1] s[0] s[0] s[-1]
1022
  // ... s[3] s[2] s[2] s[1]
1023
  // Calling multiply and add gives us half of the sum of the first half.
1024
  // Calling add gives us first half of the output. Repat again to get the whole
1025
  // output. Since avx2 allows us to use 256-bit buffer, we can do this two rows
1026
  // at a time.
1027
1028
0
  __m256i src_reg, src_reg_shift_0, src_reg_shift_2;
1029
0
  __m256i res_reg, res_first, res_last;
1030
0
  __m256i idx_shift_0 =
1031
0
      _mm256_setr_epi8(0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8, 9, 0, 1, 2,
1032
0
                       3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8, 9);
1033
0
  __m256i idx_shift_2 =
1034
0
      _mm256_setr_epi8(4, 5, 6, 7, 6, 7, 8, 9, 8, 9, 10, 11, 10, 11, 12, 13, 4,
1035
0
                       5, 6, 7, 6, 7, 8, 9, 8, 9, 10, 11, 10, 11, 12, 13);
1036
1037
0
  __m128i kernel_reg_128;  // Kernel
1038
0
  __m256i kernel_reg, kernel_reg_23,
1039
0
      kernel_reg_45;  // Segments of the kernel used
1040
0
  const __m256i reg_round =
1041
0
      _mm256_set1_epi32(CONV8_ROUNDING_NUM);  // Used for rounding
1042
0
  const __m256i reg_max = _mm256_set1_epi16((1 << bd) - 1);
1043
0
  const ptrdiff_t unrolled_src_stride = src_stride << 1;
1044
0
  const ptrdiff_t unrolled_dst_stride = dst_stride << 1;
1045
0
  int h;
1046
1047
  // Start one pixel before as we need tap/2 - 1 = 1 sample from the past
1048
0
  src_ptr -= 1;
1049
1050
  // Load Kernel
1051
0
  kernel_reg_128 = _mm_loadu_si128((const __m128i *)kernel);
1052
0
  kernel_reg = _mm256_broadcastsi128_si256(kernel_reg_128);
1053
0
  kernel_reg_23 = _mm256_shuffle_epi32(kernel_reg, 0x55);
1054
0
  kernel_reg_45 = _mm256_shuffle_epi32(kernel_reg, 0xaa);
1055
1056
0
  for (h = height; h >= 2; h -= 2) {
1057
    // Load the source
1058
0
    src_reg = mm256_loadu2_si128(src_ptr, src_ptr + src_stride);
1059
0
    src_reg_shift_0 = _mm256_shuffle_epi8(src_reg, idx_shift_0);
1060
0
    src_reg_shift_2 = _mm256_shuffle_epi8(src_reg, idx_shift_2);
1061
1062
    // Result for first half
1063
0
    res_first = mm256_madd_add_epi32(&src_reg_shift_0, &src_reg_shift_2,
1064
0
                                     &kernel_reg_23, &kernel_reg_45);
1065
1066
    // Do again to get the second half of dst
1067
    // Load the source
1068
0
    src_reg = mm256_loadu2_si128(src_ptr + 4, src_ptr + src_stride + 4);
1069
0
    src_reg_shift_0 = _mm256_shuffle_epi8(src_reg, idx_shift_0);
1070
0
    src_reg_shift_2 = _mm256_shuffle_epi8(src_reg, idx_shift_2);
1071
1072
    // Result for second half
1073
0
    res_last = mm256_madd_add_epi32(&src_reg_shift_0, &src_reg_shift_2,
1074
0
                                    &kernel_reg_23, &kernel_reg_45);
1075
1076
    // Round each result
1077
0
    res_first = mm256_round_epi32(&res_first, &reg_round, CONV8_ROUNDING_BITS);
1078
0
    res_last = mm256_round_epi32(&res_last, &reg_round, CONV8_ROUNDING_BITS);
1079
1080
    // Finally combine to get the final dst
1081
0
    res_reg = _mm256_packus_epi32(res_first, res_last);
1082
0
    res_reg = _mm256_min_epi16(res_reg, reg_max);
1083
0
    mm256_store2_si128((__m128i *)dst_ptr, (__m128i *)(dst_ptr + dst_stride),
1084
0
                       &res_reg);
1085
1086
0
    src_ptr += unrolled_src_stride;
1087
0
    dst_ptr += unrolled_dst_stride;
1088
0
  }
1089
1090
  // Repeat for the last row if needed
1091
0
  if (h > 0) {
1092
0
    src_reg = mm256_loadu2_si128(src_ptr, src_ptr + 4);
1093
0
    src_reg_shift_0 = _mm256_shuffle_epi8(src_reg, idx_shift_0);
1094
0
    src_reg_shift_2 = _mm256_shuffle_epi8(src_reg, idx_shift_2);
1095
1096
0
    res_reg = mm256_madd_add_epi32(&src_reg_shift_0, &src_reg_shift_2,
1097
0
                                   &kernel_reg_23, &kernel_reg_45);
1098
1099
0
    res_reg = mm256_round_epi32(&res_reg, &reg_round, CONV8_ROUNDING_BITS);
1100
1101
0
    res_reg = _mm256_packus_epi32(res_reg, res_reg);
1102
0
    res_reg = _mm256_min_epi16(res_reg, reg_max);
1103
1104
0
    mm256_storeu2_epi64((__m128i *)dst_ptr, (__m128i *)(dst_ptr + 4), &res_reg);
1105
0
  }
1106
0
}
1107
1108
static void vpx_highbd_filter_block1d16_h4_avx2(
1109
    const uint16_t *src_ptr, ptrdiff_t src_stride, uint16_t *dst_ptr,
1110
0
    ptrdiff_t dst_stride, uint32_t height, const int16_t *kernel, int bd) {
1111
0
  vpx_highbd_filter_block1d8_h4_avx2(src_ptr, src_stride, dst_ptr, dst_stride,
1112
0
                                     height, kernel, bd);
1113
0
  vpx_highbd_filter_block1d8_h4_avx2(src_ptr + 8, src_stride, dst_ptr + 8,
1114
0
                                     dst_stride, height, kernel, bd);
1115
0
}
1116
1117
static void vpx_highbd_filter_block1d8_v8_avg_avx2(
1118
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
1119
31.9k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
1120
31.9k
  __m256i signal[9], res0, res1;
1121
31.9k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
1122
1123
31.9k
  __m256i ff[4];
1124
31.9k
  pack_filters(filter, ff);
1125
1126
31.9k
  pack_8x9_init(src_ptr, src_pitch, signal);
1127
1128
131k
  do {
1129
131k
    pack_8x9_pixels(src_ptr, src_pitch, signal);
1130
1131
131k
    filter_8x9_pixels(signal, ff, &res0, &res1);
1132
131k
    store_8x2_avg_pixels(&res0, &res1, &max, dst_ptr, dst_pitch);
1133
131k
    update_pixels(signal);
1134
1135
131k
    src_ptr += src_pitch << 1;
1136
131k
    dst_ptr += dst_pitch << 1;
1137
131k
    height -= 2;
1138
131k
  } while (height > 0);
1139
31.9k
}
1140
1141
static void vpx_highbd_filter_block1d16_v8_avg_avx2(
1142
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
1143
40.6k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
1144
40.6k
  __m256i signal[17], res0, res1;
1145
40.6k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
1146
1147
40.6k
  __m256i ff[4];
1148
40.6k
  pack_filters(filter, ff);
1149
1150
40.6k
  pack_16x9_init(src_ptr, src_pitch, signal);
1151
1152
615k
  do {
1153
615k
    pack_16x9_pixels(src_ptr, src_pitch, signal);
1154
615k
    filter_16x9_pixels(signal, ff, &res0, &res1);
1155
615k
    store_16x2_avg_pixels(&res0, &res1, &max, dst_ptr, dst_pitch);
1156
615k
    update_16x9_pixels(signal);
1157
1158
615k
    src_ptr += src_pitch << 1;
1159
615k
    dst_ptr += dst_pitch << 1;
1160
615k
    height -= 2;
1161
615k
  } while (height > 0);
1162
40.6k
}
1163
1164
static void vpx_highbd_filter_block1d8_h2_avg_avx2(
1165
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
1166
6.23k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
1167
6.23k
  __m256i signal[2], res0, res1;
1168
6.23k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
1169
1170
6.23k
  __m256i ff;
1171
6.23k
  pack_2t_filter(filter, &ff);
1172
1173
6.23k
  src_ptr -= 3;
1174
26.6k
  do {
1175
26.6k
    pack_8x2_2t_pixels(src_ptr, src_pitch, signal);
1176
26.6k
    filter_16_2t_pixels(signal, &ff, &res0, &res1);
1177
26.6k
    store_8x2_avg_pixels(&res0, &res1, &max, dst_ptr, dst_pitch);
1178
26.6k
    height -= 2;
1179
26.6k
    src_ptr += src_pitch << 1;
1180
26.6k
    dst_ptr += dst_pitch << 1;
1181
26.6k
  } while (height > 1);
1182
1183
6.23k
  if (height > 0) {
1184
0
    pack_8x1_2t_pixels(src_ptr, signal);
1185
0
    filter_8x1_2t_pixels(signal, &ff, &res0);
1186
0
    store_8x1_avg_pixels(&res0, &max, dst_ptr);
1187
0
  }
1188
6.23k
}
1189
1190
static void vpx_highbd_filter_block1d16_h2_avg_avx2(
1191
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
1192
14.1k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
1193
14.1k
  __m256i signal[2], res0, res1;
1194
14.1k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
1195
1196
14.1k
  __m256i ff;
1197
14.1k
  pack_2t_filter(filter, &ff);
1198
1199
14.1k
  src_ptr -= 3;
1200
546k
  do {
1201
546k
    pack_16x1_2t_pixels(src_ptr, signal);
1202
546k
    filter_16_2t_pixels(signal, &ff, &res0, &res1);
1203
546k
    store_16x1_avg_pixels(&res0, &res1, &max, dst_ptr);
1204
546k
    height -= 1;
1205
546k
    src_ptr += src_pitch;
1206
546k
    dst_ptr += dst_pitch;
1207
546k
  } while (height > 0);
1208
14.1k
}
1209
1210
static void vpx_highbd_filter_block1d16_v2_avg_avx2(
1211
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
1212
35.3k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
1213
35.3k
  __m256i signal[3], res0, res1;
1214
35.3k
  const __m256i max = _mm256_set1_epi16((1 << bd) - 1);
1215
35.3k
  __m256i ff;
1216
1217
35.3k
  pack_2t_filter(filter, &ff);
1218
35.3k
  pack_16x2_init(src_ptr, signal);
1219
1220
946k
  do {
1221
946k
    pack_16x2_2t_pixels(src_ptr, src_pitch, signal);
1222
946k
    filter_16x2_2t_pixels(signal, &ff, &res0, &res1);
1223
946k
    store_16x1_avg_pixels(&res0, &res1, &max, dst_ptr);
1224
1225
946k
    src_ptr += src_pitch;
1226
946k
    dst_ptr += dst_pitch;
1227
946k
    height -= 1;
1228
946k
  } while (height > 0);
1229
35.3k
}
1230
1231
static void vpx_highbd_filter_block1d8_v2_avg_avx2(
1232
    const uint16_t *src_ptr, ptrdiff_t src_pitch, uint16_t *dst_ptr,
1233
30.1k
    ptrdiff_t dst_pitch, uint32_t height, const int16_t *filter, int bd) {
1234
30.1k
  __m128i signal[3], res0, res1;
1235
30.1k
  const __m128i max = _mm_set1_epi16((1 << bd) - 1);
1236
30.1k
  __m128i ff;
1237
1238
30.1k
  pack_8x1_2t_filter(filter, &ff);
1239
30.1k
  pack_8x2_init(src_ptr, signal);
1240
1241
248k
  do {
1242
248k
    pack_8x2_2t_pixels_ver(src_ptr, src_pitch, signal);
1243
248k
    filter_8_2t_pixels(signal, &ff, &res0, &res1);
1244
248k
    store_8x1_2t_avg_pixels_ver(&res0, &res1, &max, dst_ptr);
1245
1246
248k
    src_ptr += src_pitch;
1247
248k
    dst_ptr += dst_pitch;
1248
248k
    height -= 1;
1249
248k
  } while (height > 0);
1250
30.1k
}
1251
1252
static void vpx_highbd_filter_block1d4_v4_avx2(
1253
    const uint16_t *src_ptr, ptrdiff_t src_stride, uint16_t *dst_ptr,
1254
0
    ptrdiff_t dst_stride, uint32_t height, const int16_t *kernel, int bd) {
1255
  // We will load two rows of pixels and rearrange them into the form
1256
  // ... s[1,0] s[0,0] s[0,0] s[-1,0]
1257
  // so that we can call multiply and add with the kernel partial output. Then
1258
  // we can call add with another row to get the output.
1259
1260
  // Register for source s[-1:3, :]
1261
0
  __m256i src_reg_1, src_reg_2, src_reg_3;
1262
  // Interleaved rows of the source. lo is first half, hi second
1263
0
  __m256i src_reg_m10, src_reg_01, src_reg_12, src_reg_23;
1264
0
  __m256i src_reg_m1001, src_reg_1223;
1265
1266
  // Result after multiply and add
1267
0
  __m256i res_reg;
1268
1269
0
  __m128i kernel_reg_128;                            // Kernel
1270
0
  __m256i kernel_reg, kernel_reg_23, kernel_reg_45;  // Segments of kernel used
1271
1272
0
  const __m256i reg_round =
1273
0
      _mm256_set1_epi32(CONV8_ROUNDING_NUM);  // Used for rounding
1274
0
  const __m256i reg_max = _mm256_set1_epi16((1 << bd) - 1);
1275
0
  const ptrdiff_t src_stride_unrolled = src_stride << 1;
1276
0
  const ptrdiff_t dst_stride_unrolled = dst_stride << 1;
1277
0
  int h;
1278
1279
  // Load Kernel
1280
0
  kernel_reg_128 = _mm_loadu_si128((const __m128i *)kernel);
1281
0
  kernel_reg = _mm256_broadcastsi128_si256(kernel_reg_128);
1282
0
  kernel_reg_23 = _mm256_shuffle_epi32(kernel_reg, 0x55);
1283
0
  kernel_reg_45 = _mm256_shuffle_epi32(kernel_reg, 0xaa);
1284
1285
  // Row -1 to row 0
1286
0
  src_reg_m10 = mm256_loadu2_epi64((const __m128i *)src_ptr,
1287
0
                                   (const __m128i *)(src_ptr + src_stride));
1288
1289
  // Row 0 to row 1
1290
0
  src_reg_1 = _mm256_castsi128_si256(
1291
0
      _mm_loadu_si128((const __m128i *)(src_ptr + src_stride * 2)));
1292
0
  src_reg_01 = _mm256_permute2x128_si256(src_reg_m10, src_reg_1, 0x21);
1293
1294
  // First three rows
1295
0
  src_reg_m1001 = _mm256_unpacklo_epi16(src_reg_m10, src_reg_01);
1296
1297
0
  for (h = height; h > 1; h -= 2) {
1298
0
    src_reg_2 = _mm256_castsi128_si256(
1299
0
        _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride * 3)));
1300
1301
0
    src_reg_12 = _mm256_inserti128_si256(src_reg_1,
1302
0
                                         _mm256_castsi256_si128(src_reg_2), 1);
1303
1304
0
    src_reg_3 = _mm256_castsi128_si256(
1305
0
        _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride * 4)));
1306
1307
0
    src_reg_23 = _mm256_inserti128_si256(src_reg_2,
1308
0
                                         _mm256_castsi256_si128(src_reg_3), 1);
1309
1310
    // Last three rows
1311
0
    src_reg_1223 = _mm256_unpacklo_epi16(src_reg_12, src_reg_23);
1312
1313
    // Output
1314
0
    res_reg = mm256_madd_add_epi32(&src_reg_m1001, &src_reg_1223,
1315
0
                                   &kernel_reg_23, &kernel_reg_45);
1316
1317
    // Round the words
1318
0
    res_reg = mm256_round_epi32(&res_reg, &reg_round, CONV8_ROUNDING_BITS);
1319
1320
    // Combine to get the result
1321
0
    res_reg = _mm256_packus_epi32(res_reg, res_reg);
1322
0
    res_reg = _mm256_min_epi16(res_reg, reg_max);
1323
1324
    // Save the result
1325
0
    mm256_storeu2_epi64((__m128i *)dst_ptr, (__m128i *)(dst_ptr + dst_stride),
1326
0
                        &res_reg);
1327
1328
    // Update the source by two rows
1329
0
    src_ptr += src_stride_unrolled;
1330
0
    dst_ptr += dst_stride_unrolled;
1331
1332
0
    src_reg_m1001 = src_reg_1223;
1333
0
    src_reg_1 = src_reg_3;
1334
0
  }
1335
0
}
1336
1337
static void vpx_highbd_filter_block1d8_v4_avx2(
1338
    const uint16_t *src_ptr, ptrdiff_t src_stride, uint16_t *dst_ptr,
1339
0
    ptrdiff_t dst_stride, uint32_t height, const int16_t *kernel, int bd) {
1340
  // We will load two rows of pixels and rearrange them into the form
1341
  // ... s[1,0] s[0,0] s[0,0] s[-1,0]
1342
  // so that we can call multiply and add with the kernel partial output. Then
1343
  // we can call add with another row to get the output.
1344
1345
  // Register for source s[-1:3, :]
1346
0
  __m256i src_reg_1, src_reg_2, src_reg_3;
1347
  // Interleaved rows of the source. lo is first half, hi second
1348
0
  __m256i src_reg_m10, src_reg_01, src_reg_12, src_reg_23;
1349
0
  __m256i src_reg_m1001_lo, src_reg_m1001_hi, src_reg_1223_lo, src_reg_1223_hi;
1350
1351
0
  __m128i kernel_reg_128;                            // Kernel
1352
0
  __m256i kernel_reg, kernel_reg_23, kernel_reg_45;  // Segments of kernel
1353
1354
  // Result after multiply and add
1355
0
  __m256i res_reg, res_reg_lo, res_reg_hi;
1356
1357
0
  const __m256i reg_round =
1358
0
      _mm256_set1_epi32(CONV8_ROUNDING_NUM);  // Used for rounding
1359
0
  const __m256i reg_max = _mm256_set1_epi16((1 << bd) - 1);
1360
0
  const ptrdiff_t src_stride_unrolled = src_stride << 1;
1361
0
  const ptrdiff_t dst_stride_unrolled = dst_stride << 1;
1362
0
  int h;
1363
1364
  // Load Kernel
1365
0
  kernel_reg_128 = _mm_loadu_si128((const __m128i *)kernel);
1366
0
  kernel_reg = _mm256_broadcastsi128_si256(kernel_reg_128);
1367
0
  kernel_reg_23 = _mm256_shuffle_epi32(kernel_reg, 0x55);
1368
0
  kernel_reg_45 = _mm256_shuffle_epi32(kernel_reg, 0xaa);
1369
1370
  // Row -1 to row 0
1371
0
  src_reg_m10 = mm256_loadu2_si128((const __m128i *)src_ptr,
1372
0
                                   (const __m128i *)(src_ptr + src_stride));
1373
1374
  // Row 0 to row 1
1375
0
  src_reg_1 = _mm256_castsi128_si256(
1376
0
      _mm_loadu_si128((const __m128i *)(src_ptr + src_stride * 2)));
1377
0
  src_reg_01 = _mm256_permute2x128_si256(src_reg_m10, src_reg_1, 0x21);
1378
1379
  // First three rows
1380
0
  src_reg_m1001_lo = _mm256_unpacklo_epi16(src_reg_m10, src_reg_01);
1381
0
  src_reg_m1001_hi = _mm256_unpackhi_epi16(src_reg_m10, src_reg_01);
1382
1383
0
  for (h = height; h > 1; h -= 2) {
1384
0
    src_reg_2 = _mm256_castsi128_si256(
1385
0
        _mm_loadu_si128((const __m128i *)(src_ptr + src_stride * 3)));
1386
1387
0
    src_reg_12 = _mm256_inserti128_si256(src_reg_1,
1388
0
                                         _mm256_castsi256_si128(src_reg_2), 1);
1389
1390
0
    src_reg_3 = _mm256_castsi128_si256(
1391
0
        _mm_loadu_si128((const __m128i *)(src_ptr + src_stride * 4)));
1392
1393
0
    src_reg_23 = _mm256_inserti128_si256(src_reg_2,
1394
0
                                         _mm256_castsi256_si128(src_reg_3), 1);
1395
1396
    // Last three rows
1397
0
    src_reg_1223_lo = _mm256_unpacklo_epi16(src_reg_12, src_reg_23);
1398
0
    src_reg_1223_hi = _mm256_unpackhi_epi16(src_reg_12, src_reg_23);
1399
1400
    // Output from first half
1401
0
    res_reg_lo = mm256_madd_add_epi32(&src_reg_m1001_lo, &src_reg_1223_lo,
1402
0
                                      &kernel_reg_23, &kernel_reg_45);
1403
1404
    // Output from second half
1405
0
    res_reg_hi = mm256_madd_add_epi32(&src_reg_m1001_hi, &src_reg_1223_hi,
1406
0
                                      &kernel_reg_23, &kernel_reg_45);
1407
1408
    // Round the words
1409
0
    res_reg_lo =
1410
0
        mm256_round_epi32(&res_reg_lo, &reg_round, CONV8_ROUNDING_BITS);
1411
0
    res_reg_hi =
1412
0
        mm256_round_epi32(&res_reg_hi, &reg_round, CONV8_ROUNDING_BITS);
1413
1414
    // Combine to get the result
1415
0
    res_reg = _mm256_packus_epi32(res_reg_lo, res_reg_hi);
1416
0
    res_reg = _mm256_min_epi16(res_reg, reg_max);
1417
1418
    // Save the result
1419
0
    mm256_store2_si128((__m128i *)dst_ptr, (__m128i *)(dst_ptr + dst_stride),
1420
0
                       &res_reg);
1421
1422
    // Update the source by two rows
1423
0
    src_ptr += src_stride_unrolled;
1424
0
    dst_ptr += dst_stride_unrolled;
1425
1426
0
    src_reg_m1001_lo = src_reg_1223_lo;
1427
0
    src_reg_m1001_hi = src_reg_1223_hi;
1428
0
    src_reg_1 = src_reg_3;
1429
0
  }
1430
0
}
1431
1432
static void vpx_highbd_filter_block1d16_v4_avx2(
1433
    const uint16_t *src_ptr, ptrdiff_t src_stride, uint16_t *dst_ptr,
1434
0
    ptrdiff_t dst_stride, uint32_t height, const int16_t *kernel, int bd) {
1435
0
  vpx_highbd_filter_block1d8_v4_avx2(src_ptr, src_stride, dst_ptr, dst_stride,
1436
0
                                     height, kernel, bd);
1437
0
  vpx_highbd_filter_block1d8_v4_avx2(src_ptr + 8, src_stride, dst_ptr + 8,
1438
0
                                     dst_stride, height, kernel, bd);
1439
0
}
1440
1441
// From vpx_dsp/x86/vpx_high_subpixel_8t_sse2.asm.
1442
highbd_filter8_1dfunction vpx_highbd_filter_block1d4_h8_sse2;
1443
highbd_filter8_1dfunction vpx_highbd_filter_block1d4_v8_sse2;
1444
1445
// From vpx_dsp/x86/vpx_high_subpixel_bilinear_sse2.asm.
1446
highbd_filter8_1dfunction vpx_highbd_filter_block1d4_h2_sse2;
1447
highbd_filter8_1dfunction vpx_highbd_filter_block1d4_v2_sse2;
1448
1449
276k
#define vpx_highbd_filter_block1d4_h8_avx2 vpx_highbd_filter_block1d4_h8_sse2
1450
143k
#define vpx_highbd_filter_block1d4_h2_avx2 vpx_highbd_filter_block1d4_h2_sse2
1451
244k
#define vpx_highbd_filter_block1d4_v8_avx2 vpx_highbd_filter_block1d4_v8_sse2
1452
104k
#define vpx_highbd_filter_block1d4_v2_avx2 vpx_highbd_filter_block1d4_v2_sse2
1453
1454
// Use the [vh]8 version because there is no [vh]4 implementation.
1455
#define vpx_highbd_filter_block1d16_v4_avg_avx2 \
1456
0
  vpx_highbd_filter_block1d16_v8_avg_avx2
1457
#define vpx_highbd_filter_block1d16_h4_avg_avx2 \
1458
0
  vpx_highbd_filter_block1d16_h8_avg_avx2
1459
#define vpx_highbd_filter_block1d8_v4_avg_avx2 \
1460
0
  vpx_highbd_filter_block1d8_v8_avg_avx2
1461
#define vpx_highbd_filter_block1d8_h4_avg_avx2 \
1462
0
  vpx_highbd_filter_block1d8_h8_avg_avx2
1463
#define vpx_highbd_filter_block1d4_v4_avg_avx2 \
1464
0
  vpx_highbd_filter_block1d4_v8_avg_avx2
1465
#define vpx_highbd_filter_block1d4_h4_avg_avx2 \
1466
0
  vpx_highbd_filter_block1d4_h8_avg_avx2
1467
1468
HIGH_FUN_CONV_1D(horiz, x0_q4, x_step_q4, h, src, , avx2, 0)
1469
HIGH_FUN_CONV_1D(vert, y0_q4, y_step_q4, v,
1470
                 src - src_stride * (num_taps / 2 - 1), , avx2, 0)
1471
1.91M
HIGH_FUN_CONV_2D(, avx2, 0)
1472
1473
// From vpx_dsp/x86/vpx_high_subpixel_8t_sse2.asm.
1474
highbd_filter8_1dfunction vpx_highbd_filter_block1d4_h8_avg_sse2;
1475
highbd_filter8_1dfunction vpx_highbd_filter_block1d4_v8_avg_sse2;
1476
1477
// From vpx_dsp/x86/vpx_high_subpixel_bilinear_sse2.asm.
1478
highbd_filter8_1dfunction vpx_highbd_filter_block1d4_h2_avg_sse2;
1479
highbd_filter8_1dfunction vpx_highbd_filter_block1d4_v2_avg_sse2;
1480
1481
#define vpx_highbd_filter_block1d4_h8_avg_avx2 \
1482
13.7k
  vpx_highbd_filter_block1d4_h8_avg_sse2
1483
#define vpx_highbd_filter_block1d4_h2_avg_avx2 \
1484
9.61k
  vpx_highbd_filter_block1d4_h2_avg_sse2
1485
#define vpx_highbd_filter_block1d4_v8_avg_avx2 \
1486
56.3k
  vpx_highbd_filter_block1d4_v8_avg_sse2
1487
#define vpx_highbd_filter_block1d4_v2_avg_avx2 \
1488
45.8k
  vpx_highbd_filter_block1d4_v2_avg_sse2
1489
1490
HIGH_FUN_CONV_1D(avg_horiz, x0_q4, x_step_q4, h, src, avg_, avx2, 1)
1491
HIGH_FUN_CONV_1D(avg_vert, y0_q4, y_step_q4, v,
1492
                 src - src_stride * (num_taps / 2 - 1), avg_, avx2, 1)
1493
HIGH_FUN_CONV_2D(avg_, avx2, 1)
1494
1495
#undef HIGHBD_FUNC