Unescape HTML · Zig 🤝 GLM-5.3

SHA-256 53c3fa4fe994b3b307c5ad8019f6af961a21116d70eed288571d9aa6f162ef31

View source (52.7 KiB of Zig)
// unescape_html in Zig with Wasm SIMD: decodes every HTML character reference in UTF-8 text
// the way Python's html.unescape does, and copies all other bytes unchanged.
//
// The design goal is a short path per reference with few unpredictable branches:
// - 64 zero bytes are written after the input, so no load or loop needs a bounds check and
//   nothing stale from an earlier input can be mistaken for part of a reference.
// - Two stages per 2 KiB window. Stage 1 turns each 64-byte block into a bitmask of its `&`
//   and appends their positions to a list, without branching on what it finds. Stage 2
//   walks the list: copy the text since the last reference (32 bytes unconditionally, more
//   in a loop), then decode. The next position is known before the current reference
//   is decoded, so consecutive references do not wait on each other.
// - Inputs without `&` are returned in place; nothing is copied.
// - A name of up to 7 bytes ending in `;` is looked up without measuring it: the first `;`
//   in the next 8 bytes is found with a SWAR zero-byte test, the bytes up to it are the key,
//   and one multiply indexes an open-addressed table of 16-byte entries (key, value, length).
//   A hit proves the name; legacy and common names sit in their home slot.
// - Longer names go through a second table and one masked 32-byte vector compare.
// - Without `;` only the 106 legacy names match. A filter on the first two bytes gives the
//   longest legacy name that could start there, usually none.
// - Numeric references of up to 7 digits, decimal or hexadecimal, share one branch-free
//   SWAR path, and the code point is encoded as UTF-8 without branching on its length.
// - Output and input share one buffer (as in the leading C and Nim entries): the output is
//   written from the buffer's start and the input sits IN_OFF bytes in. A reference grows
//   by at most a fifth (`≫⃒` is 5 bytes in, 6 out), so the write position always stays
//   at least 64 bytes behind the next unread byte and decoding in place is safe. The
//   buffer holds INPUT_CAP bytes of input, the most the official suite and benchmark send.
//   The output region is larger than the output needs; before the first render it is the
//   unpacker's working memory (see `init`).
// - The lookup tables are built on the first call by range-decoding a 6.5 KB packed entity
//   list (see `sym` and `init`, and tools/gen_entities.py, the matching packer): entities
//   in code-point order, each name coded character by character with an adaptive binary
//   range coder whose per-bit probability is the logistic mix of an order-1, an order-2
//   and a match model, as in the PAQ compressors. The model lives in the buffer's start,
//   which is still zero on the first call.
//
// Build (or just run `zig build` / `zig build submit` in this repo):
//   zig build-exe this-file.zig -target wasm32-freestanding
//     -mcpu=generic+simd128+bulk_memory -O ReleaseSmall -fno-entry -rdynamic -fstrip
//     --stack 1024 --initial-memory=262144 --max-memory=262144
//   then: wasm-opt -O3 --enable-simd --enable-bulk-memory
const std = @import("std");

const V = @Vector(16, u8);
// The most the official suite and benchmark send; the player refuses larger inputs.
const INPUT_CAP: usize = 104_000;
// One buffer: the output is written from its start and the input sits IN_OFF bytes in, so
// the output follows the input through the same memory (see the header comment). Before
// the first render the buffer's start is the unpacker's working memory, laid out as:
//   model (2^MODEL_BITS u16), squash (4095 u16), stretch (4096 i16), hist (HIST_SIZE u8).
const MODEL_BYTES = (1 << MODEL_BITS) * 2;
const SQUASH_OFF = MODEL_BYTES;
const STRETCH_OFF = SQUASH_OFF + 4095 * 2;
const HIST_OFF = STRETCH_OFF + 4096 * 2;
// Where the input starts: past the unpacker's memory (the input is written before the
// first render unpacks) and past the largest growth (a fifth plus 64 bytes), rounded up
// to a multiple of 64.
const IN_OFF: usize = (@max(HIST_OFF + HIST_SIZE, INPUT_CAP / 5 + 64) + 63) / 64 * 64;
const OUTPUT_CAP: usize = INPUT_CAP / 5 * 6 + 8;
const PAD = 128;
comptime {
    if (IN_OFF % 64 != 0) @compileError("IN_OFF must be a multiple of 64");
    if (IN_OFF < INPUT_CAP / 5 + 64) @compileError("the output must stay behind the input");
}

const FAST_BITS = 12;
const FAST_TAIL = 32;
// Names of at most this many bytes (plus their ';') live in the fast table; longer
// names go to the long-name table. All legacy (semicolon-less) names fit here too.
const FAST_MAXLEN = 7;
const LEG_BITS = 8;
const LEG_TAIL = 16;
const LONG_BITS = 11;
const LONG_MASK = (1 << LONG_BITS) - 1;
const EMPTY: u64 = ~@as(u64, 0);
// Bytes of input per stage-1 window; a multiple of 64. The window's position list is also
// the unpacker's `last` table on the first call (as in the 4-page C entry).
const WINDOW = 2048;
const ONES: u64 = 0x0101010101010101;
const HIGH: u64 = 0x8080808080808080;

