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
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ Kyle J Strand <batmanaod@gmail.com> <BatmanAoD@users.noreply.github.com>
Kyle J Strand <batmanaod@gmail.com> <kyle.j.strand@gmail.com>
Kyle J Strand <batmanaod@gmail.com> <kyle.strand@pieinsurance.com>
Kyle J Strand <batmanaod@gmail.com> <kyle.strand@rms.com>
Laurențiu Nicola <lnicola@dend.ro>
Laurențiu Nicola <lnicola@dend.ro> Laurentiu Nicola <lnicola@dend.ro>
Laurențiu Nicola <lnicola@dend.ro> <lnicola@users.noreply.github.com>
Lee Jeffery <leejeffery@gmail.com> Lee Jeffery <lee@leejeffery.co.uk>
Lee Wondong <wdlee91@gmail.com>
Lennart Kudling <github@kudling.de>
Expand Down
20 changes: 17 additions & 3 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,23 @@ 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(not(target_pointer_width = "16"))]
{
// Inspired from https://pdimov.github.io/blog/2020/07/19/llvm-and-memchr/
const MASK: u32 = 1 << (b'\t' - 1)
| 1 << (b'\n' - 1)
| 1 << (b'\x0C' - 1)
| 1 << (b'\r' - 1)
| 1 << (b' ' - 1);
let ch = (*self as u32).wrapping_sub(1);
ch < (' ' as u32) && 1 << (ch as u8) & MASK != 0
}
#[cfg(target_pointer_width = "16")]
{
match *self {
'\t' | '\n' | '\x0C' | '\r' | ' ' => true,
_ => false,
}
}
}

Expand Down