1 /*
2  *  Copyright (c) 2014 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 #if defined(_MSC_VER) && _MSC_VER <= 1500
13 // Need to include math.h before calling tmmintrin.h/intrin.h
14 // in certain versions of MSVS.
15 #include <math.h>
16 #endif
17 #include <tmmintrin.h>  // SSSE3
18 
19 #include "./vp9_rtcd.h"
20 #include "vpx_dsp/x86/inv_txfm_sse2.h"
21 #include "vpx_dsp/x86/txfm_common_sse2.h"
22 
vp9_fdct8x8_quant_ssse3(const int16_t * input,int stride,int16_t * coeff_ptr,intptr_t n_coeffs,int skip_block,const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * quant_shift_ptr,int16_t * qcoeff_ptr,int16_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan_ptr,const int16_t * iscan_ptr)23 void vp9_fdct8x8_quant_ssse3(const int16_t *input, int stride,
24                              int16_t* coeff_ptr, intptr_t n_coeffs,
25                              int skip_block, const int16_t* zbin_ptr,
26                              const int16_t* round_ptr, const int16_t* quant_ptr,
27                              const int16_t* quant_shift_ptr,
28                              int16_t* qcoeff_ptr,
29                              int16_t* dqcoeff_ptr, const int16_t* dequant_ptr,
30                              uint16_t* eob_ptr,
31                              const int16_t* scan_ptr,
32                              const int16_t* iscan_ptr) {
33   __m128i zero;
34   int pass;
35   // Constants
36   //    When we use them, in one case, they are all the same. In all others
37   //    it's a pair of them that we need to repeat four times. This is done
38   //    by constructing the 32 bit constant corresponding to that pair.
39   const __m128i k__dual_p16_p16 = dual_set_epi16(23170, 23170);
40   const __m128i k__cospi_p16_p16 = _mm_set1_epi16((int16_t)cospi_16_64);
41   const __m128i k__cospi_p16_m16 = pair_set_epi16(cospi_16_64, -cospi_16_64);
42   const __m128i k__cospi_p24_p08 = pair_set_epi16(cospi_24_64, cospi_8_64);
43   const __m128i k__cospi_m08_p24 = pair_set_epi16(-cospi_8_64, cospi_24_64);
44   const __m128i k__cospi_p28_p04 = pair_set_epi16(cospi_28_64, cospi_4_64);
45   const __m128i k__cospi_m04_p28 = pair_set_epi16(-cospi_4_64, cospi_28_64);
46   const __m128i k__cospi_p12_p20 = pair_set_epi16(cospi_12_64, cospi_20_64);
47   const __m128i k__cospi_m20_p12 = pair_set_epi16(-cospi_20_64, cospi_12_64);
48   const __m128i k__DCT_CONST_ROUNDING = _mm_set1_epi32(DCT_CONST_ROUNDING);
49   // Load input
50   __m128i in0  = _mm_load_si128((const __m128i *)(input + 0 * stride));
51   __m128i in1  = _mm_load_si128((const __m128i *)(input + 1 * stride));
52   __m128i in2  = _mm_load_si128((const __m128i *)(input + 2 * stride));
53   __m128i in3  = _mm_load_si128((const __m128i *)(input + 3 * stride));
54   __m128i in4  = _mm_load_si128((const __m128i *)(input + 4 * stride));
55   __m128i in5  = _mm_load_si128((const __m128i *)(input + 5 * stride));
56   __m128i in6  = _mm_load_si128((const __m128i *)(input + 6 * stride));
57   __m128i in7  = _mm_load_si128((const __m128i *)(input + 7 * stride));
58   __m128i *in[8];
59   int index = 0;
60 
61   (void)scan_ptr;
62   (void)zbin_ptr;
63   (void)quant_shift_ptr;
64   (void)coeff_ptr;
65 
66   // Pre-condition input (shift by two)
67   in0 = _mm_slli_epi16(in0, 2);
68   in1 = _mm_slli_epi16(in1, 2);
69   in2 = _mm_slli_epi16(in2, 2);
70   in3 = _mm_slli_epi16(in3, 2);
71   in4 = _mm_slli_epi16(in4, 2);
72   in5 = _mm_slli_epi16(in5, 2);
73   in6 = _mm_slli_epi16(in6, 2);
74   in7 = _mm_slli_epi16(in7, 2);
75 
76   in[0] = &in0;
77   in[1] = &in1;
78   in[2] = &in2;
79   in[3] = &in3;
80   in[4] = &in4;
81   in[5] = &in5;
82   in[6] = &in6;
83   in[7] = &in7;
84 
85   // We do two passes, first the columns, then the rows. The results of the
86   // first pass are transposed so that the same column code can be reused. The
87   // results of the second pass are also transposed so that the rows (processed
88   // as columns) are put back in row positions.
89   for (pass = 0; pass < 2; pass++) {
90     // To store results of each pass before the transpose.
91     __m128i res0, res1, res2, res3, res4, res5, res6, res7;
92     // Add/subtract
93     const __m128i q0 = _mm_add_epi16(in0, in7);
94     const __m128i q1 = _mm_add_epi16(in1, in6);
95     const __m128i q2 = _mm_add_epi16(in2, in5);
96     const __m128i q3 = _mm_add_epi16(in3, in4);
97     const __m128i q4 = _mm_sub_epi16(in3, in4);
98     const __m128i q5 = _mm_sub_epi16(in2, in5);
99     const __m128i q6 = _mm_sub_epi16(in1, in6);
100     const __m128i q7 = _mm_sub_epi16(in0, in7);
101     // Work on first four results
102     {
103       // Add/subtract
104       const __m128i r0 = _mm_add_epi16(q0, q3);
105       const __m128i r1 = _mm_add_epi16(q1, q2);
106       const __m128i r2 = _mm_sub_epi16(q1, q2);
107       const __m128i r3 = _mm_sub_epi16(q0, q3);
108       // Interleave to do the multiply by constants which gets us into 32bits
109       const __m128i t0 = _mm_unpacklo_epi16(r0, r1);
110       const __m128i t1 = _mm_unpackhi_epi16(r0, r1);
111       const __m128i t2 = _mm_unpacklo_epi16(r2, r3);
112       const __m128i t3 = _mm_unpackhi_epi16(r2, r3);
113 
114       const __m128i u0 = _mm_madd_epi16(t0, k__cospi_p16_p16);
115       const __m128i u1 = _mm_madd_epi16(t1, k__cospi_p16_p16);
116       const __m128i u2 = _mm_madd_epi16(t0, k__cospi_p16_m16);
117       const __m128i u3 = _mm_madd_epi16(t1, k__cospi_p16_m16);
118 
119       const __m128i u4 = _mm_madd_epi16(t2, k__cospi_p24_p08);
120       const __m128i u5 = _mm_madd_epi16(t3, k__cospi_p24_p08);
121       const __m128i u6 = _mm_madd_epi16(t2, k__cospi_m08_p24);
122       const __m128i u7 = _mm_madd_epi16(t3, k__cospi_m08_p24);
123       // dct_const_round_shift
124 
125       const __m128i v0 = _mm_add_epi32(u0, k__DCT_CONST_ROUNDING);
126       const __m128i v1 = _mm_add_epi32(u1, k__DCT_CONST_ROUNDING);
127       const __m128i v2 = _mm_add_epi32(u2, k__DCT_CONST_ROUNDING);
128       const __m128i v3 = _mm_add_epi32(u3, k__DCT_CONST_ROUNDING);
129 
130       const __m128i v4 = _mm_add_epi32(u4, k__DCT_CONST_ROUNDING);
131       const __m128i v5 = _mm_add_epi32(u5, k__DCT_CONST_ROUNDING);
132       const __m128i v6 = _mm_add_epi32(u6, k__DCT_CONST_ROUNDING);
133       const __m128i v7 = _mm_add_epi32(u7, k__DCT_CONST_ROUNDING);
134 
135       const __m128i w0 = _mm_srai_epi32(v0, DCT_CONST_BITS);
136       const __m128i w1 = _mm_srai_epi32(v1, DCT_CONST_BITS);
137       const __m128i w2 = _mm_srai_epi32(v2, DCT_CONST_BITS);
138       const __m128i w3 = _mm_srai_epi32(v3, DCT_CONST_BITS);
139 
140       const __m128i w4 = _mm_srai_epi32(v4, DCT_CONST_BITS);
141       const __m128i w5 = _mm_srai_epi32(v5, DCT_CONST_BITS);
142       const __m128i w6 = _mm_srai_epi32(v6, DCT_CONST_BITS);
143       const __m128i w7 = _mm_srai_epi32(v7, DCT_CONST_BITS);
144       // Combine
145 
146       res0 = _mm_packs_epi32(w0, w1);
147       res4 = _mm_packs_epi32(w2, w3);
148       res2 = _mm_packs_epi32(w4, w5);
149       res6 = _mm_packs_epi32(w6, w7);
150     }
151     // Work on next four results
152     {
153       // Interleave to do the multiply by constants which gets us into 32bits
154       const __m128i d0 = _mm_sub_epi16(q6, q5);
155       const __m128i d1 = _mm_add_epi16(q6, q5);
156       const __m128i r0 = _mm_mulhrs_epi16(d0, k__dual_p16_p16);
157       const __m128i r1 = _mm_mulhrs_epi16(d1, k__dual_p16_p16);
158 
159       // Add/subtract
160       const __m128i x0 = _mm_add_epi16(q4, r0);
161       const __m128i x1 = _mm_sub_epi16(q4, r0);
162       const __m128i x2 = _mm_sub_epi16(q7, r1);
163       const __m128i x3 = _mm_add_epi16(q7, r1);
164       // Interleave to do the multiply by constants which gets us into 32bits
165       const __m128i t0 = _mm_unpacklo_epi16(x0, x3);
166       const __m128i t1 = _mm_unpackhi_epi16(x0, x3);
167       const __m128i t2 = _mm_unpacklo_epi16(x1, x2);
168       const __m128i t3 = _mm_unpackhi_epi16(x1, x2);
169       const __m128i u0 = _mm_madd_epi16(t0, k__cospi_p28_p04);
170       const __m128i u1 = _mm_madd_epi16(t1, k__cospi_p28_p04);
171       const __m128i u2 = _mm_madd_epi16(t0, k__cospi_m04_p28);
172       const __m128i u3 = _mm_madd_epi16(t1, k__cospi_m04_p28);
173       const __m128i u4 = _mm_madd_epi16(t2, k__cospi_p12_p20);
174       const __m128i u5 = _mm_madd_epi16(t3, k__cospi_p12_p20);
175       const __m128i u6 = _mm_madd_epi16(t2, k__cospi_m20_p12);
176       const __m128i u7 = _mm_madd_epi16(t3, k__cospi_m20_p12);
177       // dct_const_round_shift
178       const __m128i v0 = _mm_add_epi32(u0, k__DCT_CONST_ROUNDING);
179       const __m128i v1 = _mm_add_epi32(u1, k__DCT_CONST_ROUNDING);
180       const __m128i v2 = _mm_add_epi32(u2, k__DCT_CONST_ROUNDING);
181       const __m128i v3 = _mm_add_epi32(u3, k__DCT_CONST_ROUNDING);
182       const __m128i v4 = _mm_add_epi32(u4, k__DCT_CONST_ROUNDING);
183       const __m128i v5 = _mm_add_epi32(u5, k__DCT_CONST_ROUNDING);
184       const __m128i v6 = _mm_add_epi32(u6, k__DCT_CONST_ROUNDING);
185       const __m128i v7 = _mm_add_epi32(u7, k__DCT_CONST_ROUNDING);
186       const __m128i w0 = _mm_srai_epi32(v0, DCT_CONST_BITS);
187       const __m128i w1 = _mm_srai_epi32(v1, DCT_CONST_BITS);
188       const __m128i w2 = _mm_srai_epi32(v2, DCT_CONST_BITS);
189       const __m128i w3 = _mm_srai_epi32(v3, DCT_CONST_BITS);
190       const __m128i w4 = _mm_srai_epi32(v4, DCT_CONST_BITS);
191       const __m128i w5 = _mm_srai_epi32(v5, DCT_CONST_BITS);
192       const __m128i w6 = _mm_srai_epi32(v6, DCT_CONST_BITS);
193       const __m128i w7 = _mm_srai_epi32(v7, DCT_CONST_BITS);
194       // Combine
195       res1 = _mm_packs_epi32(w0, w1);
196       res7 = _mm_packs_epi32(w2, w3);
197       res5 = _mm_packs_epi32(w4, w5);
198       res3 = _mm_packs_epi32(w6, w7);
199     }
200     // Transpose the 8x8.
201     {
202       // 00 01 02 03 04 05 06 07
203       // 10 11 12 13 14 15 16 17
204       // 20 21 22 23 24 25 26 27
205       // 30 31 32 33 34 35 36 37
206       // 40 41 42 43 44 45 46 47
207       // 50 51 52 53 54 55 56 57
208       // 60 61 62 63 64 65 66 67
209       // 70 71 72 73 74 75 76 77
210       const __m128i tr0_0 = _mm_unpacklo_epi16(res0, res1);
211       const __m128i tr0_1 = _mm_unpacklo_epi16(res2, res3);
212       const __m128i tr0_2 = _mm_unpackhi_epi16(res0, res1);
213       const __m128i tr0_3 = _mm_unpackhi_epi16(res2, res3);
214       const __m128i tr0_4 = _mm_unpacklo_epi16(res4, res5);
215       const __m128i tr0_5 = _mm_unpacklo_epi16(res6, res7);
216       const __m128i tr0_6 = _mm_unpackhi_epi16(res4, res5);
217       const __m128i tr0_7 = _mm_unpackhi_epi16(res6, res7);
218       // 00 10 01 11 02 12 03 13
219       // 20 30 21 31 22 32 23 33
220       // 04 14 05 15 06 16 07 17
221       // 24 34 25 35 26 36 27 37
222       // 40 50 41 51 42 52 43 53
223       // 60 70 61 71 62 72 63 73
224       // 54 54 55 55 56 56 57 57
225       // 64 74 65 75 66 76 67 77
226       const __m128i tr1_0 = _mm_unpacklo_epi32(tr0_0, tr0_1);
227       const __m128i tr1_1 = _mm_unpacklo_epi32(tr0_2, tr0_3);
228       const __m128i tr1_2 = _mm_unpackhi_epi32(tr0_0, tr0_1);
229       const __m128i tr1_3 = _mm_unpackhi_epi32(tr0_2, tr0_3);
230       const __m128i tr1_4 = _mm_unpacklo_epi32(tr0_4, tr0_5);
231       const __m128i tr1_5 = _mm_unpacklo_epi32(tr0_6, tr0_7);
232       const __m128i tr1_6 = _mm_unpackhi_epi32(tr0_4, tr0_5);
233       const __m128i tr1_7 = _mm_unpackhi_epi32(tr0_6, tr0_7);
234       // 00 10 20 30 01 11 21 31
235       // 40 50 60 70 41 51 61 71
236       // 02 12 22 32 03 13 23 33
237       // 42 52 62 72 43 53 63 73
238       // 04 14 24 34 05 15 21 36
239       // 44 54 64 74 45 55 61 76
240       // 06 16 26 36 07 17 27 37
241       // 46 56 66 76 47 57 67 77
242       in0 = _mm_unpacklo_epi64(tr1_0, tr1_4);
243       in1 = _mm_unpackhi_epi64(tr1_0, tr1_4);
244       in2 = _mm_unpacklo_epi64(tr1_2, tr1_6);
245       in3 = _mm_unpackhi_epi64(tr1_2, tr1_6);
246       in4 = _mm_unpacklo_epi64(tr1_1, tr1_5);
247       in5 = _mm_unpackhi_epi64(tr1_1, tr1_5);
248       in6 = _mm_unpacklo_epi64(tr1_3, tr1_7);
249       in7 = _mm_unpackhi_epi64(tr1_3, tr1_7);
250       // 00 10 20 30 40 50 60 70
251       // 01 11 21 31 41 51 61 71
252       // 02 12 22 32 42 52 62 72
253       // 03 13 23 33 43 53 63 73
254       // 04 14 24 34 44 54 64 74
255       // 05 15 25 35 45 55 65 75
256       // 06 16 26 36 46 56 66 76
257       // 07 17 27 37 47 57 67 77
258     }
259   }
260   // Post-condition output and store it
261   {
262     // Post-condition (division by two)
263     //    division of two 16 bits signed numbers using shifts
264     //    n / 2 = (n - (n >> 15)) >> 1
265     const __m128i sign_in0 = _mm_srai_epi16(in0, 15);
266     const __m128i sign_in1 = _mm_srai_epi16(in1, 15);
267     const __m128i sign_in2 = _mm_srai_epi16(in2, 15);
268     const __m128i sign_in3 = _mm_srai_epi16(in3, 15);
269     const __m128i sign_in4 = _mm_srai_epi16(in4, 15);
270     const __m128i sign_in5 = _mm_srai_epi16(in5, 15);
271     const __m128i sign_in6 = _mm_srai_epi16(in6, 15);
272     const __m128i sign_in7 = _mm_srai_epi16(in7, 15);
273     in0 = _mm_sub_epi16(in0, sign_in0);
274     in1 = _mm_sub_epi16(in1, sign_in1);
275     in2 = _mm_sub_epi16(in2, sign_in2);
276     in3 = _mm_sub_epi16(in3, sign_in3);
277     in4 = _mm_sub_epi16(in4, sign_in4);
278     in5 = _mm_sub_epi16(in5, sign_in5);
279     in6 = _mm_sub_epi16(in6, sign_in6);
280     in7 = _mm_sub_epi16(in7, sign_in7);
281     in0 = _mm_srai_epi16(in0, 1);
282     in1 = _mm_srai_epi16(in1, 1);
283     in2 = _mm_srai_epi16(in2, 1);
284     in3 = _mm_srai_epi16(in3, 1);
285     in4 = _mm_srai_epi16(in4, 1);
286     in5 = _mm_srai_epi16(in5, 1);
287     in6 = _mm_srai_epi16(in6, 1);
288     in7 = _mm_srai_epi16(in7, 1);
289   }
290 
291   iscan_ptr += n_coeffs;
292   qcoeff_ptr += n_coeffs;
293   dqcoeff_ptr += n_coeffs;
294   n_coeffs = -n_coeffs;
295   zero = _mm_setzero_si128();
296 
297   if (!skip_block) {
298     __m128i eob;
299     __m128i round, quant, dequant, thr;
300     int16_t nzflag;
301     {
302       __m128i coeff0, coeff1;
303 
304       // Setup global values
305       {
306         round = _mm_load_si128((const __m128i*)round_ptr);
307         quant = _mm_load_si128((const __m128i*)quant_ptr);
308         dequant = _mm_load_si128((const __m128i*)dequant_ptr);
309       }
310 
311       {
312         __m128i coeff0_sign, coeff1_sign;
313         __m128i qcoeff0, qcoeff1;
314         __m128i qtmp0, qtmp1;
315         // Do DC and first 15 AC
316         coeff0 = *in[0];
317         coeff1 = *in[1];
318 
319         // Poor man's sign extract
320         coeff0_sign = _mm_srai_epi16(coeff0, 15);
321         coeff1_sign = _mm_srai_epi16(coeff1, 15);
322         qcoeff0 = _mm_xor_si128(coeff0, coeff0_sign);
323         qcoeff1 = _mm_xor_si128(coeff1, coeff1_sign);
324         qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
325         qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
326 
327         qcoeff0 = _mm_adds_epi16(qcoeff0, round);
328         round = _mm_unpackhi_epi64(round, round);
329         qcoeff1 = _mm_adds_epi16(qcoeff1, round);
330         qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
331         quant = _mm_unpackhi_epi64(quant, quant);
332         qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
333 
334         // Reinsert signs
335         qcoeff0 = _mm_xor_si128(qtmp0, coeff0_sign);
336         qcoeff1 = _mm_xor_si128(qtmp1, coeff1_sign);
337         qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
338         qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
339 
340         _mm_store_si128((__m128i*)(qcoeff_ptr + n_coeffs), qcoeff0);
341         _mm_store_si128((__m128i*)(qcoeff_ptr + n_coeffs) + 1, qcoeff1);
342 
343         coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
344         dequant = _mm_unpackhi_epi64(dequant, dequant);
345         coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
346 
347         _mm_store_si128((__m128i*)(dqcoeff_ptr + n_coeffs), coeff0);
348         _mm_store_si128((__m128i*)(dqcoeff_ptr + n_coeffs) + 1, coeff1);
349       }
350 
351       {
352         // Scan for eob
353         __m128i zero_coeff0, zero_coeff1;
354         __m128i nzero_coeff0, nzero_coeff1;
355         __m128i iscan0, iscan1;
356         __m128i eob1;
357         zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
358         zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
359         nzero_coeff0 = _mm_cmpeq_epi16(zero_coeff0, zero);
360         nzero_coeff1 = _mm_cmpeq_epi16(zero_coeff1, zero);
361         iscan0 = _mm_load_si128((const __m128i*)(iscan_ptr + n_coeffs));
362         iscan1 = _mm_load_si128((const __m128i*)(iscan_ptr + n_coeffs) + 1);
363         // Add one to convert from indices to counts
364         iscan0 = _mm_sub_epi16(iscan0, nzero_coeff0);
365         iscan1 = _mm_sub_epi16(iscan1, nzero_coeff1);
366         eob = _mm_and_si128(iscan0, nzero_coeff0);
367         eob1 = _mm_and_si128(iscan1, nzero_coeff1);
368         eob = _mm_max_epi16(eob, eob1);
369       }
370       n_coeffs += 8 * 2;
371     }
372 
373     // AC only loop
374     index = 2;
375     thr = _mm_srai_epi16(dequant, 1);
376     while (n_coeffs < 0) {
377       __m128i coeff0, coeff1;
378       {
379         __m128i coeff0_sign, coeff1_sign;
380         __m128i qcoeff0, qcoeff1;
381         __m128i qtmp0, qtmp1;
382 
383         assert(index < (int)(sizeof(in) / sizeof(in[0])) - 1);
384         coeff0 = *in[index];
385         coeff1 = *in[index + 1];
386 
387         // Poor man's sign extract
388         coeff0_sign = _mm_srai_epi16(coeff0, 15);
389         coeff1_sign = _mm_srai_epi16(coeff1, 15);
390         qcoeff0 = _mm_xor_si128(coeff0, coeff0_sign);
391         qcoeff1 = _mm_xor_si128(coeff1, coeff1_sign);
392         qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
393         qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
394 
395         nzflag = _mm_movemask_epi8(_mm_cmpgt_epi16(qcoeff0, thr)) |
396             _mm_movemask_epi8(_mm_cmpgt_epi16(qcoeff1, thr));
397 
398         if (nzflag) {
399           qcoeff0 = _mm_adds_epi16(qcoeff0, round);
400           qcoeff1 = _mm_adds_epi16(qcoeff1, round);
401           qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
402           qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
403 
404           // Reinsert signs
405           qcoeff0 = _mm_xor_si128(qtmp0, coeff0_sign);
406           qcoeff1 = _mm_xor_si128(qtmp1, coeff1_sign);
407           qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
408           qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
409 
410           _mm_store_si128((__m128i*)(qcoeff_ptr + n_coeffs), qcoeff0);
411           _mm_store_si128((__m128i*)(qcoeff_ptr + n_coeffs) + 1, qcoeff1);
412 
413           coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
414           coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
415 
416           _mm_store_si128((__m128i*)(dqcoeff_ptr + n_coeffs), coeff0);
417           _mm_store_si128((__m128i*)(dqcoeff_ptr + n_coeffs) + 1, coeff1);
418         } else {
419           _mm_store_si128((__m128i*)(qcoeff_ptr + n_coeffs), zero);
420           _mm_store_si128((__m128i*)(qcoeff_ptr + n_coeffs) + 1, zero);
421 
422           _mm_store_si128((__m128i*)(dqcoeff_ptr + n_coeffs), zero);
423           _mm_store_si128((__m128i*)(dqcoeff_ptr + n_coeffs) + 1, zero);
424         }
425       }
426 
427       if (nzflag) {
428         // Scan for eob
429         __m128i zero_coeff0, zero_coeff1;
430         __m128i nzero_coeff0, nzero_coeff1;
431         __m128i iscan0, iscan1;
432         __m128i eob0, eob1;
433         zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
434         zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
435         nzero_coeff0 = _mm_cmpeq_epi16(zero_coeff0, zero);
436         nzero_coeff1 = _mm_cmpeq_epi16(zero_coeff1, zero);
437         iscan0 = _mm_load_si128((const __m128i*)(iscan_ptr + n_coeffs));
438         iscan1 = _mm_load_si128((const __m128i*)(iscan_ptr + n_coeffs) + 1);
439         // Add one to convert from indices to counts
440         iscan0 = _mm_sub_epi16(iscan0, nzero_coeff0);
441         iscan1 = _mm_sub_epi16(iscan1, nzero_coeff1);
442         eob0 = _mm_and_si128(iscan0, nzero_coeff0);
443         eob1 = _mm_and_si128(iscan1, nzero_coeff1);
444         eob0 = _mm_max_epi16(eob0, eob1);
445         eob = _mm_max_epi16(eob, eob0);
446       }
447       n_coeffs += 8 * 2;
448       index += 2;
449     }
450 
451     // Accumulate EOB
452     {
453       __m128i eob_shuffled;
454       eob_shuffled = _mm_shuffle_epi32(eob, 0xe);
455       eob = _mm_max_epi16(eob, eob_shuffled);
456       eob_shuffled = _mm_shufflelo_epi16(eob, 0xe);
457       eob = _mm_max_epi16(eob, eob_shuffled);
458       eob_shuffled = _mm_shufflelo_epi16(eob, 0x1);
459       eob = _mm_max_epi16(eob, eob_shuffled);
460       *eob_ptr = _mm_extract_epi16(eob, 1);
461     }
462   } else {
463     do {
464       _mm_store_si128((__m128i*)(dqcoeff_ptr + n_coeffs), zero);
465       _mm_store_si128((__m128i*)(dqcoeff_ptr + n_coeffs) + 1, zero);
466       _mm_store_si128((__m128i*)(qcoeff_ptr + n_coeffs), zero);
467       _mm_store_si128((__m128i*)(qcoeff_ptr + n_coeffs) + 1, zero);
468       n_coeffs += 8 * 2;
469     } while (n_coeffs < 0);
470     *eob_ptr = 0;
471   }
472 }
473