forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-h265xform': transform dot SIMD + sig_ctx hoist

Browse files at this commit

765bf63d4f1277fedaff05cf050c9cd3938c850eAhmet Kaan GümüşParent: 820bda207800cd
The odd-sum dot products vectorize with wide::i32x8 (flat at this
content's TU mix, kept as a non-regression); sig_ctx_inc's
sub-block-invariant half is computed once per sub-block instead of per
significant position, encoder and decoder split identically. -0.87%
instructions, pinned hash unmoved.
crates/ec-h265/src/residual.rsmodified+52 -27
@@ -836,6 +836,7 @@ fn encode_subblock(
836836 } else {
837837 15
838838 };
839+ let sig_base = sig_ctx_base(xs, ys, sub_wide, csbf, log2_size, c_idx, scan_idx);
839840 let mut scan_pos = first;
840841 while scan_pos >= 0 {
841842 let p = scan_pos as usize;
@@ -845,7 +846,7 @@ fn encode_subblock(
845846 let yc = ys * 4 + yp as usize;
846847 let sig = levels[yc * n + xc] != 0;
847848 if p > 0 || !infer_dc_sig {
848- let inc = sig_ctx_inc(xc, yc, xs, ys, sub_wide, csbf, log2_size, c_idx, scan_idx);
849+ let inc = sig_ctx_inc(xc, yc, log2_size, c_idx, sig_base);
849850 enc.encode_bin(ctx::SIG_COEFF + inc, u32::from(sig));
850851 }
851852 significant[p] = sig;
@@ -1095,6 +1096,7 @@ pub fn decode_residual(
10951096 } else {
10961097 15
10971098 };
1099+ let sig_base = sig_ctx_base(xs, ys, sub_wide, &csbf, log2_size, c_idx, scan_idx);
10981100 let mut scan_pos = first;
10991101 while scan_pos >= 0 {
11001102 let p = scan_pos as usize;
@@ -1103,7 +1105,7 @@ pub fn decode_residual(
11031105 let xc = xs * 4 + xp as usize;
11041106 let yc = ys * 4 + yp as usize;
11051107 let sig = if p > 0 || !infer_dc_sig {
1106- let inc = sig_ctx_inc(xc, yc, xs, ys, sub_wide, &csbf, log2_size, c_idx, scan_idx);
1108+ let inc = sig_ctx_inc(xc, yc, log2_size, c_idx, sig_base);
11071109 dec.decode_bin(ctx::SIG_COEFF + inc) != 0
11081110 } else {
11091111 false // filled in by the post-loop inference below
@@ -1202,9 +1204,13 @@ pub fn decode_residual(
12021204
12031205 /// `ctxInc` for `sig_coeff_flag` (9.3.4.2.5).
12041206 #[allow(clippy::too_many_arguments)]
1205-fn sig_ctx_inc(
1206- xc: usize,
1207- yc: usize,
1207+/// Sub-block-invariant part of [`sig_ctx_inc`]'s context: `prev_csbf` and the
1208+/// additive base both depend only on the sub-block's own coordinates
1209+/// (`xs`/`ys`), never on the position within it, so a caller stepping through
1210+/// up to 15 significant positions in one sub-block computes this once instead
1211+/// of on every position. `None` when `log2_size == 2` (the small-block path
1212+/// doesn't use `prev_csbf`/base at all — [`sig_ctx_inc`] handles it directly).
1213+fn sig_ctx_base(
12081214 xs: usize,
12091215 ys: usize,
12101216 sub_wide: usize,
@@ -1212,20 +1218,52 @@ fn sig_ctx_inc(
12121218 log2_size: u32,
12131219 c_idx: usize,
12141220 scan_idx: usize,
1221+) -> Option<(usize, usize)> {
1222+ if log2_size == 2 {
1223+ return None;
1224+ }
1225+ let mut prev_csbf = 0usize;
1226+ if xs < sub_wide - 1 && csbf[ys * sub_wide + xs + 1] {
1227+ prev_csbf += 1;
1228+ }
1229+ if ys < sub_wide - 1 && csbf[(ys + 1) * sub_wide + xs] {
1230+ prev_csbf += 2;
1231+ }
1232+ let mut base = 0usize;
1233+ if c_idx == 0 {
1234+ if xs + ys > 0 {
1235+ base += 3;
1236+ }
1237+ if log2_size == 3 {
1238+ base += if scan_idx == 0 { 9 } else { 15 };
1239+ } else {
1240+ base += 21;
1241+ }
1242+ } else if log2_size == 3 {
1243+ base += 9;
1244+ } else {
1245+ base += 12;
1246+ }
1247+ if c_idx != 0 {
1248+ base += 27;
1249+ }
1250+ Some((prev_csbf, base))
1251+}
1252+
1253+fn sig_ctx_inc(
1254+ xc: usize,
1255+ yc: usize,
1256+ log2_size: u32,
1257+ c_idx: usize,
1258+ base: Option<(usize, usize)>,
12151259 ) -> usize {
1216- let mut sig_ctx;
1260+ let sig_ctx;
12171261 if log2_size == 2 {
12181262 sig_ctx = CTX_IDX_MAP[(yc << 2) + xc];
12191263 } else if xc + yc == 0 {
12201264 sig_ctx = 0;
12211265 } else {
1222- let mut prev_csbf = 0usize;
1223- if xs < sub_wide - 1 && csbf[ys * sub_wide + xs + 1] {
1224- prev_csbf += 1;
1225- }
1226- if ys < sub_wide - 1 && csbf[(ys + 1) * sub_wide + xs] {
1227- prev_csbf += 2;
1228- }
1266+ let (prev_csbf, base) = base.expect("sig_ctx_base returns Some when log2_size != 2");
12291267 let (xp, yp) = (xc & 3, yc & 3);
12301268 sig_ctx = match prev_csbf {
12311269 0 => {
@@ -1257,20 +1295,7 @@ fn sig_ctx_inc(
12571295 }
12581296 _ => 2,
12591297 };
1260- if c_idx == 0 {
1261- if xs + ys > 0 {
1262- sig_ctx += 3;
1263- }
1264- if log2_size == 3 {
1265- sig_ctx += if scan_idx == 0 { 9 } else { 15 };
1266- } else {
1267- sig_ctx += 21;
1268- }
1269- } else if log2_size == 3 {
1270- sig_ctx += 9;
1271- } else {
1272- sig_ctx += 12;
1273- }
1298+ return sig_ctx + base;
12741299 }
12751300 if c_idx == 0 { sig_ctx } else { 27 + sig_ctx }
12761301 }
crates/ec-h265/src/transform.rsmodified+37 -7
@@ -127,6 +127,33 @@ const QUANT_SCALE: [i32; 6] = [26_214, 23_302, 20_560, 18_396, 16_384, 14_564];
127127 const COEFF_MIN: i32 = -32_768;
128128 const COEFF_MAX: i32 = 32_767;
129129
130+/// Dot product of two equal-length slices, SIMD in 8-wide chunks with a
131+/// scalar tail.
132+///
133+/// Both `forward_1d` and `inverse_1d`'s odd sums are `sum_j m[j] * x[j]` over
134+/// at most 16 terms; matrix entries are `|m| <= 90` and `x` is a coefficient
135+/// or residual sample clamped to `COEFF_MIN..=COEFF_MAX` (+/-32768), so each
136+/// product is bounded by ~3.0e6 and the full sum of 16 by ~4.7e7 — nowhere
137+/// near overflowing i32, whether accumulated in one lane or spread across
138+/// eight and reduced at the end.
139+#[inline]
140+fn dot(a: &[i32], b: &[i32]) -> i32 {
141+ debug_assert_eq!(a.len(), b.len());
142+ let mut acc = wide::i32x8::splat(0);
143+ let mut i = 0;
144+ while i + 8 <= a.len() {
145+ let av = wide::i32x8::from(<[i32; 8]>::try_from(&a[i..i + 8]).unwrap());
146+ let bv = wide::i32x8::from(<[i32; 8]>::try_from(&b[i..i + 8]).unwrap());
147+ acc += av * bv;
148+ i += 8;
149+ }
150+ let mut total: i32 = acc.to_array().iter().sum();
151+ for j in i..a.len() {
152+ total += a[j] * b[j];
153+ }
154+ total
155+}
156+
130157 /// One-dimensional inverse transform, `y[i] = sum_j M[j][i] * x[j]`.
131158 ///
132159 /// Split even/odd once per level: the even rows of the size-N matrix *are* the
@@ -145,12 +172,19 @@ fn inverse_1d(n: usize, src: &[i32], dst: &mut [i32]) {
145172 }
146173 let mut even = [0i32; 16];
147174 inverse_1d(half, &even_src[..half], &mut even[..half]);
175+ // `src[2 * j + 1]` doesn't depend on `x`: gather it once instead of once
176+ // per `x` (it was being re-read `half` times over).
177+ let mut odd_src = [0i32; 16];
178+ for j in 0..half {
179+ odd_src[j] = src[2 * j + 1];
180+ }
148181 for x in 0..half {
149- let mut odd = 0i32;
150182 let matrix_col = &FULL_MATRIX_T[x];
183+ let mut odd_matrix = [0i32; 16];
151184 for j in 0..half {
152- odd += matrix_col[(2 * j + 1) * stride] * src[2 * j + 1];
185+ odd_matrix[j] = matrix_col[(2 * j + 1) * stride];
153186 }
187+ let odd = dot(&odd_matrix[..half], &odd_src[..half]);
154188 dst[x] = even[x] + odd;
155189 dst[n - 1 - x] = even[x] - odd;
156190 }
@@ -178,12 +212,8 @@ fn forward_1d(n: usize, src: &[i32], dst: &mut [i32]) {
178212 forward_1d(half, &sums[..half], &mut even[..half]);
179213 for j in 0..half {
180214 dst[2 * j] = even[j];
181- let mut odd = 0i32;
182215 let matrix_row = &FULL_MATRIX[(2 * j + 1) * stride];
183- for (x, &diff) in diffs[..half].iter().enumerate() {
184- odd += matrix_row[x] * diff;
185- }
186- dst[2 * j + 1] = odd;
216+ dst[2 * j + 1] = dot(&matrix_row[..half], &diffs[..half]);
187217 }
188218 }
189219