arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-hal/src/lib.rs
poulpy-hal/src/lib.rsRaw
  1#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code, improper_ctypes)]
  2#![deny(rustdoc::broken_intra_doc_links)]
  3#![cfg_attr(docsrs, feature(doc_cfg))]
  4#![feature(trait_alias)]
  5
  6pub mod api;
  7pub mod bench_suite;
  8pub mod delegates;
  9pub mod layouts;
 10pub mod oep;
 11pub mod reference;
 12pub mod source;
 13pub mod test_suite;
 14
 15pub mod doc {
 16    #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/docs/backend_safety_contract.md"))]
 17    pub mod backend_safety {
 18        pub const _PLACEHOLDER: () = ();
 19    }
 20}
 21
 22pub const GALOISGENERATOR: u64 = 5;
 23pub const DEFAULTALIGN: usize = 64;
 24
 25fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool {
 26    (ptr as usize).is_multiple_of(align)
 27}
 28
 29pub fn is_aligned<T>(ptr: *const T) -> bool {
 30    is_aligned_custom(ptr, DEFAULTALIGN)
 31}
 32
 33pub fn assert_alignement<T>(ptr: *const T) {
 34    assert!(
 35        is_aligned(ptr),
 36        "invalid alignement: ensure passed bytes have been allocated with [alloc_aligned_u8] or [alloc_aligned]"
 37    )
 38}
 39
 40pub fn cast<T, V>(data: &[T]) -> &[V] {
 41    let ptr: *const V = data.as_ptr() as *const V;
 42    let len: usize = data.len() / size_of::<V>();
 43    unsafe { std::slice::from_raw_parts(ptr, len) }
 44}
 45
 46#[allow(clippy::mut_from_ref)]
 47pub fn cast_mut<T, V>(data: &[T]) -> &mut [V] {
 48    let ptr: *mut V = data.as_ptr() as *mut V;
 49    let len: usize = data.len() / size_of::<V>();
 50    unsafe { std::slice::from_raw_parts_mut(ptr, len) }
 51}
 52
 53/// Allocates a block of bytes with a custom alignement.
 54/// Alignement must be a power of two and size a multiple of the alignement.
 55/// Allocated memory is initialized to zero.
 56fn alloc_aligned_custom_u8(size: usize, align: usize) -> Vec<u8> {
 57    assert!(
 58        align.is_power_of_two(),
 59        "Alignment must be a power of two but is {}",
 60        align
 61    );
 62    assert_eq!(
 63        (size * size_of::<u8>()) % align,
 64        0,
 65        "size={} must be a multiple of align={}",
 66        size,
 67        align
 68    );
 69    unsafe {
 70        let layout: std::alloc::Layout = std::alloc::Layout::from_size_align(size, align).expect("Invalid alignment");
 71        let ptr: *mut u8 = std::alloc::alloc(layout);
 72        if ptr.is_null() {
 73            panic!("Memory allocation failed");
 74        }
 75        assert!(
 76            is_aligned_custom(ptr, align),
 77            "Memory allocation at {:p} is not aligned to {} bytes",
 78            ptr,
 79            align
 80        );
 81        // Init allocated memory to zero
 82        std::ptr::write_bytes(ptr, 0, size);
 83        Vec::from_raw_parts(ptr, size, size)
 84    }
 85}
 86
 87/// Allocates a block of T aligned with [DEFAULTALIGN].
 88/// Size of T * size msut be a multiple of [DEFAULTALIGN].
 89pub fn alloc_aligned_custom<T>(size: usize, align: usize) -> Vec<T> {
 90    assert!(
 91        align.is_power_of_two(),
 92        "Alignment must be a power of two but is {}",
 93        align
 94    );
 95
 96    assert_eq!(
 97        (size * size_of::<T>()) % align,
 98        0,
 99        "size*size_of::<T>()={} must be a multiple of align={}",
100        size * size_of::<T>(),
101        align
102    );
103
104    let mut vec_u8: Vec<u8> = alloc_aligned_custom_u8(size_of::<T>() * size, align);
105    let ptr: *mut T = vec_u8.as_mut_ptr() as *mut T;
106    let len: usize = vec_u8.len() / size_of::<T>();
107    let cap: usize = vec_u8.capacity() / size_of::<T>();
108    std::mem::forget(vec_u8);
109    unsafe { Vec::from_raw_parts(ptr, len, cap) }
110}
111
112/// Allocates an aligned vector of the given size.
113/// Padds until it is size in [u8] a multiple of [DEFAULTALIGN].
114pub fn alloc_aligned<T>(size: usize) -> Vec<T> {
115    alloc_aligned_custom::<T>(
116        (size * size_of::<T>()).next_multiple_of(DEFAULTALIGN) / size_of::<T>(),
117        DEFAULTALIGN,
118    )
119}