o1_utils/math.rs
1//! This modules implements some math helper functions.
2
3/// Returns ceil(log2(d)) but panics if d = 0.
4pub fn ceil_log2(d: usize) -> usize {
5 // NOTE: should this really be usize, since usize is depended on the underlying system architecture?
6
7 assert!(d != 0);
8 let mut pow2 = 1;
9 let mut ceil_log2 = 0;
10 while d > pow2 {
11 ceil_log2 += 1;
12 pow2 = match pow2.checked_mul(2) {
13 Some(x) => x,
14 None => break,
15 }
16 }
17 ceil_log2
18}
19
20/// This function is bound to be stable soon. See <https://github.com/rust-lang/rust/issues/88581>
21pub fn div_ceil(a: usize, b: usize) -> usize {
22 (a + b - 1) / b
23}