Open
Description
In the following code, the first while
loop should process 8 bytes at a time and exit early if an invalid byte is found.
The remaining bytes should be known to be bytes.len() % 8
, but the auto-vectorization unrolls to test again for 32 bytes and 8 bytes at a time
https://rust.godbolt.org/z/z8hnb9PGY
pub const fn is_ascii(bytes: &[u8]) -> bool {
const N1: usize = 8;
let mut i = 0;
while i + N1 <= bytes.len() {
let chunk_end = i + N1;
let mut count = 0;
while i < chunk_end {
count += (bytes[i] <= 127) as u8;
i += 1;
}
if count != N1 as u8 {
return false;
}
}
// Process the remaining `bytes.len() % N` bytes.
let mut is_ascii = true;
while i < bytes.len() {
is_ascii &= bytes[i] <= 127;
i += 1;
}
is_ascii
}