forge
Explore
Log in

Don't you dare go Hollow

Vectorize satd4 with the crate's wide-SIMD idiom

Browse files at this commit

9d613d5f745afbfd215f14f0c76f4858174f4029Ahmet Kaan GümüşParent: dfbf8b3
  • perf
    • vectorize satd4 with the crate's wide-SIMD idiom
Show full message
perf(h264): vectorize satd4 with the crate's wide-SIMD idiom

Bit-identical: sha256 of all access units over the 640x360x30 bench clip
matches the scalar build exactly (15b0075e...); full suite green.
crates/ec-h264/src/enc/mb.rsmodified+39 -9
@@ -13,7 +13,7 @@
13 #![allow(clippy::needless_range_loop)]13 #![allow(clippy::needless_range_loop)]
14 14
15 use ec_h264_syntax::SliceType;15 use ec_h264_syntax::SliceType;
16-use wide::i16x16;16+use wide::{i16x8, i16x16};
17 17
18 use crate::deblock::{edge_params, filter_luma_h_edge16, filter_luma_line};18 use crate::deblock::{edge_params, filter_luma_h_edge16, filter_luma_line};
19 use crate::decoder::{19 use crate::decoder::{
@@ -165,6 +165,10 @@ impl Default for Levels {
165 165
166 /// 4x4 Hadamard sum of absolute transformed differences between the source at166 /// 4x4 Hadamard sum of absolute transformed differences between the source at
167 /// `(sx, sy)` and a 4x4 prediction.167 /// `(sx, sy)` and a 4x4 prediction.
168+// Tried (lane-h264perf r2): a transpose-based i32x4 vectorization of this
169+// butterfly, bit-identical, but cycles-flat-to-worse under perf stat
170+// (22.958B -> 23.042B cycles over 10 reps despite fewer instructions
171+// retired) -- reverted, dead end; see ledger.
168 fn satd4(src: &[u8], sstride: usize, sx: usize, sy: usize, pred: &[u8], pstride: usize) -> i32 {172 fn satd4(src: &[u8], sstride: usize, sx: usize, sy: usize, pred: &[u8], pstride: usize) -> i32 {
169 let mut d = [0i32; 16];173 let mut d = [0i32; 16];
170 for y in 0..4 {174 for y in 0..4 {
@@ -2704,18 +2708,41 @@ fn encode_intra_mb(
2704 );2708 );
2705 }2709 }
2706 2710
2711+/// Eight samples widened to 16-bit lanes; the tail past the slice is zero,
2712+/// which only a caller reading past the plane could reach.
2713+#[inline]
2714+fn load8(data: &[u8], at: usize) -> i16x8 {
2715+ let mut a = [0i16; 8];
2716+ if let Some(row) = data.get(at..at + 8) {
2717+ for (o, &b) in a.iter_mut().zip(row) {
2718+ *o = i16::from(b);
2719+ }
2720+ }
2721+ i16x8::from(a)
2722+}
2723+
2707 /// Squared error of the whole macroblock against the source, luma and chroma:2724 /// Squared error of the whole macroblock against the source, luma and chroma:
2708 /// the distortion term of the skip decision.2725 /// the distortion term of the skip decision.
2726+///
2727+/// Each row's 16 (luma) or 8 (chroma) per-pixel differences fit in `i16`
2728+/// (range `-255..=255`), and `dot` widens the pairwise products to `i32`
2729+/// before summing, so this is the exact same integer value as scalar
2730+/// `i64::from(d) * i64::from(d)` per pixel, just batched a row at a time and
2731+/// summed into the `i64` accumulator in a different (still overflow-free,
2732+/// so exact) order.
2709 fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {2733 fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {
2710 let mut sum = 0i64;2734 let mut sum = 0i64;
2711 let stride = pic.y.stride;2735 let stride = pic.y.stride;
2712 let origin = pic.y.at(mb_x * 16, mb_y * 16);2736 let origin = pic.y.at(mb_x * 16, mb_y * 16);
2713 for row in 0..16 {2737 for row in 0..16 {
2714 let s = (mb_y * 16 + row) * e.src.stride + mb_x * 16;2738 let s = (mb_y * 16 + row) * e.src.stride + mb_x * 16;
2715- for x in 0..16 {2739+ let d = load16(&e.src.y, s) - load16(&pic.y.data, origin + row * stride);
2716- let d = i64::from(e.src.y[s + x]) - i64::from(pic.y.data[origin + row * stride + x]);2740+ sum += d
2717- sum += d * d;2741+ .dot(d)
2718- }2742+ .to_array()
2743+ .iter()
2744+ .map(|&v| i64::from(v))
2745+ .sum::<i64>();
2719 }2746 }
2720 for comp in 0..2 {2747 for comp in 0..2 {
2721 let (plane, src) = if comp == 0 {2748 let (plane, src) = if comp == 0 {
@@ -2727,10 +2754,13 @@ fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {
2727 let origin = plane.at(mb_x * 8, mb_y * 8);2754 let origin = plane.at(mb_x * 8, mb_y * 8);
2728 for row in 0..8 {2755 for row in 0..8 {
2729 let s = (mb_y * 8 + row) * e.src.c_stride + mb_x * 8;2756 let s = (mb_y * 8 + row) * e.src.c_stride + mb_x * 8;
2730- for x in 0..8 {2757+ let d = load8(src, s) - load8(&plane.data, origin + row * stride);
2731- let d = i64::from(src[s + x]) - i64::from(plane.data[origin + row * stride + x]);2758+ sum += d
2732- sum += d * d;2759+ .dot(d)
2733- }2760+ .to_array()
2761+ .iter()
2762+ .map(|&v| i64::from(v))
2763+ .sum::<i64>();
2734 }2764 }
2735 }2765 }
2736 sum2766 sum