forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-cdef' (29dfc7d8): CDEF direction search row-at-a-time with an AVX2 partials twin, i16 gather window tapped in place by the filter kernel; byte-exact, -1.1% instructions on the 4K segment

Browse files at this commit

d35cd3a4f361a3efc9a9a33148665df67e1545c6Ahmet Kaan GümüşParent: 140e6628402ace
crates/ec-av1/src/decode.rsmodified+256 -59
@@ -15539,7 +15539,7 @@ fn cdef_gather(
1553915539 oy: usize,
1554015540 bw: usize,
1554115541 bh: usize,
15542- buf: &mut [i32],
15542+ buf: &mut [i16],
1554315543 ) {
1554415544 let bstride = bw + 2 * CDEF_BORDER;
1554515545 let rows = bh + 2 * CDEF_BORDER;
@@ -15555,7 +15555,7 @@ fn cdef_gather(
1555515555 let src_row = &src[s0..s0 + bstride];
1555615556 let dst_row = &mut buf[r * bstride..r * bstride + bstride];
1555715557 for (d, &v) in dst_row.iter_mut().zip(src_row) {
15558- *d = i32::from(v);
15558+ *d = v as i16;
1555915559 }
1556015560 }
1556115561 return;
@@ -15566,58 +15566,88 @@ fn cdef_gather(
1556615566 let nx = ox as isize + c as isize - CDEF_BORDER as isize;
1556715567 buf[r * bstride + c] =
1556815568 if ny < 0 || nx < 0 || ny as usize >= true_h || nx as usize >= true_w {
15569- CDEF_VERY_LARGE
15569+ CDEF_VERY_LARGE as i16
1557015570 } else {
15571- i32::from(src[ny as usize * stride + nx as usize])
15571+ src[ny as usize * stride + nx as usize] as i16
1557215572 };
1557315573 }
1557415574 }
1557515575 }
1557615576
15577+/// libaom `cdef_find_dir_c`'s eight partial-sum arrays, row at a time: each
15578+/// array takes the row, its reverse, its pairwise sums or its total added
15579+/// into consecutive slots, so the 512 scalar read-modify-writes on i64 the
15580+/// per-sample form did become a handful of lane-wide adds per row. i32 is
15581+/// exact here: every partial is a sum of at most eight values in -128..127,
15582+/// and slot 15 exists only so an eight-lane store at offset 7 fits.
15583+fn cdef_find_dir_partials(win: &[i16], bstride: usize, coeff_shift: i32) -> [[i32; 16]; 8] {
15584+ fn add_at<const N: usize>(dst: &mut [i32; 16], off: usize, src: &[i32; N]) {
15585+ for (d, &x) in dst[off..off + N].iter_mut().zip(src.iter()) {
15586+ *d += x;
15587+ }
15588+ }
15589+ let mut partial = [[0i32; 16]; 8];
15590+ for i in 0..8usize {
15591+ let base = (i + CDEF_BORDER) * bstride + CDEF_BORDER;
15592+ // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before
15593+ // accumulating, same as `constrain`'s threshold shift.
15594+ let row: [i32; 8] = core::array::from_fn(|j| (i32::from(win[base + j]) >> coeff_shift) - 128);
15595+ let rev: [i32; 8] = core::array::from_fn(|j| row[7 - j]);
15596+ let pair: [i32; 4] = core::array::from_fn(|k| row[2 * k] + row[2 * k + 1]);
15597+ let rpair: [i32; 4] = core::array::from_fn(|k| pair[3 - k]);
15598+ let (p, rest) = partial.split_at_mut(1);
15599+ add_at(&mut p[0], i, &row); // partial[0][i + j]
15600+ add_at(&mut rest[0], i, &pair); // partial[1][i + j / 2]
15601+ rest[1][i] = row.iter().sum(); // partial[2][i]
15602+ add_at(&mut rest[2], i, &rpair); // partial[3][3 + i - j / 2]
15603+ add_at(&mut rest[3], i, &rev); // partial[4][7 + i - j]
15604+ add_at(&mut rest[4], 3 - i / 2, &row); // partial[5][3 - i / 2 + j]
15605+ add_at(&mut rest[5], 0, &row); // partial[6][j]
15606+ add_at(&mut rest[6], i / 2, &row); // partial[7][i / 2 + j]
15607+ }
15608+ partial
15609+}
15610+
1557715611 /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant
1557815612 /// direction of an 8x8 luma window and the variance gap between it and its
15579-/// orthogonal, `sample(row, col)` reading that window's own pixels.
15580-fn cdef_find_dir(coeff_shift: i32, sample: impl Fn(usize, usize) -> i32) -> (usize, i32) {
15613+/// orthogonal, reading [`cdef_gather`]'s window (row stride `bstride`, the
15614+/// block's own samples starting at `CDEF_BORDER, CDEF_BORDER`).
15615+fn cdef_find_dir(win: &[i16], bstride: usize, coeff_shift: i32) -> (usize, i32) {
1558115616 const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105];
15582- let mut partial = [[0i64; 15]; 8];
15583- for i in 0..8i64 {
15584- for j in 0..8i64 {
15585- // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before
15586- // accumulating, same as `constrain`'s threshold shift below.
15587- let x = i64::from(sample(i as usize, j as usize) >> coeff_shift) - 128;
15588- partial[0][(i + j) as usize] += x;
15589- partial[1][(i + j / 2) as usize] += x;
15590- partial[2][i as usize] += x;
15591- partial[3][(3 + i - j / 2) as usize] += x;
15592- partial[4][(7 + i - j) as usize] += x;
15593- partial[5][(3 - i / 2 + j) as usize] += x;
15594- partial[6][j as usize] += x;
15595- partial[7][(i / 2 + j) as usize] += x;
15617+ #[cfg(target_arch = "x86_64")]
15618+ let partial = if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {
15619+ // SAFETY: reached only when the CPU reports avx2; the kernel reads
15620+ // the same eight rows of eight samples the scalar form does.
15621+ #[allow(unsafe_code)]
15622+ unsafe {
15623+ cdef_simd::find_dir_partials_avx2(win, bstride, coeff_shift)
1559615624 }
15597- }
15625+ } else {
15626+ cdef_find_dir_partials(win, bstride, coeff_shift)
15627+ };
15628+ #[cfg(not(target_arch = "x86_64"))]
15629+ let partial = cdef_find_dir_partials(win, bstride, coeff_shift);
15630+ let sq = |a: i32| -> i64 { i64::from(a) * i64::from(a) };
1559815631 let mut cost = [0i64; 8];
1559915632 for i in 0..8usize {
15600- cost[2] += partial[2][i] * partial[2][i];
15601- cost[6] += partial[6][i] * partial[6][i];
15633+ cost[2] += sq(partial[2][i]);
15634+ cost[6] += sq(partial[6][i]);
1560215635 }
1560315636 cost[2] *= DIV_TABLE[8];
1560415637 cost[6] *= DIV_TABLE[8];
1560515638 for i in 0..7usize {
15606- cost[0] += (partial[0][i] * partial[0][i] + partial[0][14 - i] * partial[0][14 - i])
15607- * DIV_TABLE[i + 1];
15608- cost[4] += (partial[4][i] * partial[4][i] + partial[4][14 - i] * partial[4][14 - i])
15609- * DIV_TABLE[i + 1];
15639+ cost[0] += (sq(partial[0][i]) + sq(partial[0][14 - i])) * DIV_TABLE[i + 1];
15640+ cost[4] += (sq(partial[4][i]) + sq(partial[4][14 - i])) * DIV_TABLE[i + 1];
1561015641 }
15611- cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8];
15612- cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8];
15642+ cost[0] += sq(partial[0][7]) * DIV_TABLE[8];
15643+ cost[4] += sq(partial[4][7]) * DIV_TABLE[8];
1561315644 for i in (1..8).step_by(2) {
1561415645 for j in 0..5usize {
15615- cost[i] += partial[i][3 + j] * partial[i][3 + j];
15646+ cost[i] += sq(partial[i][3 + j]);
1561615647 }
1561715648 cost[i] *= DIV_TABLE[8];
1561815649 for j in 0..3usize {
15619- cost[i] += (partial[i][j] * partial[i][j] + partial[i][10 - j] * partial[i][10 - j])
15620- * DIV_TABLE[2 * j + 2];
15650+ cost[i] += (sq(partial[i][j]) + sq(partial[i][10 - j])) * DIV_TABLE[2 * j + 2];
1562115651 }
1562215652 }
1562315653 let mut best_cost = 0i64;
@@ -15653,7 +15683,7 @@ fn cdef_adjust_strength(strength: i32, var: i32) -> i32 {
1565315683 #[allow(clippy::too_many_arguments)]
1565415684 #[allow(unsafe_code)]
1565515685 fn cdef_filter_block(
15656- src: &[i32],
15686+ src: &[i16],
1565715687 dst: &mut PlaneBuf<'static>,
1565815688 ox: usize,
1565915689 oy: usize,
@@ -15722,7 +15752,7 @@ fn cdef_filter_block(
1572215752 /// `cdef_simd_kernel_matches_the_scalar_filter` checks the kernel against.
1572315753 #[allow(clippy::too_many_arguments)]
1572415754 fn cdef_filter_block_scalar(
15725- src: &[i32],
15755+ src: &[i16],
1572615756 dst: &mut [u16],
1572715757 stride: usize,
1572815758 ox: usize,
@@ -15753,7 +15783,7 @@ fn cdef_filter_block_scalar(
1575315783 // `src` is [`cdef_gather`]'s window: (i, j) in block coordinates, taps
1575415784 // reaching +-CDEF_BORDER, so every index below is in range by construction.
1575515785 let at = |i: i32, j: i32| -> i32 {
15756- src[(i + CDEF_BORDER as i32) as usize * bstride + (j + CDEF_BORDER as i32) as usize]
15786+ i32::from(src[(i + CDEF_BORDER as i32) as usize * bstride + (j + CDEF_BORDER as i32) as usize])
1575715787 };
1575815788 let (pri_dir, sec_dir0, sec_dir1) =
1575915789 (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]);
@@ -15863,6 +15893,60 @@ mod cdef_simd {
1586315893 _mm256_sign_epi16(t, diff)
1586415894 }
1586515895
15896+ /// [`super::cdef_find_dir_partials`] in AVX2 (libaom
15897+ /// `cdef_find_dir_avx2`'s shape): eight i32 lanes per row instead of the
15898+ /// four the auto-vectorised scalar form gets without the feature.
15899+ ///
15900+ /// # Safety
15901+ /// Requires AVX2. `win` must hold [`super::cdef_gather`]'s 8x8 window
15902+ /// grown by `CDEF_BORDER` at row stride `bstride`.
15903+ #[target_feature(enable = "avx2")]
15904+ pub unsafe fn find_dir_partials_avx2(
15905+ win: &[i16],
15906+ bstride: usize,
15907+ coeff_shift: i32,
15908+ ) -> [[i32; 16]; 8] {
15909+ #[target_feature(enable = "avx2")]
15910+ unsafe fn add8(dst: &mut [i32; 16], off: usize, v: __m256i) {
15911+ let p = dst.as_mut_ptr().add(off) as *mut __m256i;
15912+ _mm256_storeu_si256(p, _mm256_add_epi32(_mm256_loadu_si256(p), v));
15913+ }
15914+ #[target_feature(enable = "avx2")]
15915+ unsafe fn add4(dst: &mut [i32; 16], off: usize, v: __m128i) {
15916+ let p = dst.as_mut_ptr().add(off) as *mut __m128i;
15917+ _mm_storeu_si128(p, _mm_add_epi32(_mm_loadu_si128(p), v));
15918+ }
15919+ let mut partial = [[0i32; 16]; 8];
15920+ let shift = _mm_cvtsi32_si128(coeff_shift);
15921+ let c128 = _mm256_set1_epi32(128);
15922+ let revidx = _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0);
15923+ let mut p6 = _mm256_setzero_si256();
15924+ for i in 0..8usize {
15925+ let base = (i + CDEF_BORDER) * bstride + CDEF_BORDER;
15926+ let raw = _mm_loadu_si128(win.as_ptr().add(base) as *const __m128i);
15927+ let row = _mm256_sub_epi32(_mm256_sra_epi32(_mm256_cvtepi16_epi32(raw), shift), c128);
15928+ let rev = _mm256_permutevar8x32_epi32(row, revidx);
15929+ // `hadd(row, row)` is [p0, p1, p0, p1 | p2, p3, p2, p3], so the
15930+ // four pairwise sums are the two lanes' low halves joined.
15931+ let h = _mm256_hadd_epi32(row, row);
15932+ let pair =
15933+ _mm_unpacklo_epi64(_mm256_castsi256_si128(h), _mm256_extracti128_si256(h, 1));
15934+ let rpair = _mm_shuffle_epi32(pair, 0b00_01_10_11);
15935+ let hs = _mm_hadd_epi32(pair, pair);
15936+ let total = _mm_cvtsi128_si32(_mm_hadd_epi32(hs, hs));
15937+ add8(&mut partial[0], i, row);
15938+ add4(&mut partial[1], i, pair);
15939+ partial[2][i] = total;
15940+ add4(&mut partial[3], i, rpair);
15941+ add8(&mut partial[4], i, rev);
15942+ add8(&mut partial[5], 3 - i / 2, row);
15943+ p6 = _mm256_add_epi32(p6, row);
15944+ add8(&mut partial[7], i / 2, row);
15945+ }
15946+ _mm256_storeu_si256(partial[6].as_mut_ptr() as *mut __m256i, p6);
15947+ partial
15948+ }
15949+
1586615950 /// [`super::cdef_filter_block_scalar`]'s arithmetic, two rows per
1586715951 /// iteration. `bw` is 8 (luma) or 4 (4:2:0 chroma) and `bh` matches; for
1586815952 /// `bw == 4` the upper four lanes of each half are window samples read
@@ -15870,12 +15954,14 @@ mod cdef_simd {
1587015954 /// computed and then dropped by the 64-bit store.
1587115955 ///
1587215956 /// # Safety
15873- /// Requires AVX2. `src` must hold the `(bw + 4) x (bh + 4)` window and
15874- /// `dst` the `bh` rows of `bw` samples at `(ox, oy)`.
15957+ /// Requires AVX2. `src` must be [`super::cdef_gather`]'s 256-entry window
15958+ /// buffer (the tap loads read up to 152 entries past its start, i.e. past
15959+ /// the `(bw + 4) x (bh + 4)` samples themselves) and `dst` the `bh` rows
15960+ /// of `bw` samples at `(ox, oy)`.
1587515961 #[target_feature(enable = "avx2")]
1587615962 #[allow(clippy::too_many_arguments)]
1587715963 pub unsafe fn filter_block_avx2(
15878- src: &[i32],
15964+ src: &[i16],
1587915965 dst: &mut [u16],
1588015966 stride: usize,
1588115967 ox: usize,
@@ -15893,14 +15979,11 @@ mod cdef_simd {
1589315979 smax: i32,
1589415980 ) {
1589515981 let bstride = bw + 2 * CDEF_BORDER;
15896- // The window as `i16`, padded so every two-row tap load stays inside
15897- // it (largest read: (bh + 3) * bstride + 8 <= 152 for the 8x8 luma
15898- // block, 72 for the 4x4 chroma one).
15899- let mut w = [0i16; 256];
15900- let rows = bh + 2 * CDEF_BORDER;
15901- for (d, &v) in w[..rows * bstride].iter_mut().zip(&src[..rows * bstride]) {
15902- *d = v as i16;
15903- }
15982+ // The caller's window is already `i16` and 256 entries long, so the
15983+ // kernel taps it in place: the largest two-row load is
15984+ // (bh + 3) * bstride + 8 <= 152 for the 8x8 luma block, 72 for the
15985+ // 4x4 chroma one, both inside the buffer's zeroed tail.
15986+ let w = src;
1590415987 let clipping_required = enable_primary && enable_secondary;
1590515988 let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize];
1590615989 let pri_shift = _mm_cvtsi32_si128(cdef_constrain_shift(pri_strength.max(1), pri_damping));
@@ -18801,8 +18884,11 @@ fn cdef_band(
1880118884 };
1880218885 // One scratch window per plane kind, reused by every unit (8x8 luma and
1880318886 // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring).
18804- let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)];
18805- let mut win_uv = [0i32; (4 + 2 * CDEF_BORDER) * (4 + 2 * CDEF_BORDER)];
18887+ // 256 entries, not the (bw + 4) x (bh + 4) samples themselves: the
18888+ // AVX2 kernel taps the window in place with two-row loads that reach
18889+ // past the last sample, and the tail stays zero for every unit.
18890+ let mut win_y = [0i16; 256];
18891+ let mut win_uv = [0i16; 256];
1880618892 let mut mi_r = r_lo;
1880718893 while mi_r < r_hi {
1880818894 let mut mi_c = 0usize;
@@ -18826,10 +18912,6 @@ fn cdef_band(
1882618912 }
1882718913 let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height);
1882818914 cdef_gather(src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y);
18829- let sample_y = |r: i32, c: i32| -> i32 {
18830- win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER)
18831- + (c + CDEF_BORDER as i32) as usize]
18832- };
1883318915 // lane-cdef r1: the direction search reads the SAME padded
1883418916 // block the taps do -- libaom runs `cdef_find_dir` on the
1883518917 // prepared filter block (`cdef_block.c:296-320` over
@@ -18838,7 +18920,7 @@ fn cdef_band(
1883818920 // Clamping to the last real sample instead made every straddling
1883918921 // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence
1884018922 // a different `adjust_strength`, hence a +-1 band at the crop edge.
18841- let (dir, var) = cdef_find_dir(coeff_shift, |r, c| sample_y(r as i32, c as i32));
18923+ let (dir, var) = cdef_find_dir(&win_y, 8 + 2 * CDEF_BORDER, coeff_shift);
1884218924
1884318925 // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0`
1884418926 // -- each plane zeroes the shared direction when *its own*
@@ -33798,15 +33880,15 @@ mod tests {
3379833880 let src: Vec<u16> = (0..stride * 24).map(|i| (i * 7 % 1024) as u16).collect();
3379933881 for &(ox, oy, bw, bh) in &[(0, 0, 8, 8), (8, 8, 8, 8), (16, 16, 8, 8), (4, 12, 4, 4)] {
3380033882 let bstride = bw + 2 * super::CDEF_BORDER;
33801- let mut win = vec![0i32; bstride * (bh + 2 * super::CDEF_BORDER)];
33883+ let mut win = vec![0i16; bstride * (bh + 2 * super::CDEF_BORDER)];
3380233884 super::cdef_gather(&src, stride, tw, th, ox, oy, bw, bh, &mut win);
3380333885 for r in -(super::CDEF_BORDER as i32)..(bh + super::CDEF_BORDER) as i32 {
3380433886 for c in -(super::CDEF_BORDER as i32)..(bw + super::CDEF_BORDER) as i32 {
3380533887 let (ny, nx) = (oy as i32 + r, ox as i32 + c);
3380633888 let want = if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw {
33807- super::CDEF_VERY_LARGE
33889+ super::CDEF_VERY_LARGE as i16
3380833890 } else {
33809- i32::from(src[ny as usize * stride + nx as usize])
33891+ src[ny as usize * stride + nx as usize] as i16
3381033892 };
3381133893 let got = win[(r + super::CDEF_BORDER as i32) as usize * bstride
3381233894 + (c + super::CDEF_BORDER as i32) as usize];
@@ -33816,6 +33898,117 @@ mod tests {
3381633898 }
3381733899 }
3381833900
33901+ /// lane-cdef: the row-at-a-time direction search (and its AVX2 twin) must
33902+ /// be value-identical to a literal transcription of libaom
33903+ /// `cdef_find_dir_c`'s per-sample accumulation, over random windows at
33904+ /// every bit depth -- including the `CDEF_VERY_LARGE` sentinel rows a
33905+ /// crop-straddling 8x8 gathers.
33906+ #[test]
33907+ fn cdef_find_dir_matches_the_per_sample_reference() {
33908+ const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105];
33909+ fn reference(win: &[i16], bstride: usize, coeff_shift: i32) -> (usize, i32) {
33910+ let mut partial = [[0i64; 15]; 8];
33911+ for i in 0..8i64 {
33912+ for j in 0..8i64 {
33913+ let s = i64::from(
33914+ win[(i as usize + super::CDEF_BORDER) * bstride
33915+ + j as usize
33916+ + super::CDEF_BORDER],
33917+ );
33918+ let x = (s >> coeff_shift) - 128;
33919+ partial[0][(i + j) as usize] += x;
33920+ partial[1][(i + j / 2) as usize] += x;
33921+ partial[2][i as usize] += x;
33922+ partial[3][(3 + i - j / 2) as usize] += x;
33923+ partial[4][(7 + i - j) as usize] += x;
33924+ partial[5][(3 - i / 2 + j) as usize] += x;
33925+ partial[6][j as usize] += x;
33926+ partial[7][(i / 2 + j) as usize] += x;
33927+ }
33928+ }
33929+ let mut cost = [0i64; 8];
33930+ for i in 0..8usize {
33931+ cost[2] += partial[2][i] * partial[2][i];
33932+ cost[6] += partial[6][i] * partial[6][i];
33933+ }
33934+ cost[2] *= DIV_TABLE[8];
33935+ cost[6] *= DIV_TABLE[8];
33936+ for i in 0..7usize {
33937+ cost[0] += (partial[0][i] * partial[0][i]
33938+ + partial[0][14 - i] * partial[0][14 - i])
33939+ * DIV_TABLE[i + 1];
33940+ cost[4] += (partial[4][i] * partial[4][i]
33941+ + partial[4][14 - i] * partial[4][14 - i])
33942+ * DIV_TABLE[i + 1];
33943+ }
33944+ cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8];
33945+ cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8];
33946+ for i in (1..8).step_by(2) {
33947+ for j in 0..5usize {
33948+ cost[i] += partial[i][3 + j] * partial[i][3 + j];
33949+ }
33950+ cost[i] *= DIV_TABLE[8];
33951+ for j in 0..3usize {
33952+ cost[i] += (partial[i][j] * partial[i][j]
33953+ + partial[i][10 - j] * partial[i][10 - j])
33954+ * DIV_TABLE[2 * j + 2];
33955+ }
33956+ }
33957+ let (mut best_cost, mut best_dir) = (0i64, 0usize);
33958+ for (i, &c) in cost.iter().enumerate() {
33959+ if c > best_cost {
33960+ best_cost = c;
33961+ best_dir = i;
33962+ }
33963+ }
33964+ (best_dir, ((best_cost - cost[(best_dir + 4) & 7]) >> 10) as i32)
33965+ }
33966+ let mut rng = 0x0bad_c0de_dead_beefu64;
33967+ let mut next = move || {
33968+ rng ^= rng << 13;
33969+ rng ^= rng >> 7;
33970+ rng ^= rng << 17;
33971+ rng
33972+ };
33973+ let bstride = 8 + 2 * super::CDEF_BORDER;
33974+ for &(bit_depth, coeff_shift) in &[(8u8, 0i32), (10, 2), (12, 4)] {
33975+ let smax = (1u64 << bit_depth) - 1;
33976+ for trial in 0..256 {
33977+ let sentinels = trial % 4;
33978+ let mut win = [0i16; 256];
33979+ for (k, w) in win[..bstride * bstride].iter_mut().enumerate() {
33980+ let (r, c) = (k / bstride, k % bstride);
33981+ *w = if sentinels > 0
33982+ && (r < sentinels
33983+ || c < sentinels
33984+ || r + sentinels >= bstride
33985+ || c + sentinels >= bstride)
33986+ {
33987+ super::CDEF_VERY_LARGE as i16
33988+ } else {
33989+ (next() % (smax + 1)) as i16
33990+ };
33991+ }
33992+ let want = reference(&win, bstride, coeff_shift);
33993+ assert_eq!(
33994+ super::cdef_find_dir(&win, bstride, coeff_shift),
33995+ want,
33996+ "bd={bit_depth} trial={trial}"
33997+ );
33998+ let partial = super::cdef_find_dir_partials(&win, bstride, coeff_shift);
33999+ #[cfg(target_arch = "x86_64")]
34000+ if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {
34001+ // SAFETY: the avx2 arm was probed above.
34002+ #[allow(unsafe_code)]
34003+ let simd = unsafe {
34004+ super::cdef_simd::find_dir_partials_avx2(&win, bstride, coeff_shift)
34005+ };
34006+ assert_eq!(simd, partial, "bd={bit_depth} trial={trial} partials");
34007+ }
34008+ }
34009+ }
34010+ }
34011+
3381934012 /// lane-perf7 step 1: the AVX2 CDEF kernel must be value-identical to
3382034013 /// [`super::cdef_filter_block_scalar`] over the whole input domain --
3382134014 /// every direction, every enable pair, both block shapes, the 8/10/12-bit
@@ -33846,7 +34039,10 @@ mod tests {
3384634039 // Every fourth window seeds sentinels, in a growing
3384734040 // border ring, so the straddling-block case is covered.
3384834041 let sentinels = trial % 4;
33849- let win: Vec<i32> = (0..rows * bstride)
34042+ // 256 entries, as `cdef_band`'s own scratch is: the
34043+ // kernel taps the window in place and reads past the last
34044+ // sample into the zeroed tail.
34045+ let mut win: Vec<i16> = (0..rows * bstride)
3385034046 .map(|k| {
3385134047 let (r, c) = (k / bstride, k % bstride);
3385234048 if sentinels > 0
@@ -33855,12 +34051,13 @@ mod tests {
3385534051 || r + sentinels >= rows
3385634052 || c + sentinels >= bstride)
3385734053 {
33858- super::CDEF_VERY_LARGE
34054+ super::CDEF_VERY_LARGE as i16
3385934055 } else {
33860- (next() % (smax as u64 + 1)) as i32
34056+ (next() % (smax as u64 + 1)) as i16
3386134057 }
3386234058 })
3386334059 .collect();
34060+ win.resize(256, 0);
3386434061 for dir in 0..8usize {
3386534062 for &pri in &[0i32, 1, 3, 15] {
3386634063 for &sec in &[0i32, 1, 2, 4] {