Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Simplify and rename Stackmap::from_vec (#1032)
Browse files Browse the repository at this point in the history
Co-Authored-By: bjorn3 <[email protected]>
  • Loading branch information
2 people authored and bnjbvr committed Sep 18, 2019
1 parent 37a1f06 commit 5045a97
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions cranelift-codegen/src/binemit/stackmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Num>() * 8;

/// Wrapper class for longer bit vectors that cannot be represented by a single BitSet.
#[derive(Clone, Debug)]
pub struct Stackmap {
bitmap: Vec<BitSet<u32>>,
bitmap: Vec<BitSet<Num>>,
}

impl Stackmap {
Expand Down Expand Up @@ -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<bool>) -> 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::<u32>(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)
}
}
Expand All @@ -91,26 +92,26 @@ mod tests {
#[test]
fn stackmaps() {
let vec: Vec<bool> = 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::<u32>(2164261024)],
Stackmap::from_vec(&vec).bitmap
vec![BitSet::<Num>(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::<u32>(2164261024), BitSet::<u32>(2)],
vec![BitSet::<Num>(2164261024), BitSet::<Num>(2)],
res.bitmap
);

Expand Down

0 comments on commit 5045a97

Please sign in to comment.