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

Open
opened 2026-11-26 21:19:37 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1974
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/rt-bcpack-decode-fast-bitwriter-and-bptc


📝 Commits (1)

  • b26f581 perf(bcpack): 128-bit register BitWriter, lazy BPTC zero/ring init, and index_row LUT

📊 Changes

3 files changed (+344 additions, -104 deletions)

View changed files

📝 runtime/CMakeLists.txt (+30 -0)
📝 runtime/src/bcpack.cpp (+186 -104)
➕ runtime/test/bcpack_decode_test.cpp (+128 -0)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1974 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-bcpack-decode-fast-bitwriter-and-bptc` --- ### 📝 Commits (1) - [`b26f581`](https://github.com/ubiquitarianism/leonida/commit/b26f5810058230fe64ad62fa408f8caa6f56272a) perf(bcpack): 128-bit register BitWriter, lazy BPTC zero/ring init, and index_row LUT ### 📊 Changes **3 files changed** (+344 additions, -104 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+30 -0) 📝 `runtime/src/bcpack.cpp` (+186 -104) ➕ `runtime/test/bcpack_decode_test.cpp` (+128 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1982
No description provided.