Coverage Report

Created: 2026-12-01 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/avm/av2/common/x86/cfl_avx2.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
3
 *
4
 * This source code is subject to the terms of the BSD 3-Clause Clear License
5
 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6
 * License was not distributed with this source code in the LICENSE file, you
7
 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/.  If the
8
 * Alliance for Open Media Patent License 1.0 was not distributed with this
9
 * source code in the PATENTS file, you can obtain it at
10
 * aomedia.org/license/patent-license/.
11
 */
12
#include <immintrin.h>
13
14
#include "config/av2_rtcd.h"
15
16
#include "av2/common/cfl.h"
17
18
#include "av2/common/x86/cfl_simd.h"
19
20
#define CFL_GET_SUBSAMPLE_FUNCTION_AVX2(sub, bd)                           \
21
  CFL_SUBSAMPLE(avx2, sub, bd, 32, 32)                                     \
22
  CFL_SUBSAMPLE(avx2, sub, bd, 32, 16)                                     \
23
  CFL_SUBSAMPLE(avx2, sub, bd, 32, 8)                                      \
24
  CFL_SUBSAMPLE(avx2, sub, bd, 32, 4)                                      \
25
  CFL_SUBSAMPLE(avx2, sub, bd, 64, 64)                                     \
26
  CFL_SUBSAMPLE(avx2, sub, bd, 64, 32)                                     \
27
  CFL_SUBSAMPLE(avx2, sub, bd, 32, 64)                                     \
28
  CFL_SUBSAMPLE(avx2, sub, bd, 64, 4)                                      \
29
  CFL_SUBSAMPLE(avx2, sub, bd, 64, 8)                                      \
30
  CFL_SUBSAMPLE(avx2, sub, bd, 64, 16)                                     \
31
  av2_cfl_subsample_##bd##_fn                                              \
32
339k
      av2_cfl_get_luma_subsampling_##sub##_##bd##_avx2(TX_SIZE tx_size) {  \
33
339k
    static const av2_cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = { \
34
339k
      av2_cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */              \
35
339k
      av2_cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */              \
36
339k
      av2_cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */            \
37
339k
      av2_cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */            \
38
339k
      av2_cfl_subsample_##bd##_##sub##_64x64_avx2,  /* 64x64 */            \
39
339k
      av2_cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */              \
40
339k
      av2_cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */              \
41
339k
      av2_cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */             \
42
339k
      av2_cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */             \
43
339k
      av2_cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */            \
44
339k
      av2_cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */            \
45
339k
      av2_cfl_subsample_##bd##_##sub##_32x64_avx2,  /* 32x64 */            \
46
339k
      av2_cfl_subsample_##bd##_##sub##_64x32_avx2,  /* 64x32 */            \
47
339k
      av2_cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */            \
48
339k
      av2_cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */            \
49
339k
      av2_cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */            \
50
339k
      av2_cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */            \
51
339k
      av2_cfl_subsample_##bd##_##sub##_16x64_ssse3, /* 16x64 */            \
52
339k
      av2_cfl_subsample_##bd##_##sub##_64x16_avx2,  /* 64x16 */            \
53
339k
      av2_cfl_subsample_##bd##_##sub##_4x32_ssse3,  /* 4x32  */            \
54
339k
      av2_cfl_subsample_##bd##_##sub##_32x4_avx2,   /* 32x4  */            \
55
339k
      av2_cfl_subsample_##bd##_##sub##_8x64_ssse3,  /* 8x64 */             \
56
339k
      av2_cfl_subsample_##bd##_##sub##_64x8_avx2,   /* 64x8 */             \
57
339k
      av2_cfl_subsample_##bd##_##sub##_4x64_ssse3,  /* 4x64 */             \
58
339k
      av2_cfl_subsample_##bd##_##sub##_64x4_avx2,   /* 64x4 */             \
59
339k
    };                                                                     \
60
339k
    return subfn_##sub[tx_size];                                           \
61
339k
  }
av2_cfl_get_luma_subsampling_420_hbd_avx2
Line
Count
Source
32
260k
      av2_cfl_get_luma_subsampling_##sub##_##bd##_avx2(TX_SIZE tx_size) {  \
33
260k
    static const av2_cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = { \
34
260k
      av2_cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */              \
35
260k
      av2_cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */              \
36
260k
      av2_cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */            \
37
260k
      av2_cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */            \
38
260k
      av2_cfl_subsample_##bd##_##sub##_64x64_avx2,  /* 64x64 */            \
39
260k
      av2_cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */              \
40
260k
      av2_cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */              \
41
260k
      av2_cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */             \
42
260k
      av2_cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */             \
43
260k
      av2_cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */            \
44
260k
      av2_cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */            \
45
260k
      av2_cfl_subsample_##bd##_##sub##_32x64_avx2,  /* 32x64 */            \
46
260k
      av2_cfl_subsample_##bd##_##sub##_64x32_avx2,  /* 64x32 */            \
47
260k
      av2_cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */            \
48
260k
      av2_cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */            \
49
260k
      av2_cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */            \
50
260k
      av2_cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */            \
51
260k
      av2_cfl_subsample_##bd##_##sub##_16x64_ssse3, /* 16x64 */            \
52
260k
      av2_cfl_subsample_##bd##_##sub##_64x16_avx2,  /* 64x16 */            \
53
260k
      av2_cfl_subsample_##bd##_##sub##_4x32_ssse3,  /* 4x32  */            \
54
260k
      av2_cfl_subsample_##bd##_##sub##_32x4_avx2,   /* 32x4  */            \
55
260k
      av2_cfl_subsample_##bd##_##sub##_8x64_ssse3,  /* 8x64 */             \
56
260k
      av2_cfl_subsample_##bd##_##sub##_64x8_avx2,   /* 64x8 */             \
57
260k
      av2_cfl_subsample_##bd##_##sub##_4x64_ssse3,  /* 4x64 */             \
58
260k
      av2_cfl_subsample_##bd##_##sub##_64x4_avx2,   /* 64x4 */             \
59
260k
    };                                                                     \
60
260k
    return subfn_##sub[tx_size];                                           \
61
260k
  }
Unexecuted instantiation: av2_cfl_get_luma_subsampling_422_hbd_avx2
av2_cfl_get_luma_subsampling_444_hbd_avx2
Line
Count
Source
32
79.4k
      av2_cfl_get_luma_subsampling_##sub##_##bd##_avx2(TX_SIZE tx_size) {  \
33
79.4k
    static const av2_cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = { \
34
79.4k
      av2_cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */              \
35
79.4k
      av2_cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */              \
36
79.4k
      av2_cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */            \
37
79.4k
      av2_cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */            \
38
79.4k
      av2_cfl_subsample_##bd##_##sub##_64x64_avx2,  /* 64x64 */            \
39
79.4k
      av2_cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */              \
40
79.4k
      av2_cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */              \
41
79.4k
      av2_cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */             \
42
79.4k
      av2_cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */             \
43
79.4k
      av2_cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */            \
44
79.4k
      av2_cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */            \
45
79.4k
      av2_cfl_subsample_##bd##_##sub##_32x64_avx2,  /* 32x64 */            \
46
79.4k
      av2_cfl_subsample_##bd##_##sub##_64x32_avx2,  /* 64x32 */            \
47
79.4k
      av2_cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */            \
48
79.4k
      av2_cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */            \
49
79.4k
      av2_cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */            \
50
79.4k
      av2_cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */            \
51
79.4k
      av2_cfl_subsample_##bd##_##sub##_16x64_ssse3, /* 16x64 */            \
52
79.4k
      av2_cfl_subsample_##bd##_##sub##_64x16_avx2,  /* 64x16 */            \
53
79.4k
      av2_cfl_subsample_##bd##_##sub##_4x32_ssse3,  /* 4x32  */            \
54
79.4k
      av2_cfl_subsample_##bd##_##sub##_32x4_avx2,   /* 32x4  */            \
55
79.4k
      av2_cfl_subsample_##bd##_##sub##_8x64_ssse3,  /* 8x64 */             \
56
79.4k
      av2_cfl_subsample_##bd##_##sub##_64x8_avx2,   /* 64x8 */             \
57
79.4k
      av2_cfl_subsample_##bd##_##sub##_4x64_ssse3,  /* 4x64 */             \
58
79.4k
      av2_cfl_subsample_##bd##_##sub##_64x4_avx2,   /* 64x4 */             \
59
79.4k
    };                                                                     \
60
79.4k
    return subfn_##sub[tx_size];                                           \
61
79.4k
  }
62
63
#define ACCUMULATE_SUMS_64(a0, a1, a2, y)                                      \
64
626k
  {                                                                            \
65
626k
    const __m256i prod00 = _mm256_mullo_epi32(a0, a0);                         \
66
626k
    const __m256i prod01 = _mm256_mullo_epi32(a0, a1);                         \
67
626k
    const __m256i prod02 = _mm256_mullo_epi32(a0, a2);                         \
68
626k
    const __m256i prod11 = _mm256_mullo_epi32(a1, a1);                         \
69
626k
    const __m256i prod12 = _mm256_mullo_epi32(a1, a2);                         \
70
626k
    const __m256i prod22 = _mm256_mullo_epi32(a2, a2);                         \
71
626k
    const __m256i prody0 = _mm256_mullo_epi32(a0, y);                          \
72
626k
    const __m256i prody1 = _mm256_mullo_epi32(a1, y);                          \
73
626k
    const __m256i prody2 = _mm256_mullo_epi32(a2, y);                          \
74
626k
                                                                               \
75
626k
    sum00_lo = _mm256_add_epi64(                                               \
76
626k
        sum00_lo, _mm256_cvtepi32_epi64(_mm256_castsi256_si128(prod00)));      \
77
626k
    sum00_hi = _mm256_add_epi64(                                               \
78
626k
        sum00_hi, _mm256_cvtepi32_epi64(_mm256_extracti128_si256(prod00, 1))); \
79
626k
                                                                               \
80
626k
    sum01_lo = _mm256_add_epi64(                                               \
81
626k
        sum01_lo, _mm256_cvtepi32_epi64(_mm256_castsi256_si128(prod01)));      \
82
626k
    sum01_hi = _mm256_add_epi64(                                               \
83
626k
        sum01_hi, _mm256_cvtepi32_epi64(_mm256_extracti128_si256(prod01, 1))); \
84
626k
                                                                               \
85
626k
    sum02_lo = _mm256_add_epi64(                                               \
86
626k
        sum02_lo, _mm256_cvtepi32_epi64(_mm256_castsi256_si128(prod02)));      \
87
626k
    sum02_hi = _mm256_add_epi64(                                               \
88
626k
        sum02_hi, _mm256_cvtepi32_epi64(_mm256_extracti128_si256(prod02, 1))); \
89
626k
                                                                               \
90
626k
    sum11_lo = _mm256_add_epi64(                                               \
91
626k
        sum11_lo, _mm256_cvtepi32_epi64(_mm256_castsi256_si128(prod11)));      \
92
626k
    sum11_hi = _mm256_add_epi64(                                               \
93
626k
        sum11_hi, _mm256_cvtepi32_epi64(_mm256_extracti128_si256(prod11, 1))); \
94
626k
                                                                               \
95
626k
    sum12_lo = _mm256_add_epi64(                                               \
96
626k
        sum12_lo, _mm256_cvtepi32_epi64(_mm256_castsi256_si128(prod12)));      \
97
626k
    sum12_hi = _mm256_add_epi64(                                               \
98
626k
        sum12_hi, _mm256_cvtepi32_epi64(_mm256_extracti128_si256(prod12, 1))); \
99
626k
                                                                               \
100
626k
    sum22_lo = _mm256_add_epi64(                                               \
101
626k
        sum22_lo, _mm256_cvtepi32_epi64(_mm256_castsi256_si128(prod22)));      \
102
626k
    sum22_hi = _mm256_add_epi64(                                               \
103
626k
        sum22_hi, _mm256_cvtepi32_epi64(_mm256_extracti128_si256(prod22, 1))); \
104
626k
                                                                               \
105
626k
    ty0_lo = _mm256_add_epi64(                                                 \
106
626k
        ty0_lo, _mm256_cvtepi32_epi64(_mm256_castsi256_si128(prody0)));        \
107
626k
    ty0_hi = _mm256_add_epi64(                                                 \
108
626k
        ty0_hi, _mm256_cvtepi32_epi64(_mm256_extracti128_si256(prody0, 1)));   \
109
626k
                                                                               \
110
626k
    ty1_lo = _mm256_add_epi64(                                                 \
111
626k
        ty1_lo, _mm256_cvtepi32_epi64(_mm256_castsi256_si128(prody1)));        \
112
626k
    ty1_hi = _mm256_add_epi64(                                                 \
113
626k
        ty1_hi, _mm256_cvtepi32_epi64(_mm256_extracti128_si256(prody1, 1)));   \
114
626k
                                                                               \
115
626k
    ty2_lo = _mm256_add_epi64(                                                 \
116
626k
        ty2_lo, _mm256_cvtepi32_epi64(_mm256_castsi256_si128(prody2)));        \
117
626k
    ty2_hi = _mm256_add_epi64(                                                 \
118
626k
        ty2_hi, _mm256_cvtepi32_epi64(_mm256_extracti128_si256(prody2, 1)));   \
119
626k
  }
120
121
/**
122
 * Adds 4 pixels (in a 2x2 grid) and multiplies them by 2. Resulting in a more
123
 * precise version of a box filter 4:2:0 pixel subsampling in Q3.
124
 *
125
 * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
126
 * active area is specified using width and height.
127
 *
128
 * Note: We don't need to worry about going over the active area, as long as we
129
 * stay inside the CfL prediction buffer.
130
 *
131
 * Note: For 4:2:0 luma subsampling, the width will never be greater than 16.
132
 */
133
static void cfl_luma_subsampling_420_hbd_avx2(const uint16_t *input,
134
                                              int input_stride,
135
                                              uint16_t *pred_buf_q3, int width,
136
127k
                                              int height) {
137
127k
  const int luma_stride = input_stride << 1;
138
127k
  __m256i *row = (__m256i *)pred_buf_q3;
139
127k
  const __m256i *row_end = row + (height >> 1) * CFL_BUF_LINE_I256;
140
2.25M
  do {
141
2.25M
    if (width == 32) {
142
915k
      __m256i top = _mm256_loadu_si256((__m256i *)input);
143
915k
      __m256i bot = _mm256_loadu_si256((__m256i *)(input + input_stride));
144
915k
      __m256i sum = _mm256_add_epi16(top, bot);
145
146
915k
      __m256i top_1 = _mm256_loadu_si256((__m256i *)(input + 16));
147
915k
      __m256i bot_1 =
148
915k
          _mm256_loadu_si256((__m256i *)(input + 16 + input_stride));
149
915k
      __m256i sum_1 = _mm256_add_epi16(top_1, bot_1);
150
151
915k
      __m256i hsum = _mm256_hadd_epi16(sum, sum_1);
152
915k
      hsum = _mm256_permute4x64_epi64(hsum, _MM_SHUFFLE(3, 1, 2, 0));
153
915k
      hsum = _mm256_add_epi16(hsum, hsum);
154
915k
      _mm256_storeu_si256(row, hsum);
155
1.34M
    } else {  // width == 64
156
1.34M
      __m256i top = _mm256_loadu_si256((__m256i *)input);
157
1.34M
      __m256i bot = _mm256_loadu_si256((__m256i *)(input + input_stride));
158
1.34M
      __m256i sum = _mm256_add_epi16(top, bot);
159
160
1.34M
      __m256i top_1 = _mm256_loadu_si256((__m256i *)(input + 16));
161
1.34M
      __m256i bot_1 =
162
1.34M
          _mm256_loadu_si256((__m256i *)(input + 16 + input_stride));
163
1.34M
      __m256i sum_1 = _mm256_add_epi16(top_1, bot_1);
164
165
1.34M
      __m256i hsum = _mm256_hadd_epi16(sum, sum_1);
166
1.34M
      hsum = _mm256_permute4x64_epi64(hsum, _MM_SHUFFLE(3, 1, 2, 0));
167
1.34M
      hsum = _mm256_add_epi16(hsum, hsum);
168
1.34M
      _mm256_storeu_si256(row, hsum);
169
170
1.34M
      __m256i top_2 = _mm256_loadu_si256((__m256i *)(input + 32));
171
1.34M
      __m256i bot_2 =
172
1.34M
          _mm256_loadu_si256((__m256i *)(input + 32 + input_stride));
173
1.34M
      __m256i sum_2 = _mm256_add_epi16(top_2, bot_2);
174
175
1.34M
      __m256i top_3 = _mm256_loadu_si256((__m256i *)(input + 48));
176
1.34M
      __m256i bot_3 =
177
1.34M
          _mm256_loadu_si256((__m256i *)(input + 48 + input_stride));
178
1.34M
      __m256i sum_3 = _mm256_add_epi16(top_3, bot_3);
179
180
1.34M
      __m256i hsum_1 = _mm256_hadd_epi16(sum_2, sum_3);
181
1.34M
      hsum_1 = _mm256_permute4x64_epi64(hsum_1, _MM_SHUFFLE(3, 1, 2, 0));
182
1.34M
      hsum_1 = _mm256_add_epi16(hsum_1, hsum_1);
183
1.34M
      _mm256_storeu_si256(row + 1, hsum_1);
184
1.34M
    }
185
2.25M
    input += luma_stride;
186
2.25M
  } while ((row += CFL_BUF_LINE_I256) < row_end);
187
127k
}
188
189
static void cfl_luma_subsampling_420_hbd_121_avx2_w4(const uint16_t *input,
190
                                                     int input_stride,
191
                                                     uint16_t *output_q3,
192
0
                                                     int height) {
193
  // Shuffle mask to get {p2, p0} from a 4-element vector.
194
0
  const __m128i center_mask =
195
0
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 4, 1, 0);
196
  // Shuffle mask for left taps {p1, p0}, handling the i=0 boundary.
