Skip to content

Commit

Permalink
Introduce Uint::new_overflowing_shr
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-3milabs committed Jan 24, 2025
1 parent 1c98833 commit 6fdf08b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions benches/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ fn bench_shr(c: &mut Criterion) {
)
});

group.bench_function("new shr, U2048", |b| {
b.iter_batched(
|| U2048::ONE,
|x| x.new_overflowing_shr(1024 + 10),
BatchSize::SmallInput,
)
});

group.finish();
}

Expand Down
6 changes: 6 additions & 0 deletions src/uint/shr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ impl<const LIMBS: usize> Uint<LIMBS> {
ConstCtOption::new(Uint::select(&result, &Self::ZERO, overflow), overflow.not())
}

pub const fn new_overflowing_shr(&self, shift: u32) -> ConstCtOption<Self> {
let (intra_limb_shift, limb_shift) = (shift % Limb::BITS, shift / Limb::BITS);
self.intra_limb_carrying_shr_internal(intra_limb_shift)
.full_limb_shr(limb_shift)
}

/// Computes `self >> shift`, for `shift < Limb::BITS`.
///
/// Returns `None` if `shift >= Limb::BITS`.
Expand Down

0 comments on commit 6fdf08b

Please sign in to comment.