Coverage Report

Created: 2026-06-13 06:29

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
909k
static uint16_t clip_SSE2(int v, int max) {
25
909k
  return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
26
909k
}
27
28
static uint64_t SharpYuvUpdateY_SSE2(const uint16_t* ref, const uint16_t* src,
29
72.9k
                                     uint16_t* dst, int len, int bit_depth) {
30
72.9k
  const int max_y = (1 << bit_depth) - 1;
31
72.9k
  uint64_t diff = 0;
32
72.9k
  uint32_t tmp[4];
33
72.9k
  int i;
34
72.9k
  const __m128i zero = _mm_setzero_si128();
35
72.9k
  const __m128i max = _mm_set1_epi16(max_y);
36
72.9k
  const __m128i one = _mm_set1_epi16(1);
37
72.9k
  __m128i sum = zero;
38
39
657k
  for (i = 0; i + 8 <= len; i += 8) {
40
584k
    const __m128i A = _mm_loadu_si128((const __m128i*)(ref + i));
41
584k
    const __m128i B = _mm_loadu_si128((const __m128i*)(src + i));
42
584k
    const __m128i C = _mm_loadu_si128((const __m128i*)(dst + i));
43
584k
    const __m128i D = _mm_sub_epi16(A, B);       // diff_y
44
584k
    const __m128i E = _mm_cmpgt_epi16(zero, D);  // sign (-1 or 0)
45
584k
    const __m128i F = _mm_add_epi16(C, D);       // new_y
46
584k
    const __m128i G = _mm_or_si128(E, one);      // -1 or 1
47
584k
    const __m128i H = _mm_max_epi16(_mm_min_epi16(F, max), zero);
48
584k
    const __m128i I = _mm_madd_epi16(D, G);      // sum(abs(...))
49
584k
    _mm_storeu_si128((__m128i*)(dst + i), H);
50
584k
    sum = _mm_add_epi32(sum, I);
51
584k
  }
52
72.9k
  _mm_storeu_si128((__m128i*)tmp, sum);
53
72.9k
  diff = tmp[3] + tmp[2] + tmp[1] + tmp[0];
54
285k
  for (; i < len; ++i) {
55
213k
    const int diff_y = ref[i] - src[i];
56
213k
    const int new_y = (int)dst[i] + diff_y;
57
213k
    dst[i] = clip_SSE2(new_y, max_y);
58
213k
    diff += (uint64_t)abs(diff_y);
59
213k
  }
60
72.9k
  return diff;
61
72.9k
}
62
63
static void SharpYuvUpdateRGB_SSE2(const int16_t* ref, const int16_t* src,
64
72.9k
                                   int16_t* dst, int len) {
65
72.9k
  int i = 0;
66
500k
  for (i = 0; i + 8 <= len; i += 8) {
67
427k
    const __m128i A = _mm_loadu_si128((const __m128i*)(ref + i));
68
427k
    const __m128i B = _mm_loadu_si128((const __m128i*)(src + i));
69
427k
    const __m128i C = _mm_loadu_si128((const __m128i*)(dst + i));
70
427k
    const __m128i D = _mm_sub_epi16(A, B);   // diff_uv
71
427k
    const __m128i E = _mm_add_epi16(C, D);   // new_uv
72
427k
    _mm_storeu_si128((__m128i*)(dst + i), E);
73
427k
  }
74
318k
  for (; i < len; ++i) {
75
245k
    const int diff_uv = ref[i] - src[i];
76
245k
    dst[i] += diff_uv;
77
245k
  }
78
72.9k
}
79
80
static void SharpYuvFilterRow16_SSE2(const int16_t* A, const int16_t* B,
81
                                     int len, const uint16_t* best_y,
82
198k
                                     uint16_t* out, int bit_depth) {
83
198k
  const int max_y = (1 << bit_depth) - 1;
84
198k
  int i;
85
198k
  const __m128i kCst8 = _mm_set1_epi16(8);
86
198k
  const __m128i max = _mm_set1_epi16(max_y);
87
198k
  const __m128i zero = _mm_setzero_si128();
88
265k
  for (i = 0; i + 8 <= len; i += 8) {
89
67.2k
    const __m128i a0 = _mm_loadu_si128((const __m128i*)(A + i + 0));
90
67.2k
    const __m128i a1 = _mm_loadu_si128((const __m128i*)(A + i + 1));
91
67.2k
    const __m128i b0 = _mm_loadu_si128((const __m128i*)(B + i + 0));
92
67.2k
    const __m128i b1 = _mm_loadu_si128((const __m128i*)(B + i + 1));
93
67.2k
    const __m128i a0b1 = _mm_add_epi16(a0, b1);
94
67.2k
    const __m128i a1b0 = _mm_add_epi16(a1, b0);
95
67.2k
    const __m128i a0a1b0b1 = _mm_add_epi16(a0b1, a1b0);  // A0+A1+B0+B1
96
67.2k
    const __m128i a0a1b0b1_8 = _mm_add_epi16(a0a1b0b1, kCst8);
97
67.2k
    const __m128i a0b1_2 = _mm_add_epi16(a0b1, a0b1);    // 2*(A0+B1)
98
67.2k
    const __m128i a1b0_2 = _mm_add_epi16(a1b0, a1b0);    // 2*(A1+B0)
99
67.2k
    const __m128i c0 = _mm_srai_epi16(_mm_add_epi16(a0b1_2, a0a1b0b1_8), 3);
100
67.2k
    const __m128i c1 = _mm_srai_epi16(_mm_add_epi16(a1b0_2, a0a1b0b1_8), 3);
101
67.2k
    const __m128i d0 = _mm_add_epi16(c1, a0);
102
67.2k
    const __m128i d1 = _mm_add_epi16(c0, a1);
103
67.2k
    const __m128i e0 = _mm_srai_epi16(d0, 1);
104
67.2k
    const __m128i e1 = _mm_srai_epi16(d1, 1);
105
67.2k
    const __m128i f0 = _mm_unpacklo_epi16(e0, e1);
106
67.2k
    const __m128i f1 = _mm_unpackhi_epi16(e0, e1);
107
67.2k
    const __m128i g0 = _mm_loadu_si128((const __m128i*)(best_y + 2 * i + 0));
108
67.2k
    const __m128i g1 = _mm_loadu_si128((const __m128i*)(best_y + 2 * i + 8));
109
67.2k
    const __m128i h0 = _mm_add_epi16(g0, f0);
110
67.2k
    const __m128i h1 = _mm_add_epi16(g1, f1);
111
67.2k
    const __m128i i0 = _mm_max_epi16(_mm_min_epi16(h0, max), zero);
112
67.2k
    const __m128i i1 = _mm_max_epi16(_mm_min_epi16(h1, max), zero);
113
67.2k
    _mm_storeu_si128((__m128i*)(out + 2 * i + 0), i0);
114
67.2k
    _mm_storeu_si128((__m128i*)(out + 2 * i + 8), i1);
115
67.2k
  }
116
340k
  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
142k
    const int a0b1 = A[i + 0] + B[i + 1];
121
142k
    const int a1b0 = A[i + 1] + B[i + 0];
122
142k
    const int a0a1b0b1 = a0b1 + a1b0 + 8;
123
142k
    const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
124
142k
    const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
125
142k
    out[2 * i + 0] = clip_SSE2(best_y[2 * i + 0] + v0, max_y);
126
142k
    out[2 * i + 1] = clip_SSE2(best_y[2 * i + 1] + v1, max_y);
127
142k
  }
