From a7c41455e978c34945c728d02363851c185c2e83 Mon Sep 17 00:00:00 2001 From: eadf Date: Fri, 11 Jul 2025 14:19:12 +0200 Subject: [PATCH] Made blocks_required() branchless. Made bits_per_block(), bytes_per_block() and block_offset() const fn. --- src/lib.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8b16615..ee72bec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1304,30 +1304,27 @@ impl Iterator for StorageIter<'_, T> { #[inline(always)] /// How many bits are stored in each underlying storage block? -fn bits_per_block() -> usize { +const fn bits_per_block() -> usize { bytes_per_block::() * 8 } #[inline(always)] /// How many bytes are stored in each underlying storage block? -fn bytes_per_block() -> usize { +const fn bytes_per_block() -> usize { size_of::() } +#[inline(always)] /// Return the offset in the vector of the storage block storing the bit `off`. -fn block_offset(off: usize) -> usize { +const fn block_offset(off: usize) -> usize { off / bits_per_block::() } +#[inline(always)] /// Takes as input a number of bits requiring storage; returns an aligned number of blocks needed /// to store those bits. fn blocks_required(num_bits: usize) -> usize { - num_bits / bits_per_block::() - + if num_bits % bits_per_block::() == 0 { - 0 - } else { - 1 - } + num_bits / bits_per_block::() + usize::from(num_bits % bits_per_block::() != 0) } #[macro_export]