197
0
  const __m128i left_mask =
198
0
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 2, 1, 0);
199
  // Shuffle mask for right taps {p3, p1}.
200
0
  const __m128i right_mask =
201
0
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 7, 6, 3, 2);
202
203
0
  for (int j = 0; j < height; j += 2) {
204
0
    const __m128i top = _mm_loadl_epi64((const __m128i *)input);
205
0
    const __m128i bot =
206
0
        _mm_loadl_epi64((const __m128i *)(input + input_stride));
207
208
0
    const __m128i top_center = _mm_shuffle_epi8(top, center_mask);
209
0
    const __m128i top_left = _mm_shuffle_epi8(top, left_mask);
210
0
    const __m128i top_right = _mm_shuffle_epi8(top, right_mask);
211
0
    __m128i top_sum = _mm_add_epi16(top_left, top_right);
212
0
    top_sum = _mm_add_epi16(top_sum, _mm_add_epi16(top_center, top_center));
213
214
0
    const __m128i bot_center = _mm_shuffle_epi8(bot, center_mask);
215
0
    const __m128i bot_left = _mm_shuffle_epi8(bot, left_mask);
216
0
    const __m128i bot_right = _mm_shuffle_epi8(bot, right_mask);
217
0
    __m128i bot_sum = _mm_add_epi16(bot_left, bot_right);
218
0
    bot_sum = _mm_add_epi16(bot_sum, _mm_add_epi16(bot_center, bot_center));
219
220
0
    const __m128i final_sum = _mm_add_epi16(top_sum, bot_sum);
221
222
0
    _mm_storeu_si32((void *)output_q3, final_sum);
223
224
0
    input += input_stride << 1;
225
0
    output_q3 += CFL_BUF_LINE;
226
0
  }
227
0
}
228
229
static void cfl_luma_subsampling_420_hbd_121_avx2_w8(const uint16_t *input,
230
                                                     int input_stride,
231
                                                     uint16_t *output_q3,
232
9.40k
                                                     int height) {
233
  // Shuffle mask to select {p6, p4, p2, p0} from a 128-bit vector.
234
9.40k
  const __m128i subsample_mask =
235
9.40k
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 13, 12, 9, 8, 5, 4, 1, 0);
236
237
  // Shuffle mask to clamp left edge: [p0, p0, p1, p2, p3, p4, p5, p6]
238
  // This mimics the C code's 'left = AVMMAX(0, i - 1)' for i=0
239
9.40k
  const __m128i left_edge_mask =
240
9.40k
      _mm_set_epi8(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0);
241
242
  // Shuffle mask to clamp right edge: [p1, p2, p3, p4, p5, p6, p7, p7]
243
  // This mimics the C code's filter for the last pixel (i=6)
244
9.40k
  const __m128i right_edge_mask =
245
9.40k
      _mm_set_epi8(15, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2);
246
247
107k
  for (int j = 0; j < height; j += 2) {
248
98.2k
    const uint16_t *top = input;
249
98.2k
    const uint16_t *bot = input + input_stride;
250
251
    // 1. Load 8 center pixels (SAFE).
252
98.2k
    const __m128i top_center = _mm_loadu_si128((const __m128i *)top);
253
98.2k
    const __m128i bot_center = _mm_loadu_si128((const __m128i *)bot);
254
255
    // 1a. Construct left/right taps by shuffling center (SAFE).
256
    // This avoids reading from (top - 1) and (top + 8).
257
98.2k
    const __m128i top_left = _mm_shuffle_epi8(top_center, left_edge_mask);
258
98.2k
    const __m128i top_right = _mm_shuffle_epi8(top_center, right_edge_mask);
259
260
98.2k
    const __m128i bot_left = _mm_shuffle_epi8(bot_center, left_edge_mask);
261
98.2k
    const __m128i bot_right = _mm_shuffle_epi8(bot_center, right_edge_mask);
262
263
    // 2. Apply the horizontal 1-2-1 filter.
264
98.2k
    const __m128i center_2x = _mm_add_epi16(top_center, top_center);
265
98.2k
    const __m128i top_sum_unpacked =
266
98.2k
        _mm_add_epi16(_mm_add_epi16(top_left, top_right), center_2x);
267
268
98.2k
    const __m128i bot_center_2x = _mm_add_epi16(bot_center, bot_center);
269
98.2k
    const __m128i bot_sum_unpacked =
270
98.2k
        _mm_add_epi16(_mm_add_epi16(bot_left, bot_right), bot_center_2x);
271
272
    // 3. Apply the vertical filter.
273
98.2k
    const __m128i sum_unpacked =
274
98.2k
        _mm_add_epi16(top_sum_unpacked, bot_sum_unpacked);
275
276
    // 4. Subsample: Select every other pixel.
277
98.2k
    const __m128i final_sum = _mm_shuffle_epi8(sum_unpacked, subsample_mask);
278
279
    // 5. Store the final 4 subsampled pixels (64 bits).
280
98.2k
    _mm_storel_epi64((__m128i *)output_q3, final_sum);
281
282
98.2k
    input += input_stride << 1;
283
98.2k
    output_q3 += CFL_BUF_LINE;
284
98.2k
  }
285
9.40k
}
286
287
static void cfl_luma_subsampling_420_hbd_121_avx2_w16(const uint16_t *input,
288
                                                      int input_stride,
289
                                                      uint16_t *output_q3,
290
7.55k
                                                      int height) {
291
  // SSSE3 shuffle mask to select {p6, p4, p2, p0} from a 128-bit lane.
292
7.55k
  const __m128i subsample_mask =
293
7.55k
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 13, 12, 9, 8, 5, 4, 1, 0);
294
295
  // 128-bit shuffle mask to clamp left edge: [p0, p0, p1, p2, p3, p4, p5, p6]
296
7.55k
  const __m128i left_edge_mask_lo =
297
7.55k
      _mm_set_epi8(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0);
298
299
  // 128-bit shuffle mask to clamp right edge: [p9, p10, p11, p12, p13, p14,
300
  // p15, p15] (relative to the high 128-bit lane)
301
7.55k
  const __m128i right_edge_mask_hi =
302
7.55k
      _mm_set_epi8(15, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2);
303
304
89.0k
  for (int j = 0; j < height; j += 2) {
305
81.5k
    const uint16_t *top = input;
306
81.5k
    const uint16_t *bot = input + input_stride;
307
308
    // 1. Load 16 center pixels (SAFE).
309
81.5k
    const __m256i top_center = _mm256_loadu_si256((const __m256i *)top);
310
81.5k
    const __m256i bot_center = _mm256_loadu_si256((const __m256i *)bot);
311
312
    // 1a. Extract 128-bit lanes to perform shuffles and alignments.
313
81.5k
    const __m128i top_lo =
314
81.5k
        _mm256_extracti128_si256(top_center, 0);  // [t7...t0]
315
81.5k
    const __m128i top_hi =
316
81.5k
        _mm256_extracti128_si256(top_center, 1);  // [t15...t8]
317
81.5k
    const __m128i bot_lo =
318
81.5k
        _mm256_extracti128_si256(bot_center, 0);  // [b7...b0]
319
81.5k
    const __m128i bot_hi =
320
81.5k
        _mm256_extracti128_si256(bot_center, 1);  // [b15...b8]
321
322
    // 1b. Construct 'left' vectors
323
    // top_left_lo = [t0, t0, t1, t2, t3, t4, t5, t6]
324
81.5k
    const __m128i top_left_lo = _mm_shuffle_epi8(top_lo, left_edge_mask_lo);
325
    // top_left_hi = [t7, t8, t9, t10, t11, t12, t13, t14]
326
81.5k
    const __m128i top_left_hi = _mm_alignr_epi8(top_hi, top_lo, 14);
327
81.5k
    const __m256i top_left = _mm256_set_m128i(top_left_hi, top_left_lo);
328
329
81.5k
    const __m128i bot_left_lo = _mm_shuffle_epi8(bot_lo, left_edge_mask_lo);
330
81.5k
    const __m128i bot_left_hi = _mm_alignr_epi8(bot_hi, bot_lo, 14);
331
81.5k
    const __m256i bot_left = _mm256_set_m128i(bot_left_hi, bot_left_lo);
332
333
    // 1c. Construct 'right' vectors
334
    // top_right_lo = [t1, t2, t3, t4, t5, t6, t7, t8]
335
81.5k
    const __m128i top_right_lo = _mm_alignr_epi8(top_hi, top_lo, 2);
336
    // top_right_hi = [t9, t10, t11, t12, t13, t14, t15, t15]
337
81.5k
    const __m128i top_right_hi = _mm_shuffle_epi8(top_hi, right_edge_mask_hi);
338
81.5k
    const __m256i top_right = _mm256_set_m128i(top_right_hi, top_right_lo);
339
340
81.5k
    const __m128i bot_right_lo = _mm_alignr_epi8(bot_hi, bot_lo, 2);
341
81.5k
    const __m128i bot_right_hi = _mm_shuffle_epi8(bot_hi, right_edge_mask_hi);
342
81.5k
    const __m256i bot_right = _mm256_set_m128i(bot_right_hi, bot_right_lo);
343
344
    // 2. Apply the horizontal 1-2-1 filter (left + 2*center + right).
345
81.5k
    const __m256i center_2x = _mm256_add_epi16(top_center, top_center);
346
81.5k
    const __m256i top_sum_unpacked =
347
81.5k
        _mm256_add_epi16(_mm256_add_epi16(top_left, top_right), center_2x);
348
349
81.5k
    const __m256i bot_center_2x = _mm256_add_epi16(bot_center, bot_center);
350
81.5k
    const __m256i bot_sum_unpacked =
351
81.5k
        _mm256_add_epi16(_mm256_add_epi16(bot_left, bot_right), bot_center_2x);
352
353
    // 3. Apply the vertical filter by adding the top and bottom results.
354
81.5k
    const __m256i sum_unpacked =
355
81.5k
        _mm256_add_epi16(top_sum_unpacked, bot_sum_unpacked);
356
357
    // 4. Subsample: Select every other pixel from the 16 results.
358
81.5k
    const __m128i sum_lo = _mm256_extracti128_si256(sum_unpacked, 0);
359
81.5k
    const __m128i sum_hi = _mm256_extracti128_si256(sum_unpacked, 1);
360
361
81.5k
    const __m128i subsampled_lo = _mm_shuffle_epi8(sum_lo, subsample_mask);
362
81.5k
    const __m128i subsampled_hi = _mm_shuffle_epi8(sum_hi, subsample_mask);
363
364
81.5k
    const __m128i final_sum = _mm_unpacklo_epi64(subsampled_lo, subsampled_hi);
365
366
    // 5. Store the final 8 subsampled pixels.
367
81.5k
    _mm_storeu_si128((__m128i *)output_q3, final_sum);
368
369
81.5k
    input += input_stride << 1;
370
81.5k
    output_q3 += CFL_BUF_LINE;
371
81.5k
  }
372
7.55k
}
373
374
static void cfl_luma_subsampling_420_hbd_121_avx2_w32(const uint16_t *input,
375
                                                      int input_stride,
376
                                                      uint16_t *output_q3,
377
10.4k
                                                      int height) {
378
10.4k
  const __m128i subsample_mask =
379
10.4k
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 13, 12, 9, 8, 5, 4, 1, 0);
380
381
  // Mask to clamp left edge of the first 16-pixel block: [p0, p0, p1...p6]
382
10.4k
  const __m128i left_edge_mask_lo =
383
10.4k
      _mm_set_epi8(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0);
384
385
  // Mask to clamp right edge of the second 16-pixel block: [p25...p31,p31]
386
10.4k
  const __m128i right_edge_mask_hi =
387
10.4k
      _mm_set_epi8(15, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2);
388
389
178k
  for (int j = 0; j < height; j += 2) {
390
168k
    const uint16_t *top = input;
391
168k
    const uint16_t *bot = input + input_stride;
392
393
    // Process first 16 pixels (Left-clamped)
394
168k
    {
395
      // 1. Load center and right taps (SAFE).
396
168k
      const __m256i top_center = _mm256_loadu_si256((const __m256i *)top);
397
168k
      const __m256i top_right = _mm256_loadu_si256((const __m256i *)(top + 1));
398
168k
      const __m256i bot_center = _mm256_loadu_si256((const __m256i *)bot);
399
168k
      const __m256i bot_right = _mm256_loadu_si256((const __m256i *)(bot + 1));
400
401
      // 1a. Extract lanes to build the clamped left-tap vectors.
402
168k
      const __m128i top_lo =
403
168k
          _mm256_extracti128_si256(top_center, 0);  // [t7...t0]
404
168k
      const __m128i top_hi =
405
168k
          _mm256_extracti128_si256(top_center, 1);  // [t15...t8]
406
168k
      const __m128i bot_lo = _mm256_extracti128_si256(bot_center, 0);
407
168k
      const __m128i bot_hi = _mm256_extracti128_si256(bot_center, 1);
408
409
      // 1b. Construct 'left' vectors (SAFE)
410
      // top_left_lo = [t0, t0, t1, t2, t3, t4, t5, t6]
411
168k
      const __m128i top_left_lo = _mm_shuffle_epi8(top_lo, left_edge_mask_lo);
412
      // top_left_hi = [t7, t8, t9, t10, t11, t12, t13, t14]
413
168k
      const __m128i top_left_hi = _mm_alignr_epi8(top_hi, top_lo, 14);
414
168k
      const __m256i top_left = _mm256_set_m128i(top_left_hi, top_left_lo);
415
416
168k
      const __m128i bot_left_lo = _mm_shuffle_epi8(bot_lo, left_edge_mask_lo);
417
168k
      const __m128i bot_left_hi = _mm_alignr_epi8(bot_hi, bot_lo, 14);
418
168k
      const __m256i bot_left = _mm256_set_m128i(bot_left_hi, bot_left_lo);
419
420
      // 2. Calculate sum
421
168k
      const __m256i top_sum_unpacked =
422
168k
          _mm256_add_epi16(_mm256_add_epi16(top_left, top_right),
423
168k
                           _mm256_add_epi16(top_center, top_center));
424
168k
      const __m256i bot_sum_unpacked =
425
168k
          _mm256_add_epi16(_mm256_add_epi16(bot_left, bot_right),
426
168k
                           _mm256_add_epi16(bot_center, bot_center));
427
168k
      const __m256i sum_unpacked =
428
168k
          _mm256_add_epi16(top_sum_unpacked, bot_sum_unpacked);
429
430
      // 3. Subsample and store
431
168k
      const __m128i sum_lo = _mm256_extracti128_si256(sum_unpacked, 0);
432
168k
      const __m128i sum_hi = _mm256_extracti128_si256(sum_unpacked, 1);
433
168k
      const __m128i subsampled_lo = _mm_shuffle_epi8(sum_lo, subsample_mask);
434
168k
      const __m128i subsampled_hi = _mm_shuffle_epi8(sum_hi, subsample_mask);
435
168k
      const __m128i final_sum =
436
168k
          _mm_unpacklo_epi64(subsampled_lo, subsampled_hi);
437
438
168k
      _mm_storeu_si128((__m128i *)output_q3, final_sum);
439
168k
    }
440
441
    // Process second 16 pixels (Right-clamped)
442
168k
    {
443
      // 1. Load left and center taps (SAFE).
444
168k
      const __m256i top_left = _mm256_loadu_si256((const __m256i *)(top + 15));
445
168k
      const __m256i top_center =
446
168k
          _mm256_loadu_si256((const __m256i *)(top + 16));
447
168k
      const __m256i bot_left = _mm256_loadu_si256((const __m256i *)(bot + 15));
448
168k
      const __m256i bot_center =
449
168k
          _mm256_loadu_si256((const __m256i *)(bot + 16));
450
451
      // 1a. Extract lanes to build the clamped right-tap vectors.
452
168k
      const __m128i top_lo =
453
168k
          _mm256_extracti128_si256(top_center, 0);  // [t23...t16]
454
168k
      const __m128i top_hi =
455
168k
          _mm256_extracti128_si256(top_center, 1);  // [t31...t24]
456
168k
      const __m128i bot_lo = _mm256_extracti128_si256(bot_center, 0);
457
168k
      const __m128i bot_hi = _mm256_extracti128_si256(bot_center, 1);
458
459
      // 1b. Construct 'right' vectors (SAFE)
460
      // top_right_lo = [t17, t18, t19, t20, t21, t22, t23, t24]
461
168k
      const __m128i top_right_lo = _mm_alignr_epi8(top_hi, top_lo, 2);
462
      // top_right_hi = [t25, t26, t27, t28, t29, t30, t31, t31]
463
168k
      const __m128i top_right_hi = _mm_shuffle_epi8(top_hi, right_edge_mask_hi);
464
168k
      const __m256i top_right = _mm256_set_m128i(top_right_hi, top_right_lo);
465
466
168k
      const __m128i bot_right_lo = _mm_alignr_epi8(bot_hi, bot_lo, 2);
467
168k
      const __m128i bot_right_hi = _mm_shuffle_epi8(bot_hi, right_edge_mask_hi);
468
168k
      const __m256i bot_right = _mm256_set_m128i(bot_right_hi, bot_right_lo);
469
470
      // 2. Calculate sum
471
168k
      const __m256i top_sum_unpacked =
472
168k
          _mm256_add_epi16(_mm256_add_epi16(top_left, top_right),
473
168k
                           _mm256_add_epi16(top_center, top_center));
474
168k
      const __m256i bot_sum_unpacked =
475
168k
          _mm256_add_epi16(_mm256_add_epi16(bot_left, bot_right),
476
168k
                           _mm256_add_epi16(bot_center, bot_center));
477
168k
      const __m256i sum_unpacked =
478
168k
          _mm256_add_epi16(top_sum_unpacked, bot_sum_unpacked);
479
480
      // 3. Subsample and store
481
168k
      const __m128i sum_lo = _mm256_extracti128_si256(sum_unpacked, 0);
482
168k
      const __m128i sum_hi = _mm256_extracti128_si256(sum_unpacked, 1);
483
168k
      const __m128i subsampled_lo = _mm_shuffle_epi8(sum_lo, subsample_mask);
484
168k
      const __m128i subsampled_hi = _mm_shuffle_epi8(sum_hi, subsample_mask);
485
168k
      const __m128i final_sum =
486
168k
          _mm_unpacklo_epi64(subsampled_lo, subsampled_hi);
487
488
168k
      _mm_storeu_si128((__m128i *)(output_q3 + 8), final_sum);
489
168k
    }
490
491
168k
    input += input_stride << 1;
492
168k
    output_q3 += CFL_BUF_LINE;
493
168k
  }