128
198k
}
129
130
6.01M
static WEBP_INLINE __m128i s16_to_s32(__m128i in) {
131
6.01M
  return _mm_srai_epi32(_mm_unpacklo_epi16(in, in), 16);
132
6.01M
}
133
134
static void SharpYuvFilterRow32_SSE2(const int16_t* A, const int16_t* B,
135
                                     int len, const uint16_t* best_y,
136
239k
                                     uint16_t* out, int bit_depth) {
137
239k
  const int max_y = (1 << bit_depth) - 1;
138
239k
  int i;
139
239k
  const __m128i kCst8 = _mm_set1_epi32(8);
140
239k
  const __m128i max = _mm_set1_epi16(max_y);
141
239k
  const __m128i zero = _mm_setzero_si128();
142
1.74M
  for (i = 0; i + 4 <= len; i += 4) {
143
1.50M
    const __m128i a0 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(A + i + 0)));
144
1.50M
    const __m128i a1 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(A + i + 1)));
145
1.50M
    const __m128i b0 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(B + i + 0)));
146
1.50M
    const __m128i b1 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(B + i + 1)));
147
1.50M
    const __m128i a0b1 = _mm_add_epi32(a0, b1);
148
1.50M
    const __m128i a1b0 = _mm_add_epi32(a1, b0);
