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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use core::{ops::Range, ptr};

use awint_internals::*;

use crate::Bits;

/// # Bitwise
impl Bits {
    /// Zero-assigns. Same as the Unsigned-minimum-value. All bits are set to 0.
    pub const fn zero_assign(&mut self) {
        unsafe { self.digit_set(false, 0..self.len(), false) }
    }

    /// Unsigned-maximum-value-assigns. All bits are set to 1.
    pub const fn umax_assign(&mut self) {
        unsafe { self.digit_set(true, 0..self.len(), true) }
    }

    /// Signed-maximum-value-assigns. All bits are set to 1, except for the most
    /// significant bit.
    pub const fn imax_assign(&mut self) {
        unsafe { self.digit_set(true, 0..self.len(), false) }
        *self.last_mut() = (isize::MAX as usize) >> self.unused();
    }

    /// Signed-minimum-value-assigns. Only the most significant bit is set.
    pub const fn imin_assign(&mut self) {
        unsafe { self.digit_set(false, 0..self.len(), false) }
        *self.last_mut() = (isize::MIN as usize) >> self.unused();
    }

    /// Unsigned-one-assigns. Only the least significant bit is set. The
    /// unsigned distinction is important, because a positive one value does
    /// not exist for signed integers with a bitwidth of 1.
    pub const fn uone_assign(&mut self) {
        unsafe { self.digit_set(false, 0..self.len(), false) }
        *self.first_mut() = 1;
    }

    /// Not-assigns `self`
    pub const fn not_assign(&mut self) {
        for_each_mut!(self, x, { *x = !*x }, true);
    }

    /// Copy-assigns the bits of `rhs` to `self`
    pub const fn copy_assign(&mut self, rhs: &Self) -> Option<()> {
        if self.bw() != rhs.bw() {
            return None
        }
        unsafe {
            ptr::copy_nonoverlapping(rhs.as_ptr(), self.as_mut_ptr(), self.len());
        }
        Some(())
    }

    /// Resize-copy-assigns `rhs` to `self`. If `self.bw() >= rhs.bw()`, the
    /// copied value of `rhs` will be extended with bits set to `extension`. If
    /// `self.bw() < rhs.bw()`, the copied value of `rhs` will be truncated.
    pub const fn resize_assign(&mut self, rhs: &Self, extension: bool) {
        // Safety: the exact number of digits needed are copied or set
        unsafe {
            if self.bw() <= rhs.bw() {
                // truncation
                ptr::copy_nonoverlapping(rhs.as_ptr(), self.as_mut_ptr(), self.len());
                self.clear_unused_bits();
            } else {
                ptr::copy_nonoverlapping(rhs.as_ptr(), self.as_mut_ptr(), rhs.len());
                if extension && (rhs.unused() != 0) {
                    *self.get_unchecked_mut(rhs.len() - 1) |= MAX << rhs.extra();
                }
                self.digit_set(extension, rhs.len()..self.len(), extension)
            }
        }
    }

    /// Zero-resize-copy-assigns `rhs` to `self` and returns overflow. This is
    /// the same as `lhs.resize_assign(rhs, false)`, but returns `true` if the
    /// unsigned meaning of the integer is changed.
    pub const fn zero_resize_assign(&mut self, rhs: &Self) -> bool {
        self.resize_assign(rhs, false);
        if self.bw() < rhs.bw() {
            // Safety: `self.len() <= rhs.len()` because of the above check
            unsafe {
                // check if there are set bits that would be truncated
                if (self.extra() != 0) && ((rhs.get_unchecked(self.len() - 1) >> self.extra()) != 0)
                {
                    return true
                }
                const_for!(i in {self.len()..rhs.len()} {
                    if rhs.get_unchecked(i) != 0 {
                        return true
                    }
                });
            }
        }
        false
    }

