Pin the typed forward transform bit-identical to its strided form
Browse files at this commite70dd649b1294bd6734c3cd235c2be19084c1552Parent: 013dc91- test
- pin the typed forward transform bit-identical to its strided form
1 changed file +84 -0
crates/ec-av1/src/transform.rsmodified+84 -0
| @@ -2656,6 +2656,90 @@ mod tests { | ||
| 2656 | 2656 | /// full-range noise. A single differing lane at any size would be a |
| 2657 | 2657 | /// silent pixel drift on whatever stream first reaches it. |
| 2658 | 2658 | #[cfg(target_arch = "x86_64")] |
| 2659 | + /// [`forward_transform_2d_typed`]'s contiguous-basis passes are the | |
| 2660 | + /// strided `.map().sum()` they replaced, bit for bit. | |
| 2661 | + /// | |
| 2662 | + /// The reordering is safe only because each output's terms stay in the | |
| 2663 | + /// same order -- only the loop that iterates the *outputs* moved -- so | |
| 2664 | + /// this compares raw bit patterns, not an epsilon: any reassociation | |
| 2665 | + /// would show up here as a last-place difference at some size. | |
| 2666 | + #[test] | |
| 2667 | + fn the_typed_forward_transform_is_bit_identical_to_the_strided_form() { | |
| 2668 | + fn strided(residual: &[i32], side: usize, tx_type: TxType) -> Vec<f64> { | |
| 2669 | + let (row_kind, col_kind) = tx_type.axes(); | |
| 2670 | + let (hb, vb) = (axis_basis(side, row_kind), axis_basis(side, col_kind)); | |
| 2671 | + let mut rows = vec![0.0f64; side * side]; | |
| 2672 | + for i in 0..side { | |
| 2673 | + let source = &residual[i * side..][..side]; | |
| 2674 | + if source.iter().all(|&r| r == 0) { | |
| 2675 | + continue; | |
| 2676 | + } | |
| 2677 | + for v in 0..side { | |
| 2678 | + rows[i * side + v] = source | |
| 2679 | + .iter() | |
| 2680 | + .zip(&hb[v * side..][..side]) | |
| 2681 | + .map(|(&r, &b)| f64::from(r) * b) | |
| 2682 | + .sum(); | |
| 2683 | + } | |
| 2684 | + } | |
| 2685 | + let mut out = vec![0.0f64; side * side]; | |
| 2686 | + for u in 0..side { | |
| 2687 | + let column = &vb[u * side..][..side]; | |
| 2688 | + for v in 0..side { | |
| 2689 | + let sum: f64 = (0..side).map(|i| rows[i * side + v] * column[i]).sum(); | |
| 2690 | + out[u * side + v] = INVERSE_GAIN_RECIPROCAL * sum; | |
| 2691 | + } | |
| 2692 | + } | |
| 2693 | + out | |
| 2694 | + } | |
| 2695 | + let mut seed = 0x9E37_79B9_7F4A_7C15u64; | |
| 2696 | + let mut next = move || { | |
| 2697 | + seed ^= seed << 13; | |
| 2698 | + seed ^= seed >> 7; | |
| 2699 | + seed ^= seed << 17; | |
| 2700 | + seed | |
| 2701 | + }; | |
| 2702 | + let types = [ | |
| 2703 | + TxType::Idtx, | |
| 2704 | + TxType::AdstAdst, | |
| 2705 | + TxType::AdstDct, | |
| 2706 | + TxType::DctAdst, | |
| 2707 | + TxType::VDct, | |
| 2708 | + TxType::HDct, | |
| 2709 | + ]; | |
| 2710 | + for side in [4usize, 8, 16, 32] { | |
| 2711 | + for tx_type in types { | |
| 2712 | + // ADST is undefined above 16 (the spec's 32+ sets are | |
| 2713 | + // DCT/identity only), so those pairs never reach this path. | |
| 2714 | + let adst = matches!(tx_type.axes(), (TxType1d::Adst, _) | (_, TxType1d::Adst)); | |
| 2715 | + if side > 16 && adst { | |
| 2716 | + continue; | |
| 2717 | + } | |
| 2718 | + for round in 0..8 { | |
| 2719 | + // Round 0 is an all-zero residual and round 1 has whole | |
| 2720 | + // zero rows, so both skips are exercised at every size. | |
| 2721 | + let residual: Vec<i32> = (0..side * side) | |
| 2722 | + .map(|i| match round { | |
| 2723 | + 0 => 0, | |
| 2724 | + 1 if (i / side) % 2 == 0 => 0, | |
| 2725 | + _ => (next() % 511) as i32 - 255, | |
| 2726 | + }) | |
| 2727 | + .collect(); | |
| 2728 | + let ours = forward_transform_2d_typed(&residual, side, tx_type); | |
| 2729 | + let theirs = strided(&residual, side, tx_type); | |
| 2730 | + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { | |
| 2731 | + assert_eq!( | |
| 2732 | + a.to_bits(), | |
| 2733 | + b.to_bits(), | |
| 2734 | + "{tx_type:?} {side}x{side} round {round} coefficient {i}: \ | |
| 2735 | + {a} vs {b}" | |
| 2736 | + ); | |
| 2737 | + } | |
| 2738 | + } | |
| 2739 | + } | |
| 2740 | + } | |
| 2741 | + } | |
| 2742 | + | |
| 2659 | 2743 | #[test] |
| 2660 | 2744 | fn the_avx2_dct_matches_the_scalar_one_over_the_full_range() { |
| 2661 | 2745 | if !std::arch::is_x86_feature_detected!("avx2") { |