poulpy_hal/
lib.rs

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 {align}"
60    );
61    assert_eq!(
62        (size * size_of::<u8>()) % align,
63        0,
64        "size={size} must be a multiple of align={align}"
65    );
66    unsafe {
67        let layout: std::alloc::Layout = std::alloc::Layout::from_size_align(size, align).expect("Invalid alignment");
68        let ptr: *mut u8 = std::alloc::alloc(layout);
69        if ptr.is_null() {
70            panic!("Memory allocation failed");
71        }
72        assert!(
73            is_aligned_custom(ptr, align),
74            "Memory allocation at {ptr:p} is not aligned to {align} bytes"
75        );
76        // Init allocated memory to zero
77        std::ptr::write_bytes(ptr, 0, size);
78        Vec::from_raw_parts(ptr, size, size)
79    }
80}
81
82/// Allocates a block of T aligned with [DEFAULTALIGN].
83/// Size of T * size msut be a multiple of [DEFAULTALIGN].
84pub fn alloc_aligned_custom<T>(size: usize, align: usize) -> Vec<T> {
85    assert!(
86        align.is_power_of_two(),
87        "Alignment must be a power of two but is {align}"
88    );
89
90    assert_eq!(
91        (size * size_of::<T>()) % align,
92        0,
93        "size*size_of::<T>()={} must be a multiple of align={align}",
94        size * size_of::<T>(),
95    );
96
97    let mut vec_u8: Vec<u8> = alloc_aligned_custom_u8(size_of::<T>() * size, align);
98    let ptr: *mut T = vec_u8.as_mut_ptr() as *mut T;
99    let len: usize = vec_u8.len() / size_of::<T>();
100    let cap: usize = vec_u8.capacity() / size_of::<T>();
101    std::mem::forget(vec_u8);
102    unsafe { Vec::from_raw_parts(ptr, len, cap) }
103}
104
105/// Allocates an aligned vector of the given size.
106/// Padds until it is size in [u8] a multiple of [DEFAULTALIGN].
107pub fn alloc_aligned<T>(size: usize) -> Vec<T> {
108    alloc_aligned_custom::<T>(
109        (size * size_of::<T>()).next_multiple_of(DEFAULTALIGN) / size_of::<T>(),
110        DEFAULTALIGN,
111    )
112}