1 use alloc::borrow::Cow;
2 
3 use num_traits::Zero;
4 use smallvec::SmallVec;
5 
6 use crate::big_digit::{BigDigit, BITS};
7 use crate::BigUint;
8 use crate::VEC_SIZE;
9 
10 #[inline]
biguint_shr(n: Cow<BigUint>, bits: usize) -> BigUint11 pub fn biguint_shr(n: Cow<BigUint>, bits: usize) -> BigUint {
12     let n_unit = bits / BITS;
13     if n_unit >= n.data.len() {
14         return Zero::zero();
15     }
16     let mut data: SmallVec<[BigDigit; VEC_SIZE]> = match n {
17         Cow::Borrowed(n) => n.data[n_unit..].into(),
18         Cow::Owned(n) => n.data[n_unit..].into(),
19     };
20 
21     let n_bits = bits % BITS;
22     if n_bits > 0 {
23         let mut borrow = 0;
24         for elem in data.iter_mut().rev() {
25             let new_borrow = *elem << (BITS - n_bits);
26             *elem = (*elem >> n_bits) | borrow;
27             borrow = new_borrow;
28         }
29     }
30 
31     BigUint::new_native(data)
32 }
33