Merge branch 'lane-tx' (07c71e86): inverse DCT monomorphised per size, eight-wide column pass through a Lane trait, all-zero transform grids skipped (60% of 4K transform units); byte-exact, -5.2% instructions on the 4K segment
Browse files at this commit1 changed file +209 -42
- crates/ec-av1/src/transform.rs +209 -42
crates/ec-av1/src/transform.rsmodified+209 -42
| @@ -105,15 +105,73 @@ fn clamp_range(x: i32, r: usize) -> i32 { | |||
| 105 | x.clamp(lo, hi) | 105 | x.clamp(lo, hi) |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | +/// One value the DCT network carries: a single coefficient (the row pass, and | ||
| 109 | +/// every caller of the slice-taking [`inverse_dct`]) or eight neighbouring | ||
| 110 | +/// columns at once (the column pass, where eight columns of the row-pass | ||
| 111 | +/// output share the whole network). The two impls are elementwise-identical | ||
| 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. | ||
| 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`. | ||
| 118 | + fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self); | ||
| 119 | + /// One Hadamard rotation's two outputs, clamped to `r` bits. | ||
| 120 | + fn had(a: Self, b: Self, r: usize) -> (Self, Self); | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | +/// `Round2(x, COS_BITS)` on a butterfly's 64-bit product sum. | ||
| 124 | +fn round_cos(x: i64) -> i32 { | ||
| 125 | + ((x + (1 << (COS_BITS - 1))) >> COS_BITS) as i32 | ||
| 126 | +} | ||
| 127 | + | ||
| 128 | +impl Lane for i32 { | ||
| 129 | + const ZERO: Self = 0; | ||
| 130 | + | ||
| 131 | + fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | ||
| 132 | + let (ta, tb) = (i64::from(a), i64::from(b)); | ||
| 133 | + (round_cos(ta * c - tb * s), round_cos(ta * s + tb * c)) | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | ||
| 137 | + (clamp_range(a.wrapping_add(b), r), clamp_range(a.wrapping_sub(b), r)) | ||
| 138 | + } | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +/// 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. | ||
| 143 | +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) { | ||
| 147 | + let mut x = [0i32; 8]; | ||
| 148 | + let mut y = [0i32; 8]; | ||
| 149 | + for l in 0..8 { | ||
| 150 | + let (ta, tb) = (i64::from(a[l]), i64::from(b[l])); | ||
| 151 | + x[l] = round_cos(ta * c - tb * s); | ||
| 152 | + y[l] = round_cos(ta * s + tb * c); | ||
| 153 | + } | ||
| 154 | + (x, y) | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | ||
| 158 | + let mut x = [0i32; 8]; | ||
| 159 | + let mut y = [0i32; 8]; | ||
| 160 | + for l in 0..8 { | ||
| 161 | + x[l] = clamp_range(a[l].wrapping_add(b[l]), r); | ||
| 162 | + y[l] = clamp_range(a[l].wrapping_sub(b[l]), r); | ||
| 163 | + } | ||
| 164 | + (x, y) | ||
| 165 | + } | ||
| 166 | +} | ||
| 167 | + | ||
| 108 | /// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an | 168 | /// `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. | 169 | /// exchange of the two entries when `flip` is set. |
| 110 | -fn butterfly(t: &mut [i32; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { | 170 | +fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { |
| 111 | - let (ta, tb) = (i64::from(t[a]), i64::from(t[b])); | ||
| 112 | let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle))); | 171 | let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle))); |
| 113 | - let x = ta * c - tb * s; | 172 | + let (x, y) = T::btf(t[a], t[b], c, s); |
| 114 | - let y = ta * s + tb * c; | 173 | + t[a] = x; |
| 115 | - t[a] = ((x + (1 << (COS_BITS - 1))) >> COS_BITS) as i32; | 174 | + t[b] = y; |
| 116 | - t[b] = ((y + (1 << (COS_BITS - 1))) >> COS_BITS) as i32; | ||
| 117 | if flip { | 175 | if flip { |
| 118 | t.swap(a, b); | 176 | t.swap(a, b); |
| 119 | } | 177 | } |
| @@ -121,20 +179,20 @@ fn butterfly(t: &mut [i32; 64], a: usize, b: usize, angle: i32, flip: bool, _r: | |||
| 121 | 179 | ||
| 122 | /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices | 180 | /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices |
| 123 | /// exchanged when `flip` is set. | 181 | /// exchanged when `flip` is set. |
| 124 | -fn hadamard(t: &mut [i32; 64], a: usize, b: usize, flip: bool, r: usize) { | 182 | +fn hadamard<T: Lane>(t: &mut [T; 64], a: usize, b: usize, flip: bool, r: usize) { |
| 125 | let (a, b) = if flip { (b, a) } else { (a, b) }; | 183 | let (a, b) = if flip { (b, a) } else { (a, b) }; |
| 126 | - let (x, y) = (t[a], t[b]); | 184 | + let (x, y) = T::had(t[a], t[b], r); |
| 127 | - t[a] = clamp_range(x.wrapping_add(y), r); | 185 | + t[a] = x; |
| 128 | - t[b] = clamp_range(x.wrapping_sub(y), r); | 186 | + t[b] = y; |
| 129 | } | 187 | } |
| 130 | 188 | ||
| 131 | /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal | 189 | /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal |
| 132 | /// of the first `1 << n` entries. | 190 | /// of the first `1 << n` entries. |
| 133 | -fn permute(t: &mut [i32; 64], n: u32) { | 191 | +fn permute<T: Lane>(t: &mut [T; 64], n: u32) { |
| 134 | let n0 = 1usize << n; | 192 | let n0 = 1usize << n; |
| 135 | // A stack copy: this ran once per 1D transform and its `Vec` was the | 193 | // 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. | 194 | // `malloc` at the top of every `inverse_dct` frame in the annotation. |
| 137 | - let mut copy = [0i32; 64]; | 195 | + let mut copy = [T::ZERO; 64]; |
| 138 | copy[..n0].copy_from_slice(&t[..n0]); | 196 | copy[..n0].copy_from_slice(&t[..n0]); |
| 139 | let tab = &BREV_TAB[n as usize]; | 197 | let tab = &BREV_TAB[n as usize]; |
| 140 | for (i, v) in t[..n0].iter_mut().enumerate() { | 198 | for (i, v) in t[..n0].iter_mut().enumerate() { |
| @@ -161,16 +219,43 @@ pub fn inverse_dct(t: &mut [i32], n: u32, r: usize) { | |||
| 161 | } | 219 | } |
| 162 | 220 | ||
| 163 | /// [`inverse_dct`] on the driver's own 64-entry scratch. | 221 | /// [`inverse_dct`] on the driver's own 64-entry scratch. |
| 222 | +/// [`inverse_dct`] on the driver's own 64-entry scratch: a thin dispatch to | ||
| 223 | +/// [`inverse_dct_n`], which is the same network monomorphised per size so | ||
| 224 | +/// the size guards, the `brev` folds and every butterfly angle become | ||
| 225 | +/// compile-time constants instead of per-call work. | ||
| 164 | fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { | 226 | fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { |
| 165 | - assert!((2..=6).contains(&n), "the inverse DCT is defined for 4..64"); | 227 | + match n { |
| 166 | - permute(t, n); | 228 | + 2 => inverse_dct_n::<i32, 2>(t, r), |
| 229 | + 3 => inverse_dct_n::<i32, 3>(t, r), | ||
| 230 | + 4 => inverse_dct_n::<i32, 4>(t, r), | ||
| 231 | + 5 => inverse_dct_n::<i32, 5>(t, r), | ||
| 232 | + 6 => inverse_dct_n::<i32, 6>(t, r), | ||
| 233 | + _ => unreachable!("the inverse DCT is defined for 4..64"), | ||
| 234 | + } | ||
| 235 | +} | ||
| 167 | 236 | ||
| 168 | - if n == 6 { | 237 | +/// [`inverse_dct_fixed`] over eight columns at once (the column pass). |
| 238 | +fn inverse_dct_x8(t: &mut [[i32; 8]; 64], n: u32, r: usize) { | ||
| 239 | + match n { | ||
| 240 | + 2 => inverse_dct_n::<[i32; 8], 2>(t, r), | ||
| 241 | + 3 => inverse_dct_n::<[i32; 8], 3>(t, r), | ||
| 242 | + 4 => inverse_dct_n::<[i32; 8], 4>(t, r), | ||
| 243 | + 5 => inverse_dct_n::<[i32; 8], 5>(t, r), | ||
| 244 | + 6 => inverse_dct_n::<[i32; 8], 6>(t, r), | ||
| 245 | + _ => unreachable!("the inverse DCT is defined for 4..64"), | ||
| 246 | + } | ||
| 247 | +} | ||
| 248 | + | ||
| 249 | +/// [`inverse_dct_fixed`] at one fixed `N = log2(size)`. | ||
| 250 | +fn inverse_dct_n<T: Lane, const N: u32>(t: &mut [T; 64], r: usize) { | ||
| 251 | + permute(t, N); | ||
| 252 | + | ||
| 253 | + if N == 6 { | ||
| 169 | for i in 0..16 { | 254 | for i in 0..16 { |
| 170 | butterfly(t, 32 + i, 63 - i, 63 - 4 * brev(4, i) as i32, false, r); | 255 | butterfly(t, 32 + i, 63 - i, 63 - 4 * brev(4, i) as i32, false, r); |
| 171 | } | 256 | } |
| 172 | } | 257 | } |
| 173 | - if n >= 5 { | 258 | + if N >= 5 { |
| 174 | for i in 0..8 { | 259 | for i in 0..8 { |
| 175 | butterfly( | 260 | butterfly( |
| 176 | t, | 261 | t, |
| @@ -182,12 +267,12 @@ fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { | |||
| 182 | ); | 267 | ); |
| 183 | } | 268 | } |
| 184 | } | 269 | } |
| 185 | - if n == 6 { | 270 | + if N == 6 { |
| 186 | for i in 0..16 { | 271 | for i in 0..16 { |
| 187 | hadamard(t, 32 + i * 2, 33 + i * 2, i & 1 == 1, r); | 272 | hadamard(t, 32 + i * 2, 33 + i * 2, i & 1 == 1, r); |
| 188 | } | 273 | } |
| 189 | } | 274 | } |
| 190 | - if n >= 4 { | 275 | + if N >= 4 { |
| 191 | for i in 0..4 { | 276 | for i in 0..4 { |
| 192 | butterfly( | 277 | butterfly( |
| 193 | t, | 278 | t, |
| @@ -199,12 +284,12 @@ fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { | |||
| 199 | ); | 284 | ); |
| 200 | } | 285 | } |
| 201 | } | 286 | } |
| 202 | - if n >= 5 { | 287 | + if N >= 5 { |
| 203 | for i in 0..8 { | 288 | for i in 0..8 { |
| 204 | hadamard(t, 16 + 2 * i, 17 + 2 * i, i & 1 == 1, r); | 289 | hadamard(t, 16 + 2 * i, 17 + 2 * i, i & 1 == 1, r); |
| 205 | } | 290 | } |
| 206 | } | 291 | } |
| 207 | - if n == 6 { | 292 | + if N == 6 { |
| 208 | for i in 0..4 { | 293 | for i in 0..4 { |
| 209 | for j in 0..2 { | 294 | for j in 0..2 { |
| 210 | let angle = 60 - 16 * brev(2, i) as i32 + 64 * j as i32; | 295 | let angle = 60 - 16 * brev(2, i) as i32 + 64 * j as i32; |
| @@ -212,17 +297,17 @@ fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { | |||
| 212 | } | 297 | } |
| 213 | } | 298 | } |
| 214 | } | 299 | } |
| 215 | - if n >= 3 { | 300 | + if N >= 3 { |
| 216 | for i in 0..2 { | 301 | for i in 0..2 { |
| 217 | butterfly(t, 4 + i, 7 - i, 56 - 32 * i as i32, false, r); | 302 | butterfly(t, 4 + i, 7 - i, 56 - 32 * i as i32, false, r); |
| 218 | } | 303 | } |
| 219 | } | 304 | } |
| 220 | - if n >= 4 { | 305 | + if N >= 4 { |
| 221 | for i in 0..4 { | 306 | for i in 0..4 { |
| 222 | hadamard(t, 8 + 2 * i, 9 + 2 * i, i & 1 == 1, r); | 307 | hadamard(t, 8 + 2 * i, 9 + 2 * i, i & 1 == 1, r); |
| 223 | } | 308 | } |
| 224 | } | 309 | } |
| 225 | - if n >= 5 { | 310 | + if N >= 5 { |
| 226 | for i in 0..2 { | 311 | for i in 0..2 { |
| 227 | for j in 0..2 { | 312 | for j in 0..2 { |
| 228 | let angle = 24 + ((j as i32) << 6) + ((1 - i as i32) << 5); | 313 | let angle = 24 + ((j as i32) << 6) + ((1 - i as i32) << 5); |
| @@ -230,7 +315,7 @@ fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { | |||
| 230 | } | 315 | } |
| 231 | } | 316 | } |
| 232 | } | 317 | } |
| 233 | - if n == 6 { | 318 | + if N == 6 { |
| 234 | for i in 0..8 { | 319 | for i in 0..8 { |
| 235 | for j in 0..2 { | 320 | for j in 0..2 { |
| 236 | hadamard(t, 32 + i * 4 + j, 35 + i * 4 - j, i & 1 == 1, r); | 321 | hadamard(t, 32 + i * 4 + j, 35 + i * 4 - j, i & 1 == 1, r); |
| @@ -240,24 +325,24 @@ fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { | |||
| 240 | for i in 0..2 { | 325 | for i in 0..2 { |
| 241 | butterfly(t, 2 * i, 2 * i + 1, 32 + 16 * i as i32, i == 0, r); | 326 | butterfly(t, 2 * i, 2 * i + 1, 32 + 16 * i as i32, i == 0, r); |
| 242 | } | 327 | } |
| 243 | - if n >= 3 { | 328 | + if N >= 3 { |
| 244 | for i in 0..2 { | 329 | for i in 0..2 { |
| 245 | hadamard(t, 4 + 2 * i, 5 + 2 * i, i == 1, r); | 330 | hadamard(t, 4 + 2 * i, 5 + 2 * i, i == 1, r); |
| 246 | } | 331 | } |
| 247 | } | 332 | } |
| 248 | - if n >= 4 { | 333 | + if N >= 4 { |
| 249 | for i in 0..2 { | 334 | for i in 0..2 { |
| 250 | butterfly(t, 14 - i, 9 + i, 48 + 64 * i as i32, true, r); | 335 | butterfly(t, 14 - i, 9 + i, 48 + 64 * i as i32, true, r); |
| 251 | } | 336 | } |
| 252 | } | 337 | } |
| 253 | - if n >= 5 { | 338 | + if N >= 5 { |
| 254 | for i in 0..4 { | 339 | for i in 0..4 { |
| 255 | for j in 0..2 { | 340 | for j in 0..2 { |
| 256 | hadamard(t, 16 + 4 * i + j, 19 + 4 * i - j, i & 1 == 1, r); | 341 | hadamard(t, 16 + 4 * i + j, 19 + 4 * i - j, i & 1 == 1, r); |
| 257 | } | 342 | } |
| 258 | } | 343 | } |
| 259 | } | 344 | } |
| 260 | - if n == 6 { | 345 | + if N == 6 { |
| 261 | for i in 0..2 { | 346 | for i in 0..2 { |
| 262 | for j in 0..4 { | 347 | for j in 0..4 { |
| 263 | let angle = 56 - (i as i32) * 32 + ((j as i32) >> 1) * 64; | 348 | let angle = 56 - (i as i32) * 32 + ((j as i32) >> 1) * 64; |
| @@ -268,77 +353,77 @@ fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { | |||
| 268 | for i in 0..2 { | 353 | for i in 0..2 { |
| 269 | hadamard(t, i, 3 - i, false, r); | 354 | hadamard(t, i, 3 - i, false, r); |
| 270 | } | 355 | } |
| 271 | - if n >= 3 { | 356 | + if N >= 3 { |
| 272 | butterfly(t, 6, 5, 32, true, r); | 357 | butterfly(t, 6, 5, 32, true, r); |
| 273 | } | 358 | } |
| 274 | - if n >= 4 { | 359 | + if N >= 4 { |
| 275 | for i in 0..2 { | 360 | for i in 0..2 { |
| 276 | for j in 0..2 { | 361 | for j in 0..2 { |
| 277 | hadamard(t, 8 + 4 * i + j, 11 + 4 * i - j, i == 1, r); | 362 | hadamard(t, 8 + 4 * i + j, 11 + 4 * i - j, i == 1, r); |
| 278 | } | 363 | } |
| 279 | } | 364 | } |
| 280 | } | 365 | } |
| 281 | - if n >= 5 { | 366 | + if N >= 5 { |
| 282 | for i in 0..4 { | 367 | for i in 0..4 { |
| 283 | butterfly(t, 29 - i, 18 + i, 48 + ((i as i32) >> 1) * 64, true, r); | 368 | butterfly(t, 29 - i, 18 + i, 48 + ((i as i32) >> 1) * 64, true, r); |
| 284 | } | 369 | } |
| 285 | } | 370 | } |
| 286 | - if n == 6 { | 371 | + if N == 6 { |
| 287 | for i in 0..4 { | 372 | for i in 0..4 { |
| 288 | for j in 0..4 { | 373 | for j in 0..4 { |
| 289 | hadamard(t, 32 + 8 * i + j, 39 + 8 * i - j, i & 1 == 1, r); | 374 | hadamard(t, 32 + 8 * i + j, 39 + 8 * i - j, i & 1 == 1, r); |
| 290 | } | 375 | } |
| 291 | } | 376 | } |
| 292 | } | 377 | } |
| 293 | - if n >= 3 { | 378 | + if N >= 3 { |
| 294 | for i in 0..4 { | 379 | for i in 0..4 { |
| 295 | hadamard(t, i, 7 - i, false, r); | 380 | hadamard(t, i, 7 - i, false, r); |
| 296 | } | 381 | } |
| 297 | } | 382 | } |
| 298 | - if n >= 4 { | 383 | + if N >= 4 { |
| 299 | for i in 0..2 { | 384 | for i in 0..2 { |
| 300 | butterfly(t, 13 - i, 10 + i, 32, true, r); | 385 | butterfly(t, 13 - i, 10 + i, 32, true, r); |
| 301 | } | 386 | } |
| 302 | } | 387 | } |
| 303 | - if n >= 5 { | 388 | + if N >= 5 { |
| 304 | for i in 0..2 { | 389 | for i in 0..2 { |
| 305 | for j in 0..4 { | 390 | for j in 0..4 { |
| 306 | hadamard(t, 16 + i * 8 + j, 23 + i * 8 - j, i == 1, r); | 391 | hadamard(t, 16 + i * 8 + j, 23 + i * 8 - j, i == 1, r); |
| 307 | } | 392 | } |
| 308 | } | 393 | } |
| 309 | } | 394 | } |
| 310 | - if n == 6 { | 395 | + if N == 6 { |
| 311 | for i in 0..8 { | 396 | for i in 0..8 { |
| 312 | butterfly(t, 59 - i, 36 + i, if i < 4 { 48 } else { 112 }, true, r); | 397 | butterfly(t, 59 - i, 36 + i, if i < 4 { 48 } else { 112 }, true, r); |
| 313 | } | 398 | } |
| 314 | } | 399 | } |
| 315 | - if n >= 4 { | 400 | + if N >= 4 { |
| 316 | for i in 0..8 { | 401 | for i in 0..8 { |
| 317 | hadamard(t, i, 15 - i, false, r); | 402 | hadamard(t, i, 15 - i, false, r); |
| 318 | } | 403 | } |
| 319 | } | 404 | } |
| 320 | - if n >= 5 { | 405 | + if N >= 5 { |
| 321 | for i in 0..4 { | 406 | for i in 0..4 { |
| 322 | butterfly(t, 27 - i, 20 + i, 32, true, r); | 407 | butterfly(t, 27 - i, 20 + i, 32, true, r); |
| 323 | } | 408 | } |
| 324 | } | 409 | } |
| 325 | - if n == 6 { | 410 | + if N == 6 { |
| 326 | for i in 0..8 { | 411 | for i in 0..8 { |
| 327 | hadamard(t, 32 + i, 47 - i, false, r); | 412 | hadamard(t, 32 + i, 47 - i, false, r); |
| 328 | hadamard(t, 48 + i, 63 - i, true, r); | 413 | hadamard(t, 48 + i, 63 - i, true, r); |
| 329 | } | 414 | } |
| 330 | } | 415 | } |
| 331 | - if n >= 5 { | 416 | + if N >= 5 { |
| 332 | for i in 0..16 { | 417 | for i in 0..16 { |
| 333 | hadamard(t, i, 31 - i, false, r); | 418 | hadamard(t, i, 31 - i, false, r); |
| 334 | } | 419 | } |
| 335 | } | 420 | } |
| 336 | - if n == 6 { | 421 | + if N == 6 { |
| 337 | for i in 0..8 { | 422 | for i in 0..8 { |
| 338 | butterfly(t, 55 - i, 40 + i, 32, true, r); | 423 | butterfly(t, 55 - i, 40 + i, 32, true, r); |
| 339 | } | 424 | } |
| 340 | } | 425 | } |
| 341 | - if n == 6 { | 426 | + if N == 6 { |
| 342 | for i in 0..32 { | 427 | for i in 0..32 { |
| 343 | hadamard(t, i, 63 - i, false, r); | 428 | hadamard(t, i, 63 - i, false, r); |
| 344 | } | 429 | } |
| @@ -971,6 +1056,38 @@ pub fn inverse_transform_2d_typed_wh( | |||
| 971 | return; | 1056 | return; |
| 972 | } | 1057 | } |
| 973 | 1058 | ||
| 1059 | + // The column pass over a plain-DCT axis takes eight neighbouring | ||
| 1060 | + // columns through one network ([`Lane`]): eight columns of one | ||
| 1061 | + // row-pass row are eight adjacent `i32`s, so the gather is a | ||
| 1062 | + // contiguous load and every butterfly is one vector op instead of | ||
| 1063 | + // 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. | ||
| 1065 | + if col_kind == TxType1d::Dct && w >= 8 && !lr_flip { | ||
| 1066 | + let mut t8 = [[0i32; 8]; 64]; | ||
| 1067 | + for j in (0..w).step_by(8) { | ||
| 1068 | + let mut any = 0i32; | ||
| 1069 | + for i in 0..h { | ||
| 1070 | + let src = &residual[i * w + j..][..8]; | ||
| 1071 | + for (d, &v) in t8[i].iter_mut().zip(src) { | ||
| 1072 | + *d = v; | ||
| 1073 | + any |= v; | ||
| 1074 | + } | ||
| 1075 | + } | ||
| 1076 | + if any == 0 { | ||
| 1077 | + continue; | ||
| 1078 | + } | ||
| 1079 | + inverse_dct_x8(&mut t8, log2h, col_clamp); | ||
| 1080 | + for i in 0..h { | ||
| 1081 | + let dst_row = if ud_flip { h - 1 - i } else { i }; | ||
| 1082 | + let dst = &mut out[dst_row * w + j..][..8]; | ||
| 1083 | + for (d, &v) in dst.iter_mut().zip(t8[i].iter()) { | ||
| 1084 | + *d = round2(v, 4); | ||
| 1085 | + } | ||
| 1086 | + } | ||
| 1087 | + } | ||
| 1088 | + return; | ||
| 1089 | + } | ||
| 1090 | + | ||
| 974 | for j in 0..w { | 1091 | for j in 0..w { |
| 975 | let src_col = if lr_flip { w - 1 - j } else { j }; | 1092 | let src_col = if lr_flip { w - 1 - j } else { j }; |
| 976 | let mut any = 0i32; | 1093 | let mut any = 0i32; |
| @@ -1042,6 +1159,15 @@ pub fn dequant_and_inverse_typed_wh( | |||
| 1042 | ac_delta: i32, | 1159 | ac_delta: i32, |
| 1043 | tx_type: TxType, | 1160 | tx_type: TxType, |
| 1044 | ) -> Vec<i32> { | 1161 | ) -> Vec<i32> { |
| 1162 | + // 60% of the transform units of a 4K inter stream carry no coefficient at | ||
| 1163 | + // all (measured on the perf segment: 3.46M of 5.7M calls). Dequantization | ||
| 1164 | + // maps zero to zero (`dequant_wh_into`) and a zero grid inverse-transforms | ||
| 1165 | + // 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 | ||
| 1167 | + // the column pass for those. | ||
| 1168 | + if levels.iter().all(|&l| l == 0) { | ||
| 1169 | + return vec![0i32; w * h]; | ||
| 1170 | + } | ||
| 1045 | DQ_SCRATCH.with(|cell| { | 1171 | DQ_SCRATCH.with(|cell| { |
| 1046 | let mut dq = cell.borrow_mut(); | 1172 | let mut dq = cell.borrow_mut(); |
| 1047 | crate::quant::dequant_wh_into(levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta); | 1173 | crate::quant::dequant_wh_into(levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta); |
| @@ -1889,4 +2015,45 @@ mod tests { | |||
| 1889 | assert_eq!(via_wrapper, via_wh, "side {side}"); | 2015 | assert_eq!(via_wrapper, via_wh, "side {side}"); |
| 1890 | } | 2016 | } |
| 1891 | } | 2017 | } |
| 2018 | + | ||
| 2019 | + /// The eight-wide column kernel is the scalar network, lane for lane: a | ||
| 2020 | + /// pseudo-random sweep over the full coefficient range at every size and | ||
| 2021 | + /// both clamp widths (`bit_depth` 8 and 10) has to agree bit for bit, or | ||
| 2022 | + /// the column pass would drift from the decoder on some block this | ||
| 2023 | + /// crate's gate streams happen not to contain. | ||
| 2024 | + #[test] | ||
| 2025 | + fn the_eight_wide_dct_matches_the_scalar_one() { | ||
| 2026 | + let mut state = 0x2545_F491_4F6C_DD1Du64; | ||
| 2027 | + let mut next = move || { | ||
| 2028 | + state ^= state << 13; | ||
| 2029 | + state ^= state >> 7; | ||
| 2030 | + state ^= state << 17; | ||
| 2031 | + // The transform's input is a dequantized coefficient, bounded by | ||
| 2032 | + // `dequant_wh_into`'s own +-(1 << (7 + bit_depth)). | ||
| 2033 | + (state >> 32) as i32 % 262_144 - 131_072 | ||
| 2034 | + }; | ||
| 2035 | + for n in 2..=6u32 { | ||
| 2036 | + for &r in &[16usize, 18] { | ||
| 2037 | + for _ in 0..64 { | ||
| 2038 | + let mut wide = [[0i32; 8]; 64]; | ||
| 2039 | + let mut cols = [[0i32; 64]; 8]; | ||
| 2040 | + for i in 0..(1usize << n) { | ||
| 2041 | + for l in 0..8 { | ||
| 2042 | + let v = next(); | ||
| 2043 | + wide[i][l] = v; | ||
| 2044 | + cols[l][i] = v; | ||
| 2045 | + } | ||
| 2046 | + } | ||
| 2047 | + inverse_dct_x8(&mut wide, n, r); | ||
| 2048 | + for (l, col) in cols.iter_mut().enumerate() { | ||
| 2049 | + inverse_dct_fixed(col, n, r); | ||
| 2050 | + for i in 0..(1usize << n) { | ||
| 2051 | + assert_eq!(wide[i][l], col[i], "n={n} r={r} lane={l} i={i}"); | ||
| 2052 | + } | ||
| 2053 | + } | ||
| 2054 | + } | ||
| 2055 | + } | ||
| 2056 | + } | ||
| 2057 | + } | ||
| 2058 | + | ||
| 1892 | } | 2059 | } |