diff --git a/src/header/string/mod.rs b/src/header/string/mod.rs index 9a6e4ddd225e182f39b5771c9dbd1a3f8777af23..0efe60f8c8151b9a155671340868545eb7685b9e 100644 --- a/src/header/string/mod.rs +++ b/src/header/string/mod.rs @@ -73,14 +73,32 @@ pub unsafe extern "C" fn memchr(s: *const c_void, c: c_int, n: usize) -> *mut c_ #[no_mangle] pub unsafe extern "C" fn memcmp(s1: *const c_void, s2: *const c_void, n: usize) -> c_int { - let mut i = 0; - while i < n { - let a = *(s1 as *const u8).offset(i as isize); - let b = *(s2 as *const u8).offset(i as isize); - if a != b { + let (div, rem) = (n / mem::size_of::<usize>(), n % mem::size_of::<usize>()); + let mut a = s1 as *const usize; + let mut b = s2 as *const usize; + for _ in 0..div { + if *a != *b { + for i in 0..mem::size_of::<usize>() { + let c = *(a as *const u8).offset(i as isize); + let d = *(b as *const u8).offset(i as isize); + if c != d { + return c as i32 - d as i32; + } + } + unreachable!() + } + a = a.offset(1); + b = b.offset(1); + } + + let mut a = a as *const u8; + let mut b = b as *const u8; + for _ in 0..rem { + if *a != *b { return a as i32 - b as i32; } - i += 1; + a = a.offset(1); + b = b.offset(1); } 0 }