Coverage Report

Created: 2026-01-16 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavif/ext/libwebp/sharpyuv/sharpyuv_sse2.c
Line
Count
Source
1
// Copyright 2022 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
// Speed-critical functions for Sharp YUV.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include "sharpyuv/sharpyuv_dsp.h"
15
16
#if defined(WEBP_USE_SSE2)
17
#include <emmintrin.h>
18
19
#include <stdlib.h>
20
21
#include "src/dsp/cpu.h"
22
#include "src/webp/types.h"
23
24
18.5M
static uint16_t clip_SSE2(int v, int max) {
25
18.5M
  return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
26
18.5M
}
27
28
static uint64_t SharpYuvUpdateY_SSE2(const uint16_t* ref, const uint16_t* src,
29
521k
                                     uint16_t* dst, int len, int bit_depth) {
30
521k
  const int max_y = (1 << bit_depth) - 1;
31
521k
  uint64_t diff = 0;
32
521k
  uint32_t tmp[4];
33
521k
  int i;
34
521k
  const __m128i zero = _mm_setzero_si128();
35
521k
  const __m128i max = _mm_set1_epi16(max_y);
36
521k
  const __m128i one = _mm_set1_epi16(1);
37
521k
  __m128i sum = zero;
38
39
71.3M
  for (i = 0; i + 8 <= len; i += 8) {
40
70.8M
    const __m128i A = _mm_loadu_si128((const __m128i*)(ref + i));
41
70.8M
    const __m128i B = _mm_loadu_si128((const __m128i*)(src + i));
42
70.8M
    const __m128i C = _mm_loadu_si128((const __m128i*)(dst + i));
43
70.8M
    const __m128i D = _mm_sub_epi16(A, B);       // diff_y
44
70.8M
    const __m128i E = _mm_cmpgt_epi16(zero, D);  // sign (-1 or 0)
45
70.8M
    const __m128i F = _mm_add_epi16(C, D);       // new_y
46
70.8M
    const __m128i G = _mm_or_si128(E, one);      // -1 or 1
47
70.8M
    const __m128i H = _mm_max_epi16(_mm_min_epi16(F, max), zero);
48
70.8M
    const __m128i I = _mm_madd_epi16(D, G);      // sum(abs(...))
49
70.8M
    _mm_storeu_si128((__m128i*)(dst + i), H);
50
70.8M
    sum = _mm_add_epi32(sum, I);
51
70.8M
  }
52
521k
  _mm_storeu_si128((__m128i*)tmp, sum);
53
521k
  diff = tmp[3] + tmp[2] + tmp[1] + tmp[0];
54
1.42M
  for (; i < len; ++i) {
55
902k
    const int diff_y = ref[i] - src[i];
56
902k
    const int new_y = (int)dst[i] + diff_y;
57
902k
    dst[i] = clip_SSE2(new_y, max_y);
58
902k
    diff += (uint64_t)abs(diff_y);
59
902k
  }
60
521k
  return diff;
61
521k
}
62
63
static void SharpYuvUpdateRGB_SSE2(const int16_t* ref, const int16_t* src,
64
521k
                                   int16_t* dst, int len) {
65
521k
  int i = 0;
66
53.4M
  for (i = 0; i + 8 <= len; i += 8) {
67
52.9M
    const __m128i A = _mm_loadu_si128((const __m128i*)(ref + i));
68
52.9M
    const __m128i B = _mm_loadu_si128((const __m128i*)(src + i));
69
52.9M
    const __m128i C = _mm_loadu_si128((const __m128i*)(dst + i));
70
52.9M
    const __m128i D = _mm_sub_epi16(A, B);   // diff_uv
71
52.9M
    const __m128i E = _mm_add_epi16(C, D);   // new_uv
72
52.9M
    _mm_storeu_si128((__m128i*)(dst + i), E);
73
52.9M
  }
74
3.16M
  for (; i < len; ++i) {
75
2.64M
    const int diff_uv = ref[i] - src[i];
76
2.64M
    dst[i] += diff_uv;
77
2.64M
  }
78
521k
}
79
80
static void SharpYuvFilterRow16_SSE2(const int16_t* A, const int16_t* B,
81
                                     int len, const uint16_t* best_y,
82
2.93M
                                     uint16_t* out, int bit_depth) {
83
2.93M
  const int max_y = (1 << bit_depth) - 1;
84
2.93M
  int i;
85
2.93M
  const __m128i kCst8 = _mm_set1_epi16(8);
86
2.93M
  const __m128i max = _mm_set1_epi16(max_y);
87
2.93M
  const __m128i zero = _mm_setzero_si128();
88
107M
  for (i = 0; i + 8 <= len; i += 8) {
89
104M
    const __m128i a0 = _mm_loadu_si128((const __m128i*)(A + i + 0));
90
104M
    const __m128i a1 = _mm_loadu_si128((const __m128i*)(A + i + 1));
91
104M
    const __m128i b0 = _mm_loadu_si128((const __m128i*)(B + i + 0));
92
104M
    const __m128i b1 = _mm_loadu_si128((const __m128i*)(B + i + 1));
93
104M
    const __m128i a0b1 = _mm_add_epi16(a0, b1);
94
104M
    const __m128i a1b0 = _mm_add_epi16(a1, b0);
95
104M
    const __m128i a0a1b0b1 = _mm_add_epi16(a0b1, a1b0);  // A0+A1+B0+B1
96
104M
    const __m128i a0a1b0b1_8 = _mm_add_epi16(a0a1b0b1, kCst8);
97
104M
    const __m128i a0b1_2 = _mm_add_epi16(a0b1, a0b1);    // 2*(A0+B1)
98
104M
    const __m128i a1b0_2 = _mm_add_epi16(a1b0, a1b0);    // 2*(A1+B0)
99
104M
    const __m128i c0 = _mm_srai_epi16(_mm_add_epi16(a0b1_2, a0a1b0b1_8), 3);
100
104M
    const __m128i c1 = _mm_srai_epi16(_mm_add_epi16(a1b0_2, a0a1b0b1_8), 3);
101
104M
    const __m128i d0 = _mm_add_epi16(c1, a0);
102
104M
    const __m128i d1 = _mm_add_epi16(c0, a1);
103
104M
    const __m128i e0 = _mm_srai_epi16(d0, 1);
104
104M
    const __m128i e1 = _mm_srai_epi16(d1, 1);
105
104M
    const __m128i f0 = _mm_unpacklo_epi16(e0, e1);
106
104M
    const __m128i f1 = _mm_unpackhi_epi16(e0, e1);
107
104M
    const __m128i g0 = _mm_loadu_si128((const __m128i*)(best_y + 2 * i + 0));
108
104M
    const __m128i g1 = _mm_loadu_si128((const __m128i*)(best_y + 2 * i + 8));
109
104M
    const __m128i h0 = _mm_add_epi16(g0, f0);
110
104M
    const __m128i h1 = _mm_add_epi16(g1, f1);
111
104M
    const __m128i i0 = _mm_max_epi16(_mm_min_epi16(h0, max), zero);
112
104M
    const __m128i i1 = _mm_max_epi16(_mm_min_epi16(h1, max), zero);
113
104M
    _mm_storeu_si128((__m128i*)(out + 2 * i + 0), i0);
114
104M
    _mm_storeu_si128((__m128i*)(out + 2 * i + 8), i1);
115
104M
  }
116
11.5M
  for (; i < len; ++i) {
117
    //   (9 * A0 + 3 * A1 + 3 * B0 + B1 + 8) >> 4 =
118
    // = (8 * A0 + 2 * (A1 + B0) + (A0 + A1 + B0 + B1 + 8)) >> 4
119
    // We reuse the common sub-expressions.
120
8.61M
    const int a0b1 = A[i + 0] + B[i + 1];
121
8.61M
    const int a1b0 = A[i + 1] + B[i + 0];
122
8.61M
    const int a0a1b0b1 = a0b1 + a1b0 + 8;
123
8.61M
    const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
124
8.61M
    const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
125
8.61M
    out[2 * i + 0] = clip_SSE2(best_y[2 * i + 0] + v0, max_y);
126
8.61M
    out[2 * i + 1] = clip_SSE2(best_y[2 * i + 1] + v1, max_y);
127
8.61M
  }
128
2.93M
}
129
130
5.39M
static WEBP_INLINE __m128i s16_to_s32(__m128i in) {
131
5.39M
  return _mm_srai_epi32(_mm_unpacklo_epi16(in, in), 16);
132
5.39M
}
133
134
static void SharpYuvFilterRow32_SSE2(const int16_t* A, const int16_t* B,
135
                                     int len, const uint16_t* best_y,
136
192k
                                     uint16_t* out, int bit_depth) {
137
192k
  const int max_y = (1 << bit_depth) - 1;
138
192k
  int i;
139
192k
  const __m128i kCst8 = _mm_set1_epi32(8);
140
192k
  const __m128i max = _mm_set1_epi16(max_y);
141
192k
  const __m128i zero = _mm_setzero_si128();
142
1.54M
  for (i = 0; i + 4 <= len; i += 4) {
143
1.34M
    const __m128i a0 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(A + i + 0)));
