SIMD the forward/inverse transform's odd dot product
Browse files at this commitb9e8391725538f005f11fdd5adb6f4d9d138ccb2Parent: 820bda2- perf
- SIMD the forward/inverse transform's odd dot product
1 changed file +37 -7
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 | 127 | const COEFF_MIN: i32 = -32_768; |
| 128 | 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 | 157 | /// One-dimensional inverse transform, `y[i] = sum_j M[j][i] * x[j]`. |
| 131 | 158 | /// |
| 132 | 159 | /// 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 | 173 | let mut even = [0i32; 16]; |
| 147 | 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 | 181 | for x in 0..half { |
| 149 | - let mut odd = 0i32; | |
| 150 | 182 | let matrix_col = &FULL_MATRIX_T[x]; |
| 183 | + let mut odd_matrix = [0i32; 16]; | |
| 151 | 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 | 188 | dst[x] = even[x] + odd; |
| 155 | 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 | 212 | forward_1d(half, &sums[..half], &mut even[..half]); |
| 179 | 213 | for j in 0..half { |
| 180 | 214 | dst[2 * j] = even[j]; |
| 181 | - let mut odd = 0i32; | |
| 182 | 215 | 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]); | |
| 187 | 217 | } |
| 188 | 218 | } |
| 189 | 219 |