BCPack: replace bit-packing loops with compile-time LUTs and precomputed masks in victory #2692

Closed
Decodere wants to merge 1 commit from upstream-pr/2702 into main
Decodere commented 2026-11-26 07:14:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Decodere on 2026-11-26T07:14:03Z.


What is in it

Replaces per-block bit-spread/deinterleave loops, dynamic mask shifts, slice anchor scans, and bounds-checked slice indexing in crates/xcl/src/hle/bcpack/victory.rs with compile-time lookup tables, precomputed masks, u16 anchor bitmasks, and fixed-size array packing helpers on the CPU BCPack decompression path.

  • Compile-time bit-spread and nibble LUTs: Adds SPREAD4 ([u8; 16]), SPREAD8 ([u16; 256]), and NIBBLES ([[i16; 2]; 256]) with #[inline(always)] interleave, wide_interleave (used in bc6), and nibbles replacing the runtime bit-spread and deinterleave folds.
  • Precomputed Widths masks and active loops in plain: Replaces bit-width arrays on Widths with precomputed ep_mask, ep_len, ix_mask, and ix_len. plain loops only over active endpoint/index counts (0..w.ep_len and 0..w.ix_len, with a fixed 16-byte SIMD fast path when w.ix_len == 16 for BC1/BC4), removing dynamic shifts from every element.
  • Fixed-size block-packing helpers: Narrows quads (&[u8; 4], &mut [u8; 16]), bc1_color (&[u8; 6], &[u8; 16], &mut [u8; 8], writing a single u64::to_le_bytes() store), and bc4_alpha (&[u8; 16], &mut [u8; 8]), uses as_chunks_mut::<2>() in pairs, and marks zz, quads, pairs, bc1_color, bc4_alpha, add_index, and Bits::put #[inline(always)] (with a 64-bit mask before widening to u128 in Bits::put).
  • Bitmask anchor checks in BC7 and BC6H: Replaces anchors.contains(...) slice scans in bc7_pack and per-pixel anchor branches in bc6 with a precomputed u16 anchor_mask.
  • Unit tests: Adds two pure-CPU unit tests (lut_helpers_match_scalar_reference_exhaustively and all_bc_fixtures_and_packers_match_baseline) exhaustively verifying interleave, wide_interleave, nibbles, Widths masks/lengths, BC7/BC6H anchor_mask, Bits::put, all 12 valid shipped tests/data/bcpack fixtures against unmodified origin/main FNV-1a hashes (plus bc7-700-2 error parity), bc1_color/bc4_alpha, and all 14 BC6H modes + all 8 BC7 modes.

Part of #1833

Tested

  • Baseline capture on origin/main (b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7): Recorded exact FNV-1a hashes for all 12 valid shipped tests/data/bcpack fixtures (and bc7-700-2 error parity) plus baseline end-to-end release bcpack::decode timings (bc1-8192-3: 701.3 us, bc7-8192-3: 1470.5 us, bc1-700-2: 185.5 us).
  • Post-fix end-to-end release bcpack::decode timings (best of 5, including untouched rANS stage):
    • bc1-8192-3 (64 KiB, 8192 blocks): 701.3 -> 650.9 us (1.08x)
    • bc7-8192-3 (64 KiB, 4096 blocks): 1470.5 -> 1147.2 us (1.28x)
    • bc1-700-2 (700 blocks): 185.5 -> 103.5 us (1.79x)
  • Standalone rustc -O per-primitive micro-benchmarks (100% exhaustive scalar parity verified):
    • interleave: 3.89 -> 3.14 ns/op (1.24x)
    • nibbles: 5.45 -> 2.78 ns/op (1.96x)
    • wide_interleave: 12.34 -> 3.25 ns/op (3.79x)
    • bc7_indices (anchor_mask): 84.95 -> 55.70 ns/op (1.53x)
    • bc1_color: 18.00 -> 12.16 ns/op (1.48x)
    • bc4_alpha: 14.64 -> 11.47 ns/op (1.28x)
    • plain residual mask loop: BC1 10.46 -> 4.22 ns/op (2.48x), BC3 12.49 -> 6.69 ns/op (1.87x)
  • Debug WARP (XCL_ADAPTER=warp, XCL_WARP=1): cargo test -p xcl --lib -- hle::bcpack:: --nocapture -> 8 passed; 0 failed; 0 ignored (0 skipped, 0.04s).
  • Release WARP (XCL_ADAPTER=warp, XCL_WARP=1): cargo test --release -p xcl --lib -- hle::bcpack:: --nocapture -> 8 passed; 0 failed; 0 ignored (0 skipped, 0.01s).
  • Clippy: cargo clippy --all-targets -> 0 new warnings in crates/xcl/src/hle/bcpack/victory.rs.
  • Release RTX 4060: cargo test --release -p xcl --lib -- hle::bcpack:: --nocapture -> 8 passed; 0 failed; 0 ignored (0 skipped, 0.01s).
  • Full release workspace on RTX 4060: cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture -> 37 test binaries, 1437 passed; 0 failed; 47 ignored (29 pre-existing host-gated SKIPPED lines: 21 wave64-only tests on a 32-lane GPU, 3 per-lane wave32 tests refused by the driver, 3 D3D12 debug layer tests without d3d12SDKLayers.dll, 1 no heap windows, 1 cannot disable the driver cache).

