fixed_bigint/fixeduint/
bit_ops_impl.rs

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use super::{FixedUInt, MachineWord};

use crate::patch_num_traits::{OverflowingShl, OverflowingShr};

use num_traits::Zero;

impl<T: MachineWord, const N: usize> core::ops::Not for FixedUInt<T, N> {
    type Output = Self;
    fn not(self) -> <Self as core::ops::Not>::Output {
        let mut ret = Self::zero();
        for i in 0..N {
            ret.array[i] = !self.array[i]
        }
        ret
    }
}

impl<T: MachineWord, const N: usize> core::ops::BitAnd for FixedUInt<T, N> {
    type Output = Self;
    fn bitand(self, other: Self) -> <Self as core::ops::BitAnd<Self>>::Output {
        let mut ret = Self::zero();
        for i in 0..N {
            ret.array[i] = self.array[i] & other.array[i]
        }
        ret
    }
}

impl<T: MachineWord, const N: usize> core::ops::BitAndAssign for FixedUInt<T, N> {
    fn bitand_assign(&mut self, other: Self) {
        for i in 0..N {
            self.array[i] &= other.array[i]
        }
    }
}

impl<T: MachineWord, const N: usize> core::ops::BitOr for FixedUInt<T, N> {
    type Output = Self;
    fn bitor(self, other: Self) -> <Self as core::ops::BitOr<Self>>::Output {
        let mut ret = Self::zero();
        for i in 0..N {
            ret.array[i] = self.array[i] | other.array[i]
        }
        ret
    }
}

impl<T: MachineWord, const N: usize> core::ops::BitOrAssign for FixedUInt<T, N> {
    fn bitor_assign(&mut self, other: Self) {
        for i in 0..N {
            self.array[i] |= other.array[i]
        }
    }
}

impl<T: MachineWord, const N: usize> core::ops::BitXor for FixedUInt<T, N> {
    type Output = Self;
    fn bitxor(self, other: Self) -> <Self as core::ops::BitXor<Self>>::Output {
        let mut ret = Self::zero();
        for i in 0..N {
            ret.array[i] = self.array[i] ^ other.array[i]
        }
        ret
    }
}

impl<T: MachineWord, const N: usize> core::ops::BitXorAssign for FixedUInt<T, N> {
    fn bitxor_assign(&mut self, other: Self) {
        for i in 0..N {
            self.array[i] ^= other.array[i]
        }
    }
}

impl<T: MachineWord, const N: usize> OverflowingShl for FixedUInt<T, N> {
    fn overflowing_shl(self, bits: u32) -> (Self, bool) {
        let bitsu = bits as usize;
        let (shift, overflow) = if bitsu >= Self::BIT_SIZE {
            (bitsu & (Self::BIT_SIZE - 1), true)
        } else {
            (bitsu, false)
        };
        let res = core::ops::Shl::<usize>::shl(self, shift);
        (res, overflow)
    }
}

impl<T: MachineWord, const N: usize> core::ops::Shl<u32> for FixedUInt<T, N> {
    type Output = Self;
    fn shl(self, bits: u32) -> <Self as core::ops::Shl<u32>>::Output {
        core::ops::Shl::<usize>::shl(self, bits as usize)
    }
}

impl<T: MachineWord, const N: usize> core::ops::Shl<usize> for FixedUInt<T, N> {
    type Output = Self;
    fn shl(self, bits: usize) -> <Self as core::ops::Shl<usize>>::Output {
        // This copy can be avoided
        let mut result = self;
        Self::shl_impl(&mut result, bits);
        result
    }
}

impl<T: MachineWord, const N: usize> core::ops::Shl<&'_ usize> for FixedUInt<T, N> {
    type Output = Self;
    fn shl(self, bits: &usize) -> <Self as core::ops::Shl<usize>>::Output {
        let mut result = self;
        Self::shl_impl(&mut result, *bits);
        result
    }
}

impl<T: MachineWord, const N: usize> num_traits::WrappingShl for FixedUInt<T, N> {
    fn wrapping_shl(&self, bits: u32) -> Self {
        self.overflowing_shl(bits).0
    }
}

impl<T: MachineWord, const N: usize> num_traits::CheckedShl for FixedUInt<T, N> {
    fn checked_shl(&self, bits: u32) -> Option<Self> {
        let res = self.overflowing_shl(bits);
        if res.1 {
            None
        } else {
            Some(res.0)
        }
    }
}

// SaturatingShl doesn't exist

impl<T: MachineWord, const N: usize> core::ops::ShlAssign<usize> for FixedUInt<T, N> {
    fn shl_assign(&mut self, bits: usize) {
        Self::shl_impl(self, bits);
    }
}

impl<T: MachineWord, const N: usize> core::ops::ShlAssign<&'_ usize> for FixedUInt<T, N> {
    fn shl_assign(&mut self, bits: &usize) {
        Self::shl_impl(self, *bits);
    }
}

impl<T: MachineWord, const N: usize> OverflowingShr for FixedUInt<T, N> {
    fn overflowing_shr(self, bits: u32) -> (Self, bool) {
        let bitsu = bits as usize;
        let (shift, overflow) = if bitsu >= Self::BIT_SIZE {
            (bitsu & (Self::BIT_SIZE - 1), true)
        } else {
            (bitsu, false)
        };
        let res = core::ops::Shr::<usize>::shr(self, shift);
        (res, overflow)
    }
}

impl<T: MachineWord, const N: usize> core::ops::Shr<u32> for FixedUInt<T, N> {
    type Output = Self;
    fn shr(self, bits: u32) -> <Self as core::ops::Shr<u32>>::Output {
        core::ops::Shr::<usize>::shr(self, bits as usize)
    }
}

impl<T: MachineWord, const N: usize> core::ops::Shr<usize> for FixedUInt<T, N> {
    type Output = Self;
    fn shr(self, bits: usize) -> <Self as core::ops::Shr<usize>>::Output {
        // Technically, this copy can be avoided
        let mut result = self;
        Self::shr_impl(&mut result, bits);
        result
    }
}

impl<T: MachineWord, const N: usize> core::ops::Shr<&'_ usize> for FixedUInt<T, N> {
    type Output = Self;
    fn shr(self, bits: &usize) -> <Self as core::ops::Shr<usize>>::Output {
        let mut result = self;
        Self::shr_impl(&mut result, *bits);
        result
    }
}

impl<T: MachineWord, const N: usize> num_traits::WrappingShr for FixedUInt<T, N> {
    fn wrapping_shr(&self, bits: u32) -> Self {
        self.overflowing_shr(bits).0
    }
}

impl<T: MachineWord, const N: usize> num_traits::CheckedShr for FixedUInt<T, N> {
    fn checked_shr(&self, bits: u32) -> Option<Self> {
        let res = self.overflowing_shr(bits);
        if res.1 {
            None
        } else {
            Some(res.0)
        }
    }
}

// SaturatingShr doesn't exist

impl<T: MachineWord, const N: usize> core::ops::ShrAssign<usize> for FixedUInt<T, N> {
    fn shr_assign(&mut self, bits: usize) {
        Self::shr_impl(self, bits);
    }
}

impl<T: MachineWord, const N: usize> core::ops::ShrAssign<&'_ usize> for FixedUInt<T, N> {
    fn shr_assign(&mut self, bits: &usize) {
        Self::shr_impl(self, *bits);
    }
}