Skip to content

[upstream #1030] XAudio2: vectorize XMA2 ImdctPlan::imdct_half with runtime AVX2+FMA - #108

Closed
ufoptg wants to merge 1 commit into
mainfrom
upstream-pr/1030
Closed

ufoptg wants to merge 1 commit into
mainfrom
upstream-pr/1030

Conversation

@ufoptg

@ufoptg ufoptg commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Part of xcl-dev#459 (item 2: XMA IMDCT / FFT).

Splits ImdctPlan's twiddle tables into SoA stage-contiguous arrays and adds a runtime-detected (is_x86_feature_detected!("avx2") && fma) AVX2+FMA implementation of ImdctPlan::imdct_half, keeping the scalar implementation as fallback (for non-AVX2/FMA CPUs and sizes < 32).

Overlap note: touches only ImdctPlan (crates/xcl/src/hle/xma2/mod.rs:455-660) and adds one unit test — disjoint from xcl-dev#525 (quantizer gains), xcl-dev#526 (PCM output), xcl-dev#884 (bit reservoir), and xcl-dev#861 (extradata tests).

What is in it

  • crates/xcl/src/hle/xma2/mod.rs:
    • SoA exp_r/exp_i and stage-contiguous stage_tw_r/stage_tw_i in ImdctPlan::new.
    • ImdctPlan::imdct_half_avx2 (#[target_feature(enable = "avx2,fma")]): 8-wide pre-rotation, fused multiply-free half = 1, 2 radix-4 pass, 4-wide VEX FMA half = 4 pass, 8-wide aligned AVX2+FMA half = 8..len2/2 passes, and 16-sample post-rotation/interleave.
    • avx2_imdct_matches_scalar_for_all_xma_sizes: verifies AVX2+FMA matches scalar (max_err < 1e-5) across all XMA sizes (N = 64, 128, 256, 512) and multiple seeds.

Measured speedup (--release, Intel Core i9-9900K, 200,000 iterations per size)

IMDCT size (N) Scalar (ns/call) AVX2+FMA (ns/call) Speedup
64 409.7 83.1 4.93x
128 808.6 141.4 5.72x
256 1643.3 259.4 6.33x
512 3281.9 530.9 6.18x

Tested

  • Hardware: Windows 11 Pro for Workstations (Build 26300), Intel Core i9-9900K, AMD Radeon RX 6800 (16337 MiB VRAM, driver 32.0.21045.29003), rustc 1.100.0 (x86_64-pc-windows-msvc), DXC 1.9.2602.17 (debug for tests, release for benchmark & smoke runs).
  • cargo test -p xcl --lib hle::xma2::: 8 passed, 0 failed (including decodes_xma2_and_xma1_bitstreams_matching_ffmpeg, fast_imdct_matches_naive_reference_for_all_xma_sizes, and avx2_imdct_matches_scalar_for_all_xma_sizes).
  • XCL_REQUIRE_GPU=wave64,mesh + cargo test --workspace --no-fail-fast -- --test-threads=4 on cdf2078: 725 passed, 3 failed, 41 ignored, 0 GPU skipped (xcl lib: 683 passed / 3 failed / 21 ignored; integration/e2e: 42 passed / 0 failed / 20 ignored; XCL_SAMPLE_* unset).

Regression run
Both runs below used the same warmed shader/PSO cache on YouKnowWho (60 s):

main (09940bb87fa1):

xcl-smoke v1
commit: 09940bb87fa11bc394fd9e5de570c2b7cfd16323
gpu: AMD Radeon RX 6800
title: YouKnowWho
seconds: 60
exit: 0x0
frames shown: 405 (PresentX 405)
presents per 2 s (last 3): 11 12 11
device removals: 0
crashes: 0
peak commit: 15870 MB

PR head (cdf2078e9bbc):

xcl-smoke v1
commit: cdf2078e9bbccc5fd40caa7da167bd5be0c4cad5
gpu: AMD Radeon RX 6800
title: YouKnowWho
seconds: 60
exit: 0x0
frames shown: 403 (PresentX 404)
presents per 2 s (last 3): 17 16 17
device removals: 0
crashes: 0
peak commit: 15867 MB

Not tested

  • A retail title that uses XMA2 voices (YouKnowWho uses Opus, so its smoke run above is a regression check only; XMA2 decoding itself is verified against the FFmpeg reference test vectors in decodes_xma2_and_xma1_bitstreams_matching_ffmpeg).
  • Sample-data tests (XCL_SAMPLE_* unset).
  • Non-AVX2/FMA host CPUs (scalar fallback is still exercised directly by avx2_imdct_matches_scalar_for_all_xma_sizes).

AI use

  • Implemented, benchmarked, tested, and verified with Antigravity (Gemini 4 Neon); every change and measurement reviewed by the author on local hardware.

Mirror of xcl-dev#1030 (auto-synced, do not edit).

Split ImdctPlan twiddles into SoA stage-contiguous arrays and add a
runtime-detected (is_x86_feature_detected!(avx2) && ma) AVX2+FMA
implementation of ImdctPlan::imdct_half covering pre-rotation, fused
multiply-free half=1/2 radix-4 stages, 4-wide/8-wide radix-2 FFT
butterflies, and post-rotation interleave, keeping the scalar path as
fallback.

Measured in release mode on Intel Core i9-9900K (200k iterations per size):
- N=64:  409.7 ->  83.1 ns/call (4.93x)
- N=128: 808.6 -> 141.4 ns/call (5.72x)
- N=256: 1643.3 -> 259.4 ns/call (6.33x)
- N=512: 3281.9 -> 530.9 ns/call (6.18x)

Part of xcl-dev#459.
Assisted-by: Antigravity (Gemini 4 Neon)
ufoptg pushed a commit that referenced this pull request Nov 24, 2026
…nment

GetResourceAllocationInfo (#25) and GetResourceAllocationInfo2 (#108)
laid several resources out one after another, each at its own
alignment, but returned the end of the last one as the size. The PC's
docs for GetResourceAllocationInfo with several descriptions: "the C++
algorithm for calculating a structure size and alignment are used" - a
4 MiB-aligned resource between two 64 KiB-aligned ones takes 12 MiB,
first or last 8 MiB, the alignment 4 MiB. XCL answered 8 MiB + 64 KiB
and 4 MiB + 128 KiB: a title sizing one heap for the group from it got
a heap that does not end on the alignment its resources need.

The arithmetic of both is now one function, `allocation_of`, which pads
the total to the largest alignment when there is more than one
resource. One resource's info is still the driver's or XG's as it is
(#108 asks #25 for each one alone); the per-resource offsets #108
writes are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ufoptg

ufoptg commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Upstream PR xcl-dev#1030 was closed or merged.

@ufoptg ufoptg closed this Nov 25, 2026
@ufoptg
ufoptg deleted the upstream-pr/1030 branch November 25, 2026 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants