forge
Explore
Log in

Don't you dare go Hollow

The compound vertical pass runs the whole block per call

Browse files at this commit

f2203de91448593e589b54bd81fa0353d70ea099Ahmet Kaan GümüşParent: 3702d63
  • perf
    • the compound vertical pass runs the whole block per call
Show full message
perf(av1): the compound vertical pass runs the whole block per call

vpass_row takes the block's row count too, and the compound kernel
accumulates straight into its i32 destination and rounds it in one
sweep, dropping the per-row accumulator round trip along with the
per-row tap setup. Byte-exact: all four gate streams hash to their
references.
crates/ec-av1/src/mc.rsmodified+33 -27
@@ -456,14 +456,15 @@ mod simd {
456456 intermediate: &[i16],
457457 block_w: usize,
458458 src_stride: usize,
459- row: usize,
459+ rows: usize,
460460 taps: &[i32; 8],
461461 acc: &mut [i32],
462462 ) {
463- debug_assert!(intermediate.len() >= (row + 7) * src_stride + block_w && acc.len() >= block_w);
463+ debug_assert!(intermediate.len() >= (rows + 6) * src_stride + block_w && acc.len() >= rows * block_w);
464464 let tp = tap_pairs(&taps16(taps));
465+ for row in 0..rows {
465466 let ip = intermediate.as_ptr().add(row * src_stride);
466- let ap = acc.as_mut_ptr();
467+ let ap = acc.as_mut_ptr().add(row * block_w);
467468 let mut c = 0usize;
468469 while c + 16 <= block_w {
469470 // `lo` accumulates columns c+0..4 and c+8..12 (one per 128-bit
@@ -486,6 +487,7 @@ mod simd {
486487 c += 16;
487488 }
488489 vpass_tail_sse(ip, block_w, src_stride, &tp, taps, ap, c);
490+ }
489491 }
490492
491493 /// [`vpass_avx2`]'s 8- and 4-column steps, 128-bit: every AV1 block width
@@ -693,13 +695,18 @@ mod simd {
693695 intermediate: &[i16],
694696 block_w: usize,
695697 src_stride: usize,
696- row: usize,
698+ rows: usize,
697699 taps: &[i32; 8],
698700 acc: &mut [i32],
699701 ) {
700- debug_assert!(intermediate.len() >= (row + 7) * src_stride + block_w && acc.len() >= block_w);
702+ debug_assert!(intermediate.len() >= (rows + 6) * src_stride + block_w && acc.len() >= rows * block_w);
701703 let tp = tap_pairs(&taps16(taps));
702- vpass_tail_sse(intermediate.as_ptr().add(row * src_stride), block_w, src_stride, &tp, taps, acc.as_mut_ptr(), 0);
704+ for row in 0..rows {
705+ vpass_tail_sse(
706+ intermediate.as_ptr().add(row * src_stride), block_w, src_stride, &tp, taps,
707+ acc.as_mut_ptr().add(row * block_w), 0,
708+ );
709+ }
703710 }
704711
705712 /// Horizontal pass, SSE2: [`hpass_avx2`]'s scheme at 8 outputs per
@@ -874,7 +881,7 @@ fn horizontal_pass_unscaled(
874881 /// eight slots. `acc` is left holding the unrounded sums.
875882 #[inline]
876883 #[allow(unsafe_code)]
877-pub(crate) fn vpass_row(intermediate: &[i16], block_w: usize, src_stride: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) {
884+pub(crate) fn vpass_row(intermediate: &[i16], block_w: usize, src_stride: usize, rows: usize, taps: &[i32; 8], acc: &mut [i32]) {
878885 #[cfg(target_arch = "x86_64")]
879886 {
880887 match simd_level() {
@@ -883,17 +890,22 @@ pub(crate) fn vpass_row(intermediate: &[i16], block_w: usize, src_stride: usize,
883890 // + block_w]` for t in 0..8 and writes `acc[..block_w]`, which
884891 // the debug asserts pin.
885892 SimdLevel::Avx2 => {
886- unsafe { simd::vpass_avx2(intermediate, block_w, src_stride, row, taps, acc) };
893+ unsafe { simd::vpass_avx2(intermediate, block_w, src_stride, rows, taps, acc) };
887894 return;
888895 }
889896 SimdLevel::Sse41 => {
890- unsafe { simd::vpass_sse41(intermediate, block_w, src_stride, row, taps, acc) };
897+ unsafe { simd::vpass_sse41(intermediate, block_w, src_stride, rows, taps, acc) };
891898 return;
892899 }
893900 SimdLevel::Scalar => {}
894901 }
895902 }
896- vpass_row_scalar(intermediate, block_w, src_stride, row, taps, acc);
903+ for row in 0..rows {
904+ vpass_row_scalar(
905+ &intermediate[row * src_stride..], block_w, src_stride, 0, taps,
906+ &mut acc[row * block_w..(row + 1) * block_w],
907+ );
908+ }
897909 }
898910
899911 /// lane-mc: [`vpass_row`] with the output rounding fused in, for the two
@@ -1687,19 +1699,15 @@ pub fn predict_compound_intermediate_kern(
16871699 #[allow(unsafe_code)]
16881700 let src: &[i16] =
16891701 unsafe { std::slice::from_raw_parts(reference.as_ptr().cast::<i16>(), reference.len()) };
1690- let mut accbuf = [0i32; 128];
1691- let acc = &mut accbuf[..block_w];
1692- for row in 0..block_h {
1693- vpass_row(&src[base..], block_w, stride, row, &v16, acc);
1694- for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
1695- *d = round2(a, INTER_ROUND_1_COMPOUND);
1696- }
1702+ vpass_row(&src[base..], block_w, stride, block_h, &v16, dst);
1703+ for d in dst.iter_mut() {
1704+ *d = round2(*d, INTER_ROUND_1_COMPOUND);
16971705 }
16981706 return;
16991707 }
17001708 }
17011709 }
1702- with_scratch(rows, block_w, |intermediate, acc| {
1710+ with_scratch(rows, block_w, |intermediate, _acc| {
17031711 horizontal_pass(
17041712 reference, stride, true_width, true_height, x_q4,
17051713 if yfrac == 0 { y0 + 3 } else { y0 }, x_scale_fp, block_w,
@@ -1712,11 +1720,9 @@ pub fn predict_compound_intermediate_kern(
17121720 }
17131721 return;
17141722 }
1715- for row in 0..block_h {
1716- vpass_row(intermediate, block_w, block_w, row, v_filter, acc);
1717- for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
1718- *d = round2(a, INTER_ROUND_1_COMPOUND);
1719- }
1723+ vpass_row(intermediate, block_w, block_w, block_h, v_filter, dst);
1724+ for d in dst.iter_mut() {
1725+ *d = round2(*d, INTER_ROUND_1_COMPOUND);
17201726 }
17211727 });
17221728 }
@@ -2424,20 +2430,20 @@ mod tests {
24242430 .collect();
24252431 for row in [0usize, 1, 4] {
24262432 let mut want = vec![0i32; w];
2427- super::vpass_row_scalar(&inter, w, st, row, taps, &mut want);
2433+ super::vpass_row_scalar(&inter[row * st..], w, st, 0, taps, &mut want);
24282434 let mut got = vec![0i32; w];
2429- super::vpass_row(&inter, w, st, row, taps, &mut got);
2435+ super::vpass_row(&inter[row * st..], w, st, 1, taps, &mut got);
24302436 assert_eq!(got, want, "dispatch w={w} frac={frac} row={row}");
24312437 #[cfg(target_arch = "x86_64")]
24322438 {
24332439 if std::arch::is_x86_feature_detected!("sse4.1") {
24342440 let mut sse = vec![0i32; w];
2435- unsafe { super::simd::vpass_sse41(&inter, w, st, row, taps, &mut sse) };
2441+ unsafe { super::simd::vpass_sse41(&inter[row * st..], w, st, 1, taps, &mut sse) };
24362442 assert_eq!(sse, want, "sse4.1 w={w} frac={frac} row={row}");
24372443 }
24382444 if std::arch::is_x86_feature_detected!("avx2") {
24392445 let mut avx = vec![0i32; w];
2440- unsafe { super::simd::vpass_avx2(&inter, w, st, row, taps, &mut avx) };
2446+ unsafe { super::simd::vpass_avx2(&inter[row * st..], w, st, 1, taps, &mut avx) };
24412447 assert_eq!(avx, want, "avx2 w={w} frac={frac} row={row}");
24422448 }
24432449 }
crates/ec-av1/src/restoration.rsmodified+1 -1
@@ -642,7 +642,7 @@ fn apply_wiener_stripe(
642642 acc.clear();
643643 acc.resize(w, 0i32);
644644 for r in 0..h {
645- crate::mc::vpass_row(&inter, w, w, r, &taps, &mut acc);
645+ crate::mc::vpass_row(&inter[r * w..], w, w, 1, &taps, &mut acc);
646646 let dst = &mut out[(v_start + r) * stride + h_start..][..w];
647647 for (o, &a) in dst.iter_mut().zip(acc.iter()) {
648648 *o = round2(i64::from(a), 11).clamp(0, smax) as u16;