perf(bcpack): 128-bit register BitWriter, lazy BPTC state init, index_row LUT, and portable decode test #1974

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

@⁠IlyassRS3 on GitHub · 2026-11-25 17:10 UTC · original
IlyassRS3:perf/rt-bcpack-decode-fast-bitwriter-and-bptc → main · +344 −104 · 3 files · 1 commits · open

Claim: RT-BCPACK-DECODE-FAST-BITWRITER-AND-BPTC (claim comment #7168983247 on #1608).

Independent of #1935 (runtime/src/bcpack_stream.h, untouched here); the two optimizations compose cleanly.

Optimizations (runtime/src/bcpack.cpp)

  1. 128-bit register-resident BitWriter for BC6H/BC7:
    • Replaces per-bit while (bits > 0) memory-RMW byte loops (called 82+ times per BC6H block) and per-block fill_n(dest, 16, 0) pre-zeroing with two uint64_t accumulators (lo/hi) and a single put1() helper, flushed in two 64-bit little-endian stores (with portable big-endian fallback).
  2. Eliminate 67 KiB up-front stack zero-fill in decode():
    • Previously every decode() call zero-initialized both array<Endpoints, 256> classic{} (8 KiB) and array<BptcState, 256> bptc{} (59 KiB) on the stack, even when decoding classic codecs or small mipmaps.
    • Now dispatches codec >= 6 vs codec < 6 into separate trivially default-constructible ring buffers without up-front zeroing (both rebuild_classic and rebuild_bptc only read past[(i - d) & 255] when 0 < d <= i, after that slot was written).
  3. Lazy BPTC zero / history fallback and per-block zeroing elimination (rebuild_bptc):
    • Uses compile-time kZeroBc6 and kZeroBc7 constants and only constructs a stack hist_state when eslot >= 0 || islot >= 0, skipping 232 bytes of stack zeroing + BC6 endpoint/index fill on steady-state blocks (i >= 256).
    • Avoids double lookup of bc6Modes/bc7Config, eliminates the 232-byte next = {} wipe per BPTC block by explicitly zeroing only unused BC7 endpoint/secondary-index slots, and uses a 6-byte little-endian memcpy in rebuild_classic.
  4. Precomputed kUnzigEven[256] / kUnzigOdd[256] LUTs for index_row(): replaces the per-pixel bit-extraction and unzig() arithmetic inside the 4–8× per-block index loop.
  5. Portable CPU-only CTest bcpack_decode (runtime/test/bcpack_decode_test.cpp): verifies all 6 codecs (BC1, BC3, BC4, BC5, BC6H, BC7) bit-exactly against dstorage_vectors.py with both fast_entropy = true/false, calibration profile history (missing-profile, confirmed, unconfirmed-predictor, fitted), classic fitted gating, and invalid/misaligned/truncated inputs.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran bcpack_decode_test cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Bit-exact end-to-end A/B benchmark at -O2 vs main (including full rANS entropy decode, best of 5 runs, Apple Silicon):
BC1_64blk_mip    (  512 B): base=    6.3 us  new=   4.5 us  speedup= 1.39x
BC6H_64blk_mip   ( 1024 B): base=   17.9 us  new=  16.2 us  speedup= 1.10x
BC7_64blk_mip    ( 1024 B): base=   18.5 us  new=  14.8 us  speedup= 1.25x
BC1_2048blk      (16384 B): base=  185.9 us  new= 177.6 us  speedup= 1.05x
BC3_2048blk      (32768 B): base=  360.9 us  new= 359.1 us  speedup= 1.01x
BC5_2048blk      (32768 B): base=  359.4 us  new= 356.8 us  speedup= 1.01x
BC6H_2048blk     (32768 B): base=  730.5 us  new= 691.5 us  speedup= 1.06x
BC7_2048blk      (32768 B): base=  694.4 us  new= 607.4 us  speedup= 1.14x
  • Ran python3 -I scripts/check_no_game_data.py on all 3 changed files and python3 -I tests/test_text_write_line_endings.py: all passed.
**@⁠IlyassRS3** on GitHub · 2026-11-25 17:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1974) `IlyassRS3:perf/rt-bcpack-decode-fast-bitwriter-and-bptc` → `main` · +344 −104 · 3 files · 1 commits · open Claim: `RT-BCPACK-DECODE-FAST-BITWRITER-AND-BPTC` (claim comment `#7168983247` on #1608). Independent of #1935 (`runtime/src/bcpack_stream.h`, untouched here); the two optimizations compose cleanly. ### Optimizations (`runtime/src/bcpack.cpp`) 1. **128-bit register-resident `BitWriter` for BC6H/BC7**: - Replaces per-bit `while (bits > 0)` memory-RMW byte loops (called 82+ times per BC6H block) and per-block `fill_n(dest, 16, 0)` pre-zeroing with two `uint64_t` accumulators (`lo`/`hi`) and a single `put1()` helper, flushed in two 64-bit little-endian stores (with portable big-endian fallback). 2. **Eliminate 67 KiB up-front stack zero-fill in `decode()`**: - Previously every `decode()` call zero-initialized both `array<Endpoints, 256> classic{}` (8 KiB) and `array<BptcState, 256> bptc{}` (59 KiB) on the stack, even when decoding classic codecs or small mipmaps. - Now dispatches `codec >= 6` vs `codec < 6` into separate trivially default-constructible ring buffers without up-front zeroing (both `rebuild_classic` and `rebuild_bptc` only read `past[(i - d) & 255]` when `0 < d <= i`, after that slot was written). 3. **Lazy BPTC `zero` / history fallback and per-block zeroing elimination (`rebuild_bptc`)**: - Uses compile-time `kZeroBc6` and `kZeroBc7` constants and only constructs a stack `hist_state` when `eslot >= 0 || islot >= 0`, skipping 232 bytes of stack zeroing + BC6 endpoint/index fill on steady-state blocks (`i >= 256`). - Avoids double lookup of `bc6Modes`/`bc7Config`, eliminates the 232-byte `next = {}` wipe per BPTC block by explicitly zeroing only unused BC7 endpoint/secondary-index slots, and uses a 6-byte little-endian `memcpy` in `rebuild_classic`. 4. **Precomputed `kUnzigEven[256]` / `kUnzigOdd[256]` LUTs for `index_row()`**: replaces the per-pixel bit-extraction and `unzig()` arithmetic inside the 4–8× per-block index loop. 5. **Portable CPU-only CTest `bcpack_decode`** (`runtime/test/bcpack_decode_test.cpp`): verifies all 6 codecs (BC1, BC3, BC4, BC5, BC6H, BC7) bit-exactly against `dstorage_vectors.py` with both `fast_entropy = true/false`, calibration profile history (`missing-profile`, `confirmed`, `unconfirmed-predictor`, `fitted`), classic fitted gating, and invalid/misaligned/truncated inputs. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (host is macOS arm64; no retail files). - Ran `bcpack_decode_test` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Bit-exact end-to-end A/B benchmark at `-O2` vs `main` (including full rANS entropy decode, best of 5 runs, Apple Silicon): ```text BC1_64blk_mip ( 512 B): base= 6.3 us new= 4.5 us speedup= 1.39x BC6H_64blk_mip ( 1024 B): base= 17.9 us new= 16.2 us speedup= 1.10x BC7_64blk_mip ( 1024 B): base= 18.5 us new= 14.8 us speedup= 1.25x BC1_2048blk (16384 B): base= 185.9 us new= 177.6 us speedup= 1.05x BC3_2048blk (32768 B): base= 360.9 us new= 359.1 us speedup= 1.01x BC5_2048blk (32768 B): base= 359.4 us new= 356.8 us speedup= 1.01x BC6H_2048blk (32768 B): base= 730.5 us new= 691.5 us speedup= 1.06x BC7_2048blk (32768 B): base= 694.4 us new= 607.4 us speedup= 1.14x ``` - Ran `python3 -I scripts/check_no_game_data.py` on all 3 changed files and `python3 -I tests/test_text_write_line_endings.py`: all passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:13:20 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 17:10 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 17:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1974#pullrequestreview-6061819573) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
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/1974/head:github-bot/gh-1974'
git switch 'github-bot/gh-1974'

Merge

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