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