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 {
456 intermediate: &[i16],456 intermediate: &[i16],
457 block_w: usize,457 block_w: usize,
458 src_stride: usize,458 src_stride: usize,
459- row: usize,459+ rows: usize,
460 taps: &[i32; 8],460 taps: &[i32; 8],
461 acc: &mut [i32],461 acc: &mut [i32],
462 ) {462 ) {
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);
464 let tp = tap_pairs(&taps16(taps));464 let tp = tap_pairs(&taps16(taps));
465+ for row in 0..rows {
465 let ip = intermediate.as_ptr().add(row * src_stride);466 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);
467 let mut c = 0usize;468 let mut c = 0usize;
468 while c + 16 <= block_w {469 while c + 16 <= block_w {
469 // `lo` accumulates columns c+0..4 and c+8..12 (one per 128-bit470 // `lo` accumulates columns c+0..4 and c+8..12 (one per 128-bit
@@ -486,6 +487,7 @@ mod simd {
486 c += 16;487 c += 16;
487 }488 }
488 vpass_tail_sse(ip, block_w, src_stride, &tp, taps, ap, c);489 vpass_tail_sse(ip, block_w, src_stride, &tp, taps, ap, c);
490+ }
489 }491 }
490 492
491 /// [`vpass_avx2`]'s 8- and 4-column steps, 128-bit: every AV1 block width493 /// [`vpass_avx2`]'s 8- and 4-column steps, 128-bit: every AV1 block width
@@ -693,13 +695,18 @@ mod simd {
693 intermediate: &[i16],695 intermediate: &[i16],
694 block_w: usize,696 block_w: usize,
695 src_stride: usize,697 src_stride: usize,
696- row: usize,698+ rows: usize,
697 taps: &[i32; 8],699 taps: &[i32; 8],
698 acc: &mut [i32],700 acc: &mut [i32],
699 ) {701 ) {
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);
701 let tp = tap_pairs(&taps16(taps));703 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+ }
703 }710 }
704 711
705 /// Horizontal pass, SSE2: [`hpass_avx2`]'s scheme at 8 outputs per712 /// Horizontal pass, SSE2: [`hpass_avx2`]'s scheme at 8 outputs per
@@ -874,7 +881,7 @@ fn horizontal_pass_unscaled(
874 /// eight slots. `acc` is left holding the unrounded sums.881 /// eight slots. `acc` is left holding the unrounded sums.
875 #[inline]882 #[inline]
876 #[allow(unsafe_code)]883 #[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]) {
878 #[cfg(target_arch = "x86_64")]885 #[cfg(target_arch = "x86_64")]
879 {886 {
880 match simd_level() {887 match simd_level() {
@@ -883,17 +890,22 @@ pub(crate) fn vpass_row(intermediate: &[i16], block_w: usize, src_stride: usize,
883 // + block_w]` for t in 0..8 and writes `acc[..block_w]`, which890 // + block_w]` for t in 0..8 and writes `acc[..block_w]`, which
884 // the debug asserts pin.891 // the debug asserts pin.
885 SimdLevel::Avx2 => {892 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) };
887 return;894 return;
888 }895 }
889 SimdLevel::Sse41 => {896 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) };
891 return;898 return;
892 }899 }
893 SimdLevel::Scalar => {}900 SimdLevel::Scalar => {}
894 }901 }
895 }902 }
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+ }
897 }909 }
898 910
899 /// lane-mc: [`vpass_row`] with the output rounding fused in, for the two911 /// lane-mc: [`vpass_row`] with the output rounding fused in, for the two
@@ -1687,19 +1699,15 @@ pub fn predict_compound_intermediate_kern(
1687 #[allow(unsafe_code)]1699 #[allow(unsafe_code)]
1688 let src: &[i16] =1700 let src: &[i16] =
1689 unsafe { std::slice::from_raw_parts(reference.as_ptr().cast::<i16>(), reference.len()) };1701 unsafe { std::slice::from_raw_parts(reference.as_ptr().cast::<i16>(), reference.len()) };
1690- let mut accbuf = [0i32; 128];1702+ vpass_row(&src[base..], block_w, stride, block_h, &v16, dst);
1691- let acc = &mut accbuf[..block_w];1703+ for d in dst.iter_mut() {
1692- for row in 0..block_h {1704+ *d = round2(*d, INTER_ROUND_1_COMPOUND);
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- }
1697 }1705 }
1698 return;1706 return;
1699 }1707 }
1700 }1708 }
1701 }1709 }
1702- with_scratch(rows, block_w, |intermediate, acc| {1710+ with_scratch(rows, block_w, |intermediate, _acc| {
1703 horizontal_pass(1711 horizontal_pass(
1704 reference, stride, true_width, true_height, x_q4,1712 reference, stride, true_width, true_height, x_q4,
1705 if yfrac == 0 { y0 + 3 } else { y0 }, x_scale_fp, block_w,1713 if yfrac == 0 { y0 + 3 } else { y0 }, x_scale_fp, block_w,
@@ -1712,11 +1720,9 @@ pub fn predict_compound_intermediate_kern(
1712 }1720 }
1713 return;1721 return;
1714 }1722 }
1715- for row in 0..block_h {1723+ vpass_row(intermediate, block_w, block_w, block_h, v_filter, dst);
1716- vpass_row(intermediate, block_w, block_w, row, v_filter, acc);1724+ for d in dst.iter_mut() {
1717- for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {1725+ *d = round2(*d, INTER_ROUND_1_COMPOUND);
1718- *d = round2(a, INTER_ROUND_1_COMPOUND);
1719- }
1720 }1726 }
1721 });1727 });
1722 }1728 }
@@ -2424,20 +2430,20 @@ mod tests {
2424 .collect();2430 .collect();
2425 for row in [0usize, 1, 4] {2431 for row in [0usize, 1, 4] {
2426 let mut want = vec![0i32; w];2432 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);
2428 let mut got = vec![0i32; w];2434 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);
2430 assert_eq!(got, want, "dispatch w={w} frac={frac} row={row}");2436 assert_eq!(got, want, "dispatch w={w} frac={frac} row={row}");
2431 #[cfg(target_arch = "x86_64")]2437 #[cfg(target_arch = "x86_64")]
2432 {2438 {
2433 if std::arch::is_x86_feature_detected!("sse4.1") {2439 if std::arch::is_x86_feature_detected!("sse4.1") {
2434 let mut sse = vec![0i32; w];2440 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) };
2436 assert_eq!(sse, want, "sse4.1 w={w} frac={frac} row={row}");2442 assert_eq!(sse, want, "sse4.1 w={w} frac={frac} row={row}");
2437 }2443 }
2438 if std::arch::is_x86_feature_detected!("avx2") {2444 if std::arch::is_x86_feature_detected!("avx2") {
2439 let mut avx = vec![0i32; w];2445 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) };
2441 assert_eq!(avx, want, "avx2 w={w} frac={frac} row={row}");2447 assert_eq!(avx, want, "avx2 w={w} frac={frac} row={row}");
2442 }2448 }
2443 }2449 }
crates/ec-av1/src/restoration.rsmodified+1 -1
@@ -642,7 +642,7 @@ fn apply_wiener_stripe(
642 acc.clear();642 acc.clear();
643 acc.resize(w, 0i32);643 acc.resize(w, 0i32);
644 for r in 0..h {644 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);
646 let dst = &mut out[(v_start + r) * stride + h_start..][..w];646 let dst = &mut out[(v_start + r) * stride + h_start..][..w];
647 for (o, &a) in dst.iter_mut().zip(acc.iter()) {647 for (o, &a) in dst.iter_mut().zip(acc.iter()) {
648 *o = round2(i64::from(a), 11).clamp(0, smax) as u16;648 *o = round2(i64::from(a), 11).clamp(0, smax) as u16;