Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/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
0
#define LOAD_256(P) _mm256_loadu_si256((const __m256i*)(P))
27
0
#define STORE_256(P, V) _mm256_storeu_si256((__m256i*)(P), (V))
28
0
#define LOAD_128(P) _mm_loadu_si128((const __m128i*)(P))
29
0
#define STORE_128(P, V) _mm_storeu_si128((__m128i*)(P), (V))
30
0
#define LOAD_64(P) _mm_loadl_epi64((const __m128i*)(P))
31
0
#define STORE_64(P, V) _mm_storel_epi64((__m128i*)(P), (V))
32
33
// Clamps 16x (resp. 8x) int16 lanes to [0, max].
34
0
static WEBP_INLINE __m256i Clip16_AVX2(__m256i v, __m256i max) {
35
0
  return _mm256_max_epi16(_mm256_min_epi16(v, max), _mm256_setzero_si256());
36
0
}
37
0
static WEBP_INLINE __m128i Clip16_128_AVX2(__m128i v, __m128i max) {
38
0
  return _mm_max_epi16(_mm_min_epi16(v, max), _mm_setzero_si128());
39
0
}
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
0
                                             __m128i* hi) {
44
0
  *lo = _mm_cvtepi16_epi32(v);
45
0
  *hi = _mm_cvtepi16_epi32(_mm_srli_si128(v, 8));
46
0
}
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
0
  do {                                                                      \
51
0
    const __m256i a0b1 = _mm256_add_##SUFFIX(a0, b1);                       \
52
0
    const __m256i a1b0 = _mm256_add_##SUFFIX(a1, b0);                       \
53
0
    const __m256i a0a1b0b1 = _mm256_add_##SUFFIX(a0b1, a1b0);               \
54
0
    const __m256i a0a1b0b1_8 = _mm256_add_##SUFFIX(a0a1b0b1, kCst8);        \
55
0
    const __m256i a0b1_2 = _mm256_add_##SUFFIX(a0b1, a0b1);                 \
56
0
    const __m256i a1b0_2 = _mm256_add_##SUFFIX(a1b0, a1b0);                 \
57
0
    const __m256i c0_ =                                                     \
58
0
        _mm256_srai_##SUFFIX(_mm256_add_##SUFFIX(a0b1_2, a0a1b0b1_8), 3);   \
59
0
    const __m256i c1_ =                                                     \
60
0
        _mm256_srai_##SUFFIX(_mm256_add_##SUFFIX(a1b0_2, a0a1b0b1_8), 3);   \
61
0
    const __m256i d0_ = _mm256_add_##SUFFIX(c1_, a0);                       \
62
0
    const __m256i d1_ = _mm256_add_##SUFFIX(c0_, a1);                       \
63
0
    e0_out = _mm256_srai_##SUFFIX(d0_, 1);                                  \
64
0
    e1_out = _mm256_srai_##SUFFIX(d1_, 1);                                  \
65
0
  } while (0)
