1 /*
2  * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <immintrin.h>
13 
14 #include "config/aom_dsp_rtcd.h"
15 
16 #include "aom/aom_integer.h"
17 
init_one_qp(const __m128i * p,__m256i * qp)18 static INLINE void init_one_qp(const __m128i *p, __m256i *qp) {
19   const __m128i sign = _mm_srai_epi16(*p, 15);
20   const __m128i dc = _mm_unpacklo_epi16(*p, sign);
21   const __m128i ac = _mm_unpackhi_epi16(*p, sign);
22   *qp = _mm256_insertf128_si256(_mm256_castsi128_si256(dc), ac, 1);
23 }
24 
update_qp(__m256i * qp)25 static INLINE void update_qp(__m256i *qp) {
26   int i;
27   for (i = 0; i < 5; ++i) {
28     qp[i] = _mm256_permute2x128_si256(qp[i], qp[i], 0x11);
29   }
30 }
31 
init_qp(const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * dequant_ptr,const int16_t * quant_shift_ptr,__m256i * qp)32 static INLINE void init_qp(const int16_t *zbin_ptr, const int16_t *round_ptr,
33                            const int16_t *quant_ptr, const int16_t *dequant_ptr,
34                            const int16_t *quant_shift_ptr, __m256i *qp) {
35   const __m128i zbin = _mm_loadu_si128((const __m128i *)zbin_ptr);
36   const __m128i round = _mm_loadu_si128((const __m128i *)round_ptr);
37   const __m128i quant = _mm_loadu_si128((const __m128i *)quant_ptr);
38   const __m128i dequant = _mm_loadu_si128((const __m128i *)dequant_ptr);
39   const __m128i quant_shift = _mm_loadu_si128((const __m128i *)quant_shift_ptr);
40   init_one_qp(&zbin, &qp[0]);
41   init_one_qp(&round, &qp[1]);
42   init_one_qp(&quant, &qp[2]);
43   init_one_qp(&dequant, &qp[3]);
44   init_one_qp(&quant_shift, &qp[4]);
45 }
46 
47 // Note:
48 // *x is vector multiplied by *y which is 16 int32_t parallel multiplication
49 // and right shift 16.  The output, 16 int32_t is save in *p.
mm256_mul_shift_epi32(const __m256i * x,const __m256i * y,__m256i * p)50 static INLINE void mm256_mul_shift_epi32(const __m256i *x, const __m256i *y,
51                                          __m256i *p) {
52   __m256i prod_lo = _mm256_mul_epi32(*x, *y);
53   __m256i prod_hi = _mm256_srli_epi64(*x, 32);
54   const __m256i mult_hi = _mm256_srli_epi64(*y, 32);
55   prod_hi = _mm256_mul_epi32(prod_hi, mult_hi);
56 
57   prod_lo = _mm256_srli_epi64(prod_lo, 16);
58   const __m256i mask = _mm256_set_epi32(0, -1, 0, -1, 0, -1, 0, -1);
59   prod_lo = _mm256_and_si256(prod_lo, mask);
60   prod_hi = _mm256_srli_epi64(prod_hi, 16);
61 
62   prod_hi = _mm256_slli_epi64(prod_hi, 32);
63   *p = _mm256_or_si256(prod_lo, prod_hi);
64 }
65 
quantize(const __m256i * qp,__m256i * c,const int16_t * iscan_ptr,tran_low_t * qcoeff,tran_low_t * dqcoeff,__m256i * eob)66 static INLINE void quantize(const __m256i *qp, __m256i *c,
67                             const int16_t *iscan_ptr, tran_low_t *qcoeff,
68                             tran_low_t *dqcoeff, __m256i *eob) {
69   const __m256i abs = _mm256_abs_epi32(*c);
70   const __m256i flag1 = _mm256_cmpgt_epi32(abs, qp[0]);
71   __m256i flag2 = _mm256_cmpeq_epi32(abs, qp[0]);
72   flag2 = _mm256_or_si256(flag1, flag2);
73   const int32_t nzflag = _mm256_movemask_epi8(flag2);
74 
75   if (LIKELY(nzflag)) {
76     __m256i q = _mm256_add_epi32(abs, qp[1]);
77     __m256i tmp;
78     mm256_mul_shift_epi32(&q, &qp[2], &tmp);
79     q = _mm256_add_epi32(tmp, q);
80 
81     mm256_mul_shift_epi32(&q, &qp[4], &q);
82     __m256i dq = _mm256_mullo_epi32(q, qp[3]);
83 
84     q = _mm256_sign_epi32(q, *c);
85     dq = _mm256_sign_epi32(dq, *c);
86     q = _mm256_and_si256(q, flag2);
87     dq = _mm256_and_si256(dq, flag2);
88 
89     _mm256_storeu_si256((__m256i *)qcoeff, q);
90     _mm256_storeu_si256((__m256i *)dqcoeff, dq);
91 
92     const __m128i isc = _mm_loadu_si128((const __m128i *)iscan_ptr);
93     const __m128i zr = _mm_setzero_si128();
94     const __m128i lo = _mm_unpacklo_epi16(isc, zr);
95     const __m128i hi = _mm_unpackhi_epi16(isc, zr);
96     const __m256i iscan =
97         _mm256_insertf128_si256(_mm256_castsi128_si256(lo), hi, 1);
98 
99     const __m256i zero = _mm256_setzero_si256();
100     const __m256i zc = _mm256_cmpeq_epi32(dq, zero);
101     const __m256i nz = _mm256_cmpeq_epi32(zc, zero);
102     __m256i cur_eob = _mm256_sub_epi32(iscan, nz);
103     cur_eob = _mm256_and_si256(cur_eob, nz);
104     *eob = _mm256_max_epi32(cur_eob, *eob);
105   } else {
106     const __m256i zero = _mm256_setzero_si256();
107     _mm256_storeu_si256((__m256i *)qcoeff, zero);
108     _mm256_storeu_si256((__m256i *)dqcoeff, zero);
109   }
110 }
111 
aom_highbd_quantize_b_avx2(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * quant_shift_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan)112 void aom_highbd_quantize_b_avx2(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
113                                 const int16_t *zbin_ptr,
114                                 const int16_t *round_ptr,
115                                 const int16_t *quant_ptr,
116                                 const int16_t *quant_shift_ptr,
117                                 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
118                                 const int16_t *dequant_ptr, uint16_t *eob_ptr,
119                                 const int16_t *scan, const int16_t *iscan) {
120   (void)scan;
121   const unsigned int step = 8;
122 
123   __m256i qp[5], coeff;
124   init_qp(zbin_ptr, round_ptr, quant_ptr, dequant_ptr, quant_shift_ptr, qp);
125   coeff = _mm256_loadu_si256((const __m256i *)coeff_ptr);
126 
127   __m256i eob = _mm256_setzero_si256();
128   quantize(qp, &coeff, iscan, qcoeff_ptr, dqcoeff_ptr, &eob);
129 
130   coeff_ptr += step;
131   qcoeff_ptr += step;
132   dqcoeff_ptr += step;
133   iscan += step;
134   n_coeffs -= step;
135 
136   update_qp(qp);
137 
138   while (n_coeffs > 0) {
139     coeff = _mm256_loadu_si256((const __m256i *)coeff_ptr);
140     quantize(qp, &coeff, iscan, qcoeff_ptr, dqcoeff_ptr, &eob);
141 
142     coeff_ptr += step;
143     qcoeff_ptr += step;
144     dqcoeff_ptr += step;
145     iscan += step;
146     n_coeffs -= step;
147   }
148   {
149     __m256i eob_s;
150     eob_s = _mm256_shuffle_epi32(eob, 0xe);
151     eob = _mm256_max_epi16(eob, eob_s);
152     eob_s = _mm256_shufflelo_epi16(eob, 0xe);
153     eob = _mm256_max_epi16(eob, eob_s);
154     eob_s = _mm256_shufflelo_epi16(eob, 1);
155     eob = _mm256_max_epi16(eob, eob_s);
156     const __m128i final_eob = _mm_max_epi16(_mm256_castsi256_si128(eob),
157                                             _mm256_extractf128_si256(eob, 1));
158     *eob_ptr = _mm_extract_epi16(final_eob, 0);
159   }
160 }
161