    /// Sign-resize-copy-assigns `rhs` to `self` and returns overflow. This is
    /// the same as `lhs.resize_assign(rhs, rhs.msb())`, but returns `true` if
    /// the signed meaning of the integer is changed.
    pub const fn sign_resize_assign(&mut self, rhs: &Self) -> bool {
        self.resize_assign(rhs, rhs.msb());
        // this function is far harder to implement than it would first seem
        if self.bw() < rhs.bw() {
            // Safety: `self.len() <= rhs.len()` because of the above check
            unsafe {
                if rhs.msb() {
                    // check if the new most significant bit is unset (which would mean overflow
                    // from negative to positive)
                    if !self.msb() {
                        return true
                    }
                    // check if there are unset bits that would be truncated
                    if self.len() == rhs.len() {
                        // first and only digit
                        if rhs.extra() != 0 {
                            //  rhs extra mask and lhs cutoff mask
                            let expected = (MAX >> (BITS - rhs.extra())) & (MAX << self.extra());
                            if (rhs.last() & expected) != expected {
                                return true
                            }
                        } else {
                            let expected = MAX << self.extra();
                            if (rhs.last() & expected) != expected {
                                return true
                            }
                        }
                        // avoid the other tests if this is the only digit
                        return false
                    }
                    // first digit
                    if self.extra() != 0 {
                        let expected = MAX << self.extra();
                        if (rhs.get_unchecked(self.len() - 1) & expected) != expected {
                            return true
                        }
                    }
                    // middle digits
                    const_for!(i in {self.len()..(rhs.len() - 1)} {
                        if rhs.get_unchecked(i) != MAX {
                            return true
                        }
                    });
                    // last digit
                    if rhs.extra() != 0 {
                        let expected = MAX >> (BITS - rhs.extra());
                        if (rhs.last() & expected) != expected {
                            return true
                        }
                    } else if rhs.last() != MAX {
                        return true
                    }
                } else {
                    // check if the new most significant bit is set (which would mean overflow from
                    // positive to negative)
                    if self.msb() {
                        return true
                    }
                    // check if there are set bits that would be truncated
                    if (self.extra() != 0)
                        && ((rhs.get_unchecked(self.len() - 1) >> self.extra()) != 0)
                    {
                        return true
                    }
                    // Safety: `self.len() <= rhs.len()` because of the above check
                    const_for!(i in {self.len()..rhs.len()} {
                        if rhs.get_unchecked(i) != 0 {
                            return true
                        }
                    });
                }
            }
        }
        false
    }

    /// Or-assigns `rhs` to `self`
    pub const fn or_assign(&mut self, rhs: &Self) -> Option<()> {
        binop_for_each_mut!(self, rhs, x, y, { *x |= y }, false)
    }

    /// And-assigns `rhs` to `self`
    pub const fn and_assign(&mut self, rhs: &Self) -> Option<()> {
        binop_for_each_mut!(self, rhs, x, y, { *x &= y }, false)
    }

    /// Xor-assigns `rhs` to `self`
    pub const fn xor_assign(&mut self, rhs: &Self) -> Option<()> {
        binop_for_each_mut!(self, rhs, x, y, { *x ^= y }, false)
    }

    /// And-assigns a range of ones to `self`. Useful for masking. An empty or
    /// reversed range zeroes `self`. `None` is returned if `range.start >=
    /// self.bw()` or `range.end > self.bw()`.
    pub const fn range_and_assign(&mut self, range: Range<usize>) -> Option<()> {
        if range.start >= self.bw() || range.end > self.bw() {
            return None
        }
        if range.start >= range.end {
            self.zero_assign();
            return Some(())
        }
        let start = range.start.wrapping_shr(BITS.trailing_zeros());
        let end = range.end.wrapping_shr(BITS.trailing_zeros());
        let start_bits = range.start & (BITS - 1);
        let end_bits = range.end & (BITS - 1);
        // Safety: the early `None` return above prevents any out of bounds indexing.
        unsafe {
            // zero all digits up to the start of the range
            self.digit_set(false, 0..start, false);
            match (end_bits == 0, start == end) {
                (false, false) => {
                    *self.get_unchecked_mut(start) &= MAX << start_bits;
                    *self.get_unchecked_mut(end) &= MAX >> (BITS - end_bits);
                }
                (false, true) => {
                    // The range is entirely contained in one digit
                    *self.get_unchecked_mut(start) &=
                        (MAX << start_bits) & (MAX >> (BITS - end_bits));
                }
                (true, _) => {
                    // Avoid overshift from `(BITS - end_bits)`
                    *self.get_unchecked_mut(start) &= MAX << start_bits;
                    // zero the end
                    if end < self.len() {
                        *self.get_unchecked_mut(end) = 0;
                    }
                }
            }
            // zero the rest of the digits
            if (end + 1) < self.len() {
                self.digit_set(false, (end + 1)..self.len(), false);
            }
        }
        Some(())
    }

    /// Or-assigns `rhs` to `self` at a position `shl`. Set bits of `rhs` that
    /// are shifted beyond the bitwidth of `self` are truncated.
    pub const fn usize_or_assign(&mut self, rhs: usize, shl: usize) {
        if shl >= self.bw() {
            return
        }
        // Safety: `digits < self.len()` because of the above check. The `digits + 1`
        // index is checked.
        let bits = shl & (BITS - 1);
        let digits = shl.wrapping_shr(BITS.trailing_zeros());
        unsafe {
            if bits == 0 {
                *self.get_unchecked_mut(digits) |= rhs;
            } else {
                *self.get_unchecked_mut(digits) |= rhs << bits;
                if (digits + 1) < self.len() {
                    *self.get_unchecked_mut(digits + 1) |= rhs >> (BITS - bits);
                }
            }
        }
    }
}