1pub fn is_pwr_two(n: u64) -> bool {
16 n & (n - 1) == 0
17}
18
19pub fn pad_bytes<const ALIGN: usize>(n: usize) -> usize {
30 debug_assert!(is_pwr_two(ALIGN as u64));
31 (ALIGN - (n & (ALIGN - 1))) & (ALIGN - 1)
32}
33
34pub fn pad_bytes_to(n: usize, align: usize) -> usize {
45 debug_assert!(is_pwr_two(align as u64));
46 (align - (n & (align - 1))) & (align - 1)
47}
48
49pub fn pad_bytes_u64<const ALIGN: u64>(n: u64) -> u64 {
60 debug_assert!(is_pwr_two(ALIGN));
61 (ALIGN - (n & (ALIGN - 1))) & (ALIGN - 1)
62}
63
64const LOG_TABLE_256: [u8; 256] = [
66 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
67 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
68 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
69 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
70 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
71 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
72 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
73 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
74];
75
76pub fn log_2_ceil(val: u32) -> u32 {
89 assert!(val > 0);
90 let upper_half = val >> 16;
91 if upper_half == 0 {
92 let third_quarter = val >> 8;
93 if third_quarter == 0 {
94 LOG_TABLE_256[val as usize] as u32
96 } else {
97 LOG_TABLE_256[third_quarter as usize] as u32 + 8
99 }
100 } else {
101 let first_quarter = upper_half >> 8;
102 if first_quarter == 0 {
103 16 + LOG_TABLE_256[upper_half as usize] as u32
105 } else {
106 24 + LOG_TABLE_256[first_quarter as usize] as u32
108 }
109 }
110}
111
112#[cfg(test)]
113pub mod tests {
114 use crate::utils::bit::{is_pwr_two, log_2_ceil, pad_bytes, pad_bytes_to, pad_bytes_u64};
115
116 #[test]
117 fn test_bit_utils() {
118 assert!(is_pwr_two(4));
120 assert!(is_pwr_two(1024));
121 assert!(!is_pwr_two(5));
122
123 assert_eq!(pad_bytes::<64>(100), 28);
125 assert_eq!(pad_bytes_to(100, 64), 28);
126 assert_eq!(pad_bytes_u64::<64>(100), 28);
127 }
128
129 #[test]
130 fn test_log_2_ceil() {
131 #[cfg_attr(coverage, coverage(off))]
132 fn classic_approach(mut val: u32) -> u32 {
133 let mut counter = 0;
134 while val > 0 {
135 val >>= 1;
136 counter += 1;
137 }
138 counter
139 }
140
141 for i in 1..(16 * 1024) {
142 assert_eq!(log_2_ceil(i), classic_approach(i));
143 }
144 assert_eq!(log_2_ceil(50 * 1024), classic_approach(50 * 1024));
145 assert_eq!(
146 log_2_ceil(1024 * 1024 * 1024),
147 classic_approach(1024 * 1024 * 1024)
148 );
149 assert_eq!(log_2_ceil(100_000), classic_approach(100_000));
152 }
153}