Run the inverse DCT network on a fixed-size scratch array
Browse files at this commite428d82db267a3df8e5a3e44ba12366efc6ab2afParent: 7086b91- perf
- run the inverse DCT network on a fixed-size scratch array
1 changed file +19 -5
crates/ec-av1/src/transform.rsmodified+19 -5
| @@ -107,7 +107,7 @@ fn clamp_range(x: i32, r: usize) -> i32 { | ||
| 107 | 107 | |
| 108 | 108 | /// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an |
| 109 | 109 | /// exchange of the two entries when `flip` is set. |
| 110 | -fn butterfly(t: &mut [i32], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { | |
| 110 | +fn butterfly(t: &mut [i32; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { | |
| 111 | 111 | let (ta, tb) = (i64::from(t[a]), i64::from(t[b])); |
| 112 | 112 | let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle))); |
| 113 | 113 | let x = ta * c - tb * s; |
| @@ -121,7 +121,7 @@ fn butterfly(t: &mut [i32], a: usize, b: usize, angle: i32, flip: bool, _r: usiz | ||
| 121 | 121 | |
| 122 | 122 | /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices |
| 123 | 123 | /// exchanged when `flip` is set. |
| 124 | -fn hadamard(t: &mut [i32], a: usize, b: usize, flip: bool, r: usize) { | |
| 124 | +fn hadamard(t: &mut [i32; 64], a: usize, b: usize, flip: bool, r: usize) { | |
| 125 | 125 | let (a, b) = if flip { (b, a) } else { (a, b) }; |
| 126 | 126 | let (x, y) = (t[a], t[b]); |
| 127 | 127 | t[a] = clamp_range(x.wrapping_add(y), r); |
| @@ -130,7 +130,7 @@ fn hadamard(t: &mut [i32], a: usize, b: usize, flip: bool, r: usize) { | ||
| 130 | 130 | |
| 131 | 131 | /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal |
| 132 | 132 | /// of the first `1 << n` entries. |
| 133 | -fn permute(t: &mut [i32], n: u32) { | |
| 133 | +fn permute(t: &mut [i32; 64], n: u32) { | |
| 134 | 134 | let n0 = 1usize << n; |
| 135 | 135 | // A stack copy: this ran once per 1D transform and its `Vec` was the |
| 136 | 136 | // `malloc` at the top of every `inverse_dct` frame in the annotation. |
| @@ -148,6 +148,20 @@ fn permute(t: &mut [i32], n: u32) { | ||
| 148 | 148 | /// The stage list is the spec's, in the spec's order; the guards on `n` are |
| 149 | 149 | /// what make one network serve every transform size from 4 to 64. |
| 150 | 150 | pub fn inverse_dct(t: &mut [i32], n: u32, r: usize) { |
| 151 | + // lane-perf10: the network below indexes a 64-entry scratch with computed | |
| 152 | + // indices, and on a `&mut [i32]` every one of them carried a bounds check. | |
| 153 | + // A `&mut [i32; 64]` lets the optimiser discharge them all against the | |
| 154 | + // constant length; this wrapper keeps the slice-taking signature for | |
| 155 | + // callers outside the 2D driver. | |
| 156 | + let m = 1usize << n; | |
| 157 | + let mut fixed = [0i32; 64]; | |
| 158 | + fixed[..m].copy_from_slice(&t[..m]); | |
| 159 | + inverse_dct_fixed(&mut fixed, n, r); | |
| 160 | + t[..m].copy_from_slice(&fixed[..m]); | |
| 161 | +} | |
| 162 | + | |
| 163 | +/// [`inverse_dct`] on the driver's own 64-entry scratch. | |
| 164 | +fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { | |
| 151 | 165 | assert!((2..=6).contains(&n), "the inverse DCT is defined for 4..64"); |
| 152 | 166 | permute(t, n); |
| 153 | 167 | |
| @@ -827,9 +841,9 @@ fn inverse_identity(t: &mut [i32], side: usize) { | ||
| 827 | 841 | |
| 828 | 842 | /// Dispatches one row or column's 1D inverse transform by [`TxType1d`], |
| 829 | 843 | /// `log2` the spec's transform-size log (as [`inverse_dct`] takes). |
| 830 | -fn inverse_1d(t: &mut [i32], log2: u32, r: usize, kind: TxType1d) { | |
| 844 | +fn inverse_1d(t: &mut [i32; 64], log2: u32, r: usize, kind: TxType1d) { | |
| 831 | 845 | match kind { |
| 832 | - TxType1d::Dct => inverse_dct(t, log2, r), | |
| 846 | + TxType1d::Dct => inverse_dct_fixed(t, log2, r), | |
| 833 | 847 | TxType1d::Adst => match log2 { |
| 834 | 848 | 2 => inverse_adst4(t), |
| 835 | 849 | 3 => inverse_adst8(t, r), |