Coverage Report

Created: 2023-06-07 06:31

/src/aom/av1/common/x86/cfl_avx2.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
#include <immintrin.h>
12
13
#include "config/av1_rtcd.h"
14
15
#include "av1/common/cfl.h"
16
17
#include "av1/common/x86/cfl_simd.h"
18
19
#define CFL_GET_SUBSAMPLE_FUNCTION_AVX2(sub, bd)                               \
20
  CFL_SUBSAMPLE(avx2, sub, bd, 32, 32)                                         \
21
  CFL_SUBSAMPLE(avx2, sub, bd, 32, 16)                                         \
22
  CFL_SUBSAMPLE(avx2, sub, bd, 32, 8)                                          \
23
  cfl_subsample_##bd##_fn cfl_get_luma_subsampling_##sub##_##bd##_avx2(        \
24
3.31M
      TX_SIZE tx_size) {                                                       \
25
3.31M
    static const cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = {         \
26
3.31M
      cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */                      \
27
3.31M
      cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */                      \
28
3.31M
      cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */                    \
29
3.31M
      cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */                    \
30
3.31M
      NULL,                                     /* 64x64 (invalid CFL size) */ \
31
3.31M
      cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */                      \
32
3.31M
      cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */                      \
33
3.31M
      cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */                     \
34
3.31M
      cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */                     \
35
3.31M
      cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */                    \
36
3.31M
      cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */                    \
37
3.31M
      NULL,                                     /* 32x64 (invalid CFL size) */ \
38
3.31M
      NULL,                                     /* 64x32 (invalid CFL size) */ \
39
3.31M
      cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */                    \
40
3.31M
      cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */                    \
41
3.31M
      cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */                    \
42
3.31M
      cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */                    \
43
3.31M
      NULL,                                     /* 16x64 (invalid CFL size) */ \
44
3.31M
      NULL,                                     /* 64x16 (invalid CFL size) */ \
45
3.31M
    };                                                                         \
46
3.31M
    return subfn_##sub[tx_size];                                               \
47
3.31M
  }
cfl_get_luma_subsampling_420_lbd_avx2
Line
Count
Source
24
1.78M
      TX_SIZE tx_size) {                                                       \
25
1.78M
    static const cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = {         \
26
1.78M
      cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */                      \
27
1.78M
      cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */                      \
28
1.78M
      cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */                    \
29
1.78M
      cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */                    \
30
1.78M
      NULL,                                     /* 64x64 (invalid CFL size) */ \
31
1.78M
      cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */                      \
32
1.78M
      cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */                      \
33
1.78M
      cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */                     \
34
1.78M
      cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */                     \
35
1.78M
      cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */                    \
36
1.78M
      cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */                    \
37
1.78M
      NULL,                                     /* 32x64 (invalid CFL size) */ \
38
1.78M
      NULL,                                     /* 64x32 (invalid CFL size) */ \
39
1.78M
      cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */                    \
40
1.78M
      cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */                    \
41
1.78M
      cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */                    \
42
1.78M
      cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */                    \
43
1.78M
      NULL,                                     /* 16x64 (invalid CFL size) */ \
44
1.78M
      NULL,                                     /* 64x16 (invalid CFL size) */ \
45
1.78M
    };                                                                         \
46
1.78M
    return subfn_##sub[tx_size];                                               \
47
1.78M
  }
cfl_get_luma_subsampling_422_lbd_avx2
Line
Count
Source
24
5.65k
      TX_SIZE tx_size) {                                                       \
25
5.65k
    static const cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = {         \
26
5.65k
      cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */                      \
27
5.65k
      cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */                      \
28
5.65k
      cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */                    \
29
5.65k
      cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */                    \
30
5.65k
      NULL,                                     /* 64x64 (invalid CFL size) */ \
31
5.65k
      cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */                      \
32
5.65k
      cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */                      \
33
5.65k
      cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */                     \
34
5.65k
      cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */                     \
35
5.65k
      cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */                    \
36
5.65k
      cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */                    \
37
5.65k
      NULL,                                     /* 32x64 (invalid CFL size) */ \
38
5.65k
      NULL,                                     /* 64x32 (invalid CFL size) */ \
39
5.65k
      cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */                    \
40
5.65k
      cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */                    \
41
5.65k
      cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */                    \
42
5.65k
      cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */                    \
43
5.65k
      NULL,                                     /* 16x64 (invalid CFL size) */ \
44
5.65k
      NULL,                                     /* 64x16 (invalid CFL size) */ \
45
5.65k
    };                                                                         \
46
5.65k
    return subfn_##sub[tx_size];                                               \
47
5.65k
  }
