Lime Parser Generator 0.1.0
Runtime-extensible LALR(1) parser with SIMD tokenization and LLVM JIT
Loading...
Searching...
No Matches
tokenize_simd.h
1/*
2** SIMD Character Classification
3**
4** Provides parallel character classification using SIMD instructions
5** (AVX2 on x86_64, NEON on ARM) with a scalar fallback.
6**
7** All backends classify 32 characters per call and return a
8** 32-bit bitmask per character class. AVX2 uses a single 256-bit
9** register; NEON uses two 128-bit register operations.
10*/
11#ifndef TOKENIZE_SIMD_H
12#define TOKENIZE_SIMD_H
13
14#include <stdint.h>
15#include <stddef.h>
16#include <stdbool.h>
17
18typedef struct CharClassVector {
19 uint32_t is_alpha_mask; /* Bitmask: bit i set if character i is alphabetic */
20 uint32_t is_digit_mask; /* Bitmask: bit i set if character i is a digit */
21 uint32_t is_space_mask; /* Bitmask: bit i set if character i is whitespace */
22 uint32_t is_high_byte_mask; /* Bitmask: bit i set if byte i >= 0x80 (UTF-8 lead/continuation) */
24
25/* Function pointer type for classification.
26** Classifies 32 characters starting at input+offset.
27** The caller must ensure at least 32 bytes are readable from input+offset.
28*/
29typedef CharClassVector (*ClassifyFunc)(const char *input, size_t offset);
30
31/*
32** Return the best available classification function for the current CPU.
33** On x86_64 with AVX2 support, returns the AVX2 implementation.
34** On ARM with NEON, returns the NEON implementation.
35** Otherwise returns the scalar fallback.
36*/
37ClassifyFunc get_classify_func(void);
38
39/*
40** Scalar fallback -- always available on every platform.
41** Classifies 32 characters starting at input+offset.
42*/
43CharClassVector classify_scalar(const char *input, size_t offset);
44
45#if defined(__x86_64__) || defined(__i386__)
46/*
47** AVX2 implementation -- classifies 32 characters in a single 256-bit op.
48** Uses target("avx2") attribute; safe to call only after checking
49** CPU support via get_classify_func() or cpu_supports_avx2().
50*/
51CharClassVector classify_simd_avx2(const char *input, size_t offset);
52#endif
53
54#ifdef __ARM_NEON
55/*
56** NEON implementation -- classifies 32 characters using two 128-bit
57** register ops. All 32 bits of each mask are meaningful.
58*/
59CharClassVector classify_simd_neon(const char *input, size_t offset);
60#endif
61
62#if defined(__riscv) && defined(__riscv_v_intrinsic)
63/*
64** RVV (RISC-V Vector Extension) 1.0 implementation -- classifies 32
65** characters via vsetvl_e8m1(32) and vector predicate ops. Works
66** with any VLEN; the hardware vector length affects throughput but
67** not program correctness.
68*/
69CharClassVector classify_simd_rvv(const char *input, size_t offset);
70#endif
71
72#endif /* TOKENIZE_SIMD_H */