Skip to content

Commit 82ff02b

Browse files
committed
Make char::is_ascii_whitespace branchless on 64-bit
1 parent e0bf356 commit 82ff02b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

library/core/src/char/methods.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,9 +1544,18 @@ impl char {
15441544
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
15451545
#[inline]
15461546
pub const fn is_ascii_whitespace(&self) -> bool {
1547-
match *self {
1548-
'\t' | '\n' | '\x0C' | '\r' | ' ' => true,
1549-
_ => false,
1547+
#[cfg(target_pointer_width = "64")]
1548+
{
1549+
// Inspired from https://pdimov.github.io/blog/2020/07/19/llvm-and-memchr/
1550+
const MASK: u64 = 1 << b'\t' | 1 << b'\n' | 1 << b'\x0C' | 1 << b'\r' | 1 << b' ';
1551+
*self <= ' ' && 1u64 << (*self as u8) & MASK != 0
1552+
}
1553+
#[cfg(not(target_pointer_width = "64"))]
1554+
{
1555+
match *self {
1556+
'\t' | '\n' | '\x0C' | '\r' | ' ' => true,
1557+
_ => false,
1558+
}
15501559
}
15511560
}
15521561

0 commit comments

Comments
 (0)