-
Notifications
You must be signed in to change notification settings - Fork 230
Specialize strlen
for x86_64
.
#516
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
Changes from 3 commits
2a67ad7
7711331
1fdf932
0a0fa0b
1a2f3b2
7e4742d
9c0a19c
afa3d3e
1df0d1c
4f77170
6488b26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,6 +173,35 @@ pub unsafe fn compare_bytes(a: *const u8, b: *const u8, n: usize) -> i32 { | |
c16(a.cast(), b.cast(), n) | ||
} | ||
|
||
#[inline(always)] | ||
pub unsafe fn c_string_length(s: *const core::ffi::c_char) -> usize { | ||
let mut n: usize; | ||
|
||
asm!( | ||
// search for a zero byte | ||
"xor al, al", | ||
|
||
// unbounded memory region | ||
"xor rcx, rcx", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"not rcx", | ||
|
||
// forward direction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is guaranteed to be set to the forward direction already due to abi requirements. |
||
"cld", | ||
|
||
// perform search | ||
"repne scasb", | ||
|
||
// extract length | ||
"not rcx", | ||
"dec rcx", | ||
inout("rdi") s => _, | ||
out("rcx") n, | ||
options(nostack), | ||
); | ||
|
||
n | ||
} | ||
|
||
/// Determine optimal parameters for a `rep` instruction. | ||
fn rep_param(dest: *mut u8, mut count: usize) -> (usize, usize, usize) { | ||
// Unaligned writes are still slow on modern processors, so align the destination address. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xor eax, eax
avoids a potential partial register stall and is 1 byte shorter I believe.