The arithmetic reader takes its two-symbol shape straight
Browse files at this commit864a703b4860a81f2d125c9e5c58b378f9652b95Parent: cb9db45- perf
- the arithmetic reader takes its two-symbol shape straight
1 changed file +139 -20
- crates/ec-av1/src/msac.rs +139 -20
crates/ec-av1/src/msac.rsmodified+139 -20
| @@ -348,38 +348,68 @@ impl<'a> SymbolDecoder<'a> { | ||
| 348 | 348 | #[inline] |
| 349 | 349 | pub fn symbol_fixed(&mut self, cdf: &[u16]) -> usize { |
| 350 | 350 | let nsyms = cdf.len() - 1; |
| 351 | - let mut cur = self.range; | |
| 352 | - let mut prev = cur; | |
| 353 | - // The last boundary is always `CDF_TOP`, whose `cur` is 0, so the walk | |
| 354 | - // always stops inside the alphabet; iterating the slice instead of | |
| 355 | - // indexing it says so to the bounds checker. | |
| 356 | - let mut symbol = nsyms - 1; | |
| 351 | + // Two-symbol alphabets are most of the stream (every flag, every bit of | |
| 352 | + // a literal), and their walk has exactly one boundary: computing it | |
| 353 | + // straight skips the loop and its bounds work. | |
| 354 | + if nsyms == 2 { | |
| 355 | + let f = u32::from(CDF_TOP - cdf[0]); | |
| 356 | + let cur = (((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT)) | |
| 357 | + + EC_MIN_PROB; | |
| 358 | + return self.bool_cur(cur); | |
| 359 | + } | |
| 357 | 360 | // Loop-invariant: the coder's range does not move until the symbol is |
| 358 | 361 | // chosen, so its top byte is computed once rather than per candidate. |
| 359 | 362 | let base = self.range >> 8; |
| 360 | - for (i, &c) in cdf[..nsyms].iter().enumerate() { | |
| 361 | - prev = cur; | |
| 363 | + let mut prev = self.range; | |
| 364 | + // The floor every symbol above the candidate is owed, counted down | |
| 365 | + // instead of multiplied out per candidate. | |
| 366 | + let mut minp = EC_MIN_PROB * (nsyms - 1) as u32; | |
| 367 | + // The last boundary is always `CDF_TOP`, whose `cur` is 0, so the walk | |
| 368 | + // always stops inside the alphabet: the last candidate needs no test | |
| 369 | + // and its arithmetic is the fall-through below. | |
| 370 | + let mut symbol = nsyms - 1; | |
| 371 | + let mut cur = 0; | |
| 372 | + for (i, &c) in cdf[..nsyms - 1].iter().enumerate() { | |
| 362 | 373 | let f = u32::from(CDF_TOP - c); |
| 363 | - cur = ((base * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT)) | |
| 364 | - + EC_MIN_PROB * (nsyms - 1 - i) as u32; | |
| 365 | - if self.value >= cur { | |
| 374 | + let c = ((base * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT)) + minp; | |
| 375 | + if self.value >= c { | |
| 366 | 376 | symbol = i; |
| 377 | + cur = c; | |
| 367 | 378 | break; |
| 368 | 379 | } |
| 380 | + prev = c; | |
| 381 | + minp -= EC_MIN_PROB; | |
| 369 | 382 | } |
| 370 | - self.range = prev - cur; | |
| 371 | - self.value -= cur; | |
| 383 | + self.renorm(prev - cur, self.value - cur); | |
| 384 | + symbol | |
| 385 | + } | |
| 372 | 386 | |
| 373 | - // Renormalise back into `[2^15, 2^16)`, the same shift the encoder | |
| 374 | - // applied when it narrowed to this symbol. | |
| 375 | - let bits = 16 - (32 - self.range.leading_zeros()); | |
| 376 | - self.range <<= bits; | |
| 387 | + /// Finishes a two-symbol read whose single boundary is `cur`: symbol 0 owns | |
| 388 | + /// `[cur, range)` and symbol 1 owns `[0, cur)`. | |
| 389 | + #[inline(always)] | |
| 390 | + fn bool_cur(&mut self, cur: u32) -> usize { | |
| 391 | + let value = self.value; | |
| 392 | + let (symbol, rng, val) = if value >= cur { | |
| 393 | + (0, self.range - cur, value - cur) | |
| 394 | + } else { | |
| 395 | + (1, cur, value) | |
| 396 | + }; | |
| 397 | + self.renorm(rng, val); | |
| 398 | + symbol | |
| 399 | + } | |
| 400 | + | |
| 401 | + /// Renormalises `rng` back into `[2^15, 2^16)` and shifts the same number of | |
| 402 | + /// bits into `val`, the tail every symbol read shares — the same shift the | |
| 403 | + /// encoder applied when it narrowed to this symbol. | |
| 404 | + #[inline(always)] | |
| 405 | + fn renorm(&mut self, rng: u32, val: u32) { | |
| 406 | + let bits = 16 - (32 - rng.leading_zeros()); | |
| 407 | + self.range = rng << bits; | |
| 377 | 408 | let num_bits = u32::min(bits, self.max_bits.max(0) as u32); |
| 378 | 409 | let new_data = self.f(num_bits); |
| 379 | 410 | let padded = new_data << (bits - num_bits); |
| 380 | - self.value = padded ^ (((self.value + 1) << bits) - 1); | |
| 411 | + self.value = padded ^ (((val + 1) << bits) - 1); | |
| 381 | 412 | self.max_bits -= bits as i32; |
| 382 | - symbol | |
| 383 | 413 | } |
| 384 | 414 | |
| 385 | 415 | /// Debug-only: the current bit offset into `data`, for cross-checking |
| @@ -402,7 +432,10 @@ impl<'a> SymbolDecoder<'a> { | ||
| 402 | 432 | pub fn literal(&mut self, bits: u32) -> u32 { |
| 403 | 433 | let mut v = 0; |
| 404 | 434 | for _ in 0..bits { |
| 405 | - v = (v << 1) | self.symbol_fixed(&EQUIPROBABLE) as u32; | |
| 435 | + // `EQUIPROBABLE`'s single boundary: `f >> EC_PROB_SHIFT` is 256, so | |
| 436 | + // the range multiply is a shift. | |
| 437 | + let cur = ((self.range >> 8) << 7) + EC_MIN_PROB; | |
| 438 | + v = (v << 1) | self.bool_cur(cur) as u32; | |
| 406 | 439 | } |
| 407 | 440 | v |
| 408 | 441 | } |
| @@ -612,4 +645,90 @@ pub(crate) mod tests { | ||
| 612 | 645 | fn empty_payload_is_short() { |
| 613 | 646 | assert!(SymbolEncoder::new().finish().len() <= 2); |
| 614 | 647 | } |
| 648 | + /// The spec-literal 8.2.6 walk, as `symbol_fixed` read it before the | |
| 649 | + /// two-symbol shortcut and the last-candidate fall-through: every | |
| 650 | + /// candidate tested, `EC_MIN_PROB` multiplied out, one shared tail. | |
| 651 | + fn ref_symbol_fixed(d: &mut SymbolDecoder<'_>, cdf: &[u16]) -> usize { | |
| 652 | + let nsyms = cdf.len() - 1; | |
| 653 | + let mut cur = d.range; | |
| 654 | + let mut prev = cur; | |
| 655 | + let mut symbol = nsyms - 1; | |
| 656 | + let base = d.range >> 8; | |
| 657 | + for (i, &c) in cdf[..nsyms].iter().enumerate() { | |
| 658 | + prev = cur; | |
| 659 | + let f = u32::from(CDF_TOP - c); | |
| 660 | + cur = ((base * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT)) | |
| 661 | + + EC_MIN_PROB * (nsyms - 1 - i) as u32; | |
| 662 | + if d.value >= cur { | |
| 663 | + symbol = i; | |
| 664 | + break; | |
| 665 | + } | |
| 666 | + } | |
| 667 | + d.range = prev - cur; | |
| 668 | + d.value -= cur; | |
| 669 | + let bits = 16 - (32 - d.range.leading_zeros()); | |
| 670 | + d.range <<= bits; | |
| 671 | + let num_bits = u32::min(bits, d.max_bits.max(0) as u32); | |
| 672 | + let new_data = d.f(num_bits); | |
| 673 | + let padded = new_data << (bits - num_bits); | |
| 674 | + d.value = padded ^ (((d.value + 1) << bits) - 1); | |
| 675 | + d.max_bits -= bits as i32; | |
| 676 | + symbol | |
| 677 | + } | |
| 678 | + | |
| 679 | + /// The reader's fast paths must be the spec walk, symbol for symbol and | |
| 680 | + /// register for register: a long mixed stream is decoded twice in lockstep | |
| 681 | + /// and both the symbol and the coder state `(range, value)` are compared | |
| 682 | + /// after every read, so a divergence that has not yet changed a symbol | |
| 683 | + /// still fails here. | |
| 684 | + #[test] | |
| 685 | + fn fast_paths_match_the_spec_literal_walk() { | |
| 686 | + let mut state = 0x0123_4567_89ab_cdef; | |
| 687 | + let mut enc = SymbolEncoder::new(); | |
| 688 | + let mut cdfs: Vec<Vec<u16>> = (2..=16).map(flat_cdf).collect(); | |
| 689 | + let mut written: Vec<(usize, u32)> = Vec::new(); | |
| 690 | + for _ in 0..20_000 { | |
| 691 | + let pick = (rng(&mut state) % 16) as usize; | |
| 692 | + if pick == 15 { | |
| 693 | + let width = 1 + (rng(&mut state) % 16) as u32; | |
| 694 | + let v = (rng(&mut state) as u32) & ((1u32 << width) - 1); | |
| 695 | + enc.literal(v, width); | |
| 696 | + written.push((usize::MAX, width)); | |
| 697 | + } else { | |
| 698 | + let nsyms = pick + 2; | |
| 699 | + let sym = (rng(&mut state) % nsyms as u64) as usize; | |
| 700 | + enc.symbol(sym, &mut cdfs[pick]); | |
| 701 | + written.push((pick, sym as u32)); | |
| 702 | + } | |
| 703 | + } | |
| 704 | + let payload = enc.finish(); | |
| 705 | + | |
| 706 | + let mut fast = SymbolDecoder::new(&payload); | |
| 707 | + let mut slow = SymbolDecoder::new(&payload); | |
| 708 | + let mut fast_cdfs: Vec<Vec<u16>> = (2..=16).map(flat_cdf).collect(); | |
| 709 | + let mut slow_cdfs: Vec<Vec<u16>> = (2..=16).map(flat_cdf).collect(); | |
| 710 | + for (i, &(pick, arg)) in written.iter().enumerate() { | |
| 711 | + let (a, b) = if pick == usize::MAX { | |
| 712 | + let a = fast.literal(arg); | |
| 713 | + let mut b = 0u32; | |
| 714 | + for _ in 0..arg { | |
| 715 | + b = (b << 1) | ref_symbol_fixed(&mut slow, &EQUIPROBABLE) as u32; | |
| 716 | + } | |
| 717 | + (a, b) | |
| 718 | + } else { | |
| 719 | + let a = fast.symbol(&mut fast_cdfs[pick]) as u32; | |
| 720 | + let b = ref_symbol_fixed(&mut slow, &slow_cdfs[pick]); | |
| 721 | + update_cdf(&mut slow_cdfs[pick], b); | |
| 722 | + (a, b as u32) | |
| 723 | + }; | |
| 724 | + assert_eq!(a, b, "item {i} (pick {pick})"); | |
| 725 | + assert_eq!(a, if pick == usize::MAX { a } else { arg }, "item {i} vs written"); | |
| 726 | + assert_eq!( | |
| 727 | + fast.debug_state(), | |
| 728 | + slow.debug_state(), | |
| 729 | + "coder state after item {i} (pick {pick})" | |
| 730 | + ); | |
| 731 | + assert_eq!(fast_cdfs[pick.min(14)], slow_cdfs[pick.min(14)], "cdf after item {i}"); | |
| 732 | + } | |
| 733 | + } | |
| 615 | 734 | } |