• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <immintrin.h>  // AVX2
13 #include "aom_dsp/x86/mem_sse2.h"
14 #include "aom_dsp/x86/synonyms.h"
15 #include "aom_dsp/x86/synonyms_avx2.h"
16 #include "aom_dsp/x86/transpose_sse2.h"
17 
18 #include "config/av1_rtcd.h"
19 #include "av1/common/restoration.h"
20 #include "av1/encoder/pickrst.h"
21 
22 #if CONFIG_AV1_HIGHBITDEPTH
acc_stat_highbd_avx2(int64_t * dst,const uint16_t * dgd,const __m256i * shuffle,const __m256i * dgd_ijkl)23 static INLINE void acc_stat_highbd_avx2(int64_t *dst, const uint16_t *dgd,
24                                         const __m256i *shuffle,
25                                         const __m256i *dgd_ijkl) {
26   // Load two 128-bit chunks from dgd
27   const __m256i s0 = _mm256_inserti128_si256(
28       _mm256_castsi128_si256(_mm_loadu_si128((__m128i *)dgd)),
29       _mm_loadu_si128((__m128i *)(dgd + 4)), 1);
30   // s0 = [11 10 9 8 7 6 5 4] [7 6 5 4 3 2 1 0] as u16 (values are dgd indices)
31   // The weird order is so the shuffle stays within 128-bit lanes
32 
33   // Shuffle 16x u16 values within lanes according to the mask:
34   // [0 1 1 2 2 3 3 4] [0 1 1 2 2 3 3 4]
35   // (Actually we shuffle u8 values as there's no 16-bit shuffle)
36   const __m256i s1 = _mm256_shuffle_epi8(s0, *shuffle);
37   // s1 = [8 7 7 6 6 5 5 4] [4 3 3 2 2 1 1 0] as u16 (values are dgd indices)
38 
39   // Multiply 16x 16-bit integers in dgd_ijkl and s1, resulting in 16x 32-bit
40   // integers then horizontally add pairs of these integers resulting in 8x
41   // 32-bit integers
42   const __m256i d0 = _mm256_madd_epi16(*dgd_ijkl, s1);
43   // d0 = [a b c d] [e f g h] as u32
44 
45   // Take the lower-half of d0, extend to u64, add it on to dst (H)
46   const __m256i d0l = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(d0, 0));
47   // d0l = [a b] [c d] as u64
48   const __m256i dst0 = yy_load_256(dst);
49   yy_store_256(dst, _mm256_add_epi64(d0l, dst0));
50 
51   // Take the upper-half of d0, extend to u64, add it on to dst (H)
52   const __m256i d0h = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(d0, 1));
53   // d0h = [e f] [g h] as u64
54   const __m256i dst1 = yy_load_256(dst + 4);
55   yy_store_256(dst + 4, _mm256_add_epi64(d0h, dst1));
56 }
57 
acc_stat_highbd_win7_one_line_avx2(const uint16_t * dgd,const uint16_t * src,int h_start,int h_end,int dgd_stride,const __m256i * shuffle,int32_t * sumX,int32_t sumY[WIENER_WIN][WIENER_WIN],int64_t M_int[WIENER_WIN][WIENER_WIN],int64_t H_int[WIENER_WIN2][WIENER_WIN * 8])58 static INLINE void acc_stat_highbd_win7_one_line_avx2(
59     const uint16_t *dgd, const uint16_t *src, int h_start, int h_end,
60     int dgd_stride, const __m256i *shuffle, int32_t *sumX,
61     int32_t sumY[WIENER_WIN][WIENER_WIN], int64_t M_int[WIENER_WIN][WIENER_WIN],
62     int64_t H_int[WIENER_WIN2][WIENER_WIN * 8]) {
63   int j, k, l;
64   const int wiener_win = WIENER_WIN;
65   // Main loop handles two pixels at a time
66   // We can assume that h_start is even, since it will always be aligned to
67   // a tile edge + some number of restoration units, and both of those will
68   // be 64-pixel aligned.
69   // However, at the edge of the image, h_end may be odd, so we need to handle
70   // that case correctly.
71   assert(h_start % 2 == 0);
72   const int h_end_even = h_end & ~1;
73   const int has_odd_pixel = h_end & 1;
74   for (j = h_start; j < h_end_even; j += 2) {
75     const uint16_t X1 = src[j];
76     const uint16_t X2 = src[j + 1];
77     *sumX += X1 + X2;
78     const uint16_t *dgd_ij = dgd + j;
79     for (k = 0; k < wiener_win; k++) {
80       const uint16_t *dgd_ijk = dgd_ij + k * dgd_stride;
81       for (l = 0; l < wiener_win; l++) {
82         int64_t *H_ = &H_int[(l * wiener_win + k)][0];
83         const uint16_t D1 = dgd_ijk[l];
84         const uint16_t D2 = dgd_ijk[l + 1];
85         sumY[k][l] += D1 + D2;
86         M_int[k][l] += D1 * X1 + D2 * X2;
87 
88         // Load two u16 values from dgd_ijkl combined as a u32,
89         // then broadcast to 8x u32 slots of a 256
90         const __m256i dgd_ijkl = _mm256_set1_epi32(loadu_int32(dgd_ijk + l));
91         // dgd_ijkl = [y x y x y x y x] [y x y x y x y x] where each is a u16
92 
93         acc_stat_highbd_avx2(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle,
94                              &dgd_ijkl);
95         acc_stat_highbd_avx2(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle,
96                              &dgd_ijkl);
97         acc_stat_highbd_avx2(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle,
98                              &dgd_ijkl);
99         acc_stat_highbd_avx2(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle,
100                              &dgd_ijkl);
101         acc_stat_highbd_avx2(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle,
102                              &dgd_ijkl);
103         acc_stat_highbd_avx2(H_ + 5 * 8, dgd_ij + 5 * dgd_stride, shuffle,
104                              &dgd_ijkl);
105         acc_stat_highbd_avx2(H_ + 6 * 8, dgd_ij + 6 * dgd_stride, shuffle,
106                              &dgd_ijkl);
107       }
108     }
109   }
110   // If the width is odd, add in the final pixel
111   if (has_odd_pixel) {
112     const uint16_t X1 = src[j];
113     *sumX += X1;
114     const uint16_t *dgd_ij = dgd + j;
115     for (k = 0; k < wiener_win; k++) {
116       const uint16_t *dgd_ijk = dgd_ij + k * dgd_stride;
117       for (l = 0; l < wiener_win; l++) {
118         int64_t *H_ = &H_int[(l * wiener_win + k)][0];
119         const uint16_t D1 = dgd_ijk[l];
120         sumY[k][l] += D1;
121         M_int[k][l] += D1 * X1;
122 
123         // The `acc_stat_highbd_avx2` function wants its input to have
124         // interleaved copies of two pixels, but we only have one. However, the
125         // pixels are (effectively) used as inputs to a multiply-accumulate. So
126         // if we set the extra pixel slot to 0, then it is effectively ignored.
127         const __m256i dgd_ijkl = _mm256_set1_epi32((int)D1);
128 
129         acc_stat_highbd_avx2(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle,
130                              &dgd_ijkl);
131         acc_stat_highbd_avx2(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle,
132                              &dgd_ijkl);
133         acc_stat_highbd_avx2(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle,
134                              &dgd_ijkl);
135         acc_stat_highbd_avx2(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle,
136                              &dgd_ijkl);
137         acc_stat_highbd_avx2(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle,
138                              &dgd_ijkl);
139         acc_stat_highbd_avx2(H_ + 5 * 8, dgd_ij + 5 * dgd_stride, shuffle,
140                              &dgd_ijkl);
141         acc_stat_highbd_avx2(H_ + 6 * 8, dgd_ij + 6 * dgd_stride, shuffle,
142                              &dgd_ijkl);
143       }
144     }
145   }
146 }
147 
compute_stats_highbd_win7_opt_avx2(const uint8_t * dgd8,const uint8_t * src8,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,aom_bit_depth_t bit_depth)148 static INLINE void compute_stats_highbd_win7_opt_avx2(
149     const uint8_t *dgd8, const uint8_t *src8, int h_start, int h_end,
150     int v_start, int v_end, int dgd_stride, int src_stride, int64_t *M,
151     int64_t *H, aom_bit_depth_t bit_depth) {
152   int i, j, k, l, m, n;
153   const int wiener_win = WIENER_WIN;
154   const int pixel_count = (h_end - h_start) * (v_end - v_start);
155   const int wiener_win2 = wiener_win * wiener_win;
156   const int wiener_halfwin = (wiener_win >> 1);
157   const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
158   const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
159   const uint16_t avg =
160       find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
161 
162   int64_t M_int[WIENER_WIN][WIENER_WIN] = { { 0 } };
163   DECLARE_ALIGNED(32, int64_t, H_int[WIENER_WIN2][WIENER_WIN * 8]) = { { 0 } };
164   int32_t sumY[WIENER_WIN][WIENER_WIN] = { { 0 } };
165   int32_t sumX = 0;
166   const uint16_t *dgd_win = dgd - wiener_halfwin * dgd_stride - wiener_halfwin;
167 
168   const __m256i shuffle = yy_loadu_256(g_shuffle_stats_highbd_data);
169   for (j = v_start; j < v_end; j += 64) {
170     const int vert_end = AOMMIN(64, v_end - j) + j;
171     for (i = j; i < vert_end; i++) {
172       acc_stat_highbd_win7_one_line_avx2(
173           dgd_win + i * dgd_stride, src + i * src_stride, h_start, h_end,
174           dgd_stride, &shuffle, &sumX, sumY, M_int, H_int);
175     }
176   }
177 
178   uint8_t bit_depth_divider = 1;
179   if (bit_depth == AOM_BITS_12)
180     bit_depth_divider = 16;
181   else if (bit_depth == AOM_BITS_10)
182     bit_depth_divider = 4;
183 
184   const int64_t avg_square_sum = (int64_t)avg * (int64_t)avg * pixel_count;
185   for (k = 0; k < wiener_win; k++) {
186     for (l = 0; l < wiener_win; l++) {
187       const int32_t idx0 = l * wiener_win + k;
188       M[idx0] = (M_int[k][l] +
189                  (avg_square_sum - (int64_t)avg * (sumX + sumY[k][l]))) /
190                 bit_depth_divider;
191       int64_t *H_ = H + idx0 * wiener_win2;
192       int64_t *H_int_ = &H_int[idx0][0];
193       for (m = 0; m < wiener_win; m++) {
194         for (n = 0; n < wiener_win; n++) {
195           H_[m * wiener_win + n] =
196               (H_int_[n * 8 + m] +
197                (avg_square_sum - (int64_t)avg * (sumY[k][l] + sumY[n][m]))) /
198               bit_depth_divider;
199         }
200       }
201     }
202   }
203 }
204 
acc_stat_highbd_win5_one_line_avx2(const uint16_t * dgd,const uint16_t * src,int h_start,int h_end,int dgd_stride,const __m256i * shuffle,int32_t * sumX,int32_t sumY[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],int64_t M_int[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],int64_t H_int[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8])205 static INLINE void acc_stat_highbd_win5_one_line_avx2(
206     const uint16_t *dgd, const uint16_t *src, int h_start, int h_end,
207     int dgd_stride, const __m256i *shuffle, int32_t *sumX,
208     int32_t sumY[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],
209     int64_t M_int[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],
210     int64_t H_int[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8]) {
211   int j, k, l;
212   const int wiener_win = WIENER_WIN_CHROMA;
213   // Main loop handles two pixels at a time
214   // We can assume that h_start is even, since it will always be aligned to
215   // a tile edge + some number of restoration units, and both of those will
216   // be 64-pixel aligned.
217   // However, at the edge of the image, h_end may be odd, so we need to handle
218   // that case correctly.
219   assert(h_start % 2 == 0);
220   const int h_end_even = h_end & ~1;
221   const int has_odd_pixel = h_end & 1;
222   for (j = h_start; j < h_end_even; j += 2) {
223     const uint16_t X1 = src[j];
224     const uint16_t X2 = src[j + 1];
225     *sumX += X1 + X2;
226     const uint16_t *dgd_ij = dgd + j;
227     for (k = 0; k < wiener_win; k++) {
228       const uint16_t *dgd_ijk = dgd_ij + k * dgd_stride;
229       for (l = 0; l < wiener_win; l++) {
230         int64_t *H_ = &H_int[(l * wiener_win + k)][0];
231         const uint16_t D1 = dgd_ijk[l];
232         const uint16_t D2 = dgd_ijk[l + 1];
233         sumY[k][l] += D1 + D2;
234         M_int[k][l] += D1 * X1 + D2 * X2;
235 
236         // Load two u16 values from dgd_ijkl combined as a u32,
237         // then broadcast to 8x u32 slots of a 256
238         const __m256i dgd_ijkl = _mm256_set1_epi32(loadu_int32(dgd_ijk + l));
239         // dgd_ijkl = [x y x y x y x y] [x y x y x y x y] where each is a u16
240 
241         acc_stat_highbd_avx2(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle,
242                              &dgd_ijkl);
243         acc_stat_highbd_avx2(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle,
244                              &dgd_ijkl);
245         acc_stat_highbd_avx2(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle,
246                              &dgd_ijkl);
247         acc_stat_highbd_avx2(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle,
248                              &dgd_ijkl);
249         acc_stat_highbd_avx2(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle,
250                              &dgd_ijkl);
251       }
252     }
253   }
254   // If the width is odd, add in the final pixel
255   if (has_odd_pixel) {
256     const uint16_t X1 = src[j];
257     *sumX += X1;
258     const uint16_t *dgd_ij = dgd + j;
259     for (k = 0; k < wiener_win; k++) {
260       const uint16_t *dgd_ijk = dgd_ij + k * dgd_stride;
261       for (l = 0; l < wiener_win; l++) {
262         int64_t *H_ = &H_int[(l * wiener_win + k)][0];
263         const uint16_t D1 = dgd_ijk[l];
264         sumY[k][l] += D1;
265         M_int[k][l] += D1 * X1;
266 
267         // The `acc_stat_highbd_avx2` function wants its input to have
268         // interleaved copies of two pixels, but we only have one. However, the
269         // pixels are (effectively) used as inputs to a multiply-accumulate. So
270         // if we set the extra pixel slot to 0, then it is effectively ignored.
271         const __m256i dgd_ijkl = _mm256_set1_epi32((int)D1);
272 
273         acc_stat_highbd_avx2(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle,
274                              &dgd_ijkl);
275         acc_stat_highbd_avx2(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle,
276                              &dgd_ijkl);
277         acc_stat_highbd_avx2(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle,
278                              &dgd_ijkl);
279         acc_stat_highbd_avx2(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle,
280                              &dgd_ijkl);
281         acc_stat_highbd_avx2(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle,
282                              &dgd_ijkl);
283       }
284     }
285   }
286 }
287 
compute_stats_highbd_win5_opt_avx2(const uint8_t * dgd8,const uint8_t * src8,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,aom_bit_depth_t bit_depth)288 static INLINE void compute_stats_highbd_win5_opt_avx2(
289     const uint8_t *dgd8, const uint8_t *src8, int h_start, int h_end,
290     int v_start, int v_end, int dgd_stride, int src_stride, int64_t *M,
291     int64_t *H, aom_bit_depth_t bit_depth) {
292   int i, j, k, l, m, n;
293   const int wiener_win = WIENER_WIN_CHROMA;
294   const int pixel_count = (h_end - h_start) * (v_end - v_start);
295   const int wiener_win2 = wiener_win * wiener_win;
296   const int wiener_halfwin = (wiener_win >> 1);
297   const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
298   const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
299   const uint16_t avg =
300       find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
301 
302   int64_t M_int64[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA] = { { 0 } };
303   DECLARE_ALIGNED(
304       32, int64_t,
305       H_int64[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8]) = { { 0 } };
306   int32_t sumY[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA] = { { 0 } };
307   int32_t sumX = 0;
308   const uint16_t *dgd_win = dgd - wiener_halfwin * dgd_stride - wiener_halfwin;
309 
310   const __m256i shuffle = yy_loadu_256(g_shuffle_stats_highbd_data);
311   for (j = v_start; j < v_end; j += 64) {
312     const int vert_end = AOMMIN(64, v_end - j) + j;
313     for (i = j; i < vert_end; i++) {
314       acc_stat_highbd_win5_one_line_avx2(
315           dgd_win + i * dgd_stride, src + i * src_stride, h_start, h_end,
316           dgd_stride, &shuffle, &sumX, sumY, M_int64, H_int64);
317     }
318   }
319 
320   uint8_t bit_depth_divider = 1;
321   if (bit_depth == AOM_BITS_12)
322     bit_depth_divider = 16;
323   else if (bit_depth == AOM_BITS_10)
324     bit_depth_divider = 4;
325 
326   const int64_t avg_square_sum = (int64_t)avg * (int64_t)avg * pixel_count;
327   for (k = 0; k < wiener_win; k++) {
328     for (l = 0; l < wiener_win; l++) {
329       const int32_t idx0 = l * wiener_win + k;
330       M[idx0] = (M_int64[k][l] +
331                  (avg_square_sum - (int64_t)avg * (sumX + sumY[k][l]))) /
332                 bit_depth_divider;
333       int64_t *H_ = H + idx0 * wiener_win2;
334       int64_t *H_int_ = &H_int64[idx0][0];
335       for (m = 0; m < wiener_win; m++) {
336         for (n = 0; n < wiener_win; n++) {
337           H_[m * wiener_win + n] =
338               (H_int_[n * 8 + m] +
339                (avg_square_sum - (int64_t)avg * (sumY[k][l] + sumY[n][m]))) /
340               bit_depth_divider;
341         }
342       }
343     }
344   }
345 }
346 
av1_compute_stats_highbd_avx2(int wiener_win,const uint8_t * dgd8,const uint8_t * src8,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,aom_bit_depth_t bit_depth)347 void av1_compute_stats_highbd_avx2(int wiener_win, const uint8_t *dgd8,
348                                    const uint8_t *src8, int h_start, int h_end,
349                                    int v_start, int v_end, int dgd_stride,
350                                    int src_stride, int64_t *M, int64_t *H,
351                                    aom_bit_depth_t bit_depth) {
352   if (wiener_win == WIENER_WIN) {
353     compute_stats_highbd_win7_opt_avx2(dgd8, src8, h_start, h_end, v_start,
354                                        v_end, dgd_stride, src_stride, M, H,
355                                        bit_depth);
356   } else if (wiener_win == WIENER_WIN_CHROMA) {
357     compute_stats_highbd_win5_opt_avx2(dgd8, src8, h_start, h_end, v_start,
358                                        v_end, dgd_stride, src_stride, M, H,
359                                        bit_depth);
360   } else {
361     av1_compute_stats_highbd_c(wiener_win, dgd8, src8, h_start, h_end, v_start,
362                                v_end, dgd_stride, src_stride, M, H, bit_depth);
363   }
364 }
365 #endif  // CONFIG_AV1_HIGHBITDEPTH
366 
madd_and_accum_avx2(__m256i src,__m256i dgd,__m256i * sum)367 static INLINE void madd_and_accum_avx2(__m256i src, __m256i dgd, __m256i *sum) {
368   *sum = _mm256_add_epi32(*sum, _mm256_madd_epi16(src, dgd));
369 }
370 
convert_and_add_avx2(__m256i src)371 static INLINE __m256i convert_and_add_avx2(__m256i src) {
372   const __m256i s0 = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(src));
373   const __m256i s1 = _mm256_cvtepi32_epi64(_mm256_extracti128_si256(src, 1));
374   return _mm256_add_epi64(s0, s1);
375 }
376 
hadd_four_32_to_64_avx2(__m256i src0,__m256i src1,__m256i * src2,__m256i * src3)377 static INLINE __m256i hadd_four_32_to_64_avx2(__m256i src0, __m256i src1,
378                                               __m256i *src2, __m256i *src3) {
379   // 00 01 10 11 02 03 12 13
380   const __m256i s_0 = _mm256_hadd_epi32(src0, src1);
381   // 20 21 30 31 22 23 32 33
382   const __m256i s_1 = _mm256_hadd_epi32(*src2, *src3);
383   // 00+01 10+11 20+21 30+31 02+03 12+13 22+23 32+33
384   const __m256i s_2 = _mm256_hadd_epi32(s_0, s_1);
385   return convert_and_add_avx2(s_2);
386 }
387 
add_64bit_lvl_avx2(__m256i src0,__m256i src1)388 static INLINE __m128i add_64bit_lvl_avx2(__m256i src0, __m256i src1) {
389   // 00 10 02 12
390   const __m256i t0 = _mm256_unpacklo_epi64(src0, src1);
391   // 01 11 03 13
392   const __m256i t1 = _mm256_unpackhi_epi64(src0, src1);
393   // 00+01 10+11 02+03 12+13
394   const __m256i sum = _mm256_add_epi64(t0, t1);
395   // 00+01 10+11
396   const __m128i sum0 = _mm256_castsi256_si128(sum);
397   // 02+03 12+13
398   const __m128i sum1 = _mm256_extracti128_si256(sum, 1);
399   // 00+01+02+03 10+11+12+13
400   return _mm_add_epi64(sum0, sum1);
401 }
402 
convert_32_to_64_add_avx2(__m256i src0,__m256i src1)403 static INLINE __m128i convert_32_to_64_add_avx2(__m256i src0, __m256i src1) {
404   // 00 01 02 03
405   const __m256i s0 = convert_and_add_avx2(src0);
406   // 10 11 12 13
407   const __m256i s1 = convert_and_add_avx2(src1);
408   return add_64bit_lvl_avx2(s0, s1);
409 }
410 
calc_sum_of_register(__m256i src)411 static INLINE int32_t calc_sum_of_register(__m256i src) {
412   const __m128i src_l = _mm256_castsi256_si128(src);
413   const __m128i src_h = _mm256_extracti128_si256(src, 1);
414   const __m128i sum = _mm_add_epi32(src_l, src_h);
415   const __m128i dst0 = _mm_add_epi32(sum, _mm_srli_si128(sum, 8));
416   const __m128i dst1 = _mm_add_epi32(dst0, _mm_srli_si128(dst0, 4));
417   return _mm_cvtsi128_si32(dst1);
418 }
419 
transpose_64bit_4x4_avx2(const __m256i * const src,__m256i * const dst)420 static INLINE void transpose_64bit_4x4_avx2(const __m256i *const src,
421                                             __m256i *const dst) {
422   // Unpack 64 bit elements. Goes from:
423   // src[0]: 00 01 02 03
424   // src[1]: 10 11 12 13
425   // src[2]: 20 21 22 23
426   // src[3]: 30 31 32 33
427   // to:
428   // reg0:    00 10 02 12
429   // reg1:    20 30 22 32
430   // reg2:    01 11 03 13
431   // reg3:    21 31 23 33
432   const __m256i reg0 = _mm256_unpacklo_epi64(src[0], src[1]);
433   const __m256i reg1 = _mm256_unpacklo_epi64(src[2], src[3]);
434   const __m256i reg2 = _mm256_unpackhi_epi64(src[0], src[1]);
435   const __m256i reg3 = _mm256_unpackhi_epi64(src[2], src[3]);
436 
437   // Unpack 64 bit elements resulting in:
438   // dst[0]: 00 10 20 30
439   // dst[1]: 01 11 21 31
440   // dst[2]: 02 12 22 32
441   // dst[3]: 03 13 23 33
442   dst[0] = _mm256_inserti128_si256(reg0, _mm256_castsi256_si128(reg1), 1);
443   dst[1] = _mm256_inserti128_si256(reg2, _mm256_castsi256_si128(reg3), 1);
444   dst[2] = _mm256_inserti128_si256(reg1, _mm256_extracti128_si256(reg0, 1), 0);
445   dst[3] = _mm256_inserti128_si256(reg3, _mm256_extracti128_si256(reg2, 1), 0);
446 }
447 
448 // When we load 32 values of int8_t type and need less than 32 values for
449 // processing, the below mask is used to make the extra values zero.
450 static const int8_t mask_8bit[32] = {
451   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 16 bytes
452   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,   // 16 bytes
453 };
454 
455 // When we load 16 values of int16_t type and need less than 16 values for
456 // processing, the below mask is used to make the extra values zero.
457 static const int16_t mask_16bit[32] = {
458   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 16 bytes
459   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,   // 16 bytes
460 };
461 
calc_dgd_buf_avg_avx2(const uint8_t * src,int32_t h_start,int32_t h_end,int32_t v_start,int32_t v_end,int32_t stride)462 static INLINE uint8_t calc_dgd_buf_avg_avx2(const uint8_t *src, int32_t h_start,
463                                             int32_t h_end, int32_t v_start,
464                                             int32_t v_end, int32_t stride) {
465   const uint8_t *src_temp = src + v_start * stride + h_start;
466   const __m256i zero = _mm256_setzero_si256();
467   const int32_t width = h_end - h_start;
468   const int32_t height = v_end - v_start;
469   const int32_t wd_beyond_mul32 = width & 31;
470   const int32_t wd_mul32 = width - wd_beyond_mul32;
471   __m128i mask_low, mask_high;
472   __m256i ss = zero;
473 
474   // When width is not multiple of 32, it still loads 32 and to make the data
475   // which is extra (beyond required) as zero using the below mask.
476   if (wd_beyond_mul32 >= 16) {
477     mask_low = _mm_set1_epi8(-1);
478     mask_high = _mm_loadu_si128((__m128i *)(&mask_8bit[32 - wd_beyond_mul32]));
479   } else {
480     mask_low = _mm_loadu_si128((__m128i *)(&mask_8bit[16 - wd_beyond_mul32]));
481     mask_high = _mm_setzero_si128();
482   }
483   const __m256i mask =
484       _mm256_inserti128_si256(_mm256_castsi128_si256(mask_low), mask_high, 1);
485 
486   int32_t proc_ht = 0;
487   do {
488     // Process width in multiple of 32.
489     int32_t proc_wd = 0;
490     while (proc_wd < wd_mul32) {
491       const __m256i s_0 = _mm256_loadu_si256((__m256i *)(src_temp + proc_wd));
492       const __m256i sad_0 = _mm256_sad_epu8(s_0, zero);
493       ss = _mm256_add_epi32(ss, sad_0);
494       proc_wd += 32;
495     }
496 
497     // Process the remaining width.
498     if (wd_beyond_mul32) {
499       const __m256i s_0 = _mm256_loadu_si256((__m256i *)(src_temp + proc_wd));
500       const __m256i s_m_0 = _mm256_and_si256(s_0, mask);
501       const __m256i sad_0 = _mm256_sad_epu8(s_m_0, zero);
502       ss = _mm256_add_epi32(ss, sad_0);
503     }
504     src_temp += stride;
505     proc_ht++;
506   } while (proc_ht < height);
507 
508   const uint32_t sum = calc_sum_of_register(ss);
509   const uint8_t avg = sum / (width * height);
510   return avg;
511 }
512 
513 // Fill (src-avg) or (dgd-avg) buffers. Note that when n = (width % 16) is not
514 // 0, it writes (16 - n) more data than required.
sub_avg_block_avx2(const uint8_t * src,int32_t src_stride,uint8_t avg,int32_t width,int32_t height,int16_t * dst,int32_t dst_stride,int use_downsampled_wiener_stats)515 static INLINE void sub_avg_block_avx2(const uint8_t *src, int32_t src_stride,
516                                       uint8_t avg, int32_t width,
517                                       int32_t height, int16_t *dst,
518                                       int32_t dst_stride,
519                                       int use_downsampled_wiener_stats) {
520   const __m256i avg_reg = _mm256_set1_epi16(avg);
521 
522   int32_t proc_ht = 0;
523   do {
524     int ds_factor =
525         use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;
526     if (use_downsampled_wiener_stats &&
527         (height - proc_ht < WIENER_STATS_DOWNSAMPLE_FACTOR)) {
528       ds_factor = height - proc_ht;
529     }
530 
531     int32_t proc_wd = 0;
532     while (proc_wd < width) {
533       const __m128i s = _mm_loadu_si128((__m128i *)(src + proc_wd));
534       const __m256i ss = _mm256_cvtepu8_epi16(s);
535       const __m256i d = _mm256_sub_epi16(ss, avg_reg);
536       _mm256_storeu_si256((__m256i *)(dst + proc_wd), d);
537       proc_wd += 16;
538     }
539 
540     src += ds_factor * src_stride;
541     dst += ds_factor * dst_stride;
542     proc_ht += ds_factor;
543   } while (proc_ht < height);
544 }
545 
546 // Fills lower-triangular elements of H buffer from upper triangular elements of
547 // the same
fill_lower_triag_elements_avx2(const int32_t wiener_win2,int64_t * const H)548 static INLINE void fill_lower_triag_elements_avx2(const int32_t wiener_win2,
549                                                   int64_t *const H) {
550   for (int32_t i = 0; i < wiener_win2 - 1; i += 4) {
551     __m256i in[4], out[4];
552 
553     in[0] = _mm256_loadu_si256((__m256i *)(H + (i + 0) * wiener_win2 + i + 1));
554     in[1] = _mm256_loadu_si256((__m256i *)(H + (i + 1) * wiener_win2 + i + 1));
555     in[2] = _mm256_loadu_si256((__m256i *)(H + (i + 2) * wiener_win2 + i + 1));
556     in[3] = _mm256_loadu_si256((__m256i *)(H + (i + 3) * wiener_win2 + i + 1));
557 
558     transpose_64bit_4x4_avx2(in, out);
559 
560     _mm_storel_epi64((__m128i *)(H + (i + 1) * wiener_win2 + i),
561                      _mm256_castsi256_si128(out[0]));
562     _mm_storeu_si128((__m128i *)(H + (i + 2) * wiener_win2 + i),
563                      _mm256_castsi256_si128(out[1]));
564     _mm256_storeu_si256((__m256i *)(H + (i + 3) * wiener_win2 + i), out[2]);
565     _mm256_storeu_si256((__m256i *)(H + (i + 4) * wiener_win2 + i), out[3]);
566 
567     for (int32_t j = i + 5; j < wiener_win2; j += 4) {
568       in[0] = _mm256_loadu_si256((__m256i *)(H + (i + 0) * wiener_win2 + j));
569       in[1] = _mm256_loadu_si256((__m256i *)(H + (i + 1) * wiener_win2 + j));
570       in[2] = _mm256_loadu_si256((__m256i *)(H + (i + 2) * wiener_win2 + j));
571       in[3] = _mm256_loadu_si256((__m256i *)(H + (i + 3) * wiener_win2 + j));
572 
573       transpose_64bit_4x4_avx2(in, out);
574 
575       _mm256_storeu_si256((__m256i *)(H + (j + 0) * wiener_win2 + i), out[0]);
576       _mm256_storeu_si256((__m256i *)(H + (j + 1) * wiener_win2 + i), out[1]);
577       _mm256_storeu_si256((__m256i *)(H + (j + 2) * wiener_win2 + i), out[2]);
578       _mm256_storeu_si256((__m256i *)(H + (j + 3) * wiener_win2 + i), out[3]);
579     }
580   }
581 }
582 
583 // Fill H buffer based on loop_count.
584 #define INIT_H_VALUES(d, loop_count)                           \
585   for (int g = 0; g < (loop_count); g++) {                     \
586     const __m256i dgd0 =                                       \
587         _mm256_loadu_si256((__m256i *)((d) + (g * d_stride))); \
588     madd_and_accum_avx2(dgd_mul_df, dgd0, &sum_h[g]);          \
589   }
590 
591 // Fill M & H buffer.
592 #define INIT_MH_VALUES(d)                                      \
593   for (int g = 0; g < wiener_win; g++) {                       \
594     const __m256i dgds_0 =                                     \
595         _mm256_loadu_si256((__m256i *)((d) + (g * d_stride))); \
596     madd_and_accum_avx2(src_mul_df, dgds_0, &sum_m[g]);        \
597     madd_and_accum_avx2(dgd_mul_df, dgds_0, &sum_h[g]);        \
598   }
599 
600 // Update the dgd pointers appropriately.
601 #define INITIALIZATION(wiener_window_sz)                                 \
602   j = i / (wiener_window_sz);                                            \
603   const int16_t *d_window = d + j;                                       \
604   const int16_t *d_current_row =                                         \
605       d + j + ((i % (wiener_window_sz)) * d_stride);                     \
606   int proc_ht = v_start;                                                 \
607   downsample_factor =                                                    \
608       use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1; \
609   __m256i sum_h[wiener_window_sz];                                       \
610   memset(sum_h, 0, sizeof(sum_h));
611 
612 // Update the downsample factor appropriately.
613 #define UPDATE_DOWNSAMPLE_FACTOR                              \
614   int proc_wd = 0;                                            \
615   if (use_downsampled_wiener_stats &&                         \
616       ((v_end - proc_ht) < WIENER_STATS_DOWNSAMPLE_FACTOR)) { \
617     downsample_factor = v_end - proc_ht;                      \
618   }                                                           \
619   const __m256i df_reg = _mm256_set1_epi16(downsample_factor);
620 
621 #define CALCULATE_REMAINING_H_WIN5                                             \
622   while (j < wiener_win) {                                                     \
623     d_window = d;                                                              \
624     d_current_row = d + (i / wiener_win) + ((i % wiener_win) * d_stride);      \
625     const __m256i zero = _mm256_setzero_si256();                               \
626     sum_h[0] = zero;                                                           \
627     sum_h[1] = zero;                                                           \
628     sum_h[2] = zero;                                                           \
629     sum_h[3] = zero;                                                           \
630     sum_h[4] = zero;                                                           \
631                                                                                \
632     proc_ht = v_start;                                                         \
633     downsample_factor =                                                        \
634         use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;     \
635     do {                                                                       \
636       UPDATE_DOWNSAMPLE_FACTOR;                                                \
637                                                                                \
638       /* Process the amount of width multiple of 16.*/                         \
639       while (proc_wd < wd_mul16) {                                             \
640         const __m256i dgd =                                                    \
641             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));          \
642         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);            \
643         INIT_H_VALUES(d_window + j + proc_wd, 5)                               \
644                                                                                \
645         proc_wd += 16;                                                         \
646       };                                                                       \
647                                                                                \
648       /* Process the remaining width here. */                                  \
649       if (wd_beyond_mul16) {                                                   \
650         const __m256i dgd =                                                    \
651             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));          \
652         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);                  \
653         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);       \
654         INIT_H_VALUES(d_window + j + proc_wd, 5)                               \
655       }                                                                        \
656       proc_ht += downsample_factor;                                            \
657       d_window += downsample_factor * d_stride;                                \
658       d_current_row += downsample_factor * d_stride;                           \
659     } while (proc_ht < v_end);                                                 \
660     const __m256i s_h0 =                                                       \
661         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);     \
662     _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + (wiener_win * j)), \
663                         s_h0);                                                 \
664     const __m256i s_m_h = convert_and_add_avx2(sum_h[4]);                      \
665     const __m128i s_m_h0 = add_64bit_lvl_avx2(s_m_h, s_m_h);                   \
666     _mm_storel_epi64(                                                          \
667         (__m128i *)(H + (i * wiener_win2) + (wiener_win * j) + 4), s_m_h0);    \
668     j++;                                                                       \
669   }
670 
671 #define CALCULATE_REMAINING_H_WIN7                                             \
672   while (j < wiener_win) {                                                     \
673     d_window = d;                                                              \
674     d_current_row = d + (i / wiener_win) + ((i % wiener_win) * d_stride);      \
675     const __m256i zero = _mm256_setzero_si256();                               \
676     sum_h[0] = zero;                                                           \
677     sum_h[1] = zero;                                                           \
678     sum_h[2] = zero;                                                           \
679     sum_h[3] = zero;                                                           \
680     sum_h[4] = zero;                                                           \
681     sum_h[5] = zero;                                                           \
682     sum_h[6] = zero;                                                           \
683                                                                                \
684     proc_ht = v_start;                                                         \
685     downsample_factor =                                                        \
686         use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;     \
687     do {                                                                       \
688       UPDATE_DOWNSAMPLE_FACTOR;                                                \
689                                                                                \
690       /* Process the amount of width multiple of 16.*/                         \
691       while (proc_wd < wd_mul16) {                                             \
692         const __m256i dgd =                                                    \
693             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));          \
694         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);            \
695         INIT_H_VALUES(d_window + j + proc_wd, 7)                               \
696                                                                                \
697         proc_wd += 16;                                                         \
698       };                                                                       \
699                                                                                \
700       /* Process the remaining width here. */                                  \
701       if (wd_beyond_mul16) {                                                   \
702         const __m256i dgd =                                                    \
703             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));          \
704         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);                  \
705         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);       \
706         INIT_H_VALUES(d_window + j + proc_wd, 7)                               \
707       }                                                                        \
708       proc_ht += downsample_factor;                                            \
709       d_window += downsample_factor * d_stride;                                \
710       d_current_row += downsample_factor * d_stride;                           \
711     } while (proc_ht < v_end);                                                 \
712     const __m256i s_h1 =                                                       \
713         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);     \
714     _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + (wiener_win * j)), \
715                         s_h1);                                                 \
716     const __m256i s_h2 =                                                       \
717         hadd_four_32_to_64_avx2(sum_h[4], sum_h[5], &sum_h[6], &sum_h[6]);     \
718     _mm256_storeu_si256(                                                       \
719         (__m256i *)(H + (i * wiener_win2) + (wiener_win * j) + 4), s_h2);      \
720     j++;                                                                       \
721   }
722 
723 // The buffers H(auto-covariance) and M(cross-correlation) are used to estimate
724 // the filter tap values required for wiener filtering. Here, the buffer H is of
725 // size ((wiener_window_size^2)*(wiener_window_size^2)) and M is of size
726 // (wiener_window_size*wiener_window_size). H is a symmetric matrix where the
727 // value above the diagonal (upper triangle) are equal to the values below the
728 // diagonal (lower triangle). The calculation of elements/stats of H(upper
729 // triangle) and M is done in steps as described below where each step fills
730 // specific values of H and M.
731 // Once the upper triangular elements of H matrix are derived, the same will be
732 // copied to lower triangular using the function
733 // fill_lower_triag_elements_avx2().
734 // Example: Wiener window size =
735 // WIENER_WIN_CHROMA (5) M buffer = [M0 M1 M2 ---- M23 M24] H buffer = Hxy
736 // (x-row, y-column) [H00 H01 H02 ---- H023 H024] [H10 H11 H12 ---- H123 H124]
737 // [H30 H31 H32 ---- H323 H324]
738 // [H40 H41 H42 ---- H423 H424]
739 // [H50 H51 H52 ---- H523 H524]
740 // [H60 H61 H62 ---- H623 H624]
741 //            ||
742 //            ||
743 // [H230 H231 H232 ---- H2323 H2324]
744 // [H240 H241 H242 ---- H2423 H2424]
745 // In Step 1, whole M buffers (i.e., M0 to M24) and the first row of H (i.e.,
746 // H00 to H024) is filled. The remaining rows of H buffer are filled through
747 // steps 2 to 6.
compute_stats_win5_avx2(const int16_t * const d,int32_t d_stride,const int16_t * const s,int32_t s_stride,int32_t width,int v_start,int v_end,int64_t * const M,int64_t * const H,int use_downsampled_wiener_stats)748 static void compute_stats_win5_avx2(const int16_t *const d, int32_t d_stride,
749                                     const int16_t *const s, int32_t s_stride,
750                                     int32_t width, int v_start, int v_end,
751                                     int64_t *const M, int64_t *const H,
752                                     int use_downsampled_wiener_stats) {
753   const int32_t wiener_win = WIENER_WIN_CHROMA;
754   const int32_t wiener_win2 = wiener_win * wiener_win;
755   // Amount of width which is beyond multiple of 16. This case is handled
756   // appropriately to process only the required width towards the end.
757   const int32_t wd_mul16 = width & ~15;
758   const int32_t wd_beyond_mul16 = width - wd_mul16;
759   const __m256i mask =
760       _mm256_loadu_si256((__m256i *)(&mask_16bit[16 - wd_beyond_mul16]));
761   int downsample_factor;
762 
763   // Step 1: Full M (i.e., M0 to M24) and first row H (i.e., H00 to H024)
764   // values are filled here. Here, the loop over 'j' is executed for values 0
765   // to 4 (wiener_win-1). When the loop executed for a specific 'j', 5 values of
766   // M and H are filled as shown below.
767   // j=0: M0-M4 and H00-H04, j=1: M5-M9 and H05-H09 are filled etc,.
768   int j = 0;
769   do {
770     const int16_t *s_t = s;
771     const int16_t *d_t = d;
772     __m256i sum_m[WIENER_WIN_CHROMA] = { _mm256_setzero_si256() };
773     __m256i sum_h[WIENER_WIN_CHROMA] = { _mm256_setzero_si256() };
774     downsample_factor =
775         use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;
776     int proc_ht = v_start;
777     do {
778       UPDATE_DOWNSAMPLE_FACTOR
779 
780       // Process the amount of width multiple of 16.
781       while (proc_wd < wd_mul16) {
782         const __m256i src = _mm256_loadu_si256((__m256i *)(s_t + proc_wd));
783         const __m256i dgd = _mm256_loadu_si256((__m256i *)(d_t + proc_wd));
784         const __m256i src_mul_df = _mm256_mullo_epi16(src, df_reg);
785         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
786         INIT_MH_VALUES(d_t + j + proc_wd)
787 
788         proc_wd += 16;
789       }
790 
791       // Process the remaining width here.
792       if (wd_beyond_mul16) {
793         const __m256i src = _mm256_loadu_si256((__m256i *)(s_t + proc_wd));
794         const __m256i dgd = _mm256_loadu_si256((__m256i *)(d_t + proc_wd));
795         const __m256i src_mask = _mm256_and_si256(src, mask);
796         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
797         const __m256i src_mul_df = _mm256_mullo_epi16(src_mask, df_reg);
798         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
799         INIT_MH_VALUES(d_t + j + proc_wd)
800       }
801       proc_ht += downsample_factor;
802       s_t += downsample_factor * s_stride;
803       d_t += downsample_factor * d_stride;
804     } while (proc_ht < v_end);
805 
806     const __m256i s_m =
807         hadd_four_32_to_64_avx2(sum_m[0], sum_m[1], &sum_m[2], &sum_m[3]);
808     const __m128i s_m_h = convert_32_to_64_add_avx2(sum_m[4], sum_h[4]);
809     _mm256_storeu_si256((__m256i *)(M + wiener_win * j), s_m);
810     _mm_storel_epi64((__m128i *)&M[wiener_win * j + 4], s_m_h);
811 
812     const __m256i s_h =
813         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
814     _mm256_storeu_si256((__m256i *)(H + wiener_win * j), s_h);
815     _mm_storeh_epi64((__m128i *)&H[wiener_win * j + 4], s_m_h);
816   } while (++j < wiener_win);
817 
818   // The below steps are designed to fill remaining rows of H buffer. Here, aim
819   // is to fill only upper triangle elements correspond to each row and lower
820   // triangle elements are copied from upper-triangle elements. Also, as
821   // mentioned in Step 1, the core function is designed to fill 5
822   // elements/stats/values of H buffer.
823   //
824   // Step 2: Here, the rows 1, 6, 11, 16 and 21 are filled. As we need to fill
825   // only upper-triangle elements, H10 from row1, H60-H64 and H65 from row6,etc,
826   // are need not be filled. As the core function process 5 values, in first
827   // iteration of 'j' only 4 values to be filled i.e., H11-H14 from row1,H66-H69
828   // from row6, etc.
829   for (int i = 1; i < wiener_win2; i += wiener_win) {
830     // Update the dgd pointers appropriately and also derive the 'j'th iteration
831     // from where the H buffer filling needs to be started.
832     INITIALIZATION(WIENER_WIN_CHROMA)
833 
834     do {
835       UPDATE_DOWNSAMPLE_FACTOR
836 
837       // Process the amount of width multiple of 16.
838       while (proc_wd < wd_mul16) {
839         const __m256i dgd =
840             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
841         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
842         INIT_H_VALUES(d_window + proc_wd + (1 * d_stride), 4)
843 
844         proc_wd += 16;
845       }
846 
847       // Process the remaining width here.
848       if (wd_beyond_mul16) {
849         const __m256i dgd =
850             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
851         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
852         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
853         INIT_H_VALUES(d_window + proc_wd + (1 * d_stride), 4)
854       }
855       proc_ht += downsample_factor;
856       d_window += downsample_factor * d_stride;
857       d_current_row += downsample_factor * d_stride;
858     } while (proc_ht < v_end);
859     const __m256i s_h =
860         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
861     _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + i), s_h);
862 
863     // process the remaining 'j' iterations.
864     j++;
865     CALCULATE_REMAINING_H_WIN5
866   }
867 
868   // Step 3: Here, the rows 2, 7, 12, 17 and 22 are filled. As we need to fill
869   // only upper-triangle elements, H20-H21 from row2, H70-H74 and H75-H76 from
870   // row7, etc, are need not be filled. As the core function process 5 values,
871   // in first iteration of 'j' only 3 values to be filled i.e., H22-H24 from
872   // row2, H77-H79 from row7, etc.
873   for (int i = 2; i < wiener_win2; i += wiener_win) {
874     // Update the dgd pointers appropriately and also derive the 'j'th iteration
875     // from where the H buffer filling needs to be started.
876     INITIALIZATION(WIENER_WIN_CHROMA)
877 
878     do {
879       UPDATE_DOWNSAMPLE_FACTOR
880 
881       // Process the amount of width multiple of 16.
882       while (proc_wd < wd_mul16) {
883         const __m256i dgd =
884             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
885         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
886         INIT_H_VALUES(d_window + proc_wd + (2 * d_stride), 3)
887 
888         proc_wd += 16;
889       }
890 
891       // Process the remaining width here.
892       if (wd_beyond_mul16) {
893         const __m256i dgd =
894             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
895         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
896         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
897         INIT_H_VALUES(d_window + proc_wd + (2 * d_stride), 3)
898       }
899       proc_ht += downsample_factor;
900       d_window += downsample_factor * d_stride;
901       d_current_row += downsample_factor * d_stride;
902     } while (proc_ht < v_end);
903     const __m256i s_h =
904         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
905     _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + i), s_h);
906 
907     // process the remaining 'j' iterations.
908     j++;
909     CALCULATE_REMAINING_H_WIN5
910   }
911 
912   // Step 4: Here, the rows 3, 8, 13, 18 and 23 are filled. As we need to fill
913   // only upper-triangle elements, H30-H32 from row3, H80-H84 and H85-H87 from
914   // row8, etc, are need not be filled. As the core function process 5 values,
915   // in first iteration of 'j' only 2 values to be filled i.e., H33-H34 from
916   // row3, H88-89 from row8, etc.
917   for (int i = 3; i < wiener_win2; i += wiener_win) {
918     // Update the dgd pointers appropriately and also derive the 'j'th iteration
919     // from where the H buffer filling needs to be started.
920     INITIALIZATION(WIENER_WIN_CHROMA)
921 
922     do {
923       UPDATE_DOWNSAMPLE_FACTOR
924 
925       // Process the amount of width multiple of 16.
926       while (proc_wd < wd_mul16) {
927         const __m256i dgd =
928             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
929         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
930         INIT_H_VALUES(d_window + proc_wd + (3 * d_stride), 2)
931 
932         proc_wd += 16;
933       }
934 
935       // Process the remaining width here.
936       if (wd_beyond_mul16) {
937         const __m256i dgd =
938             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
939         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
940         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
941         INIT_H_VALUES(d_window + proc_wd + (3 * d_stride), 2)
942       }
943       proc_ht += downsample_factor;
944       d_window += downsample_factor * d_stride;
945       d_current_row += downsample_factor * d_stride;
946     } while (proc_ht < v_end);
947     const __m128i s_h = convert_32_to_64_add_avx2(sum_h[0], sum_h[1]);
948     _mm_storeu_si128((__m128i *)(H + (i * wiener_win2) + i), s_h);
949 
950     // process the remaining 'j' iterations.
951     j++;
952     CALCULATE_REMAINING_H_WIN5
953   }
954 
955   // Step 5: Here, the rows 4, 9, 14, 19 and 24 are filled. As we need to fill
956   // only upper-triangle elements, H40-H43 from row4, H90-H94 and H95-H98 from
957   // row9, etc, are need not be filled. As the core function process 5 values,
958   // in first iteration of 'j' only 1 values to be filled i.e., H44 from row4,
959   // H99 from row9, etc.
960   for (int i = 4; i < wiener_win2; i += wiener_win) {
961     // Update the dgd pointers appropriately and also derive the 'j'th iteration
962     // from where the H buffer filling needs to be started.
963     INITIALIZATION(WIENER_WIN_CHROMA)
964     do {
965       UPDATE_DOWNSAMPLE_FACTOR
966 
967       // Process the amount of width multiple of 16.
968       while (proc_wd < wd_mul16) {
969         const __m256i dgd =
970             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
971         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
972         INIT_H_VALUES(d_window + proc_wd + (4 * d_stride), 1)
973 
974         proc_wd += 16;
975       }
976 
977       // Process the remaining width here.
978       if (wd_beyond_mul16) {
979         const __m256i dgd =
980             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
981         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
982         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
983         INIT_H_VALUES(d_window + proc_wd + (4 * d_stride), 1)
984       }
985       proc_ht += downsample_factor;
986       d_window += downsample_factor * d_stride;
987       d_current_row += downsample_factor * d_stride;
988     } while (proc_ht < v_end);
989     const __m128i s_h = convert_32_to_64_add_avx2(sum_h[0], sum_h[1]);
990     _mm_storeu_si128((__m128i *)(H + (i * wiener_win2) + i), s_h);
991 
992     // process the remaining 'j' iterations.
993     j++;
994     CALCULATE_REMAINING_H_WIN5
995   }
996 
997   // Step 6: Here, the rows 5, 10, 15 and 20 are filled. As we need to fill only
998   // upper-triangle elements, H50-H54 from row5, H100-H104 and H105-H109 from
999   // row10,etc, are need not be filled. The first iteration of 'j' fills H55-H59
1000   // from row5 and H1010-H1014 from row10, etc.
1001   for (int i = 5; i < wiener_win2; i += wiener_win) {
1002     // Derive j'th iteration from where the H buffer filling needs to be
1003     // started.
1004     j = i / wiener_win;
1005     int shift = 0;
1006     do {
1007       // Update the dgd pointers appropriately.
1008       int proc_ht = v_start;
1009       const int16_t *d_window = d + (i / wiener_win);
1010       const int16_t *d_current_row =
1011           d + (i / wiener_win) + ((i % wiener_win) * d_stride);
1012       downsample_factor =
1013           use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;
1014       __m256i sum_h[WIENER_WIN_CHROMA] = { _mm256_setzero_si256() };
1015       do {
1016         UPDATE_DOWNSAMPLE_FACTOR
1017 
1018         // Process the amount of width multiple of 16.
1019         while (proc_wd < wd_mul16) {
1020           const __m256i dgd =
1021               _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1022           const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
1023           INIT_H_VALUES(d_window + shift + proc_wd, 5)
1024 
1025           proc_wd += 16;
1026         }
1027 
1028         // Process the remaining width here.
1029         if (wd_beyond_mul16) {
1030           const __m256i dgd =
1031               _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1032           const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
1033           const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
1034           INIT_H_VALUES(d_window + shift + proc_wd, 5)
1035         }
1036         proc_ht += downsample_factor;
1037         d_window += downsample_factor * d_stride;
1038         d_current_row += downsample_factor * d_stride;
1039       } while (proc_ht < v_end);
1040 
1041       const __m256i s_h =
1042           hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
1043       _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + (wiener_win * j)),
1044                           s_h);
1045       const __m256i s_m_h = convert_and_add_avx2(sum_h[4]);
1046       const __m128i s_m_h0 = add_64bit_lvl_avx2(s_m_h, s_m_h);
1047       _mm_storel_epi64(
1048           (__m128i *)(H + (i * wiener_win2) + (wiener_win * j) + 4), s_m_h0);
1049       shift++;
1050     } while (++j < wiener_win);
1051   }
1052 
1053   fill_lower_triag_elements_avx2(wiener_win2, H);
1054 }
1055 
1056 // The buffers H(auto-covariance) and M(cross-correlation) are used to estimate
1057 // the filter tap values required for wiener filtering. Here, the buffer H is of
1058 // size ((wiener_window_size^2)*(wiener_window_size^2)) and M is of size
1059 // (wiener_window_size*wiener_window_size). H is a symmetric matrix where the
1060 // value above the diagonal (upper triangle) are equal to the values below the
1061 // diagonal (lower triangle). The calculation of elements/stats of H(upper
1062 // triangle) and M is done in steps as described below where each step fills
1063 // specific values of H and M.
1064 // Example:
1065 // Wiener window size = WIENER_WIN (7)
1066 // M buffer = [M0 M1 M2 ---- M47 M48]
1067 // H buffer = Hxy (x-row, y-column)
1068 // [H00 H01 H02 ---- H047 H048]
1069 // [H10 H11 H12 ---- H147 H148]
1070 // [H30 H31 H32 ---- H347 H348]
1071 // [H40 H41 H42 ---- H447 H448]
1072 // [H50 H51 H52 ---- H547 H548]
1073 // [H60 H61 H62 ---- H647 H648]
1074 //            ||
1075 //            ||
1076 // [H470 H471 H472 ---- H4747 H4748]
1077 // [H480 H481 H482 ---- H4847 H4848]
1078 // In Step 1, whole M buffers (i.e., M0 to M48) and the first row of H (i.e.,
1079 // H00 to H048) is filled. The remaining rows of H buffer are filled through
1080 // steps 2 to 8.
compute_stats_win7_avx2(const int16_t * const d,int32_t d_stride,const int16_t * const s,int32_t s_stride,int32_t width,int v_start,int v_end,int64_t * const M,int64_t * const H,int use_downsampled_wiener_stats)1081 static void compute_stats_win7_avx2(const int16_t *const d, int32_t d_stride,
1082                                     const int16_t *const s, int32_t s_stride,
1083                                     int32_t width, int v_start, int v_end,
1084                                     int64_t *const M, int64_t *const H,
1085                                     int use_downsampled_wiener_stats) {
1086   const int32_t wiener_win = WIENER_WIN;
1087   const int32_t wiener_win2 = wiener_win * wiener_win;
1088   // Amount of width which is beyond multiple of 16. This case is handled
1089   // appropriately to process only the required width towards the end.
1090   const int32_t wd_mul16 = width & ~15;
1091   const int32_t wd_beyond_mul16 = width - wd_mul16;
1092   const __m256i mask =
1093       _mm256_loadu_si256((__m256i *)(&mask_16bit[16 - wd_beyond_mul16]));
1094   int downsample_factor;
1095 
1096   // Step 1: Full M (i.e., M0 to M48) and first row H (i.e., H00 to H048)
1097   // values are filled here. Here, the loop over 'j' is executed for values 0
1098   // to 6. When the loop executed for a specific 'j', 7 values of M and H are
1099   // filled as shown below.
1100   // j=0: M0-M6 and H00-H06, j=1: M7-M13 and H07-H013 are filled etc,.
1101   int j = 0;
1102   do {
1103     const int16_t *s_t = s;
1104     const int16_t *d_t = d;
1105     __m256i sum_m[WIENER_WIN] = { _mm256_setzero_si256() };
1106     __m256i sum_h[WIENER_WIN] = { _mm256_setzero_si256() };
1107     downsample_factor =
1108         use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;
1109     int proc_ht = v_start;
1110     do {
1111       UPDATE_DOWNSAMPLE_FACTOR
1112 
1113       // Process the amount of width multiple of 16.
1114       while (proc_wd < wd_mul16) {
1115         const __m256i src = _mm256_loadu_si256((__m256i *)(s_t + proc_wd));
1116         const __m256i dgd = _mm256_loadu_si256((__m256i *)(d_t + proc_wd));
1117         const __m256i src_mul_df = _mm256_mullo_epi16(src, df_reg);
1118         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
1119         INIT_MH_VALUES(d_t + j + proc_wd)
1120 
1121         proc_wd += 16;
1122       }
1123 
1124       if (wd_beyond_mul16) {
1125         const __m256i src = _mm256_loadu_si256((__m256i *)(s_t + proc_wd));
1126         const __m256i dgd = _mm256_loadu_si256((__m256i *)(d_t + proc_wd));
1127         const __m256i src_mask = _mm256_and_si256(src, mask);
1128         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
1129         const __m256i src_mul_df = _mm256_mullo_epi16(src_mask, df_reg);
1130         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
1131         INIT_MH_VALUES(d_t + j + proc_wd)
1132       }
1133       proc_ht += downsample_factor;
1134       s_t += downsample_factor * s_stride;
1135       d_t += downsample_factor * d_stride;
1136     } while (proc_ht < v_end);
1137 
1138     const __m256i s_m0 =
1139         hadd_four_32_to_64_avx2(sum_m[0], sum_m[1], &sum_m[2], &sum_m[3]);
1140     const __m256i s_m1 =
1141         hadd_four_32_to_64_avx2(sum_m[4], sum_m[5], &sum_m[6], &sum_m[6]);
1142     _mm256_storeu_si256((__m256i *)(M + wiener_win * j + 0), s_m0);
1143     _mm_storeu_si128((__m128i *)(M + wiener_win * j + 4),
1144                      _mm256_castsi256_si128(s_m1));
1145     _mm_storel_epi64((__m128i *)&M[wiener_win * j + 6],
1146                      _mm256_extracti128_si256(s_m1, 1));
1147 
1148     const __m256i sh_0 =
1149         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
1150     const __m256i sh_1 =
1151         hadd_four_32_to_64_avx2(sum_h[4], sum_h[5], &sum_h[6], &sum_h[6]);
1152     _mm256_storeu_si256((__m256i *)(H + wiener_win * j + 0), sh_0);
1153     _mm_storeu_si128((__m128i *)(H + wiener_win * j + 4),
1154                      _mm256_castsi256_si128(sh_1));
1155     _mm_storel_epi64((__m128i *)&H[wiener_win * j + 6],
1156                      _mm256_extracti128_si256(sh_1, 1));
1157   } while (++j < wiener_win);
1158 
1159   // The below steps are designed to fill remaining rows of H buffer. Here, aim
1160   // is to fill only upper triangle elements correspond to each row and lower
1161   // triangle elements are copied from upper-triangle elements. Also, as
1162   // mentioned in Step 1, the core function is designed to fill 7
1163   // elements/stats/values of H buffer.
1164   //
1165   // Step 2: Here, the rows 1, 8, 15, 22, 29, 36 and 43 are filled. As we need
1166   // to fill only upper-triangle elements, H10 from row1, H80-H86 and H87 from
1167   // row8, etc. are need not be filled. As the core function process 7 values,
1168   // in first iteration of 'j' only 6 values to be filled i.e., H11-H16 from
1169   // row1 and H88-H813 from row8, etc.
1170   for (int i = 1; i < wiener_win2; i += wiener_win) {
1171     // Update the dgd pointers appropriately and also derive the 'j'th iteration
1172     // from where the H buffer filling needs to be started.
1173     INITIALIZATION(WIENER_WIN)
1174 
1175     do {
1176       UPDATE_DOWNSAMPLE_FACTOR
1177 
1178       // Process the amount of width multiple of 16.
1179       while (proc_wd < wd_mul16) {
1180         const __m256i dgd =
1181             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1182         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
1183         INIT_H_VALUES(d_window + proc_wd + (1 * d_stride), 6)
1184 
1185         proc_wd += 16;
1186       }
1187 
1188       // Process the remaining width here.
1189       if (wd_beyond_mul16) {
1190         const __m256i dgd =
1191             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1192         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
1193         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
1194         INIT_H_VALUES(d_window + proc_wd + (1 * d_stride), 6)
1195       }
1196       proc_ht += downsample_factor;
1197       d_window += downsample_factor * d_stride;
1198       d_current_row += downsample_factor * d_stride;
1199     } while (proc_ht < v_end);
1200     const __m256i s_h =
1201         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
1202     _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + i), s_h);
1203     const __m128i s_h0 = convert_32_to_64_add_avx2(sum_h[4], sum_h[5]);
1204     _mm_storeu_si128((__m128i *)(H + (i * wiener_win2) + i + 4), s_h0);
1205 
1206     // process the remaining 'j' iterations.
1207     j++;
1208     CALCULATE_REMAINING_H_WIN7
1209   }
1210 
1211   // Step 3: Here, the rows 2, 9, 16, 23, 30, 37 and 44 are filled. As we need
1212   // to fill only upper-triangle elements, H20-H21 from row2, H90-H96 and
1213   // H97-H98 from row9, etc. are need not be filled. As the core function
1214   // process 7 values, in first iteration of 'j' only 5 values to be filled
1215   // i.e., H22-H26 from row2 and H99-H913 from row9, etc.
1216   for (int i = 2; i < wiener_win2; i += wiener_win) {
1217     // Update the dgd pointers appropriately and also derive the 'j'th iteration
1218     // from where the H buffer filling needs to be started.
1219     INITIALIZATION(WIENER_WIN)
1220     do {
1221       UPDATE_DOWNSAMPLE_FACTOR
1222 
1223       // Process the amount of width multiple of 16.
1224       while (proc_wd < wd_mul16) {
1225         const __m256i dgd =
1226             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1227         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
1228         INIT_H_VALUES(d_window + proc_wd + (2 * d_stride), 5)
1229 
1230         proc_wd += 16;
1231       }
1232 
1233       // Process the remaining width here.
1234       if (wd_beyond_mul16) {
1235         const __m256i dgd =
1236             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1237         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
1238         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
1239         INIT_H_VALUES(d_window + proc_wd + (2 * d_stride), 5)
1240       }
1241       proc_ht += downsample_factor;
1242       d_window += downsample_factor * d_stride;
1243       d_current_row += downsample_factor * d_stride;
1244     } while (proc_ht < v_end);
1245     const __m256i s_h =
1246         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
1247     _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + i), s_h);
1248     const __m256i s_m_h = convert_and_add_avx2(sum_h[4]);
1249     const __m128i s_m_h0 = add_64bit_lvl_avx2(s_m_h, s_m_h);
1250     _mm_storel_epi64((__m128i *)(H + (i * wiener_win2) + i + 4), s_m_h0);
1251 
1252     // process the remaining 'j' iterations.
1253     j++;
1254     CALCULATE_REMAINING_H_WIN7
1255   }
1256 
1257   // Step 4: Here, the rows 3, 10, 17, 24, 31, 38 and 45 are filled. As we need
1258   // to fill only upper-triangle elements, H30-H32 from row3, H100-H106 and
1259   // H107-H109 from row10, etc. are need not be filled. As the core function
1260   // process 7 values, in first iteration of 'j' only 4 values to be filled
1261   // i.e., H33-H36 from row3 and H1010-H1013 from row10, etc.
1262   for (int i = 3; i < wiener_win2; i += wiener_win) {
1263     // Update the dgd pointers appropriately and also derive the 'j'th iteration
1264     // from where the H buffer filling needs to be started.
1265     INITIALIZATION(WIENER_WIN)
1266 
1267     do {
1268       UPDATE_DOWNSAMPLE_FACTOR
1269 
1270       // Process the amount of width multiple of 16.
1271       while (proc_wd < wd_mul16) {
1272         const __m256i dgd =
1273             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1274         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
1275         INIT_H_VALUES(d_window + proc_wd + (3 * d_stride), 4)
1276 
1277         proc_wd += 16;
1278       }
1279 
1280       // Process the remaining width here.
1281       if (wd_beyond_mul16) {
1282         const __m256i dgd =
1283             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1284         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
1285         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
1286         INIT_H_VALUES(d_window + proc_wd + (3 * d_stride), 4)
1287       }
1288       proc_ht += downsample_factor;
1289       d_window += downsample_factor * d_stride;
1290       d_current_row += downsample_factor * d_stride;
1291     } while (proc_ht < v_end);
1292     const __m256i s_h =
1293         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
1294     _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + i), s_h);
1295 
1296     // process the remaining 'j' iterations.
1297     j++;
1298     CALCULATE_REMAINING_H_WIN7
1299   }
1300 
1301   // Step 5: Here, the rows 4, 11, 18, 25, 32, 39 and 46 are filled. As we need
1302   // to fill only upper-triangle elements, H40-H43 from row4, H110-H116 and
1303   // H117-H1110 from row10, etc. are need not be filled. As the core function
1304   // process 7 values, in first iteration of 'j' only 3 values to be filled
1305   // i.e., H44-H46 from row4 and H1111-H1113 from row11, etc.
1306   for (int i = 4; i < wiener_win2; i += wiener_win) {
1307     // Update the dgd pointers appropriately and also derive the 'j'th iteration
1308     // from where the H buffer filling needs to be started.
1309     INITIALIZATION(WIENER_WIN)
1310 
1311     do {
1312       UPDATE_DOWNSAMPLE_FACTOR
1313 
1314       // Process the amount of width multiple of 16.
1315       while (proc_wd < wd_mul16) {
1316         const __m256i dgd =
1317             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1318         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
1319         INIT_H_VALUES(d_window + proc_wd + (4 * d_stride), 3)
1320 
1321         proc_wd += 16;
1322       }
1323 
1324       // Process the remaining width here.
1325       if (wd_beyond_mul16) {
1326         const __m256i dgd =
1327             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1328         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
1329         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
1330         INIT_H_VALUES(d_window + proc_wd + (4 * d_stride), 3)
1331       }
1332       proc_ht += downsample_factor;
1333       d_window += downsample_factor * d_stride;
1334       d_current_row += downsample_factor * d_stride;
1335     } while (proc_ht < v_end);
1336     const __m256i s_h =
1337         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
1338     _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + i), s_h);
1339 
1340     // process the remaining 'j' iterations.
1341     j++;
1342     CALCULATE_REMAINING_H_WIN7
1343   }
1344 
1345   // Step 6: Here, the rows 5, 12, 19, 26, 33, 40 and 47 are filled. As we need
1346   // to fill only upper-triangle elements, H50-H54 from row5, H120-H126 and
1347   // H127-H1211 from row12, etc. are need not be filled. As the core function
1348   // process 7 values, in first iteration of 'j' only 2 values to be filled
1349   // i.e., H55-H56 from row5 and H1212-H1213 from row12, etc.
1350   for (int i = 5; i < wiener_win2; i += wiener_win) {
1351     // Update the dgd pointers appropriately and also derive the 'j'th iteration
1352     // from where the H buffer filling needs to be started.
1353     INITIALIZATION(WIENER_WIN)
1354     do {
1355       UPDATE_DOWNSAMPLE_FACTOR
1356 
1357       // Process the amount of width multiple of 16.
1358       while (proc_wd < wd_mul16) {
1359         const __m256i dgd =
1360             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1361         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
1362         INIT_H_VALUES(d_window + proc_wd + (5 * d_stride), 2)
1363 
1364         proc_wd += 16;
1365       }
1366 
1367       // Process the remaining width here.
1368       if (wd_beyond_mul16) {
1369         const __m256i dgd =
1370             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1371         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
1372         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
1373         INIT_H_VALUES(d_window + proc_wd + (5 * d_stride), 2)
1374       }
1375       proc_ht += downsample_factor;
1376       d_window += downsample_factor * d_stride;
1377       d_current_row += downsample_factor * d_stride;
1378     } while (proc_ht < v_end);
1379     const __m256i s_h =
1380         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
1381     _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + i), s_h);
1382 
1383     // process the remaining 'j' iterations.
1384     j++;
1385     CALCULATE_REMAINING_H_WIN7
1386   }
1387 
1388   // Step 7: Here, the rows 6, 13, 20, 27, 34, 41 and 48 are filled. As we need
1389   // to fill only upper-triangle elements, H60-H65 from row6, H130-H136 and
1390   // H137-H1312 from row13, etc. are need not be filled. As the core function
1391   // process 7 values, in first iteration of 'j' only 1 value to be filled
1392   // i.e., H66 from row6 and H1313 from row13, etc.
1393   for (int i = 6; i < wiener_win2; i += wiener_win) {
1394     // Update the dgd pointers appropriately and also derive the 'j'th iteration
1395     // from where the H buffer filling needs to be started.
1396     INITIALIZATION(WIENER_WIN)
1397     do {
1398       UPDATE_DOWNSAMPLE_FACTOR
1399 
1400       // Process the amount of width multiple of 16.
1401       while (proc_wd < wd_mul16) {
1402         const __m256i dgd =
1403             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1404         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
1405         INIT_H_VALUES(d_window + proc_wd + (6 * d_stride), 1)
1406 
1407         proc_wd += 16;
1408       }
1409 
1410       // Process the remaining width here.
1411       if (wd_beyond_mul16) {
1412         const __m256i dgd =
1413             _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1414         const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
1415         const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
1416         INIT_H_VALUES(d_window + proc_wd + (6 * d_stride), 1)
1417       }
1418       proc_ht += downsample_factor;
1419       d_window += downsample_factor * d_stride;
1420       d_current_row += downsample_factor * d_stride;
1421     } while (proc_ht < v_end);
1422     const __m256i s_h =
1423         hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
1424     xx_storel_64(&H[(i * wiener_win2) + i], _mm256_castsi256_si128(s_h));
1425 
1426     // process the remaining 'j' iterations.
1427     j++;
1428     CALCULATE_REMAINING_H_WIN7
1429   }
1430 
1431   // Step 8: Here, the rows 7, 14, 21, 28, 35 and 42 are filled. As we need
1432   // to fill only upper-triangle elements, H70-H75 from row7, H140-H146 and
1433   // H147-H1413 from row14, etc. are need not be filled. The first iteration of
1434   // 'j' fills H77-H713 from row7 and H1414-H1420 from row14, etc.
1435   for (int i = 7; i < wiener_win2; i += wiener_win) {
1436     // Derive j'th iteration from where the H buffer filling needs to be
1437     // started.
1438     j = i / wiener_win;
1439     int shift = 0;
1440     do {
1441       // Update the dgd pointers appropriately.
1442       int proc_ht = v_start;
1443       const int16_t *d_window = d + (i / WIENER_WIN);
1444       const int16_t *d_current_row =
1445           d + (i / WIENER_WIN) + ((i % WIENER_WIN) * d_stride);
1446       downsample_factor =
1447           use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;
1448       __m256i sum_h[WIENER_WIN] = { _mm256_setzero_si256() };
1449       do {
1450         UPDATE_DOWNSAMPLE_FACTOR
1451 
1452         // Process the amount of width multiple of 16.
1453         while (proc_wd < wd_mul16) {
1454           const __m256i dgd =
1455               _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1456           const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd, df_reg);
1457           INIT_H_VALUES(d_window + shift + proc_wd, 7)
1458 
1459           proc_wd += 16;
1460         }
1461 
1462         // Process the remaining width here.
1463         if (wd_beyond_mul16) {
1464           const __m256i dgd =
1465               _mm256_loadu_si256((__m256i *)(d_current_row + proc_wd));
1466           const __m256i dgd_mask = _mm256_and_si256(dgd, mask);
1467           const __m256i dgd_mul_df = _mm256_mullo_epi16(dgd_mask, df_reg);
1468           INIT_H_VALUES(d_window + shift + proc_wd, 7)
1469         }
1470         proc_ht += downsample_factor;
1471         d_window += downsample_factor * d_stride;
1472         d_current_row += downsample_factor * d_stride;
1473       } while (proc_ht < v_end);
1474 
1475       const __m256i sh_0 =
1476           hadd_four_32_to_64_avx2(sum_h[0], sum_h[1], &sum_h[2], &sum_h[3]);
1477       const __m256i sh_1 =
1478           hadd_four_32_to_64_avx2(sum_h[4], sum_h[5], &sum_h[6], &sum_h[6]);
1479       _mm256_storeu_si256((__m256i *)(H + (i * wiener_win2) + (wiener_win * j)),
1480                           sh_0);
1481       _mm_storeu_si128(
1482           (__m128i *)(H + (i * wiener_win2) + (wiener_win * j) + 4),
1483           _mm256_castsi256_si128(sh_1));
1484       _mm_storel_epi64((__m128i *)&H[(i * wiener_win2) + (wiener_win * j) + 6],
1485                        _mm256_extracti128_si256(sh_1, 1));
1486       shift++;
1487     } while (++j < wiener_win);
1488   }
1489 
1490   fill_lower_triag_elements_avx2(wiener_win2, H);
1491 }
1492 
av1_compute_stats_avx2(int wiener_win,const uint8_t * dgd,const uint8_t * src,int16_t * dgd_avg,int16_t * src_avg,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,int use_downsampled_wiener_stats)1493 void av1_compute_stats_avx2(int wiener_win, const uint8_t *dgd,
1494                             const uint8_t *src, int16_t *dgd_avg,
1495                             int16_t *src_avg, int h_start, int h_end,
1496                             int v_start, int v_end, int dgd_stride,
1497                             int src_stride, int64_t *M, int64_t *H,
1498                             int use_downsampled_wiener_stats) {
1499   if (wiener_win != WIENER_WIN && wiener_win != WIENER_WIN_CHROMA) {
1500     // Currently, libaom supports Wiener filter processing with window sizes as
1501     // WIENER_WIN_CHROMA(5) and WIENER_WIN(7). For any other window size, SIMD
1502     // support is not facilitated. Hence, invoke C function for the same.
1503     av1_compute_stats_c(wiener_win, dgd, src, dgd_avg, src_avg, h_start, h_end,
1504                         v_start, v_end, dgd_stride, src_stride, M, H,
1505                         use_downsampled_wiener_stats);
1506     return;
1507   }
1508 
1509   const int32_t wiener_halfwin = wiener_win >> 1;
1510   const uint8_t avg =
1511       calc_dgd_buf_avg_avx2(dgd, h_start, h_end, v_start, v_end, dgd_stride);
1512   const int32_t width = h_end - h_start;
1513   const int32_t height = v_end - v_start;
1514   const int32_t d_stride = (width + 2 * wiener_halfwin + 15) & ~15;
1515   const int32_t s_stride = (width + 15) & ~15;
1516 
1517   // Based on the sf 'use_downsampled_wiener_stats', process either once for
1518   // UPDATE_DOWNSAMPLE_FACTOR or for each row.
1519   sub_avg_block_avx2(src + v_start * src_stride + h_start, src_stride, avg,
1520                      width, height, src_avg, s_stride,
1521                      use_downsampled_wiener_stats);
1522 
1523   // Compute (dgd-avg) buffer here which is used to fill H buffer.
1524   sub_avg_block_avx2(
1525       dgd + (v_start - wiener_halfwin) * dgd_stride + h_start - wiener_halfwin,
1526       dgd_stride, avg, width + 2 * wiener_halfwin, height + 2 * wiener_halfwin,
1527       dgd_avg, d_stride, 0);
1528   if (wiener_win == WIENER_WIN) {
1529     compute_stats_win7_avx2(dgd_avg, d_stride, src_avg, s_stride, width,
1530                             v_start, v_end, M, H, use_downsampled_wiener_stats);
1531   } else if (wiener_win == WIENER_WIN_CHROMA) {
1532     compute_stats_win5_avx2(dgd_avg, d_stride, src_avg, s_stride, width,
1533                             v_start, v_end, M, H, use_downsampled_wiener_stats);
1534   }
1535 }
1536 
pair_set_epi16(int a,int b)1537 static INLINE __m256i pair_set_epi16(int a, int b) {
1538   return _mm256_set1_epi32(
1539       (int32_t)(((uint16_t)(a)) | (((uint32_t)(b)) << 16)));
1540 }
1541 
av1_lowbd_pixel_proj_error_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int xq[2],const sgr_params_type * params)1542 int64_t av1_lowbd_pixel_proj_error_avx2(
1543     const uint8_t *src8, int width, int height, int src_stride,
1544     const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
1545     int32_t *flt1, int flt1_stride, int xq[2], const sgr_params_type *params) {
1546   int i, j, k;
1547   const int32_t shift = SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS;
1548   const __m256i rounding = _mm256_set1_epi32(1 << (shift - 1));
1549   __m256i sum64 = _mm256_setzero_si256();
1550   const uint8_t *src = src8;
1551   const uint8_t *dat = dat8;
1552   int64_t err = 0;
1553   if (params->r[0] > 0 && params->r[1] > 0) {
1554     __m256i xq_coeff = pair_set_epi16(xq[0], xq[1]);
1555     for (i = 0; i < height; ++i) {
1556       __m256i sum32 = _mm256_setzero_si256();
1557       for (j = 0; j <= width - 16; j += 16) {
1558         const __m256i d0 = _mm256_cvtepu8_epi16(xx_loadu_128(dat + j));
1559         const __m256i s0 = _mm256_cvtepu8_epi16(xx_loadu_128(src + j));
1560         const __m256i flt0_16b = _mm256_permute4x64_epi64(
1561             _mm256_packs_epi32(yy_loadu_256(flt0 + j),
1562                                yy_loadu_256(flt0 + j + 8)),
1563             0xd8);
1564         const __m256i flt1_16b = _mm256_permute4x64_epi64(
1565             _mm256_packs_epi32(yy_loadu_256(flt1 + j),
1566                                yy_loadu_256(flt1 + j + 8)),
1567             0xd8);
1568         const __m256i u0 = _mm256_slli_epi16(d0, SGRPROJ_RST_BITS);
1569         const __m256i flt0_0_sub_u = _mm256_sub_epi16(flt0_16b, u0);
1570         const __m256i flt1_0_sub_u = _mm256_sub_epi16(flt1_16b, u0);
1571         const __m256i v0 = _mm256_madd_epi16(
1572             xq_coeff, _mm256_unpacklo_epi16(flt0_0_sub_u, flt1_0_sub_u));
1573         const __m256i v1 = _mm256_madd_epi16(
1574             xq_coeff, _mm256_unpackhi_epi16(flt0_0_sub_u, flt1_0_sub_u));
1575         const __m256i vr0 =
1576             _mm256_srai_epi32(_mm256_add_epi32(v0, rounding), shift);
1577         const __m256i vr1 =
1578             _mm256_srai_epi32(_mm256_add_epi32(v1, rounding), shift);
1579         const __m256i e0 = _mm256_sub_epi16(
1580             _mm256_add_epi16(_mm256_packs_epi32(vr0, vr1), d0), s0);
1581         const __m256i err0 = _mm256_madd_epi16(e0, e0);
1582         sum32 = _mm256_add_epi32(sum32, err0);
1583       }
1584       for (k = j; k < width; ++k) {
1585         const int32_t u = (int32_t)(dat[k] << SGRPROJ_RST_BITS);
1586         int32_t v = xq[0] * (flt0[k] - u) + xq[1] * (flt1[k] - u);
1587         const int32_t e = ROUND_POWER_OF_TWO(v, shift) + dat[k] - src[k];
1588         err += ((int64_t)e * e);
1589       }
1590       dat += dat_stride;
1591       src += src_stride;
1592       flt0 += flt0_stride;
1593       flt1 += flt1_stride;
1594       const __m256i sum64_0 =
1595           _mm256_cvtepi32_epi64(_mm256_castsi256_si128(sum32));
1596       const __m256i sum64_1 =
1597           _mm256_cvtepi32_epi64(_mm256_extracti128_si256(sum32, 1));
1598       sum64 = _mm256_add_epi64(sum64, sum64_0);
1599       sum64 = _mm256_add_epi64(sum64, sum64_1);
1600     }
1601   } else if (params->r[0] > 0 || params->r[1] > 0) {
1602     const int xq_active = (params->r[0] > 0) ? xq[0] : xq[1];
1603     const __m256i xq_coeff =
1604         pair_set_epi16(xq_active, -xq_active * (1 << SGRPROJ_RST_BITS));
1605     const int32_t *flt = (params->r[0] > 0) ? flt0 : flt1;
1606     const int flt_stride = (params->r[0] > 0) ? flt0_stride : flt1_stride;
1607     for (i = 0; i < height; ++i) {
1608       __m256i sum32 = _mm256_setzero_si256();
1609       for (j = 0; j <= width - 16; j += 16) {
1610         const __m256i d0 = _mm256_cvtepu8_epi16(xx_loadu_128(dat + j));
1611         const __m256i s0 = _mm256_cvtepu8_epi16(xx_loadu_128(src + j));
1612         const __m256i flt_16b = _mm256_permute4x64_epi64(
1613             _mm256_packs_epi32(yy_loadu_256(flt + j),
1614                                yy_loadu_256(flt + j + 8)),
1615             0xd8);
1616         const __m256i v0 =
1617             _mm256_madd_epi16(xq_coeff, _mm256_unpacklo_epi16(flt_16b, d0));
1618         const __m256i v1 =
1619             _mm256_madd_epi16(xq_coeff, _mm256_unpackhi_epi16(flt_16b, d0));
1620         const __m256i vr0 =
1621             _mm256_srai_epi32(_mm256_add_epi32(v0, rounding), shift);
1622         const __m256i vr1 =
1623             _mm256_srai_epi32(_mm256_add_epi32(v1, rounding), shift);
1624         const __m256i e0 = _mm256_sub_epi16(
1625             _mm256_add_epi16(_mm256_packs_epi32(vr0, vr1), d0), s0);
1626         const __m256i err0 = _mm256_madd_epi16(e0, e0);
1627         sum32 = _mm256_add_epi32(sum32, err0);
1628       }
1629       for (k = j; k < width; ++k) {
1630         const int32_t u = (int32_t)(dat[k] << SGRPROJ_RST_BITS);
1631         int32_t v = xq_active * (flt[k] - u);
1632         const int32_t e = ROUND_POWER_OF_TWO(v, shift) + dat[k] - src[k];
1633         err += ((int64_t)e * e);
1634       }
1635       dat += dat_stride;
1636       src += src_stride;
1637       flt += flt_stride;
1638       const __m256i sum64_0 =
1639           _mm256_cvtepi32_epi64(_mm256_castsi256_si128(sum32));
1640       const __m256i sum64_1 =
1641           _mm256_cvtepi32_epi64(_mm256_extracti128_si256(sum32, 1));
1642       sum64 = _mm256_add_epi64(sum64, sum64_0);
1643       sum64 = _mm256_add_epi64(sum64, sum64_1);
1644     }
1645   } else {
1646     __m256i sum32 = _mm256_setzero_si256();
1647     for (i = 0; i < height; ++i) {
1648       for (j = 0; j <= width - 16; j += 16) {
1649         const __m256i d0 = _mm256_cvtepu8_epi16(xx_loadu_128(dat + j));
1650         const __m256i s0 = _mm256_cvtepu8_epi16(xx_loadu_128(src + j));
1651         const __m256i diff0 = _mm256_sub_epi16(d0, s0);
1652         const __m256i err0 = _mm256_madd_epi16(diff0, diff0);
1653         sum32 = _mm256_add_epi32(sum32, err0);
1654       }
1655       for (k = j; k < width; ++k) {
1656         const int32_t e = (int32_t)(dat[k]) - src[k];
1657         err += ((int64_t)e * e);
1658       }
1659       dat += dat_stride;
1660       src += src_stride;
1661     }
1662     const __m256i sum64_0 =
1663         _mm256_cvtepi32_epi64(_mm256_castsi256_si128(sum32));
1664     const __m256i sum64_1 =
1665         _mm256_cvtepi32_epi64(_mm256_extracti128_si256(sum32, 1));
1666     sum64 = _mm256_add_epi64(sum64_0, sum64_1);
1667   }
1668   int64_t sum[4];
1669   yy_storeu_256(sum, sum64);
1670   err += sum[0] + sum[1] + sum[2] + sum[3];
1671   return err;
1672 }
1673 
1674 // When params->r[0] > 0 and params->r[1] > 0. In this case all elements of
1675 // C and H need to be computed.
calc_proj_params_r0_r1_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])1676 static AOM_INLINE void calc_proj_params_r0_r1_avx2(
1677     const uint8_t *src8, int width, int height, int src_stride,
1678     const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
1679     int32_t *flt1, int flt1_stride, int64_t H[2][2], int64_t C[2]) {
1680   const int size = width * height;
1681   const uint8_t *src = src8;
1682   const uint8_t *dat = dat8;
1683   __m256i h00, h01, h11, c0, c1;
1684   const __m256i zero = _mm256_setzero_si256();
1685   h01 = h11 = c0 = c1 = h00 = zero;
1686 
1687   for (int i = 0; i < height; ++i) {
1688     for (int j = 0; j < width; j += 8) {
1689       const __m256i u_load = _mm256_cvtepu8_epi32(
1690           _mm_loadl_epi64((__m128i *)(dat + i * dat_stride + j)));
1691       const __m256i s_load = _mm256_cvtepu8_epi32(
1692           _mm_loadl_epi64((__m128i *)(src + i * src_stride + j)));
1693       __m256i f1 = _mm256_loadu_si256((__m256i *)(flt0 + i * flt0_stride + j));
1694       __m256i f2 = _mm256_loadu_si256((__m256i *)(flt1 + i * flt1_stride + j));
1695       __m256i d = _mm256_slli_epi32(u_load, SGRPROJ_RST_BITS);
1696       __m256i s = _mm256_slli_epi32(s_load, SGRPROJ_RST_BITS);
1697       s = _mm256_sub_epi32(s, d);
1698       f1 = _mm256_sub_epi32(f1, d);
1699       f2 = _mm256_sub_epi32(f2, d);
1700 
1701       const __m256i h00_even = _mm256_mul_epi32(f1, f1);
1702       const __m256i h00_odd = _mm256_mul_epi32(_mm256_srli_epi64(f1, 32),
1703                                                _mm256_srli_epi64(f1, 32));
1704       h00 = _mm256_add_epi64(h00, h00_even);
1705       h00 = _mm256_add_epi64(h00, h00_odd);
1706 
1707       const __m256i h01_even = _mm256_mul_epi32(f1, f2);
1708       const __m256i h01_odd = _mm256_mul_epi32(_mm256_srli_epi64(f1, 32),
1709                                                _mm256_srli_epi64(f2, 32));
1710       h01 = _mm256_add_epi64(h01, h01_even);
1711       h01 = _mm256_add_epi64(h01, h01_odd);
1712 
1713       const __m256i h11_even = _mm256_mul_epi32(f2, f2);
1714       const __m256i h11_odd = _mm256_mul_epi32(_mm256_srli_epi64(f2, 32),
1715                                                _mm256_srli_epi64(f2, 32));
1716       h11 = _mm256_add_epi64(h11, h11_even);
1717       h11 = _mm256_add_epi64(h11, h11_odd);
1718 
1719       const __m256i c0_even = _mm256_mul_epi32(f1, s);
1720       const __m256i c0_odd =
1721           _mm256_mul_epi32(_mm256_srli_epi64(f1, 32), _mm256_srli_epi64(s, 32));
1722       c0 = _mm256_add_epi64(c0, c0_even);
1723       c0 = _mm256_add_epi64(c0, c0_odd);
1724 
1725       const __m256i c1_even = _mm256_mul_epi32(f2, s);
1726       const __m256i c1_odd =
1727           _mm256_mul_epi32(_mm256_srli_epi64(f2, 32), _mm256_srli_epi64(s, 32));
1728       c1 = _mm256_add_epi64(c1, c1_even);
1729       c1 = _mm256_add_epi64(c1, c1_odd);
1730     }
1731   }
1732 
1733   __m256i c_low = _mm256_unpacklo_epi64(c0, c1);
1734   const __m256i c_high = _mm256_unpackhi_epi64(c0, c1);
1735   c_low = _mm256_add_epi64(c_low, c_high);
1736   const __m128i c_128bit = _mm_add_epi64(_mm256_extracti128_si256(c_low, 1),
1737                                          _mm256_castsi256_si128(c_low));
1738 
1739   __m256i h0x_low = _mm256_unpacklo_epi64(h00, h01);
1740   const __m256i h0x_high = _mm256_unpackhi_epi64(h00, h01);
1741   h0x_low = _mm256_add_epi64(h0x_low, h0x_high);
1742   const __m128i h0x_128bit = _mm_add_epi64(_mm256_extracti128_si256(h0x_low, 1),
1743                                            _mm256_castsi256_si128(h0x_low));
1744 
1745   // Using the symmetric properties of H,  calculations of H[1][0] are not
1746   // needed.
1747   __m256i h1x_low = _mm256_unpacklo_epi64(zero, h11);
1748   const __m256i h1x_high = _mm256_unpackhi_epi64(zero, h11);
1749   h1x_low = _mm256_add_epi64(h1x_low, h1x_high);
1750   const __m128i h1x_128bit = _mm_add_epi64(_mm256_extracti128_si256(h1x_low, 1),
1751                                            _mm256_castsi256_si128(h1x_low));
1752 
1753   xx_storeu_128(C, c_128bit);
1754   xx_storeu_128(H[0], h0x_128bit);
1755   xx_storeu_128(H[1], h1x_128bit);
1756 
1757   H[0][0] /= size;
1758   H[0][1] /= size;
1759   H[1][1] /= size;
1760 
1761   // Since H is a symmetric matrix
1762   H[1][0] = H[0][1];
1763   C[0] /= size;
1764   C[1] /= size;
1765 }
1766 
1767 // When only params->r[0] > 0. In this case only H[0][0] and C[0] are
1768 // non-zero and need to be computed.
calc_proj_params_r0_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int64_t H[2][2],int64_t C[2])1769 static AOM_INLINE void calc_proj_params_r0_avx2(const uint8_t *src8, int width,
1770                                                 int height, int src_stride,
1771                                                 const uint8_t *dat8,
1772                                                 int dat_stride, int32_t *flt0,
1773                                                 int flt0_stride,
1774                                                 int64_t H[2][2], int64_t C[2]) {
1775   const int size = width * height;
1776   const uint8_t *src = src8;
1777   const uint8_t *dat = dat8;
1778   __m256i h00, c0;
1779   const __m256i zero = _mm256_setzero_si256();
1780   c0 = h00 = zero;
1781 
1782   for (int i = 0; i < height; ++i) {
1783     for (int j = 0; j < width; j += 8) {
1784       const __m256i u_load = _mm256_cvtepu8_epi32(
1785           _mm_loadl_epi64((__m128i *)(dat + i * dat_stride + j)));
1786       const __m256i s_load = _mm256_cvtepu8_epi32(
1787           _mm_loadl_epi64((__m128i *)(src + i * src_stride + j)));
1788       __m256i f1 = _mm256_loadu_si256((__m256i *)(flt0 + i * flt0_stride + j));
1789       __m256i d = _mm256_slli_epi32(u_load, SGRPROJ_RST_BITS);
1790       __m256i s = _mm256_slli_epi32(s_load, SGRPROJ_RST_BITS);
1791       s = _mm256_sub_epi32(s, d);
1792       f1 = _mm256_sub_epi32(f1, d);
1793 
1794       const __m256i h00_even = _mm256_mul_epi32(f1, f1);
1795       const __m256i h00_odd = _mm256_mul_epi32(_mm256_srli_epi64(f1, 32),
1796                                                _mm256_srli_epi64(f1, 32));
1797       h00 = _mm256_add_epi64(h00, h00_even);
1798       h00 = _mm256_add_epi64(h00, h00_odd);
1799 
1800       const __m256i c0_even = _mm256_mul_epi32(f1, s);
1801       const __m256i c0_odd =
1802           _mm256_mul_epi32(_mm256_srli_epi64(f1, 32), _mm256_srli_epi64(s, 32));
1803       c0 = _mm256_add_epi64(c0, c0_even);
1804       c0 = _mm256_add_epi64(c0, c0_odd);
1805     }
1806   }
1807   const __m128i h00_128bit = _mm_add_epi64(_mm256_extracti128_si256(h00, 1),
1808                                            _mm256_castsi256_si128(h00));
1809   const __m128i h00_val =
1810       _mm_add_epi64(h00_128bit, _mm_srli_si128(h00_128bit, 8));
1811 
1812   const __m128i c0_128bit = _mm_add_epi64(_mm256_extracti128_si256(c0, 1),
1813                                           _mm256_castsi256_si128(c0));
1814   const __m128i c0_val = _mm_add_epi64(c0_128bit, _mm_srli_si128(c0_128bit, 8));
1815 
1816   const __m128i c = _mm_unpacklo_epi64(c0_val, _mm256_castsi256_si128(zero));
1817   const __m128i h0x = _mm_unpacklo_epi64(h00_val, _mm256_castsi256_si128(zero));
1818 
1819   xx_storeu_128(C, c);
1820   xx_storeu_128(H[0], h0x);
1821 
1822   H[0][0] /= size;
1823   C[0] /= size;
1824 }
1825 
1826 // When only params->r[1] > 0. In this case only H[1][1] and C[1] are
1827 // non-zero and need to be computed.
calc_proj_params_r1_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])1828 static AOM_INLINE void calc_proj_params_r1_avx2(const uint8_t *src8, int width,
1829                                                 int height, int src_stride,
1830                                                 const uint8_t *dat8,
1831                                                 int dat_stride, int32_t *flt1,
1832                                                 int flt1_stride,
1833                                                 int64_t H[2][2], int64_t C[2]) {
1834   const int size = width * height;
1835   const uint8_t *src = src8;
1836   const uint8_t *dat = dat8;
1837   __m256i h11, c1;
1838   const __m256i zero = _mm256_setzero_si256();
1839   c1 = h11 = zero;
1840 
1841   for (int i = 0; i < height; ++i) {
1842     for (int j = 0; j < width; j += 8) {
1843       const __m256i u_load = _mm256_cvtepu8_epi32(
1844           _mm_loadl_epi64((__m128i *)(dat + i * dat_stride + j)));
1845       const __m256i s_load = _mm256_cvtepu8_epi32(
1846           _mm_loadl_epi64((__m128i *)(src + i * src_stride + j)));
1847       __m256i f2 = _mm256_loadu_si256((__m256i *)(flt1 + i * flt1_stride + j));
1848       __m256i d = _mm256_slli_epi32(u_load, SGRPROJ_RST_BITS);
1849       __m256i s = _mm256_slli_epi32(s_load, SGRPROJ_RST_BITS);
1850       s = _mm256_sub_epi32(s, d);
1851       f2 = _mm256_sub_epi32(f2, d);
1852 
1853       const __m256i h11_even = _mm256_mul_epi32(f2, f2);
1854       const __m256i h11_odd = _mm256_mul_epi32(_mm256_srli_epi64(f2, 32),
1855                                                _mm256_srli_epi64(f2, 32));
1856       h11 = _mm256_add_epi64(h11, h11_even);
1857       h11 = _mm256_add_epi64(h11, h11_odd);
1858 
1859       const __m256i c1_even = _mm256_mul_epi32(f2, s);
1860       const __m256i c1_odd =
1861           _mm256_mul_epi32(_mm256_srli_epi64(f2, 32), _mm256_srli_epi64(s, 32));
1862       c1 = _mm256_add_epi64(c1, c1_even);
1863       c1 = _mm256_add_epi64(c1, c1_odd);
1864     }
1865   }
1866 
1867   const __m128i h11_128bit = _mm_add_epi64(_mm256_extracti128_si256(h11, 1),
1868                                            _mm256_castsi256_si128(h11));
1869   const __m128i h11_val =
1870       _mm_add_epi64(h11_128bit, _mm_srli_si128(h11_128bit, 8));
1871 
1872   const __m128i c1_128bit = _mm_add_epi64(_mm256_extracti128_si256(c1, 1),
1873                                           _mm256_castsi256_si128(c1));
1874   const __m128i c1_val = _mm_add_epi64(c1_128bit, _mm_srli_si128(c1_128bit, 8));
1875 
1876   const __m128i c = _mm_unpacklo_epi64(_mm256_castsi256_si128(zero), c1_val);
1877   const __m128i h1x = _mm_unpacklo_epi64(_mm256_castsi256_si128(zero), h11_val);
1878 
1879   xx_storeu_128(C, c);
1880   xx_storeu_128(H[1], h1x);
1881 
1882   H[1][1] /= size;
1883   C[1] /= size;
1884 }
1885 
1886 // AVX2 variant of av1_calc_proj_params_c.
av1_calc_proj_params_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2],const sgr_params_type * params)1887 void av1_calc_proj_params_avx2(const uint8_t *src8, int width, int height,
1888                                int src_stride, const uint8_t *dat8,
1889                                int dat_stride, int32_t *flt0, int flt0_stride,
1890                                int32_t *flt1, int flt1_stride, int64_t H[2][2],
1891                                int64_t C[2], const sgr_params_type *params) {
1892   if ((params->r[0] > 0) && (params->r[1] > 0)) {
1893     calc_proj_params_r0_r1_avx2(src8, width, height, src_stride, dat8,
1894                                 dat_stride, flt0, flt0_stride, flt1,
1895                                 flt1_stride, H, C);
1896   } else if (params->r[0] > 0) {
1897     calc_proj_params_r0_avx2(src8, width, height, src_stride, dat8, dat_stride,
1898                              flt0, flt0_stride, H, C);
1899   } else if (params->r[1] > 0) {
1900     calc_proj_params_r1_avx2(src8, width, height, src_stride, dat8, dat_stride,
1901                              flt1, flt1_stride, H, C);
1902   }
1903 }
1904 
calc_proj_params_r0_r1_high_bd_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])1905 static AOM_INLINE void calc_proj_params_r0_r1_high_bd_avx2(
1906     const uint8_t *src8, int width, int height, int src_stride,
1907     const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
1908     int32_t *flt1, int flt1_stride, int64_t H[2][2], int64_t C[2]) {
1909   const int size = width * height;
1910   const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1911   const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
1912   __m256i h00, h01, h11, c0, c1;
1913   const __m256i zero = _mm256_setzero_si256();
1914   h01 = h11 = c0 = c1 = h00 = zero;
1915 
1916   for (int i = 0; i < height; ++i) {
1917     for (int j = 0; j < width; j += 8) {
1918       const __m256i u_load = _mm256_cvtepu16_epi32(
1919           _mm_load_si128((__m128i *)(dat + i * dat_stride + j)));
1920       const __m256i s_load = _mm256_cvtepu16_epi32(
1921           _mm_load_si128((__m128i *)(src + i * src_stride + j)));
1922       __m256i f1 = _mm256_loadu_si256((__m256i *)(flt0 + i * flt0_stride + j));
1923       __m256i f2 = _mm256_loadu_si256((__m256i *)(flt1 + i * flt1_stride + j));
1924       __m256i d = _mm256_slli_epi32(u_load, SGRPROJ_RST_BITS);
1925       __m256i s = _mm256_slli_epi32(s_load, SGRPROJ_RST_BITS);
1926       s = _mm256_sub_epi32(s, d);
1927       f1 = _mm256_sub_epi32(f1, d);
1928       f2 = _mm256_sub_epi32(f2, d);
1929 
1930       const __m256i h00_even = _mm256_mul_epi32(f1, f1);
1931       const __m256i h00_odd = _mm256_mul_epi32(_mm256_srli_epi64(f1, 32),
1932                                                _mm256_srli_epi64(f1, 32));
1933       h00 = _mm256_add_epi64(h00, h00_even);
1934       h00 = _mm256_add_epi64(h00, h00_odd);
1935 
1936       const __m256i h01_even = _mm256_mul_epi32(f1, f2);
1937       const __m256i h01_odd = _mm256_mul_epi32(_mm256_srli_epi64(f1, 32),
1938                                                _mm256_srli_epi64(f2, 32));
1939       h01 = _mm256_add_epi64(h01, h01_even);
1940       h01 = _mm256_add_epi64(h01, h01_odd);
1941 
1942       const __m256i h11_even = _mm256_mul_epi32(f2, f2);
1943       const __m256i h11_odd = _mm256_mul_epi32(_mm256_srli_epi64(f2, 32),
1944                                                _mm256_srli_epi64(f2, 32));
1945       h11 = _mm256_add_epi64(h11, h11_even);
1946       h11 = _mm256_add_epi64(h11, h11_odd);
1947 
1948       const __m256i c0_even = _mm256_mul_epi32(f1, s);
1949       const __m256i c0_odd =
1950           _mm256_mul_epi32(_mm256_srli_epi64(f1, 32), _mm256_srli_epi64(s, 32));
1951       c0 = _mm256_add_epi64(c0, c0_even);
1952       c0 = _mm256_add_epi64(c0, c0_odd);
1953 
1954       const __m256i c1_even = _mm256_mul_epi32(f2, s);
1955       const __m256i c1_odd =
1956           _mm256_mul_epi32(_mm256_srli_epi64(f2, 32), _mm256_srli_epi64(s, 32));
1957       c1 = _mm256_add_epi64(c1, c1_even);
1958       c1 = _mm256_add_epi64(c1, c1_odd);
1959     }
1960   }
1961 
1962   __m256i c_low = _mm256_unpacklo_epi64(c0, c1);
1963   const __m256i c_high = _mm256_unpackhi_epi64(c0, c1);
1964   c_low = _mm256_add_epi64(c_low, c_high);
1965   const __m128i c_128bit = _mm_add_epi64(_mm256_extracti128_si256(c_low, 1),
1966                                          _mm256_castsi256_si128(c_low));
1967 
1968   __m256i h0x_low = _mm256_unpacklo_epi64(h00, h01);
1969   const __m256i h0x_high = _mm256_unpackhi_epi64(h00, h01);
1970   h0x_low = _mm256_add_epi64(h0x_low, h0x_high);
1971   const __m128i h0x_128bit = _mm_add_epi64(_mm256_extracti128_si256(h0x_low, 1),
1972                                            _mm256_castsi256_si128(h0x_low));
1973 
1974   // Using the symmetric properties of H,  calculations of H[1][0] are not
1975   // needed.
1976   __m256i h1x_low = _mm256_unpacklo_epi64(zero, h11);
1977   const __m256i h1x_high = _mm256_unpackhi_epi64(zero, h11);
1978   h1x_low = _mm256_add_epi64(h1x_low, h1x_high);
1979   const __m128i h1x_128bit = _mm_add_epi64(_mm256_extracti128_si256(h1x_low, 1),
1980                                            _mm256_castsi256_si128(h1x_low));
1981 
1982   xx_storeu_128(C, c_128bit);
1983   xx_storeu_128(H[0], h0x_128bit);
1984   xx_storeu_128(H[1], h1x_128bit);
1985 
1986   H[0][0] /= size;
1987   H[0][1] /= size;
1988   H[1][1] /= size;
1989 
1990   // Since H is a symmetric matrix
1991   H[1][0] = H[0][1];
1992   C[0] /= size;
1993   C[1] /= size;
1994 }
1995 
calc_proj_params_r0_high_bd_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int64_t H[2][2],int64_t C[2])1996 static AOM_INLINE void calc_proj_params_r0_high_bd_avx2(
1997     const uint8_t *src8, int width, int height, int src_stride,
1998     const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
1999     int64_t H[2][2], int64_t C[2]) {
2000   const int size = width * height;
2001   const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
2002   const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
2003   __m256i h00, c0;
2004   const __m256i zero = _mm256_setzero_si256();
2005   c0 = h00 = zero;
2006 
2007   for (int i = 0; i < height; ++i) {
2008     for (int j = 0; j < width; j += 8) {
2009       const __m256i u_load = _mm256_cvtepu16_epi32(
2010           _mm_load_si128((__m128i *)(dat + i * dat_stride + j)));
2011       const __m256i s_load = _mm256_cvtepu16_epi32(
2012           _mm_load_si128((__m128i *)(src + i * src_stride + j)));
2013       __m256i f1 = _mm256_loadu_si256((__m256i *)(flt0 + i * flt0_stride + j));
2014       __m256i d = _mm256_slli_epi32(u_load, SGRPROJ_RST_BITS);
2015       __m256i s = _mm256_slli_epi32(s_load, SGRPROJ_RST_BITS);
2016       s = _mm256_sub_epi32(s, d);
2017       f1 = _mm256_sub_epi32(f1, d);
2018 
2019       const __m256i h00_even = _mm256_mul_epi32(f1, f1);
2020       const __m256i h00_odd = _mm256_mul_epi32(_mm256_srli_epi64(f1, 32),
2021                                                _mm256_srli_epi64(f1, 32));
2022       h00 = _mm256_add_epi64(h00, h00_even);
2023       h00 = _mm256_add_epi64(h00, h00_odd);
2024 
2025       const __m256i c0_even = _mm256_mul_epi32(f1, s);
2026       const __m256i c0_odd =
2027           _mm256_mul_epi32(_mm256_srli_epi64(f1, 32), _mm256_srli_epi64(s, 32));
2028       c0 = _mm256_add_epi64(c0, c0_even);
2029       c0 = _mm256_add_epi64(c0, c0_odd);
2030     }
2031   }
2032   const __m128i h00_128bit = _mm_add_epi64(_mm256_extracti128_si256(h00, 1),
2033                                            _mm256_castsi256_si128(h00));
2034   const __m128i h00_val =
2035       _mm_add_epi64(h00_128bit, _mm_srli_si128(h00_128bit, 8));
2036 
2037   const __m128i c0_128bit = _mm_add_epi64(_mm256_extracti128_si256(c0, 1),
2038                                           _mm256_castsi256_si128(c0));
2039   const __m128i c0_val = _mm_add_epi64(c0_128bit, _mm_srli_si128(c0_128bit, 8));
2040 
2041   const __m128i c = _mm_unpacklo_epi64(c0_val, _mm256_castsi256_si128(zero));
2042   const __m128i h0x = _mm_unpacklo_epi64(h00_val, _mm256_castsi256_si128(zero));
2043 
2044   xx_storeu_128(C, c);
2045   xx_storeu_128(H[0], h0x);
2046 
2047   H[0][0] /= size;
2048   C[0] /= size;
2049 }
2050 
calc_proj_params_r1_high_bd_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])2051 static AOM_INLINE void calc_proj_params_r1_high_bd_avx2(
2052     const uint8_t *src8, int width, int height, int src_stride,
2053     const uint8_t *dat8, int dat_stride, int32_t *flt1, int flt1_stride,
2054     int64_t H[2][2], int64_t C[2]) {
2055   const int size = width * height;
2056   const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
2057   const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
2058   __m256i h11, c1;
2059   const __m256i zero = _mm256_setzero_si256();
2060   c1 = h11 = zero;
2061 
2062   for (int i = 0; i < height; ++i) {
2063     for (int j = 0; j < width; j += 8) {
2064       const __m256i u_load = _mm256_cvtepu16_epi32(
2065           _mm_load_si128((__m128i *)(dat + i * dat_stride + j)));
2066       const __m256i s_load = _mm256_cvtepu16_epi32(
2067           _mm_load_si128((__m128i *)(src + i * src_stride + j)));
2068       __m256i f2 = _mm256_loadu_si256((__m256i *)(flt1 + i * flt1_stride + j));
2069       __m256i d = _mm256_slli_epi32(u_load, SGRPROJ_RST_BITS);
2070       __m256i s = _mm256_slli_epi32(s_load, SGRPROJ_RST_BITS);
2071       s = _mm256_sub_epi32(s, d);
2072       f2 = _mm256_sub_epi32(f2, d);
2073 
2074       const __m256i h11_even = _mm256_mul_epi32(f2, f2);
2075       const __m256i h11_odd = _mm256_mul_epi32(_mm256_srli_epi64(f2, 32),
2076                                                _mm256_srli_epi64(f2, 32));
2077       h11 = _mm256_add_epi64(h11, h11_even);
2078       h11 = _mm256_add_epi64(h11, h11_odd);
2079 
2080       const __m256i c1_even = _mm256_mul_epi32(f2, s);
2081       const __m256i c1_odd =
2082           _mm256_mul_epi32(_mm256_srli_epi64(f2, 32), _mm256_srli_epi64(s, 32));
2083       c1 = _mm256_add_epi64(c1, c1_even);
2084       c1 = _mm256_add_epi64(c1, c1_odd);
2085     }
2086   }
2087 
2088   const __m128i h11_128bit = _mm_add_epi64(_mm256_extracti128_si256(h11, 1),
2089                                            _mm256_castsi256_si128(h11));
2090   const __m128i h11_val =
2091       _mm_add_epi64(h11_128bit, _mm_srli_si128(h11_128bit, 8));
2092 
2093   const __m128i c1_128bit = _mm_add_epi64(_mm256_extracti128_si256(c1, 1),
2094                                           _mm256_castsi256_si128(c1));
2095   const __m128i c1_val = _mm_add_epi64(c1_128bit, _mm_srli_si128(c1_128bit, 8));
2096 
2097   const __m128i c = _mm_unpacklo_epi64(_mm256_castsi256_si128(zero), c1_val);
2098   const __m128i h1x = _mm_unpacklo_epi64(_mm256_castsi256_si128(zero), h11_val);
2099 
2100   xx_storeu_128(C, c);
2101   xx_storeu_128(H[1], h1x);
2102 
2103   H[1][1] /= size;
2104   C[1] /= size;
2105 }
2106 
2107 // AVX2 variant of av1_calc_proj_params_high_bd_c.
av1_calc_proj_params_high_bd_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2],const sgr_params_type * params)2108 void av1_calc_proj_params_high_bd_avx2(const uint8_t *src8, int width,
2109                                        int height, int src_stride,
2110                                        const uint8_t *dat8, int dat_stride,
2111                                        int32_t *flt0, int flt0_stride,
2112                                        int32_t *flt1, int flt1_stride,
2113                                        int64_t H[2][2], int64_t C[2],
2114                                        const sgr_params_type *params) {
2115   if ((params->r[0] > 0) && (params->r[1] > 0)) {
2116     calc_proj_params_r0_r1_high_bd_avx2(src8, width, height, src_stride, dat8,
2117                                         dat_stride, flt0, flt0_stride, flt1,
2118                                         flt1_stride, H, C);
2119   } else if (params->r[0] > 0) {
2120     calc_proj_params_r0_high_bd_avx2(src8, width, height, src_stride, dat8,
2121                                      dat_stride, flt0, flt0_stride, H, C);
2122   } else if (params->r[1] > 0) {
2123     calc_proj_params_r1_high_bd_avx2(src8, width, height, src_stride, dat8,
2124                                      dat_stride, flt1, flt1_stride, H, C);
2125   }
2126 }
2127 
2128 #if CONFIG_AV1_HIGHBITDEPTH
av1_highbd_pixel_proj_error_avx2(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int xq[2],const sgr_params_type * params)2129 int64_t av1_highbd_pixel_proj_error_avx2(
2130     const uint8_t *src8, int width, int height, int src_stride,
2131     const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
2132     int32_t *flt1, int flt1_stride, int xq[2], const sgr_params_type *params) {
2133   int i, j, k;
2134   const int32_t shift = SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS;
2135   const __m256i rounding = _mm256_set1_epi32(1 << (shift - 1));
2136   __m256i sum64 = _mm256_setzero_si256();
2137   const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
2138   const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
2139   int64_t err = 0;
2140   if (params->r[0] > 0 && params->r[1] > 0) {  // Both filters are enabled
2141     const __m256i xq0 = _mm256_set1_epi32(xq[0]);
2142     const __m256i xq1 = _mm256_set1_epi32(xq[1]);
2143     for (i = 0; i < height; ++i) {
2144       __m256i sum32 = _mm256_setzero_si256();
2145       for (j = 0; j <= width - 16; j += 16) {  // Process 16 pixels at a time
2146         // Load 16 pixels each from source image and corrupted image
2147         const __m256i s0 = yy_loadu_256(src + j);
2148         const __m256i d0 = yy_loadu_256(dat + j);
2149         // s0 = [15 14 13 12 11 10 9 8] [7 6 5 4 3 2 1 0] as u16 (indices)
2150 
2151         // Shift-up each pixel to match filtered image scaling
2152         const __m256i u0 = _mm256_slli_epi16(d0, SGRPROJ_RST_BITS);
2153 
2154         // Split u0 into two halves and pad each from u16 to i32
2155         const __m256i u0l = _mm256_cvtepu16_epi32(_mm256_castsi256_si128(u0));
2156         const __m256i u0h =
2157             _mm256_cvtepu16_epi32(_mm256_extracti128_si256(u0, 1));
2158         // u0h, u0l = [15 14 13 12] [11 10 9 8], [7 6 5 4] [3 2 1 0] as u32
2159 
2160         // Load 16 pixels from each filtered image
2161         const __m256i flt0l = yy_loadu_256(flt0 + j);
2162         const __m256i flt0h = yy_loadu_256(flt0 + j + 8);
2163         const __m256i flt1l = yy_loadu_256(flt1 + j);
2164         const __m256i flt1h = yy_loadu_256(flt1 + j + 8);
2165         // flt?l, flt?h = [15 14 13 12] [11 10 9 8], [7 6 5 4] [3 2 1 0] as u32
2166 
2167         // Subtract shifted corrupt image from each filtered image
2168         const __m256i flt0l_subu = _mm256_sub_epi32(flt0l, u0l);
2169         const __m256i flt0h_subu = _mm256_sub_epi32(flt0h, u0h);
2170         const __m256i flt1l_subu = _mm256_sub_epi32(flt1l, u0l);
2171         const __m256i flt1h_subu = _mm256_sub_epi32(flt1h, u0h);
2172 
2173         // Multiply basis vectors by appropriate coefficients
2174         const __m256i v0l = _mm256_mullo_epi32(flt0l_subu, xq0);
2175         const __m256i v0h = _mm256_mullo_epi32(flt0h_subu, xq0);
2176         const __m256i v1l = _mm256_mullo_epi32(flt1l_subu, xq1);
2177         const __m256i v1h = _mm256_mullo_epi32(flt1h_subu, xq1);
2178 
2179         // Add together the contributions from the two basis vectors
2180         const __m256i vl = _mm256_add_epi32(v0l, v1l);
2181         const __m256i vh = _mm256_add_epi32(v0h, v1h);
2182 
2183         // Right-shift v with appropriate rounding
2184         const __m256i vrl =
2185             _mm256_srai_epi32(_mm256_add_epi32(vl, rounding), shift);
2186         const __m256i vrh =
2187             _mm256_srai_epi32(_mm256_add_epi32(vh, rounding), shift);
2188         // vrh, vrl = [15 14 13 12] [11 10 9 8], [7 6 5 4] [3 2 1 0]
2189 
2190         // Saturate each i32 to an i16 then combine both halves
2191         // The permute (control=[3 1 2 0]) fixes weird ordering from AVX lanes
2192         const __m256i vr =
2193             _mm256_permute4x64_epi64(_mm256_packs_epi32(vrl, vrh), 0xd8);
2194         // intermediate = [15 14 13 12 7 6 5 4] [11 10 9 8 3 2 1 0]
2195         // vr = [15 14 13 12 11 10 9 8] [7 6 5 4 3 2 1 0]
2196 
2197         // Add twin-subspace-sgr-filter to corrupt image then subtract source
2198         const __m256i e0 = _mm256_sub_epi16(_mm256_add_epi16(vr, d0), s0);
2199 
2200         // Calculate squared error and add adjacent values
2201         const __m256i err0 = _mm256_madd_epi16(e0, e0);
2202 
2203         sum32 = _mm256_add_epi32(sum32, err0);
2204       }
2205 
2206       const __m256i sum32l =
2207           _mm256_cvtepu32_epi64(_mm256_castsi256_si128(sum32));
2208       sum64 = _mm256_add_epi64(sum64, sum32l);
2209       const __m256i sum32h =
2210           _mm256_cvtepu32_epi64(_mm256_extracti128_si256(sum32, 1));
2211       sum64 = _mm256_add_epi64(sum64, sum32h);
2212 
2213       // Process remaining pixels in this row (modulo 16)
2214       for (k = j; k < width; ++k) {
2215         const int32_t u = (int32_t)(dat[k] << SGRPROJ_RST_BITS);
2216         int32_t v = xq[0] * (flt0[k] - u) + xq[1] * (flt1[k] - u);
2217         const int32_t e = ROUND_POWER_OF_TWO(v, shift) + dat[k] - src[k];
2218         err += ((int64_t)e * e);
2219       }
2220       dat += dat_stride;
2221       src += src_stride;
2222       flt0 += flt0_stride;
2223       flt1 += flt1_stride;
2224     }
2225   } else if (params->r[0] > 0 || params->r[1] > 0) {  // Only one filter enabled
2226     const int32_t xq_on = (params->r[0] > 0) ? xq[0] : xq[1];
2227     const __m256i xq_active = _mm256_set1_epi32(xq_on);
2228     const __m256i xq_inactive =
2229         _mm256_set1_epi32(-xq_on * (1 << SGRPROJ_RST_BITS));
2230     const int32_t *flt = (params->r[0] > 0) ? flt0 : flt1;
2231     const int flt_stride = (params->r[0] > 0) ? flt0_stride : flt1_stride;
2232     for (i = 0; i < height; ++i) {
2233       __m256i sum32 = _mm256_setzero_si256();
2234       for (j = 0; j <= width - 16; j += 16) {
2235         // Load 16 pixels from source image
2236         const __m256i s0 = yy_loadu_256(src + j);
2237         // s0 = [15 14 13 12 11 10 9 8] [7 6 5 4 3 2 1 0] as u16
2238 
2239         // Load 16 pixels from corrupted image and pad each u16 to i32
2240         const __m256i d0 = yy_loadu_256(dat + j);
2241         const __m256i d0h =
2242             _mm256_cvtepu16_epi32(_mm256_extracti128_si256(d0, 1));
2243         const __m256i d0l = _mm256_cvtepu16_epi32(_mm256_castsi256_si128(d0));
2244         // d0 = [15 14 13 12 11 10 9 8] [7 6 5 4 3 2 1 0] as u16
2245         // d0h, d0l = [15 14 13 12] [11 10 9 8], [7 6 5 4] [3 2 1 0] as i32
2246 
2247         // Load 16 pixels from the filtered image
2248         const __m256i flth = yy_loadu_256(flt + j + 8);
2249         const __m256i fltl = yy_loadu_256(flt + j);
2250         // flth, fltl = [15 14 13 12] [11 10 9 8], [7 6 5 4] [3 2 1 0] as i32
2251 
2252         const __m256i flth_xq = _mm256_mullo_epi32(flth, xq_active);
2253         const __m256i fltl_xq = _mm256_mullo_epi32(fltl, xq_active);
2254         const __m256i d0h_xq = _mm256_mullo_epi32(d0h, xq_inactive);
2255         const __m256i d0l_xq = _mm256_mullo_epi32(d0l, xq_inactive);
2256 
2257         const __m256i vh = _mm256_add_epi32(flth_xq, d0h_xq);
2258         const __m256i vl = _mm256_add_epi32(fltl_xq, d0l_xq);
2259 
2260         // Shift this down with appropriate rounding
2261         const __m256i vrh =
2262             _mm256_srai_epi32(_mm256_add_epi32(vh, rounding), shift);
2263         const __m256i vrl =
2264             _mm256_srai_epi32(_mm256_add_epi32(vl, rounding), shift);
2265         // vrh, vrl = [15 14 13 12] [11 10 9 8], [7 6 5 4] [3 2 1 0] as i32
2266 
2267         // Saturate each i32 to an i16 then combine both halves
2268         // The permute (control=[3 1 2 0]) fixes weird ordering from AVX lanes
2269         const __m256i vr =
2270             _mm256_permute4x64_epi64(_mm256_packs_epi32(vrl, vrh), 0xd8);
2271         // intermediate = [15 14 13 12 7 6 5 4] [11 10 9 8 3 2 1 0] as u16
2272         // vr = [15 14 13 12 11 10 9 8] [7 6 5 4 3 2 1 0] as u16
2273 
2274         // Subtract twin-subspace-sgr filtered from source image to get error
2275         const __m256i e0 = _mm256_sub_epi16(_mm256_add_epi16(vr, d0), s0);
2276 
2277         // Calculate squared error and add adjacent values
2278         const __m256i err0 = _mm256_madd_epi16(e0, e0);
2279 
2280         sum32 = _mm256_add_epi32(sum32, err0);
2281       }
2282 
2283       const __m256i sum32l =
2284           _mm256_cvtepu32_epi64(_mm256_castsi256_si128(sum32));
2285       sum64 = _mm256_add_epi64(sum64, sum32l);
2286       const __m256i sum32h =
2287           _mm256_cvtepu32_epi64(_mm256_extracti128_si256(sum32, 1));
2288       sum64 = _mm256_add_epi64(sum64, sum32h);
2289 
2290       // Process remaining pixels in this row (modulo 16)
2291       for (k = j; k < width; ++k) {
2292         const int32_t u = (int32_t)(dat[k] << SGRPROJ_RST_BITS);
2293         int32_t v = xq_on * (flt[k] - u);
2294         const int32_t e = ROUND_POWER_OF_TWO(v, shift) + dat[k] - src[k];
2295         err += ((int64_t)e * e);
2296       }
2297       dat += dat_stride;
2298       src += src_stride;
2299       flt += flt_stride;
2300     }
2301   } else {  // Neither filter is enabled
2302     for (i = 0; i < height; ++i) {
2303       __m256i sum32 = _mm256_setzero_si256();
2304       for (j = 0; j <= width - 32; j += 32) {
2305         // Load 2x16 u16 from source image
2306         const __m256i s0l = yy_loadu_256(src + j);
2307         const __m256i s0h = yy_loadu_256(src + j + 16);
2308 
2309         // Load 2x16 u16 from corrupted image
2310         const __m256i d0l = yy_loadu_256(dat + j);
2311         const __m256i d0h = yy_loadu_256(dat + j + 16);
2312 
2313         // Subtract corrupted image from source image
2314         const __m256i diffl = _mm256_sub_epi16(d0l, s0l);
2315         const __m256i diffh = _mm256_sub_epi16(d0h, s0h);
2316 
2317         // Square error and add adjacent values
2318         const __m256i err0l = _mm256_madd_epi16(diffl, diffl);
2319         const __m256i err0h = _mm256_madd_epi16(diffh, diffh);
2320 
2321         sum32 = _mm256_add_epi32(sum32, err0l);
2322         sum32 = _mm256_add_epi32(sum32, err0h);
2323       }
2324 
2325       const __m256i sum32l =
2326           _mm256_cvtepu32_epi64(_mm256_castsi256_si128(sum32));
2327       sum64 = _mm256_add_epi64(sum64, sum32l);
2328       const __m256i sum32h =
2329           _mm256_cvtepu32_epi64(_mm256_extracti128_si256(sum32, 1));
2330       sum64 = _mm256_add_epi64(sum64, sum32h);
2331 
2332       // Process remaining pixels (modulu 16)
2333       for (k = j; k < width; ++k) {
2334         const int32_t e = (int32_t)(dat[k]) - src[k];
2335         err += ((int64_t)e * e);
2336       }
2337       dat += dat_stride;
2338       src += src_stride;
2339     }
2340   }
2341 
2342   // Sum 4 values from sum64l and sum64h into err
2343   int64_t sum[4];
2344   yy_storeu_256(sum, sum64);
2345   err += sum[0] + sum[1] + sum[2] + sum[3];
2346   return err;
2347 }
2348 #endif  // CONFIG_AV1_HIGHBITDEPTH
2349