// The multiplier was searched offline (see the repo's tools/) so that the common
// entity names sit in their home slot even at the smaller table's 72% load.
const FAST_MUL: u64 = 0xa59d054b12f5b905;
const LEG_MUL: u64 = 0xf880d97d1854e551;
const LONG_MUL: u64 = 0xa0c2995f40498cb3;
const ENTITY_COUNT = 2125;
const LONG16_COUNT = 402;
const LONG32_COUNT = 104;
const packed_list = [6861]u8{
    222,225,76,127,180,18,170,54,188,219,134,11,44,149,95,69,181,148,105,202,75,7,123,143,
    210,236,36,131,238,38,116,134,123,21,163,145,50,228,10,188,228,117,190,56,213,189,195,159,
    232,137,95,219,202,251,132,215,105,107,169,17,124,67,61,239,174,159,226,127,200,90,32,0,
    26,123,198,170,144,101,241,181,241,219,248,183,234,27,127,9,33,110,220,136,25,160,248,75,
    164,76,238,166,174,138,35,46,124,224,58,103,154,237,242,130,14,100,172,18,3,72,241,140,
    254,141,32,18,164,2,57,199,220,1,39,165,170,40,16,84,55,179,102,60,223,119,123,95,
    238,4,137,58,161,247,97,24,208,223,203,247,204,118,241,140,3,229,162,163,26,95,121,3,
    195,143,194,20,213,246,94,85,168,1,66,156,131,245,236,197,99,15,250,108,3,214,203,91,
    84,228,128,23,90,124,101,146,3,14,178,106,95,29,49,145,119,166,124,35,167,3,148,12,
    224,130,193,44,95,75,95,2,39,190,181,238,150,163,152,193,98,96,190,216,30,115,46,148,
    75,121,227,158,243,84,187,155,221,224,74,6,194,72,104,85,8,97,218,142,82,102,25,64,
    89,116,126,112,8,235,33,215,227,17,1,152,228,149,181,189,215,182,90,186,55,64,190,243,
    138,85,6,234,21,127,20,178,4,19,232,6,164,88,76,29,142,199,82,14,229,243,166,168,
    148,140,173,78,208,128,171,156,216,105,243,36,172,33,1,43,203,210,221,105,72,86,175,142,
    121,93,218,90,29,202,140,165,222,56,120,17,217,6,191,160,138,6,124,11,226,166,79,126,
    199,30,151,66,228,204,84,35,7,111,16,176,3,167,238,197,170,113,163,251,117,135,143,114,
    224,60,50,83,254,43,174,89,209,241,120,156,243,253,76,78,243,232,246,202,135,187,121,23,
    219,243,194,155,174,14,8,62,141,173,176,251,92,62,145,128,180,184,198,173,79,171,95,124,
    122,93,164,160,182,180,64,219,167,77,244,29,171,132,27,204,67,167,194,112,59,123,88,162,
    71,60,134,200,90,136,206,143,13,236,252,58,169,242,32,143,10,117,130,52,250,120,236,34,
    231,232,115,180,78,182,168,46,231,139,203,123,231,243,190,110,163,69,201,12,103,226,131,140,
    174,160,114,147,47,26,155,152,236,53,159,56,139,171,253,73,89,43,36,44,90,193,214,134,
    110,165,117,84,152,55,143,250,33,70,179,18,61,176,16,194,243,104,253,142,135,226,186,14,
    191,173,127,20,183,162,147,229,75,32,209,1,112,254,189,0,21,45,3,246,9,249,174,18,
    203,140,108,40,96,215,238,229,10,187,235,146,140,103,147,138,163,233,100,131,108,81,154,31,
    62,198,162,49,20,240,120,245,152,74,183,144,181,28,89,182,99,201,138,232,226,233,225,209,
    69,132,49,25,205,133,132,178,63,134,105,11,247,77,16,54,205,241,120,6,79,217,102,70,
    71,101,10,10,222,209,205,216,10,158,154,82,130,9,143,132,255,216,236,57,211,139,245,131,
    18,42,111,227,122,135,74,2,205,150,213,240,243,45,102,191,101,40,44,225,218,155,122,81,
    233,149,15,201,20,24,162,163,214,208,45,110,22,147,215,113,235,200,144,207,105,152,208,194,
    49,73,127,114,150,44,149,49,69,59,163,248,153,1,2,126,58,228,166,211,46,18,122,132,
    68,210,219,217,164,72,167,53,201,183,67,200,171,209,9,106,6,240,186,228,48,88,11,142,
    32,6,184,238,225,191,229,220,110,150,32,211,30,227,131,164,97,255,245,223,46,205,121,164,
    107,244,187,13,109,176,169,148,224,43,73,152,51,206,218,76,60,66,159,83,49,3,45,51,
    135,23,56,44,113,93,89,58,164,53,144,218,70,218,206,178,225,82,200,232,89,123,103,67,
    45,106,118,72,227,73,115,168,223,146,116,181,16,87,37,44,85,197,160,52,223,240,38,157,
    112,175,254,80,46,217,224,96,250,0,54,209,78,125,87,199,177,112,18,188,49,137,142,213,
    223,17,237,252,219,168,97,157,5,60,177,252,149,216,217,183,179,249,218,117,130,141,236,187,
    70,49,98,53,125,236,242,185,97,22,61,179,44,56,60,212,140,28,145,45,80,20,68,123,
    70,28,210,12,218,106,37,57,15,30,120,27,104,81,103,54,225,30,2,237,79,186,201,223,
    249,6,175,76,122,231,111,114,212,207,54,201,109,225,111,41,114,202,134,234,115,113,203,91,
    230,237,42,35,63,208,194,78,135,181,90,245,181,198,82,17,189,210,180,255,218,231,227,23,
    154,251,130,50,141,191,23,77,200,160,91,19,90,236,195,177,144,82,6,230,62,146,213,243,
    33,101,68,129,174,126,58,0,75,233,23,88,251,247,153,65,138,230,215,17,242,217,131,90,
    39,22,60,175,22,151,238,180,93,211,30,59,27,137,51,56,20,91,72,124,90,136,70,54,
    118,34,143,164,18,223,84,40,18,156,223,83,141,236,168,189,79,199,204,213,144,164,1,73,
    20,58,44,158,118,37,14,111,39,100,167,238,137,75,94,240,240,181,38,232,182,214,136,89,
    99,245,253,144,67,140,77,0,130,176,0,169,38,132,48,211,33,37,187,253,155,2,194,34,
    76,115,227,213,53,208,22,38,68,186,38,171,255,14,69,23,99,219,94,235,239,25,31,69,
    190,166,91,218,60,11,83,145,253,21,177,233,148,164,3,87,90,95,120,153,214,91,228,57,
    126,201,30,100,220,141,3,50,17,202,92,181,61,253,130,77,45,135,76,183,59,5,228,202,
    186,209,46,243,212,213,247,33,193,235,240,32,30,54,87,157,240,176,126,209,175,181,17,224,
    143,126,194,121,190,242,119,201,3,255,156,202,87,120,99,226,105,85,212,104,135,240,27,177,
    28,17,112,230,220,135,96,122,147,80,119,0,31,46,43,31,156,255,10,15,245,18,212,32,
    232,158,108,155,61,85,37,158,48,167,217,94,205,202,23,86,135,26,95,37,242,229,247,205,
    41,10,85,74,169,132,224,57,68,4,69,196,125,67,204,192,151,219,252,142,192,85,47,227,
    127,22,17,196,169,10,85,29,110,173,14,0,76,156,140,20,135,210,185,250,18,116,200,105,
    119,38,43,73,227,149,19,250,118,101,97,5,243,236,50,55,215,226,153,178,85,17,75,92,
    57,188,184,101,75,1,113,122,199,86,7,195,7,38,103,108,6,54,128,251,185,156,84,70,
    15,7,54,199,35,100,131,21,206,186,225,21,164,187,221,227,92,99,74,177,219,80,112,190,
    228,44,21,97,69,136,78,149,178,230,12,190,83,83,8,170,233,31,204,145,121,186,187,73,
    10,172,128,104,109,116,81,92,191,5,141,79,191,220,119,45,154,105,192,119,107,86,28,37,
    77,2,69,138,124,3,21,35,117,213,108,99,86,157,140,77,246,144,36,100,26,141,128,231,
    252,242,117,2,41,95,213,140,153,137,30,43,98,77,32,142,199,73,3,153,175,231,148,251,
    12,61,29,87,166,210,137,239,115,137,44,241,127,149,115,109,65,238,242,86,164,254,88,141,
    38,20,227,147,198,154,132,160,153,8,191,29,41,232,46,185,172,31,90,242,141,106,229,243,
    197,58,252,134,107,101,159,72,108,71,196,61,214,33,42,31,62,105,31,84,92,238,78,202,
    72,33,250,216,55,4,174,44,180,151,215,141,127,170,251,62,119,77,86,11,107,66,228,243,
    63,206,198,43,113,158,120,142,50,33,50,89,1,113,174,35,182,31,61,171,130,0,201,172,
    132,238,171,118,46,76,84,75,220,192,60,126,251,11,145,250,232,170,214,89,185,97,69,214,
    201,201,156,210,156,33,144,229,50,29,182,110,16,65,163,76,32,128,62,4,18,112,36,22,
    91,2,164,40,21,101,169,129,113,38,166,49,238,145,72,195,45,72,67,57,73,158,152,255,
    205,117,123,139,203,14,174,6,15,230,142,236,148,69,145,77,229,216,183,169,134,135,240,92,
    124,44,124,140,186,128,210,248,129,171,123,61,168,202,123,35,255,14,196,228,228,167,102,101,
    216,108,69,132,82,98,134,130,225,183,2,31,176,246,177,82,22,226,140,102,32,228,211,43,
    152,132,178,49,3,153,26,170,132,58,188,113,13,222,233,149,119,7,59,223,249,233,159,226,
    206,12,78,93,178,51,57,179,243,150,97,135,216,3,34,218,127,72,191,173,246,96,50,221,
    225,170,80,17,253,141,121,100,203,167,155,88,74,161,14,145,20,1,15,199,213,184,81,154,
    250,9,99,30,238,163,113,35,110,58,67,87,30,156,141,99,142,144,221,49,199,108,176,58,
    76,228,56,92,244,202,203,230,122,167,141,95,4,51,0,220,60,185,216,3,145,239,215,229,
    163,44,166,232,205,141,31,122,145,8,154,161,53,240,13,30,227,137,137,131,197,50,75,112,
    207,145,56,217,53,155,124,186,149,38,30,54,217,120,107,27,225,108,96,213,134,103,50,77,
    245,150,69,99,66,237,211,146,164,3,156,11,69,89,239,33,134,181,252,88,151,132,241,53,
    170,151,178,226,180,33,94,112,255,79,155,107,204,228,60,213,44,0,73,60,16,44,154,215,
    107,101,131,65,35,120,42,64,166,131,154,119,189,175,15,87,75,170,190,177,214,34,44,141,
    90,156,92,209,189,74,134,30,217,59,102,112,250,143,234,19,143,202,214,41,66,174,186,58,
    162,85,156,83,157,18,204,222,209,62,244,56,245,188,54,245,221,15,3,186,201,111,242,125,
    57,220,90,193,38,209,208,165,254,80,213,17,120,6,97,220,232,16,14,220,172,103,255,52,
    131,138,94,182,88,59,74,16,201,60,240,240,91,107,38,85,28,31,140,70,49,87,78,205,
    97,54,227,149,15,164,33,164,138,206,127,156,168,27,33,41,233,17,168,245,184,125,209,42,
    166,107,254,163,44,13,71,73,127,224,87,57,222,125,208,84,181,71,150,71,169,10,143,147,
    180,46,192,33,130,118,137,13,151,136,88,34,60,195,194,134,32,220,40,127,145,74,55,246,
    147,217,84,116,127,106,183,191,75,204,31,99,99,0,10,195,136,193,203,221,100,197,203,199,
    75,180,244,170,96,178,224,27,253,79,28,170,6,64,76,65,165,202,34,120,154,166,140,212,
    62,242,2,98,219,246,138,198,52,60,104,253,140,184,210,24,114,188,223,193,242,44,37,159,
    40,34,13,194,182,172,96,104,143,35,197,92,76,75,120,168,13,111,128,176,184,144,109,11,
    108,198,109,154,127,111,222,253,45,63,115,150,130,27,30,38,141,84,235,156,159,40,85,244,
    103,153,57,221,2,139,45,166,24,47,192,103,93,227,61,159,229,165,130,127,64,54,161,185,
    202,159,31,210,143,73,187,218,103,190,210,87,192,200,183,67,116,205,142,122,159,195,174,53,
    176,24,214,23,218,117,5,144,253,245,103,38,129,16,176,177,96,239,34,21,124,194,196,222,
    211,73,98,202,112,112,155,60,124,44,231,123,121,36,241,153,62,149,83,186,16,204,188,170,
    100,178,193,11,232,174,84,135,65,124,49,131,235,147,79,179,133,182,5,180,124,3,132,176,
    108,236,156,10,232,144,168,156,35,183,110,25,136,143,44,118,253,70,19,3,194,200,248,139,
    80,181,85,102,24,21,10,96,84,238,118,164,174,182,96,132,117,229,131,0,78,92,85,159,
    250,63,222,58,232,17,17,121,159,229,132,191,208,63,188,8,124,85,241,18,83,35,189,6,
    130,221,183,97,241,145,172,47,240,181,255,128,98,143,145,52,198,224,72,71,77,27,23,140,
    17,156,144,139,194,107,123,123,52,61,18,165,217,106,120,153,87,34,230,120,163,178,26,123,
    59,80,20,37,114,161,98,144,170,172,215,200,212,52,63,122,192,243,9,207,122,35,79,169,
    28,232,63,83,119,27,107,140,35,228,116,196,87,55,172,184,180,159,148,72,34,123,14,81,
    238,98,235,79,242,202,209,180,243,201,255,230,37,71,196,16,141,170,40,21,46,41,0,168,
    244,205,43,205,157,103,158,156,216,160,99,189,166,177,175,161,79,41,205,208,18,154,186,58,
    220,156,207,3,154,43,175,110,149,97,184,4,232,125,44,169,67,2,150,226,170,147,124,104,
    134,106,218,151,253,186,96,19,177,22,48,151,48,10,195,138,245,106,17,243,13,255,140,231,
    92,140,181,46,57,106,101,54,82,69,213,24,38,20,72,41,133,205,121,88,135,173,206,14,
    27,23,136,121,199,163,59,180,103,222,19,205,220,14,159,49,104,168,38,137,14,116,243,88,
    140,147,197,77,12,165,226,98,65,167,247,135,255,9,163,148,104,92,169,45,225,90,242,82,
    207,98,87,232,58,213,169,117,72,64,140,111,22,55,33,23,149,212,142,209,84,114,213,53,
    58,104,1,9,209,209,65,178,206,131,101,85,139,192,230,46,170,122,46,93,17,232,253,137,
    15,73,190,198,51,231,126,126,197,181,104,55,80,145,65,204,138,94,69,110,220,51,239,138,
    114,158,237,139,175,103,150,132,25,247,148,126,120,160,77,85,44,158,15,76,190,102,94,26,
    12,194,138,241,143,124,87,195,207,84,159,41,40,180,149,162,84,2,229,157,78,94,106,11,
    37,161,227,145,93,252,24,108,2,30,56,217,255,232,20,86,24,31,133,164,230,177,208,59,
    145,80,52,3,87,44,250,50,161,116,5,205,67,164,63,190,251,190,56,38,59,79,99,251,
    245,60,249,62,141,23,40,227,186,170,3,173,114,188,247,95,104,93,175,32,186,232,217,38,
    212,254,213,57,53,22,132,156,140,30,163,218,136,47,148,55,140,136,69,56,166,168,35,1,
    27,179,207,43,194,167,138,18,11,172,145,58,8,189,234,207,136,73,123,122,135,245,2,193,
    71,40,159,98,48,88,144,89,69,233,165,216,184,190,56,207,153,60,223,37,152,8,188,56,
    141,188,224,244,216,109,16,7,141,179,110,33,71,150,235,18,83,95,25,29,244,171,96,215,
    244,24,125,230,183,38,192,210,59,158,223,137,151,211,36,77,226,71,194,121,78,139,74,213,
    172,196,87,235,238,34,206,134,100,253,136,238,205,20,230,159,44,170,96,191,106,80,81,152,
    156,177,232,2,234,63,235,200,17,219,243,187,35,249,102,190,68,91,59,200,11,16,38,99,
    117,48,23,197,36,143,126,64,243,170,34,183,91,39,70,110,195,69,106,193,236,7,87,241,
    28,48,98,229,163,107,67,239,168,199,167,157,74,221,103,26,220,54,226,166,127,162,213,212,
    150,221,230,125,22,70,174,220,149,199,115,219,84,66,248,230,76,140,103,103,131,9,250,59,
    7,129,63,93,108,110,89,139,54,187,85,96,205,241,30,215,51,32,30,205,217,30,70,114,
    64,135,57,250,218,254,73,100,140,196,118,210,103,63,142,118,120,38,193,186,144,30,111,209,
    41,35,170,90,21,0,214,32,21,37,36,237,74,16,150,134,25,119,42,80,208,1,179,29,
    237,103,68,151,3,131,159,81,165,80,188,47,47,215,204,60,188,67,21,170,5,163,229,33,
    105,39,133,246,37,56,150,49,187,212,246,180,51,35,9,59,55,40,19,247,128,205,167,125,
    44,8,211,216,188,11,92,92,2,65,54,105,160,217,28,240,146,100,85,246,85,109,118,109,
    239,62,38,163,237,35,38,146,99,231,173,57,151,50,60,59,15,181,156,121,16,1,144,22,
    47,70,127,61,37,167,70,215,229,222,193,64,81,214,143,110,216,3,64,171,131,190,195,31,
    186,57,50,167,177,136,221,197,46,77,11,64,170,211,246,10,114,48,210,113,176,199,28,53,
    219,78,225,66,123,246,117,154,190,179,107,189,42,142,12,188,175,168,167,23,120,84,33,175,
    165,223,87,98,3,12,0,179,238,142,132,139,58,91,117,61,65,240,168,26,209,189,2,247,
    121,147,217,165,193,125,101,233,61,217,235,143,110,228,179,229,124,253,148,47,251,97,52,172,
    62,102,13,38,107,246,147,41,101,240,236,245,218,82,12,36,106,61,205,239,67,160,255,132,
    1,184,120,3,243,166,145,97,156,104,129,190,52,0,212,179,171,91,66,73,205,103,27,54,
    7,204,89,81,6,48,253,18,151,131,59,234,195,192,207,2,12,9,64,173,225,46,52,174,
    248,245,218,28,29,248,12,121,207,153,38,146,97,251,235,23,172,242,37,119,241,148,191,25,
    49,202,135,98,65,116,48,86,203,222,0,127,28,223,187,175,63,246,37,216,180,14,1,163,
    187,137,42,230,198,48,31,115,127,51,202,121,0,88,55,65,99,34,93,95,70,153,27,226,
    87,113,117,36,212,158,112,98,161,219,208,49,251,247,18,137,21,8,0,214,197,151,164,136,
    150,48,98,235,12,124,135,160,177,48,116,70,92,205,183,179,116,123,56,220,69,214,197,15,
    221,145,11,9,26,80,240,3,70,131,180,186,202,64,141,133,95,62,39,147,64,66,123,225,
    111,17,82,79,25,172,213,83,126,216,248,190,34,26,76,255,71,138,4,239,164,50,38,92,
    138,32,102,198,91,147,111,12,108,254,114,27,157,159,154,253,170,26,77,99,155,157,47,84,
    55,254,143,233,246,25,229,235,246,105,144,144,200,55,129,175,43,87,18,131,90,123,188,118,
    82,86,216,96,47,89,241,166,51,63,191,165,240,198,9,74,145,52,194,24,44,179,125,127,
    192,48,55,55,173,233,4,33,177,215,192,104,86,183,147,112,26,141,47,104,145,223,248,237,
    154,42,144,120,134,242,163,209,166,3,173,82,239,242,39,201,113,150,102,185,15,197,109,16,
    189,122,217,49,143,101,153,209,213,49,173,49,128,96,5,223,171,146,170,148,38,168,76,66,
    52,145,124,246,188,80,95,160,160,233,208,209,85,188,247,205,49,66,135,170,254,51,186,58,
    230,250,124,119,152,58,85,172,224,45,113,207,126,143,218,217,119,100,111,192,235,122,239,125,
    165,225,119,108,190,146,71,113,186,188,117,235,235,152,52,1,167,138,175,213,7,23,97,164,
    231,139,24,98,108,22,112,75,172,37,6,172,36,157,134,29,234,36,243,139,60,130,223,132,
    27,114,125,75,98,26,201,190,39,84,11,48,26,19,36,201,18,60,199,74,203,101,227,140,
    103,226,31,201,16,185,174,169,120,207,71,244,31,141,33,143,85,131,97,186,66,201,71,170,
    9,39,27,169,126,159,90,221,224,156,189,4,244,128,191,156,99,102,246,95,156,95,107,25,
    227,230,70,145,91,189,115,230,21,65,210,236,134,130,109,108,192,35,97,161,208,111,20,14,
    155,201,181,217,210,221,118,248,95,162,105,192,65,6,28,207,58,213,245,166,174,29,208,71,
    243,142,27,139,152,100,46,143,42,163,240,96,63,63,173,59,178,230,156,40,1,83,210,123,
    133,93,227,224,229,77,137,83,248,172,39,231,204,217,93,48,10,15,232,255,135,86,173,132,
    77,115,25,193,77,131,251,243,116,6,62,55,184,102,216,43,168,32,190,21,170,216,80,52,
    169,143,138,68,145,188,128,250,116,254,213,133,120,211,238,104,44,25,100,5,95,93,67,84,
    77,190,87,178,103,68,151,177,5,119,124,1,66,223,80,208,113,34,87,240,64,80,184,177,
    123,78,158,20,235,189,33,219,91,45,196,75,137,210,206,181,143,215,116,202,224,201,226,20,
    71,31,8,193,89,99,175,183,149,185,38,8,110,218,132,160,234,6,154,22,101,69,193,176,
    241,67,19,220,87,182,197,80,86,210,218,11,117,16,121,192,203,42,64,102,144,1,224,2,
    161,173,141,223,0,3,136,191,180,116,89,28,188,53,132,90,84,15,159,229,7,231,164,36,
    192,5,100,220,14,127,82,156,233,231,5,220,104,193,235,104,61,172,21,64,229,36,32,168,
    109,237,195,99,215,239,79,232,8,63,110,93,194,192,172,119,239,134,179,227,254,44,184,78,
    125,215,13,78,78,213,84,143,69,146,19,22,24,191,234,68,169,180,221,211,99,77,254,219,
    238,100,118,62,140,132,187,103,65,134,102,75,25,84,185,114,174,182,112,105,54,64,192,178,
    52,61,123,74,91,55,88,99,129,143,226,193,160,77,249,139,219,250,226,168,165,238,5,27,
    165,221,203,127,172,198,163,92,226,205,145,225,44,134,64,159,144,45,144,79,138,151,157,23,
    108,62,54,147,58,84,43,213,111,120,28,209,173,178,16,45,151,54,127,153,160,247,180,39,
    93,41,148,148,82,10,108,252,118,179,48,171,102,104,54,243,3,182,204,154,208,140,188,213,
    129,56,66,159,189,13,168,222,157,217,12,48,105,151,205,57,67,108,242,32,137,251,0,109,
    58,149,201,47,187,122,49,111,37,3,174,161,197,12,80,241,39,134,48,144,179,249,143,242,
    136,43,104,228,18,132,229,168,56,249,107,14,229,117,109,133,87,124,28,178,161,80,135,53,
    207,21,161,252,247,200,246,222,170,26,83,132,123,103,97,109,247,56,2,110,130,137,20,247,
    179,120,34,200,17,233,249,206,94,191,107,220,85,230,60,229,156,134,214,101,123,34,242,29,
    107,158,134,59,12,127,169,78,228,144,69,198,113,115,222,175,250,14,54,61,11,124,246,44,
    226,186,92,77,52,29,116,101,162,202,145,109,182,55,187,82,232,16,72,200,126,141,128,145,
    196,153,243,0,246,128,154,61,19,97,225,226,212,73,55,233,8,29,95,15,105,148,48,160,
    177,174,164,220,71,200,178,210,157,63,186,69,31,77,179,12,98,3,84,127,252,202,192,85,
    108,16,111,49,217,1,14,134,11,40,4,93,117,45,11,50,158,224,252,231,174,75,7,177,
    216,179,69,189,56,180,113,164,233,61,114,150,137,239,102,46,170,174,76,148,55,233,205,87,
    127,207,108,54,182,242,167,95,46,58,158,100,195,113,212,187,1,181,136,94,198,159,153,10,
    13,10,105,27,255,87,233,207,221,215,18,66,153,18,116,51,152,207,13,92,124,238,115,100,
    33,74,101,45,225,64,9,6,186,121,87,156,156,85,26,128,219,172,220,250,1,9,232,13,
    36,80,148,171,90,162,60,77,35,55,153,89,104,232,211,200,177,249,166,42,21,76,97,134,
    173,29,115,215,6,149,132,138,54,118,252,109,213,191,255,105,30,86,97,119,98,12,106,94,
    164,84,177,231,81,69,126,60,101,103,61,225,247,88,67,66,97,242,51,154,19,97,170,180,
    107,150,162,237,170,246,53,75,131,171,190,40,7,185,100,85,146,76,93,81,166,186,87,211,
    245,26,122,6,55,53,23,86,146,73,139,218,52,189,155,137,160,181,141,10,125,67,170,153,
    121,233,159,92,60,67,61,205,197,251,140,87,29,15,172,4,54,213,223,154,0,91,194,146,
    14,103,160,227,19,115,242,180,13,96,46,207,194,160,184,161,60,171,128,16,151,208,92,171,
    126,86,6,201,116,6,132,93,29,202,154,134,13,49,81,166,104,185,105,214,83,208,167,220,
    183,231,28,2,155,12,199,247,147,108,57,141,128,168,69,73,1,23,190,39,120,8,153,118,
    216,44,208,16,140,67,68,148,175,46,49,54,152,237,236,97,52,139,62,9,198,79,62,128,
    180,159,110,38,117,104,132,102,167,6,20,6,52,191,12,147,253,171,50,226,249,7,117,104,
    63,168,78,48,11,54,38,145,42,221,32,31,56,130,199,74,45,172,93,101,158,30,212,65,
    100,215,3,151,222,210,216,105,153,95,125,101,182,25,144,88,104,57,146,126,170,55,70,25,
    193,209,104,97,2,198,48,41,7,109,166,176,198,195,228,237,214,54,108,110,21,2,123,242,
    248,243,247,154,99,109,108,166,122,152,4,13,45,33,216,130,21,228,38,245,239,9,181,111,
    123,53,25,99,135,147,2,211,131,92,215,12,237,76,150,190,120,131,142,27,179,56,232,10,
    145,106,5,129,107,73,154,4,230,152,120,36,185,204,212,53,184,30,191,75,12,6,147,243,
    124,138,168,120,14,25,180,220,89,12,149,232,218,248,249,230,169,234,187,24,4,164,200,144,
    201,50,181,241,9,245,11,136,141,37,30,128,85,63,254,197,44,166,216,136,221,102,238,215,
    177,102,195,213,28,22,51,132,154,214,240,244,169,26,38,248,195,49,154,216,162,29,114,31,
    38,141,165,212,62,61,179,14,69,167,9,72,66,197,150,92,153,182,72,93,224,20,217,47,
    219,80,183,28,34,24,122,149,17,213,97,32,57,229,245,173,158,58,205,197,161,64,171,35,
    7,179,111,7,229,196,74,105,105,77,114,149,252,58,213,7,42,233,120,76,118,89,208,216,
    148,163,207,186,9,47,227,49,204,204,26,163,231,142,99,226,246,252,178,244,36,190,52,79,
    32,53,255,165,25,129,180,110,249,200,200,82,146,160,110,32,104,158,76,159,118,146,113,37,
    152,193,237,7,150,12,113,45,213,223,243,232,7,206,179,126,219,67,140,73,244,94,210,210,
    94,38,60,22,66,47,65,31,101,106,254,22,19,197,193,100,114,184,103,245,216,133,106,100,
    228,96,8,239,3,144,99,76,67,78,189,78,54,218,99,176,225,224,11,126,185,8,104,51,
    63,164,179,60,230,126,156,154,65,17,92,115,131,212,2,233,168,144,207,157,193,48,254,90,
    126,37,81,6,156,85,226,238,6,7,105,166,132,3,224,127,241,139,252,83,160,82,190,151,
    233,140,111,125,224,25,22,245,3,212,44,155,4,178,163,143,15,116,107,72,236,110,231,237,
    4,43,176,56,166,16,5,34,161,200,50,51,235,60,30,85,96,122,244,222,234,152,251,17,
    3,206,24,191,44,163,161,240,234,119,148,93,221,63,74,46,139,45,161,50,184,225,168,93,
    175,195,98,253,54,179,138,152,172,23,233,150,56,123,61,193,89,44,243,6,138,226,174,189,
    254,82,154,38,215,123,27,229,167,62,198,81,90,28,212,36,48,141,67,102,239,204,11,218,
    242,251,19,249,119,11,184,218,9,55,150,156,219,231,86,36,15,39,188,191,211,121,64,66,
    228,83,73,80,209,87,220,201,6,13,217,168,139,34,230,123,106,131,142,130,235,180,226,58,
    77,205,150,102,43,42,23,81,214,212,244,138,195,89,85,43,196,205,15,150,26,2,166,77,
    14,193,96,222,13,125,167,211,85,88,116,146,69,13,234,127,181,204,49,57,68,193,52,161,
    195,114,242,231,62,29,52,27,228,221,252,1,200,90,235,43,34,46,160,185,183,136,130,7,
    204,19,0,168,107,20,231,14,68,203,28,91,246,71,191,197,250,160,198,60,225,183,87,145,
    110,27,38,84,60,10,184,20,159,213,137,153,53,24,180,171,24,138,178,21,165,11,88,45,
    118,126,191,172,137,152,176,163,65,45,38,18,69,243,192,127,5,154,186,170,180,232,181,208,
    144,15,95,77,16,173,76,67,91,28,118,138,222,156,103,144,162,247,220,153,234,49,114,156,
    224,195,170,173,187,59,133,132,250,110,167,60,73,233,190,133,11,140,235,6,211,9,97,194,
    123,120,173,240,206,110,8,201,181,8,234,129,89,199,19,87,22,85,130,130,87,155,73,196,
    203,210,78,165,201,23,155,211,10,28,205,82,27,167,214,204,124,21,134,64,89,14,107,76,
    202,223,249,147,244,44,197,179,112,3,80,59,207,141,113,211,72,238,64,134,67,188,130,22,
    187,244,152,77,157,252,131,245,143,58,230,185,164,97,211,84,237,21,54,214,89,178,114,128,
    184,212,109,188,244,46,34,56,148,219,87,166,219,11,68,33,99,218,180,58,141,155,5,226,
    152,68,218,163,176,88,126,122,155,243,8,249,205,139,131,221,227,39,211,64,43,235,134,255,
    223,243,141,233,179,254,19,197,193,161,156,119,18,213,213,155,174,249,210,43,136,0,167,223,
    37,83,82,173,5,45,167,174,99,34,7,213,164,194,44,60,240,29,166,99,246,39,27,212,
    68,86,0,64,39,94,244,53,20,207,176,17,171,200,118,132,20,249,194,43,157,188,10,138,
    32,240,8,107,113,54,87,74,70,213,31,8,240,12,166,61,119,194,184,34,141,98,73,46,
    36,230,90,222,92,3,126,206,51,100,164,207,74,141,135,21,158,8,95,38,51,171,168,72,
    103,34,193,237,112,197,169,86,175,239,46,3,14,117,186,207,0,201,152,223,136,249,157,215,
    82,99,35,62,239,59,70,204,11,227,8,247,128,238,39,89,160,240,72,95,113,150,171,57,
    128,247,49,216,172,172,30,222,227,236,204,197,41,78,97,31,162,228,48,70,87,179,219,215,
    123,23,124,139,223,98,190,82,179,48,159,246,70,3,128,223,198,119,48,91,80,121,200,159,
    23,197,150,29,231,164,174,135,27,39,148,182,210,116,14,237,253,115,249,255,177,127,253,43,
    70,88,106,141,152,167,147,80,113,176,183,12,250,211,143,10,250,18,254,50,137,191,145,125,
    162,244,36,232,27,107,1,47,252,188,27,37,161,60,87,129,224,214,82,53,40,141,26,222,
    31,255,229,212,234,198,233,83,139,178,184,74,131,29,36,75,159,207,170,123,162,72,2,53,
    185,152,17,96,179,145,35,19,110,69,125,140,6,65,144,120,129,198,186,83,7,55,210,110,
    193,90,138,19,173,202,49,242,130,66,54,46,242,100,141,241,243,194,183,177,254,184,183,56,
    218,243,217,11,24,244,241,206,56,113,184,66,85,66,36,4,157,99,236,27,87,170,22,37,
    146,115,235,155,201,103,42,0,13,103,24,73,87,79,102,102,240,74,178,190,144,62,247,170,
    139,72,2,223,93,6,114,0,119,242,38,184,236,102,135,230,253,225,106,28,205,211,104,165,
    45,94,115,182,176,215,142,26,189,114,123,49,247,83,210,253,206,98,120,185,186,181,130,122,
    217,247,60,248,130,143,247,182,166,10,238,57,150,162,12,184,190,27,5,233,61,184,172,150,
    195,251,46,43,227,238,190,199,162,191,13,58,247,248,171,197,168,248,221,76,108,76,187,253,
    25,105,17,242,111,202,177,146,151,57,60,245,70,116,216,183,153,96,3,172,199,49,128,87,
    68,25,80,37,205,220,158,107,64,98,58,229,114,235,152,32,248,167,123,60,227,124,200,221,
    125,96,12,155,67,180,235,231,196,36,11,75,60,188,49,28,179,71,226,210,74,137,43,149,
    35,121,231,175,234,200,183,0,135,54,87,68,151,247,66,75,163,132,106,142,58,187,95,22,
    154,157,253,150,24,229,156,121,45,49,80,14,155,254,197,150,96,255,139,81,99,44,180,154,
    98,28,35,85,69,238,123,179,195,140,205,152,169,208,58,169,63,38,110,228,29,41,126,79,
    17,251,21,178,53,51,19,111,155,26,97,188,33,231,59,60,211,212,233,116,207,209,5,213,
    13,34,24,243,219,154,113,84,171,169,249,219,75,237,111,12,103,200,218,112,46,204,152,218,
    54,47,138,69,137,209,64,115,144,97,9,240,72,171,41,178,167,143,119,170,68,88,115,178,
    136,157,82,215,252,49,74,198,188,3,36,147,102,74,103,232,120,164,163,138,188,156,248,76,
    129,198,84,164,51,104,190,161,231,132,235,52,214,98,195,199,202,3,12,107,169,244,108,156,
    174,109,237,29,11,148,15,72,15,228,166,240,242,
    0,0,0,0,0,0,0,0,
};

