forge
Explore
Log in

Don't you dare go Hollow

Run the inverse DCT network on a fixed-size scratch array

Browse files at this commit

e428d82db267a3df8e5a3e44ba12366efc6ab2afAhmet Kaan GümüşParent: 7086b91
  • perf
    • run the inverse DCT network on a fixed-size scratch array
Show full message
perf(av1): run the inverse DCT network on a fixed-size scratch array

Every butterfly, Hadamard and permutation of the spec's DCT network
indexes the 64-entry scratch with a computed index, and on a `&mut [i32]`
each one carried its own bounds check against a length the optimiser
could not see. The network now runs on `&mut [i32; 64]`, which discharges
them against the constant length; `inverse_dct` keeps its slice-taking
signature as a wrapper for callers outside the 2D driver, and `inverse_1d`
calls the fixed-size body directly.

Byte-exact on seg1080/seg4k/seg4k_lr/palette_screen_witness. seg1080
189.06 G instructions (was 190.19 G, -0.59%); seg4k 599.04 G (was
602.71 G, -0.61%).
crates/ec-av1/src/transform.rsmodified+19 -5
@@ -107,7 +107,7 @@ fn clamp_range(x: i32, r: usize) -> i32 {
107 107
108 /// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an108 /// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an
109 /// exchange of the two entries when `flip` is set.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 let (ta, tb) = (i64::from(t[a]), i64::from(t[b]));111 let (ta, tb) = (i64::from(t[a]), i64::from(t[b]));
112 let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle)));112 let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle)));
113 let x = ta * c - tb * s;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 /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices122 /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices
123 /// exchanged when `flip` is set.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 let (a, b) = if flip { (b, a) } else { (a, b) };125 let (a, b) = if flip { (b, a) } else { (a, b) };
126 let (x, y) = (t[a], t[b]);126 let (x, y) = (t[a], t[b]);
127 t[a] = clamp_range(x.wrapping_add(y), r);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 /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal131 /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal
132 /// of the first `1 << n` entries.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 let n0 = 1usize << n;134 let n0 = 1usize << n;
135 // A stack copy: this ran once per 1D transform and its `Vec` was the135 // A stack copy: this ran once per 1D transform and its `Vec` was the
136 // `malloc` at the top of every `inverse_dct` frame in the annotation.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 /// The stage list is the spec's, in the spec's order; the guards on `n` are148 /// The stage list is the spec's, in the spec's order; the guards on `n` are
149 /// what make one network serve every transform size from 4 to 64.149 /// what make one network serve every transform size from 4 to 64.
150 pub fn inverse_dct(t: &mut [i32], n: u32, r: usize) {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 assert!((2..=6).contains(&n), "the inverse DCT is defined for 4..64");165 assert!((2..=6).contains(&n), "the inverse DCT is defined for 4..64");
152 permute(t, n);166 permute(t, n);
153 167
@@ -827,9 +841,9 @@ fn inverse_identity(t: &mut [i32], side: usize) {
827 841
828 /// Dispatches one row or column's 1D inverse transform by [`TxType1d`],842 /// Dispatches one row or column's 1D inverse transform by [`TxType1d`],
829 /// `log2` the spec's transform-size log (as [`inverse_dct`] takes).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 match kind {845 match kind {
832- TxType1d::Dct => inverse_dct(t, log2, r),846+ TxType1d::Dct => inverse_dct_fixed(t, log2, r),
833 TxType1d::Adst => match log2 {847 TxType1d::Adst => match log2 {
834 2 => inverse_adst4(t),848 2 => inverse_adst4(t),
835 3 => inverse_adst8(t, r),849 3 => inverse_adst8(t, r),