num_bigint_dig/algorithms/
shr.rs1use alloc::borrow::Cow;
2
3use num_traits::Zero;
4use smallvec::SmallVec;
5
6use crate::big_digit::{BigDigit, BITS};
7use crate::BigUint;
8use crate::VEC_SIZE;
9
10#[inline]
11pub 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}