forked from fast-pack/FastPFOR-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.rs
More file actions
37 lines (32 loc) · 1.02 KB
/
Copy pathhelpers.rs
File metadata and controls
37 lines (32 loc) · 1.02 KB
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
29
30
31
32
33
34
35
36
37
/// Finds the greatest multiple of `factor` that is less than or equal to `value`.
pub fn greatest_multiple(value: u32, factor: u32) -> u32 {
value - value % factor
}
/// Returns the number of bits needed to represent `i`.
/// Returns 0 for input 0.
pub fn bits(i: u32) -> usize {
32 - i.leading_zeros() as usize
}
/// Extracts a byte from an i32 array treated as packed bytes in big-endian order.
#[expect(dead_code)]
pub fn grab_byte(input: &[i32], index: u32) -> u8 {
(input[(index / 4) as usize] >> (24 - (index % 4) * 8)) as u8
}
/// Returns the position of the most significant bit in `x` (1-indexed).
/// Returns 0 for input 0.
#[expect(dead_code)]
pub fn leading_bit_position(x: u32) -> i32 {
bitlen(u64::from(x))
}
/// Counts the number of leading zeros in `x`.
fn clz(x: u64) -> u64 {
u64::from(x.leading_zeros())
}
/// Returns the bit length of `x` (number of bits needed to represent it).
/// Returns 0 for input 0.
fn bitlen(x: u64) -> i32 {
if x == 0 {
return 0;
}
64 - clz(x) as i32
}