144
1.34M
    const __m128i a1 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(A + i + 1)));
145
1.34M
    const __m128i b0 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(B + i + 0)));
146
1.34M
    const __m128i b1 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(B + i + 1)));
147
1.34M
    const __m128i a0b1 = _mm_add_epi32(a0, b1);
148
1.34M
    const __m128i a1b0 = _mm_add_epi32(a1, b0);
149
1.34M
    const __m128i a0a1b0b1 = _mm_add_epi32(a0b1, a1b0);  // A0+A1+B0+B1
150
1.34M
    const __m128i a0a1b0b1_8 = _mm_add_epi32(a0a1b0b1, kCst8);
151
1.34M
    const __m128i a0b1_2 = _mm_add_epi32(a0b1, a0b1);  // 2*(A0+B1)
152
1.34M
    const __m128i a1b0_2 = _mm_add_epi32(a1b0, a1b0);  // 2*(A1+B0)
153
1.34M
    const __m128i c0 = _mm_srai_epi32(_mm_add_epi32(a0b1_2, a0a1b0b1_8), 3);
154
1.34M
    const __m128i c1 = _mm_srai_epi32(_mm_add_epi32(a1b0_2, a0a1b0b1_8), 3);
155
1.34M
    const __m128i d0 = _mm_add_epi32(c1, a0);