// The range coder's parameters; gen_entities.py must agree.
const MODEL_BITS = 13; // 8,192 16-bit entries: the most that fits before the input
const MODEL_LIMIT = 3;
const MIX_SHIFT: u5 = 11;
const MIX_W0: i32 = 16384;
const K: u32 = 1 << 20;
const HASH_A: u32 = 0x2545F491;
const HASH_B: u32 = 0x9E3779B1;
const HIST_SIZE = 16_130;
// e^(2047 / 256) and e^(-1 / 256) as the bits of a float64.
const SQUASH_E0: u64 = 0x40a732ac33de5528;
const SQUASH_STEP: u64 = 0x3fefe00ffaabffbc;
// Step toward the bit just seen, as a 16-bit fraction: 2 / (2n + 3) after n uses.
const rate = [4]i32{ 43690, 26214, 18724, 14563 };


// The value's UTF-8 bytes in the low bytes, its length in bits 56-58 and, for
// long-name entries, the name's length in bits 59-63.
const Entry = extern struct { key: u64, val: u64 };
const Long16 = extern struct { name: [16]u8, val: u64 };
const Long32 = extern struct { name: [32]u8, val: u64 };

// One buffer: the output is written from its start and the input sits IN_OFF bytes in, so
// the output follows the input through the same memory (see the header comment).
var buf align(64) = std.mem.zeroes([IN_OFF + INPUT_CAP + PAD]u8);
const input: [*]u8 = buf[IN_OFF..].ptr;
const output: [*]u8 = buf[0..].ptr;
// The unpacker's tables, inside the buffer's still-zero start on the first call.
const model: [*]u16 = @ptrCast(buf[0..].ptr);
const squash: [*]u16 = @ptrCast(buf[SQUASH_OFF..].ptr);
const stretch: [*]i16 = @ptrCast(buf[STRETCH_OFF..].ptr);
const hist: [*]u8 = buf[HIST_OFF..].ptr;
// The match model's position list, in stage 1's still-zero position list.
const last: [*]u16 = @ptrCast(&pos);
var weights = std.mem.zeroes([3]i32);
var rc_in: u32 = undefined;
var rc_range: u32 = undefined;
var rc_code: u32 = undefined;
var fast: [(1 << FAST_BITS) + FAST_TAIL]Entry = undefined;
var leg: [(1 << LEG_BITS) + LEG_TAIL]Entry = undefined;
var longs16: [LONG16_COUNT]Long16 align(16) = std.mem.zeroes([LONG16_COUNT]Long16);
var longs32: [LONG32_COUNT]Long32 align(16) = std.mem.zeroes([LONG32_COUNT]Long32);
// Long-name slots: 0 = empty, else (tier << 15) | index + 1, tier 0 = longs16, 1 = longs32.
var long_slots = std.mem.zeroes([1 << LONG_BITS]u16);
// Longest legacy name starting with these two bytes (hashed), or 0.
var leg_max = std.mem.zeroes([2048]u8);
// Stage 1's per-window position list; zeroed, because the unpacker's `last` table lives
// here on the first call.
var pos = std.mem.zeroes([WINDOW + 16]u32);
var ready: bool = false;