Regression run

Baseline (origin/main):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1571 (PresentX 1571)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13975 MB

Branch (HEAD):

xcl-smoke v1
commit: d4baa281541c058cda6cd4553d5c0f9f26249918
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1443 (PresentX 1443)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14015 MB

Not tested

  • External XCL_SAMPLE_BCPACK sample corpus (tests/bcpack_parity.rs skips when the env var is unset).
  • Wave64-only GPU tests and D3D12 debug-layer tests (unavailable on this 32-lane RTX 4060 host without d3d12SDKLayers.dll).

AI use

Implemented, unit-tested, benchmarked, and verified with assistance from Google Antigravity (Gemini 4 Neon); every change and test result was reviewed and executed locally.

<!-- greyfinch-sync:ivall/xcl:issue:5505675949 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2702). Originally posted by [Decodere](https://github.com/Decodere) on 2026-11-26T07:14:03Z. --- **What is in it** Replaces per-block bit-spread/deinterleave loops, dynamic mask shifts, slice anchor scans, and bounds-checked slice indexing in `crates/xcl/src/hle/bcpack/victory.rs` with compile-time lookup tables, precomputed masks, `u16` anchor bitmasks, and fixed-size array packing helpers on the CPU BCPack decompression path. - **Compile-time bit-spread and nibble LUTs:** Adds `SPREAD4` (`[u8; 16]`), `SPREAD8` (`[u16; 256]`), and `NIBBLES` (`[[i16; 2]; 256]`) with `#[inline(always)]` `interleave`, `wide_interleave` (used in `bc6`), and `nibbles` replacing the runtime bit-spread and deinterleave folds. - **Precomputed `Widths` masks and active loops in `plain`:** Replaces bit-width arrays on `Widths` with precomputed `ep_mask`, `ep_len`, `ix_mask`, and `ix_len`. `plain` loops only over active endpoint/index counts (`0..w.ep_len` and `0..w.ix_len`, with a fixed 16-byte SIMD fast path when `w.ix_len == 16` for BC1/BC4), removing dynamic shifts from every element. - **Fixed-size block-packing helpers:** Narrows `quads` (`&[u8; 4]`, `&mut [u8; 16]`), `bc1_color` (`&[u8; 6]`, `&[u8; 16]`, `&mut [u8; 8]`, writing a single `u64::to_le_bytes()` store), and `bc4_alpha` (`&[u8; 16]`, `&mut [u8; 8]`), uses `as_chunks_mut::<2>()` in `pairs`, and marks `zz`, `quads`, `pairs`, `bc1_color`, `bc4_alpha`, `add_index`, and `Bits::put` `#[inline(always)]` (with a 64-bit mask before widening to `u128` in `Bits::put`). - **Bitmask anchor checks in BC7 and BC6H:** Replaces `anchors.contains(...)` slice scans in `bc7_pack` and per-pixel anchor branches in `bc6` with a precomputed `u16` `anchor_mask`. - **Unit tests:** Adds two pure-CPU unit tests (`lut_helpers_match_scalar_reference_exhaustively` and `all_bc_fixtures_and_packers_match_baseline`) exhaustively verifying `interleave`, `wide_interleave`, `nibbles`, `Widths` masks/lengths, BC7/BC6H `anchor_mask`, `Bits::put`, all 12 valid shipped `tests/data/bcpack` fixtures against unmodified `origin/main` FNV-1a hashes (plus `bc7-700-2` error parity), `bc1_color`/`bc4_alpha`, and all 14 BC6H modes + all 8 BC7 modes. Part of #1833 **Tested** - **Baseline capture on `origin/main` (`b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7`):** Recorded exact FNV-1a hashes for all 12 valid shipped `tests/data/bcpack` fixtures (and `bc7-700-2` error parity) plus baseline end-to-end release `bcpack::decode` timings (`bc1-8192-3`: `701.3 us`, `bc7-8192-3`: `1470.5 us`, `bc1-700-2`: `185.5 us`). - **Post-fix end-to-end release `bcpack::decode` timings (best of 5, including untouched rANS stage):** - `bc1-8192-3` (64 KiB, 8192 blocks): `701.3 -> 650.9 us` (`1.08x`) - `bc7-8192-3` (64 KiB, 4096 blocks): `1470.5 -> 1147.2 us` (`1.28x`) - `bc1-700-2` (700 blocks): `185.5 -> 103.5 us` (`1.79x`) - **Standalone `rustc -O` per-primitive micro-benchmarks (100% exhaustive scalar parity verified):** - `interleave`: `3.89 -> 3.14 ns/op` (`1.24x`) - `nibbles`: `5.45 -> 2.78 ns/op` (`1.96x`) - `wide_interleave`: `12.34 -> 3.25 ns/op` (`3.79x`) - `bc7_indices` (`anchor_mask`): `84.95 -> 55.70 ns/op` (`1.53x`) - `bc1_color`: `18.00 -> 12.16 ns/op` (`1.48x`) - `bc4_alpha`: `14.64 -> 11.47 ns/op` (`1.28x`) - `plain` residual mask loop: BC1 `10.46 -> 4.22 ns/op` (`2.48x`), BC3 `12.49 -> 6.69 ns/op` (`1.87x`) - **Debug WARP (`XCL_ADAPTER=warp`, `XCL_WARP=1`):** `cargo test -p xcl --lib -- hle::bcpack:: --nocapture` -> `8 passed; 0 failed; 0 ignored` (`0` skipped, `0.04s`). - **Release WARP (`XCL_ADAPTER=warp`, `XCL_WARP=1`):** `cargo test --release -p xcl --lib -- hle::bcpack:: --nocapture` -> `8 passed; 0 failed; 0 ignored` (`0` skipped, `0.01s`). - **Clippy:** `cargo clippy --all-targets` -> `0` new warnings in `crates/xcl/src/hle/bcpack/victory.rs`. - **Release RTX 4060:** `cargo test --release -p xcl --lib -- hle::bcpack:: --nocapture` -> `8 passed; 0 failed; 0 ignored` (`0` skipped, `0.01s`). - **Full release workspace on RTX 4060:** `cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture` -> `37` test binaries, `1437 passed; 0 failed; 47 ignored` (`29` pre-existing host-gated `SKIPPED` lines: `21` wave64-only tests on a 32-lane GPU, `3` per-lane wave32 tests refused by the driver, `3` D3D12 debug layer tests without `d3d12SDKLayers.dll`, `1` `no heap windows`, `1` `cannot disable the driver cache`). **Regression run** Baseline (`origin/main`): ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1571 (PresentX 1571) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 13975 MB ``` Branch (`HEAD`): ``` xcl-smoke v1 commit: d4baa281541c058cda6cd4553d5c0f9f26249918 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1443 (PresentX 1443) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14015 MB ``` **Not tested** - External `XCL_SAMPLE_BCPACK` sample corpus (`tests/bcpack_parity.rs` skips when the env var is unset). - Wave64-only GPU tests and D3D12 debug-layer tests (unavailable on this 32-lane RTX 4060 host without `d3d12SDKLayers.dll`). **AI use** Implemented, unit-tested, benchmarked, and verified with assistance from Google Antigravity (Gemini 4 Neon); every change and test result was reviewed and executed locally.
throny commented 2026-11-30 20:10:50 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-11-30T20:10:50Z.


Closing: this change is already on main as commit ec5c3e21 (brought in by b0b041fd Merge pull request #2959 from bmjask/stack/308-benchmark-thre). This PR's diff applies in reverse cleanly on current main (ff0c364b), so merging it would change nothing. If something here is not on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5) checked this; part of the duplicate sweep on #3041.

<!-- greyfinch-sync:ivall/xcl:comment:7290527517 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2702#issuecomment-7290527517). Originally posted by [throny](https://github.com/throny) on 2026-11-30T20:10:50Z. --- Closing: this change is already on `main` as commit ec5c3e21 (brought in by b0b041fd Merge pull request #2959 from bmjask/stack/308-benchmark-thre). This PR's diff applies in reverse cleanly on current `main` (`ff0c364b`), so merging it would change nothing. If something here is not on `main`, say so and reopen it, or ask and I will. AI use: Claude Code (Claude Opus 5.5) checked this; part of the duplicate sweep on #3041.

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
xcl-dev/xcl!2692
No description provided.