494
10.4k
}
495
496
static void cfl_luma_subsampling_420_hbd_121_avx2_w64(const uint16_t *input,
497
                                                      int input_stride,
498
                                                      uint16_t *output_q3,
499
15.7k
                                                      int height) {
500
15.7k
  const __m128i subsample_mask =
501
15.7k
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 13, 12, 9, 8, 5, 4, 1, 0);
502
503
  // Mask to clamp left edge of the first 16-pixel block: [p0, p0, p1...p6]
504
15.7k
  const __m128i left_edge_mask_lo =
505
15.7k
      _mm_set_epi8(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0);
506
507
  // Mask to clamp right edge of the last 16-pixel block: [p57...p63,p63]
508
  // (relative to the high 128-bit lane of the last 16-pixel chunk)
509
15.7k
  const __m128i right_edge_mask_hi =
510
15.7k
      _mm_set_epi8(15, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2);
511
512
291k
  for (int j = 0; j < height; j += 2) {
513
276k
    const uint16_t *top = input;
514
276k
    const uint16_t *bot = input + input_stride;
515
516
    // --- Block 0 (i=0, pixels 0-15): Left-clamped ---
517
276k
    {
518
      // 1. Load center and right taps (SAFE).
519
276k
      const __m256i top_center = _mm256_loadu_si256((const __m256i *)top);
520
276k
      const __m256i top_right = _mm256_loadu_si256((const __m256i *)(top + 1));
521
276k
      const __m256i bot_center = _mm256_loadu_si256((const __m256i *)bot);
522
276k
      const __m256i bot_right = _mm256_loadu_si256((const __m256i *)(bot + 1));
523
524
      // 1a. Extract lanes to build the clamped left-tap vectors.
525
276k
      const __m128i top_lo = _mm256_extracti128_si256(top_center, 0);
526
276k
      const __m128i top_hi = _mm256_extracti128_si256(top_center, 1);
527
276k
      const __m128i bot_lo = _mm256_extracti128_si256(bot_center, 0);
528
276k
      const __m128i bot_hi = _mm256_extracti128_si256(bot_center, 1);
529
530
      // 1b. Construct 'left' vectors (SAFE)
531
276k
      const __m128i top_left_lo = _mm_shuffle_epi8(top_lo, left_edge_mask_lo);
532
276k
      const __m128i top_left_hi = _mm_alignr_epi8(top_hi, top_lo, 14);
533
276k
      const __m256i top_left = _mm256_set_m128i(top_left_hi, top_left_lo);
534
535
276k
      const __m128i bot_left_lo = _mm_shuffle_epi8(bot_lo, left_edge_mask_lo);
536
276k
      const __m128i bot_left_hi = _mm_alignr_epi8(bot_hi, bot_lo, 14);
537
276k
      const __m256i bot_left = _mm256_set_m128i(bot_left_hi, bot_left_lo);
538
539
      // 2. Calculate sum
540
276k
      const __m256i top_sum_unpacked =
541
276k
          _mm256_add_epi16(_mm256_add_epi16(top_left, top_right),
542
276k
                           _mm256_add_epi16(top_center, top_center));
543
276k
      const __m256i bot_sum_unpacked =
544
276k
          _mm256_add_epi16(_mm256_add_epi16(bot_left, bot_right),
545
276k
                           _mm256_add_epi16(bot_center, bot_center));
546
276k
      const __m256i sum_unpacked =
547
276k
          _mm256_add_epi16(top_sum_unpacked, bot_sum_unpacked);
548
549
      // 3. Subsample and store
550
276k
      const __m128i sum_lo = _mm256_extracti128_si256(sum_unpacked, 0);
551
276k
      const __m128i sum_hi = _mm256_extracti128_si256(sum_unpacked, 1);
552
276k
      const __m128i subsampled_lo = _mm_shuffle_epi8(sum_lo, subsample_mask);
553
276k
      const __m128i subsampled_hi = _mm_shuffle_epi8(sum_hi, subsample_mask);
554
276k
      const __m128i final_sum =
555
276k
          _mm_unpacklo_epi64(subsampled_lo, subsampled_hi);
556
557
276k
      _mm_storeu_si128((__m128i *)output_q3, final_sum);
558
276k
    }
559
560
    // --- Block 1 (i=1, pixels 16-31): Middle (SAFE) ---
561
276k
    {
562
276k
      const int offset = 16;
563
276k
      const __m256i top_left =
564
276k
          _mm256_loadu_si256((const __m256i *)(top + offset - 1));
565
276k
      const __m256i top_center =
566
276k
          _mm256_loadu_si256((const __m256i *)(top + offset));
567
276k
      const __m256i top_right =
568
276k
          _mm256_loadu_si256((const __m256i *)(top + offset + 1));
569
276k
      const __m256i bot_left =
570
276k
          _mm256_loadu_si256((const __m256i *)(bot + offset - 1));
571
276k
      const __m256i bot_center =
572
276k
          _mm256_loadu_si256((const __m256i *)(bot + offset));
573
276k
      const __m256i bot_right =
574
276k
          _mm256_loadu_si256((const __m256i *)(bot + offset + 1));
575
576
276k
      const __m256i top_sum_unpacked =
577
276k
          _mm256_add_epi16(_mm256_add_epi16(top_left, top_right),
578
276k
                           _mm256_add_epi16(top_center, top_center));
579
276k
      const __m256i bot_sum_unpacked =
580
276k
          _mm256_add_epi16(_mm256_add_epi16(bot_left, bot_right),
581
276k
                           _mm256_add_epi16(bot_center, bot_center));
582
276k
      const __m256i sum_unpacked =
583
276k
          _mm256_add_epi16(top_sum_unpacked, bot_sum_unpacked);
584
585
276k
      const __m128i sum_lo = _mm256_extracti128_si256(sum_unpacked, 0);
586
276k
      const __m128i sum_hi = _mm256_extracti128_si256(sum_unpacked, 1);
587
276k
      const __m128i subsampled_lo = _mm_shuffle_epi8(sum_lo, subsample_mask);
588
276k
      const __m128i subsampled_hi = _mm_shuffle_epi8(sum_hi, subsample_mask);
589
276k
      const __m128i final_sum =
590
276k
          _mm_unpacklo_epi64(subsampled_lo, subsampled_hi);
591
592
276k
      _mm_storeu_si128((__m128i *)(output_q3 + 8), final_sum);
593
276k
    }
594
595
    // --- Block 2 (i=2, pixels 32-47): Middle (SAFE) ---
596
276k
    {
597
276k
      const int offset = 32;
598
276k
      const __m256i top_left =
599
276k
          _mm256_loadu_si256((const __m256i *)(top + offset - 1));
600
276k
      const __m256i top_center =
601
276k
          _mm256_loadu_si256((const __m256i *)(top + offset));
602
276k
      const __m256i top_right =
603
276k
          _mm256_loadu_si256((const __m256i *)(top + offset + 1));
604
276k
      const __m256i bot_left =
605
276k
          _mm256_loadu_si256((const __m256i *)(bot + offset - 1));
606
276k
      const __m256i bot_center =
607
276k
          _mm256_loadu_si256((const __m256i *)(bot + offset));
608
276k
      const __m256i bot_right =
609
276k
          _mm256_loadu_si256((const __m256i *)(bot + offset + 1));
610
611
276k
      const __m256i top_sum_unpacked =
612
276k
          _mm256_add_epi16(_mm256_add_epi16(top_left, top_right),
613
276k
                           _mm256_add_epi16(top_center, top_center));
614
276k
      const __m256i bot_sum_unpacked =
615
276k
          _mm256_add_epi16(_mm256_add_epi16(bot_left, bot_right),
616
276k
                           _mm256_add_epi16(bot_center, bot_center));
617
276k
      const __m256i sum_unpacked =
618
276k
          _mm256_add_epi16(top_sum_unpacked, bot_sum_unpacked);
619
620
276k
      const __m128i sum_lo = _mm256_extracti128_si256(sum_unpacked, 0);
621
276k
      const __m128i sum_hi = _mm256_extracti128_si256(sum_unpacked, 1);
622
276k
      const __m128i subsampled_lo = _mm_shuffle_epi8(sum_lo, subsample_mask);
623
276k
      const __m128i subsampled_hi = _mm_shuffle_epi8(sum_hi, subsample_mask);
624
276k
      const __m128i final_sum =
625
276k
          _mm_unpacklo_epi64(subsampled_lo, subsampled_hi);
626
627
276k
      _mm_storeu_si128((__m128i *)(output_q3 + 16), final_sum);
628
276k
    }
629
630
    // --- Block 3 (i=3, pixels 48-63): Right-clamped ---
631
276k
    {
632
276k
      const int offset = 48;
633
      // 1. Load left and center taps (SAFE).
634
276k
      const __m256i top_left =
635
276k
          _mm256_loadu_si256((const __m256i *)(top + offset - 1));
636
276k
      const __m256i top_center =
637
276k
          _mm256_loadu_si256((const __m256i *)(top + offset));
638
276k
      const __m256i bot_left =
639
276k
          _mm256_loadu_si256((const __m256i *)(bot + offset - 1));
640
276k
      const __m256i bot_center =
641
276k
          _mm256_loadu_si256((const __m256i *)(bot + offset));
642
643
      // 1a. Extract lanes to build the clamped right-tap vectors.
644
276k
      const __m128i top_lo =
645
276k
          _mm256_extracti128_si256(top_center, 0);  // [t55...t48]
646
276k
      const __m128i top_hi =
647
276k
          _mm256_extracti128_si256(top_center, 1);  // [t63...t56]
648
276k
      const __m128i bot_lo = _mm256_extracti128_si256(bot_center, 0);
649
276k
      const __m128i bot_hi = _mm256_extracti128_si256(bot_center, 1);
650
651
      // 1b. Construct 'right' vectors (SAFE)
652
      // top_right_lo = [t49, t50, t51, t52, t53, t54, t55, t56]
653
276k
      const __m128i top_right_lo = _mm_alignr_epi8(top_hi, top_lo, 2);
654
      // top_right_hi = [t57, t58, t59, t60, t61, t62, t63, t63]
655
276k
      const __m128i top_right_hi = _mm_shuffle_epi8(top_hi, right_edge_mask_hi);
656
276k
      const __m256i top_right = _mm256_set_m128i(top_right_hi, top_right_lo);
657
658
276k
      const __m128i bot_right_lo = _mm_alignr_epi8(bot_hi, bot_lo, 2);
659
276k
      const __m128i bot_right_hi = _mm_shuffle_epi8(bot_hi, right_edge_mask_hi);
660
276k
      const __m256i bot_right = _mm256_set_m128i(bot_right_hi, bot_right_lo);
661
662
      // 2. Calculate sum
663
276k
      const __m256i top_sum_unpacked =
664
276k
          _mm256_add_epi16(_mm256_add_epi16(top_left, top_right),
665
276k
                           _mm256_add_epi16(top_center, top_center));
666
276k
      const __m256i bot_sum_unpacked =
667
276k
          _mm256_add_epi16(_mm256_add_epi16(bot_left, bot_right),
668
276k
                           _mm256_add_epi16(bot_center, bot_center));
669
276k
      const __m256i sum_unpacked =
670
276k
          _mm256_add_epi16(top_sum_unpacked, bot_sum_unpacked);
671
672
      // 3. Subsample and store
673
276k
      const __m128i sum_lo = _mm256_extracti128_si256(sum_unpacked, 0);
674
276k
      const __m128i sum_hi = _mm256_extracti128_si256(sum_unpacked, 1);
675
276k
      const __m128i subsampled_lo = _mm_shuffle_epi8(sum_lo, subsample_mask);
676
276k
      const __m128i subsampled_hi = _mm_shuffle_epi8(sum_hi, subsample_mask);
677
276k
      const __m128i final_sum =
678
276k
          _mm_unpacklo_epi64(subsampled_lo, subsampled_hi);
679
680
276k
      _mm_storeu_si128((__m128i *)(output_q3 + 24), final_sum);
681
276k
    }
682
683
276k
    input += input_stride << 1;
684
276k
    output_q3 += CFL_BUF_LINE;
685
276k
  }
686
15.7k
}
687
688
static void cfl_luma_subsampling_420_hbd_121_avx2(const uint16_t *input,
689
                                                  int input_stride,
690
                                                  uint16_t *output_q3,
691
43.1k
                                                  int width, int height) {
692
43.1k
  switch (width) {
693
0
    case 4:
694
0
      cfl_luma_subsampling_420_hbd_121_avx2_w4(input, input_stride, output_q3,
695
0
                                               height);
696
0
      break;
697
9.40k
    case 8:
698
9.40k
      cfl_luma_subsampling_420_hbd_121_avx2_w8(input, input_stride, output_q3,
699
9.40k
                                               height);
700
9.40k
      break;
701
7.55k
    case 16:
702
7.55k
      cfl_luma_subsampling_420_hbd_121_avx2_w16(input, input_stride, output_q3,
703
7.55k
                                                height);
704
7.55k
      break;
705
10.4k
    case 32:
706
10.4k
      cfl_luma_subsampling_420_hbd_121_avx2_w32(input, input_stride, output_q3,
707
10.4k
                                                height);
708
10.4k
      break;
709
15.7k
    case 64:
710
15.7k
      cfl_luma_subsampling_420_hbd_121_avx2_w64(input, input_stride, output_q3,
711
15.7k
                                                height);
712
15.7k
      break;
713
0
    default: assert(0 && "Invalid width.");
714
43.1k
  }
715
43.1k
}
716
717
CFL_GET_SUBSAMPLE_FUNCTION_AVX2(420, hbd)
718
719
// AVX2 implementation for cfl_luma_subsampling_420_hbd_colocated_c with width=4
720
static void cfl_luma_subsampling_420_hbd_colocated_avx2_w4(
721
0
    const uint16_t *input, int input_stride, uint16_t *output_q3, int height) {
722
  // Masks to construct {p2, p0} and {p1, p0} etc. from a 4-pixel vector
723
0
  const __m128i center_mask =
724
0
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 4, 1, 0);
725
0
  const __m128i left_mask =
