/* AI-assisted (Claude), human review required.
   lemire8.h — parsers for "Parsing 8-bit integers quickly" (Lemire, 28 Nov 2023), written over the 4-byte word the
   SWAR versions load: x holds str[0..3] little-endian (str[i] = byte i), len is the string length.  Every function
   returns 0 for invalid input and 0x100 | value for valid input, so outputs compare exactly.  Bytes at positions
   >= len are garbage the parser must ignore.  Re-implemented from the post's description; constants as there.
   Self-contained (no includes) so the same header also compiles as a Metal kernel. */

static inline uint32_t bswap32_(uint32_t v) { return (v >> 24) | ((v >> 8) & 0xFF00u) | ((v << 8) & 0xFF0000u) | (v << 24); }
static inline uint32_t byte_(uint32_t x, uint32_t i) { return (x >> (8 * i)) & 0xFFu; }

/* The specification: parse_uint8_naive. */
static inline uint32_t p_ref(uint32_t x, uint32_t len) {
    if (len == 0 || len > 3) return 0;
    uint32_t n = 0;
    for (uint32_t i = 0; i < len; i++) {
        uint32_t d = (byte_(x, i) - 0x30u) & 0xFFu;
        if (d > 9) return 0;
        n = n * 10 + d;
    }
    return n < 256 ? (0x100u | n) : 0;
}

/* Lemire's parse_uint8_fastswar (with Jean-Marc Bourguet's validation). */
static inline uint32_t p_fastswar(uint32_t x, uint32_t len) {
    if (len == 0 || len > 3) return 0;
    uint32_t d = x ^ 0x30303030u;
    d <<= (4 - len) * 8;
    uint32_t all_digits = ((d | (0x06060606u + d)) & 0xF0F0F0F0u) == 0;
    uint32_t num = ((0x640a01u * d) >> 24) & 0xFFu;
    return (all_digits & (bswap32_(d) <= 0x020505u)) ? (0x100u | num) : 0;
}

/* Perforated Bob's parse_uint8_fastswar_bob.  As posted the shift runs before the length test; for len >= 4 it is a
   shift by >= 32 (undefined in C).  Hardware masks the count to 5 bits, modelled here with & 31. */
static inline uint32_t p_bob(uint32_t x, uint32_t len) {
    uint32_t d = x ^ 0x303030u;
    d <<= ((len ^ 3) * 8) & 31;
    uint32_t num = ((0x640a01u * d) >> 16) & 0xFFu;
    uint32_t ok = ((((d + 0x767676u) | d) & 0x808080u) == 0) & ((len ^ 3) < 3) & (bswap32_(d) <= 0x020505ffu);
    return ok ? (0x100u | num) : 0;
}

/* Bob's version with the length test first (no undefined shift). */
static inline uint32_t p_bob_safe(uint32_t x, uint32_t len) {
    if (len - 1u > 2u) return 0;
    uint32_t d = (x ^ 0x303030u) << ((3 - len) * 8);
    uint32_t num = ((0x640a01u * d) >> 16) & 0xFFu;
    uint32_t ok = ((((d + 0x767676u) | d) & 0x808080u) == 0) & (bswap32_(d) <= 0x020505ffu);
    return ok ? (0x100u | num) : 0;
}

/* Multiply first, select by length (after KWillets' comment): no shift of the input; the value byte is taken from
   the product at bit 8(len - 1), digits are tested under a length mask, and the range test runs only for len = 3. */
static inline uint32_t p_mulfirst(uint32_t x, uint32_t len) {
    if (len - 1u > 2u) return 0;
    uint32_t d = x ^ 0x303030u;
    uint32_t m = 0xFFFFFFu >> ((3 - len) * 8);
    uint32_t dm = d & m;
    uint32_t num = ((0x640a01u * d) >> (8 * (len - 1))) & 0xFFu;
    uint32_t ok = ((((dm + 0x767676u) | dm) & 0x808080u) == 0) & ((len != 3) | (bswap32_(dm << 8) <= 0x020505u));
    return ok ? (0x100u | num) : 0;
}