// Windows-1252 remapping for references to 0x80-0x9F.
const c1 = [32]u16{
    0x20AC, 0x81,   0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
    0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x8D,   0x017D, 0x8F,
    0x90,   0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
    0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x9D,   0x017E, 0x0178,
};
const utf8_marks = [5]u32{ 0, 0, 0x80C0, 0x8080E0, 0x808080F0 };
// Byte j of the 32 loaded at `len_mask + 32 - n` is 0xFF when j < n: 32 bytes of 0xFF, then 32 of 0.
var len_mask align(16) = std.mem.zeroes([64]u8);

inline fn ld(p: [*]const u8) V {
    return p[0..16].*;
}
inline fn st(p: [*]u8, v: V) void {
    p[0..16].* = v;
}
inline fn ld64(p: [*]const u8) u64 {
    return @as(*align(1) const u64, @ptrCast(p)).*;
}
inline fn st64(p: [*]u8, x: u64) void {
    @as(*align(1) u64, @ptrCast(p)).* = x;
}
inline fn st32(p: [*]u8, x: u32) void {
    @as(*align(1) u32, @ptrCast(p)).* = x;
}
inline fn eqMask(v: V, c: u8) u32 {
    return @as(u16, @bitCast(v == @as(V, @splat(c))));
}
inline fn legSlot(w: u64) usize {
    return ((@as(u32, @truncate(w)) & 0xFFFF) *% 0x9E3779B1) >> 21;
}
inline fn longHash(head7: u64, len: u64) usize {
    // head7 is the name's first 7 bytes (the 8th masked off, so shorter names hash
    // consistently with the unpacker), len rides on top.
    return @intCast(((head7 | len << 56) *% LONG_MUL) >> (64 - LONG_BITS));
}