cfl_get_luma_subsampling_444_lbd_avx2
Line
Count
Source
24
309k
      TX_SIZE tx_size) {                                                       \
25
309k
    static const cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = {         \
26
309k
      cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */                      \
27
309k
      cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */                      \
28
309k
      cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */                    \
29
309k
      cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */                    \
30
309k
      NULL,                                     /* 64x64 (invalid CFL size) */ \
31
309k
      cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */                      \
32
309k
      cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */                      \
33
309k
      cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */                     \
34
309k
      cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */                     \
35
309k
      cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */                    \
36
309k
      cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */                    \
37
309k
      NULL,                                     /* 32x64 (invalid CFL size) */ \
38
309k
      NULL,                                     /* 64x32 (invalid CFL size) */ \
39
309k
      cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */                    \
40
309k
      cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */                    \
41
309k
      cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */                    \
42
309k
      cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */                    \
43
309k
      NULL,                                     /* 16x64 (invalid CFL size) */ \
44
309k
      NULL,                                     /* 64x16 (invalid CFL size) */ \
45
309k
    };                                                                         \
46
309k
    return subfn_##sub[tx_size];                                               \
47
309k
  }
cfl_get_luma_subsampling_420_hbd_avx2
Line
Count
Source
24
526k
      TX_SIZE tx_size) {                                                       \
25
526k
    static const cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = {         \
26
526k
      cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */                      \
27
526k
      cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */                      \
28
526k
      cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */                    \
29
526k
      cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */                    \
30
526k
      NULL,                                     /* 64x64 (invalid CFL size) */ \
31
526k
      cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */                      \
32
526k
      cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */                      \
33
526k
      cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */                     \
34
526k
      cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */                     \
35
526k
      cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */                    \
36
526k
      cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */                    \
37
526k
      NULL,                                     /* 32x64 (invalid CFL size) */ \
38
526k
      NULL,                                     /* 64x32 (invalid CFL size) */ \
39
526k
      cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */                    \
40
526k
      cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */                    \
41
526k
      cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */                    \
42
526k
      cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */                    \
43
526k
      NULL,                                     /* 16x64 (invalid CFL size) */ \
44
526k
      NULL,                                     /* 64x16 (invalid CFL size) */ \
45
526k
    };                                                                         \
46
526k
    return subfn_##sub[tx_size];                                               \
47
526k
  }
cfl_get_luma_subsampling_422_hbd_avx2
Line
Count
Source
24
2.32k
      TX_SIZE tx_size) {                                                       \
25
2.32k
    static const cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = {         \
26
2.32k
      cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */                      \
27
2.32k
      cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */                      \
28
2.32k
      cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */                    \
29
2.32k
      cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */                    \
30
2.32k
      NULL,                                     /* 64x64 (invalid CFL size) */ \
31
2.32k
      cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */                      \
32
2.32k
      cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */                      \
33
2.32k
      cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */                     \
34
2.32k
      cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */                     \
35
2.32k
      cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */                    \
36
2.32k
      cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */                    \
37
2.32k
      NULL,                                     /* 32x64 (invalid CFL size) */ \
38
2.32k
      NULL,                                     /* 64x32 (invalid CFL size) */ \
39
2.32k
      cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */                    \
40
2.32k
      cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */                    \
41
2.32k
      cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */                    \
42
2.32k
      cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */                    \
43
2.32k
      NULL,                                     /* 16x64 (invalid CFL size) */ \
44
2.32k
      NULL,                                     /* 64x16 (invalid CFL size) */ \
45
2.32k
    };                                                                         \
46
2.32k
    return subfn_##sub[tx_size];                                               \
47
2.32k
  }
cfl_get_luma_subsampling_444_hbd_avx2
Line
Count
Source
24
685k
      TX_SIZE tx_size) {                                                       \
25
685k
    static const cfl_subsample_##bd##_fn subfn_##sub[TX_SIZES_ALL] = {         \
26
685k
      cfl_subsample_##bd##_##sub##_4x4_ssse3,   /* 4x4 */                      \
27
685k
      cfl_subsample_##bd##_##sub##_8x8_ssse3,   /* 8x8 */                      \
28
685k
      cfl_subsample_##bd##_##sub##_16x16_ssse3, /* 16x16 */                    \
29
685k
      cfl_subsample_##bd##_##sub##_32x32_avx2,  /* 32x32 */                    \
30
685k
      NULL,                                     /* 64x64 (invalid CFL size) */ \
31
685k
      cfl_subsample_##bd##_##sub##_4x8_ssse3,   /* 4x8 */                      \
32
685k
      cfl_subsample_##bd##_##sub##_8x4_ssse3,   /* 8x4 */                      \
33
685k
      cfl_subsample_##bd##_##sub##_8x16_ssse3,  /* 8x16 */                     \
34
685k
      cfl_subsample_##bd##_##sub##_16x8_ssse3,  /* 16x8 */                     \
35
685k
      cfl_subsample_##bd##_##sub##_16x32_ssse3, /* 16x32 */                    \
36
685k
      cfl_subsample_##bd##_##sub##_32x16_avx2,  /* 32x16 */                    \
37
685k
      NULL,                                     /* 32x64 (invalid CFL size) */ \
38
685k
      NULL,                                     /* 64x32 (invalid CFL size) */ \
39
685k
      cfl_subsample_##bd##_##sub##_4x16_ssse3,  /* 4x16  */                    \
40
685k
      cfl_subsample_##bd##_##sub##_16x4_ssse3,  /* 16x4  */                    \
41
685k
      cfl_subsample_##bd##_##sub##_8x32_ssse3,  /* 8x32  */                    \
42
685k
      cfl_subsample_##bd##_##sub##_32x8_avx2,   /* 32x8  */                    \
43
685k
      NULL,                                     /* 16x64 (invalid CFL size) */ \
44
685k
      NULL,                                     /* 64x16 (invalid CFL size) */ \
45
685k
    };                                                                         \
