From b26f5810058230fe64ad62fa408f8caa6f56272a Mon Sep 17 00:00:00 2001 From: IlyassRS Date: Wed, 25 Nov 2026 18:10:47 +0100 Subject: [PATCH] perf(bcpack): 128-bit register BitWriter, lazy BPTC zero/ring init, and index_row LUT --- runtime/CMakeLists.txt | 30 +++ runtime/src/bcpack.cpp | 290 ++++++++++++++++++---------- runtime/test/bcpack_decode_test.cpp | 128 ++++++++++++ 3 files changed, 344 insertions(+), 104 deletions(-) create mode 100644 runtime/test/bcpack_decode_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index c1d0e7cb53..66eebc8df6 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1359,6 +1359,36 @@ if(BUILD_TESTING) # LEONIDA_SLOT133_LAYOUT_WORD=1 (set by the test itself): the slot 133 stand-in reads its fourth word as a layout. add_test(NAME xs_proxy_mock_slot133_layout COMMAND xs_proxy_mock_test --slot133-layout) endif() + # Portable BCPack codec/BPTC decode test (src/bcpack.cpp) on synthetic Python-generated vectors. + find_package(Python3 COMPONENTS Interpreter QUIET) + if(Python3_Interpreter_FOUND) + set(bcpack_decode_test_dir "${CMAKE_CURRENT_BINARY_DIR}/bcpack_decode_test_gen") + set(bcpack_decode_test_tables "${bcpack_decode_test_dir}/bcpack_tables.inc") + set(bcpack_decode_test_vectors "${bcpack_decode_test_dir}/dstorage_vectors.inc") + add_custom_command(OUTPUT "${bcpack_decode_test_tables}" + COMMAND "${CMAKE_COMMAND}" -E make_directory "${bcpack_decode_test_dir}" + COMMAND "${Python3_EXECUTABLE}" -I "${CMAKE_CURRENT_SOURCE_DIR}/../tools/gen_bcpack_runtime_tables.py" + "${bcpack_decode_test_tables}" + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../tools/gen_bcpack_runtime_tables.py" + "${CMAKE_CURRENT_SOURCE_DIR}/../tools/bcpack.py" + "${CMAKE_CURRENT_SOURCE_DIR}/../tools/bcpack_tables.py" + "${CMAKE_CURRENT_SOURCE_DIR}/../tools/bcpack_tables_bptc.py" + VERBATIM) + add_custom_command(OUTPUT "${bcpack_decode_test_vectors}" + COMMAND "${CMAKE_COMMAND}" -E make_directory "${bcpack_decode_test_dir}" + COMMAND "${Python3_EXECUTABLE}" -I "${CMAKE_CURRENT_SOURCE_DIR}/test/dstorage_vectors.py" + "${bcpack_decode_test_vectors}" + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/test/dstorage_vectors.py" + "${CMAKE_CURRENT_SOURCE_DIR}/../tests/test_bcpack.py" + "${CMAKE_CURRENT_SOURCE_DIR}/../tools/bcpack.py" + "${CMAKE_CURRENT_SOURCE_DIR}/../tools/bcpack_tables.py" + "${CMAKE_CURRENT_SOURCE_DIR}/../tools/bcpack_tables_bptc.py" + VERBATIM) + add_executable(bcpack_decode_test test/bcpack_decode_test.cpp src/bcpack.cpp + "${bcpack_decode_test_tables}" "${bcpack_decode_test_vectors}") + target_include_directories(bcpack_decode_test PRIVATE "${bcpack_decode_test_dir}") + add_test(NAME bcpack_decode COMMAND bcpack_decode_test) + endif() # Reads d3d12.h and takes MSVC options, so only where the Windows runtime is built. if(TARGET gta6rt) add_executable(pso_stream_test diff --git a/runtime/src/bcpack.cpp b/runtime/src/bcpack.cpp index bb4646787e..57d3920585 100644 --- a/runtime/src/bcpack.cpp +++ b/runtime/src/bcpack.cpp @@ -24,7 +24,7 @@ void color(uint8_t *dst, const int *e, int a, int b, int c, int d) { } struct Endpoints { - int v[8]{}; + int v[8]; }; bool rebuild_classic(unsigned codec, size_t i, const Symbols &a, @@ -34,7 +34,7 @@ bool rebuild_classic(unsigned codec, size_t i, const Symbols &a, if (a[1] || b[1]) return false; const unsigned distance = unzig(single ? a[0] : interleave(a[0], b[0])) & 255; - Endpoints e; + Endpoints e{}; const int count = codec == 1 ? 6 : codec == 3 ? 8 : codec == 4 ? 2 : 4; if (!distance) { if (codec != 1) @@ -74,8 +74,13 @@ bool rebuild_classic(unsigned codec, size_t i, const Symbols &a, } *dst++ = uint8_t(e.v[ch * 2]); *dst++ = uint8_t(e.v[ch * 2 + 1]); - for (int j = 0; j < 6; ++j) - *dst++ = uint8_t(bits >> (8 * j)); + if constexpr (std::endian::native == std::endian::little) { + std::memcpy(dst, &bits, 6); + dst += 6; + } else { + for (int j = 0; j < 6; ++j) + *dst++ = uint8_t(bits >> (8 * j)); + } } if (codec == 1) color(dst, e.v, quad1[a[8]], quad1[a[9]], quad1[a[10]], quad1[a[11]]); @@ -84,38 +89,89 @@ bool rebuild_classic(unsigned codec, size_t i, const Symbols &a, return true; } +// Register-resident 128-bit accumulator for 16-byte BPTC (BC6H/BC7) blocks; flushed in two 64-bit stores. struct BitWriter { - uint8_t *dst; + uint64_t lo{}, hi{}; int pos{}, violations{}; void put(int value, int bits) { - while (bits > 0) { - const int n = min(bits, 8 - pos % 8); - dst[pos / 8] |= uint8_t((value & ((1 << n) - 1)) << (pos % 8)); - value >>= n; - bits -= n; - pos += n; + if (bits <= 0 || pos >= 128) return; + const uint64_t mask = bits >= 64 ? ~uint64_t{0} : ((uint64_t{1} << bits) - 1u); + const uint64_t v = static_cast(static_cast(value)) & mask; + if (pos < 64) { + lo |= v << pos; + if (pos + bits > 64) hi |= v >> (64 - pos); + } else { + hi |= v << (pos - 64); + } + pos += bits; + } + void put1(int bit) { + const uint64_t v = static_cast(bit & 1); + if (pos < 64) lo |= v << pos; + else if (pos < 128) hi |= v << (pos - 64); + ++pos; + } + void flush(uint8_t *dst) const { + if constexpr (std::endian::native == std::endian::little) { + std::memcpy(dst, &lo, 8); + std::memcpy(dst + 8, &hi, 8); + } else { + for (int i = 0; i < 8; ++i) { + dst[i] = uint8_t(lo >> (8 * i)); + dst[8 + i] = uint8_t(hi >> (8 * i)); + } } } }; +// Precomputed unzigged 4-bit deltas from even/odd bits of an 8-bit index-row lane symbol. +constexpr array make_unzig_even() { + array t{}; + for (int s = 0; s < 256; ++s) { + const int d = (s & 1) | ((s >> 1) & 2) | ((s >> 2) & 4) | ((s >> 3) & 8); + t[s] = int8_t(d & 1 ? -((d + 1) >> 1) : d >> 1); + } + return t; +} +constexpr array make_unzig_odd() { + array t{}; + for (int s = 0; s < 256; ++s) { + const int u = s >> 1; + const int d = (u & 1) | ((u >> 1) & 2) | ((u >> 2) & 4) | ((u >> 3) & 8); + t[s] = int8_t(d & 1 ? -((d + 1) >> 1) : d >> 1); + } + return t; +} +inline constexpr array kUnzigEven = make_unzig_even(); +inline constexpr array kUnzigOdd = make_unzig_odd(); + // Endpoints and indices have independent predictors, with distances limited to -// eight bits. +// eight bits. Kept trivially default-constructible so decode()'s 256-slot ring +// skips a 59 KiB stack zero-fill per call (slots are only read after being written). struct BptcState { - array ep{}; - array ix{}; - int ep_root = -1, ix_root = -1; + array ep; + array ix; + int ep_root, ix_root; }; int mod_bits(int v, int n) { return v & ((1 << n) - 1); } void index_row(int a, int b, int row, int bits, const int *ref, int *value, int *history) { + const int shift = 4 - bits; + const int mask = (1 << bits) - 1; + const int base = 4 * row; + const int ia = a & 255; + const int ib = b & 255; + const int d0 = row < 3 ? kUnzigOdd[ia] : kUnzigEven[ia]; + const int d1 = row < 3 ? kUnzigEven[ia] : kUnzigOdd[ia]; + const int d2 = row < 3 ? kUnzigOdd[ib] : kUnzigEven[ib]; + const int d3 = row < 3 ? kUnzigEven[ib] : kUnzigOdd[ib]; + const int deltas[4] = {d0, d1, d2, d3}; for (int x = 0; x < 4; ++x) { - const int s = (x < 2 ? a : b) >> ((x % 2) ^ (row < 3)); - const int delta = - (s & 1) | ((s >> 1) & 2) | ((s >> 2) & 4) | ((s >> 3) & 8); - const int p = 4 * row + x; - value[p] = mod_bits((ref[p] >> (4 - bits)) + unzig(delta), bits); - history[p] = value[p] << (4 - bits); + const int p = base + x; + const int v = ((ref[p] >> shift) + deltas[x]) & mask; + value[p] = v; + history[p] = v << shift; } } @@ -130,11 +186,8 @@ bool put_indices(BitWriter &writer, const int *values, int bits, return true; } -bool bc6(const Symbols &a, const Symbols &b, const BptcState &ep, +bool bc6(const Symbols &a, const Symbols &b, const BC6Mode &m, const BptcState &ep, const BptcState &ix, BptcState &next, BitWriter &writer) { - if (a[15] >= 14) - return false; - const auto &m = bc6Modes[a[15]]; const int partition = min(b[15], 31), base_shift = 16 - m.base; int fields[3][4]{}; for (int c = 0; c < 3; ++c) { @@ -171,7 +224,7 @@ bool bc6(const Symbols &a, const Symbols &b, const BptcState &ep, writer.put(m.code, m.code < 2 ? 2 : 5); for (int j = 0; j < m.count; ++j) { const auto &f = bc6Plan[m.first + j]; - writer.put((f[0] < 0 ? partition : fields[f[0]][f[1]]) >> f[2], 1); + writer.put1((f[0] < 0 ? partition : fields[f[0]][f[1]]) >> f[2]); } int values[16]; const int bits = m.regions == 2 ? 3 : 4; @@ -182,15 +235,10 @@ bool bc6(const Symbols &a, const Symbols &b, const BptcState &ep, m.regions == 2 ? anchor2[partition] : -1); } -bool bc7(const Symbols &a, const Symbols &b, const BptcState &ep, +bool bc7(const Symbols &a, const Symbols &b, const uint8_t (&cfg)[4], const BptcState &ep, const BptcState &ix, BptcState &next, BitWriter &writer) { - if (a[15] >= 32 || b[15] >= 16) - return false; - const auto &cfg = bc7Config[a[15] * 16 + b[15]]; const auto [tag, part, rot, isel] = cfg; const int mode = tag & 127; - if (mode > 7 || part >= 64) - return false; const auto &f = bc7_formats[mode]; int widths[4] = {f.color, f.color, f.color, f.alpha}; if (rot) @@ -200,10 +248,12 @@ bool bc7(const Symbols &a, const Symbols &b, const BptcState &ep, for (int sub = 0; sub < f.subsets; ++sub) { for (int end = 0; end < 2; ++end) { for (int c = 0; c < 4; ++c) { - if (!widths[c] || lane[sub][c] < 0) + const int k = 8 * sub + 4 * end + c; + if (!widths[c] || lane[sub][c] < 0) { + next.ep[k] = 0; continue; + } const int n = widths[c] + (f.pbits && c == 0); - const int k = 8 * sub + 4 * end + c; v[sub][end][c] = mod_bits( (ep.ep[k] >> (8 - n)) + unzig((end ? b : a)[lane[sub][c]]), n); next.ep[k] = v[sub][end][c] << (8 - n); @@ -224,6 +274,8 @@ bool bc7(const Symbols &a, const Symbols &b, const BptcState &ep, index_row(a[second_rows[y]], b[second_rows[y]], y, 2, ix.ix.data() + 16, secondary, next.ix.data() + 16); } + if (!two) + fill_n(next.ix.data() + 16, 16, 0); if (mode == 0 && part == 0 && ranges::all_of(next.ep, [](int v) { return !v; }) && ranges::all_of(primary, [](int v) { return !v; })) @@ -245,7 +297,7 @@ bool bc7(const Symbols &a, const Symbols &b, const BptcState &ep, if (f.pbits) { for (int sub = 0; sub < f.subsets; ++sub) for (int end = 0; end < (f.pbits == 1 ? 2 : 1); ++end) - writer.put(v[sub][end][0], 1); + writer.put1(v[sub][end][0]); } const int anc1 = f.subsets == 2 ? anchor2[part] : f.subsets == 3 ? anchor3a[part] @@ -274,6 +326,18 @@ struct Coverage { } }; +constexpr BptcState kZeroBc6 = [] { + BptcState s{{}, {}, -1, -1}; + for (int c = 0; c < 3; ++c) { + s.ep[4 * c] = 32768; + s.ep[4 * c + 1] = 512; + s.ep[4 * c + 2] = s.ep[4 * c + 3] = 32; + } + s.ix.fill(8); + return s; +}(); +inline constexpr BptcState kZeroBc7{{}, {}, -1, -1}; + bool rebuild_bptc(unsigned codec, size_t i, size_t blocks, const Symbols &a, const Symbols &b, array &past, uint8_t *dest, const Calibration *history, bool allow_fitted, @@ -286,44 +350,48 @@ bool rebuild_bptc(unsigned codec, size_t i, size_t blocks, const Symbols &a, } const int islot = di > i ? int(256 + i - di) : -1; const int eslot = de > i ? int(256 + i - de) : -1; + const bool need_hist = eslot >= 0 || islot >= 0; bool fitted = false; - BptcState zero{}; - if (codec == 6) { - for (int c = 0; c < 3; ++c) { - zero.ep[4 * c] = 32768; - zero.ep[4 * c + 1] = 512; - zero.ep[4 * c + 2] = zero.ep[4 * c + 3] = 32; - } - zero.ix.fill(8); - } + BptcState hist_state; + const BptcState *ep_ptr = nullptr; + const BptcState *ix_ptr = nullptr; + const BC6Mode *bc6_mode = nullptr; + const uint8_t (*bc7_cfg)[4] = nullptr; + if (codec == 6) { if (a[15] >= 14) return false; - const auto &m = bc6Modes[a[15]]; - if (eslot >= 0) { - const auto &row = history->bc6[eslot]; - for (int c = 0; c < 3; ++c) { - const int needs[] = {m.base, m.delta[c], - m.regions == 2 ? m.delta[c] : 0, - m.regions == 2 ? m.delta[c] : 0}; - for (int k = 0; k < 4; ++k) { - const int j = 4 * c + k; - zero.ep[j] = row.ep[2 * j] | (row.ep[2 * j + 1] << 8); - fitted |= row.ep_confirmed[j] < needs[k]; + bc6_mode = &bc6Modes[a[15]]; + const auto &m = *bc6_mode; + if (need_hist) { + hist_state = kZeroBc6; + if (eslot >= 0) { + const auto &row = history->bc6[eslot]; + for (int c = 0; c < 3; ++c) { + const int needs[] = {m.base, m.delta[c], + m.regions == 2 ? m.delta[c] : 0, + m.regions == 2 ? m.delta[c] : 0}; + for (int k = 0; k < 4; ++k) { + const int j = 4 * c + k; + hist_state.ep[j] = row.ep[2 * j] | (row.ep[2 * j + 1] << 8); + fitted |= row.ep_confirmed[j] < needs[k]; + } } + fitted |= row.ep_class != 3; + coverage.missing_ep[eslot] |= !row.ep_class; + } + if (islot >= 0) { + const auto &row = history->bc6[islot]; + copy(row.ix.begin(), row.ix.end(), hist_state.ix.begin()); + fitted |= + row.ix_class != 3 || ranges::any_of(row.ix_confirmed, [&](int n) { + return n < (m.regions == 2 ? 3 : 4); + }); + coverage.missing_ix[islot] |= !row.ix_class; } - fitted |= row.ep_class != 3; - coverage.missing_ep[eslot] |= !row.ep_class; - } - if (islot >= 0) { - const auto &row = history->bc6[islot]; - copy(row.ix.begin(), row.ix.end(), zero.ix.begin()); - fitted |= - row.ix_class != 3 || ranges::any_of(row.ix_confirmed, [&](int n) { - return n < (m.regions == 2 ? 3 : 4); - }); - coverage.missing_ix[islot] |= !row.ix_class; } + ep_ptr = eslot >= 0 ? &hist_state : (de ? &past[(i - de) & 255] : &kZeroBc6); + ix_ptr = islot >= 0 ? &hist_state : (di ? &past[(i - di) & 255] : &kZeroBc6); } else { if (a[15] >= 32 || b[15] >= 16) return false; @@ -331,31 +399,37 @@ bool rebuild_bptc(unsigned codec, size_t i, size_t blocks, const Symbols &a, const unsigned mode = cfg[0] & 127; if (mode >= 8 || cfg[1] >= 64) return false; + bc7_cfg = &cfg; if (cfg[0] & 128) fitted = true; - const auto &format = bc7_formats[mode]; - if (eslot >= 0) { - const auto &row = history->bc7[eslot]; - copy(row.ep.begin(), row.ep.end(), zero.ep.begin()); - int width[] = {format.color, format.color, format.color, format.alpha}; - if (cfg[2]) - swap(width[3], width[cfg[2] - 1]); - width[0] += format.pbits != 0; - for (int j = 0; j < 8 * format.subsets; ++j) { - const int need = width[j % 4]; - fitted |= row.ep_confirmed[j] < need || row.ep_bits[j] < need; - coverage.missing_ep[eslot] |= row.ep_bits[j] < min(need, 4); + if (need_hist) { + hist_state = kZeroBc7; + const auto &format = bc7_formats[mode]; + if (eslot >= 0) { + const auto &row = history->bc7[eslot]; + copy(row.ep.begin(), row.ep.end(), hist_state.ep.begin()); + int width[] = {format.color, format.color, format.color, format.alpha}; + if (cfg[2]) + swap(width[3], width[cfg[2] - 1]); + width[0] += format.pbits != 0; + for (int j = 0; j < 8 * format.subsets; ++j) { + const int need = width[j % 4]; + fitted |= row.ep_confirmed[j] < need || row.ep_bits[j] < need; + coverage.missing_ep[eslot] |= row.ep_bits[j] < min(need, 4); + } } - } - if (islot >= 0) { - const auto &row = history->bc7[islot]; - copy(row.ix.begin(), row.ix.end(), zero.ix.begin()); - for (int j = 0; j < (mode == 4 || mode == 5 ? 32 : 16); ++j) { - int need = j < 16 ? format.index : 2; - fitted |= row.ix_confirmed[j] < need || row.ix_bits[j] < need; - coverage.missing_ix[islot] |= row.ix_bits[j] < need; + if (islot >= 0) { + const auto &row = history->bc7[islot]; + copy(row.ix.begin(), row.ix.end(), hist_state.ix.begin()); + for (int j = 0; j < (mode == 4 || mode == 5 ? 32 : 16); ++j) { + int need = j < 16 ? format.index : 2; + fitted |= row.ix_confirmed[j] < need || row.ix_bits[j] < need; + coverage.missing_ix[islot] |= row.ix_bits[j] < need; + } } } + ep_ptr = eslot >= 0 ? &hist_state : (de ? &past[(i - de) & 255] : &kZeroBc7); + ix_ptr = islot >= 0 ? &hist_state : (di ? &past[(i - di) & 255] : &kZeroBc7); } if (fitted) { if (!allow_fitted) { @@ -364,16 +438,15 @@ bool rebuild_bptc(unsigned codec, size_t i, size_t blocks, const Symbols &a, } quality = Quality::fitted; } - const auto &ep = de && eslot < 0 ? past[(i - de) & 255] : zero; - const auto &ix = di && islot < 0 ? past[(i - di) & 255] : zero; + const auto &ep = *ep_ptr; + const auto &ix = *ix_ptr; auto &next = past[i & 255]; - next = {}; next.ep_root = eslot >= 0 ? eslot : ep.ep_root; next.ix_root = islot >= 0 ? islot : ix.ix_root; - fill_n(dest, 16, 0); - BitWriter writer{dest}; - const bool valid = codec == 6 ? bc6(a, b, ep, ix, next, writer) - : bc7(a, b, ep, ix, next, writer); + BitWriter writer{}; + const bool valid = codec == 6 ? bc6(a, b, *bc6_mode, ep, ix, next, writer) + : bc7(a, b, *bc7_cfg, ep, ix, next, writer); + writer.flush(dest); coverage.inconsistent += codec == 6 ? writer.violations : int(writer.violations != 0); if (i + 1 == min(256, blocks)) { @@ -404,18 +477,27 @@ Quality decode(unsigned codec, std::span source, const unsigned block_size = single ? 8 : 16; if (output.size() % block_size) return Quality::failed; - array classic{}; - array bptc{}; + // Allocate only the history ring needed by `codec` without zero-filling 67 KiB up front: + // both rebuild_classic and rebuild_bptc only read past[(i - d) & 255] when 0 < d <= i (already written). + bool valid = false; Coverage coverage{}; - coverage.limit = allow_fitted && history - ? max(2, output.size() / block_size / 1024) - : 0; - const bool valid = visit_blocks(codec, source, output.size(), [&](size_t i, const Symbols &first, const Symbols &tuple) { - auto *dst = output.data() + i * block_size; - if (codec >= 6) - return rebuild_bptc(codec, i, output.size() / block_size, first, tuple, bptc, dst, history, allow_fitted, quality, coverage, reason); - return rebuild_classic(codec, i, first, tuple, classic, dst, allow_fitted, quality); - }, fast_entropy); + if (codec >= 6) { + array bptc; + coverage.limit = allow_fitted && history + ? max(2, output.size() / block_size / 1024) + : 0; + const size_t total_blocks = output.size() / block_size; + valid = visit_blocks(codec, source, output.size(), [&](size_t i, const Symbols &first, const Symbols &tuple) { + auto *dst = output.data() + i * block_size; + return rebuild_bptc(codec, i, total_blocks, first, tuple, bptc, dst, history, allow_fitted, quality, coverage, reason); + }, fast_entropy); + } else { + array classic; + valid = visit_blocks(codec, source, output.size(), [&](size_t i, const Symbols &first, const Symbols &tuple) { + auto *dst = output.data() + i * block_size; + return rebuild_classic(codec, i, first, tuple, classic, dst, allow_fitted, quality); + }, fast_entropy); + } if (valid && (!coverage.inconsistent || quality == Quality::fitted)) return quality; if (failure) *failure = coverage.inconsistent && valid ? "anchor-or-pbit" : reason; return Quality::failed; diff --git a/runtime/test/bcpack_decode_test.cpp b/runtime/test/bcpack_decode_test.cpp new file mode 100644 index 0000000000..4d27b98d05 --- /dev/null +++ b/runtime/test/bcpack_decode_test.cpp @@ -0,0 +1,128 @@ +// Portable CPU-only unit tests for runtime/src/bcpack.cpp (RT-BCPACK-DECODE-FAST-BITWRITER-AND-BPTC): +// verifies bit-exact decode across all 6 BCPack codecs (BC1, BC3, BC4, BC5, BC6H, BC7), calibration history +// profiles, fitted-predictor gating, and malformed input rejection against generated synthetic vectors. +// No Windows headers, no Vulkan, no game files. +#include "../src/bcpack.h" + +#include +#include +#include +#include +#include + +namespace { + +struct Vector { + unsigned codec; + std::span raw, plain, wrapped; +}; + +#include "dstorage_vectors.inc" + +leonida::bcpack::Calibration test_profile() { + leonida::bcpack::Calibration history{}; + auto& six = history.bc6[255]; + for (unsigned c = 0; c < 3; ++c) six.ep[8 * c + 1] = 16; + six.ep_class = six.ix_class = 3; + six.ep_confirmed.fill(16); + six.ix_confirmed.fill(4); + auto& seven = history.bc7[255]; + seven.ep.fill(64); + seven.ep_bits.fill(8); + seven.ep_confirmed.fill(8); + seven.ix_bits.fill(4); + seven.ix_confirmed.fill(4); + return history; +} + +} // namespace + +#define CHECK(cond) do { \ + if (!(cond)) { \ + std::fprintf(stderr, "line %d: CHECK failed: %s\n", __LINE__, #cond); \ + return 1; \ + } \ +} while (0) + +int main() { + using leonida::bcpack::Quality; + using leonida::bcpack::decode; + + // 1. All 6 codecs (BC1, BC3, BC4, BC5, BC6H, BC7) bit-exact with both fast and reference entropy. + for (const Vector& v : vectors) { + for (bool fast : {true, false}) { + std::vector out(v.plain.size(), 0xA5); + const char* failure = nullptr; + const Quality q = decode(v.codec, v.raw, out, false, nullptr, &failure, fast); + CHECK(q == Quality::confirmed); + CHECK(std::memcmp(out.data(), v.plain.data(), v.plain.size()) == 0); + } + } + + // 2. BPTC (BC6H / BC7) calibration profile history: missing-profile -> confirmed -> unconfirmed -> fitted. + { + const auto full_history = test_profile(); + struct ProfileCase { + unsigned codec; + std::span raw; + std::span first; + }; + const ProfileCase cases[] = { + {6, profile_raw6, profile_first6}, + {7, profile_raw7, profile_first7}, + }; + for (const auto& c : cases) { + std::vector out(512, 0xCC); + const char* failure = nullptr; + CHECK(decode(c.codec, c.raw, out, false, nullptr, &failure) == Quality::failed); + CHECK(failure != nullptr && std::strcmp(failure, "missing-profile") == 0); + + CHECK(decode(c.codec, c.raw, out, false, &full_history, &failure) == Quality::confirmed); + for (std::size_t off = 0; off < out.size(); off += 16) { + CHECK(std::memcmp(out.data() + off, c.first.data(), 16) == 0); + } + + // Unconfirmed calibration predictor: refused when allow_fitted=false, fitted when allow_fitted=true. + auto partial = full_history; + if (c.codec == 6) { + partial.bc6[255].ep_confirmed.fill(0); + } else { + partial.bc7[255].ep_confirmed.fill(0); + } + std::fill(out.begin(), out.end(), uint8_t{0xDD}); + CHECK(decode(c.codec, c.raw, out, false, &partial, &failure) == Quality::failed); + CHECK(failure != nullptr && std::strcmp(failure, "unconfirmed-predictor") == 0); + CHECK(decode(c.codec, c.raw, out, true, &partial, &failure) == Quality::fitted); + for (std::size_t off = 0; off < out.size(); off += 16) { + CHECK(std::memcmp(out.data() + off, c.first.data(), 16) == 0); + } + } + } + + // 3. Classic BC1 fitted-history gating. + { + std::vector out(512, 0xEE); + CHECK(decode(1, fitted_raw, out, false) == Quality::failed); + CHECK(decode(1, fitted_raw, out, true) == Quality::fitted); + CHECK(std::memcmp(out.data(), fitted_first, sizeof(fitted_first)) == 0); + } + + // 4. Invalid codecs, misaligned output sizes, and truncated streams fail cleanly. + { + std::vector out(64, 0); + CHECK(decode(0, raw1, out) == Quality::failed); + CHECK(decode(2, raw1, out) == Quality::failed); + CHECK(decode(8, raw1, out) == Quality::failed); + + std::vector bad_single(12, 0); + CHECK(decode(1, raw1, bad_single) == Quality::failed); + std::vector bad_double(24, 0); + CHECK(decode(7, raw7, bad_double) == Quality::failed); + + std::vector out7(sizeof(plain7), 0); + CHECK(decode(7, std::span(raw7, sizeof(raw7) / 2), out7) == Quality::failed); + } + + std::printf("bcpack_decode_test: all checks passed\n"); + return 0; +}