arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/src/lib.rs
src/lib.rsRaw
  1#![feature(bigint_helper_methods)]
  2#![feature(test)]
  3
  4pub mod modulus;
  5pub mod dft;
  6pub mod ring;
  7pub mod poly;
  8
  9pub mod macros{
 10    
 11    #[macro_export]
 12    macro_rules! apply_unary {
 13
 14        ($self:expr, $f:expr, $a:expr, $CHUNK:expr) => {
 15
 16            match CHUNK{
 17                8 => {
 18                    
 19                    $a.chunks_exact_mut(8).for_each(|a| {
 20                        $f(&$self, &mut a[0]);
 21                        $f(&$self, &mut a[1]);
 22                        $f(&$self, &mut a[2]);
 23                        $f(&$self, &mut a[3]);
 24                        $f(&$self, &mut a[4]);
 25                        $f(&$self, &mut a[5]);
 26                        $f(&$self, &mut a[6]);
 27                        $f(&$self, &mut a[7]);
 28                    });
 29
 30                    let n: usize = $a.len();
 31                    let m = n - (n&(CHUNK-1));
 32                    $a[m..].iter_mut().for_each(|a| {
 33                        $f(&$self, a);
 34                    });
 35                },
 36                _=>{
 37                    $a.iter_mut().for_each(|a| {
 38                        $f(&$self, a);
 39                    });
 40                }
 41            }
 42        };
 43    }
 44
 45    #[macro_export]
 46    macro_rules! apply_binary {
 47
 48        ($self:expr, $f:expr, $a:expr, $b:expr, $CHUNK:expr) => {
 49
 50            let n: usize = $a.len();
 51            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
 52            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
 53
 54            match CHUNK{
 55                8 => {
 56                    
 57                    izip!($a.chunks_exact(8), $b.chunks_exact_mut(8)).for_each(|(a, b)| {
 58                        $f(&$self, &a[0], &mut b[0]);
 59                        $f(&$self, &a[1], &mut b[1]);
 60                        $f(&$self, &a[2], &mut b[2]);
 61                        $f(&$self, &a[3], &mut b[3]);
 62                        $f(&$self, &a[4], &mut b[4]);
 63                        $f(&$self, &a[5], &mut b[5]);
 64                        $f(&$self, &a[6], &mut b[6]);
 65                        $f(&$self, &a[7], &mut b[7]);
 66                    });
 67
 68                    let m = n - (n&(CHUNK-1));
 69                    izip!($a[m..].iter(), $b[m..].iter_mut()).for_each(|(a, b)| {
 70                        $f(&$self, a, b);
 71                    });
 72                },
 73                _=>{
 74                    izip!($a.iter(), $b.iter_mut()).for_each(|(a, b)| {
 75                        $f(&$self, a, b);
 76                    });
 77                }
 78            }
 79        };
 80    }
 81
 82    #[macro_export]
 83    macro_rules! apply_ternary {
 84
 85        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $CHUNK:expr) => {
 86
 87            let n: usize = $a.len();
 88            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
 89            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
 90
 91            match CHUNK{
 92                8 => {
 93                    
 94                    izip!($a.chunks_exact(8), $b.chunks_exact(8), $c.chunks_exact_mut(8)).for_each(|(a, b, c)| {
 95                        $f(&$self, &a[0], &b[0], &mut c[0]);
 96                        $f(&$self, &a[1], &b[1], &mut c[1]);
 97                        $f(&$self, &a[2], &b[2], &mut c[2]);
 98                        $f(&$self, &a[3], &b[3], &mut c[3]);
 99                        $f(&$self, &a[4], &b[4], &mut c[4]);
100                        $f(&$self, &a[5], &b[5], &mut c[5]);
101                        $f(&$self, &a[6], &b[6], &mut c[6]);
102                        $f(&$self, &a[7], &b[7], &mut c[7]);
103                    });
104
105                    let m = n - (n&7);
106                    izip!($a[m..].iter(), $b[m..].iter(), $c[m..].iter_mut()).for_each(|(a, b, c)| {
107                        $f(&$self, a, b, c);
108                    });
109                },
110                _=>{
111                    izip!($a.iter(), $b.iter(), $c.iter_mut()).for_each(|(a, b, c)| {
112                        $f(&$self, a, b, c);
113                    });
114                }
115            }
116        };
117    }
118}