poulpy/base2k/src/lib.rs
base2k/src/lib.rsRaw
1pub mod encoding;
2#[allow(
3 non_camel_case_types,
4 non_snake_case,
5 non_upper_case_globals,
6 dead_code,
7 improper_ctypes
8)]
9// Other modules and exports
10pub mod ffi;
11pub mod free;
12pub mod infos;
13pub mod module;
14pub mod sampling;
15pub mod stats;
16pub mod svp;
17pub mod vec_znx;
18pub mod vec_znx_big;
19pub mod vec_znx_dft;
20pub mod vmp;
21
22pub use encoding::*;
23pub use free::*;
24pub use infos::*;
25pub use module::*;
26pub use sampling::*;
27#[allow(unused_imports)]
28pub use stats::*;
29pub use svp::*;
30pub use vec_znx::*;
31pub use vec_znx_big::*;
32pub use vec_znx_dft::*;
33pub use vmp::*;
34
35pub const GALOISGENERATOR: u64 = 5;
36pub const DEFAULTALIGN: usize = 64;
37
38fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool {
39 (ptr as usize) % align == 0
40}
41
42fn is_aligned<T>(ptr: *const T) -> bool {
43 is_aligned_custom(ptr, DEFAULTALIGN)
44}
45
46pub fn cast<T, V>(data: &[T]) -> &[V] {
47 let ptr: *const V = data.as_ptr() as *const V;
48 let len: usize = data.len() / std::mem::size_of::<V>();
49 unsafe { std::slice::from_raw_parts(ptr, len) }
50}
51
52pub fn cast_mut<T, V>(data: &[T]) -> &mut [V] {
53 let ptr: *mut V = data.as_ptr() as *mut V;
54 let len: usize = data.len() / std::mem::size_of::<V>();
55 unsafe { std::slice::from_raw_parts_mut(ptr, len) }
56}
57
58use std::alloc::{alloc, Layout};
59use std::ptr;
60
61/// Allocates a block of bytes with a custom alignement.
62/// Alignement must be a power of two and size a multiple of the alignement.
63/// Allocated memory is initialized to zero.
64pub fn alloc_aligned_custom_u8(size: usize, align: usize) -> Vec<u8> {
65 assert!(
66 align.is_power_of_two(),
67 "Alignment must be a power of two but is {}",
68 align
69 );
70 assert_eq!(
71 (size * std::mem::size_of::<u8>()) % align,
72 0,
73 "size={} must be a multiple of align={}",
74 size,
75 align
76 );
77 unsafe {
78 let layout: Layout = Layout::from_size_align(size, align).expect("Invalid alignment");
79 let ptr: *mut u8 = alloc(layout);
80 if ptr.is_null() {
81 panic!("Memory allocation failed");
82 }
83 assert!(
84 is_aligned_custom(ptr, align),
85 "Memory allocation at {:p} is not aligned to {} bytes",
86 ptr,
87 align
88 );
89 // Init allocated memory to zero
90 ptr::write_bytes(ptr, 0, size);
91 Vec::from_raw_parts(ptr, size, size)
92 }
93}
94
95/// Allocates a block of bytes aligned with [DEFAULTALIGN].
96/// Size must be amultiple of [DEFAULTALIGN].
97/// /// Allocated memory is initialized to zero.
98pub fn alloc_aligned_u8(size: usize) -> Vec<u8> {
99 alloc_aligned_custom_u8(size, DEFAULTALIGN)
100}
101
102/// Allocates a block of T aligned with [DEFAULTALIGN].
103/// Size of T * size msut be a multiple of [DEFAULTALIGN].
104pub fn alloc_aligned_custom<T>(size: usize, align: usize) -> Vec<T> {
105 assert_eq!(
106 (size * std::mem::size_of::<T>()) % align,
107 0,
108 "size={} must be a multiple of align={}",
109 size,
110 align
111 );
112 let mut vec_u8: Vec<u8> = alloc_aligned_custom_u8(std::mem::size_of::<T>() * size, align);
113 let ptr: *mut T = vec_u8.as_mut_ptr() as *mut T;
114 let len: usize = vec_u8.len() / std::mem::size_of::<T>();
115 let cap: usize = vec_u8.capacity() / std::mem::size_of::<T>();
116 std::mem::forget(vec_u8);
117 unsafe { Vec::from_raw_parts(ptr, len, cap) }
118}
119
120pub fn alloc_aligned<T>(size: usize) -> Vec<T> {
121 alloc_aligned_custom::<T>(size, DEFAULTALIGN)
122}
123
124fn alias_mut_slice_to_vec<T>(slice: &[T]) -> Vec<T> {
125 unsafe {
126 let ptr: *mut T = slice.as_ptr() as *mut T;
127 let len: usize = slice.len();
128 Vec::from_raw_parts(ptr, len, len)
129 }
130}