• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // SSE2 variant of methods for lossless encoder
11 //
12 // Author: Skal (pascal.massimino@gmail.com)
13 
14 #include "src/dsp/dsp.h"
15 
16 #if defined(WEBP_USE_SSE2)
17 #include <assert.h>
18 #include <emmintrin.h>
19 #include "src/dsp/lossless.h"
20 #include "src/dsp/common_sse2.h"
21 #include "src/dsp/lossless_common.h"
22 
23 // For sign-extended multiplying constants, pre-shifted by 5:
24 #define CST_5b(X)  (((int16_t)((uint16_t)(X) << 8)) >> 5)
25 
26 //------------------------------------------------------------------------------
27 // Subtract-Green Transform
28 
SubtractGreenFromBlueAndRed_SSE2(uint32_t * argb_data,int num_pixels)29 static void SubtractGreenFromBlueAndRed_SSE2(uint32_t* argb_data,
30                                              int num_pixels) {
31   int i;
32   for (i = 0; i + 4 <= num_pixels; i += 4) {
33     const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]); // argb
34     const __m128i A = _mm_srli_epi16(in, 8);     // 0 a 0 g
35     const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0));
36     const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0));  // 0g0g
37     const __m128i out = _mm_sub_epi8(in, C);
38     _mm_storeu_si128((__m128i*)&argb_data[i], out);
39   }
40   // fallthrough and finish off with plain-C
41   if (i != num_pixels) {
42     VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);
43   }
44 }
45 
46 //------------------------------------------------------------------------------
47 // Color Transform
48 
49 #define MK_CST_16(HI, LO) \
50   _mm_set1_epi32((int)(((uint32_t)(HI) << 16) | ((LO) & 0xffff)))
51 
TransformColor_SSE2(const VP8LMultipliers * const m,uint32_t * argb_data,int num_pixels)52 static void TransformColor_SSE2(const VP8LMultipliers* const m,
53                                 uint32_t* argb_data, int num_pixels) {
54   const __m128i mults_rb = MK_CST_16(CST_5b(m->green_to_red_),
55                                      CST_5b(m->green_to_blue_));
56   const __m128i mults_b2 = MK_CST_16(CST_5b(m->red_to_blue_), 0);
57   const __m128i mask_ag = _mm_set1_epi32(0xff00ff00);  // alpha-green masks
58   const __m128i mask_rb = _mm_set1_epi32(0x00ff00ff);  // red-blue masks
59   int i;
60   for (i = 0; i + 4 <= num_pixels; i += 4) {
61     const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]); // argb
62     const __m128i A = _mm_and_si128(in, mask_ag);     // a   0   g   0
63     const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0));
64     const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0));  // g0g0
65     const __m128i D = _mm_mulhi_epi16(C, mults_rb);    // x dr  x db1
66     const __m128i E = _mm_slli_epi16(in, 8);           // r 0   b   0
67     const __m128i F = _mm_mulhi_epi16(E, mults_b2);    // x db2 0   0
68     const __m128i G = _mm_srli_epi32(F, 16);           // 0 0   x db2
69     const __m128i H = _mm_add_epi8(G, D);              // x dr  x  db
70     const __m128i I = _mm_and_si128(H, mask_rb);       // 0 dr  0  db
71     const __m128i out = _mm_sub_epi8(in, I);
72     _mm_storeu_si128((__m128i*)&argb_data[i], out);
73   }
74   // fallthrough and finish off with plain-C
75   if (i != num_pixels) {
76     VP8LTransformColor_C(m, argb_data + i, num_pixels - i);
77   }
78 }
79 
80 //------------------------------------------------------------------------------
81 #define SPAN 8
CollectColorBlueTransforms_SSE2(const uint32_t * argb,int stride,int tile_width,int tile_height,int green_to_blue,int red_to_blue,int histo[])82 static void CollectColorBlueTransforms_SSE2(const uint32_t* argb, int stride,
83                                             int tile_width, int tile_height,
84                                             int green_to_blue, int red_to_blue,
85                                             int histo[]) {
86   const __m128i mults_r = MK_CST_16(CST_5b(red_to_blue), 0);
87   const __m128i mults_g = MK_CST_16(0, CST_5b(green_to_blue));
88   const __m128i mask_g = _mm_set1_epi32(0x00ff00);  // green mask
89   const __m128i mask_b = _mm_set1_epi32(0x0000ff);  // blue mask
90   int y;
91   for (y = 0; y < tile_height; ++y) {
92     const uint32_t* const src = argb + y * stride;
93     int i, x;
94     for (x = 0; x + SPAN <= tile_width; x += SPAN) {
95       uint16_t values[SPAN];
96       const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x +        0]);
97       const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);
98       const __m128i A0 = _mm_slli_epi16(in0, 8);        // r 0  | b 0
99       const __m128i A1 = _mm_slli_epi16(in1, 8);
100       const __m128i B0 = _mm_and_si128(in0, mask_g);    // 0 0  | g 0
101       const __m128i B1 = _mm_and_si128(in1, mask_g);
102       const __m128i C0 = _mm_mulhi_epi16(A0, mults_r);  // x db | 0 0
103       const __m128i C1 = _mm_mulhi_epi16(A1, mults_r);
104       const __m128i D0 = _mm_mulhi_epi16(B0, mults_g);  // 0 0  | x db
105       const __m128i D1 = _mm_mulhi_epi16(B1, mults_g);
106       const __m128i E0 = _mm_sub_epi8(in0, D0);         // x x  | x b'
107       const __m128i E1 = _mm_sub_epi8(in1, D1);
108       const __m128i F0 = _mm_srli_epi32(C0, 16);        // 0 0  | x db
109       const __m128i F1 = _mm_srli_epi32(C1, 16);
110       const __m128i G0 = _mm_sub_epi8(E0, F0);          // 0 0  | x b'
111       const __m128i G1 = _mm_sub_epi8(E1, F1);
112       const __m128i H0 = _mm_and_si128(G0, mask_b);     // 0 0  | 0 b
113       const __m128i H1 = _mm_and_si128(G1, mask_b);
114       const __m128i I = _mm_packs_epi32(H0, H1);        // 0 b' | 0 b'
115       _mm_storeu_si128((__m128i*)values, I);
116       for (i = 0; i < SPAN; ++i) ++histo[values[i]];
117     }
118   }
119   {
120     const int left_over = tile_width & (SPAN - 1);
121     if (left_over > 0) {
122       VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,
123                                        left_over, tile_height,
124                                        green_to_blue, red_to_blue, histo);
125     }
126   }
127 }
128 
CollectColorRedTransforms_SSE2(const uint32_t * argb,int stride,int tile_width,int tile_height,int green_to_red,int histo[])129 static void CollectColorRedTransforms_SSE2(const uint32_t* argb, int stride,
130                                            int tile_width, int tile_height,
131                                            int green_to_red, int histo[]) {
132   const __m128i mults_g = MK_CST_16(0, CST_5b(green_to_red));
133   const __m128i mask_g = _mm_set1_epi32(0x00ff00);  // green mask
134   const __m128i mask = _mm_set1_epi32(0xff);
135 
136   int y;
137   for (y = 0; y < tile_height; ++y) {
138     const uint32_t* const src = argb + y * stride;
139     int i, x;
140     for (x = 0; x + SPAN <= tile_width; x += SPAN) {
141       uint16_t values[SPAN];
142       const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x +        0]);
143       const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);
144       const __m128i A0 = _mm_and_si128(in0, mask_g);    // 0 0  | g 0
145       const __m128i A1 = _mm_and_si128(in1, mask_g);
146       const __m128i B0 = _mm_srli_epi32(in0, 16);       // 0 0  | x r
147       const __m128i B1 = _mm_srli_epi32(in1, 16);
148       const __m128i C0 = _mm_mulhi_epi16(A0, mults_g);  // 0 0  | x dr
149       const __m128i C1 = _mm_mulhi_epi16(A1, mults_g);
150       const __m128i E0 = _mm_sub_epi8(B0, C0);          // x x  | x r'
151       const __m128i E1 = _mm_sub_epi8(B1, C1);
152       const __m128i F0 = _mm_and_si128(E0, mask);       // 0 0  | 0 r'
153       const __m128i F1 = _mm_and_si128(E1, mask);
154       const __m128i I = _mm_packs_epi32(F0, F1);
155       _mm_storeu_si128((__m128i*)values, I);
156       for (i = 0; i < SPAN; ++i) ++histo[values[i]];
157     }
158   }
159   {
160     const int left_over = tile_width & (SPAN - 1);
161     if (left_over > 0) {
162       VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,
163                                       left_over, tile_height,
164                                       green_to_red, histo);
165     }
166   }
167 }
168 #undef SPAN
169 #undef MK_CST_16
170 
171 //------------------------------------------------------------------------------
172 
173 // Note we are adding uint32_t's as *signed* int32's (using _mm_add_epi32). But
174 // that's ok since the histogram values are less than 1<<28 (max picture size).
175 #define LINE_SIZE 16    // 8 or 16
AddVector_SSE2(const uint32_t * a,const uint32_t * b,uint32_t * out,int size)176 static void AddVector_SSE2(const uint32_t* a, const uint32_t* b, uint32_t* out,
177                            int size) {
178   int i;
179   for (i = 0; i + LINE_SIZE <= size; i += LINE_SIZE) {
180     const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i +  0]);
181     const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i +  4]);
182 #if (LINE_SIZE == 16)
183     const __m128i a2 = _mm_loadu_si128((const __m128i*)&a[i +  8]);
184     const __m128i a3 = _mm_loadu_si128((const __m128i*)&a[i + 12]);
185 #endif
186     const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[i +  0]);
187     const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[i +  4]);
188 #if (LINE_SIZE == 16)
189     const __m128i b2 = _mm_loadu_si128((const __m128i*)&b[i +  8]);
190     const __m128i b3 = _mm_loadu_si128((const __m128i*)&b[i + 12]);
191 #endif
192     _mm_storeu_si128((__m128i*)&out[i +  0], _mm_add_epi32(a0, b0));
193     _mm_storeu_si128((__m128i*)&out[i +  4], _mm_add_epi32(a1, b1));
194 #if (LINE_SIZE == 16)
195     _mm_storeu_si128((__m128i*)&out[i +  8], _mm_add_epi32(a2, b2));
196     _mm_storeu_si128((__m128i*)&out[i + 12], _mm_add_epi32(a3, b3));
197 #endif
198   }
199   for (; i < size; ++i) {
200     out[i] = a[i] + b[i];
201   }
202 }
203 
AddVectorEq_SSE2(const uint32_t * a,uint32_t * out,int size)204 static void AddVectorEq_SSE2(const uint32_t* a, uint32_t* out, int size) {
205   int i;
206   for (i = 0; i + LINE_SIZE <= size; i += LINE_SIZE) {
207     const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i +  0]);
208     const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i +  4]);
209 #if (LINE_SIZE == 16)
210     const __m128i a2 = _mm_loadu_si128((const __m128i*)&a[i +  8]);
211     const __m128i a3 = _mm_loadu_si128((const __m128i*)&a[i + 12]);
212 #endif
213     const __m128i b0 = _mm_loadu_si128((const __m128i*)&out[i +  0]);
214     const __m128i b1 = _mm_loadu_si128((const __m128i*)&out[i +  4]);
215 #if (LINE_SIZE == 16)
216     const __m128i b2 = _mm_loadu_si128((const __m128i*)&out[i +  8]);
217     const __m128i b3 = _mm_loadu_si128((const __m128i*)&out[i + 12]);
218 #endif
219     _mm_storeu_si128((__m128i*)&out[i +  0], _mm_add_epi32(a0, b0));
220     _mm_storeu_si128((__m128i*)&out[i +  4], _mm_add_epi32(a1, b1));
221 #if (LINE_SIZE == 16)
222     _mm_storeu_si128((__m128i*)&out[i +  8], _mm_add_epi32(a2, b2));
223     _mm_storeu_si128((__m128i*)&out[i + 12], _mm_add_epi32(a3, b3));
224 #endif
225   }
226   for (; i < size; ++i) {
227     out[i] += a[i];
228   }
229 }
230 #undef LINE_SIZE
231 
232 //------------------------------------------------------------------------------
233 // Entropy
234 
235 // Checks whether the X or Y contribution is worth computing and adding.
236 // Used in loop unrolling.
237 #define ANALYZE_X_OR_Y(x_or_y, j)                                           \
238   do {                                                                      \
239     if ((x_or_y)[i + (j)] != 0) retval -= VP8LFastSLog2((x_or_y)[i + (j)]); \
240   } while (0)
241 
242 // Checks whether the X + Y contribution is worth computing and adding.
243 // Used in loop unrolling.
244 #define ANALYZE_XY(j)                  \
245   do {                                 \
246     if (tmp[j] != 0) {                 \
247       retval -= VP8LFastSLog2(tmp[j]); \
248       ANALYZE_X_OR_Y(X, j);            \
249     }                                  \
250   } while (0)
251 
252 #if !(defined(__i386__) || defined(_M_IX86))
CombinedShannonEntropy_SSE2(const int X[256],const int Y[256])253 static float CombinedShannonEntropy_SSE2(const int X[256], const int Y[256]) {
254   int i;
255   double retval = 0.;
256   int sumX, sumXY;
257   int32_t tmp[4];
258   __m128i zero = _mm_setzero_si128();
259   // Sums up X + Y, 4 ints at a time (and will merge it at the end for sumXY).
260   __m128i sumXY_128 = zero;
261   __m128i sumX_128 = zero;
262 
263   for (i = 0; i < 256; i += 4) {
264     const __m128i x = _mm_loadu_si128((const __m128i*)(X + i));
265     const __m128i y = _mm_loadu_si128((const __m128i*)(Y + i));
266 
267     // Check if any X is non-zero: this actually provides a speedup as X is
268     // usually sparse.
269     if (_mm_movemask_epi8(_mm_cmpeq_epi32(x, zero)) != 0xFFFF) {
270       const __m128i xy_128 = _mm_add_epi32(x, y);
271       sumXY_128 = _mm_add_epi32(sumXY_128, xy_128);
272 
273       sumX_128 = _mm_add_epi32(sumX_128, x);
274 
275       // Analyze the different X + Y.
276       _mm_storeu_si128((__m128i*)tmp, xy_128);
277 
278       ANALYZE_XY(0);
279       ANALYZE_XY(1);
280       ANALYZE_XY(2);
281       ANALYZE_XY(3);
282     } else {
283       // X is fully 0, so only deal with Y.
284       sumXY_128 = _mm_add_epi32(sumXY_128, y);
285 
286       ANALYZE_X_OR_Y(Y, 0);
287       ANALYZE_X_OR_Y(Y, 1);
288       ANALYZE_X_OR_Y(Y, 2);
289       ANALYZE_X_OR_Y(Y, 3);
290     }
291   }
292 
293   // Sum up sumX_128 to get sumX.
294   _mm_storeu_si128((__m128i*)tmp, sumX_128);
295   sumX = tmp[3] + tmp[2] + tmp[1] + tmp[0];
296 
297   // Sum up sumXY_128 to get sumXY.
298   _mm_storeu_si128((__m128i*)tmp, sumXY_128);
299   sumXY = tmp[3] + tmp[2] + tmp[1] + tmp[0];
300 
301   retval += VP8LFastSLog2(sumX) + VP8LFastSLog2(sumXY);
302   return (float)retval;
303 }
304 #endif  // !(defined(__i386__) || defined(_M_IX86))
305 
306 #undef ANALYZE_X_OR_Y
307 #undef ANALYZE_XY
308 
309 //------------------------------------------------------------------------------
310 
VectorMismatch_SSE2(const uint32_t * const array1,const uint32_t * const array2,int length)311 static int VectorMismatch_SSE2(const uint32_t* const array1,
312                                const uint32_t* const array2, int length) {
313   int match_len;
314 
315   if (length >= 12) {
316     __m128i A0 = _mm_loadu_si128((const __m128i*)&array1[0]);
317     __m128i A1 = _mm_loadu_si128((const __m128i*)&array2[0]);
318     match_len = 0;
319     do {
320       // Loop unrolling and early load both provide a speedup of 10% for the
321       // current function. Also, max_limit can be MAX_LENGTH=4096 at most.
322       const __m128i cmpA = _mm_cmpeq_epi32(A0, A1);
323       const __m128i B0 =
324           _mm_loadu_si128((const __m128i*)&array1[match_len + 4]);
325       const __m128i B1 =
326           _mm_loadu_si128((const __m128i*)&array2[match_len + 4]);
327       if (_mm_movemask_epi8(cmpA) != 0xffff) break;
328       match_len += 4;
329 
330       {
331         const __m128i cmpB = _mm_cmpeq_epi32(B0, B1);
332         A0 = _mm_loadu_si128((const __m128i*)&array1[match_len + 4]);
333         A1 = _mm_loadu_si128((const __m128i*)&array2[match_len + 4]);
334         if (_mm_movemask_epi8(cmpB) != 0xffff) break;
335         match_len += 4;
336       }
337     } while (match_len + 12 < length);
338   } else {
339     match_len = 0;
340     // Unroll the potential first two loops.
341     if (length >= 4 &&
342         _mm_movemask_epi8(_mm_cmpeq_epi32(
343             _mm_loadu_si128((const __m128i*)&array1[0]),
344             _mm_loadu_si128((const __m128i*)&array2[0]))) == 0xffff) {
345       match_len = 4;
346       if (length >= 8 &&
347           _mm_movemask_epi8(_mm_cmpeq_epi32(
348               _mm_loadu_si128((const __m128i*)&array1[4]),
349               _mm_loadu_si128((const __m128i*)&array2[4]))) == 0xffff) {
350         match_len = 8;
351       }
352     }
353   }
354 
355   while (match_len < length && array1[match_len] == array2[match_len]) {
356     ++match_len;
357   }
358   return match_len;
359 }
360 
361 // Bundles multiple (1, 2, 4 or 8) pixels into a single pixel.
BundleColorMap_SSE2(const uint8_t * const row,int width,int xbits,uint32_t * dst)362 static void BundleColorMap_SSE2(const uint8_t* const row, int width, int xbits,
363                                 uint32_t* dst) {
364   int x;
365   assert(xbits >= 0);
366   assert(xbits <= 3);
367   switch (xbits) {
368     case 0: {
369       const __m128i ff = _mm_set1_epi16((short)0xff00);
370       const __m128i zero = _mm_setzero_si128();
371       // Store 0xff000000 | (row[x] << 8).
372       for (x = 0; x + 16 <= width; x += 16, dst += 16) {
373         const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]);
374         const __m128i in_lo = _mm_unpacklo_epi8(zero, in);
375         const __m128i dst0 = _mm_unpacklo_epi16(in_lo, ff);
376         const __m128i dst1 = _mm_unpackhi_epi16(in_lo, ff);
377         const __m128i in_hi = _mm_unpackhi_epi8(zero, in);
378         const __m128i dst2 = _mm_unpacklo_epi16(in_hi, ff);
379         const __m128i dst3 = _mm_unpackhi_epi16(in_hi, ff);
380         _mm_storeu_si128((__m128i*)&dst[0], dst0);
381         _mm_storeu_si128((__m128i*)&dst[4], dst1);
382         _mm_storeu_si128((__m128i*)&dst[8], dst2);
383         _mm_storeu_si128((__m128i*)&dst[12], dst3);
384       }
385       break;
386     }
387     case 1: {
388       const __m128i ff = _mm_set1_epi16((short)0xff00);
389       const __m128i mul = _mm_set1_epi16(0x110);
390       for (x = 0; x + 16 <= width; x += 16, dst += 8) {
391         // 0a0b | (where a/b are 4 bits).
392         const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]);
393         const __m128i tmp = _mm_mullo_epi16(in, mul);  // aba0
394         const __m128i pack = _mm_and_si128(tmp, ff);   // ab00
395         const __m128i dst0 = _mm_unpacklo_epi16(pack, ff);
396         const __m128i dst1 = _mm_unpackhi_epi16(pack, ff);
397         _mm_storeu_si128((__m128i*)&dst[0], dst0);
398         _mm_storeu_si128((__m128i*)&dst[4], dst1);
399       }
400       break;
401     }
402     case 2: {
403       const __m128i mask_or = _mm_set1_epi32(0xff000000);
404       const __m128i mul_cst = _mm_set1_epi16(0x0104);
405       const __m128i mask_mul = _mm_set1_epi16(0x0f00);
406       for (x = 0; x + 16 <= width; x += 16, dst += 4) {
407         // 000a000b000c000d | (where a/b/c/d are 2 bits).
408         const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]);
409         const __m128i mul = _mm_mullo_epi16(in, mul_cst);  // 00ab00b000cd00d0
410         const __m128i tmp = _mm_and_si128(mul, mask_mul);  // 00ab000000cd0000
411         const __m128i shift = _mm_srli_epi32(tmp, 12);     // 00000000ab000000
412         const __m128i pack = _mm_or_si128(shift, tmp);     // 00000000abcd0000
413         // Convert to 0xff00**00.
414         const __m128i res = _mm_or_si128(pack, mask_or);
415         _mm_storeu_si128((__m128i*)dst, res);
416       }
417       break;
418     }
419     default: {
420       assert(xbits == 3);
421       for (x = 0; x + 16 <= width; x += 16, dst += 2) {
422         // 0000000a00000000b... | (where a/b are 1 bit).
423         const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]);
424         const __m128i shift = _mm_slli_epi64(in, 7);
425         const uint32_t move = _mm_movemask_epi8(shift);
426         dst[0] = 0xff000000 | ((move & 0xff) << 8);
427         dst[1] = 0xff000000 | (move & 0xff00);
428       }
429       break;
430     }
431   }
432   if (x != width) {
433     VP8LBundleColorMap_C(row + x, width - x, xbits, dst);
434   }
435 }
436 
437 //------------------------------------------------------------------------------
438 // Batch version of Predictor Transform subtraction
439 
Average2_m128i(const __m128i * const a0,const __m128i * const a1,__m128i * const avg)440 static WEBP_INLINE void Average2_m128i(const __m128i* const a0,
441                                        const __m128i* const a1,
442                                        __m128i* const avg) {
443   // (a + b) >> 1 = ((a + b + 1) >> 1) - ((a ^ b) & 1)
444   const __m128i ones = _mm_set1_epi8(1);
445   const __m128i avg1 = _mm_avg_epu8(*a0, *a1);
446   const __m128i one = _mm_and_si128(_mm_xor_si128(*a0, *a1), ones);
447   *avg = _mm_sub_epi8(avg1, one);
448 }
449 
450 // Predictor0: ARGB_BLACK.
PredictorSub0_SSE2(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)451 static void PredictorSub0_SSE2(const uint32_t* in, const uint32_t* upper,
452                                int num_pixels, uint32_t* out) {
453   int i;
454   const __m128i black = _mm_set1_epi32(ARGB_BLACK);
455   for (i = 0; i + 4 <= num_pixels; i += 4) {
456     const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
457     const __m128i res = _mm_sub_epi8(src, black);
458     _mm_storeu_si128((__m128i*)&out[i], res);
459   }
460   if (i != num_pixels) {
461     VP8LPredictorsSub_C[0](in + i, NULL, num_pixels - i, out + i);
462   }
463   (void)upper;
464 }
465 
466 #define GENERATE_PREDICTOR_1(X, IN)                                         \
467   static void PredictorSub##X##_SSE2(const uint32_t* const in,              \
468                                      const uint32_t* const upper,           \
469                                      int num_pixels, uint32_t* const out) { \
470     int i;                                                                  \
471     for (i = 0; i + 4 <= num_pixels; i += 4) {                              \
472       const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);          \
473       const __m128i pred = _mm_loadu_si128((const __m128i*)&(IN));          \
474       const __m128i res = _mm_sub_epi8(src, pred);                          \
475       _mm_storeu_si128((__m128i*)&out[i], res);                             \
476     }                                                                       \
477     if (i != num_pixels) {                                                  \
478       VP8LPredictorsSub_C[(X)](in + i, WEBP_OFFSET_PTR(upper, i),           \
479                                num_pixels - i, out + i);                    \
480     }                                                                       \
481   }
482 
483 GENERATE_PREDICTOR_1(1, in[i - 1])       // Predictor1: L
484 GENERATE_PREDICTOR_1(2, upper[i])        // Predictor2: T
485 GENERATE_PREDICTOR_1(3, upper[i + 1])    // Predictor3: TR
486 GENERATE_PREDICTOR_1(4, upper[i - 1])    // Predictor4: TL
487 #undef GENERATE_PREDICTOR_1
488 
489 // Predictor5: avg2(avg2(L, TR), T)
PredictorSub5_SSE2(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)490 static void PredictorSub5_SSE2(const uint32_t* in, const uint32_t* upper,
491                                int num_pixels, uint32_t* out) {
492   int i;
493   for (i = 0; i + 4 <= num_pixels; i += 4) {
494     const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]);
495     const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
496     const __m128i TR = _mm_loadu_si128((const __m128i*)&upper[i + 1]);
497     const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
498     __m128i avg, pred, res;
499     Average2_m128i(&L, &TR, &avg);
500     Average2_m128i(&avg, &T, &pred);
501     res = _mm_sub_epi8(src, pred);
502     _mm_storeu_si128((__m128i*)&out[i], res);
503   }
504   if (i != num_pixels) {
505     VP8LPredictorsSub_C[5](in + i, upper + i, num_pixels - i, out + i);
506   }
507 }
508 
509 #define GENERATE_PREDICTOR_2(X, A, B)                                         \
510 static void PredictorSub##X##_SSE2(const uint32_t* in, const uint32_t* upper, \
511                                    int num_pixels, uint32_t* out) {           \
512   int i;                                                                      \
513   for (i = 0; i + 4 <= num_pixels; i += 4) {                                  \
514     const __m128i tA = _mm_loadu_si128((const __m128i*)&(A));                 \
515     const __m128i tB = _mm_loadu_si128((const __m128i*)&(B));                 \
516     const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);              \
517     __m128i pred, res;                                                        \
518     Average2_m128i(&tA, &tB, &pred);                                          \
519     res = _mm_sub_epi8(src, pred);                                            \
520     _mm_storeu_si128((__m128i*)&out[i], res);                                 \
521   }                                                                           \
522   if (i != num_pixels) {                                                      \
523     VP8LPredictorsSub_C[(X)](in + i, upper + i, num_pixels - i, out + i);     \
524   }                                                                           \
525 }
526 
527 GENERATE_PREDICTOR_2(6, in[i - 1], upper[i - 1])   // Predictor6: avg(L, TL)
528 GENERATE_PREDICTOR_2(7, in[i - 1], upper[i])       // Predictor7: avg(L, T)
529 GENERATE_PREDICTOR_2(8, upper[i - 1], upper[i])    // Predictor8: avg(TL, T)
530 GENERATE_PREDICTOR_2(9, upper[i], upper[i + 1])    // Predictor9: average(T, TR)
531 #undef GENERATE_PREDICTOR_2
532 
533 // Predictor10: avg(avg(L,TL), avg(T, TR)).
PredictorSub10_SSE2(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)534 static void PredictorSub10_SSE2(const uint32_t* in, const uint32_t* upper,
535                                 int num_pixels, uint32_t* out) {
536   int i;
537   for (i = 0; i + 4 <= num_pixels; i += 4) {
538     const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]);
539     const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
540     const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]);
541     const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
542     const __m128i TR = _mm_loadu_si128((const __m128i*)&upper[i + 1]);
543     __m128i avgTTR, avgLTL, avg, res;
544     Average2_m128i(&T, &TR, &avgTTR);
545     Average2_m128i(&L, &TL, &avgLTL);
546     Average2_m128i(&avgTTR, &avgLTL, &avg);
547     res = _mm_sub_epi8(src, avg);
548     _mm_storeu_si128((__m128i*)&out[i], res);
549   }
550   if (i != num_pixels) {
551     VP8LPredictorsSub_C[10](in + i, upper + i, num_pixels - i, out + i);
552   }
553 }
554 
555 // Predictor11: select.
GetSumAbsDiff32_SSE2(const __m128i * const A,const __m128i * const B,__m128i * const out)556 static void GetSumAbsDiff32_SSE2(const __m128i* const A, const __m128i* const B,
557                                  __m128i* const out) {
558   // We can unpack with any value on the upper 32 bits, provided it's the same
559   // on both operands (to that their sum of abs diff is zero). Here we use *A.
560   const __m128i A_lo = _mm_unpacklo_epi32(*A, *A);
561   const __m128i B_lo = _mm_unpacklo_epi32(*B, *A);
562   const __m128i A_hi = _mm_unpackhi_epi32(*A, *A);
563   const __m128i B_hi = _mm_unpackhi_epi32(*B, *A);
564   const __m128i s_lo = _mm_sad_epu8(A_lo, B_lo);
565   const __m128i s_hi = _mm_sad_epu8(A_hi, B_hi);
566   *out = _mm_packs_epi32(s_lo, s_hi);
567 }
568 
PredictorSub11_SSE2(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)569 static void PredictorSub11_SSE2(const uint32_t* in, const uint32_t* upper,
570                                 int num_pixels, uint32_t* out) {
571   int i;
572   for (i = 0; i + 4 <= num_pixels; i += 4) {
573     const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]);
574     const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
575     const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]);
576     const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
577     __m128i pa, pb;
578     GetSumAbsDiff32_SSE2(&T, &TL, &pa);   // pa = sum |T-TL|
579     GetSumAbsDiff32_SSE2(&L, &TL, &pb);   // pb = sum |L-TL|
580     {
581       const __m128i mask = _mm_cmpgt_epi32(pb, pa);
582       const __m128i A = _mm_and_si128(mask, L);
583       const __m128i B = _mm_andnot_si128(mask, T);
584       const __m128i pred = _mm_or_si128(A, B);    // pred = (L > T)? L : T
585       const __m128i res = _mm_sub_epi8(src, pred);
586       _mm_storeu_si128((__m128i*)&out[i], res);
587     }
588   }
589   if (i != num_pixels) {
590     VP8LPredictorsSub_C[11](in + i, upper + i, num_pixels - i, out + i);
591   }
592 }
593 
594 // Predictor12: ClampedSubSubtractFull.
PredictorSub12_SSE2(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)595 static void PredictorSub12_SSE2(const uint32_t* in, const uint32_t* upper,
596                                 int num_pixels, uint32_t* out) {
597   int i;
598   const __m128i zero = _mm_setzero_si128();
599   for (i = 0; i + 4 <= num_pixels; i += 4) {
600     const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
601     const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]);
602     const __m128i L_lo = _mm_unpacklo_epi8(L, zero);
603     const __m128i L_hi = _mm_unpackhi_epi8(L, zero);
604     const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
605     const __m128i T_lo = _mm_unpacklo_epi8(T, zero);
606     const __m128i T_hi = _mm_unpackhi_epi8(T, zero);
607     const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]);
608     const __m128i TL_lo = _mm_unpacklo_epi8(TL, zero);
609     const __m128i TL_hi = _mm_unpackhi_epi8(TL, zero);
610     const __m128i diff_lo = _mm_sub_epi16(T_lo, TL_lo);
611     const __m128i diff_hi = _mm_sub_epi16(T_hi, TL_hi);
612     const __m128i pred_lo = _mm_add_epi16(L_lo, diff_lo);
613     const __m128i pred_hi = _mm_add_epi16(L_hi, diff_hi);
614     const __m128i pred = _mm_packus_epi16(pred_lo, pred_hi);
615     const __m128i res = _mm_sub_epi8(src, pred);
616     _mm_storeu_si128((__m128i*)&out[i], res);
617   }
618   if (i != num_pixels) {
619     VP8LPredictorsSub_C[12](in + i, upper + i, num_pixels - i, out + i);
620   }
621 }
622 
623 // Predictors13: ClampedAddSubtractHalf
PredictorSub13_SSE2(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)624 static void PredictorSub13_SSE2(const uint32_t* in, const uint32_t* upper,
625                                 int num_pixels, uint32_t* out) {
626   int i;
627   const __m128i zero = _mm_setzero_si128();
628   for (i = 0; i + 2 <= num_pixels; i += 2) {
629     // we can only process two pixels at a time
630     const __m128i L = _mm_loadl_epi64((const __m128i*)&in[i - 1]);
631     const __m128i src = _mm_loadl_epi64((const __m128i*)&in[i]);
632     const __m128i T = _mm_loadl_epi64((const __m128i*)&upper[i]);
633     const __m128i TL = _mm_loadl_epi64((const __m128i*)&upper[i - 1]);
634     const __m128i L_lo = _mm_unpacklo_epi8(L, zero);
635     const __m128i T_lo = _mm_unpacklo_epi8(T, zero);
636     const __m128i TL_lo = _mm_unpacklo_epi8(TL, zero);
637     const __m128i sum = _mm_add_epi16(T_lo, L_lo);
638     const __m128i avg = _mm_srli_epi16(sum, 1);
639     const __m128i A1 = _mm_sub_epi16(avg, TL_lo);
640     const __m128i bit_fix = _mm_cmpgt_epi16(TL_lo, avg);
641     const __m128i A2 = _mm_sub_epi16(A1, bit_fix);
642     const __m128i A3 = _mm_srai_epi16(A2, 1);
643     const __m128i A4 = _mm_add_epi16(avg, A3);
644     const __m128i pred = _mm_packus_epi16(A4, A4);
645     const __m128i res = _mm_sub_epi8(src, pred);
646     _mm_storel_epi64((__m128i*)&out[i], res);
647   }
648   if (i != num_pixels) {
649     VP8LPredictorsSub_C[13](in + i, upper + i, num_pixels - i, out + i);
650   }
651 }
652 
653 //------------------------------------------------------------------------------
654 // Entry point
655 
656 extern void VP8LEncDspInitSSE2(void);
657 
VP8LEncDspInitSSE2(void)658 WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE2(void) {
659   VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE2;
660   VP8LTransformColor = TransformColor_SSE2;
661   VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE2;
662   VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE2;
663   VP8LAddVector = AddVector_SSE2;
664   VP8LAddVectorEq = AddVectorEq_SSE2;
665   // TODO(https://crbug.com/webp/499): this function produces different results
666   // from the C code due to use of double/float resulting in output differences
667   // when compared to -noasm.
668 #if !(defined(__i386__) || defined(_M_IX86))
669   VP8LCombinedShannonEntropy = CombinedShannonEntropy_SSE2;
670 #endif
671   VP8LVectorMismatch = VectorMismatch_SSE2;
672   VP8LBundleColorMap = BundleColorMap_SSE2;
673 
674   VP8LPredictorsSub[0] = PredictorSub0_SSE2;
675   VP8LPredictorsSub[1] = PredictorSub1_SSE2;
676   VP8LPredictorsSub[2] = PredictorSub2_SSE2;
677   VP8LPredictorsSub[3] = PredictorSub3_SSE2;
678   VP8LPredictorsSub[4] = PredictorSub4_SSE2;
679   VP8LPredictorsSub[5] = PredictorSub5_SSE2;
680   VP8LPredictorsSub[6] = PredictorSub6_SSE2;
681   VP8LPredictorsSub[7] = PredictorSub7_SSE2;
682   VP8LPredictorsSub[8] = PredictorSub8_SSE2;
683   VP8LPredictorsSub[9] = PredictorSub9_SSE2;
684   VP8LPredictorsSub[10] = PredictorSub10_SSE2;
685   VP8LPredictorsSub[11] = PredictorSub11_SSE2;
686   VP8LPredictorsSub[12] = PredictorSub12_SSE2;
687   VP8LPredictorsSub[13] = PredictorSub13_SSE2;
688   VP8LPredictorsSub[14] = PredictorSub0_SSE2;  // <- padding security sentinels
689   VP8LPredictorsSub[15] = PredictorSub0_SSE2;
690 }
691 
692 #else  // !WEBP_USE_SSE2
693 
694 WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE2)
695 
696 #endif  // WEBP_USE_SSE2
697