rasn_compiler/intermediate/encoding_rules/
mod.rs

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
pub mod per_visible;

pub fn bit_length(min: i128, max: i128) -> usize {
    let number_of_values = max - min + 1;
    let mut power = 0;
    while number_of_values > 2_i128.pow(power) {
        power += 1;
    }
    power as usize
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn computes_bit_size() {
        assert_eq!(bit_length(1, 1), 0);
        assert_eq!(bit_length(-1, 0), 1);
        assert_eq!(bit_length(3, 6), 2);
        assert_eq!(bit_length(4000, 4255), 8);
        assert_eq!(bit_length(4000, 4256), 9);
        assert_eq!(bit_length(0, 32000), 15);
        assert_eq!(bit_length(0, 65538), 17);
        assert_eq!(bit_length(-1, 127), 8);
        assert_eq!(bit_length(-900000000, 900000001), 31);
    }
}