Skip to content

Commit c9c5577

Browse files
authored
Merge pull request #85 from eadf/const_fn
Make blocks_required() branchless.
2 parents 921cfd8 + a7c4145 commit c9c5577

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,30 +1304,27 @@ impl<T: Debug + PrimInt> Iterator for StorageIter<'_, T> {
13041304

13051305
#[inline(always)]
13061306
/// How many bits are stored in each underlying storage block?
1307-
fn bits_per_block<T>() -> usize {
1307+
const fn bits_per_block<T>() -> usize {
13081308
bytes_per_block::<T>() * 8
13091309
}
13101310

13111311
#[inline(always)]
13121312
/// How many bytes are stored in each underlying storage block?
1313-
fn bytes_per_block<T>() -> usize {
1313+
const fn bytes_per_block<T>() -> usize {
13141314
size_of::<T>()
13151315
}
13161316

1317+
#[inline(always)]
13171318
/// Return the offset in the vector of the storage block storing the bit `off`.
1318-
fn block_offset<T>(off: usize) -> usize {
1319+
const fn block_offset<T>(off: usize) -> usize {
13191320
off / bits_per_block::<T>()
13201321
}
13211322

1323+
#[inline(always)]
13221324
/// Takes as input a number of bits requiring storage; returns an aligned number of blocks needed
13231325
/// to store those bits.
13241326
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-
}
1327+
num_bits / bits_per_block::<T>() + usize::from(num_bits % bits_per_block::<T>() != 0)
13311328
}
13321329

13331330
#[macro_export]

0 commit comments

Comments
 (0)