From c4fe59653e7e5e5ef6d1b39fa3bc923a097b65ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 12 Sep 2026 23:46:53 +0300 Subject: [PATCH] ec-vp8 perf: MC kernels -- AVX2 six-tap + bilinear passes 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.rs | 16 +- crates/ec-vp8/src/mc.rs | 496 ++++++++++++++++++++++++++++++-- 2 files changed, 484 insertions(+), 28 deletions(-) diff --git a/crates/ec-vp8/src/loopfilter.rs b/crates/ec-vp8/src/loopfilter.rs index 8ae4c396..f716fb20 100644 --- a/crates/ec-vp8/src/loopfilter.rs +++ b/crates/ec-vp8/src/loopfilter.rs @@ -493,8 +493,12 @@ fn filter_mb_h_edge( // per-column). let n = 8 * size; let mut px = [[0u8; 64]; 8]; + debug_assert!(anchor >= 4 * stride && anchor + 3 * stride + n <= plane.len()); for (k, r) in px.iter_mut().enumerate() { - let a = anchor - (4 - k) * stride; + // Add the row offset before backing up 4 rows: `anchor` is only + // guaranteed to sit >= 4 rows into the plane, and the subtracted + // form `anchor - (4 - k) * stride` underflows for k > 4. + let a = anchor + k * stride - 4 * stride; r[..n].copy_from_slice(&plane[a..a + n]); } for i in 0..n { @@ -542,8 +546,11 @@ fn filter_subblock_h_edge( ) { let n = 8 * size; let mut px = [[0u8; 64]; 8]; + debug_assert!(anchor >= 4 * stride && anchor + 3 * stride + n <= plane.len()); for (k, r) in px.iter_mut().enumerate() { - let a = anchor - (4 - k) * stride; + // Same add-first form as [`filter_mb_h_edge`]: the subtracted + // expression underflows for k > 4. + let a = anchor + k * stride - 4 * stride; r[..n].copy_from_slice(&plane[a..a + n]); } for i in 0..n { @@ -591,8 +598,11 @@ fn filter_v_edge_simple(plane: &mut [u8], stride: usize, anchor: usize, limit: i fn filter_h_edge_simple(plane: &mut [u8], stride: usize, anchor: usize, limit: i32) { // Contiguous columns, strided taps: chunked loads, conditional stores. let mut px = [[0u8; 16]; 4]; + debug_assert!(anchor >= 2 * stride && anchor + stride + 16 <= plane.len()); for (k, r) in px.iter_mut().enumerate() { - let a = anchor - (2 - k) * stride; + // Add-first form (see [`filter_mb_h_edge`]): the subtracted + // expression underflows for k > 2. + let a = anchor + k * stride - 2 * stride; r.copy_from_slice(&plane[a..a + 16]); } for i in 0..16 { diff --git a/crates/ec-vp8/src/mc.rs b/crates/ec-vp8/src/mc.rs index 01d64181..298455cf 100644 --- a/crates/ec-vp8/src/mc.rs +++ b/crates/ec-vp8/src/mc.rs @@ -154,6 +154,13 @@ pub(crate) fn predict_luma( /// Six-tap separable sub-pel predictor (libvpx vp8_sixtap_predict*, /// filter.c). `(sx, sy)` is the full-pel block origin in the bordered /// reference, `(fx, fy)` the sub-pel phases 0..7 (0 = copy). +/// +/// Both passes share one clamped intermediate buffer: the first pass is +/// clamped to 0..=255 per pass (libvpx `clamp_pixel`), so it is stored as +/// `u8` at a fixed 16-lane stride. The AVX2 kernels always compute all 16 +/// lanes of an intermediate row (reads stay inside the 32-px border for +/// every block shape) and the vertical pass discards the tail lanes with +/// its `bw`-byte store; the scalar reference only fills the first `bw`. fn sixtap_block( src: &[u8], stride: usize, @@ -177,13 +184,44 @@ fn sixtap_block( let hf = &SUB_PEL_FILTERS[fx]; let vf = &SUB_PEL_FILTERS[fy]; + let rows = bh + 5; + let mut tmp = [0u8; 21 * 16]; // C: int FData[21 * 24], clamped to u8 + + #[cfg(target_arch = "x86_64")] + { + if avx2_supported() { + // SAFETY: AVX2 availability checked by `avx2_supported` just + // above; the border/bounds contracts of both kernels hold for + // every caller (`bw`/`bh` in 4..=16, bordered source plane). + #[allow(unsafe_code)] + unsafe { + simd::sixtap_h(src, stride, sx, sy, hf, &mut tmp, rows); + simd::sixtap_v(&tmp, vf, dst, dst_stride, bw, bh); + } + return; + } + } - // filter.c filter_block2d_first_pass on `src - 2*stride`: `bh + 5` - // horizontal lines (2 above the block, 3 below, for the vertical taps). - // Phase 0 is the exact identity (`(p * 128 + 64) >> 7 == p`), so the - // uniform loop reproduces the C for every phase combination. - let mut tmp = [0i32; 21 * 16]; // C: int FData[21 * 24] - for i in 0..bh + 5 { + sixtap_h_scalar(src, stride, sx, sy, hf, &mut tmp, bw, rows); + sixtap_v_scalar(&tmp, vf, dst, dst_stride, bw, bh); +} + +/// Scalar first pass of [`sixtap_block`] (filter.c +/// `filter_block2d_first_pass` on `src - 2*stride`): `rows` = `bh + 5` +/// horizontal lines, each sample `(dot + 64) >> 7` clamped to 0..=255. +/// Phase 0 is the exact identity (`(p * 128 + 64) >> 7 == p`), so the +/// uniform loop reproduces the C for every phase combination. +fn sixtap_h_scalar( + src: &[u8], + stride: usize, + sx: usize, + sy: usize, + hf: &[i32; 6], + tmp: &mut [u8; 21 * 16], + bw: usize, + rows: usize, +) { + for i in 0..rows { let row = (sy + i - 2) * stride + sx - 2; for j in 0..bw { let p = row + j; @@ -194,30 +232,278 @@ fn sixtap_block( + i32::from(src[p + 4]) * hf[4] + i32::from(src[p + 5]) * hf[5] + (VP8_FILTER_WEIGHT >> 1); /* Rounding */ - tmp[i * bw + j] = clamp255(acc >> VP8_FILTER_SHIFT); + tmp[i * 16 + j] = clamp255(acc >> VP8_FILTER_SHIFT) as u8; } } +} - // filter.c filter_block2d_second_pass on `FData + 2*bw`: taps read the - // intermediate rows `r .. r + 5`. +/// Scalar second pass of [`sixtap_block`] (filter.c +/// `filter_block2d_second_pass` on `FData + 2*bw`): taps read the +/// intermediate rows `r .. r + 5` at the fixed 16-lane stride. +fn sixtap_v_scalar( + tmp: &[u8; 21 * 16], + vf: &[i32; 6], + dst: &mut [u8], + dst_stride: usize, + bw: usize, + bh: usize, +) { for r in 0..bh { for c in 0..bw { - let acc = tmp[r * bw + c] * vf[0] - + tmp[(r + 1) * bw + c] * vf[1] - + tmp[(r + 2) * bw + c] * vf[2] - + tmp[(r + 3) * bw + c] * vf[3] - + tmp[(r + 4) * bw + c] * vf[4] - + tmp[(r + 5) * bw + c] * vf[5] + let acc = i32::from(tmp[r * 16 + c]) * vf[0] + + i32::from(tmp[(r + 1) * 16 + c]) * vf[1] + + i32::from(tmp[(r + 2) * 16 + c]) * vf[2] + + i32::from(tmp[(r + 3) * 16 + c]) * vf[3] + + i32::from(tmp[(r + 4) * 16 + c]) * vf[4] + + i32::from(tmp[(r + 5) * 16 + c]) * vf[5] + (VP8_FILTER_WEIGHT >> 1); /* Rounding */ dst[r * dst_stride + c] = clamp255(acc >> VP8_FILTER_SHIFT) as u8; } } } +/// Cached AVX2 availability (the per-block detection load is cheap, but +/// MC blocks are the hottest call in an inter decode). +#[cfg(target_arch = "x86_64")] +fn avx2_supported() -> bool { + static AVX2: std::sync::LazyLock = + std::sync::LazyLock::new(|| std::arch::is_x86_feature_detected!("avx2")); + *AVX2 +} + +/// Explicit `std::arch::x86_64` kernels for the six-tap and bilinear +/// passes (the 18% of a decode the profile pins on `sixtap_block` and its +/// chroma siblings). Nothing here is spec logic: each kernel is the scalar +/// function's arithmetic lane-parallel, with the same rounding, the same +/// truncation points and the same clamps -- the equivalence tests run +/// every kernel against its scalar reference over all shapes, phases and +/// worst-case input bytes. +/// +/// Exactness notes shared by the six-tap kernels: +/// - `_mm*_madd_epi16` multiplies *pairs of samples* by a packed tap pair +/// and accumulates in `i32`; every product is exact (widened samples +/// are 0..=255, `|byte * tap| <= 255 * 128 < 2^15`) and the six-product +/// sum is exact in `i32` (`|dot + 64| <= 255 * 160 + 64 < 2^23`). +/// - The even/odd column split exists because `madd` pairs *adjacent* +/// samples: the even accumulator's `madd` lane m is output `2m`, the +/// odd one output `2m+1` (load offset +1 shifts the sample window). +/// - `packs_epi32`'s `i32 -> i16` saturation can never fire: after +/// `(dot + 64) >> 7` the range is `[-76, 319]`. +/// - `packus_epi16`'s `u8` saturation *is* filter.c's `clamp_pixel`: +/// values below 0 saturate to 0, above 255 to 255, the rest unchanged. +#[cfg(target_arch = "x86_64")] +#[allow(unsafe_code)] +#[allow(unsafe_op_in_unsafe_fn)] // each kernel's whole body is its contract +mod simd { + use super::{VP8_FILTER_SHIFT, VP8_FILTER_WEIGHT}; + use std::arch::x86_64::*; + + /// Tap pair `[t0, t1]` packed into one `i32`, the operand shape + /// `_mm*_madd_epi16` wants: lane `j` of `madd(samples, set1(pair))` + /// is `s[2j] * t0 + s[2j+1] * t1` (ec-av1 `mc::simd::tap_pairs`). + #[inline] + fn tap_pair(a: i32, b: i32) -> i32 { + (b << 16) | (a as u16 as i32) + } + + /// Six-tap first pass, all 16 lanes of every intermediate row (see + /// [`sixtap_block`] for the layout). + /// + /// # Safety + /// Requires AVX2. `src` must be a bordered MC plane such that for + /// every `i < rows`, the 21 bytes starting at + /// `(sy + i - 2) * stride + sx - 2` are in bounds (`sx, sy >= 2`). + #[target_feature(enable = "avx2")] + pub(super) unsafe fn sixtap_h( + src: &[u8], + stride: usize, + sx: usize, + sy: usize, + hf: &[i32; 6], + tmp: &mut [u8; 21 * 16], + rows: usize, + ) { + debug_assert!(sx >= 2 && sy >= 2); + debug_assert!((sy + rows - 2) * stride + sx + 19 <= src.len()); + let tv = [ + _mm256_set1_epi32(tap_pair(hf[0], hf[1])), + _mm256_set1_epi32(tap_pair(hf[2], hf[3])), + _mm256_set1_epi32(tap_pair(hf[4], hf[5])), + ]; + let rnd = _mm256_set1_epi32(VP8_FILTER_WEIGHT >> 1); + for i in 0..rows { + let sp = src.as_ptr().add((sy + i - 2) * stride + sx - 2); + // `cvtepu8_epi16` of the 16-byte window at byte offset `2k` + // puts each even output's tap pair in one `madd` lane pair: + // `e` accumulates outputs 0,2,..,14, `o` (window +1 byte) + // outputs 1,3,..,15. + let mut e = _mm256_setzero_si256(); + let mut o = _mm256_setzero_si256(); + for k in 0..3 { + let se = _mm256_cvtepu8_epi16(_mm_loadu_si128(sp.add(2 * k).cast())); + e = _mm256_add_epi32(e, _mm256_madd_epi16(se, tv[k])); + let so = _mm256_cvtepu8_epi16(_mm_loadu_si128(sp.add(2 * k + 1).cast())); + o = _mm256_add_epi32(o, _mm256_madd_epi16(so, tv[k])); + } + e = _mm256_srai_epi32(_mm256_add_epi32(e, rnd), VP8_FILTER_SHIFT); + o = _mm256_srai_epi32(_mm256_add_epi32(o, rnd), VP8_FILTER_SHIFT); + // `unpacklo/hi` + `packs` interleave even/odd outputs back in + // order (0..8 in the low 128-bit half, 8..16 in the high + // half), then `packus` narrows both halves in sequence -- its + // `u8` saturation is filter.c's `clamp_pixel` itself. + let lo = _mm256_unpacklo_epi32(e, o); + let hi = _mm256_unpackhi_epi32(e, o); + let i16 = _mm256_packs_epi32(lo, hi); + let u8 = _mm_packus_epi16( + _mm256_castsi256_si128(i16), + _mm256_extracti128_si256::<1>(i16), + ); + _mm_storeu_si128(tmp.as_mut_ptr().add(i * 16).cast(), u8); + } + } + + /// Six-tap second pass: `bh` output rows of `bw` (4/8/16) samples from + /// the 16-lane-strided intermediate. + /// + /// # Safety + /// Requires AVX2. `dst` must be writable for `bh` rows of `bw` bytes + /// at `dst_stride`; `bw` is 4, 8 or 16. + #[target_feature(enable = "avx2")] + pub(super) unsafe fn sixtap_v( + tmp: &[u8; 21 * 16], + vf: &[i32; 6], + dst: &mut [u8], + dst_stride: usize, + bw: usize, + bh: usize, + ) { + debug_assert!(matches!(bw, 4 | 8 | 16)); + debug_assert!(dst.len() >= (bh - 1) * dst_stride + bw); + let tv = [ + _mm256_set1_epi32(tap_pair(vf[0], vf[1])), + _mm256_set1_epi32(tap_pair(vf[2], vf[3])), + _mm256_set1_epi32(tap_pair(vf[4], vf[5])), + ]; + let rnd = _mm256_set1_epi32(VP8_FILTER_WEIGHT >> 1); + for r in 0..bh { + let ip = tmp.as_ptr().add(r * 16); + // Rows `r + 2k` / `r + 2k + 1` widened to samples, interleaved + // by `unpack_epi16` so one `madd` covers two taps of every + // output; `lo` holds even columns, `hi` odd ones (one run per + // 128-bit lane). + let mut lo = _mm256_setzero_si256(); + let mut hi = _mm256_setzero_si256(); + for k in 0..3 { + let a = _mm256_cvtepu8_epi16(_mm_loadu_si128(ip.add(2 * k * 16).cast())); + let b = _mm256_cvtepu8_epi16(_mm_loadu_si128(ip.add((2 * k + 1) * 16).cast())); + lo = _mm256_add_epi32(lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(a, b), tv[k])); + hi = _mm256_add_epi32(hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(a, b), tv[k])); + } + lo = _mm256_srai_epi32(_mm256_add_epi32(lo, rnd), VP8_FILTER_SHIFT); + hi = _mm256_srai_epi32(_mm256_add_epi32(hi, rnd), VP8_FILTER_SHIFT); + // `packs` reorders to outputs 0..8 | 8..16 across the two + // 128-bit halves, in column order. + let i16 = _mm256_packs_epi32(lo, hi); + let u8 = _mm_packus_epi16( + _mm256_castsi256_si128(i16), + _mm256_extracti128_si256::<1>(i16), + ); + let dp = dst.as_mut_ptr().add(r * dst_stride); + match bw { + 16 => _mm_storeu_si128(dp.cast(), u8), + 8 => _mm_storel_epi64(dp.cast(), u8), + 4 => _mm_storeu_si32(dp.cast(), u8), + _ => unreachable!("bw is 4, 8 or 16"), + } + } + } + + /// Bilinear first pass, 8 output columns per step (`bw` is 4, 8 or + /// 16; `bw = 16` runs the loop twice). The `i16` accumulator is exact: + /// taps are non-negative and sum to 128, so every product and the sum + /// stay `<= 255 * 128 + 64 < 2^15` -- this *is* the C's `unsigned + /// short` intermediate, narrowed at a provably-in-range point. + /// + /// # Safety + /// Requires AVX2. For every `i < rows`, the 17 bytes starting at + /// `(sy + i) * stride + sx` must be in bounds of `src`. + #[target_feature(enable = "avx2")] + pub(super) unsafe fn bilinear_h( + src: &[u8], + stride: usize, + sx: usize, + sy: usize, + hf: &[i32; 2], + tmp: &mut [u8; 17 * 16], + rows: usize, + ) { + debug_assert!((sy + rows - 1) * stride + sx + 16 < src.len()); + let f0 = _mm_set1_epi16(hf[0] as i16); + let f1 = _mm_set1_epi16(hf[1] as i16); + let rnd = _mm_set1_epi16((VP8_FILTER_WEIGHT / 2) as i16); + for i in 0..rows { + let sp = src.as_ptr().add((sy + i) * stride + sx); + for c in (0..16).step_by(8) { + // Lane j pairs the samples `j` and `j + 1` (tap `[0]` on + // the window at `c`, tap `[1]` shifted by one byte). + let a = _mm_cvtepu8_epi16(_mm_loadu_si128(sp.add(c).cast())); + let b = _mm_cvtepu8_epi16(_mm_loadu_si128(sp.add(c + 1).cast())); + let acc = _mm_add_epi16( + _mm_add_epi16(_mm_mullo_epi16(a, f0), _mm_mullo_epi16(b, f1)), + rnd, + ); + let u8 = _mm_packus_epi16(_mm_srai_epi16::(acc), acc); + _mm_storel_epi64(tmp.as_mut_ptr().add(i * 16 + c).cast(), u8); + } + } + } + + /// Bilinear second pass: rows `r` / `r + 1` of the intermediate, same + /// exact-`i16` shape as [`bilinear_h`]. + /// + /// # Safety + /// Requires AVX2. `dst` must be writable for `bh` rows of `bw` bytes + /// at `dst_stride`; `bw` is 4, 8 or 16. + #[target_feature(enable = "avx2")] + pub(super) unsafe fn bilinear_v( + tmp: &[u8; 17 * 16], + vf: &[i32; 2], + dst: &mut [u8], + dst_stride: usize, + bw: usize, + bh: usize, + ) { + debug_assert!(matches!(bw, 4 | 8 | 16)); + debug_assert!(dst.len() >= (bh - 1) * dst_stride + bw); + let f0 = _mm_set1_epi16(vf[0] as i16); + let f1 = _mm_set1_epi16(vf[1] as i16); + let rnd = _mm_set1_epi16((VP8_FILTER_WEIGHT / 2) as i16); + for r in 0..bh { + let ip = tmp.as_ptr().add(r * 16); + let dp = dst.as_mut_ptr().add(r * dst_stride); + for c in (0..bw).step_by(8) { + let a = _mm_cvtepu8_epi16(_mm_loadl_epi64(ip.add(c).cast())); + let b = _mm_cvtepu8_epi16(_mm_loadl_epi64(ip.add(16 + c).cast())); + let acc = _mm_add_epi16( + _mm_add_epi16(_mm_mullo_epi16(a, f0), _mm_mullo_epi16(b, f1)), + rnd, + ); + let u8 = _mm_packus_epi16(_mm_srai_epi16::(acc), acc); + match bw - c { + 8 | 16 => _mm_storel_epi64(dp.add(c).cast(), u8), + _ => _mm_storeu_si32(dp.add(c).cast(), u8), + } + } + } + } +} + /// Two-tap bilinear separable predictor (libvpx vp8_bilinear_predict*, /// filter.c). The dot products are provably in 0..=255 (taps are /// non-negative and sum to 128 over 0..=255 inputs), which is why libvpx's -/// bilinear passes store `unsigned short` intermediates and skip the clamp. +/// bilinear passes store `unsigned short` intermediates and skip the +/// clamp: the intermediate is stored as `u8` at a fixed 16-lane stride +/// (representation-only change, same argument as [`sixtap_block`]). fn bilinear_block( src: &[u8], stride: usize, @@ -240,26 +526,66 @@ fn bilinear_block( let hf = &BILINEAR_FILTERS[fx]; let vf = &BILINEAR_FILTERS[fy]; + let rows = bh + 1; + let mut tmp = [0u8; 17 * 16]; // C: unsigned short FData[17 * 16], <= 255 + + #[cfg(target_arch = "x86_64")] + { + if avx2_supported() { + // SAFETY: AVX2 checked by `avx2_supported`; border/bounds + // contracts hold for every caller (see [`sixtap_block`]). + #[allow(unsafe_code)] // SAFETY: avx2_supported() checked above + unsafe { + simd::bilinear_h(src, stride, sx, sy, hf, &mut tmp, rows); + simd::bilinear_v(&tmp, vf, dst, dst_stride, bw, bh); + } + return; + } + } + + bilinear_h_scalar(src, stride, sx, sy, hf, &mut tmp, bw, rows); + bilinear_v_scalar(&tmp, vf, dst, dst_stride, bw, bh); +} - // filter.c filter_block2d_bil_first_pass: `bh + 1` rows, horizontal - // taps [0] and [1]. Phase 0 is the exact identity, as above. - let mut tmp = [0u16; 17 * 16]; // C: unsigned short FData[17 * 16] - for i in 0..bh + 1 { +/// Scalar first pass of [`bilinear_block`] (filter.c +/// `filter_block2d_bil_first_pass`): `bh + 1` rows, taps `[0]` and `[1]`. +/// Phase 0 is the exact identity, as in [`sixtap_h_scalar`]. +fn bilinear_h_scalar( + src: &[u8], + stride: usize, + sx: usize, + sy: usize, + hf: &[i32; 2], + tmp: &mut [u8; 17 * 16], + bw: usize, + rows: usize, +) { + for i in 0..rows { let row = (sy + i) * stride + sx; for j in 0..bw { let p = row + j; let acc = i32::from(src[p]) * hf[0] + i32::from(src[p + 1]) * hf[1] + (VP8_FILTER_WEIGHT / 2); /* Rounding */ - tmp[i * bw + j] = (acc >> VP8_FILTER_SHIFT) as u16; + tmp[i * 16 + j] = (acc >> VP8_FILTER_SHIFT) as u8; } } +} - // filter.c filter_block2d_bil_second_pass: vertical taps [0] and - // `[width]` (the next intermediate row). +/// Scalar second pass of [`bilinear_block`] (filter.c +/// `filter_block2d_bil_second_pass`): taps `[0]` and `[1]` read rows +/// `r` / `r + 1` of the intermediate at the fixed 16-lane stride. +fn bilinear_v_scalar( + tmp: &[u8; 17 * 16], + vf: &[i32; 2], + dst: &mut [u8], + dst_stride: usize, + bw: usize, + bh: usize, +) { for r in 0..bh { for c in 0..bw { - let acc = i32::from(tmp[r * bw + c]) * vf[0] - + i32::from(tmp[(r + 1) * bw + c]) * vf[1] + let acc = i32::from(tmp[r * 16 + c]) * vf[0] + + i32::from(tmp[(r + 1) * 16 + c]) * vf[1] + (VP8_FILTER_WEIGHT / 2); /* Rounding */ dst[r * dst_stride + c] = (acc >> VP8_FILTER_SHIFT) as u8; } @@ -760,4 +1086,124 @@ mod tests { ); assert_eq!(a, b); } + + /// Fills a bordered plane with a 0x00/0xFF checkerboard: maximizes the + /// six-tap dot products' excursions in both directions (negative taps + /// over 0xFF while positive taps sit on 0x00 and vice versa), so the + /// clamp/saturation paths of both implementations are exercised. + fn checkerboard_plane() -> Vec { + let mut out = vec![0u8; STRIDE * ROWS]; + for r in 0..ROWS { + for c in 0..STRIDE { + out[r * STRIDE + c] = if (r + c) & 1 == 0 { 0xFF } else { 0x00 }; + } + } + out + } + + /// The AVX2 kernels must be lane-for-lane identical to the scalar + /// reference (the libvpx transcription) on every block shape, every + /// phase pair, at both a central and a right/bottom-edge origin where + /// the wide loads reach furthest into the border. + #[test] + #[cfg(target_arch = "x86_64")] + fn simd_sixtap_matches_the_scalar_reference_everywhere() { + if !avx2_supported() { + return; + } + let planes = [plane(), checkerboard_plane()]; + let origins = [ + (BORDER + 2, BORDER + 3), + (BORDER + VIS_W - 16, BORDER + VIS_H - 16), + ]; + for p in &planes { + for &(sx, sy) in &origins { + for bw in [4usize, 8, 16] { + for bh in [4usize, 8, 16] { + for fx in 0..8usize { + for fy in 0..8usize { + if fx == 0 && fy == 0 { + continue; // whole-pel copy, shared path + } + let mut want = [0u8; 16 * 16]; + let mut got = [0u8; 16 * 16]; + let mut tmp = [0u8; 21 * 16]; + sixtap_h_scalar( + p, STRIDE, sx, sy, &SUB_PEL_FILTERS[fx], &mut tmp, bw, bh + 5, + ); + sixtap_v_scalar(&tmp, &SUB_PEL_FILTERS[fy], &mut want, 16, bw, bh); + // SAFETY: avx2_supported() checked above. + #[allow(unsafe_code)] + unsafe { + let mut tmp2 = [0u8; 21 * 16]; + simd::sixtap_h( + p, STRIDE, sx, sy, &SUB_PEL_FILTERS[fx], &mut tmp2, bh + 5, + ); + simd::sixtap_v(&tmp2, &SUB_PEL_FILTERS[fy], &mut got, 16, bw, bh); + } + let n = bh * 16; + assert_eq!( + &got[..n], + &want[..n], + "sixtap bw={bw} bh={bh} fx={fx} fy={fy} origin=({sx},{sy})" + ); + } + } + } + } + } + } + } + + /// Same gate for the bilinear kernels. + #[test] + #[cfg(target_arch = "x86_64")] + fn simd_bilinear_matches_the_scalar_reference_everywhere() { + if !avx2_supported() { + return; + } + let planes = [plane(), checkerboard_plane()]; + let origins = [ + (BORDER + 2, BORDER + 3), + (BORDER + VIS_W - 16, BORDER + VIS_H - 16), + ]; + for p in &planes { + for &(sx, sy) in &origins { + for bw in [4usize, 8, 16] { + for bh in [4usize, 8, 16] { + for fx in 0..8usize { + for fy in 0..8usize { + if fx == 0 && fy == 0 { + continue; + } + let mut want = [0u8; 16 * 16]; + let mut got = [0u8; 16 * 16]; + let mut tmp = [0u8; 17 * 16]; + bilinear_h_scalar( + p, STRIDE, sx, sy, &BILINEAR_FILTERS[fx], &mut tmp, bw, bh + 1, + ); + bilinear_v_scalar(&tmp, &BILINEAR_FILTERS[fy], &mut want, 16, bw, bh); + // SAFETY: avx2_supported() checked above. + #[allow(unsafe_code)] + unsafe { + let mut tmp2 = [0u8; 17 * 16]; + simd::bilinear_h( + p, STRIDE, sx, sy, &BILINEAR_FILTERS[fx], &mut tmp2, bh + 1, + ); + simd::bilinear_v(&tmp2, &BILINEAR_FILTERS[fy], &mut got, 16, bw, bh); + } + for r in 0..bh { + assert_eq!( + got[r * 16..r * 16 + bw], + want[r * 16..r * 16 + bw], + "bilinear bw={bw} bh={bh} fx={fx} fy={fy} origin=({sx},{sy}) row={r}" + ); + } + } + } + } + } + } + } + } }