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(
836 } else {836 } else {
837 15837 15
838 };838 };
839+ let sig_base = sig_ctx_base(xs, ys, sub_wide, csbf, log2_size, c_idx, scan_idx);
839 let mut scan_pos = first;840 let mut scan_pos = first;
840 while scan_pos >= 0 {841 while scan_pos >= 0 {
841 let p = scan_pos as usize;842 let p = scan_pos as usize;
@@ -845,7 +846,7 @@ fn encode_subblock(
845 let yc = ys * 4 + yp as usize;846 let yc = ys * 4 + yp as usize;
846 let sig = levels[yc * n + xc] != 0;847 let sig = levels[yc * n + xc] != 0;
847 if p > 0 || !infer_dc_sig {848 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);
849 enc.encode_bin(ctx::SIG_COEFF + inc, u32::from(sig));850 enc.encode_bin(ctx::SIG_COEFF + inc, u32::from(sig));
850 }851 }
851 significant[p] = sig;852 significant[p] = sig;
@@ -1095,6 +1096,7 @@ pub fn decode_residual(
1095 } else {1096 } else {
1096 151097 15
1097 };1098 };
1099+ let sig_base = sig_ctx_base(xs, ys, sub_wide, &csbf, log2_size, c_idx, scan_idx);
1098 let mut scan_pos = first;1100 let mut scan_pos = first;
1099 while scan_pos >= 0 {1101 while scan_pos >= 0 {
1100 let p = scan_pos as usize;1102 let p = scan_pos as usize;
@@ -1103,7 +1105,7 @@ pub fn decode_residual(
1103 let xc = xs * 4 + xp as usize;1105 let xc = xs * 4 + xp as usize;
1104 let yc = ys * 4 + yp as usize;1106 let yc = ys * 4 + yp as usize;
1105 let sig = if p > 0 || !infer_dc_sig {1107 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);
1107 dec.decode_bin(ctx::SIG_COEFF + inc) != 01109 dec.decode_bin(ctx::SIG_COEFF + inc) != 0
1108 } else {1110 } else {
1109 false // filled in by the post-loop inference below1111 false // filled in by the post-loop inference below
@@ -1202,9 +1204,13 @@ pub fn decode_residual(
1202 1204
1203 /// `ctxInc` for `sig_coeff_flag` (9.3.4.2.5).1205 /// `ctxInc` for `sig_coeff_flag` (9.3.4.2.5).
1204 #[allow(clippy::too_many_arguments)]1206 #[allow(clippy::too_many_arguments)]
1205-fn sig_ctx_inc(1207+/// Sub-block-invariant part of [`sig_ctx_inc`]'s context: `prev_csbf` and the
1206- xc: usize,1208+/// additive base both depend only on the sub-block's own coordinates
1207- yc: usize,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(
1208 xs: usize,1214 xs: usize,
1209 ys: usize,1215 ys: usize,
1210 sub_wide: usize,1216 sub_wide: usize,
@@ -1212,20 +1218,52 @@ fn sig_ctx_inc(
1212 log2_size: u32,1218 log2_size: u32,
1213 c_idx: usize,1219 c_idx: usize,
1214 scan_idx: usize,1220 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)>,
1215 ) -> usize {1259 ) -> usize {
1216- let mut sig_ctx;1260+ let sig_ctx;
1217 if log2_size == 2 {1261 if log2_size == 2 {
1218 sig_ctx = CTX_IDX_MAP[(yc << 2) + xc];1262 sig_ctx = CTX_IDX_MAP[(yc << 2) + xc];
1219 } else if xc + yc == 0 {1263 } else if xc + yc == 0 {
1220 sig_ctx = 0;1264 sig_ctx = 0;
1221 } else {1265 } else {
1222- let mut prev_csbf = 0usize;1266+ let (prev_csbf, base) = base.expect("sig_ctx_base returns Some when log2_size != 2");
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- }
1229 let (xp, yp) = (xc & 3, yc & 3);1267 let (xp, yp) = (xc & 3, yc & 3);
1230 sig_ctx = match prev_csbf {1268 sig_ctx = match prev_csbf {
1231 0 => {1269 0 => {
@@ -1257,20 +1295,7 @@ fn sig_ctx_inc(
1257 }1295 }
1258 _ => 2,1296 _ => 2,
1259 };1297 };
1260- if c_idx == 0 {1298+ return sig_ctx + base;
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- }
1274 }1299 }
1275 if c_idx == 0 { sig_ctx } else { 27 + sig_ctx }1300 if c_idx == 0 { sig_ctx } else { 27 + sig_ctx }
1276 }1301 }
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];
127 const COEFF_MIN: i32 = -32_768;127 const COEFF_MIN: i32 = -32_768;
128 const COEFF_MAX: i32 = 32_767;128 const COEFF_MAX: i32 = 32_767;
129 129
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+
130 /// One-dimensional inverse transform, `y[i] = sum_j M[j][i] * x[j]`.157 /// One-dimensional inverse transform, `y[i] = sum_j M[j][i] * x[j]`.
131 ///158 ///
132 /// Split even/odd once per level: the even rows of the size-N matrix *are* the159 /// 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]) {
145 }172 }
146 let mut even = [0i32; 16];173 let mut even = [0i32; 16];
147 inverse_1d(half, &even_src[..half], &mut even[..half]);174 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+ }
148 for x in 0..half {181 for x in 0..half {
149- let mut odd = 0i32;
150 let matrix_col = &FULL_MATRIX_T[x];182 let matrix_col = &FULL_MATRIX_T[x];
183+ let mut odd_matrix = [0i32; 16];
151 for j in 0..half {184 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];
153 }186 }
187+ let odd = dot(&odd_matrix[..half], &odd_src[..half]);
154 dst[x] = even[x] + odd;188 dst[x] = even[x] + odd;
155 dst[n - 1 - x] = even[x] - odd;189 dst[n - 1 - x] = even[x] - odd;
156 }190 }
@@ -178,12 +212,8 @@ fn forward_1d(n: usize, src: &[i32], dst: &mut [i32]) {
178 forward_1d(half, &sums[..half], &mut even[..half]);212 forward_1d(half, &sums[..half], &mut even[..half]);
179 for j in 0..half {213 for j in 0..half {
180 dst[2 * j] = even[j];214 dst[2 * j] = even[j];
181- let mut odd = 0i32;
182 let matrix_row = &FULL_MATRIX[(2 * j + 1) * stride];215 let matrix_row = &FULL_MATRIX[(2 * j + 1) * stride];
183- for (x, &diff) in diffs[..half].iter().enumerate() {216+ dst[2 * j + 1] = dot(&matrix_row[..half], &diffs[..half]);
184- odd += matrix_row[x] * diff;
185- }
186- dst[2 * j + 1] = odd;
187 }217 }
188 }218 }
189 219