refactor(lexer): use consts for classify LUT masks (#28677)

Pure refactor.

Introduce a set of `const`s for the masks used in `MERGED_LUTS` and `WORD_LUTS`. Previously these values were hard-coded in multiple places.
This commit is contained in:
overlookmotel committed 2026-11-02 00:20:35 +00:00
1 parent bba211ed27
commit e7dc8bbbec
3 files changed
+39 -22

No files matched your search

@@ -8,7 +8,7 @@ use crate::{
use crate::pipeline::chunk::{load256, mm, veq};
use super::{
luts::{MERGED_LUTS, WORD_LUTS},
luts::{DIGIT_MASK, KWINIT_MASK, MERGED_LUTS, OPCH_MASK, WORD_LUTS, WORD_MASK, WS_MASK},
punct1_luts::{PH_A, PH_B, PH_T0, PH_T1},
};
@@ -56,11 +56,11 @@ pub(super) unsafe fn classify_avx2(
_mm256_broadcastsi128_si256(_mm_loadu_si128(WORD_LUTS.lo.as_ptr() as *const __m128i));
let v_wbhi =
_mm256_broadcastsi128_si256(_mm_loadu_si128(WORD_LUTS.hi.as_ptr() as *const __m128i));
let v_kwpl = _mm256_set1_epi8(0x03);
let v_oppl = _mm256_set1_epi8(0x3c);
let v_wdpl = _mm256_set1_epi8(0x3f);
let v_wspl = _mm256_set1_epi8(0xc0u8 as i8);
let v_dgpl = _mm256_set1_epi8(0x02);
let v_kwpl = _mm256_set1_epi8(KWINIT_MASK as i8);
let v_oppl = _mm256_set1_epi8(OPCH_MASK as i8);
let v_wdpl = _mm256_set1_epi8(WORD_MASK as i8);
let v_wspl = _mm256_set1_epi8(WS_MASK as i8);
let v_dgpl = _mm256_set1_epi8(DIGIT_MASK as i8);
let v_ones = _mm256_set1_epi8(0xffu8 as i8);
let v_zero = _mm256_setzero_si256();
let v_x0f = _mm256_set1_epi8(0x0f);
+24 -9
View File
@@ -31,6 +31,14 @@ pub(super) struct MergedLuts {
pub lo_ts: [u8; 16],
}
/// Bits of a [`MERGED_LUTS`] lookup which are set if a byte can start a keyword.
pub(super) const KWINIT_MASK: u8 = 0x03;
/// Bits of a [`MERGED_LUTS`] lookup which are set if a byte is an operator char.
pub(super) const OPCH_MASK: u8 = 0x3c;
/// Bit of a [`MERGED_LUTS`] lookup which is set if a byte is `.`.
#[cfg_attr(all(target_arch = "x86_64", not(test)), expect(dead_code))]
pub(super) const DOT_MASK: u8 = 0x80;
pub(super) const MERGED_LUTS: MergedLuts = {
let cpu = BASELINE;
@@ -88,6 +96,13 @@ pub(super) struct WordLuts {
pub hi: [u8; 16],
}
/// Bits of a [`WORD_LUTS`] lookup which are set if a byte is an identifier char.
pub(super) const WORD_MASK: u8 = 0x3f;
/// Bits of a [`WORD_LUTS`] lookup which are set if a byte is whitespace.
pub(super) const WS_MASK: u8 = 0xc0;
/// Bit of a [`WORD_LUTS`] lookup which is set if a byte is a digit.
pub(super) const DIGIT_MASK: u8 = 0x02;
pub(super) const WORD_LUTS: WordLuts = {
let cpu = BASELINE;
@@ -139,9 +154,9 @@ mod tests {
let cb = c as u8;
let t = if c < 0x80 { MERGED_LUTS.lo[c & 15] & MERGED_LUTS.hi[c >> 4] } else { 0 };
let kw = (t & 0x03) != 0;
let opp = (t & 0x3C) != 0;
let dt = (t & 0x80) != 0;
let kw = (t & KWINIT_MASK) != 0;
let opp = (t & OPCH_MASK) != 0;
let dt = (t & DOT_MASK) != 0;
assert!(
kw == is_kw_init(cb) && opp == is_op_char(cb, BASELINE) && dt == (cb == b'.'),
"MERGED_LUTS.lo / hi wrong at byte {c:#04x}"
@@ -149,9 +164,9 @@ mod tests {
let ts_t =
if c < 0x80 { MERGED_LUTS.lo_ts[c & 15] & MERGED_LUTS.hi[c >> 4] } else { 0 };
let ts_kw = (ts_t & 0x03) != 0;
let ts_opp = (ts_t & 0x3C) != 0;
let ts_dt = (ts_t & 0x80) != 0;
let ts_kw = (ts_t & KWINIT_MASK) != 0;
let ts_opp = (ts_t & OPCH_MASK) != 0;
let ts_dt = (ts_t & DOT_MASK) != 0;
assert!(
ts_kw == is_kw_init_ts(cb) && ts_opp == opp && ts_dt == dt,
"MERGED_LUTS.lo_ts wrong at byte {c:#04x}"
@@ -165,9 +180,9 @@ mod tests {
let cb = c as u8;
let tb = if c < 0x80 { WORD_LUTS.lo[c & 15] & WORD_LUTS.hi[c >> 4] } else { 0 };
let wd = (c >= 0x80) || (tb & 0x3F) != 0;
let ws = (tb & 0xC0) != 0;
let dg = (tb & 0x02) != 0;
let wd = (c >= 0x80) || (tb & WORD_MASK) != 0;
let ws = (tb & WS_MASK) != 0;
let dg = (tb & DIGIT_MASK) != 0;
assert!(
wd == is_word(cb, BASELINE)
&& ws == is_ws(cb, BASELINE)
@@ -6,7 +6,9 @@ use crate::{
};
use super::{
luts::{MERGED_LUTS, WORD_LUTS},
luts::{
DIGIT_MASK, DOT_MASK, KWINIT_MASK, MERGED_LUTS, OPCH_MASK, WORD_LUTS, WORD_MASK, WS_MASK,
},
punct1_luts::{PH_A, PH_B, PH_T0, PH_T1},
};
@@ -121,9 +123,9 @@ fn chunk(v: uint8x16_t, l: &Luts, cpu: Neon) -> Classified {
let t = vandq_u8(vqtbl1q_u8(l.mrg_lo, lo), vqtbl1q_u8(l.mrg_hi, hn));
let na = vtstq_u8(v, vdupq_n_u8(0x80));
let word = vorrq_u8(vtstq_u8(tb, vdupq_n_u8(0x3f)), na);
let ws = vtstq_u8(tb, vdupq_n_u8(0xc0));
let digit = vtstq_u8(tb, vdupq_n_u8(0x02));
let word = vorrq_u8(vtstq_u8(tb, vdupq_n_u8(WORD_MASK)), na);
let ws = vtstq_u8(tb, vdupq_n_u8(WS_MASK));
let digit = vtstq_u8(tb, vdupq_n_u8(DIGIT_MASK));
let misc = vorrq_u8(vorrq_u8(veq(v, b'#', cpu), veq(v, b'\\', cpu)), na);
let mut kind = punct1(v, lo, hn, l, cpu);
@@ -135,9 +137,9 @@ fn chunk(v: uint8x16_t, l: &Luts, cpu: Neon) -> Classified {
word,
ws,
digit,
dot: vtstq_u8(t, vdupq_n_u8(0x80)),
kwinit: vtstq_u8(t, vdupq_n_u8(0x03)),
opch: vtstq_u8(t, vdupq_n_u8(0x3c)),
dot: vtstq_u8(t, vdupq_n_u8(DOT_MASK)),
kwinit: vtstq_u8(t, vdupq_n_u8(KWINIT_MASK)),
opch: vtstq_u8(t, vdupq_n_u8(OPCH_MASK)),
misc,
kind,
}