726
0
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 2, 1, 0);
727
0
  const __m128i right_mask =
728
0
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 7, 6, 3, 2);
729
730
0
  for (int j = 0; j < height; j += 2) {
731
0
    const uint16_t *top_ptr = ((j & 63) == 0) ? input : (input - input_stride);
732
0
    const uint16_t *bot_ptr = input + input_stride;
733
734
0
    const __m128i v_in = _mm_loadl_epi64((const __m128i *)input);
735
0
    const __m128i v_top = _mm_loadl_epi64((const __m128i *)top_ptr);
736
0
    const __m128i v_bot = _mm_loadl_epi64((const __m128i *)bot_ptr);
737
738
    // left + right
739
0
    __m128i sum = _mm_add_epi16(_mm_shuffle_epi8(v_in, left_mask),
740
0
                                _mm_shuffle_epi8(v_in, right_mask));
741
    // + top + bottom
742
0
    sum = _mm_add_epi16(sum, _mm_shuffle_epi8(v_top, center_mask));
743
0
    sum = _mm_add_epi16(sum, _mm_shuffle_epi8(v_bot, center_mask));
744
    // + 4 * center
745
0
    const __m128i center4x =
746
0
        _mm_slli_epi16(_mm_shuffle_epi8(v_in, center_mask), 2);
747
0
    sum = _mm_add_epi16(sum, center4x);
748
749
0
    _mm_storeu_si32((void *)output_q3, sum);
750
751
0
    input += input_stride << 1;
752
0
    output_q3 += CFL_BUF_LINE;
753
0
  }
754
0
}
755
756
// AVX2 implementation for cfl_luma_subsampling_420_hbd_colocated_c with width=8
757
static void cfl_luma_subsampling_420_hbd_colocated_avx2_w8(
758
9.85k
    const uint16_t *input, int input_stride, uint16_t *output_q3, int height) {
759
9.85k
  const __m128i subsample_mask =
760
9.85k
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 13, 12, 9, 8, 5, 4, 1, 0);
761
762
  // Shuffle mask to clamp left edge: [p0, p0, p1, p2, p3, p4, p5, p6]
763
9.85k
  const __m128i left_edge_mask =
764
9.85k
      _mm_set_epi8(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0);
765
766
  // Shuffle mask to clamp right edge: [p1, p2, p3, p4, p5, p6, p7, p7]
767
9.85k
  const __m128i right_edge_mask =
768
9.85k
      _mm_set_epi8(15, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2);
769
770
113k
  for (int j = 0; j < height; j += 2) {
771
103k
    const uint16_t *top_ptr = ((j & 63) == 0) ? input : (input - input_stride);
772
103k
    const uint16_t *bot_ptr = input + input_stride;
773
774
    // 1. Load center, top, and bottom taps (SAFE).
775
103k
    const __m128i center = _mm_loadu_si128((const __m128i *)input);
776
103k
    const __m128i top = _mm_loadu_si128((const __m128i *)top_ptr);
777
103k
    const __m128i bottom = _mm_loadu_si128((const __m128i *)bot_ptr);
778
779
    // 2. Construct left and right taps from center (SAFE).
780
103k
    const __m128i left = _mm_shuffle_epi8(center, left_edge_mask);
781
103k
    const __m128i right = _mm_shuffle_epi8(center, right_edge_mask);
782
783
    // 3. Perform filter calculation.
784
103k
    __m128i sum = _mm_add_epi16(left, right);
785
103k
    sum = _mm_add_epi16(sum, top);
786
103k
    sum = _mm_add_epi16(sum, bottom);
787
103k
    const __m128i center4x = _mm_slli_epi16(center, 2);  // 4 * center
788
103k
    sum = _mm_add_epi16(sum, center4x);
789
790
    // 4. Subsample and store.
791
103k
    const __m128i subsampled = _mm_shuffle_epi8(sum, subsample_mask);
792
103k
    _mm_storel_epi64((__m128i *)output_q3, subsampled);
793
794
103k
    input += input_stride << 1;
795
103k
    output_q3 += CFL_BUF_LINE;
796
103k
  }
797
9.85k
}
798
799
// AVX2 implementation for cfl_luma_subsampling_420_hbd_colocated_c with
800
// width=16
801
static void cfl_luma_subsampling_420_hbd_colocated_avx2_w16(
802
13.4k
    const uint16_t *input, int input_stride, uint16_t *output_q3, int height) {
803
13.4k
  const __m128i subsample_mask =
804
13.4k
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 13, 12, 9, 8, 5, 4, 1, 0);
805
806
  // 128-bit shuffle mask to clamp left edge: [p0, p0, p1, p2, p3, p4, p5, p6]
807
13.4k
  const __m128i left_edge_mask_lo =
808
13.4k
      _mm_set_epi8(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0);
809
810
  // 128-bit shuffle mask to clamp right edge: [p9, p10, p11, p12, p13, p14,
811
  // p15, p15] (relative to the high 128-bit lane)
812
13.4k
  const __m128i right_edge_mask_hi =
813
13.4k
      _mm_set_epi8(15, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2);
814
815
179k
  for (int j = 0; j < height; j += 2) {
816
165k
    const uint16_t *top_ptr = ((j & 63) == 0) ? input : (input - input_stride);
817
165k
    const uint16_t *bot_ptr = input + input_stride;
818
819
    // 1. Load center, top, and bottom taps (SAFE).
820
165k
    const __m256i center = _mm256_loadu_si256((const __m256i *)input);
821
165k
    const __m256i top = _mm256_loadu_si256((const __m256i *)top_ptr);
822
165k
    const __m256i bottom = _mm256_loadu_si256((const __m256i *)bot_ptr);
823
824
    // 1a. Extract 128-bit lanes from center to build left/right taps.
825
165k
    const __m128i center_lo = _mm256_extracti128_si256(center, 0);  // [t7...t0]
826
165k
    const __m128i center_hi =
827
165k
        _mm256_extracti128_si256(center, 1);  // [t15...t8]
828
829
    // 1b. Construct 'left' vector (SAFE)
830
    // left_lo = [t0, t0, t1, t2, t3, t4, t5, t6]
831
165k
    const __m128i left_lo = _mm_shuffle_epi8(center_lo, left_edge_mask_lo);
832
    // left_hi = [t7, t8, t9, t10, t11, t12, t13, t14]
833
165k
    const __m128i left_hi = _mm_alignr_epi8(center_hi, center_lo, 14);
834
165k
    const __m256i left = _mm256_set_m128i(left_hi, left_lo);
835
836
    // 1c. Construct 'right' vector (SAFE)
837
    // right_lo = [t1, t2, t3, t4, t5, t6, t7, t8]
838
165k
    const __m128i right_lo = _mm_alignr_epi8(center_hi, center_lo, 2);
839
    // right_hi = [t9, t10, t11, t12, t13, t14, t15, t15]
840
165k
    const __m128i right_hi = _mm_shuffle_epi8(center_hi, right_edge_mask_hi);
841
165k
    const __m256i right = _mm256_set_m128i(right_hi, right_lo);
842
843
    // 2. Apply the filter.
844
165k
    __m256i sum = _mm256_add_epi16(left, right);
845
165k
    sum = _mm256_add_epi16(sum, top);
846
165k
    sum = _mm256_add_epi16(sum, bottom);
847
165k
    const __m256i center4x = _mm256_slli_epi16(center, 2);  // 4 * center
848
165k
    sum = _mm256_add_epi16(sum, center4x);
849
850
    // 3. Subsample and store.
851
165k
    const __m128i sum_lo = _mm256_extracti128_si256(sum, 0);
852
165k
    const __m128i sum_hi = _mm256_extracti128_si256(sum, 1);
853
165k
    const __m128i subsampled_lo = _mm_shuffle_epi8(sum_lo, subsample_mask);
854
165k
    const __m128i subsampled_hi = _mm_shuffle_epi8(sum_hi, subsample_mask);
855
165k
    const __m128i final_sum = _mm_unpacklo_epi64(subsampled_lo, subsampled_hi);
856
857
165k
    _mm_storeu_si128((__m128i *)output_q3, final_sum);
858
859
165k
    input += input_stride << 1;
860
165k
    output_q3 += CFL_BUF_LINE;
861
165k
  }
862
13.4k
}
863
864
// AVX2 implementation for cfl_luma_subsampling_420_hbd_colocated_c with
865
// width=32
866
static void cfl_luma_subsampling_420_hbd_colocated_avx2_w32(
867
7.06k
    const uint16_t *input, int input_stride, uint16_t *output_q3, int height) {
868
7.06k
  const __m128i subsample_mask =
869
7.06k
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 13, 12, 9, 8, 5, 4, 1, 0);
870
871
  // Mask to clamp left edge: [p0, p0, p1...p6]
872
7.06k
  const __m128i left_edge_mask_lo =
873
7.06k
      _mm_set_epi8(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0);
874
875
  // Mask to clamp right edge: [p25...p31,p31]
876
7.06k
  const __m128i right_edge_mask_hi =
877
7.06k
      _mm_set_epi8(15, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2);
878
879
114k
  for (int j = 0; j < height; j += 2) {
880
107k
    const uint16_t *top_ptr = ((j & 63) == 0) ? input : (input - input_stride);
881
107k
    const uint16_t *bot_ptr = input + input_stride;
882
883
    // Process first 16 pixels (Left-clamped)
884
107k
    {
885
      // 1. Load center, right, top, bottom (all SAFE).
886
107k
      const __m256i center = _mm256_loadu_si256((const __m256i *)input);
887
107k
      const __m256i right = _mm256_loadu_si256((const __m256i *)(input + 1));
888
107k
      const __m256i top = _mm256_loadu_si256((const __m256i *)top_ptr);
889
107k
      const __m256i bot = _mm256_loadu_si256((const __m256i *)bot_ptr);
890
891
      // 1a. Extract lanes from center to build 'left' vector.
892
107k
      const __m128i center_lo = _mm256_extracti128_si256(center, 0);
893
107k
      const __m128i center_hi = _mm256_extracti128_si256(center, 1);
894
895
      // 1b. Construct 'left' vector (SAFE)
896
107k
      const __m128i left_lo = _mm_shuffle_epi8(center_lo, left_edge_mask_lo);
897
107k
      const __m128i left_hi = _mm_alignr_epi8(center_hi, center_lo, 14);
898
107k
      const __m256i left = _mm256_set_m128i(left_hi, left_lo);
899
900
      // 2. Calculate sum
901
107k
      __m256i sum = _mm256_add_epi16(left, right);
902
107k
      sum = _mm256_add_epi16(sum, top);
903
107k
      sum = _mm256_add_epi16(sum, bot);
904
107k
      sum = _mm256_add_epi16(sum, _mm256_slli_epi16(center, 2));
905
906
      // 3. Subsample and store
907
107k
      const __m128i sum_lo = _mm256_extracti128_si256(sum, 0);
908
107k
      const __m128i sum_hi = _mm256_extracti128_si256(sum, 1);
909
107k
      const __m128i subsampled =
910
107k
          _mm_unpacklo_epi64(_mm_shuffle_epi8(sum_lo, subsample_mask),
911
107k
                             _mm_shuffle_epi8(sum_hi, subsample_mask));
912
107k
      _mm_storeu_si128((__m128i *)output_q3, subsampled);
913
107k
    }
914
915
    // Process second 16 pixels (Right-clamped)
916
107k
    {
917
      // 1. Load left, center, top, bottom (all SAFE).
918
107k
      const __m256i left = _mm256_loadu_si256((const __m256i *)(input + 15));
919
107k
      const __m256i center = _mm256_loadu_si256((const __m256i *)(input + 16));
920
107k
      const __m256i top = _mm256_loadu_si256((const __m256i *)(top_ptr + 16));
921
107k
      const __m256i bot = _mm256_loadu_si256((const __m256i *)(bot_ptr + 16));
922
923
      // 1a. Extract lanes from center to build 'right' vector.
924
107k
      const __m128i center_lo =
925
107k
          _mm256_extracti128_si256(center, 0);  // [t23...t16]
926
107k
      const __m128i center_hi =
927
107k
          _mm256_extracti128_si256(center, 1);  // [t31...t24]
928
929
      // 1b. Construct 'right' vector (SAFE)
930
107k
      const __m128i right_lo = _mm_alignr_epi8(center_hi, center_lo, 2);
931
107k
      const __m128i right_hi = _mm_shuffle_epi8(center_hi, right_edge_mask_hi);
932
107k
      const __m256i right = _mm256_set_m128i(right_hi, right_lo);
933
934
      // 2. Calculate sum
935
107k
      __m256i sum = _mm256_add_epi16(left, right);
936
107k
      sum = _mm256_add_epi16(sum, top);
937
107k
      sum = _mm256_add_epi16(sum, bot);
938
107k
      sum = _mm256_add_epi16(sum, _mm256_slli_epi16(center, 2));
939
940
      // 3. Subsample and store
941
107k
      const __m128i sum_lo = _mm256_extracti128_si256(sum, 0);
942
107k
      const __m128i sum_hi = _mm256_extracti128_si256(sum, 1);
943
107k
      const __m128i subsampled =
944
107k
          _mm_unpacklo_epi64(_mm_shuffle_epi8(sum_lo, subsample_mask),
945
107k
                             _mm_shuffle_epi8(sum_hi, subsample_mask));
946
107k
      _mm_storeu_si128((__m128i *)(output_q3 + 8), subsampled);
947
107k
    }
948
949
107k
    input += input_stride << 1;
950
107k
    output_q3 += CFL_BUF_LINE;
951
107k
  }
952
7.06k
}
953
954
// AVX2 implementation for cfl_luma_subsampling_420_hbd_colocated_c with
955
// width=64
956
static void cfl_luma_subsampling_420_hbd_colocated_avx2_w64(
957
5.33k
    const uint16_t *input, int input_stride, uint16_t *output_q3, int height) {
958
5.33k
  const __m128i subsample_mask =
959
5.33k
      _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 13, 12, 9, 8, 5, 4, 1, 0);
960
961
  // Mask to clamp left edge of the first 16-pixel block: [p0, p0, p1...p6]
962
5.33k
  const __m128i left_edge_mask_lo =
963
5.33k
      _mm_set_epi8(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0);
964
965
  // Mask to clamp right edge of the last 16-pixel block: [p57...p63,p63]
966
  // (relative to the high 128-bit lane of the last 16-pixel chunk)
967
5.33k
  const __m128i right_edge_mask_hi =
968
5.33k
      _mm_set_epi8(15, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2);
