const LO: u64 = 0x0101_0101_0101_0101;
const HI: u64 = 0x8080_8080_8080_8080;
#[inline(always)]
pub(crate) fn broadcast(byte: u8) -> u64 {
byte as u64 * LO
}
#[inline(always)]
pub(crate) fn zero_lanes(v: u64) -> u64 {
v.wrapping_sub(LO) & !v & HI
}
#[inline(always)]
pub(crate) fn less_lanes(word: u64, limit_mask: u64) -> u64 {
word.wrapping_sub(limit_mask) & !word & HI
}
#[inline(always)]
pub(crate) fn non_ascii_lanes(word: u64) -> u64 {
word & HI
}
#[inline(always)]
pub(crate) fn load_word(bytes: &[u8], i: usize) -> u64 {
let mut chunk = [0u8; 8];
chunk.copy_from_slice(&bytes[i..i + 8]);
u64::from_ne_bytes(chunk)
}
pub(crate) fn find_either(bytes: &[u8], mut i: usize, a: u8, b: u8) -> usize {
let (wide_a, wide_b) = (broadcast(a), broadcast(b));
while i + 8 <= bytes.len() {
let word = load_word(bytes, i);
if zero_lanes(word ^ wide_a) | zero_lanes(word ^ wide_b) != 0 {
break;
}
i += 8;
}
while i < bytes.len() && bytes[i] != a && bytes[i] != b {
i += 1;
}
i
}
#[cfg(test)]
mod tests {
use super::*;
fn hasbyte(word: u64, needle: u64) -> bool {
zero_lanes(word ^ needle) != 0
}
fn hasless(word: u64, limit: u64) -> bool {
less_lanes(word, limit) != 0
}
fn has_non_ascii(word: u64) -> bool {
non_ascii_lanes(word) != 0
}
#[test]
fn combined_masks_match_a_scalar_reference() {
let needles = *b"\"\\&";
for fill in [b'a', b' ', b'~', 0x21, 0x7F, b'"' + 1, b'\\' - 1] {
for pos in 0..8 {
for probe in (0u8..=0xFF)
.step_by(3)
.chain([b'"', b'\\', b'&', 0x1F, 0x20, 0x80])
{
let mut bytes = [fill; 8];
bytes[pos] = probe;
let word = load_word(&bytes, 0);
let stop = needles.iter().fold(
non_ascii_lanes(word) | less_lanes(word, broadcast(0x20)),
|acc, n| acc | zero_lanes(word ^ broadcast(*n)),
);
let want = bytes
.iter()
.any(|b| *b >= 0x80 || *b < 0x20 || needles.contains(b));
assert_eq!(stop != 0, want, "bytes {bytes:?}");
}
}
}
}
#[test]
fn detects_each_byte_value() {
let word = load_word(b"a\"b\\c\x01de", 0); assert!(hasbyte(word, broadcast(b'"')));
assert!(hasbyte(word, broadcast(b'\\')));
assert!(!hasbyte(word, broadcast(b'x')));
}
#[test]
fn detects_controls_and_non_ascii() {
let word = load_word(b"abcdefg\x01", 0);
assert!(hasless(word, broadcast(0x20)));
let word = load_word("日本語です!".as_bytes(), 0);
assert!(has_non_ascii(word));
let word = load_word(b"abcdefgh", 0);
assert!(!hasless(word, broadcast(0x20)));
assert!(!has_non_ascii(word));
assert!(!hasbyte(word, broadcast(b'"')));
}
#[test]
fn space_is_not_a_control() {
let word = load_word(b"abc defg", 0);
assert!(!hasless(word, broadcast(0x20)));
}
#[test]
fn find_either_matches_a_scalar_scan() {
let text = "plain ascii then \u{e9}\u{201c} and a quote\" far \\ away".repeat(3);
let bytes = text.as_bytes();
for start in 0..bytes.len() {
let want = (start..bytes.len())
.find(|&i| bytes[i] == b'"' || bytes[i] == b'\\')
.unwrap_or(bytes.len());
assert_eq!(find_either(bytes, start, b'"', b'\\'), want, "from {start}");
}
assert_eq!(find_either(b"", 0, b'"', b'\\'), 0);
}
}