#[allow(warnings)]
#[cfg(target_pointer_width = "64")]
type c_int = u64;
#[allow(warnings)]
#[cfg(target_pointer_width = "16")]
type c_int = u16;
#[allow(warnings)]
#[cfg(not(any(target_pointer_width = "16", target_pointer_width = "64")))]
type c_int = u32;
use core::mem::size_of;
const PTR_SIZE: usize = size_of::<c_int>();
const CHUNK_SIZES_MEMCPY: [usize; 3] = [32, 4, 1]; const CHUNK_SIZES_MEMSET: [usize; 3] = [64, 8, 1]; const CHUNK_SIZES_MEMCMP: [usize; 2] = [8, 1];
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)]
pub unsafe extern "C" fn memcpy(mut dst: *mut u8, src: *const u8, n: usize) -> *mut u8 {
if n == PTR_SIZE {
*(dst as *mut c_int) = *(src as *mut c_int);
return dst;
} else if PTR_SIZE > 4 && n == 4 {
*(dst as *mut u32) = *(src as *mut u32);
return dst;
} else if PTR_SIZE > 2 && n == 2 {
*(dst as *mut u16) = *(src as *mut u16);
return dst;
} else if PTR_SIZE > 1 && n == 1 {
*(dst as *mut u8) = *(src as *mut u8);
return dst;
} else if PTR_SIZE > 3 && n == 3 {
*(dst as *mut u16) = *(src as *mut u16);
*dst.wrapping_add(2) = *src.wrapping_add(2);
return dst;
}
let src_off = (src as usize).wrapping_sub(dst as usize);
let dst_align = dst.align_offset(PTR_SIZE);
let src_align = src.align_offset(PTR_SIZE);
let end = dst.wrapping_add(n);
let min_align = core::cmp::min(dst_align, src_align);
let core = dst.wrapping_add(min_align);
while dst < core {
*dst = *dst.wrapping_add(src_off);
dst = dst.wrapping_add(1);
if dst >= end {
return dst;
}
}
for chunk_size in &CHUNK_SIZES_MEMCPY {
while dst <= end.wrapping_sub(*chunk_size * PTR_SIZE) {
let mut i = 0;
while i < *chunk_size {
*(dst as *mut c_int).wrapping_add(i) =
*(dst.wrapping_add(src_off) as *mut c_int).wrapping_add(i);
i = i + 1;
}
dst = dst.wrapping_add(*chunk_size * PTR_SIZE);
}
}
while dst < end {
*dst = *dst.wrapping_add(src_off);
dst = dst.wrapping_add(1);
}
return dst;
}
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)]
pub unsafe extern "C" fn memcpy_reverse(dst: *mut u8, src: *const u8, n: usize) -> *mut u8 {
if n == PTR_SIZE {
*(dst as *mut c_int) = *(src as *mut c_int);
return dst;
} else if PTR_SIZE > 4 && n == 4 {
*(dst as *mut u32) = *(src as *mut u32);
return dst;
} else if PTR_SIZE > 2 && n == 2 {
*(dst as *mut u16) = *(src as *mut u16);
return dst;
} else if PTR_SIZE > 1 && n == 1 {
*(dst as *mut u8) = *(src as *mut u8);
return dst;
} else if PTR_SIZE > 3 && n == 3 {
*dst.wrapping_add(2) = *src.wrapping_add(2);
*(dst as *mut u16) = *(src as *mut u16);
return dst;
}
let src_off = (src as usize).wrapping_sub(dst as usize);
let dst_end_align = (dst.wrapping_add(n) as usize) % PTR_SIZE;
let src_end_align = (src.wrapping_add(n) as usize) % PTR_SIZE;
let min_align = core::cmp::min(dst_end_align, src_end_align);
let core = dst.wrapping_add(n).wrapping_sub(min_align);
let mut cur = dst.wrapping_add(n);
while cur > core {
cur = cur.wrapping_sub(1);
*cur = *cur.wrapping_add(src_off);
if cur == dst {
return dst;
}
}
for chunk_size in &CHUNK_SIZES_MEMCPY {
while cur >= dst.wrapping_add(*chunk_size * PTR_SIZE) {
cur = cur.wrapping_sub(*chunk_size * PTR_SIZE);
let mut i = *chunk_size;
while i > 0 {
i = i - 1;
*(cur as *mut c_int).wrapping_add(i) =
*(cur.wrapping_add(src_off) as *mut c_int).wrapping_add(i);
}
}
}
while cur > dst {
cur = cur.wrapping_sub(1);
*cur = *cur.wrapping_add(src_off);
}
return dst;
}
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)]
pub unsafe extern "C" fn memmove(dst: *mut u8, src: *const u8, n: usize) -> *mut u8 {
if src < dst as *const u8 {
memcpy_reverse(dst, src, n)
} else {
memcpy(dst, src, n)
}
}
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)]
pub unsafe extern "C" fn memset(mut s: *mut u8, c: c_int, n: usize) -> *mut u8 {
if n == PTR_SIZE {
*(s as *mut c_int) = c_int::from_ne_bytes([c as u8; PTR_SIZE]);
return s;
} else if PTR_SIZE > 4 && n == 4 {
*(s as *mut u32) = u32::from_ne_bytes([c as u8; 4]);
return s;
} else if PTR_SIZE > 2 && n == 2 {
*(s as *mut u16) = u16::from_ne_bytes([c as u8; 2]);
return s;
} else if PTR_SIZE > 1 && n == 1 {
*(s as *mut u8) = c as u8;
return s;
} else if PTR_SIZE > 3 && n == 3 {
*(s as *mut u16) = u16::from_ne_bytes([c as u8; 2]);
*s.wrapping_add(2) = c as u8;
return s;
}
let end = s.wrapping_add(n);
let core = s.wrapping_add(s.align_offset(PTR_SIZE));
while s < core {
*s = c as u8;
s = s.wrapping_add(1);
if s >= end {
return s;
}
}
for chunk_size in &CHUNK_SIZES_MEMSET {
while s <= end.wrapping_sub(*chunk_size * PTR_SIZE) {
let mut i = 0;
while i < *chunk_size {
*(s as *mut c_int).wrapping_add(i) = c_int::from_ne_bytes([c as u8; PTR_SIZE]);
i = i + 1;
}
s = s.wrapping_add(*chunk_size * PTR_SIZE);
}
}
while s < end {
*s = c as u8;
s = s.wrapping_add(1);
}
s
}
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)]
pub unsafe extern "C" fn memcmp(mut s1: *const u8, s2: *const u8, n: usize) -> i32 {
let s2_off = (s2 as usize).wrapping_sub(s1 as usize);
let end = s1.wrapping_add(n);
if n <= PTR_SIZE {
while s1 < end {
let res = *s1 - *s1.wrapping_add(s2_off);
if res != 0 {
return res as i8 as i32;
}
s1 = s1.wrapping_add(1);
}
return 0;
}
let s1_align = s1.align_offset(PTR_SIZE);
let s2_align = s2.align_offset(PTR_SIZE);
let min_align = core::cmp::min(s1_align, s2_align);
let core = s1.wrapping_add(min_align);
while s1 < core {
let res = *s1 - *s1.wrapping_add(s2_off);
if res != 0 {
return res as i8 as i32;
}
s1 = s1.wrapping_add(1);
if s1 >= end {
return 0;
}
}
for chunk_size in &CHUNK_SIZES_MEMCMP {
while s1 <= end.wrapping_sub(*chunk_size * PTR_SIZE) {
let mut i = 0;
while i < *chunk_size {
let res = *(s1 as *mut c_int).wrapping_add(i)
- *(s1.wrapping_add(s2_off) as *mut c_int).wrapping_add(i);
if res != 0 {
let mut j = 0;
while j < 4 {
let res = *((s1 as *mut c_int).wrapping_add(i) as *mut u8).wrapping_add(j)
- *((s1.wrapping_add(s2_off) as *mut c_int).wrapping_add(i) as *mut u8)
.wrapping_add(j);
if res != 0 {
return res as i8 as i32;
}
j = j + 1;
}
unreachable!();
}
i = i + 1;
}
s1 = s1.wrapping_add(*chunk_size * PTR_SIZE);
}
}
while s1 < end {
let res = *s1 - *s1.wrapping_add(s2_off);
if res != 0 {
return res as i8 as i32;
}
s1 = s1.wrapping_add(1);
}
return 0;
}
#[cfg_attr(all(feature = "mem", not(feature = "mangled-names")), no_mangle)]
pub unsafe extern "C" fn bcmp(mut s1: *const u8, s2: *const u8, n: usize) -> i32 {
if n == PTR_SIZE {
return (*(s1 as *const c_int) != *(s2 as *const c_int)) as i32;
} else if PTR_SIZE > 4 && n == 4 {
return (*(s1 as *const u32) != *(s2 as *const u32)) as i32;
} else if PTR_SIZE > 2 && n == 2 {
return (*(s1 as *const u16) != *(s2 as *const u16)) as i32;
} else if PTR_SIZE > 1 && n == 1 {
return (*(s1 as *const u8) != *(s2 as *const u8)) as i32;
} else if PTR_SIZE > 3 && n == 3 {
return (*(s1 as *const u16) != *(s2 as *const u16)
|| *s1.wrapping_add(1) != *s2.wrapping_add(1)) as i32;
}
let s2_off = (s2 as usize).wrapping_sub(s1 as usize);
let s1_align = s1.align_offset(PTR_SIZE);
let s2_align = s2.align_offset(PTR_SIZE);
let end = s1.wrapping_add(n);
let min_align = core::cmp::min(s1_align, s2_align);
let core = s1.wrapping_add(min_align);
while s1 < core {
if *s1 != *s1.wrapping_add(s2_off) {
return true as i32;
}
s1 = s1.wrapping_add(1);
if s1 >= end {
return false as i32;
}
}
for chunk_size in &CHUNK_SIZES_MEMCMP {
while s1 <= end.wrapping_sub(*chunk_size * PTR_SIZE) {
let mut i = 0;
while i < *chunk_size {
if *(s1 as *mut c_int).wrapping_add(i)
!= *(s1.wrapping_add(s2_off) as *mut c_int).wrapping_add(i)
{
return true as i32;
}
i = i + 1;
}
s1 = s1.wrapping_add(*chunk_size * PTR_SIZE);
}
}
while s1 < end {
if *s1 != *s1.wrapping_add(s2_off) {
return true as i32;
}
s1 = s1.wrapping_add(1);
}
return false as i32;
}