969
970
106k
  for (int j = 0; j < height; j += 2) {
971
101k
    const uint16_t *top_ptr = ((j & 63) == 0) ? input : (input - input_stride);
972
101k
    const uint16_t *bot_ptr = input + input_stride;
973
974
    // --- Block 0 (i=0, pixels 0-15): Left-clamped ---
975
101k
    {
976
      // 1. Load center, right, top, bottom (all SAFE).
977
101k
      const __m256i center = _mm256_loadu_si256((const __m256i *)input);
978
101k
      const __m256i right = _mm256_loadu_si256((const __m256i *)(input + 1));
979
101k
      const __m256i top = _mm256_loadu_si256((const __m256i *)top_ptr);
980
101k
      const __m256i bot = _mm256_loadu_si256((const __m256i *)bot_ptr);
981
982
      // 1a. Extract lanes to build the clamped left-tap vector.
983
101k
      const __m128i center_lo = _mm256_extracti128_si256(center, 0);
984
101k
      const __m128i center_hi = _mm256_extracti128_si256(center, 1);
985
986
      // 1b. Construct 'left' vector (SAFE)
987
101k
      const __m128i left_lo = _mm_shuffle_epi8(center_lo, left_edge_mask_lo);
988
101k
      const __m128i left_hi = _mm_alignr_epi8(center_hi, center_lo, 14);
989
101k
      const __m256i left = _mm256_set_m128i(left_hi, left_lo);
990
991
      // 2. Calculate sum
992
101k
      __m256i sum = _mm256_add_epi16(left, right);
993
101k
      sum = _mm256_add_epi16(sum, top);
994
101k
      sum = _mm256_add_epi16(sum, bot);
995
101k
      sum = _mm256_add_epi16(sum, _mm256_slli_epi16(center, 2));
996
997
      // 3. Subsample and store
998
101k
      const __m128i sum_lo = _mm256_extracti128_si256(sum, 0);
999
101k
      const __m128i sum_hi = _mm256_extracti128_si256(sum, 1);
1000
101k
      const __m128i subsampled =
1001
101k
          _mm_unpacklo_epi64(_mm_shuffle_epi8(sum_lo, subsample_mask),
1002
101k
                             _mm_shuffle_epi8(sum_hi, subsample_mask));
1003
101k
      _mm_storeu_si128((__m128i *)output_q3, subsampled);
1004
101k
    }
1005
1006
    // --- Block 1 (i=1, pixels 16-31): Middle (SAFE) ---
1007
101k
    {
1008
101k
      const int offset = 16;
1009
101k
      const __m256i left =
1010
101k
          _mm256_loadu_si256((const __m256i *)(input + offset - 1));
1011
101k
      const __m256i center =
1012
101k
          _mm256_loadu_si256((const __m256i *)(input + offset));
1013
101k
      const __m256i right =
1014
101k
          _mm256_loadu_si256((const __m256i *)(input + offset + 1));
1015
101k
      const __m256i top =
1016
101k
          _mm256_loadu_si256((const __m256i *)(top_ptr + offset));
1017
101k
      const __m256i bot =
1018
101k
          _mm256_loadu_si256((const __m256i *)(bot_ptr + offset));
1019
1020
101k
      __m256i sum = _mm256_add_epi16(left, right);
1021
101k
      sum = _mm256_add_epi16(sum, top);
1022
101k
      sum = _mm256_add_epi16(sum, bot);
1023
101k
      sum = _mm256_add_epi16(sum, _mm256_slli_epi16(center, 2));
1024
1025
101k
      const __m128i sum_lo = _mm256_extracti128_si256(sum, 0);
1026
101k
      const __m128i sum_hi = _mm256_extracti128_si256(sum, 1);
1027
101k
      const __m128i subsampled =
1028
101k
          _mm_unpacklo_epi64(_mm_shuffle_epi8(sum_lo, subsample_mask),
1029
101k
                             _mm_shuffle_epi8(sum_hi, subsample_mask));
1030
101k
      _mm_storeu_si128((__m128i *)(output_q3 + 8), subsampled);
1031
101k
    }
1032
1033
    // --- Block 2 (i=2, pixels 32-47): Middle (SAFE) ---
1034
101k
    {
1035
101k
      const int offset = 32;
1036
101k
      const __m256i left =
1037
101k
          _mm256_loadu_si256((const __m256i *)(input + offset - 1));
1038
101k
      const __m256i center =
1039
101k
          _mm256_loadu_si256((const __m256i *)(input + offset));
1040
101k
      const __m256i right =
1041
101k
          _mm256_loadu_si256((const __m256i *)(input + offset + 1));
1042
101k
      const __m256i top =
1043
101k
          _mm256_loadu_si256((const __m256i *)(top_ptr + offset));
1044
101k
      const __m256i bot =
1045
101k
          _mm256_loadu_si256((const __m256i *)(bot_ptr + offset));
1046
1047
101k
      __m256i sum = _mm256_add_epi16(left, right);
1048
101k
      sum = _mm256_add_epi16(sum, top);
1049
101k
      sum = _mm256_add_epi16(sum, bot);
1050
101k
      sum = _mm256_add_epi16(sum, _mm256_slli_epi16(center, 2));
1051
1052
101k
      const __m128i sum_lo = _mm256_extracti128_si256(sum, 0);
1053
101k
      const __m128i sum_hi = _mm256_extracti128_si256(sum, 1);
1054
101k
      const __m128i subsampled =
1055
101k
          _mm_unpacklo_epi64(_mm_shuffle_epi8(sum_lo, subsample_mask),
1056
101k
                             _mm_shuffle_epi8(sum_hi, subsample_mask));
1057
101k
      _mm_storeu_si128((__m128i *)(output_q3 + 16), subsampled);
1058
101k
    }
1059
1060
    // --- Block 3 (i=3, pixels 48-63): Right-clamped ---
1061
101k
    {
1062
101k
      const int offset = 48;
1063
      // 1. Load left, center, top, bottom (all SAFE).
1064
101k
      const __m256i left =
1065
101k
          _mm256_loadu_si256((const __m256i *)(input + offset - 1));
1066
101k
      const __m256i center =
1067
101k
          _mm256_loadu_si256((const __m256i *)(input + offset));
1068
101k
      const __m256i top =
1069
101k
          _mm256_loadu_si256((const __m256i *)(top_ptr + offset));
1070
101k
      const __m256i bot =
1071
101k
          _mm256_loadu_si256((const __m256i *)(bot_ptr + offset));
1072
1073
      // 1a. Extract lanes to build the clamped right-tap vector.
1074
101k
      const __m128i center_lo =
1075
101k
          _mm256_extracti128_si256(center, 0);  // [t55...t48]
1076
101k
      const __m128i center_hi =
1077
101k
          _mm256_extracti128_si256(center, 1);  // [t63...t56]
1078
1079
      // 1b. Construct 'right' vector (SAFE)
1080
101k
      const __m128i right_lo = _mm_alignr_epi8(center_hi, center_lo, 2);
1081
101k
      const __m128i right_hi = _mm_shuffle_epi8(center_hi, right_edge_mask_hi);
1082
101k
      const __m256i right = _mm256_set_m128i(right_hi, right_lo);
1083
1084
      // 2. Calculate sum
1085
101k
      __m256i sum = _mm256_add_epi16(left, right);
1086
101k
      sum = _mm256_add_epi16(sum, top);
1087
101k
      sum = _mm256_add_epi16(sum, bot);
1088
101k
      sum = _mm256_add_epi16(sum, _mm256_slli_epi16(center, 2));
1089
1090
      // 3. Subsample and store
1091
101k
      const __m128i sum_lo = _mm256_extracti128_si256(sum, 0);
1092
101k
      const __m128i sum_hi = _mm256_extracti128_si256(sum, 1);
1093
101k
      const __m128i subsampled =
1094
101k
          _mm_unpacklo_epi64(_mm_shuffle_epi8(sum_lo, subsample_mask),
1095
101k
                             _mm_shuffle_epi8(sum_hi, subsample_mask));
1096
101k
      _mm_storeu_si128((__m128i *)(output_q3 + 24), subsampled);
1097
101k
    }
1098
1099
101k
    input += input_stride << 1;
1100
101k
    output_q3 += CFL_BUF_LINE;
1101
101k
  }
1102
5.33k
}
1103
1104
static void cfl_luma_subsampling_420_hbd_colocated_avx2(const uint16_t *input,
1105
                                                        int input_stride,
1106
                                                        uint16_t *output_q3,
1107
35.6k
                                                        int width, int height) {
1108
35.6k
  switch (width) {
1109
0
    case 4:
1110
0
      cfl_luma_subsampling_420_hbd_colocated_avx2_w4(input, input_stride,
1111
0
                                                     output_q3, height);
1112
0
      break;
1113
9.86k
    case 8:
1114
9.86k
      cfl_luma_subsampling_420_hbd_colocated_avx2_w8(input, input_stride,
1115
9.86k
                                                     output_q3, height);
1116
9.86k
      break;
1117
13.4k
    case 16:
1118
13.4k
      cfl_luma_subsampling_420_hbd_colocated_avx2_w16(input, input_stride,
1119
13.4k
                                                      output_q3, height);
1120
13.4k
      break;
1121
7.06k
    case 32:
1122
7.06k
      cfl_luma_subsampling_420_hbd_colocated_avx2_w32(input, input_stride,
1123
7.06k
                                                      output_q3, height);
1124
7.06k
      break;
1125
5.33k
    case 64:
1126
5.33k
      cfl_luma_subsampling_420_hbd_colocated_avx2_w64(input, input_stride,
1127
5.33k
                                                      output_q3, height);
1128
5.33k
      break;
1129
0
    default: assert(0 && "Invalid width."); break;
1130
35.6k
  }
1131
35.6k
}
1132
1133
/**
1134
 * Adds 2 pixels (in a 2x1 grid) and multiplies them by 4. Resulting in a more
1135
 * precise version of a box filter 4:2:2 pixel subsampling in Q3.
1136
 *
1137
 * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
1138
 * active area is specified using width and height.
1139
 *
1140
 * Note: We don't need to worry about going over the active area, as long as we
1141
 * stay inside the CfL prediction buffer.
1142
 *
1143
 */