46
685k
    return subfn_##sub[tx_size];                                               \
47
685k
  }
48
49
/**
50
 * Adds 4 pixels (in a 2x2 grid) and multiplies them by 2. Resulting in a more
51
 * precise version of a box filter 4:2:0 pixel subsampling in Q3.
52
 *
53
 * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
54
 * active area is specified using width and height.
55
 *
56
 * Note: We don't need to worry about going over the active area, as long as we
57
 * stay inside the CfL prediction buffer.
58
 *
59
 * Note: For 4:2:0 luma subsampling, the width will never be greater than 16.
60
 */
61
static void cfl_luma_subsampling_420_lbd_avx2(const uint8_t *input,
62
                                              int input_stride,
63
                                              uint16_t *pred_buf_q3, int width,
64
50.1k
                                              int height) {
65
50.1k
  (void)width;                               // Forever 32
66
50.1k
  const __m256i twos = _mm256_set1_epi8(2);  // Thirty two twos
67
50.1k
  const int luma_stride = input_stride << 1;
68
50.1k
  __m256i *row = (__m256i *)pred_buf_q3;
69
50.1k
  const __m256i *row_end = row + (height >> 1) * CFL_BUF_LINE_I256;
70
580k
  do {
71
580k
    __m256i top = _mm256_loadu_si256((__m256i *)input);
72
580k
    __m256i bot = _mm256_loadu_si256((__m256i *)(input + input_stride));
73
74
580k
    __m256i top_16x16 = _mm256_maddubs_epi16(top, twos);
75
580k
    __m256i bot_16x16 = _mm256_maddubs_epi16(bot, twos);
76
580k
    __m256i sum_16x16 = _mm256_add_epi16(top_16x16, bot_16x16);
77
78
580k
    _mm256_storeu_si256(row, sum_16x16);
79
80
580k
    input += luma_stride;
81
580k
  } while ((row += CFL_BUF_LINE_I256) < row_end);
82
50.1k
}
83
84
CFL_GET_SUBSAMPLE_FUNCTION_AVX2(420, lbd)
85
86
/**
87
 * Adds 2 pixels (in a 2x1 grid) and multiplies them by 4. Resulting in a more
88
 * precise version of a box filter 4:2:2 pixel subsampling in Q3.
89
 *
90
 * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
91
 * active area is specified using width and height.
92
 *
93
 * Note: We don't need to worry about going over the active area, as long as we
94
 * stay inside the CfL prediction buffer.
95
 */
96
static void cfl_luma_subsampling_422_lbd_avx2(const uint8_t *input,
97
                                              int input_stride,
98
                                              uint16_t *pred_buf_q3, int width,
99
1.75k
                                              int height) {
100
1.75k
  (void)width;                                // Forever 32
101
1.75k
  const __m256i fours = _mm256_set1_epi8(4);  // Thirty two fours
102
1.75k
  __m256i *row = (__m256i *)pred_buf_q3;
103
1.75k
  const __m256i *row_end = row + height * CFL_BUF_LINE_I256;
104
38.1k
  do {
105
38.1k
    __m256i top = _mm256_loadu_si256((__m256i *)input);
106
38.1k
    __m256i top_16x16 = _mm256_maddubs_epi16(top, fours);
107
38.1k
    _mm256_storeu_si256(row, top_16x16);
108
38.1k
    input += input_stride;
109
38.1k
  } while ((row += CFL_BUF_LINE_I256) < row_end);
110
1.75k
}
111
112
CFL_GET_SUBSAMPLE_FUNCTION_AVX2(422, lbd)
113
114
/**
115
 * Multiplies the pixels by 8 (scaling in Q3). The AVX2 subsampling is only
116
 * performed on block of width 32.
117
 *
118
 * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
119
 * active area is specified using width and height.
120
 *
121
 * Note: We don't need to worry about going over the active area, as long as we
122
 * stay inside the CfL prediction buffer.
123
 */
