Coverage Report

Created: 2026-07-20 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/dsp/lossless_enc_sse41.c
Line
Count
Source
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
// SSE4.1 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_SSE41)
17
#include <assert.h>
18
#include <emmintrin.h>
19
#include <smmintrin.h>
20
21
#include "src/dsp/cpu.h"
22
#include "src/dsp/lossless.h"
23
#include "src/webp/types.h"
24
25
//------------------------------------------------------------------------------
26
// Cost operations.
27
28
128k
static WEBP_INLINE uint32_t HorizontalSum_SSE41(__m128i cost) {
29
128k
  cost = _mm_add_epi32(cost, _mm_srli_si128(cost, 8));
30
128k
  cost = _mm_add_epi32(cost, _mm_srli_si128(cost, 4));
31
128k
  return _mm_cvtsi128_si32(cost);
32
128k
}
33
34
128k
static uint32_t ExtraCost_SSE41(const uint32_t* const a, int length) {
35
128k
  int i;
36
128k
  __m128i cost = _mm_set_epi32(2 * a[7], 2 * a[6], a[5], a[4]);
37
128k
  assert(length % 8 == 0);
38
39
514k
  for (i = 8; i + 8 <= length; i += 8) {
40
385k
    const int j = (i - 2) >> 1;
41
385k
    const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i]);
42
385k
    const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i + 4]);
43
385k
    const __m128i w = _mm_set_epi32(j + 3, j + 2, j + 1, j);
44
385k
    const __m128i a2 = _mm_hadd_epi32(a0, a1);
45
385k
    const __m128i mul = _mm_mullo_epi32(a2, w);
46
385k
    cost = _mm_add_epi32(mul, cost);
47
385k
  }
48
128k
  return HorizontalSum_SSE41(cost);
49
128k
}
50
51
//------------------------------------------------------------------------------
52
// Subtract-Green Transform
53
54
static void SubtractGreenFromBlueAndRed_SSE41(uint32_t* argb_data,
55
76
                                              int num_pixels) {
56
76
  int i;
57
76
  const __m128i kCstShuffle =
58
76
      _mm_set_epi8(-1, 13, -1, 13, -1, 9, -1, 9, -1, 5, -1, 5, -1, 1, -1, 1);
59
125
  for (i = 0; i + 4 <= num_pixels; i += 4) {
60
49
    const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]);
61
49
    const __m128i in_0g0g = _mm_shuffle_epi8(in, kCstShuffle);
62
49
    const __m128i out = _mm_sub_epi8(in, in_0g0g);
63
49
    _mm_storeu_si128((__m128i*)&argb_data[i], out);
64
49
  }
65
  // fallthrough and finish off with plain-C
66
76
  if (i != num_pixels) {
67
55
    VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);
68
55
  }
69
76
}
70
71
//------------------------------------------------------------------------------
72
// Color Transform
73
74
// For sign-extended multiplying constants, pre-shifted by 5:
75
#define CST_5b(X) (((int16_t)((uint16_t)(X) << 8)) >> 5)
76
77
#define MK_CST_16(HI, LO) \
78
518k
  _mm_set1_epi32((int)(((uint32_t)(HI) << 16) | ((LO) & 0xffff)))
79
80
static void CollectColorBlueTransforms_SSE41(const uint32_t* WEBP_RESTRICT argb,
81
                                             int stride, int tile_width,
82
                                             int tile_height, int green_to_blue,
83
                                             int red_to_blue,
84
400k
                                             uint32_t histo[]) {
85
400k
  const __m128i mult =
86
400k
      MK_CST_16(CST_5b(red_to_blue) + 256, CST_5b(green_to_blue));
87
400k
  const __m128i perm =
88
400k
      _mm_setr_epi8(-1, 1, -1, 2, -1, 5, -1, 6, -1, 9, -1, 10, -1, 13, -1, 14);
89
400k
  if (tile_width >= 4) {
90
216k
    int y;
91
4.06M
    for (y = 0; y < tile_height; ++y) {
92
3.84M
      const uint32_t* const src = argb + y * stride;
93
3.84M
      const __m128i A1 = _mm_loadu_si128((const __m128i*)src);
94
3.84M
      const __m128i B1 = _mm_shuffle_epi8(A1, perm);
95
3.84M
      const __m128i C1 = _mm_mulhi_epi16(B1, mult);
96
3.84M
      const __m128i D1 = _mm_sub_epi16(A1, C1);
97
3.84M
      __m128i E = _mm_add_epi16(_mm_srli_epi32(D1, 16), D1);
98
3.84M
      int x;
99
3.84M
      for (x = 4; x + 4 <= tile_width; x += 4) {
100
0
        const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x));
101
0
        __m128i B2, C2, D2;
102
0
        ++histo[_mm_extract_epi8(E, 0)];
103
0
        B2 = _mm_shuffle_epi8(A2, perm);
104
0
        ++histo[_mm_extract_epi8(E, 4)];
105
0
        C2 = _mm_mulhi_epi16(B2, mult);
106
0
        ++histo[_mm_extract_epi8(E, 8)];
107
0
        D2 = _mm_sub_epi16(A2, C2);
108
0
        ++histo[_mm_extract_epi8(E, 12)];
109
0
        E = _mm_add_epi16(_mm_srli_epi32(D2, 16), D2);
110
0
      }
111
3.84M
      ++histo[_mm_extract_epi8(E, 0)];
112
3.84M
      ++histo[_mm_extract_epi8(E, 4)];
113
3.84M
      ++histo[_mm_extract_epi8(E, 8)];
114
3.84M
      ++histo[_mm_extract_epi8(E, 12)];
115
3.84M
    }
