arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-hal/src/reference/znx/mul.rs
poulpy-hal/src/reference/znx/mul.rsRaw
 1use crate::reference::znx::{znx_add_inplace_ref, znx_copy_ref};
 2
 3pub fn znx_mul_power_of_two_ref(mut k: i64, res: &mut [i64], a: &[i64]) {
 4    #[cfg(debug_assertions)]
 5    {
 6        assert_eq!(res.len(), a.len());
 7    }
 8
 9    if k == 0 {
10        znx_copy_ref(res, a);
11        return;
12    }
13
14    if k > 0 {
15        for (y, x) in res.iter_mut().zip(a.iter()) {
16            *y = *x << k
17        }
18        return;
19    }
20
21    k = -k;
22
23    for (y, x) in res.iter_mut().zip(a.iter()) {
24        let sign_bit: i64 = (x >> 63) & 1;
25        let bias: i64 = (1_i64 << (k - 1)) - sign_bit;
26        *y = (x + bias) >> k;
27    }
28}
29
30pub fn znx_mul_power_of_two_inplace_ref(mut k: i64, res: &mut [i64]) {
31    if k == 0 {
32        return;
33    }
34
35    if k > 0 {
36        for x in res.iter_mut() {
37            *x <<= k
38        }
39        return;
40    }
41
42    k = -k;
43
44    for x in res.iter_mut() {
45        let sign_bit: i64 = (*x >> 63) & 1;
46        let bias: i64 = (1_i64 << (k - 1)) - sign_bit;
47        *x = (*x + bias) >> k;
48    }
49}
50
51pub fn znx_mul_add_power_of_two_ref(mut k: i64, res: &mut [i64], a: &[i64]) {
52    #[cfg(debug_assertions)]
53    {
54        assert_eq!(res.len(), a.len());
55    }
56
57    if k == 0 {
58        znx_add_inplace_ref(res, a);
59        return;
60    }
61
62    if k > 0 {
63        for (y, x) in res.iter_mut().zip(a.iter()) {
64            *y += *x << k
65        }
66        return;
67    }
68
69    k = -k;
70
71    for (y, x) in res.iter_mut().zip(a.iter()) {
72        let sign_bit: i64 = (x >> 63) & 1;
73        let bias: i64 = (1_i64 << (k - 1)) - sign_bit;
74        *y += (x + bias) >> k;
75    }
76}