Skip to content

Commit 611808e

Browse files
committed
Use quad-word rep string instructions
Signed-off-by: Joe Richey <[email protected]>
1 parent 5886c98 commit 611808e

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/mem/x86_64.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ use super::c_int;
1515

1616
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)]
1717
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, count: usize) -> *mut u8 {
18+
let qword_count = count >> 3;
19+
let byte_count = count & 0b111;
1820
asm!(
21+
"rep movsq [rdi], [rsi]",
22+
"mov ecx, {byte_count:e}",
1923
"rep movsb [rdi], [rsi]",
20-
inout("rcx") count => _,
24+
byte_count = in(reg) byte_count,
25+
inout("rcx") qword_count => _,
2126
inout("rdi") dest => _,
2227
inout("rsi") src => _,
2328
options(nostack, preserves_flags)
@@ -34,25 +39,37 @@ pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, count: usize) ->
3439
return self::memcpy(dest, src, count);
3540
}
3641
// copy backwards
42+
let qword_count = count >> 3;
43+
let byte_count = count & 0b111;
3744
asm!(
3845
"std",
46+
"rep movsq [rdi], [rsi]",
47+
"mov ecx, {byte_count:e}",
48+
"add rdi, 7",
49+
"add rsi, 7",
3950
"rep movsb [rdi], [rsi]",
4051
"cld",
41-
inout("rcx") count => _,
42-
inout("rdi") dest.add(count).sub(1) => _,
43-
inout("rsi") src.add(count).sub(1) => _,
44-
options(nostack, preserves_flags)
52+
byte_count = in(reg) byte_count,
53+
inout("rcx") qword_count => _,
54+
inout("rdi") dest.offset(count as isize).wrapping_sub(8) => _,
55+
inout("rsi") src.offset(count as isize).wrapping_sub(8) => _,
56+
options(nostack)
4557
);
4658
dest
4759
}
4860

4961
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)]
5062
pub unsafe extern "C" fn memset(dest: *mut u8, c: c_int, count: usize) -> *mut u8 {
63+
let qword_count = count >> 3;
64+
let byte_count = count & 0b111;
5165
asm!(
66+
"rep stosq [rdi], rax",
67+
"mov ecx, {byte_count:e}",
5268
"rep stosb [rdi], al",
53-
inout("rcx") count => _,
69+
byte_count = in(reg) byte_count,
70+
inout("rcx") qword_count => _,
5471
inout("rdi") dest => _,
55-
in("al") c as u8,
72+
in("rax") (c as u8 as u64) * 0x0101010101010101,
5673
options(nostack, preserves_flags)
5774
);
5875
dest

0 commit comments

Comments
 (0)