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