/* Branch-free fastswar (found by the benchmark: on mixed input the early "len == 0 || len > 3" return mispredicts).
   The shift count is masked, the length test joins the other tests, and the result is selected with a mask. */
static inline uint32_t p_fastswar_bl(uint32_t x, uint32_t len) {
    uint32_t okl = (len - 1u) < 3u;
    uint32_t d = (x ^ 0x30303030u) << (((4u - len) * 8u) & 31u);
    uint32_t all_digits = ((d | (0x06060606u + d)) & 0xF0F0F0F0u) == 0;
    uint32_t num = ((0x640a01u * d) >> 24) & 0xFFu;
    uint32_t ok = okl & all_digits & (bswap32_(d) <= 0x020505u);
    return (0x100u | num) & (0u - ok);
}

/* Found by an exhaustive constant search (2026-11, see lemire8/superopt): one mask test replaces both the digit test
   and Bourguet's byte-swap range test.  With d as in fastswar (digits in bytes 1..3, byte 0 zero) and the 64-bit
   product P = d * 0x640a01, the word (d + 0x76060000) | d | (P >> 1) has none of the bits 0x80707c7f set exactly
   when the string is valid: the digit failures land in bytes 1..2, and P >> 1 brings "value >= 256" (bit 32 of P)
   down to bit 31.  On AArch64 the >> 1 is free in the orr, and the shift count is one neg (hardware masks it). */
static inline uint32_t p_so(uint32_t x, uint32_t len) {
    uint32_t d = (x ^ 0x30303030u) << ((0u - (len << 3)) & 31u);
    uint64_t P = (uint64_t)d * 0x640a01u;
    uint32_t u = ((d + 0x76060000u) | d) | (uint32_t)(P >> 1);
    uint32_t ok = ((u & 0x80707c7fu) == 0) & (len - 1u < 3u);
    return (0x100u | (uint32_t)((P >> 24) & 0xFFu)) & (0u - ok);
}

/* The same test in 32-bit arithmetic (for SIMD lanes): d is even, so (d >> 1) * M mod 2^32 is P >> 1 mod 2^32. */
static inline uint32_t p_so32(uint32_t x, uint32_t len) {
    uint32_t d = (x ^ 0x30303030u) << ((0u - (len << 3)) & 31u);
    uint32_t p = (d >> 1) * 0x640a01u;
    uint32_t u = (d + 0x76060000u) | d | p;
    uint32_t ok = ((u & 0x80707c7fu) == 0) & (len - 1u < 3u);
    return (0x100u | ((p >> 23) & 0xFFu)) & (0u - ok);
}

/* Jeroen Koekkoek's unvalidated version (value only; meaningful on valid input). */
static inline uint32_t p_koekkoek(uint32_t x, uint32_t len) {
    uint32_t d = x & 0x000F0F0Fu;
    return ((0x640a01u * d) >> (((len << 3) - 8) & 0x18)) & 0xFFu;
}

/* Historical bugs from the comments. */
static inline uint32_t p_nibble_only(uint32_t x, uint32_t len) {      /* high-nibble test only: misses ':'..'?' */
    if (len == 0 || len > 3) return 0;
    uint32_t d = (x ^ 0x30303030u) << ((4 - len) * 8);
    uint32_t num = ((0x640a01u * d) >> 24) & 0xFFu;
    return (((d & 0xF0F0F0F0u) == 0) & (bswap32_(d) <= 0x020505u)) ? (0x100u | num) : 0;
}
static inline uint32_t p_no_or(uint32_t x, uint32_t len) {            /* drops "d |": misses 0xCA..0xCF */
    if (len == 0 || len > 3) return 0;
    uint32_t d = (x ^ 0x30303030u) << ((4 - len) * 8);
    uint32_t num = ((0x640a01u * d) >> 24) & 0xFFu;
    return ((((0x06060606u + d) & 0xF0F0F0F0u) == 0) & (bswap32_(d) <= 0x020505u)) ? (0x100u | num) : 0;
}
