Skip to content

Commit a75fe7d

Browse files
committed
Made blocks_required() branchless.
Made bits_per_block(), bytes_per_block() and block_offset() const fn.
1 parent 921cfd8 commit a75fe7d

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,32 +1302,27 @@ impl<T: Debug + PrimInt> Iterator for StorageIter<'_, T> {
13021302
}
13031303
}
13041304

1305-
#[inline(always)]
13061305
/// How many bits are stored in each underlying storage block?
1307-
fn bits_per_block<T>() -> usize {
1306+
const fn bits_per_block<T>() -> usize {
13081307
bytes_per_block::<T>() * 8
13091308
}
13101309

1311-
#[inline(always)]
13121310
/// How many bytes are stored in each underlying storage block?
1313-
fn bytes_per_block<T>() -> usize {
1311+
const fn bytes_per_block<T>() -> usize {
13141312
size_of::<T>()
13151313
}
13161314

1315+
#[inline(always)]
13171316
/// Return the offset in the vector of the storage block storing the bit `off`.
1318-
fn block_offset<T>(off: usize) -> usize {
1317+
const fn block_offset<T>(off: usize) -> usize {
13191318
off / bits_per_block::<T>()
13201319
}
13211320

1321+
#[inline(always)]
13221322
/// Takes as input a number of bits requiring storage; returns an aligned number of blocks needed
1323-
/// to store those bits.
1324-
fn blocks_required<T>(num_bits: usize) -> usize {
1325-
num_bits / bits_per_block::<T>()
1326-
+ if num_bits % bits_per_block::<T>() == 0 {
1327-
0
1328-
} else {
1329-
1
1330-
}
1323+
/// to store those bits. (Using the classic ceiling division formula: ⌈n / b⌉ = ⌊(n + b - 1) / b⌋)
1324+
const fn blocks_required<T>(num_bits: usize) -> usize {
1325+
(num_bits + bits_per_block::<T>() - 1) / bits_per_block::<T>()
13311326
}
13321327

13331328
#[macro_export]

0 commit comments

Comments
 (0)