acompiled

The catalog contains seeded project examples. Your workspace stores your own projects and conversations.

tsvet/utf8-check

Public
Star9

UTF-8 validation in x86-64: a scalar reference and an AVX2 version that checks 32 bytes per step. Every build fuzzes one against the other.

Every case at offsets 0 to 63

Browse files
Each sample from table 3-7 now sits behind 0 to 63 bytes of ASCII, once at the end of the input and once with 32 more bytes after it, so each one crosses a block boundary somewhere: 27 samples, 3,456 runs.
latchagentcommitted 1 parent 4e510c0 commit 2f412224656950ab138bc8215866dc486c2a6d9e
failing 6 steps in 1 ms
1 file changed +20−17
modified test/test.c +20−17
@@ -46,29 +46,32 @@ static const struct {
4646 };
4747 #define SAMPLES (sizeof samples / sizeof samples[0])
4848
49−// Each sample is tried on its own, then again at the very end of a full
50−// 32-byte block, where no next byte shows that it was cut short.
49+// Each sample is tried at offsets 0 to 63 behind ASCII, so it also straddles
50+// a 32-byte block boundary, once at the end of the input and once followed
51+// by 32 more bytes of ASCII.
5152 static int fixed_cases(void)
5253 {
53− uint8_t buf[32];
54− int runs = 0, wrong = 0;
54+ uint8_t buf[160];
55+ int runs = 0, failures = 0;
5556 for (size_t i = 0; i < SAMPLES; i++) {
5657 size_t n = strlen(samples[i].bytes);
57− memset(buf, 'a', sizeof buf);
58− memcpy(buf + 32 - n, samples[i].bytes, n);
59− for (int full = 0; full <= 1; full++) {
60− const uint8_t *p = full ? buf : (const uint8_t *)samples[i].bytes;
61− size_t len = full ? 32 : n;
62− int a = utf8_valid_scalar(p, len), b = utf8_valid_avx2(p, len);
63− runs++;
64− if (a == samples[i].valid && b == samples[i].valid)
65− continue;
66− wrong++;
67− printf("wrong: %s%s: scalar %d, avx2 %d\n", samples[i].what, full ? " at the end of a block" : "", a, b);
58+ for (size_t offset = 0; offset < 64; offset++) {
59+ for (size_t after = 0; after <= 32; after += 32) {
60+ memset(buf, 'a', sizeof buf);
61+ memcpy(buf + offset, samples[i].bytes, n);
62+ int a = utf8_valid_scalar(buf, offset + n + after);
63+ int b = utf8_valid_avx2(buf, offset + n + after);
64+ runs++;
65+ if (a == samples[i].valid && b == samples[i].valid)
66+ continue;
67+ if (failures++ < 8)
68+ printf("wrong: %s at offset %zu%s: scalar %d, avx2 %d\n", samples[i].what, offset,
69+ after ? " with ASCII after" : "", a, b);
70+ }
6871 }
6972 }
70− printf("cases: %zu samples, %d runs, %d wrong\n", SAMPLES, runs, wrong);
71− return wrong;
73+ printf("cases: %zu samples, %d runs, %d wrong\n", SAMPLES, runs, failures);
74+ return failures;
7275 }
7376
7477 static uint64_t rng = 0x9E3779B97F4A7C15ull;