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

Open
github-bot wants to merge 1 commit from github-bot/gh-1240 into main AGit
Owner

@⁠AlpPesen on GitHub · 2026-11-24 13:36 UTC · original
AlpPesen:perf/acphal-avx2-i16-to-f32 → main · +108 −5 · 1 files · 1 commits · open

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.

**@⁠AlpPesen** on GitHub · 2026-11-24 13:36 UTC · [original](https://github.com/xcl-dev/xcl/pull/1240) `AlpPesen:perf/acphal-avx2-i16-to-f32` → `main` · +108 −5 · 1 files · 1 commits · open 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.
Add runtime-detected AVX2 conversion (8 samples per iteration with a 256-bit SIMD zero-lane accumulator) for Opus PCM16 -> f32 output in acphal::decode_into, keeping the scalar fallback for non-AVX2 CPUs and tail samples.

Measured on Intel Core i9-9900K (--release, 200k iterations):
- N=480:  227.2 ns ->  45.0 ns (5.05x)
- N=960:  366.6 ns ->  71.8 ns (5.11x)
- N=1920: 717.3 ns -> 148.2 ns (4.84x)
- N=5760: 2139.8 ns -> 444.6 ns (4.81x)

Part of #459.

Assisted-by: Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:48:10 +00:00
Author
Owner

@⁠thehollowedone on GitHub · 2026-11-24 13:47 UTC · original

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.

**@⁠thehollowedone** on GitHub · 2026-11-24 13:47 UTC · [original](https://github.com/xcl-dev/xcl/pull/1240#issuecomment-7133329833) 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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 14:02 UTC · original

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** on GitHub · 2026-11-24 14:02 UTC · [original](https://github.com/xcl-dev/xcl/pull/1240#issuecomment-7133727521) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - cargo test -p xcl --lib lib acphal: 8 passed, 0 failed. Thanks!
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 04:32 UTC · original

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

**@⁠DavidM1911** on GitHub · 2026-11-26 04:32 UTC · [original](https://github.com/xcl-dev/xcl/pull/1240#issuecomment-7182683370) 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
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1240/head:github-bot/gh-1240'
git switch 'github-bot/gh-1240'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1240'
git switch 'github-bot/gh-1240'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1240'
git switch 'github-bot/gh-1240'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1240'
git switch 'main'
git merge --squash 'github-bot/gh-1240'
git switch 'main'
git merge --ff-only 'github-bot/gh-1240'
git switch 'main'
git merge 'github-bot/gh-1240'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.