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
//! Cryptographic utilities.
pub mod aes_hash;
pub mod aes_rng;
pub mod block;
pub mod tokio_rayon;
pub use block::Block;

#[allow(unused)]
pub(crate) fn log2_floor(val: usize) -> u32 {
    assert!(val > 0);
    usize::BITS - val.leading_zeros() - 1
}

#[allow(unused)]
pub(crate) fn log2_ceil(val: usize) -> u32 {
    let floor = log2_floor(val);
    if val > (1 << floor) {
        floor + 1
    } else {
        floor
    }
}

#[cfg(test)]
mod test {
    use crate::util::{log2_ceil, log2_floor};

    #[test]
    fn log2() {
        assert_eq!(log2_floor(2_usize.pow(24)), 24);
        assert_eq!(log2_floor(2_usize.pow(24) + 42), 24);
        assert_eq!(log2_ceil(2_usize.pow(24)), 24);
        assert_eq!(log2_ceil(2_usize.pow(24) + 42), 25);
    }
}