forge
Explore
Log in

Don't you dare go Hollow

SIMD the forward/inverse transform's odd dot product

Browse files at this commit

b9e8391725538f005f11fdd5adb6f4d9d138ccb2Ahmet Kaan GümüşParent: 820bda2
  • perf
    • SIMD the forward/inverse transform's odd dot product
Show full message
perf(h265): SIMD the forward/inverse transform's odd dot product

wide::i32x8 across the 8+ wide chunks of forward_1d's/inverse_1d's
matrix*sample sums, scalar tail for the rest; also hoist inverse_1d's
src[2j+1] gather out of the per-x loop (it didn't depend on x). Bounds
for i32 accumulation documented on `dot`. Correctness gate green
(cargo test -p ec-h265 --release, rdoq_cache_hash_is_pinned unmoved);
instruction count over a 4-frame 1080p single-thread profile loop:
142,512,057,557 -> 142,473,095,572 (-0.03%, most TUs at this content/QP
are 8x8/4x4 where half<8 never enters the vector path).
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