1144
static void cfl_luma_subsampling_422_hbd_avx2(const uint16_t *input,
1145
                                              int input_stride,
1146
                                              uint16_t *pred_buf_q3, int width,
1147
0
                                              int height) {
1148
0
  __m256i *row = (__m256i *)pred_buf_q3;
1149
0
  const __m256i *row_end = row + height * CFL_BUF_LINE_I256;
1150
0
  do {
1151
0
    if (width == 32) {
1152
0
      __m256i top = _mm256_loadu_si256((__m256i *)input);
1153
0
      __m256i top_1 = _mm256_loadu_si256((__m256i *)(input + 16));
1154
0
      __m256i hsum = _mm256_hadd_epi16(top, top_1);
1155
0
      hsum = _mm256_permute4x64_epi64(hsum, _MM_SHUFFLE(3, 1, 2, 0));
1156
0
      hsum = _mm256_slli_epi16(hsum, 2);
1157
0
      _mm256_storeu_si256(row, hsum);
1158
0
    } else {  // width == 64
1159
0
      __m256i top = _mm256_loadu_si256((__m256i *)input);
1160
0
      __m256i top_1 = _mm256_loadu_si256((__m256i *)(input + 16));
1161
0
      __m256i hsum = _mm256_hadd_epi16(top, top_1);
1162
0
      hsum = _mm256_permute4x64_epi64(hsum, _MM_SHUFFLE(3, 1, 2, 0));
1163
0
      hsum = _mm256_slli_epi16(hsum, 2);
1164
0
      _mm256_storeu_si256(row, hsum);
1165
1166
0
      __m256i top_2 = _mm256_loadu_si256((__m256i *)(input + 32));
1167
0
      __m256i top_3 = _mm256_loadu_si256((__m256i *)(input + 48));
1168
0
      __m256i hsum_1 = _mm256_hadd_epi16(top_2, top_3);
1169
0
      hsum_1 = _mm256_permute4x64_epi64(hsum_1, _MM_SHUFFLE(3, 1, 2, 0));
1170
0
      hsum_1 = _mm256_slli_epi16(hsum_1, 2);
1171
0
      _mm256_storeu_si256(row + 1, hsum_1);
1172
0
    }
1173
0
    input += input_stride;
1174
0
  } while ((row += CFL_BUF_LINE_I256) < row_end);
1175
0
}
1176
1177
CFL_GET_SUBSAMPLE_FUNCTION_AVX2(422, hbd)
1178
1179
static void cfl_luma_subsampling_444_hbd_avx2(const uint16_t *input,
1180
                                              int input_stride,
1181
                                              uint16_t *pred_buf_q3, int width,
1182
29.2k
                                              int height) {
1183
29.2k
  __m256i *row = (__m256i *)pred_buf_q3;
1184
29.2k
  const __m256i *row_end = row + height * CFL_BUF_LINE_I256;
1185
681k
  do {
1186
681k
    if (width == 32) {
1187
351k
      __m256i top = _mm256_loadu_si256((__m256i *)input);
1188
351k
      __m256i top_1 = _mm256_loadu_si256((__m256i *)(input + 16));
1189
351k
      _mm256_storeu_si256(row, _mm256_slli_epi16(top, 3));
1190
351k
      _mm256_storeu_si256(row + 1, _mm256_slli_epi16(top_1, 3));
1191
351k
    } else {  // width == 64
1192
329k
      __m256i top = _mm256_loadu_si256((__m256i *)input);
1193
329k
      __m256i top_1 = _mm256_loadu_si256((__m256i *)(input + 16));
1194
329k
      _mm256_storeu_si256(row, _mm256_slli_epi16(top, 3));
1195
329k
      _mm256_storeu_si256(row + 1, _mm256_slli_epi16(top_1, 3));
1196
329k
      __m256i top_2 = _mm256_loadu_si256((__m256i *)(input + 32));
1197
329k
      __m256i top_3 = _mm256_loadu_si256((__m256i *)(input + 48));
1198
329k
      _mm256_storeu_si256(row + 2, _mm256_slli_epi16(top_2, 3));
1199
329k
      _mm256_storeu_si256(row + 3, _mm256_slli_epi16(top_3, 3));
1200
329k
    }
1201
681k
    input += input_stride;
1202
681k
  } while ((row += CFL_BUF_LINE_I256) < row_end);
1203
29.2k
}
1204
1205
CFL_GET_SUBSAMPLE_FUNCTION_AVX2(444, hbd)
1206
1207
CFL_GET_SUBSAMPLE_121_FUNCTION(avx2)
1208
1209
CFL_GET_SUBSAMPLE_COLOCATED_FUNCTION(avx2)
1210
1211
static INLINE __m256i predict_unclipped(const __m256i *input, __m256i alpha_q12,
1212
1.93M
                                        __m256i alpha_sign, __m256i dc_q0) {
1213
1.93M
  __m256i ac_q3 = _mm256_loadu_si256(input);
1214
1.93M
  __m256i ac_sign = _mm256_sign_epi16(alpha_sign, ac_q3);
1215
1.93M
  __m256i scaled_luma_q0 =
1216
1.93M
      _mm256_mulhrs_epi16(_mm256_abs_epi16(ac_q3), alpha_q12);
1217
1.93M
  scaled_luma_q0 = _mm256_sign_epi16(scaled_luma_q0, ac_sign);
1218
1.93M
  return _mm256_add_epi16(scaled_luma_q0, dc_q0);
1219
1.93M
}
1220
1221
78.3k
static __m256i highbd_max_epi16(int bd) {
1222
78.3k
  const __m256i neg_one = _mm256_set1_epi16(-1);
1223
  // (1 << bd) - 1 => -(-1 << bd) -1 => -1 - (-1 << bd) => -1 ^ (-1 << bd)
1224
78.3k
  return _mm256_xor_si256(_mm256_slli_epi16(neg_one, bd), neg_one);
1225
78.3k
}
1226
1227
1.93M
static __m256i highbd_clamp_epi16(__m256i u, __m256i zero, __m256i max) {
1228
1.93M
  return _mm256_max_epi16(_mm256_min_epi16(u, max), zero);
1229
1.93M
}
1230
1231
static INLINE void av2_cfl_predict_hbd_avx2(const int16_t *pred_buf_q3,
1232
                                            uint16_t *dst, int dst_stride,
1233
                                            int alpha_q3, int bd, int width,
1234
78.3k
                                            int height) {
1235
  // Use SSSE3 version for smaller widths
1236
78.3k
  assert(width == 16 || width == 32);
1237
78.3k
  const __m256i alpha_sign = _mm256_set1_epi16(alpha_q3);
1238
78.3k
  const __m256i alpha_q12 =
1239
78.3k
      _mm256_slli_epi16(_mm256_abs_epi16(alpha_sign), (9 - CFL_ADD_BITS_ALPHA));
1240
78.3k
  const __m256i dc_q0 = _mm256_loadu_si256((__m256i *)dst);
1241
78.3k
  const __m256i max = highbd_max_epi16(bd);
1242
1243
78.3k
  __m256i *row = (__m256i *)pred_buf_q3;
1244
78.3k
  const __m256i *row_end = row + height * CFL_BUF_LINE_I256;
1245
1.25M
  do {
1246
1.25M
    const __m256i res = predict_unclipped(row, alpha_q12, alpha_sign, dc_q0);
1247
1.25M
    _mm256_storeu_si256((__m256i *)dst,
1248
1.25M
                        highbd_clamp_epi16(res, _mm256_setzero_si256(), max));
1249
1.25M
    if (width == 32) {
1250
686k
      const __m256i res_1 =
1251
686k
          predict_unclipped(row + 1, alpha_q12, alpha_sign, dc_q0);
1252
686k
      _mm256_storeu_si256(
1253
686k
          (__m256i *)(dst + 16),
1254
686k
          highbd_clamp_epi16(res_1, _mm256_setzero_si256(), max));
1255
686k
    }
1256
1.25M
    dst += dst_stride;
1257
1.25M
  } while ((row += CFL_BUF_LINE_I256) < row_end);
1258
78.3k
}
1259
1260
CFL_PREDICT_X(avx2, 16, 4, hbd)
1261
CFL_PREDICT_X(avx2, 16, 8, hbd)
1262
CFL_PREDICT_X(avx2, 16, 16, hbd)
1263
CFL_PREDICT_X(avx2, 16, 32, hbd)
1264
CFL_PREDICT_X(avx2, 32, 8, hbd)
1265
CFL_PREDICT_X(avx2, 32, 16, hbd)
1266
CFL_PREDICT_X(avx2, 32, 32, hbd)
1267
CFL_PREDICT_X(avx2, 32, 4, hbd)
1268
1269
184k
av2_cfl_predict_hbd_fn av2_cfl_get_predict_hbd_fn_avx2(TX_SIZE tx_size) {
1270
184k
  static const av2_cfl_predict_hbd_fn pred[TX_SIZES_ALL] = {
1271
184k
    av2_cfl_predict_hbd_4x4_ssse3,  /* 4x4 */
1272
184k
    av2_cfl_predict_hbd_8x8_ssse3,  /* 8x8 */
1273
184k
    av2_cfl_predict_hbd_16x16_avx2, /* 16x16 */
1274
184k
    av2_cfl_predict_hbd_32x32_avx2, /* 32x32 */
1275
184k
    NULL,                           /* 64x64 (invalid CFL size) */
1276
184k
    av2_cfl_predict_hbd_4x8_ssse3,  /* 4x8 */
1277
184k
    av2_cfl_predict_hbd_8x4_ssse3,  /* 8x4 */
1278
184k
    av2_cfl_predict_hbd_8x16_ssse3, /* 8x16 */
1279
184k
    av2_cfl_predict_hbd_16x8_avx2,  /* 16x8 */
1280
184k
    av2_cfl_predict_hbd_16x32_avx2, /* 16x32 */
1281
184k
    av2_cfl_predict_hbd_32x16_avx2, /* 32x16 */
1282
184k
    NULL,                           /* 32x64 (invalid CFL size) */
1283
184k
    NULL,                           /* 64x32 (invalid CFL size) */
1284
184k
    av2_cfl_predict_hbd_4x16_ssse3, /* 4x16  */
1285
184k
    av2_cfl_predict_hbd_16x4_avx2,  /* 16x4  */
1286
184k
    av2_cfl_predict_hbd_8x32_ssse3, /* 8x32  */
1287
184k
    av2_cfl_predict_hbd_32x8_avx2,  /* 32x8  */
1288
184k
    NULL,                           /* 16x64 (invalid CFL size) */
1289
184k
    NULL,                           /* 64x16 (invalid CFL size) */
1290
184k
    av2_cfl_predict_hbd_4x32_ssse3, /* 4x32  */
1291
184k
    av2_cfl_predict_hbd_32x4_avx2,  /* 32x4  */
1292
184k
    NULL,                           /* 8x64 (invalid CFL size) */
1293
184k
    NULL,                           /* 64x8 (invalid CFL size) */
1294
184k
    NULL,                           /* 4x64 (invalid CFL size) */
1295
184k
    NULL,                           /* 64x4 (invalid CFL size) */
1296
184k
  };
1297
  // Modulo TX_SIZES_ALL to ensure that an attacker won't be able to index the
1298
  // function pointer array out of bounds.
1299
184k
  return pred[tx_size % TX_SIZES_ALL];
1300
184k
}
1301
1302
// Returns a vector where all the (32-bits) elements are the sum of all the
1303
// lanes in a.
1304
0
static INLINE __m256i fill_sum_epi32(__m256i a) {
1305
  // Given that a == [A, B, C, D, E, F, G, H]
1306
0
  a = _mm256_hadd_epi32(a, a);
1307
  // Given that A' == A + B, C' == C + D, E' == E + F, G' == G + H
1308
  // a == [A', C', A', C', E', G', E', G']
1309
0
  a = _mm256_permute4x64_epi64(a, _MM_SHUFFLE(3, 1, 2, 0));
1310
  // a == [A', C', E', G', A', C', E', G']
1311
0
  a = _mm256_hadd_epi32(a, a);
1312
  // Given that A'' == A' + C' and E'' == E' + G'
1313
  // a == [A'', E'', A'', E'', A'', E'', A'', E'']
1314
0
  return _mm256_hadd_epi32(a, a);
1315
  // Given that A''' == A'' + E''
1316
  // a == [A''', A''', A''', A''', A''', A''', A''', A''']
1317
0
}
1318
1319
0
static INLINE __m256i _mm256_addl_epi16(__m256i a) {
1320
0
  return _mm256_add_epi32(_mm256_unpacklo_epi16(a, _mm256_setzero_si256()),
1321
0
                          _mm256_unpackhi_epi16(a, _mm256_setzero_si256()));
1322
0
}
1323
1324
static INLINE void subtract_average_avx2(const uint16_t *src_ptr,
1325
                                         int16_t *dst_ptr, int width,
1326
                                         int height, int round_offset,
1327
0
                                         int num_pel_log2) {
1328
  // Use SSE2 version for smaller widths
1329
0
  assert(width == 16 || width == 32);
1330
1331
0
  const __m256i *src = (__m256i *)src_ptr;
1332
0
  const __m256i *const end = src + height * CFL_BUF_LINE_I256;
1333
  // To maximize usage of the AVX2 registers, we sum two rows per loop
1334
  // iteration
1335
0
  const int step = 2 * CFL_BUF_LINE_I256;
1336
1337
0
  __m256i sum = _mm256_setzero_si256();
1338
  // For width 32, we use a second sum accumulator to reduce accumulator
1339
  // dependencies in the loop.
1340
0
  __m256i sum2;
1341
0
  if (width == 32) sum2 = _mm256_setzero_si256();
1342
1343
0
  do {
1344
    // Add top row to the bottom row
1345
0
    __m256i l0 = _mm256_add_epi16(_mm256_loadu_si256(src),
1346
0
                                  _mm256_loadu_si256(src + CFL_BUF_LINE_I256));
1347
0
    sum = _mm256_add_epi32(sum, _mm256_addl_epi16(l0));
1348
0
    if (width == 32) { /* Don't worry, this if it gets optimized out. */
1349
      // Add the second part of the top row to the second part of the bottom row
1350
0
      __m256i l1 =
1351
0
          _mm256_add_epi16(_mm256_loadu_si256(src + 1),
1352
0
                           _mm256_loadu_si256(src + 1 + CFL_BUF_LINE_I256));
1353
0
      sum2 = _mm256_add_epi32(sum2, _mm256_addl_epi16(l1));
1354
0
    }
1355
0
    src += step;
1356
0
  } while (src < end);
1357
  // Combine both sum accumulators
1358
0
  if (width == 32) sum = _mm256_add_epi32(sum, sum2);
1359
1360
0
  __m256i fill = fill_sum_epi32(sum);
1361
1362
0
  __m256i avg_epi16 = _mm256_srli_epi32(
1363
0
      _mm256_add_epi32(fill, _mm256_set1_epi32(round_offset)), num_pel_log2);
1364
0
  avg_epi16 = _mm256_packs_epi32(avg_epi16, avg_epi16);
1365
1366
  // Store and subtract loop
1367
0
  src = (__m256i *)src_ptr;
1368
0
  __m256i *dst = (__m256i *)dst_ptr;
1369
0
  do {
1370
0
    _mm256_storeu_si256(dst,
1371
0
                        _mm256_sub_epi16(_mm256_loadu_si256(src), avg_epi16));
1372
0
    if (width == 32) {
1373
0
      _mm256_storeu_si256(
1374
0
          dst + 1, _mm256_sub_epi16(_mm256_loadu_si256(src + 1), avg_epi16));
1375
0
    }
1376
0
    src += CFL_BUF_LINE_I256;
1377
0
    dst += CFL_BUF_LINE_I256;
1378
0
  } while (src < end);
1379
0
}
1380
1381
// Declare wrappers for AVX2 sizes
1382
CFL_SUB_AVG_X(avx2, 16, 4, 32, 6)
1383
CFL_SUB_AVG_X(avx2, 16, 8, 64, 7)
1384
CFL_SUB_AVG_X(avx2, 16, 16, 128, 8)
1385
CFL_SUB_AVG_X(avx2, 16, 32, 256, 9)
1386
CFL_SUB_AVG_X(avx2, 32, 8, 128, 8)
1387
CFL_SUB_AVG_X(avx2, 32, 16, 256, 9)
1388
CFL_SUB_AVG_X(avx2, 32, 32, 512, 10)
1389
CFL_SUB_AVG_X(avx2, 32, 4, 64, 7)
1390
1391
// Based on the observation that for small blocks AVX2 does not outperform
1392
// SSE2, we call the SSE2 code for block widths 4 and 8.
1393
av2_cfl_subtract_average_fn av2_cfl_get_subtract_average_fn_avx2(
1394
0
    TX_SIZE tx_size) {
1395
0
  static const av2_cfl_subtract_average_fn sub_avg[TX_SIZES_ALL] = {
1396
0
    av2_cfl_subtract_average_4x4_sse2,   /* 4x4 */
1397
0
    av2_cfl_subtract_average_8x8_sse2,   /* 8x8 */
1398
0
    av2_cfl_subtract_average_16x16_avx2, /* 16x16 */
1399
0
    av2_cfl_subtract_average_32x32_avx2, /* 32x32 */
1400
0
    NULL,                                /* 64x64 (invalid CFL size) */
1401
0
    av2_cfl_subtract_average_4x8_sse2,   /* 4x8 */
1402
0
    av2_cfl_subtract_average_8x4_sse2,   /* 8x4 */
1403
0
    av2_cfl_subtract_average_8x16_sse2,  /* 8x16 */
1404
0
    av2_cfl_subtract_average_16x8_avx2,  /* 16x8 */
1405
0
    av2_cfl_subtract_average_16x32_avx2, /* 16x32 */
1406
0
    av2_cfl_subtract_average_32x16_avx2, /* 32x16 */
1407
0
    NULL,                                /* 32x64 (invalid CFL size) */
1408
0
    NULL,                                /* 64x32 (invalid CFL size) */
1409
0
    av2_cfl_subtract_average_4x16_sse2,  /* 4x16 */
1410
0
    av2_cfl_subtract_average_16x4_avx2,  /* 16x4 */
1411
0
    av2_cfl_subtract_average_8x32_sse2,  /* 8x32 */
1412
0
    av2_cfl_subtract_average_32x8_avx2,  /* 32x8 */
1413
0
    NULL,                                /* 16x64 (invalid CFL size) */
1414
0
    NULL,                                /* 64x16 (invalid CFL size) */
1415
0
    av2_cfl_subtract_average_4x32_sse2,  /* 4x32  */
1416
0
    av2_cfl_subtract_average_32x4_avx2,  /* 32x4  */
1417
0
    NULL,                                /* 8x64 (invalid CFL size) */
1418
0
    NULL,                                /* 64x8 (invalid CFL size) */
1419
0
    NULL,                                /* 4x64 (invalid CFL size) */
1420
0
    NULL,                                /* 64x4 (invalid CFL size) */
1421
0
  };
1422
  // Modulo TX_SIZES_ALL to ensure that an attacker won't be able to
1423
  // index the function pointer array out of bounds.
1424
0
  return sub_avg[tx_size % TX_SIZES_ALL];
1425
0
}
1426
1427
8.42M
static INLINE __m256i non_linear_avx2(__m256i v, __m256i mid, int bit_depth) {
1428
8.42M
  __m256i v2 = _mm256_mullo_epi32(v, v);
1429
8.42M
  v2 = _mm256_add_epi32(v2, mid);
1430
8.42M
  v2 = _mm256_srai_epi32(v2, bit_depth);
1431
8.42M
  return v2;
1432
8.42M
}
1433
1434
23.9M
static INLINE __m256i convert_int64_to_int32_avx2(__m256i a, __m256i b) {
1435
  // Extract low 32-bit  of each 64-bit element from a and b
1436
23.9M
  const __m256 low32 = _mm256_shuffle_ps(
1437
23.9M
      _mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(2, 0, 2, 0));
1438
1439
  // a0 a1 a2 a3 b0 b1 b2 b3
1440
23.9M
  const __m256d ordered =
1441
23.9M
      _mm256_permute4x64_pd(_mm256_castps_pd(low32), _MM_SHUFFLE(3, 1, 2, 0));
1442
1443
23.9M
  return _mm256_castpd_si256(ordered);
1444
23.9M
}
1445
1446
23.9M
static INLINE __m256i floor_log2_uint32_avx2(__m256i x) {
1447
23.9M
  const __m256i zero_mask = _mm256_cmpeq_epi32(x, _mm256_setzero_si256());
1448
1449
23.9M
  const __m128i x_lo = _mm256_castsi256_si128(x);
1450
23.9M
  const __m128i x_hi = _mm256_extracti128_si256(x, 1);
1451
1452
  // Convert int32 to double to access exponent bits
1453
23.9M
  const __m256d d_lo = _mm256_cvtepi32_pd(x_lo);
1454
23.9M
  const __m256d d_hi = _mm256_cvtepi32_pd(x_hi);
1455
1456
23.9M
  const __m256i bits_lo = _mm256_castpd_si256(d_lo);
1457
23.9M
  const __m256i bits_hi = _mm256_castpd_si256(d_hi);
1458
1459
  // Extract exponent bits [52..62]
1460
23.9M
  const __m256i exp_lo = _mm256_srli_epi64(bits_lo, 52);
1461
23.9M
  const __m256i exp_hi = _mm256_srli_epi64(bits_hi, 52);
1462
1463
23.9M
  __m256i exp = convert_int64_to_int32_avx2(exp_lo, exp_hi);
1464
  // Subtract exponent bias
1465
23.9M
  exp = _mm256_sub_epi32(exp, _mm256_set1_epi32(1023));
1466
  // Set result to 0 for input x = 0
1467
23.9M
  exp = _mm256_andnot_si256(zero_mask, exp);
1468
23.9M
  return exp;
1469
23.9M
}
1470
1471
static INLINE __m256i mul_fixed32_adapt_avx2(__m256i a, __m256i b,
1472
23.9M
                                             __m256i shift, __m256i bits_a) {
1473
23.9M
  const __m256i zero = _mm256_setzero_si256();
1474
23.9M
  const __m256i one = _mm256_set1_epi32(1);
1475
1476
  // Compute the bit-width of |b|
1477
23.9M
  __m256i bits_b = floor_log2_uint32_avx2(_mm256_abs_epi32(b));
1478
23.9M
  bits_b = _mm256_add_epi32(bits_b, one);
1479
1480
23.9M
  const int bits_limit = 29;
1481
  // Decide how many bits to drop in total to avoid mul overflow
1482
23.9M
  __m256i need = _mm256_sub_epi32(_mm256_add_epi32(bits_a, bits_b),
1483
23.9M
                                  _mm256_set1_epi32(bits_limit));
1484
23.9M
  need = _mm256_max_epi32(need, zero);
1485
1486
  // Split the drop across a and b to minimize error
1487
23.9M
  const __m256i s1 = _mm256_srai_epi32(need, 1);
1488
23.9M
  const __m256i s2 = _mm256_sub_epi32(need, s1);
1489
1490
  // Adjust dropped bits in final shift
1491
23.9M
  const __m256i adj = _mm256_sub_epi32(shift, need);
1492
1493
  // Safe 32-bit product
1494
23.9M
  __m256i prod =
1495
23.9M
      _mm256_mullo_epi32(_mm256_srav_epi32(a, s1), _mm256_srav_epi32(b, s2));
1496
1497
  // Compute bias and bias=0 when adj is 0
1498
23.9M
  const __m256i bias_mask = _mm256_cmpgt_epi32(adj, zero);
1499
23.9M
  const __m256i adj_minus_1 =
1500
23.9M
      _mm256_max_epi32(_mm256_sub_epi32(adj, one), zero);
1501
23.9M
  const __m256i bias =
1502
23.9M
      _mm256_and_si256(bias_mask, _mm256_sllv_epi32(one, adj_minus_1));
1503
1504
  // Final right shift with symmetric rounding to nearest
1505
23.9M
  const __m256i sign = _mm256_srai_epi32(prod, 31);
1506
23.9M
  prod = _mm256_add_epi32(_mm256_add_epi32(prod, bias), sign);
1507
23.9M
  return _mm256_srav_epi32(prod, adj);
1508
23.9M
}
1509
1510
#define CONVOLVE_FIXED32_AND_STORE                                            \
1511
8.16M
  {                                                                           \
1512
8.16M
    __m256i sum =                                                             \
1513
8.16M
        mul_fixed32_adapt_avx2(alpha[0], v, mhccp_decim_bits, bits_alpha[0]); \
1514
8.16M
    sum = _mm256_add_epi32(                                                   \
1515
8.16M
        sum, mul_fixed32_adapt_avx2(alpha[1], v2, mhccp_decim_bits,           \
1516
8.16M
                                    bits_alpha[1]));                          \
1517
8.16M
    sum = _mm256_add_epi32(                                                   \
1518
8.16M
        sum, mul_fixed32_adapt_avx2(alpha[2], mid, mhccp_decim_bits,          \
1519
8.16M
                                    bits_alpha[2]));                          \
1520
8.16M
    sum = _mm256_max_epi32(sum, zero);                                        \
1521
8.16M
    sum = _mm256_min_epi32(sum, pixel_max);                                   \
1522
8.16M
                                                                              \
1523
8.16M
    const __m128i res_lo = _mm256_castsi256_si128(sum);                       \
1524
8.16M
    const __m128i res_hi = _mm256_extracti128_si256(sum, 1);                  \
1525
8.16M
    const __m128i res = _mm_packs_epi32(res_lo, res_hi);                      \
1526
8.16M
                                                                              \
1527
8.16M
    if (width > 4)                                                            \
1528
8.16M
      _mm_storeu_si128((__m128i *)(dst + i), res);                            \
1529
8.16M
    else                                                                      \
1530
8.16M
      _mm_storeu_si64((__m128i *)(dst + i), res);                             \
1531
8.16M
  }
