XAudio2: vectorize acphal i16-to-f32 PCM conversion with runtime AVX2 #1230

Open
real-alponzo wants to merge 1 commit from upstream-pr/1240 into main
real-alponzo commented 2026-11-24 13:36:17 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by real-alponzo on 2026-11-24T13:36:17Z.


Vectorizes the Opus i16 -> f32 sample conversion and non-zero sample counter in acphal::decode_into using runtime-detected AVX2 (#459 item 3a), keeping the scalar implementation as fallback for non-AVX2 CPUs and tail elements.

Part of #459. (Disjoint from open acphal.rs PRs #463, #468, #471, and #939.)

What is in it

  • crates/xcl/src/hle/acphal.rs:
    • Extract convert_i16_to_f32_scalar, convert_i16_to_f32_avx2 (#[target_feature(enable = "avx2")]), and runtime-dispatched convert_i16_to_f32 (OnceLock<bool> via is_x86_feature_detected!("avx2")).
    • Process 8 i16 samples per iteration (_mm256_cvtepi16_epi32 -> _mm256_cvtepi32_ps -> _mm256_mul_ps(1.0 / 32768.0)) and count zero lanes directly in a 256-bit SIMD accumulator (_mm256_sub_epi32 on _mm256_cmpeq_epi32), avoiding per-iteration movemask/popcnt dependencies. Because 32768.0 = 2^15 is an exact power of two, multiplying by 1.0 / 32768.0 is bit-identical to dividing by 32768.0 across all 65,536 i16 values.
    • Add convert_i16_to_f32_avx2_matches_scalar_bit_for_bit unit test verifying exhaustive bit-exact f32 and non-zero count parity across i16::MIN..=i16::MAX, tail lengths 0..=17, and shorter destination slices.

Measured on Intel Core i9-9900K (--release, 200,000 iterations per buffer length):

Samples (N) Scalar Runtime AVX2 Speedup
480 (10 ms mono 48 kHz) 227.2 ns 45.0 ns 5.05x
960 (20 ms mono 48 kHz) 366.6 ns 71.8 ns 5.11x
1920 (20 ms stereo 48 kHz) 717.3 ns 148.2 ns 4.84x
5760 (20 ms 5.1 48 kHz) 2139.8 ns 444.6 ns 4.81x

Regression run

Baseline (main):

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

This PR (perf/acphal-avx2-i16-to-f32):

xcl-smoke v1
commit: ae27c9e101166065a2e72279123a4c240608d00b
gpu: AMD Radeon RX 6800
title: YouKnowWho
seconds: 60
exit: 0x0
frames shown: 816 (PresentX 817)
presents per 2 s (last 3): 19 5 14
device removals: 0
crashes: 0
peak commit: 12221 MB

Tested

Hardware: Windows 11 Pro for Workstations (Build 26300), Intel Core i9-9900K (AVX2), 32 GB RAM, AMD Radeon RX 6800 (16337 MiB VRAM, Driver 32.0.21045.29003), rustc 1.100.0 (0ef1bc386 2026-11-09).

  • cargo test -p xcl --lib hle::acphal::: 8 passed; 0 failed; 0 ignored; 0 skipped.
  • XCL_REQUIRE_GPU=wave64,mesh cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 739 passed; 3 failed; 41 ignored; 0 skipped. All 3 failures are the pre-existing RDNA2 placed_title_textures_share_vram_and_preserve_pixels + 2 VRAM_TEST_LOCK PoisonError cascades on unmodified main (tracked in #470).
  • .\scripts\smoke.ps1 -Game '<game folder>\YouKnowWho' on ae27c9e101166065a2e72279123a4c240608d00b (see Regression run above).

Not tested

  • Non-AVX2 x86-64 CPUs (scalar fallback verified directly in convert_i16_to_f32_avx2_matches_scalar_bit_for_bit).
  • Retail title with active Opus acphal audio decode traffic (microbenchmarked in --release on i9-9900K; YouKnowWho smoke verified no regression).
  • Non-AMD GPUs and Linux / Wine.

AI use

Antigravity (Gemini 4 Neon) implemented the runtime-detected AVX2 conversion and bit-exact unit test in crates/xcl/src/hle/acphal.rs, ran the release benchmark, full test suite, and smoke run.

<!-- greyfinch-sync:ivall/xcl:issue:5473187462 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1240). Originally posted by [real-alponzo](https://github.com/real-alponzo) on 2026-11-24T13:36:17Z. --- Vectorizes the Opus `i16 -> f32` sample conversion and non-zero sample counter in `acphal::decode_into` using runtime-detected AVX2 (`#459` item 3a), keeping the scalar implementation as fallback for non-AVX2 CPUs and tail elements. Part of #459. (Disjoint from open `acphal.rs` PRs #463, #468, #471, and #939.) **What is in it** - **`crates/xcl/src/hle/acphal.rs`**: - Extract `convert_i16_to_f32_scalar`, `convert_i16_to_f32_avx2` (`#[target_feature(enable = "avx2")]`), and runtime-dispatched `convert_i16_to_f32` (`OnceLock<bool>` via `is_x86_feature_detected!("avx2")`). - Process 8 `i16` samples per iteration (`_mm256_cvtepi16_epi32` -> `_mm256_cvtepi32_ps` -> `_mm256_mul_ps(1.0 / 32768.0)`) and count zero lanes directly in a 256-bit SIMD accumulator (`_mm256_sub_epi32` on `_mm256_cmpeq_epi32`), avoiding per-iteration `movemask`/`popcnt` dependencies. Because `32768.0 = 2^15` is an exact power of two, multiplying by `1.0 / 32768.0` is bit-identical to dividing by `32768.0` across all 65,536 `i16` values. - Add `convert_i16_to_f32_avx2_matches_scalar_bit_for_bit` unit test verifying exhaustive bit-exact `f32` and non-zero count parity across `i16::MIN..=i16::MAX`, tail lengths `0..=17`, and shorter destination slices. Measured on Intel Core i9-9900K (`--release`, 200,000 iterations per buffer length): | Samples (`N`) | Scalar | Runtime AVX2 | Speedup | |---:|---:|---:|---:| | 480 (10 ms mono 48 kHz) | 227.2 ns | 45.0 ns | **5.05x** | | 960 (20 ms mono 48 kHz) | 366.6 ns | 71.8 ns | **5.11x** | | 1920 (20 ms stereo 48 kHz) | 717.3 ns | 148.2 ns | **4.84x** | | 5760 (20 ms 5.1 48 kHz) | 2139.8 ns | 444.6 ns | **4.81x** | **Regression run** Baseline (`main`): ``` 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 ``` This PR (`perf/acphal-avx2-i16-to-f32`): ``` xcl-smoke v1 commit: ae27c9e101166065a2e72279123a4c240608d00b gpu: AMD Radeon RX 6800 title: YouKnowWho seconds: 60 exit: 0x0 frames shown: 816 (PresentX 817) presents per 2 s (last 3): 19 5 14 device removals: 0 crashes: 0 peak commit: 12221 MB ``` **Tested** Hardware: Windows 11 Pro for Workstations (Build 26300), Intel Core i9-9900K (AVX2), 32 GB RAM, AMD Radeon RX 6800 (16337 MiB VRAM, Driver `32.0.21045.29003`), `rustc 1.100.0 (0ef1bc386 2026-11-09)`. - `cargo test -p xcl --lib hle::acphal::`: `8 passed; 0 failed; 0 ignored; 0 skipped`. - `XCL_REQUIRE_GPU=wave64,mesh cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4`: `739 passed; 3 failed; 41 ignored; 0 skipped`. All 3 failures are the pre-existing RDNA2 `placed_title_textures_share_vram_and_preserve_pixels` + 2 `VRAM_TEST_LOCK` `PoisonError` cascades on unmodified `main` (tracked in #470). - `.\scripts\smoke.ps1 -Game '<game folder>\YouKnowWho'` on `ae27c9e101166065a2e72279123a4c240608d00b` (see Regression run above). **Not tested** - Non-AVX2 x86-64 CPUs (scalar fallback verified directly in `convert_i16_to_f32_avx2_matches_scalar_bit_for_bit`). - Retail title with active Opus `acphal` audio decode traffic (microbenchmarked in `--release` on i9-9900K; `YouKnowWho` smoke verified no regression). - Non-AMD GPUs and Linux / Wine. **AI use** Antigravity (Gemini 4 Neon) implemented the runtime-detected AVX2 conversion and bit-exact unit test in `crates/xcl/src/hle/acphal.rs`, ran the release benchmark, full test suite, and smoke run.
thehollowedone commented 2026-11-24 14:06:39 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by thehollowedone on 2026-11-24T13:47:00Z.


Independent Ryzen 7 9800X3D check on ae27c9e (Windows 11 build 26300, Rust 1.99.1 MSVC, release). The stock exhaustive parity test passed with AVX2 available. For 1,920 interleaved samples, the converter took a median 101 ns with runtime dispatch versus 425 ns for scalar, about 4.2× faster.

I also timed XCL’s decode_into with the in-tree synthetic 20 ms stereo CELT packets. In the scalar comparison build I changed only which converter decode_into called. Five alternating pairs of 2,048 packets per run gave medians of 25.8 µs per packet (AVX2, 24.9–26.7) and 26.6 µs (scalar, 24.1–27.1). Every packet produced 960 stereo frames and each run’s final PCM block had the same digest. The decode ranges overlap, so I could only establish a clear speedup for the conversion step.

Synthetic audio only. AI-assisted. Codex (GPT-6, exact variant unavailable) wrote and ran the scratch benchmark and drafted this report.

<!-- greyfinch-sync:ivall/xcl:comment:7133329833 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1240#issuecomment-7133329833). Originally posted by [thehollowedone](https://github.com/thehollowedone) on 2026-11-24T13:47:00Z. --- Independent Ryzen 7 9800X3D check on [`ae27c9e`](https://github.com/ivall/xcl/commit/ae27c9e101166065a2e72279123a4c240608d00b) (Windows 11 build 26300, Rust 1.99.1 MSVC, release). The stock exhaustive parity test passed with AVX2 available. For 1,920 interleaved samples, the converter took a median 101 ns with runtime dispatch versus 425 ns for scalar, about 4.2× faster. I also timed XCL’s `decode_into` with the in-tree synthetic 20 ms stereo CELT packets. In the scalar comparison build I changed only which converter `decode_into` called. Five alternating pairs of 2,048 packets per run gave medians of 25.8 µs per packet (AVX2, 24.9–26.7) and 26.6 µs (scalar, 24.1–27.1). Every packet produced 960 stereo frames and each run’s final PCM block had the same digest. The decode ranges overlap, so I could only establish a clear speedup for the conversion step. Synthetic audio only. AI-assisted. Codex (GPT-6, exact variant unavailable) wrote and ran the scratch benchmark and drafted this report.
DavidM1911 commented 2026-11-24 14:06:50 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T14:02:01Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib lib acphal: 8 passed, 0 failed.

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7133727521 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1240#issuecomment-7133727521). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T14:02:01Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - cargo test -p xcl --lib lib acphal: 8 passed, 0 failed. Thanks!
DavidM1911 commented 2026-11-26 05:05:34 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T04:32:25Z.


Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7182683370 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1240#issuecomment-7182683370). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T04:32:25Z. --- Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
github-actions[bot] commented 2026-12-01 02:31:32 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-12-01T02:31:32Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7298709569 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1240#issuecomment-7298709569). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-12-01T02:31:32Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 05:12:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:12:43Z.


Triage (#1607) – head ae27c9e

No open or merged PR vectorizes this conversion; #884 (same issue #459) changes the XMA2 bit reservoir, unrelated code.
convert_i16_to_f32_avx2 matches the scalar loop it replaces in decode_into (multiply by 1/32768 is exact, the length is the shorter slice, the zero count is per lane), and the test compares all 65 536 values and the tails bit for bit.
The loop runs once per Opus frame (about 960 samples per channel) next to the Opus decode itself; the diff has no measurement showing the conversion costs anything noticeable.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7301533554 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1240#issuecomment-7301533554). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:12:43Z. --- **Triage (#1607)** – head ae27c9e No open or merged PR vectorizes this conversion; #884 (same issue #459) changes the XMA2 bit reservoir, unrelated code. `convert_i16_to_f32_avx2` matches the scalar loop it replaces in `decode_into` (multiply by 1/32768 is exact, the length is the shorter slice, the zero count is per lane), and the test compares all 65 536 values and the tails bit for bit. The loop runs once per Opus frame (about 960 samples per channel) next to the Opus decode itself; the diff has no measurement showing the conversion costs anything noticeable. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1240:upstream-pr/1240'
git switch 'upstream-pr/1240'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1240'
git switch 'upstream-pr/1240'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1240'
git switch 'upstream-pr/1240'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1240'
git switch 'main'
git merge --squash 'upstream-pr/1240'
git switch 'main'
git merge --ff-only 'upstream-pr/1240'
git switch 'main'
git merge 'upstream-pr/1240'
git push origin 'main'
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
xcl-dev/xcl!1230
No description provided.