forge
Explore
Log in

Don't you dare go Hollow

ec-vp8 perf: MC kernels -- AVX2 six-tap + bilinear passes

Browse files at this commit

c4fe59653e7e5e5ef6d1b39fa3bc923a097b65ecAhmet Kaan GümüşParent: 289ae2c
sixtap_block and bilinear_block (18% of a decode: the sub-pel MC passes
for luma and chroma) now run on explicit std::arch::x86_64 AVX2 kernels
following the ec-av1 mc::simd pattern: the scalar functions stay as the
libvpx transcription and the reference oracle, feature-gated at the
block level via a LazyLock capability probe.

Both passes share one clamped u8 intermediate at a fixed 16-lane stride
(the first pass is clamped to 0..=255 by filter.c's clamp_pixel, so u8
storage is representation-only; lanes bw..16 hold discarded values).
The six-tap kernels pair adjacent samples into _mm*_madd_epi16 with
packed tap pairs -- exact in i32 (|byte*tap| <= 255*128, six-product
sums < 2^23) -- and the two-stage packs/packus narrowing reproduces the
per-pass clamp exactly: packs' i16 saturation provably never fires
(range [-76, 319]) and packus' u8 saturation IS clamp_pixel. Bilinear
taps are non-negative and sum to 128, so its i16 accumulator is exact.

Also repairs a debug-build regression from 2e4c5f96's chunked loop-
filter loads: the hoisted row address 'anchor - (4 - k) * stride'
underflows usize for k past the upper taps at frame-top edges (the
add-first form anchor + k*stride - 4*stride is what the per-pixel code
evaluated). The three h-edge kernels get the add-first form plus the
bounds contract as debug_asserts; the three driver tests that had been
panicking in debug builds are green again (57 lib tests now).

Witness suite: 57 lib + 4 header_parse + 4 keyframe_exact + 3
stream_exact, byte-exact, debug and release.
crates/ec-vp8/src/loopfilter.rsmodified+13 -3
@@ -493,8 +493,12 @@ fn filter_mb_h_edge(
493 // per-column).493 // per-column).
494 let n = 8 * size;494 let n = 8 * size;
495 let mut px = [[0u8; 64]; 8];495 let mut px = [[0u8; 64]; 8];
496+ debug_assert!(anchor >= 4 * stride && anchor + 3 * stride + n <= plane.len());
496 for (k, r) in px.iter_mut().enumerate() {497 for (k, r) in px.iter_mut().enumerate() {
497- let a = anchor - (4 - k) * stride;498+ // Add the row offset before backing up 4 rows: `anchor` is only
499+ // guaranteed to sit >= 4 rows into the plane, and the subtracted
500+ // form `anchor - (4 - k) * stride` underflows for k > 4.
501+ let a = anchor + k * stride - 4 * stride;
498 r[..n].copy_from_slice(&plane[a..a + n]);502 r[..n].copy_from_slice(&plane[a..a + n]);
499 }503 }
500 for i in 0..n {504 for i in 0..n {
@@ -542,8 +546,11 @@ fn filter_subblock_h_edge(
542 ) {546 ) {
543 let n = 8 * size;547 let n = 8 * size;
544 let mut px = [[0u8; 64]; 8];548 let mut px = [[0u8; 64]; 8];
549+ debug_assert!(anchor >= 4 * stride && anchor + 3 * stride + n <= plane.len());
545 for (k, r) in px.iter_mut().enumerate() {550 for (k, r) in px.iter_mut().enumerate() {
546- let a = anchor - (4 - k) * stride;551+ // Same add-first form as [`filter_mb_h_edge`]: the subtracted
552+ // expression underflows for k > 4.
553+ let a = anchor + k * stride - 4 * stride;
547 r[..n].copy_from_slice(&plane[a..a + n]);554 r[..n].copy_from_slice(&plane[a..a + n]);
548 }555 }
549 for i in 0..n {556 for i in 0..n {
@@ -591,8 +598,11 @@ fn filter_v_edge_simple(plane: &mut [u8], stride: usize, anchor: usize, limit: i
591 fn filter_h_edge_simple(plane: &mut [u8], stride: usize, anchor: usize, limit: i32) {598 fn filter_h_edge_simple(plane: &mut [u8], stride: usize, anchor: usize, limit: i32) {
592 // Contiguous columns, strided taps: chunked loads, conditional stores.599 // Contiguous columns, strided taps: chunked loads, conditional stores.
593 let mut px = [[0u8; 16]; 4];600 let mut px = [[0u8; 16]; 4];
601+ debug_assert!(anchor >= 2 * stride && anchor + stride + 16 <= plane.len());
594 for (k, r) in px.iter_mut().enumerate() {602 for (k, r) in px.iter_mut().enumerate() {
595- let a = anchor - (2 - k) * stride;603+ // Add-first form (see [`filter_mb_h_edge`]): the subtracted
604+ // expression underflows for k > 2.
605+ let a = anchor + k * stride - 2 * stride;
596 r.copy_from_slice(&plane[a..a + 16]);606 r.copy_from_slice(&plane[a..a + 16]);
597 }607 }
598 for i in 0..16 {608 for i in 0..16 {
crates/ec-vp8/src/mc.rsmodified+471 -25
@@ -154,6 +154,13 @@ pub(crate) fn predict_luma(
154 /// Six-tap separable sub-pel predictor (libvpx vp8_sixtap_predict*,154 /// Six-tap separable sub-pel predictor (libvpx vp8_sixtap_predict*,
155 /// filter.c). `(sx, sy)` is the full-pel block origin in the bordered155 /// filter.c). `(sx, sy)` is the full-pel block origin in the bordered
156 /// reference, `(fx, fy)` the sub-pel phases 0..7 (0 = copy).156 /// reference, `(fx, fy)` the sub-pel phases 0..7 (0 = copy).
157+///
158+/// Both passes share one clamped intermediate buffer: the first pass is
159+/// clamped to 0..=255 per pass (libvpx `clamp_pixel`), so it is stored as
160+/// `u8` at a fixed 16-lane stride. The AVX2 kernels always compute all 16
161+/// lanes of an intermediate row (reads stay inside the 32-px border for
162+/// every block shape) and the vertical pass discards the tail lanes with
163+/// its `bw`-byte store; the scalar reference only fills the first `bw`.
157 fn sixtap_block(164 fn sixtap_block(
158 src: &[u8],165 src: &[u8],
159 stride: usize,166 stride: usize,
@@ -177,13 +184,44 @@ fn sixtap_block(
177 184
178 let hf = &SUB_PEL_FILTERS[fx];185 let hf = &SUB_PEL_FILTERS[fx];
179 let vf = &SUB_PEL_FILTERS[fy];186 let vf = &SUB_PEL_FILTERS[fy];
187+ let rows = bh + 5;
188+ let mut tmp = [0u8; 21 * 16]; // C: int FData[21 * 24], clamped to u8
180 189
181- // filter.c filter_block2d_first_pass on `src - 2*stride`: `bh + 5`190+ #[cfg(target_arch = "x86_64")]
182- // horizontal lines (2 above the block, 3 below, for the vertical taps).191+ {
183- // Phase 0 is the exact identity (`(p * 128 + 64) >> 7 == p`), so the192+ if avx2_supported() {
184- // uniform loop reproduces the C for every phase combination.193+ // SAFETY: AVX2 availability checked by `avx2_supported` just
185- let mut tmp = [0i32; 21 * 16]; // C: int FData[21 * 24]194+ // above; the border/bounds contracts of both kernels hold for
186- for i in 0..bh + 5 {195+ // every caller (`bw`/`bh` in 4..=16, bordered source plane).
196+ #[allow(unsafe_code)]
197+ unsafe {
198+ simd::sixtap_h(src, stride, sx, sy, hf, &mut tmp, rows);
199+ simd::sixtap_v(&tmp, vf, dst, dst_stride, bw, bh);
200+ }
201+ return;
202+ }
203+ }
204+
205+ sixtap_h_scalar(src, stride, sx, sy, hf, &mut tmp, bw, rows);
206+ sixtap_v_scalar(&tmp, vf, dst, dst_stride, bw, bh);
207+}
208+
209+/// Scalar first pass of [`sixtap_block`] (filter.c
210+/// `filter_block2d_first_pass` on `src - 2*stride`): `rows` = `bh + 5`
211+/// horizontal lines, each sample `(dot + 64) >> 7` clamped to 0..=255.
212+/// Phase 0 is the exact identity (`(p * 128 + 64) >> 7 == p`), so the
213+/// uniform loop reproduces the C for every phase combination.
214+fn sixtap_h_scalar(
215+ src: &[u8],
216+ stride: usize,
217+ sx: usize,
218+ sy: usize,
219+ hf: &[i32; 6],
220+ tmp: &mut [u8; 21 * 16],
221+ bw: usize,
222+ rows: usize,
223+) {
224+ for i in 0..rows {
187 let row = (sy + i - 2) * stride + sx - 2;225 let row = (sy + i - 2) * stride + sx - 2;
188 for j in 0..bw {226 for j in 0..bw {
189 let p = row + j;227 let p = row + j;
@@ -194,30 +232,278 @@ fn sixtap_block(
194 + i32::from(src[p + 4]) * hf[4]232 + i32::from(src[p + 4]) * hf[4]
195 + i32::from(src[p + 5]) * hf[5]233 + i32::from(src[p + 5]) * hf[5]
196 + (VP8_FILTER_WEIGHT >> 1); /* Rounding */234 + (VP8_FILTER_WEIGHT >> 1); /* Rounding */
197- tmp[i * bw + j] = clamp255(acc >> VP8_FILTER_SHIFT);235+ tmp[i * 16 + j] = clamp255(acc >> VP8_FILTER_SHIFT) as u8;
236+ }
198 }237 }
199 }238 }
200 239
201- // filter.c filter_block2d_second_pass on `FData + 2*bw`: taps read the240+/// Scalar second pass of [`sixtap_block`] (filter.c
202- // intermediate rows `r .. r + 5`.241+/// `filter_block2d_second_pass` on `FData + 2*bw`): taps read the
242+/// intermediate rows `r .. r + 5` at the fixed 16-lane stride.
243+fn sixtap_v_scalar(
244+ tmp: &[u8; 21 * 16],
245+ vf: &[i32; 6],
246+ dst: &mut [u8],
247+ dst_stride: usize,
248+ bw: usize,
249+ bh: usize,
250+) {
203 for r in 0..bh {251 for r in 0..bh {
204 for c in 0..bw {252 for c in 0..bw {
205- let acc = tmp[r * bw + c] * vf[0]253+ let acc = i32::from(tmp[r * 16 + c]) * vf[0]
206- + tmp[(r + 1) * bw + c] * vf[1]254+ + i32::from(tmp[(r + 1) * 16 + c]) * vf[1]
207- + tmp[(r + 2) * bw + c] * vf[2]255+ + i32::from(tmp[(r + 2) * 16 + c]) * vf[2]
208- + tmp[(r + 3) * bw + c] * vf[3]256+ + i32::from(tmp[(r + 3) * 16 + c]) * vf[3]
209- + tmp[(r + 4) * bw + c] * vf[4]257+ + i32::from(tmp[(r + 4) * 16 + c]) * vf[4]
210- + tmp[(r + 5) * bw + c] * vf[5]258+ + i32::from(tmp[(r + 5) * 16 + c]) * vf[5]
211 + (VP8_FILTER_WEIGHT >> 1); /* Rounding */259 + (VP8_FILTER_WEIGHT >> 1); /* Rounding */
212 dst[r * dst_stride + c] = clamp255(acc >> VP8_FILTER_SHIFT) as u8;260 dst[r * dst_stride + c] = clamp255(acc >> VP8_FILTER_SHIFT) as u8;
213 }261 }
214 }262 }
215 }263 }
216 264
265+/// Cached AVX2 availability (the per-block detection load is cheap, but
266+/// MC blocks are the hottest call in an inter decode).
267+#[cfg(target_arch = "x86_64")]
268+fn avx2_supported() -> bool {
269+ static AVX2: std::sync::LazyLock<bool> =
270+ std::sync::LazyLock::new(|| std::arch::is_x86_feature_detected!("avx2"));
271+ *AVX2
272+}
273+
274+/// Explicit `std::arch::x86_64` kernels for the six-tap and bilinear
275+/// passes (the 18% of a decode the profile pins on `sixtap_block` and its
276+/// chroma siblings). Nothing here is spec logic: each kernel is the scalar
277+/// function's arithmetic lane-parallel, with the same rounding, the same
278+/// truncation points and the same clamps -- the equivalence tests run
279+/// every kernel against its scalar reference over all shapes, phases and
280+/// worst-case input bytes.
281+///
282+/// Exactness notes shared by the six-tap kernels:
283+/// - `_mm*_madd_epi16` multiplies *pairs of samples* by a packed tap pair
284+/// and accumulates in `i32`; every product is exact (widened samples
285+/// are 0..=255, `|byte * tap| <= 255 * 128 < 2^15`) and the six-product
286+/// sum is exact in `i32` (`|dot + 64| <= 255 * 160 + 64 < 2^23`).
287+/// - The even/odd column split exists because `madd` pairs *adjacent*
288+/// samples: the even accumulator's `madd` lane m is output `2m`, the
289+/// odd one output `2m+1` (load offset +1 shifts the sample window).
290+/// - `packs_epi32`'s `i32 -> i16` saturation can never fire: after
291+/// `(dot + 64) >> 7` the range is `[-76, 319]`.
292+/// - `packus_epi16`'s `u8` saturation *is* filter.c's `clamp_pixel`:
293+/// values below 0 saturate to 0, above 255 to 255, the rest unchanged.
294+#[cfg(target_arch = "x86_64")]
295+#[allow(unsafe_code)]
296+#[allow(unsafe_op_in_unsafe_fn)] // each kernel's whole body is its contract
297+mod simd {
298+ use super::{VP8_FILTER_SHIFT, VP8_FILTER_WEIGHT};
299+ use std::arch::x86_64::*;
300+
301+ /// Tap pair `[t0, t1]` packed into one `i32`, the operand shape
302+ /// `_mm*_madd_epi16` wants: lane `j` of `madd(samples, set1(pair))`
303+ /// is `s[2j] * t0 + s[2j+1] * t1` (ec-av1 `mc::simd::tap_pairs`).
304+ #[inline]
305+ fn tap_pair(a: i32, b: i32) -> i32 {
306+ (b << 16) | (a as u16 as i32)
307+ }
308+
309+ /// Six-tap first pass, all 16 lanes of every intermediate row (see
310+ /// [`sixtap_block`] for the layout).
311+ ///
312+ /// # Safety
313+ /// Requires AVX2. `src` must be a bordered MC plane such that for
314+ /// every `i < rows`, the 21 bytes starting at
315+ /// `(sy + i - 2) * stride + sx - 2` are in bounds (`sx, sy >= 2`).
316+ #[target_feature(enable = "avx2")]
317+ pub(super) unsafe fn sixtap_h(
318+ src: &[u8],
319+ stride: usize,
320+ sx: usize,
321+ sy: usize,
322+ hf: &[i32; 6],
323+ tmp: &mut [u8; 21 * 16],
324+ rows: usize,
325+ ) {
326+ debug_assert!(sx >= 2 && sy >= 2);
327+ debug_assert!((sy + rows - 2) * stride + sx + 19 <= src.len());
328+ let tv = [
329+ _mm256_set1_epi32(tap_pair(hf[0], hf[1])),
330+ _mm256_set1_epi32(tap_pair(hf[2], hf[3])),
331+ _mm256_set1_epi32(tap_pair(hf[4], hf[5])),
332+ ];
333+ let rnd = _mm256_set1_epi32(VP8_FILTER_WEIGHT >> 1);
334+ for i in 0..rows {
335+ let sp = src.as_ptr().add((sy + i - 2) * stride + sx - 2);
336+ // `cvtepu8_epi16` of the 16-byte window at byte offset `2k`
337+ // puts each even output's tap pair in one `madd` lane pair:
338+ // `e` accumulates outputs 0,2,..,14, `o` (window +1 byte)
339+ // outputs 1,3,..,15.
340+ let mut e = _mm256_setzero_si256();
341+ let mut o = _mm256_setzero_si256();
342+ for k in 0..3 {
343+ let se = _mm256_cvtepu8_epi16(_mm_loadu_si128(sp.add(2 * k).cast()));
344+ e = _mm256_add_epi32(e, _mm256_madd_epi16(se, tv[k]));
345+ let so = _mm256_cvtepu8_epi16(_mm_loadu_si128(sp.add(2 * k + 1).cast()));
346+ o = _mm256_add_epi32(o, _mm256_madd_epi16(so, tv[k]));
347+ }
348+ e = _mm256_srai_epi32(_mm256_add_epi32(e, rnd), VP8_FILTER_SHIFT);
349+ o = _mm256_srai_epi32(_mm256_add_epi32(o, rnd), VP8_FILTER_SHIFT);
350+ // `unpacklo/hi` + `packs` interleave even/odd outputs back in
351+ // order (0..8 in the low 128-bit half, 8..16 in the high
352+ // half), then `packus` narrows both halves in sequence -- its
353+ // `u8` saturation is filter.c's `clamp_pixel` itself.
354+ let lo = _mm256_unpacklo_epi32(e, o);
355+ let hi = _mm256_unpackhi_epi32(e, o);
356+ let i16 = _mm256_packs_epi32(lo, hi);
357+ let u8 = _mm_packus_epi16(
358+ _mm256_castsi256_si128(i16),
359+ _mm256_extracti128_si256::<1>(i16),
360+ );
361+ _mm_storeu_si128(tmp.as_mut_ptr().add(i * 16).cast(), u8);
362+ }
363+ }
364+
365+ /// Six-tap second pass: `bh` output rows of `bw` (4/8/16) samples from
366+ /// the 16-lane-strided intermediate.
367+ ///
368+ /// # Safety
369+ /// Requires AVX2. `dst` must be writable for `bh` rows of `bw` bytes
370+ /// at `dst_stride`; `bw` is 4, 8 or 16.
371+ #[target_feature(enable = "avx2")]
372+ pub(super) unsafe fn sixtap_v(
373+ tmp: &[u8; 21 * 16],
374+ vf: &[i32; 6],
375+ dst: &mut [u8],
376+ dst_stride: usize,
377+ bw: usize,
378+ bh: usize,
379+ ) {
380+ debug_assert!(matches!(bw, 4 | 8 | 16));
381+ debug_assert!(dst.len() >= (bh - 1) * dst_stride + bw);
382+ let tv = [
383+ _mm256_set1_epi32(tap_pair(vf[0], vf[1])),
384+ _mm256_set1_epi32(tap_pair(vf[2], vf[3])),
385+ _mm256_set1_epi32(tap_pair(vf[4], vf[5])),
386+ ];
387+ let rnd = _mm256_set1_epi32(VP8_FILTER_WEIGHT >> 1);
388+ for r in 0..bh {
389+ let ip = tmp.as_ptr().add(r * 16);
390+ // Rows `r + 2k` / `r + 2k + 1` widened to samples, interleaved
391+ // by `unpack_epi16` so one `madd` covers two taps of every
392+ // output; `lo` holds even columns, `hi` odd ones (one run per
393+ // 128-bit lane).
394+ let mut lo = _mm256_setzero_si256();
395+ let mut hi = _mm256_setzero_si256();
396+ for k in 0..3 {
397+ let a = _mm256_cvtepu8_epi16(_mm_loadu_si128(ip.add(2 * k * 16).cast()));
398+ let b = _mm256_cvtepu8_epi16(_mm_loadu_si128(ip.add((2 * k + 1) * 16).cast()));
399+ lo = _mm256_add_epi32(lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(a, b), tv[k]));
400+ hi = _mm256_add_epi32(hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(a, b), tv[k]));
401+ }
402+ lo = _mm256_srai_epi32(_mm256_add_epi32(lo, rnd), VP8_FILTER_SHIFT);
403+ hi = _mm256_srai_epi32(_mm256_add_epi32(hi, rnd), VP8_FILTER_SHIFT);
404+ // `packs` reorders to outputs 0..8 | 8..16 across the two
405+ // 128-bit halves, in column order.
406+ let i16 = _mm256_packs_epi32(lo, hi);
407+ let u8 = _mm_packus_epi16(
408+ _mm256_castsi256_si128(i16),
409+ _mm256_extracti128_si256::<1>(i16),
410+ );
411+ let dp = dst.as_mut_ptr().add(r * dst_stride);
412+ match bw {
413+ 16 => _mm_storeu_si128(dp.cast(), u8),
414+ 8 => _mm_storel_epi64(dp.cast(), u8),
415+ 4 => _mm_storeu_si32(dp.cast(), u8),
416+ _ => unreachable!("bw is 4, 8 or 16"),
417+ }
418+ }
419+ }
420+
421+ /// Bilinear first pass, 8 output columns per step (`bw` is 4, 8 or
422+ /// 16; `bw = 16` runs the loop twice). The `i16` accumulator is exact:
423+ /// taps are non-negative and sum to 128, so every product and the sum
424+ /// stay `<= 255 * 128 + 64 < 2^15` -- this *is* the C's `unsigned
425+ /// short` intermediate, narrowed at a provably-in-range point.
426+ ///
427+ /// # Safety
428+ /// Requires AVX2. For every `i < rows`, the 17 bytes starting at
429+ /// `(sy + i) * stride + sx` must be in bounds of `src`.
430+ #[target_feature(enable = "avx2")]
431+ pub(super) unsafe fn bilinear_h(
432+ src: &[u8],
433+ stride: usize,
434+ sx: usize,
435+ sy: usize,
436+ hf: &[i32; 2],
437+ tmp: &mut [u8; 17 * 16],
438+ rows: usize,
439+ ) {
440+ debug_assert!((sy + rows - 1) * stride + sx + 16 < src.len());
441+ let f0 = _mm_set1_epi16(hf[0] as i16);
442+ let f1 = _mm_set1_epi16(hf[1] as i16);
443+ let rnd = _mm_set1_epi16((VP8_FILTER_WEIGHT / 2) as i16);
444+ for i in 0..rows {
445+ let sp = src.as_ptr().add((sy + i) * stride + sx);
446+ for c in (0..16).step_by(8) {
447+ // Lane j pairs the samples `j` and `j + 1` (tap `[0]` on
448+ // the window at `c`, tap `[1]` shifted by one byte).
449+ let a = _mm_cvtepu8_epi16(_mm_loadu_si128(sp.add(c).cast()));
450+ let b = _mm_cvtepu8_epi16(_mm_loadu_si128(sp.add(c + 1).cast()));
451+ let acc = _mm_add_epi16(
452+ _mm_add_epi16(_mm_mullo_epi16(a, f0), _mm_mullo_epi16(b, f1)),
453+ rnd,
454+ );
455+ let u8 = _mm_packus_epi16(_mm_srai_epi16::<VP8_FILTER_SHIFT>(acc), acc);
456+ _mm_storel_epi64(tmp.as_mut_ptr().add(i * 16 + c).cast(), u8);
457+ }
458+ }
459+ }
460+
461+ /// Bilinear second pass: rows `r` / `r + 1` of the intermediate, same
462+ /// exact-`i16` shape as [`bilinear_h`].
463+ ///
464+ /// # Safety
465+ /// Requires AVX2. `dst` must be writable for `bh` rows of `bw` bytes
466+ /// at `dst_stride`; `bw` is 4, 8 or 16.
467+ #[target_feature(enable = "avx2")]
468+ pub(super) unsafe fn bilinear_v(
469+ tmp: &[u8; 17 * 16],
470+ vf: &[i32; 2],
471+ dst: &mut [u8],
472+ dst_stride: usize,
473+ bw: usize,
474+ bh: usize,
475+ ) {
476+ debug_assert!(matches!(bw, 4 | 8 | 16));
477+ debug_assert!(dst.len() >= (bh - 1) * dst_stride + bw);
478+ let f0 = _mm_set1_epi16(vf[0] as i16);
479+ let f1 = _mm_set1_epi16(vf[1] as i16);
480+ let rnd = _mm_set1_epi16((VP8_FILTER_WEIGHT / 2) as i16);
481+ for r in 0..bh {
482+ let ip = tmp.as_ptr().add(r * 16);
483+ let dp = dst.as_mut_ptr().add(r * dst_stride);
484+ for c in (0..bw).step_by(8) {
485+ let a = _mm_cvtepu8_epi16(_mm_loadl_epi64(ip.add(c).cast()));
486+ let b = _mm_cvtepu8_epi16(_mm_loadl_epi64(ip.add(16 + c).cast()));
487+ let acc = _mm_add_epi16(
488+ _mm_add_epi16(_mm_mullo_epi16(a, f0), _mm_mullo_epi16(b, f1)),
489+ rnd,
490+ );
491+ let u8 = _mm_packus_epi16(_mm_srai_epi16::<VP8_FILTER_SHIFT>(acc), acc);
492+ match bw - c {
493+ 8 | 16 => _mm_storel_epi64(dp.add(c).cast(), u8),
494+ _ => _mm_storeu_si32(dp.add(c).cast(), u8),
495+ }
496+ }
497+ }
498+ }
499+}
500+
217 /// Two-tap bilinear separable predictor (libvpx vp8_bilinear_predict*,501 /// Two-tap bilinear separable predictor (libvpx vp8_bilinear_predict*,
218 /// filter.c). The dot products are provably in 0..=255 (taps are502 /// filter.c). The dot products are provably in 0..=255 (taps are
219 /// non-negative and sum to 128 over 0..=255 inputs), which is why libvpx's503 /// non-negative and sum to 128 over 0..=255 inputs), which is why libvpx's
220-/// bilinear passes store `unsigned short` intermediates and skip the clamp.504+/// bilinear passes store `unsigned short` intermediates and skip the
505+/// clamp: the intermediate is stored as `u8` at a fixed 16-lane stride
506+/// (representation-only change, same argument as [`sixtap_block`]).
221 fn bilinear_block(507 fn bilinear_block(
222 src: &[u8],508 src: &[u8],
223 stride: usize,509 stride: usize,
@@ -240,26 +526,66 @@ fn bilinear_block(
240 526
241 let hf = &BILINEAR_FILTERS[fx];527 let hf = &BILINEAR_FILTERS[fx];
242 let vf = &BILINEAR_FILTERS[fy];528 let vf = &BILINEAR_FILTERS[fy];
529+ let rows = bh + 1;
530+ let mut tmp = [0u8; 17 * 16]; // C: unsigned short FData[17 * 16], <= 255
531+
532+ #[cfg(target_arch = "x86_64")]
533+ {
534+ if avx2_supported() {
535+ // SAFETY: AVX2 checked by `avx2_supported`; border/bounds
536+ // contracts hold for every caller (see [`sixtap_block`]).
537+ #[allow(unsafe_code)] // SAFETY: avx2_supported() checked above
538+ unsafe {
539+ simd::bilinear_h(src, stride, sx, sy, hf, &mut tmp, rows);
540+ simd::bilinear_v(&tmp, vf, dst, dst_stride, bw, bh);
541+ }
542+ return;
543+ }
544+ }
545+
546+ bilinear_h_scalar(src, stride, sx, sy, hf, &mut tmp, bw, rows);
547+ bilinear_v_scalar(&tmp, vf, dst, dst_stride, bw, bh);
548+}
243 549
244- // filter.c filter_block2d_bil_first_pass: `bh + 1` rows, horizontal550+/// Scalar first pass of [`bilinear_block`] (filter.c
245- // taps [0] and [1]. Phase 0 is the exact identity, as above.551+/// `filter_block2d_bil_first_pass`): `bh + 1` rows, taps `[0]` and `[1]`.
246- let mut tmp = [0u16; 17 * 16]; // C: unsigned short FData[17 * 16]552+/// Phase 0 is the exact identity, as in [`sixtap_h_scalar`].
247- for i in 0..bh + 1 {553+fn bilinear_h_scalar(
554+ src: &[u8],
555+ stride: usize,
556+ sx: usize,
557+ sy: usize,
558+ hf: &[i32; 2],
559+ tmp: &mut [u8; 17 * 16],
560+ bw: usize,
561+ rows: usize,
562+) {
563+ for i in 0..rows {
248 let row = (sy + i) * stride + sx;564 let row = (sy + i) * stride + sx;
249 for j in 0..bw {565 for j in 0..bw {
250 let p = row + j;566 let p = row + j;
251 let acc = i32::from(src[p]) * hf[0] + i32::from(src[p + 1]) * hf[1]567 let acc = i32::from(src[p]) * hf[0] + i32::from(src[p + 1]) * hf[1]
252 + (VP8_FILTER_WEIGHT / 2); /* Rounding */568 + (VP8_FILTER_WEIGHT / 2); /* Rounding */
253- tmp[i * bw + j] = (acc >> VP8_FILTER_SHIFT) as u16;569+ tmp[i * 16 + j] = (acc >> VP8_FILTER_SHIFT) as u8;
570+ }
254 }571 }
255 }572 }
256 573
257- // filter.c filter_block2d_bil_second_pass: vertical taps [0] and574+/// Scalar second pass of [`bilinear_block`] (filter.c
258- // `[width]` (the next intermediate row).575+/// `filter_block2d_bil_second_pass`): taps `[0]` and `[1]` read rows
576+/// `r` / `r + 1` of the intermediate at the fixed 16-lane stride.
577+fn bilinear_v_scalar(
578+ tmp: &[u8; 17 * 16],
579+ vf: &[i32; 2],
580+ dst: &mut [u8],
581+ dst_stride: usize,
582+ bw: usize,
583+ bh: usize,
584+) {
259 for r in 0..bh {585 for r in 0..bh {
260 for c in 0..bw {586 for c in 0..bw {
261- let acc = i32::from(tmp[r * bw + c]) * vf[0]587+ let acc = i32::from(tmp[r * 16 + c]) * vf[0]
262- + i32::from(tmp[(r + 1) * bw + c]) * vf[1]588+ + i32::from(tmp[(r + 1) * 16 + c]) * vf[1]
263 + (VP8_FILTER_WEIGHT / 2); /* Rounding */589 + (VP8_FILTER_WEIGHT / 2); /* Rounding */
264 dst[r * dst_stride + c] = (acc >> VP8_FILTER_SHIFT) as u8;590 dst[r * dst_stride + c] = (acc >> VP8_FILTER_SHIFT) as u8;
265 }591 }
@@ -760,4 +1086,124 @@ mod tests {
760 );1086 );
761 assert_eq!(a, b);1087 assert_eq!(a, b);
762 }1088 }
1089+
1090+ /// Fills a bordered plane with a 0x00/0xFF checkerboard: maximizes the
1091+ /// six-tap dot products' excursions in both directions (negative taps
1092+ /// over 0xFF while positive taps sit on 0x00 and vice versa), so the
1093+ /// clamp/saturation paths of both implementations are exercised.
1094+ fn checkerboard_plane() -> Vec<u8> {
1095+ let mut out = vec![0u8; STRIDE * ROWS];
1096+ for r in 0..ROWS {
1097+ for c in 0..STRIDE {
1098+ out[r * STRIDE + c] = if (r + c) & 1 == 0 { 0xFF } else { 0x00 };
1099+ }
1100+ }
1101+ out
1102+ }
1103+
1104+ /// The AVX2 kernels must be lane-for-lane identical to the scalar
1105+ /// reference (the libvpx transcription) on every block shape, every
1106+ /// phase pair, at both a central and a right/bottom-edge origin where
1107+ /// the wide loads reach furthest into the border.
1108+ #[test]
1109+ #[cfg(target_arch = "x86_64")]
1110+ fn simd_sixtap_matches_the_scalar_reference_everywhere() {
1111+ if !avx2_supported() {
1112+ return;
1113+ }
1114+ let planes = [plane(), checkerboard_plane()];
1115+ let origins = [
1116+ (BORDER + 2, BORDER + 3),
1117+ (BORDER + VIS_W - 16, BORDER + VIS_H - 16),
1118+ ];
1119+ for p in &planes {
1120+ for &(sx, sy) in &origins {
1121+ for bw in [4usize, 8, 16] {
1122+ for bh in [4usize, 8, 16] {
1123+ for fx in 0..8usize {
1124+ for fy in 0..8usize {
1125+ if fx == 0 && fy == 0 {
1126+ continue; // whole-pel copy, shared path
1127+ }
1128+ let mut want = [0u8; 16 * 16];
1129+ let mut got = [0u8; 16 * 16];
1130+ let mut tmp = [0u8; 21 * 16];
1131+ sixtap_h_scalar(
1132+ p, STRIDE, sx, sy, &SUB_PEL_FILTERS[fx], &mut tmp, bw, bh + 5,
1133+ );
1134+ sixtap_v_scalar(&tmp, &SUB_PEL_FILTERS[fy], &mut want, 16, bw, bh);
1135+ // SAFETY: avx2_supported() checked above.
1136+ #[allow(unsafe_code)]
1137+ unsafe {
1138+ let mut tmp2 = [0u8; 21 * 16];
1139+ simd::sixtap_h(
1140+ p, STRIDE, sx, sy, &SUB_PEL_FILTERS[fx], &mut tmp2, bh + 5,
1141+ );
1142+ simd::sixtap_v(&tmp2, &SUB_PEL_FILTERS[fy], &mut got, 16, bw, bh);
1143+ }
1144+ let n = bh * 16;
1145+ assert_eq!(
1146+ &got[..n],
1147+ &want[..n],
1148+ "sixtap bw={bw} bh={bh} fx={fx} fy={fy} origin=({sx},{sy})"
1149+ );
1150+ }
1151+ }
1152+ }
1153+ }
1154+ }
1155+ }
1156+ }
1157+
1158+ /// Same gate for the bilinear kernels.
1159+ #[test]
1160+ #[cfg(target_arch = "x86_64")]
1161+ fn simd_bilinear_matches_the_scalar_reference_everywhere() {
1162+ if !avx2_supported() {
1163+ return;
1164+ }
1165+ let planes = [plane(), checkerboard_plane()];
1166+ let origins = [
1167+ (BORDER + 2, BORDER + 3),
1168+ (BORDER + VIS_W - 16, BORDER + VIS_H - 16),
1169+ ];
1170+ for p in &planes {
1171+ for &(sx, sy) in &origins {
1172+ for bw in [4usize, 8, 16] {
1173+ for bh in [4usize, 8, 16] {
1174+ for fx in 0..8usize {
1175+ for fy in 0..8usize {
1176+ if fx == 0 && fy == 0 {
1177+ continue;
1178+ }
1179+ let mut want = [0u8; 16 * 16];
1180+ let mut got = [0u8; 16 * 16];
1181+ let mut tmp = [0u8; 17 * 16];
1182+ bilinear_h_scalar(
1183+ p, STRIDE, sx, sy, &BILINEAR_FILTERS[fx], &mut tmp, bw, bh + 1,
1184+ );
1185+ bilinear_v_scalar(&tmp, &BILINEAR_FILTERS[fy], &mut want, 16, bw, bh);
1186+ // SAFETY: avx2_supported() checked above.
1187+ #[allow(unsafe_code)]
1188+ unsafe {
1189+ let mut tmp2 = [0u8; 17 * 16];
1190+ simd::bilinear_h(
1191+ p, STRIDE, sx, sy, &BILINEAR_FILTERS[fx], &mut tmp2, bh + 1,
1192+ );
1193+ simd::bilinear_v(&tmp2, &BILINEAR_FILTERS[fy], &mut got, 16, bw, bh);
1194+ }
1195+ for r in 0..bh {
1196+ assert_eq!(
1197+ got[r * 16..r * 16 + bw],
1198+ want[r * 16..r * 16 + bw],
1199+ "bilinear bw={bw} bh={bh} fx={fx} fy={fy} origin=({sx},{sy}) row={r}"
1200+ );
1201+ }
1202+ }
1203+ }
1204+ }
1205+ }
1206+ }
1207+ }
1208+ }
763 }1209 }