124
static void cfl_luma_subsampling_444_lbd_avx2(const uint8_t *input,
125
                                              int input_stride,
126
                                              uint16_t *pred_buf_q3, int width,
127
27.8k
                                              int height) {
128
27.8k
  (void)width;  // Forever 32
129
27.8k
  __m256i *row = (__m256i *)pred_buf_q3;
130
27.8k
  const __m256i *row_end = row + height * CFL_BUF_LINE_I256;
131
27.8k
  const __m256i zeros = _mm256_setzero_si256();
132
562k
  do {
133
562k
    __m256i top = _mm256_loadu_si256((__m256i *)input);
134
562k
    top = _mm256_permute4x64_epi64(top, _MM_SHUFFLE(3, 1, 2, 0));
135
136
562k
    __m256i row_lo = _mm256_unpacklo_epi8(top, zeros);
137
562k
    row_lo = _mm256_slli_epi16(row_lo, 3);
138
562k
    __m256i row_hi = _mm256_unpackhi_epi8(top, zeros);
139
562k
    row_hi = _mm256_slli_epi16(row_hi, 3);
140
141
562k
    _mm256_storeu_si256(row, row_lo);
142
562k
    _mm256_storeu_si256(row + 1, row_hi);
143
144
562k
    input += input_stride;
145
562k
  } while ((row += CFL_BUF_LINE_I256) < row_end);
146
27.8k
}
147
148
CFL_GET_SUBSAMPLE_FUNCTION_AVX2(444, lbd)
149
150
#if CONFIG_AV1_HIGHBITDEPTH
151
/**
152
 * Adds 4 pixels (in a 2x2 grid) and multiplies them by 2. Resulting in a more
153
 * precise version of a box filter 4:2:0 pixel subsampling in Q3.
154
 *
155
 * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
156
 * active area is specified using width and height.
157
 *
158
 * Note: We don't need to worry about going over the active area, as long as we
159
 * stay inside the CfL prediction buffer.
160
 *
161
 * Note: For 4:2:0 luma subsampling, the width will never be greater than 16.
162
 */
163
static void cfl_luma_subsampling_420_hbd_avx2(const uint16_t *input,
164
                                              int input_stride,
165
                                              uint16_t *pred_buf_q3, int width,
166
20.5k
                                              int height) {
167
20.5k
  (void)width;  // Forever 32
168
20.5k
  const int luma_stride = input_stride << 1;
169
20.5k
  __m256i *row = (__m256i *)pred_buf_q3;
170
20.5k
  const __m256i *row_end = row + (height >> 1) * CFL_BUF_LINE_I256;
171
211k
  do {
172
211k
    __m256i top = _mm256_loadu_si256((__m256i *)input);
173
211k
    __m256i bot = _mm256_loadu_si256((__m256i *)(input + input_stride));
174
211k
    __m256i sum = _mm256_add_epi16(top, bot);
175
176
211k
    __m256i top_1 = _mm256_loadu_si256((__m256i *)(input + 16));
177
211k
    __m256i bot_1 = _mm256_loadu_si256((__m256i *)(input + 16 + input_stride));
178
211k
    __m256i sum_1 = _mm256_add_epi16(top_1, bot_1);
179
180
211k
    __m256i hsum = _mm256_hadd_epi16(sum, sum_1);
181
211k
    hsum = _mm256_permute4x64_epi64(hsum, _MM_SHUFFLE(3, 1, 2, 0));
182
211k
    hsum = _mm256_add_epi16(hsum, hsum);
183
184
211k
    _mm256_storeu_si256(row, hsum);
185
186
211k
    input += luma_stride;
187
211k
  } while ((row += CFL_BUF_LINE_I256) < row_end);
188
20.5k
}
189
190
CFL_GET_SUBSAMPLE_FUNCTION_AVX2(420, hbd)
191
192
/**
193
 * Adds 2 pixels (in a 2x1 grid) and multiplies them by 4. Resulting in a more
194
 * precise version of a box filter 4:2:2 pixel subsampling in Q3.
195
 *
196
 * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
197
 * active area is specified using width and height.
198
 *
199
 * Note: We don't need to worry about going over the active area, as long as we
200
 * stay inside the CfL prediction buffer.
201
 *
202
 */
