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 @@
1313 #![allow(clippy::needless_range_loop)]
1414
1515 use ec_h264_syntax::SliceType;
16-use wide::i16x16;
16+use wide::{i16x8, i16x16};
1717
1818 use crate::deblock::{edge_params, filter_luma_h_edge16, filter_luma_line};
1919 use crate::decoder::{
@@ -165,6 +165,10 @@ impl Default for Levels {
165165
166166 /// 4x4 Hadamard sum of absolute transformed differences between the source at
167167 /// `(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.
168172 fn satd4(src: &[u8], sstride: usize, sx: usize, sy: usize, pred: &[u8], pstride: usize) -> i32 {
169173 let mut d = [0i32; 16];
170174 for y in 0..4 {
@@ -2704,18 +2708,41 @@ fn encode_intra_mb(
27042708 );
27052709 }
27062710
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+
27072724 /// Squared error of the whole macroblock against the source, luma and chroma:
27082725 /// 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.
27092733 fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {
27102734 let mut sum = 0i64;
27112735 let stride = pic.y.stride;
27122736 let origin = pic.y.at(mb_x * 16, mb_y * 16);
27132737 for row in 0..16 {
27142738 let s = (mb_y * 16 + row) * e.src.stride + mb_x * 16;
2715- for x in 0..16 {
2716- let d = i64::from(e.src.y[s + x]) - i64::from(pic.y.data[origin + row * stride + x]);
2717- sum += d * d;
2718- }
2739+ let d = load16(&e.src.y, s) - load16(&pic.y.data, origin + row * stride);
2740+ sum += d
2741+ .dot(d)
2742+ .to_array()
2743+ .iter()
2744+ .map(|&v| i64::from(v))
2745+ .sum::<i64>();
27192746 }
27202747 for comp in 0..2 {
27212748 let (plane, src) = if comp == 0 {
@@ -2727,10 +2754,13 @@ fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {
27272754 let origin = plane.at(mb_x * 8, mb_y * 8);
27282755 for row in 0..8 {
27292756 let s = (mb_y * 8 + row) * e.src.c_stride + mb_x * 8;
2730- for x in 0..8 {
2731- let d = i64::from(src[s + x]) - i64::from(plane.data[origin + row * stride + x]);
2732- sum += d * d;
2733- }
2757+ let d = load8(src, s) - load8(&plane.data, origin + row * stride);
2758+ sum += d
2759+ .dot(d)
2760+ .to_array()
2761+ .iter()
2762+ .map(|&v| i64::from(v))
2763+ .sum::<i64>();
27342764 }
27352765 }
27362766 sum