Skip to content

Make char::is_ascii_whitespace branchless on 32 and 64-bit targets #77021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make char::is_ascii_whitespace branchless on 64-bit
  • Loading branch information
lnicola committed Sep 21, 2020
commit 82ff02b031e70b9ce692f4793f8225da9111286c
15 changes: 12 additions & 3 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,18 @@ impl char {
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
#[inline]
pub const fn is_ascii_whitespace(&self) -> bool {
match *self {
'\t' | '\n' | '\x0C' | '\r' | ' ' => true,
_ => false,
#[cfg(target_pointer_width = "64")]
{
// Inspired from https://pdimov.github.io/blog/2020/07/19/llvm-and-memchr/
const MASK: u64 = 1 << b'\t' | 1 << b'\n' | 1 << b'\x0C' | 1 << b'\r' | 1 << b' ';
*self <= ' ' && 1u64 << (*self as u8) & MASK != 0
}
#[cfg(not(target_pointer_width = "64"))]
{
match *self {
'\t' | '\n' | '\x0C' | '\r' | ' ' => true,
_ => false,
}
}
}

Expand Down