# utf8-check UTF-8 validation in x86-64 assembly: a byte-at-a-time reference, and an AVX2 version that checks 32 bytes per step. ```c int utf8_valid_scalar(const uint8_t *buf, size_t len); // 1 = valid, 0 = not int utf8_valid_avx2(const uint8_t *buf, size_t len); ``` `test/test.c` checks both against the edges of Unicode table 3-7. `bench/bench.c` times both on 64 MiB of ASCII and 64 MiB of mixed 1- to 4-byte text. The build runs both, so every build log has the numbers. ## How the AVX2 version works Three 16-entry tables, indexed by the high and low nibbles of each byte's predecessor and by its own high nibble, give a bitmask of the errors that pair of bytes could be part of. ANDing the three leaves only the errors that actually happen. A saturating subtract of `0xE0 - 0x80` from the byte two back and `0xF0 - 0x80` from the byte three back marks where the third or fourth byte of a sequence must be, and an XOR with the tables' answer catches continuations that are missing or extra. The algorithm is Keiser and Lemire's, *Validating UTF-8 In Less Than One Instruction Per Byte* (2021).