use std::alloc::{alloc, Layout};
#[no_mangle]
pub extern "C" fn walloc(size: usize) -> *mut u8 {
unsafe { alloc(Layout::from_size_align(size, 8).unwrap()) }
}
#[no_mangle]
pub unsafe extern "C" fn backend_name(out: *mut u8, cap: usize) -> usize {
let name = tape_sha256::backend().as_bytes();
let n = name.len().min(cap);
std::ptr::copy_nonoverlapping(name.as_ptr(), out, n);
n
}
#[no_mangle]
pub unsafe extern "C" fn hash_many_prefixed_raw(
prefix: *const u8,
prefix_len: usize,
data: *const u8,
lens: *const u32,
count: usize,
out: *mut u8,
) {
let prefix = std::slice::from_raw_parts(prefix, prefix_len);
let lens = std::slice::from_raw_parts(lens, count);
let mut bodies: Vec<&[u8]> = Vec::with_capacity(count);
let mut off = 0usize;
for &l in lens {
bodies.push(std::slice::from_raw_parts(data.add(off), l as usize));
off += l as usize;
}
let out = std::slice::from_raw_parts_mut(out.cast::<[u8; 32]>(), count);
tape_sha256::hash_many_prefixed(prefix, &bodies, out);
}
#[cfg(target_feature = "simd128")]
macro_rules! wave_entry {
($name:ident, $backend:ident) => {
#[no_mangle]
pub unsafe extern "C" fn $name(
prefix: *const u8,
prefix_len: usize,
data: *const u8,
lens: *const u32,
count: usize,
out: *mut u8,
) {
let prefix = std::slice::from_raw_parts(prefix, prefix_len);
let lens = std::slice::from_raw_parts(lens, count);
let mut bodies: Vec<&[u8]> = Vec::with_capacity(count);
let mut off = 0usize;
for &l in lens {
bodies.push(std::slice::from_raw_parts(data.add(off), l as usize));
off += l as usize;
}
let out = std::slice::from_raw_parts_mut(out.cast::<[u8; 32]>(), count);
tape_sha256::backends::$backend(prefix, &bodies, out);
}
};
}
#[cfg(target_feature = "simd128")]
wave_entry!(hash_many_prefixed_raw_2x4, simd128_8_slices);
#[cfg(target_feature = "simd128")]
wave_entry!(hash_many_prefixed_raw_4x4, simd128_16_slices);