Merge branch 'lane-tx2' (c084630a): explicit AVX2 Lane impl for the inverse-DCT column pass (exact i64 products, full-range equivalence test; target_feature on the leaf methods), all-zero transform units carry an empty residual marker instead of allocating and cloning zero grids; byte-exact, -4.0% instructions on the 4K segment
Browse files at this commit2 changed files +295 -25
- crates/ec-av1/src/decode.rs +33 -3
- crates/ec-av1/src/transform.rs +262 -22
crates/ec-av1/src/decode.rsmodified+33 -3
| @@ -2088,6 +2088,11 @@ impl TxParams { | ||
| 2088 | 2088 | grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta, |
| 2089 | 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 | 2096 | if self.stride == 0 { |
| 2092 | 2097 | return residual; |
| 2093 | 2098 | } |
| @@ -2108,6 +2113,16 @@ pub(crate) enum Residual { | ||
| 2108 | 2113 | } |
| 2109 | 2114 | |
| 2110 | 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 | 2126 | fn resolve(self) -> Vec<i32> { |
| 2112 | 2127 | match self { |
| 2113 | 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 | 2146 | /// Where a recorded motion-compensated write reads its prediction from. |
| 2120 | 2147 | /// |
| 2121 | 2148 | /// lane-wave4: a block's whole-block prediction (mc + warp + interintra) is |
| @@ -2846,7 +2873,7 @@ fn push_intra_tx( | ||
| 2846 | 2873 | let pred = take_palette_pred(fctx); |
| 2847 | 2874 | push_op(fctx, ReconOp::Intra { |
| 2848 | 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 | 2904 | push_op(fctx, ReconOp::Mc { |
| 2878 | 2905 | plane, x, y, stride: w, w, h, |
| 2879 | 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 | 2909 | return; |
| 2883 | 2910 | } |
| @@ -2918,7 +2945,7 @@ fn push_mc_rect_tx( | ||
| 2918 | 2945 | tx.stride = 0; |
| 2919 | 2946 | push_op(fctx, ReconOp::Mc { |
| 2920 | 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 | 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 | 12839 | for row in 0..bh { |
| 12812 | 12840 | // Same bottom/right frame-edge clip as [`Self::reconstruct`]'s |
| 12813 | 12841 | // (lane-sb128c r8) -- a 64x128 block at the right edge of a frame |
| @@ -12914,6 +12942,7 @@ impl PlaneBuf<'_> { | ||
| 12914 | 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 | 12946 | for row in 0..side { |
| 12918 | 12947 | // lane-sb128c r8: libaom's frame buffer is superblock |
| 12919 | 12948 | // aligned; ours is padded to 32 (`block_grid`), so a 64x64 |
| @@ -21888,6 +21917,7 @@ impl PlaneBuf<'_> { | ||
| 21888 | 21917 | // three row bases are computed once per row instead of a |
| 21889 | 21918 | // multiply-add per sample (the samples themselves are unchanged). |
| 21890 | 21919 | let max = crate::decode::sample_max(fctx); |
| 21920 | + let residual = dense_residual(residual, side * h); | |
| 21891 | 21921 | for row in 0..h { |
| 21892 | 21922 | let src = row * side; |
| 21893 | 21923 | let dst = (y + row) * self.width + x; |
crates/ec-av1/src/transform.rsmodified+262 -22
| @@ -111,13 +111,26 @@ fn clamp_range(x: i32, r: usize) -> i32 { | ||
| 111 | 111 | /// output share the whole network). The two impls are elementwise-identical |
| 112 | 112 | /// integer arithmetic, so the eight-wide pass is byte-exact with the scalar |
| 113 | 113 | /// one by construction -- the network itself is written once, above. |
| 114 | +// `unsafe fn` only so the AVX2 impl can carry `#[target_feature]` (rustc | |
| 115 | +// rejects it on a SAFE trait method); the two scalar impls below require | |
| 116 | +// nothing of their caller. | |
| 117 | +#[allow(unsafe_code)] | |
| 114 | 118 | trait Lane: Copy { |
| 115 | - /// The additive identity, for [`permute`]'s scratch. | |
| 116 | - const ZERO: Self; | |
| 117 | 119 | /// One butterfly rotation's two outputs, at pre-multiplied `cos`/`sin`. |
| 118 | - fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self); | |
| 120 | + /// | |
| 121 | + /// # Safety | |
| 122 | + /// An impl may require CPU features: `M256`'s is `#[target_feature]` AVX2, | |
| 123 | + /// which is the only way its intrinsics compile INLINE -- as plain safe | |
| 124 | + /// methods every `_mm256_*` stayed an out-of-line call (13% of the 4K | |
| 125 | + /// segment's profile). `M256` values exist only inside | |
| 126 | + /// [`simd::column_pass_dct`], which its caller has feature-detected; the | |
| 127 | + /// scalar impls require nothing. | |
| 128 | + unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self); | |
| 119 | 129 | /// One Hadamard rotation's two outputs, clamped to `r` bits. |
| 120 | - fn had(a: Self, b: Self, r: usize) -> (Self, Self); | |
| 130 | + /// | |
| 131 | + /// # Safety | |
| 132 | + /// As [`Lane::btf`]. | |
| 133 | + unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self); | |
| 121 | 134 | } |
| 122 | 135 | |
| 123 | 136 | /// `Round2(x, COS_BITS)` on a butterfly's 64-bit product sum. |
| @@ -125,25 +138,23 @@ fn round_cos(x: i64) -> i32 { | ||
| 125 | 138 | ((x + (1 << (COS_BITS - 1))) >> COS_BITS) as i32 |
| 126 | 139 | } |
| 127 | 140 | |
| 141 | +#[allow(unsafe_code)] | |
| 128 | 142 | impl Lane for i32 { |
| 129 | - const ZERO: Self = 0; | |
| 130 | - | |
| 131 | - fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 143 | + unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 132 | 144 | let (ta, tb) = (i64::from(a), i64::from(b)); |
| 133 | 145 | (round_cos(ta * c - tb * s), round_cos(ta * s + tb * c)) |
| 134 | 146 | } |
| 135 | 147 | |
| 136 | - fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 148 | + unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 137 | 149 | (clamp_range(a.wrapping_add(b), r), clamp_range(a.wrapping_sub(b), r)) |
| 138 | 150 | } |
| 139 | 151 | } |
| 140 | 152 | |
| 141 | 153 | /// Eight columns at once. Every operation is the [`i32`] impl's, lane by lane, |
| 142 | 154 | /// which is what the optimiser turns into one vector op per line. |
| 155 | +#[allow(unsafe_code)] | |
| 143 | 156 | 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) { | |
| 157 | + unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 147 | 158 | let mut x = [0i32; 8]; |
| 148 | 159 | let mut y = [0i32; 8]; |
| 149 | 160 | for l in 0..8 { |
| @@ -154,7 +165,7 @@ impl Lane for [i32; 8] { | ||
| 154 | 165 | (x, y) |
| 155 | 166 | } |
| 156 | 167 | |
| 157 | - fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 168 | + unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 158 | 169 | let mut x = [0i32; 8]; |
| 159 | 170 | let mut y = [0i32; 8]; |
| 160 | 171 | for l in 0..8 { |
| @@ -165,11 +176,145 @@ impl Lane for [i32; 8] { | ||
| 165 | 176 | } |
| 166 | 177 | } |
| 167 | 178 | |
| 179 | +/// The same eight-wide column pass in explicit AVX2. Every operation below is | |
| 180 | +/// the [`i32`] impl's arithmetic, lane for lane, in 64-bit products -- so it is | |
| 181 | +/// byte-exact with the scalar network on every input, not only on conformant | |
| 182 | +/// ones (`the_avx2_dct_matches_the_scalar_one_over_the_full_range`). | |
| 183 | +#[cfg(target_arch = "x86_64")] | |
| 184 | +#[allow(unsafe_code)] | |
| 185 | +mod simd { | |
| 186 | + use super::{COS_BITS, Lane, inverse_dct_n}; | |
| 187 | + use std::arch::x86_64::*; | |
| 188 | + | |
| 189 | + /// Eight `i32` lanes in one AVX2 register. | |
| 190 | + #[derive(Clone, Copy)] | |
| 191 | + pub(super) struct M256(pub(super) __m256i); | |
| 192 | + | |
| 193 | + impl Lane for M256 { | |
| 194 | + /// `round_cos(a*c - b*s)`, `round_cos(a*s + b*c)`. | |
| 195 | + /// | |
| 196 | + /// `_mm256_mul_epi32` multiplies the sign-extended low `i32` of each | |
| 197 | + /// 64-bit lane, so the four even lanes go through directly and the four | |
| 198 | + /// odd ones after a shuffle; the products and their sum are exact in 64 | |
| 199 | + /// bits (`|a| < 2^31`, `|c| <= 2^12`), and only the low 32 bits of the | |
| 200 | + /// shifted sum are kept -- which is what the scalar `as i32` keeps, so | |
| 201 | + /// the logical shift below stands in for the arithmetic one exactly. | |
| 202 | + #[target_feature(enable = "avx2")] | |
| 203 | + unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 204 | + let cv = _mm256_set1_epi32(c as i32); | |
| 205 | + let sv = _mm256_set1_epi32(s as i32); | |
| 206 | + let rnd = _mm256_set1_epi64x(1 << (COS_BITS - 1)); | |
| 207 | + let (ae, be) = (a.0, b.0); | |
| 208 | + let (ao, bo) = (_mm256_shuffle_epi32(a.0, 0xB1), _mm256_shuffle_epi32(b.0, 0xB1)); | |
| 209 | + let half = |x: __m256i, y: __m256i, u: __m256i, v: __m256i| { | |
| 210 | + let p = _mm256_sub_epi64(_mm256_mul_epi32(x, u), _mm256_mul_epi32(y, v)); | |
| 211 | + let q = _mm256_add_epi64(_mm256_mul_epi32(x, v), _mm256_mul_epi32(y, u)); | |
| 212 | + ( | |
| 213 | + _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(p, rnd)), | |
| 214 | + _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(q, rnd)), | |
| 215 | + ) | |
| 216 | + }; | |
| 217 | + let (xe, ye) = half(ae, be, cv, sv); | |
| 218 | + let (xo, yo) = half(ao, bo, cv, sv); | |
| 219 | + ( | |
| 220 | + M256(_mm256_blend_epi32::<0xAA>(xe, _mm256_slli_epi64::<32>(xo))), | |
| 221 | + M256(_mm256_blend_epi32::<0xAA>(ye, _mm256_slli_epi64::<32>(yo))), | |
| 222 | + ) | |
| 223 | + } | |
| 224 | + | |
| 225 | + #[target_feature(enable = "avx2")] | |
| 226 | + unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 227 | + let hi = _mm256_set1_epi32(((1i64 << (r - 1)) - 1) as i32); | |
| 228 | + let lo = _mm256_set1_epi32((-(1i64 << (r - 1))) as i32); | |
| 229 | + let clamp = |v| _mm256_min_epi32(_mm256_max_epi32(v, lo), hi); | |
| 230 | + ( | |
| 231 | + M256(clamp(_mm256_add_epi32(a.0, b.0))), | |
| 232 | + M256(clamp(_mm256_sub_epi32(a.0, b.0))), | |
| 233 | + ) | |
| 234 | + } | |
| 235 | + } | |
| 236 | + | |
| 237 | + /// [`super::inverse_dct_fixed`] over eight AVX2 lanes -- the network the | |
| 238 | + /// column pass below runs, reachable on its own for the equivalence gate. | |
| 239 | + /// | |
| 240 | + /// # Safety | |
| 241 | + /// The caller has checked for AVX2 ([`crate::mc::simd_level`]). | |
| 242 | + #[target_feature(enable = "avx2")] | |
| 243 | + pub(super) unsafe fn dct_x8_avx2(t: &mut [M256; 64], n: u32, r: usize) { | |
| 244 | + match n { | |
| 245 | + 2 => inverse_dct_n::<M256, 2>(t, r), | |
| 246 | + 3 => inverse_dct_n::<M256, 3>(t, r), | |
| 247 | + 4 => inverse_dct_n::<M256, 4>(t, r), | |
| 248 | + 5 => inverse_dct_n::<M256, 5>(t, r), | |
| 249 | + 6 => inverse_dct_n::<M256, 6>(t, r), | |
| 250 | + _ => unreachable!("the inverse DCT is defined for 4..64"), | |
| 251 | + } | |
| 252 | + } | |
| 253 | + | |
| 254 | + /// The whole DCT column pass of one transform unit: `residual` is the | |
| 255 | + /// row-pass output (`h` rows of `w`), `out` the residual it writes. | |
| 256 | + /// | |
| 257 | + /// # Safety | |
| 258 | + /// The caller has checked for AVX2 ([`crate::mc::simd_level`]). | |
| 259 | + #[target_feature(enable = "avx2")] | |
| 260 | + pub(super) unsafe fn column_pass_dct( | |
| 261 | + residual: &[i32], | |
| 262 | + out: &mut [i32], | |
| 263 | + w: usize, | |
| 264 | + h: usize, | |
| 265 | + log2h: u32, | |
| 266 | + r: usize, | |
| 267 | + ud_flip: bool, | |
| 268 | + ) { | |
| 269 | + let mut t = [M256(_mm256_setzero_si256()); 64]; | |
| 270 | + for j in (0..w).step_by(8) { | |
| 271 | + let mut any = _mm256_setzero_si256(); | |
| 272 | + for i in 0..h { | |
| 273 | + let v = unsafe { _mm256_loadu_si256(residual[i * w + j..].as_ptr().cast()) }; | |
| 274 | + t[i] = M256(v); | |
| 275 | + any = _mm256_or_si256(any, v); | |
| 276 | + } | |
| 277 | + // Zero in, zero out -- and `out` is already zero here. | |
| 278 | + if _mm256_testz_si256(any, any) == 1 { | |
| 279 | + continue; | |
| 280 | + } | |
| 281 | + // SAFETY: this function's own AVX2 contract. | |
| 282 | + unsafe { dct_x8_avx2(&mut t, log2h, r) }; | |
| 283 | + // `round2(v, 4)` on eight lanes: the shift is arithmetic here | |
| 284 | + // because the value stays in its own 32-bit lane. | |
| 285 | + let rnd = _mm256_set1_epi32(8); | |
| 286 | + for i in 0..h { | |
| 287 | + let dst_row = if ud_flip { h - 1 - i } else { i }; | |
| 288 | + let v = _mm256_srai_epi32::<4>(_mm256_add_epi32(t[i].0, rnd)); | |
| 289 | + unsafe { _mm256_storeu_si256(out[dst_row * w + j..].as_mut_ptr().cast(), v) }; | |
| 290 | + } | |
| 291 | + } | |
| 292 | + } | |
| 293 | + | |
| 294 | + /// The eight scalar lanes of `v`, for the equivalence gate. | |
| 295 | + #[cfg(test)] | |
| 296 | + pub(super) fn lanes(v: M256) -> [i32; 8] { | |
| 297 | + let mut o = [0i32; 8]; | |
| 298 | + unsafe { _mm256_storeu_si256(o.as_mut_ptr().cast(), v.0) }; | |
| 299 | + o | |
| 300 | + } | |
| 301 | + | |
| 302 | + /// `M256` from eight lanes, for the equivalence gate. | |
| 303 | + #[cfg(test)] | |
| 304 | + pub(super) fn splat(v: [i32; 8]) -> M256 { | |
| 305 | + unsafe { M256(_mm256_loadu_si256(v.as_ptr().cast())) } | |
| 306 | + } | |
| 307 | +} | |
| 308 | + | |
| 168 | 309 | /// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an |
| 169 | 310 | /// exchange of the two entries when `flip` is set. |
| 311 | +#[inline(always)] | |
| 170 | 312 | fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { |
| 171 | 313 | let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle))); |
| 172 | - let (x, y) = T::btf(t[a], t[b], c, s); | |
| 314 | + // SAFETY: [`Lane::btf`] -- an `M256` reaches here only from the | |
| 315 | + // feature-detected [`simd::column_pass_dct`]. | |
| 316 | + #[allow(unsafe_code)] | |
| 317 | + let (x, y) = unsafe { T::btf(t[a], t[b], c, s) }; | |
| 173 | 318 | t[a] = x; |
| 174 | 319 | t[b] = y; |
| 175 | 320 | if flip { |
| @@ -179,24 +324,30 @@ fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: boo | ||
| 179 | 324 | |
| 180 | 325 | /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices |
| 181 | 326 | /// exchanged when `flip` is set. |
| 327 | +#[inline(always)] | |
| 182 | 328 | fn hadamard<T: Lane>(t: &mut [T; 64], a: usize, b: usize, flip: bool, r: usize) { |
| 183 | 329 | let (a, b) = if flip { (b, a) } else { (a, b) }; |
| 184 | - let (x, y) = T::had(t[a], t[b], r); | |
| 330 | + // SAFETY: as [`butterfly`]'s. | |
| 331 | + #[allow(unsafe_code)] | |
| 332 | + let (x, y) = unsafe { T::had(t[a], t[b], r) }; | |
| 185 | 333 | t[a] = x; |
| 186 | 334 | t[b] = y; |
| 187 | 335 | } |
| 188 | 336 | |
| 189 | 337 | /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal |
| 190 | 338 | /// of the first `1 << n` entries. |
| 339 | +#[inline(always)] | |
| 191 | 340 | fn permute<T: Lane>(t: &mut [T; 64], n: u32) { |
| 192 | 341 | let n0 = 1usize << n; |
| 193 | - // A stack copy: this ran once per 1D transform and its `Vec` was the | |
| 194 | - // `malloc` at the top of every `inverse_dct` frame in the annotation. | |
| 195 | - let mut copy = [T::ZERO; 64]; | |
| 196 | - copy[..n0].copy_from_slice(&t[..n0]); | |
| 342 | + // In place: a bit reversal is an involution, so swapping every `i` with | |
| 343 | + // `brev(i) > i` visits each transposed pair once. The scratch copy this | |
| 344 | + // replaces was a `[T; 64]` fill -- 2 KB per call at `T = M256`. | |
| 197 | 345 | let tab = &BREV_TAB[n as usize]; |
| 198 | - for (i, v) in t[..n0].iter_mut().enumerate() { | |
| 199 | - *v = copy[tab[i] as usize]; | |
| 346 | + for i in 0..n0 { | |
| 347 | + let j = tab[i] as usize; | |
| 348 | + if j > i { | |
| 349 | + t.swap(i, j); | |
| 350 | + } | |
| 200 | 351 | } |
| 201 | 352 | } |
| 202 | 353 | |
| @@ -247,6 +398,11 @@ fn inverse_dct_x8(t: &mut [[i32; 8]; 64], n: u32, r: usize) { | ||
| 247 | 398 | } |
| 248 | 399 | |
| 249 | 400 | /// [`inverse_dct_fixed`] at one fixed `N = log2(size)`. |
| 401 | +// `#[inline(always)]`: the AVX2 instantiation has to land INSIDE the | |
| 402 | +// `target_feature` wrapper, or every `_mm256_*` in it stays an out-of-line | |
| 403 | +// call (measured: +5.6% instructions on the 4K segment). Every other | |
| 404 | +// instantiation has exactly one caller, so this costs no extra copy. | |
| 405 | +#[inline(always)] | |
| 250 | 406 | fn inverse_dct_n<T: Lane, const N: u32>(t: &mut [T; 64], r: usize) { |
| 251 | 407 | permute(t, N); |
| 252 | 408 | |
| @@ -1063,6 +1219,15 @@ pub fn inverse_transform_2d_typed_wh( | ||
| 1063 | 1219 | // eight scalar ones. `lr_flip` reads the columns right-to-left, which |
| 1064 | 1220 | // is not a contiguous chunk, so it stays on the scalar path below. |
| 1065 | 1221 | if col_kind == TxType1d::Dct && w >= 8 && !lr_flip { |
| 1222 | + #[cfg(target_arch = "x86_64")] | |
| 1223 | + if matches!(crate::mc::simd_level(), crate::mc::SimdLevel::Avx2) { | |
| 1224 | + // SAFETY: the branch above is the AVX2 feature detection. | |
| 1225 | + #[allow(unsafe_code)] | |
| 1226 | + unsafe { | |
| 1227 | + simd::column_pass_dct(residual, &mut out, w, h, log2h, col_clamp, ud_flip); | |
| 1228 | + } | |
| 1229 | + return; | |
| 1230 | + } | |
| 1066 | 1231 | let mut t8 = [[0i32; 8]; 64]; |
| 1067 | 1232 | for j in (0..w).step_by(8) { |
| 1068 | 1233 | let mut any = 0i32; |
| @@ -1141,7 +1306,12 @@ pub fn dequant_and_inverse_typed( | ||
| 1141 | 1306 | ac_delta: i32, |
| 1142 | 1307 | tx_type: TxType, |
| 1143 | 1308 | ) -> Vec<i32> { |
| 1144 | - dequant_and_inverse_typed_wh(levels, side, side, bit_depth, q_idx, dc_delta, ac_delta, tx_type) | |
| 1309 | + let r = | |
| 1310 | + dequant_and_inverse_typed_wh(levels, side, side, bit_depth, q_idx, dc_delta, ac_delta, tx_type); | |
| 1311 | + // This square entry point keeps the dense contract (its callers are the | |
| 1312 | + // encoder-side model and the gates, which index the result directly); | |
| 1313 | + // only the decoder's `_wh` path speaks the empty marker. | |
| 1314 | + if r.is_empty() { vec![0i32; side * side] } else { r } | |
| 1145 | 1315 | } |
| 1146 | 1316 | |
| 1147 | 1317 | /// [`dequant_and_inverse_typed`] widened to `(w, h)` (lane-recttx). `dc_delta`/ |
| @@ -1165,8 +1335,13 @@ pub fn dequant_and_inverse_typed_wh( | ||
| 1165 | 1335 | // to a zero residual, which is exactly the buffer below already returns -- |
| 1166 | 1336 | // so one scan of the levels replaces the dequantization, the row pass and |
| 1167 | 1337 | // the column pass for those. |
| 1338 | + // The residual is returned EMPTY in that case, not as `w * h` zeros: an | |
| 1339 | + // empty residual is this decoder's "all zero" marker (`ZERO_RESIDUAL` in | |
| 1340 | + // `decode.rs`), which also drops the `vec![0; w * h]` memset here and the | |
| 1341 | + // `to_vec` clone the recorded [`crate::decode::Residual`] made of it -- | |
| 1342 | + // together 6.5% of the 4K segment's profile (memset + memmove). | |
| 1168 | 1343 | if levels.iter().all(|&l| l == 0) { |
| 1169 | - return vec![0i32; w * h]; | |
| 1344 | + return Vec::new(); | |
| 1170 | 1345 | } |
| 1171 | 1346 | DQ_SCRATCH.with(|cell| { |
| 1172 | 1347 | let mut dq = cell.borrow_mut(); |
| @@ -2019,6 +2194,71 @@ mod tests { | ||
| 2019 | 2194 | /// The eight-wide column kernel is the scalar network, lane for lane: a |
| 2020 | 2195 | /// pseudo-random sweep over the full coefficient range at every size and |
| 2021 | 2196 | /// both clamp widths (`bit_depth` 8 and 10) has to agree bit for bit, or |
| 2197 | + /// The AVX2 column pass against the scalar network over the FULL `i32` | |
| 2198 | + /// range, not only the dequantizer's own bound: the explicit intrinsics | |
| 2199 | + /// claim exact 64-bit products, so the sweep includes `i32::MIN`/`MAX`, | |
| 2200 | + /// both clamp boundaries at every `r` the decoder uses, and uniform | |
| 2201 | + /// full-range noise. A single differing lane at any size would be a | |
| 2202 | + /// silent pixel drift on whatever stream first reaches it. | |
| 2203 | + #[cfg(target_arch = "x86_64")] | |
| 2204 | + #[test] | |
| 2205 | + fn the_avx2_dct_matches_the_scalar_one_over_the_full_range() { | |
| 2206 | + if !std::arch::is_x86_feature_detected!("avx2") { | |
| 2207 | + return; | |
| 2208 | + } | |
| 2209 | + let mut state = 0x9E37_79B9_7F4A_7C15u64; | |
| 2210 | + let mut next = move || { | |
| 2211 | + state ^= state << 13; | |
| 2212 | + state ^= state >> 7; | |
| 2213 | + state ^= state << 17; | |
| 2214 | + state | |
| 2215 | + }; | |
| 2216 | + for n in 2..=6u32 { | |
| 2217 | + for &r in &[16usize, 18, 20] { | |
| 2218 | + let (hi, lo) = (((1i64 << (r - 1)) - 1) as i32, (-(1i64 << (r - 1))) as i32); | |
| 2219 | + let edges = [0, 1, -1, hi, lo, hi - 1, lo + 1, i32::MAX, i32::MIN]; | |
| 2220 | + for round in 0..96 { | |
| 2221 | + let mut wide = [[0i32; 8]; 64]; | |
| 2222 | + let mut cols = [[0i32; 64]; 8]; | |
| 2223 | + for i in 0..(1usize << n) { | |
| 2224 | + for l in 0..8 { | |
| 2225 | + let raw = next(); | |
| 2226 | + let v = match round % 3 { | |
| 2227 | + // every extreme, and every pair of them | |
| 2228 | + 0 => edges[(raw >> 32) as usize % edges.len()], | |
| 2229 | + // uniform over the whole 32-bit range | |
| 2230 | + 1 => raw as i32, | |
| 2231 | + // the range a conformant stream stays inside | |
| 2232 | + _ => raw as i32 % (hi / 2 + 1), | |
| 2233 | + }; | |
| 2234 | + wide[i][l] = v; | |
| 2235 | + cols[l][i] = v; | |
| 2236 | + } | |
| 2237 | + } | |
| 2238 | + let mut avx = [simd::splat([0i32; 8]); 64]; | |
| 2239 | + for i in 0..(1usize << n) { | |
| 2240 | + avx[i] = simd::splat(wide[i]); | |
| 2241 | + } | |
| 2242 | + // SAFETY: guarded by the feature detection above. | |
| 2243 | + #[allow(unsafe_code)] | |
| 2244 | + unsafe { | |
| 2245 | + simd::dct_x8_avx2(&mut avx, n, r); | |
| 2246 | + } | |
| 2247 | + for (l, col) in cols.iter_mut().enumerate() { | |
| 2248 | + inverse_dct_fixed(col, n, r); | |
| 2249 | + for i in 0..(1usize << n) { | |
| 2250 | + assert_eq!( | |
| 2251 | + simd::lanes(avx[i])[l], | |
| 2252 | + col[i], | |
| 2253 | + "avx2 lane {l} of entry {i} at n={n} r={r} round={round}" | |
| 2254 | + ); | |
| 2255 | + } | |
| 2256 | + } | |
| 2257 | + } | |
| 2258 | + } | |
| 2259 | + } | |
| 2260 | + } | |
| 2261 | + | |
| 2022 | 2262 | /// the column pass would drift from the decoder on some block this |
| 2023 | 2263 | /// crate's gate streams happen not to contain. |
| 2024 | 2264 | #[test] |