Precompute the XMA inverse MDCT's sine/cosine tables #35

Closed
github-bot wants to merge 1 commit from github-bot/gh-35 into main AGit
Owner

@⁠romainhedouin on GitHub · 2026-11-25 12:33 UTC · original
romainhedouin:perf/xma-imdct-twiddles → main · +69 −5 · 1 files · 1 commits · closed without merging

For every audio subframe, the XMA decoder computed n sine/cosine pairs that depend only on the size n (128, 256 or 512), and allocated two vectors. The pairs are now computed once per size and kept, and the work buffers live on the stack. The output is bit-for-bit identical: a new test (cached_twiddles_give_the_same_bits) compares it with the old function on random input for every size.

imdct_half, release mode, on a Mac Before After
n = 128 1,470 ns 767 ns
n = 256 2,985 ns 1,610 ns
n = 512 6,500 ns 3,510 ns
sin_cos calls / allocations per call n / 2 0 / 0

cargo test -p victory-formats passes; clippy for Windows reports no new warnings.

🤖 Generated with Claude Code

**@⁠romainhedouin** on GitHub · 2026-11-25 12:33 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/35) `romainhedouin:perf/xma-imdct-twiddles` → `main` · +69 −5 · 1 files · 1 commits · closed without merging For every audio subframe, the XMA decoder computed n sine/cosine pairs that depend only on the size n (128, 256 or 512), and allocated two vectors. The pairs are now computed once per size and kept, and the work buffers live on the stack. The output is bit-for-bit identical: a new test (`cached_twiddles_give_the_same_bits`) compares it with the old function on random input for every size. | `imdct_half`, release mode, on a Mac | Before | After | |---|---|---| | n = 128 | 1,470 ns | 767 ns | | n = 256 | 2,985 ns | 1,610 ns | | n = 512 | 6,500 ns | 3,510 ns | | `sin_cos` calls / allocations per call | n / 2 | 0 / 0 | `cargo test -p victory-formats` passes; clippy for Windows reports no new warnings. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:34:11 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-25 18:14 UTC · original

Release XMA IMDCT comparison at PR #35 (8851e3d), Intel i9-11900K, Bazzite 45, Rust 1.98.1. The existing bit-equivalence test passed for all 150 synthetic inputs across the three sizes. A local timing harness called the PR's uncached reference and cached implementation in alternating order. Both used the same synthetic input and preallocated output. Median ns per call across eight batches of 2,000 calls each, with the cached table warm:

n Uncached Cached
128 1936.5 752
256 3803 1573.5
512 7950.5 3518.5

Apply timing.patch to the indicated revision and run cargo test --release --locked -p victory-formats compare_imdct_timings -- --ignored --nocapture. These timings cover the transform only.
Timing harness: github.com/KernelSmoker/VIctory-Belobug-AMD@1bd2782648/benchmarks/pr35/timing.patch

**@⁠KernelSmoker** on GitHub · 2026-11-25 18:14 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/35#issuecomment-7170626287) Release XMA IMDCT comparison at PR #35 (8851e3d), Intel i9-11900K, Bazzite 45, Rust 1.98.1. The existing bit-equivalence test passed for all 150 synthetic inputs across the three sizes. A local timing harness called the PR's uncached reference and cached implementation in alternating order. Both used the same synthetic input and preallocated output. Median ns per call across eight batches of 2,000 calls each, with the cached table warm: | n | Uncached | Cached | |---:|---:|---:| |128|1936.5|752| |256|3803|1573.5| |512|7950.5|3518.5| Apply timing.patch to the indicated revision and run cargo test --release --locked -p victory-formats compare_imdct_timings -- --ignored --nocapture. These timings cover the transform only. Timing harness: https://github.com/KernelSmoker/VIctory-Belobug-AMD/blob/1bd2782648d71c20f9bda0eedae736267cb6c084/benchmarks/pr35/timing.patch
Author
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:29 UTC · original · approved these changes

