mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-04-12 04:47:40 +00:00
crates/asm: add bcmp and memcmp for static linking without libc
riscv64 codegen emits calls to bcmp for byte comparisons, which is undefined when linking with -static and no libc. Provide both bcmp and memcmp implementations alongside the existing memcpy/memset/strlen.
This commit is contained in:
parent
d118e591d0
commit
c426e88d99
1 changed files with 27 additions and 0 deletions
|
|
@ -55,6 +55,33 @@ pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: usize) -> *mut u8 {
|
|||
s
|
||||
}
|
||||
|
||||
/// Compares two byte sequences.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// `s1` and `s2` must be valid pointers to memory of at least `n` bytes.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
|
||||
for i in 0..n {
|
||||
let a = unsafe { *s1.add(i) };
|
||||
let b = unsafe { *s2.add(i) };
|
||||
if a != b {
|
||||
return i32::from(a) - i32::from(b);
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
/// Compares two byte sequences.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// `s1` and `s2` must be valid pointers to memory of at least `n` bytes.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
|
||||
unsafe { bcmp(s1, s2, n) }
|
||||
}
|
||||
|
||||
/// Calculates the length of a null-terminated string.
|
||||
///
|
||||
/// # Safety
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue