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

Closed
romainhedouin wants to merge 1 commit from upstream-pr/35 into main
romainhedouin commented 2026-11-25 12:33:06 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by romainhedouin on 2026-11-25T12:33:06Z.


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

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:issue:5491392716 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/35). Originally posted by [romainhedouin](https://github.com/romainhedouin) on 2026-11-25T12:33:06Z. --- 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)
KernelSmoker commented 2026-11-25 18:14:01 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by KernelSmoker on 2026-11-25T18:14:01Z.


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

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7170626287 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/35#issuecomment-7170626287). Originally posted by [KernelSmoker](https://github.com/KernelSmoker) on 2026-11-25T18:14:01Z. --- 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
lcg007xd-c commented 2026-11-25 21:29:30 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by lcg007xd-c on 2026-11-25T21:29:30Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review:6065235511 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/35#pullrequestreview-6065235511). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T21:29:30Z. **Review:** APPROVED --- ### 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.
romainhedouin commented 2026-11-26 22:25:54 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by romainhedouin on 2026-11-26T22:25:54Z.


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.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7204671355 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/35#issuecomment-7204671355). Originally posted by [romainhedouin](https://github.com/romainhedouin) on 2026-11-26T22:25:54Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Belobug/VIctory-Belobug-AMD!35
No description provided.