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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Copyright 2024-2025 Owain Davies
SPDX-License-Identifier: Apache-2.0 OR MIT
*/
use crate::macros::for_all_ints;
use crate::{Arbi, BitCount, Digit};
use core::ops::{Shl, ShlAssign};
impl Arbi {
pub(crate) fn dslice_lshift_inplace(
digits: &mut [Digit],
start_idx: usize,
size: usize,
bit_shift: u32,
) -> Digit {
debug_assert!(size > 0);
debug_assert!(digits.len() >= start_idx.saturating_add(size));
debug_assert!((1..Digit::BITS).contains(&bit_shift));
let com_bit_shift = Digit::BITS - bit_shift;
let shifted_out = digits[size - 1] >> com_bit_shift;
let mut lower = digits[size - 1];
let mut upper = lower << bit_shift;
for i in (1..size).rev() {
lower = digits[i - 1];
digits[start_idx + i] = upper | (lower >> com_bit_shift);
upper = lower << bit_shift;
}
digits[start_idx] = upper;
shifted_out
}
/// Shift `self` left by `bits` bits.
/// Equivalent to `self` * (2 ** bits).
pub(crate) fn lshift(&mut self, bits: BitCount) {
if self.is_zero() {
return;
}
let dig_shift = (bits / Digit::BITS as BitCount)
.try_into()
.unwrap_or(usize::MAX);
let bit_shift = (bits % Digit::BITS as BitCount) as u32;
let old_size = self.size();
let new_size = old_size
.saturating_add(dig_shift)
.saturating_add(usize::from(bit_shift != 0));
self.vec.resize(new_size, 0);
if bit_shift == 0 {
self.vec.copy_within(0..old_size, dig_shift);
} else {
self.vec[dig_shift + old_size] = Self::dslice_lshift_inplace(
&mut self.vec,
dig_shift,
old_size,
bit_shift,
);
}
self.vec[..dig_shift].fill(0);
self.trim();
}
}
/* !impl_shl_integral */
macro_rules! impl_shl_integral {
($($bitcount:ty),*) => {
$(
/// Return an `Arbi` integer representing this integer left-shifted `shift` bit
/// positions with vacated bits zero-filled.
///
/// Mathematically, the value of the resulting integer is
/// \\[
/// x \times 2^{\text{shift}}
/// \\]
/// where \\( x \\) is the big integer.
///
/// The right-hand-side (RHS) of a left shift operation can be a nonnegative
/// value of any primitive integer type.
///
/// # Panics
/// - Panics if `rhs` is negative.
/// - Panics if the result of the operation exceeds `Vec`'s limits.
///
/// # Examples
/// ```
/// use arbi::Arbi;
///
/// assert_eq!(Arbi::zero() << 1, 0);
/// assert_eq!(0 << 1, 0);
///
/// let mut a = Arbi::neg_one();
///
/// a <<= 0;
/// assert_eq!(a, -1);
/// assert_eq!(a, -1 << 0);
///
/// a <<= 1; // in-place
/// assert_eq!(a, -2);
/// assert_eq!(a, -1 << 1);
///
/// a = a << 1; // in-place
/// assert_eq!(a, -4);
/// assert_eq!(a, -1 << 2);
///
/// a = &a << 1; // clones (currently)
/// assert_eq!(a, -8);
/// assert_eq!(a, -1 << 3);
/// ```
///
/// Negative RHS values cause a panic:
/// ```should_panic
/// use arbi::Arbi;
/// let _ = Arbi::zero() << -1;
/// ```
///
/// This panics because it would exceed `Vec`'s limits:
/// ```should_panic
/// use arbi::Arbi;
/// let _ = Arbi::one() << Arbi::MAX_BITS;
/// ```
///
/// # Complexity
/// \\( O(n) \\)
impl Shl<$bitcount> for &Arbi {
type Output = Arbi;
fn shl(self, rhs: $bitcount) -> Arbi {
let mut ret = self.clone();
ret <<= rhs;
ret
}
}
/// See, for example, [`Shl<u32> for &Arbi`](#impl-Shl<u32>-for-%26Arbi).
impl<'a> Shl<&'a $bitcount> for &Arbi {
type Output = Arbi;
fn shl(self, rhs: &'a $bitcount) -> Arbi {
self << *rhs
}
}
/// See, for example, [`Shl<u32> for &Arbi`](#impl-Shl<u32>-for-%26Arbi).
impl Shl<$bitcount> for Arbi {
type Output = Arbi;
fn shl(mut self, rhs: $bitcount) -> Arbi {
self <<= rhs;
self
}
}
/// See, for example, [`Shl<u32> for &Arbi`](#impl-Shl<u32>-for-%26Arbi).
impl Shl<&$bitcount> for Arbi {
type Output = Arbi;
fn shl(self, rhs: &$bitcount) -> Arbi {
self << *rhs
}
}
/// See, for example, [`Shl<u32> for &Arbi`](#impl-Shl<u32>-for-%26Arbi).
impl ShlAssign<$bitcount> for Arbi {
#[allow(unused_comparisons)]
fn shl_assign(&mut self, rhs: $bitcount) {
assert!(rhs >= 0, "Only nonnegative shifts are supported");
self.lshift(rhs.try_into().unwrap_or(BitCount::MAX));
}
}
/// See, for example, [`Shl<u32> for &Arbi`](#impl-Shl<u32>-for-%26Arbi).
impl ShlAssign<&$bitcount> for Arbi {
fn shl_assign(&mut self, rhs: &$bitcount) {
*self <<= *rhs
}
}
)*
};
}
/* impl_shl_integral! */
for_all_ints!(impl_shl_integral);
#[cfg(test)]
mod tests {
use super::*;
use crate::{BitCount, DDigit};
// On rustc < 1.65, fails with message, "memory allocation of {isize::MAX as
// usize + 1} bytes failed", but in 1.65 (MSRV) and later, does not.
#[test]
#[should_panic = "capacity overflow"] // From `Vec`
fn test_large_shift_panics_max_bits() {
let one = Arbi::one();
let _ = one << Arbi::MAX_BITS;
}
#[test]
#[should_panic = "Only nonnegative shifts are supported"]
fn test_negative_shift_panics() {
let _ = Arbi::zero() << -1;
}
#[test]
fn test_left_shift_zero() {
assert_eq!(Arbi::zero() << 1 as BitCount, 0);
assert_eq!(Arbi::zero() << Digit::BITS as BitCount, 0);
assert_eq!(Arbi::zero() << 0 as BitCount, 0);
}
#[test]
fn test_left_shift_assign_zero() {
let mut zero: Arbi;
zero = Arbi::zero();
zero <<= 1 as BitCount;
assert_eq!(zero, 0);
zero = Arbi::zero();
zero <<= Digit::BITS as BitCount;
assert_eq!(zero, 0);
zero = Arbi::zero();
zero <<= 0 as BitCount;
assert_eq!(zero, 0);
}
#[test]
fn test_left_shift_misc() {
assert_eq!(Arbi::from(4) << 2 as BitCount, 16);
assert_eq!(Arbi::from(-4) << 2 as BitCount, -16);
assert_eq!(Arbi::from(4) << 0 as BitCount, 4);
assert_eq!(Arbi::from(-4) << 0 as BitCount, -4);
}
#[test]
fn test_left_shift_assign_misc() {
let mut a: Arbi;
a = Arbi::from(4);
a <<= 2 as BitCount;
assert_eq!(a, 16);
a = Arbi::from(-4);
a <<= 2 as BitCount;
assert_eq!(a, -16);
a = Arbi::from(4);
a <<= 0 as BitCount;
assert_eq!(a, 4);
a = Arbi::from(-4);
a <<= 0 as BitCount;
assert_eq!(a, -4);
}
#[test]
fn test_left_shift_powers_of_2_in_digit() {
let one = Arbi::one();
for i in 0..(Digit::BITS as BitCount * 2) {
assert_eq!(&one << i, (1 as DDigit) << i);
}
}
#[test]
fn test_left_shift_assign_powers_of_2_in_digit() {
for i in 0..(Digit::BITS as BitCount * 2) {
let mut one = Arbi::one();
one <<= i;
assert_eq!(one, (1 as DDigit) << i);
}
}
#[test]
fn test_lshift() {
let mut one = Arbi::one();
let mut one_prim: u128 = 1;
for i in (1 as BitCount)..(128 as BitCount) {
assert_eq!(Arbi::one() << i, 1_u128 << i);
assert_eq!(one, one_prim, "i = {}", i);
one <<= 1 as BitCount;
one_prim <<= 1 as BitCount;
}
}
}