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