/// A name of 8 to 31 bytes: probe the long-name table. Names of at most 15 bytes
/// sit in the 16-byte tier (one vector compare); longer ones in the 32-byte tier.
inline fn longName(w: u64, s0: V, s1: V, len: usize, dst: [*]u8, o: *usize) bool {
    var h = longHash(w & 0x00FF_FFFF_FFFF_FFFF, len);
    const lm: [*]const u8 = &len_mask;
    if (len <= 15) {
        const m0 = ld(lm + 32 - len);
        while (true) {
            const s = long_slots[h];
            if (s == 0) return false;
            if (s & 0x8000 == 0) {
                const l = &longs16[s - 1];
                const nm: [*]const u8 = &l.name;
                const diff = (s0 & m0) ^ ld(nm);
                if (l.val >> 59 == len and @reduce(.Or, diff) == 0) {
                    st64(dst + o.*, l.val);
                    o.* += @intCast((l.val >> 56) & 7);
                    return true;
                }
            }
            h = (h + 1) & LONG_MASK;
        }
    } else {
        const m0 = ld(lm + 32 - len);
        const m1 = ld(lm + 48 - len);
        while (true) {
            const s = long_slots[h];
            if (s == 0) return false;
            if (s & 0x8000 != 0) {
                const l = &longs32[(s & 0x7FFF) - 1];
                const nm: [*]const u8 = &l.name;
                const diff = ((s0 & m0) ^ ld(nm)) | ((s1 & m1) ^ ld(nm + 16));
                if (l.val >> 59 == len and @reduce(.Or, diff) == 0) {
                    st64(dst + o.*, l.val);
                    o.* += @intCast((l.val >> 56) & 7);
                    return true;
                }
            }
            h = (h + 1) & LONG_MASK;
        }
    }
}

