Skip to main content

steel_utils/
mth.rs

1//! Vanilla `Mth` helpers shared by gameplay systems.
2
3/// Returns vanilla `Mth.ceillog2`: the number of bits needed to represent
4/// every id in `0..n`.
5#[must_use]
6pub const fn ceil_log2(n: usize) -> u8 {
7    if n <= 1 { 0 } else { (n - 1).bit_width() as u8 }
8}
9
10#[cfg(test)]
11mod tests {
12    use super::ceil_log2;
13
14    #[test]
15    fn ceil_log2_matches_vanilla_worked_examples() {
16        assert_eq!(ceil_log2(0), 0);
17        assert_eq!(ceil_log2(1), 0);
18        assert_eq!(ceil_log2(64), 6);
19        assert_eq!(ceil_log2(65), 7);
20        assert_eq!(ceil_log2(32_366), 15);
21        assert_eq!(ceil_log2(35_723), 16);
22    }
23}