diff --git a/docs/textures.md b/docs/textures.md index 2a6fb2df99..8f64972f86 100644 --- a/docs/textures.md +++ b/docs/textures.md @@ -959,6 +959,21 @@ provisional/last-resort entries (inspectable via `bcpack.decode_with_info`). - **BC6H (`codec 6`):** **268 / 268** chunks (100%) decode in provisional mode (`0` strict because every chunk references built-in history, `0` refused). - **BC7 (`codec 7`):** **583 / 5,167** chunks (11.3%) decode in strict mode; **5,150 / 5,167** (99.67%) decode in provisional mode; **17 / 5,167** (0.33%) cleanly refuse when unknown built-in history references exceed the consistency budget. +### 6. Native decoder for the runtime (RT-186) + +`runtime/src/bcpack.{h,cpp}` is a dependency-free C++20 port of `tools/bcpack.py`: +`leonida::bcpack::decode(raw, codec, out, allow_provisional)` gives the same bytes, the same refusals and the +same provisional labels as `bcpack.decode_with_info(raw, codec, len(out), allow_provisional=...)` with +`history=None`. Its tables (`runtime/src/bcpack_tables.inc`) are generated from the Python ones by +`tools/gen_bcpack_tables.py`. `tests/test_bcpack_native.py` encodes random lanes for all six codecs with the test +encoder of `tests/test_bcpack.py` (multi-fragment lanes, narrowed widths, every `q`), corrupts some of them, and +checks that both decoders agree on status, message, labels and bytes, in strict and provisional mode; ctest runs +it as `bcpack_native_vs_python` against the built `bcpack_test`. + +Not ported yet: `--history` loading. Without it the native decoder, like `tools/bcpack.py` without `--history`, +refuses BC6H/BC7 chunks that reference built-in history, which on the corpus above is every BC6H chunk and most +BC7 chunks. + ## What `textures.rpf` holds The 192.6 MB archive has no composition table elsewhere in the docs (only census rows). `tools/rpf9_carve.py` diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 9dc2a7b66c..690543fd39 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -396,6 +396,15 @@ if(BUILD_TESTING) add_executable(retire_tickets_test test/retire_tickets_test.cpp) add_test(NAME retire_tickets COMMAND retire_tickets_test) + # Native BCPack decoder (RT-186): built-in checks, then a byte-for-byte comparison with tools/bcpack.py on + # synthetic chunks (tests/test_bcpack_native.py). Pure stdlib, no GPU or game files. + add_executable(bcpack_test test/bcpack_test.cpp src/bcpack.cpp) + add_test(NAME bcpack COMMAND bcpack_test) + find_package(Python3 COMPONENTS Interpreter QUIET) + if(Python3_FOUND AND NOT CMAKE_CROSSCOMPILING) + add_test(NAME bcpack_native_vs_python COMMAND "${Python3_EXECUTABLE}" -I + "${CMAKE_CURRENT_SOURCE_DIR}/../tests/test_bcpack_native.py" --bin "$") + endif() # VulkanContext::buffer_at's VA index (runtime/src/buffer_va_index.h): pure stdlib, no Vulkan instance. add_executable(buffer_va_index_test test/buffer_va_index_test.cpp) add_test(NAME buffer_va_index COMMAND buffer_va_index_test) diff --git a/runtime/src/bcpack.cpp b/runtime/src/bcpack.cpp new file mode 100644 index 0000000000..2aff8d664f --- /dev/null +++ b/runtime/src/bcpack.cpp @@ -0,0 +1,1336 @@ +// Native BCPack decoder: a line-for-line port of tools/bcpack.py with `history=None` (see bcpack.h). +// Function names follow the Python ones; where the two differ, the Python decoder is the reference. + +#include "bcpack.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace leonida::bcpack { +namespace { + +#include "bcpack_tables.inc" + +constexpr std::int64_t rans_l = std::int64_t{1} << 24; +constexpr int max_width = 10; +constexpr int prefetch_budget = 8; +constexpr int max_virtual_zeros = 8; +constexpr std::size_t element_bytes = 8; + +using Lanes = std::vector>; +using Labels = std::set; + +struct Failure { + Status status; + std::string message; +}; + +[[noreturn]] void fail(std::string message) { throw Failure{Status::error, std::move(message)}; } +[[noreturn]] void refuse(std::string message) { throw Failure{Status::provisional, std::move(message)}; } + +std::string str(long long v) { return std::to_string(v); } + +std::string join(const std::vector& items, std::size_t first, std::size_t last) { + std::string out; + for (std::size_t i = first; i < last && i < items.size(); ++i) { + if (!out.empty()) out += ", "; + out += items[i]; + } + return out; +} + +const char* codec_name(int codec) { + switch (codec) { + case 1: return "BC1"; + case 3: return "BC3"; + case 4: return "BC4"; + case 5: return "BC5"; + case 6: return "BC6H"; + case 7: return "BC7"; + default: return nullptr; + } +} + +// Default per-lane symbol widths (bits) for each codec (bcpack.LANE_WIDTHS). +constexpr std::uint8_t widths_bc1[] = {8, 8, 5, 6, 5, 5, 6, 5, 8, 8, 8, 8}; +constexpr std::uint8_t widths_bc3[] = {4, 4, 8, 6, 6, 6, 6, 5, 6, 5, 8, 8}; +constexpr std::uint8_t widths_bc4[] = {8, 8, 8, 8, 6, 6, 6, 6, 6, 6, 6, 6}; +constexpr std::uint8_t widths_bc5[] = {4, 4, 8, 8, 6, 6, 6, 6, 6, 6, 6, 6}; +constexpr std::uint8_t widths_bc6h[] = {4, 4, 8, 8, 8, 5, 5, 5, 6, 6, 6, 8, 8, 8, 8, 5}; +constexpr std::uint8_t widths_bc7[] = {4, 4, 8, 8, 8, 8, 8, 7, 7, 5, 6, 8, 8, 8, 8, 5}; + +std::span lane_widths(int codec) { + switch (codec) { + case 1: return widths_bc1; + case 3: return widths_bc3; + case 4: return widths_bc4; + case 5: return widths_bc5; + case 6: return widths_bc6h; + case 7: return widths_bc7; + default: return {}; + } +} + +// --- Entropy layer ------------------------------------------------------------------------------------------------- + +// 16-bit rANS banded frequency model for one (symbol_width, q) pair (bcpack._Model). +struct Model { + int w = 0; + const std::uint16_t* freq = nullptr; + std::uint32_t base[11] = {}; + std::uint32_t total = 0; + + int lookup(std::uint32_t cf) const { + int p = w; + while (p > 0 && cf < base[p]) --p; + return p == 0 ? 0 : (1 << (p - 1)) + static_cast((cf - base[p]) / freq[p]); + } + + // The next state, or -1 for any negative one: Python keeps the exact negative value, but a negative state + // only ever compares below RANS_L and stays negative, so its sign is all that matters. + std::int64_t advance(int s, std::int64_t x) const { + const int p = static_cast(std::bit_width(static_cast(s))); + std::int64_t f = freq[p]; + std::int64_t c = base[p] + f * (s - (p == 0 ? 0 : (1 << (p - 1)))); + if (c == 0) { + f -= 1; + c = 1; + } + const std::int64_t next = f * (x >> 16) + (x & 0xFFFF) - c; + return next < 0 ? -1 : next; + } +}; + +const Model& get_model(int w, int q) { + static const auto models = [] { + std::array all{}; + for (int wi = 1; wi <= 10; ++wi) { + for (int qi = 0; qi < 16; ++qi) { + Model& m = all[(wi - 1) * 16 + qi]; + m.w = wi; + m.freq = prob_freq[wi - 1][qi]; + std::uint32_t acc = 0; + for (int p = 0; p <= wi; ++p) { + m.base[p] = acc; + acc += m.freq[p] * (p == 0 ? 1u : 1u << (p - 1)); + } + m.total = acc; + } + } + return all; + }(); + const Model& m = models[(w - 1) * 16 + q]; + if (m.total != 65536) + fail("probability table (" + str(w) + ") does not sum to 65536 (got " + str(m.total) + ")"); + return m; +} + +// A state shifted left by 8 bits, with a byte merged in; negative states stay negative (see Model::advance). +std::int64_t shift_state(std::int64_t st, int byte = 0) { return st < 0 ? -1 : ((st << 8) | byte); } + +// bcpack._decode_lanes_raw: the interleaved rANS lane array into one symbol list per lane. +std::size_t decode_lanes_raw(std::span raw, std::span widths, + std::int64_t nsym, Lanes& out) { + enum { hdr0, hdr1, hdr2, proc }; + const int nl = static_cast(widths.size()); + const std::int64_t L = rans_l; + std::vector ctrl(nl, hdr0), w(nl, 0), q(nl, 0), h3(nl, 0), flush(nl, 0), vz(nl, 0), inbuf(nl, 0), + sym(nl, 0), slotval(nl, 0); + std::vector out_rem(nl, nsym), in_rem(nl, 0), p0(nl, 0), p1(nl, 0); + std::vector done(nl, nsym <= 0), full(nl, 0), held(nl, 0), slotfull(nl, 0), stall(nl, 0); + std::vector model(nl, nullptr); + out.assign(nl, {}); + for (auto& lane : out) lane.reserve(nsym > 0 ? static_cast(nsym) : 0); + std::size_t pos = 0; + const std::size_t nraw = raw.size(); + + auto lane_fail = [&](const char* msg, int lane) { + fail("lane " + str(lane) + " (width " + str(w[lane]) + ", q " + str(q[lane]) + ") at input byte " + + str(static_cast(pos)) + ": " + msg); + }; + + while (true) { + bool all_done = true; + for (int i = 0; i < nl; ++i) { + if (!(done[i] && !slotfull[i])) { + all_done = false; + break; + } + } + if (all_done) return pos; + + // Phase 1: state update, prefetch arbitration (at most 8 bytes/cycle), and symbol release. + int left = prefetch_budget; + for (int i = 0; i < nl; ++i) { + if (done[i]) { + stall[i] = 1; + continue; + } + if (held[i] && slotfull[i]) { + stall[i] = 1; + continue; + } + stall[i] = 0; + if (ctrl[i] == proc) p0[i] = held[i] ? model[i]->advance(sym[i], p1[i]) : p1[i]; + if (!full[i] && (ctrl[i] != proc || in_rem[i] > 0)) { + if (left == 0) { + stall[i] = 1; + continue; + } + if (pos >= nraw) lane_fail("input ran out", i); + inbuf[i] = raw[pos]; + ++pos; + full[i] = 1; + --left; + } + if (held[i]) { + slotfull[i] = 1; + slotval[i] = sym[i]; + held[i] = 0; + } + } + + // Phase 2: header parsing, byte merge, virtual zero fill on flush, and symbol lookup. + for (int i = 0; i < nl; ++i) { + if (done[i] || stall[i]) continue; + const int c = ctrl[i]; + if (c == hdr0) { + if (!full[i]) continue; + const int h = inbuf[i]; + q[i] = h & 15; + flush[i] = (h >> 4) & 1; + h3[i] = (h >> 5) & 1; + w[i] = widths[i] - (h >> 6); + full[i] = 0; + ctrl[i] = hdr1; + vz[i] = 0; + } else if (c == hdr1) { + if (!full[i]) continue; + full[i] = 0; + if (!(1 <= w[i] && w[i] <= max_width)) lane_fail("symbol width out of range", i); + model[i] = &get_model(w[i], q[i]); + if (h3[i]) { + in_rem[i] = inbuf[i]; + ctrl[i] = hdr2; + } else { + in_rem[i] = inbuf[i] + 1; + ctrl[i] = proc; + } + } else if (c == hdr2) { + if (!full[i]) continue; + in_rem[i] = ((static_cast(inbuf[i]) << 8) | (in_rem[i] & 0xFF)) + 257; + ctrl[i] = proc; + full[i] = 0; + } else { + std::int64_t st = p0[i]; + if (st < L) { + if (in_rem[i] > 0) { + if (full[i]) { + st = shift_state(st, inbuf[i]); + --in_rem[i]; + full[i] = 0; + if (in_rem[i] == 0 && flush[i]) { + while (st < L && st != 0 && vz[i] < max_virtual_zeros) { + st = shift_state(st); + ++vz[i]; + } + } + } + } else if (flush[i]) { + if (st == 0 || vz[i] >= max_virtual_zeros) + lane_fail("fragment did not end at its home state", i); + while (st < L && st != 0 && vz[i] < max_virtual_zeros) { + st = shift_state(st); + ++vz[i]; + } + } else { + p1[i] = st; + if (out_rem[i] > 0) + ctrl[i] = hdr0; + else + done[i] = 1; + continue; + } + } + const bool nxt = st > L && out_rem[i] > 0; + if (nxt) { + const int s = model[i]->lookup(static_cast(st & 0xFFFF)); + sym[i] = s; + --out_rem[i]; + if (s >> w[i]) lane_fail("symbol outside its width", i); + } + held[i] = nxt; + p1[i] = st; + if (in_rem[i] == 0) { + if (flush[i]) { + if (st == L) { + if (out_rem[i] > 0) { + ctrl[i] = hdr0; + p0[i] = 0; + p1[i] = 0; + } else { + done[i] = 1; + } + } else if (st > L && !nxt) { + lane_fail("fragment did not end at its home state", i); + } + } else if (out_rem[i] == 0 && !nxt) { + done[i] = 1; + } + } else if (st == L || (out_rem[i] == 0 && !nxt && st > L)) { + lane_fail("fragment did not end at its home state", i); + } + } + } + + // Phase 3: emit one synchronized element when every active lane has filled its slot. + bool ready = true; + bool anyfull = false; + for (int i = 0; i < nl; ++i) { + if (slotfull[i]) { + anyfull = true; + } else if (!(done[i] && !held[i])) { + ready = false; + break; + } + } + if (ready && anyfull) { + for (int i = 0; i < nl; ++i) { + if (slotfull[i]) { + out[i].push_back(static_cast(slotval[i])); + slotfull[i] = 0; + } + } + } + for (int i = 0; i < nl; ++i) { + if (done[i] && held[i] && !slotfull[i]) { + slotfull[i] = 1; + slotval[i] = sym[i]; + held[i] = 0; + } + } + } +} + +std::size_t run_decode_lanes(std::span raw, int codec, std::size_t out_size, Lanes& lanes) { + const auto widths = lane_widths(codec); + if (widths.empty()) fail("unsupported BCPack codec id: " + str(codec)); + const std::size_t bb = block_bytes(codec); + if (out_size % bb != 0) + fail("decoded size " + str(static_cast(out_size)) + " is not a whole number of " + + codec_name(codec) + " blocks"); + const std::size_t used = decode_lanes_raw(raw, widths, static_cast(out_size / element_bytes), lanes); + for (std::size_t i = used; i < raw.size(); ++i) + if (raw[i]) fail("non-zero data left over after the last lane finished"); + return used; +} + +// --- Shared helpers -------------------------------------------------------------------------------------------------- + +int unzig(int v) { return (v & 1) ? -((v + 1) >> 1) : (v >> 1); } + +int inter(int a, int b, int n) { + int d = 0; + for (int k = 0; k < n; ++k) d |= (((a >> k) & 1) << (2 * k)) | (((b >> k) & 1) << (2 * k + 1)); + return d; +} + +int inter8(int a, int b) { return inter(a, b, 4); } + +// 4-bit de-interleave of 8-bit index-pair symbols: even bits, odd bits (bcpack._DEINT). +constexpr auto deint = [] { + std::array, 2> t{}; + for (int s = 0; s < 256; ++s) { + t[0][s] = static_cast(((s >> 0) & 1) | (((s >> 2) & 1) << 1) | (((s >> 4) & 1) << 2) | + (((s >> 6) & 1) << 3)); + t[1][s] = static_cast(((s >> 1) & 1) | (((s >> 3) & 1) << 1) | (((s >> 5) & 1) << 2) | + (((s >> 7) & 1) << 3)); + } + return t; +}(); + +int pymod(int a, int m) { return ((a % m) + m) % m; } + +// Maximum undetermined blocks permitted in provisional mode (1 in 1,024, min 2). +std::size_t loose_limit(std::size_t nblocks) { return std::max(2, nblocks / 1024); } + +bool any_nonzero(const std::vector& lane) { + return std::any_of(lane.begin(), lane.end(), [](std::uint16_t v) { return v != 0; }); +} + +void put_u16(std::uint8_t* at, int v) { + at[0] = static_cast(v & 0xFF); + at[1] = static_cast((v >> 8) & 0xFF); +} + +void put_le(std::uint8_t* at, std::uint64_t v, int nbytes) { + for (int k = 0; k < nbytes; ++k) at[k] = static_cast((v >> (8 * k)) & 0xFF); +} + +// ORs the low `nbits` of `v` into a 128-bit little-endian block at bit `pos`; bits past 128 are dropped. +void put_bits(std::uint8_t* block, int pos, std::uint64_t v, int nbits) { + for (int k = 0; k < nbits; ++k) { + const int bit = pos + k; + if (bit >= 128) break; + if ((v >> k) & 1) block[bit >> 3] |= static_cast(1u << (bit & 7)); + } +} + +// The four BC1-style colour index bytes from the quadrant symbols of pixels TL, TR, BL, BR. +void put_quadrants(std::uint8_t* at, int qa, int qb, int qc, int qd) { + at[0] = static_cast((qa & 15) | ((qb & 15) << 4)); + at[1] = static_cast((qa >> 4) | ((qb >> 4) << 4)); + at[2] = static_cast((qc & 15) | ((qd & 15) << 4)); + at[3] = static_cast((qc >> 4) | ((qd >> 4) << 4)); +} + +std::string sorted_list(const std::set& items) { // Python's repr of sorted(set_of_ints) + std::string out = "["; + for (int v : items) { + if (out.size() > 1) out += ", "; + out += str(v); + } + return out + "]"; +} + +// --- BC1 / BC3 / BC4 / BC5 ------------------------------------------------------------------------------------------ + +void bc1_blocks(const Lanes& lanes, std::size_t nblocks, Labels* provisional, std::uint8_t* out) { + if (any_nonzero(lanes[1])) fail("BC1 lane 1 is non-zero (unexpected)"); + constexpr int mods[6] = {32, 64, 32, 32, 64, 32}; + std::vector> hist(nblocks); + const auto& l0 = lanes[0]; + for (std::size_t bi = 0; bi < nblocks; ++bi) { + const long long i = static_cast(bi); + const int v = l0[bi]; + const int d = (v & 1) ? 256 - ((v + 1) >> 1) : (v >> 1); + std::array base{}; + if (d == 0) { + } else if (i - d >= 0) { + base = hist[i - d]; + } else { + const int entry = static_cast(256 + i - d); + const int state = bc1_history_state[entry]; + if (state == 1 || (state == 2 && provisional)) { + for (int k = 0; k < 6; ++k) base[k] = bc1_history[entry][k]; + if (state == 2) provisional->insert("BC1 history " + str(entry)); + } else { + refuse("BC1 block " + str(i) + " references " + (state == 2 ? "unconfirmed" : "unknown") + + " built-in history entry " + str(entry)); + } + } + std::array e{}; + for (int k = 0; k < 6; ++k) e[k] = pymod(base[k] + unzig(lanes[2 + k][bi]), mods[k]); + hist[bi] = e; + std::uint8_t* at = out + bi * 8; + put_u16(at, (e[0] << 11) | (e[1] << 5) | e[2]); + put_u16(at + 2, (e[3] << 11) | (e[4] << 5) | e[5]); + put_quadrants(at + 4, bc1_quadrant[lanes[8][bi]], bc1_quadrant[lanes[9][bi]], bc1_quadrant[lanes[10][bi]], + bc1_quadrant[lanes[11][bi]]); + } +} + +// 48 bits of 3-bit indices from four rows of (left, right) pair symbols. +std::uint64_t pair_rows(const std::uint8_t (*pairs)[2], const std::uint16_t* const rows_a[4], + const std::uint16_t* const rows_b[4]) { + std::uint64_t bits = 0; + for (int r = 0; r < 4; ++r) { + const auto& left = pairs[*rows_a[r]]; + const auto& right = pairs[*rows_b[r]]; + bits |= static_cast(left[0] | (left[1] << 3) | (right[0] << 6) | (right[1] << 9)) << (12 * r); + } + return bits; +} + +void bc3_blocks(const Lanes& lanes, std::size_t nblocks, Labels* provisional, std::uint8_t* out) { + if (any_nonzero(lanes[1])) fail("BC3 lane 1 is non-zero (unexpected)"); + constexpr int mods[8] = {256, 256, 32, 64, 32, 32, 64, 32}; + constexpr std::array none = {128, 128, 0, 0, 0, 0, 0, 0}; + std::vector> hist(nblocks); + std::vector lost(nblocks, 0); + std::set unknown; + const auto &l0 = lanes[0], &alpha = lanes[2], &lr = lanes[7], &lg = lanes[8], &lb = lanes[9]; + const auto &top = lanes[10], &bottom = lanes[11]; + for (std::size_t bi = 0; bi < nblocks; ++bi) { + const long long i = static_cast(bi); + const std::size_t a = 2 * bi, b = 2 * bi + 1; + const int d = unzig(inter8(l0[a], l0[b])) & 255; + std::array base = none; + if (d == 0) { + } else if (i - d >= 0) { + base = hist[i - d]; + lost[bi] = lost[i - d]; + } else { + const int entry = static_cast(256 + i - d); + const int state = bc3_history_state[entry]; + if (state == 1) { + for (int k = 0; k < 8; ++k) base[k] = bc3_history[entry][k]; + } else if (state == 2) { + if (!provisional) + refuse("BC3 block " + str(i) + " references unconfirmed built-in history entry " + str(entry)); + for (int k = 0; k < 8; ++k) base[k] = bc3_history[entry][k]; + provisional->insert("BC3 history " + str(entry)); + } else { + if (!provisional) + refuse("BC3 block " + str(i) + " references unknown built-in history entry " + str(entry)); + lost[bi] = 1; + unknown.insert(entry); + } + } + const int dz[8] = {alpha[a], alpha[b], lr[a], lg[a], lb[a], lr[b], lg[b], lb[b]}; + std::array e{}; + for (int k = 0; k < 8; ++k) e[k] = pymod(base[k] + unzig(dz[k]), mods[k]); + hist[bi] = e; + const std::uint16_t* rows_a[4] = {&lanes[3][a], &lanes[4][a], &lanes[5][a], &lanes[6][a]}; + const std::uint16_t* rows_b[4] = {&lanes[3][b], &lanes[4][b], &lanes[5][b], &lanes[6][b]}; + std::uint8_t* at = out + bi * 16; + at[0] = static_cast(e[0]); + at[1] = static_cast(e[1]); + put_le(at + 2, pair_rows(bc3_alpha_pair, rows_a, rows_b), 6); + put_u16(at + 8, (e[2] << 11) | (e[3] << 5) | e[4]); + put_u16(at + 10, (e[5] << 11) | (e[6] << 5) | e[7]); + put_quadrants(at + 12, bc3_colour_quadrant[top[a]], bc3_colour_quadrant[top[b]], + bc3_colour_quadrant[bottom[a]], bc3_colour_quadrant[bottom[b]]); + } + if (!unknown.empty()) { + const auto total = static_cast(std::count(lost.begin(), lost.end(), 1)); + if (total > loose_limit(nblocks)) + refuse("BC3 chunk has " + str(static_cast(total)) + "/" + str(static_cast(nblocks)) + + " blocks from unknown built-in history entries"); + provisional->insert("BC3 unknown history " + sorted_list(unknown) + " (" + + str(static_cast(total)) + " blocks)"); + } +} + +void bc4_blocks(const Lanes& lanes, std::size_t nblocks, Labels* provisional, std::uint8_t* out) { + if (any_nonzero(lanes[1])) fail("BC4 lane 1 is non-zero (unexpected)"); + std::vector> hist(nblocks); + const auto &l0 = lanes[0], &l2 = lanes[2], &l3 = lanes[3]; + for (std::size_t bi = 0; bi < nblocks; ++bi) { + const long long i = static_cast(bi); + const int v = l0[bi]; + const int d = (v & 1) ? 256 - ((v + 1) >> 1) : (v >> 1); + std::array base = {128, 128}; + if (d == 0) { + } else if (i - d >= 0) { + base = hist[i - d]; + } else { + const int entry = static_cast(256 + i - d); + const int state = bc4_history_state[entry]; + if (state == 1 || (state == 2 && provisional)) { + base = {bc4_history[entry][0], bc4_history[entry][1]}; + if (state == 2) provisional->insert("BC4 history " + str(entry)); + } else { + refuse("BC4 block " + str(i) + " references " + (state == 2 ? "unconfirmed" : "unknown") + + " built-in history entry " + str(entry)); + } + } + const int a0 = (base[0] + unzig(l2[bi])) & 255; + const int a1 = (base[1] + unzig(l3[bi])) & 255; + hist[bi] = {a0, a1}; + const std::uint16_t* rows_a[4] = {&lanes[4][bi], &lanes[6][bi], &lanes[8][bi], &lanes[10][bi]}; + const std::uint16_t* rows_b[4] = {&lanes[5][bi], &lanes[7][bi], &lanes[9][bi], &lanes[11][bi]}; + std::uint8_t* at = out + bi * 8; + at[0] = static_cast(a0); + at[1] = static_cast(a1); + put_le(at + 2, pair_rows(bc4_pair, rows_a, rows_b), 6); + } +} + +void bc5_blocks(const Lanes& lanes, std::size_t nblocks, Labels* provisional, std::uint8_t* out) { + if (any_nonzero(lanes[1])) fail("BC5 lane 1 is non-zero (unexpected)"); + constexpr std::array none = {128, 128, 128, 128}; + std::vector> hist(nblocks); + std::vector lost(nblocks, 0); + std::set unknown; + const auto &l0 = lanes[0], &red = lanes[2], &green = lanes[3]; + for (std::size_t bi = 0; bi < nblocks; ++bi) { + const long long i = static_cast(bi); + const std::size_t a = 2 * bi, b = 2 * bi + 1; + const int d = unzig(inter8(l0[a], l0[b])) & 255; + std::array base = none; + if (d == 0) { + } else if (i - d >= 0) { + base = hist[i - d]; + lost[bi] = lost[i - d]; + } else { + const int entry = static_cast(256 + i - d); + const int state = bc5_history_state[entry]; + if (state == 1) { + for (int k = 0; k < 4; ++k) base[k] = bc5_history[entry][k]; + } else if (state == 2) { + if (!provisional) + refuse("BC5 block " + str(i) + " references unconfirmed built-in history entry " + str(entry)); + for (int k = 0; k < 4; ++k) base[k] = bc5_history[entry][k]; + provisional->insert("BC5 history " + str(entry)); + } else { + if (!provisional) + refuse("BC5 block " + str(i) + " references unknown built-in history entry " + str(entry)); + lost[bi] = 1; + unknown.insert(entry); + } + } + const std::array e = {(base[0] + unzig(red[a])) & 255, (base[1] + unzig(red[b])) & 255, + (base[2] + unzig(green[a])) & 255, (base[3] + unzig(green[b])) & 255}; + hist[bi] = e; + std::uint8_t* at = out + bi * 16; + for (int ch = 0; ch < 2; ++ch) { + const std::uint16_t* rows_a[4]; + const std::uint16_t* rows_b[4]; + for (int r = 0; r < 4; ++r) { + rows_a[r] = &lanes[4 + 4 * ch + r][a]; + rows_b[r] = &lanes[4 + 4 * ch + r][b]; + } + at[8 * ch] = static_cast(e[2 * ch]); + at[8 * ch + 1] = static_cast(e[2 * ch + 1]); + put_le(at + 8 * ch + 2, pair_rows(bc5_pair, rows_a, rows_b), 6); + } + } + if (!unknown.empty()) { + const auto total = static_cast(std::count(lost.begin(), lost.end(), 1)); + if (total > loose_limit(nblocks)) + refuse("BC5 chunk has " + str(static_cast(total)) + "/" + str(static_cast(nblocks)) + + " blocks from unknown built-in history entries"); + provisional->insert("BC5 unknown history " + sorted_list(unknown) + " (" + + str(static_cast(total)) + " blocks)"); + } +} + +// --- BC6H ----------------------------------------------------------------------------------------------------------- + +// BC6H initial history as bcpack_tables_bptc.load_bc6h(None, with_confirmed_bits=True) returns it: all unknown. +struct Bc6hHistory { + std::uint16_t endpoints[256][3][4] = {}; + std::uint8_t indices[256][16] = {}; + std::uint8_t endpoint_class[256] = {}; + std::uint8_t index_class[256] = {}; + std::uint8_t endpoint_confirmed_bits[256][3][4] = {}; + std::uint8_t index_confirmed_bits[256][16] = {}; +}; + +const Bc6hHistory& builtin_bc6h_history() { + static const Bc6hHistory empty{}; + return empty; +} + +constexpr const char* history_hint = " (pass --history )"; + +// Map BCPack's base endpoint `w` into the D3D11/Khronos BC6H bit order (bcpack._bc6h_stream_w). +int bc6h_stream_w(int mode, int w) { + if (mode == 13) return (w & 0x3FF) | (((w >> 10) & 1) << 11) | (((w >> 11) & 1) << 10); + if (mode == 14) { + int out = w & 0x3FF; + for (int b = 10; b < 16; ++b) out |= ((w >> b) & 1) << (25 - b); + return out; + } + return w; +} + +// First-reference order and per-entry counts of the built-in history entries a chunk's blocks resolve to +// (the insertion-ordered `ends` / `idxs` dicts of bcpack._bc6h_blocks). +struct RootCounts { + std::array count{}; + std::vector order; + void add(int root) { + if (count[root]++ == 0) order.push_back(root); + } +}; + +void bc6h_blocks(const Lanes& lanes, std::size_t nblocks, Labels* provisional, std::uint8_t* out) { + if (lanes[0].size() != 2 * nblocks) fail("BC6H lane length does not match the block count"); + const Bc6hHistory& hist = builtin_bc6h_history(); + using Endpoints = std::array, 3>; + const Endpoints none_e = {{{0x8000, 0x200, 0x20, 0x20}, {0x8000, 0x200, 0x20, 0x20}, {0x8000, 0x200, 0x20, 0x20}}}; + std::array none_i; + none_i.fill(8); + std::vector st_e(nblocks); + std::vector> st_i(nblocks); + std::vector eroot(nblocks, -1), iroot(nblocks, -1); + RootCounts ends, idxs; + std::array weak_e{}, weak_i{}; + std::size_t violations = 0; + const auto &l0 = lanes[0], &l1 = lanes[1], &l15 = lanes[15]; + + for (std::size_t bi = 0; bi < nblocks; ++bi) { + const long long i = static_cast(bi); + const std::size_t a = 2 * bi, b = 2 * bi + 1; + const int m_sym = l15[a]; + if (m_sym > 13) fail("BC6H block " + str(i) + " mode symbol " + str(m_sym) + " outside 0..13"); + const int mode = m_sym + 1; + const auto& info = bc6h_mode_info[mode]; + const int code = info[0], trans = info[1], base = info[2], reg = info[6], header = info[7]; + const int deltas[3] = {info[3], info[4], info[5]}; + const int part = l15[b] < 31 ? l15[b] : 31; + + const int di = inter8(l1[a], l1[b]); + const int de = (di + unzig(inter8(l0[a], l0[b]))) & 255; + Endpoints eref; + if (de == 0) { + eref = none_e; + } else if (i >= de) { + eref = st_e[i - de]; + eroot[bi] = eroot[i - de]; + } else { + const int entry = static_cast(256 + i - de); + for (int c = 0; c < 3; ++c) + for (int k = 0; k < 4; ++k) eref[c][k] = hist.endpoints[entry][c][k]; + eroot[bi] = entry; + // Only direct initial-history reads consume the calibrated bits. + for (int c = 0; c < 3; ++c) { + const int dc = deltas[c]; + const int need[4] = {base, dc, reg == 2 ? dc : 0, reg == 2 ? dc : 0}; + for (int qi = 0; qi < 4; ++qi) + if (hist.endpoint_confirmed_bits[entry][c][qi] < need[qi]) weak_e[entry] = true; + } + } + if (eroot[bi] >= 0) ends.add(eroot[bi]); + + std::array iref; + if (di == 0) { + iref = none_i; + } else if (i >= di) { + iref = st_i[i - di]; + iroot[bi] = iroot[i - di]; + } else { + const int entry = static_cast(256 + i - di); + for (int p = 0; p < 16; ++p) iref[p] = hist.indices[entry][p]; + iroot[bi] = entry; + for (int p = 0; p < 16; ++p) + if (hist.index_confirmed_bits[entry][p] < (reg == 2 ? 3 : 4)) weak_i[entry] = true; + } + if (iroot[bi] >= 0) idxs.add(iroot[bi]); + + const int mw = (1 << base) - 1; + const int w_shift = 16 - base; + std::array, 3> f_block{}; + Endpoints st_block{}; + for (int c = 0; c < 3; ++c) { + const int dc = deltas[c]; + const int mD = (1 << dc) - 1; + const int sh = dc < 6 ? 6 - dc : 0; + const int dz0 = unzig(inter(lanes[2 + c][a], lanes[2 + c][b], 8)); + const int dz1 = unzig(inter(lanes[5 + c][a], lanes[5 + c][b], 5)); + const int dz2 = unzig(lanes[8 + c][a]); + const int dz3 = unzig(lanes[8 + c][b]); + const auto& r = eref[c]; + const int w = ((r[0] >> w_shift) + dz0) & mw; + const int x = ((r[1] >> (10 - dc)) + dz1) & mD; + const int y = ((r[2] >> sh) + dz2) & mD; + const int z = ((r[3] >> sh) + dz3) & mD; + const int x10 = x << (10 - dc); + if (reg == 2) { + st_block[c] = {w << w_shift, x10, y << sh, z << sh}; + f_block[c] = {bc6h_stream_w(mode, w), x, y, z}; + } else { + const int v = (w & mD) ^ (trans == 1 ? (1 << (dc - 1)) : 0); + const int low_y = dc >= 6 ? (v >> (dc - 6)) : (v << sh); + st_block[c] = {w << w_shift, x10, low_y, x10 >> 4}; + f_block[c] = {bc6h_stream_w(mode, w), x, 0, 0}; + } + } + st_e[bi] = st_block; + + const int ibits = reg == 2 ? 3 : 4; + const int imask = (1 << ibits) - 1; + const int ishift = 4 - ibits; + std::array ix_block{}, si_block{}; + for (int yy = 0; yy < 4; ++yy) { + const int sa = lanes[11 + yy][a]; + const int sb = lanes[11 + yy][b]; + int dv[4]; + if (yy < 3) { + dv[0] = deint[1][sa], dv[1] = deint[0][sa], dv[2] = deint[1][sb], dv[3] = deint[0][sb]; + } else { + dv[0] = deint[0][sa], dv[1] = deint[1][sa], dv[2] = deint[0][sb], dv[3] = deint[1][sb]; + } + for (int off = 0; off < 4; ++off) { + const int p = yy * 4 + off; + const int ix = ((iref[p] >> ishift) + unzig(dv[off])) & imask; + ix_block[p] = ix; + si_block[p] = ix << ishift; + } + } + st_i[bi] = si_block; + + const int second = reg == 2 ? bc7_anchor2[part] : -1; + if (ix_block[0] & (reg == 2 ? 4 : 8)) ++violations; + if (reg == 2 && (ix_block[second] & 4)) ++violations; + + std::uint8_t* blk = out + 16 * bi; + std::memset(blk, 0, 16); + put_bits(blk, 0, static_cast(code), 5); + for (int k = 0; k < bc6h_plan_count[mode]; ++k) { + const auto& item = bc6h_plan[mode][k]; + const int c = item[0], qi = item[1], bit = item[2], pos = item[3]; + const int value = c < 0 ? part : f_block[c][qi]; + if ((value >> bit) & 1) put_bits(blk, pos, 1, 1); + } + int at = header; + for (int p = 0; p < 16; ++p) { + const int nb = (p == 0 || p == second) ? ibits - 1 : ibits; + put_bits(blk, at, static_cast(ix_block[p] & ((1 << nb) - 1)), nb); + at += nb; + } + } + + if (!provisional) { + std::vector unconf_e, unconf_i; + for (int k : ends.order) + if (hist.endpoint_class[k] != 3 || weak_e[k]) unconf_e.push_back(k); + for (int k : idxs.order) + if (hist.index_class[k] != 3 || weak_i[k]) unconf_i.push_back(k); + if (!unconf_e.empty() || !unconf_i.empty()) { + const bool endpoint = !unconf_e.empty(); + const int k = endpoint ? unconf_e[0] : unconf_i[0]; + const int cls = endpoint ? hist.endpoint_class[k] : hist.index_class[k]; + refuse(std::string("BC6H chunk references ") + (cls == 0 ? "unknown" : "unconfirmed") + " built-in " + + (endpoint ? "endpoint" : "index") + " history entry " + str(k) + history_hint); + } + if (violations) + fail("BC6H chunk has " + str(static_cast(violations)) + + " anchor pixel(s) with an inconsistent index"); + return; + } + + std::array unknown_e{}, unknown_i{}; + std::set missing_set; + for (int k : ends.order) + if (hist.endpoint_class[k] == 0) unknown_e[k] = true, missing_set.insert("endpoint " + str(k)); + for (int k : idxs.order) + if (hist.index_class[k] == 0) unknown_i[k] = true, missing_set.insert("index " + str(k)); + std::size_t lost = 0; + for (std::size_t bi = 0; bi < nblocks; ++bi) + if ((eroot[bi] >= 0 && unknown_e[eroot[bi]]) || (iroot[bi] >= 0 && unknown_i[iroot[bi]])) ++lost; + const std::vector missing(missing_set.begin(), missing_set.end()); + if (lost > loose_limit(nblocks)) + refuse("BC6H chunk has " + str(static_cast(lost)) + "/" + str(static_cast(nblocks)) + + " blocks from unknown built-in history entries (" + join(missing, 0, 6) + + (missing.size() > 6 ? ", ..." : "") + ")" + history_hint); + if (violations > loose_limit(nblocks)) + refuse("BC6H chunk has " + str(static_cast(violations)) + + " anchor pixels with an inconsistent index"); + if (!missing.empty()) + provisional->insert("BC6H unknown history " + join(missing, 0, missing.size()) + " (" + + str(static_cast(lost)) + " blocks)"); + if (violations) + provisional->insert("BC6H anchor index inconsistent in " + str(static_cast(violations)) + " pixels"); + for (int k = 0; k < 256; ++k) { + if (weak_e[k] && hist.endpoint_class[k] == 3) + provisional->insert("BC6H endpoint history " + str(k) + " (unconfirmed bits)"); + if (weak_i[k] && hist.index_class[k] == 3) + provisional->insert("BC6H index history " + str(k) + " (unconfirmed bits)"); + } + const std::pair kinds[2] = {{"endpoint", &ends}, {"index", &idxs}}; + for (int kind = 0; kind < 2; ++kind) { + const std::uint8_t* cls = kind == 0 ? hist.endpoint_class : hist.index_class; + std::vector keys = kinds[kind].second->order; + std::sort(keys.begin(), keys.end()); + std::vector fitted, rough; + for (int k : keys) { + if (cls[k] == 1) fitted.push_back(str(k)); + if (cls[k] == 2) rough.push_back(str(k)); + } + const std::string name = std::string("BC6H ") + kinds[kind].first + " history "; + if (!fitted.empty()) provisional->insert(name + join(fitted, 0, fitted.size())); + if (!rough.empty()) provisional->insert(name + join(rough, 0, rough.size()) + " (last-resort)"); + } +} + +// --- BC7 ------------------------------------------------------------------------------------------------------------ + +constexpr int b7_ns[8] = {3, 2, 3, 2, 1, 1, 1, 2}; +constexpr int b7_cb[8] = {4, 6, 5, 7, 5, 7, 7, 5}; +constexpr int b7_ab[8] = {0, 0, 0, 0, 6, 8, 7, 5}; +constexpr int b7_pk[8] = {1, 2, 0, 1, 0, 0, 1, 1}; // 0 = none, 1 = per-endpoint p-bit, 2 = shared per subset +constexpr int b7_ib[8] = {3, 3, 2, 2, 3, 2, 4, 2}; // index bits of the primary index set (lanes 11..14) +constexpr int b7_pb[8] = {4, 6, 6, 6, 0, 0, 0, 6}; // partition field bits +constexpr int b7_lane[3][4] = {{2, 3, 4, 5}, {6, 7, 8, 9}, {10, 5, 9, -1}}; +constexpr int b7_anchor = 8; +constexpr int b7_pbit = 16; +constexpr int b7_signature = 32; +constexpr int b7_min_depth = 4; + +// BC7 initial history as bcpack_tables_bptc.load_bc7(None) returns it: depth 0 (unknown) everywhere. +struct Bc7History { + std::uint8_t endpoints[256][24] = {}; + std::uint8_t endpoint_bits[256][24] = {}; + std::uint8_t endpoint_confirmed_bits[256][24] = {}; + std::uint8_t indices[256][32] = {}; + std::uint8_t index_bits[256][32] = {}; + std::uint8_t index_confirmed_bits[256][32] = {}; + std::set> last_resort; // (entry, slot) +}; + +const Bc7History& builtin_bc7_history() { + static const Bc7History empty{}; + return empty; +} + +int bc7_rank(int a, int b) { return (a < 32 && b < 16) ? bc7_rank_of[a * 16 + b] : -1; } + +bool bc7_inferred_rank(int rank) { + return std::find(std::begin(bc7_inferred), std::end(bc7_inferred), rank) != std::end(bc7_inferred); +} + +// Leading bits read from a stored endpoint byte for channels (R, G, B, A). +std::array bc7_field_bits(int mode, int rot) { + std::array w = {b7_cb[mode], b7_cb[mode], b7_cb[mode], b7_ab[mode]}; + if (rot) w[rot - 1] = b7_ab[mode], w[3] = b7_cb[mode]; + const int pk = b7_pk[mode]; + std::array out{}; + for (int c = 0; c < 4; ++c) out[c] = w[c] ? w[c] + ((pk && c == 0) ? 1 : 0) : 0; + return out; +} + +int bc7_delta_e(const Lanes& lanes, std::size_t i, int di) { + return (di + unzig(inter8(lanes[0][2 * i], lanes[0][2 * i + 1]))) & 255; +} + +int bc7_delta_i(const Lanes& lanes, std::size_t i) { return inter8(lanes[1][2 * i], lanes[1][2 * i + 1]); } + +// Signature and built-in history coverage (bcpack._bc7_check). Returns the (kind, entry) pairs, kind 0 = +// endpoint and 1 = index, too shallow to use, in provisional mode. +std::vector> bc7_check(const Lanes& lanes, std::size_t nblocks, const Bc7History& hist, + Labels* provisional) { + const auto& l15 = lanes[15]; + std::set ranks; + for (std::size_t i = 0; i < nblocks; ++i) { + const int a = l15[2 * i], b = l15[2 * i + 1]; + const int rank = bc7_rank(a, b); + if (rank < 0 || bc7_config[rank][0] > 7 || bc7_config[rank][1] == 255) + fail("BC7 signature (" + str(a) + ", " + str(b) + ") is outside the signature table"); + ranks.insert(rank); + } + for (int rank : ranks) { + if (!bc7_inferred_rank(rank)) continue; + if (!provisional) refuse("BC7 signature rank " + str(rank) + " is inferred-only (unconfirmed)"); + provisional->insert("BC7 signature " + str(rank)); + } + + std::map, 3>> need_e; + std::map> need_i; + for (std::size_t bi = 0; bi < std::min(nblocks, 256); ++bi) { + const int i = static_cast(bi); + const int rank = bc7_rank_of[l15[2 * bi] * 16 + l15[2 * bi + 1]]; + const int mode = bc7_config[rank][0], rot = bc7_config[rank][2]; + const int di = bc7_delta_i(lanes, bi); + const int de = bc7_delta_e(lanes, bi, di); + if (de > i) { + const auto bits = bc7_field_bits(mode, rot); + auto& row = need_e[256 + i - de]; + for (int sub = 0; sub < b7_ns[mode]; ++sub) + for (int c = 0; c < 4; ++c) row[sub][c] = std::max(row[sub][c], bits[c]); + } + if (di > i) { + auto& row = need_i[256 + i - di]; + row[0] = std::max(row[0], b7_ib[mode]); + if (mode == 4 || mode == 5) row[1] = 2; + } + } + + std::vector> unknown; + for (int kind = 0; kind < 2; ++kind) { + const char* kinds = kind == 0 ? "endpoint" : "index"; + std::vector>>> needs; // entry -> (slot, bits) + if (kind == 0) { + for (const auto& [k, want] : need_e) { + std::vector> slots; + for (int sub = 0; sub < 3; ++sub) + for (int e = 0; e < 2; ++e) + for (int c = 0; c < 4; ++c) + if (want[sub][c]) slots.emplace_back(sub * 8 + e * 4 + c, want[sub][c]); + needs.emplace_back(k, std::move(slots)); + } + } else { + for (const auto& [k, want] : need_i) { + std::vector> slots; + for (int st = 0; st < 2; ++st) + for (int p = 0; p < 16; ++p) + if (want[st]) slots.emplace_back(st * 16 + p, want[st]); + needs.emplace_back(k, std::move(slots)); + } + } + for (const auto& [k, slots] : needs) { + const std::uint8_t* bk = kind == 0 ? hist.endpoint_bits[k] : hist.index_bits[k]; + const std::uint8_t* ck = kind == 0 ? hist.endpoint_confirmed_bits[k] : hist.index_confirmed_bits[k]; + bool weak = false, missing = false; + int shallow = 8; + for (const auto& [j, b] : slots) { + if (bk[j] < b) { + missing = true; + shallow = std::min(shallow, bk[j]); + } + if (ck[j] < b) weak = true; + } + if (missing) { + if (!provisional) + refuse(std::string("BC7 chunk references unknown built-in ") + kinds + " history entry " + str(k) + + history_hint); + if (kind == 0 && shallow >= b7_min_depth) + provisional->insert("BC7 endpoint history " + str(k) + " (top " + str(shallow) + " bits known)"); + else + unknown.emplace_back(kind, k); + } else if (weak) { + if (!provisional) + refuse(std::string("BC7 chunk references unconfirmed built-in ") + kinds + " history entry " + + str(k)); + provisional->insert(std::string("BC7 ") + kinds + " history " + str(k)); + } + if (kind == 0 && provisional) { + for (const auto& [j, b] : slots) { + if (hist.last_resort.count({k, j})) { + provisional->insert("BC7 endpoint history " + str(k) + " (last-resort)"); + break; + } + } + } + } + } + return unknown; +} + +// Rebuilds the 16-byte BC7 blocks and per-block consistency status flags (bcpack._bc7_rebuild). +std::vector bc7_rebuild(const Lanes& lanes, std::size_t nblocks, const Bc7History& hist, std::uint8_t* out) { + std::vector status(nblocks, 0); + std::vector> eps(nblocks); + std::vector> ixs(nblocks); + const std::array zero_e{}; + const std::array zero_i{}; + const std::vector* rows1[4] = {&lanes[11], &lanes[12], &lanes[13], &lanes[14]}; + const std::vector* rows2[4] = {&lanes[6], &lanes[7], &lanes[8], &lanes[10]}; + const auto& l15 = lanes[15]; + + for (std::size_t bi = 0; bi < nblocks; ++bi) { + const long long i = static_cast(bi); + const std::size_t a = 2 * bi, b = 2 * bi + 1; + const int rank = bc7_rank(l15[a], l15[b]); + if (rank < 0 || bc7_config[rank][0] > 7) { + status[bi] = b7_signature; + eps[bi] = zero_e; + ixs[bi] = zero_i; + continue; + } + const int mode = bc7_config[rank][0], part = bc7_config[rank][1], rot = bc7_config[rank][2], + isel = bc7_config[rank][3]; + int st = 0; + const int di = bc7_delta_i(lanes, bi); + const int de = bc7_delta_e(lanes, bi, di); + std::array rep = zero_e; + if (de != 0) { + if (i >= de) + rep = eps[i - de]; + else + for (int k = 0; k < 24; ++k) rep[k] = hist.endpoints[256 + i - de][k]; + } + std::array rix = zero_i; + if (di != 0) { + if (i >= di) + rix = ixs[i - di]; + else + for (int k = 0; k < 32; ++k) rix[k] = hist.indices[256 + i - di][k]; + } + + const int ns = b7_ns[mode], cb = b7_cb[mode], ab = b7_ab[mode], pk = b7_pk[mode]; + int w[4] = {cb, cb, cb, ab}; + if (rot) w[rot - 1] = ab, w[3] = cb; + + int val[3][2][4] = {}; + std::array sep{}; + int pb[3][2] = {}; + for (int sub = 0; sub < ns; ++sub) { + const int base_sub = sub * 8; + for (int c = 0; c < 4; ++c) { + const int lane_idx = b7_lane[sub][c]; + const int wc = w[c]; + if (!wc || lane_idx < 0) continue; + const int bits = wc + ((pk && c == 0) ? 1 : 0); + const int shift = 8 - bits; + const int mask = (1 << bits) - 1; + const auto& ln = lanes[lane_idx]; + const int v0 = ((rep[base_sub + c] >> shift) + unzig(ln[a])) & mask; + const int v1 = ((rep[base_sub + 4 + c] >> shift) + unzig(ln[b])) & mask; + val[sub][0][c] = v0; + val[sub][1][c] = v1; + sep[base_sub + c] = v0 << shift; + sep[base_sub + 4 + c] = v1 << shift; + } + if (pk == 1) { + pb[sub][0] = val[sub][0][0] & 1; + pb[sub][1] = val[sub][1][0] & 1; + } else if (pk == 2) { + const int p0 = val[sub][0][0] & 1; + pb[sub][0] = p0; + pb[sub][1] = p0; + if ((val[sub][1][0] & 1) != p0) st |= b7_pbit; + } + } + for (int sub = ns; sub < 3; ++sub) + for (int k = 0; k < 8; ++k) sep[sub * 8 + k] = sep[k]; + eps[bi] = sep; + + const int b1 = b7_ib[mode]; + const int b1_shift = 4 - b1; + const int b1_mask = (1 << b1) - 1; + const bool two = mode == 4 || mode == 5; + std::array s1{}, s2{}; + std::array six{}; + for (int y = 0; y < 4; ++y) { + const int sa = (*rows1[y])[a], sb = (*rows1[y])[b]; + int dv[4]; + if (y < 3) + dv[0] = deint[1][sa], dv[1] = deint[0][sa], dv[2] = deint[1][sb], dv[3] = deint[0][sb]; + else + dv[0] = deint[0][sa], dv[1] = deint[1][sa], dv[2] = deint[0][sb], dv[3] = deint[1][sb]; + for (int off = 0; off < 4; ++off) { + const int p = y * 4 + off; + const int iv = ((rix[p] >> b1_shift) + unzig(dv[off])) & b1_mask; + s1[p] = iv; + six[p] = iv << b1_shift; + } + if (two) { + const int s2a = (*rows2[y])[a], s2b = (*rows2[y])[b]; + int ev[4]; + if (y < 3) + ev[0] = deint[1][s2a], ev[1] = deint[0][s2a], ev[2] = deint[1][s2b], ev[3] = deint[0][s2b]; + else + ev[0] = deint[0][s2a], ev[1] = deint[1][s2a], ev[2] = deint[0][s2b], ev[3] = deint[1][s2b]; + for (int off = 0; off < 4; ++off) { + const int p = y * 4 + off; + const int iv2 = ((rix[16 + p] >> 2) + unzig(ev[off])) & 3; + s2[p] = iv2; + six[16 + p] = iv2 << 2; + } + } + } + ixs[bi] = six; + + auto all_zero = [](const auto& arr) { return std::all_of(arr.begin(), arr.end(), [](int v) { return !v; }); }; + if (mode == 0 && part == 0 && all_zero(sep) && all_zero(s1)) { + status[bi] = st; + continue; + } + + // Subset of each pixel and the anchor pixel of each subset. + int sm[16] = {}; + if (ns == 2) + for (int p = 0; p < 16; ++p) sm[p] = (bc7_part2_packed[part] >> p) & 1; + else if (ns == 3) + for (int p = 0; p < 16; ++p) sm[p] = (bc7_part3_packed[part] >> (2 * p)) & 3; + const int anchors[3] = {0, ns == 2 ? bc7_anchor2[part] : (ns == 3 ? bc7_anchor3a[part] : 0), + ns == 3 ? bc7_anchor3b[part] : 0}; + const std::array* pri = &s1; + const std::array* sec = nullptr; + int ib = b1, ib2 = 0; + if (mode == 4) { + pri = &s2, ib = 2, sec = &s1, ib2 = 3; + } else if (mode == 5) { + pri = &s1, ib = 2, sec = &s2, ib2 = 2; + } + + std::uint8_t* blk = out + 16 * bi; + std::memset(blk, 0, 16); + put_bits(blk, mode, 1, 1); + int pos = mode + 1; + const int pbits = b7_pb[mode]; + if (pbits) { + put_bits(blk, pos, static_cast(part & ((1 << pbits) - 1)), pbits); + pos += pbits; + } + if (mode == 4 || mode == 5) { + put_bits(blk, pos, static_cast(rot & 3), 2); + pos += 2; + } + if (mode == 4) { + put_bits(blk, pos, static_cast(isel & 1), 1); + pos += 1; + } + const int cb_mask = (1 << cb) - 1; + for (int c = 0; c < 3; ++c) { + const int tc = (rot && c == rot - 1) ? 3 : c; + const bool drop = pk && tc == 0; + for (int sub = 0; sub < ns; ++sub) { + for (int e = 0; e < 2; ++e) { + const int v = val[sub][e][tc]; + put_bits(blk, pos, static_cast((drop ? v >> 1 : v) & cb_mask), cb); + pos += cb; + } + } + } + if (ab) { + const int ab_mask = (1 << ab) - 1; + const int tc = rot ? rot - 1 : 3; + const bool drop = pk && tc == 0; + for (int sub = 0; sub < ns; ++sub) { + for (int e = 0; e < 2; ++e) { + const int v = val[sub][e][tc]; + put_bits(blk, pos, static_cast((drop ? v >> 1 : v) & ab_mask), ab); + pos += ab; + } + } + } + if (pk == 1) { + for (int sub = 0; sub < ns; ++sub) { + put_bits(blk, pos++, static_cast(pb[sub][0] & 1), 1); + put_bits(blk, pos++, static_cast(pb[sub][1] & 1), 1); + } + } else if (pk == 2) { + for (int sub = 0; sub < ns; ++sub) put_bits(blk, pos++, static_cast(pb[sub][0] & 1), 1); + } + + const int ib_mask = (1 << ib) - 1; + const int ib_amask = (1 << (ib - 1)) - 1; + for (int p = 0; p < 16; ++p) { + const bool anchor = p == anchors[ns > 1 ? sm[p] : 0]; + const int pv = (*pri)[p]; + if (anchor) { + if (pv >> (ib - 1)) st |= b7_anchor; + put_bits(blk, pos, static_cast(pv & ib_amask), ib - 1); + pos += ib - 1; + } else { + put_bits(blk, pos, static_cast(pv & ib_mask), ib); + pos += ib; + } + } + if (sec) { + const int ib2_mask = (1 << ib2) - 1; + const int ib2_amask = (1 << (ib2 - 1)) - 1; + for (int p = 0; p < 16; ++p) { + const int sv = (*sec)[p]; + if (p == 0) { + if (sv >> (ib2 - 1)) st |= b7_anchor; + put_bits(blk, pos, static_cast(sv & ib2_amask), ib2 - 1); + pos += ib2 - 1; + } else { + put_bits(blk, pos, static_cast(sv & ib2_mask), ib2); + pos += ib2; + } + } + } + if (pos != 128) st |= b7_signature; + status[bi] = st; + } + return status; +} + +void bc7_blocks(const Lanes& lanes, std::size_t nblocks, Labels* provisional, std::uint8_t* out) { + if (lanes[0].size() != 2 * nblocks) fail("BC7 lane length does not match the block count"); + const Bc7History& hist = builtin_bc7_history(); + const auto unknown = bc7_check(lanes, nblocks, hist, provisional); + if (!unknown.empty()) { + std::vector er(nblocks, 0), ir(nblocks, 0); + for (std::size_t bi = 0; bi < nblocks; ++bi) { + const long long i = static_cast(bi); + const int di = bc7_delta_i(lanes, bi); + const int de = bc7_delta_e(lanes, bi, di); + er[bi] = de == 0 ? -1 : (de > i ? static_cast(256 + i - de) : er[i - de]); + ir[bi] = di == 0 ? -1 : (di > i ? static_cast(256 + i - di) : ir[i - di]); + } + std::array unknown_e{}, unknown_i{}; + std::vector names; + std::vector labels; + for (const auto& [kind, k] : unknown) { + const auto& roots = kind == 0 ? er : ir; + const auto count = std::count(roots.begin(), roots.end(), k); + (kind == 0 ? unknown_e : unknown_i)[k] = true; + const std::string name = std::string(kind == 0 ? "endpoint " : "index ") + str(k); + names.push_back(name); + labels.push_back("BC7 unknown " + name.substr(0, name.find(' ')) + " history " + str(k) + " (" + + str(static_cast(count)) + " blocks)"); + } + std::size_t total = 0; + for (std::size_t bi = 0; bi < nblocks; ++bi) + if ((er[bi] >= 0 && unknown_e[er[bi]]) || (ir[bi] >= 0 && unknown_i[ir[bi]])) ++total; + if (total > loose_limit(nblocks)) + refuse("BC7 chunk has " + str(static_cast(total)) + "/" + str(static_cast(nblocks)) + + " blocks from unknown built-in history entries (" + join(names, 0, 6) + + (names.size() > 6 ? ", ..." : "") + ")" + history_hint); + provisional->insert(labels.begin(), labels.end()); + } + const auto status = bc7_rebuild(lanes, nblocks, hist, out); + std::size_t bad = 0, first = 0; + for (std::size_t bi = 0; bi < nblocks; ++bi) { + if (status[bi]) { + if (!bad) first = bi; + ++bad; + } + } + if (bad) { + const std::string where = str(static_cast(bad)) + "/" + str(static_cast(nblocks)); + if (!(status[first] & (b7_anchor | b7_pbit))) + fail("BC7 chunk failed signature check (" + where + " blocks; first: block " + + str(static_cast(first)) + ")"); + if (!provisional || provisional->empty()) + fail("BC7 chunk failed anchor/p-bit consistency check (" + where + " blocks; first: block " + + str(static_cast(first)) + ")"); + if (bad > loose_limit(nblocks)) + refuse("BC7 chunk failed anchor/p-bit consistency check in " + where + " blocks"); + provisional->insert("BC7 consistency check failed in " + str(static_cast(bad)) + " blocks"); + } +} + +} // namespace + +std::size_t block_bytes(int codec) { + switch (codec) { + case 1: + case 4: return 8; + case 3: + case 5: + case 6: + case 7: return 16; + default: return 0; + } +} + +Result decode_lanes(std::span raw, int codec, std::size_t out_size, + std::vector>& lanes, std::size_t* bytes_used) { + Result result; + try { + const std::size_t used = run_decode_lanes(raw, codec, out_size, lanes); + if (bytes_used) *bytes_used = used; + } catch (const Failure& f) { + result.status = f.status; + result.message = f.message; + } + return result; +} + +Result decode(std::span raw, int codec, std::span out, bool allow_provisional) { + Result result; + const char* name = codec_name(codec); + if (!name) { + result.status = Status::unsupported; + result.message = "BCPack block rebuilder for codec " + str(codec) + " (" + str(codec) + + ") is not implemented yet"; + return result; + } + try { + Lanes lanes; + run_decode_lanes(raw, codec, out.size(), lanes); + const std::size_t nblocks = out.size() / block_bytes(codec); + Labels labels; + Labels* provisional = allow_provisional ? &labels : nullptr; + std::fill(out.begin(), out.end(), std::uint8_t{0}); + switch (codec) { + case 1: bc1_blocks(lanes, nblocks, provisional, out.data()); break; + case 3: bc3_blocks(lanes, nblocks, provisional, out.data()); break; + case 4: bc4_blocks(lanes, nblocks, provisional, out.data()); break; + case 5: bc5_blocks(lanes, nblocks, provisional, out.data()); break; + case 6: bc6h_blocks(lanes, nblocks, provisional, out.data()); break; + case 7: bc7_blocks(lanes, nblocks, provisional, out.data()); break; + } + result.provisional.assign(labels.begin(), labels.end()); + } catch (const Failure& f) { + result.status = f.status; + result.message = f.message; + } + return result; +} + +} // namespace leonida::bcpack diff --git a/runtime/src/bcpack.h b/runtime/src/bcpack.h new file mode 100644 index 0000000000..0cdf581b8b --- /dev/null +++ b/runtime/src/bcpack.h @@ -0,0 +1,49 @@ +#pragma once + +// Native BCPack decoder: a dependency-free C++ port of tools/bcpack.py (RT-186). +// +// Decodes one Xbox Series DirectStorage BCPack chunk (the interleaved 12/16-lane rANS entropy layer, then the +// BC1/BC3/BC4/BC5/BC6H/BC7 block rebuilders) into standard BCn blocks. It gives the same bytes, the same refusals +// and the same provisional labels as `bcpack.decode_with_info(raw, codec, out_size, allow_provisional=...)` with +// `history=None`: the built-in BC1/BC3/BC4/BC5 history tables of tools/bcpack_tables.py, and no BC6H/BC7 initial +// history, so BC6H/BC7 chunks decode only when their delta chains stay inside the chunk. The tables come from +// runtime/src/bcpack_tables.inc, which tools/gen_bcpack_tables.py generates from the Python tables; +// tests/test_bcpack_native.py compares the two decoders on synthetic chunks. docs/textures.md has the format. + +#include +#include +#include +#include +#include + +namespace leonida::bcpack { + +// DSTORAGE_BCPACK_MODE: 1 = BC1, 3 = BC3, 4 = BC4, 5 = BC5, 6 = BC6H, 7 = BC7. +enum class Status : std::uint8_t { + ok = 0, + error = 1, // bcpack.BcpackError: the chunk is malformed + provisional = 2, // bcpack.BcpackProvisionalError: needs an unconfirmed or unknown built-in history entry + unsupported = 3, // bcpack.BcpackUnsupportedCodec: no block rebuilder for this codec id +}; + +struct Result { + Status status = Status::ok; + std::string message; // why the chunk was refused (empty when it decoded) + std::vector provisional; // with allow_provisional: what the output relies on, sorted + explicit operator bool() const { return status == Status::ok; } +}; + +// Bytes per BCn block of a codec id, 0 for an unknown one. +std::size_t block_bytes(int codec); + +// Decodes `raw` into `out`, whose size is the chunk's decoded size (a whole number of blocks). `out` is only +// meaningful when the result is ok. Strict by default: any reference to an unconfirmed or unknown built-in history +// entry is refused; `allow_provisional` accepts them and lists them in `Result::provisional`. +Result decode(std::span raw, int codec, std::span out, + bool allow_provisional = false); + +// The entropy layer alone (bcpack.decode_lanes): one symbol list per lane, and the input bytes it consumed. +Result decode_lanes(std::span raw, int codec, std::size_t out_size, + std::vector>& lanes, std::size_t* bytes_used = nullptr); + +} // namespace leonida::bcpack diff --git a/runtime/src/bcpack_tables.inc b/runtime/src/bcpack_tables.inc new file mode 100644 index 0000000000..2dda403505 --- /dev/null +++ b/runtime/src/bcpack_tables.inc @@ -0,0 +1,1496 @@ +// Generated by tools/gen_bcpack_tables.py from tools/bcpack_tables.py and tools/bcpack_tables_bptc.py. +// Do not edit by hand: rerun the generator (tests/test_bcpack_native.py checks it is current). + +// (symbol_width, q) -> per-band rANS frequencies; band p covers symbols [2^(p-1), 2^p). +inline constexpr std::uint16_t prob_freq[10][16][11] = { + { + {32768, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {38761, 26775, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {44484, 21052, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {49818, 15718, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {54623, 10913, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {58721, 6815, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {61911, 3625, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {64036, 1500, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {65121, 415, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {65477, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {65534, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {65535, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {65535, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {65535, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {65535, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {65535, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + }, + { + {16384, 16384, 16384, 0, 0, 0, 0, 0, 0, 0, 0}, + {21168, 17592, 13388, 0, 0, 0, 0, 0, 0, 0, 0}, + {26354, 18130, 10526, 0, 0, 0, 0, 0, 0, 0, 0}, + {31900, 17918, 7859, 0, 0, 0, 0, 0, 0, 0, 0}, + {37750, 16874, 5456, 0, 0, 0, 0, 0, 0, 0, 0}, + {43800, 14920, 3408, 0, 0, 0, 0, 0, 0, 0, 0}, + {49850, 12062, 1812, 0, 0, 0, 0, 0, 0, 0, 0}, + {55537, 8499, 750, 0, 0, 0, 0, 0, 0, 0, 0}, + {60308, 4812, 208, 0, 0, 0, 0, 0, 0, 0, 0}, + {63575, 1901, 30, 0, 0, 0, 0, 0, 0, 0, 0}, + {65132, 402, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {65511, 23, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {65533, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {65533, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {65533, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {65533, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + }, + { + {8192, 8192, 8192, 8192, 0, 0, 0, 0, 0, 0, 0}, + {11073, 10095, 8796, 6694, 0, 0, 0, 0, 0, 0, 0}, + {14406, 11948, 9065, 5263, 0, 0, 0, 0, 0, 0, 0}, + {18234, 13666, 8958, 3930, 0, 0, 0, 0, 0, 0, 0}, + {22624, 15126, 8437, 2728, 0, 0, 0, 0, 0, 0, 0}, + {27657, 16143, 7460, 1704, 0, 0, 0, 0, 0, 0, 0}, + {33414, 16436, 6031, 906, 0, 0, 0, 0, 0, 0, 0}, + {39920, 15616, 4250, 375, 0, 0, 0, 0, 0, 0, 0}, + {47024, 13284, 2406, 104, 0, 0, 0, 0, 0, 0, 0}, + {54201, 9375, 950, 15, 0, 0, 0, 0, 0, 0, 0}, + {60388, 4744, 200, 1, 0, 0, 0, 0, 0, 0, 0}, + {64250, 1262, 10, 1, 0, 0, 0, 0, 0, 0, 0}, + {65456, 74, 1, 1, 0, 0, 0, 0, 0, 0, 0}, + {65529, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, + {65529, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, + {65529, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, + }, + { + {4096, 4096, 4096, 4096, 4096, 0, 0, 0, 0, 0, 0}, + {5665, 5407, 5048, 4398, 3347, 0, 0, 0, 0, 0, 0}, + {7539, 6867, 5973, 4532, 2632, 0, 0, 0, 0, 0, 0}, + {9773, 8461, 6833, 4479, 1965, 0, 0, 0, 0, 0, 0}, + {12447, 10177, 7564, 4218, 1364, 0, 0, 0, 0, 0, 0}, + {15679, 11977, 8072, 3730, 852, 0, 0, 0, 0, 0, 0}, + {19640, 13774, 8217, 3016, 453, 0, 0, 0, 0, 0, 0}, + {24559, 15361, 7808, 2124, 188, 0, 0, 0, 0, 0, 0}, + {30704, 16320, 6642, 1203, 52, 0, 0, 0, 0, 0, 0}, + {38280, 15920, 4688, 476, 7, 0, 0, 0, 0, 0, 0}, + {47169, 13219, 2372, 99, 1, 0, 0, 0, 0, 0, 0}, + {56354, 7896, 631, 4, 1, 0, 0, 0, 0, 0, 0}, + {63248, 2208, 34, 1, 1, 0, 0, 0, 0, 0, 0}, + {65465, 57, 1, 1, 1, 0, 0, 0, 0, 0, 0}, + {65521, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, + {65521, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, + }, + { + {2048, 2048, 2048, 2048, 2048, 2048, 0, 0, 0, 0, 0}, + {2865, 2799, 2704, 2524, 2200, 1673, 0, 0, 0, 0, 0}, + {3858, 3682, 3434, 2986, 2266, 1316, 0, 0, 0, 0, 0}, + {5063, 4709, 4230, 3418, 2240, 982, 0, 0, 0, 0, 0}, + {6536, 5912, 5088, 3782, 2109, 682, 0, 0, 0, 0, 0}, + {8366, 7314, 5988, 4036, 1865, 426, 0, 0, 0, 0, 0}, + {10688, 8952, 6888, 4108, 1507, 227, 0, 0, 0, 0, 0}, + {13713, 10847, 7680, 3904, 1062, 94, 0, 0, 0, 0, 0}, + {17758, 12946, 8160, 3320, 602, 26, 0, 0, 0, 0, 0}, + {23272, 15008, 7960, 2344, 237, 4, 0, 0, 0, 0, 0}, + {30841, 16327, 6610, 1187, 48, 1, 0, 0, 0, 0, 0}, + {41006, 15348, 3947, 316, 1, 1, 0, 0, 0, 0, 0}, + {53291, 9957, 1104, 14, 1, 1, 0, 0, 0, 0, 0}, + {63380, 2084, 22, 1, 1, 1, 0, 0, 0, 0, 0}, + {65505, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, + {65505, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, + }, + { + {1024, 1024, 1024, 1024, 1024, 1024, 1024, 0, 0, 0, 0}, + {1441, 1423, 1400, 1352, 1262, 1099, 837, 0, 0, 0, 0}, + {1951, 1907, 1841, 1717, 1493, 1133, 658, 0, 0, 0, 0}, + {2577, 2487, 2354, 2115, 1709, 1120, 491, 0, 0, 0, 0}, + {3350, 3186, 2956, 2544, 1890, 1055, 341, 0, 0, 0, 0}, + {4324, 4042, 3657, 2994, 2019, 932, 213, 0, 0, 0, 0}, + {5581, 5107, 4476, 3444, 2055, 754, 113, 0, 0, 0, 0}, + {7258, 6454, 5424, 3840, 1952, 531, 47, 0, 0, 0, 0}, + {9579, 8179, 6473, 4080, 1660, 301, 13, 0, 0, 0, 0}, + {12907, 10365, 7504, 3980, 1171, 119, 2, 0, 0, 0, 0}, + {17852, 12988, 8164, 3304, 594, 23, 1, 0, 0, 0, 0}, + {25441, 15565, 7673, 1974, 155, 1, 1, 0, 0, 0, 0}, + {37208, 16084, 4978, 552, 4, 1, 1, 0, 0, 0, 0}, + {53649, 9731, 1042, 4, 1, 1, 1, 0, 0, 0, 0}, + {65147, 327, 1, 1, 1, 1, 1, 0, 0, 0, 0}, + {65473, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0}, + }, + { + {512, 512, 512, 512, 512, 512, 512, 512, 0, 0, 0}, + {722, 718, 712, 700, 676, 632, 550, 418, 0, 0, 0}, + {981, 971, 952, 920, 858, 748, 566, 329, 0, 0, 0}, + {1300, 1276, 1244, 1178, 1058, 854, 559, 246, 0, 0, 0}, + {1696, 1654, 1593, 1478, 1272, 946, 526, 171, 0, 0, 0}, + {2198, 2126, 2022, 1828, 1498, 1009, 467, 106, 0, 0, 0}, + {2852, 2728, 2554, 2238, 1721, 1028, 376, 57, 0, 0, 0}, + {3736, 3522, 3227, 2712, 1920, 977, 266, 23, 0, 0, 0}, + {4979, 4601, 4090, 3236, 2040, 831, 151, 6, 0, 0, 0}, + {6807, 6101, 5182, 3752, 1991, 586, 59, 1, 0, 0, 0}, + {9634, 8218, 6494, 4082, 1652, 296, 11, 1, 0, 0, 0}, + {14275, 11165, 7784, 3836, 988, 74, 1, 1, 0, 0, 0}, + {22449, 14759, 8042, 2489, 272, 1, 1, 1, 0, 0, 0}, + {37625, 16023, 4866, 509, 1, 1, 1, 1, 0, 0, 0}, + {60485, 4663, 132, 1, 1, 1, 1, 1, 0, 0, 0}, + {65409, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, + }, + { + {256, 256, 256, 256, 256, 256, 256, 256, 256, 0, 0}, + {362, 360, 359, 356, 350, 338, 316, 275, 209, 0, 0}, + {492, 488, 486, 476, 460, 429, 374, 284, 164, 0, 0}, + {653, 647, 638, 622, 589, 529, 426, 280, 123, 0, 0}, + {853, 843, 828, 796, 739, 636, 473, 264, 85, 0, 0}, + {1108, 1090, 1063, 1011, 914, 748, 504, 234, 53, 0, 0}, + {1442, 1410, 1364, 1277, 1120, 860, 514, 189, 28, 0, 0}, + {1895, 1841, 1760, 1614, 1356, 961, 488, 132, 12, 0, 0}, + {2538, 2442, 2300, 2045, 1618, 1021, 414, 76, 3, 0, 0}, + {3497, 3311, 3050, 2591, 1875, 996, 292, 29, 1, 0, 0}, + {5008, 4626, 4109, 3247, 2041, 826, 149, 4, 1, 0, 0}, + {7575, 6701, 5582, 3892, 1918, 494, 34, 1, 1, 0, 0}, + {12397, 10051, 7380, 4020, 1245, 129, 1, 1, 1, 0, 0}, + {22767, 14857, 8012, 2432, 240, 1, 1, 1, 1, 0, 0}, + {47343, 13141, 2332, 35, 1, 1, 1, 1, 1, 0, 0}, + {65281, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, + }, + { + {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0}, + {181, 181, 179, 180, 178, 175, 169, 157, 137, 105, 0}, + {246, 246, 244, 243, 238, 231, 214, 187, 142, 82, 0}, + {327, 325, 324, 319, 310, 294, 265, 213, 141, 61, 0}, + {428, 424, 422, 414, 397, 370, 319, 236, 131, 43, 0}, + {557, 551, 546, 530, 506, 457, 374, 252, 116, 27, 0}, + {725, 717, 705, 683, 638, 560, 430, 256, 95, 14, 0}, + {955, 941, 920, 880, 807, 677, 481, 244, 66, 6, 0}, + {1282, 1256, 1219, 1150, 1023, 810, 510, 207, 37, 2, 0}, + {1772, 1724, 1656, 1524, 1295, 938, 498, 147, 13, 1, 0}, + {2554, 2454, 2312, 2054, 1625, 1020, 414, 74, 1, 1, 0}, + {3904, 3672, 3350, 2791, 1946, 959, 247, 14, 1, 1, 0}, + {6523, 5873, 5026, 3690, 2011, 621, 58, 1, 1, 1, 0}, + {12593, 10175, 7428, 4006, 1216, 105, 1, 1, 1, 1, 0}, + {31009, 16335, 6572, 1136, 1, 1, 1, 1, 1, 1, 0}, + {65025, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}, + }, + { + {64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64}, + {91, 89, 90, 90, 90, 90, 88, 85, 79, 69, 52}, + {123, 123, 123, 123, 121, 120, 115, 108, 93, 71, 41}, + {164, 164, 162, 161, 160, 155, 148, 131, 108, 69, 31}, + {214, 214, 212, 211, 206, 199, 184, 160, 117, 67, 21}, + {279, 277, 276, 273, 265, 252, 229, 187, 126, 59, 13}, + {364, 360, 358, 352, 342, 319, 280, 215, 129, 47, 7}, + {479, 477, 470, 460, 441, 404, 339, 240, 122, 33, 3}, + {644, 638, 627, 610, 576, 511, 405, 254, 105, 18, 1}, + {892, 880, 862, 828, 763, 647, 469, 248, 73, 6, 1}, + {1290, 1264, 1227, 1156, 1028, 812, 510, 207, 34, 1, 1}, + {1982, 1922, 1836, 1674, 1396, 972, 481, 123, 4, 1, 1}, + {3347, 3177, 2936, 2513, 1846, 1004, 311, 22, 1, 1, 1}, + {6632, 5962, 5087, 3714, 2003, 609, 37, 1, 1, 1, 1}, + {17963, 13041, 8168, 3285, 506, 1, 1, 1, 1, 1, 1}, + {64513, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + }, +}; + +inline constexpr std::uint8_t bc1_quadrant[256] = { + 170, 255, 0, 85, 250, 175, 251, 191, 254, 239, 187, 238, 223, 186, 90, 174, + 127, 234, 171, 247, 253, 165, 95, 245, 221, 119, 102, 153, 42, 138, 162, 80, + 5, 168, 17, 154, 68, 63, 166, 169, 207, 190, 252, 243, 106, 235, 126, 231, + 219, 189, 122, 167, 218, 15, 173, 87, 213, 10, 93, 117, 47, 110, 240, 143, + 248, 230, 61, 242, 185, 37, 124, 155, 59, 133, 206, 236, 88, 179, 211, 160, + 82, 222, 123, 43, 183, 94, 199, 237, 91, 178, 142, 204, 111, 25, 246, 159, + 100, 232, 229, 41, 249, 51, 70, 34, 181, 215, 45, 120, 86, 134, 136, 104, + 145, 149, 146, 210, 135, 125, 118, 103, 157, 89, 101, 217, 21, 55, 147, 92, + 198, 53, 108, 205, 57, 69, 220, 84, 115, 81, 83, 197, 46, 139, 141, 39, + 226, 31, 244, 29, 216, 241, 114, 163, 79, 116, 184, 202, 58, 172, 209, 137, + 71, 38, 98, 152, 112, 13, 161, 26, 74, 107, 27, 208, 164, 233, 7, 177, + 158, 182, 228, 78, 188, 227, 62, 203, 130, 49, 2, 40, 76, 19, 196, 32, + 14, 11, 8, 150, 128, 224, 9, 176, 105, 96, 6, 144, 35, 151, 140, 214, + 33, 109, 121, 50, 200, 72, 132, 23, 18, 212, 77, 113, 1, 54, 99, 156, + 201, 64, 4, 16, 225, 22, 148, 73, 75, 30, 180, 97, 3, 12, 192, 66, + 129, 24, 48, 36, 195, 60, 194, 65, 20, 131, 44, 56, 67, 52, 28, 193, +}; +inline constexpr std::uint8_t bc3_colour_quadrant[256] = { + 85, 170, 255, 0, 80, 5, 175, 250, 174, 234, 171, 186, 238, 187, 254, 191, + 239, 251, 165, 90, 240, 17, 15, 68, 245, 95, 160, 10, 138, 162, 168, 42, + 127, 247, 119, 223, 253, 221, 102, 153, 136, 190, 235, 34, 204, 51, 143, 248, + 87, 117, 213, 93, 47, 242, 122, 133, 167, 218, 88, 37, 173, 82, 63, 243, + 207, 232, 252, 142, 43, 126, 106, 166, 154, 112, 7, 13, 169, 120, 128, 8, + 2, 231, 178, 32, 219, 208, 135, 94, 189, 124, 45, 229, 199, 236, 206, 91, + 179, 210, 46, 181, 59, 224, 230, 110, 104, 139, 155, 226, 185, 14, 184, 11, + 61, 211, 81, 134, 39, 69, 21, 222, 84, 141, 183, 237, 123, 176, 216, 41, + 114, 146, 125, 215, 53, 163, 197, 130, 55, 83, 40, 92, 86, 101, 118, 149, + 89, 205, 159, 58, 115, 137, 202, 172, 103, 157, 246, 217, 249, 145, 25, 220, + 152, 108, 70, 100, 111, 38, 57, 98, 198, 147, 9, 35, 6, 96, 140, 200, + 144, 50, 3, 62, 192, 19, 49, 227, 76, 158, 12, 196, 203, 188, 182, 233, + 48, 107, 31, 241, 29, 209, 71, 116, 79, 16, 64, 244, 1, 4, 161, 26, + 164, 27, 74, 177, 228, 78, 54, 201, 156, 99, 151, 113, 121, 23, 72, 105, + 33, 18, 60, 132, 131, 109, 214, 150, 195, 56, 30, 77, 225, 212, 194, 44, + 24, 129, 180, 75, 22, 97, 193, 36, 28, 73, 20, 66, 148, 65, 52, 67, +}; +inline constexpr std::uint8_t bc3_alpha_pair[64][2] = { + {0, 0}, {1, 1}, {6, 6}, {4, 4}, {3, 3}, {5, 5}, {7, 7}, {1, 6}, + {6, 1}, {0, 1}, {1, 0}, {7, 1}, {1, 7}, {0, 3}, {3, 0}, {0, 2}, + {2, 0}, {2, 2}, {5, 4}, {3, 4}, {4, 3}, {4, 5}, {1, 5}, {5, 1}, + {0, 4}, {4, 0}, {2, 3}, {3, 2}, {5, 3}, {3, 5}, {6, 5}, {5, 6}, + {6, 7}, {7, 6}, {5, 7}, {4, 6}, {6, 4}, {7, 5}, {6, 0}, {0, 6}, + {4, 2}, {2, 4}, {4, 1}, {1, 4}, {5, 0}, {0, 5}, {6, 3}, {3, 6}, + {3, 1}, {5, 2}, {1, 3}, {2, 5}, {7, 4}, {4, 7}, {7, 3}, {3, 7}, + {2, 6}, {6, 2}, {2, 1}, {1, 2}, {0, 7}, {7, 0}, {2, 7}, {7, 2}, +}; +inline constexpr std::uint8_t bc4_pair[64][2] = { + {1, 1}, {0, 0}, {3, 3}, {6, 6}, {4, 4}, {5, 5}, {3, 0}, {0, 3}, + {6, 1}, {1, 6}, {7, 7}, {2, 2}, {6, 3}, {3, 6}, {1, 0}, {0, 1}, + {4, 1}, {1, 4}, {3, 1}, {1, 3}, {6, 0}, {0, 6}, {4, 2}, {7, 5}, + {5, 7}, {2, 4}, {0, 5}, {5, 0}, {6, 4}, {4, 6}, {5, 3}, {3, 5}, + {4, 0}, {0, 4}, {1, 5}, {5, 1}, {3, 4}, {4, 3}, {2, 6}, {6, 2}, + {7, 3}, {3, 7}, {1, 2}, {2, 1}, {0, 7}, {7, 0}, {2, 5}, {5, 2}, + {7, 4}, {4, 7}, {4, 5}, {5, 4}, {3, 2}, {2, 3}, {0, 2}, {2, 0}, + {1, 7}, {7, 1}, {6, 5}, {5, 6}, {6, 7}, {7, 6}, {2, 7}, {7, 2}, +}; +inline constexpr std::uint8_t bc5_pair[64][2] = { + {0, 0}, {1, 1}, {4, 4}, {3, 3}, {6, 6}, {7, 7}, {4, 0}, {0, 4}, + {0, 1}, {2, 2}, {1, 0}, {3, 0}, {0, 3}, {5, 5}, {2, 0}, {0, 2}, + {1, 6}, {6, 1}, {4, 3}, {1, 7}, {3, 4}, {1, 4}, {7, 1}, {6, 0}, + {0, 6}, {6, 3}, {6, 4}, {3, 6}, {3, 2}, {4, 6}, {1, 5}, {1, 3}, + {2, 3}, {5, 4}, {5, 3}, {5, 0}, {4, 1}, {7, 6}, {4, 5}, {4, 2}, + {3, 1}, {3, 5}, {5, 1}, {6, 7}, {2, 4}, {7, 5}, {7, 4}, {0, 5}, + {6, 5}, {4, 7}, {5, 7}, {7, 0}, {5, 6}, {0, 7}, {7, 3}, {3, 7}, + {5, 2}, {2, 5}, {1, 2}, {6, 2}, {2, 1}, {2, 6}, {7, 2}, {2, 7}, +}; + +inline constexpr std::uint8_t bc1_history_state[256] = { + 0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +}; +inline constexpr std::uint8_t bc1_history[256][6] = { + {0, 0, 0, 0, 0, 0}, + {7, 29, 6, 8, 30, 7}, + {15, 29, 15, 16, 36, 17}, + {20, 38, 14, 3, 5, 2}, + {12, 27, 13, 6, 12, 6}, + {10, 22, 11, 6, 14, 7}, + {12, 24, 11, 7, 14, 6}, + {12, 26, 12, 7, 14, 7}, + {22, 36, 15, 20, 32, 14}, + {14, 23, 8, 1, 2, 1}, + {23, 5, 30, 9, 5, 30}, + {11, 22, 11, 7, 15, 7}, + {17, 22, 7, 10, 11, 3}, + {12, 25, 12, 7, 16, 7}, + {13, 28, 8, 5, 11, 3}, + {11, 20, 8, 16, 31, 14}, + {23, 32, 30, 17, 12, 29}, + {8, 44, 30, 6, 26, 30}, + {10, 24, 30, 6, 45, 30}, + {10, 12, 4, 8, 8, 2}, + {16, 23, 30, 14, 6, 26}, + {12, 25, 13, 10, 22, 12}, + {19, 27, 10, 12, 15, 5}, + {11, 24, 12, 4, 11, 6}, + {29, 60, 29, 6, 13, 6}, + {17, 31, 11, 1, 2, 1}, + {26, 54, 24, 12, 24, 10}, + {23, 49, 30, 20, 36, 30}, + {16, 23, 10, 10, 14, 6}, + {11, 24, 10, 6, 13, 5}, + {18, 52, 30, 14, 22, 30}, + {14, 51, 30, 9, 34, 30}, + {2, 3, 1, 19, 37, 16}, + {14, 15, 30, 9, 31, 30}, + {29, 57, 27, 27, 52, 25}, + {13, 29, 15, 8, 20, 11}, + {13, 17, 5, 9, 10, 3}, + {18, 50, 29, 15, 10, 29}, + {11, 25, 13, 8, 19, 10}, + {17, 32, 12, 6, 11, 4}, + {22, 18, 30, 19, 41, 30}, + {10, 22, 11, 8, 18, 9}, + {18, 30, 31, 10, 44, 30}, + {25, 50, 24, 2, 4, 1}, + {11, 30, 31, 7, 15, 29}, + {12, 19, 8, 11, 16, 7}, + {19, 29, 30, 14, 55, 28}, + {9, 20, 10, 4, 10, 5}, + {12, 24, 10, 7, 13, 5}, + {26, 53, 25, 6, 13, 6}, + {19, 27, 31, 11, 19, 30}, + {21, 31, 30, 22, 31, 30}, + {10, 19, 5, 11, 21, 6}, + {30, 50, 3, 29, 46, 2}, + {15, 9, 30, 7, 9, 30}, + {14, 18, 6, 11, 14, 4}, + {28, 56, 26, 4, 7, 3}, + {9, 20, 10, 5, 13, 7}, + {28, 58, 26, 15, 32, 14}, + {11, 19, 8, 6, 9, 4}, + {16, 22, 9, 13, 17, 7}, + {1, 2, 1, 14, 28, 12}, + {22, 45, 30, 9, 15, 30}, + {19, 22, 30, 16, 23, 31}, + {25, 50, 24, 21, 43, 20}, + {6, 11, 4, 16, 31, 13}, + {13, 25, 8, 3, 6, 1}, + {5, 9, 3, 12, 24, 10}, + {16, 29, 13, 7, 13, 5}, + {20, 38, 16, 13, 25, 10}, + {18, 35, 15, 5, 9, 3}, + {13, 25, 11, 6, 11, 4}, + {11, 21, 7, 7, 13, 4}, + {20, 39, 16, 11, 20, 8}, + {16, 31, 12, 4, 8, 2}, + {22, 41, 30, 18, 23, 30}, + {24, 50, 24, 9, 19, 8}, + {3, 12, 7, 4, 16, 8}, + {18, 27, 12, 16, 22, 10}, + {24, 49, 23, 13, 28, 12}, + {24, 50, 24, 18, 37, 18}, + {17, 33, 12, 14, 27, 9}, + {20, 10, 31, 13, 10, 31}, + {7, 8, 2, 5, 5, 1}, + {1, 2, 1, 10, 21, 9}, + {10, 18, 6, 1, 1, 0}, + {22, 46, 21, 1, 3, 1}, + {11, 20, 8, 8, 14, 5}, + {17, 35, 31, 14, 14, 29}, + {16, 22, 31, 12, 40, 31}, + {12, 22, 9, 8, 15, 6}, + {18, 15, 30, 15, 33, 31}, + {19, 32, 30, 15, 43, 31}, + {18, 35, 15, 8, 15, 5}, + {21, 43, 20, 18, 37, 17}, + {10, 17, 6, 9, 15, 6}, + {14, 27, 12, 7, 13, 5}, + {8, 16, 7, 12, 25, 11}, + {18, 37, 17, 1, 1, 0}, + {17, 45, 30, 15, 33, 31}, + {13, 25, 11, 8, 16, 7}, + {23, 47, 22, 16, 32, 15}, + {18, 33, 13, 11, 21, 8}, + {4, 10, 4, 8, 17, 7}, + {1, 2, 1, 3, 7, 3}, + {11, 22, 9, 6, 11, 4}, + {16, 29, 11, 13, 23, 9}, + {12, 24, 12, 6, 12, 6}, + {23, 47, 22, 7, 14, 6}, + {11, 32, 30, 11, 31, 30}, + {14, 26, 11, 10, 18, 7}, + {20, 41, 19, 1, 3, 1}, + {14, 28, 12, 8, 16, 6}, + {11, 21, 9, 8, 15, 6}, + {1, 2, 1, 7, 13, 6}, + {19, 35, 14, 16, 29, 12}, + {13, 27, 13, 7, 15, 7}, + {15, 33, 16, 8, 18, 9}, + {15, 42, 31, 11, 22, 30}, + {23, 48, 23, 4, 9, 4}, + {22, 45, 21, 11, 22, 10}, + {31, 62, 30, 31, 61, 29}, + {13, 26, 11, 5, 8, 3}, + {19, 39, 19, 9, 20, 10}, + {7, 14, 2, 6, 12, 2}, + {21, 42, 20, 3, 7, 3}, + {10, 17, 7, 8, 13, 5}, + {11, 20, 8, 9, 17, 7}, + {19, 33, 31, 12, 31, 31}, + {21, 43, 20, 5, 11, 5}, + {10, 20, 9, 7, 13, 6}, + {10, 21, 10, 6, 13, 6}, + {15, 29, 12, 11, 20, 8}, + {21, 43, 20, 9, 18, 8}, + {3, 8, 3, 5, 11, 5}, + {16, 31, 13, 9, 17, 7}, + {11, 22, 11, 6, 12, 6}, + {10, 21, 10, 7, 15, 7}, + {27, 3, 1, 26, 1, 0}, + {18, 37, 18, 16, 32, 16}, + {12, 23, 10, 9, 17, 7}, + {12, 25, 12, 8, 17, 8}, + {11, 21, 9, 2, 4, 1}, + {7, 15, 7, 8, 18, 9}, + {17, 33, 15, 13, 24, 11}, + {9, 19, 9, 7, 15, 7}, + {21, 43, 20, 13, 27, 12}, + {17, 36, 17, 2, 4, 2}, + {10, 20, 9, 6, 12, 5}, + {20, 41, 19, 7, 14, 7}, + {3, 9, 6, 2, 8, 5}, + {13, 24, 10, 11, 20, 8}, + {18, 38, 18, 4, 7, 3}, + {19, 38, 18, 8, 17, 8}, + {16, 35, 17, 4, 10, 4}, + {11, 23, 11, 10, 21, 10}, + {17, 36, 17, 7, 14, 7}, + {13, 27, 13, 6, 12, 6}, + {9, 15, 4, 8, 13, 4}, + {10, 19, 7, 5, 9, 3}, + {19, 40, 19, 11, 24, 11}, + {11, 23, 11, 8, 17, 8}, + {14, 29, 14, 7, 15, 7}, + {12, 22, 9, 5, 8, 3}, + {10, 20, 9, 8, 17, 8}, + {21, 42, 19, 15, 31, 14}, + {14, 29, 13, 1, 1, 0}, + {8, 16, 8, 7, 14, 7}, + {9, 18, 8, 8, 16, 7}, + {10, 17, 7, 3, 4, 1}, + {14, 28, 13, 2, 4, 1}, + {14, 30, 14, 3, 6, 3}, + {19, 38, 18, 5, 11, 5}, + {12, 26, 12, 1, 2, 1}, + {9, 16, 6, 8, 13, 5}, + {8, 13, 4, 7, 11, 4}, + {16, 34, 16, 3, 7, 3}, + {13, 28, 13, 4, 9, 4}, + {15, 31, 15, 6, 13, 6}, + {12, 24, 11, 9, 19, 9}, + {9, 19, 8, 7, 14, 6}, + {14, 28, 13, 5, 10, 4}, + {10, 20, 7, 4, 6, 2}, + {16, 34, 17, 11, 25, 12}, + {15, 30, 15, 8, 16, 8}, + {13, 27, 13, 10, 20, 10}, + {9, 20, 9, 0, 1, 0}, + {16, 32, 15, 2, 4, 2}, + {13, 27, 12, 3, 6, 3}, + {13, 26, 12, 12, 23, 11}, + {12, 25, 12, 5, 10, 5}, + {11, 23, 11, 5, 11, 5}, + {16, 34, 16, 6, 12, 5}, + {16, 35, 31, 15, 25, 31}, + {16, 33, 16, 7, 15, 7}, + {13, 27, 13, 8, 18, 8}, + {18, 36, 17, 11, 22, 10}, + {18, 37, 18, 13, 27, 13}, + {12, 25, 12, 4, 8, 4}, + {9, 18, 7, 6, 11, 4}, + {15, 30, 14, 9, 19, 9}, + {17, 35, 16, 9, 18, 9}, + {11, 24, 11, 2, 4, 2}, + {16, 33, 16, 0, 1, 0}, + {15, 31, 15, 4, 9, 4}, + {16, 31, 15, 14, 27, 13}, + {14, 29, 14, 13, 25, 12}, + {11, 23, 11, 3, 6, 3}, + {16, 32, 16, 10, 21, 10}, + {11, 22, 11, 4, 9, 4}, + {11, 22, 10, 1, 2, 0}, + {8, 14, 5, 2, 4, 1}, + {9, 16, 6, 6, 10, 4}, + {10, 21, 10, 9, 19, 9}, + {14, 30, 14, 11, 22, 11}, + {9, 18, 8, 6, 13, 6}, + {6, 13, 6, 5, 11, 5}, + {7, 14, 6, 0, 1, 0}, + {4, 7, 2, 3, 4, 1}, + {10, 20, 9, 4, 8, 4}, + {9, 17, 7, 5, 9, 3}, + {7, 14, 7, 1, 2, 1}, + {10, 20, 10, 5, 10, 5}, + {8, 15, 5, 4, 6, 2}, + {8, 17, 8, 1, 2, 1}, + {10, 20, 10, 3, 6, 3}, + {7, 10, 3, 6, 8, 3}, + {8, 15, 7, 6, 12, 6}, + {9, 19, 9, 2, 4, 2}, + {6, 11, 4, 3, 4, 1}, + {6, 11, 4, 0, 1, 0}, + {8, 16, 7, 2, 4, 2}, + {16, 34, 16, 15, 29, 15}, + {7, 16, 7, 5, 10, 5}, + {8, 14, 5, 5, 9, 3}, + {9, 18, 8, 4, 8, 4}, + {5, 9, 3, 4, 7, 2}, + {9, 18, 8, 5, 10, 5}, + {6, 12, 6, 2, 4, 2}, + {7, 12, 5, 4, 7, 3}, + {5, 11, 5, 1, 2, 1}, + {8, 17, 8, 3, 6, 3}, + {16, 31, 31, 16, 32, 31}, + {7, 14, 7, 3, 6, 3}, + {5, 11, 6, 4, 9, 4}, + {7, 15, 7, 4, 8, 4}, + {2, 4, 2, 0, 0, 1}, + {5, 11, 5, 3, 6, 3}, + {4, 8, 4, 1, 1, 1}, + {3, 6, 3, 1, 2, 1}, + {2, 4, 1, 1, 2, 1}, + {4, 8, 4, 3, 6, 3}, + {3, 6, 3, 2, 4, 2}, + {4, 9, 4, 2, 4, 2}, + {17, 27, 31, 15, 34, 31}, + {0, 0, 1, 0, 0, 0}, +}; + +inline constexpr std::uint8_t bc3_history_state[256] = { + 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, + 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, + 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, + 0, 2, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; +inline constexpr std::uint8_t bc3_history[256][8] = { + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {177, 134, 27, 55, 24, 14, 54, 24}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {255, 255, 21, 34, 8, 15, 22, 3}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {220, 109, 17, 35, 17, 15, 30, 15}, + {219, 96, 2, 53, 14, 2, 59, 16}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {95, 65, 16, 31, 16, 15, 31, 15}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {136, 13, 16, 33, 16, 15, 32, 15}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {146, 108, 18, 37, 18, 13, 26, 13}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {255, 255, 20, 38, 16, 3, 5, 2}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {200, 51, 17, 34, 17, 15, 31, 15}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {170, 150, 2, 60, 20, 1, 61, 20}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {248, 10, 3, 4, 2, 0, 1, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {237, 229, 26, 51, 25, 1, 3, 1}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {255, 0, 19, 38, 18, 14, 29, 13}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {255, 6, 13, 27, 13, 8, 17, 8}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {254, 4, 15, 27, 11, 3, 4, 2}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {117, 39, 21, 41, 21, 1, 3, 1}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {170, 119, 16, 31, 16, 15, 30, 15}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {122, 121, 14, 29, 14, 13, 27, 13}, + {247, 1, 15, 28, 13, 9, 16, 7}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {105, 126, 2, 7, 4, 1, 3, 2}, + {93, 100, 20, 40, 20, 15, 30, 15}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {91, 99, 17, 35, 17, 15, 30, 15}, + {155, 99, 16, 33, 16, 15, 31, 15}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {255, 255, 13, 24, 12, 11, 23, 10}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {254, 255, 10, 17, 7, 2, 3, 1}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {253, 255, 11, 20, 9, 6, 11, 4}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {254, 255, 5, 10, 5, 1, 2, 1}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {255, 255, 11, 20, 9, 9, 17, 7}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {106, 113, 24, 39, 23, 0, 13, 0}, + {1, 255, 8, 15, 7, 5, 10, 4}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {1, 2, 0, 1, 1, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {140, 131, 16, 31, 16, 15, 34, 16}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {255, 254, 5, 11, 5, 4, 9, 3}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {255, 255, 1, 1, 1, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {253, 254, 3, 5, 2, 1, 2, 1}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, +}; + +inline constexpr std::uint8_t bc4_history_state[256] = { + 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, + 2, 2, 2, 2, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 2, + 2, 0, 2, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, + 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 0, 0, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, + 2, 2, 0, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 2, + 2, 0, 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 0, 2, 0, 2, 0, +}; +inline constexpr std::uint8_t bc4_history[256][2] = { + {0, 0}, + {0, 0}, + {0, 0}, + {122, 77}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {67, 91}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {83, 49}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {114, 158}, + {58, 31}, + {83, 58}, + {103, 121}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {246, 65}, + {244, 137}, + {101, 141}, + {181, 141}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {224, 201}, + {0, 0}, + {0, 0}, + {217, 182}, + {0, 0}, + {0, 0}, + {227, 73}, + {0, 0}, + {213, 205}, + {0, 0}, + {0, 0}, + {125, 83}, + {0, 0}, + {243, 91}, + {214, 190}, + {226, 53}, + {0, 0}, + {230, 227}, + {0, 0}, + {0, 0}, + {156, 116}, + {250, 174}, + {220, 86}, + {238, 115}, + {227, 42}, + {166, 80}, + {200, 166}, + {0, 0}, + {164, 124}, + {0, 0}, + {0, 0}, + {74, 115}, + {83, 99}, + {186, 74}, + {140, 148}, + {204, 141}, + {220, 103}, + {208, 44}, + {147, 108}, + {190, 88}, + {0, 0}, + {203, 82}, + {222, 142}, + {218, 217}, + {206, 63}, + {186, 189}, + {0, 0}, + {197, 48}, + {0, 0}, + {203, 203}, + {147, 116}, + {196, 14}, + {172, 137}, + {214, 124}, + {200, 174}, + {122, 83}, + {152, 157}, + {88, 51}, + {178, 87}, + {169, 149}, + {170, 98}, + {171, 71}, + {202, 183}, + {138, 100}, + {0, 0}, + {153, 148}, + {0, 0}, + {77, 34}, + {140, 82}, + {171, 106}, + {194, 127}, + {197, 193}, + {179, 118}, + {0, 0}, + {190, 161}, + {179, 167}, + {115, 126}, + {139, 108}, + {132, 115}, + {161, 89}, + {182, 152}, + {186, 61}, + {165, 56}, + {170, 175}, + {186, 177}, + {167, 159}, + {153, 81}, + {0, 0}, + {145, 140}, + {0, 0}, + {151, 94}, + {57, 74}, + {112, 107}, + {132, 91}, + {0, 0}, + {0, 0}, + {74, 16}, + {0, 0}, + {177, 43}, + {151, 124}, + {123, 107}, + {145, 73}, + {113, 75}, + {75, 71}, + {107, 90}, + {194, 106}, + {158, 69}, + {159, 40}, + {71, 51}, + {144, 132}, + {156, 15}, + {144, 62}, + {81, 75}, + {122, 91}, + {55, 33}, + {76, 58}, + {152, 53}, + {0, 0}, + {89, 83}, + {114, 83}, + {65, 74}, + {0, 0}, + {152, 39}, + {140, 119}, + {42, 15}, + {65, 41}, + {114, 91}, + {0, 0}, + {0, 0}, + {90, 50}, + {48, 8}, + {102, 102}, + {132, 70}, + {73, 56}, + {123, 119}, + {72, 34}, + {159, 136}, + {131, 107}, + {105, 12}, + {143, 40}, + {113, 99}, + {43, 25}, + {0, 0}, + {79, 85}, + {126, 100}, + {0, 0}, + {98, 58}, + {90, 74}, + {133, 135}, + {96, 90}, + {0, 0}, + {55, 17}, + {0, 0}, + {82, 42}, + {71, 66}, + {133, 52}, + {89, 58}, + {113, 6}, + {132, 123}, + {39, 9}, + {136, 27}, + {0, 0}, + {39, 23}, + {81, 50}, + {19, 41}, + {39, 17}, + {103, 82}, + {0, 0}, + {115, 21}, + {120, 60}, + {81, 58}, + {73, 42}, + {47, 17}, + {0, 0}, + {64, 25}, + {119, 34}, + {0, 0}, + {66, 58}, + {33, 16}, + {101, 67}, + {66, 49}, + {101, 75}, + {0, 0}, + {78, 5}, + {0, 0}, + {33, 60}, + {57, 24}, + {69, 2}, + {0, 0}, + {0, 0}, + {105, 25}, + {96, 35}, + {112, 46}, + {0, 0}, + {47, 25}, + {88, 22}, + {85, 66}, + {49, 33}, + {22, 17}, + {55, 41}, + {0, 0}, + {130, 129}, + {0, 0}, + {27, 8}, + {0, 0}, + {12, 6}, + {0, 0}, +}; + +inline constexpr std::uint8_t bc5_history_state[256] = { + 0, 0, 0, 2, 2, 2, 2, 2, 0, 2, 0, 0, 0, 0, 2, 2, 2, 0, 0, 2, 2, 0, 2, 0, 2, 0, 0, 2, 2, 2, 2, 2, + 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 0, 2, + 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, + 2, 0, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 2, + 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 1, 2, + 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, +}; +inline constexpr std::uint8_t bc5_history[256][4] = { + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {130, 218, 45, 192}, + {172, 109, 158, 94}, + {183, 136, 113, 69}, + {125, 129, 86, 34}, + {171, 196, 33, 82}, + {0, 0, 0, 0}, + {142, 149, 102, 118}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {82, 62, 26, 23}, + {100, 94, 32, 32}, + {97, 123, 61, 97}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {133, 140, 118, 123}, + {206, 194, 128, 123}, + {0, 0, 0, 0}, + {147, 145, 64, 66}, + {0, 0, 0, 0}, + {76, 189, 55, 171}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {94, 115, 86, 102}, + {163, 38, 152, 29}, + {143, 133, 89, 80}, + {197, 67, 172, 50}, + {164, 218, 130, 127}, + {141, 145, 111, 132}, + {177, 125, 131, 32}, + {49, 113, 26, 94}, + {0, 0, 0, 0}, + {173, 169, 40, 28}, + {0, 0, 0, 0}, + {117, 125, 43, 50}, + {139, 111, 99, 72}, + {123, 93, 83, 49}, + {189, 166, 135, 116}, + {198, 97, 178, 73}, + {205, 201, 76, 86}, + {172, 174, 73, 72}, + {0, 0, 0, 0}, + {157, 210, 136, 188}, + {126, 149, 115, 129}, + {158, 67, 137, 41}, + {214, 120, 131, 80}, + {0, 0, 0, 0}, + {147, 150, 119, 119}, + {169, 107, 135, 65}, + {0, 0, 0, 0}, + {231, 168, 223, 154}, + {115, 144, 99, 120}, + {74, 104, 50, 83}, + {220, 132, 32, 123}, + {150, 136, 110, 96}, + {0, 0, 0, 0}, + {139, 147, 124, 108}, + {121, 124, 78, 94}, + {0, 0, 0, 0}, + {169, 151, 124, 103}, + {120, 106, 103, 88}, + {134, 147, 110, 109}, + {0, 0, 0, 0}, + {133, 125, 103, 107}, + {160, 133, 118, 123}, + {175, 78, 163, 68}, + {229, 133, 166, 121}, + {233, 147, 224, 134}, + {127, 142, 104, 121}, + {134, 150, 99, 121}, + {110, 149, 83, 129}, + {175, 187, 129, 141}, + {0, 0, 0, 0}, + {115, 124, 96, 104}, + {164, 128, 141, 109}, + {129, 85, 114, 22}, + {246, 131, 241, 125}, + {0, 0, 0, 0}, + {168, 186, 145, 165}, + {192, 133, 158, 100}, + {107, 143, 105, 133}, + {201, 212, 172, 181}, + {124, 138, 116, 121}, + {92, 60, 71, 40}, + {203, 134, 149, 126}, + {0, 0, 0, 0}, + {61, 123, 25, 114}, + {123, 160, 113, 145}, + {150, 169, 135, 150}, + {134, 139, 101, 109}, + {253, 94, 255, 63}, + {123, 87, 106, 67}, + {238, 133, 121, 121}, + {0, 0, 0, 0}, + {24, 18, 15, 10}, + {117, 41, 97, 25}, + {123, 121, 70, 67}, + {64, 59, 51, 49}, + {11, 103, 12, 88}, + {144, 133, 111, 123}, + {189, 200, 65, 52}, + {219, 153, 195, 135}, + {150, 110, 138, 98}, + {118, 142, 60, 117}, + {139, 145, 114, 118}, + {216, 225, 191, 200}, + {46, 46, 38, 33}, + {91, 132, 85, 126}, + {154, 178, 115, 132}, + {0, 0, 0, 0}, + {132, 126, 122, 15}, + {136, 134, 15, 123}, + {131, 129, 105, 123}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {132, 215, 122, 149}, + {129, 138, 112, 111}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {136, 145, 119, 131}, + {179, 196, 165, 183}, + {126, 125, 111, 92}, + {136, 150, 122, 123}, + {168, 158, 136, 126}, + {146, 153, 129, 134}, + {127, 130, 85, 124}, + {132, 241, 120, 126}, + {155, 124, 125, 85}, + {121, 115, 115, 108}, + {106, 72, 89, 51}, + {136, 155, 118, 137}, + {121, 145, 106, 133}, + {119, 220, 102, 209}, + {179, 136, 150, 122}, + {120, 133, 102, 113}, + {145, 142, 111, 112}, + {130, 108, 123, 62}, + {121, 132, 37, 122}, + {157, 142, 135, 119}, + {80, 201, 59, 191}, + {130, 133, 122, 104}, + {138, 136, 112, 120}, + {131, 107, 123, 37}, + {122, 120, 107, 119}, + {126, 181, 121, 146}, + {143, 141, 137, 135}, + {134, 178, 121, 161}, + {103, 135, 55, 126}, + {193, 132, 128, 123}, + {141, 129, 111, 114}, + {79, 77, 53, 51}, + {133, 72, 127, 64}, + {0, 0, 0, 0}, + {102, 101, 83, 80}, + {0, 0, 0, 0}, + {92, 137, 39, 125}, + {108, 136, 87, 122}, + {102, 97, 65, 57}, + {163, 149, 151, 134}, + {133, 17, 121, 15}, + {0, 0, 0, 0}, + {179, 162, 153, 133}, + {119, 132, 98, 125}, + {175, 132, 130, 124}, + {0, 0, 0, 0}, + {132, 92, 124, 81}, + {40, 31, 27, 19}, + {188, 211, 180, 202}, + {139, 121, 117, 101}, + {150, 125, 130, 106}, + {129, 184, 123, 131}, + {132, 158, 126, 148}, + {130, 155, 126, 129}, + {131, 167, 124, 127}, + {155, 131, 132, 125}, + {198, 139, 170, 124}, + {200, 196, 190, 186}, + {0, 0, 0, 0}, + {133, 126, 124, 55}, + {128, 126, 116, 112}, + {173, 157, 163, 147}, + {197, 175, 158, 137}, + {102, 129, 98, 126}, + {131, 216, 121, 198}, + {127, 135, 115, 130}, + {139, 196, 94, 111}, + {132, 106, 123, 94}, + {219, 130, 193, 116}, + {212, 133, 127, 124}, + {196, 176, 173, 151}, + {56, 192, 15, 27}, + {129, 198, 124, 131}, + {131, 124, 125, 99}, + {176, 130, 169, 126}, + {137, 127, 121, 111}, + {144, 137, 131, 123}, + {132, 130, 114, 120}, + {182, 165, 169, 153}, + {138, 136, 113, 109}, + {185, 172, 179, 164}, + {131, 127, 123, 81}, + {124, 141, 121, 137}, + {173, 131, 136, 92}, + {0, 0, 0, 0}, + {120, 133, 108, 123}, + {147, 134, 123, 110}, + {130, 142, 125, 129}, + {136, 137, 122, 131}, + {130, 119, 127, 114}, + {0, 0, 0, 0}, + {117, 136, 111, 131}, + {199, 181, 187, 167}, + {132, 215, 124, 125}, + {135, 138, 120, 114}, + {131, 85, 123, 45}, + {197, 131, 193, 126}, + {0, 0, 0, 0}, + {69, 206, 57, 195}, + {131, 128, 123, 37}, + {161, 156, 96, 92}, + {0, 0, 0, 0}, + {130, 136, 123, 121}, + {126, 60, 118, 46}, + {139, 137, 120, 124}, + {140, 130, 124, 123}, + {122, 127, 118, 123}, + {0, 0, 0, 0}, + {130, 141, 127, 138}, + {54, 205, 50, 185}, + {75, 74, 69, 68}, + {223, 130, 217, 125}, + {111, 129, 108, 127}, + {160, 129, 157, 126}, + {129, 217, 126, 217}, + {148, 128, 146, 126}, + {130, 129, 127, 118}, + {0, 0, 0, 0}, + {60, 200, 50, 200}, + {131, 130, 120, 124}, + {123, 132, 116, 127}, + {132, 135, 130, 132}, + {129, 41, 126, 37}, + {122, 133, 120, 133}, + {118, 128, 116, 126}, + {136, 128, 133, 126}, + {128, 133, 123, 131}, + {82, 81, 78, 77}, + {124, 130, 122, 128}, + {130, 130, 125, 124}, + {128, 128, 127, 127}, + {127, 126, 126, 125}, + {129, 127, 128, 126}, +}; + +inline constexpr std::uint16_t bc7_part2_packed[64] = { + 0xcccc, 0x8888, 0xeeee, 0xecc8, 0xc880, 0xfeec, 0xfec8, 0xec80, + 0xc800, 0xffec, 0xfe80, 0xe800, 0xffe8, 0xff00, 0xfff0, 0xf000, + 0xf710, 0x8e, 0x7100, 0x8ce, 0x8c, 0x7310, 0x3100, 0x8cce, + 0x88c, 0x3110, 0x6666, 0x366c, 0x17e8, 0xff0, 0x718e, 0x399c, + 0xaaaa, 0xf0f0, 0x5a5a, 0x33cc, 0x3c3c, 0x55aa, 0x9696, 0xa55a, + 0x73ce, 0x13c8, 0x324c, 0x3bdc, 0x6996, 0xc33c, 0x9966, 0x660, + 0x272, 0x4e4, 0x4e40, 0x2720, 0xc936, 0x936c, 0x39c6, 0x639c, + 0x9336, 0x9cc6, 0x817e, 0xe718, 0xccf0, 0xfcc, 0x7744, 0xee22, +}; +inline constexpr std::uint32_t bc7_part3_packed[64] = { + 0xaa685050, 0x6a5a5040, 0x5a5a4200, 0x5450a0a8, 0xa5a50000, 0xa0a05050, + 0x5555a0a0, 0x5a5a5050, 0xaa550000, 0xaa555500, 0xaaaa5500, 0x90909090, + 0x94949494, 0xa4a4a4a4, 0xa9a59450, 0x2a0a4250, 0xa5945040, 0xa425054, + 0xa5a5a500, 0x55a0a0a0, 0xa8a85454, 0x6a6a4040, 0xa4a45000, 0x1a1a0500, + 0x50a4a4, 0xaaa59090, 0x14696914, 0x69691400, 0xa08585a0, 0xaa821414, + 0x50a4a450, 0x6a5a0200, 0xa9a58000, 0x5090a0a8, 0xa8a09050, 0x24242424, + 0xaa5500, 0x24924924, 0x24499224, 0x50a50a50, 0x500aa550, 0xaaaa4444, + 0x66660000, 0xa5a0a5a0, 0x50a050a0, 0x69286928, 0x44aaaa44, 0x66666600, + 0xaa444444, 0x54a854a8, 0x95809580, 0x96969600, 0xa85454a8, 0x80959580, + 0xaa141414, 0x96960000, 0xaaaa1414, 0xa05050a0, 0xa0a5a5a0, 0x96000000, + 0x40804080, 0xa9a8a9a8, 0xaaaaaa44, 0x2a4a5254, +}; +inline constexpr std::uint8_t bc7_anchor2[64] = { + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 2, 8, 2, 2, 8, 8, 15, 2, 8, 2, 2, 8, 8, 2, 2, + 15, 15, 6, 8, 2, 8, 15, 15, 2, 8, 2, 2, 2, 15, 15, 6, + 6, 2, 6, 8, 15, 15, 2, 2, 15, 15, 15, 15, 15, 2, 2, 15, +}; +inline constexpr std::uint8_t bc7_anchor3a[64] = { + 3, 3, 15, 15, 8, 3, 15, 15, 8, 8, 6, 6, 6, 5, 3, 3, + 3, 3, 8, 15, 3, 3, 6, 10, 5, 8, 8, 6, 8, 5, 15, 15, + 8, 15, 3, 5, 6, 10, 8, 15, 15, 3, 15, 5, 15, 15, 15, 15, + 3, 15, 5, 5, 5, 8, 5, 10, 5, 10, 8, 13, 15, 12, 3, 3, +}; +inline constexpr std::uint8_t bc7_anchor3b[64] = { + 15, 8, 8, 3, 15, 15, 3, 8, 15, 15, 15, 15, 15, 15, 15, 8, + 15, 8, 15, 3, 15, 8, 15, 8, 3, 15, 6, 10, 15, 15, 10, 8, + 15, 3, 15, 10, 10, 8, 9, 10, 6, 15, 8, 15, 3, 6, 6, 8, + 15, 3, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 3, 15, 15, 8, +}; + +// BC7 lane-15 signature rank -> (mode, partition, rotation, index_selection); mode 255 = no block. +inline constexpr int bc7_config_count = 286; +inline constexpr std::uint8_t bc7_config[286][4] = { + {7, 63, 0, 0}, {7, 62, 0, 0}, {7, 61, 0, 0}, {7, 60, 0, 0}, {7, 59, 0, 0}, {7, 58, 0, 0}, + {7, 57, 0, 0}, {7, 56, 0, 0}, {7, 55, 0, 0}, {7, 54, 0, 0}, {7, 53, 0, 0}, {7, 52, 0, 0}, + {7, 51, 0, 0}, {7, 50, 0, 0}, {7, 49, 0, 0}, {7, 48, 0, 0}, {7, 47, 0, 0}, {7, 46, 0, 0}, + {7, 45, 0, 0}, {7, 44, 0, 0}, {7, 43, 0, 0}, {7, 42, 0, 0}, {7, 41, 0, 0}, {7, 40, 0, 0}, + {7, 39, 0, 0}, {7, 38, 0, 0}, {7, 37, 0, 0}, {7, 36, 0, 0}, {7, 35, 0, 0}, {7, 34, 0, 0}, + {7, 33, 0, 0}, {7, 32, 0, 0}, {7, 31, 0, 0}, {7, 30, 0, 0}, {7, 29, 0, 0}, {7, 28, 0, 0}, + {7, 27, 0, 0}, {7, 26, 0, 0}, {7, 25, 0, 0}, {7, 24, 0, 0}, {7, 23, 0, 0}, {7, 22, 0, 0}, + {7, 21, 0, 0}, {7, 20, 0, 0}, {7, 19, 0, 0}, {7, 18, 0, 0}, {7, 17, 0, 0}, {7, 16, 0, 0}, + {7, 15, 0, 0}, {7, 14, 0, 0}, {7, 13, 0, 0}, {7, 12, 0, 0}, {7, 11, 0, 0}, {7, 10, 0, 0}, + {7, 9, 0, 0}, {7, 8, 0, 0}, {7, 7, 0, 0}, {7, 6, 0, 0}, {7, 5, 0, 0}, {7, 4, 0, 0}, + {7, 3, 0, 0}, {7, 2, 0, 0}, {7, 1, 0, 0}, {7, 0, 0, 0}, {6, 0, 0, 0}, {5, 0, 3, 0}, + {5, 0, 2, 0}, {5, 0, 1, 0}, {5, 0, 0, 0}, {4, 0, 3, 1}, {4, 0, 3, 0}, {4, 0, 2, 1}, + {4, 0, 2, 0}, {4, 0, 1, 1}, {4, 0, 1, 0}, {4, 0, 0, 1}, {4, 0, 0, 0}, {3, 63, 0, 0}, + {3, 62, 0, 0}, {3, 61, 0, 0}, {3, 60, 0, 0}, {3, 59, 0, 0}, {3, 58, 0, 0}, {3, 57, 0, 0}, + {3, 56, 0, 0}, {3, 55, 0, 0}, {3, 54, 0, 0}, {3, 53, 0, 0}, {3, 52, 0, 0}, {3, 51, 0, 0}, + {3, 50, 0, 0}, {3, 49, 0, 0}, {3, 48, 0, 0}, {3, 47, 0, 0}, {3, 46, 0, 0}, {3, 45, 0, 0}, + {3, 44, 0, 0}, {3, 43, 0, 0}, {3, 42, 0, 0}, {3, 41, 0, 0}, {3, 40, 0, 0}, {3, 39, 0, 0}, + {3, 38, 0, 0}, {3, 37, 0, 0}, {3, 36, 0, 0}, {3, 35, 0, 0}, {3, 34, 0, 0}, {3, 33, 0, 0}, + {3, 32, 0, 0}, {3, 31, 0, 0}, {3, 30, 0, 0}, {3, 29, 0, 0}, {3, 28, 0, 0}, {3, 27, 0, 0}, + {3, 26, 0, 0}, {3, 25, 0, 0}, {3, 24, 0, 0}, {3, 23, 0, 0}, {3, 22, 0, 0}, {3, 21, 0, 0}, + {3, 20, 0, 0}, {3, 19, 0, 0}, {3, 18, 0, 0}, {3, 17, 0, 0}, {3, 16, 0, 0}, {3, 15, 0, 0}, + {3, 14, 0, 0}, {3, 13, 0, 0}, {3, 12, 0, 0}, {3, 11, 0, 0}, {3, 10, 0, 0}, {3, 9, 0, 0}, + {3, 8, 0, 0}, {3, 7, 0, 0}, {3, 6, 0, 0}, {3, 5, 0, 0}, {3, 4, 0, 0}, {3, 3, 0, 0}, + {3, 2, 0, 0}, {3, 1, 0, 0}, {3, 0, 0, 0}, {2, 63, 0, 0}, {2, 59, 0, 0}, {2, 58, 0, 0}, + {2, 57, 0, 0}, {2, 54, 0, 0}, {2, 53, 0, 0}, {2, 52, 0, 0}, {2, 47, 0, 0}, {2, 43, 0, 0}, + {2, 42, 0, 0}, {2, 40, 0, 0}, {2, 38, 0, 0}, {2, 37, 0, 0}, {2, 35, 0, 0}, {2, 34, 0, 0}, + {2, 32, 0, 0}, {2, 23, 0, 0}, {2, 21, 0, 0}, {2, 18, 0, 0}, {2, 17, 0, 0}, {2, 16, 0, 0}, + {2, 11, 0, 0}, {1, 63, 0, 0}, {1, 62, 0, 0}, {1, 61, 0, 0}, {1, 60, 0, 0}, {1, 59, 0, 0}, + {1, 58, 0, 0}, {1, 57, 0, 0}, {1, 56, 0, 0}, {1, 55, 0, 0}, {1, 54, 0, 0}, {1, 53, 0, 0}, + {1, 52, 0, 0}, {1, 51, 0, 0}, {1, 50, 0, 0}, {1, 49, 0, 0}, {1, 48, 0, 0}, {1, 47, 0, 0}, + {1, 46, 0, 0}, {1, 45, 0, 0}, {1, 44, 0, 0}, {1, 43, 0, 0}, {1, 42, 0, 0}, {1, 41, 0, 0}, + {1, 40, 0, 0}, {1, 39, 0, 0}, {1, 38, 0, 0}, {1, 37, 0, 0}, {1, 36, 0, 0}, {1, 35, 0, 0}, + {1, 34, 0, 0}, {1, 33, 0, 0}, {1, 32, 0, 0}, {1, 31, 0, 0}, {1, 30, 0, 0}, {1, 29, 0, 0}, + {1, 28, 0, 0}, {1, 27, 0, 0}, {1, 26, 0, 0}, {1, 25, 0, 0}, {1, 24, 0, 0}, {1, 23, 0, 0}, + {1, 22, 0, 0}, {1, 21, 0, 0}, {1, 20, 0, 0}, {1, 19, 0, 0}, {1, 18, 0, 0}, {1, 17, 0, 0}, + {1, 16, 0, 0}, {1, 15, 0, 0}, {1, 14, 0, 0}, {1, 13, 0, 0}, {1, 12, 0, 0}, {1, 11, 0, 0}, + {1, 10, 0, 0}, {1, 9, 0, 0}, {1, 8, 0, 0}, {1, 7, 0, 0}, {1, 6, 0, 0}, {1, 5, 0, 0}, + {1, 4, 0, 0}, {1, 3, 0, 0}, {1, 2, 0, 0}, {1, 1, 0, 0}, {1, 0, 0, 0}, {0, 15, 0, 0}, + {0, 14, 0, 0}, {0, 13, 0, 0}, {0, 12, 0, 0}, {0, 11, 0, 0}, {0, 10, 0, 0}, {0, 9, 0, 0}, + {0, 8, 0, 0}, {0, 5, 0, 0}, {0, 4, 0, 0}, {0, 3, 0, 0}, {0, 2, 0, 0}, {0, 1, 0, 0}, + {0, 0, 0, 0}, {2, 62, 0, 0}, {2, 61, 0, 0}, {2, 60, 0, 0}, {2, 56, 0, 0}, {2, 55, 0, 0}, + {2, 51, 0, 0}, {2, 50, 0, 0}, {2, 49, 0, 0}, {2, 48, 0, 0}, {2, 46, 0, 0}, {2, 45, 0, 0}, + {2, 44, 0, 0}, {2, 41, 0, 0}, {2, 39, 0, 0}, {2, 36, 0, 0}, {2, 33, 0, 0}, {2, 31, 0, 0}, + {2, 30, 0, 0}, {2, 29, 0, 0}, {2, 28, 0, 0}, {2, 27, 0, 0}, {2, 26, 0, 0}, {2, 25, 0, 0}, + {2, 24, 0, 0}, {2, 22, 0, 0}, {2, 20, 0, 0}, {2, 19, 0, 0}, {2, 15, 0, 0}, {2, 14, 0, 0}, + {2, 13, 0, 0}, {2, 12, 0, 0}, {2, 10, 0, 0}, {2, 9, 0, 0}, {2, 8, 0, 0}, {2, 7, 0, 0}, + {2, 6, 0, 0}, {2, 5, 0, 0}, {2, 4, 0, 0}, {2, 3, 0, 0}, {2, 2, 0, 0}, {2, 1, 0, 0}, + {2, 0, 0, 0}, {0, 7, 0, 0}, {0, 6, 0, 0}, {255, 255, 0, 0}, +}; +inline constexpr std::uint16_t bc7_inferred[10] = {149, 151, 152, 153, 246, 248, 251, 252, 253, 254}; +// Lane-15 symbol pair (a, b) -> signature rank (a * 16 + b), -1 outside the table. +inline constexpr std::int16_t bc7_rank_of[512] = { + 0, 1, 3, 5, 8, 10, 14, 16, 20, 23, 27, 29, 35, 37, 41, 45, + 2, 6, 11, 17, 24, 30, 38, 46, 51, 57, 65, 69, 78, 84, 90, 98, + 4, 12, 21, 31, 42, 52, 62, 70, 81, 91, 103, 108, 115, 122, 130, 135, + 7, 18, 32, 47, 58, 71, 85, 99, 109, 117, 127, 136, 147, 156, 163, 175, + 9, 25, 43, 59, 76, 92, 106, 118, 131, 143, 154, 164, 179, 186, 196, 205, + 13, 33, 53, 72, 93, 110, 123, 137, 150, 165, 181, 190, 202, 212, 223, 234, + 15, 39, 63, 86, 107, 124, 142, 157, 172, 187, 200, 213, 229, 240, 251, 261, + 19, 48, 73, 100, 119, 138, 158, 176, 191, 206, 220, 235, 248, 262, 272, -1, + 22, 54, 82, 111, 132, 151, 173, 192, 210, 224, 242, 254, 269, 282, -1, -1, + 26, 60, 94, 120, 144, 166, 188, 207, 225, 244, 258, 273, -1, -1, -1, -1, + 28, 66, 104, 128, 155, 182, 201, 221, 243, 259, 279, -1, -1, -1, -1, -1, + 34, 74, 112, 139, 167, 193, 214, 236, 255, 274, -1, -1, -1, -1, -1, -1, + 36, 79, 116, 148, 180, 203, 230, 249, 270, -1, -1, -1, -1, -1, -1, -1, + 40, 87, 125, 159, 189, 215, 241, 263, 283, -1, -1, -1, -1, -1, -1, -1, + 44, 95, 133, 168, 197, 226, 252, 275, -1, -1, -1, -1, -1, -1, -1, -1, + 49, 101, 140, 177, 208, 237, 264, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 50, 105, 146, 184, 218, 247, 271, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 55, 113, 152, 194, 227, 256, 284, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 56, 114, 161, 199, 233, 266, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 61, 121, 169, 209, 245, 276, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 64, 126, 174, 216, 253, 285, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 67, 129, 183, 222, 260, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 68, 134, 185, 231, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 75, 141, 195, 238, 277, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 77, 145, 198, 246, 281, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 80, 149, 204, 250, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 83, 153, 211, 257, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 88, 160, 217, 265, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 89, 162, 219, 268, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 96, 170, 228, 278, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 97, 171, 232, 280, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 102, 178, 239, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, +}; + +inline constexpr std::uint8_t bc6h_mode_info[15][8] = { + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 1, 10, 5, 5, 5, 2, 82}, + {1, 1, 7, 6, 6, 6, 2, 82}, + {2, 1, 11, 5, 4, 4, 2, 82}, + {6, 1, 11, 4, 5, 4, 2, 82}, + {10, 1, 11, 4, 4, 5, 2, 82}, + {14, 1, 9, 5, 5, 5, 2, 82}, + {18, 1, 8, 6, 5, 5, 2, 82}, + {22, 1, 8, 5, 6, 5, 2, 82}, + {26, 1, 8, 5, 5, 6, 2, 82}, + {30, 0, 6, 6, 6, 6, 2, 82}, + {3, 0, 10, 10, 10, 10, 1, 65}, + {7, 1, 11, 9, 9, 9, 1, 65}, + {11, 1, 12, 8, 8, 8, 1, 65}, + {15, 1, 16, 4, 4, 4, 1, 65}, +}; +inline constexpr std::uint8_t bc6h_plan_count[15] = {0, 80, 80, 77, 77, 77, 77, 77, 77, 77, 77, 60, 60, 60, 60}; +inline constexpr std::int8_t bc6h_plan[15][80][4] = { + {}, + {{1, 2, 4, 2}, {2, 2, 4, 3}, {2, 3, 4, 4}, {0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {0, 0, 8, 13}, {0, 0, 9, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 0, 8, 23}, {1, 0, 9, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 0, 8, 33}, {2, 0, 9, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {1, 3, 4, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {2, 3, 0, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 3, 1, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {0, 2, 4, 69}, {2, 3, 2, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {0, 3, 4, 75}, {2, 3, 3, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{1, 2, 5, 2}, {1, 3, 4, 3}, {1, 3, 5, 4}, {0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {2, 3, 0, 12}, {2, 3, 1, 13}, {2, 2, 4, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {2, 2, 5, 22}, {2, 3, 2, 23}, {1, 2, 4, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 3, 3, 32}, {2, 3, 5, 33}, {2, 3, 4, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {0, 1, 5, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {1, 1, 5, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 1, 5, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {0, 2, 4, 69}, {0, 2, 5, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {0, 3, 4, 75}, {0, 3, 5, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {0, 0, 8, 13}, {0, 0, 9, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 0, 8, 23}, {1, 0, 9, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 0, 8, 33}, {2, 0, 9, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {0, 0, 10, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 0, 10, 49}, {2, 3, 0, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 0, 10, 59}, {2, 3, 1, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {0, 2, 4, 69}, {2, 3, 2, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {0, 3, 4, 75}, {2, 3, 3, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {0, 0, 8, 13}, {0, 0, 9, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 0, 8, 23}, {1, 0, 9, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 0, 8, 33}, {2, 0, 9, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 0, 10, 39}, {1, 3, 4, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {1, 0, 10, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 0, 10, 59}, {2, 3, 1, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {2, 3, 0, 69}, {2, 3, 2, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {1, 2, 4, 75}, {2, 3, 3, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {0, 0, 8, 13}, {0, 0, 9, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 0, 8, 23}, {1, 0, 9, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 0, 8, 33}, {2, 0, 9, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 0, 10, 39}, {2, 2, 4, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 0, 10, 49}, {2, 3, 0, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 0, 10, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {2, 3, 1, 69}, {2, 3, 2, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {2, 3, 4, 75}, {2, 3, 3, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {0, 0, 8, 13}, {2, 2, 4, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 0, 8, 23}, {1, 2, 4, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 0, 8, 33}, {2, 3, 4, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {1, 3, 4, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {2, 3, 0, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 3, 1, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {0, 2, 4, 69}, {2, 3, 2, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {0, 3, 4, 75}, {2, 3, 3, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {1, 3, 4, 13}, {2, 2, 4, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {2, 3, 2, 23}, {1, 2, 4, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 3, 3, 33}, {2, 3, 4, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {0, 1, 5, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {2, 3, 0, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 3, 1, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {0, 2, 4, 69}, {0, 2, 5, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {0, 3, 4, 75}, {0, 3, 5, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {2, 3, 0, 13}, {2, 2, 4, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 2, 5, 23}, {1, 2, 4, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {1, 3, 5, 33}, {2, 3, 4, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {1, 3, 4, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {1, 1, 5, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 3, 1, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {0, 2, 4, 69}, {2, 3, 2, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {0, 3, 4, 75}, {2, 3, 3, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {2, 3, 1, 13}, {2, 2, 4, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {2, 2, 5, 23}, {1, 2, 4, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 3, 5, 33}, {2, 3, 4, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {1, 3, 4, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {2, 3, 0, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 1, 5, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {0, 2, 4, 69}, {2, 3, 2, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {0, 3, 4, 75}, {2, 3, 3, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {1, 3, 4, 11}, {2, 3, 0, 12}, {2, 3, 1, 13}, {2, 2, 4, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 2, 5, 21}, {2, 2, 5, 22}, {2, 3, 2, 23}, {1, 2, 4, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {1, 3, 5, 31}, {2, 3, 3, 32}, {2, 3, 5, 33}, {2, 3, 4, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {0, 1, 5, 40}, {1, 2, 0, 41}, {1, 2, 1, 42}, {1, 2, 2, 43}, {1, 2, 3, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {1, 1, 5, 50}, {1, 3, 0, 51}, {1, 3, 1, 52}, {1, 3, 2, 53}, {1, 3, 3, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 1, 5, 60}, {2, 2, 0, 61}, {2, 2, 1, 62}, {2, 2, 2, 63}, {2, 2, 3, 64}, {0, 2, 0, 65}, {0, 2, 1, 66}, {0, 2, 2, 67}, {0, 2, 3, 68}, {0, 2, 4, 69}, {0, 2, 5, 70}, {0, 3, 0, 71}, {0, 3, 1, 72}, {0, 3, 2, 73}, {0, 3, 3, 74}, {0, 3, 4, 75}, {0, 3, 5, 76}, {-1, -1, 0, 77}, {-1, -1, 1, 78}, {-1, -1, 2, 79}, {-1, -1, 3, 80}, {-1, -1, 4, 81}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {0, 0, 8, 13}, {0, 0, 9, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 0, 8, 23}, {1, 0, 9, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 0, 8, 33}, {2, 0, 9, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {0, 1, 5, 40}, {0, 1, 6, 41}, {0, 1, 7, 42}, {0, 1, 8, 43}, {0, 1, 9, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {1, 1, 5, 50}, {1, 1, 6, 51}, {1, 1, 7, 52}, {1, 1, 8, 53}, {1, 1, 9, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 1, 5, 60}, {2, 1, 6, 61}, {2, 1, 7, 62}, {2, 1, 8, 63}, {2, 1, 9, 64}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {0, 0, 8, 13}, {0, 0, 9, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 0, 8, 23}, {1, 0, 9, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 0, 8, 33}, {2, 0, 9, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {0, 1, 5, 40}, {0, 1, 6, 41}, {0, 1, 7, 42}, {0, 1, 8, 43}, {0, 0, 10, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {1, 1, 5, 50}, {1, 1, 6, 51}, {1, 1, 7, 52}, {1, 1, 8, 53}, {1, 0, 10, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 1, 5, 60}, {2, 1, 6, 61}, {2, 1, 7, 62}, {2, 1, 8, 63}, {2, 0, 10, 64}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {0, 0, 8, 13}, {0, 0, 9, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 0, 8, 23}, {1, 0, 9, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 0, 8, 33}, {2, 0, 9, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 1, 4, 39}, {0, 1, 5, 40}, {0, 1, 6, 41}, {0, 1, 7, 42}, {0, 0, 11, 43}, {0, 0, 10, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 1, 4, 49}, {1, 1, 5, 50}, {1, 1, 6, 51}, {1, 1, 7, 52}, {1, 0, 11, 53}, {1, 0, 10, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 1, 4, 59}, {2, 1, 5, 60}, {2, 1, 6, 61}, {2, 1, 7, 62}, {2, 0, 11, 63}, {2, 0, 10, 64}}, + {{0, 0, 0, 5}, {0, 0, 1, 6}, {0, 0, 2, 7}, {0, 0, 3, 8}, {0, 0, 4, 9}, {0, 0, 5, 10}, {0, 0, 6, 11}, {0, 0, 7, 12}, {0, 0, 8, 13}, {0, 0, 9, 14}, {1, 0, 0, 15}, {1, 0, 1, 16}, {1, 0, 2, 17}, {1, 0, 3, 18}, {1, 0, 4, 19}, {1, 0, 5, 20}, {1, 0, 6, 21}, {1, 0, 7, 22}, {1, 0, 8, 23}, {1, 0, 9, 24}, {2, 0, 0, 25}, {2, 0, 1, 26}, {2, 0, 2, 27}, {2, 0, 3, 28}, {2, 0, 4, 29}, {2, 0, 5, 30}, {2, 0, 6, 31}, {2, 0, 7, 32}, {2, 0, 8, 33}, {2, 0, 9, 34}, {0, 1, 0, 35}, {0, 1, 1, 36}, {0, 1, 2, 37}, {0, 1, 3, 38}, {0, 0, 15, 39}, {0, 0, 14, 40}, {0, 0, 13, 41}, {0, 0, 12, 42}, {0, 0, 11, 43}, {0, 0, 10, 44}, {1, 1, 0, 45}, {1, 1, 1, 46}, {1, 1, 2, 47}, {1, 1, 3, 48}, {1, 0, 15, 49}, {1, 0, 14, 50}, {1, 0, 13, 51}, {1, 0, 12, 52}, {1, 0, 11, 53}, {1, 0, 10, 54}, {2, 1, 0, 55}, {2, 1, 1, 56}, {2, 1, 2, 57}, {2, 1, 3, 58}, {2, 0, 15, 59}, {2, 0, 14, 60}, {2, 0, 13, 61}, {2, 0, 12, 62}, {2, 0, 11, 63}, {2, 0, 10, 64}}, +}; diff --git a/runtime/test/bcpack_test.cpp b/runtime/test/bcpack_test.cpp new file mode 100644 index 0000000000..05b400a0b1 --- /dev/null +++ b/runtime/test/bcpack_test.cpp @@ -0,0 +1,133 @@ +// Native BCPack decoder (runtime/src/bcpack.cpp) checks. +// +// bcpack_test built-in checks that need nothing else +// bcpack_test --batch decode every chunk of a batch file for tests/test_bcpack_native.py +// +// Batch input, repeated to the end of the file (little-endian): +// u32 codec, u32 out_size, u8 allow_provisional, u32 raw_size, raw_size bytes +// Batch output, one record per input chunk: +// u8 status, u32 message size, message, u32 label count, (u32 size, label) per label, u32 out size, out bytes +// (out bytes only when the status is ok). + +#include "../src/bcpack.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace bcpack = leonida::bcpack; + +#define CHECK(condition) \ + do { \ + if (!(condition)) { \ + std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ + return 1; \ + } \ + } while (0) + +namespace { + +int self_test() { + std::vector out; + + // An empty chunk decodes to nothing for every codec; an unknown codec has no rebuilder. + for (int codec : {1, 3, 4, 5, 6, 7}) CHECK(bcpack::decode({}, codec, out)); + CHECK(bcpack::decode({}, 2, out).status == bcpack::Status::unsupported); + CHECK(bcpack::block_bytes(1) == 8 && bcpack::block_bytes(7) == 16 && bcpack::block_bytes(2) == 0); + + // A size that is not a whole number of blocks, and input that runs out, are malformed. + out.assign(12, 0); + auto r = bcpack::decode({}, 1, out); + CHECK(r.status == bcpack::Status::error && r.message.find("whole number of BC1 blocks") != std::string::npos); + out.assign(8, 0); + r = bcpack::decode({}, 1, out); + CHECK(r.status == bcpack::Status::error && r.message.find("input ran out") != std::string::npos); + + // Leftover non-zero bytes after an empty chunk are refused; zero padding is not. + const std::uint8_t pad[3] = {0, 0, 0}, junk[3] = {0, 1, 0}; + out.clear(); + CHECK(bcpack::decode(pad, 4, out)); + CHECK(bcpack::decode(junk, 4, out).status == bcpack::Status::error); + return 0; +} + +std::uint32_t read_u32(const std::vector& in, std::size_t& at) { + std::uint32_t v = 0; + for (int k = 0; k < 4; ++k) v |= static_cast(in.at(at + k)) << (8 * k); + at += 4; + return v; +} + +void write_u32(std::string& out, std::uint32_t v) { + for (int k = 0; k < 4; ++k) out.push_back(static_cast((v >> (8 * k)) & 0xFF)); +} + +void write_blob(std::string& out, const void* data, std::size_t size) { + write_u32(out, static_cast(size)); + out.append(static_cast(data), size); +} + +// Paths go through std::filesystem::path so that, with wmain on Windows, a temporary folder outside the ANSI code +// page (a user name in another script) still opens. +int batch(const std::filesystem::path& in_path, const std::filesystem::path& out_path) { + std::ifstream in_file(in_path, std::ios::binary); + if (!in_file) { + std::fprintf(stderr, "cannot read the batch input\n"); + return 2; + } + const std::vector in((std::istreambuf_iterator(in_file)), std::istreambuf_iterator()); + std::string out; + std::size_t at = 0; + while (at < in.size()) { + const int codec = static_cast(read_u32(in, at)); + const std::uint32_t out_size = read_u32(in, at); + const bool allow_provisional = in.at(at++) != 0; + const std::uint32_t raw_size = read_u32(in, at); + if (at + raw_size > in.size()) { + std::fprintf(stderr, "truncated batch file\n"); + return 2; + } + const std::span raw(in.data() + at, raw_size); + at += raw_size; + std::vector decoded(out_size); + const auto result = bcpack::decode(raw, codec, decoded, allow_provisional); + out.push_back(static_cast(result.status)); + write_blob(out, result.message.data(), result.message.size()); + write_u32(out, static_cast(result.provisional.size())); + for (const auto& label : result.provisional) write_blob(out, label.data(), label.size()); + if (result) + write_blob(out, decoded.data(), decoded.size()); + else + write_u32(out, 0); + } + std::ofstream out_file(out_path, std::ios::binary); + out_file.write(out.data(), static_cast(out.size())); + if (!out_file) { + std::fprintf(stderr, "cannot write the batch output\n"); + return 2; + } + return 0; +} + +} // namespace + +#ifdef _WIN32 +int wmain(int argc, wchar_t** argv) { + if (argc == 4 && std::wcscmp(argv[1], L"--batch") == 0) return batch(argv[2], argv[3]); +#else +int main(int argc, char** argv) { + if (argc == 4 && std::strcmp(argv[1], "--batch") == 0) return batch(argv[2], argv[3]); +#endif + if (argc != 1) { + std::fprintf(stderr, "usage: bcpack_test [--batch ]\n"); + return 2; + } + const int failed = self_test(); + if (!failed) std::puts("bcpack_test: ok"); + return failed; +} diff --git a/tests/test_bcpack_native.py b/tests/test_bcpack_native.py new file mode 100644 index 0000000000..a0ab086ca4 --- /dev/null +++ b/tests/test_bcpack_native.py @@ -0,0 +1,350 @@ +"""Compare the native BCPack decoder (runtime/src/bcpack.cpp) with tools/bcpack.py on synthetic chunks. + +Random lane symbols for every codec are encoded with the test encoder of tests/test_bcpack.py (single and +multi-fragment lanes, narrowed widths, every q), some of them corrupted, and every chunk is decoded by both +decoders in strict and provisional mode. The status, message, provisional labels and decoded bytes must match. +No game files are used. Also checks that runtime/src/bcpack_tables.inc is current (tools/gen_bcpack_tables.py). + + python -I tests/test_bcpack_native.py [--bin ] [--seed N] [--chunks N] + +Without --bin it looks for bcpack_test under runtime/build and skips the comparison when there is none +(ctest passes --bin). +""" +import glob +import os +import random +import struct +import subprocess +import sys +import tempfile +import unittest + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.join(ROOT, "tools")) +sys.path.insert(0, os.path.join(ROOT, "tests")) + +import bcpack # noqa: E402 +import bcpack_tables_bptc as bptc # noqa: E402 +import gen_bcpack_tables # noqa: E402 +import test_bcpack as enc # noqa: E402 + +OPTIONS = {"bin": None, "seed": 186, "chunks": 24} + +STATUS_OK, STATUS_ERROR, STATUS_PROVISIONAL, STATUS_UNSUPPORTED = 0, 1, 2, 3 + + +def find_binary(): + if OPTIONS["bin"]: + return OPTIONS["bin"] + env = os.environ.get("LEONIDA_BCPACK_TEST") + if env: + return env + for name in ("bcpack_test", "bcpack_test.exe"): + hits = sorted(glob.glob(os.path.join(ROOT, "runtime", "build", "**", name), recursive=True)) + if hits: + return hits[0] + return None + + +# --- Synthetic chunks ------------------------------------------------------------------------------------------------ + +def _encodable(model, syms): + """Whether the test encoder can encode every symbol with `model` (a non-zero band frequency after the c == 0 + adjustment).""" + for s in set(syms): + p = s.bit_length() + if p > model.w: + return False + f = model.freq[p] + c = model.base[p] + f * (s - (0 if p == 0 else 1 << (p - 1))) + if f == 0 or (c == 0 and f <= 1): + return False + return True + + +def _pick_q(rng, w, syms): + qs = [q for q in range(16) if _encodable(bcpack._get_model(w, q), syms)] + return rng.choice(qs) if qs else None + + +def _lane_stream(rng, syms, dw): + """One lane's byte stream: one flush fragment, or several with narrowed widths and non-flush boundaries.""" + if len(syms) >= 4 and rng.random() < 0.5: + # The test encoder handles only some fragment chains, so keep a chain only when it decodes back on its own. + try: + stream = _multi_fragment_stream(rng, syms, dw) + lane = enc.interleave_lanes([stream], (dw,), len(syms)) + if bcpack._decode_lanes_raw(memoryview(lane), (dw,), len(syms)) == ([syms], len(lane)): + return stream + except (AssertionError, IndexError, bcpack.BcpackError): + pass + q = _pick_q(rng, dw, syms) + assert q is not None, f"no q encodes width {dw}" + return enc._encode_lane_stream([(syms, dw, q, True)], dw) + + +def _multi_fragment_stream(rng, syms, dw): + cuts = sorted(rng.sample(range(1, len(syms)), min(len(syms) - 1, rng.randint(1, 3)))) + pieces = [syms[a:b] for a, b in zip([0] + cuts, cuts + [len(syms)])] + fragments = [] + for k, piece in enumerate(pieces): + top = max(piece).bit_length() + w = rng.choice([w for w in range(max(1, top, dw - 3), dw + 1)]) + q = _pick_q(rng, w, piece) + if q is None: + w = dw + q = _pick_q(rng, w, piece) + flush = k == len(pieces) - 1 or rng.random() < 0.5 + fragments.append((piece, w, q, flush)) + return enc._encode_lane_stream(fragments, dw) + + +def encode(rng, lanes, codec): + widths = bcpack.LANE_WIDTHS[codec] + streams = [_lane_stream(rng, syms, dw) for syms, dw in zip(lanes, widths)] + return enc.interleave_lanes(streams, widths, len(lanes[0])) + + +def _rand_lane(rng, n, width): + style = rng.random() + if style < 0.25: + return [0] * n + if style < 0.5: # small deltas around zero (zigzag values) + return [min((1 << width) - 1, rng.choice((0, 0, 1, 2, 3, 4))) for _ in range(n)] + return [rng.randrange(1 << width) for _ in range(n)] + + +def _distance_symbols(rng, i, far, clean=False): + """A back-reference distance for block i: mostly inside the chunk, sometimes into the built-in history.""" + if far or (not clean and rng.random() < 0.15): + return rng.randrange(256) + return 0 if i == 0 or rng.random() < 0.3 else rng.randint(1, min(i, 255)) + + +VALID_BC7_CELLS = None + + +def _bc7_cells(): + global VALID_BC7_CELLS + if VALID_BC7_CELLS is None: + table = bptc.load_bc7(None) + cfg, rank_of = table["config"], table["rank_of"] + VALID_BC7_CELLS = [ + (a, b) for a in range(32) for b in range(16) + if rank_of[a * 16 + b] >= 0 and cfg[rank_of[a * 16 + b]][0] <= 7 and cfg[rank_of[a * 16 + b]][1] != 255 + ] + return VALID_BC7_CELLS + + +def make_lanes(rng, codec, nblocks, clean=None, bc7_cells=None): + widths = bcpack.LANE_WIDTHS[codec] + per_block = 1 if codec in (1, 4) else 2 + n = nblocks * per_block + lanes = [_rand_lane(rng, n, w) for w in widths] + # A clean chunk stays inside itself and keeps BC6H/BC7 anchor indices and BC7 p-bits consistent, so it + # decodes; the others mostly exercise refusals. + if clean is None: + clean = rng.random() < 0.5 + far = not clean and rng.random() < 0.2 + if codec in (1, 3, 4, 5) and (clean or rng.random() < 0.9): + lanes[1] = [0] * n + if clean and codec in (6, 7): + for lane in (2, 6, 7, 8, 10, 11, 12, 13, 14) if codec == 7 else (11, 12, 13, 14): + lanes[lane] = [0] * n + if codec in (1, 4): + for i in range(nblocks): + d = _distance_symbols(rng, i, far, clean) + lanes[0][i] = enc._zig(d if d < 128 else d - 256) + elif codec in (3, 5): + for i in range(nblocks): + d = _distance_symbols(rng, i, far, clean) + lanes[0][2 * i], lanes[0][2 * i + 1] = enc._deint4(enc._zig(d if d < 128 else d - 256) & 255) + else: + for i in range(nblocks): + di = _distance_symbols(rng, i, far, clean) + de = _distance_symbols(rng, i, far, clean) + delta = (de - di) & 255 + delta = delta - 256 if delta >= 128 else delta + lanes[1][2 * i], lanes[1][2 * i + 1] = enc._deint4(di) + lanes[0][2 * i], lanes[0][2 * i + 1] = enc._deint4(enc._zig(delta) & 255) + if codec == 6: + for i in range(nblocks): + lanes[15][2 * i] = rng.randrange(14) if rng.random() < 0.97 else rng.randrange(32) + lanes[15][2 * i + 1] = rng.randrange(32) + else: + cells = bc7_cells or _bc7_cells() + for i in range(nblocks): + if bc7_cells or rng.random() < 0.97: + lanes[15][2 * i], lanes[15][2 * i + 1] = rng.choice(cells) + else: + lanes[15][2 * i], lanes[15][2 * i + 1] = rng.randrange(32), rng.randrange(32) + return lanes + + +def corrupt(rng, raw): + kind = rng.randrange(5) + data = bytearray(raw) + if kind == 0 and data: + data[rng.randrange(len(data))] ^= 1 << rng.randrange(8) + elif kind == 1 and data: + del data[rng.randrange(len(data)):] + elif kind == 2: + data += bytes([rng.randint(1, 255)]) + elif kind == 3: + data += bytes(rng.randint(1, 9)) + else: + data = bytearray(rng.randrange(256) for _ in range(rng.randint(1, 64))) + return bytes(data) + + +# The BC7 (mode, rotation) pairs with a channel swap: modes 4 and 5, rotation 1-3. +BC7_ROTATIONS = [(mode, rot) for mode in (4, 5) for rot in (1, 2, 3)] + + +def bc7_rotation_cases(): + """Fixed BC7 chunks, one per rotated (mode, rotation) pair, that every block uses and Python decodes. + + The seeded chunks reach rotated blocks only by chance (one chunk at the default seed), so a broken channel swap + could pass; these make it a deterministic check whatever --seed is. + """ + table = bptc.load_bc7(None) + cfg, rank_of = table["config"], table["rank_of"] + rng = random.Random(7186) + cases = [] + for mode, rot in BC7_ROTATIONS: + cells = [(a, b) for a, b in _bc7_cells() if cfg[rank_of[a * 16 + b]][0] == mode + and cfg[rank_of[a * 16 + b]][2] == rot] + for _ in range(200): + nblocks = rng.choice((4, 16, 33)) + lanes = make_lanes(rng, 7, nblocks, clean=True, bc7_cells=cells) + # A swap of two all-zero channels changes nothing, so every endpoint lane (R, G, B, A: lanes 2-5 for + # one subset) gets full-range deltas. Modes 4 and 5 have no p-bits, so the chunk stays clean. + for lane in (2, 3, 4, 5): + lanes[lane] = [rng.randrange(1 << bcpack.LANE_WIDTHS[7][lane]) for _ in range(2 * nblocks)] + raw = encode(rng, lanes, 7) + out = python_decode(7, nblocks * 16, raw, False) + if out[0] == STATUS_OK: + cases.append((7, nblocks * 16, raw)) + break + else: + raise AssertionError(f"no decodable BC7 mode {mode} rotation {rot} chunk; widen the generator") + return cases + + +def make_cases(seed, chunks): + """(codec, out_size, raw) chunks: valid ones, corrupted ones, wrong sizes and unsupported codecs.""" + rng = random.Random(seed) + cases = [] + for codec in (1, 3, 4, 5, 6, 7): + bb = bcpack.BLOCK_BYTES[codec] + for k in range(chunks): + nblocks = rng.choice((1, 2, 3, 5, 16, 33, 64, 130, 257, 300)) + raw = encode(rng, make_lanes(rng, codec, nblocks), codec) + cases.append((codec, nblocks * bb, raw)) + if k % 2 == 0: + cases.append((codec, nblocks * bb, corrupt(rng, raw))) + if k % 6 == 0: + cases.append((codec, nblocks * bb + rng.choice((-bb, bb, bb // 2)), raw)) + cases += [(2, 16, b""), (0, 8, b"\x00"), (8, 16, b"")] + return cases + bc7_rotation_cases() + + +# --- Both decoders ---------------------------------------------------------------------------------------------------- + +def python_decode(codec, out_size, raw, allow_provisional): + try: + data, prov = bcpack.decode_with_info(raw, codec, out_size, allow_provisional=allow_provisional) + return STATUS_OK, "", sorted(prov), data + except bcpack.BcpackUnsupportedCodec as e: + return STATUS_UNSUPPORTED, str(e), [], b"" + except bcpack.BcpackProvisionalError as e: + return STATUS_PROVISIONAL, str(e), [], b"" + except bcpack.BcpackError as e: + return STATUS_ERROR, str(e), [], b"" + + +def native_decode_all(binary, records): + with tempfile.TemporaryDirectory() as tmp: + src, dst = os.path.join(tmp, "in.bin"), os.path.join(tmp, "out.bin") + with open(src, "wb") as f: + for codec, out_size, raw, allow in records: + f.write(struct.pack("_history_state[256]` (0 none, 1 confirmed, 2 provisional) and `_history[256][ncols]`. + + A provisional entry that is also confirmed is dropped, as `bcpack._resolve_bc_history` does.""" + state = [0] * 256 + vals = [(0,) * ncols] * 256 + for k, v in provisional.items(): + if k not in confirmed: + state[k], vals[k] = 2, tuple(v) + for k, v in confirmed.items(): + state[k], vals[k] = 1, tuple(v) + for v in vals: + assert len(v) == ncols + body = "\n".join(" {" + ", ".join(str(x) for x in v) + "}," for v in vals) + return ( + f"inline constexpr std::uint8_t {name}_history_state[256] = {{\n{_rows(state, 32)}\n}};\n" + f"inline constexpr std::uint8_t {name}_history[256][{ncols}] = {{\n{body}\n}};\n" + ) + + +def _rank_of(): + cells = sorted(((a, b) for a in range(32) for b in range(16)), key=lambda ab: ((ab[0] + 1) * (ab[1] + 1), ab[0])) + rank_of = [-1] * 512 + for rank, (a, b) in enumerate(cells): + if rank < len(_bptc.BC7_CONFIG): + rank_of[a * 16 + b] = rank + return rank_of + + +def generate(): + out = [ + "// Generated by tools/gen_bcpack_tables.py from tools/bcpack_tables.py and tools/bcpack_tables_bptc.py.", + "// Do not edit by hand: rerun the generator (tests/test_bcpack_native.py checks it is current).", + "", + ] + + # rANS models: prob_freq[w - 1][q][band], band 0..w; unused bands are 0. + out.append("// (symbol_width, q) -> per-band rANS frequencies; band p covers symbols [2^(p-1), 2^p).") + out.append("inline constexpr std::uint16_t prob_freq[10][16][11] = {") + for w in range(1, 11): + out.append(" {") + for q in range(16): + freq = _tables.PROB_TABLES[(w, q)] + assert len(freq) == w + 1 and all(0 <= f <= 0xFFFF for f in freq) + row = list(freq) + [0] * (11 - len(freq)) + out.append(" {" + ", ".join(str(f) for f in row) + "},") + out.append(" },") + out.append("};") + out.append("") + + for name, table in (("bc1_quadrant", _tables.BC1_QUADRANT), ("bc3_colour_quadrant", _tables.BC3_COLOUR_QUADRANT)): + assert len(table) == 256 + out.append(f"inline constexpr std::uint8_t {name}[256] = {{\n{_rows(table, 16)}\n}};") + for name, pairs in (("bc3_alpha_pair", _tables.BC3_ALPHA_PAIR), ("bc4_pair", _tables.BC4_PAIR), + ("bc5_pair", _tables.BC5_PAIR)): + assert len(pairs) == 64 and all(len(p) == 2 for p in pairs) + body = _rows([f"{{{p[0]}, {p[1]}}}" for p in pairs], 8) + out.append(f"inline constexpr std::uint8_t {name}[64][2] = {{\n{body}\n}};") + out.append("") + + out.append(_history("bc1", _tables.BC1_HISTORY_CONFIRMED, _tables.BC1_HISTORY_PROVISIONAL, 6)) + out.append(_history("bc3", _tables.BC3_HISTORY_CONFIRMED, _tables.BC3_HISTORY_PROVISIONAL, 8)) + out.append(_history("bc4", _tables.BC4_HISTORY_CONFIRMED, _tables.BC4_HISTORY_PROVISIONAL, 2)) + out.append(_history("bc5", _tables.BC5_HISTORY_CONFIRMED, _tables.BC5_HISTORY_PROVISIONAL, 4)) + + hexfmt = lambda v: f"0x{v:x}" # noqa: E731 + out.append(f"inline constexpr std::uint16_t bc7_part2_packed[64] = {{\n{_rows(_bptc.BC7_PART2_PACKED, 8, hexfmt)}\n}};") + out.append(f"inline constexpr std::uint32_t bc7_part3_packed[64] = {{\n{_rows(_bptc.BC7_PART3_PACKED, 6, hexfmt)}\n}};") + for name, table in (("bc7_anchor2", _bptc.BC7_ANCHOR2), ("bc7_anchor3a", _bptc.BC7_ANCHOR3A), + ("bc7_anchor3b", _bptc.BC7_ANCHOR3B)): + assert len(table) == 64 + out.append(f"inline constexpr std::uint8_t {name}[64] = {{\n{_rows(table, 16)}\n}};") + out.append("") + + cfg = _bptc.BC7_CONFIG + out.append("// BC7 lane-15 signature rank -> (mode, partition, rotation, index_selection); mode 255 = no block.") + out.append(f"inline constexpr int bc7_config_count = {len(cfg)};") + body = _rows([f"{{{m}, {p}, {r}, {s}}}" for m, p, r, s in cfg], 6) + out.append(f"inline constexpr std::uint8_t bc7_config[{len(cfg)}][4] = {{\n{body}\n}};") + inferred = sorted(_bptc.BC7_CONFIG_INFERRED) + out.append(f"inline constexpr std::uint16_t bc7_inferred[{len(inferred)}] = {{{', '.join(map(str, inferred))}}};") + out.append("// Lane-15 symbol pair (a, b) -> signature rank (a * 16 + b), -1 outside the table.") + out.append(f"inline constexpr std::int16_t bc7_rank_of[512] = {{\n{_rows(_rank_of(), 16)}\n}};") + out.append("") + + # BC6H modes 1..14 (mode = lane-15 symbol + 1; row 0 unused): (code, transformed, base_bits, dr, dg, db, + # regions, header_bits), and the header bit plan of `bcpack._bc6h_plan`: (channel, endpoint slot, bit, stream + # position), channel -1 for the partition field. + info_rows, plan_counts, plan_rows = ["{0, 0, 0, 0, 0, 0, 0, 0}"], [0], ["{}"] + max_items = 0 + for mode in range(1, 15): + code = _bcpack._BC6H_CODE[mode] + trans, base, deltas, regions = _bcpack._BC6H_INFO[code] + plan, header = _bcpack._bc6h_plan(code) + max_items = max(max_items, len(plan)) + info_rows.append(f"{{{code}, {trans}, {base}, {deltas[0]}, {deltas[1]}, {deltas[2]}, {regions}, {header}}}") + plan_counts.append(len(plan)) + plan_rows.append("{" + ", ".join(f"{{{c}, {q}, {b}, {p}}}" for c, q, b, p in plan) + "}") + out.append(f"inline constexpr std::uint8_t bc6h_mode_info[15][8] = {{\n{_rows(info_rows, 1)}\n}};") + out.append(f"inline constexpr std::uint8_t bc6h_plan_count[15] = {{{', '.join(map(str, plan_counts))}}};") + out.append(f"inline constexpr std::int8_t bc6h_plan[15][{max_items}][4] = {{\n{_rows(plan_rows, 1)}\n}};") + out.append("") + return "\n".join(out) + + +def main(argv): + text = generate() + if "--check" in argv: + try: + with open(OUT, encoding="utf-8", newline="") as f: + current = f.read() + except OSError: + current = None + if current != text: + print(f"{OUT} is out of date: run python -I tools/gen_bcpack_tables.py", file=sys.stderr) + return 1 + return 0 + with open(OUT, "w", encoding="utf-8", newline="\n") as f: + f.write(text) + print(f"wrote {OUT}") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:]))