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:
Amaan Qureshi 2026-03-27 18:03:37 -04:00
commit c426e88d99
No known key found for this signature in database

View file

@ -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