1532
1533
12.1M
static INLINE __m256i load_and_shift(const uint16_t *ptr) {
1534
12.1M
  __m256i v = _mm256_cvtepu16_epi32(_mm_loadu_si128((__m128i const *)ptr));
1535
12.1M
  v = _mm256_srli_epi32(v, 3);
1536
12.1M
  return v;
1537
12.1M
}
1538
1539
void mhccp_predict_hv_hbd_avx2(const uint16_t *input, uint16_t *dst,
1540
                               bool have_top, bool have_left, int dst_stride,
1541
                               int32_t *alpha_q3, int bit_depth, int width,
1542
219k
                               int height, int dir) {
1543
219k
  const __m256i mid = _mm256_set1_epi32(1 << (bit_depth - 1));
1544
219k
  const __m256i pixel_max = _mm256_set1_epi32((1 << bit_depth) - 1);
1545
219k
  const __m256i mhccp_decim_bits = _mm256_set1_epi32(MHCCP_DECIM_BITS);
1546
219k
  const __m256i zero = _mm256_setzero_si256();
1547
219k
  assert(MHCCP_NUM_PARAMS == 3);
1548
1549
219k
  __m256i alpha[MHCCP_NUM_PARAMS];
1550
219k
  __m256i bits_alpha[MHCCP_NUM_PARAMS];
1551
1552
877k
  for (int i = 0; i < MHCCP_NUM_PARAMS; i++) {
1553
657k
    alpha[i] = _mm256_set1_epi32(alpha_q3[i]);
1554
1555
657k
    const uint32_t abs_a =
1556
657k
        (uint32_t)(alpha_q3[i] < 0 ? -alpha_q3[i] : alpha_q3[i]);
1557
657k
    const int bits_a = ilog2_32(abs_a) + 1;
1558
657k
    bits_alpha[i] = _mm256_set1_epi32(bits_a);
1559
657k
  }
1560
1561
219k
  if (dir == 0) {
1562
1.43M
    for (int j = 0; j < height; j++) {
1563
4.98M
      for (int i = 0; i < width; i += 8) {
1564
3.63M
        const __m256i v = load_and_shift(input + i);
1565
1566
3.63M
        const __m256i v2 = non_linear_avx2(v, mid, bit_depth);
1567
1568
3.63M
        CONVOLVE_FIXED32_AND_STORE
1569
3.63M
      }
1570
1.35M
      input += CFL_BUF_LINE * 2;
1571
1.35M
      dst += dst_stride;
1572
1.35M
    }
1573
140k
  } else if (dir == 1) {
1574
44.5k
    const uint16_t *top_row = !have_top ? input : &input[-2 * CFL_BUF_LINE];
1575
44.5k
    const uint16_t *cur_row = input;
1576
1577
692k
    for (int j = 0; j < height; j++) {
1578
2.08M
      for (int i = 0; i < width; i += 8) {
1579
1.43M
        const __m256i v = load_and_shift(top_row + i);
1580
1581
1.43M
        const __m256i v2 =
1582
1.43M
            non_linear_avx2(load_and_shift(cur_row + i), mid, bit_depth);
1583
1584
1.43M
        CONVOLVE_FIXED32_AND_STORE
1585
1.43M
      }
1586
648k
      top_row = cur_row;
1587
648k
      cur_row += CFL_BUF_LINE * 2;
1588
648k
      dst += dst_stride;
1589
648k
    }
1590
96.1k
  } else {
1591
96.1k
    assert(dir == 2);
1592
1.51M
    for (int j = 0; j < height; j++) {
1593
4.52M
      for (int i = 0; i < width; i += 8) {
1594
3.10M
        const __m256i cur = load_and_shift(input + i);
1595
3.10M
        __m256i v;
1596
3.10M
        if (i == 0 && !have_left) {
1597
317k
          const __m256i idx = _mm256_setr_epi32(0, 0, 1, 2, 3, 4, 5, 6);
1598
317k
          v = _mm256_permutevar8x32_epi32(cur, idx);
1599
2.78M
        } else {
1600
2.78M
          v = load_and_shift(input + i - 1);
1601
2.78M
        }
1602
1603
3.10M
        const __m256i v2 = non_linear_avx2(cur, mid, bit_depth);
1604
1605
3.10M
        CONVOLVE_FIXED32_AND_STORE
1606
3.10M
      }
1607
1.42M
      input += CFL_BUF_LINE * 2;
1608
1.42M
      dst += dst_stride;
1609
1.42M
    }
1610
96.1k
  }
1611
219k
}
1612
1613
// Horizontal sum of 4x 64-bit integers
1614
3.90M
static INLINE int32_t hsum_epi64_to_i32_avx2(__m256i v) {
1615
3.90M
  __m128i lo128 = _mm256_castsi256_si128(v);
1616
3.90M
  __m128i hi128 = _mm256_extracti128_si256(v, 1);
1617
3.90M
  __m128i sum128 = _mm_add_epi64(lo128, hi128);
1618
3.90M
  __m128i shuf = _mm_shuffle_epi32(sum128, _MM_SHUFFLE(1, 0, 3, 2));
1619
3.90M
  sum128 = _mm_add_epi64(sum128, shuf);
1620
3.90M
  return _mm_cvtsi128_si32(sum128);
1621
3.90M
}
1622
1623
4.00M
#define NON_LINEAR(V, M, BD) ((V * V + M) >> BD)
1624
// Derives multi-parameter chroma prediction coefficients from neighboring luma
1625
// and chroma reference samples.
1626
void av2_mhccp_derive_multi_param_hv_avx2(MACROBLOCKD *const xd, int plane,
1627
                                          int above_lines, int left_lines,
1628
                                          int ref_width, int ref_height,
1629
219k
                                          int dir, int is_top_sb_boundary) {
1630
219k
  const CFL_CTX *const cfl = &xd->cfl;
1631
219k
  MB_MODE_INFO *mbmi = xd->mi[0];
1632
1633
219k
  int count = 0;
1634
219k
  int16_t A[MHCCP_NUM_PARAMS][MHCCP_MAX_REF_SAMPLES];
1635
219k
  uint16_t YCb[MHCCP_MAX_REF_SAMPLES];
1636
1637
219k
  if (above_lines || left_lines) {
1638
216k
    const int16_t mid = 1 << (xd->bd - 1);
1639
216k
    const uint16_t *l = cfl->mhccp_ref_buf_q3[AVM_PLANE_Y];
1640
216k
    const uint16_t *c = cfl->mhccp_ref_buf_q3[plane];
1641
216k
    const int ref_stride = CFL_BUF_LINE * 2;
1642
216k
    assert(dir >= 0 && dir <= 2);
1643
216k
    const int dir_offset = (dir == 0) ? 0 : (dir == 1 ? -ref_stride : -1);
1644
1645
216k
    const __m256i mid32 = _mm256_set1_epi32(mid);
1646
216k
    const __m256i mid16 = _mm256_set1_epi16(mid);
1647
1648
4.19M
    for (int j = 1; j < ref_height - 1; ++j) {
1649
3.97M
      int ref_h_offset = 0;
1650
3.97M
      if (is_top_sb_boundary && above_lines == (LINE_NUM + 1) &&
1651
1.67M
          j < above_lines)
1652
63.8k
        ref_h_offset = above_lines - 1 - j;
1653
1654
3.97M
      const int base = (j + ref_h_offset) * ref_stride;
1655
3.97M
      int lines = ref_width - 1;
1656
3.97M
      if (j >= above_lines) lines = AVMMIN(lines, left_lines);
1657
1658
3.97M
      const uint16_t *ptrl = l + base + 1;                   // for NON_LINEAR
1659
3.97M
      const uint16_t *ptrl_dir = l + base + 1 + dir_offset;  // for A0
1660
3.97M
      const uint16_t *ptrc = c + base + 1;
1661
1662
3.97M
      int i = 1;
1663
7.22M
      for (; i < lines;) {
1664
3.24M
        const int remaining = lines - i;
1665
3.24M
        if (remaining >= 16) {
1666
217k
          const __m256i l_vec =
1667
217k
              _mm256_loadu_si256((const __m256i *)(ptrl_dir + i - 1));
1668
217k
          const __m256i l_ref_vec =
1669
217k
              _mm256_loadu_si256((const __m256i *)(ptrl + i - 1));
1670
217k
          const __m256i c_vec =
1671
217k
              _mm256_loadu_si256((const __m256i *)(ptrc + i - 1));
1672
1673
217k
          const __m256i a0 = _mm256_srli_epi16(l_vec, 3);
1674
217k
          _mm256_storeu_si256((__m256i *)(A[0] + count), a0);
1675
217k
          _mm256_storeu_si256((__m256i *)(YCb + count), c_vec);
1676
1677
217k
          const __m256i v16 = _mm256_srli_epi16(l_ref_vec, 3);
1678
1679
217k
          const __m128i v_lo = _mm256_castsi256_si128(v16);
1680
217k
          const __m128i v_hi = _mm256_extracti128_si256(v16, 1);
1681
1682
217k
          const __m256i v_lo32 = _mm256_cvtepi16_epi32(v_lo);
1683
217k
          const __m256i v_hi32 = _mm256_cvtepi16_epi32(v_hi);
1684
1685
217k
          const __m256i v_lo_sq = non_linear_avx2(v_lo32, mid32, xd->bd);
1686
217k
          const __m256i v_hi_sq = non_linear_avx2(v_hi32, mid32, xd->bd);
1687
1688
217k
          const __m256i mask = _mm256_set1_epi32(0xFFFF);
1689
217k
          const __m256i v_lo_masked = _mm256_and_si256(v_lo_sq, mask);
1690
217k
          const __m256i v_hi_masked = _mm256_and_si256(v_hi_sq, mask);
1691
217k
          const __m128i v_lo_lo128 = _mm256_castsi256_si128(v_lo_masked);
1692
217k
          const __m128i v_lo_hi128 = _mm256_extracti128_si256(v_lo_masked, 1);
1693
217k
          const __m128i packed_lo = _mm_packus_epi32(v_lo_lo128, v_lo_hi128);
1694
1695
217k
          const __m128i v_hi_lo128 = _mm256_castsi256_si128(v_hi_masked);
1696
217k
          const __m128i v_hi_hi128 = _mm256_extracti128_si256(v_hi_masked, 1);
1697
217k
          const __m128i packed_hi = _mm_packus_epi32(v_hi_lo128, v_hi_hi128);
1698
217k
          const __m256i result = _mm256_set_m128i(packed_hi, packed_lo);
1699
1700
217k
          _mm256_storeu_si256((__m256i *)&A[1][count], result);
1701
217k
          _mm256_storeu_si256((__m256i *)(A[2] + count), mid16);
1702
1703
217k
          count += 16;
1704
217k
          i += 16;
1705
3.02M
        } else {
1706
7.03M
          for (; i < lines; ++i) {
1707
4.00M
            A[0][count] = ptrl_dir[i - 1] >> 3;
1708
4.00M
            A[1][count] = NON_LINEAR((ptrl[i - 1] >> 3), mid, xd->bd);
1709
4.00M
            A[2][count] = mid;
1710
4.00M
            YCb[count] = ptrc[i - 1];
1711
4.00M
            ++count;
1712
4.00M
          }
1713
3.02M
        }
1714
3.24M
      }
1715
3.97M
    }
1716
216k
  }
1717
1718
219k
  if (count > 0) {
1719
216k
    int32_t ATA[MHCCP_NUM_PARAMS][MHCCP_NUM_PARAMS];
1720
216k
    int32_t Ty[MHCCP_NUM_PARAMS];
1721
    // One more column is added to store the derived parameters
1722
216k
    int32_t C[MHCCP_NUM_PARAMS][MHCCP_NUM_PARAMS + 1];
1723
1724
216k
    __m256i sum00_lo = _mm256_setzero_si256(), sum00_hi = sum00_lo;
1725
216k
    __m256i sum01_lo = sum00_lo, sum01_hi = sum00_lo;
1726
216k
    __m256i sum02_lo = sum00_lo, sum02_hi = sum00_lo;
1727
216k
    __m256i sum11_lo = sum00_lo, sum11_hi = sum00_lo;
1728
216k
    __m256i sum12_lo = sum00_lo, sum12_hi = sum00_lo;
1729
216k
    __m256i sum22_lo = sum00_lo, sum22_hi = sum00_lo;
1730
1731
216k
    __m256i ty0_lo = sum00_lo, ty0_hi = sum00_lo;
1732
216k
    __m256i ty1_lo = sum00_lo, ty1_hi = sum00_lo;
1733
216k
    __m256i ty2_lo = sum00_lo, ty2_hi = sum00_lo;
1734
1735
216k
    int k = 0;
1736
529k
    for (; k + 15 < count; k += 16) {
1737
313k
      const __m256i v0_256 = _mm256_loadu_si256((const __m256i *)&A[0][k]);
1738
313k
      const __m256i v1_256 = _mm256_loadu_si256((const __m256i *)&A[1][k]);
1739
313k
      const __m256i v2_256 = _mm256_loadu_si256((const __m256i *)&A[2][k]);
1740
313k
      const __m256i y_256 = _mm256_loadu_si256((const __m256i *)&YCb[k]);
1741
1742
      // Unpack low/high halves to 32-bit
1743
313k
      const __m256i v0_lo =
1744
313k
          _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v0_256));
1745
313k
      const __m256i v0_hi =
1746
313k
          _mm256_cvtepi16_epi32(_mm256_extracti128_si256(v0_256, 1));
1747
313k
      const __m256i v1_lo =
1748
313k
          _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v1_256));
1749
313k
      const __m256i v1_hi =
1750
313k
          _mm256_cvtepi16_epi32(_mm256_extracti128_si256(v1_256, 1));
1751
313k
      const __m256i v2_lo =
1752
313k
          _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v2_256));
1753
313k
      const __m256i v2_hi =
1754
313k
          _mm256_cvtepi16_epi32(_mm256_extracti128_si256(v2_256, 1));
1755
1756
313k
      const __m256i y_lo = _mm256_cvtepu16_epi32(_mm256_castsi256_si128(y_256));
1757
313k
      const __m256i y_hi =
1758
313k
          _mm256_cvtepu16_epi32(_mm256_extracti128_si256(y_256, 1));
1759
1760
313k
      ACCUMULATE_SUMS_64(v0_lo, v1_lo, v2_lo, y_lo)
1761
313k
      ACCUMULATE_SUMS_64(v0_hi, v1_hi, v2_hi, y_hi)
1762
313k
    }
1763
1764
216k
    ATA[0][0] =
1765
216k
        hsum_epi64_to_i32_avx2(sum00_lo) + hsum_epi64_to_i32_avx2(sum00_hi);
1766
216k
    ATA[0][1] =
1767
216k
        hsum_epi64_to_i32_avx2(sum01_lo) + hsum_epi64_to_i32_avx2(sum01_hi);
1768
216k
    ATA[0][2] =
1769
216k
        hsum_epi64_to_i32_avx2(sum02_lo) + hsum_epi64_to_i32_avx2(sum02_hi);
1770
216k
    ATA[1][1] =
1771
216k
        hsum_epi64_to_i32_avx2(sum11_lo) + hsum_epi64_to_i32_avx2(sum11_hi);
1772
216k
    ATA[1][2] =
1773
216k
        hsum_epi64_to_i32_avx2(sum12_lo) + hsum_epi64_to_i32_avx2(sum12_hi);
1774
216k
    ATA[2][2] =
1775
216k
        hsum_epi64_to_i32_avx2(sum22_lo) + hsum_epi64_to_i32_avx2(sum22_hi);
1776
216k
    ATA[1][0] = 0;
1777
216k
    ATA[2][0] = 0;
1778
216k
    ATA[2][1] = 0;
1779
1780
216k
    Ty[0] = hsum_epi64_to_i32_avx2(ty0_lo) + hsum_epi64_to_i32_avx2(ty0_hi);
1781
216k
    Ty[1] = hsum_epi64_to_i32_avx2(ty1_lo) + hsum_epi64_to_i32_avx2(ty1_hi);
1782
216k
    Ty[2] = hsum_epi64_to_i32_avx2(ty2_lo) + hsum_epi64_to_i32_avx2(ty2_hi);
