Coverage Report

Created: 2026-12-01 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/sharpyuv/sharpyuv_avx2.c
Line
Count
Source
1
// Copyright 2026 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
// AVX2 speed-critical functions for Sharp YUV.
11
12
#include "./sharpyuv.h"
13
#include "./sharpyuv_dsp.h"
14
#include "./sharpyuv_gamma.h"
15
16
#if defined(WEBP_USE_AVX2)
17
#include <assert.h>
18
#include <immintrin.h>
19
#include <stddef.h>
20
#include <stdint.h>
21
#include <stdlib.h>
22
23
#include "src/dsp/cpu.h"
24
#include "webp/types.h"
25
26
211M
#define LOAD_256(P) _mm256_loadu_si256((const __m256i*)(P))
27
52.9M
#define STORE_256(P, V) _mm256_storeu_si256((__m256i*)(P), (V))
28
190M
#define LOAD_128(P) _mm_loadu_si128((const __m128i*)(P))
29
65.1M
#define STORE_128(P, V) _mm_storeu_si128((__m128i*)(P), (V))
30
39.2M
#define LOAD_64(P) _mm_loadl_epi64((const __m128i*)(P))
31
19.5M
#define STORE_64(P, V) _mm_storel_epi64((__m128i*)(P), (V))
32
33
// Clamps 16x (resp. 8x) int16 lanes to [0, max].
34
43.1M
static WEBP_INLINE __m256i Clip16_AVX2(__m256i v, __m256i max) {
35
43.1M
  return _mm256_max_epi16(_mm256_min_epi16(v, max), _mm256_setzero_si256());
36
43.1M
}
37
459k
static WEBP_INLINE __m128i Clip16_128_AVX2(__m128i v, __m128i max) {
38
459k
  return _mm_max_epi16(_mm_min_epi16(v, max), _mm_setzero_si128());
39
459k
}
40
41
// Widens 8x int16 lanes into low/high 4x32 halves (sign-extending).
42
static WEBP_INLINE void WidenLoHi16To32_AVX2(__m128i v, __m128i* lo,
43
48.9M
                                             __m128i* hi) {
44
48.9M
  *lo = _mm_cvtepi16_epi32(v);
45
48.9M
  *hi = _mm_cvtepi16_epi32(_mm_srli_si128(v, 8));
46
48.9M
}
47
48
// e0 = (9*a0+3*a1+3*b0+b1+8)>>4, e1 symmetric. SUFFIX is epi16 or epi32.
49
#define SHARP_FILTER933_AVX2(SUFFIX, a0, a1, b0, b1, kCst8, e0_out, e1_out) \
50
15.7M
  do {                                                                      \
51
15.7M
    const __m256i a0b1 = _mm256_add_##SUFFIX(a0, b1);                       \
52
15.7M
    const __m256i a1b0 = _mm256_add_##SUFFIX(a1, b0);                       \
53
15.7M
    const __m256i a0a1b0b1 = _mm256_add_##SUFFIX(a0b1, a1b0);               \
54
15.7M
    const __m256i a0a1b0b1_8 = _mm256_add_##SUFFIX(a0a1b0b1, kCst8);        \
55
15.7M
    const __m256i a0b1_2 = _mm256_add_##SUFFIX(a0b1, a0b1);                 \
56
15.7M
    const __m256i a1b0_2 = _mm256_add_##SUFFIX(a1b0, a1b0);                 \
57
15.7M
    const __m256i c0_ =                                                     \
58
15.7M
        _mm256_srai_##SUFFIX(_mm256_add_##SUFFIX(a0b1_2, a0a1b0b1_8), 3);   \
59
15.7M
    const __m256i c1_ =                                                     \
60
15.7M
        _mm256_srai_##SUFFIX(_mm256_add_##SUFFIX(a1b0_2, a0a1b0b1_8), 3);   \
61
15.7M
    const __m256i d0_ = _mm256_add_##SUFFIX(c1_, a0);                       \
62
15.7M
    const __m256i d1_ = _mm256_add_##SUFFIX(c0_, a1);                       \
63
15.7M
    e0_out = _mm256_srai_##SUFFIX(d0_, 1);                                  \
64
15.7M
    e1_out = _mm256_srai_##SUFFIX(d1_, 1);                                  \
65
15.7M
  } while (0)