// The entity list is range-coded and read once, on the first call, by `sym` below; see
// tools/gen_entities.py (the matching packer) for the format and the field sequence.

// A code point as UTF-8 bytes in a word, low byte first.
noinline fn utf8(v: u32, len: *u32) u32 {
    @branchHint(.cold);
    const n: u32 = 1 + @as(u32, @intFromBool(v > 0x7F)) + @intFromBool(v > 0x7FF) + @intFromBool(v > 0xFFFF);
    const bits = (v >> 18) | (((v >> 12) & 0x3F) << 8) | (((v >> 6) & 0x3F) << 16) | ((v & 0x3F) << 24);
    len.* = n;
    return if (n == 1) v else (bits >> @intCast(32 - 8 * n)) | utf8_marks[n];
}

// squash[x + 2047] = 4096 / (1 + e^(-x / 256)) rounded, for x = -2047..2047, and stretch,
// its inverse. Only IEEE multiply, add and divide, so every machine (and the packer)
// gets the same bits.
noinline fn buildSquash() void {
    @setFloatMode(.strict);
    var e: f64 = @bitCast(SQUASH_E0);
    var pi: u32 = 0;
    for (0..4095) |x| {
        const v: u32 = (@as(u32, @intFromFloat(8192.0 / (1.0 + e))) + 1) >> 1;
        squash[x] = @intCast(v);
        while (pi <= v) : (pi += 1) stretch[pi] = @intCast(@as(i32, @intCast(x)) - 2047);
        e *= @bitCast(SQUASH_STEP);
    }
}

// Decode an nbits-bit field, one bit at a time. Each bit's probability is the logistic
// mix of the model entries chosen by the N contexts (field bits use one context, name
// characters three); after the bit is known, each weight moves by error * stretch(p)
// and each entry steps toward the bit.
noinline fn symN(comptime N: usize, ctx: [N]u32, nbits: u5) u32 {
    var node: u32 = 1;
    const top = @as(u32, 1) << nbits;
    while (true) {
        var stretched: [N]i32 = undefined;
        var slot: [N]u32 = undefined;
        // The packer asserts |sum| < 2^31 (measured max 2.2e8), so 32-bit is exact here.
        var dot: i32 = 0;
        inline for (0..N) |k| {
            const s: u32 = ((ctx[k] +% node *% HASH_A) *% HASH_B) >> (32 - MODEL_BITS);
            slot[k] = s;
            stretched[k] = stretch[(model[s] >> 4) ^ 0x800];
            dot +%= stretched[k] *% (weights[k] +% MIX_W0);
        }
        var d: i32 = dot >> 16;
        if (d < -2047) d = -2047;
        if (d > 2047) d = 2047;
        // 12-bit probability that the bit is 1, from 1 to 4095.
        const p: u32 = squash[@intCast(d + 2047)];
        const bound = (rc_range >> 12) * p;
        const bit: u32 = @intFromBool(rc_code < bound);
        if (bit != 0) {
            rc_range = bound;
        } else {
            rc_code -= bound;
            rc_range -= bound;
        }
        while (rc_range < (1 << 24)) {
            rc_range <<= 8;
            rc_code = (rc_code << 8) | packed_list[rc_in];
            rc_in += 1;
        }
        const one: i32 = @as(i32, @intCast(bit)) << 12;
        const err: i32 = one - @as(i32, @intCast(p));
        inline for (0..N) |k| {
            weights[k] +%= (stretched[k] *% err) >> MIX_SHIFT;
            const e = model[slot[k]];
            var q: i32 = (e >> 4) ^ 0x800;
            const n = e & 15;
            q += ((one - q) *% rate[n]) >> 16;
            model[slot[k]] = @intCast((q ^ 0x800) << 4 | (n + @intFromBool(n < MODEL_LIMIT)));
        }
        node = node * 2 + bit;
        if (node >= top) break;
    }
    return node - top;
}

