xref: /aosp_15_r20/external/libvpx/vp9/encoder/x86/vp9_quantize_avx2.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2017 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <assert.h>
12 #include <immintrin.h>  // AVX2
13 
14 #include "./vp9_rtcd.h"
15 #include "vpx/vpx_integer.h"
16 #include "vpx_dsp/vpx_dsp_common.h"
17 #include "vpx_dsp/x86/bitdepth_conversion_avx2.h"
18 #include "vpx_dsp/x86/quantize_sse2.h"
19 #include "vp9/common/vp9_scan.h"
20 #include "vp9/encoder/vp9_block.h"
21 
22 // Zero fill 8 positions in the output buffer.
store_zero_tran_low(tran_low_t * a)23 static VPX_FORCE_INLINE void store_zero_tran_low(tran_low_t *a) {
24   const __m256i zero = _mm256_setzero_si256();
25 #if CONFIG_VP9_HIGHBITDEPTH
26   _mm256_storeu_si256((__m256i *)(a), zero);
27   _mm256_storeu_si256((__m256i *)(a + 8), zero);
28 #else
29   _mm256_storeu_si256((__m256i *)(a), zero);
30 #endif
31 }
32 
load_fp_values_avx2(const struct macroblock_plane * mb_plane,__m256i * round,__m256i * quant,const int16_t * dequant_ptr,__m256i * dequant)33 static VPX_FORCE_INLINE void load_fp_values_avx2(
34     const struct macroblock_plane *mb_plane, __m256i *round, __m256i *quant,
35     const int16_t *dequant_ptr, __m256i *dequant) {
36   *round = _mm256_castsi128_si256(
37       _mm_load_si128((const __m128i *)mb_plane->round_fp));
38   *round = _mm256_permute4x64_epi64(*round, 0x54);
39   *quant = _mm256_castsi128_si256(
40       _mm_load_si128((const __m128i *)mb_plane->quant_fp));
41   *quant = _mm256_permute4x64_epi64(*quant, 0x54);
42   *dequant =
43       _mm256_castsi128_si256(_mm_load_si128((const __m128i *)dequant_ptr));
44   *dequant = _mm256_permute4x64_epi64(*dequant, 0x54);
45 }
46 
get_max_lane_eob(const int16_t * iscan,__m256i v_eobmax,__m256i v_mask)47 static VPX_FORCE_INLINE __m256i get_max_lane_eob(const int16_t *iscan,
48                                                  __m256i v_eobmax,
49                                                  __m256i v_mask) {
50 #if CONFIG_VP9_HIGHBITDEPTH
51   const __m256i v_iscan = _mm256_permute4x64_epi64(
52       _mm256_loadu_si256((const __m256i *)iscan), 0xD8);
53 #else
54   const __m256i v_iscan = _mm256_loadu_si256((const __m256i *)iscan);
55 #endif
56   const __m256i v_nz_iscan = _mm256_and_si256(v_iscan, v_mask);
57   return _mm256_max_epi16(v_eobmax, v_nz_iscan);
58 }
59 
get_max_eob(__m256i eob256)60 static VPX_FORCE_INLINE uint16_t get_max_eob(__m256i eob256) {
61   const __m256i eob_lo = eob256;
62   // Copy upper 128 to lower 128
63   const __m256i eob_hi = _mm256_permute2x128_si256(eob256, eob256, 0X81);
64   __m256i eob = _mm256_max_epi16(eob_lo, eob_hi);
65   __m256i eob_s = _mm256_shuffle_epi32(eob, 0xe);
66   eob = _mm256_max_epi16(eob, eob_s);
67   eob_s = _mm256_shufflelo_epi16(eob, 0xe);
68   eob = _mm256_max_epi16(eob, eob_s);
69   eob_s = _mm256_shufflelo_epi16(eob, 1);
70   eob = _mm256_max_epi16(eob, eob_s);
71 #if defined(_MSC_VER) && (_MSC_VER < 1910)
72   return _mm_cvtsi128_si32(_mm256_extracti128_si256(eob, 0)) & 0xffff;
73 #else
74   return (uint16_t)_mm256_extract_epi16(eob, 0);
75 #endif
76 }
77 
quantize_fp_16(const __m256i * round,const __m256i * quant,const __m256i * dequant,const __m256i * thr,const tran_low_t * coeff_ptr,const int16_t * iscan_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,__m256i * eob_max)78 static VPX_FORCE_INLINE void quantize_fp_16(
79     const __m256i *round, const __m256i *quant, const __m256i *dequant,
80     const __m256i *thr, const tran_low_t *coeff_ptr, const int16_t *iscan_ptr,
81     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, __m256i *eob_max) {
82   const __m256i coeff = load_tran_low(coeff_ptr);
83   const __m256i abs_coeff = _mm256_abs_epi16(coeff);
84   const int32_t nzflag =
85       _mm256_movemask_epi8(_mm256_cmpgt_epi16(abs_coeff, *thr));
86 
87   if (nzflag) {
88     const __m256i tmp_rnd = _mm256_adds_epi16(abs_coeff, *round);
89     const __m256i abs_qcoeff = _mm256_mulhi_epi16(tmp_rnd, *quant);
90     const __m256i qcoeff = _mm256_sign_epi16(abs_qcoeff, coeff);
91     const __m256i dqcoeff = _mm256_mullo_epi16(qcoeff, *dequant);
92     const __m256i nz_mask =
93         _mm256_cmpgt_epi16(abs_qcoeff, _mm256_setzero_si256());
94     store_tran_low(qcoeff, qcoeff_ptr);
95     store_tran_low(dqcoeff, dqcoeff_ptr);
96 
97     *eob_max = get_max_lane_eob(iscan_ptr, *eob_max, nz_mask);
98   } else {
99     store_zero_tran_low(qcoeff_ptr);
100     store_zero_tran_low(dqcoeff_ptr);
101   }
102 }
103 
vp9_quantize_fp_avx2(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)104 void vp9_quantize_fp_avx2(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
105                           const struct macroblock_plane *const mb_plane,
106                           tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
107                           const int16_t *dequant_ptr, uint16_t *eob_ptr,
108                           const struct ScanOrder *const scan_order) {
109   __m256i round, quant, dequant, thr;
110   __m256i eob_max = _mm256_setzero_si256();
111   const int16_t *iscan = scan_order->iscan;
112 
113   coeff_ptr += n_coeffs;
114   iscan += n_coeffs;
115   qcoeff_ptr += n_coeffs;
116   dqcoeff_ptr += n_coeffs;
117   n_coeffs = -n_coeffs;
118 
119   // Setup global values
120   load_fp_values_avx2(mb_plane, &round, &quant, dequant_ptr, &dequant);
121   thr = _mm256_setzero_si256();
122 
123   quantize_fp_16(&round, &quant, &dequant, &thr, coeff_ptr + n_coeffs,
124                  iscan + n_coeffs, qcoeff_ptr + n_coeffs,
125                  dqcoeff_ptr + n_coeffs, &eob_max);
126 
127   n_coeffs += 8 * 2;
128 
129   // remove dc constants
130   dequant = _mm256_permute2x128_si256(dequant, dequant, 0x31);
131   quant = _mm256_permute2x128_si256(quant, quant, 0x31);
132   round = _mm256_permute2x128_si256(round, round, 0x31);
133   thr = _mm256_srai_epi16(dequant, 1);
134 
135   // AC only loop
136   while (n_coeffs < 0) {
137     quantize_fp_16(&round, &quant, &dequant, &thr, coeff_ptr + n_coeffs,
138                    iscan + n_coeffs, qcoeff_ptr + n_coeffs,
139                    dqcoeff_ptr + n_coeffs, &eob_max);
140     n_coeffs += 8 * 2;
141   }
142 
143   *eob_ptr = get_max_eob(eob_max);
144 }
145 
146 // Enable this flag when matching the optimized code to
147 // vp9_quantize_fp_32x32_c(). Disabled, the optimized code will match the
148 // existing ssse3 code and quantize_fp_32x32_nz_c().
149 //
150 // #define MATCH_VP9_QUANTIZE_FP_32X32_C
151 
152 #ifndef MATCH_VP9_QUANTIZE_FP_32X32_C
quantize_fp_32x32_16_no_nzflag(const __m256i * round,const __m256i * quant,const __m256i * dequant,const __m256i * thr,const tran_low_t * coeff_ptr,const int16_t * iscan_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,__m256i * eob_max)153 static VPX_FORCE_INLINE void quantize_fp_32x32_16_no_nzflag(
154     const __m256i *round, const __m256i *quant, const __m256i *dequant,
155     const __m256i *thr, const tran_low_t *coeff_ptr, const int16_t *iscan_ptr,
156     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, __m256i *eob_max) {
157   const __m256i coeff = load_tran_low(coeff_ptr);
158   const __m256i abs_coeff = _mm256_abs_epi16(coeff);
159   const __m256i tmp_rnd = _mm256_adds_epi16(abs_coeff, *round);
160   const __m256i abs_qcoeff = _mm256_mulhi_epi16(tmp_rnd, *quant);
161   const __m256i qcoeff = _mm256_sign_epi16(abs_qcoeff, coeff);
162   const __m256i abs_dqcoeff =
163       _mm256_srli_epi16(_mm256_mullo_epi16(abs_qcoeff, *dequant), 1);
164   const __m256i dqcoeff = _mm256_sign_epi16(abs_dqcoeff, coeff);
165   const __m256i nz_mask =
166       _mm256_cmpgt_epi16(abs_qcoeff, _mm256_setzero_si256());
167   store_tran_low(qcoeff, qcoeff_ptr);
168   store_tran_low(dqcoeff, dqcoeff_ptr);
169 
170   *eob_max = get_max_lane_eob(iscan_ptr, *eob_max, nz_mask);
171   (void)thr;
172 }
173 #endif
174 
quantize_fp_32x32_16(const __m256i * round,const __m256i * quant,const __m256i * dequant,const __m256i * thr,const tran_low_t * coeff_ptr,const int16_t * iscan_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,__m256i * eob_max)175 static VPX_FORCE_INLINE void quantize_fp_32x32_16(
176     const __m256i *round, const __m256i *quant, const __m256i *dequant,
177     const __m256i *thr, const tran_low_t *coeff_ptr, const int16_t *iscan_ptr,
178     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, __m256i *eob_max) {
179   const __m256i coeff = load_tran_low(coeff_ptr);
180   const __m256i abs_coeff = _mm256_abs_epi16(coeff);
181   const __m256i thr_mask = _mm256_cmpgt_epi16(abs_coeff, *thr);
182   const int32_t nzflag = _mm256_movemask_epi8(thr_mask);
183 
184   if (nzflag) {
185 #ifdef MATCH_VP9_QUANTIZE_FP_32X32_C
186     const __m256i tmp_rnd =
187         _mm256_and_si256(_mm256_adds_epi16(abs_coeff, *round), thr_mask);
188 #else
189     const __m256i tmp_rnd = _mm256_adds_epi16(abs_coeff, *round);
190 #endif
191     const __m256i abs_qcoeff = _mm256_mulhi_epi16(tmp_rnd, *quant);
192     const __m256i qcoeff = _mm256_sign_epi16(abs_qcoeff, coeff);
193     const __m256i abs_dqcoeff =
194         _mm256_srli_epi16(_mm256_mullo_epi16(abs_qcoeff, *dequant), 1);
195     const __m256i dqcoeff = _mm256_sign_epi16(abs_dqcoeff, coeff);
196     const __m256i nz_mask =
197         _mm256_cmpgt_epi16(abs_qcoeff, _mm256_setzero_si256());
198     store_tran_low(qcoeff, qcoeff_ptr);
199     store_tran_low(dqcoeff, dqcoeff_ptr);
200 
201     *eob_max = get_max_lane_eob(iscan_ptr, *eob_max, nz_mask);
202   } else {
203     store_zero_tran_low(qcoeff_ptr);
204     store_zero_tran_low(dqcoeff_ptr);
205   }
206 }
207 
vp9_quantize_fp_32x32_avx2(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)208 void vp9_quantize_fp_32x32_avx2(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
209                                 const struct macroblock_plane *const mb_plane,
210                                 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
211                                 const int16_t *dequant_ptr, uint16_t *eob_ptr,
212                                 const struct ScanOrder *const scan_order) {
213   __m256i round, quant, dequant, thr;
214   __m256i eob_max = _mm256_setzero_si256();
215   const int16_t *iscan = scan_order->iscan;
216 
217   coeff_ptr += n_coeffs;
218   iscan += n_coeffs;
219   qcoeff_ptr += n_coeffs;
220   dqcoeff_ptr += n_coeffs;
221   n_coeffs = -n_coeffs;
222 
223   // Setup global values
224   load_fp_values_avx2(mb_plane, &round, &quant, dequant_ptr, &dequant);
225   thr = _mm256_srli_epi16(dequant, 2);
226   quant = _mm256_slli_epi16(quant, 1);
227   {
228     const __m256i rnd = _mm256_set1_epi16((int16_t)1);
229     round = _mm256_add_epi16(round, rnd);
230     round = _mm256_srai_epi16(round, 1);
231   }
232 
233 #ifdef MATCH_VP9_QUANTIZE_FP_32X32_C
234   // Subtracting 1 here eliminates a _mm256_cmpeq_epi16() instruction when
235   // calculating the zbin mask.
236   thr = _mm256_sub_epi16(thr, _mm256_set1_epi16(1));
237   quantize_fp_32x32_16(&round, &quant, &dequant, &thr, coeff_ptr + n_coeffs,
238                        iscan + n_coeffs, qcoeff_ptr + n_coeffs,
239                        dqcoeff_ptr + n_coeffs, &eob_max);
240 #else
241   quantize_fp_32x32_16_no_nzflag(
242       &round, &quant, &dequant, &thr, coeff_ptr + n_coeffs, iscan + n_coeffs,
243       qcoeff_ptr + n_coeffs, dqcoeff_ptr + n_coeffs, &eob_max);
244 #endif
245 
246   n_coeffs += 8 * 2;
247 
248   // remove dc constants
249   dequant = _mm256_permute2x128_si256(dequant, dequant, 0x31);
250   quant = _mm256_permute2x128_si256(quant, quant, 0x31);
251   round = _mm256_permute2x128_si256(round, round, 0x31);
252   thr = _mm256_permute2x128_si256(thr, thr, 0x31);
253 
254   // AC only loop
255   while (n_coeffs < 0) {
256     quantize_fp_32x32_16(&round, &quant, &dequant, &thr, coeff_ptr + n_coeffs,
257                          iscan + n_coeffs, qcoeff_ptr + n_coeffs,
258                          dqcoeff_ptr + n_coeffs, &eob_max);
259     n_coeffs += 8 * 2;
260   }
261 
262   *eob_ptr = get_max_eob(eob_max);
263 }
264 
265 #if CONFIG_VP9_HIGHBITDEPTH
mm256_mul_shift_epi32_logscale(const __m256i * x,const __m256i * y,int log_scale)266 static VPX_FORCE_INLINE __m256i mm256_mul_shift_epi32_logscale(const __m256i *x,
267                                                                const __m256i *y,
268                                                                int log_scale) {
269   __m256i prod_lo = _mm256_mul_epi32(*x, *y);
270   __m256i prod_hi = _mm256_srli_epi64(*x, 32);
271   const __m256i mult_hi = _mm256_srli_epi64(*y, 32);
272   const __m256i mask = _mm256_set_epi32(0, -1, 0, -1, 0, -1, 0, -1);
273   prod_hi = _mm256_mul_epi32(prod_hi, mult_hi);
274   prod_lo = _mm256_srli_epi64(prod_lo, 16 - log_scale);
275   prod_lo = _mm256_and_si256(prod_lo, mask);
276   prod_hi = _mm256_srli_epi64(prod_hi, 16 - log_scale);
277   prod_hi = _mm256_slli_epi64(prod_hi, 32);
278   return _mm256_or_si256(prod_lo, prod_hi);
279 }
280 
highbd_init_256(const int16_t * val_ptr)281 static VPX_FORCE_INLINE __m256i highbd_init_256(const int16_t *val_ptr) {
282   const __m128i v = _mm_load_si128((const __m128i *)val_ptr);
283   const __m128i zero = _mm_setzero_si128();
284   const __m128i dc = _mm_unpacklo_epi16(v, zero);
285   const __m128i ac = _mm_unpackhi_epi16(v, zero);
286   return _mm256_insertf128_si256(_mm256_castsi128_si256(dc), ac, 1);
287 }
288 
highbd_load_fp_values(const struct macroblock_plane * mb_plane,__m256i * round,__m256i * quant,const int16_t * dequant_ptr,__m256i * dequant)289 static VPX_FORCE_INLINE void highbd_load_fp_values(
290     const struct macroblock_plane *mb_plane, __m256i *round, __m256i *quant,
291     const int16_t *dequant_ptr, __m256i *dequant) {
292   *round = highbd_init_256(mb_plane->round_fp);
293   *quant = highbd_init_256(mb_plane->quant_fp);
294   *dequant = highbd_init_256(dequant_ptr);
295 }
296 
highbd_get_max_lane_eob(const int16_t * iscan_ptr,__m256i eobmax,__m256i nz_mask)297 static VPX_FORCE_INLINE __m256i highbd_get_max_lane_eob(
298     const int16_t *iscan_ptr, __m256i eobmax, __m256i nz_mask) {
299   const __m256i packed_nz_mask =
300       _mm256_packs_epi32(nz_mask, _mm256_setzero_si256());
301   const __m256i packed_nz_mask_perm =
302       _mm256_permute4x64_epi64(packed_nz_mask, 0xD8);
303   const __m256i iscan =
304       _mm256_castsi128_si256(_mm_loadu_si128((const __m128i *)iscan_ptr));
305   const __m256i nz_iscan = _mm256_and_si256(iscan, packed_nz_mask_perm);
306   return _mm256_max_epi16(eobmax, nz_iscan);
307 }
308 
highbd_quantize_fp(const __m256i * round,const __m256i * quant,const __m256i * dequant,const tran_low_t * coeff_ptr,const int16_t * iscan_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,__m256i * eob)309 static VPX_FORCE_INLINE void highbd_quantize_fp(
310     const __m256i *round, const __m256i *quant, const __m256i *dequant,
311     const tran_low_t *coeff_ptr, const int16_t *iscan_ptr,
312     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, __m256i *eob) {
313   const __m256i coeff = _mm256_loadu_si256((const __m256i *)coeff_ptr);
314   const __m256i abs_coeff = _mm256_abs_epi32(coeff);
315   const __m256i tmp_rnd = _mm256_add_epi32(abs_coeff, *round);
316   const __m256i abs_q = mm256_mul_shift_epi32_logscale(&tmp_rnd, quant, 0);
317   const __m256i abs_dq = _mm256_mullo_epi32(abs_q, *dequant);
318   const __m256i q = _mm256_sign_epi32(abs_q, coeff);
319   const __m256i dq = _mm256_sign_epi32(abs_dq, coeff);
320   const __m256i nz_mask = _mm256_cmpgt_epi32(abs_q, _mm256_setzero_si256());
321 
322   _mm256_storeu_si256((__m256i *)qcoeff_ptr, q);
323   _mm256_storeu_si256((__m256i *)dqcoeff_ptr, dq);
324 
325   *eob = highbd_get_max_lane_eob(iscan_ptr, *eob, nz_mask);
326 }
327 
vp9_highbd_quantize_fp_avx2(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)328 void vp9_highbd_quantize_fp_avx2(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
329                                  const struct macroblock_plane *const mb_plane,
330                                  tran_low_t *qcoeff_ptr,
331                                  tran_low_t *dqcoeff_ptr,
332                                  const int16_t *dequant_ptr, uint16_t *eob_ptr,
333                                  const struct ScanOrder *const scan_order) {
334   const int step = 8;
335   __m256i round, quant, dequant;
336   __m256i eob_max = _mm256_setzero_si256();
337   const int16_t *iscan = scan_order->iscan;
338 
339   coeff_ptr += n_coeffs;
340   iscan += n_coeffs;
341   qcoeff_ptr += n_coeffs;
342   dqcoeff_ptr += n_coeffs;
343   n_coeffs = -n_coeffs;
344 
345   // Setup global values
346   highbd_load_fp_values(mb_plane, &round, &quant, dequant_ptr, &dequant);
347 
348   highbd_quantize_fp(&round, &quant, &dequant, coeff_ptr + n_coeffs,
349                      iscan + n_coeffs, qcoeff_ptr + n_coeffs,
350                      dqcoeff_ptr + n_coeffs, &eob_max);
351 
352   n_coeffs += step;
353 
354   // remove dc constants
355   dequant = _mm256_permute2x128_si256(dequant, dequant, 0x31);
356   quant = _mm256_permute2x128_si256(quant, quant, 0x31);
357   round = _mm256_permute2x128_si256(round, round, 0x31);
358 
359   // AC only loop
360   while (n_coeffs < 0) {
361     highbd_quantize_fp(&round, &quant, &dequant, coeff_ptr + n_coeffs,
362                        iscan + n_coeffs, qcoeff_ptr + n_coeffs,
363                        dqcoeff_ptr + n_coeffs, &eob_max);
364     n_coeffs += step;
365   }
366 
367   *eob_ptr = get_max_eob(eob_max);
368 }
369 
highbd_quantize_fp_32x32(const __m256i * round,const __m256i * quant,const __m256i * dequant,const __m256i * thr,const tran_low_t * coeff_ptr,const int16_t * iscan_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,__m256i * eob)370 static VPX_FORCE_INLINE void highbd_quantize_fp_32x32(
371     const __m256i *round, const __m256i *quant, const __m256i *dequant,
372     const __m256i *thr, const tran_low_t *coeff_ptr, const int16_t *iscan_ptr,
373     tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, __m256i *eob) {
374   const __m256i coeff = _mm256_loadu_si256((const __m256i *)coeff_ptr);
375   const __m256i abs_coeff = _mm256_abs_epi32(coeff);
376   const __m256i thr_mask = _mm256_cmpgt_epi32(abs_coeff, *thr);
377   const __m256i tmp_rnd =
378       _mm256_and_si256(_mm256_add_epi32(abs_coeff, *round), thr_mask);
379   const __m256i abs_q = mm256_mul_shift_epi32_logscale(&tmp_rnd, quant, 0);
380   const __m256i abs_dq =
381       _mm256_srli_epi32(_mm256_mullo_epi32(abs_q, *dequant), 1);
382   const __m256i q = _mm256_sign_epi32(abs_q, coeff);
383   const __m256i dq = _mm256_sign_epi32(abs_dq, coeff);
384   const __m256i nz_mask = _mm256_cmpgt_epi32(abs_q, _mm256_setzero_si256());
385 
386   _mm256_storeu_si256((__m256i *)qcoeff_ptr, q);
387   _mm256_storeu_si256((__m256i *)dqcoeff_ptr, dq);
388 
389   *eob = highbd_get_max_lane_eob(iscan_ptr, *eob, nz_mask);
390 }
391 
vp9_highbd_quantize_fp_32x32_avx2(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)392 void vp9_highbd_quantize_fp_32x32_avx2(
393     const tran_low_t *coeff_ptr, intptr_t n_coeffs,
394     const struct macroblock_plane *const mb_plane, tran_low_t *qcoeff_ptr,
395     tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
396     const struct ScanOrder *const scan_order) {
397   const int step = 8;
398   __m256i round, quant, dequant, thr;
399   __m256i eob_max = _mm256_setzero_si256();
400   const int16_t *iscan = scan_order->iscan;
401 
402   coeff_ptr += n_coeffs;
403   iscan += n_coeffs;
404   qcoeff_ptr += n_coeffs;
405   dqcoeff_ptr += n_coeffs;
406   n_coeffs = -n_coeffs;
407 
408   // Setup global values
409   highbd_load_fp_values(mb_plane, &round, &quant, dequant_ptr, &dequant);
410   thr = _mm256_srli_epi32(dequant, 2);
411   // Subtracting 1 here eliminates a _mm256_cmpeq_epi32() instruction when
412   // calculating the zbin mask.
413   thr = _mm256_sub_epi32(thr, _mm256_set1_epi32(1));
414   quant = _mm256_slli_epi32(quant, 1);
415   round = _mm256_srai_epi32(_mm256_add_epi32(round, _mm256_set1_epi32(1)), 1);
416 
417   highbd_quantize_fp_32x32(&round, &quant, &dequant, &thr, coeff_ptr + n_coeffs,
418                            iscan + n_coeffs, qcoeff_ptr + n_coeffs,
419                            dqcoeff_ptr + n_coeffs, &eob_max);
420 
421   n_coeffs += step;
422 
423   // remove dc constants
424   dequant = _mm256_permute2x128_si256(dequant, dequant, 0x31);
425   quant = _mm256_permute2x128_si256(quant, quant, 0x31);
426   round = _mm256_permute2x128_si256(round, round, 0x31);
427   thr = _mm256_permute2x128_si256(thr, thr, 0x31);
428 
429   // AC only loop
430   while (n_coeffs < 0) {
431     highbd_quantize_fp_32x32(
432         &round, &quant, &dequant, &thr, coeff_ptr + n_coeffs, iscan + n_coeffs,
433         qcoeff_ptr + n_coeffs, dqcoeff_ptr + n_coeffs, &eob_max);
434     n_coeffs += step;
435   }
436 
437   *eob_ptr = get_max_eob(eob_max);
438 }
439 #endif  // CONFIG_VP9_HIGHBITDEPTH
440