Verified on Linux (Rust 1.98.1)

  • cargo test -p victory-formats --lib on 8851e3d: 25 passed, 0 failed, 4 ignored. That includes cached_twiddles_give_the_same_bits and the existing fast_imdct_matches_the_direct_sum. The same with the PR merged into current main (--no-commit).
  • cargo clippy -p victory-formats --target x86_64-pc-windows-msvc: 0 warnings, same as main.
  • git merge --no-commit --no-ff into main: clean. It also merges cleanly after each of the other open victory-formats PRs I looked at (#7, #21, #37, #181).

Review notes

  • The output is bit-identical by construction. The table entries come from the same sin_cos expressions, evaluated the same way, so the per-call values are unchanged. The new test confirms this on 150 random inputs.
  • Bounds: imdct_twiddles()[log2(n) as usize - 7] and the FRAME_SAMPLES / 2 stack buffers are safe today. The only caller (xma.rs:965) passes subframe_len, and subframe_length() (xma.rs:585-597) limits that to FRAME_SAMPLES >> {0,1,2} = 512/256/128, with a MIN_SUBFRAME check.
  • An optional suggestion: the - 7 and [Twiddles; 3] silently encode MIN_SUBFRAME = 128 and FRAME_SAMPLES = 512. If either constant changes, for example to 64-sample subframes, this becomes a usize underflow or an out-of-bounds panic in the audio thread instead of a decode error. A debug_assert!(n.is_power_of_two() && (MIN_SUBFRAME..=FRAME_SAMPLES).contains(&n)), or an index computed as log2(n / MIN_SUBFRAME) with the table size derived from the two constants, would tie them together.

I found no defects.

Not verified

  • Decoding real XMA streams (the walks_saved_streams / matches_a_reference_decoder tests need data from outside the repository) and timing on Windows.

Reviewed with Claude Code.

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:29 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/35#pullrequestreview-6065235511) · **approved these changes** ### Verified on Linux (Rust 1.98.1) - `cargo test -p victory-formats --lib` on 8851e3d: 25 passed, 0 failed, 4 ignored. That includes `cached_twiddles_give_the_same_bits` and the existing `fast_imdct_matches_the_direct_sum`. The same with the PR merged into current main (`--no-commit`). - `cargo clippy -p victory-formats --target x86_64-pc-windows-msvc`: 0 warnings, same as main. - `git merge --no-commit --no-ff` into main: clean. It also merges cleanly after each of the other open `victory-formats` PRs I looked at (#7, #21, #37, #181). ### Review notes - The output is bit-identical by construction. The table entries come from the same `sin_cos` expressions, evaluated the same way, so the per-call values are unchanged. The new test confirms this on 150 random inputs. - Bounds: `imdct_twiddles()[log2(n) as usize - 7]` and the `FRAME_SAMPLES / 2` stack buffers are safe today. The only caller (`xma.rs:965`) passes `subframe_len`, and `subframe_length()` (`xma.rs:585-597`) limits that to `FRAME_SAMPLES >> {0,1,2}` = 512/256/128, with a `MIN_SUBFRAME` check. - An optional suggestion: the `- 7` and `[Twiddles; 3]` silently encode `MIN_SUBFRAME = 128` and `FRAME_SAMPLES = 512`. If either constant changes, for example to 64-sample subframes, this becomes a `usize` underflow or an out-of-bounds panic in the audio thread instead of a decode error. A `debug_assert!(n.is_power_of_two() && (MIN_SUBFRAME..=FRAME_SAMPLES).contains(&n))`, or an index computed as `log2(n / MIN_SUBFRAME)` with the table size derived from the two constants, would tie them together. I found no defects. ### Not verified - Decoding real XMA streams (the `walks_saved_streams` / `matches_a_reference_decoder` tests need data from outside the repository) and timing on Windows. Reviewed with Claude Code.
github-bot closed this pull request 2026-11-27 01:00:14 +00:00
Author
Owner

@⁠romainhedouin on GitHub · 2026-11-26 22:25 UTC · original

Closing: a-publier already has this and more in ade1da1. It caches the rotations, bit reversal and butterfly twiddles per subframe size (static ROTATIONS, xma.rs:483) with a bit-identity test (tabled_imdct_gives_the_same_bits). Its lookup falls back to building the table when the index doesn't match, which also covers lcg007xd-c's underflow concern. Thanks for the review and the timings.

**@⁠romainhedouin** on GitHub · 2026-11-26 22:25 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/35#issuecomment-7204671355) Closing: `a-publier` already has this and more in ade1da1. It caches the rotations, bit reversal and butterfly twiddles per subframe size (`static ROTATIONS`, xma.rs:483) with a bit-identity test (`tabled_imdct_gives_the_same_bits`). Its lookup falls back to building the table when the index doesn't match, which also covers lcg007xd-c's underflow concern. Thanks for the review and the timings.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.