116
216k
  }
117
400k
  {
118
400k
    const int left_over = tile_width & 3;
119
400k
    if (left_over > 0) {
120
337k
      VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,
121
337k
                                       left_over, tile_height, green_to_blue,
122
337k
                                       red_to_blue, histo);
123
337k
    }
124
400k
  }
125
400k
}
126
127
static void CollectColorRedTransforms_SSE41(const uint32_t* WEBP_RESTRICT argb,
128
                                            int stride, int tile_width,
129
                                            int tile_height, int green_to_red,
130
118k
                                            uint32_t histo[]) {
131
118k
  const __m128i mult = MK_CST_16(0, CST_5b(green_to_red));
132
118k
  const __m128i mask_g = _mm_set1_epi32(0x0000ff00);
133
118k
  if (tile_width >= 4) {
134
63.4k
    int y;
135
1.19M
    for (y = 0; y < tile_height; ++y) {
136
1.12M
      const uint32_t* const src = argb + y * stride;
137
1.12M
      const __m128i A1 = _mm_loadu_si128((const __m128i*)src);
138
1.12M
      const __m128i B1 = _mm_and_si128(A1, mask_g);
139
1.12M
      const __m128i C1 = _mm_madd_epi16(B1, mult);
140
1.12M
      __m128i D = _mm_sub_epi16(A1, C1);
141
1.12M
      int x;
142
1.12M
      for (x = 4; x + 4 <= tile_width; x += 4) {
143
0
        const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x));
144
0
        __m128i B2, C2;
145
0
        ++histo[_mm_extract_epi8(D, 2)];
146
0
        B2 = _mm_and_si128(A2, mask_g);
147
0
        ++histo[_mm_extract_epi8(D, 6)];
148
0
        C2 = _mm_madd_epi16(B2, mult);
149
0
        ++histo[_mm_extract_epi8(D, 10)];
150
0
        ++histo[_mm_extract_epi8(D, 14)];
151
0
        D = _mm_sub_epi16(A2, C2);
152
0
      }
153
1.12M
      ++histo[_mm_extract_epi8(D, 2)];
154
1.12M
      ++histo[_mm_extract_epi8(D, 6)];
155
1.12M
      ++histo[_mm_extract_epi8(D, 10)];
156
1.12M
      ++histo[_mm_extract_epi8(D, 14)];
157
1.12M
    }
158
63.4k
  }
159
118k
  {
160
118k
    const int left_over = tile_width & 3;
161
118k
    if (left_over > 0) {
162
99.8k
      VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,
163
99.8k
                                      left_over, tile_height, green_to_red,
164
99.8k
                                      histo);
165
99.8k
    }
166
118k
  }
167
118k
}
168
169
#undef MK_CST_16
170
171
//------------------------------------------------------------------------------
172
// Entry point
173
174
extern void VP8LEncDspInitSSE41(void);
175
176
1
WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE41(void) {
177
1
  VP8LExtraCost = ExtraCost_SSE41;
178
179
  // SSE exports for AVX and above.
180
1
  VP8LSubtractGreenFromBlueAndRed_SSE = SubtractGreenFromBlueAndRed_SSE41;
181
1
  VP8LCollectColorBlueTransforms_SSE = CollectColorBlueTransforms_SSE41;
182
1
  VP8LCollectColorRedTransforms_SSE = CollectColorRedTransforms_SSE41;
183
184
1
  VP8LSubtractGreenFromBlueAndRed = VP8LSubtractGreenFromBlueAndRed_SSE;
185
1
  VP8LCollectColorBlueTransforms = VP8LCollectColorBlueTransforms_SSE;
186
1
  VP8LCollectColorRedTransforms = VP8LCollectColorRedTransforms_SSE;
187
1
}
188
189
#else  // !WEBP_USE_SSE41
190
191
WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE41)
192
193
#endif  // WEBP_USE_SSE41