203
static void cfl_luma_subsampling_422_hbd_avx2(const uint16_t *input,
204
                                              int input_stride,
205
                                              uint16_t *pred_buf_q3, int width,
206
119
                                              int height) {
207
119
  (void)width;  // Forever 32
208
119
  __m256i *row = (__m256i *)pred_buf_q3;
209
119
  const __m256i *row_end = row + height * CFL_BUF_LINE_I256;
210
2.21k
  do {
211
2.21k
    __m256i top = _mm256_loadu_si256((__m256i *)input);
212
2.21k
    __m256i top_1 = _mm256_loadu_si256((__m256i *)(input + 16));
213
2.21k
    __m256i hsum = _mm256_hadd_epi16(top, top_1);
214
2.21k
    hsum = _mm256_permute4x64_epi64(hsum, _MM_SHUFFLE(3, 1, 2, 0));
215
2.21k
    hsum = _mm256_slli_epi16(hsum, 2);
216
217
2.21k
    _mm256_storeu_si256(row, hsum);
218
219
2.21k
    input += input_stride;
220
2.21k
  } while ((row += CFL_BUF_LINE_I256) < row_end);
221
119
}
222
223
CFL_GET_SUBSAMPLE_FUNCTION_AVX2(422, hbd)
224
225
static void cfl_luma_subsampling_444_hbd_avx2(const uint16_t *input,
226
                                              int input_stride,
227
                                              uint16_t *pred_buf_q3, int width,
228
58.0k
                                              int height) {
229
58.0k
  (void)width;  // Forever 32
230
58.0k
  __m256i *row = (__m256i *)pred_buf_q3;
231
58.0k
  const __m256i *row_end = row + height * CFL_BUF_LINE_I256;
232
994k
  do {
233
994k
    __m256i top = _mm256_loadu_si256((__m256i *)input);
234
994k
    __m256i top_1 = _mm256_loadu_si256((__m256i *)(input + 16));
235
994k
    _mm256_storeu_si256(row, _mm256_slli_epi16(top, 3));
236
994k
    _mm256_storeu_si256(row + 1, _mm256_slli_epi16(top_1, 3));
237
994k
    input += input_stride;
238
994k
  } while ((row += CFL_BUF_LINE_I256) < row_end);
239
58.0k
}
240
241
CFL_GET_SUBSAMPLE_FUNCTION_AVX2(444, hbd)
242
#endif  // CONFIG_AV1_HIGHBITDEPTH
243
244
static INLINE __m256i predict_unclipped(const __m256i *input, __m256i alpha_q12,
245
10.3M
                                        __m256i alpha_sign, __m256i dc_q0) {
246
10.3M
  __m256i ac_q3 = _mm256_loadu_si256(input);
247
10.3M
  __m256i ac_sign = _mm256_sign_epi16(alpha_sign, ac_q3);
248
10.3M
  __m256i scaled_luma_q0 =
249
10.3M
      _mm256_mulhrs_epi16(_mm256_abs_epi16(ac_q3), alpha_q12);
250
10.3M
  scaled_luma_q0 = _mm256_sign_epi16(scaled_luma_q0, ac_sign);
251
10.3M
  return _mm256_add_epi16(scaled_luma_q0, dc_q0);
252
10.3M
}
253
254
static INLINE void cfl_predict_lbd_avx2(const int16_t *pred_buf_q3,
255
                                        uint8_t *dst, int dst_stride,
256
56.2k
                                        int alpha_q3, int width, int height) {
257
56.2k
  (void)width;
258
56.2k
  const __m256i alpha_sign = _mm256_set1_epi16(alpha_q3);
259
56.2k
  const __m256i alpha_q12 = _mm256_slli_epi16(_mm256_abs_epi16(alpha_sign), 9);
260
56.2k
  const __m256i dc_q0 = _mm256_set1_epi16(*dst);
261
56.2k
  __m256i *row = (__m256i *)pred_buf_q3;
262
56.2k
  const __m256i *row_end = row + height * CFL_BUF_LINE_I256;
263
264
1.13M
  do {
265
1.13M
    __m256i res = predict_unclipped(row, alpha_q12, alpha_sign, dc_q0);
266
1.13M
    __m256i next = predict_unclipped(row + 1, alpha_q12, alpha_sign, dc_q0);
267
1.13M
    res = _mm256_packus_epi16(res, next);
268
1.13M
    res = _mm256_permute4x64_epi64(res, _MM_SHUFFLE(3, 1, 2, 0));
269
1.13M
    _mm256_storeu_si256((__m256i *)dst, res);
270
1.13M
    dst += dst_stride;
271
1.13M
  } while ((row += CFL_BUF_LINE_I256) < row_end);
272
56.2k
}
273
274
CFL_PREDICT_X(avx2, 32, 8, lbd)
275
CFL_PREDICT_X(avx2, 32, 16, lbd)
276
CFL_PREDICT_X(avx2, 32, 32, lbd)
277
278
1.33M
cfl_predict_lbd_fn cfl_get_predict_lbd_fn_avx2(TX_SIZE tx_size) {
279
1.33M
  static const cfl_predict_lbd_fn pred[TX_SIZES_ALL] = {
280
1.33M
    cfl_predict_lbd_4x4_ssse3,   /* 4x4 */
281
1.33M
    cfl_predict_lbd_8x8_ssse3,   /* 8x8 */
282
1.33M
    cfl_predict_lbd_16x16_ssse3, /* 16x16 */
283
1.33M
    cfl_predict_lbd_32x32_avx2,  /* 32x32 */
284
1.33M
    NULL,                        /* 64x64 (invalid CFL size) */
285
1.33M
    cfl_predict_lbd_4x8_ssse3,   /* 4x8 */
286
1.33M
    cfl_predict_lbd_8x4_ssse3,   /* 8x4 */
287
1.33M
    cfl_predict_lbd_8x16_ssse3,  /* 8x16 */
288
1.33M
    cfl_predict_lbd_16x8_ssse3,  /* 16x8 */
289
1.33M
    cfl_predict_lbd_16x32_ssse3, /* 16x32 */
290
1.33M
    cfl_predict_lbd_32x16_avx2,  /* 32x16 */
291
1.33M
    NULL,                        /* 32x64 (invalid CFL size) */
292
1.33M
    NULL,                        /* 64x32 (invalid CFL size) */
293
1.33M
    cfl_predict_lbd_4x16_ssse3,  /* 4x16  */
294
1.33M
    cfl_predict_lbd_16x4_ssse3,  /* 16x4  */
295
1.33M
    cfl_predict_lbd_8x32_ssse3,  /* 8x32  */
296
1.33M
    cfl_predict_lbd_32x8_avx2,   /* 32x8  */
297
1.33M
    NULL,                        /* 16x64 (invalid CFL size) */
298
1.33M
    NULL,                        /* 64x16 (invalid CFL size) */
299
1.33M
  };
300
  // Modulo TX_SIZES_ALL to ensure that an attacker won't be able to index the
301
  // function pointer array out of bounds.
302
1.33M
  return pred[tx_size % TX_SIZES_ALL];
303
1.33M
}
304
305
#if CONFIG_AV1_HIGHBITDEPTH
306
489k
static __m256i highbd_max_epi16(int bd) {
307
489k
  const __m256i neg_one = _mm256_set1_epi16(-1);
308
  // (1 << bd) - 1 => -(-1 << bd) -1 => -1 - (-1 << bd) => -1 ^ (-1 << bd)
309
489k
  return _mm256_xor_si256(_mm256_slli_epi16(neg_one, bd), neg_one);
310
489k
}
311
312
8.12M
static __m256i highbd_clamp_epi16(__m256i u, __m256i zero, __m256i max) {
313
8.12M
  return _mm256_max_epi16(_mm256_min_epi16(u, max), zero);
314
8.12M
}
315
316
static INLINE void cfl_predict_hbd_avx2(const int16_t *pred_buf_q3,
317
                                        uint16_t *dst, int dst_stride,
318
                                        int alpha_q3, int bd, int width,
319
489k
                                        int height) {
320
  // Use SSSE3 version for smaller widths
321
489k
  assert(width == 16 || width == 32);
322
0
  const __m256i alpha_sign = _mm256_set1_epi16(alpha_q3);
323
489k
  const __m256i alpha_q12 = _mm256_slli_epi16(_mm256_abs_epi16(alpha_sign), 9);
324
489k
  const __m256i dc_q0 = _mm256_loadu_si256((__m256i *)dst);
325
489k
  const __m256i max = highbd_max_epi16(bd);
326
327
489k
  __m256i *row = (__m256i *)pred_buf_q3;
328
489k
  const __m256i *row_end = row + height * CFL_BUF_LINE_I256;
329
6.04M
  do {
330
6.04M
    const __m256i res = predict_unclipped(row, alpha_q12, alpha_sign, dc_q0);
331
6.04M
    _mm256_storeu_si256((__m256i *)dst,
332
6.04M
                        highbd_clamp_epi16(res, _mm256_setzero_si256(), max));
333
6.04M
    if (width == 32) {
334
2.08M
      const __m256i res_1 =
335
2.08M
          predict_unclipped(row + 1, alpha_q12, alpha_sign, dc_q0);
336
2.08M
      _mm256_storeu_si256(
337
2.08M
          (__m256i *)(dst + 16),
338
2.08M
          highbd_clamp_epi16(res_1, _mm256_setzero_si256(), max));
339
2.08M
    }
340
6.04M
    dst += dst_stride;
341
6.04M
  } while ((row += CFL_BUF_LINE_I256) < row_end);
342
489k
}
343
344
CFL_PREDICT_X(avx2, 16, 4, hbd)
345
CFL_PREDICT_X(avx2, 16, 8, hbd)
346
CFL_PREDICT_X(avx2, 16, 16, hbd)
347
CFL_PREDICT_X(avx2, 16, 32, hbd)
348
CFL_PREDICT_X(avx2, 32, 8, hbd)
349
CFL_PREDICT_X(avx2, 32, 16, hbd)
350
CFL_PREDICT_X(avx2, 32, 32, hbd)
351
352
1.27M
cfl_predict_hbd_fn cfl_get_predict_hbd_fn_avx2(TX_SIZE tx_size) {
353
1.27M
  static const cfl_predict_hbd_fn pred[TX_SIZES_ALL] = {
354
1.27M
    cfl_predict_hbd_4x4_ssse3,  /* 4x4 */
355
1.27M
    cfl_predict_hbd_8x8_ssse3,  /* 8x8 */
356
1.27M
    cfl_predict_hbd_16x16_avx2, /* 16x16 */
357
1.27M
    cfl_predict_hbd_32x32_avx2, /* 32x32 */
358
1.27M
    NULL,                       /* 64x64 (invalid CFL size) */
359
1.27M
    cfl_predict_hbd_4x8_ssse3,  /* 4x8 */
360
1.27M
    cfl_predict_hbd_8x4_ssse3,  /* 8x4 */
361
1.27M
    cfl_predict_hbd_8x16_ssse3, /* 8x16 */
362
1.27M
    cfl_predict_hbd_16x8_avx2,  /* 16x8 */
363
1.27M
    cfl_predict_hbd_16x32_avx2, /* 16x32 */
364
1.27M
    cfl_predict_hbd_32x16_avx2, /* 32x16 */
365
1.27M
    NULL,                       /* 32x64 (invalid CFL size) */
366
1.27M
    NULL,                       /* 64x32 (invalid CFL size) */
367
1.27M
    cfl_predict_hbd_4x16_ssse3, /* 4x16  */
368
1.27M
    cfl_predict_hbd_16x4_avx2,  /* 16x4  */
369
1.27M
    cfl_predict_hbd_8x32_ssse3, /* 8x32  */
370
1.27M
    cfl_predict_hbd_32x8_avx2,  /* 32x8  */
371
1.27M
    NULL,                       /* 16x64 (invalid CFL size) */
372
1.27M
    NULL,                       /* 64x16 (invalid CFL size) */
373
1.27M
  };
374
  // Modulo TX_SIZES_ALL to ensure that an attacker won't be able to index the
375
  // function pointer array out of bounds.
376
1.27M
  return pred[tx_size % TX_SIZES_ALL];
377
1.27M
}
378
#endif  // CONFIG_AV1_HIGHBITDEPTH
379
380
// Returns a vector where all the (32-bits) elements are the sum of all the
381
// lanes in a.
382
427k
static INLINE __m256i fill_sum_epi32(__m256i a) {
383
  // Given that a == [A, B, C, D, E, F, G, H]
384
427k
  a = _mm256_hadd_epi32(a, a);
385
  // Given that A' == A + B, C' == C + D, E' == E + F, G' == G + H
386
  // a == [A', C', A', C', E', G', E', G']
387
427k
  a = _mm256_permute4x64_epi64(a, _MM_SHUFFLE(3, 1, 2, 0));
388
  // a == [A', C', E', G', A', C', E', G']
389
427k
  a = _mm256_hadd_epi32(a, a);
390
  // Given that A'' == A' + C' and E'' == E' + G'
391
  // a == [A'', E'', A'', E'', A'', E'', A'', E'']
392
427k
  return _mm256_hadd_epi32(a, a);
393
  // Given that A''' == A'' + E''
394
  // a == [A''', A''', A''', A''', A''', A''', A''', A''']
395
427k
}
396
397
3.45M
static INLINE __m256i _mm256_addl_epi16(__m256i a) {
398
3.45M
  return _mm256_add_epi32(_mm256_unpacklo_epi16(a, _mm256_setzero_si256()),
399
3.45M
                          _mm256_unpackhi_epi16(a, _mm256_setzero_si256()));
400
3.45M
}
401
402
static INLINE void subtract_average_avx2(const uint16_t *src_ptr,
403
                                         int16_t *dst_ptr, int width,
404
                                         int height, int round_offset,
405
427k
                                         int num_pel_log2) {
406
  // Use SSE2 version for smaller widths
407
427k
  assert(width == 16 || width == 32);
408
409
0
  const __m256i *src = (__m256i *)src_ptr;
410
427k
  const __m256i *const end = src + height * CFL_BUF_LINE_I256;
411
  // To maximize usage of the AVX2 registers, we sum two rows per loop
412
  // iteration
413
427k
  const int step = 2 * CFL_BUF_LINE_I256;
414
415
427k
  __m256i sum = _mm256_setzero_si256();
416
  // For width 32, we use a second sum accumulator to reduce accumulator
417
  // dependencies in the loop.
418
427k
  __m256i sum2;
419
427k
  if (width == 32) sum2 = _mm256_setzero_si256();
420
421
2.65M
  do {
422
    // Add top row to the bottom row
423
2.65M
    __m256i l0 = _mm256_add_epi16(_mm256_loadu_si256(src),
424
2.65M
                                  _mm256_loadu_si256(src + CFL_BUF_LINE_I256));
425
2.65M
    sum = _mm256_add_epi32(sum, _mm256_addl_epi16(l0));
426
2.65M
    if (width == 32) { /* Don't worry, this if it gets optimized out. */
427
      // Add the second part of the top row to the second part of the bottom row
428
804k
      __m256i l1 =
429
804k
          _mm256_add_epi16(_mm256_loadu_si256(src + 1),
430
804k
                           _mm256_loadu_si256(src + 1 + CFL_BUF_LINE_I256));
431
804k
      sum2 = _mm256_add_epi32(sum2, _mm256_addl_epi16(l1));
432
804k
    }
433
2.65M
    src += step;
434
2.65M
  } while (src < end);
435
  // Combine both sum accumulators
436
427k
  if (width == 32) sum = _mm256_add_epi32(sum, sum2);
437
438
427k
  __m256i fill = fill_sum_epi32(sum);
439
440
427k
  __m256i avg_epi16 = _mm256_srli_epi32(
441
427k
      _mm256_add_epi32(fill, _mm256_set1_epi32(round_offset)), num_pel_log2);
442
427k
  avg_epi16 = _mm256_packs_epi32(avg_epi16, avg_epi16);
443
444
  // Store and subtract loop
445
427k
  src = (__m256i *)src_ptr;
446
427k
  __m256i *dst = (__m256i *)dst_ptr;
447
5.30M
  do {
448
5.30M
    _mm256_storeu_si256(dst,
449
5.30M
                        _mm256_sub_epi16(_mm256_loadu_si256(src), avg_epi16));
450
5.30M
    if (width == 32) {
451
1.60M
      _mm256_storeu_si256(
452
1.60M
          dst + 1, _mm256_sub_epi16(_mm256_loadu_si256(src + 1), avg_epi16));
453
1.60M
    }
454
5.30M
    src += CFL_BUF_LINE_I256;
455
5.30M
    dst += CFL_BUF_LINE_I256;
456
5.30M
  } while (src < end);
457
427k
}
458
459
// Declare wrappers for AVX2 sizes
460
CFL_SUB_AVG_X(avx2, 16, 4, 32, 6)
461
CFL_SUB_AVG_X(avx2, 16, 8, 64, 7)
462
CFL_SUB_AVG_X(avx2, 16, 16, 128, 8)
463
CFL_SUB_AVG_X(avx2, 16, 32, 256, 9)
464
CFL_SUB_AVG_X(avx2, 32, 8, 128, 8)
465
CFL_SUB_AVG_X(avx2, 32, 16, 256, 9)
466
CFL_SUB_AVG_X(avx2, 32, 32, 512, 10)
467
468
// Based on the observation that for small blocks AVX2 does not outperform
469
// SSE2, we call the SSE2 code for block widths 4 and 8.
470
1.30M
cfl_subtract_average_fn cfl_get_subtract_average_fn_avx2(TX_SIZE tx_size) {
471
1.30M
  static const cfl_subtract_average_fn sub_avg[TX_SIZES_ALL] = {
472
1.30M
    cfl_subtract_average_4x4_sse2,   /* 4x4 */
473
1.30M
    cfl_subtract_average_8x8_sse2,   /* 8x8 */
474
1.30M
    cfl_subtract_average_16x16_avx2, /* 16x16 */
475
1.30M
    cfl_subtract_average_32x32_avx2, /* 32x32 */
476
1.30M
    NULL,                            /* 64x64 (invalid CFL size) */
477
1.30M
    cfl_subtract_average_4x8_sse2,   /* 4x8 */
478
1.30M
    cfl_subtract_average_8x4_sse2,   /* 8x4 */
479
1.30M
    cfl_subtract_average_8x16_sse2,  /* 8x16 */
480
1.30M
    cfl_subtract_average_16x8_avx2,  /* 16x8 */
481
1.30M
    cfl_subtract_average_16x32_avx2, /* 16x32 */
482
1.30M
    cfl_subtract_average_32x16_avx2, /* 32x16 */
483
1.30M
    NULL,                            /* 32x64 (invalid CFL size) */
484
1.30M
    NULL,                            /* 64x32 (invalid CFL size) */
485
1.30M
    cfl_subtract_average_4x16_sse2,  /* 4x16 */
486
1.30M
    cfl_subtract_average_16x4_avx2,  /* 16x4 */
487
1.30M
    cfl_subtract_average_8x32_sse2,  /* 8x32 */
488
1.30M
    cfl_subtract_average_32x8_avx2,  /* 32x8 */
489
1.30M
    NULL,                            /* 16x64 (invalid CFL size) */
490
1.30M
    NULL,                            /* 64x16 (invalid CFL size) */
491
1.30M
  };
492
  // Modulo TX_SIZES_ALL to ensure that an attacker won't be able to
493
  // index the function pointer array out of bounds.
494
1.30M
  return sub_avg[tx_size % TX_SIZES_ALL];
495
1.30M
}