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(
15510 oy: usize,15510 oy: usize,
15511 bw: usize,15511 bw: usize,
15512 bh: usize,15512 bh: usize,
15513- buf: &mut [i32],15513+ buf: &mut [i16],
15514 ) {15514 ) {
15515 let bstride = bw + 2 * CDEF_BORDER;15515 let bstride = bw + 2 * CDEF_BORDER;
15516 let rows = bh + 2 * CDEF_BORDER;15516 let rows = bh + 2 * CDEF_BORDER;
@@ -15526,7 +15526,7 @@ fn cdef_gather(
15526 let src_row = &src[s0..s0 + bstride];15526 let src_row = &src[s0..s0 + bstride];
15527 let dst_row = &mut buf[r * bstride..r * bstride + bstride];15527 let dst_row = &mut buf[r * bstride..r * bstride + bstride];
15528 for (d, &v) in dst_row.iter_mut().zip(src_row) {15528 for (d, &v) in dst_row.iter_mut().zip(src_row) {
15529- *d = i32::from(v);15529+ *d = v as i16;
15530 }15530 }
15531 }15531 }
15532 return;15532 return;
@@ -15537,58 +15537,88 @@ fn cdef_gather(
15537 let nx = ox as isize + c as isize - CDEF_BORDER as isize;15537 let nx = ox as isize + c as isize - CDEF_BORDER as isize;
15538 buf[r * bstride + c] =15538 buf[r * bstride + c] =
15539 if ny < 0 || nx < 0 || ny as usize >= true_h || nx as usize >= true_w {15539 if ny < 0 || nx < 0 || ny as usize >= true_h || nx as usize >= true_w {
15540- CDEF_VERY_LARGE15540+ CDEF_VERY_LARGE as i16
15541 } else {15541 } else {
15542- i32::from(src[ny as usize * stride + nx as usize])15542+ src[ny as usize * stride + nx as usize] as i16
15543 };15543 };
15544 }15544 }
15545 }15545 }
15546 }15546 }
15547 15547
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+
15548 /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant15582 /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant
15549 /// direction of an 8x8 luma window and the variance gap between it and its15583 /// 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.15584+/// orthogonal, reading [`cdef_gather`]'s window (row stride `bstride`, the
15551-fn cdef_find_dir(coeff_shift: i32, sample: impl Fn(usize, usize) -> i32) -> (usize, i32) {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) {
15552 const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105];15587 const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105];
15553- let mut partial = [[0i64; 15]; 8];15588+ #[cfg(target_arch = "x86_64")]
15554- for i in 0..8i64 {15589+ let partial = if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 {
15555- for j in 0..8i64 {15590+ // SAFETY: reached only when the CPU reports avx2; the kernel reads
15556- // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before15591+ // the same eight rows of eight samples the scalar form does.
15557- // accumulating, same as `constrain`'s threshold shift below.15592+ #[allow(unsafe_code)]
15558- let x = i64::from(sample(i as usize, j as usize) >> coeff_shift) - 128;15593+ unsafe {
15559- partial[0][(i + j) as usize] += x;15594+ cdef_simd::find_dir_partials_avx2(win, bstride, coeff_shift)
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;
15567- }
15568 }15595 }
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) };
15569 let mut cost = [0i64; 8];15602 let mut cost = [0i64; 8];
15570 for i in 0..8usize {15603 for i in 0..8usize {
15571- cost[2] += partial[2][i] * partial[2][i];15604+ cost[2] += sq(partial[2][i]);
15572- cost[6] += partial[6][i] * partial[6][i];15605+ cost[6] += sq(partial[6][i]);
15573 }15606 }
15574 cost[2] *= DIV_TABLE[8];15607 cost[2] *= DIV_TABLE[8];
15575 cost[6] *= DIV_TABLE[8];15608 cost[6] *= DIV_TABLE[8];
15576 for i in 0..7usize {15609 for i in 0..7usize {
15577- cost[0] += (partial[0][i] * partial[0][i] + partial[0][14 - i] * partial[0][14 - i])15610+ cost[0] += (sq(partial[0][i]) + sq(partial[0][14 - i])) * DIV_TABLE[i + 1];
15578- * DIV_TABLE[i + 1];15611+ cost[4] += (sq(partial[4][i]) + sq(partial[4][14 - i])) * 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];
15581 }15612 }
15582- cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8];15613+ cost[0] += sq(partial[0][7]) * DIV_TABLE[8];
15583- cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8];15614+ cost[4] += sq(partial[4][7]) * DIV_TABLE[8];
15584 for i in (1..8).step_by(2) {15615 for i in (1..8).step_by(2) {
15585 for j in 0..5usize {15616 for j in 0..5usize {
15586- cost[i] += partial[i][3 + j] * partial[i][3 + j];15617+ cost[i] += sq(partial[i][3 + j]);
15587 }15618 }
15588 cost[i] *= DIV_TABLE[8];15619 cost[i] *= DIV_TABLE[8];
15589 for j in 0..3usize {15620 for j in 0..3usize {
15590- cost[i] += (partial[i][j] * partial[i][j] + partial[i][10 - j] * partial[i][10 - j])15621+ cost[i] += (sq(partial[i][j]) + sq(partial[i][10 - j])) * DIV_TABLE[2 * j + 2];
15591- * DIV_TABLE[2 * j + 2];
15592 }15622 }
15593 }15623 }
15594 let mut best_cost = 0i64;15624 let mut best_cost = 0i64;
@@ -15624,7 +15654,7 @@ fn cdef_adjust_strength(strength: i32, var: i32) -> i32 {
15624 #[allow(clippy::too_many_arguments)]15654 #[allow(clippy::too_many_arguments)]
15625 #[allow(unsafe_code)]15655 #[allow(unsafe_code)]
15626 fn cdef_filter_block(15656 fn cdef_filter_block(
15627- src: &[i32],15657+ src: &[i16],
15628 dst: &mut PlaneBuf<'static>,15658 dst: &mut PlaneBuf<'static>,
15629 ox: usize,15659 ox: usize,
15630 oy: usize,15660 oy: usize,
@@ -15693,7 +15723,7 @@ fn cdef_filter_block(
15693 /// `cdef_simd_kernel_matches_the_scalar_filter` checks the kernel against.15723 /// `cdef_simd_kernel_matches_the_scalar_filter` checks the kernel against.
15694 #[allow(clippy::too_many_arguments)]15724 #[allow(clippy::too_many_arguments)]
15695 fn cdef_filter_block_scalar(15725 fn cdef_filter_block_scalar(
15696- src: &[i32],15726+ src: &[i16],
15697 dst: &mut [u16],15727 dst: &mut [u16],
15698 stride: usize,15728 stride: usize,
15699 ox: usize,15729 ox: usize,
@@ -15724,7 +15754,7 @@ fn cdef_filter_block_scalar(
15724 // `src` is [`cdef_gather`]'s window: (i, j) in block coordinates, taps15754 // `src` is [`cdef_gather`]'s window: (i, j) in block coordinates, taps
15725 // reaching +-CDEF_BORDER, so every index below is in range by construction.15755 // reaching +-CDEF_BORDER, so every index below is in range by construction.
15726 let at = |i: i32, j: i32| -> i32 {15756 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])
15728 };15758 };
15729 let (pri_dir, sec_dir0, sec_dir1) =15759 let (pri_dir, sec_dir0, sec_dir1) =
15730 (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]);15760 (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]);
@@ -15834,6 +15864,60 @@ mod cdef_simd {
15834 _mm256_sign_epi16(t, diff)15864 _mm256_sign_epi16(t, diff)
15835 }15865 }
15836 15866
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+
15837 /// [`super::cdef_filter_block_scalar`]'s arithmetic, two rows per15921 /// [`super::cdef_filter_block_scalar`]'s arithmetic, two rows per
15838 /// iteration. `bw` is 8 (luma) or 4 (4:2:0 chroma) and `bh` matches; for15922 /// iteration. `bw` is 8 (luma) or 4 (4:2:0 chroma) and `bh` matches; for
15839 /// `bw == 4` the upper four lanes of each half are window samples read15923 /// `bw == 4` the upper four lanes of each half are window samples read
@@ -15841,12 +15925,14 @@ mod cdef_simd {
15841 /// computed and then dropped by the 64-bit store.15925 /// computed and then dropped by the 64-bit store.
15842 ///15926 ///
15843 /// # Safety15927 /// # Safety
15844- /// Requires AVX2. `src` must hold the `(bw + 4) x (bh + 4)` window and15928+ /// Requires AVX2. `src` must be [`super::cdef_gather`]'s 256-entry window
15845- /// `dst` the `bh` rows of `bw` samples at `(ox, oy)`.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)`.
15846 #[target_feature(enable = "avx2")]15932 #[target_feature(enable = "avx2")]
15847 #[allow(clippy::too_many_arguments)]15933 #[allow(clippy::too_many_arguments)]
15848 pub unsafe fn filter_block_avx2(15934 pub unsafe fn filter_block_avx2(
15849- src: &[i32],15935+ src: &[i16],
15850 dst: &mut [u16],15936 dst: &mut [u16],
15851 stride: usize,15937 stride: usize,
15852 ox: usize,15938 ox: usize,
@@ -15864,14 +15950,11 @@ mod cdef_simd {
15864 smax: i32,15950 smax: i32,
15865 ) {15951 ) {
15866 let bstride = bw + 2 * CDEF_BORDER;15952 let bstride = bw + 2 * CDEF_BORDER;
15867- // The window as `i16`, padded so every two-row tap load stays inside15953+ // The caller's window is already `i16` and 256 entries long, so the
15868- // it (largest read: (bh + 3) * bstride + 8 <= 152 for the 8x8 luma15954+ // kernel taps it in place: the largest two-row load is
15869- // block, 72 for the 4x4 chroma one).15955+ // (bh + 3) * bstride + 8 <= 152 for the 8x8 luma block, 72 for the
15870- let mut w = [0i16; 256];15956+ // 4x4 chroma one, both inside the buffer's zeroed tail.
15871- let rows = bh + 2 * CDEF_BORDER;15957+ let w = src;
15872- for (d, &v) in w[..rows * bstride].iter_mut().zip(&src[..rows * bstride]) {
15873- *d = v as i16;
15874- }
15875 let clipping_required = enable_primary && enable_secondary;15958 let clipping_required = enable_primary && enable_secondary;
15876 let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize];15959 let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize];
15877 let pri_shift = _mm_cvtsi32_si128(cdef_constrain_shift(pri_strength.max(1), pri_damping));15960 let pri_shift = _mm_cvtsi32_si128(cdef_constrain_shift(pri_strength.max(1), pri_damping));
@@ -18772,8 +18855,11 @@ fn cdef_band(
18772 };18855 };
18773 // One scratch window per plane kind, reused by every unit (8x8 luma and18856 // One scratch window per plane kind, reused by every unit (8x8 luma and
18774 // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring).18857 // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring).
18775- let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)];18858+ // 256 entries, not the (bw + 4) x (bh + 4) samples themselves: the
18776- let mut win_uv = [0i32; (4 + 2 * CDEF_BORDER) * (4 + 2 * CDEF_BORDER)];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];
18777 let mut mi_r = r_lo;18863 let mut mi_r = r_lo;
18778 while mi_r < r_hi {18864 while mi_r < r_hi {
18779 let mut mi_c = 0usize;18865 let mut mi_c = 0usize;
@@ -18797,10 +18883,6 @@ fn cdef_band(
18797 }18883 }
18798 let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height);18884 let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height);
18799 cdef_gather(src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y);18885 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- };
18804 // lane-cdef r1: the direction search reads the SAME padded18886 // lane-cdef r1: the direction search reads the SAME padded
18805 // block the taps do -- libaom runs `cdef_find_dir` on the18887 // block the taps do -- libaom runs `cdef_find_dir` on the
18806 // prepared filter block (`cdef_block.c:296-320` over18888 // prepared filter block (`cdef_block.c:296-320` over
@@ -18809,7 +18891,7 @@ fn cdef_band(
18809 // Clamping to the last real sample instead made every straddling18891 // Clamping to the last real sample instead made every straddling
18810 // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence18892 // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence
18811 // a different `adjust_strength`, hence a +-1 band at the crop edge.18893 // 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);
18813 18895
18814 // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0`18896 // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0`
18815 // -- each plane zeroes the shared direction when *its own*18897 // -- each plane zeroes the shared direction when *its own*
@@ -33768,15 +33850,15 @@ mod tests {
33768 let src: Vec<u16> = (0..stride * 24).map(|i| (i * 7 % 1024) as u16).collect();33850 let src: Vec<u16> = (0..stride * 24).map(|i| (i * 7 % 1024) as u16).collect();
33769 for &(ox, oy, bw, bh) in &[(0, 0, 8, 8), (8, 8, 8, 8), (16, 16, 8, 8), (4, 12, 4, 4)] {33851 for &(ox, oy, bw, bh) in &[(0, 0, 8, 8), (8, 8, 8, 8), (16, 16, 8, 8), (4, 12, 4, 4)] {
33770 let bstride = bw + 2 * super::CDEF_BORDER;33852 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)];
33772 super::cdef_gather(&src, stride, tw, th, ox, oy, bw, bh, &mut win);33854 super::cdef_gather(&src, stride, tw, th, ox, oy, bw, bh, &mut win);
33773 for r in -(super::CDEF_BORDER as i32)..(bh + super::CDEF_BORDER) as i32 {33855 for r in -(super::CDEF_BORDER as i32)..(bh + super::CDEF_BORDER) as i32 {
33774 for c in -(super::CDEF_BORDER as i32)..(bw + super::CDEF_BORDER) as i32 {33856 for c in -(super::CDEF_BORDER as i32)..(bw + super::CDEF_BORDER) as i32 {
33775 let (ny, nx) = (oy as i32 + r, ox as i32 + c);33857 let (ny, nx) = (oy as i32 + r, ox as i32 + c);
33776 let want = if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw {33858 let want = if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw {
33777- super::CDEF_VERY_LARGE33859+ super::CDEF_VERY_LARGE as i16
33778 } else {33860 } else {
33779- i32::from(src[ny as usize * stride + nx as usize])33861+ src[ny as usize * stride + nx as usize] as i16
33780 };33862 };
33781 let got = win[(r + super::CDEF_BORDER as i32) as usize * bstride33863 let got = win[(r + super::CDEF_BORDER as i32) as usize * bstride
33782 + (c + super::CDEF_BORDER as i32) as usize];33864 + (c + super::CDEF_BORDER as i32) as usize];
@@ -33786,6 +33868,117 @@ mod tests {
33786 }33868 }
33787 }33869 }
33788 33870
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+
33789 /// lane-perf7 step 1: the AVX2 CDEF kernel must be value-identical to33982 /// lane-perf7 step 1: the AVX2 CDEF kernel must be value-identical to
33790 /// [`super::cdef_filter_block_scalar`] over the whole input domain --33983 /// [`super::cdef_filter_block_scalar`] over the whole input domain --
33791 /// every direction, every enable pair, both block shapes, the 8/10/12-bit33984 /// every direction, every enable pair, both block shapes, the 8/10/12-bit
@@ -33816,7 +34009,10 @@ mod tests {
33816 // Every fourth window seeds sentinels, in a growing34009 // Every fourth window seeds sentinels, in a growing
33817 // border ring, so the straddling-block case is covered.34010 // border ring, so the straddling-block case is covered.
33818 let sentinels = trial % 4;34011 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)
33820 .map(|k| {34016 .map(|k| {
33821 let (r, c) = (k / bstride, k % bstride);34017 let (r, c) = (k / bstride, k % bstride);
33822 if sentinels > 034018 if sentinels > 0
@@ -33825,12 +34021,13 @@ mod tests {
33825 || r + sentinels >= rows34021 || r + sentinels >= rows
33826 || c + sentinels >= bstride)34022 || c + sentinels >= bstride)
33827 {34023 {
33828- super::CDEF_VERY_LARGE34024+ super::CDEF_VERY_LARGE as i16
33829 } else {34025 } else {
33830- (next() % (smax as u64 + 1)) as i3234026+ (next() % (smax as u64 + 1)) as i16
33831 }34027 }
33832 })34028 })
33833 .collect();34029 .collect();
34030+ win.resize(256, 0);
33834 for dir in 0..8usize {34031 for dir in 0..8usize {
33835 for &pri in &[0i32, 1, 3, 15] {34032 for &pri in &[0i32, 1, 3, 15] {
33836 for &sec in &[0i32, 1, 2, 4] {34033 for &sec in &[0i32, 1, 2, 4] {