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(
15539 oy: usize,15539 oy: usize,
15540 bw: usize,15540 bw: usize,
15541 bh: usize,15541 bh: usize,
15542- buf: &mut [i32],15542+ buf: &mut [i16],
15543 ) {15543 ) {
15544 let bstride = bw + 2 * CDEF_BORDER;15544 let bstride = bw + 2 * CDEF_BORDER;
15545 let rows = bh + 2 * CDEF_BORDER;15545 let rows = bh + 2 * CDEF_BORDER;
@@ -15555,7 +15555,7 @@ fn cdef_gather(
15555 let src_row = &src[s0..s0 + bstride];15555 let src_row = &src[s0..s0 + bstride];
15556 let dst_row = &mut buf[r * bstride..r * bstride + bstride];15556 let dst_row = &mut buf[r * bstride..r * bstride + bstride];
15557 for (d, &v) in dst_row.iter_mut().zip(src_row) {15557 for (d, &v) in dst_row.iter_mut().zip(src_row) {
15558- *d = i32::from(v);15558+ *d = v as i16;
15559 }15559 }
15560 }15560 }
15561 return;15561 return;
@@ -15566,58 +15566,88 @@ fn cdef_gather(
15566 let nx = ox as isize + c as isize - CDEF_BORDER as isize;15566 let nx = ox as isize + c as isize - CDEF_BORDER as isize;
15567 buf[r * bstride + c] =15567 buf[r * bstride + c] =
15568 if ny < 0 || nx < 0 || ny as usize >= true_h || nx as usize >= true_w {15568 if ny < 0 || nx < 0 || ny as usize >= true_h || nx as usize >= true_w {
15569- CDEF_VERY_LARGE15569+ CDEF_VERY_LARGE as i16
15570 } else {15570 } else {
15571- i32::from(src[ny as usize * stride + nx as usize])15571+ src[ny as usize * stride + nx as usize] as i16
15572 };15572 };
15573 }15573 }
15574 }15574 }
15575 }15575 }
15576 15576
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+
15577 /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant15611 /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant
15578 /// direction of an 8x8 luma window and the variance gap between it and its15612 /// 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.15613+/// orthogonal, reading [`cdef_gather`]'s window (row stride `bstride`, the
15580-fn cdef_find_dir(coeff_shift: i32, sample: impl Fn(usize, usize) -> i32) -> (usize, i32) {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) {
15581 const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105];15616 const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105];
15582- let mut partial = [[0i64; 15]; 8];15617+ #[cfg(target_arch = "x86_64")]
15583- for i in 0..8i64 {15618+ let partial = if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {
15584- for j in 0..8i64 {15619+ // SAFETY: reached only when the CPU reports avx2; the kernel reads
15585- // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before15620+ // the same eight rows of eight samples the scalar form does.
15586- // accumulating, same as `constrain`'s threshold shift below.15621+ #[allow(unsafe_code)]
15587- let x = i64::from(sample(i as usize, j as usize) >> coeff_shift) - 128;15622+ unsafe {
15588- partial[0][(i + j) as usize] += x;15623+ cdef_simd::find_dir_partials_avx2(win, bstride, coeff_shift)
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;
15596 }15624 }
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) };
15598 let mut cost = [0i64; 8];15631 let mut cost = [0i64; 8];
15599 for i in 0..8usize {15632 for i in 0..8usize {
15600- cost[2] += partial[2][i] * partial[2][i];15633+ cost[2] += sq(partial[2][i]);
15601- cost[6] += partial[6][i] * partial[6][i];15634+ cost[6] += sq(partial[6][i]);
15602 }15635 }
15603 cost[2] *= DIV_TABLE[8];15636 cost[2] *= DIV_TABLE[8];
15604 cost[6] *= DIV_TABLE[8];15637 cost[6] *= DIV_TABLE[8];
15605 for i in 0..7usize {15638 for i in 0..7usize {
15606- cost[0] += (partial[0][i] * partial[0][i] + partial[0][14 - i] * partial[0][14 - i])15639+ cost[0] += (sq(partial[0][i]) + sq(partial[0][14 - i])) * DIV_TABLE[i + 1];
15607- * DIV_TABLE[i + 1];15640+ cost[4] += (sq(partial[4][i]) + sq(partial[4][14 - i])) * 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];
15610 }15641 }
15611- cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8];15642+ cost[0] += sq(partial[0][7]) * DIV_TABLE[8];
15612- cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8];15643+ cost[4] += sq(partial[4][7]) * DIV_TABLE[8];
15613 for i in (1..8).step_by(2) {15644 for i in (1..8).step_by(2) {
15614 for j in 0..5usize {15645 for j in 0..5usize {
15615- cost[i] += partial[i][3 + j] * partial[i][3 + j];15646+ cost[i] += sq(partial[i][3 + j]);
15616 }15647 }
15617 cost[i] *= DIV_TABLE[8];15648 cost[i] *= DIV_TABLE[8];
15618 for j in 0..3usize {15649 for j in 0..3usize {
15619- cost[i] += (partial[i][j] * partial[i][j] + partial[i][10 - j] * partial[i][10 - j])15650+ cost[i] += (sq(partial[i][j]) + sq(partial[i][10 - j])) * DIV_TABLE[2 * j + 2];
15620- * DIV_TABLE[2 * j + 2];
15621 }15651 }
15622 }15652 }
15623 let mut best_cost = 0i64;15653 let mut best_cost = 0i64;
@@ -15653,7 +15683,7 @@ fn cdef_adjust_strength(strength: i32, var: i32) -> i32 {
15653 #[allow(clippy::too_many_arguments)]15683 #[allow(clippy::too_many_arguments)]
15654 #[allow(unsafe_code)]15684 #[allow(unsafe_code)]
15655 fn cdef_filter_block(15685 fn cdef_filter_block(
15656- src: &[i32],15686+ src: &[i16],
15657 dst: &mut PlaneBuf<'static>,15687 dst: &mut PlaneBuf<'static>,
15658 ox: usize,15688 ox: usize,
15659 oy: usize,15689 oy: usize,
@@ -15722,7 +15752,7 @@ fn cdef_filter_block(
15722 /// `cdef_simd_kernel_matches_the_scalar_filter` checks the kernel against.15752 /// `cdef_simd_kernel_matches_the_scalar_filter` checks the kernel against.
15723 #[allow(clippy::too_many_arguments)]15753 #[allow(clippy::too_many_arguments)]
15724 fn cdef_filter_block_scalar(15754 fn cdef_filter_block_scalar(
15725- src: &[i32],15755+ src: &[i16],
15726 dst: &mut [u16],15756 dst: &mut [u16],
15727 stride: usize,15757 stride: usize,
15728 ox: usize,15758 ox: usize,
@@ -15753,7 +15783,7 @@ fn cdef_filter_block_scalar(
15753 // `src` is [`cdef_gather`]'s window: (i, j) in block coordinates, taps15783 // `src` is [`cdef_gather`]'s window: (i, j) in block coordinates, taps
15754 // reaching +-CDEF_BORDER, so every index below is in range by construction.15784 // reaching +-CDEF_BORDER, so every index below is in range by construction.
15755 let at = |i: i32, j: i32| -> i32 {15785 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])
15757 };15787 };
15758 let (pri_dir, sec_dir0, sec_dir1) =15788 let (pri_dir, sec_dir0, sec_dir1) =
15759 (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]);15789 (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]);
@@ -15863,6 +15893,60 @@ mod cdef_simd {
15863 _mm256_sign_epi16(t, diff)15893 _mm256_sign_epi16(t, diff)
15864 }15894 }
15865 15895
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+
15866 /// [`super::cdef_filter_block_scalar`]'s arithmetic, two rows per15950 /// [`super::cdef_filter_block_scalar`]'s arithmetic, two rows per
15867 /// iteration. `bw` is 8 (luma) or 4 (4:2:0 chroma) and `bh` matches; for15951 /// iteration. `bw` is 8 (luma) or 4 (4:2:0 chroma) and `bh` matches; for
15868 /// `bw == 4` the upper four lanes of each half are window samples read15952 /// `bw == 4` the upper four lanes of each half are window samples read
@@ -15870,12 +15954,14 @@ mod cdef_simd {
15870 /// computed and then dropped by the 64-bit store.15954 /// computed and then dropped by the 64-bit store.
15871 ///15955 ///
15872 /// # Safety15956 /// # Safety
15873- /// Requires AVX2. `src` must hold the `(bw + 4) x (bh + 4)` window and15957+ /// Requires AVX2. `src` must be [`super::cdef_gather`]'s 256-entry window
15874- /// `dst` the `bh` rows of `bw` samples at `(ox, oy)`.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)`.
15875 #[target_feature(enable = "avx2")]15961 #[target_feature(enable = "avx2")]
15876 #[allow(clippy::too_many_arguments)]15962 #[allow(clippy::too_many_arguments)]
15877 pub unsafe fn filter_block_avx2(15963 pub unsafe fn filter_block_avx2(
15878- src: &[i32],15964+ src: &[i16],
15879 dst: &mut [u16],15965 dst: &mut [u16],
15880 stride: usize,15966 stride: usize,
15881 ox: usize,15967 ox: usize,
@@ -15893,14 +15979,11 @@ mod cdef_simd {
15893 smax: i32,15979 smax: i32,
15894 ) {15980 ) {
15895 let bstride = bw + 2 * CDEF_BORDER;15981 let bstride = bw + 2 * CDEF_BORDER;
15896- // The window as `i16`, padded so every two-row tap load stays inside15982+ // The caller's window is already `i16` and 256 entries long, so the
15897- // it (largest read: (bh + 3) * bstride + 8 <= 152 for the 8x8 luma15983+ // kernel taps it in place: the largest two-row load is
15898- // block, 72 for the 4x4 chroma one).15984+ // (bh + 3) * bstride + 8 <= 152 for the 8x8 luma block, 72 for the
15899- let mut w = [0i16; 256];15985+ // 4x4 chroma one, both inside the buffer's zeroed tail.
15900- let rows = bh + 2 * CDEF_BORDER;15986+ let w = src;
15901- for (d, &v) in w[..rows * bstride].iter_mut().zip(&src[..rows * bstride]) {
15902- *d = v as i16;
15903- }
15904 let clipping_required = enable_primary && enable_secondary;15987 let clipping_required = enable_primary && enable_secondary;
15905 let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize];15988 let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize];
15906 let pri_shift = _mm_cvtsi32_si128(cdef_constrain_shift(pri_strength.max(1), pri_damping));15989 let pri_shift = _mm_cvtsi32_si128(cdef_constrain_shift(pri_strength.max(1), pri_damping));
@@ -18801,8 +18884,11 @@ fn cdef_band(
18801 };18884 };
18802 // One scratch window per plane kind, reused by every unit (8x8 luma and18885 // One scratch window per plane kind, reused by every unit (8x8 luma and
18803 // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring).18886 // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring).
18804- let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)];18887+ // 256 entries, not the (bw + 4) x (bh + 4) samples themselves: the
18805- let mut win_uv = [0i32; (4 + 2 * CDEF_BORDER) * (4 + 2 * CDEF_BORDER)];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];
18806 let mut mi_r = r_lo;18892 let mut mi_r = r_lo;
18807 while mi_r < r_hi {18893 while mi_r < r_hi {
18808 let mut mi_c = 0usize;18894 let mut mi_c = 0usize;
@@ -18826,10 +18912,6 @@ fn cdef_band(
18826 }18912 }
18827 let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height);18913 let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height);
18828 cdef_gather(src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y);18914 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- };
18833 // lane-cdef r1: the direction search reads the SAME padded18915 // lane-cdef r1: the direction search reads the SAME padded
18834 // block the taps do -- libaom runs `cdef_find_dir` on the18916 // block the taps do -- libaom runs `cdef_find_dir` on the
18835 // prepared filter block (`cdef_block.c:296-320` over18917 // prepared filter block (`cdef_block.c:296-320` over
@@ -18838,7 +18920,7 @@ fn cdef_band(
18838 // Clamping to the last real sample instead made every straddling18920 // Clamping to the last real sample instead made every straddling
18839 // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence18921 // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence
18840 // a different `adjust_strength`, hence a +-1 band at the crop edge.18922 // 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);
18842 18924
18843 // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0`18925 // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0`
18844 // -- each plane zeroes the shared direction when *its own*18926 // -- each plane zeroes the shared direction when *its own*
@@ -33798,15 +33880,15 @@ mod tests {
33798 let src: Vec<u16> = (0..stride * 24).map(|i| (i * 7 % 1024) as u16).collect();33880 let src: Vec<u16> = (0..stride * 24).map(|i| (i * 7 % 1024) as u16).collect();
33799 for &(ox, oy, bw, bh) in &[(0, 0, 8, 8), (8, 8, 8, 8), (16, 16, 8, 8), (4, 12, 4, 4)] {33881 for &(ox, oy, bw, bh) in &[(0, 0, 8, 8), (8, 8, 8, 8), (16, 16, 8, 8), (4, 12, 4, 4)] {
33800 let bstride = bw + 2 * super::CDEF_BORDER;33882 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)];
33802 super::cdef_gather(&src, stride, tw, th, ox, oy, bw, bh, &mut win);33884 super::cdef_gather(&src, stride, tw, th, ox, oy, bw, bh, &mut win);
33803 for r in -(super::CDEF_BORDER as i32)..(bh + super::CDEF_BORDER) as i32 {33885 for r in -(super::CDEF_BORDER as i32)..(bh + super::CDEF_BORDER) as i32 {
33804 for c in -(super::CDEF_BORDER as i32)..(bw + super::CDEF_BORDER) as i32 {33886 for c in -(super::CDEF_BORDER as i32)..(bw + super::CDEF_BORDER) as i32 {
33805 let (ny, nx) = (oy as i32 + r, ox as i32 + c);33887 let (ny, nx) = (oy as i32 + r, ox as i32 + c);
33806 let want = if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw {33888 let want = if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw {
33807- super::CDEF_VERY_LARGE33889+ super::CDEF_VERY_LARGE as i16
33808 } else {33890 } else {
33809- i32::from(src[ny as usize * stride + nx as usize])33891+ src[ny as usize * stride + nx as usize] as i16
33810 };33892 };
33811 let got = win[(r + super::CDEF_BORDER as i32) as usize * bstride33893 let got = win[(r + super::CDEF_BORDER as i32) as usize * bstride
33812 + (c + super::CDEF_BORDER as i32) as usize];33894 + (c + super::CDEF_BORDER as i32) as usize];
@@ -33816,6 +33898,117 @@ mod tests {
33816 }33898 }
33817 }33899 }
33818 33900
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+
33819 /// lane-perf7 step 1: the AVX2 CDEF kernel must be value-identical to34012 /// lane-perf7 step 1: the AVX2 CDEF kernel must be value-identical to
33820 /// [`super::cdef_filter_block_scalar`] over the whole input domain --34013 /// [`super::cdef_filter_block_scalar`] over the whole input domain --
33821 /// every direction, every enable pair, both block shapes, the 8/10/12-bit34014 /// every direction, every enable pair, both block shapes, the 8/10/12-bit
@@ -33846,7 +34039,10 @@ mod tests {
33846 // Every fourth window seeds sentinels, in a growing34039 // Every fourth window seeds sentinels, in a growing
33847 // border ring, so the straddling-block case is covered.34040 // border ring, so the straddling-block case is covered.
33848 let sentinels = trial % 4;34041 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)
33850 .map(|k| {34046 .map(|k| {
33851 let (r, c) = (k / bstride, k % bstride);34047 let (r, c) = (k / bstride, k % bstride);
33852 if sentinels > 034048 if sentinels > 0
@@ -33855,12 +34051,13 @@ mod tests {
33855 || r + sentinels >= rows34051 || r + sentinels >= rows
33856 || c + sentinels >= bstride)34052 || c + sentinels >= bstride)
33857 {34053 {
33858- super::CDEF_VERY_LARGE34054+ super::CDEF_VERY_LARGE as i16
33859 } else {34055 } else {
33860- (next() % (smax as u64 + 1)) as i3234056+ (next() % (smax as u64 + 1)) as i16
33861 }34057 }
33862 })34058 })
33863 .collect();34059 .collect();
34060+ win.resize(256, 0);
33864 for dir in 0..8usize {34061 for dir in 0..8usize {
33865 for &pri in &[0i32, 1, 3, 15] {34062 for &pri in &[0i32, 1, 3, 15] {
33866 for &sec in &[0i32, 1, 2, 4] {34063 for &sec in &[0i32, 1, 2, 4] {