inline fn sym1(c0: u32, nbits: u5) u32 {
    return symN(1, .{c0}, nbits);
}
inline fn sym3(ca: u32, cb: u32, cc: u32, nbits: u5) u32 {
    return symN(3, .{ ca, cb, cc }, nbits);
}

noinline fn init() void {
    @branchHint(.cold);
    @memset(std.mem.asBytes(&fast), 0xFF);
    @memset(std.mem.asBytes(&leg), 0xFF);
    @memset(len_mask[0..32], 0xFF);
    buildSquash();
    // The model, hist and last tables live in the buffer's zeroed start; nothing to clear.
    rc_in = 4;
    rc_range = 0xFFFF_FFFF;
    rc_code = (@as(u32, packed_list[0]) << 24) | (@as(u32, packed_list[1]) << 16) |
        (@as(u32, packed_list[2]) << 8) | packed_list[3];
    var cp: u32 = 0;
    var nl16: usize = 0;
    var nl32: usize = 0;
    var hn: u32 = 1; // hist[0] is the always-zero sentinel
    for (0..ENTITY_COUNT) |_| {
        // Step from the previous code point: its bit length, then the bits below the top one.
        const nb = sym1(K, 5);
        var d = nb;
        if (nb > 1) {
            d = (@as(u32, 1) << @intCast(nb - 1)) + sym1(2 * K + nb, @intCast(nb - 1));
        }
        cp += d;
        var vn: u32 = 0;
        var vn2: u32 = 0;
        var val: u64 = utf8(cp, &vn);
        // A few values have a second code point.
        if (sym1(3 * K + @intFromBool(d == 0), 1) != 0) {
            val |= @as(u64, utf8(sym1(4 * K, 16), &vn2)) << @intCast(8 * vn);
        }
        val |= @as(u64, vn + vn2) << 56;
        const legacy = sym1(5 * K + @intFromBool(cp < 256), 1) != 0;
        // Names use a 6-bit alphabet: 0 ends the name, then digits, capitals, small letters.
        var name = std.mem.zeroes([40]u8);
        var n: u32 = 0;
        var h: u32 = 0;
        var mp: u32 = 0;
        var ml: u32 = 0;
        while (true) {
            // The match model predicts the character that followed the last place the
            // previous two were seen, and how many predictions in a row have been right.
            const c = sym3(
                8 * K + (h & 63),
                9 * K + (h & 4095),
                13 * K + (if (ml != 0) @as(u32, hist[mp]) else 0) * 16 + @min(ml, 3),
                6,
            );
            if (ml != 0 and hist[mp] == c) {
                ml += 1;
                mp += 1;
            } else {
                ml = 0;
            }
            hist[hn] = @intCast(c);
            hn += 1;
            if (c == 0) break;
            name[n] = @intCast(if (c < 11) c + &#39;0&#39; - 1 else if (c < 37) c + &#39;A&#39; - 11 else c + &#39;a&#39; - 37);
            n += 1;
            h = (h << 6) | c;
            if (n >= 2) {
                const l = &last[h & 4095];
                if (ml == 0 and l.* != 0) {
                    mp = l.*;
                    ml = 1;
                }
                l.* = @intCast(hn);
            }
        }
        const nm: [*]const u8 = &name;
        if (n <= FAST_MAXLEN) {
            var key = ld64(nm) | (@as(u64, &#39;;&#39;) << @intCast(8 * n));
            var e: [*]Entry = @ptrCast(&fast[@intCast((key *% FAST_MUL) >> (64 - FAST_BITS))]);
            while (e[0].key != EMPTY) e += 1;
            e[0] = .{ .key = key, .val = val };
            if (legacy) {
                key = ld64(nm) | (@as(u64, n) << 56);
                e = @ptrCast(&leg[@intCast((key *% LEG_MUL) >> (64 - LEG_BITS))]);
                while (e[0].key != EMPTY) e += 1;
                e[0] = .{ .key = key, .val = val };
                const s = legSlot(key); // only the first two bytes matter
                if (leg_max[s] < n) leg_max[s] = @intCast(n);
            }
        } else {
            // The hash covers the name's first 7 bytes (with a ';' patched in at
            // name[n], as the lookup's masked load sees it) and the length.
            name[n] = &#39;;&#39;;
            var hh = longHash(ld64(nm) & 0x00FF_FFFF_FFFF_FFFF, n);
            name[n] = 0;
            if (n <= 15) {
                const l = &longs16[nl16];
                nl16 += 1;
                l.name = name[0..16].*;
                l.val = val | @as(u64, n) << 59;
                while (long_slots[hh] != 0) hh = (hh + 1) & LONG_MASK;
                long_slots[hh] = @intCast(nl16);
            } else {
                const l = &longs32[nl32];
                nl32 += 1;
                l.name = name[0..32].*;
                l.val = val | @as(u64, n) << 59;
                while (long_slots[hh] != 0) hh = (hh + 1) & LONG_MASK;
                long_slots[hh] = @intCast(0x8000 | nl32);
            }
        }
    }
    ready = true;
}

export fn input_ptr() u32 {
    return @intCast(@intFromPtr(input));
}
export fn input_utf8_cap() u32 {
    return INPUT_CAP;
}
export fn output_ptr() u32 {
    return @intCast(@intFromPtr(output));
}
export fn output_utf8_cap() u32 {
    return OUTPUT_CAP;
}

export fn render(n: u32) u64 {
    if (!ready) init();
    const size: usize = @min(n, INPUT_CAP);
    const src: [*]u8 = input;
    const dst: [*]u8 = output;
    const zero: V = @splat(0);
    st(src + size, zero);
    st(src + size + 16, zero);
    st(src + size + 32, zero);
    st(src + size + 48, zero);

    // Stage 1 lists the position of every `&` in a window; stage 2 copies the text between
    // references and decodes each one.
    var e: usize = 0;
    var o: usize = 0;
    var base: usize = 0;
    while (base < size) {
        const lim = if (size - base > WINDOW) base + WINDOW else size;
        var count: usize = 0;
        while (true) {
            const ma: u64 = eqMask(ld(src + base), &#39;&&#39;);
            const mb: u64 = eqMask(ld(src + base + 16), &#39;&&#39;);
            const mc: u64 = eqMask(ld(src + base + 32), &#39;&&#39;);
            const md: u64 = eqMask(ld(src + base + 48), &#39;&&#39;);
            var mm: u64 = ma | mb << 16 | mc << 32 | md << 48;
            const cnt: usize = @popCount(mm);
            const b: u32 = @intCast(base);
            const out: [*]u32 = pos[0..].ptr + count;
            // Entries past cnt are garbage and are overwritten or ignored.
            inline for (0..4) |k| {
                out[k] = b + @ctz(mm);
                mm &= mm -% 1;
            }
            if (cnt > 4) {
                @branchHint(.unlikely);
                inline for (4..8) |k| {
                    out[k] = b + @ctz(mm);
                    mm &= mm -% 1;
                }
                var k: usize = 8;
                while (mm != 0) : (k += 1) {
                    out[k] = b + @ctz(mm);
                    mm &= mm -% 1;
                }
            }
            count += cnt;
            base += 64;
            if (base >= lim) break;
        }

        var q: usize = 0;
        while (q < count) : (q += 1) {
            var i: usize = pos[q];
            // Output and input are one buffer, so the compiler cannot move a load across
            // a store: everything this reference needs is loaded before the first store.
            const w = ld64(src + i + 1);
            // Copy the text since the last reference; src[i] is `&` and i < size.
            {
                const len = i - e;
                const ca = ld(src + e);
                const cb = ld(src + e + 16);
                st(dst + o, ca);
                st(dst + o + 16, cb);
                if (len > 32) {
                    var j: usize = 32;
                    while (true) {
                        st(dst + o + j, ld(src + e + j));
                        j += 16;
                        if (j >= len) break;
                    }
                }
                o += len;
            }
            done: {
                if (@as(u8, @truncate(w)) == &#39;#&#39;) numeric: {
                    // Decimal and hexadecimal share one branch-free path: classify 8 bytes,
                    // take the digits before the first other byte, and fold them pairwise.
                    const hex = (@as(u8, @truncate(w >> 8)) | 0x20) == &#39;x&#39;;
                    var p = src + i + 2 + @intFromBool(hex);
                    const d = ld64(p);
                    const t = d ^ 0x3030303030303030;
                    const digit = ~(((t & ~HIGH) +% 0x7676767676767676) | t) & HIGH;
                    const y = d | 0x2020202020202020;
                    const ty = y & ~HIGH;
                    const letter = (ty +% ONES * (0x80 - &#39;a&#39;)) & ~(ty +% ONES * (0x7F - &#39;f&#39;)) & ~y & HIGH;
                    const stop = ~(digit | (if (hex) letter else 0)) & HIGH;
                    var v: u32 = 0;
                    var end: usize = undefined;
                    if (stop != 0) {
                        const nb: u32 = @ctz(stop) & 0x38;
                        if (nb == 0) break :numeric;
                        var x = ((d & 0x0F0F0F0F0F0F0F0F) +% 9 * (letter >> 7)) << @intCast(64 - nb);
                        x = (x *% @as(u64, if (hex) 16 else 10) +% (x >> 8)) & 0x00FF00FF00FF00FF;
                        x = (x *% @as(u64, if (hex) 256 else 100) +% (x >> 16)) & 0x0000FFFF0000FFFF;
                        v = @truncate(x *% @as(u64, if (hex) 65536 else 10000) +% (x >> 32));
                        end = @intFromPtr(p) - @intFromPtr(src) + (nb >> 3);
                    } else {
                        // Eight or more digits: saturate past U+10FFFF.
                        @branchHint(.unlikely);
                        const radix: u32 = if (hex) 16 else 10;
                        while (true) {
                            var c: u32 = @as(u32, p[0]) -% &#39;0&#39;;
                            if (c > 9) {
                                c = (@as(u32, p[0]) | 0x20) -% &#39;a&#39; +% 10;
                                if (c < 10) c = 16;
                            }
                            if (c >= radix) break;
                            v = v * radix + c;
                            if (v > 0x10FFFF) v = 0x110000;
                            p += 1;
                        }
                        end = @intFromPtr(p) - @intFromPtr(src);
                    }
                    i = end + @intFromBool(src[end] == &#39;;&#39;);
                    if (v -% 0x80 < 0x20) {
                        v = c1[v - 0x80];
                    } else if (v == 0 or v -% 0xD800 < 0x800 or v > 0x10FFFF) {
                        v = 0xFFFD;
                    }
                    // Lay the bits out as four UTF-8 bytes, then drop the unused leading ones.
                    const len: u32 = 1 + @as(u32, @intFromBool(v > 0x7F)) + @intFromBool(v > 0x7FF) + @intFromBool(v > 0xFFFF);
                    const bits = v >> 18 | (v >> 12 & 0x3F) << 8 | (v >> 6 & 0x3F) << 16 | (v & 0x3F) << 24;
                    const word = bits >> @intCast(32 - 8 * len) | utf8_marks[len];
                    st32(dst + o, if (len == 1) v else word);
                    o += len;
                    break :done;
                } else {
                    const t = w ^ (ONES * &#39;;&#39;);
                    const z = (t -% ONES) & ~t & HIGH;
                    if (z != 0) {
                        // Bit index of the `;` byte; the key is the name and its `;`.
                        const kb: u32 = @ctz(z) & 0x38;
                        const key = w & ((@as(u64, 2) << @intCast(kb + 7)) -% 1);
                        var en: [*]const Entry = @ptrCast(&fast[@intCast((key *% FAST_MUL) >> (64 - FAST_BITS))]);
                        while (true) {
                            const k = en[0].key;
                            if (k == key) {
                                const val = en[0].val;
                                st64(dst + o, val);
                                o += @intCast(val >> 56);
                                i += 2 + (kb >> 3);
                                break :done;
                            }
                            if (k == EMPTY) break;
                            en += 1;
                        }
                    } else {
                        // No `;` in 8 bytes: a name of 8 to 31 bytes needs its `;` in the next 24.
                        const s0 = ld(src + i + 1);
                        const s1 = ld(src + i + 17);
                        const sm: u32 = eqMask(s0, &#39;;&#39;) | eqMask(s1, &#39;;&#39;) << 16;
                        if (sm != 0) {
                            const len: usize = @ctz(sm);
                            if (longName(w, s0, s1, len, dst, &o)) {
                                i += 2 + len;
                                break :done;
                            }
                        }
                    }
                    // Without `;`, the longest legacy name that is a prefix.
                    var k: usize = leg_max[legSlot(w)];
                    while (k >= 2) : (k -= 1) {
                        // The length is part of the key, so a NUL after a shorter name cannot match.
                        const key = (w & ((@as(u64, 1) << @intCast(8 * k)) - 1)) | @as(u64, k) << 56;
                        var en: [*]const Entry = @ptrCast(&leg[@intCast((key *% LEG_MUL) >> (64 - LEG_BITS))]);
                        while (true) {
                            if (en[0].key == key) {
                                st64(dst + o, en[0].val);
                                o += @intCast(en[0].val >> 56);
                                i += 1 + k;
                                break :done;
                            }
                            if (en[0].key == EMPTY) break;
                            en += 1;
                        }
                    }
                }
                dst[o] = &#39;&&#39;;
                o += 1;
                i += 1;
            }
            e = i;
        }
    }
    // Without a reference the input is the output.
    if (e == 0) return @as(u64, @intFromPtr(input)) << 32 | size;
    var j = e;
    while (j < size) : (j += 16) st(dst + o + (j - e), ld(src + j));
    o += size - e;
    return @as(u64, @intFromPtr(output)) << 32 | o;
}

Run it on the example input with qipdo (Node.js 22 or newer):

printf '%s' 'Fish &amp; chips &mdash; caf&eacute; &lt;3 &#x1F600; &copy 2026 &notit;' | npx qipdo calculated.world components/53c3fa4fe994b3b307c5ad8019f6af961a21116d70eed288571d9aa6f162ef31.wasm

The #1 entry's exact memory profile reached from the other direction: the full 4,096-slot fast hash table (40% load) kept, and the range coder's model shrunk to 2^13 entries so its working set plus the 104,000-byte input cap fit 256 KiB total - four pages, L2-resident like the leading C entry. Long names of 8-15 bytes moved to a 16-byte-entry tier (one vector compare); 16+ byte names keep the 32-byte tier. Same two-stage SIMD decoder, in-place shared buffer, branchless stage 1, 16-byte-loop tail copy.

Build details

Compiler, size, memory and checksum
Submitted
27 November 2026 at 16:55 UTC
Language
Zig
Compiler
zig 0.17.1 zig build-exe -target wasm32-freestanding -mcpu=generic+simd128+bulk_memory -O ReleaseSmall -fno-entry -rdynamic -fstrip --stack 1024 --initial-memory=262144 --max-memory=262144; wasm-opt -O3 --enable-simd --enable-bulk-memory --enable-nontrapping-float-to-int
Size
12.3 KiB, gzip 10.1 KiB
Download
~150 ms on Slow 4G (Lighthouse’s 1.6 Mbps, 150 ms round trips)
Memory
4 pages (256 KiB)
SIMD
yes
AI models
GLM-5.3
Functions, loops, branches and other static counts
Count Value
Functions 10
Instructions 2,397
Loops 24
Conditional branches 57
Indirect calls 0
Memory loads 63
Memory stores 61
SIMD instructions 64
Potentially trapping instructions 153

Instruction sites in the binary, not executions, counted by qip.dev’s wasm-counts.