1783
1784
2.77M
    for (; k < count; ++k) {
1785
2.55M
      const int16_t a0 = A[0][k];
1786
2.55M
      const int16_t a1 = A[1][k];
1787
2.55M
      const int16_t a2 = A[2][k];
1788
1789
2.55M
      ATA[0][0] += a0 * a0;
1790
2.55M
      ATA[0][1] += a0 * a1;
1791
2.55M
      ATA[0][2] += a0 * a2;
1792
2.55M
      ATA[1][1] += a1 * a1;
1793
2.55M
      ATA[1][2] += a1 * a2;
1794
2.55M
      ATA[2][2] += a2 * a2;
1795
1796
2.55M
      Ty[0] += a0 * YCb[k];
1797
2.55M
      Ty[1] += a1 * YCb[k];
1798
2.55M
      Ty[2] += a2 * YCb[k];
1799
2.55M
    }
1800
1801
    // Scale the matrix and vector to selected dynamic range
1802
216k
    const int matrix_shift =
1803
216k
        (MHCCP_DECIM_BITS + 6) - 2 * xd->bd - avm_ceil_log2(count);
1804
1805
216k
    if (matrix_shift > 0) {
1806
20.5k
      for (int i = 0; i < MHCCP_NUM_PARAMS; ++i) {
1807
15.4k
        Ty[i] <<= matrix_shift;
1808
61.7k
        for (int j = 0; j < MHCCP_NUM_PARAMS; ++j) ATA[i][j] <<= matrix_shift;
1809
15.4k
      }
1810
210k
    } else if (matrix_shift < 0) {
1811
840k
      for (int i = 0; i < MHCCP_NUM_PARAMS; ++i) {
1812
630k
        Ty[i] >>= -matrix_shift;
1813
2.52M
        for (int j = 0; j < MHCCP_NUM_PARAMS; ++j) ATA[i][j] >>= -matrix_shift;
1814
630k
      }
1815
210k
    }
1816
1817
216k
    gauss_elimination_mhccp(ATA, C, Ty, mbmi->mhccp_implicit_param[plane - 1],
1818
216k
                            MHCCP_NUM_PARAMS, xd->bd);
1819
216k
  } else {
1820
9.32k
    for (int i = 0; i < MHCCP_NUM_PARAMS - 1; ++i)
1821
5.63k
      mbmi->mhccp_implicit_param[plane - 1][i] = 0;
1822
3.69k
    mbmi->mhccp_implicit_param[plane - 1][MHCCP_NUM_PARAMS - 1] =
1823
3.69k
        1 << MHCCP_DECIM_BITS;
1824
3.69k
  }
1825
219k
}
1826
1827
// AVX2 implementation for av2_mhccp_implicit_fetch_neighbor_chroma_c
1828
void av2_mhccp_implicit_fetch_neighbor_chroma_avx2(
1829
    const uint16_t *input, int input_stride, TX_SIZE tx_size, int above_lines,
1830
    int left_lines, int is_top_sb_boundary, int ref_width, int ref_height,
1831
219k
    uint16_t *output_q3) {
1832
219k
  (void)is_top_sb_boundary;
1833
219k
  const int width = tx_size_wide[tx_size];
1834
219k
  const int height = tx_size_high[tx_size];
1835
1836
219k
  int output_stride = CFL_BUF_LINE * 2;
1837
219k
  if (above_lines || left_lines) {
1838
4.69M
    for (int h = 0; h < ref_height; ++h) {
1839
4.47M
      assert(LINE_NUM == 1);
1840
4.47M
      int ref_h_offset = (above_lines == (LINE_NUM + 1) && h == 0) ? 1 : 0;
1841
4.47M
      int h_offset_stride = ref_h_offset * input_stride;
1842
4.47M
      int w = 0;
1843
14.4M
      for (; w < ref_width; w += 16) {
1844
10.0M
        if ((h >= above_lines && w >= left_lines + width) ||
1845
8.22M
            (h >= above_lines + height && w >= left_lines))
1846
2.49M
          continue;
1847
7.51M
        __m256i l_vec =
1848
7.51M
            _mm256_loadu_si256((const __m256i *)(input + h_offset_stride + w));
1849
7.51M
        if (left_lines == (LINE_NUM + 1) && (w == 0)) {
1850
3.46M
          __m128i lo = _mm256_castsi256_si128(l_vec);
1851
3.46M
          lo = _mm_shufflelo_epi16(lo, _MM_SHUFFLE(3, 2, 1, 1));
1852
3.46M
          l_vec = _mm256_inserti128_si256(l_vec, lo, 0);
1853
3.46M
        }
1854
7.51M
        _mm256_storeu_si256((__m256i *)(output_q3 + w), l_vec);
1855
7.51M
      }
1856
4.47M
      output_q3 += output_stride;
1857
4.47M
      input += input_stride;
1858
4.47M
    }
1859
216k
  }
1860
219k
}
1861
1862
DECLARE_ALIGNED(32, static const uint8_t,
1863
                shuffle_index[32]) = { 0,  1,  4, 5,  8,  9, 12, 13, 0,  1, 4,
1864
                                       5,  8,  9, 12, 13, 0, 1,  4,  5,  8, 9,
1865
                                       12, 13, 0, 1,  4,  5, 8,  9,  12, 13 };
1866
DECLARE_ALIGNED(32, static const uint16_t,
1867
                blend_index[16]) = { 0xFFFF, 0, 0, 0, 0, 0, 0, 0,
1868
                                     0xFFFF, 0, 0, 0, 0, 0, 0, 0 };
1869
1870
// AVX2 implementation for av2_mhccp_implicit_fetch_neighbor_luma_420_c
1871
void av2_mhccp_implicit_fetch_neighbor_luma_420_avx2(
1872
    const uint16_t *input, int input_stride, int above_lines, int left_lines,
1873
    int is_top_sb_boundary, int ref_width, int ref_height, int sub_y,
1874
    uint8_t cfl_ds_filter_index, int width, int height, uint16_t *output_q3,
1875
173k
    int output_stride) {
1876
3.75M
  for (int h = 0; h < ref_height; h += 2) {
1877
3.58M
    int ref_h_t_off = 0;
1878
3.58M
    int ref_h_c_off = 0;
1879
3.58M
    int ref_h_b_off = 0;
1880
3.58M
    int ref_w_off = 0;
1881
3.58M
    if (above_lines == ((LINE_NUM + 1) << sub_y)) {
1882
3.47M
      if (is_top_sb_boundary && (h < above_lines)) {
1883
        // For the top boundary of the superblock, we need to offset the
1884
        // reference region
1885
117k
        ref_h_t_off = h != 0 ? ((LINE_NUM + 1) << sub_y) - h
1886
117k
                             : ((LINE_NUM + 1) << sub_y) - (h + 1);
1887
117k
        ref_h_c_off = ((LINE_NUM + 1) << sub_y) - (h + 1);
1888
117k
        ref_h_b_off = ((LINE_NUM + 1) << sub_y) - (h + 2);
1889
117k
      }
1890
3.47M
    }
1891
3.58M
    int h_c_offset_stride = ref_h_c_off * input_stride;
1892
3.58M
    int h_b_offset_stride = ref_h_b_off * input_stride + input_stride;
1893
11.0M
    for (int w = 0; w < ref_width; w += 32) {
1894
7.50M
      if ((h >= above_lines && w >= left_lines + width) ||
1895
6.32M
          (h >= above_lines + height && w >= left_lines))
1896
1.64M
        continue;
1897
1898
      // h_c0, h_c1, ... h_c15
1899
5.86M
      __m256i h_c0 =
1900
5.86M
          _mm256_loadu_si256((const __m256i *)(input + h_c_offset_stride + w));
1901
1902
      // h_c16, h_c17, ... h_c31
1903
5.86M
      __m256i h_c16 = _mm256_loadu_si256(
1904
5.86M
          (const __m256i *)(input + h_c_offset_stride + w + 16));
1905
1906
      // h_b0, h_b1, ..... h_b15
1907
5.86M
      __m256i h_b0 =
1908
5.86M
          _mm256_loadu_si256((const __m256i *)(input + h_b_offset_stride + w));
1909
1910
      // h_b16, h_b17, ..... h_b31
1911
5.86M
      __m256i h_b16 = _mm256_loadu_si256(
1912
5.86M
          (const __m256i *)(input + h_b_offset_stride + w + 16));
1913
1914
5.86M
      if (cfl_ds_filter_index == 1 || cfl_ds_filter_index == 2) {
1915
931k
        __m256i blend_mask = _mm256_load_si256((__m256i *)blend_index);
1916
931k
        __m256i h_cm1;
1917
931k
        if (w > 0) {
1918
392k
          h_cm1 = _mm256_loadu_si256(
1919
392k
              (const __m256i *)(input + h_c_offset_stride + ref_w_off + w - 1));
1920
538k
        } else {
1921
          // h_c7, h_c0, h_c1, h_c2, h_c3, h_c4, h_c5, h_c6, h_c15, h_c8, h_c9,
1922
          // h_c10, h_c11, h_c12, h_c13, h_c14
1923
538k
          __m256i w_reorder1 = _mm256_alignr_epi8(h_c0, h_c0, 14);
1924
          // h_c0, h_c1, h_c2, h_c3, h_c4, h_c5, h_c6, h_c7, h_c7, h_c0, h_c1,
1925
          // h_c2, h_c3, h_c4, h_c5, h_c6
1926
538k
          __m256i w_reorder2 =
1927
538k
              _mm256_permute2x128_si256(h_c0, w_reorder1, 0x20);
1928
          // h_c0, h_c0, h_c1, h_c2, h_c3, h_c4, h_c5, h_c6, h_c7, h_c8, h_c9,
1929
          // h_c10, h_c11, h_c12, h_c13, h_c14
1930
538k
          h_cm1 = _mm256_blendv_epi8(w_reorder1, w_reorder2, blend_mask);
1931
538k
        }
1932
1933
        // h_c15, h_c16, ... h_c30
1934
931k
        __m256i h_c15 = _mm256_loadu_si256(
1935
931k
            (const __m256i *)(input + h_c_offset_stride + w + 15));
1936
1937
931k
        if (cfl_ds_filter_index == 1) {
1938
742k
          __m256i h_bm1;
1939
742k
          if (w > 0) {
1940
318k
            h_bm1 =
1941
318k
                _mm256_loadu_si256((const __m256i *)(input + h_b_offset_stride +
1942
318k
                                                     ref_w_off + w - 1));
1943
423k
          } else {
1944
            // h_b7, h_b0, h_b1, h_b2, h_b3, h_b4, h_b5, h_b6, h_b15, h_b8,
1945
            // h_b9, h_b10, h_b11, h_b12, h_b13, h_b14
1946
423k
            __m256i b_reorder1 = _mm256_alignr_epi8(h_b0, h_b0, 14);
1947
            // h_b0, h_b1, h_b2, h_b3, h_b4, h_b5, h_b6, h_b7, h_b7, h_b0, h_b1,
1948
            // h_b2, h_b3, h_b4, h_b5, h_b6
1949
423k
            __m256i b_reorder2 =
1950
423k
                _mm256_permute2x128_si256(h_b0, b_reorder1, 0x20);
1951
1952
            // h_b0, h_b0, h_b1, h_b2, h_b3, h_b4, h_b5, h_b6, h_b7, h_b8, h_b9,
1953
            // h_b10, h_b11, h_b12, h_b13, h_b14
1954
423k
            h_bm1 = _mm256_blendv_epi8(b_reorder1, b_reorder2, blend_mask);
1955
423k
          }
1956
1957
          // h_b15, h_b16, ..... h_b30
1958
742k
          __m256i h_b15 = _mm256_loadu_si256(
1959
742k
              (const __m256i *)(input + h_b_offset_stride + w + 15));
1960
1961
742k
          __m256i h_cm1_c0 = _mm256_add_epi16(h_cm1, h_c0);
1962
742k
          __m256i h_bm1_b0 = _mm256_add_epi16(h_bm1, h_b0);
1963
742k
          __m256i h_c15_c16 = _mm256_add_epi16(h_c15, h_c16);
1964
742k
          __m256i h_b15_b16 = _mm256_add_epi16(h_b15, h_b16);
1965
1966
742k
          __m256i h_cm1_c0_bm1_b0 = _mm256_add_epi16(h_cm1_c0, h_bm1_b0);
1967
742k
          __m256i h_c15_c16_b15_b16 = _mm256_add_epi16(h_c15_c16, h_b15_b16);
1968
1969
742k
          __m256i res = _mm256_hadd_epi16(h_cm1_c0_bm1_b0, h_c15_c16_b15_b16);
1970
1971
742k
          res = _mm256_permute4x64_epi64(res, 0xD8);
1972
1973
742k
          _mm256_storeu_si256((__m256i *)(output_q3 + (w >> 1)), res);
1974
742k
        } else if (cfl_ds_filter_index == 2) {
1975
189k
          const int top = h != 0 ? (-input_stride) : 0;
1976
189k
          int h_t_offset_stride = top + ref_h_t_off * input_stride;
1977
1978
189k
          __m256i h_t0 = _mm256_loadu_si256(
1979
189k
              (const __m256i *)(input + h_t_offset_stride + w));
1980
189k
          __m256i h_t16 = _mm256_loadu_si256(
1981
189k
              (const __m256i *)(input + h_t_offset_stride + w + 16));
1982
1983
189k
          const __m256i shuffle_mask =
1984
189k
              _mm256_load_si256((__m256i *)shuffle_index);
1985
          // h_c0, h_c2, h_c4, ....
1986
189k
          __m256i h_c0_even = _mm256_shuffle_epi8(h_c0, shuffle_mask);
1987
          // h_b0, h_b2, h_b4, ....
1988
189k
          __m256i h_b0_even = _mm256_shuffle_epi8(h_b0, shuffle_mask);
1989
189k
          __m256i b0_c0_even = _mm256_unpacklo_epi16(h_b0_even, h_c0_even);
1990
          // h_t0, h_t2, h_t4, ....
1991
189k
          __m256i h_t0_even = _mm256_shuffle_epi8(h_t0, shuffle_mask);
1992
189k
          __m256i t0_c0_even = _mm256_unpacklo_epi16(h_t0_even, h_c0_even);
1993
1994
          // h_c16, h_c18, h_c20, ....
1995
189k
          __m256i h_c16_even = _mm256_shuffle_epi8(h_c16, shuffle_mask);
1996
          // h_b16, h_b18, h_b20, ....
1997
189k
          __m256i h_b16_even = _mm256_shuffle_epi8(h_b16, shuffle_mask);
1998
189k
          __m256i b16_c16_even = _mm256_unpacklo_epi16(h_b16_even, h_c16_even);
1999
          // h_t16, h_t18, h_t20, ....
2000
189k
          __m256i h_t16_even = _mm256_shuffle_epi8(h_t16, shuffle_mask);
2001
189k
          __m256i t16_c16_even = _mm256_unpacklo_epi16(h_t16_even, h_c16_even);
2002
2003
189k
          __m256i h_cm1_c0 = _mm256_add_epi16(h_cm1, h_c0);
2004
189k
          __m256i bc0_tc0_even = _mm256_add_epi16(b0_c0_even, t0_c0_even);
2005
189k
          __m256i h_c15_c16 = _mm256_add_epi16(h_c15, h_c16);
2006
189k
          __m256i bc16_tc16_even = _mm256_add_epi16(b16_c16_even, t16_c16_even);
2007
2008
189k
          __m256i h_cm1_c0_bc0_tc0_even =
2009
189k
              _mm256_add_epi16(h_cm1_c0, bc0_tc0_even);
2010
189k
          __m256i h_c15_c16_bc16_tc16_even =
2011
189k
              _mm256_add_epi16(h_c15_c16, bc16_tc16_even);
2012
2013
189k
          __m256i res = _mm256_hadd_epi16(h_cm1_c0_bc0_tc0_even,
2014
189k
                                          h_c15_c16_bc16_tc16_even);
2015
2016
189k
          res = _mm256_permute4x64_epi64(res, 0xD8);
2017
189k
          _mm256_storeu_si256((__m256i *)(output_q3 + (w >> 1)), res);
2018
189k
        }
2019
4.93M
      } else {
2020
4.93M
        __m256i h_c0_b0 = _mm256_add_epi16(h_c0, h_b0);
2021
4.93M
        __m256i h_c16_b16 = _mm256_add_epi16(h_c16, h_b16);
2022
2023
4.93M
        __m256i res = _mm256_hadd_epi16(h_c0_b0, h_c16_b16);
2024
4.93M
        res = _mm256_permute4x64_epi64(res, 0xD8);
2025
4.93M
        res = _mm256_slli_epi16(res, 1);
2026
2027
4.93M
        _mm256_storeu_si256((__m256i *)(output_q3 + (w >> 1)), res);
2028
4.93M
      }
2029
5.86M
    }
2030
3.58M
    output_q3 += output_stride;
2031
3.58M
    input += (input_stride << 1);
2032
3.58M
  }
2033
173k
}