Skip to content

Commit

Permalink
Fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
qinsoon committed Aug 27, 2024
1 parent 52c7604 commit 52de48d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
18 changes: 0 additions & 18 deletions src/util/metadata/metadata_val_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,12 @@ pub trait Bits {
const BITS: u32;
/// The size (in log2) of this atomic type in bits.
const LOG2: u32;

fn leading_zeros(self) -> u32;
fn trailing_zeros(self) -> u32;
fn leading_ones(self) -> u32;
fn trailing_ones(self) -> u32;
}
macro_rules! impl_bits_trait {
($t: ty) => {
impl Bits for $t {
const BITS: u32 = <$t>::BITS;
const LOG2: u32 = Self::BITS.trailing_zeros();

fn leading_zeros(self) -> u32 {
<$t>::leading_zeros(self)
}
fn trailing_zeros(self) -> u32 {
<$t>::trailing_zeros(self)
}
fn leading_ones(self) -> u32 {
<$t>::leading_ones(self)
}
fn trailing_ones(self) -> u32 {
<$t>::trailing_ones(self)
}
}
};
}
Expand Down
42 changes: 28 additions & 14 deletions src/util/metadata/side_metadata/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,24 +287,35 @@ pub fn find_last_non_zero_bit_in_metadata_bits(
FindMetaBitResult::NotFound
}

macro_rules! impl_find_last_non_zero_bit {
($int_ty: ty, $value: expr, $start: expr, $end: expr) => {{
let mask = match (1 as $int_ty).checked_shl(($end - $start) as u32) {
Some(shl) => (shl - 1) << $start,
None => <$int_ty>::MAX << $start,
};
let leading_zeroes = ($value & mask).leading_zeros();
if leading_zeroes >= <$int_ty>::BITS {
None
} else {
Some(<$int_ty>::BITS as u8 - leading_zeroes as u8 - 1)
}
}};
}

fn find_last_non_zero_bit_u8(value: u8, start: u8, end: u8) -> Option<u8> {
let mask: u8 = ((1 << (end - start + 1)) - 1) << start;
let leading_zeroes = (value & mask).leading_zeros();
if leading_zeroes >= u8::BITS {
None
} else {
Some(u8::BITS as u8 - leading_zeroes as u8)
}
// TODO: Ideally we implement the function as a generic function.
// However Rust does not have a generic trait for integers yet. We can use
// third party traits or our own trait (such as MetadataValue), but it is cumbersome,
// and does not worth the efforts.
impl_find_last_non_zero_bit!(u8, value, start, end)
}

fn find_last_non_zero_bit_usize(value: usize, start: u8, end: u8) -> Option<u8> {
let mask: usize = ((1 << (end - start + 1)) - 1) << start;
let leading_zeroes = (value & mask).leading_zeros();
if leading_zeroes >= usize::BITS {
None
} else {
Some(usize::BITS as u8 - leading_zeroes as u8)
}
// TODO: Ideally we implement the function as a generic function.
// However Rust does not have a generic trait for integers yet. We can use
// third party traits or our own trait (such as MetadataValue), but it is cumbersome,
// and does not worth the efforts.
impl_find_last_non_zero_bit!(usize, value, start, end)
}

pub fn scan_non_zero_bits_in_metadata_bytes(
Expand Down Expand Up @@ -491,6 +502,9 @@ mod tests {

let bit = find_last_non_zero_bit_u8(0b100101, 0, 8);
assert_eq!(bit, Some(5));

let bit = find_last_non_zero_bit_u8(0b0, 0, 1);
assert_eq!(bit, None);
}

#[test]
Expand Down

0 comments on commit 52de48d

Please sign in to comment.