Skip to content
Merged
48 changes: 48 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,54 @@ add_test(NAME startup_storage_refused_reuse_accept_refused_control
set_tests_properties(startup_storage_refused_reuse startup_storage_refused_reuse_accept_refused_control PROPERTIES TIMEOUT 60)
set_tests_properties(startup_storage_refused_reuse_accept_refused_control PROPERTIES PASS_REGULAR_EXPRESSION
"FAIL: R1 destination taken as data matches the stream prefix in 0 of 4096 byte")

# Test-only BCPack encoder (tests/bcpack_encoder.h): random synthetic streams must decode back to their
# symbols. Interleaving with a 7-byte cycle budget is the negative control.
add_executable(bcpack_encoder_check tests/bcpack_encoder_check.cpp)
target_compile_features(bcpack_encoder_check PRIVATE cxx_std_17)
target_include_directories(bcpack_encoder_check PRIVATE src)
add_test(NAME bcpack_encoder_roundtrip COMMAND bcpack_encoder_check)
add_test(NAME bcpack_encoder_window_control COMMAND bcpack_encoder_check --mutate window)
set_tests_properties(bcpack_encoder_roundtrip bcpack_encoder_window_control PROPERTIES TIMEOUT 120)
set_tests_properties(bcpack_encoder_window_control PROPERTIES WILL_FAIL TRUE)
# Each decode_lanes path against the original cycle loop on whole, truncated and bit-flipped streams
# (tests/bcpack_lanes_check.cpp), one test per path; 77 means this CPU lacks that path. The control
# build takes a wrong short DRAIN step at the home state and must fail on every path.
foreach(variant IN ITEMS check control)
add_executable(bcpack_lanes_${variant} tests/bcpack_lanes_check.cpp)
target_compile_features(bcpack_lanes_${variant} PRIVATE cxx_std_17)
target_include_directories(bcpack_lanes_${variant} PRIVATE src)
target_compile_definitions(bcpack_lanes_${variant} PRIVATE X2W_BCPACK_LANE_TESTS)
endforeach()
target_compile_definitions(bcpack_lanes_control PRIVATE X2W_BCPACK_LANES_CONTROL)
set(bcpack_lane_paths scalar avx2)
# Each path is also checked against outside anchors: the symbols stored in the repository vectors, the
# expected bytes in tests/bcpack_mode6_vector.py and tests/startup_stub_consumer.cpp, and the vendored
# Python decoder on a sample of streams (tests/bcpack_lanes_python_check.py).
foreach(path IN LISTS bcpack_lane_paths)
add_test(NAME bcpack_lanes_${path} COMMAND bcpack_lanes_check "${CMAKE_SOURCE_DIR}/tools/_bcpack_rans/vectors"
--path ${path} --consumer "${CMAKE_SOURCE_DIR}/tests/startup_stub_consumer.cpp")
add_test(NAME bcpack_lanes_${path}_drain_control
COMMAND bcpack_lanes_control "${CMAKE_SOURCE_DIR}/tools/_bcpack_rans/vectors" --path ${path})
set(bcpack_lane_tests bcpack_lanes_${path} bcpack_lanes_${path}_drain_control)
if(EXISTS "${INTAKE_PYTHON}")
foreach(variant IN ITEMS check control)
set(name bcpack_lanes_python_${path})
if(variant STREQUAL control)
set(name bcpack_lanes_python_${path}_drain_control)
endif()
add_test(NAME ${name} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/bcpack_lanes_python_check.py"
"$<TARGET_FILE:bcpack_lanes_${variant}>" "${CMAKE_SOURCE_DIR}/tools/_bcpack_rans/vectors" --path ${path})
list(APPEND bcpack_lane_tests ${name})
endforeach()
set_tests_properties(bcpack_lanes_python_${path}_drain_control PROPERTIES
PASS_REGULAR_EXPRESSION "${path} path, [0-9]+ bytes, widths .*: Python accepts, C\\+\\+ rejects")
endif()
set_tests_properties(${bcpack_lane_tests} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77)
# Both the vector symbols and the reference sweep must catch the control build.
set_tests_properties(bcpack_lanes_${path}_drain_control PROPERTIES PASS_REGULAR_EXPRESSION
"${path} path: synthetic_11 lane [0-9]+ differs from the vector's symbols.*result: anchors failed, [1-9][0-9]* mismatches")
endforeach()
# A queue whose worker cannot start (both CreateThread attempts fail, or the construction of its work
# throws std::bad_alloc) completes status entries and fences at Submit, never inside Enqueue (#1199).
# The test compiles the stand-in's source into itself so it can redirect CreateThread and operator new.
Expand Down
68 changes: 68 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -9786,6 +9786,74 @@ mapping, archive stream discovery and rendered pixels remain separate work.
The existing prefix probe keeps its diagnostic behavior. No local archive or
texture payload is used by these checks.

### BCPack lane decoder paths

The startup storage stand-in decodes BCPack streams with `decode_lanes` in
`src/startup_stubs/bcpack_decoder.h`. All lanes of one stream share one byte cursor. Each cycle, the
first eight lanes that request a byte receive consecutive stream bytes, in lane order. When a lane asks
for a byte depends on its rANS state, and so on every symbol it has decoded. One stream therefore cannot
be split between threads. Within a single cycle the lanes are independent apart from that prefix
count, so the decoder runs them side by side.

`decode_lanes` uses an AVX2 path (16 lanes as two 8-lane halves) when `cpuid` reports AVX, AVX2 and
POPCNT and the OS saves the YMM state. Otherwise it uses a scalar loop that runs both phases of a cycle
in one pass. On every path, header bytes, fragment ends and flushes go through the original per-lane
step. The original loop is kept as `decode_lanes_reference` for tests. The SIMD code is compiled only by
MSVC for x64; a clang-cl build has only the scalar path, and its AVX2 tests report 77 with that reason
rather than a CPU limit.

Each path has its own tests: `bcpack_lanes_scalar`, `bcpack_lanes_avx2` and their
`bcpack_lanes_python_*` counterparts. A path test compares only that path with the reference and
returns 77 (capability skip) when the CPU lacks the path. A CPU without AVX2 therefore passes the
scalar tests and reports the AVX2 tests as skipped.

The comparison uses the two repository vectors, the mode-6 vector and synthetic streams from the
test-only encoder in `tests/bcpack_encoder.h`. The synthetic streams cover BC1, BC7 and other lane
tables, flushed and unflushed fragments, and wrong symbol counts. Each stream is tested whole,
truncated and with single bits flipped. The check requires the same accept or reject decision and the
same symbols, also on rejection. It also compares every `decode_bcpack` destination byte, including
for damaged streams that the reference accepts. Inputs end at a no-access page.

The encoder learns the stream order from the decoder's own lane code. So each path is also checked
against data it did not produce:
- the lane symbols stored in the repository vectors;
- the expected bytes in `tests/bcpack_mode6_vector.py` and `tests/startup_stub_consumer.cpp`;
- the vendored Python decoder (`tests/bcpack_lanes_python_check.py`), on 225 sampled streams.

The `*_drain_control` builds take a wrong DRAIN step and must fail; `bcpack_encoder_window_control`
must fail as well.

Real fragment lengths are unknown. The repository vectors used at most 150 symbols per fragment; v12
averages about 72. The benchmark runs at most 150 and at most 4096 symbols per fragment. Each round runs
every path in turn, the first round is a warm-up and the best of seven is reported. Every path must
give the same checksum of symbols and destination.

```powershell
cmake --preset release
cmake --build --preset release --target bcpack_lanes_check
.\out\build\release\bcpack_lanes_check.exe tools\_bcpack_rans\vectors --full
.\out\build\release\bcpack_lanes_check.exe tools\_bcpack_rans\vectors --bench --large
ctest --preset dev --no-tests=error -R '^bcpack_(lanes|encoder)_' -V
```

Observed on a Ryzen 7 9800X3D with Windows 11 build 26300 and MSVC Release (`/O2`):
- `--full` compared 423,874 streams with 0 mismatches on every path. The reference accepted 48,004
damaged streams, and every path matched it on each one.
- `--bench --large` gave the `decode_bcpack` times below for 4 MiB destinations (524,288 BC1 or
262,144 BC7 blocks), with the lane-decode rate in parentheses.

| Stream | Reference | Scalar | AVX2 |
|---|---|---|---|
| BC1, at most 150 symbols per fragment | 161 ms (24.5 ns/symbol) | 127 ms (18.9) | 46 ms (5.9) |
| BC1, at most 4096 | 119 ms (17.4) | 91 ms (13.0) | 31 ms (3.7) |
| BC7, at most 150 | 283 ms (27.0) | 232 ms (20.8) | 107 ms (6.0) |
| BC7, at most 4096 | 227 ms (20.2) | 181 ms (15.0) | 89 ms (4.0) |

End to end, the scalar loop is 1.2-1.3x faster than the reference; the lane step alone is
1.3-1.4x faster here and up to 2x on the 700-block vector. AVX2 is 3.5-3.8x faster for BC1 and
2.6x for BC7. BC7 block reconstruction, unchanged, now takes about half of the BC7 time. The AVX2
gain was measured on Zen 5, which has fast gathers; CPUs with slow gathers are unmeasured.

### Reading logical ranges and probing structured resources

`tools/paged_resource.py` reads a logical byte range from an open archive and a supplied segment map. Each segment gives a physical offset, stored byte count excluding padding, decoded byte count, and codec. Raw and zlib segments may be mixed. The reader verifies the complete compressed stream and decoded size before returning bytes from it. Reads that touch an unknown codec fail. Segments after it remain independently addressable.
Expand Down
Loading
Loading