66
67
static uint64_t SharpYuvUpdateY_AVX2(const uint16_t* ref, const uint16_t* src,
68
0
                                     uint16_t* dst, int len, int bit_depth) {
69
0
  const int max_y = (1 << bit_depth) - 1;
70
0
  int i;
71
0
  const __m256i max = _mm256_set1_epi16(max_y);
72
0
  const __m256i one = _mm256_set1_epi16(1);
73
0
  __m256i sum = _mm256_setzero_si256();
74
0
  uint64_t diff;
75
76
0
  for (i = 0; i <= len - 16; i += 16) {
77
0
    const __m256i A = LOAD_256(ref + i);
78
0
    const __m256i B = LOAD_256(src + i);
79
0
    const __m256i C = LOAD_256(dst + i);
80
0
    const __m256i D = _mm256_sub_epi16(A, B);        // diff_y
81
0
    const __m256i F = _mm256_add_epi16(C, D);        // new_y
82
0
    const __m256i AbsD = _mm256_abs_epi16(D);        // abs(diff_y)
83
0
    const __m256i I = _mm256_madd_epi16(AbsD, one);  // 32-bit pairs
84
0
    STORE_256(dst + i, Clip16_AVX2(F, max));
85
0
    sum = _mm256_add_epi32(sum, I);
86
0
  }
87
0
  {
88
0
    uint32_t tmp[8];
89
0
    STORE_256(tmp, sum);
90
0
    diff = (uint64_t)tmp[0] + tmp[1] + tmp[2] + tmp[3] + tmp[4] + tmp[5] +
91
0
           tmp[6] + tmp[7];
92
0
  }
93
0
  for (; i < len; ++i) {
94
0
    const int diff_y = ref[i] - src[i];
95
0
    const int new_y = (int)dst[i] + diff_y;
96
0
    dst[i] = SharpYuvClip16(new_y, max_y);
97
0
    diff += (uint64_t)abs(diff_y);
98
0
  }
99
0
  return diff;
100
0
}
101
102
static void SharpYuvUpdateRGB_AVX2(const int16_t* ref, const int16_t* src,
103
0
                                   int16_t* dst, int len) {
104
0
  int i;
105
0
  for (i = 0; i <= len - 16; i += 16) {
106
0
    const __m256i A = LOAD_256(ref + i);
107
0
    const __m256i B = LOAD_256(src + i);
108
0
    const __m256i C = LOAD_256(dst + i);
109
0
    const __m256i D = _mm256_sub_epi16(A, B);  // diff_uv
110
0
    const __m256i E = _mm256_add_epi16(C, D);  // new_uv
111
0
    STORE_256(dst + i, E);
112
0
  }
113
0
  for (; i < len; ++i) {
114
0
    const int diff_uv = ref[i] - src[i];
115
0
    dst[i] += diff_uv;
116
0
  }
117
0
}
118
119
static void SharpYuvFilterRow16_AVX2(const int16_t* A, const int16_t* B,
120
                                     int len, const uint16_t* best_y,
121
0
                                     uint16_t* out, int bit_depth) {
122
0
  const int max_y = (1 << bit_depth) - 1;
123
0
  int i;
124
0
  const __m256i kCst8 = _mm256_set1_epi16(8);
125
0
  const __m256i max = _mm256_set1_epi16(max_y);
126
0
  for (i = 0; i + 16 <= len; i += 16) {
127
0
    const __m256i a0 = LOAD_256(A + i + 0);
128
0
    const __m256i a1 = LOAD_256(A + i + 1);
129
0
    const __m256i b0 = LOAD_256(B + i + 0);
130
0
    const __m256i b1 = LOAD_256(B + i + 1);
131
0
    __m256i e0, e1;
132
0
    SHARP_FILTER933_AVX2(epi16, a0, a1, b0, b1, kCst8, e0, e1);
133
0
    {
134
0
      const __m256i lo = _mm256_unpacklo_epi16(e0, e1);
135
0
      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
0
      const __m256i f0 = _mm256_permute2x128_si256(lo, hi, 0x20);
139
0
      const __m256i f1 = _mm256_permute2x128_si256(lo, hi, 0x31);
140
0
      const __m256i g0 = LOAD_256(best_y + 2 * i + 0);
141
0
      const __m256i g1 = LOAD_256(best_y + 2 * i + 16);
142
0
      STORE_256(out + 2 * i + 0, Clip16_AVX2(_mm256_add_epi16(g0, f0), max));
143
0
      STORE_256(out + 2 * i + 16, Clip16_AVX2(_mm256_add_epi16(g1, f1), max));
144
0
    }
145
0
  }
146
0
  for (; i < len; ++i) {
147
0
    const int v0 =
148
0
        (A[i + 0] * 9 + A[i + 1] * 3 + B[i + 0] * 3 + B[i + 1] + 8) >> 4;
149
0
    const int v1 =
150
0
        (A[i + 1] * 9 + A[i + 0] * 3 + B[i + 1] * 3 + B[i + 0] + 8) >> 4;
151
0
    out[2 * i + 0] = SharpYuvClip16(best_y[2 * i + 0] + v0, max_y);
152
0
    out[2 * i + 1] = SharpYuvClip16(best_y[2 * i + 1] + v1, max_y);
153
0
  }
154
0
}
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
0
                                     uint16_t* out, int bit_depth) {
164
0
  const int max_y = (1 << bit_depth) - 1;
165
0
  int i;
166
0
  const __m256i kCst8 = _mm256_set1_epi32(8);
167
0
  const __m128i max = _mm_set1_epi16(max_y);
168
0
  for (i = 0; i + 8 <= len; i += 8) {
169
0
    const __m256i a0 = _mm256_cvtepi16_epi32(LOAD_128(A + i + 0));
170
0
    const __m256i a1 = _mm256_cvtepi16_epi32(LOAD_128(A + i + 1));
171
0
    const __m256i b0 = _mm256_cvtepi16_epi32(LOAD_128(B + i + 0));
172
0
    const __m256i b1 = _mm256_cvtepi16_epi32(LOAD_128(B + i + 1));
173
0
    __m256i e0, e1;
174
0
    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
0
    {
178
0
      const __m128i e0_lo = _mm256_castsi256_si128(e0);
179
0
      const __m128i e0_hi = _mm256_extracti128_si256(e0, 1);
180
0
      const __m128i e1_lo = _mm256_castsi256_si128(e1);
181
0
      const __m128i e1_hi = _mm256_extracti128_si256(e1, 1);
182
0
      const __m128i out_lo = _mm_packs_epi32(_mm_unpacklo_epi32(e0_lo, e1_lo),
183
0
                                             _mm_unpackhi_epi32(e0_lo, e1_lo));
184
0
      const __m128i out_hi = _mm_packs_epi32(_mm_unpacklo_epi32(e0_hi, e1_hi),
185
0
                                             _mm_unpackhi_epi32(e0_hi, e1_hi));
186
0
      const __m128i g_lo = LOAD_128(best_y + 2 * i + 0);
187
0
      const __m128i g_hi = LOAD_128(best_y + 2 * i + 8);
188
0
      STORE_128(out + 2 * i + 0,
189
0
                Clip16_128_AVX2(_mm_add_epi16(g_lo, out_lo), max));
190
0
      STORE_128(out + 2 * i + 8,
191
0
                Clip16_128_AVX2(_mm_add_epi16(g_hi, out_hi), max));
192
0
    }
193
0
  }
194
0
  for (; i < len; ++i) {
195
0
    const int a0b1 = A[i + 0] + B[i + 1];
196
0
    const int a1b0 = A[i + 1] + B[i + 0];
197
0
    const int a0a1b0b1 = a0b1 + a1b0 + 8;
198
0
    const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
199
0
    const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
200
0
    out[2 * i + 0] = SharpYuvClip16(best_y[2 * i + 0] + v0, max_y);
201
0
    out[2 * i + 1] = SharpYuvClip16(best_y[2 * i + 1] + v1, max_y);
202
0
  }
203
0
}
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
0
                                   int bit_depth) {
208
0
  if (bit_depth <= 10) {
209
0
    SharpYuvFilterRow16_AVX2(A, B, len, best_y, out, bit_depth);
210
0
  } else {
211
0
    SharpYuvFilterRow32_AVX2(A, B, len, best_y, out, bit_depth);
212
0
  }
213
0
}
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
0
static WEBP_INLINE __m256i Sra64_AVX2(__m256i a, int n) {
222
0
  const __m128i cnt = _mm_cvtsi32_si128(n);
223
0
  const __m128i cnt2 = _mm_cvtsi32_si128(64 - n);
224
0
  const __m256i sign = _mm256_cmpgt_epi64(_mm256_setzero_si256(), a);
225
0
  const __m256i shifted = _mm256_srl_epi64(a, cnt);
226
0
  const __m256i topbits = _mm256_sll_epi64(sign, cnt2);
227
0
  return _mm256_or_si256(shifted, topbits);
228
0
}
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
0
static WEBP_INLINE __m128i TruncateNarrow32To16_AVX2(__m256i v) {
233
0
  const __m256i masked = _mm256_and_si256(v, _mm256_set1_epi32(0xffff));
234
0
  const __m256i packed = _mm256_packus_epi32(masked, masked);
235
0
  const __m128i lo = _mm256_castsi256_si128(packed);
236
0
  const __m128i hi = _mm256_extracti128_si256(packed, 1);
237
0
  return _mm_unpacklo_epi64(lo, hi);
238
0
}
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
0
static WEBP_INLINE __m128i Narrow64To32_AVX2(__m256i v) {
243
0
  const __m256i shuffled = _mm256_shuffle_epi32(v, _MM_SHUFFLE(2, 0, 2, 0));
244
0
  const __m128i lo = _mm256_castsi256_si128(shuffled);
245
0
  const __m128i hi = _mm256_extracti128_si256(shuffled, 1);
246
0
  return _mm_unpacklo_epi64(lo, hi);
247
0
}
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
0
                                         int shift) {
255
0
  const __m256i R64 = _mm256_cvtepi32_epi64(R);
256
0
  const __m256i G64 = _mm256_cvtepi32_epi64(G);
257
0
  const __m256i B64 = _mm256_cvtepi32_epi64(B);
258
0
  __m256i acc = _mm256_add_epi64(
259
0
      bias, _mm256_mul_epi32(R64, _mm256_set1_epi64x(coeffs[0])));
260
0
  acc = _mm256_add_epi64(acc,
261
0
                         _mm256_mul_epi32(G64, _mm256_set1_epi64x(coeffs[1])));
262
0
  acc = _mm256_add_epi64(acc,
263
0
                         _mm256_mul_epi32(B64, _mm256_set1_epi64x(coeffs[2])));
264
0
  acc = Sra64_AVX2(acc, shift);
265
0
  return Narrow64To32_AVX2(acc);
266
0
}
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
0
                                     int yuv_bit_depth, void* y_out) {
272
0
  const int shift = SHARPYUV_YUV_FIX + sfix;
273
0
  const int64_t rounder = 1LL << (shift - 1);
274
0
  const __m256i bias = _mm256_set1_epi64x(coeffs[3] + rounder);
275
0
  const int yuv_max = (1 << yuv_bit_depth) - 1;
276
0
  const size_t uv_w_size = (size_t)uv_w;
277
0
  int i = 0;
278
0
  for (; i <= width - 8; i += 8) {
279
0
    const size_t off = (size_t)(i >> 1);
280
0
    const __m128i W = LOAD_128(best_y + i);
281
0
    const __m128i Ro = LOAD_64(best_uv + off + 0 * uv_w_size);
282
0
    const __m128i Go = LOAD_64(best_uv + off + 1 * uv_w_size);
283
0
    const __m128i Bo = LOAD_64(best_uv + off + 2 * uv_w_size);
284
0
    const __m128i Rv = _mm_add_epi16(_mm_unpacklo_epi16(Ro, Ro), W);
285
0
    const __m128i Gv = _mm_add_epi16(_mm_unpacklo_epi16(Go, Go), W);
286
0
    const __m128i Bv = _mm_add_epi16(_mm_unpacklo_epi16(Bo, Bo), W);
287
0
    __m128i R_lo, R_hi, G_lo, G_hi, B_lo, B_hi;
288
0
    WidenLoHi16To32_AVX2(Rv, &R_lo, &R_hi);
289
0
    WidenLoHi16To32_AVX2(Gv, &G_lo, &G_hi);
290
0
    WidenLoHi16To32_AVX2(Bv, &B_lo, &B_hi);
291
0
    {
292
0
      const __m128i y_lo = Convert4_AVX2(R_lo, G_lo, B_lo, coeffs, bias, shift);
293
0
      const __m128i y_hi = Convert4_AVX2(R_hi, G_hi, B_hi, coeffs, bias, shift);
294
0
      const __m128i y16 = _mm_packs_epi32(y_lo, y_hi);  // saturating:
295
                                                        // matches
296
                                                        // clip()/clip_8b().
297
0
      if (yuv_bit_depth <= 8) {
298
0
        STORE_64((uint8_t*)y_out + i, _mm_packus_epi16(y16, y16));
299
0
      } else {
300
0
        const __m128i maxv = _mm_set1_epi16((int16_t)yuv_max);
301
0
        STORE_128((uint16_t*)y_out + i, Clip16_128_AVX2(y16, maxv));
302
0
      }
303
0
    }
304
0
  }
305
0
  for (; i < width; ++i) {
306
0
    const size_t off = (size_t)(i >> 1);
307
0
    const int W = best_y[i];
308
0
    const int r = best_uv[off + 0 * uv_w_size] + W;
309
0
    const int g = best_uv[off + 1 * uv_w_size] + W;
310
0
    const int b = best_uv[off + 2 * uv_w_size] + W;
311
0
    const int y = SharpYuvConvertComponent(r, g, b, coeffs, sfix);
312
0
    if (yuv_bit_depth <= 8) {
313
0
      ((uint8_t*)y_out)[i] = (uint8_t)SharpYuvClip16(y, 255);
314
0
    } else {
315
0
      ((uint16_t*)y_out)[i] = SharpYuvClip16(y, yuv_max);
316
0
    }
317
0
  }
318
0
}
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
0
                                      void* v_out) {
325
0
  const int shift = SHARPYUV_YUV_FIX + sfix;
326
0
  const int64_t rounder = 1LL << (shift - 1);
327
0
  const __m256i bias_u = _mm256_set1_epi64x(coeffs_u[3] + rounder);
328
0
  const __m256i bias_v = _mm256_set1_epi64x(coeffs_v[3] + rounder);
329
0
  const int yuv_max = (1 << yuv_bit_depth) - 1;
330
0
  const size_t uv_w_size = (size_t)uv_w;
331
0
  int i = 0;
332
0
  for (; i <= uv_w - 8; i += 8) {
333
0
    const __m128i R16 = LOAD_128(best_uv + i + 0 * uv_w_size);
334
0
    const __m128i G16 = LOAD_128(best_uv + i + 1 * uv_w_size);
335
0
    const __m128i B16 = LOAD_128(best_uv + i + 2 * uv_w_size);
336
0
    __m128i R_lo, R_hi, G_lo, G_hi, B_lo, B_hi;
337
0
    WidenLoHi16To32_AVX2(R16, &R_lo, &R_hi);
338
0
    WidenLoHi16To32_AVX2(G16, &G_lo, &G_hi);
339
0
    WidenLoHi16To32_AVX2(B16, &B_lo, &B_hi);
340
0
    {
341
0
      const __m128i u_lo =
342
0
          Convert4_AVX2(R_lo, G_lo, B_lo, coeffs_u, bias_u, shift);
343
0
      const __m128i u_hi =
344
0
          Convert4_AVX2(R_hi, G_hi, B_hi, coeffs_u, bias_u, shift);
345
0
      const __m128i v_lo =
346
0
          Convert4_AVX2(R_lo, G_lo, B_lo, coeffs_v, bias_v, shift);
347
0
      const __m128i v_hi =
348
0
          Convert4_AVX2(R_hi, G_hi, B_hi, coeffs_v, bias_v, shift);
349
0
      const __m128i u16 = _mm_packs_epi32(u_lo, u_hi);
350
0
      const __m128i v16 = _mm_packs_epi32(v_lo, v_hi);
351
0
      if (yuv_bit_depth <= 8) {
352
0
        STORE_64((uint8_t*)u_out + i, _mm_packus_epi16(u16, u16));
353
0
        STORE_64((uint8_t*)v_out + i, _mm_packus_epi16(v16, v16));
354
0
      } else {
355
0
        const __m128i maxv = _mm_set1_epi16((int16_t)yuv_max);
356
0
        STORE_128((uint16_t*)u_out + i, Clip16_128_AVX2(u16, maxv));
357
0
        STORE_128((uint16_t*)v_out + i, Clip16_128_AVX2(v16, maxv));
358
0
      }
359
0
    }
360
0
  }
361
0
  for (; i < uv_w; ++i) {
362
0
    const int r = best_uv[i + 0 * uv_w_size];
363
0
    const int g = best_uv[i + 1 * uv_w_size];
364
0
    const int b = best_uv[i + 2 * uv_w_size];
365
0
    const int u = SharpYuvConvertComponent(r, g, b, coeffs_u, sfix);
366
0
    const int v = SharpYuvConvertComponent(r, g, b, coeffs_v, sfix);
367
0
    if (yuv_bit_depth <= 8) {
368
0
      ((uint8_t*)u_out)[i] = (uint8_t)SharpYuvClip16(u, 255);
369
0
      ((uint8_t*)v_out)[i] = (uint8_t)SharpYuvClip16(v, 255);
370
0
    } else {
371
0
      ((uint16_t*)u_out)[i] = SharpYuvClip16(u, yuv_max);
372
0
      ((uint16_t*)v_out)[i] = SharpYuvClip16(v, yuv_max);
373
0
    }
374
0
  }
375
0
}
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
0
                                               const uint32_t* g2l_tab) {
385
0
  const __m256i v32 = _mm256_cvtepu16_epi32(v16);
386
0
  const __m128i cnt = _mm_cvtsi32_si128(shift_right);
387
0
  const __m256i pos = _mm256_srl_epi32(v32, cnt);
388
0
  const __m256i x = _mm256_sub_epi32(v32, _mm256_sll_epi32(pos, cnt));
389
0
  const __m256i v0 = _mm256_i32gather_epi32((const int*)g2l_tab, pos, 4);
390
0
  const __m256i v1 = _mm256_i32gather_epi32(
391
0
      (const int*)g2l_tab, _mm256_add_epi32(pos, _mm256_set1_epi32(1)), 4);
392
  // d * x <= 65536 * 2^shift_right: fits in int32
393
0
  const __m256i prod = _mm256_mullo_epi32(_mm256_sub_epi32(v1, v0), x);
394
0
  const int half = (shift_right > 0) ? (1 << (shift_right - 1)) : 0;
395
0
  const __m256i sum = _mm256_add_epi32(prod, _mm256_set1_epi32(half));
396
0
  const __m256i shifted = _mm256_srl_epi32(sum, cnt);
397
0
  return _mm256_add_epi32(v0, shifted);
398
0
}
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
0
                                               const uint32_t* l2g_tab) {
403
0
  const __m256i pos = _mm256_srli_epi32(v, 7);  // table bits are fixed.
404
0
  const __m256i x = _mm256_sub_epi32(v, _mm256_slli_epi32(pos, 7));
405
0
  const __m256i v0_raw = _mm256_i32gather_epi32((const int*)l2g_tab, pos, 4);
406
0
  const __m256i v1_raw = _mm256_i32gather_epi32(
407
0
      (const int*)l2g_tab, _mm256_add_epi32(pos, _mm256_set1_epi32(1)), 4);
408
0
  const __m128i cnt = _mm_cvtsi32_si128(out_shift);
409
0
  const __m256i v0 = _mm256_srl_epi32(v0_raw, cnt);
410
0
  const __m256i v1 = _mm256_srl_epi32(v1_raw, cnt);
411
  // d * x <= 65536 * 128: fits in int32
412
0
  const __m256i prod = _mm256_mullo_epi32(_mm256_sub_epi32(v1, v0), x);
413
0
  const __m256i sum = _mm256_add_epi32(prod, _mm256_set1_epi32(64));
414
0
  const __m256i shifted = _mm256_srai_epi32(sum, 7);
415
0
  return _mm256_add_epi32(v0, shifted);
416
0
}
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
0
                                                   __m256i B) {
421
0
  static const int kGrayCoeffs[4] = {13933, 46871, 4732, 0};
422
0
  const __m256i bias = _mm256_set1_epi64x(1LL << (SHARPYUV_YUV_FIX - 1));
423
0
  const __m128i y_lo = Convert4_AVX2(
424
0
      _mm256_castsi256_si128(R), _mm256_castsi256_si128(G),
425
0
      _mm256_castsi256_si128(B), kGrayCoeffs, bias, SHARPYUV_YUV_FIX);
426
0
  const __m128i y_hi = Convert4_AVX2(
427
0
      _mm256_extracti128_si256(R, 1), _mm256_extracti128_si256(G, 1),
428
0
      _mm256_extracti128_si256(B, 1), kGrayCoeffs, bias, SHARPYUV_YUV_FIX);
429
0
  return _mm256_insertf128_si256(_mm256_castsi128_si256(y_lo), y_hi, 1);
430
0
}
431
432
static void UpdateW_AVX2(const uint16_t* src, uint16_t* dst, int w,
433
                         int bit_depth,
434
                         SharpYuvTransferFunctionType transfer_type,
435
0
                         const uint32_t* g2l_tab, const uint32_t* l2g_tab) {
436
0
  const int shift_right = bit_depth - SHARPYUV_GAMMA_TO_LINEAR_TAB_BITS;
437
0
  const int out_shift = SHARPYUV_GAMMA_TO_LINEAR_BITS - bit_depth;
438
0
  const size_t w_size = (size_t)w;
439
0
  int i = 0;
440
0
  assert(shift_right >= 0);
441
0
  for (; i <= w - 8; i += 8) {
442
0
    const __m256i R = GammaToLinear8_AVX2(LOAD_128(src + 0 * w_size + i),
443
0
                                          shift_right, g2l_tab);
444
0
    const __m256i G = GammaToLinear8_AVX2(LOAD_128(src + 1 * w_size + i),
445
0
                                          shift_right, g2l_tab);
446
0
    const __m256i B = GammaToLinear8_AVX2(LOAD_128(src + 2 * w_size + i),
447
0
                                          shift_right, g2l_tab);
448
0
    const __m256i Y = SharpYuvRGBToGray8_AVX2(R, G, B);
449
0
    STORE_128(dst + i, TruncateNarrow32To16_AVX2(
450
0
                           LinearToGamma8_AVX2(Y, out_shift, l2g_tab)));
451
0
  }
452
0
  for (; i < w; ++i) {
453
0
    const uint32_t R =
454
0
        SharpYuvGammaToLinear(src[0 * w_size + i], bit_depth, transfer_type);
455
0
    const uint32_t G =
456
0
        SharpYuvGammaToLinear(src[1 * w_size + i], bit_depth, transfer_type);
457
0
    const uint32_t B =
458
0
        SharpYuvGammaToLinear(src[2 * w_size + i], bit_depth, transfer_type);
459
0
    const int Y = SharpYuvRGBToGray(R, G, B);
460
0
    dst[i] = SharpYuvLinearToGamma((uint32_t)Y, bit_depth, transfer_type);
461
0
  }
462
0
}
463
464
static void SharpYuvUpdateW709_AVX2(const uint16_t* src, uint16_t* dst, int w,
465
0
                                    int bit_depth) {
466
0
  UpdateW_AVX2(src, dst, w, bit_depth, kSharpYuvTransferFunctionBt709,
467
0
               kSharpYuvGammaToLinearTabS, kSharpYuvLinearToGammaTabS);
468
0
}
469
470
static void SharpYuvUpdateWSrgb_AVX2(const uint16_t* src, uint16_t* dst, int w,
471
0
                                     int bit_depth) {
472
0
  UpdateW_AVX2(src, dst, w, bit_depth, kSharpYuvTransferFunctionSrgb,
473
0
               kSharpYuvGammaToLinearTabSrgb, kSharpYuvLinearToGammaTabSrgb);
474
0
}
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
0
                                           __m128i* b) {
479
0
  static const uint8_t kM[16] = {0, 1, 4, 5, 8,  9,  12, 13,
480
0
                                 2, 3, 6, 7, 10, 11, 14, 15};
481
0
  const __m256i mask = _mm256_broadcastsi128_si256(LOAD_128(kM));
482
0
  const __m256i v = LOAD_256(p);
483
0
  const __m256i shuffled = _mm256_shuffle_epi8(v, mask);
484
0
  const __m128i lane0 = _mm256_castsi256_si128(shuffled);
485
0
  const __m128i lane1 = _mm256_extracti128_si256(shuffled, 1);
486
0
  *a = _mm_unpacklo_epi64(lane0, lane1);
487
0
  *b = _mm_unpackhi_epi64(lane0, lane1);
488
0
}
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
0
                                           const uint32_t* l2g_tab) {
496
0
  __m128i a, b, c, d;
497
0
  Deinterleave8_AVX2(p1, &a, &b);
498
0
  Deinterleave8_AVX2(p2, &c, &d);
499
0
  {
500
0
    const __m256i A = GammaToLinear8_AVX2(a, shift_right, g2l_tab);
501
0
    const __m256i B = GammaToLinear8_AVX2(b, shift_right, g2l_tab);
502
0
    const __m256i C = GammaToLinear8_AVX2(c, shift_right, g2l_tab);
503
0
    const __m256i D = GammaToLinear8_AVX2(d, shift_right, g2l_tab);
504
0
    const __m256i sum =
505
0
        _mm256_add_epi32(_mm256_add_epi32(A, B), _mm256_add_epi32(C, D));
506
0
    const __m256i avg =
507
0
        _mm256_srli_epi32(_mm256_add_epi32(sum, _mm256_set1_epi32(2)), 2);
508
0
    return TruncateNarrow32To16_AVX2(
509
0
        LinearToGamma8_AVX2(avg, out_shift, l2g_tab));
510
0
  }
511
0
}
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
0
                              const uint32_t* l2g_tab) {
518
0
  const int shift_right = bit_depth - SHARPYUV_GAMMA_TO_LINEAR_TAB_BITS;
519
0
  const int out_shift = SHARPYUV_GAMMA_TO_LINEAR_BITS - bit_depth;
520
0
  const size_t uv_w_size = (size_t)uv_w;
521
0
  int i = 0;
522
0
  assert(shift_right >= 0);
523
0
  for (; i <= uv_w - 8; i += 8) {
524
0
    const size_t off = 2 * (size_t)i;
525
0
    const __m128i r16 =
526
0
        ScaleDown8_AVX2(src1 + off + 0 * uv_w_size, src2 + off + 0 * uv_w_size,
527
0
                        shift_right, out_shift, g2l_tab, l2g_tab);
528
0
    const __m128i g16 =
529
0
        ScaleDown8_AVX2(src1 + off + 2 * uv_w_size, src2 + off + 2 * uv_w_size,
530
0
                        shift_right, out_shift, g2l_tab, l2g_tab);
531
0
    const __m128i b16 =
532
0
        ScaleDown8_AVX2(src1 + off + 4 * uv_w_size, src2 + off + 4 * uv_w_size,
533
0
                        shift_right, out_shift, g2l_tab, l2g_tab);
534
0
    const __m256i r32 = _mm256_cvtepu16_epi32(r16);
535
0
    const __m256i g32 = _mm256_cvtepu16_epi32(g16);
536
0
    const __m256i b32 = _mm256_cvtepu16_epi32(b16);
537
0
    const __m256i w32 = SharpYuvRGBToGray8_AVX2(r32, g32, b32);
538
0
    STORE_128(dst + i + 0 * uv_w_size,
539
0
              TruncateNarrow32To16_AVX2(_mm256_sub_epi32(r32, w32)));
540
0
    STORE_128(dst + i + 1 * uv_w_size,
541
0
              TruncateNarrow32To16_AVX2(_mm256_sub_epi32(g32, w32)));
542
0
    STORE_128(dst + i + 2 * uv_w_size,
543
0
              TruncateNarrow32To16_AVX2(_mm256_sub_epi32(b32, w32)));
544
0
  }
545
0
  for (; i < uv_w; ++i) {
546
0
    const size_t off = 2 * (size_t)i;
547
0
    uint32_t rgb[3];
548
0
    int c;
549
0
    for (c = 0; c < 3; ++c) {
550
0
      const uint16_t* s1 = src1 + off + 2 * (size_t)c * uv_w_size;
551
0
      const uint16_t* s2 = src2 + off + 2 * (size_t)c * uv_w_size;
552
0
      const uint32_t A = SharpYuvGammaToLinear(s1[0], bit_depth, transfer_type);
553
0
      const uint32_t B = SharpYuvGammaToLinear(s1[1], bit_depth, transfer_type);
554
0
      const uint32_t C = SharpYuvGammaToLinear(s2[0], bit_depth, transfer_type);
555
0
      const uint32_t D = SharpYuvGammaToLinear(s2[1], bit_depth, transfer_type);
556
0
      rgb[c] = SharpYuvLinearToGamma((A + B + C + D + 2) >> 2, bit_depth,
557
0
                                     transfer_type);
558
0
    }
559
0
    {
560
0
      const int W = SharpYuvRGBToGray(rgb[0], rgb[1], rgb[2]);
561
0
      dst[i + 0 * uv_w_size] = (int16_t)((int)rgb[0] - W);
562
0
      dst[i + 1 * uv_w_size] = (int16_t)((int)rgb[1] - W);
563
0
      dst[i + 2 * uv_w_size] = (int16_t)((int)rgb[2] - W);
564
0
    }
565
0
  }
566
0
}
567
568
static void SharpYuvUpdateChroma709_AVX2(const uint16_t* src1,
569
                                         const uint16_t* src2, int16_t* dst,
570
0
                                         int uv_w, int bit_depth) {
571
0
  UpdateChroma_AVX2(src1, src2, dst, uv_w, bit_depth,
572
0
                    kSharpYuvTransferFunctionBt709, kSharpYuvGammaToLinearTabS,
573
0
                    kSharpYuvLinearToGammaTabS);
574
0
}
575
576
static void SharpYuvUpdateChromaSrgb_AVX2(const uint16_t* src1,
577
                                          const uint16_t* src2, int16_t* dst,
578
0
                                          int uv_w, int bit_depth) {
579
0
  UpdateChroma_AVX2(
580
0
      src1, src2, dst, uv_w, bit_depth, kSharpYuvTransferFunctionSrgb,
581
0
      kSharpYuvGammaToLinearTabSrgb, kSharpYuvLinearToGammaTabSrgb);
582
0
}
583
584
//------------------------------------------------------------------------------
585
586
extern void InitSharpYuvAVX2(void);
587
588
0
WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvAVX2(void) {
589
0
  SharpYuvUpdateY = SharpYuvUpdateY_AVX2;
590
0
  SharpYuvUpdateRGB = SharpYuvUpdateRGB_AVX2;
591
0
  SharpYuvFilterRow = SharpYuvFilterRow_AVX2;
592
0
  SharpYuvConvertRowY = SharpYuvConvertRowY_AVX2;
593
0
  SharpYuvConvertRowUV = SharpYuvConvertRowUV_AVX2;
594
0
  SharpYuvUpdateW709 = SharpYuvUpdateW709_AVX2;
595
0
  SharpYuvUpdateChroma709 = SharpYuvUpdateChroma709_AVX2;
596
0
  SharpYuvUpdateWSrgb = SharpYuvUpdateWSrgb_AVX2;
597
0
  SharpYuvUpdateChromaSrgb = SharpYuvUpdateChromaSrgb_AVX2;
598
0
}
599
600
#else  // !WEBP_USE_AVX2
601
602
extern void InitSharpYuvAVX2(void);
603
604
void InitSharpYuvAVX2(void) {}
605
606
#endif  // WEBP_USE_AVX2