From 5045a970eb8fac2c97a8cc9051ad7876befdae22 Mon Sep 17 00:00:00 2001 From: AnthonyMikh Date: Wed, 18 Sep 2019 11:11:32 +0300 Subject: [PATCH] Simplify and rename `Stackmap::from_vec` (#1032) Co-Authored-By: bjorn3 --- cranelift-codegen/src/binemit/stackmap.rs | 49 ++++++++++++----------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/cranelift-codegen/src/binemit/stackmap.rs b/cranelift-codegen/src/binemit/stackmap.rs index 8d0d2a320..4051b47e1 100644 --- a/cranelift-codegen/src/binemit/stackmap.rs +++ b/cranelift-codegen/src/binemit/stackmap.rs @@ -3,10 +3,13 @@ use crate::ir; use crate::isa::TargetIsa; use std::vec::Vec; +type Num = u32; +const NUM_BITS: usize = std::mem::size_of::() * 8; + /// Wrapper class for longer bit vectors that cannot be represented by a single BitSet. #[derive(Clone, Debug)] pub struct Stackmap { - bitmap: Vec>, + bitmap: Vec>, } impl Stackmap { @@ -52,34 +55,32 @@ impl Stackmap { } } - Stackmap::from_vec(&vec) + Stackmap::from_slice(&vec) } - /// Create a vec of Bitsets from a vec of bools. - pub fn from_vec(vec: &Vec) -> Self { - let mut rem = vec.len(); - let num_word = ((rem as f32) / 32.0).ceil() as usize; + /// Create a vec of Bitsets from a slice of bools. + pub fn from_slice(vec: &[bool]) -> Self { + let len = vec.len(); + let num_word = len / NUM_BITS + (len % NUM_BITS != 0) as usize; let mut bitmap = Vec::with_capacity(num_word); - for i in 0..num_word { + for segment in vec.chunks(NUM_BITS) { let mut curr_word = 0; - let count = if rem > 32 { 32 } else { rem }; - for j in 0..count { - if vec[i * 32 + j] { - curr_word |= 1 << j; + for (i, set) in segment.iter().enumerate() { + if *set { + curr_word |= 1 << i; } } - bitmap.push(BitSet::(curr_word)); - rem -= count; + bitmap.push(BitSet(curr_word)); } Self { bitmap } } /// Returns a specified bit. pub fn get_bit(&self, bit_index: usize) -> bool { - assert!(bit_index < 32 * self.bitmap.len()); - let word_index = bit_index / 32; - let word_offset = (bit_index % 32) as u8; + assert!(bit_index < NUM_BITS * self.bitmap.len()); + let word_index = bit_index / NUM_BITS; + let word_offset = (bit_index % NUM_BITS) as u8; self.bitmap[word_index].contains(word_offset) } } @@ -91,26 +92,26 @@ mod tests { #[test] fn stackmaps() { let vec: Vec = Vec::new(); - assert!(Stackmap::from_vec(&vec).bitmap.is_empty()); + assert!(Stackmap::from_slice(&vec).bitmap.is_empty()); - let mut vec: [bool; 32] = Default::default(); + let mut vec: [bool; NUM_BITS] = Default::default(); let set_true_idx = [5, 7, 24, 31]; - for idx in set_true_idx.iter() { - vec[*idx] = true; + for &idx in &set_true_idx { + vec[idx] = true; } let mut vec = vec.to_vec(); assert_eq!( - vec![BitSet::(2164261024)], - Stackmap::from_vec(&vec).bitmap + vec![BitSet::(2164261024)], + Stackmap::from_slice(&vec).bitmap ); vec.push(false); vec.push(true); - let res = Stackmap::from_vec(&vec); + let res = Stackmap::from_slice(&vec); assert_eq!( - vec![BitSet::(2164261024), BitSet::(2)], + vec![BitSet::(2164261024), BitSet::(2)], res.bitmap );