num_bigint_dig/algorithms/bits.rs
1use core::mem;
2
3/// Find last set bit
4/// fls(0) == 0, fls(u32::MAX) == 32
5pub fn fls<T: num_traits::PrimInt>(v: T) -> usize {
6 mem::size_of::<T>() * 8 - v.leading_zeros() as usize
7}
8
9pub fn ilog2<T: num_traits::PrimInt>(v: T) -> usize {
10 fls(v) - 1
11}
12
13/// Divide two integers, and ceil the result.
14pub fn idiv_ceil<T: num_traits::PrimInt>(a: T, b: T) -> T {
15 if a % b != T::zero() {
16 a / b + T::one()
17 } else {
18 a / b
19 }
20}