/* AI-assisted (Claude), human review required.
   lemire8bench.c — speed of the 8-bit parsers in lemire8.h, called the way the blog post calls them:
   int parse(const char *str, size_t len, uint8_t *num).  Each string sits in its own 4-byte slot whose unused bytes
   are random garbage, so the SWAR versions read 4 bytes without overrunning.  Two inputs: "valid" (uniform 0..255,
   as in the post) and "mixed" (half valid, half invalid: bad characters, 256..999, length 0 or 4).
   Before timing, every parser is checked against the naive one on every string (koekkoek on valid strings only).
   Build: cc -O3 -o lemire8bench lemire8bench.c   Run: ./lemire8bench [count] */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "lemire8.h"

#define NOINL __attribute__((noinline))
static inline uint32_t load4(const char *s) { uint32_t x; memcpy(&x, s, 4); return x; }
static inline uint32_t clen(size_t len) { return len > 4 ? 4u : (uint32_t)len; }

/* The post's parse_uint8_naive, re-typed: a byte loop over the string. */
static NOINL int w_naive(const char *s, size_t len, uint8_t *o) {
    uint32_t n = 0;
    for (size_t i = 0, r = len & 3; i < r; i++) {
        uint8_t d = (uint8_t)(s[i] - '0');
        if (d > 9) return 0;
        n = n * 10 + d;
    }
    *o = (uint8_t)n;
    return n < 256 && len && len < 4;
}
#define WRAP(name) static NOINL int w_##name(const char *s, size_t len, uint8_t *o) { \
    uint32_t r = p_##name(load4(s), clen(len)); *o = (uint8_t)r; return (int)(r >> 8); }
WRAP(fastswar) WRAP(fastswar_bl) WRAP(bob) WRAP(bob_safe) WRAP(mulfirst) WRAP(so) WRAP(so32)
static NOINL int w_koekkoek(const char *s, size_t len, uint8_t *o) { *o = (uint8_t)p_koekkoek(load4(s), clen(len)); return 1; }

typedef int (*parser)(const char *, size_t, uint8_t *);
static const struct { const char *name; parser f; int validated; } P[] = {
    {"naive", w_naive, 1}, {"fastswar", w_fastswar, 1}, {"fastswar_bl (branch-free)", w_fastswar_bl, 1},
    {"bob (masked shift)", w_bob, 1}, {"bob_safe", w_bob_safe, 1},
    {"mulfirst", w_mulfirst, 1}, {"so (superopt, 64-bit mul)", w_so, 1},
    {"so32 (superopt, 32-bit)", w_so32, 1}, {"koekkoek (no validation)", w_koekkoek, 0},
};
#define NP (int)(sizeof P / sizeof P[0])

static uint64_t rs = 0x9E3779B97F4A7C15ull;
static uint32_t rnd(void) { rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; return (uint32_t)(rs >> 16); }

static void fill(char *buf, uint8_t *lens, size_t n, int mixed) {
    for (size_t i = 0; i < n; i++) {
        char *s = buf + 4 * i;
        for (int k = 0; k < 4; k++) s[k] = (char)rnd();          /* garbage in unused bytes */
        int kind = mixed ? (int)(rnd() % 8) : 0;                 /* mixed: 0-3 valid, 4-7 invalid */
        if (kind < 4) { lens[i] = (uint8_t)snprintf(s, 4, "%u", rnd() % 256); s[lens[i]] = (char)rnd(); }
        else if (kind == 4) { lens[i] = (uint8_t)snprintf(s, 4, "%u", 256 + rnd() % 744); }
        else if (kind == 5) { lens[i] = (uint8_t)(1 + rnd() % 3); s[rnd() % lens[i]] = (char)("/:a -"[rnd() % 5]); }
        else if (kind == 6) { lens[i] = 0; }
        else { lens[i] = 4; for (int k = 0; k < 4; k++) s[k] = (char)('0' + rnd() % 10); }
    }
}

static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec + 1e-9 * t.tv_nsec; }

int main(int argc, char **argv) {
    size_t n = argc > 1 ? strtoul(argv[1], 0, 0) : 10000000;
    char *buf = malloc(4 * n + 4); uint8_t *lens = malloc(n);
    for (int mixed = 0; mixed < 2; mixed++) {
        fill(buf, lens, n, mixed);
        size_t nvalid = 0;
        for (size_t i = 0; i < n; i++) { uint8_t v; nvalid += w_naive(buf + 4 * i, lens[i], &v); }
        for (int p = 1; p < NP; p++) {                           /* agreement check before timing */
            for (size_t i = 0; i < n; i++) {
                uint8_t a = 0, b = 0; int ra = w_naive(buf + 4 * i, lens[i], &a), rb = P[p].f(buf + 4 * i, lens[i], &b);
                if (P[p].validated ? (ra != rb || (ra && a != b)) : (ra && a != b)) {
                    printf("MISMATCH %s on \"%.*s\" len %u\n", P[p].name, lens[i], buf + 4 * i, lens[i]); return 1;
                }
            }
        }
        printf("%s input: %zu strings, %.1f%% valid\n", mixed ? "mixed" : "valid", n, 100.0 * nvalid / n);
        for (int p = 0; p < NP; p++) {
            double best = 1e9; uint64_t sink = 0;
            for (int rep = 0; rep < 5; rep++) {
                double t0 = now();
                for (size_t i = 0; i < n; i++) { uint8_t v = 0; int r = P[p].f(buf + 4 * i, lens[i], &v); sink += (uint64_t)r + v; }
                double t = now() - t0; if (t < best) best = t;
            }
            printf("  %-28s %6.3f ns/parse  %7.1f M parses/s  (sink %llu)\n", P[p].name, 1e9 * best / n, n / best / 1e6,
                   (unsigned long long)(sink & 0xff));
        }
    }
    return 0;
}
