code wiki / (root) / nx_zstd_huf.nx

nx_zstd_huf.nx

buildroot/runtime/nx_zstd_huf.nx

5607 B161 linesdepth 3pulls 3 transitivereach 5 importersview sourcekind librarytopic zstd
docsdependenciesstructsconstsfunctions

about

nx_zstd_huf.nx -- Zstandard's Huffman decode table (the X1 flat form). The literals section's engine. zstd does not walk a Huffman tree bit by bit; it builds a FLAT table of 2^tableLog entries where each symbol occupies a contiguous run, peeks tableLog bits to index it in one step, then consumes only the code's true length. That is why the reader needs peek and skip as separate operations. SLOT COUNT IS DRIVEN BY WEIGHT, NOT LENGTH. A symbol of weight w occupies 2^(w-1) slots, which is the same as 2^(tableLog - length) since length = tableLog + 1 - w. Both forms appear in the literature and they are equal only when the table log is right -- so a table built with the wrong log fills a plausible number of slots and still fails to tile the table. The build REFUSES unless the slots sum to exactly 2^tableLog. RANKS ARE LAID OUT BY DESCENDING WEIGHT. Heaviest symbols (shortest codes) take the lowest table indices. Laying them out in symbol order instead produces a table that decodes self-consistently and disagrees with every other zstd implementation -- the failure mode is a file that only this decoder can read. genealogy_id: zstandard_rfc8878_huffman lineage_id: nx_zstd_huf_v1 license_tier: ORIGINAL

dependencies 2 imports · 5 importers

nx_syscalls.nx nx_zstd_bits.nx nx_zstd_huf.nx nx_zstd_block.nx nx_zstd_block_gate.nx nx_zstd_huf_gate.nx nx_zstd_lit.nx nx_zstd_lit_gate.nx

imports: nx_syscalls.nxnx_zstd_bits.nx

imported by: nx_zstd_block.nxnx_zstd_block_gate.nxnx_zstd_huf_gate.nxnx_zstd_lit.nxnx_zstd_lit_gate.nx

structs

33struct NxZstdHuf

consts

29const NX_HUF_MAX_LOG: i64 = 12
30const NX_HUF_MAX_SYMBOLS: i64 = 256
31const NX_HUF_TBL_BYTES: i64 = 40

functions

47func nx_zstd_huf_build(weights: *i64, count: i64, table_log: i64) -> *NxZstdHuf
called by 3: nx_zstd_block_treemainmain calls 1: sys_mmap
131func nx_zstd_huf_symbol_at(t: *NxZstdHuf, idx: i64) -> i64
139func nx_zstd_huf_nbits_at(t: *NxZstdHuf, idx: i64) -> i64
151func nx_zstd_huf_decode(t: *NxZstdHuf, b: *NxZstdBits) -> i64