arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/backend/src/hal/api/scratch.rs
backend/src/hal/api/scratch.rsRaw
  1use crate::hal::layouts::{Backend, MatZnx, Module, ScalarZnx, Scratch, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat};
  2
  3/// Allocates a new [crate::hal::layouts::ScratchOwned] of `size` aligned bytes.
  4pub trait ScratchOwnedAlloc<B: Backend> {
  5    fn alloc(size: usize) -> Self;
  6}
  7
  8/// Borrows a slice of bytes into a [Scratch].
  9pub trait ScratchOwnedBorrow<B: Backend> {
 10    fn borrow(&mut self) -> &mut Scratch<B>;
 11}
 12
 13/// Wrap an array of mutable borrowed bytes into a [Scratch].
 14pub trait ScratchFromBytes<B: Backend> {
 15    fn from_bytes(data: &mut [u8]) -> &mut Scratch<B>;
 16}
 17
 18/// Returns how many bytes left can be taken from the scratch.
 19pub trait ScratchAvailable {
 20    fn available(&self) -> usize;
 21}
 22
 23/// Takes a slice of bytes from a [Scratch] and return a new [Scratch] minus the taken array of bytes.
 24pub trait TakeSlice {
 25    fn take_slice<T>(&mut self, len: usize) -> (&mut [T], &mut Self);
 26}
 27
 28/// Take a slice of bytes from a [Scratch], wraps it into a [ScalarZnx] and returns it
 29/// as well as a new [Scratch] minus the taken array of bytes.
 30pub trait TakeScalarZnx<B: Backend> {
 31    fn take_scalar_znx(&mut self, module: &Module<B>, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self);
 32}
 33
 34/// Take a slice of bytes from a [Scratch], wraps it into a [SvpPPol] and returns it
 35/// as well as a new [Scratch] minus the taken array of bytes.
 36pub trait TakeSvpPPol<B: Backend> {
 37    fn take_svp_ppol(&mut self, module: &Module<B>, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self);
 38}
 39
 40/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnx] and returns it
 41/// as well as a new [Scratch] minus the taken array of bytes.
 42pub trait TakeVecZnx<B: Backend> {
 43    fn take_vec_znx(&mut self, module: &Module<B>, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self);
 44}
 45
 46/// Take a slice of bytes from a [Scratch], slices it into a vector of [VecZnx] aand returns it
 47/// as well as a new [Scratch] minus the taken array of bytes.
 48pub trait TakeVecZnxSlice<B: Backend> {
 49    fn take_vec_znx_slice(
 50        &mut self,
 51        len: usize,
 52        module: &Module<B>,
 53        cols: usize,
 54        size: usize,
 55    ) -> (Vec<VecZnx<&mut [u8]>>, &mut Self);
 56}
 57
 58/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnxBig] and returns it
 59/// as well as a new [Scratch] minus the taken array of bytes.
 60pub trait TakeVecZnxBig<B: Backend> {
 61    fn take_vec_znx_big(&mut self, module: &Module<B>, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self);
 62}
 63
 64/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnxDft] and returns it
 65/// as well as a new [Scratch] minus the taken array of bytes.
 66pub trait TakeVecZnxDft<B: Backend> {
 67    fn take_vec_znx_dft(&mut self, module: &Module<B>, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self);
 68}
 69
 70/// Take a slice of bytes from a [Scratch], slices it into a vector of [VecZnxDft] and returns it
 71/// as well as a new [Scratch] minus the taken array of bytes.
 72pub trait TakeVecZnxDftSlice<B: Backend> {
 73    fn take_vec_znx_dft_slice(
 74        &mut self,
 75        len: usize,
 76        module: &Module<B>,
 77        cols: usize,
 78        size: usize,
 79    ) -> (Vec<VecZnxDft<&mut [u8], B>>, &mut Self);
 80}
 81
 82/// Take a slice of bytes from a [Scratch], wraps it into a [VmpPMat] and returns it
 83/// as well as a new [Scratch] minus the taken array of bytes.
 84pub trait TakeVmpPMat<B: Backend> {
 85    fn take_vmp_pmat(
 86        &mut self,
 87        module: &Module<B>,
 88        rows: usize,
 89        cols_in: usize,
 90        cols_out: usize,
 91        size: usize,
 92    ) -> (VmpPMat<&mut [u8], B>, &mut Self);
 93}
 94
 95/// Take a slice of bytes from a [Scratch], wraps it into a [MatZnx] and returns it
 96/// as well as a new [Scratch] minus the taken array of bytes.
 97pub trait TakeMatZnx<B: Backend> {
 98    fn take_mat_znx(
 99        &mut self,
100        module: &Module<B>,
101        rows: usize,
102        cols_in: usize,
103        cols_out: usize,
104        size: usize,
105    ) -> (MatZnx<&mut [u8]>, &mut Self);
106}
107
108/// Take a slice of bytes from a [Scratch], wraps it into the template's type and returns it
109/// as well as a new [Scratch] minus the taken array of bytes.
110pub trait TakeLike<'a, B: Backend, T> {
111    type Output;
112    fn take_like(&'a mut self, template: &T) -> (Self::Output, &'a mut Self);
113}