arnaucube
arnaucube/poulpyPublic Self-hosted repository
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
 38pub fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool {
 39    (ptr as usize) % align == 0
 40}
 41
 42pub fn is_aligned<T>(ptr: *const T) -> bool {
 43    is_aligned_custom(ptr, DEFAULTALIGN)
 44}
 45
 46pub fn assert_alignement<T>(ptr: *const T) {
 47    assert!(is_aligned(ptr), "invalid alignement: ensure passed bytes have been allocated with [alloc_aligned_u8] or [alloc_aligned]")
 48}
 49
 50pub fn cast<T, V>(data: &[T]) -> &[V] {
 51    let ptr: *const V = data.as_ptr() as *const V;
 52    let len: usize = data.len() / std::mem::size_of::<V>();
 53    unsafe { std::slice::from_raw_parts(ptr, len) }
 54}
 55
 56pub fn cast_mut<T, V>(data: &[T]) -> &mut [V] {
 57    let ptr: *mut V = data.as_ptr() as *mut V;
 58    let len: usize = data.len() / std::mem::size_of::<V>();
 59    unsafe { std::slice::from_raw_parts_mut(ptr, len) }
 60}
 61
 62use std::alloc::{alloc, Layout};
 63use std::ptr;
 64
 65/// Allocates a block of bytes with a custom alignement.
 66/// Alignement must be a power of two and size a multiple of the alignement.
 67/// Allocated memory is initialized to zero.
 68pub fn alloc_aligned_custom_u8(size: usize, align: usize) -> Vec<u8> {
 69    assert!(
 70        align.is_power_of_two(),
 71        "Alignment must be a power of two but is {}",
 72        align
 73    );
 74    assert_eq!(
 75        (size * std::mem::size_of::<u8>()) % align,
 76        0,
 77        "size={} must be a multiple of align={}",
 78        size,
 79        align
 80    );
 81    unsafe {
 82        let layout: Layout = Layout::from_size_align(size, align).expect("Invalid alignment");
 83        let ptr: *mut u8 = alloc(layout);
 84        if ptr.is_null() {
 85            panic!("Memory allocation failed");
 86        }
 87        assert!(
 88            is_aligned_custom(ptr, align),
 89            "Memory allocation at {:p} is not aligned to {} bytes",
 90            ptr,
 91            align
 92        );
 93        // Init allocated memory to zero
 94        ptr::write_bytes(ptr, 0, size);
 95        Vec::from_raw_parts(ptr, size, size)
 96    }
 97}
 98
 99/// Allocates a block of bytes aligned with [DEFAULTALIGN].
100/// Size must be amultiple of [DEFAULTALIGN].
101/// /// Allocated memory is initialized to zero.
102pub fn alloc_aligned_u8(size: usize) -> Vec<u8> {
103    alloc_aligned_custom_u8(size, DEFAULTALIGN)
104}
105
106/// Allocates a block of T aligned with [DEFAULTALIGN].
107/// Size of T * size msut be a multiple of [DEFAULTALIGN].
108pub fn alloc_aligned_custom<T>(size: usize, align: usize) -> Vec<T> {
109    assert_eq!(
110        (size * std::mem::size_of::<T>()) % align,
111        0,
112        "size={} must be a multiple of align={}",
113        size,
114        align
115    );
116    let mut vec_u8: Vec<u8> = alloc_aligned_custom_u8(std::mem::size_of::<T>() * size, align);
117    let ptr: *mut T = vec_u8.as_mut_ptr() as *mut T;
118    let len: usize = vec_u8.len() / std::mem::size_of::<T>();
119    let cap: usize = vec_u8.capacity() / std::mem::size_of::<T>();
120    std::mem::forget(vec_u8);
121    unsafe { Vec::from_raw_parts(ptr, len, cap) }
122}
123
124pub fn alloc_aligned<T>(size: usize) -> Vec<T> {
125    alloc_aligned_custom::<T>(size, DEFAULTALIGN)
126}
127
128fn alias_mut_slice_to_vec<T>(slice: &[T]) -> Vec<T> {
129    unsafe {
130        let ptr: *mut T = slice.as_ptr() as *mut T;
131        let len: usize = slice.len();
132        Vec::from_raw_parts(ptr, len, len)
133    }
134}