156
1.34M
    const __m128i d1 = _mm_add_epi32(c0, a1);
157
1.34M
    const __m128i e0 = _mm_srai_epi32(d0, 1);
158
1.34M
    const __m128i e1 = _mm_srai_epi32(d1, 1);
159
1.34M
    const __m128i f0 = _mm_unpacklo_epi32(e0, e1);
160
1.34M
    const __m128i f1 = _mm_unpackhi_epi32(e0, e1);
161
1.34M
    const __m128i g = _mm_loadu_si128((const __m128i*)(best_y + 2 * i + 0));
162
1.34M
    const __m128i h_16 = _mm_add_epi16(g, _mm_packs_epi32(f0, f1));
163
1.34M
    const __m128i final = _mm_max_epi16(_mm_min_epi16(h_16, max), zero);
164
1.34M
    _mm_storeu_si128((__m128i*)(out + 2 * i + 0), final);
165
1.34M
  }
166
377k
  for (; i < len; ++i) {
167
    //   (9 * A0 + 3 * A1 + 3 * B0 + B1 + 8) >> 4 =
168
    // = (8 * A0 + 2 * (A1 + B0) + (A0 + A1 + B0 + B1 + 8)) >> 4
169
    // We reuse the common sub-expressions.
170
184k
    const int a0b1 = A[i + 0] + B[i + 1];
171
184k
    const int a1b0 = A[i + 1] + B[i + 0];
172
184k
    const int a0a1b0b1 = a0b1 + a1b0 + 8;
173
184k
    const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
174
184k
    const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
175
184k
    out[2 * i + 0] = clip_SSE2(best_y[2 * i + 0] + v0, max_y);
176
184k
    out[2 * i + 1] = clip_SSE2(best_y[2 * i + 1] + v1, max_y);
177
184k
  }
178
192k
}
179
180
static void SharpYuvFilterRow_SSE2(const int16_t* A, const int16_t* B, int len,
181
                                   const uint16_t* best_y, uint16_t* out,
182
3.12M
                                   int bit_depth) {
183
3.12M
  if (bit_depth <= 10) {
184
2.93M
    SharpYuvFilterRow16_SSE2(A, B, len, best_y, out, bit_depth);
185
2.93M
  } else {
186
192k
    SharpYuvFilterRow32_SSE2(A, B, len, best_y, out, bit_depth);
187
192k
  }
188
3.12M
}
189
190
//------------------------------------------------------------------------------
191
192
extern void InitSharpYuvSSE2(void);
193
194
2
WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvSSE2(void) {
195
2
  SharpYuvUpdateY = SharpYuvUpdateY_SSE2;
196
2
  SharpYuvUpdateRGB = SharpYuvUpdateRGB_SSE2;
197
2
  SharpYuvFilterRow = SharpYuvFilterRow_SSE2;
198
2
}
199
#else  // !WEBP_USE_SSE2
200
201
extern void InitSharpYuvSSE2(void);
202
203
void InitSharpYuvSSE2(void) {}
204
205
#endif  // WEBP_USE_SSE2