// unescape_html in C 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 4 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. // The lookup tables are built from a packed entity list: see `sym` and `init`. // // Memory is 4 pages (256 KiB). There is one buffer: the input sits 20 KB into it and the // output is written from its start, so the output follows the input through the same // memory. A reference grows by at most a fifth (`≫⃒`, 5 bytes, becomes 6), so the output // always stays at least 64 bytes behind the next unread byte. The buffer holds 104,000 // bytes of input, the most the official suite and benchmark send. The tables are unpacked // on the first call, when the input is already in the buffer, so the unpacker's working // memory is what is still free then: the part of the buffer before the input, 3.5 KB in // front of it, and the position list of stage 1. // // Build: zig cc -target wasm32-freestanding -O3 -msimd128 -mbulk-memory -nostdlib // -Wl,--no-entry -Wl,--export-memory -Wl,--initial-memory=262144 -Wl,--max-memory=262144 // -Wl,-z,stack-size=4096 -Wl,--strip-all -o unescape-4p.wasm unescape-4p.c #include #include typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; #define INPUT_CAP 104000u // The largest growth is `≫⃒` (5 bytes) becoming 6 bytes. #define OUTPUT_CAP (INPUT_CAP / 5 * 6 + 8) #define PAD 128 // Where the input starts in the buffer: the largest growth plus 64 bytes, a multiple of 64. #define IN_OFF 20864 // Bytes in front of the buffer that only the unpacker uses; a multiple of 64. #define EXTRA 3520 #define FAST_BITS 12 #define FAST_TAIL 32 #define LEG_BITS 8 #define LEG_TAIL 16 #define LONG_BITS 11 #define EMPTY (~(u64)0) // Bytes of input per stage-1 window; a multiple of 64. #define WINDOW 2048 #define HIST_SIZE 16130 #define ENTITY_COUNT 2125 #define LONG_COUNT 506 #define MODEL_BITS 12 #define MODEL_LIMIT 5 #define FAST_MUL 0xa59d054b12f5b905ull #define LEG_MUL 0xf880d97d1854e551ull #define LONG_MUL 0xa0c2995f40498cb3ull static const u8 packed[8672] = { 222,225,26,184,203,87,79,214,211,103,216,158,195,33,70,24,169,238,204,58,244,185,119,108, 146,116,139,203,255,201,162,91,13,56,106,232,129,134,56,112,97,22,194,216,138,236,242,206, 115,245,235,161,178,223,141,108,158,147,126,212,62,84,125,184,58,63,212,76,110,62,68,8, 67,108,12,144,113,190,155,186,178,20,216,211,48,132,119,203,74,224,28,141,39,155,77,38, 217,207,254,235,124,99,163,153,141,56,231,13,85,22,41,134,90,38,102,115,199,37,170,97, 54,23,78,158,224,168,90,237,94,95,168,203,230,179,231,97,163,179,110,32,44,71,85,12, 123,176,123,235,5,92,78,190,64,117,121,237,223,202,205,157,155,73,152,18,139,85,114,129, 150,163,199,226,71,39,51,190,45,157,244,186,9,142,32,163,208,0,118,252,236,231,247,240, 154,139,14,236,54,251,24,93,63,110,42,197,190,90,30,135,251,219,197,98,126,85,152,72, 183,78,134,159,175,178,148,224,55,97,177,12,124,33,174,119,108,96,60,229,2,225,115,29, 153,113,235,233,4,13,105,51,101,2,176,150,50,4,155,145,232,184,165,206,45,180,68,170, 138,30,90,228,61,194,220,19,44,120,79,64,153,11,30,150,159,11,136,222,155,251,121,200, 153,36,104,170,152,8,242,198,77,195,238,201,76,73,190,10,30,91,150,218,52,124,113,212, 30,177,126,94,138,77,9,32,17,62,226,244,193,201,39,61,81,83,64,204,111,81,220,218, 179,182,234,86,93,203,209,139,251,211,41,120,60,154,189,136,21,32,67,192,230,83,137,213, 245,186,89,66,157,176,87,225,211,172,131,156,165,47,250,218,147,64,58,144,187,35,53,229, 157,64,188,103,20,230,218,101,116,76,79,49,232,16,76,102,162,225,161,174,169,32,117,185, 117,206,29,34,21,155,129,148,155,56,169,235,216,16,106,205,107,212,51,112,195,186,68,188, 126,51,81,204,92,42,162,193,84,137,110,125,135,186,8,35,47,216,90,200,131,72,205,160, 187,25,156,26,84,217,153,68,156,239,244,124,123,180,231,184,168,109,104,94,172,193,248,240, 38,23,39,248,73,53,84,251,41,46,163,21,171,181,159,59,34,27,159,187,96,107,9,215, 14,50,173,18,57,91,144,10,47,163,221,214,27,155,76,216,95,29,62,106,7,237,58,245, 83,252,9,185,72,19,160,73,203,183,146,193,167,147,182,151,233,193,86,117,139,162,110,121, 73,163,226,48,240,152,146,54,85,43,76,43,220,233,106,167,43,28,41,240,193,136,39,48, 93,67,193,76,225,124,236,189,69,165,34,147,235,242,210,251,141,212,63,133,23,3,66,200, 2,147,15,247,97,46,241,155,115,199,147,156,238,221,233,81,80,28,95,251,69,244,76,111, 156,234,74,60,47,183,58,94,195,120,148,34,156,242,172,66,124,5,126,69,135,28,173,95, 124,161,132,19,48,25,28,236,133,206,199,199,23,125,144,204,201,22,129,90,49,176,14,217, 36,76,1,198,206,231,30,194,175,112,208,145,122,234,230,143,179,71,74,178,101,80,88,168, 25,212,101,212,2,205,40,128,177,229,63,19,106,82,253,54,127,23,141,182,85,73,118,72, 27,89,11,62,119,227,179,73,237,180,32,48,188,32,131,53,19,26,114,146,83,251,77,104, 110,109,1,3,244,105,175,247,230,213,81,65,185,253,152,87,44,157,16,169,54,27,125,219, 219,228,110,222,6,117,206,172,148,96,18,109,161,254,28,45,133,59,218,125,32,230,75,61, 150,170,166,133,129,92,231,61,227,87,110,210,226,231,244,81,165,24,227,241,231,43,45,143, 160,79,235,32,165,150,58,0,205,11,178,238,124,72,227,142,124,83,96,152,249,225,55,162, 171,13,36,139,221,213,162,141,37,255,47,88,10,230,205,249,171,105,207,179,233,134,200,126, 94,245,119,171,247,149,111,9,212,101,39,246,169,39,112,54,128,12,35,21,55,170,15,197, 105,74,121,22,179,227,160,223,221,70,85,167,47,227,0,164,99,243,142,42,160,31,234,116, 108,204,131,246,173,136,218,24,200,149,95,13,40,96,5,161,207,70,19,34,230,169,78,226, 71,210,6,237,158,198,98,246,149,201,86,24,121,241,45,233,76,182,38,132,87,126,31,168, 163,238,165,245,83,74,210,95,16,166,140,200,185,136,169,24,200,58,205,16,24,71,180,223, 46,239,247,11,200,114,177,49,135,207,199,93,244,134,193,184,8,43,126,120,77,145,13,247, 126,31,69,97,197,91,157,196,96,88,114,190,150,217,65,75,159,83,166,44,34,167,95,223, 253,55,181,182,45,179,196,77,156,162,69,243,91,92,168,130,187,86,247,230,25,255,27,143, 232,26,243,235,81,3,221,224,104,157,81,123,247,53,28,79,52,61,40,115,108,132,98,37, 52,177,137,130,248,146,197,118,97,119,83,241,243,250,20,181,57,29,81,123,9,117,195,166, 191,12,48,147,55,227,19,140,211,98,26,165,68,138,232,93,110,245,135,149,137,165,24,201, 43,20,71,164,75,93,248,41,83,238,118,61,245,232,235,98,30,63,30,2,109,248,144,175, 246,45,37,45,67,40,22,32,164,64,31,66,119,145,143,196,168,242,116,144,195,119,183,46, 210,230,98,82,148,83,95,158,23,209,165,185,78,18,72,124,44,12,179,255,91,218,18,217, 235,71,18,247,155,131,33,199,235,75,176,161,148,136,75,123,149,73,249,160,188,190,192,0, 220,128,42,116,34,206,192,160,134,228,184,217,59,116,233,203,102,237,87,120,78,34,118,139, 10,129,200,207,59,185,252,120,142,166,238,34,4,53,86,22,100,237,26,215,150,128,240,229, 170,74,232,213,58,99,168,62,245,202,143,14,206,254,219,78,52,151,253,137,191,93,5,84, 42,174,124,97,61,228,125,141,149,15,159,17,132,27,223,68,78,226,47,240,4,144,98,0, 94,129,20,7,51,13,76,230,215,124,98,67,79,93,186,249,124,93,158,82,249,40,141,229, 243,137,76,62,97,43,24,8,118,58,19,124,228,250,93,54,142,145,26,233,95,201,226,146, 66,135,110,170,14,163,113,215,118,117,147,87,142,186,10,248,250,27,211,93,0,90,231,108, 140,208,57,149,151,173,23,46,128,13,18,72,218,155,128,23,127,90,7,185,161,45,23,210, 239,249,90,98,4,236,153,236,250,204,233,0,127,160,238,181,154,240,163,74,203,26,215,77, 6,195,22,135,30,245,66,142,237,79,191,145,190,32,20,154,60,89,157,0,2,201,32,110, 50,198,252,71,67,94,157,199,124,122,199,91,111,74,203,112,204,125,4,18,108,172,32,202, 206,15,110,71,227,54,175,97,167,122,168,29,32,35,49,58,249,149,145,158,189,111,2,203, 158,24,223,54,33,145,173,51,79,191,12,2,251,236,105,203,183,249,169,187,169,228,108,43, 64,242,137,73,224,200,13,21,41,132,156,244,21,146,72,77,82,245,195,187,203,237,101,79, 17,42,155,114,41,13,165,122,154,252,140,255,209,184,135,210,171,54,49,247,124,127,34,112, 203,243,195,136,123,56,7,40,106,254,40,149,129,155,13,2,210,89,193,85,237,84,201,223, 179,56,211,126,203,236,0,47,236,15,207,41,150,202,248,58,79,3,21,247,162,152,232,150, 120,74,74,76,202,106,97,176,144,125,116,81,170,111,5,3,170,249,36,180,207,48,117,16, 20,159,223,21,208,67,106,225,143,187,204,100,91,216,8,26,135,81,10,3,180,165,84,25, 175,46,167,150,1,60,144,80,149,216,231,24,177,91,232,51,217,15,62,110,59,174,69,109, 93,29,38,155,217,192,151,88,248,59,133,111,42,173,8,8,69,215,154,218,70,189,210,240, 195,61,12,210,71,103,117,42,4,95,115,153,255,130,220,49,29,220,66,114,208,21,122,176, 215,211,108,74,54,149,133,197,191,122,235,129,25,10,28,167,116,195,43,116,1,59,214,3, 5,72,154,81,225,178,191,106,172,103,30,65,178,34,84,105,234,58,52,216,147,153,185,210, 117,36,124,197,188,213,207,245,91,33,210,158,78,75,36,109,29,28,2,146,48,2,181,184, 87,176,164,213,116,58,239,237,134,61,183,167,9,101,179,186,128,235,26,189,58,159,42,249, 53,100,38,191,237,48,255,61,112,182,239,240,189,156,233,253,14,254,214,57,39,209,189,166, 120,0,11,149,109,253,130,33,220,212,36,87,124,16,255,14,153,49,0,226,210,196,141,117, 99,228,2,45,213,8,107,234,71,6,247,8,211,79,202,66,158,113,214,10,6,43,226,196, 22,90,93,120,65,61,196,236,51,141,114,139,104,213,26,222,252,70,168,0,216,210,91,226, 161,157,189,184,169,126,82,110,40,68,112,223,255,231,21,62,137,30,99,140,129,194,254,124, 88,105,245,105,168,67,179,246,35,55,116,96,53,98,196,126,188,63,220,49,201,231,246,225, 196,114,174,231,176,20,161,2,235,117,182,237,15,217,89,46,60,176,224,49,206,170,238,3, 6,200,212,75,210,177,19,81,48,182,169,214,99,109,175,43,101,133,236,115,206,195,213,82, 160,29,203,174,247,179,4,150,43,14,181,184,126,61,39,225,227,238,24,95,123,20,226,53, 174,231,204,123,160,8,156,170,227,83,226,116,229,70,176,154,100,53,57,141,17,129,125,136, 60,82,212,95,198,254,14,94,240,185,21,104,73,85,132,20,228,116,237,222,2,169,126,79, 163,217,173,51,56,136,178,147,228,144,9,86,169,213,66,57,66,193,154,92,120,55,72,176, 8,78,78,101,11,174,124,99,61,236,18,213,172,77,217,36,236,58,139,199,143,206,173,88, 3,61,255,247,153,59,69,22,196,127,109,7,99,91,58,116,61,221,246,153,191,11,103,219, 196,74,164,76,14,28,8,198,97,39,102,191,107,109,171,243,100,82,209,254,66,37,61,209, 73,24,160,185,13,125,58,199,100,100,113,134,172,117,174,194,154,144,82,191,86,146,109,33, 8,65,17,28,45,216,51,184,253,28,136,206,102,79,173,114,169,113,218,89,18,48,65,66, 138,23,190,176,133,215,14,235,122,139,80,65,20,254,131,160,149,128,117,230,31,227,220,168, 194,11,149,172,131,109,192,238,162,78,83,176,75,134,51,229,131,236,221,19,155,65,29,128, 136,95,63,244,95,39,246,197,248,190,233,76,200,134,112,85,254,63,216,64,44,21,187,117, 104,186,114,92,229,48,220,226,163,94,153,6,218,106,84,102,128,238,102,153,34,222,74,235, 183,90,99,210,228,201,248,114,83,229,145,250,42,75,5,159,236,183,190,119,172,188,119,170, 3,93,172,152,233,226,167,196,33,190,118,107,231,72,38,16,54,73,238,215,167,65,161,90, 202,88,179,160,235,248,91,40,70,127,210,226,99,29,249,0,158,203,247,151,47,144,91,171, 7,129,63,200,206,235,195,254,71,114,170,28,171,205,81,165,114,113,181,49,242,54,169,6, 107,133,71,102,97,113,162,189,157,241,240,152,30,154,99,156,29,20,225,37,26,221,26,56, 255,194,62,87,76,243,167,179,222,219,168,255,8,184,88,70,243,156,120,12,159,245,242,21, 18,25,102,16,192,10,52,173,243,231,182,163,160,26,241,83,71,210,118,49,115,190,50,206, 168,182,177,190,132,250,54,144,72,215,51,226,158,31,41,148,217,251,6,239,169,215,214,3, 123,49,68,186,111,235,254,203,67,206,244,132,66,120,86,109,98,34,190,246,91,69,16,28, 33,36,21,94,52,43,65,235,226,93,71,206,58,219,42,46,50,101,206,8,157,114,126,254, 94,182,146,91,225,152,176,125,105,151,146,188,154,78,153,67,192,53,223,201,66,139,113,238, 12,55,131,165,79,123,27,19,49,127,169,89,253,168,199,88,138,15,211,14,154,215,176,200, 224,133,91,34,150,18,94,219,156,188,117,4,129,242,91,207,34,130,245,20,167,149,188,27, 92,119,29,80,114,248,233,200,206,29,16,244,246,27,136,182,173,66,251,141,138,170,24,244, 69,253,158,82,100,165,163,159,161,105,238,193,169,100,61,142,90,207,156,160,110,126,150,197, 26,68,13,20,129,78,126,5,79,155,196,20,159,189,90,57,223,128,184,230,196,100,168,140, 210,98,132,141,61,218,79,237,37,84,79,24,198,136,213,233,255,180,41,166,67,213,217,227, 161,186,98,52,68,140,199,54,85,67,121,164,209,255,55,113,227,66,50,150,46,29,151,180, 173,220,16,139,254,92,244,172,64,110,249,1,246,51,195,217,13,228,231,24,80,33,150,33, 57,7,31,90,10,231,181,19,87,27,14,35,191,147,172,33,111,254,226,49,97,233,245,87, 49,3,8,113,177,83,46,173,106,65,9,196,8,102,238,247,243,52,29,53,193,104,119,42, 80,116,135,160,18,11,128,212,221,105,125,119,91,0,117,231,137,99,3,1,180,248,163,176, 166,35,206,73,238,237,236,120,174,204,90,62,89,237,127,88,225,65,188,148,99,203,32,117, 143,149,190,218,162,87,171,182,58,64,125,70,240,18,169,187,211,66,111,156,223,153,13,148, 190,70,142,93,232,203,78,87,74,93,56,77,162,130,123,45,89,207,130,130,131,218,61,72, 83,105,166,236,69,77,174,51,236,108,248,1,162,176,209,156,214,4,21,112,116,90,97,115, 38,2,81,191,88,146,131,54,0,170,128,31,151,172,173,103,96,117,84,70,10,150,126,6, 26,54,84,205,38,109,173,108,120,30,162,252,126,110,68,208,134,22,77,72,176,22,163,146, 108,70,92,178,72,200,197,133,255,158,148,73,224,175,173,225,136,233,229,14,217,169,163,49, 223,117,15,68,36,191,174,96,67,218,93,135,227,115,131,185,209,125,38,6,170,220,89,195, 245,43,108,96,221,234,197,234,107,170,123,111,182,159,244,34,141,91,12,249,242,173,195,181, 69,43,198,205,78,32,187,170,206,123,183,175,222,198,249,35,34,84,217,215,29,19,166,145, 38,44,246,188,106,151,37,197,141,103,70,189,79,103,114,198,167,46,100,76,220,161,30,121, 229,108,63,155,33,19,64,192,33,51,7,12,164,151,166,185,254,50,205,179,237,179,108,10, 208,219,22,72,80,104,76,84,39,242,164,49,30,158,138,29,250,171,138,242,235,203,125,101, 157,169,29,138,109,251,8,218,38,216,233,170,141,112,24,195,8,82,245,143,208,21,243,241, 61,88,169,209,146,163,188,125,151,55,85,86,24,107,116,71,96,137,245,114,178,103,33,60, 102,134,201,119,62,140,3,168,98,132,25,171,221,55,38,86,47,193,50,121,160,179,113,239, 202,25,236,71,174,64,130,120,254,12,116,87,173,76,82,27,1,159,63,8,89,159,69,134, 164,55,116,100,45,37,18,174,199,143,249,221,216,154,68,201,96,110,234,251,98,62,109,196, 239,227,229,219,226,240,161,231,212,234,41,20,200,29,237,115,184,251,251,180,123,120,11,152, 65,63,216,138,129,204,5,65,25,122,255,123,151,71,127,154,183,88,45,221,187,59,217,244, 80,113,78,246,198,32,156,255,222,14,160,26,17,213,42,88,82,121,210,113,228,249,246,255, 225,121,247,69,129,80,178,4,182,234,7,141,126,226,70,123,145,130,150,162,90,107,167,99, 73,146,99,0,209,220,76,247,251,11,214,91,71,5,94,198,7,6,179,246,150,22,119,252, 93,213,191,151,49,87,44,212,145,50,87,48,39,158,234,95,142,44,153,241,139,223,70,168, 225,59,205,114,201,118,128,9,206,83,166,171,24,128,45,15,104,230,59,171,226,143,29,9, 86,78,125,224,81,96,7,173,139,186,37,64,14,163,70,242,153,52,107,189,128,181,106,147, 210,215,1,185,173,241,63,31,84,115,137,186,57,8,83,158,0,7,202,241,109,210,66,124, 24,26,206,153,14,79,54,115,129,119,1,78,54,130,50,152,216,194,29,138,6,157,155,238, 161,42,228,246,74,134,221,107,152,228,107,173,41,144,224,21,213,250,29,34,36,16,203,92, 84,46,115,120,141,15,208,29,92,124,89,154,137,183,194,122,155,29,178,103,154,230,91,101, 34,229,35,208,234,35,230,146,98,87,121,225,149,3,181,85,88,139,91,69,186,193,133,126, 51,69,223,225,37,231,25,238,180,114,40,111,249,252,163,134,77,37,86,83,96,190,217,27, 19,139,170,82,112,50,64,224,186,132,23,140,75,43,58,45,135,103,134,146,196,16,44,58, 123,202,153,108,33,10,219,71,91,28,119,113,48,81,127,101,208,140,196,151,234,98,44,59, 79,171,79,124,182,128,239,223,67,118,59,95,33,162,134,65,55,243,251,146,203,52,255,198, 164,196,48,15,250,56,34,23,85,77,150,101,123,229,162,15,150,52,6,96,123,94,196,216, 172,152,116,243,36,233,43,64,125,121,97,233,81,44,131,87,115,15,59,227,188,108,180,59, 164,194,36,200,9,118,3,4,53,221,112,199,150,211,1,221,203,30,19,122,172,132,93,60, 45,221,240,34,51,138,16,124,39,252,66,30,109,43,68,59,11,172,218,207,212,96,229,108, 69,89,213,65,74,138,252,217,41,89,38,47,227,169,203,190,240,55,142,69,233,33,84,55, 86,80,71,119,196,98,160,237,107,226,163,102,154,62,142,237,166,25,114,114,99,233,6,195, 181,111,187,217,53,45,36,19,129,109,239,208,69,152,72,6,83,80,55,203,53,184,26,54, 254,217,16,194,32,112,59,115,97,154,246,63,142,187,57,204,85,239,104,118,35,59,5,192, 45,233,180,93,111,198,169,160,65,179,220,223,163,157,150,84,100,31,16,245,69,167,204,28, 138,135,148,36,208,239,74,117,10,52,61,214,250,91,238,154,125,44,85,173,123,95,39,186, 179,71,20,88,67,135,118,69,229,135,25,57,231,41,182,103,211,177,208,188,127,209,73,146, 214,235,246,74,169,57,144,155,187,227,29,186,81,80,55,21,117,171,200,7,23,75,54,232, 243,109,59,79,254,196,58,254,113,162,49,164,123,125,126,144,93,133,200,224,67,35,167,142, 148,29,14,104,141,114,84,153,25,159,216,202,130,190,116,42,219,167,200,81,181,71,176,152, 220,240,81,103,127,231,13,204,128,33,180,123,58,74,198,181,161,24,118,87,117,152,49,158, 159,47,79,231,41,47,54,110,223,173,147,40,111,177,157,251,186,226,192,73,58,205,252,147, 148,165,255,118,146,226,50,53,75,17,220,228,57,130,254,124,184,193,126,38,40,54,43,227, 191,83,173,44,148,6,212,231,60,246,230,17,129,175,160,61,1,110,212,231,170,201,233,64, 161,248,56,134,148,249,218,28,25,1,51,11,150,252,42,4,75,8,14,212,31,85,141,149, 188,196,145,104,0,183,214,203,106,133,118,43,31,197,73,250,53,45,170,157,171,74,135,159, 3,162,167,123,49,248,144,143,71,143,25,212,227,58,190,53,75,114,56,183,168,82,222,49, 147,239,228,136,128,73,90,40,73,79,159,27,118,243,123,247,219,195,219,207,230,217,80,99, 82,101,144,20,120,50,168,78,157,236,124,228,82,219,188,33,172,51,84,233,138,187,110,111, 100,180,109,50,214,193,222,149,229,125,54,162,220,70,173,31,64,116,195,203,111,116,250,105, 29,140,89,46,115,243,192,35,149,209,30,93,43,108,20,107,97,44,134,34,76,248,223,86, 60,21,236,154,245,65,144,198,151,171,251,175,145,231,213,181,63,55,198,135,1,43,21,138, 205,103,169,180,61,93,101,19,214,247,127,50,166,204,36,135,43,171,35,148,184,33,228,254, 59,93,60,213,157,40,190,167,171,201,87,251,120,202,127,207,194,165,5,95,229,70,72,224, 207,27,155,254,224,157,115,176,99,100,23,232,89,180,160,109,62,25,226,217,100,44,78,80, 229,59,117,227,18,112,190,226,215,157,178,168,74,141,180,221,82,52,115,165,210,157,143,16, 76,169,43,79,118,18,248,110,14,27,128,200,21,193,28,219,30,114,106,225,70,89,33,117, 223,194,63,147,168,11,16,31,48,235,12,72,133,207,88,143,156,51,84,51,50,225,226,244, 231,72,41,249,216,221,138,141,58,127,199,105,177,105,249,67,204,144,253,62,15,126,225,15, 8,177,30,19,88,69,6,248,60,127,249,38,133,225,29,230,28,229,190,176,176,189,228,252, 12,227,216,157,69,196,138,101,57,107,47,168,83,107,38,202,180,125,83,8,54,172,12,187, 74,177,140,98,197,21,0,119,46,108,101,144,116,56,87,254,185,150,9,51,112,184,106,131, 152,229,64,65,38,122,79,141,104,251,104,166,225,134,144,225,56,170,164,247,172,31,198,148, 126,49,245,157,73,5,204,152,129,57,254,15,118,94,198,143,225,226,166,70,16,7,116,78, 71,237,138,255,102,80,236,105,222,193,38,31,161,208,99,25,140,97,21,208,47,164,84,55, 70,157,241,179,18,52,79,239,156,179,63,107,211,235,42,24,148,148,43,75,139,127,59,214, 98,128,129,205,203,148,185,77,190,0,233,118,15,75,137,184,81,160,74,88,77,208,225,0, 90,7,31,166,196,105,4,239,105,46,253,79,194,185,59,147,214,112,115,75,225,220,200,34, 49,214,30,41,237,241,21,104,33,108,255,196,193,46,129,248,86,31,183,190,208,178,225,111, 40,246,57,127,116,35,111,219,157,98,216,133,104,231,231,178,122,111,163,191,142,60,139,206, 57,223,120,176,76,195,5,154,175,233,133,195,225,246,151,36,88,93,5,96,63,121,137,184, 222,96,215,254,36,62,92,209,204,156,61,137,233,160,38,12,211,119,235,196,211,92,60,220, 170,239,249,244,163,96,74,32,130,101,4,204,21,67,191,196,120,173,7,89,172,160,245,183, 58,109,134,63,196,73,252,197,57,203,66,249,165,193,55,137,100,123,108,87,114,131,109,201, 56,17,88,194,122,248,226,199,247,23,100,152,120,157,5,32,206,83,239,104,229,43,4,27, 141,202,142,169,50,119,182,46,27,175,103,94,155,99,71,10,36,10,95,44,127,189,41,234, 171,59,47,14,124,20,229,112,41,0,203,48,185,127,222,201,16,66,202,204,170,236,150,2, 236,182,77,97,120,174,191,249,214,250,143,52,163,46,154,255,76,9,18,79,52,218,150,121, 90,208,133,121,164,48,36,158,43,132,182,47,16,101,108,67,105,16,63,5,173,40,84,30, 93,200,231,71,178,76,5,121,141,94,181,119,74,254,187,159,151,98,247,73,101,109,82,24, 93,215,99,42,183,11,28,79,203,42,35,65,114,146,75,193,248,87,36,244,159,177,91,164, 165,183,0,232,43,100,169,81,23,175,147,124,32,31,134,81,196,251,247,229,232,104,0,21, 191,164,65,81,149,3,96,148,133,108,104,229,190,185,214,89,199,3,49,141,48,147,199,214, 5,93,232,128,194,184,208,116,164,65,186,46,0,139,240,30,37,62,56,187,89,203,163,84, 11,101,36,168,240,242,117,84,173,163,207,77,235,99,96,183,21,84,158,14,112,155,253,229, 130,227,78,172,123,46,220,70,159,182,82,139,90,36,243,234,4,20,75,177,158,47,216,186, 203,205,216,200,184,144,235,85,155,63,19,21,101,2,182,4,211,82,123,195,199,81,132,6, 42,115,198,16,176,45,68,173,55,104,249,83,97,97,223,48,243,15,55,249,209,128,63,22, 90,114,9,49,145,49,75,202,48,117,199,53,149,172,150,210,228,173,66,140,249,191,155,205, 135,45,178,159,35,11,65,180,31,51,190,126,128,149,155,157,211,180,156,123,92,62,118,242, 100,137,148,108,104,110,238,201,60,195,116,42,53,149,163,118,211,188,3,82,78,245,167,8, 86,239,121,234,119,39,23,230,187,8,203,29,133,5,99,187,226,162,151,163,103,97,143,103, 241,23,49,113,82,255,210,253,220,128,59,20,214,222,82,154,77,161,184,128,60,148,129,176, 14,130,143,1,19,238,87,93,174,120,7,122,45,15,181,124,114,73,64,245,220,40,54,83, 252,248,97,208,112,228,63,145,132,243,63,230,24,225,227,65,213,159,195,201,228,255,213,214, 122,173,178,246,97,201,113,80,57,67,14,228,39,175,144,11,165,189,221,65,238,94,45,59, 130,26,30,190,178,127,199,65,93,208,234,68,29,144,206,232,198,217,119,233,202,136,61,18, 92,148,216,128,108,17,153,46,173,183,24,19,67,170,110,3,212,207,135,63,242,109,129,95, 150,159,31,225,179,224,55,181,48,147,34,39,189,135,188,240,192,239,5,27,246,89,120,114, 56,188,189,247,242,161,33,193,231,58,3,40,186,202,49,226,162,81,36,251,246,27,250,163, 210,205,56,255,113,216,131,169,160,95,178,14,71,39,118,238,237,220,31,103,212,116,172,81, 94,223,208,96,93,146,30,127,201,44,83,23,247,23,136,125,30,180,120,86,146,191,172,70, 213,239,203,239,157,19,43,169,130,135,103,30,75,33,245,107,184,184,202,132,177,12,45,110, 119,217,107,239,223,97,7,232,195,95,59,230,213,30,243,254,167,208,151,143,51,117,160,37, 25,51,158,156,63,125,65,148,225,65,40,174,54,79,241,107,33,110,211,133,186,26,136,108, 167,201,226,251,139,183,205,160,152,138,197,87,147,177,189,43,136,77,176,171,125,151,74,182, 163,213,134,139,45,230,39,134,219,151,140,100,223,192,216,193,233,8,107,133,219,129,14,97, 75,53,255,138,189,123,195,91,50,229,11,53,24,38,210,234,159,13,103,100,202,225,161,34, 64,74,155,162,17,35,90,226,19,233,159,253,177,170,250,187,18,135,125,124,69,12,62,64, 254,27,107,108,96,50,255,2,23,190,210,104,44,33,233,159,64,211,0,117,138,154,49,43, 188,30,3,65,100,165,243,44,129,186,7,217,98,224,146,189,83,113,88,176,157,162,70,143, 203,121,65,129,225,108,30,155,63,76,212,100,190,137,166,70,161,248,187,209,23,75,240,183, 86,252,154,162,191,95,71,207,82,200,157,134,53,243,92,120,111,145,162,58,170,19,253,15, 146,246,60,144,140,69,202,187,29,119,224,188,145,199,243,121,252,108,165,253,66,122,174,9, 143,176,156,7,204,94,37,25,61,141,55,137,242,18,14,85,103,23,49,229,164,212,234,86, 229,45,114,169,67,223,168,232,6,138,163,196,168,252,123,108,36,194,13,69,128,0,34,236, 136,61,24,203,104,178,209,29,96,93,188,224,93,71,40,96,157,15,102,38,146,241,161,30, 147,129,120,239,50,250,22,255,167,212,111,83,29,117,27,251,17,51,11,167,144,10,73,215, 76,197,217,146,105,192,161,65,71,16,61,203,40,233,10,48,216,173,140,203,214,150,166,124, 151,236,49,243,132,60,171,5,227,252,168,222,3,22,197,147,148,233,169,113,229,176,178,57, 101,48,4,94,251,184,120,32,127,190,97,212,40,247,198,236,191,58,151,21,217,204,150,196, 104,46,237,239,5,133,126,50,166,59,88,115,234,75,35,245,111,241,230,124,27,55,92,42, 174,2,70,152,77,187,69,133,244,214,104,50,12,231,148,172,14,199,117,218,56,238,230,89, 200,141,221,228,245,177,1,35,121,38,114,129,20,216,126,29,54,242,140,27,96,101,62,192, 140,2,81,189,205,230,125,233,158,60,173,66,234,226,166,196,215,51,139,125,245,48,126,34, 251,12,219,96,168,39,248,227,217,32,60,155,180,101,124,237,182,223,114,246,215,48,112,127, 87,239,169,153,227,213,254,133,101,220,31,249,133,78,153,50,85,239,33,116,73,93,11,189, 78,145,121,175,76,162,18,116,143,115,44,210,36,96,223,222,193,250,133,89,220,219,225,164, 240,135,22,47,162,5,154,87,37,225,247,89,54,64,134,37,156,24,16,15,0,209,93,6, 6,124,138,76,1,217,135,4,216,144,34,213,240,114,134,92,145,45,28,43,87,117,223,126, 237,217,220,83,35,229,214,236,172,244,39,128,195,144,115,122,249,96,159,23,105,59,24,86, 183,80,149,223,135,89,102,96,180,147,4,203,104,194,120,187,8,247,62,229,149,185,244,156, 33,26,33,148,181,114,139,252,206,103,6,90,198,44,233,82,199,228,150,138,156,214,71,61, 72,178,145,83,51,206,24,217,60,208,88,174,233,41,52,155,127,251,183,45,59,239,109,230, 113,43,228,29,54,40,122,21,164,0,20,102,53,49,123,118,23,181,26,91,72,71,219,143, 216,158,185,86,39,75,86,5,32,49,16,71,143,237,246,76,121,76,54,52,84,66,223,169, 73,174,120,159,102,1,238,147,43,156,96,228,74,116,15,240,66,78,157,44,98,56,203,85, 229,227,81,171,22,63,76,154,54,196,88,63,228,4,121,140,127,212,43,9,99,46,149,224, 152,61,89,146,159,41,100,213,82,33,191,249,216,37,135,188,116,154,148,216,67,245,81,219, 43,205,123,31,222,22,245,247,245,139,247,112,175,139,79,25,14,122,128,211,22,101,216,239, 22,187,130,161,119,208,244,49,203,14,123,96,0,165,222,2,1,98,118,64,36,151,143,13, 76,254,179,255,121,190,197,2,28,191,35,131,129,57,185,165,162,128,8,127,213,12,216,80, 25,229,200,165,192,40,122,0,79,209,76,81,152,96,25,218,91,66,182,234,130,190,47,234, 161,138,115,164,160,233,128,61,37,242,93,207,232,197,89,244,13,242,156,211,198,239,158,253, 59,111,118,237,31,182,120,18,239,79,201,240,141,93,203,197,11,252,113,39,165,3,9,211, 199,198,206,240,28,5,201,128,248,237,20,60,87,230,17,103,160,107,39,196,61,81,175,247, 13,239,133,192,102,236,116,144,26,223,72,230,12,44,1,93,108,160,205,81,101,200,86,226, 1,43,94,233,36,150,34,118,86,203,6,150,191,134,0,73,153,173,132,2,46,44,76,158, 199,78,138,255,165,244,189,104,149,87,184,36,76,31,177,224,184,7,141,66,111,146,217,7, 46,231,25,221,88,78,159,95,149,60,239,113,130,123,19,227,55,31,230,236,114,217,235,191, 42,139,252,143,161,189,134,83,109,178,124,112,103,29,215,247,111,88,83,97,140,192,155,126, 105,214,226,152,240,78,63,244,116,118,178,155,128,18,117,132,119,175,42,72,15,230,150,19, 83,225,169,218,250,52,116,115,13,133,183,200,145,25,149,126,219,19,247,88,147,93,45,57, 29,64,166,1,165,46,118,191,208,56,49,203,225,109,71,1,135,152,131,6,181,239,78,30, 206,244,173,140,199,149,137,211,41,51,59,123,10,131,184,77,118,110,245,170,133,114,20,51, 79,171,152,13,52,249,152,9,78,113,149,40,157,24,141,214,27,98,235,85,54,156,115,232, 190,167,33,184,27,56,148,146,15,24,254,155,97,66,40,110,186,103,246,79,87,235,242,195, 216,44,219,68,76,80,230,210,51,191,169,118,33,37,227,47,62,49,102,137,193,175,4,190, 100,77,16,230,189,217,40,134,148,253,186,23,166,56,244,194,162,252,29,2,236,29,240,202, 54,218,163,95,144,88,192,81,244,37,51,75,28,37,136,27,11,91,241,209,117,122,207,98, 157,40,30,195,30,246,226,98,185,211,241,213,16,192,67,120,15,81,174,112,221,219,172,26, 70,36,136,20,184,54,122,50,10,169,250,126,158,171,254,152,165,19,93,75,53,217,213,238, 177,137,18,38,229,147,253,215,119,41,230,135,164,119,95,157,245,255,51,2,101,113,0,195, 31,74,9,84,134,30,20,217,246,141,72,85,123,80,77,119,22,147,231,189,6,245,237,214, 10,173,104,176,36,38,245,52,97,28,71,43,56,214,95,99,45,140,31,26,89,162,27,2, 66,62,137,217,80,205,52,140,33,72,11,58,84,147,185,15,9,240,16,35,132,223,211,196, 4,210,4,217,43,62,191,136,225,225,170,72,44,155,71,187,168,128,128,84,21,52,252,239, 251,105,162,149,112,169,88,213,116,210,209,220,20,166,108,33,203,92,236,246,124,140,136,188, 241,17,199,108,95,142,116,21,107,198,191,24,167,47,125,122,51,217,58,103,123,140,132,78, 73,150,119,89,75,123,40,61,28,81,208,48,47,102,39,184,147,120,49,106,17,191,5,154, 214,89,13,194,90,92,167,45,34,172,80,180,82,88,155,11,37,13,79,96,245,187,211,133, 243,147,46,139,22,64,215,52,59,229,206,1,157,246,84,130,17,244,79,188,240,143,159,232, 83,235,101,76,188,204,232,1,92,11,234,252,40,139,16,66,204,141,92,165,195,140,214,109, 109,111,170,230,82,45,132,24,46,209,19,178,72,64,170,79,51,173,55,36,67,232,243,246, 66,144,18,54,16,79,246,204,182,2,9,121,168,200,36,82,40,133,72,66,35,217,130,211, 198,205,211,10,25,255,153,192,219,105,61,96,118,94,55,61,185,57,172,117,128,244,120,225, 234,13,1,185,44,124,226,3,245,241,143,123,207,219,96,48,64,254,67,71,217,23,142,223, 203,161,119,164,69,166,105,108,104,180,98,194,245,143,234,97,165,176,13,163,17,8,251,223, 115,180,74,15,25,126,212,23,203,75,192,40,67,98,166,203,31,244,163,86,199,106,95,167, 7,240,138,79,64,245,105,134,143,36,154,61,21,232,115,238,66,215,164,19,63,90,117,160, 152,51,232,36,201,21,204,70,101,74,79,38,6,244,20,151,175,155,134,3,98,124,168,208, 156,164,130,248,209,92,125,163,84,176,131,88,207,117,148,120,47,251,137,100,169,71,95,3, 7,7,102,52,241,179,138,183,172,5,190,123,140,45,76,109,81,140,209,37,69,158,168,192, 145,40,217,229,29,155,9,44,241,37,228,95,143,108,202,201,60,252,202,140,149,203,121,39, 119,150,226,226,109,112,177,236,54,236,241,108,122,199,207,72,134,27,87,46,19,173,0,32, 206,235,54,238,245,108,128,41,155,169,124,169,5,25,236,61,103,228,139,64,191,253,140,161, 106,120,67,49,117,172,2,203,65,60,59,65,154,119,38,214,35,40,215,33,214,167,83,228, 111,25,57,233,143,62,247,98,23,15,194,218,21,46,88,250,232,204,152,177,84,184,210,244, 122,219,198,79,72,126,9,157,231,142,90,174,44,114,78,154,237,15,104,90,251,222,45,186, 103,80,219,213,191,72,168,175,11,165,154,45,244,125,33,26,176,37,140,107,208,66,140,133, 218,81,233,89,89,208,79,46,80,238,24,62,27,190,14,193,101,152,10,91,184,199,100,237, 156,36,251,109,143,151,86,120,5,154,136,113,218,26,166,247,133,194,122,168,1,50,179,23, 97,241,120,203,128,232,136,162,38,188,61,72,132,215,22,179,121,107,88,251,0,57,206,68, 255,96,71,178,211,196,79,161,113,151,145,168,105,20,125,165,234,106,123,80,85,84,189,191, 79,9,138,75,156,129,111,121,215,67,29,203,240,197,182,91,63,84,93,121,220,240,224,204, 78,206,215,207,228,40,227,31,31,145,82,41,68,198,187,221,120,156,117,149,94,43,191,163, 54,200,68,194,6,229,39,164,246,249,234,254,119,34,62,236,237,250,116,173,8,78,239,86, 229,45,230,24,87,56,217,120,179,131,137,5,12,29,139,57,170,173,17,183,143,103,232,152, 202,137,70,134,57,117,255,254,81,250,232,10,122,61,35,162,22,76,236,241,82,171,10,247, 57,146,185,180,216,6,72,241,181,59,93,31,29,38,75,83,32,112,164,110,23,67,145,126, 64,253,108,241,141,152,121,78,79,40,98,48,243,195,49,105,99,222,148,170,76,34,246,106, 102,186,202,254,243,191,126,69,191,42,65,196,250,255,225,136,182,231,224,43,246,210,132,147, 245,62,105,102,110,61,212,131,70,21,176,6,139,101,76,51,238,26,248,71,8,215,95,181, 165,233,170,65,220,216,7,215,105,197,24,225,137,208,158,183,75,254,243,163,160,35,95,0, 236,217,176,234,31,235,210,222,58,20,248,205,221,90,229,69,102,36,50,131,122,154,49,65, 33,204,104,37,176,182,161,23,247,134,145,119,102,210,90,104,173,72,124,127,228,24,66,15, 1,132,122,50,55,218,126,248,2,7,111,44,9,215,203,154,54,154,140,105,255,58,235,127, 181,96,103,139,25,111,6,188,229,211,42,148,5,178,214,101,237,186,6,29,43,209,205,242, 195,115,184,232,184,209,144,153,99,196,147,222,46,118,228,194,27,161,143,61,175,202,86,112, 253,112,177,148,81,117,103,210,113,106,200,13,238,247,97,252,49,58,24,84,76,70,36,88, 244,170,190,220,235,77,81,161,35,213,56,112,81,149,158,133,96,89,149,199,74,147,185,122, 42,102,162,87,212,201,90,14,199,40,74,198,101,236,114,24,61,225,42,5,179,10,46,219, 85,181,155,92,182,5,153,119,79,195,143,72,189,237,244,128,193,107,182,143,105,13,40,6, 156,27,39,197,181,145,233,119,65,242,243,165,252,62,85,211,71,227,136,191,148,10,149,237, 98,43,168,44,106,92,120,98,1,46,35,79,147,231,106,154,25,207,9,47,72,223,153,218, 98,87,171,63,52,248,95,173,101,194,8,101,247,165,20,0,87,196,10,102,32,196,170,15, 140,39,63,102,27,149,6,119,77,240,235,197,140,206,183,6,157,78,47,170,101,211,222,146, 104,151,88,176,179,74,156,255,22,197,185,2,8,96,242,37,98,225,239,100,255,49,12,130, 110,228,24,43,200,35,107,118,208,49,174,224,35,12,209,179,46,184,235,239,141,159,13,84, 221,136,202,160,13,128,96,178,239,87,80,166,48,245,96,232,12,37,7,143,87,203,164,14, 185,255,48,202,148,150,212,170,152,1,76,58,105,141,236,216,43,81,251,174,193,59,141,12, 185,26,138,39,62,185,17,103,69,247,181,252,78,235,66,224,131,27,25,4,46,128,111,5, 139,21,8,37,234,107,214,97,37,168,104,113,219,89,144,170,191,224,232,210,26,232,77,18, 55,122,45,102,214,110,237,6,210,74,67,163,44,214,69,250,146,30,110,42,143,48,225,194, 128,203,246,247,129,155,43,76,132,136,116,94,52,154,31,108,204,161,110,112,96,80,54,232, 53,227,174,21,206,90,168,114,129,242,156,175,146,120,14,48,212,99,53,129,249,63,90,238, 44,202,94,179,215,23,82,186,27,246,218,228,58,27,153,123,190,115,92,215,170,193,74,106, 234,72,233,95,84,228,21,205,93,128,117,249,87,113,221,62,180,0,68,229,237,195,58,199, 196,105,215,221,91,134,206,225,49,153,203,146,65,149,120,129,73,170,95,249,206,156,249,46, 96,123,182,171,180,160,145,238,93,89,74,167,184,208,11,238,58,39,196,250,85,244,169,12, 124,84,15,66,221,31,141,78,134,250,223,45,37,113,64,25,16,207,194,110,98,134,136,44, 124,244,145,175,65,53,116,86,232,122,97,79,156,252,68,71,112,42,9,25,117,39,132,147, 38,231,211,120,221,84,80,94, }; // The value's UTF-8 bytes in the low bytes and its length in the top byte. typedef struct { u64 key, val; } Entry; typedef struct { u8 name[32]; u64 val; u32 len; u32 pad; } Long; static u8 mem[EXTRA + IN_OFF + INPUT_CAP + PAD] __attribute__((aligned(64))); #define buf (mem + EXTRA) #define input (buf + IN_OFF) #define output buf _Static_assert(IN_OFF % 64 == 0 && IN_OFF >= INPUT_CAP / 5 + 64, "the output must stay behind the input"); static Entry fast[(1 << FAST_BITS) + FAST_TAIL]; static Entry leg[(1 << LEG_BITS) + LEG_TAIL]; static Long longs[LONG_COUNT] __attribute__((aligned(16))); static u16 long_slots[1 << LONG_BITS]; // Longest legacy name starting with these two bytes (hashed), or 0. static u8 leg_max[4096]; static u32 pos[WINDOW + 16]; static u32 ready; // Windows-1252 remapping for references to 0x80-0x9F. static const u16 c1[32] = { 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, }; static const u32 utf8_marks[5] = {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. static u8 len_mask[64] __attribute__((aligned(16))); #define ONES 0x0101010101010101ull #define HIGH 0x8080808080808080ull #define LIKELY(x) __builtin_expect(!!(x), 1) #define UNLIKELY(x) __builtin_expect(!!(x), 0) static inline u64 ld64(const u8 *p) { u64 x; __builtin_memcpy(&x, p, 8); return x; } static inline void st64(u8 *p, u64 x) { __builtin_memcpy(p, &x, 8); } static inline void st32(u8 *p, u32 x) { __builtin_memcpy(p, &x, 4); } static inline v128_t ld(const u8 *p) { return wasm_v128_load(p); } static inline void st(u8 *p, v128_t v) { wasm_v128_store(p, v); } static inline u32 amps(v128_t v) { return wasm_i8x16_bitmask(wasm_i8x16_eq(v, wasm_i8x16_splat('&'))); } static inline u32 leg_slot(u64 w) { return (((u32)w & 0xFFFF) * 0x9E3779B1u) >> 20; } static inline u64 rotl(u64 x, u32 r) { return x << r | x >> (64 - r); } static inline u32 long_hash(u64 head, u64 tail, u32 n) { return (u32)((((head ^ rotl(tail, 29)) + n) * LONG_MUL) >> (64 - LONG_BITS)); } // Lay the bits of a code point out as four UTF-8 bytes, then drop the unused leading ones. __attribute__((always_inline)) static inline u32 utf8(u32 v, u32 *len) { u32 n = 1 + (v > 0x7F) + (v > 0x7FF) + (v > 0xFFFF); u32 bits = v >> 18 | (v >> 12 & 0x3F) << 8 | (v >> 6 & 0x3F) << 16 | (v & 0x3F) << 24; *len = n; return n == 1 ? v : bits >> (32 - 8 * n) | utf8_marks[n]; } // The entity list is stored packed and read once, on the first call. Entities are ordered // by code point, so a value is a small step from the one before, and each name is coded // with an adaptive binary range coder (the LZMA one) whose model mixes four contexts: // the previous one, two and three characters, and a match model that predicts the // character that followed the last place the previous two were seen. pack.py, printed // at the end of this file, is the matching packer. // // A model entry is a 12-bit probability (stored XOR 0x800, so zeroed memory means one // half) and a 4-bit count of how often it was used. The model and the names unpacked so // far live in front of the input, and the match model's index in the position list; all // of it is still zero on the first call. #define MODEL ((u16 *)mem) #define hist (mem + (2u << MODEL_BITS)) #define last ((u16 *)pos) _Static_assert((2u << MODEL_BITS) + HIST_SIZE <= EXTRA + IN_OFF && sizeof pos >= 8192, "working memory must not reach the input"); #define SMALL __attribute__((minsize, noinline)) #define K (1u << 20) SMALL static u32 utf8_once(u32 v, u32 *len) { return utf8(v, len); } // Step toward the bit just seen, as a 16-bit fraction: 2 / (2n + 3) after n uses. static const u16 rate[6] = {43690, 26214, 18724, 14563, 11915, 10082}; static const u8 *rc_in; static u32 rc_range, rc_code; SMALL static u32 sym(const u32 *ctx, u32 nctx, u32 nbits) { u32 node = 1, top = 1u << nbits; do { u16 *slot[4]; u32 num = 0, den = 0; for (u32 k = 0; k < nctx; k++) { u16 *s = slot[k] = &MODEL[((ctx[k] + node * 0x2545F491u) * 0x9E3779B1u) >> (32 - MODEL_BITS)]; int32_t p = *s >> 4 ^ 0x800, a = p - 0x800; // Weigh a context by its order, how often it was seen and how sure it is. u32 w = ((*s & 15u) * (1 + ((u32)(a < 0 ? -a : a) >> 8))) << k; num += (u32)p * w; den += w; } u32 p = den ? num / den : 0x800; if (!p) p = 1; u32 bound = (rc_range >> 12) * p, bit = rc_code < bound; if (bit) rc_range = bound; else rc_code -= bound, rc_range -= bound; while (rc_range < 1u << 24) { rc_range <<= 8; rc_code = rc_code << 8 | *rc_in++; } for (u32 k = 0; k < nctx; k++) { int32_t q = *slot[k] >> 4 ^ 0x800, n = *slot[k] & 15; q += ((((int32_t)bit << 12) - q) * rate[n]) >> 16; if (n < MODEL_LIMIT) n++; *slot[k] = (u16)((q ^ 0x800) << 4 | n); } node = node * 2 + bit; } while (node < top); return node - top; } SMALL static void init(void) { __builtin_memset(fast, 255, sizeof fast); __builtin_memset(leg, 255, sizeof leg); __builtin_memset(len_mask, 255, 32); rc_in = packed + 4; rc_range = ~0u; rc_code = (u32)packed[0] << 24 | (u32)packed[1] << 16 | (u32)packed[2] << 8 | packed[3]; u32 cp = 0, nl = 0, hn = 1, ctx[4]; for (u32 r = 0; r < ENTITY_COUNT; r++) { // Step from the previous code point: its bit length, then the bits below the top one. ctx[0] = K; u32 nb = sym(ctx, 1, 5), d = nb; if (nb > 1) { ctx[0] = 2 * K + nb; d = (1u << (nb - 1)) + sym(ctx, 1, nb - 1); } cp += d; u32 vn, vn2 = 0; u64 val = utf8_once(cp, &vn); ctx[0] = 3 * K + !d; if (sym(ctx, 1, 1)) { ctx[0] = 4 * K; val |= (u64)utf8_once(sym(ctx, 1, 16), &vn2) << (8 * vn); } val |= (u64)(vn + vn2) << 56; ctx[0] = 5 * K + (cp < 256); u32 legacy = sym(ctx, 1, 1); // Names use a 6-bit alphabet: 0 ends the name, then digits, capitals, small letters. u8 name[40]; __builtin_memset(name, 0, 40); u32 n = 0, h = 0, c, mp = 0, ml = 0; for (;;) { ctx[0] = 8 * K + (h & 63); ctx[1] = 9 * K + (h & 4095); ctx[2] = 13 * K + (ml ? hist[mp] : 0) * 16 + (ml < 3 ? ml : 3); ctx[3] = 10 * K + (h & 0x3FFFF); c = sym(ctx, 4, 6); if (ml && hist[mp] == c) ml++, mp++; else ml = 0; hist[hn++] = (u8)c; if (!c) break; name[n++] = (u8)(c + (c < 11 ? '0' - 1 : c < 37 ? 'A' - 11 : 'a' - 37)); h = h << 6 | c; if (n >= 2) { u16 *l = &last[h & 4095]; if (!ml && *l) mp = *l, ml = 1; *l = (u16)hn; } } if (n <= 7) { u64 key = ld64(name) | (u64)';' << (8 * n); Entry *e = &fast[(key * FAST_MUL) >> (64 - FAST_BITS)]; while (e->key != EMPTY) e++; e->key = key; e->val = val; if (legacy) { key = ld64(name) | (u64)n << 56; e = &leg[(key * LEG_MUL) >> (64 - LEG_BITS)]; while (e->key != EMPTY) e++; e->key = key; e->val = val; u32 s = leg_slot(key); // only the first two bytes matter if (leg_max[s] < n) leg_max[s] = n; } } else { Long *l = &longs[nl++]; __builtin_memcpy(l->name, name, 32); l->val = val; l->len = n; u32 hh = long_hash(ld64(name), ld64(name + n - 8), n); while (long_slots[hh]) hh = (hh + 1) & ((1 << LONG_BITS) - 1); long_slots[hh] = nl; } } ready = 1; } __attribute__((export_name("input_ptr"))) u32 input_ptr(void) { return (u32)(uintptr_t)input; } __attribute__((export_name("input_utf8_cap"))) u32 input_utf8_cap(void) { return INPUT_CAP; } __attribute__((export_name("output_ptr"))) u32 output_ptr(void) { return (u32)(uintptr_t)output; } __attribute__((export_name("output_utf8_cap"))) u32 output_utf8_cap(void) { return OUTPUT_CAP; } __attribute__((export_name("render"))) u64 render(u32 n) { if (UNLIKELY(!ready)) init(); u32 size = n < INPUT_CAP ? n : INPUT_CAP; const u8 *src = input; u8 *dst = output; const v128_t zero = wasm_i8x16_splat(0); st(input + size, zero); st(input + size + 16, zero); st(input + size + 32, zero); st(input + size + 48, zero); // Stage 1 lists the position of every `&` in a window; stage 2 copies the text between // references and decodes each one. Knowing the next position before the current // reference is decoded keeps the references independent of each other. u32 e = 0, o = 0, base = 0; while (base < size) { u32 lim = size - base > WINDOW ? base + WINDOW : size; u32 *out = pos; do { const v128_t amp = wasm_i8x16_splat('&'); v128_t ea = wasm_i8x16_eq(ld(src + base), amp), eb = wasm_i8x16_eq(ld(src + base + 16), amp); v128_t ec = wasm_i8x16_eq(ld(src + base + 32), amp), ed = wasm_i8x16_eq(ld(src + base + 48), amp); { u64 mm = (u64)wasm_i8x16_bitmask(ea) | (u64)wasm_i8x16_bitmask(eb) << 16 | (u64)wasm_i8x16_bitmask(ec) << 32 | (u64)wasm_i8x16_bitmask(ed) << 48; u32 cnt = __builtin_popcountll(mm); // Entries past cnt are garbage and are overwritten or ignored. out[0] = base + __builtin_ctzll(mm); mm &= mm - 1; out[1] = base + __builtin_ctzll(mm); mm &= mm - 1; out[2] = base + __builtin_ctzll(mm); mm &= mm - 1; out[3] = base + __builtin_ctzll(mm); mm &= mm - 1; if (UNLIKELY(cnt > 4)) { out[4] = base + __builtin_ctzll(mm); mm &= mm - 1; out[5] = base + __builtin_ctzll(mm); mm &= mm - 1; out[6] = base + __builtin_ctzll(mm); mm &= mm - 1; out[7] = base + __builtin_ctzll(mm); mm &= mm - 1; for (u32 k = 8; UNLIKELY(mm); k++) { out[k] = base + __builtin_ctzll(mm); mm &= mm - 1; } } out += cnt; } base += 64; } while (base < lim); for (const u32 *q = pos; q < out; q++) { u32 i = *q; // Everything this reference needs is loaded before the first store. u64 w = ld64(src + i + 1); // Copy the text since the last reference; src[i] is `&` and i < size. { u32 len = i - e; v128_t ca = ld(src + e), cb = ld(src + e + 16); st(dst + o, ca); st(dst + o + 16, cb); if (len > 32) { u32 j = 32; do { st(dst + o + j, ld(src + e + j)); j += 16; } while (j < len); } o += len; } if ((u8)w == '#') { // Decimal and hexadecimal share one branch-free path: classify 8 bytes, take // the digits before the first other byte, and fold them pairwise. u32 hex = (((u32)w >> 8 & 0xFF) | 0x20) == 'x'; const u8 *p = src + i + 2 + hex; u64 d = ld64(p); u64 t = d ^ 0x3030303030303030ull; u64 digit = ~(((t & ~HIGH) + 0x7676767676767676ull) | t) & HIGH; u64 y = d | 0x2020202020202020ull, ty = y & ~HIGH; u64 letter = (ty + ONES * (0x80 - 'a')) & ~(ty + ONES * (0x7F - 'f')) & ~y & HIGH; u64 stop = ~(digit | (hex ? letter : 0)) & HIGH; u32 v, end; if (LIKELY(stop)) { u32 nb = __builtin_ctzll(stop) & 0x38; if (UNLIKELY(!nb)) goto bare; u64 x = ((d & 0x0F0F0F0F0F0F0F0Full) + 9 * (letter >> 7)) << (64 - nb); x = (x * (hex ? 16 : 10) + (x >> 8)) & 0x00FF00FF00FF00FFull; x = (x * (hex ? 256 : 100) + (x >> 16)) & 0x0000FFFF0000FFFFull; v = (u32)(x * (hex ? 65536 : 10000) + (x >> 32)); end = (u32)(p - src) + (nb >> 3); } else { // Eight or more digits: saturate past U+10FFFF. u32 c, base = hex ? 16 : 10; v = 0; for (;;) { c = *p - '0'; if (c > 9) c = ((*p | 0x20) - 'a') + 10, c = c < 10 ? 16 : c; if (c >= base) break; v = v * base + c; if (v > 0x10FFFF) v = 0x110000; p++; } end = (u32)(p - src); } i = end + (src[end] == ';'); if (UNLIKELY(v - 0x80 < 0x20)) v = c1[v - 0x80]; else if (UNLIKELY(v == 0 || v - 0xD800 < 0x800 || v > 0x10FFFF)) v = 0xFFFD; u32 len, word = utf8(v, &len); st32(dst + o, word); o += len; goto next; } { u64 t = w ^ (ONES * ';'); u64 z = (t - ONES) & ~t & HIGH; if (LIKELY(z)) { // Bit index of the `;` byte; the key is the name and its `;`. u32 kb = __builtin_ctzll(z) & 0x38; u64 key = w & (((u64)2 << (kb + 7)) - 1); // The probe loop only finds the slot; the hit is handled after it. const Entry *e = &fast[(key * FAST_MUL) >> (64 - FAST_BITS)]; u64 k = e->key; while (UNLIKELY(k != key && k != EMPTY)) k = (++e)->key; if (LIKELY(k == key)) { u64 val = e->val; st64(dst + o, val); o += (u32)(val >> 56); i += 2 + (kb >> 3); goto next; } } else { // No `;` in 8 bytes: a name of 8 to 31 bytes needs its `;` in the next 24. v128_t s0 = ld(src + i + 1), s1 = ld(src + i + 17); const v128_t semi = wasm_i8x16_splat(';'); u32 sm = wasm_i8x16_bitmask(wasm_i8x16_eq(s0, semi)) | wasm_i8x16_bitmask(wasm_i8x16_eq(s1, semi)) << 16; if (sm) { u32 len = __builtin_ctz(sm); u32 h = long_hash(w, ld64(src + i + 1 + len - 8), len); v128_t m0 = ld(len_mask + 32 - len), m1 = ld(len_mask + 48 - len); for (u32 s; (s = long_slots[h]) != 0; h = (h + 1) & ((1 << LONG_BITS) - 1)) { const Long *l = &longs[s - 1]; v128_t diff = wasm_v128_or( wasm_v128_xor(wasm_v128_and(s0, m0), ld(l->name)), wasm_v128_xor(wasm_v128_and(s1, m1), ld(l->name + 16))); if (l->len == len && !wasm_v128_any_true(diff)) { st64(dst + o, l->val); o += (u32)(l->val >> 56); i += 2 + len; goto next; } } } } // Without `;`, the longest legacy name that is a prefix. for (u32 k = leg_max[leg_slot(w)]; k >= 2; k--) { // The length is part of the key, so a NUL after a shorter name cannot match. u64 key = (w & (((u64)1 << (8 * k)) - 1)) | (u64)k << 56; const Entry *e = &leg[(key * LEG_MUL) >> (64 - LEG_BITS)]; for (;;) { if (e->key == key) { st64(dst + o, e->val); o += (u32)(e->val >> 56); i += 1 + k; goto next; } if (e->key == EMPTY) break; e++; } } } bare: dst[o] = '&'; o += 1; i += 1; next: e = i; } } // Without a reference the input is the output. if (e == 0) return (u64)(u32)(uintptr_t)input << 32 | size; for (u32 j = e; j < size; j += 16) st(dst + o + (j - e), ld(src + j)); o += size - e; return (u64)(u32)(uintptr_t)output << 32 | o; } /* pack.py, which produced `packed`: """Packs the entity list for unescape-small: entities ordered by code point, coded with an adaptive binary range coder whose contexts are the previous one, two and three name characters and a match model. The C unpacker in unescape-small.in.c mirrors Model and Decoder exactly. """ import html.entities M32 = 0xFFFFFFFF K = 1 << 20 HASH_A, HASH_B = 0x2545F491, 0x9E3779B1 # Step toward the bit just seen, as a 16-bit fraction: 2 / (2n + 3) after n uses. RATE = [43690, 26214, 18724, 14563, 11915, 10082] html5 = html.entities.html5 NAMES = {k[:-1]: v for k, v in html5.items() if k.endswith(';')} LEGACY = {k for k in html5 if not k.endswith(';')} ALPHA = '\0' + '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' CODE = {c: i for i, c in enumerate(ALPHA)} ORDER = sorted(NAMES, key=lambda n: ([ord(c) for c in NAMES[n]], n)) def tdiv(a, b): """Division truncating toward zero, as in C.""" q = abs(a) // abs(b) return q if (a < 0) == (b < 0) else -q class Model: def __init__(self, bits=16, limit=6, narrow=False): # narrow: 16-bit entries holding a 12-bit probability and a 4-bit count, # instead of 32-bit entries holding 16 bits of each. self.t = [0] * (1 << bits) self.bits, self.limit, self.narrow = bits, limit, narrow def load(self, s): e = self.t[s] return ((e >> 4) ^ 0x800, e & 15) if self.narrow else ((e >> 16) ^ 0x8000, e & 0xFFFF) def slots(self, ctxs, node): return [(((c + node * HASH_A) * HASH_B) & M32) >> (32 - self.bits) for c in ctxs] def predict(self, slots): """12-bit probability that the bit is 1.""" num = den = 0 for k, s in enumerate(slots): p, n = self.load(s) half, shift = (0x800, 8) if self.narrow else (0x8000, 12) w = (n * (1 + (abs(p - half) >> shift))) << k num += p * w den += w if self.narrow: p = num // den if den else 0x800 else: p = (num // den if den else 0x8000) >> 4 return min(max(p, 1), 4095) def update(self, slots, bit): for s in slots: p, n = self.load(s) one = 0x1000 if self.narrow else 0x10000 p += (((one if bit else 0) - p) * RATE[n]) >> 16 if n < self.limit: n += 1 self.t[s] = (p ^ 0x800) << 4 | n if self.narrow else (p ^ 0x8000) << 16 | n class Encoder: def __init__(self): self.low, self.range, self.cache, self.pending, self.out = 0, M32, 0, 1, bytearray() def shift_low(self): if self.low < 0xFF000000 or self.low > M32: carry = self.low >> 32 byte = self.cache while self.pending: self.out.append((byte + carry) & 0xFF) byte = 0xFF self.pending -= 1 self.cache = self.low >> 24 & 0xFF self.pending += 1 self.low = (self.low << 8) & M32 def bit(self, p, bit): bound = (self.range >> 12) * p if bit: self.range = bound else: self.low += bound self.range -= bound while self.range < 1 << 24: self.range = (self.range << 8) & M32 self.shift_low() def finish(self): for _ in range(5): self.shift_low() assert self.out[0] == 0 return bytes(self.out[1:]) class Decoder: def __init__(self, data): self.data, self.pos, self.range = data + bytes(8), 4, M32 self.code = int.from_bytes(data[:4], 'big') def bit(self, p): bound = (self.range >> 12) * p if self.code < bound: self.range = bound bit = 1 else: self.code -= bound self.range -= bound bit = 0 while self.range < 1 << 24: self.range = (self.range << 8) & M32 self.code = (self.code << 8 | self.data[self.pos]) & M32 self.pos += 1 return bit class Coder: """Runs the same field sequence for packing (values given) and unpacking (values None).""" def __init__(self, data=None, **kw): self.m = Model(**kw) self.enc = Encoder() if data is None else None self.dec = Decoder(data) if data is not None else None self.hist, self.last = [0], [0] * 4096 def sym(self, ctxs, nbits, value=None): node = 1 for i in range(nbits - 1, -1, -1): slots = self.m.slots(ctxs, node) p = self.m.predict(slots) if self.enc: b = value >> i & 1 self.enc.bit(p, b) else: b = self.dec.bit(p) self.m.update(slots, b) node = node * 2 + b return node - (1 << nbits) def entity(self, cp, rec=None): """rec = (name, [code points], legacy) when packing. Returns the same when unpacking.""" name, cps, legacy = rec if rec else (None, None, None) d = cps[0] - cp if rec else None nb = self.sym([1 * K], 5, d.bit_length() if rec else None) if nb > 1: d = (1 << (nb - 1)) + self.sym([2 * K + nb], nb - 1, d - (1 << (nb - 1)) if rec else None) else: d = nb cp += d second = 0 if self.sym([3 * K + (d == 0)], 1, len(cps) - 1 if rec else None): second = self.sym([4 * K], 16, cps[1] if rec else None) leg = self.sym([5 * K + (cp < 256)], 1, int(legacy) if rec else None) # The match model predicts the character that followed the last place the previous # two characters were seen, and how many predictions in a row have been right. h, out, mp, ml = 0, '', 0, 0 for i in range(64): ctxs = [8 * K + (h & 63), 9 * K + (h & 4095), 13 * K + (self.hist[mp] if ml else 0) * 16 + min(ml, 3), 10 * K + (h & 0x3FFFF)] c = self.sym(ctxs, 6, CODE[(name + '\0')[i]] if rec else None) if ml and self.hist[mp] == c: ml += 1 mp += 1 else: ml = 0 self.hist.append(c) if c == 0: break out += ALPHA[c] h = h << 6 | c if i >= 1: if ml == 0 and self.last[h & 4095]: mp, ml = self.last[h & 4095], 1 self.last[h & 4095] = len(self.hist) return out, [cp, second] if second else [cp], bool(leg) def records(): return [(n, [ord(c) for c in NAMES[n]], n in LEGACY) for n in ORDER] def pack(**kw): c = Coder(**kw) cp = 0 for rec in records(): assert len(rec[1]) <= 2 and rec[1][0] >= cp and (len(rec[1]) == 1 or rec[1][1] < 0x10000) c.entity(cp, rec) cp = rec[1][0] return c.enc.finish() def unpack(data, count, **kw): c = Coder(data, **kw) cp, out = 0, [] for _ in range(count): rec = c.entity(cp) cp = rec[1][0] out.append(rec) return out if __name__ == '__main__': import sys blob = pack(bits=18, limit=5) assert unpack(blob, len(ORDER), bits=18, limit=5) == records() print('round trip ok', len(blob)) # This file: pack(bits=12, limit=5, narrow=True) */