66
67
static uint64_t SharpYuvUpdateY_AVX2(const uint16_t* ref, const uint16_t* src,
68
753k
                                     uint16_t* dst, int len, int bit_depth) {
69
753k
  const int max_y = (1 << bit_depth) - 1;
70
753k
  int i;
71
753k
  const __m256i max = _mm256_set1_epi16(max_y);
72
753k
  const __m256i one = _mm256_set1_epi16(1);
73
753k
  __m256i sum = _mm256_setzero_si256();
74
753k
  uint64_t diff;
75
76
12.8M
  for (i = 0; i <= len - 16; i += 16) {
77
12.1M
    const __m256i A = LOAD_256(ref + i);
78
12.1M
    const __m256i B = LOAD_256(src + i);
79
12.1M
    const __m256i C = LOAD_256(dst + i);
80
12.1M
    const __m256i D = _mm256_sub_epi16(A, B);        // diff_y
81
12.1M
    const __m256i F = _mm256_add_epi16(C, D);        // new_y
82
12.1M
    const __m256i AbsD = _mm256_abs_epi16(D);        // abs(diff_y)
83
12.1M
    const __m256i I = _mm256_madd_epi16(AbsD, one);  // 32-bit pairs
84
12.1M
    STORE_256(dst + i, Clip16_AVX2(F, max));
85
12.1M
    sum = _mm256_add_epi32(sum, I);
86
12.1M
  }
87
753k
  {
88
753k
    uint32_t tmp[8];
89
753k
    STORE_256(tmp, sum);
90
753k
    diff = (uint64_t)tmp[0] + tmp[1] + tmp[2] + tmp[3] + tmp[4] + tmp[5] +
91
753k
           tmp[6] + tmp[7];
92
753k
  }
93
3.92M
  for (; i < len; ++i) {
94
3.17M
    const int diff_y = ref[i] - src[i];
95
3.17M
    const int new_y = (int)dst[i] + diff_y;
96
3.17M
    dst[i] = SharpYuvClip16(new_y, max_y);
97
3.17M
    diff += (uint64_t)abs(diff_y);
98
3.17M
  }
99
753k
  return diff;
100
753k
}
101
102
static void SharpYuvUpdateRGB_AVX2(const int16_t* ref, const int16_t* src,
103
753k
                                   int16_t* dst, int len) {
104
753k
  int i;
105
9.74M
  for (i = 0; i <= len - 16; i += 16) {
106
8.98M
    const __m256i A = LOAD_256(ref + i);
107
8.98M
    const __m256i B = LOAD_256(src + i);
108
8.98M
    const __m256i C = LOAD_256(dst + i);
109
8.98M
    const __m256i D = _mm256_sub_epi16(A, B);  // diff_uv
110
8.98M
    const __m256i E = _mm256_add_epi16(C, D);  // new_uv
111
8.98M
    STORE_256(dst + i, E);
112
8.98M
  }
113
4.70M
  for (; i < len; ++i) {
114
3.95M
    const int diff_uv = ref[i] - src[i];
115
3.95M
    dst[i] += diff_uv;
116
3.95M
  }
117
753k
}
118
119
static void SharpYuvFilterRow16_AVX2(const int16_t* A, const int16_t* B,
120
                                     int len, const uint16_t* best_y,
121
4.34M
                                     uint16_t* out, int bit_depth) {
122
4.34M
  const int max_y = (1 << bit_depth) - 1;
123
4.34M
  int i;
124
4.34M
  const __m256i kCst8 = _mm256_set1_epi16(8);
125
4.34M
  const __m256i max = _mm256_set1_epi16(max_y);
126
19.8M
  for (i = 0; i + 16 <= len; i += 16) {
127
15.5M
    const __m256i a0 = LOAD_256(A + i + 0);
128
15.5M
    const __m256i a1 = LOAD_256(A + i + 1);
129
15.5M
    const __m256i b0 = LOAD_256(B + i + 0);
130
15.5M
    const __m256i b1 = LOAD_256(B + i + 1);
131
15.5M
    __m256i e0, e1;
132
15.5M
    SHARP_FILTER933_AVX2(epi16, a0, a1, b0, b1, kCst8, e0, e1);
133
15.5M
    {
134
15.5M
      const __m256i lo = _mm256_unpacklo_epi16(e0, e1);
135
15.5M
      const __m256i hi = _mm256_unpackhi_epi16(e0, e1);
136
      // unpacklo/hi interleave within 128-bit lanes; permute to restore the
137
      // sequential (i, i+8) ordering matching best_y/out layout.
138
15.5M
      const __m256i f0 = _mm256_permute2x128_si256(lo, hi, 0x20);
139
15.5M
      const __m256i f1 = _mm256_permute2x128_si256(lo, hi, 0x31);
140
15.5M
      const __m256i g0 = LOAD_256(best_y + 2 * i + 0);
141
15.5M
      const __m256i g1 = LOAD_256(best_y + 2 * i + 16);
142
15.5M
      STORE_256(out + 2 * i + 0, Clip16_AVX2(_mm256_add_epi16(g0, f0), max));
143
15.5M
      STORE_256(out + 2 * i + 16, Clip16_AVX2(_mm256_add_epi16(g1, f1), max));
144
15.5M
    }
145
15.5M
  }
146
44.8M
  for (; i < len; ++i) {
147
40.5M
    const int v0 =
148
40.5M
        (A[i + 0] * 9 + A[i + 1] * 3 + B[i + 0] * 3 + B[i + 1] + 8) >> 4;
149
40.5M
    const int v1 =
150
40.5M
        (A[i + 1] * 9 + A[i + 0] * 3 + B[i + 1] * 3 + B[i + 0] + 8) >> 4;
151
40.5M
    out[2 * i + 0] = SharpYuvClip16(best_y[2 * i + 0] + v0, max_y);
152
40.5M
    out[2 * i + 1] = SharpYuvClip16(best_y[2 * i + 1] + v1, max_y);
153
40.5M
  }
154
4.34M
}
155
156
// SharpYuvFilterRow16_AVX2's intermediate sums (e.g. a0a1b0b1_8) can exceed
157
// int16 range once bit_depth > 10 (chroma deltas get large enough that
158
// 2*(A+B) overflows signed 16 bits and wraps). This variant does the same
159
// computation in 32-bit lanes to stay safe, matching
160
// SharpYuvFilterRow32_SSE2/NEON.
161
static void SharpYuvFilterRow32_AVX2(const int16_t* A, const int16_t* B,
162
                                     int len, const uint16_t* best_y,
163
175k
                                     uint16_t* out, int bit_depth) {
164
175k
  const int max_y = (1 << bit_depth) - 1;
165
175k
  int i;
166
175k
  const __m256i kCst8 = _mm256_set1_epi32(8);
167
175k
  const __m128i max = _mm_set1_epi16(max_y);
168
370k
  for (i = 0; i + 8 <= len; i += 8) {
169
194k
    const __m256i a0 = _mm256_cvtepi16_epi32(LOAD_128(A + i + 0));
170
194k
    const __m256i a1 = _mm256_cvtepi16_epi32(LOAD_128(A + i + 1));
171
194k
    const __m256i b0 = _mm256_cvtepi16_epi32(LOAD_128(B + i + 0));
172
194k
    const __m256i b1 = _mm256_cvtepi16_epi32(LOAD_128(B + i + 1));
173
194k
    __m256i e0, e1;
174
194k
    SHARP_FILTER933_AVX2(epi32, a0, a1, b0, b1, kCst8, e0, e1);
175
    // Split into 128-bit halves and interleave/pack each independently (pure
176
    // SSE-style ops): avoids AVX2's cross-128-lane pack/unpack pitfalls.
177
194k
    {
178
194k
      const __m128i e0_lo = _mm256_castsi256_si128(e0);
179
194k
      const __m128i e0_hi = _mm256_extracti128_si256(e0, 1);
180
194k
      const __m128i e1_lo = _mm256_castsi256_si128(e1);
181
194k
      const __m128i e1_hi = _mm256_extracti128_si256(e1, 1);
182
194k
      const __m128i out_lo = _mm_packs_epi32(_mm_unpacklo_epi32(e0_lo, e1_lo),
183
194k
                                             _mm_unpackhi_epi32(e0_lo, e1_lo));
184
194k
      const __m128i out_hi = _mm_packs_epi32(_mm_unpacklo_epi32(e0_hi, e1_hi),
185
194k
                                             _mm_unpackhi_epi32(e0_hi, e1_hi));
186
194k
      const __m128i g_lo = LOAD_128(best_y + 2 * i + 0);
187
194k
      const __m128i g_hi = LOAD_128(best_y + 2 * i + 8);
188
194k
      STORE_128(out + 2 * i + 0,
189
194k
                Clip16_128_AVX2(_mm_add_epi16(g_lo, out_lo), max));
190
194k
      STORE_128(out + 2 * i + 8,
191
194k
                Clip16_128_AVX2(_mm_add_epi16(g_hi, out_hi), max));
192
194k
    }
193
194k
  }
194
514k
  for (; i < len; ++i) {
195
338k
    const int a0b1 = A[i + 0] + B[i + 1];
196
338k
    const int a1b0 = A[i + 1] + B[i + 0];
197
338k
    const int a0a1b0b1 = a0b1 + a1b0 + 8;
198
338k
    const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
199
338k
    const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
200
338k
    out[2 * i + 0] = SharpYuvClip16(best_y[2 * i + 0] + v0, max_y);
201
338k
    out[2 * i + 1] = SharpYuvClip16(best_y[2 * i + 1] + v1, max_y);
202
338k
  }
203
175k
}
204
205
static void SharpYuvFilterRow_AVX2(const int16_t* A, const int16_t* B, int len,
206
                                   const uint16_t* best_y, uint16_t* out,
207
4.52M
                                   int bit_depth) {
208
4.52M
  if (bit_depth <= 10) {
209
4.34M
    SharpYuvFilterRow16_AVX2(A, B, len, best_y, out, bit_depth);
210
4.34M
  } else {
211
175k
    SharpYuvFilterRow32_AVX2(A, B, len, best_y, out, bit_depth);
212
175k
  }
213
4.52M
}
214
215
//------------------------------------------------------------------------------
216
// Final W/RGB -> YUV reconstruction. Must stay numerically equivalent to
217
// RGBToYUVComponent() in sharpyuv.c, including its int64_t precision.
218
219
// Arithmetic right shift by a runtime amount n (1 <= n <= 63): AVX2 has no
220
// psraq, so build it from the logical shift plus a sign-extended top.
221
131M
static WEBP_INLINE __m256i Sra64_AVX2(__m256i a, int n) {
222
131M
  const __m128i cnt = _mm_cvtsi32_si128(n);
223
131M
  const __m128i cnt2 = _mm_cvtsi32_si128(64 - n);
224
131M
  const __m256i sign = _mm256_cmpgt_epi64(_mm256_setzero_si256(), a);
225
131M
  const __m256i shifted = _mm256_srl_epi64(a, cnt);
226
131M
  const __m256i topbits = _mm256_sll_epi64(sign, cnt2);
227
131M
  return _mm256_or_si256(shifted, topbits);
228
131M
}
229
230
// Truncating (non-saturating) narrow of 8x int32 lanes to 8x int16: matches
231
// a plain C (int16_t)/(uint16_t) cast, unlike _mm256_packs/packus_epi32.
232
92.1M
static WEBP_INLINE __m128i TruncateNarrow32To16_AVX2(__m256i v) {
233
92.1M
  const __m256i masked = _mm256_and_si256(v, _mm256_set1_epi32(0xffff));
234
92.1M
  const __m256i packed = _mm256_packus_epi32(masked, masked);
235
92.1M
  const __m128i lo = _mm256_castsi256_si128(packed);
236
92.1M
  const __m128i hi = _mm256_extracti128_si256(packed, 1);
237
92.1M
  return _mm_unpacklo_epi64(lo, hi);
238
92.1M
}
239
240
// Narrow 4x64-bit lanes (2 groups of 2, from the mul_epi32 lane layout) to
241
// 4x32-bit, keeping only the low 32 bits of each 64-bit lane.
242
131M
static WEBP_INLINE __m128i Narrow64To32_AVX2(__m256i v) {
243
131M
  const __m256i shuffled = _mm256_shuffle_epi32(v, _MM_SHUFFLE(2, 0, 2, 0));
244
131M
  const __m128i lo = _mm256_castsi256_si128(shuffled);
245
131M
  const __m128i hi = _mm256_extracti128_si256(shuffled, 1);
246
131M
  return _mm_unpacklo_epi64(lo, hi);
247
131M
}
248
249
// round((R*coeffs[0] + G*coeffs[1] + B*coeffs[2] + bias) >> shift) for 4
250
// lanes, in exact 64-bit precision (matches the int64_t path in
251
// RGBToYUVComponent / RGBToGray).
252
static WEBP_INLINE __m128i Convert4_AVX2(__m128i R, __m128i G, __m128i B,
253
                                         const int coeffs[4], __m256i bias,
254
131M
                                         int shift) {
255
131M
  const __m256i R64 = _mm256_cvtepi32_epi64(R);
256
131M
  const __m256i G64 = _mm256_cvtepi32_epi64(G);
257
131M
  const __m256i B64 = _mm256_cvtepi32_epi64(B);
258
131M
  __m256i acc = _mm256_add_epi64(
259
131M
      bias, _mm256_mul_epi32(R64, _mm256_set1_epi64x(coeffs[0])));
260
131M
  acc = _mm256_add_epi64(acc,
261
131M
                         _mm256_mul_epi32(G64, _mm256_set1_epi64x(coeffs[1])));
262
131M
  acc = _mm256_add_epi64(acc,
263
131M
                         _mm256_mul_epi32(B64, _mm256_set1_epi64x(coeffs[2])));
264
131M
  acc = Sra64_AVX2(acc, shift);
265
131M
  return Narrow64To32_AVX2(acc);
266
131M
}
267
268
static void SharpYuvConvertRowY_AVX2(const uint16_t* best_y,
269
                                     const int16_t* best_uv, int width,
270
                                     int uv_w, const int coeffs[4], int sfix,
271
790k
                                     int yuv_bit_depth, void* y_out) {
272
790k
  const int shift = SHARPYUV_YUV_FIX + sfix;
273
790k
  const int64_t rounder = 1LL << (shift - 1);
274
790k
  const __m256i bias = _mm256_set1_epi64x(coeffs[3] + rounder);
275
790k
  const int yuv_max = (1 << yuv_bit_depth) - 1;
276
790k
  const size_t uv_w_size = (size_t)uv_w;
277
790k
  int i = 0;
278
13.8M
  for (; i <= width - 8; i += 8) {
279
13.0M
    const size_t off = (size_t)(i >> 1);
280
13.0M
    const __m128i W = LOAD_128(best_y + i);
281
13.0M
    const __m128i Ro = LOAD_64(best_uv + off + 0 * uv_w_size);
282
13.0M
    const __m128i Go = LOAD_64(best_uv + off + 1 * uv_w_size);
283
13.0M
    const __m128i Bo = LOAD_64(best_uv + off + 2 * uv_w_size);
284
13.0M
    const __m128i Rv = _mm_add_epi16(_mm_unpacklo_epi16(Ro, Ro), W);
285
13.0M
    const __m128i Gv = _mm_add_epi16(_mm_unpacklo_epi16(Go, Go), W);
286
13.0M
    const __m128i Bv = _mm_add_epi16(_mm_unpacklo_epi16(Bo, Bo), W);
287
13.0M
    __m128i R_lo, R_hi, G_lo, G_hi, B_lo, B_hi;
288
13.0M
    WidenLoHi16To32_AVX2(Rv, &R_lo, &R_hi);
289
13.0M
    WidenLoHi16To32_AVX2(Gv, &G_lo, &G_hi);
290
13.0M
    WidenLoHi16To32_AVX2(Bv, &B_lo, &B_hi);
291
13.0M
    {
292
13.0M
      const __m128i y_lo = Convert4_AVX2(R_lo, G_lo, B_lo, coeffs, bias, shift);
293
13.0M
      const __m128i y_hi = Convert4_AVX2(R_hi, G_hi, B_hi, coeffs, bias, shift);
294
13.0M
      const __m128i y16 = _mm_packs_epi32(y_lo, y_hi);  // saturating:
295
                                                        // matches
296
                                                        // clip()/clip_8b().
297
13.0M
      if (yuv_bit_depth <= 8) {
298
13.0M
        STORE_64((uint8_t*)y_out + i, _mm_packus_epi16(y16, y16));
299
13.0M
      } else {
300
47.3k
        const __m128i maxv = _mm_set1_epi16((int16_t)yuv_max);
301
47.3k
        STORE_128((uint16_t*)y_out + i, Clip16_128_AVX2(y16, maxv));
302
47.3k
      }
303
13.0M
    }
304
13.0M
  }
305
2.58M
  for (; i < width; ++i) {
306
1.79M
    const size_t off = (size_t)(i >> 1);
307
1.79M
    const int W = best_y[i];
308
1.79M
    const int r = best_uv[off + 0 * uv_w_size] + W;
309
1.79M
    const int g = best_uv[off + 1 * uv_w_size] + W;
310
1.79M
    const int b = best_uv[off + 2 * uv_w_size] + W;
311
1.79M
    const int y = SharpYuvConvertComponent(r, g, b, coeffs, sfix);
312
1.79M
    if (yuv_bit_depth <= 8) {
313
1.76M
      ((uint8_t*)y_out)[i] = (uint8_t)SharpYuvClip16(y, 255);
314
1.76M
    } else {
315
35.4k
      ((uint16_t*)y_out)[i] = SharpYuvClip16(y, yuv_max);
316
35.4k
    }
317
1.79M
  }
318
790k
}
319
320
static void SharpYuvConvertRowUV_AVX2(const int16_t* best_uv, int uv_w,
321
                                      const int coeffs_u[4],
322
                                      const int coeffs_v[4], int sfix,
323
                                      int yuv_bit_depth, void* u_out,
324
398k
                                      void* v_out) {
325
398k
  const int shift = SHARPYUV_YUV_FIX + sfix;
326
398k
  const int64_t rounder = 1LL << (shift - 1);
327
398k
  const __m256i bias_u = _mm256_set1_epi64x(coeffs_u[3] + rounder);
328
398k
  const __m256i bias_v = _mm256_set1_epi64x(coeffs_v[3] + rounder);
329
398k
  const int yuv_max = (1 << yuv_bit_depth) - 1;
330
398k
  const size_t uv_w_size = (size_t)uv_w;
331
398k
  int i = 0;
332
3.65M
  for (; i <= uv_w - 8; i += 8) {
333
3.25M
    const __m128i R16 = LOAD_128(best_uv + i + 0 * uv_w_size);
334
3.25M
    const __m128i G16 = LOAD_128(best_uv + i + 1 * uv_w_size);
335
3.25M
    const __m128i B16 = LOAD_128(best_uv + i + 2 * uv_w_size);
336
3.25M
    __m128i R_lo, R_hi, G_lo, G_hi, B_lo, B_hi;
337
3.25M
    WidenLoHi16To32_AVX2(R16, &R_lo, &R_hi);
338
3.25M
    WidenLoHi16To32_AVX2(G16, &G_lo, &G_hi);
339
3.25M
    WidenLoHi16To32_AVX2(B16, &B_lo, &B_hi);
340
3.25M
    {
341
3.25M
      const __m128i u_lo =
342
3.25M
          Convert4_AVX2(R_lo, G_lo, B_lo, coeffs_u, bias_u, shift);
343
3.25M
      const __m128i u_hi =
344
3.25M
          Convert4_AVX2(R_hi, G_hi, B_hi, coeffs_u, bias_u, shift);
345
3.25M
      const __m128i v_lo =
346
3.25M
          Convert4_AVX2(R_lo, G_lo, B_lo, coeffs_v, bias_v, shift);
347
3.25M
      const __m128i v_hi =
348
3.25M
          Convert4_AVX2(R_hi, G_hi, B_hi, coeffs_v, bias_v, shift);
349
3.25M
      const __m128i u16 = _mm_packs_epi32(u_lo, u_hi);
350
3.25M
      const __m128i v16 = _mm_packs_epi32(v_lo, v_hi);
351
3.25M
      if (yuv_bit_depth <= 8) {
352
3.24M
        STORE_64((uint8_t*)u_out + i, _mm_packus_epi16(u16, u16));
353
3.24M
        STORE_64((uint8_t*)v_out + i, _mm_packus_epi16(v16, v16));
354
3.24M
      } else {
355
11.1k
        const __m128i maxv = _mm_set1_epi16((int16_t)yuv_max);
356
11.1k
        STORE_128((uint16_t*)u_out + i, Clip16_128_AVX2(u16, maxv));
357
11.1k
        STORE_128((uint16_t*)v_out + i, Clip16_128_AVX2(v16, maxv));
358
11.1k
      }
359
3.25M
    }
360
3.25M
  }
361
1.15M
  for (; i < uv_w; ++i) {
362
761k
    const int r = best_uv[i + 0 * uv_w_size];
363
761k
    const int g = best_uv[i + 1 * uv_w_size];
364
761k
    const int b = best_uv[i + 2 * uv_w_size];
365
761k
    const int u = SharpYuvConvertComponent(r, g, b, coeffs_u, sfix);
366
761k
    const int v = SharpYuvConvertComponent(r, g, b, coeffs_v, sfix);
367
761k
    if (yuv_bit_depth <= 8) {
368
740k
      ((uint8_t*)u_out)[i] = (uint8_t)SharpYuvClip16(u, 255);
369
740k
      ((uint8_t*)v_out)[i] = (uint8_t)SharpYuvClip16(v, 255);
370
740k
    } else {
371
20.5k
      ((uint16_t*)u_out)[i] = SharpYuvClip16(u, yuv_max);
372
20.5k
      ((uint16_t*)v_out)[i] = SharpYuvClip16(v, yuv_max);
373
20.5k
    }
374
761k
  }
375
398k
}
376
377
//------------------------------------------------------------------------------
378
// BT.709 and sRGB gamma<->linear fast paths for UpdateW/UpdateChroma.
379
// Leverage vpgatherdd gather instruction!
380
381
// Widens 8 samples to 16.16 fixed-point linear values using the
382
// gamma->linear table (shift_right = bit_depth - table_bits, >= 0).
383
static WEBP_INLINE __m256i GammaToLinear8_AVX2(__m128i v16, int shift_right,
384
221M
                                               const uint32_t* g2l_tab) {
385
221M
  const __m256i v32 = _mm256_cvtepu16_epi32(v16);
386
221M
  const __m128i cnt = _mm_cvtsi32_si128(shift_right);
387
221M
  const __m256i pos = _mm256_srl_epi32(v32, cnt);
388
221M
  const __m256i x = _mm256_sub_epi32(v32, _mm256_sll_epi32(pos, cnt));
389
221M
  const __m256i v0 = _mm256_i32gather_epi32((const int*)g2l_tab, pos, 4);
390
221M
  const __m256i v1 = _mm256_i32gather_epi32(
391
221M
      (const int*)g2l_tab, _mm256_add_epi32(pos, _mm256_set1_epi32(1)), 4);
392
  // d * x <= 65536 * 2^shift_right: fits in int32
393
221M
  const __m256i prod = _mm256_mullo_epi32(_mm256_sub_epi32(v1, v0), x);
394
221M
  const int half = (shift_right > 0) ? (1 << (shift_right - 1)) : 0;
395
221M
  const __m256i sum = _mm256_add_epi32(prod, _mm256_set1_epi32(half));
396
221M
  const __m256i shifted = _mm256_srl_epi32(sum, cnt);
397
221M
  return _mm256_add_epi32(v0, shifted);
398
221M
}
399
400
// Reverse direction: 8 16.16 fixed-point values -> 8 gamma samples.
401
static WEBP_INLINE __m256i LinearToGamma8_AVX2(__m256i v, int out_shift,
402
64.6M
                                               const uint32_t* l2g_tab) {
403
64.6M
  const __m256i pos = _mm256_srli_epi32(v, 7);  // table bits are fixed.
404
64.6M
  const __m256i x = _mm256_sub_epi32(v, _mm256_slli_epi32(pos, 7));
405
64.6M
  const __m256i v0_raw = _mm256_i32gather_epi32((const int*)l2g_tab, pos, 4);
406
64.6M
  const __m256i v1_raw = _mm256_i32gather_epi32(
407
64.6M
      (const int*)l2g_tab, _mm256_add_epi32(pos, _mm256_set1_epi32(1)), 4);
408
64.6M
  const __m128i cnt = _mm_cvtsi32_si128(out_shift);
409
64.6M
  const __m256i v0 = _mm256_srl_epi32(v0_raw, cnt);
410
64.6M
  const __m256i v1 = _mm256_srl_epi32(v1_raw, cnt);
411
  // d * x <= 65536 * 128: fits in int32
412
64.6M
  const __m256i prod = _mm256_mullo_epi32(_mm256_sub_epi32(v1, v0), x);
413
64.6M
  const __m256i sum = _mm256_add_epi32(prod, _mm256_set1_epi32(64));
414
64.6M
  const __m256i shifted = _mm256_srai_epi32(sum, 7);
415
64.6M
  return _mm256_add_epi32(v0, shifted);
416
64.6M
}
417
418
// (13933*R + 46871*G + 4732*B + 32768) >> 16, fits tightly in 32bits!
419
static WEBP_INLINE __m256i SharpYuvRGBToGray8_AVX2(__m256i R, __m256i G,
420
46.3M
                                                   __m256i B) {
421
46.3M
  static const int kGrayCoeffs[4] = {13933, 46871, 4732, 0};
422
46.3M
  const __m256i bias = _mm256_set1_epi64x(1LL << (SHARPYUV_YUV_FIX - 1));
423
46.3M
  const __m128i y_lo = Convert4_AVX2(
424
46.3M
      _mm256_castsi256_si128(R), _mm256_castsi256_si128(G),
425
46.3M
      _mm256_castsi256_si128(B), kGrayCoeffs, bias, SHARPYUV_YUV_FIX);
426
46.3M
  const __m128i y_hi = Convert4_AVX2(
427
46.3M
      _mm256_extracti128_si256(R, 1), _mm256_extracti128_si256(G, 1),
428
46.3M
      _mm256_extracti128_si256(B, 1), kGrayCoeffs, bias, SHARPYUV_YUV_FIX);
429
46.3M
  return _mm256_insertf128_si256(_mm256_castsi128_si256(y_lo), y_hi, 1);
430
46.3M
}
431
432
static void UpdateW_AVX2(const uint16_t* src, uint16_t* dst, int w,
433
                         int bit_depth,
434
                         SharpYuvTransferFunctionType transfer_type,
435
2.22M
                         const uint32_t* g2l_tab, const uint32_t* l2g_tab) {
436
2.22M
  const int shift_right = bit_depth - SHARPYUV_GAMMA_TO_LINEAR_TAB_BITS;
437
2.22M
  const int out_shift = SHARPYUV_GAMMA_TO_LINEAR_BITS - bit_depth;
438
2.22M
  const size_t w_size = (size_t)w;
439
2.22M
  int i = 0;
440
2.22M
  assert(shift_right >= 0);
441
39.4M
  for (; i <= w - 8; i += 8) {
442
37.2M
    const __m256i R = GammaToLinear8_AVX2(LOAD_128(src + 0 * w_size + i),
443
37.2M
                                          shift_right, g2l_tab);
444
37.2M
    const __m256i G = GammaToLinear8_AVX2(LOAD_128(src + 1 * w_size + i),
445
37.2M
                                          shift_right, g2l_tab);
446
37.2M
    const __m256i B = GammaToLinear8_AVX2(LOAD_128(src + 2 * w_size + i),
447
37.2M
                                          shift_right, g2l_tab);
448
37.2M
    const __m256i Y = SharpYuvRGBToGray8_AVX2(R, G, B);
449
37.2M
    STORE_128(dst + i, TruncateNarrow32To16_AVX2(
450
37.2M
                           LinearToGamma8_AVX2(Y, out_shift, l2g_tab)));
451
37.2M
  }
452
6.67M
  for (; i < w; ++i) {
453
4.44M
    const uint32_t R =
454
4.44M
        SharpYuvGammaToLinear(src[0 * w_size + i], bit_depth, transfer_type);
455
4.44M
    const uint32_t G =
456
4.44M
        SharpYuvGammaToLinear(src[1 * w_size + i], bit_depth, transfer_type);
457
4.44M
    const uint32_t B =
458
4.44M
        SharpYuvGammaToLinear(src[2 * w_size + i], bit_depth, transfer_type);
459
4.44M
    const int Y = SharpYuvRGBToGray(R, G, B);
460
4.44M
    dst[i] = SharpYuvLinearToGamma((uint32_t)Y, bit_depth, transfer_type);
461
4.44M
  }
462
2.22M
}
463
464
static void SharpYuvUpdateW709_AVX2(const uint16_t* src, uint16_t* dst, int w,
465
2.22M
                                    int bit_depth) {
466
2.22M
  UpdateW_AVX2(src, dst, w, bit_depth, kSharpYuvTransferFunctionBt709,
467
2.22M
               kSharpYuvGammaToLinearTabS, kSharpYuvLinearToGammaTabS);
468
2.22M
}
469
470
static void SharpYuvUpdateWSrgb_AVX2(const uint16_t* src, uint16_t* dst, int w,
471
4.76k
                                     int bit_depth) {
472
4.76k
  UpdateW_AVX2(src, dst, w, bit_depth, kSharpYuvTransferFunctionSrgb,
473
4.76k
               kSharpYuvGammaToLinearTabSrgb, kSharpYuvLinearToGammaTabSrgb);
474
4.76k
}
475
476
// Deinterleaves 8 (a,b) pairs into a=[a0..a7], b=[b0..b7].
477
static WEBP_INLINE void Deinterleave8_AVX2(const uint16_t* p, __m128i* a,
478
54.9M
                                           __m128i* b) {
479
54.9M
  static const uint8_t kM[16] = {0, 1, 4, 5, 8,  9,  12, 13,
480
54.9M
                                 2, 3, 6, 7, 10, 11, 14, 15};
481
54.9M
  const __m256i mask = _mm256_broadcastsi128_si256(LOAD_128(kM));
482
54.9M
  const __m256i v = LOAD_256(p);
483
54.9M
  const __m256i shuffled = _mm256_shuffle_epi8(v, mask);
484
54.9M
  const __m128i lane0 = _mm256_castsi256_si128(shuffled);
485
54.9M
  const __m128i lane1 = _mm256_extracti128_si256(shuffled, 1);
486
54.9M
  *a = _mm_unpacklo_epi64(lane0, lane1);
487
54.9M
  *b = _mm_unpackhi_epi64(lane0, lane1);
488
54.9M
}
489
490
// Box-downsamples 8 consecutive 2x2 uv-groups, matching ScaleDown().
491
static WEBP_INLINE __m128i ScaleDown8_AVX2(const uint16_t* p1,
492
                                           const uint16_t* p2, int shift_right,
493
                                           int out_shift,
494
                                           const uint32_t* g2l_tab,
495
27.4M
                                           const uint32_t* l2g_tab) {
496
27.4M
  __m128i a, b, c, d;
497
27.4M
  Deinterleave8_AVX2(p1, &a, &b);
498
27.4M
  Deinterleave8_AVX2(p2, &c, &d);
499
27.4M
  {
500
27.4M
    const __m256i A = GammaToLinear8_AVX2(a, shift_right, g2l_tab);
501
27.4M
    const __m256i B = GammaToLinear8_AVX2(b, shift_right, g2l_tab);
502
27.4M
    const __m256i C = GammaToLinear8_AVX2(c, shift_right, g2l_tab);
503
27.4M
    const __m256i D = GammaToLinear8_AVX2(d, shift_right, g2l_tab);
504
27.4M
    const __m256i sum =
505
27.4M
        _mm256_add_epi32(_mm256_add_epi32(A, B), _mm256_add_epi32(C, D));
506
27.4M
    const __m256i avg =
507
27.4M
        _mm256_srli_epi32(_mm256_add_epi32(sum, _mm256_set1_epi32(2)), 2);
508
27.4M
    return TruncateNarrow32To16_AVX2(
509
27.4M
        LinearToGamma8_AVX2(avg, out_shift, l2g_tab));
510
27.4M
  }
511
27.4M
}
512
513
static void UpdateChroma_AVX2(const uint16_t* src1, const uint16_t* src2,
514
                              int16_t* dst, int uv_w, int bit_depth,
515
                              SharpYuvTransferFunctionType transfer_type,
516
                              const uint32_t* g2l_tab,
517
1.11M
                              const uint32_t* l2g_tab) {
518
1.11M
  const int shift_right = bit_depth - SHARPYUV_GAMMA_TO_LINEAR_TAB_BITS;
519
1.11M
  const int out_shift = SHARPYUV_GAMMA_TO_LINEAR_BITS - bit_depth;
520
1.11M
  const size_t uv_w_size = (size_t)uv_w;
521
1.11M
  int i = 0;
522
1.11M
  assert(shift_right >= 0);
523
10.2M
  for (; i <= uv_w - 8; i += 8) {
524
9.15M
    const size_t off = 2 * (size_t)i;
525
9.15M
    const __m128i r16 =
526
9.15M
        ScaleDown8_AVX2(src1 + off + 0 * uv_w_size, src2 + off + 0 * uv_w_size,
527
9.15M
                        shift_right, out_shift, g2l_tab, l2g_tab);
528
9.15M
    const __m128i g16 =
529
9.15M
        ScaleDown8_AVX2(src1 + off + 2 * uv_w_size, src2 + off + 2 * uv_w_size,
530
9.15M
                        shift_right, out_shift, g2l_tab, l2g_tab);
531
9.15M
    const __m128i b16 =
532
9.15M
        ScaleDown8_AVX2(src1 + off + 4 * uv_w_size, src2 + off + 4 * uv_w_size,
533
9.15M
                        shift_right, out_shift, g2l_tab, l2g_tab);
534
9.15M
    const __m256i r32 = _mm256_cvtepu16_epi32(r16);
535
9.15M
    const __m256i g32 = _mm256_cvtepu16_epi32(g16);
536
9.15M
    const __m256i b32 = _mm256_cvtepu16_epi32(b16);
537
9.15M
    const __m256i w32 = SharpYuvRGBToGray8_AVX2(r32, g32, b32);
538
9.15M
    STORE_128(dst + i + 0 * uv_w_size,
539
9.15M
              TruncateNarrow32To16_AVX2(_mm256_sub_epi32(r32, w32)));
540
9.15M
    STORE_128(dst + i + 1 * uv_w_size,
541
9.15M
              TruncateNarrow32To16_AVX2(_mm256_sub_epi32(g32, w32)));
542
9.15M
    STORE_128(dst + i + 2 * uv_w_size,
543
9.15M
              TruncateNarrow32To16_AVX2(_mm256_sub_epi32(b32, w32)));
544
9.15M
  }
545
3.43M
  for (; i < uv_w; ++i) {
546
2.31M
    const size_t off = 2 * (size_t)i;
547
2.31M
    uint32_t rgb[3];
548
2.31M
    int c;
549
9.27M
    for (c = 0; c < 3; ++c) {
550
6.95M
      const uint16_t* s1 = src1 + off + 2 * (size_t)c * uv_w_size;
551
6.95M
      const uint16_t* s2 = src2 + off + 2 * (size_t)c * uv_w_size;
552
6.95M
      const uint32_t A = SharpYuvGammaToLinear(s1[0], bit_depth, transfer_type);
553
6.95M
      const uint32_t B = SharpYuvGammaToLinear(s1[1], bit_depth, transfer_type);
554
6.95M
      const uint32_t C = SharpYuvGammaToLinear(s2[0], bit_depth, transfer_type);
555
6.95M
      const uint32_t D = SharpYuvGammaToLinear(s2[1], bit_depth, transfer_type);
556
6.95M
      rgb[c] = SharpYuvLinearToGamma((A + B + C + D + 2) >> 2, bit_depth,
557
6.95M
                                     transfer_type);
558
6.95M
    }
559
2.31M
    {
560
2.31M
      const int W = SharpYuvRGBToGray(rgb[0], rgb[1], rgb[2]);
561
2.31M
      dst[i + 0 * uv_w_size] = (int16_t)((int)rgb[0] - W);
562
2.31M
      dst[i + 1 * uv_w_size] = (int16_t)((int)rgb[1] - W);
563
2.31M
      dst[i + 2 * uv_w_size] = (int16_t)((int)rgb[2] - W);
564
2.31M
    }
565
2.31M
  }
566
1.11M
}
567
568
static void SharpYuvUpdateChroma709_AVX2(const uint16_t* src1,
569
                                         const uint16_t* src2, int16_t* dst,
570
1.11M
                                         int uv_w, int bit_depth) {
571
1.11M
  UpdateChroma_AVX2(src1, src2, dst, uv_w, bit_depth,
572
1.11M
                    kSharpYuvTransferFunctionBt709, kSharpYuvGammaToLinearTabS,
573
1.11M
                    kSharpYuvLinearToGammaTabS);
574
1.11M
}
575
576
static void SharpYuvUpdateChromaSrgb_AVX2(const uint16_t* src1,
577
                                          const uint16_t* src2, int16_t* dst,
578
2.38k
                                          int uv_w, int bit_depth) {
579
2.38k
  UpdateChroma_AVX2(
580
2.38k
      src1, src2, dst, uv_w, bit_depth, kSharpYuvTransferFunctionSrgb,
581
2.38k
      kSharpYuvGammaToLinearTabSrgb, kSharpYuvLinearToGammaTabSrgb);
582
2.38k
}
583
584
//------------------------------------------------------------------------------
585
586
extern void InitSharpYuvAVX2(void);
587
588
6.58k
WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvAVX2(void) {
589
6.58k
  SharpYuvUpdateY = SharpYuvUpdateY_AVX2;
590
6.58k
  SharpYuvUpdateRGB = SharpYuvUpdateRGB_AVX2;
591
6.58k
  SharpYuvFilterRow = SharpYuvFilterRow_AVX2;
592
6.58k
  SharpYuvConvertRowY = SharpYuvConvertRowY_AVX2;
593
6.58k
  SharpYuvConvertRowUV = SharpYuvConvertRowUV_AVX2;
594
6.58k
  SharpYuvUpdateW709 = SharpYuvUpdateW709_AVX2;
595
6.58k
  SharpYuvUpdateChroma709 = SharpYuvUpdateChroma709_AVX2;
596
6.58k
  SharpYuvUpdateWSrgb = SharpYuvUpdateWSrgb_AVX2;
597
6.58k
  SharpYuvUpdateChromaSrgb = SharpYuvUpdateChromaSrgb_AVX2;
598
6.58k
}
599
600
#else  // !WEBP_USE_AVX2
601
602
extern void InitSharpYuvAVX2(void);
603
604
void InitSharpYuvAVX2(void) {}
605
606
#endif  // WEBP_USE_AVX2