Skip to main content

o1_utils/
math.rs

1//! This modules implements some math helper functions.
2
3/// Returns ceil(log2(d)) but panics if d = 0.
4///
5/// # Panics
6///
7/// Panics if `d` is 0.
8#[must_use]
9pub fn ceil_log2(d: usize) -> usize {
10    // NOTE: should this really be usize, since usize is depended on the underlying system architecture?
11
12    assert!(d != 0);
13    let mut pow2 = 1;
14    let mut ceil_log2 = 0;
15    while d > pow2 {
16        ceil_log2 += 1;
17        pow2 = match pow2.checked_mul(2) {
18            Some(x) => x,
19            None => break,
20        }
21    }
22    ceil_log2
23}