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