-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mem: Add REP MOVSB/STOSB implementations
The assembly generated seems correct: https://rust.godbolt.org/z/GGnec8 Signed-off-by: Joe Richey <[email protected]>
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use super::c_int; | ||
|
||
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)] | ||
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, count: usize) -> *mut u8 { | ||
asm!( | ||
"rep movsb [rdi], [rsi]", | ||
inout("rcx") count => _, | ||
inout("rdi") dest => _, | ||
inout("rsi") src => _, | ||
options(nostack, preserves_flags) | ||
); | ||
dest | ||
} | ||
|
||
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)] | ||
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, count: usize) -> *mut u8 { | ||
let delta = dest as usize - src as usize; | ||
if delta >= count { | ||
// We can copy forwards because either dest is far enough ahead of src, | ||
// or src is ahead of dest (and delta overflowed). | ||
return self::memcpy(dest, src, count); | ||
} | ||
// copy backwards | ||
asm!( | ||
"std", | ||
"rep movsb [rdi], [rsi]", | ||
"cld", | ||
inout("rcx") count => _, | ||
inout("rdi") dest.add(count).sub(1) => _, | ||
inout("rsi") src.add(count).sub(1) => _, | ||
options(nostack, preserves_flags) | ||
); | ||
dest | ||
} | ||
|
||
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)] | ||
pub unsafe extern "C" fn memset(dest: *mut u8, c: c_int, count: usize) -> *mut u8 { | ||
asm!( | ||
"rep stosb [rdi], al", | ||
inout("rcx") count => _, | ||
inout("rdi") dest => _, | ||
in("al") c as u8, | ||
options(nostack, preserves_flags) | ||
); | ||
dest | ||
} |