Refactor some char
, u8
ascii functions to be branchless
Decompose singular `matches!` with or-patterns to individual `matches!` statements to enable branchless code output. The following functions were changed: - `is_ascii_alphanumeric` - `is_ascii_hexdigit` - `is_ascii_punctuation` Add codegen tests Co-authored-by: George Bateman <george.bateman16@gmail.com> Co-authored-by: scottmcm <scottmcm@users.noreply.github.com>
This commit is contained in:
parent
8396efecf7
commit
465ffc9ca7
@ -1450,7 +1450,7 @@ pub const fn is_ascii_lowercase(&self) -> bool {
|
|||||||
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn is_ascii_alphanumeric(&self) -> bool {
|
pub const fn is_ascii_alphanumeric(&self) -> bool {
|
||||||
matches!(*self, '0'..='9' | 'A'..='Z' | 'a'..='z')
|
matches!(*self, '0'..='9') | matches!(*self, 'A'..='Z') | matches!(*self, 'a'..='z')
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the value is an ASCII decimal digit:
|
/// Checks if the value is an ASCII decimal digit:
|
||||||
@ -1553,7 +1553,7 @@ pub const fn is_ascii_octdigit(&self) -> bool {
|
|||||||
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn is_ascii_hexdigit(&self) -> bool {
|
pub const fn is_ascii_hexdigit(&self) -> bool {
|
||||||
matches!(*self, '0'..='9' | 'A'..='F' | 'a'..='f')
|
matches!(*self, '0'..='9') | matches!(*self, 'A'..='F') | matches!(*self, 'a'..='f')
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the value is an ASCII punctuation character:
|
/// Checks if the value is an ASCII punctuation character:
|
||||||
@ -1591,7 +1591,10 @@ pub const fn is_ascii_hexdigit(&self) -> bool {
|
|||||||
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn is_ascii_punctuation(&self) -> bool {
|
pub const fn is_ascii_punctuation(&self) -> bool {
|
||||||
matches!(*self, '!'..='/' | ':'..='@' | '['..='`' | '{'..='~')
|
matches!(*self, '!'..='/')
|
||||||
|
| matches!(*self, ':'..='@')
|
||||||
|
| matches!(*self, '['..='`')
|
||||||
|
| matches!(*self, '{'..='~')
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the value is an ASCII graphic character:
|
/// Checks if the value is an ASCII graphic character:
|
||||||
|
@ -791,7 +791,7 @@ pub const fn is_ascii_lowercase(&self) -> bool {
|
|||||||
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn is_ascii_alphanumeric(&self) -> bool {
|
pub const fn is_ascii_alphanumeric(&self) -> bool {
|
||||||
matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
|
matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'Z') | matches!(*self, b'a'..=b'z')
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the value is an ASCII decimal digit:
|
/// Checks if the value is an ASCII decimal digit:
|
||||||
@ -894,7 +894,7 @@ pub const fn is_ascii_octdigit(&self) -> bool {
|
|||||||
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn is_ascii_hexdigit(&self) -> bool {
|
pub const fn is_ascii_hexdigit(&self) -> bool {
|
||||||
matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
|
matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'F') | matches!(*self, b'a'..=b'f')
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the value is an ASCII punctuation character:
|
/// Checks if the value is an ASCII punctuation character:
|
||||||
@ -932,7 +932,10 @@ pub const fn is_ascii_hexdigit(&self) -> bool {
|
|||||||
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn is_ascii_punctuation(&self) -> bool {
|
pub const fn is_ascii_punctuation(&self) -> bool {
|
||||||
matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
|
matches!(*self, b'!'..=b'/')
|
||||||
|
| matches!(*self, b':'..=b'@')
|
||||||
|
| matches!(*self, b'['..=b'`')
|
||||||
|
| matches!(*self, b'{'..=b'~')
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the value is an ASCII graphic character:
|
/// Checks if the value is an ASCII graphic character:
|
||||||
|
47
tests/codegen/char-ascii-branchless.rs
Normal file
47
tests/codegen/char-ascii-branchless.rs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// Checks that these functions are branchless.
|
||||||
|
//
|
||||||
|
// compile-flags: -O
|
||||||
|
|
||||||
|
#![crate_type = "lib"]
|
||||||
|
|
||||||
|
// CHECK-LABEL: @is_ascii_alphanumeric_char
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn is_ascii_alphanumeric_char(x: char) -> bool {
|
||||||
|
// CHECK-NOT: br
|
||||||
|
x.is_ascii_alphanumeric()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CHECK-LABEL: @is_ascii_alphanumeric_u8
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn is_ascii_alphanumeric_u8(x: u8) -> bool {
|
||||||
|
// CHECK-NOT: br
|
||||||
|
x.is_ascii_alphanumeric()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CHECK-LABEL: @is_ascii_hexdigit_char
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn is_ascii_hexdigit_char(x: char) -> bool {
|
||||||
|
// CHECK-NOT: br
|
||||||
|
x.is_ascii_hexdigit()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CHECK-LABEL: @is_ascii_hexdigit_u8
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn is_ascii_hexdigit_u8(x: u8) -> bool {
|
||||||
|
// CHECK-NOT: br
|
||||||
|
x.is_ascii_hexdigit()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CHECK-LABEL: @is_ascii_punctuation_char
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn is_ascii_punctuation_char(x: char) -> bool {
|
||||||
|
// CHECK-NOT: br
|
||||||
|
x.is_ascii_punctuation()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CHECK-LABEL: @is_ascii_punctuation_u8
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn is_ascii_punctuation_u8(x: u8) -> bool {
|
||||||
|
// CHECK-NOT: br
|
||||||
|
x.is_ascii_punctuation()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user