forge
Explore
Log in

Don't you dare go Hollow

CDEF direction search row-at-a-time, i16 filter windows

Browse files at this commit

8402acecd16119df28a8156ab2b9755818a2444eAhmet Kaan GümüşParent: 74e98d8
  • perf
    • CDEF direction search row-at-a-time, i16 filter windows
Show full message
perf(av1): CDEF direction search row-at-a-time, i16 filter windows

`cdef_find_dir`'s 64-sample accumulation was 57% of `cdef_band` (22.6% of
the function on decode.rs:15560 alone): 512 scalar read-modify-writes on
i64 partials. Each of libaom `cdef_find_dir_c`'s eight index forms is,
once the row is fixed, "add the row / its reverse / its pairwise sums /
its total into consecutive slots", so the loop is rewritten row at a time
over i32 partials (exact: a partial is at most eight samples in -128..127)
and gains an AVX2 twin, `cdef_find_dir_partials_avx2`, for eight lanes
instead of the four the scalar form auto-vectorises to.

`cdef_gather` now writes the window as i16, which is what the AVX2 tap
kernel wanted anyway: it taps the caller's 256-entry scratch in place
instead of copying and narrowing 144 entries per block.

Byte-exact: all four gate streams unchanged. seg4k instructions:u
504.77 -> 499.27 G (-1.09%); cdef_band 3.02% -> 1.78% of cycles.
New gate `cdef_find_dir_matches_the_per_sample_reference` pins both forms
against a literal transcription of `cdef_find_dir_c`, sentinel rows
included.
crates/ec-av1/src/decode.rsmodified+256 -59
@@ -15510,7 +15510,7 @@ fn cdef_gather(
1551015510 oy: usize,
1551115511 bw: usize,
1551215512 bh: usize,
15513- buf: &mut [i32],
15513+ buf: &mut [i16],
1551415514 ) {
1551515515 let bstride = bw + 2 * CDEF_BORDER;
1551615516 let rows = bh + 2 * CDEF_BORDER;
@@ -15526,7 +15526,7 @@ fn cdef_gather(
1552615526 let src_row = &src[s0..s0 + bstride];
1552715527 let dst_row = &mut buf[r * bstride..r * bstride + bstride];
1552815528 for (d, &v) in dst_row.iter_mut().zip(src_row) {
15529- *d = i32::from(v);
15529+ *d = v as i16;
1553015530 }
1553115531 }
1553215532 return;
@@ -15537,58 +15537,88 @@ fn cdef_gather(
1553715537 let nx = ox as isize + c as isize - CDEF_BORDER as isize;
1553815538 buf[r * bstride + c] =
1553915539 if ny < 0 || nx < 0 || ny as usize >= true_h || nx as usize >= true_w {
15540- CDEF_VERY_LARGE
15540+ CDEF_VERY_LARGE as i16
1554115541 } else {
15542- i32::from(src[ny as usize * stride + nx as usize])
15542+ src[ny as usize * stride + nx as usize] as i16
1554315543 };
1554415544 }
1554515545 }
1554615546 }
1554715547
15548+/// libaom `cdef_find_dir_c`'s eight partial-sum arrays, row at a time: each
15549+/// array takes the row, its reverse, its pairwise sums or its total added
15550+/// into consecutive slots, so the 512 scalar read-modify-writes on i64 the
15551+/// per-sample form did become a handful of lane-wide adds per row. i32 is
15552+/// exact here: every partial is a sum of at most eight values in -128..127,
15553+/// and slot 15 exists only so an eight-lane store at offset 7 fits.
15554+fn cdef_find_dir_partials(win: &[i16], bstride: usize, coeff_shift: i32) -> [[i32; 16]; 8] {
15555+ fn add_at<const N: usize>(dst: &mut [i32; 16], off: usize, src: &[i32; N]) {
15556+ for (d, &x) in dst[off..off + N].iter_mut().zip(src.iter()) {
15557+ *d += x;
15558+ }
15559+ }
15560+ let mut partial = [[0i32; 16]; 8];
15561+ for i in 0..8usize {
15562+ let base = (i + CDEF_BORDER) * bstride + CDEF_BORDER;
15563+ // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before
15564+ // accumulating, same as `constrain`'s threshold shift.
15565+ let row: [i32; 8] = core::array::from_fn(|j| (i32::from(win[base + j]) >> coeff_shift) - 128);
15566+ let rev: [i32; 8] = core::array::from_fn(|j| row[7 - j]);
15567+ let pair: [i32; 4] = core::array::from_fn(|k| row[2 * k] + row[2 * k + 1]);
15568+ let rpair: [i32; 4] = core::array::from_fn(|k| pair[3 - k]);
15569+ let (p, rest) = partial.split_at_mut(1);
15570+ add_at(&mut p[0], i, &row); // partial[0][i + j]
15571+ add_at(&mut rest[0], i, &pair); // partial[1][i + j / 2]
15572+ rest[1][i] = row.iter().sum(); // partial[2][i]
15573+ add_at(&mut rest[2], i, &rpair); // partial[3][3 + i - j / 2]
15574+ add_at(&mut rest[3], i, &rev); // partial[4][7 + i - j]
15575+ add_at(&mut rest[4], 3 - i / 2, &row); // partial[5][3 - i / 2 + j]
15576+ add_at(&mut rest[5], 0, &row); // partial[6][j]
15577+ add_at(&mut rest[6], i / 2, &row); // partial[7][i / 2 + j]
15578+ }
15579+ partial
15580+}
15581+
1554815582 /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant
1554915583 /// direction of an 8x8 luma window and the variance gap between it and its
15550-/// orthogonal, `sample(row, col)` reading that window's own pixels.
15551-fn cdef_find_dir(coeff_shift: i32, sample: impl Fn(usize, usize) -> i32) -> (usize, i32) {
15584+/// orthogonal, reading [`cdef_gather`]'s window (row stride `bstride`, the
15585+/// block's own samples starting at `CDEF_BORDER, CDEF_BORDER`).
15586+fn cdef_find_dir(win: &[i16], bstride: usize, coeff_shift: i32) -> (usize, i32) {
1555215587 const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105];
15553- let mut partial = [[0i64; 15]; 8];
15554- for i in 0..8i64 {
15555- for j in 0..8i64 {
15556- // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before
15557- // accumulating, same as `constrain`'s threshold shift below.
15558- let x = i64::from(sample(i as usize, j as usize) >> coeff_shift) - 128;
15559- partial[0][(i + j) as usize] += x;
15560- partial[1][(i + j / 2) as usize] += x;
15561- partial[2][i as usize] += x;
15562- partial[3][(3 + i - j / 2) as usize] += x;
15563- partial[4][(7 + i - j) as usize] += x;
15564- partial[5][(3 - i / 2 + j) as usize] += x;
15565- partial[6][j as usize] += x;
15566- partial[7][(i / 2 + j) as usize] += x;
15588+ #[cfg(target_arch = "x86_64")]
15589+ let partial = if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {
15590+ // SAFETY: reached only when the CPU reports avx2; the kernel reads
15591+ // the same eight rows of eight samples the scalar form does.
15592+ #[allow(unsafe_code)]
15593+ unsafe {
15594+ cdef_simd::find_dir_partials_avx2(win, bstride, coeff_shift)
1556715595 }
15568- }
15596+ } else {
15597+ cdef_find_dir_partials(win, bstride, coeff_shift)
15598+ };
15599+ #[cfg(not(target_arch = "x86_64"))]
15600+ let partial = cdef_find_dir_partials(win, bstride, coeff_shift);
15601+ let sq = |a: i32| -> i64 { i64::from(a) * i64::from(a) };
1556915602 let mut cost = [0i64; 8];
1557015603 for i in 0..8usize {
15571- cost[2] += partial[2][i] * partial[2][i];
15572- cost[6] += partial[6][i] * partial[6][i];
15604+ cost[2] += sq(partial[2][i]);
15605+ cost[6] += sq(partial[6][i]);
1557315606 }
1557415607 cost[2] *= DIV_TABLE[8];
1557515608 cost[6] *= DIV_TABLE[8];
1557615609 for i in 0..7usize {
15577- cost[0] += (partial[0][i] * partial[0][i] + partial[0][14 - i] * partial[0][14 - i])
15578- * DIV_TABLE[i + 1];
15579- cost[4] += (partial[4][i] * partial[4][i] + partial[4][14 - i] * partial[4][14 - i])
15580- * DIV_TABLE[i + 1];
15610+ cost[0] += (sq(partial[0][i]) + sq(partial[0][14 - i])) * DIV_TABLE[i + 1];
15611+ cost[4] += (sq(partial[4][i]) + sq(partial[4][14 - i])) * DIV_TABLE[i + 1];
1558115612 }
15582- cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8];
15583- cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8];
15613+ cost[0] += sq(partial[0][7]) * DIV_TABLE[8];
15614+ cost[4] += sq(partial[4][7]) * DIV_TABLE[8];
1558415615 for i in (1..8).step_by(2) {
1558515616 for j in 0..5usize {
15586- cost[i] += partial[i][3 + j] * partial[i][3 + j];
15617+ cost[i] += sq(partial[i][3 + j]);
1558715618 }
1558815619 cost[i] *= DIV_TABLE[8];
1558915620 for j in 0..3usize {
15590- cost[i] += (partial[i][j] * partial[i][j] + partial[i][10 - j] * partial[i][10 - j])
15591- * DIV_TABLE[2 * j + 2];
15621+ cost[i] += (sq(partial[i][j]) + sq(partial[i][10 - j])) * DIV_TABLE[2 * j + 2];
1559215622 }
1559315623 }
1559415624 let mut best_cost = 0i64;
@@ -15624,7 +15654,7 @@ fn cdef_adjust_strength(strength: i32, var: i32) -> i32 {
1562415654 #[allow(clippy::too_many_arguments)]
1562515655 #[allow(unsafe_code)]
1562615656 fn cdef_filter_block(
15627- src: &[i32],
15657+ src: &[i16],
1562815658 dst: &mut PlaneBuf<'static>,
1562915659 ox: usize,
1563015660 oy: usize,
@@ -15693,7 +15723,7 @@ fn cdef_filter_block(
1569315723 /// `cdef_simd_kernel_matches_the_scalar_filter` checks the kernel against.
1569415724 #[allow(clippy::too_many_arguments)]
1569515725 fn cdef_filter_block_scalar(
15696- src: &[i32],
15726+ src: &[i16],
1569715727 dst: &mut [u16],
1569815728 stride: usize,
1569915729 ox: usize,
@@ -15724,7 +15754,7 @@ fn cdef_filter_block_scalar(
1572415754 // `src` is [`cdef_gather`]'s window: (i, j) in block coordinates, taps
1572515755 // reaching +-CDEF_BORDER, so every index below is in range by construction.
1572615756 let at = |i: i32, j: i32| -> i32 {
15727- src[(i + CDEF_BORDER as i32) as usize * bstride + (j + CDEF_BORDER as i32) as usize]
15757+ i32::from(src[(i + CDEF_BORDER as i32) as usize * bstride + (j + CDEF_BORDER as i32) as usize])
1572815758 };
1572915759 let (pri_dir, sec_dir0, sec_dir1) =
1573015760 (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]);
@@ -15834,6 +15864,60 @@ mod cdef_simd {
1583415864 _mm256_sign_epi16(t, diff)
1583515865 }
1583615866
15867+ /// [`super::cdef_find_dir_partials`] in AVX2 (libaom
15868+ /// `cdef_find_dir_avx2`'s shape): eight i32 lanes per row instead of the
15869+ /// four the auto-vectorised scalar form gets without the feature.
15870+ ///
15871+ /// # Safety
15872+ /// Requires AVX2. `win` must hold [`super::cdef_gather`]'s 8x8 window
15873+ /// grown by `CDEF_BORDER` at row stride `bstride`.
15874+ #[target_feature(enable = "avx2")]
15875+ pub unsafe fn find_dir_partials_avx2(
15876+ win: &[i16],
15877+ bstride: usize,
15878+ coeff_shift: i32,
15879+ ) -> [[i32; 16]; 8] {
15880+ #[target_feature(enable = "avx2")]
15881+ unsafe fn add8(dst: &mut [i32; 16], off: usize, v: __m256i) {
15882+ let p = dst.as_mut_ptr().add(off) as *mut __m256i;
15883+ _mm256_storeu_si256(p, _mm256_add_epi32(_mm256_loadu_si256(p), v));
15884+ }
15885+ #[target_feature(enable = "avx2")]
15886+ unsafe fn add4(dst: &mut [i32; 16], off: usize, v: __m128i) {
15887+ let p = dst.as_mut_ptr().add(off) as *mut __m128i;
15888+ _mm_storeu_si128(p, _mm_add_epi32(_mm_loadu_si128(p), v));
15889+ }
15890+ let mut partial = [[0i32; 16]; 8];
15891+ let shift = _mm_cvtsi32_si128(coeff_shift);
15892+ let c128 = _mm256_set1_epi32(128);
15893+ let revidx = _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0);
15894+ let mut p6 = _mm256_setzero_si256();
15895+ for i in 0..8usize {
15896+ let base = (i + CDEF_BORDER) * bstride + CDEF_BORDER;
15897+ let raw = _mm_loadu_si128(win.as_ptr().add(base) as *const __m128i);
15898+ let row = _mm256_sub_epi32(_mm256_sra_epi32(_mm256_cvtepi16_epi32(raw), shift), c128);
15899+ let rev = _mm256_permutevar8x32_epi32(row, revidx);
15900+ // `hadd(row, row)` is [p0, p1, p0, p1 | p2, p3, p2, p3], so the
15901+ // four pairwise sums are the two lanes' low halves joined.
15902+ let h = _mm256_hadd_epi32(row, row);
15903+ let pair =
15904+ _mm_unpacklo_epi64(_mm256_castsi256_si128(h), _mm256_extracti128_si256(h, 1));
15905+ let rpair = _mm_shuffle_epi32(pair, 0b00_01_10_11);
15906+ let hs = _mm_hadd_epi32(pair, pair);
15907+ let total = _mm_cvtsi128_si32(_mm_hadd_epi32(hs, hs));
15908+ add8(&mut partial[0], i, row);
15909+ add4(&mut partial[1], i, pair);
15910+ partial[2][i] = total;
15911+ add4(&mut partial[3], i, rpair);
15912+ add8(&mut partial[4], i, rev);
15913+ add8(&mut partial[5], 3 - i / 2, row);
15914+ p6 = _mm256_add_epi32(p6, row);
15915+ add8(&mut partial[7], i / 2, row);
15916+ }
15917+ _mm256_storeu_si256(partial[6].as_mut_ptr() as *mut __m256i, p6);
15918+ partial
15919+ }
15920+
1583715921 /// [`super::cdef_filter_block_scalar`]'s arithmetic, two rows per
1583815922 /// iteration. `bw` is 8 (luma) or 4 (4:2:0 chroma) and `bh` matches; for
1583915923 /// `bw == 4` the upper four lanes of each half are window samples read
@@ -15841,12 +15925,14 @@ mod cdef_simd {
1584115925 /// computed and then dropped by the 64-bit store.
1584215926 ///
1584315927 /// # Safety
15844- /// Requires AVX2. `src` must hold the `(bw + 4) x (bh + 4)` window and
15845- /// `dst` the `bh` rows of `bw` samples at `(ox, oy)`.
15928+ /// Requires AVX2. `src` must be [`super::cdef_gather`]'s 256-entry window
15929+ /// buffer (the tap loads read up to 152 entries past its start, i.e. past
15930+ /// the `(bw + 4) x (bh + 4)` samples themselves) and `dst` the `bh` rows
15931+ /// of `bw` samples at `(ox, oy)`.
1584615932 #[target_feature(enable = "avx2")]
1584715933 #[allow(clippy::too_many_arguments)]
1584815934 pub unsafe fn filter_block_avx2(
15849- src: &[i32],
15935+ src: &[i16],
1585015936 dst: &mut [u16],
1585115937 stride: usize,
1585215938 ox: usize,
@@ -15864,14 +15950,11 @@ mod cdef_simd {
1586415950 smax: i32,
1586515951 ) {
1586615952 let bstride = bw + 2 * CDEF_BORDER;
15867- // The window as `i16`, padded so every two-row tap load stays inside
15868- // it (largest read: (bh + 3) * bstride + 8 <= 152 for the 8x8 luma
15869- // block, 72 for the 4x4 chroma one).
15870- let mut w = [0i16; 256];
15871- let rows = bh + 2 * CDEF_BORDER;
15872- for (d, &v) in w[..rows * bstride].iter_mut().zip(&src[..rows * bstride]) {
15873- *d = v as i16;
15874- }
15953+ // The caller's window is already `i16` and 256 entries long, so the
15954+ // kernel taps it in place: the largest two-row load is
15955+ // (bh + 3) * bstride + 8 <= 152 for the 8x8 luma block, 72 for the
15956+ // 4x4 chroma one, both inside the buffer's zeroed tail.
15957+ let w = src;
1587515958 let clipping_required = enable_primary && enable_secondary;
1587615959 let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize];
1587715960 let pri_shift = _mm_cvtsi32_si128(cdef_constrain_shift(pri_strength.max(1), pri_damping));
@@ -18772,8 +18855,11 @@ fn cdef_band(
1877218855 };
1877318856 // One scratch window per plane kind, reused by every unit (8x8 luma and
1877418857 // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring).
18775- let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)];
18776- let mut win_uv = [0i32; (4 + 2 * CDEF_BORDER) * (4 + 2 * CDEF_BORDER)];
18858+ // 256 entries, not the (bw + 4) x (bh + 4) samples themselves: the
18859+ // AVX2 kernel taps the window in place with two-row loads that reach
18860+ // past the last sample, and the tail stays zero for every unit.
18861+ let mut win_y = [0i16; 256];
18862+ let mut win_uv = [0i16; 256];
1877718863 let mut mi_r = r_lo;
1877818864 while mi_r < r_hi {
1877918865 let mut mi_c = 0usize;
@@ -18797,10 +18883,6 @@ fn cdef_band(
1879718883 }
1879818884 let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height);
1879918885 cdef_gather(src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y);
18800- let sample_y = |r: i32, c: i32| -> i32 {
18801- win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER)
18802- + (c + CDEF_BORDER as i32) as usize]
18803- };
1880418886 // lane-cdef r1: the direction search reads the SAME padded
1880518887 // block the taps do -- libaom runs `cdef_find_dir` on the
1880618888 // prepared filter block (`cdef_block.c:296-320` over
@@ -18809,7 +18891,7 @@ fn cdef_band(
1880918891 // Clamping to the last real sample instead made every straddling
1881018892 // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence
1881118893 // a different `adjust_strength`, hence a +-1 band at the crop edge.
18812- let (dir, var) = cdef_find_dir(coeff_shift, |r, c| sample_y(r as i32, c as i32));
18894+ let (dir, var) = cdef_find_dir(&win_y, 8 + 2 * CDEF_BORDER, coeff_shift);
1881318895
1881418896 // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0`
1881518897 // -- each plane zeroes the shared direction when *its own*
@@ -33768,15 +33850,15 @@ mod tests {
3376833850 let src: Vec<u16> = (0..stride * 24).map(|i| (i * 7 % 1024) as u16).collect();
3376933851 for &(ox, oy, bw, bh) in &[(0, 0, 8, 8), (8, 8, 8, 8), (16, 16, 8, 8), (4, 12, 4, 4)] {
3377033852 let bstride = bw + 2 * super::CDEF_BORDER;
33771- let mut win = vec![0i32; bstride * (bh + 2 * super::CDEF_BORDER)];
33853+ let mut win = vec![0i16; bstride * (bh + 2 * super::CDEF_BORDER)];
3377233854 super::cdef_gather(&src, stride, tw, th, ox, oy, bw, bh, &mut win);
3377333855 for r in -(super::CDEF_BORDER as i32)..(bh + super::CDEF_BORDER) as i32 {
3377433856 for c in -(super::CDEF_BORDER as i32)..(bw + super::CDEF_BORDER) as i32 {
3377533857 let (ny, nx) = (oy as i32 + r, ox as i32 + c);
3377633858 let want = if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw {
33777- super::CDEF_VERY_LARGE
33859+ super::CDEF_VERY_LARGE as i16
3377833860 } else {
33779- i32::from(src[ny as usize * stride + nx as usize])
33861+ src[ny as usize * stride + nx as usize] as i16
3378033862 };
3378133863 let got = win[(r + super::CDEF_BORDER as i32) as usize * bstride
3378233864 + (c + super::CDEF_BORDER as i32) as usize];
@@ -33786,6 +33868,117 @@ mod tests {
3378633868 }
3378733869 }
3378833870
33871+ /// lane-cdef: the row-at-a-time direction search (and its AVX2 twin) must
33872+ /// be value-identical to a literal transcription of libaom
33873+ /// `cdef_find_dir_c`'s per-sample accumulation, over random windows at
33874+ /// every bit depth -- including the `CDEF_VERY_LARGE` sentinel rows a
33875+ /// crop-straddling 8x8 gathers.
33876+ #[test]
33877+ fn cdef_find_dir_matches_the_per_sample_reference() {
33878+ const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105];
33879+ fn reference(win: &[i16], bstride: usize, coeff_shift: i32) -> (usize, i32) {
33880+ let mut partial = [[0i64; 15]; 8];
33881+ for i in 0..8i64 {
33882+ for j in 0..8i64 {
33883+ let s = i64::from(
33884+ win[(i as usize + super::CDEF_BORDER) * bstride
33885+ + j as usize
33886+ + super::CDEF_BORDER],
33887+ );
33888+ let x = (s >> coeff_shift) - 128;
33889+ partial[0][(i + j) as usize] += x;
33890+ partial[1][(i + j / 2) as usize] += x;
33891+ partial[2][i as usize] += x;
33892+ partial[3][(3 + i - j / 2) as usize] += x;
33893+ partial[4][(7 + i - j) as usize] += x;
33894+ partial[5][(3 - i / 2 + j) as usize] += x;
33895+ partial[6][j as usize] += x;
33896+ partial[7][(i / 2 + j) as usize] += x;
33897+ }
33898+ }
33899+ let mut cost = [0i64; 8];
33900+ for i in 0..8usize {
33901+ cost[2] += partial[2][i] * partial[2][i];
33902+ cost[6] += partial[6][i] * partial[6][i];
33903+ }
33904+ cost[2] *= DIV_TABLE[8];
33905+ cost[6] *= DIV_TABLE[8];
33906+ for i in 0..7usize {
33907+ cost[0] += (partial[0][i] * partial[0][i]
33908+ + partial[0][14 - i] * partial[0][14 - i])
33909+ * DIV_TABLE[i + 1];
33910+ cost[4] += (partial[4][i] * partial[4][i]
33911+ + partial[4][14 - i] * partial[4][14 - i])
33912+ * DIV_TABLE[i + 1];
33913+ }
33914+ cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8];
33915+ cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8];
33916+ for i in (1..8).step_by(2) {
33917+ for j in 0..5usize {
33918+ cost[i] += partial[i][3 + j] * partial[i][3 + j];
33919+ }
33920+ cost[i] *= DIV_TABLE[8];
33921+ for j in 0..3usize {
33922+ cost[i] += (partial[i][j] * partial[i][j]
33923+ + partial[i][10 - j] * partial[i][10 - j])
33924+ * DIV_TABLE[2 * j + 2];
33925+ }
33926+ }
33927+ let (mut best_cost, mut best_dir) = (0i64, 0usize);
33928+ for (i, &c) in cost.iter().enumerate() {
33929+ if c > best_cost {
33930+ best_cost = c;
33931+ best_dir = i;
33932+ }
33933+ }
33934+ (best_dir, ((best_cost - cost[(best_dir + 4) & 7]) >> 10) as i32)
33935+ }
33936+ let mut rng = 0x0bad_c0de_dead_beefu64;
33937+ let mut next = move || {
33938+ rng ^= rng << 13;
33939+ rng ^= rng >> 7;
33940+ rng ^= rng << 17;
33941+ rng
33942+ };
33943+ let bstride = 8 + 2 * super::CDEF_BORDER;
33944+ for &(bit_depth, coeff_shift) in &[(8u8, 0i32), (10, 2), (12, 4)] {
33945+ let smax = (1u64 << bit_depth) - 1;
33946+ for trial in 0..256 {
33947+ let sentinels = trial % 4;
33948+ let mut win = [0i16; 256];
33949+ for (k, w) in win[..bstride * bstride].iter_mut().enumerate() {
33950+ let (r, c) = (k / bstride, k % bstride);
33951+ *w = if sentinels > 0
33952+ && (r < sentinels
33953+ || c < sentinels
33954+ || r + sentinels >= bstride
33955+ || c + sentinels >= bstride)
33956+ {
33957+ super::CDEF_VERY_LARGE as i16
33958+ } else {
33959+ (next() % (smax + 1)) as i16
33960+ };
33961+ }
33962+ let want = reference(&win, bstride, coeff_shift);
33963+ assert_eq!(
33964+ super::cdef_find_dir(&win, bstride, coeff_shift),
33965+ want,
33966+ "bd={bit_depth} trial={trial}"
33967+ );
33968+ let partial = super::cdef_find_dir_partials(&win, bstride, coeff_shift);
33969+ #[cfg(target_arch = "x86_64")]
33970+ if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {
33971+ // SAFETY: the avx2 arm was probed above.
33972+ #[allow(unsafe_code)]
33973+ let simd = unsafe {
33974+ super::cdef_simd::find_dir_partials_avx2(&win, bstride, coeff_shift)
33975+ };
33976+ assert_eq!(simd, partial, "bd={bit_depth} trial={trial} partials");
33977+ }
33978+ }
33979+ }
33980+ }
33981+
3378933982 /// lane-perf7 step 1: the AVX2 CDEF kernel must be value-identical to
3379033983 /// [`super::cdef_filter_block_scalar`] over the whole input domain --
3379133984 /// every direction, every enable pair, both block shapes, the 8/10/12-bit
@@ -33816,7 +34009,10 @@ mod tests {
3381634009 // Every fourth window seeds sentinels, in a growing
3381734010 // border ring, so the straddling-block case is covered.
3381834011 let sentinels = trial % 4;
33819- let win: Vec<i32> = (0..rows * bstride)
34012+ // 256 entries, as `cdef_band`'s own scratch is: the
34013+ // kernel taps the window in place and reads past the last
34014+ // sample into the zeroed tail.
34015+ let mut win: Vec<i16> = (0..rows * bstride)
3382034016 .map(|k| {
3382134017 let (r, c) = (k / bstride, k % bstride);
3382234018 if sentinels > 0
@@ -33825,12 +34021,13 @@ mod tests {
3382534021 || r + sentinels >= rows
3382634022 || c + sentinels >= bstride)
3382734023 {
33828- super::CDEF_VERY_LARGE
34024+ super::CDEF_VERY_LARGE as i16
3382934025 } else {
33830- (next() % (smax as u64 + 1)) as i32
34026+ (next() % (smax as u64 + 1)) as i16
3383134027 }
3383234028 })
3383334029 .collect();
34030+ win.resize(256, 0);
3383434031 for dir in 0..8usize {
3383534032 for &pri in &[0i32, 1, 3, 15] {
3383634033 for &sec in &[0i32, 1, 2, 4] {