Skip to content

Commit

Permalink
Specialize strlen for x86_64.
Browse files Browse the repository at this point in the history
  • Loading branch information
TDecking authored Feb 21, 2023
1 parent 5511f30 commit 2a67ad7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/mem/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,13 @@ pub unsafe fn compare_bytes(s1: *const u8, s2: *const u8, n: usize) -> i32 {
}
0
}

#[inline(always)]
pub unsafe fn c_string_length(mut s: *const core::ffi::c_char) -> usize {
let mut n = 0;
while *s != 0 {
n += 1;
s = s.add(1);
}
n
}
8 changes: 1 addition & 7 deletions src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ intrinsics! {
#[mem_builtin]
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), linkage = "weak")]
pub unsafe extern "C" fn strlen(s: *const core::ffi::c_char) -> usize {
let mut n = 0;
let mut s = s;
while *s != 0 {
n += 1;
s = s.offset(1);
}
n
impls::c_string_length(s)
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/mem/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 std::ffi::c_char) -> usize {
let mut n: usize;

std::arch::asm!(
// search for a zero byte
"xor al, al",

// unbounded memory region
"xor rcx, rcx",
"not rcx",

// forward direction
"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.
Expand Down

0 comments on commit 2a67ad7

Please sign in to comment.