New comit of SDL2
[supertux.git] / src / SDL2 / external / libwebp-0.3.0 / src / dsp / enc_sse2.c
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // This code is licensed under the same terms as WebM:
4 //  Software License Agreement:  http://www.webmproject.org/license/software/
5 //  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
7 //
8 // SSE2 version of speed-critical encoding functions.
9 //
10 // Author: Christian Duvivier (cduvivier@google.com)
11
12 #include "./dsp.h"
13
14 #if defined(__cplusplus) || defined(c_plusplus)
15 extern "C" {
16 #endif
17
18 #if defined(WEBP_USE_SSE2)
19 #include <stdlib.h>  // for abs()
20 #include <emmintrin.h>
21
22 #include "../enc/vp8enci.h"
23
24 //------------------------------------------------------------------------------
25 // Quite useful macro for debugging. Left here for convenience.
26
27 #if 0
28 #include <stdio.h>
29 static void PrintReg(const __m128i r, const char* const name, int size) {
30   int n;
31   union {
32     __m128i r;
33     uint8_t i8[16];
34     uint16_t i16[8];
35     uint32_t i32[4];
36     uint64_t i64[2];
37   } tmp;
38   tmp.r = r;
39   printf("%s\t: ", name);
40   if (size == 8) {
41     for (n = 0; n < 16; ++n) printf("%.2x ", tmp.i8[n]);
42   } else if (size == 16) {
43     for (n = 0; n < 8; ++n) printf("%.4x ", tmp.i16[n]);
44   } else if (size == 32) {
45     for (n = 0; n < 4; ++n) printf("%.8x ", tmp.i32[n]);
46   } else {
47     for (n = 0; n < 2; ++n) printf("%.16lx ", tmp.i64[n]);
48   }
49   printf("\n");
50 }
51 #endif
52
53 //------------------------------------------------------------------------------
54 // Compute susceptibility based on DCT-coeff histograms:
55 // the higher, the "easier" the macroblock is to compress.
56
57 static void CollectHistogramSSE2(const uint8_t* ref, const uint8_t* pred,
58                                  int start_block, int end_block,
59                                  VP8Histogram* const histo) {
60   const __m128i max_coeff_thresh = _mm_set1_epi16(MAX_COEFF_THRESH);
61   int j;
62   for (j = start_block; j < end_block; ++j) {
63     int16_t out[16];
64     int k;
65
66     VP8FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
67
68     // Convert coefficients to bin (within out[]).
69     {
70       // Load.
71       const __m128i out0 = _mm_loadu_si128((__m128i*)&out[0]);
72       const __m128i out1 = _mm_loadu_si128((__m128i*)&out[8]);
73       // sign(out) = out >> 15  (0x0000 if positive, 0xffff if negative)
74       const __m128i sign0 = _mm_srai_epi16(out0, 15);
75       const __m128i sign1 = _mm_srai_epi16(out1, 15);
76       // abs(out) = (out ^ sign) - sign
77       const __m128i xor0 = _mm_xor_si128(out0, sign0);
78       const __m128i xor1 = _mm_xor_si128(out1, sign1);
79       const __m128i abs0 = _mm_sub_epi16(xor0, sign0);
80       const __m128i abs1 = _mm_sub_epi16(xor1, sign1);
81       // v = abs(out) >> 3
82       const __m128i v0 = _mm_srai_epi16(abs0, 3);
83       const __m128i v1 = _mm_srai_epi16(abs1, 3);
84       // bin = min(v, MAX_COEFF_THRESH)
85       const __m128i bin0 = _mm_min_epi16(v0, max_coeff_thresh);
86       const __m128i bin1 = _mm_min_epi16(v1, max_coeff_thresh);
87       // Store.
88       _mm_storeu_si128((__m128i*)&out[0], bin0);
89       _mm_storeu_si128((__m128i*)&out[8], bin1);
90     }
91
92     // Convert coefficients to bin.
93     for (k = 0; k < 16; ++k) {
94       histo->distribution[out[k]]++;
95     }
96   }
97 }
98
99 //------------------------------------------------------------------------------
100 // Transforms (Paragraph 14.4)
101
102 // Does one or two inverse transforms.
103 static void ITransformSSE2(const uint8_t* ref, const int16_t* in, uint8_t* dst,
104                            int do_two) {
105   // This implementation makes use of 16-bit fixed point versions of two
106   // multiply constants:
107   //    K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16
108   //    K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16
109   //
110   // To be able to use signed 16-bit integers, we use the following trick to
111   // have constants within range:
112   // - Associated constants are obtained by subtracting the 16-bit fixed point
113   //   version of one:
114   //      k = K - (1 << 16)  =>  K = k + (1 << 16)
115   //      K1 = 85267  =>  k1 =  20091
116   //      K2 = 35468  =>  k2 = -30068
117   // - The multiplication of a variable by a constant become the sum of the
118   //   variable and the multiplication of that variable by the associated
119   //   constant:
120   //      (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x
121   const __m128i k1 = _mm_set1_epi16(20091);
122   const __m128i k2 = _mm_set1_epi16(-30068);
123   __m128i T0, T1, T2, T3;
124
125   // Load and concatenate the transform coefficients (we'll do two inverse
126   // transforms in parallel). In the case of only one inverse transform, the
127   // second half of the vectors will just contain random value we'll never
128   // use nor store.
129   __m128i in0, in1, in2, in3;
130   {
131     in0 = _mm_loadl_epi64((__m128i*)&in[0]);
132     in1 = _mm_loadl_epi64((__m128i*)&in[4]);
133     in2 = _mm_loadl_epi64((__m128i*)&in[8]);
134     in3 = _mm_loadl_epi64((__m128i*)&in[12]);
135     // a00 a10 a20 a30   x x x x
136     // a01 a11 a21 a31   x x x x
137     // a02 a12 a22 a32   x x x x
138     // a03 a13 a23 a33   x x x x
139     if (do_two) {
140       const __m128i inB0 = _mm_loadl_epi64((__m128i*)&in[16]);
141       const __m128i inB1 = _mm_loadl_epi64((__m128i*)&in[20]);
142       const __m128i inB2 = _mm_loadl_epi64((__m128i*)&in[24]);
143       const __m128i inB3 = _mm_loadl_epi64((__m128i*)&in[28]);
144       in0 = _mm_unpacklo_epi64(in0, inB0);
145       in1 = _mm_unpacklo_epi64(in1, inB1);
146       in2 = _mm_unpacklo_epi64(in2, inB2);
147       in3 = _mm_unpacklo_epi64(in3, inB3);
148       // a00 a10 a20 a30   b00 b10 b20 b30
149       // a01 a11 a21 a31   b01 b11 b21 b31
150       // a02 a12 a22 a32   b02 b12 b22 b32
151       // a03 a13 a23 a33   b03 b13 b23 b33
152     }
153   }
154
155   // Vertical pass and subsequent transpose.
156   {
157     // First pass, c and d calculations are longer because of the "trick"
158     // multiplications.
159     const __m128i a = _mm_add_epi16(in0, in2);
160     const __m128i b = _mm_sub_epi16(in0, in2);
161     // c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3
162     const __m128i c1 = _mm_mulhi_epi16(in1, k2);
163     const __m128i c2 = _mm_mulhi_epi16(in3, k1);
164     const __m128i c3 = _mm_sub_epi16(in1, in3);
165     const __m128i c4 = _mm_sub_epi16(c1, c2);
166     const __m128i c = _mm_add_epi16(c3, c4);
167     // d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3
168     const __m128i d1 = _mm_mulhi_epi16(in1, k1);
169     const __m128i d2 = _mm_mulhi_epi16(in3, k2);
170     const __m128i d3 = _mm_add_epi16(in1, in3);
171     const __m128i d4 = _mm_add_epi16(d1, d2);
172     const __m128i d = _mm_add_epi16(d3, d4);
173
174     // Second pass.
175     const __m128i tmp0 = _mm_add_epi16(a, d);
176     const __m128i tmp1 = _mm_add_epi16(b, c);
177     const __m128i tmp2 = _mm_sub_epi16(b, c);
178     const __m128i tmp3 = _mm_sub_epi16(a, d);
179
180     // Transpose the two 4x4.
181     // a00 a01 a02 a03   b00 b01 b02 b03
182     // a10 a11 a12 a13   b10 b11 b12 b13
183     // a20 a21 a22 a23   b20 b21 b22 b23
184     // a30 a31 a32 a33   b30 b31 b32 b33
185     const __m128i transpose0_0 = _mm_unpacklo_epi16(tmp0, tmp1);
186     const __m128i transpose0_1 = _mm_unpacklo_epi16(tmp2, tmp3);
187     const __m128i transpose0_2 = _mm_unpackhi_epi16(tmp0, tmp1);
188     const __m128i transpose0_3 = _mm_unpackhi_epi16(tmp2, tmp3);
189     // a00 a10 a01 a11   a02 a12 a03 a13
190     // a20 a30 a21 a31   a22 a32 a23 a33
191     // b00 b10 b01 b11   b02 b12 b03 b13
192     // b20 b30 b21 b31   b22 b32 b23 b33
193     const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
194     const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
195     const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
196     const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
197     // a00 a10 a20 a30 a01 a11 a21 a31
198     // b00 b10 b20 b30 b01 b11 b21 b31
199     // a02 a12 a22 a32 a03 a13 a23 a33
200     // b02 b12 a22 b32 b03 b13 b23 b33
201     T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
202     T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
203     T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
204     T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
205     // a00 a10 a20 a30   b00 b10 b20 b30
206     // a01 a11 a21 a31   b01 b11 b21 b31
207     // a02 a12 a22 a32   b02 b12 b22 b32
208     // a03 a13 a23 a33   b03 b13 b23 b33
209   }
210
211   // Horizontal pass and subsequent transpose.
212   {
213     // First pass, c and d calculations are longer because of the "trick"
214     // multiplications.
215     const __m128i four = _mm_set1_epi16(4);
216     const __m128i dc = _mm_add_epi16(T0, four);
217     const __m128i a =  _mm_add_epi16(dc, T2);
218     const __m128i b =  _mm_sub_epi16(dc, T2);
219     // c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3
220     const __m128i c1 = _mm_mulhi_epi16(T1, k2);
221     const __m128i c2 = _mm_mulhi_epi16(T3, k1);
222     const __m128i c3 = _mm_sub_epi16(T1, T3);
223     const __m128i c4 = _mm_sub_epi16(c1, c2);
224     const __m128i c = _mm_add_epi16(c3, c4);
225     // d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3
226     const __m128i d1 = _mm_mulhi_epi16(T1, k1);
227     const __m128i d2 = _mm_mulhi_epi16(T3, k2);
228     const __m128i d3 = _mm_add_epi16(T1, T3);
229     const __m128i d4 = _mm_add_epi16(d1, d2);
230     const __m128i d = _mm_add_epi16(d3, d4);
231
232     // Second pass.
233     const __m128i tmp0 = _mm_add_epi16(a, d);
234     const __m128i tmp1 = _mm_add_epi16(b, c);
235     const __m128i tmp2 = _mm_sub_epi16(b, c);
236     const __m128i tmp3 = _mm_sub_epi16(a, d);
237     const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);
238     const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);
239     const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);
240     const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);
241
242     // Transpose the two 4x4.
243     // a00 a01 a02 a03   b00 b01 b02 b03
244     // a10 a11 a12 a13   b10 b11 b12 b13
245     // a20 a21 a22 a23   b20 b21 b22 b23
246     // a30 a31 a32 a33   b30 b31 b32 b33
247     const __m128i transpose0_0 = _mm_unpacklo_epi16(shifted0, shifted1);
248     const __m128i transpose0_1 = _mm_unpacklo_epi16(shifted2, shifted3);
249     const __m128i transpose0_2 = _mm_unpackhi_epi16(shifted0, shifted1);
250     const __m128i transpose0_3 = _mm_unpackhi_epi16(shifted2, shifted3);
251     // a00 a10 a01 a11   a02 a12 a03 a13
252     // a20 a30 a21 a31   a22 a32 a23 a33
253     // b00 b10 b01 b11   b02 b12 b03 b13
254     // b20 b30 b21 b31   b22 b32 b23 b33
255     const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
256     const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
257     const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
258     const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
259     // a00 a10 a20 a30 a01 a11 a21 a31
260     // b00 b10 b20 b30 b01 b11 b21 b31
261     // a02 a12 a22 a32 a03 a13 a23 a33
262     // b02 b12 a22 b32 b03 b13 b23 b33
263     T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
264     T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
265     T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
266     T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
267     // a00 a10 a20 a30   b00 b10 b20 b30
268     // a01 a11 a21 a31   b01 b11 b21 b31
269     // a02 a12 a22 a32   b02 b12 b22 b32
270     // a03 a13 a23 a33   b03 b13 b23 b33
271   }
272
273   // Add inverse transform to 'ref' and store.
274   {
275     const __m128i zero = _mm_setzero_si128();
276     // Load the reference(s).
277     __m128i ref0, ref1, ref2, ref3;
278     if (do_two) {
279       // Load eight bytes/pixels per line.
280       ref0 = _mm_loadl_epi64((__m128i*)&ref[0 * BPS]);
281       ref1 = _mm_loadl_epi64((__m128i*)&ref[1 * BPS]);
282       ref2 = _mm_loadl_epi64((__m128i*)&ref[2 * BPS]);
283       ref3 = _mm_loadl_epi64((__m128i*)&ref[3 * BPS]);
284     } else {
285       // Load four bytes/pixels per line.
286       ref0 = _mm_cvtsi32_si128(*(int*)&ref[0 * BPS]);
287       ref1 = _mm_cvtsi32_si128(*(int*)&ref[1 * BPS]);
288       ref2 = _mm_cvtsi32_si128(*(int*)&ref[2 * BPS]);
289       ref3 = _mm_cvtsi32_si128(*(int*)&ref[3 * BPS]);
290     }
291     // Convert to 16b.
292     ref0 = _mm_unpacklo_epi8(ref0, zero);
293     ref1 = _mm_unpacklo_epi8(ref1, zero);
294     ref2 = _mm_unpacklo_epi8(ref2, zero);
295     ref3 = _mm_unpacklo_epi8(ref3, zero);
296     // Add the inverse transform(s).
297     ref0 = _mm_add_epi16(ref0, T0);
298     ref1 = _mm_add_epi16(ref1, T1);
299     ref2 = _mm_add_epi16(ref2, T2);
300     ref3 = _mm_add_epi16(ref3, T3);
301     // Unsigned saturate to 8b.
302     ref0 = _mm_packus_epi16(ref0, ref0);
303     ref1 = _mm_packus_epi16(ref1, ref1);
304     ref2 = _mm_packus_epi16(ref2, ref2);
305     ref3 = _mm_packus_epi16(ref3, ref3);
306     // Store the results.
307     if (do_two) {
308       // Store eight bytes/pixels per line.
309       _mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0);
310       _mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1);
311       _mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2);
312       _mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3);
313     } else {
314       // Store four bytes/pixels per line.
315       *((int32_t *)&dst[0 * BPS]) = _mm_cvtsi128_si32(ref0);
316       *((int32_t *)&dst[1 * BPS]) = _mm_cvtsi128_si32(ref1);
317       *((int32_t *)&dst[2 * BPS]) = _mm_cvtsi128_si32(ref2);
318       *((int32_t *)&dst[3 * BPS]) = _mm_cvtsi128_si32(ref3);
319     }
320   }
321 }
322
323 static void FTransformSSE2(const uint8_t* src, const uint8_t* ref,
324                            int16_t* out) {
325   const __m128i zero = _mm_setzero_si128();
326   const __m128i seven = _mm_set1_epi16(7);
327   const __m128i k937 = _mm_set1_epi32(937);
328   const __m128i k1812 = _mm_set1_epi32(1812);
329   const __m128i k51000 = _mm_set1_epi32(51000);
330   const __m128i k12000_plus_one = _mm_set1_epi32(12000 + (1 << 16));
331   const __m128i k5352_2217 = _mm_set_epi16(5352,  2217, 5352,  2217,
332                                            5352,  2217, 5352,  2217);
333   const __m128i k2217_5352 = _mm_set_epi16(2217, -5352, 2217, -5352,
334                                            2217, -5352, 2217, -5352);
335   const __m128i k88p = _mm_set_epi16(8, 8, 8, 8, 8, 8, 8, 8);
336   const __m128i k88m = _mm_set_epi16(-8, 8, -8, 8, -8, 8, -8, 8);
337   const __m128i k5352_2217p = _mm_set_epi16(2217, 5352, 2217, 5352,
338                                             2217, 5352, 2217, 5352);
339   const __m128i k5352_2217m = _mm_set_epi16(-5352, 2217, -5352, 2217,
340                                             -5352, 2217, -5352, 2217);
341   __m128i v01, v32;
342
343
344   // Difference between src and ref and initial transpose.
345   {
346     // Load src and convert to 16b.
347     const __m128i src0 = _mm_loadl_epi64((__m128i*)&src[0 * BPS]);
348     const __m128i src1 = _mm_loadl_epi64((__m128i*)&src[1 * BPS]);
349     const __m128i src2 = _mm_loadl_epi64((__m128i*)&src[2 * BPS]);
350     const __m128i src3 = _mm_loadl_epi64((__m128i*)&src[3 * BPS]);
351     const __m128i src_0 = _mm_unpacklo_epi8(src0, zero);
352     const __m128i src_1 = _mm_unpacklo_epi8(src1, zero);
353     const __m128i src_2 = _mm_unpacklo_epi8(src2, zero);
354     const __m128i src_3 = _mm_unpacklo_epi8(src3, zero);
355     // Load ref and convert to 16b.
356     const __m128i ref0 = _mm_loadl_epi64((__m128i*)&ref[0 * BPS]);
357     const __m128i ref1 = _mm_loadl_epi64((__m128i*)&ref[1 * BPS]);
358     const __m128i ref2 = _mm_loadl_epi64((__m128i*)&ref[2 * BPS]);
359     const __m128i ref3 = _mm_loadl_epi64((__m128i*)&ref[3 * BPS]);
360     const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero);
361     const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero);
362     const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero);
363     const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero);
364     // Compute difference. -> 00 01 02 03 00 00 00 00
365     const __m128i diff0 = _mm_sub_epi16(src_0, ref_0);
366     const __m128i diff1 = _mm_sub_epi16(src_1, ref_1);
367     const __m128i diff2 = _mm_sub_epi16(src_2, ref_2);
368     const __m128i diff3 = _mm_sub_epi16(src_3, ref_3);
369
370
371     // Unpack and shuffle
372     // 00 01 02 03   0 0 0 0
373     // 10 11 12 13   0 0 0 0
374     // 20 21 22 23   0 0 0 0
375     // 30 31 32 33   0 0 0 0
376     const __m128i shuf01 = _mm_unpacklo_epi32(diff0, diff1);
377     const __m128i shuf23 = _mm_unpacklo_epi32(diff2, diff3);
378     // 00 01 10 11 02 03 12 13
379     // 20 21 30 31 22 23 32 33
380     const __m128i shuf01_p =
381         _mm_shufflehi_epi16(shuf01, _MM_SHUFFLE(2, 3, 0, 1));
382     const __m128i shuf23_p =
383         _mm_shufflehi_epi16(shuf23, _MM_SHUFFLE(2, 3, 0, 1));
384     // 00 01 10 11 03 02 13 12
385     // 20 21 30 31 23 22 33 32
386     const __m128i s01 = _mm_unpacklo_epi64(shuf01_p, shuf23_p);
387     const __m128i s32 = _mm_unpackhi_epi64(shuf01_p, shuf23_p);
388     // 00 01 10 11 20 21 30 31
389     // 03 02 13 12 23 22 33 32
390     const __m128i a01 = _mm_add_epi16(s01, s32);
391     const __m128i a32 = _mm_sub_epi16(s01, s32);
392     // [d0 + d3 | d1 + d2 | ...] = [a0 a1 | a0' a1' | ... ]
393     // [d0 - d3 | d1 - d2 | ...] = [a3 a2 | a3' a2' | ... ]
394
395     const __m128i tmp0 = _mm_madd_epi16(a01, k88p);  // [ (a0 + a1) << 3, ... ]
396     const __m128i tmp2 = _mm_madd_epi16(a01, k88m);  // [ (a0 - a1) << 3, ... ]
397     const __m128i tmp1_1 = _mm_madd_epi16(a32, k5352_2217p);
398     const __m128i tmp3_1 = _mm_madd_epi16(a32, k5352_2217m);
399     const __m128i tmp1_2 = _mm_add_epi32(tmp1_1, k1812);
400     const __m128i tmp3_2 = _mm_add_epi32(tmp3_1, k937);
401     const __m128i tmp1   = _mm_srai_epi32(tmp1_2, 9);
402     const __m128i tmp3   = _mm_srai_epi32(tmp3_2, 9);
403     const __m128i s03 = _mm_packs_epi32(tmp0, tmp2);
404     const __m128i s12 = _mm_packs_epi32(tmp1, tmp3);
405     const __m128i s_lo = _mm_unpacklo_epi16(s03, s12);   // 0 1 0 1 0 1...
406     const __m128i s_hi = _mm_unpackhi_epi16(s03, s12);   // 2 3 2 3 2 3
407     const __m128i v23 = _mm_unpackhi_epi32(s_lo, s_hi);
408     v01 = _mm_unpacklo_epi32(s_lo, s_hi);
409     v32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2));  // 3 2 3 2 3 2..
410   }
411
412   // Second pass
413   {
414     // Same operations are done on the (0,3) and (1,2) pairs.
415     // a0 = v0 + v3
416     // a1 = v1 + v2
417     // a3 = v0 - v3
418     // a2 = v1 - v2
419     const __m128i a01 = _mm_add_epi16(v01, v32);
420     const __m128i a32 = _mm_sub_epi16(v01, v32);
421     const __m128i a11 = _mm_unpackhi_epi64(a01, a01);
422     const __m128i a22 = _mm_unpackhi_epi64(a32, a32);
423     const __m128i a01_plus_7 = _mm_add_epi16(a01, seven);
424
425     // d0 = (a0 + a1 + 7) >> 4;
426     // d2 = (a0 - a1 + 7) >> 4;
427     const __m128i c0 = _mm_add_epi16(a01_plus_7, a11);
428     const __m128i c2 = _mm_sub_epi16(a01_plus_7, a11);
429     const __m128i d0 = _mm_srai_epi16(c0, 4);
430     const __m128i d2 = _mm_srai_epi16(c2, 4);
431
432     // f1 = ((b3 * 5352 + b2 * 2217 + 12000) >> 16)
433     // f3 = ((b3 * 2217 - b2 * 5352 + 51000) >> 16)
434     const __m128i b23 = _mm_unpacklo_epi16(a22, a32);
435     const __m128i c1 = _mm_madd_epi16(b23, k5352_2217);
436     const __m128i c3 = _mm_madd_epi16(b23, k2217_5352);
437     const __m128i d1 = _mm_add_epi32(c1, k12000_plus_one);
438     const __m128i d3 = _mm_add_epi32(c3, k51000);
439     const __m128i e1 = _mm_srai_epi32(d1, 16);
440     const __m128i e3 = _mm_srai_epi32(d3, 16);
441     const __m128i f1 = _mm_packs_epi32(e1, e1);
442     const __m128i f3 = _mm_packs_epi32(e3, e3);
443     // f1 = f1 + (a3 != 0);
444     // The compare will return (0xffff, 0) for (==0, !=0). To turn that into the
445     // desired (0, 1), we add one earlier through k12000_plus_one.
446     // -> f1 = f1 + 1 - (a3 == 0)
447     const __m128i g1 = _mm_add_epi16(f1, _mm_cmpeq_epi16(a32, zero));
448
449     _mm_storel_epi64((__m128i*)&out[ 0], d0);
450     _mm_storel_epi64((__m128i*)&out[ 4], g1);
451     _mm_storel_epi64((__m128i*)&out[ 8], d2);
452     _mm_storel_epi64((__m128i*)&out[12], f3);
453   }
454 }
455
456 static void FTransformWHTSSE2(const int16_t* in, int16_t* out) {
457   int16_t tmp[16];
458   int i;
459   for (i = 0; i < 4; ++i, in += 64) {
460     const int a0 = (in[0 * 16] + in[2 * 16]);
461     const int a1 = (in[1 * 16] + in[3 * 16]);
462     const int a2 = (in[1 * 16] - in[3 * 16]);
463     const int a3 = (in[0 * 16] - in[2 * 16]);
464     tmp[0 + i * 4] = a0 + a1;
465     tmp[1 + i * 4] = a3 + a2;
466     tmp[2 + i * 4] = a3 - a2;
467     tmp[3 + i * 4] = a0 - a1;
468   }
469   {
470     const __m128i src0 = _mm_loadl_epi64((__m128i*)&tmp[0]);
471     const __m128i src1 = _mm_loadl_epi64((__m128i*)&tmp[4]);
472     const __m128i src2 = _mm_loadl_epi64((__m128i*)&tmp[8]);
473     const __m128i src3 = _mm_loadl_epi64((__m128i*)&tmp[12]);
474     const __m128i a0 = _mm_add_epi16(src0, src2);
475     const __m128i a1 = _mm_add_epi16(src1, src3);
476     const __m128i a2 = _mm_sub_epi16(src1, src3);
477     const __m128i a3 = _mm_sub_epi16(src0, src2);
478     const __m128i b0 = _mm_srai_epi16(_mm_adds_epi16(a0, a1), 1);
479     const __m128i b1 = _mm_srai_epi16(_mm_adds_epi16(a3, a2), 1);
480     const __m128i b2 = _mm_srai_epi16(_mm_subs_epi16(a3, a2), 1);
481     const __m128i b3 = _mm_srai_epi16(_mm_subs_epi16(a0, a1), 1);
482     _mm_storel_epi64((__m128i*)&out[ 0], b0);
483     _mm_storel_epi64((__m128i*)&out[ 4], b1);
484     _mm_storel_epi64((__m128i*)&out[ 8], b2);
485     _mm_storel_epi64((__m128i*)&out[12], b3);
486   }
487 }
488
489 //------------------------------------------------------------------------------
490 // Metric
491
492 static int SSE_Nx4SSE2(const uint8_t* a, const uint8_t* b,
493                        int num_quads, int do_16) {
494   const __m128i zero = _mm_setzero_si128();
495   __m128i sum1 = zero;
496   __m128i sum2 = zero;
497
498   while (num_quads-- > 0) {
499     // Note: for the !do_16 case, we read 16 pixels instead of 8 but that's ok,
500     // thanks to buffer over-allocation to that effect.
501     const __m128i a0 = _mm_loadu_si128((__m128i*)&a[BPS * 0]);
502     const __m128i a1 = _mm_loadu_si128((__m128i*)&a[BPS * 1]);
503     const __m128i a2 = _mm_loadu_si128((__m128i*)&a[BPS * 2]);
504     const __m128i a3 = _mm_loadu_si128((__m128i*)&a[BPS * 3]);
505     const __m128i b0 = _mm_loadu_si128((__m128i*)&b[BPS * 0]);
506     const __m128i b1 = _mm_loadu_si128((__m128i*)&b[BPS * 1]);
507     const __m128i b2 = _mm_loadu_si128((__m128i*)&b[BPS * 2]);
508     const __m128i b3 = _mm_loadu_si128((__m128i*)&b[BPS * 3]);
509
510     // compute clip0(a-b) and clip0(b-a)
511     const __m128i a0p = _mm_subs_epu8(a0, b0);
512     const __m128i a0m = _mm_subs_epu8(b0, a0);
513     const __m128i a1p = _mm_subs_epu8(a1, b1);
514     const __m128i a1m = _mm_subs_epu8(b1, a1);
515     const __m128i a2p = _mm_subs_epu8(a2, b2);
516     const __m128i a2m = _mm_subs_epu8(b2, a2);
517     const __m128i a3p = _mm_subs_epu8(a3, b3);
518     const __m128i a3m = _mm_subs_epu8(b3, a3);
519
520     // compute |a-b| with 8b arithmetic as clip0(a-b) | clip0(b-a)
521     const __m128i diff0 = _mm_or_si128(a0p, a0m);
522     const __m128i diff1 = _mm_or_si128(a1p, a1m);
523     const __m128i diff2 = _mm_or_si128(a2p, a2m);
524     const __m128i diff3 = _mm_or_si128(a3p, a3m);
525
526     // unpack (only four operations, instead of eight)
527     const __m128i low0 = _mm_unpacklo_epi8(diff0, zero);
528     const __m128i low1 = _mm_unpacklo_epi8(diff1, zero);
529     const __m128i low2 = _mm_unpacklo_epi8(diff2, zero);
530     const __m128i low3 = _mm_unpacklo_epi8(diff3, zero);
531
532     // multiply with self
533     const __m128i low_madd0 = _mm_madd_epi16(low0, low0);
534     const __m128i low_madd1 = _mm_madd_epi16(low1, low1);
535     const __m128i low_madd2 = _mm_madd_epi16(low2, low2);
536     const __m128i low_madd3 = _mm_madd_epi16(low3, low3);
537
538     // collect in a cascading way
539     const __m128i low_sum0 = _mm_add_epi32(low_madd0, low_madd1);
540     const __m128i low_sum1 = _mm_add_epi32(low_madd2, low_madd3);
541     sum1 = _mm_add_epi32(sum1, low_sum0);
542     sum2 = _mm_add_epi32(sum2, low_sum1);
543
544     if (do_16) {  // if necessary, process the higher 8 bytes similarly
545       const __m128i hi0 = _mm_unpackhi_epi8(diff0, zero);
546       const __m128i hi1 = _mm_unpackhi_epi8(diff1, zero);
547       const __m128i hi2 = _mm_unpackhi_epi8(diff2, zero);
548       const __m128i hi3 = _mm_unpackhi_epi8(diff3, zero);
549
550       const __m128i hi_madd0 = _mm_madd_epi16(hi0, hi0);
551       const __m128i hi_madd1 = _mm_madd_epi16(hi1, hi1);
552       const __m128i hi_madd2 = _mm_madd_epi16(hi2, hi2);
553       const __m128i hi_madd3 = _mm_madd_epi16(hi3, hi3);
554       const __m128i hi_sum0 = _mm_add_epi32(hi_madd0, hi_madd1);
555       const __m128i hi_sum1 = _mm_add_epi32(hi_madd2, hi_madd3);
556       sum1 = _mm_add_epi32(sum1, hi_sum0);
557       sum2 = _mm_add_epi32(sum2, hi_sum1);
558     }
559     a += 4 * BPS;
560     b += 4 * BPS;
561   }
562   {
563     int32_t tmp[4];
564     const __m128i sum = _mm_add_epi32(sum1, sum2);
565     _mm_storeu_si128((__m128i*)tmp, sum);
566     return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
567   }
568 }
569
570 static int SSE16x16SSE2(const uint8_t* a, const uint8_t* b) {
571   return SSE_Nx4SSE2(a, b, 4, 1);
572 }
573
574 static int SSE16x8SSE2(const uint8_t* a, const uint8_t* b) {
575   return SSE_Nx4SSE2(a, b, 2, 1);
576 }
577
578 static int SSE8x8SSE2(const uint8_t* a, const uint8_t* b) {
579   return SSE_Nx4SSE2(a, b, 2, 0);
580 }
581
582 static int SSE4x4SSE2(const uint8_t* a, const uint8_t* b) {
583   const __m128i zero = _mm_setzero_si128();
584
585   // Load values. Note that we read 8 pixels instead of 4,
586   // but the a/b buffers are over-allocated to that effect.
587   const __m128i a0 = _mm_loadl_epi64((__m128i*)&a[BPS * 0]);
588   const __m128i a1 = _mm_loadl_epi64((__m128i*)&a[BPS * 1]);
589   const __m128i a2 = _mm_loadl_epi64((__m128i*)&a[BPS * 2]);
590   const __m128i a3 = _mm_loadl_epi64((__m128i*)&a[BPS * 3]);
591   const __m128i b0 = _mm_loadl_epi64((__m128i*)&b[BPS * 0]);
592   const __m128i b1 = _mm_loadl_epi64((__m128i*)&b[BPS * 1]);
593   const __m128i b2 = _mm_loadl_epi64((__m128i*)&b[BPS * 2]);
594   const __m128i b3 = _mm_loadl_epi64((__m128i*)&b[BPS * 3]);
595
596   // Combine pair of lines and convert to 16b.
597   const __m128i a01 = _mm_unpacklo_epi32(a0, a1);
598   const __m128i a23 = _mm_unpacklo_epi32(a2, a3);
599   const __m128i b01 = _mm_unpacklo_epi32(b0, b1);
600   const __m128i b23 = _mm_unpacklo_epi32(b2, b3);
601   const __m128i a01s = _mm_unpacklo_epi8(a01, zero);
602   const __m128i a23s = _mm_unpacklo_epi8(a23, zero);
603   const __m128i b01s = _mm_unpacklo_epi8(b01, zero);
604   const __m128i b23s = _mm_unpacklo_epi8(b23, zero);
605
606   // Compute differences; (a-b)^2 = (abs(a-b))^2 = (sat8(a-b) + sat8(b-a))^2
607   // TODO(cduvivier): Dissassemble and figure out why this is fastest. We don't
608   //                  need absolute values, there is no need to do calculation
609   //                  in 8bit as we are already in 16bit, ... Yet this is what
610   //                  benchmarks the fastest!
611   const __m128i d0 = _mm_subs_epu8(a01s, b01s);
612   const __m128i d1 = _mm_subs_epu8(b01s, a01s);
613   const __m128i d2 = _mm_subs_epu8(a23s, b23s);
614   const __m128i d3 = _mm_subs_epu8(b23s, a23s);
615
616   // Square and add them all together.
617   const __m128i madd0 = _mm_madd_epi16(d0, d0);
618   const __m128i madd1 = _mm_madd_epi16(d1, d1);
619   const __m128i madd2 = _mm_madd_epi16(d2, d2);
620   const __m128i madd3 = _mm_madd_epi16(d3, d3);
621   const __m128i sum0 = _mm_add_epi32(madd0, madd1);
622   const __m128i sum1 = _mm_add_epi32(madd2, madd3);
623   const __m128i sum2 = _mm_add_epi32(sum0, sum1);
624
625   int32_t tmp[4];
626   _mm_storeu_si128((__m128i*)tmp, sum2);
627   return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
628 }
629
630 //------------------------------------------------------------------------------
631 // Texture distortion
632 //
633 // We try to match the spectral content (weighted) between source and
634 // reconstructed samples.
635
636 // Hadamard transform
637 // Returns the difference between the weighted sum of the absolute value of
638 // transformed coefficients.
639 static int TTransformSSE2(const uint8_t* inA, const uint8_t* inB,
640                           const uint16_t* const w) {
641   int32_t sum[4];
642   __m128i tmp_0, tmp_1, tmp_2, tmp_3;
643   const __m128i zero = _mm_setzero_si128();
644
645   // Load, combine and tranpose inputs.
646   {
647     const __m128i inA_0 = _mm_loadl_epi64((__m128i*)&inA[BPS * 0]);
648     const __m128i inA_1 = _mm_loadl_epi64((__m128i*)&inA[BPS * 1]);
649     const __m128i inA_2 = _mm_loadl_epi64((__m128i*)&inA[BPS * 2]);
650     const __m128i inA_3 = _mm_loadl_epi64((__m128i*)&inA[BPS * 3]);
651     const __m128i inB_0 = _mm_loadl_epi64((__m128i*)&inB[BPS * 0]);
652     const __m128i inB_1 = _mm_loadl_epi64((__m128i*)&inB[BPS * 1]);
653     const __m128i inB_2 = _mm_loadl_epi64((__m128i*)&inB[BPS * 2]);
654     const __m128i inB_3 = _mm_loadl_epi64((__m128i*)&inB[BPS * 3]);
655
656     // Combine inA and inB (we'll do two transforms in parallel).
657     const __m128i inAB_0 = _mm_unpacklo_epi8(inA_0, inB_0);
658     const __m128i inAB_1 = _mm_unpacklo_epi8(inA_1, inB_1);
659     const __m128i inAB_2 = _mm_unpacklo_epi8(inA_2, inB_2);
660     const __m128i inAB_3 = _mm_unpacklo_epi8(inA_3, inB_3);
661     // a00 b00 a01 b01 a02 b03 a03 b03   0 0 0 0 0 0 0 0
662     // a10 b10 a11 b11 a12 b12 a13 b13   0 0 0 0 0 0 0 0
663     // a20 b20 a21 b21 a22 b22 a23 b23   0 0 0 0 0 0 0 0
664     // a30 b30 a31 b31 a32 b32 a33 b33   0 0 0 0 0 0 0 0
665
666     // Transpose the two 4x4, discarding the filling zeroes.
667     const __m128i transpose0_0 = _mm_unpacklo_epi8(inAB_0, inAB_2);
668     const __m128i transpose0_1 = _mm_unpacklo_epi8(inAB_1, inAB_3);
669     // a00 a20  b00 b20  a01 a21  b01 b21  a02 a22  b02 b22  a03 a23  b03 b23
670     // a10 a30  b10 b30  a11 a31  b11 b31  a12 a32  b12 b32  a13 a33  b13 b33
671     const __m128i transpose1_0 = _mm_unpacklo_epi8(transpose0_0, transpose0_1);
672     const __m128i transpose1_1 = _mm_unpackhi_epi8(transpose0_0, transpose0_1);
673     // a00 a10 a20 a30  b00 b10 b20 b30  a01 a11 a21 a31  b01 b11 b21 b31
674     // a02 a12 a22 a32  b02 b12 b22 b32  a03 a13 a23 a33  b03 b13 b23 b33
675
676     // Convert to 16b.
677     tmp_0 = _mm_unpacklo_epi8(transpose1_0, zero);
678     tmp_1 = _mm_unpackhi_epi8(transpose1_0, zero);
679     tmp_2 = _mm_unpacklo_epi8(transpose1_1, zero);
680     tmp_3 = _mm_unpackhi_epi8(transpose1_1, zero);
681     // a00 a10 a20 a30   b00 b10 b20 b30
682     // a01 a11 a21 a31   b01 b11 b21 b31
683     // a02 a12 a22 a32   b02 b12 b22 b32
684     // a03 a13 a23 a33   b03 b13 b23 b33
685   }
686
687   // Horizontal pass and subsequent transpose.
688   {
689     // Calculate a and b (two 4x4 at once).
690     const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);
691     const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);
692     const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);
693     const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);
694     const __m128i b0 = _mm_add_epi16(a0, a1);
695     const __m128i b1 = _mm_add_epi16(a3, a2);
696     const __m128i b2 = _mm_sub_epi16(a3, a2);
697     const __m128i b3 = _mm_sub_epi16(a0, a1);
698     // a00 a01 a02 a03   b00 b01 b02 b03
699     // a10 a11 a12 a13   b10 b11 b12 b13
700     // a20 a21 a22 a23   b20 b21 b22 b23
701     // a30 a31 a32 a33   b30 b31 b32 b33
702
703     // Transpose the two 4x4.
704     const __m128i transpose0_0 = _mm_unpacklo_epi16(b0, b1);
705     const __m128i transpose0_1 = _mm_unpacklo_epi16(b2, b3);
706     const __m128i transpose0_2 = _mm_unpackhi_epi16(b0, b1);
707     const __m128i transpose0_3 = _mm_unpackhi_epi16(b2, b3);
708     // a00 a10 a01 a11   a02 a12 a03 a13
709     // a20 a30 a21 a31   a22 a32 a23 a33
710     // b00 b10 b01 b11   b02 b12 b03 b13
711     // b20 b30 b21 b31   b22 b32 b23 b33
712     const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
713     const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
714     const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
715     const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
716     // a00 a10 a20 a30 a01 a11 a21 a31
717     // b00 b10 b20 b30 b01 b11 b21 b31
718     // a02 a12 a22 a32 a03 a13 a23 a33
719     // b02 b12 a22 b32 b03 b13 b23 b33
720     tmp_0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
721     tmp_1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
722     tmp_2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
723     tmp_3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
724     // a00 a10 a20 a30   b00 b10 b20 b30
725     // a01 a11 a21 a31   b01 b11 b21 b31
726     // a02 a12 a22 a32   b02 b12 b22 b32
727     // a03 a13 a23 a33   b03 b13 b23 b33
728   }
729
730   // Vertical pass and difference of weighted sums.
731   {
732     // Load all inputs.
733     // TODO(cduvivier): Make variable declarations and allocations aligned so
734     //                  we can use _mm_load_si128 instead of _mm_loadu_si128.
735     const __m128i w_0 = _mm_loadu_si128((__m128i*)&w[0]);
736     const __m128i w_8 = _mm_loadu_si128((__m128i*)&w[8]);
737
738     // Calculate a and b (two 4x4 at once).
739     const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);
740     const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);
741     const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);
742     const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);
743     const __m128i b0 = _mm_add_epi16(a0, a1);
744     const __m128i b1 = _mm_add_epi16(a3, a2);
745     const __m128i b2 = _mm_sub_epi16(a3, a2);
746     const __m128i b3 = _mm_sub_epi16(a0, a1);
747
748     // Separate the transforms of inA and inB.
749     __m128i A_b0 = _mm_unpacklo_epi64(b0, b1);
750     __m128i A_b2 = _mm_unpacklo_epi64(b2, b3);
751     __m128i B_b0 = _mm_unpackhi_epi64(b0, b1);
752     __m128i B_b2 = _mm_unpackhi_epi64(b2, b3);
753
754     {
755       // sign(b) = b >> 15  (0x0000 if positive, 0xffff if negative)
756       const __m128i sign_A_b0 = _mm_srai_epi16(A_b0, 15);
757       const __m128i sign_A_b2 = _mm_srai_epi16(A_b2, 15);
758       const __m128i sign_B_b0 = _mm_srai_epi16(B_b0, 15);
759       const __m128i sign_B_b2 = _mm_srai_epi16(B_b2, 15);
760
761       // b = abs(b) = (b ^ sign) - sign
762       A_b0 = _mm_xor_si128(A_b0, sign_A_b0);
763       A_b2 = _mm_xor_si128(A_b2, sign_A_b2);
764       B_b0 = _mm_xor_si128(B_b0, sign_B_b0);
765       B_b2 = _mm_xor_si128(B_b2, sign_B_b2);
766       A_b0 = _mm_sub_epi16(A_b0, sign_A_b0);
767       A_b2 = _mm_sub_epi16(A_b2, sign_A_b2);
768       B_b0 = _mm_sub_epi16(B_b0, sign_B_b0);
769       B_b2 = _mm_sub_epi16(B_b2, sign_B_b2);
770     }
771
772     // weighted sums
773     A_b0 = _mm_madd_epi16(A_b0, w_0);
774     A_b2 = _mm_madd_epi16(A_b2, w_8);
775     B_b0 = _mm_madd_epi16(B_b0, w_0);
776     B_b2 = _mm_madd_epi16(B_b2, w_8);
777     A_b0 = _mm_add_epi32(A_b0, A_b2);
778     B_b0 = _mm_add_epi32(B_b0, B_b2);
779
780     // difference of weighted sums
781     A_b0 = _mm_sub_epi32(A_b0, B_b0);
782     _mm_storeu_si128((__m128i*)&sum[0], A_b0);
783   }
784   return sum[0] + sum[1] + sum[2] + sum[3];
785 }
786
787 static int Disto4x4SSE2(const uint8_t* const a, const uint8_t* const b,
788                         const uint16_t* const w) {
789   const int diff_sum = TTransformSSE2(a, b, w);
790   return abs(diff_sum) >> 5;
791 }
792
793 static int Disto16x16SSE2(const uint8_t* const a, const uint8_t* const b,
794                           const uint16_t* const w) {
795   int D = 0;
796   int x, y;
797   for (y = 0; y < 16 * BPS; y += 4 * BPS) {
798     for (x = 0; x < 16; x += 4) {
799       D += Disto4x4SSE2(a + x + y, b + x + y, w);
800     }
801   }
802   return D;
803 }
804
805 //------------------------------------------------------------------------------
806 // Quantization
807 //
808
809 // Simple quantization
810 static int QuantizeBlockSSE2(int16_t in[16], int16_t out[16],
811                              int n, const VP8Matrix* const mtx) {
812   const __m128i max_coeff_2047 = _mm_set1_epi16(MAX_LEVEL);
813   const __m128i zero = _mm_setzero_si128();
814   __m128i coeff0, coeff8;
815   __m128i out0, out8;
816   __m128i packed_out;
817
818   // Load all inputs.
819   // TODO(cduvivier): Make variable declarations and allocations aligned so that
820   //                  we can use _mm_load_si128 instead of _mm_loadu_si128.
821   __m128i in0 = _mm_loadu_si128((__m128i*)&in[0]);
822   __m128i in8 = _mm_loadu_si128((__m128i*)&in[8]);
823   const __m128i sharpen0 = _mm_loadu_si128((__m128i*)&mtx->sharpen_[0]);
824   const __m128i sharpen8 = _mm_loadu_si128((__m128i*)&mtx->sharpen_[8]);
825   const __m128i iq0 = _mm_loadu_si128((__m128i*)&mtx->iq_[0]);
826   const __m128i iq8 = _mm_loadu_si128((__m128i*)&mtx->iq_[8]);
827   const __m128i bias0 = _mm_loadu_si128((__m128i*)&mtx->bias_[0]);
828   const __m128i bias8 = _mm_loadu_si128((__m128i*)&mtx->bias_[8]);
829   const __m128i q0 = _mm_loadu_si128((__m128i*)&mtx->q_[0]);
830   const __m128i q8 = _mm_loadu_si128((__m128i*)&mtx->q_[8]);
831   const __m128i zthresh0 = _mm_loadu_si128((__m128i*)&mtx->zthresh_[0]);
832   const __m128i zthresh8 = _mm_loadu_si128((__m128i*)&mtx->zthresh_[8]);
833
834   // sign(in) = in >> 15  (0x0000 if positive, 0xffff if negative)
835   const __m128i sign0 = _mm_srai_epi16(in0, 15);
836   const __m128i sign8 = _mm_srai_epi16(in8, 15);
837
838   // coeff = abs(in) = (in ^ sign) - sign
839   coeff0 = _mm_xor_si128(in0, sign0);
840   coeff8 = _mm_xor_si128(in8, sign8);
841   coeff0 = _mm_sub_epi16(coeff0, sign0);
842   coeff8 = _mm_sub_epi16(coeff8, sign8);
843
844   // coeff = abs(in) + sharpen
845   coeff0 = _mm_add_epi16(coeff0, sharpen0);
846   coeff8 = _mm_add_epi16(coeff8, sharpen8);
847
848   // out = (coeff * iQ + B) >> QFIX;
849   {
850     // doing calculations with 32b precision (QFIX=17)
851     // out = (coeff * iQ)
852     __m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0);
853     __m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0);
854     __m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8);
855     __m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8);
856     __m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H);
857     __m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H);
858     __m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H);
859     __m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H);
860     // expand bias from 16b to 32b
861     __m128i bias_00 = _mm_unpacklo_epi16(bias0, zero);
862     __m128i bias_04 = _mm_unpackhi_epi16(bias0, zero);
863     __m128i bias_08 = _mm_unpacklo_epi16(bias8, zero);
864     __m128i bias_12 = _mm_unpackhi_epi16(bias8, zero);
865     // out = (coeff * iQ + B)
866     out_00 = _mm_add_epi32(out_00, bias_00);
867     out_04 = _mm_add_epi32(out_04, bias_04);
868     out_08 = _mm_add_epi32(out_08, bias_08);
869     out_12 = _mm_add_epi32(out_12, bias_12);
870     // out = (coeff * iQ + B) >> QFIX;
871     out_00 = _mm_srai_epi32(out_00, QFIX);
872     out_04 = _mm_srai_epi32(out_04, QFIX);
873     out_08 = _mm_srai_epi32(out_08, QFIX);
874     out_12 = _mm_srai_epi32(out_12, QFIX);
875
876     // pack result as 16b
877     out0 = _mm_packs_epi32(out_00, out_04);
878     out8 = _mm_packs_epi32(out_08, out_12);
879
880     // if (coeff > 2047) coeff = 2047
881     out0 = _mm_min_epi16(out0, max_coeff_2047);
882     out8 = _mm_min_epi16(out8, max_coeff_2047);
883   }
884
885   // get sign back (if (sign[j]) out_n = -out_n)
886   out0 = _mm_xor_si128(out0, sign0);
887   out8 = _mm_xor_si128(out8, sign8);
888   out0 = _mm_sub_epi16(out0, sign0);
889   out8 = _mm_sub_epi16(out8, sign8);
890
891   // in = out * Q
892   in0 = _mm_mullo_epi16(out0, q0);
893   in8 = _mm_mullo_epi16(out8, q8);
894
895   // if (coeff <= mtx->zthresh_) {in=0; out=0;}
896   {
897     __m128i cmp0 = _mm_cmpgt_epi16(coeff0, zthresh0);
898     __m128i cmp8 = _mm_cmpgt_epi16(coeff8, zthresh8);
899     in0 = _mm_and_si128(in0, cmp0);
900     in8 = _mm_and_si128(in8, cmp8);
901     _mm_storeu_si128((__m128i*)&in[0], in0);
902     _mm_storeu_si128((__m128i*)&in[8], in8);
903     out0 = _mm_and_si128(out0, cmp0);
904     out8 = _mm_and_si128(out8, cmp8);
905   }
906
907   // zigzag the output before storing it.
908   //
909   // The zigzag pattern can almost be reproduced with a small sequence of
910   // shuffles. After it, we only need to swap the 7th (ending up in third
911   // position instead of twelfth) and 8th values.
912   {
913     __m128i outZ0, outZ8;
914     outZ0 = _mm_shufflehi_epi16(out0,  _MM_SHUFFLE(2, 1, 3, 0));
915     outZ0 = _mm_shuffle_epi32  (outZ0, _MM_SHUFFLE(3, 1, 2, 0));
916     outZ0 = _mm_shufflehi_epi16(outZ0, _MM_SHUFFLE(3, 1, 0, 2));
917     outZ8 = _mm_shufflelo_epi16(out8,  _MM_SHUFFLE(3, 0, 2, 1));
918     outZ8 = _mm_shuffle_epi32  (outZ8, _MM_SHUFFLE(3, 1, 2, 0));
919     outZ8 = _mm_shufflelo_epi16(outZ8, _MM_SHUFFLE(1, 3, 2, 0));
920     _mm_storeu_si128((__m128i*)&out[0], outZ0);
921     _mm_storeu_si128((__m128i*)&out[8], outZ8);
922     packed_out = _mm_packs_epi16(outZ0, outZ8);
923   }
924   {
925     const int16_t outZ_12 = out[12];
926     const int16_t outZ_3 = out[3];
927     out[3] = outZ_12;
928     out[12] = outZ_3;
929   }
930
931   // detect if all 'out' values are zeroes or not
932   {
933     int32_t tmp[4];
934     _mm_storeu_si128((__m128i*)tmp, packed_out);
935     if (n) {
936       tmp[0] &= ~0xff;
937     }
938     return (tmp[3] || tmp[2] || tmp[1] || tmp[0]);
939   }
940 }
941
942 #endif   // WEBP_USE_SSE2
943
944 //------------------------------------------------------------------------------
945 // Entry point
946
947 extern void VP8EncDspInitSSE2(void);
948
949 void VP8EncDspInitSSE2(void) {
950 #if defined(WEBP_USE_SSE2)
951   VP8CollectHistogram = CollectHistogramSSE2;
952   VP8EncQuantizeBlock = QuantizeBlockSSE2;
953   VP8ITransform = ITransformSSE2;
954   VP8FTransform = FTransformSSE2;
955   VP8FTransformWHT = FTransformWHTSSE2;
956   VP8SSE16x16 = SSE16x16SSE2;
957   VP8SSE16x8 = SSE16x8SSE2;
958   VP8SSE8x8 = SSE8x8SSE2;
959   VP8SSE4x4 = SSE4x4SSE2;
960   VP8TDisto4x4 = Disto4x4SSE2;
961   VP8TDisto16x16 = Disto16x16SSE2;
962 #endif   // WEBP_USE_SSE2
963 }
964
965 #if defined(__cplusplus) || defined(c_plusplus)
966 }    // extern "C"
967 #endif