149
1.50M
    const __m128i a0a1b0b1 = _mm_add_epi32(a0b1, a1b0);  // A0+A1+B0+B1
150
1.50M
    const __m128i a0a1b0b1_8 = _mm_add_epi32(a0a1b0b1, kCst8);
151
1.50M
    const __m128i a0b1_2 = _mm_add_epi32(a0b1, a0b1);  // 2*(A0+B1)
152
1.50M
    const __m128i a1b0_2 = _mm_add_epi32(a1b0, a1b0);  // 2*(A1+B0)
153
1.50M
    const __m128i c0 = _mm_srai_epi32(_mm_add_epi32(a0b1_2, a0a1b0b1_8), 3);
154
1.50M
    const __m128i c1 = _mm_srai_epi32(_mm_add_epi32(a1b0_2, a0a1b0b1_8), 3);
155
1.50M
    const __m128i d0 = _mm_add_epi32(c1, a0);
156
1.50M
    const __m128i d1 = _mm_add_epi32(c0, a1);
157
1.50M
    const __m128i e0 = _mm_srai_epi32(d0, 1);
158
1.50M
    const __m128i e1 = _mm_srai_epi32(d1, 1);
159
1.50M
    const __m128i f0 = _mm_unpacklo_epi32(e0, e1);
160
1.50M
    const __m128i f1 = _mm_unpackhi_epi32(e0, e1);
161
1.50M
    const __m128i g = _mm_loadu_si128((const __m128i*)(best_y + 2 * i + 0));
162
1.50M
    const __m128i h_16 = _mm_add_epi16(g, _mm_packs_epi32(f0, f1));
163
1.50M
    const __m128i final = _mm_max_epi16(_mm_min_epi16(h_16, max), zero);
164
1.50M
    _mm_storeu_si128((__m128i*)(out + 2 * i + 0), final);
165
1.50M
  }
166
445k
  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
205k
    const int a0b1 = A[i + 0] + B[i + 1];
171
205k
    const int a1b0 = A[i + 1] + B[i + 0];
172
205k
    const int a0a1b0b1 = a0b1 + a1b0 + 8;
173
205k
    const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
174
205k
    const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
175
205k
    out[2 * i + 0] = clip_SSE2(best_y[2 * i + 0] + v0, max_y);
176
205k
    out[2 * i + 1] = clip_SSE2(best_y[2 * i + 1] + v1, max_y);
177
205k
  }
178
239k
}
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
437k
                                   int bit_depth) {
183
437k
  if (bit_depth <= 10) {
184
198k
    SharpYuvFilterRow16_SSE2(A, B, len, best_y, out, bit_depth);
185
239k
  } else {
186
239k
    SharpYuvFilterRow32_SSE2(A, B, len, best_y, out, bit_depth);
187
239k
  }
188
437k
}
189
190
//------------------------------------------------------------------------------
191
192
extern void InitSharpYuvSSE2(void);
193
194
1
WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvSSE2(void) {
195
1
  SharpYuvUpdateY = SharpYuvUpdateY_SSE2;
196
1
  SharpYuvUpdateRGB = SharpYuvUpdateRGB_SSE2;
197
1
  SharpYuvFilterRow = SharpYuvFilterRow_SSE2;
198
1
}
199
#else  // !WEBP_USE_SSE2
200
201
extern void InitSharpYuvSSE2(void);
202
203
void InitSharpYuvSSE2(void) {}
204
205
#endif  // WEBP_USE_SSE2