forge
Explore
Log in

Don't you dare go Hollow

MC hoists the interior row test and fuses whole-pel-horizontal blocks onto the reference

Browse files at this commit

37ae4978c00d819f8187d5544ea74f4641a88f26Ahmet Kaan GümüşParent: e27b2d5
  • perf
    • MC hoists the interior row test and fuses whole-pel-horizontal blocks onto the reference
Show full message
perf(av1): MC hoists the interior row test and fuses whole-pel-horizontal blocks onto the reference

The unscaled horizontal pass tested the vertical clamp, the interior
window, the slice bounds, the tap narrowing and the SIMD level once per
row; when the whole footprint is inside the reference -- the common case
-- every one of those has the same answer for all rows, so the test is
hoisted and the loop is a walk of the stride.

A whole-pel horizontal position makes the horizontal pass the identity
tap, whose intermediate is exactly `s << 4`, so the vertical pass is the
same 8-tap sum taken straight off the reference rows with the taps
scaled by 16. Both the single and the compound kernel now take that
path for interior blocks, which drops the intermediate entirely; the
vertical kernels gained a source-stride parameter to read it.

Byte-exact: all four gate streams hash to their references.
crates/ec-av1/src/mc.rsmodified+157 -42
@@ -421,13 +421,14 @@ mod simd {
421 pub unsafe fn vpass_avx2(421 pub unsafe fn vpass_avx2(
422 intermediate: &[i16],422 intermediate: &[i16],
423 block_w: usize,423 block_w: usize,
424+ src_stride: usize,
424 row: usize,425 row: usize,
425 taps: &[i32; 8],426 taps: &[i32; 8],
426 acc: &mut [i32],427 acc: &mut [i32],
427 ) {428 ) {
428- debug_assert!(intermediate.len() >= (row + 8) * block_w && acc.len() >= block_w);429+ debug_assert!(intermediate.len() >= (row + 7) * src_stride + block_w && acc.len() >= block_w);
429 let tp = tap_pairs(&taps16(taps));430 let tp = tap_pairs(&taps16(taps));
430- let ip = intermediate.as_ptr().add(row * block_w);431+ let ip = intermediate.as_ptr().add(row * src_stride);
431 let ap = acc.as_mut_ptr();432 let ap = acc.as_mut_ptr();
432 let mut c = 0usize;433 let mut c = 0usize;
433 while c + 16 <= block_w {434 while c + 16 <= block_w {
@@ -441,8 +442,8 @@ mod simd {
441 continue;442 continue;
442 }443 }
443 let tv = _mm256_set1_epi32(tp[k]);444 let tv = _mm256_set1_epi32(tp[k]);
444- let a = _mm256_loadu_si256(ip.add(2 * k * block_w + c).cast());445+ let a = _mm256_loadu_si256(ip.add(2 * k * src_stride + c).cast());
445- let b = _mm256_loadu_si256(ip.add((2 * k + 1) * block_w + c).cast());446+ let b = _mm256_loadu_si256(ip.add((2 * k + 1) * src_stride + c).cast());
446 lo = _mm256_add_epi32(lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(a, b), tv));447 lo = _mm256_add_epi32(lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(a, b), tv));
447 hi = _mm256_add_epi32(hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(a, b), tv));448 hi = _mm256_add_epi32(hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(a, b), tv));
448 }449 }
@@ -450,7 +451,7 @@ mod simd {
450 _mm256_storeu_si256(ap.add(c + 8).cast(), _mm256_permute2x128_si256(lo, hi, 0x31));451 _mm256_storeu_si256(ap.add(c + 8).cast(), _mm256_permute2x128_si256(lo, hi, 0x31));
451 c += 16;452 c += 16;
452 }453 }
453- vpass_tail_sse(ip, block_w, &tp, taps, ap, c);454+ vpass_tail_sse(ip, block_w, src_stride, &tp, taps, ap, c);
454 }455 }
455 456
456 /// [`vpass_avx2`]'s 8- and 4-column steps, 128-bit: every AV1 block width457 /// [`vpass_avx2`]'s 8- and 4-column steps, 128-bit: every AV1 block width
@@ -464,6 +465,7 @@ mod simd {
464 unsafe fn vpass_tail_sse(465 unsafe fn vpass_tail_sse(
465 ip: *const i16,466 ip: *const i16,
466 block_w: usize,467 block_w: usize,
468+ src_stride: usize,
467 tp: &[i32; 4],469 tp: &[i32; 4],
468 taps: &[i32; 8],470 taps: &[i32; 8],
469 ap: *mut i32,471 ap: *mut i32,
@@ -477,8 +479,8 @@ mod simd {
477 continue;479 continue;
478 }480 }
479 let tv = _mm_set1_epi32(tp[k]);481 let tv = _mm_set1_epi32(tp[k]);
480- let a = _mm_loadu_si128(ip.add(2 * k * block_w + c).cast());482+ let a = _mm_loadu_si128(ip.add(2 * k * src_stride + c).cast());
481- let b = _mm_loadu_si128(ip.add((2 * k + 1) * block_w + c).cast());483+ let b = _mm_loadu_si128(ip.add((2 * k + 1) * src_stride + c).cast());
482 lo = _mm_add_epi32(lo, _mm_madd_epi16(_mm_unpacklo_epi16(a, b), tv));484 lo = _mm_add_epi32(lo, _mm_madd_epi16(_mm_unpacklo_epi16(a, b), tv));
483 hi = _mm_add_epi32(hi, _mm_madd_epi16(_mm_unpackhi_epi16(a, b), tv));485 hi = _mm_add_epi32(hi, _mm_madd_epi16(_mm_unpackhi_epi16(a, b), tv));
484 }486 }
@@ -493,8 +495,8 @@ mod simd {
493 continue;495 continue;
494 }496 }
495 let tv = _mm_set1_epi32(tp[k]);497 let tv = _mm_set1_epi32(tp[k]);
496- let a = _mm_loadl_epi64(ip.add(2 * k * block_w + c).cast());498+ let a = _mm_loadl_epi64(ip.add(2 * k * src_stride + c).cast());
497- let b = _mm_loadl_epi64(ip.add((2 * k + 1) * block_w + c).cast());499+ let b = _mm_loadl_epi64(ip.add((2 * k + 1) * src_stride + c).cast());
498 lo = _mm_add_epi32(lo, _mm_madd_epi16(_mm_unpacklo_epi16(a, b), tv));500 lo = _mm_add_epi32(lo, _mm_madd_epi16(_mm_unpacklo_epi16(a, b), tv));
499 }501 }
500 _mm_storeu_si128(ap.add(c).cast(), lo);502 _mm_storeu_si128(ap.add(c).cast(), lo);
@@ -506,7 +508,7 @@ mod simd {
506 while c < block_w {508 while c < block_w {
507 let mut sum = 0i32;509 let mut sum = 0i32;
508 for (t, &tap) in taps.iter().enumerate() {510 for (t, &tap) in taps.iter().enumerate() {
509- sum += tap * i32::from(*ip.add(t * block_w + c));511+ sum += tap * i32::from(*ip.add(t * src_stride + c));
510 }512 }
511 *ap.add(c) = sum;513 *ap.add(c) = sum;
512 c += 1;514 c += 1;
@@ -526,14 +528,15 @@ mod simd {
526 pub unsafe fn vpass_row_u16_avx2(528 pub unsafe fn vpass_row_u16_avx2(
527 intermediate: &[i16],529 intermediate: &[i16],
528 block_w: usize,530 block_w: usize,
531+ src_stride: usize,
529 row: usize,532 row: usize,
530 taps: &[i32; 8],533 taps: &[i32; 8],
531 max: i32,534 max: i32,
532 dst: &mut [u16],535 dst: &mut [u16],
533 ) {536 ) {
534- debug_assert!(intermediate.len() >= (row + 8) * block_w && dst.len() >= block_w);537+ debug_assert!(intermediate.len() >= row * src_stride + 7 * src_stride + block_w && dst.len() >= block_w);
535 let tp = tap_pairs(&taps16(taps));538 let tp = tap_pairs(&taps16(taps));
536- let ip = intermediate.as_ptr().add(row * block_w);539+ let ip = intermediate.as_ptr().add(row * src_stride);
537 let dp = dst.as_mut_ptr();540 let dp = dst.as_mut_ptr();
538 let rnd = _mm256_set1_epi32(1 << (INTER_ROUND_1 - 1));541 let rnd = _mm256_set1_epi32(1 << (INTER_ROUND_1 - 1));
539 let mx = _mm256_set1_epi16(max as i16);542 let mx = _mm256_set1_epi16(max as i16);
@@ -546,8 +549,8 @@ mod simd {
546 continue;549 continue;
547 }550 }
548 let tv = _mm256_set1_epi32(tp[k]);551 let tv = _mm256_set1_epi32(tp[k]);
549- let a = _mm256_loadu_si256(ip.add(2 * k * block_w + c).cast());552+ let a = _mm256_loadu_si256(ip.add(2 * k * src_stride + c).cast());
550- let b = _mm256_loadu_si256(ip.add((2 * k + 1) * block_w + c).cast());553+ let b = _mm256_loadu_si256(ip.add((2 * k + 1) * src_stride + c).cast());
551 lo = _mm256_add_epi32(lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(a, b), tv));554 lo = _mm256_add_epi32(lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(a, b), tv));
552 hi = _mm256_add_epi32(hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(a, b), tv));555 hi = _mm256_add_epi32(hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(a, b), tv));
553 }556 }
@@ -559,7 +562,7 @@ mod simd {
559 _mm256_storeu_si256(dp.add(c).cast(), v);562 _mm256_storeu_si256(dp.add(c).cast(), v);
560 c += 16;563 c += 16;
561 }564 }
562- vpass_u16_tail_sse(ip, block_w, &tp, taps, max, dp, c);565+ vpass_u16_tail_sse(ip, block_w, src_stride, &tp, taps, max, dp, c);
563 }566 }
564 567
565 /// [`vpass_row_u16_avx2`]'s 8- and 4-column steps (SSE4.1 for568 /// [`vpass_row_u16_avx2`]'s 8- and 4-column steps (SSE4.1 for
@@ -571,6 +574,7 @@ mod simd {
571 unsafe fn vpass_u16_tail_sse(574 unsafe fn vpass_u16_tail_sse(
572 ip: *const i16,575 ip: *const i16,
573 block_w: usize,576 block_w: usize,
577+ src_stride: usize,
574 tp: &[i32; 4],578 tp: &[i32; 4],
575 taps: &[i32; 8],579 taps: &[i32; 8],
576 max: i32,580 max: i32,
@@ -588,7 +592,7 @@ mod simd {
588 continue;592 continue;
589 }593 }
590 let tv = _mm_set1_epi32(tp[k]);594 let tv = _mm_set1_epi32(tp[k]);
591- let (pa, pb) = (ip.add(2 * k * block_w + c), ip.add((2 * k + 1) * block_w + c));595+ let (pa, pb) = (ip.add(2 * k * src_stride + c), ip.add((2 * k + 1) * src_stride + c));
592 let (a, b) = if wide {596 let (a, b) = if wide {
593 (_mm_loadu_si128(pa.cast()), _mm_loadu_si128(pb.cast()))597 (_mm_loadu_si128(pa.cast()), _mm_loadu_si128(pb.cast()))
594 } else {598 } else {
@@ -614,7 +618,7 @@ mod simd {
614 while c < block_w {618 while c < block_w {
615 let mut sum = 0i32;619 let mut sum = 0i32;
616 for (t, &tap) in taps.iter().enumerate() {620 for (t, &tap) in taps.iter().enumerate() {
617- sum += tap * i32::from(*ip.add(t * block_w + c));621+ sum += tap * i32::from(*ip.add(t * src_stride + c));
618 }622 }
619 *dp.add(c) = round2(sum, INTER_ROUND_1).clamp(0, max) as u16;623 *dp.add(c) = round2(sum, INTER_ROUND_1).clamp(0, max) as u16;
620 c += 1;624 c += 1;
@@ -629,15 +633,16 @@ mod simd {
629 pub unsafe fn vpass_row_u16_sse41(633 pub unsafe fn vpass_row_u16_sse41(
630 intermediate: &[i16],634 intermediate: &[i16],
631 block_w: usize,635 block_w: usize,
636+ src_stride: usize,
632 row: usize,637 row: usize,
633 taps: &[i32; 8],638 taps: &[i32; 8],
634 max: i32,639 max: i32,
635 dst: &mut [u16],640 dst: &mut [u16],
636 ) {641 ) {
637- debug_assert!(intermediate.len() >= (row + 8) * block_w && dst.len() >= block_w);642+ debug_assert!(intermediate.len() >= row * src_stride + 7 * src_stride + block_w && dst.len() >= block_w);
638 let tp = tap_pairs(&taps16(taps));643 let tp = tap_pairs(&taps16(taps));
639 vpass_u16_tail_sse(644 vpass_u16_tail_sse(
640- intermediate.as_ptr().add(row * block_w), block_w, &tp, taps, max,645+ intermediate.as_ptr().add(row * src_stride), block_w, src_stride, &tp, taps, max,
641 dst.as_mut_ptr(), 0,646 dst.as_mut_ptr(), 0,
642 );647 );
643 }648 }
@@ -649,13 +654,14 @@ mod simd {
649 pub unsafe fn vpass_sse41(654 pub unsafe fn vpass_sse41(
650 intermediate: &[i16],655 intermediate: &[i16],
651 block_w: usize,656 block_w: usize,
657+ src_stride: usize,
652 row: usize,658 row: usize,
653 taps: &[i32; 8],659 taps: &[i32; 8],
654 acc: &mut [i32],660 acc: &mut [i32],
655 ) {661 ) {
656- debug_assert!(intermediate.len() >= (row + 8) * block_w && acc.len() >= block_w);662+ debug_assert!(intermediate.len() >= (row + 7) * src_stride + block_w && acc.len() >= block_w);
657 let tp = tap_pairs(&taps16(taps));663 let tp = tap_pairs(&taps16(taps));
658- vpass_tail_sse(intermediate.as_ptr().add(row * block_w), block_w, &tp, taps, acc.as_mut_ptr(), 0);664+ vpass_tail_sse(intermediate.as_ptr().add(row * src_stride), block_w, src_stride, &tp, taps, acc.as_mut_ptr(), 0);
659 }665 }
660 666
661 /// Horizontal pass, SSE2: [`hpass_avx2`]'s scheme at 8 outputs per667 /// Horizontal pass, SSE2: [`hpass_avx2`]'s scheme at 8 outputs per
@@ -758,6 +764,38 @@ fn horizontal_pass_unscaled(
758 // InterRound0) == 16 * s` exactly, so a whole-pel horizontal position764 // InterRound0) == 16 * s` exactly, so a whole-pel horizontal position
759 // needs no filter at all -- one widened row read.765 // needs no filter at all -- one widened row read.
760 let identity = taps[3] == 128 && taps.iter().enumerate().all(|(i, &t)| i == 3 || t == 0);766 let identity = taps[3] == 128 && taps.iter().enumerate().all(|(i, &t)| i == 3 || t == 0);
767+ // lane-mc2: when the whole `rows x (block_w+7)` footprint lies inside the
768+ // reference -- the overwhelmingly common case -- every per-row test in
769+ // the general loop below (the vertical clamp, the horizontal interior
770+ // test, the slice bounds check, the tap narrowing and the SIMD dispatch)
771+ // has the same answer for every row, so it is hoisted out and the loop
772+ // becomes a walk of `stride`. Same sums, same order.
773+ let start = x0 - 3;
774+ let y_top = y0 - 3;
775+ if !identity
776+ && start >= 0
777+ && y_top >= 0
778+ && start as usize + block_w + 7 <= true_width
779+ && y_top as usize + rows <= true_height
780+ {
781+ let base = y_top as usize * stride + start as usize;
782+ let span = block_w + 7;
783+ if base + (rows - 1) * stride + span <= reference.len() {
784+ let t16 = [
785+ taps[0] as i16, taps[1] as i16, taps[2] as i16, taps[3] as i16,
786+ taps[4] as i16, taps[5] as i16, taps[6] as i16, taps[7] as i16,
787+ ];
788+ for r in 0..rows {
789+ let s = base + r * stride;
790+ hpass_contig(
791+ &reference[s..s + span],
792+ &t16,
793+ &mut intermediate[r * block_w..(r + 1) * block_w],
794+ );
795+ }
796+ return;
797+ }
798+ }
761 for r in 0..rows {799 for r in 0..rows {
762 let y = (y0 - 3 + r as i32).clamp(0, true_height as i32 - 1) as usize;800 let y = (y0 - 3 + r as i32).clamp(0, true_height as i32 - 1) as usize;
763 let out = &mut intermediate[r * block_w..(r + 1) * block_w];801 let out = &mut intermediate[r * block_w..(r + 1) * block_w];
@@ -788,7 +826,7 @@ fn horizontal_pass_unscaled(
788 /// eight slots. `acc` is left holding the unrounded sums.826 /// eight slots. `acc` is left holding the unrounded sums.
789 #[inline]827 #[inline]
790 #[allow(unsafe_code)]828 #[allow(unsafe_code)]
791-pub(crate) fn vpass_row(intermediate: &[i16], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) {829+pub(crate) fn vpass_row(intermediate: &[i16], block_w: usize, src_stride: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) {
792 #[cfg(target_arch = "x86_64")]830 #[cfg(target_arch = "x86_64")]
793 {831 {
794 match simd_level() {832 match simd_level() {
@@ -797,17 +835,17 @@ pub(crate) fn vpass_row(intermediate: &[i16], block_w: usize, row: usize, taps:
797 // + block_w]` for t in 0..8 and writes `acc[..block_w]`, which835 // + block_w]` for t in 0..8 and writes `acc[..block_w]`, which
798 // the debug asserts pin.836 // the debug asserts pin.
799 SimdLevel::Avx2 => {837 SimdLevel::Avx2 => {
800- unsafe { simd::vpass_avx2(intermediate, block_w, row, taps, acc) };838+ unsafe { simd::vpass_avx2(intermediate, block_w, src_stride, row, taps, acc) };
801 return;839 return;
802 }840 }
803 SimdLevel::Sse41 => {841 SimdLevel::Sse41 => {
804- unsafe { simd::vpass_sse41(intermediate, block_w, row, taps, acc) };842+ unsafe { simd::vpass_sse41(intermediate, block_w, src_stride, row, taps, acc) };
805 return;843 return;
806 }844 }
807 SimdLevel::Scalar => {}845 SimdLevel::Scalar => {}
808 }846 }
809 }847 }
810- vpass_row_scalar(intermediate, block_w, row, taps, acc);848+ vpass_row_scalar(intermediate, block_w, src_stride, row, taps, acc);
811 }849 }
812 850
813 /// lane-mc: [`vpass_row`] with the output rounding fused in, for the two851 /// lane-mc: [`vpass_row`] with the output rounding fused in, for the two
@@ -819,6 +857,7 @@ pub(crate) fn vpass_row(intermediate: &[i16], block_w: usize, row: usize, taps:
819 fn vpass_row_u16(857 fn vpass_row_u16(
820 intermediate: &[i16],858 intermediate: &[i16],
821 block_w: usize,859 block_w: usize,
860+ src_stride: usize,
822 row: usize,861 row: usize,
823 taps: &[i32; 8],862 taps: &[i32; 8],
824 max: i32,863 max: i32,
@@ -832,23 +871,24 @@ fn vpass_row_u16(
832 // + block_w]` for t in 0..8 and writes `dst[..block_w]`, which871 // + block_w]` for t in 0..8 and writes `dst[..block_w]`, which
833 // the debug asserts pin.872 // the debug asserts pin.
834 SimdLevel::Avx2 => {873 SimdLevel::Avx2 => {
835- unsafe { simd::vpass_row_u16_avx2(intermediate, block_w, row, taps, max, dst) };874+ unsafe { simd::vpass_row_u16_avx2(intermediate, block_w, src_stride, row, taps, max, dst) };
836 return;875 return;
837 }876 }
838 SimdLevel::Sse41 => {877 SimdLevel::Sse41 => {
839- unsafe { simd::vpass_row_u16_sse41(intermediate, block_w, row, taps, max, dst) };878+ unsafe { simd::vpass_row_u16_sse41(intermediate, block_w, src_stride, row, taps, max, dst) };
840 return;879 return;
841 }880 }
842 SimdLevel::Scalar => {}881 SimdLevel::Scalar => {}
843 }882 }
844 }883 }
845- vpass_row_u16_scalar(intermediate, block_w, row, taps, max, dst);884+ vpass_row_u16_scalar(intermediate, block_w, src_stride, row, taps, max, dst);
846 }885 }
847 886
848 /// The scalar reference for [`vpass_row_u16`].887 /// The scalar reference for [`vpass_row_u16`].
849 fn vpass_row_u16_scalar(888 fn vpass_row_u16_scalar(
850 intermediate: &[i16],889 intermediate: &[i16],
851 block_w: usize,890 block_w: usize,
891+ src_stride: usize,
852 row: usize,892 row: usize,
853 taps: &[i32; 8],893 taps: &[i32; 8],
854 max: i32,894 max: i32,
@@ -857,20 +897,20 @@ fn vpass_row_u16_scalar(
857 for (c, d) in dst[..block_w].iter_mut().enumerate() {897 for (c, d) in dst[..block_w].iter_mut().enumerate() {
858 let mut sum = 0i32;898 let mut sum = 0i32;
859 for (t, &tap) in taps.iter().enumerate() {899 for (t, &tap) in taps.iter().enumerate() {
860- sum += tap * i32::from(intermediate[(row + t) * block_w + c]);900+ sum += tap * i32::from(intermediate[(row + t) * src_stride + c]);
861 }901 }
862 *d = round2(sum, INTER_ROUND_1).clamp(0, max) as u16;902 *d = round2(sum, INTER_ROUND_1).clamp(0, max) as u16;
863 }903 }
864 }904 }
865 905
866 /// The scalar reference for [`vpass_row`] (lane-perf2's tap-major loop).906 /// The scalar reference for [`vpass_row`] (lane-perf2's tap-major loop).
867-fn vpass_row_scalar(intermediate: &[i16], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) {907+fn vpass_row_scalar(intermediate: &[i16], block_w: usize, src_stride: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) {
868 acc[..block_w].fill(0);908 acc[..block_w].fill(0);
869 for (t, &tap) in taps.iter().enumerate() {909 for (t, &tap) in taps.iter().enumerate() {
870 if tap == 0 {910 if tap == 0 {
871 continue;911 continue;
872 }912 }
873- let base = (row + t) * block_w;913+ let base = (row + t) * src_stride;
874 for (a, &s) in acc[..block_w].iter_mut().zip(&intermediate[base..base + block_w]) {914 for (a, &s) in acc[..block_w].iter_mut().zip(&intermediate[base..base + block_w]) {
875 *a += tap * i32::from(s);915 *a += tap * i32::from(s);
876 }916 }
@@ -1108,6 +1148,46 @@ pub(crate) fn predict_with_filters_kern(
1108 // block's own first row where the pass would have put the 4th.1148 // block's own first row where the pass would have put the 4th.
1109 let rows = if yfrac == 0 { block_h } else { block_h + 7 };1149 let rows = if yfrac == 0 { block_h } else { block_h + 7 };
1110 let max = crate::decode::sample_max(fctx);1150 let max = crate::decode::sample_max(fctx);
1151+ // lane-mc2: a whole-pel horizontal position makes the horizontal pass the
1152+ // identity tap, whose output is `s << 4` (`Round2(128 * s, InterRound0)`
1153+ // with the tap 128). The vertical pass then computes
1154+ // `Round2(sum(tap * (s << 4)), InterRound1)`, which is the same 8-tap sum
1155+ // taken straight off the reference rows with every tap scaled by 16 --
1156+ // bit-identical, and it skips materialising `block_h + 7` intermediate
1157+ // rows. Interior blocks only; an edge block still needs the clamped read.
1158+ if xfrac != 0 || yfrac == 0 {
1159+ // fall through to the two-pass path
1160+ } else {
1161+ let top = y0 - 3;
1162+ if top >= 0
1163+ && x0 >= 0
1164+ && x0 as usize + block_w <= true_width
1165+ && top as usize + block_h + 7 <= true_height
1166+ {
1167+ let base = top as usize * stride + x0 as usize;
1168+ if base + (block_h + 6) * stride + block_w <= reference.len() {
1169+ let mut v16 = [0i32; 8];
1170+ for (o, &t) in v16.iter_mut().zip(v_filter.iter()) {
1171+ *o = t * 16;
1172+ }
1173+ // SAFETY: samples are below `2^15` at every supported bit
1174+ // depth, so the same bytes read as `i16` are the same values;
1175+ // the length is unchanged.
1176+ #[allow(unsafe_code)]
1177+ let src: &[i16] =
1178+ unsafe { std::slice::from_raw_parts(reference.as_ptr().cast::<i16>(), reference.len()) };
1179+ for row in 0..block_h {
1180+ vpass_row_u16(
1181+ &src[base..], block_w, stride, row, &v16, max,
1182+ &mut dst[row * block_w..(row + 1) * block_w],
1183+ );
1184+ }
1185+ #[cfg(test)]
1186+ crate::encode::stage_add(1, stage_t.elapsed());
1187+ return;
1188+ }
1189+ }
1190+ }
1111 with_scratch(rows, block_w, |intermediate, _acc| {1191 with_scratch(rows, block_w, |intermediate, _acc| {
1112 horizontal_pass_unscaled(1192 horizontal_pass_unscaled(
1113 reference, stride, true_width, true_height, x0,1193 reference, stride, true_width, true_height, x0,
@@ -1122,7 +1202,7 @@ pub(crate) fn predict_with_filters_kern(
1122 }1202 }
1123 for row in 0..block_h {1203 for row in 0..block_h {
1124 vpass_row_u16(1204 vpass_row_u16(
1125- intermediate, block_w, row, v_filter, max,1205+ intermediate, block_w, block_w, row, v_filter, max,
1126 &mut dst[row * block_w..(row + 1) * block_w],1206 &mut dst[row * block_w..(row + 1) * block_w],
1127 );1207 );
1128 }1208 }
@@ -1412,7 +1492,7 @@ pub(crate) fn predict_scaled_kern(
1412 }1492 }
1413 for row in 0..block_h {1493 for row in 0..block_h {
1414 vpass_row_u16(1494 vpass_row_u16(
1415- intermediate, block_w, row, v_filter, max,1495+ intermediate, block_w, block_w, row, v_filter, max,
1416 &mut dst[row * block_w..(row + 1) * block_w],1496 &mut dst[row * block_w..(row + 1) * block_w],
1417 );1497 );
1418 }1498 }
@@ -1546,6 +1626,41 @@ pub fn predict_compound_intermediate_kern(
1546 };1626 };
1547 1627
1548 let rows = if yfrac == 0 { block_h } else { block_h + 7 };1628 let rows = if yfrac == 0 { block_h } else { block_h + 7 };
1629+ // lane-mc2: the unscaled whole-pel horizontal position is the identity
1630+ // tap, whose intermediate is `s << 4`, so the vertical pass is the same
1631+ // 8-tap sum taken straight off the reference rows with the taps scaled by
1632+ // 16 (see `predict_with_filters_kern`). Interior blocks only.
1633+ let x0 = x_q4.div_euclid(16);
1634+ if x_scale_fp == REF_NO_SCALE && x_q4.rem_euclid(16) == 0 && yfrac != 0 && block_w <= 128 {
1635+ let top = y0 - 3;
1636+ if top >= 0
1637+ && x0 >= 0
1638+ && x0 as usize + block_w <= true_width
1639+ && top as usize + block_h + 7 <= true_height
1640+ {
1641+ let base = top as usize * stride + x0 as usize;
1642+ if base + (block_h + 6) * stride + block_w <= reference.len() {
1643+ let mut v16 = [0i32; 8];
1644+ for (o, &t) in v16.iter_mut().zip(v_filter.iter()) {
1645+ *o = t * 16;
1646+ }
1647+ // SAFETY: samples are below `2^15` at every supported bit
1648+ // depth, so the same bytes read as `i16` are the same values.
1649+ #[allow(unsafe_code)]
1650+ let src: &[i16] =
1651+ unsafe { std::slice::from_raw_parts(reference.as_ptr().cast::<i16>(), reference.len()) };
1652+ let mut accbuf = [0i32; 128];
1653+ let acc = &mut accbuf[..block_w];
1654+ for row in 0..block_h {
1655+ vpass_row(&src[base..], block_w, stride, row, &v16, acc);
1656+ for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
1657+ *d = round2(a, INTER_ROUND_1_COMPOUND);
1658+ }
1659+ }
1660+ return;
1661+ }
1662+ }
1663+ }
1549 with_scratch(rows, block_w, |intermediate, acc| {1664 with_scratch(rows, block_w, |intermediate, acc| {
1550 horizontal_pass(1665 horizontal_pass(
1551 reference, stride, true_width, true_height, x_q4,1666 reference, stride, true_width, true_height, x_q4,
@@ -1560,7 +1675,7 @@ pub fn predict_compound_intermediate_kern(
1560 return;1675 return;
1561 }1676 }
1562 for row in 0..block_h {1677 for row in 0..block_h {
1563- vpass_row(intermediate, block_w, row, v_filter, acc);1678+ vpass_row(intermediate, block_w, block_w, row, v_filter, acc);
1564 for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {1679 for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
1565 *d = round2(a, INTER_ROUND_1_COMPOUND);1680 *d = round2(a, INTER_ROUND_1_COMPOUND);
1566 }1681 }
@@ -2213,20 +2328,20 @@ mod tests {
2213 .collect();2328 .collect();
2214 for row in [0usize, 1, 4] {2329 for row in [0usize, 1, 4] {
2215 let mut want = vec![0i32; w];2330 let mut want = vec![0i32; w];
2216- super::vpass_row_scalar(&inter, w, row, taps, &mut want);2331+ super::vpass_row_scalar(&inter, w, w, row, taps, &mut want);
2217 let mut got = vec![0i32; w];2332 let mut got = vec![0i32; w];
2218- super::vpass_row(&inter, w, row, taps, &mut got);2333+ super::vpass_row(&inter, w, w, row, taps, &mut got);
2219 assert_eq!(got, want, "dispatch w={w} frac={frac} row={row}");2334 assert_eq!(got, want, "dispatch w={w} frac={frac} row={row}");
2220 #[cfg(target_arch = "x86_64")]2335 #[cfg(target_arch = "x86_64")]
2221 {2336 {
2222 if std::arch::is_x86_feature_detected!("sse4.1") {2337 if std::arch::is_x86_feature_detected!("sse4.1") {
2223 let mut sse = vec![0i32; w];2338 let mut sse = vec![0i32; w];
2224- unsafe { super::simd::vpass_sse41(&inter, w, row, taps, &mut sse) };2339+ unsafe { super::simd::vpass_sse41(&inter, w, w, row, taps, &mut sse) };
2225 assert_eq!(sse, want, "sse4.1 w={w} frac={frac} row={row}");2340 assert_eq!(sse, want, "sse4.1 w={w} frac={frac} row={row}");
2226 }2341 }
2227 if std::arch::is_x86_feature_detected!("avx2") {2342 if std::arch::is_x86_feature_detected!("avx2") {
2228 let mut avx = vec![0i32; w];2343 let mut avx = vec![0i32; w];
2229- unsafe { super::simd::vpass_avx2(&inter, w, row, taps, &mut avx) };2344+ unsafe { super::simd::vpass_avx2(&inter, w, w, row, taps, &mut avx) };
2230 assert_eq!(avx, want, "avx2 w={w} frac={frac} row={row}");2345 assert_eq!(avx, want, "avx2 w={w} frac={frac} row={row}");
2231 }2346 }
2232 }2347 }
@@ -2236,7 +2351,7 @@ mod tests {
2236 // `round2(_, InterRound1).clamp(0, max)`.2351 // `round2(_, InterRound1).clamp(0, max)`.
2237 for max in [255i32, 1023] {2352 for max in [255i32, 1023] {
2238 let mut want_u16 = vec![0u16; w];2353 let mut want_u16 = vec![0u16; w];
2239- super::vpass_row_u16_scalar(&inter, w, row, taps, max, &mut want_u16);2354+ super::vpass_row_u16_scalar(&inter, w, w, row, taps, max, &mut want_u16);
2240 for (d, &a) in want_u16.iter().zip(want.iter()) {2355 for (d, &a) in want_u16.iter().zip(want.iter()) {
2241 assert_eq!(2356 assert_eq!(
2242 i32::from(*d),2357 i32::from(*d),
@@ -2245,18 +2360,18 @@ mod tests {
2245 );2360 );
2246 }2361 }
2247 let mut got_u16 = vec![0u16; w];2362 let mut got_u16 = vec![0u16; w];
2248- super::vpass_row_u16(&inter, w, row, taps, max, &mut got_u16);2363+ super::vpass_row_u16(&inter, w, w, row, taps, max, &mut got_u16);
2249 assert_eq!(got_u16, want_u16, "fused w={w} frac={frac} row={row} max={max}");2364 assert_eq!(got_u16, want_u16, "fused w={w} frac={frac} row={row} max={max}");
2250 #[cfg(target_arch = "x86_64")]2365 #[cfg(target_arch = "x86_64")]
2251 {2366 {
2252 if std::arch::is_x86_feature_detected!("sse4.1") {2367 if std::arch::is_x86_feature_detected!("sse4.1") {
2253 let mut sse = vec![0u16; w];2368 let mut sse = vec![0u16; w];
2254- unsafe { super::simd::vpass_row_u16_sse41(&inter, w, row, taps, max, &mut sse) };2369+ unsafe { super::simd::vpass_row_u16_sse41(&inter, w, w, row, taps, max, &mut sse) };
2255 assert_eq!(sse, want_u16, "fused sse4.1 w={w} max={max}");2370 assert_eq!(sse, want_u16, "fused sse4.1 w={w} max={max}");
2256 }2371 }
2257 if std::arch::is_x86_feature_detected!("avx2") {2372 if std::arch::is_x86_feature_detected!("avx2") {
2258 let mut avx = vec![0u16; w];2373 let mut avx = vec![0u16; w];
2259- unsafe { super::simd::vpass_row_u16_avx2(&inter, w, row, taps, max, &mut avx) };2374+ unsafe { super::simd::vpass_row_u16_avx2(&inter, w, w, row, taps, max, &mut avx) };
2260 assert_eq!(avx, want_u16, "fused avx2 w={w} max={max}");2375 assert_eq!(avx, want_u16, "fused avx2 w={w} max={max}");
2261 }2376 }
2262 }2377 }
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, r, &taps, &mut acc);645+ crate::mc::vpass_row(&inter, w, w, r, &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;