Explicit AVX2 inverse-DCT column pass, and an empty all-zero residual
Browse files at this commitef53b7eaccf2a7ee6a04c72fb78c417503eb065eParent: d60c70e- perf
- explicit AVX2 inverse-DCT column pass, and an empty all-zero residual
2 changed files +193 -17
- crates/ec-av1/src/decode.rs +33 -3
- crates/ec-av1/src/transform.rs +160 -14
crates/ec-av1/src/decode.rsmodified+33 -3
| @@ -2088,6 +2088,11 @@ impl TxParams { | |||
| 2088 | grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta, | 2088 | grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta, |
| 2089 | self.tx_type, | 2089 | self.tx_type, |
| 2090 | ); | 2090 | ); |
| 2091 | + // An all-zero grid transforms to the empty marker, and a strided copy | ||
| 2092 | + // of zeros is still zeros: the marker passes straight through. | ||
| 2093 | + if residual.is_empty() { | ||
| 2094 | + return residual; | ||
| 2095 | + } | ||
| 2091 | if self.stride == 0 { | 2096 | if self.stride == 0 { |
| 2092 | return residual; | 2097 | return residual; |
| 2093 | } | 2098 | } |
| @@ -2108,6 +2113,16 @@ pub(crate) enum Residual { | |||
| 2108 | } | 2113 | } |
| 2109 | 2114 | ||
| 2110 | impl Residual { | 2115 | impl Residual { |
| 2116 | + /// The record for a transform unit whose coefficient grid is all zero: | ||
| 2117 | + /// the empty marker, so neither the grid nor the residual is cloned and | ||
| 2118 | + /// the worker never runs the transform (60% of the 4K segment's units). | ||
| 2119 | + fn coeffs(tx: TxParams, grid: &[i32]) -> Self { | ||
| 2120 | + if grid.iter().all(|&c| c == 0) { | ||
| 2121 | + return Self::Done(Vec::new()); | ||
| 2122 | + } | ||
| 2123 | + Self::Coeffs(tx, grid.to_vec()) | ||
| 2124 | + } | ||
| 2125 | + | ||
| 2111 | fn resolve(self) -> Vec<i32> { | 2126 | fn resolve(self) -> Vec<i32> { |
| 2112 | match self { | 2127 | match self { |
| 2113 | Self::Done(v) => v, | 2128 | Self::Done(v) => v, |
| @@ -2116,6 +2131,18 @@ impl Residual { | |||
| 2116 | } | 2131 | } |
| 2117 | } | 2132 | } |
| 2118 | 2133 | ||
| 2134 | +/// An all-zero residual, for the transform units that code no coefficient: | ||
| 2135 | +/// [`crate::transform::dequant_and_inverse_typed_wh`] returns those EMPTY, | ||
| 2136 | +/// and the reconstruction loops below read this instead of a per-unit | ||
| 2137 | +/// `vec![0; w * h]`. Sized for the largest stride a recorded unit can carry | ||
| 2138 | +/// (a 128-wide inter block's prediction stride). | ||
| 2139 | +static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128]; | ||
| 2140 | + | ||
| 2141 | +/// `residual`, or `n` zeros when it is the empty all-zero marker. | ||
| 2142 | +fn dense_residual(residual: &[i32], n: usize) -> &[i32] { | ||
| 2143 | + if residual.is_empty() { &ZERO_RESIDUAL[..n] } else { residual } | ||
| 2144 | +} | ||
| 2145 | + | ||
| 2119 | /// Where a recorded motion-compensated write reads its prediction from. | 2146 | /// Where a recorded motion-compensated write reads its prediction from. |
| 2120 | /// | 2147 | /// |
| 2121 | /// lane-wave4: a block's whole-block prediction (mc + warp + interintra) is | 2148 | /// lane-wave4: a block's whole-block prediction (mc + warp + interintra) is |
| @@ -2846,7 +2873,7 @@ fn push_intra_tx( | |||
| 2846 | let pred = take_palette_pred(fctx); | 2873 | let pred = take_palette_pred(fctx); |
| 2847 | push_op(fctx, ReconOp::Intra { | 2874 | push_op(fctx, ReconOp::Intra { |
| 2848 | plane, x, y, bw, bh, rect, mode, angle_delta, reach, | 2875 | plane, x, y, bw, bh, rect, mode, angle_delta, reach, |
| 2849 | - residual: Residual::Coeffs(tx, grid.to_vec()), cfl, filter_intra, smooth_neighbor, pred, | 2876 | + residual: Residual::coeffs(tx, grid), cfl, filter_intra, smooth_neighbor, pred, |
| 2850 | }); | 2877 | }); |
| 2851 | } | 2878 | } |
| 2852 | 2879 | ||
| @@ -2877,7 +2904,7 @@ fn push_mc_rect_tx( | |||
| 2877 | push_op(fctx, ReconOp::Mc { | 2904 | push_op(fctx, ReconOp::Mc { |
| 2878 | plane, x, y, stride: w, w, h, | 2905 | plane, x, y, stride: w, w, h, |
| 2879 | prediction: PredSrc::Cur { plane: sp, off, src: ss }, | 2906 | prediction: PredSrc::Cur { plane: sp, off, src: ss }, |
| 2880 | - residual: Residual::Coeffs(tx, grid.to_vec()), | 2907 | + residual: Residual::coeffs(tx, grid), |
| 2881 | }); | 2908 | }); |
| 2882 | return; | 2909 | return; |
| 2883 | } | 2910 | } |
| @@ -2918,7 +2945,7 @@ fn push_mc_rect_tx( | |||
| 2918 | tx.stride = 0; | 2945 | tx.stride = 0; |
| 2919 | push_op(fctx, ReconOp::Mc { | 2946 | push_op(fctx, ReconOp::Mc { |
| 2920 | plane, x, y, stride: w, w, h, prediction: PredSrc::Owned(window), | 2947 | plane, x, y, stride: w, w, h, prediction: PredSrc::Owned(window), |
| 2921 | - residual: Residual::Coeffs(tx, grid.to_vec()), | 2948 | + residual: Residual::coeffs(tx, grid), |
| 2922 | }); | 2949 | }); |
| 2923 | } | 2950 | } |
| 2924 | 2951 | ||
| @@ -12808,6 +12835,7 @@ impl PlaneBuf<'_> { | |||
| 12808 | "@{} OUR_PRED x={x} y={y} bw={bw} bh={bh} mode={mode} ad={angle_delta} ft={} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller(), i32::from(smooth_neighbor) | 12835 | "@{} OUR_PRED x={x} y={y} bw={bw} bh={bh} mode={mode} ad={angle_delta} ft={} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller(), i32::from(smooth_neighbor) |
| 12809 | ); | 12836 | ); |
| 12810 | } | 12837 | } |
| 12838 | + let residual = dense_residual(residual, bw * bh); | ||
| 12811 | for row in 0..bh { | 12839 | for row in 0..bh { |
| 12812 | // Same bottom/right frame-edge clip as [`Self::reconstruct`]'s | 12840 | // Same bottom/right frame-edge clip as [`Self::reconstruct`]'s |
| 12813 | // (lane-sb128c r8) -- a 64x128 block at the right edge of a frame | 12841 | // (lane-sb128c r8) -- a 64x128 block at the right edge of a frame |
| @@ -12914,6 +12942,7 @@ impl PlaneBuf<'_> { | |||
| 12914 | "@{} OUR_PRED x={x} y={y} side={side} side={side} mode={mode} ad={angle_delta} ft={} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller(), i32::from(smooth_neighbor) | 12942 | "@{} OUR_PRED x={x} y={y} side={side} side={side} mode={mode} ad={angle_delta} ft={} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller(), i32::from(smooth_neighbor) |
| 12915 | ); | 12943 | ); |
| 12916 | } | 12944 | } |
| 12945 | + let residual = dense_residual(residual, side * side); | ||
| 12917 | for row in 0..side { | 12946 | for row in 0..side { |
| 12918 | // lane-sb128c r8: libaom's frame buffer is superblock | 12947 | // lane-sb128c r8: libaom's frame buffer is superblock |
| 12919 | // aligned; ours is padded to 32 (`block_grid`), so a 64x64 | 12948 | // aligned; ours is padded to 32 (`block_grid`), so a 64x64 |
| @@ -21797,6 +21826,7 @@ impl PlaneBuf<'_> { | |||
| 21797 | // three row bases are computed once per row instead of a | 21826 | // three row bases are computed once per row instead of a |
| 21798 | // multiply-add per sample (the samples themselves are unchanged). | 21827 | // multiply-add per sample (the samples themselves are unchanged). |
| 21799 | let max = crate::decode::sample_max(fctx); | 21828 | let max = crate::decode::sample_max(fctx); |
| 21829 | + let residual = dense_residual(residual, side * h); | ||
| 21800 | for row in 0..h { | 21830 | for row in 0..h { |
| 21801 | let src = row * side; | 21831 | let src = row * side; |
| 21802 | let dst = (y + row) * self.width + x; | 21832 | let dst = (y + row) * self.width + x; |
crates/ec-av1/src/transform.rsmodified+160 -14
| @@ -112,8 +112,6 @@ fn clamp_range(x: i32, r: usize) -> i32 { | |||
| 112 | /// integer arithmetic, so the eight-wide pass is byte-exact with the scalar | 112 | /// integer arithmetic, so the eight-wide pass is byte-exact with the scalar |
| 113 | /// one by construction -- the network itself is written once, above. | 113 | /// one by construction -- the network itself is written once, above. |
| 114 | trait Lane: Copy { | 114 | trait Lane: Copy { |
| 115 | - /// The additive identity, for [`permute`]'s scratch. | ||
| 116 | - const ZERO: Self; | ||
| 117 | /// One butterfly rotation's two outputs, at pre-multiplied `cos`/`sin`. | 115 | /// One butterfly rotation's two outputs, at pre-multiplied `cos`/`sin`. |
| 118 | fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self); | 116 | fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self); |
| 119 | /// One Hadamard rotation's two outputs, clamped to `r` bits. | 117 | /// One Hadamard rotation's two outputs, clamped to `r` bits. |
| @@ -126,8 +124,6 @@ fn round_cos(x: i64) -> i32 { | |||
| 126 | } | 124 | } |
| 127 | 125 | ||
| 128 | impl Lane for i32 { | 126 | impl Lane for i32 { |
| 129 | - const ZERO: Self = 0; | ||
| 130 | - | ||
| 131 | fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | 127 | fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { |
| 132 | let (ta, tb) = (i64::from(a), i64::from(b)); | 128 | let (ta, tb) = (i64::from(a), i64::from(b)); |
| 133 | (round_cos(ta * c - tb * s), round_cos(ta * s + tb * c)) | 129 | (round_cos(ta * c - tb * s), round_cos(ta * s + tb * c)) |
| @@ -141,8 +137,6 @@ impl Lane for i32 { | |||
| 141 | /// Eight columns at once. Every operation is the [`i32`] impl's, lane by lane, | 137 | /// Eight columns at once. Every operation is the [`i32`] impl's, lane by lane, |
| 142 | /// which is what the optimiser turns into one vector op per line. | 138 | /// which is what the optimiser turns into one vector op per line. |
| 143 | impl Lane for [i32; 8] { | 139 | impl Lane for [i32; 8] { |
| 144 | - const ZERO: Self = [0; 8]; | ||
| 145 | - | ||
| 146 | fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | 140 | fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { |
| 147 | let mut x = [0i32; 8]; | 141 | let mut x = [0i32; 8]; |
| 148 | let mut y = [0i32; 8]; | 142 | let mut y = [0i32; 8]; |
| @@ -165,8 +159,132 @@ impl Lane for [i32; 8] { | |||
| 165 | } | 159 | } |
| 166 | } | 160 | } |
| 167 | 161 | ||
| 162 | +/// The same eight-wide column pass in explicit AVX2. Every operation below is | ||
| 163 | +/// the [`i32`] impl's arithmetic, lane for lane, in 64-bit products -- so it is | ||
| 164 | +/// byte-exact with the scalar network on every input, not only on conformant | ||
| 165 | +/// ones (`the_avx2_dct_matches_the_scalar_one_over_the_full_range`). | ||
| 166 | +#[cfg(target_arch = "x86_64")] | ||
| 167 | +#[allow(unsafe_code)] | ||
| 168 | +mod simd { | ||
| 169 | + use super::{COS_BITS, Lane, inverse_dct_n}; | ||
| 170 | + use std::arch::x86_64::*; | ||
| 171 | + | ||
| 172 | + /// Eight `i32` lanes in one AVX2 register. | ||
| 173 | + #[derive(Clone, Copy)] | ||
| 174 | + pub(super) struct M256(pub(super) __m256i); | ||
| 175 | + | ||
| 176 | + impl Lane for M256 { | ||
| 177 | + /// `round_cos(a*c - b*s)`, `round_cos(a*s + b*c)`. | ||
| 178 | + /// | ||
| 179 | + /// `_mm256_mul_epi32` multiplies the sign-extended low `i32` of each | ||
| 180 | + /// 64-bit lane, so the four even lanes go through directly and the four | ||
| 181 | + /// odd ones after a shuffle; the products and their sum are exact in 64 | ||
| 182 | + /// bits (`|a| < 2^31`, `|c| <= 2^12`), and only the low 32 bits of the | ||
| 183 | + /// shifted sum are kept -- which is what the scalar `as i32` keeps, so | ||
| 184 | + /// the logical shift below stands in for the arithmetic one exactly. | ||
| 185 | + #[inline(always)] | ||
| 186 | + fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | ||
| 187 | + unsafe { | ||
| 188 | + let cv = _mm256_set1_epi32(c as i32); | ||
| 189 | + let sv = _mm256_set1_epi32(s as i32); | ||
| 190 | + let rnd = _mm256_set1_epi64x(1 << (COS_BITS - 1)); | ||
| 191 | + let (ae, be) = (a.0, b.0); | ||
| 192 | + let (ao, bo) = (_mm256_shuffle_epi32(a.0, 0xB1), _mm256_shuffle_epi32(b.0, 0xB1)); | ||
| 193 | + let half = |x: __m256i, y: __m256i, u: __m256i, v: __m256i| { | ||
| 194 | + let p = _mm256_sub_epi64(_mm256_mul_epi32(x, u), _mm256_mul_epi32(y, v)); | ||
| 195 | + let q = _mm256_add_epi64(_mm256_mul_epi32(x, v), _mm256_mul_epi32(y, u)); | ||
| 196 | + ( | ||
| 197 | + _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(p, rnd)), | ||
| 198 | + _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(q, rnd)), | ||
| 199 | + ) | ||
| 200 | + }; | ||
| 201 | + let (xe, ye) = half(ae, be, cv, sv); | ||
| 202 | + let (xo, yo) = half(ao, bo, cv, sv); | ||
| 203 | + ( | ||
| 204 | + M256(_mm256_blend_epi32::<0xAA>(xe, _mm256_slli_epi64::<32>(xo))), | ||
| 205 | + M256(_mm256_blend_epi32::<0xAA>(ye, _mm256_slli_epi64::<32>(yo))), | ||
| 206 | + ) | ||
| 207 | + } | ||
| 208 | + } | ||
| 209 | + | ||
| 210 | + #[inline(always)] | ||
| 211 | + fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | ||
| 212 | + unsafe { | ||
| 213 | + let hi = _mm256_set1_epi32(((1i64 << (r - 1)) - 1) as i32); | ||
| 214 | + let lo = _mm256_set1_epi32((-(1i64 << (r - 1))) as i32); | ||
| 215 | + let clamp = |v| _mm256_min_epi32(_mm256_max_epi32(v, lo), hi); | ||
| 216 | + ( | ||
| 217 | + M256(clamp(_mm256_add_epi32(a.0, b.0))), | ||
| 218 | + M256(clamp(_mm256_sub_epi32(a.0, b.0))), | ||
| 219 | + ) | ||
| 220 | + } | ||
| 221 | + } | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + /// The whole DCT column pass of one transform unit: `residual` is the | ||
| 225 | + /// row-pass output (`h` rows of `w`), `out` the residual it writes. | ||
| 226 | + /// | ||
| 227 | + /// # Safety | ||
| 228 | + /// The caller has checked for AVX2 ([`crate::mc::simd_level`]). | ||
| 229 | + #[target_feature(enable = "avx2")] | ||
| 230 | + pub(super) unsafe fn column_pass_dct( | ||
| 231 | + residual: &[i32], | ||
| 232 | + out: &mut [i32], | ||
| 233 | + w: usize, | ||
| 234 | + h: usize, | ||
| 235 | + log2h: u32, | ||
| 236 | + r: usize, | ||
| 237 | + ud_flip: bool, | ||
| 238 | + ) { | ||
| 239 | + let mut t = [M256(_mm256_setzero_si256()); 64]; | ||
| 240 | + for j in (0..w).step_by(8) { | ||
| 241 | + let mut any = _mm256_setzero_si256(); | ||
| 242 | + for i in 0..h { | ||
| 243 | + let v = unsafe { _mm256_loadu_si256(residual[i * w + j..].as_ptr().cast()) }; | ||
| 244 | + t[i] = M256(v); | ||
| 245 | + any = _mm256_or_si256(any, v); | ||
| 246 | + } | ||
| 247 | + // Zero in, zero out -- and `out` is already zero here. | ||
| 248 | + if _mm256_testz_si256(any, any) == 1 { | ||
| 249 | + continue; | ||
| 250 | + } | ||
| 251 | + match log2h { | ||
| 252 | + 2 => inverse_dct_n::<M256, 2>(&mut t, r), | ||
| 253 | + 3 => inverse_dct_n::<M256, 3>(&mut t, r), | ||
| 254 | + 4 => inverse_dct_n::<M256, 4>(&mut t, r), | ||
| 255 | + 5 => inverse_dct_n::<M256, 5>(&mut t, r), | ||
| 256 | + 6 => inverse_dct_n::<M256, 6>(&mut t, r), | ||
| 257 | + _ => unreachable!("the inverse DCT is defined for 4..64"), | ||
| 258 | + } | ||
| 259 | + // `round2(v, 4)` on eight lanes: the shift is arithmetic here | ||
| 260 | + // because the value stays in its own 32-bit lane. | ||
| 261 | + let rnd = _mm256_set1_epi32(8); | ||
| 262 | + for i in 0..h { | ||
| 263 | + let dst_row = if ud_flip { h - 1 - i } else { i }; | ||
| 264 | + let v = _mm256_srai_epi32::<4>(_mm256_add_epi32(t[i].0, rnd)); | ||
| 265 | + unsafe { _mm256_storeu_si256(out[dst_row * w + j..].as_mut_ptr().cast(), v) }; | ||
| 266 | + } | ||
| 267 | + } | ||
| 268 | + } | ||
| 269 | + | ||
| 270 | + /// The eight scalar lanes of `v`, for the equivalence gate. | ||
| 271 | + #[cfg(test)] | ||
| 272 | + pub(super) fn lanes(v: M256) -> [i32; 8] { | ||
| 273 | + let mut o = [0i32; 8]; | ||
| 274 | + unsafe { _mm256_storeu_si256(o.as_mut_ptr().cast(), v.0) }; | ||
| 275 | + o | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + /// `M256` from eight lanes, for the equivalence gate. | ||
| 279 | + #[cfg(test)] | ||
| 280 | + pub(super) fn splat(v: [i32; 8]) -> M256 { | ||
| 281 | + unsafe { M256(_mm256_loadu_si256(v.as_ptr().cast())) } | ||
| 282 | + } | ||
| 283 | +} | ||
| 284 | + | ||
| 168 | /// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an | 285 | /// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an |
| 169 | /// exchange of the two entries when `flip` is set. | 286 | /// exchange of the two entries when `flip` is set. |
| 287 | +#[inline(always)] | ||
| 170 | fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { | 288 | fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { |
| 171 | let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle))); | 289 | let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle))); |
| 172 | let (x, y) = T::btf(t[a], t[b], c, s); | 290 | let (x, y) = T::btf(t[a], t[b], c, s); |
| @@ -179,6 +297,7 @@ fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: boo | |||
| 179 | 297 | ||
| 180 | /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices | 298 | /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices |
| 181 | /// exchanged when `flip` is set. | 299 | /// exchanged when `flip` is set. |
| 300 | +#[inline(always)] | ||
| 182 | fn hadamard<T: Lane>(t: &mut [T; 64], a: usize, b: usize, flip: bool, r: usize) { | 301 | fn hadamard<T: Lane>(t: &mut [T; 64], a: usize, b: usize, flip: bool, r: usize) { |
| 183 | let (a, b) = if flip { (b, a) } else { (a, b) }; | 302 | let (a, b) = if flip { (b, a) } else { (a, b) }; |
| 184 | let (x, y) = T::had(t[a], t[b], r); | 303 | let (x, y) = T::had(t[a], t[b], r); |
| @@ -188,15 +307,18 @@ fn hadamard<T: Lane>(t: &mut [T; 64], a: usize, b: usize, flip: bool, r: usize) | |||
| 188 | 307 | ||
| 189 | /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal | 308 | /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal |
| 190 | /// of the first `1 << n` entries. | 309 | /// of the first `1 << n` entries. |
| 310 | +#[inline(always)] | ||
| 191 | fn permute<T: Lane>(t: &mut [T; 64], n: u32) { | 311 | fn permute<T: Lane>(t: &mut [T; 64], n: u32) { |
| 192 | let n0 = 1usize << n; | 312 | let n0 = 1usize << n; |
| 193 | - // A stack copy: this ran once per 1D transform and its `Vec` was the | 313 | + // In place: a bit reversal is an involution, so swapping every `i` with |
| 194 | - // `malloc` at the top of every `inverse_dct` frame in the annotation. | 314 | + // `brev(i) > i` visits each transposed pair once. The scratch copy this |
| 195 | - let mut copy = [T::ZERO; 64]; | 315 | + // replaces was a `[T; 64]` fill -- 2 KB per call at `T = M256`. |
| 196 | - copy[..n0].copy_from_slice(&t[..n0]); | ||
| 197 | let tab = &BREV_TAB[n as usize]; | 316 | let tab = &BREV_TAB[n as usize]; |
| 198 | - for (i, v) in t[..n0].iter_mut().enumerate() { | 317 | + for i in 0..n0 { |
| 199 | - *v = copy[tab[i] as usize]; | 318 | + let j = tab[i] as usize; |
| 319 | + if j > i { | ||
| 320 | + t.swap(i, j); | ||
| 321 | + } | ||
| 200 | } | 322 | } |
| 201 | } | 323 | } |
| 202 | 324 | ||
| @@ -247,6 +369,11 @@ fn inverse_dct_x8(t: &mut [[i32; 8]; 64], n: u32, r: usize) { | |||
| 247 | } | 369 | } |
| 248 | 370 | ||
| 249 | /// [`inverse_dct_fixed`] at one fixed `N = log2(size)`. | 371 | /// [`inverse_dct_fixed`] at one fixed `N = log2(size)`. |
| 372 | +// `#[inline(always)]`: the AVX2 instantiation has to land INSIDE the | ||
| 373 | +// `target_feature` wrapper, or every `_mm256_*` in it stays an out-of-line | ||
| 374 | +// call (measured: +5.6% instructions on the 4K segment). Every other | ||
| 375 | +// instantiation has exactly one caller, so this costs no extra copy. | ||
| 376 | +#[inline(always)] | ||
| 250 | fn inverse_dct_n<T: Lane, const N: u32>(t: &mut [T; 64], r: usize) { | 377 | fn inverse_dct_n<T: Lane, const N: u32>(t: &mut [T; 64], r: usize) { |
| 251 | permute(t, N); | 378 | permute(t, N); |
| 252 | 379 | ||
| @@ -1063,6 +1190,15 @@ pub fn inverse_transform_2d_typed_wh( | |||
| 1063 | // eight scalar ones. `lr_flip` reads the columns right-to-left, which | 1190 | // eight scalar ones. `lr_flip` reads the columns right-to-left, which |
| 1064 | // is not a contiguous chunk, so it stays on the scalar path below. | 1191 | // is not a contiguous chunk, so it stays on the scalar path below. |
| 1065 | if col_kind == TxType1d::Dct && w >= 8 && !lr_flip { | 1192 | if col_kind == TxType1d::Dct && w >= 8 && !lr_flip { |
| 1193 | + #[cfg(target_arch = "x86_64")] | ||
| 1194 | + if matches!(crate::mc::simd_level(), crate::mc::SimdLevel::Avx2) { | ||
| 1195 | + // SAFETY: the branch above is the AVX2 feature detection. | ||
| 1196 | + #[allow(unsafe_code)] | ||
| 1197 | + unsafe { | ||
| 1198 | + simd::column_pass_dct(residual, &mut out, w, h, log2h, col_clamp, ud_flip); | ||
| 1199 | + } | ||
| 1200 | + return; | ||
| 1201 | + } | ||
| 1066 | let mut t8 = [[0i32; 8]; 64]; | 1202 | let mut t8 = [[0i32; 8]; 64]; |
| 1067 | for j in (0..w).step_by(8) { | 1203 | for j in (0..w).step_by(8) { |
| 1068 | let mut any = 0i32; | 1204 | let mut any = 0i32; |
| @@ -1141,7 +1277,12 @@ pub fn dequant_and_inverse_typed( | |||
| 1141 | ac_delta: i32, | 1277 | ac_delta: i32, |
| 1142 | tx_type: TxType, | 1278 | tx_type: TxType, |
| 1143 | ) -> Vec<i32> { | 1279 | ) -> Vec<i32> { |
| 1144 | - dequant_and_inverse_typed_wh(levels, side, side, bit_depth, q_idx, dc_delta, ac_delta, tx_type) | 1280 | + let r = |
| 1281 | + dequant_and_inverse_typed_wh(levels, side, side, bit_depth, q_idx, dc_delta, ac_delta, tx_type); | ||
| 1282 | + // This square entry point keeps the dense contract (its callers are the | ||
| 1283 | + // encoder-side model and the gates, which index the result directly); | ||
| 1284 | + // only the decoder's `_wh` path speaks the empty marker. | ||
| 1285 | + if r.is_empty() { vec![0i32; side * side] } else { r } | ||
| 1145 | } | 1286 | } |
| 1146 | 1287 | ||
| 1147 | /// [`dequant_and_inverse_typed`] widened to `(w, h)` (lane-recttx). `dc_delta`/ | 1288 | /// [`dequant_and_inverse_typed`] widened to `(w, h)` (lane-recttx). `dc_delta`/ |
| @@ -1165,8 +1306,13 @@ pub fn dequant_and_inverse_typed_wh( | |||
| 1165 | // to a zero residual, which is exactly the buffer below already returns -- | 1306 | // to a zero residual, which is exactly the buffer below already returns -- |
| 1166 | // so one scan of the levels replaces the dequantization, the row pass and | 1307 | // so one scan of the levels replaces the dequantization, the row pass and |
| 1167 | // the column pass for those. | 1308 | // the column pass for those. |
| 1309 | + // The residual is returned EMPTY in that case, not as `w * h` zeros: an | ||
| 1310 | + // empty residual is this decoder's "all zero" marker (`ZERO_RESIDUAL` in | ||
| 1311 | + // `decode.rs`), which also drops the `vec![0; w * h]` memset here and the | ||
| 1312 | + // `to_vec` clone the recorded [`crate::decode::Residual`] made of it -- | ||
| 1313 | + // together 6.5% of the 4K segment's profile (memset + memmove). | ||
| 1168 | if levels.iter().all(|&l| l == 0) { | 1314 | if levels.iter().all(|&l| l == 0) { |
| 1169 | - return vec![0i32; w * h]; | 1315 | + return Vec::new(); |
| 1170 | } | 1316 | } |
| 1171 | DQ_SCRATCH.with(|cell| { | 1317 | DQ_SCRATCH.with(|cell| { |
| 1172 | let mut dq = cell.borrow_mut(); | 1318 | let mut dq = cell.borrow_mut(); |