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