Coverage Report

Created: 2025-08-11 08:01

/src/libwebp/src/dsp/lossless_enc_sse41.c
Line
Count
Source (jump to first uncovered line)
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
325k
static WEBP_INLINE uint32_t HorizontalSum_SSE41(__m128i cost) {
29
325k
  cost = _mm_add_epi32(cost, _mm_srli_si128(cost, 8));
30
325k
  cost = _mm_add_epi32(cost, _mm_srli_si128(cost, 4));
31
325k
  return _mm_cvtsi128_si32(cost);
32
325k
}
33
34
325k
static uint32_t ExtraCost_SSE41(const uint32_t* const a, int length) {
35
325k
  int i;
36
325k
  __m128i cost = _mm_set_epi32(2 * a[7], 2 * a[6], a[5], a[4]);
37
325k
  assert(length % 8 == 0);
38
39
1.30M
  for (i = 8; i + 8 <= length; i += 8) {
40
976k
    const int j = (i - 2) >> 1;
41
976k
    const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i]);
42
976k
    const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i + 4]);
43
976k
    const __m128i w = _mm_set_epi32(j + 3, j + 2, j + 1, j);
44
976k
    const __m128i a2 = _mm_hadd_epi32(a0, a1);
45
976k
    const __m128i mul = _mm_mullo_epi32(a2, w);
46
976k
    cost = _mm_add_epi32(mul, cost);
47
976k
  }
48
325k
  return HorizontalSum_SSE41(cost);
49
325k
}
50
51
//------------------------------------------------------------------------------
52
// Subtract-Green Transform
53
54
static void SubtractGreenFromBlueAndRed_SSE41(uint32_t* argb_data,
55
0
                                              int num_pixels) {
56
0
  int i;
57
0
  const __m128i kCstShuffle =
58
0
      _mm_set_epi8(-1, 13, -1, 13, -1, 9, -1, 9, -1, 5, -1, 5, -1, 1, -1, 1);
59
0
  for (i = 0; i + 4 <= num_pixels; i += 4) {
60
0
    const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]);
61
0
    const __m128i in_0g0g = _mm_shuffle_epi8(in, kCstShuffle);
62
0
    const __m128i out = _mm_sub_epi8(in, in_0g0g);
63
0
    _mm_storeu_si128((__m128i*)&argb_data[i], out);
64
0
  }
65
  // fallthrough and finish off with plain-C
66
0
  if (i != num_pixels) {
67
0
    VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);
68
0
  }
69
0
}
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
0
  _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
0
                                             uint32_t histo[]) {
85
0
  const __m128i mult =
86
0
      MK_CST_16(CST_5b(red_to_blue) + 256, CST_5b(green_to_blue));
87
0
  const __m128i perm =
88
0
      _mm_setr_epi8(-1, 1, -1, 2, -1, 5, -1, 6, -1, 9, -1, 10, -1, 13, -1, 14);
89
0
  if (tile_width >= 4) {
90
0
    int y;
91
0
    for (y = 0; y < tile_height; ++y) {
92
0
      const uint32_t* const src = argb + y * stride;
93
0
      const __m128i A1 = _mm_loadu_si128((const __m128i*)src);
94
0
      const __m128i B1 = _mm_shuffle_epi8(A1, perm);
95
0
      const __m128i C1 = _mm_mulhi_epi16(B1, mult);
96
0
      const __m128i D1 = _mm_sub_epi16(A1, C1);
97
0
      __m128i E = _mm_add_epi16(_mm_srli_epi32(D1, 16), D1);
98
0
      int x;
99
0
      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
0
      ++histo[_mm_extract_epi8(E, 0)];
112
0
      ++histo[_mm_extract_epi8(E, 4)];
113
0
      ++histo[_mm_extract_epi8(E, 8)];
114
0
      ++histo[_mm_extract_epi8(E, 12)];
115
0
    }
116
0
  }
117
0
  {
118
0
    const int left_over = tile_width & 3;
119
0
    if (left_over > 0) {
120
0
      VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,
121
0
                                       left_over, tile_height, green_to_blue,
122
0
                                       red_to_blue, histo);
123
0
    }
124
0
  }
125
0
}
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
0
                                            uint32_t histo[]) {
131
0
  const __m128i mult = MK_CST_16(0, CST_5b(green_to_red));
132
0
  const __m128i mask_g = _mm_set1_epi32(0x0000ff00);
133
0
  if (tile_width >= 4) {
134
0
    int y;
135
0
    for (y = 0; y < tile_height; ++y) {
136
0
      const uint32_t* const src = argb + y * stride;
137
0
      const __m128i A1 = _mm_loadu_si128((const __m128i*)src);
138
0
      const __m128i B1 = _mm_and_si128(A1, mask_g);
139
0
      const __m128i C1 = _mm_madd_epi16(B1, mult);
140
0
      __m128i D = _mm_sub_epi16(A1, C1);
141
0
      int x;
142
0
      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
0
      ++histo[_mm_extract_epi8(D, 2)];
154
0
      ++histo[_mm_extract_epi8(D, 6)];
155
0
      ++histo[_mm_extract_epi8(D, 10)];
156
0
      ++histo[_mm_extract_epi8(D, 14)];
157
0
    }
158
0
  }
159
0
  {
160
0
    const int left_over = tile_width & 3;
161
0
    if (left_over > 0) {
162
0
      VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,
163
0
                                      left_over, tile_height, green_to_red,
164
0
                                      histo);
165
0
    }
166
0
  }
167
0
}
168
169
#undef MK_CST_16
170
171
//------------------------------------------------------------------------------
172
// Entry point
173
174
extern void VP8LEncDspInitSSE41(void);
175
176
4
WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE41(void) {
177
4
  VP8LExtraCost = ExtraCost_SSE41;
178
4
  VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE41;
179
4
  VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE41;
180
4
  VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE41;
181
182
  // SSE exports for AVX and above.
183
4
  VP8LSubtractGreenFromBlueAndRed_SSE = SubtractGreenFromBlueAndRed_SSE41;
184
4
  VP8LCollectColorBlueTransforms_SSE = CollectColorBlueTransforms_SSE41;
185
4
  VP8LCollectColorRedTransforms_SSE = CollectColorRedTransforms_SSE41;
186
4
}
187
188
#else  // !WEBP_USE_SSE41
189
190
WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE41)
191
192
#endif  // WEBP_USE_SSE41