1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::{
    fmt,
    ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub},
};

#[derive(Clone, Copy, Eq, Hash, PartialEq, PartialOrd)]
pub struct BitVector(pub u64);

impl BitVector {
    pub fn ones() -> BitVector {
        BitVector(u64::max_value())
    }

    pub fn ctz(&self) -> u32 {
        self.0.trailing_zeros()
    }

    pub fn odd(&self) -> bool {
        self.0 % 2 == 1
    }

    pub fn lsb(&self) -> u64 {
        self.0 & 1
    }

    pub fn modinverse(&self) -> Option<BitVector> {
        match modinverse::modinverse((self.0 as u128) as i128, 2_i128.pow(64)) {
            Some(res) => Some(BitVector(res as u64)),
            None => None,
        }
    }

    pub fn addo(&self, t: BitVector) -> bool {
        self.0.overflowing_add(t.0).1
    }

    pub fn mulo(&self, t: BitVector) -> bool {
        self.0.overflowing_mul(t.0).1
    }
}

impl Neg for BitVector {
    type Output = BitVector;

    fn neg(self) -> Self::Output {
        Self(-(self.0 as i64) as u64)
    }
}

impl Add<BitVector> for BitVector {
    type Output = BitVector;

    fn add(self, other: BitVector) -> Self::Output {
        Self(self.0.wrapping_add(other.0))
    }
}

impl Sub<BitVector> for BitVector {
    type Output = BitVector;

    fn sub(self, other: BitVector) -> Self::Output {
        Self(self.0.wrapping_sub(other.0))
    }
}

impl Mul<BitVector> for BitVector {
    type Output = BitVector;

    fn mul(self, other: BitVector) -> Self::Output {
        Self(self.0.wrapping_mul(other.0))
    }
}

impl Div<BitVector> for BitVector {
    type Output = BitVector;

    fn div(self, other: BitVector) -> Self::Output {
        if other == BitVector(0) {
            Self::ones()
        } else {
            Self(self.0.wrapping_div(other.0))
        }
    }
}

impl Rem<BitVector> for BitVector {
    type Output = BitVector;

    fn rem(self, other: BitVector) -> Self::Output {
        if other == BitVector(0) {
            self
        } else {
            Self(self.0.wrapping_rem(other.0))
        }
    }
}

impl BitOr<BitVector> for BitVector {
    type Output = BitVector;

    fn bitor(self, other: BitVector) -> Self::Output {
        Self(self.0 | other.0)
    }
}

impl BitAnd<BitVector> for BitVector {
    type Output = BitVector;

    fn bitand(self, other: BitVector) -> Self::Output {
        Self(self.0 & other.0)
    }
}

impl BitXor<BitVector> for BitVector {
    type Output = BitVector;

    fn bitxor(self, other: BitVector) -> Self::Output {
        Self(self.0 ^ other.0)
    }
}

impl Shl<u32> for BitVector {
    type Output = BitVector;

    fn shl(self, other: u32) -> Self::Output {
        Self(self.0.wrapping_shl(other))
    }
}

impl Shr<u32> for BitVector {
    type Output = BitVector;

    fn shr(self, other: u32) -> Self::Output {
        Self(self.0.wrapping_shr(other))
    }
}

impl Not for BitVector {
    type Output = BitVector;

    fn not(self) -> Self::Output {
        BitVector(!self.0)
    }
}

impl fmt::Debug for BitVector {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fn bit_to_char(x: &BitVector, b: u32) -> char {
            if *x & (BitVector(1) << b) > BitVector(0) {
                '1'
            } else {
                '0'
            }
        }

        let bit_vector = (0..64)
            .rev()
            .map(|b| bit_to_char(self, b))
            .skip_while(|c| *c == '0')
            .collect::<String>();

        write!(f, "<{}>", bit_vector)
    }
}