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_v {
 15
 16        ($self:expr, $f:expr, $a:expr, $CHUNK:expr) => {
 17
 18            match CHUNK{
 19                8 => {
 20                    $a.chunks_exact_mut(8).for_each(|a| {
 21                        $f(&$self, &mut a[0]);
 22                        $f(&$self, &mut a[1]);
 23                        $f(&$self, &mut a[2]);
 24                        $f(&$self, &mut a[3]);
 25                        $f(&$self, &mut a[4]);
 26                        $f(&$self, &mut a[5]);
 27                        $f(&$self, &mut a[6]);
 28                        $f(&$self, &mut a[7]);
 29                    });
 30
 31                    let n: usize = $a.len();
 32                    let m = n - (n&(CHUNK-1));
 33                    $a[m..].iter_mut().for_each(|a| {
 34                        $f(&$self, a);
 35                    });
 36                },
 37                _=>{
 38                    $a.iter_mut().for_each(|a| {
 39                        $f(&$self, a);
 40                    });
 41                }
 42            }
 43        };
 44    }
 45
 46    #[macro_export]
 47    macro_rules! apply_vv {
 48
 49        ($self:expr, $f:expr, $a:expr, $b:expr, $CHUNK:expr) => {
 50
 51            let n: usize = $a.len();
 52            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
 53            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
 54
 55            match CHUNK{
 56                8 => {
 57                    
 58                    izip!($a.chunks_exact(8), $b.chunks_exact_mut(8)).for_each(|(a, b)| {
 59                        $f(&$self, &a[0], &mut b[0]);
 60                        $f(&$self, &a[1], &mut b[1]);
 61                        $f(&$self, &a[2], &mut b[2]);
 62                        $f(&$self, &a[3], &mut b[3]);
 63                        $f(&$self, &a[4], &mut b[4]);
 64                        $f(&$self, &a[5], &mut b[5]);
 65                        $f(&$self, &a[6], &mut b[6]);
 66                        $f(&$self, &a[7], &mut b[7]);
 67                    });
 68
 69                    let m = n - (n&(CHUNK-1));
 70                    izip!($a[m..].iter(), $b[m..].iter_mut()).for_each(|(a, b)| {
 71                        $f(&$self, a, b);
 72                    });
 73                },
 74                _=>{
 75                    izip!($a.iter(), $b.iter_mut()).for_each(|(a, b)| {
 76                        $f(&$self, a, b);
 77                    });
 78                }
 79            }
 80        };
 81    }
 82
 83    #[macro_export]
 84    macro_rules! apply_vvv {
 85
 86        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $CHUNK:expr) => {
 87
 88            let n: usize = $a.len();
 89            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
 90            debug_assert!($c.len() == n, "invalid argument c: b.len() = {} != a.len() = {}", $c.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
121    #[macro_export]
122    macro_rules! apply_sv {
123
124        ($self:expr, $f:expr, $a:expr, $b:expr, $CHUNK:expr) => {
125
126            let n: usize = $b.len();
127
128            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
129
130            match CHUNK{
131                8 => {
132                    
133                    izip!($b.chunks_exact_mut(8)).for_each(|b| {
134                        $f(&$self, $a, &mut b[0]);
135                        $f(&$self, $a, &mut b[1]);
136                        $f(&$self, $a, &mut b[2]);
137                        $f(&$self, $a, &mut b[3]);
138                        $f(&$self, $a, &mut b[4]);
139                        $f(&$self, $a, &mut b[5]);
140                        $f(&$self, $a, &mut b[6]);
141                        $f(&$self, $a, &mut b[7]);
142                    });
143
144                    let m = n - (n&7);
145                    izip!($b[m..].iter_mut()).for_each(|b| {
146                        $f(&$self, $a, b);
147                    });
148                },
149                _=>{
150                    izip!($b.iter_mut()).for_each(|b| {
151                        $f(&$self, $a, b);
152                    });
153                }
154            }
155        };
156    }
157
158    #[macro_export]
159    macro_rules! apply_svv {
160
161        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $CHUNK:expr) => {
162
163            let n: usize = $b.len();
164            debug_assert!($c.len() == n, "invalid argument c: c.len() = {} != b.len() = {}", $c.len(), n);
165            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
166
167            match CHUNK{
168                8 => {
169                    
170                    izip!($b.chunks_exact(8), $c.chunks_exact_mut(8)).for_each(|(b, c)| {
171                        $f(&$self, $a, &b[0], &mut c[0]);
172                        $f(&$self, $a, &b[1], &mut c[1]);
173                        $f(&$self, $a, &b[2], &mut c[2]);
174                        $f(&$self, $a, &b[3], &mut c[3]);
175                        $f(&$self, $a, &b[4], &mut c[4]);
176                        $f(&$self, $a, &b[5], &mut c[5]);
177                        $f(&$self, $a, &b[6], &mut c[6]);
178                        $f(&$self, $a, &b[7], &mut c[7]);
179                    });
180
181                    let m = n - (n&7);
182                    izip!($b[m..].iter(), $c[m..].iter_mut()).for_each(|(b, c)| {
183                        $f(&$self, $a, b, c);
184                    });
185                },
186                _=>{
187                    izip!($b.iter(), $c.iter_mut()).for_each(|(b, c)| {
188                        $f(&$self, $a, b, c);
189                    });
190                }
191            }
192        };
193    }
194
195    #[macro_export]
196    macro_rules! apply_vvsv {
197
198        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $d:expr, $CHUNK:expr) => {
199
200            let n: usize = $a.len();
201            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
202            debug_assert!($d.len() == n, "invalid argument d: d.len() = {} != a.len() = {}", $d.len(), n);
203            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
204
205            match CHUNK{
206                8 => {
207                    
208                    izip!($a.chunks_exact(8), $b.chunks_exact(8), $d.chunks_exact_mut(8)).for_each(|(a, b, d)| {
209                        $f(&$self, &a[0], &b[0], $c, &mut d[0]);
210                        $f(&$self, &a[1], &b[1], $c, &mut d[1]);
211                        $f(&$self, &a[2], &b[2], $c, &mut d[2]);
212                        $f(&$self, &a[3], &b[3], $c, &mut d[3]);
213                        $f(&$self, &a[4], &b[4], $c, &mut d[4]);
214                        $f(&$self, &a[5], &b[5], $c, &mut d[5]);
215                        $f(&$self, &a[6], &b[6], $c, &mut d[6]);
216                        $f(&$self, &a[7], &b[7], $c, &mut d[7]);
217                    });
218
219                    let m = n - (n&7);
220                    izip!($a[m..].iter(), $b[m..].iter(), $d[m..].iter_mut()).for_each(|(a, b, d)| {
221                        $f(&$self, a, b, $c, d);
222                    });
223                },
224                _=>{
225                    izip!($a.iter(), $b.iter(), $d.iter_mut()).for_each(|(a, b, d)| {
226                        $f(&$self, a, b, $c, d);
227                    });
228                }
229            }
230        };
231    }
232
233    #[macro_export]
234    macro_rules! apply_vsv {
235
236        ($self:expr, $f:expr, $a:expr, $c:expr, $b:expr, $CHUNK:expr) => {
237
238            let n: usize = $a.len();
239            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
240            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
241
242            match CHUNK{
243                8 => {
244                    
245                    izip!($a.chunks_exact(8), $b.chunks_exact_mut(8)).for_each(|(a, b)| {
246                        $f(&$self, &a[0], $c, &mut b[0]);
247                        $f(&$self, &a[1], $c, &mut b[1]);
248                        $f(&$self, &a[2], $c, &mut b[2]);
249                        $f(&$self, &a[3], $c, &mut b[3]);
250                        $f(&$self, &a[4], $c, &mut b[4]);
251                        $f(&$self, &a[5], $c, &mut b[5]);
252                        $f(&$self, &a[6], $c, &mut b[6]);
253                        $f(&$self, &a[7], $c, &mut b[7]);
254                    });
255
256                    let m = n - (n&7);
257                    izip!($a[m..].iter(), $b[m..].iter_mut()).for_each(|(a, b)| {
258                        $f(&$self, a, $c, b);
259                    });
260                },
261                _=>{
262                    izip!($a.iter(), $b.iter_mut()).for_each(|(a, b)| {
263                        $f(&$self, a, $c, b);
264                    });
265                }
266            }
267        };
268    }
269}