Skip to content

Commit

Permalink
refactor to represent bits as u32; remove superfluous ILs
Browse files Browse the repository at this point in the history
  • Loading branch information
xorpse committed May 17, 2024
1 parent bcb3390 commit a32069d
Show file tree
Hide file tree
Showing 22 changed files with 370 additions and 5,574 deletions.
4 changes: 2 additions & 2 deletions fugue-bv/src/core_bigint/mask_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2060,8 +2060,8 @@ fn mask_value(bits: u32) -> BigInt {
}

#[inline(always)]
pub(crate) fn lookup_mask(bits: usize) -> &'static BigInt {
pub(crate) fn lookup_mask(bits: u32) -> &'static BigInt {
assert!(bits <= 2048, "unsupported BitVec size: {bits}");

&MASK_TABLE[bits]
&MASK_TABLE[bits as usize]
}
69 changes: 38 additions & 31 deletions fugue-bv/src/core_bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl FromStr for BitVec {
}
.map_err(|_| ParseError::InvalidConst)?;

let bits = usize::from_str(sz).map_err(|_| ParseError::InvalidSize)?;
let bits = u32::from_str(sz).map_err(|_| ParseError::InvalidSize)?;

Ok(Self::from_bigint(val, bits))
}
Expand All @@ -74,11 +74,11 @@ impl BitVec {
(bits << 1) | (sign as u32)
}

fn set_bits(&mut self, bits: usize) {
fn set_bits(&mut self, bits: u32) {
self.1 = ((bits as u32) << 1) | (self.1 & 1);
}

pub fn from_bigint(v: BigInt, bits: usize) -> Self {
pub fn from_bigint(v: BigInt, bits: u32) -> Self {
Self(Box::new(v), Self::pack_meta(false, bits as u32)).mask()
}

Expand Down Expand Up @@ -112,7 +112,7 @@ impl BitVec {
}
}

pub fn zero(bits: usize) -> Self {
pub fn zero(bits: u32) -> Self {
if bits == 0 {
//|| bits % 8 != 0 {
panic!("bits must be > 0")
Expand All @@ -121,7 +121,7 @@ impl BitVec {
Self::from_bigint(BigInt::from(0), bits)
}

pub fn one(bits: usize) -> Self {
pub fn one(bits: u32) -> Self {
if bits == 0 {
//|| bits % 8 != 0 {
panic!("bits must be > 0")
Expand Down Expand Up @@ -158,8 +158,8 @@ impl BitVec {
lz
}

pub fn bits(&self) -> usize {
(self.1 >> 1) as usize
pub fn bits(&self) -> u32 {
(self.1 >> 1) as u32
}

pub fn signed(self) -> Self {
Expand Down Expand Up @@ -223,12 +223,17 @@ impl BitVec {
self.0.get_bit(0)
}

pub fn bytes(&self) -> usize {
let bits = self.bits() as usize;
bits / 8 + if bits % 8 == 0 { 0 } else { 1 }
}

pub fn from_be_bytes(buf: &[u8]) -> Self {
Self::from_bigint(BigInt::from_digits(&buf, BVOrder::MsfBe), buf.len() * 8)
Self::from_bigint(BigInt::from_digits(&buf, BVOrder::MsfBe), buf.len() as u32 * 8)
}

pub fn from_le_bytes(buf: &[u8]) -> Self {
Self::from_bigint(BigInt::from_digits(&buf, BVOrder::LsfLe), buf.len() * 8)
Self::from_bigint(BigInt::from_digits(&buf, BVOrder::LsfLe), buf.len() as u32 * 8)
}

#[inline(always)]
Expand All @@ -241,7 +246,8 @@ impl BitVec {
}

pub fn to_be_bytes(&self, buf: &mut [u8]) {
let size = self.bits() / 8 + if self.bits() % 8 == 0 { 0 } else { 1 };
let bits = self.bits() as usize;
let size = bits / 8 + if bits % 8 == 0 { 0 } else { 1 };
if buf.len() != size {
panic!("invalid buf size {}; expected {}", buf.len(), size);
}
Expand All @@ -252,13 +258,14 @@ impl BitVec {
};

self.0.write_digits(
&mut buf[((self.bits() / 8) - self.0.significant_digits::<u8>())..],
&mut buf[(bits / 8 - self.0.significant_digits::<u8>())..],
BVOrder::MsfBe,
);
}

pub fn to_le_bytes(&self, buf: &mut [u8]) {
let size = self.bits() / 8 + if self.bits() % 8 == 0 { 0 } else { 1 };
let bits = self.bits() as usize;
let size = bits / 8 + if bits % 8 == 0 { 0 } else { 1 };
if buf.len() != size {
panic!("invalid buf size {}; expected {}", buf.len(), size);
}
Expand Down Expand Up @@ -458,7 +465,7 @@ impl BitVec {
)
}

pub fn max_value_with(bits: usize, signed: bool) -> Self {
pub fn max_value_with(bits: u32, signed: bool) -> Self {
let mask = lookup_mask(bits);
if signed {
Self::from_bigint_with(
Expand Down Expand Up @@ -486,7 +493,7 @@ impl BitVec {
}
}

pub fn min_value_with(bits: usize, signed: bool) -> Self {
pub fn min_value_with(bits: u32, signed: bool) -> Self {
let mask = lookup_mask(bits);
if signed {
Self::from_bigint_with(-(BigInt::from(1) << (bits - 1) as u32), mask).signed()
Expand All @@ -503,25 +510,25 @@ impl BitVec {
}
}

pub fn signed_cast(&self, size: usize) -> Self {
pub fn signed_cast(&self, size: u32) -> Self {
self.clone().signed().cast(size)
}

pub fn unsigned_cast(&self, size: usize) -> Self {
pub fn unsigned_cast(&self, size: u32) -> Self {
self.clone().unsigned().cast(size)
}

pub fn signed_cast_assign(&mut self, size: usize) {
pub fn signed_cast_assign(&mut self, size: u32) {
self.signed_assign();
self.cast_assign(size)
}

pub fn unsigned_cast_assign(&mut self, size: usize) {
pub fn unsigned_cast_assign(&mut self, size: u32) {
self.unsigned_assign();
self.cast_assign(size)
}

pub fn cast(self, size: usize) -> Self {
pub fn cast(self, size: u32) -> Self {
if self.is_signed() {
if size > self.bits() && self.msb() {
let mask = lookup_mask(size);
Expand All @@ -536,7 +543,7 @@ impl BitVec {
}
}

pub fn cast_assign(&mut self, size: usize) {
pub fn cast_assign(&mut self, size: u32) {
if self.is_signed() {
if size > self.bits() && self.msb() {
let mask = lookup_mask(size);
Expand Down Expand Up @@ -1535,7 +1542,7 @@ impl Shr<u32> for BitVec {

fn shr(self, rhs: u32) -> Self::Output {
let size = self.bits();
if rhs as usize >= size {
if rhs as u32 >= size {
if self.is_signed() {
-Self::one(size)
} else {
Expand All @@ -1545,7 +1552,7 @@ impl Shr<u32> for BitVec {
// perform ASR
let smask = lookup_mask(self.bits());
let mask =
smask ^ ((BigInt::from(1) << (size - rhs as usize) as u32) - BigInt::from(1));
smask ^ ((BigInt::from(1) << (size - rhs as u32) as u32) - BigInt::from(1));
Self::from_bigint_with((*self.0 >> rhs) | mask, smask)
} else {
let mask = lookup_mask(self.bits());
Expand All @@ -1559,7 +1566,7 @@ impl<'a> Shr<u32> for &'a BitVec {

fn shr(self, rhs: u32) -> Self::Output {
let size = self.bits();
if rhs as usize >= size {
if rhs as u32 >= size {
if self.is_signed() {
-BitVec::one(size)
} else {
Expand All @@ -1569,7 +1576,7 @@ impl<'a> Shr<u32> for &'a BitVec {
// perform ASR
let smask = lookup_mask(self.bits());
let mask =
smask ^ ((BigInt::from(1) << (size - rhs as usize) as u32) - BigInt::from(1));
smask ^ ((BigInt::from(1) << (size - rhs as u32) as u32) - BigInt::from(1));
BitVec::from_bigint_with(BigInt::from(&*self.0 >> rhs) | mask, smask)
} else {
BitVec::from_bigint_with(BigInt::from(&*self.0 >> rhs), lookup_mask(self.bits()))
Expand All @@ -1580,7 +1587,7 @@ impl<'a> Shr<u32> for &'a BitVec {
impl ShrAssign<u32> for BitVec {
fn shr_assign(&mut self, rhs: u32) {
let size = self.bits();
if rhs as usize >= size {
if rhs as u32 >= size {
if self.is_signed() {
(*self.0).clone_from(&*lookup_mask(self.bits()));
} else {
Expand All @@ -1590,7 +1597,7 @@ impl ShrAssign<u32> for BitVec {
// perform ASR
let smask = lookup_mask(self.bits());
let mask =
smask ^ ((BigInt::from(1u32) << (size - rhs as usize) as u32) - BigInt::from(1u32));
smask ^ ((BigInt::from(1u32) << (size - rhs as u32) as u32) - BigInt::from(1u32));
*self.0 >>= rhs;
*self.0 |= mask;
self.mask_assign();
Expand Down Expand Up @@ -1623,7 +1630,7 @@ impl Shr for BitVec {
if let Some(rhs) = rhs.0.to_u32() {
let smask = lookup_mask(self.bits());
let mask = smask
^ ((BigInt::from(1) << (self.bits() - rhs as usize) as u32) - BigInt::from(1));
^ ((BigInt::from(1) << (self.bits() - rhs as u32) as u32) - BigInt::from(1));
Self::from_bigint_with((*self.0 >> rhs) | mask, smask)
} else {
-Self::one(self.bits())
Expand Down Expand Up @@ -1661,7 +1668,7 @@ impl<'a> Shr for &'a BitVec {
if let Some(rhs) = rhs.0.to_u32() {
let smask = lookup_mask(self.bits());
let mask = smask
^ ((BigInt::from(1) << (self.bits() - rhs as usize) as u32) - BigInt::from(1));
^ ((BigInt::from(1) << (self.bits() - rhs as u32) as u32) - BigInt::from(1));
BitVec::from_bigint_with(BigInt::from(&*self.0 >> rhs) | mask, smask)
} else {
-BitVec::one(self.bits())
Expand Down Expand Up @@ -1730,7 +1737,7 @@ impl BitVec {
if let Some(rhs) = rhs.0.to_u32() {
let smask = lookup_mask(self.bits());
let mask = smask
^ ((BigInt::from(1) << (self.bits() - rhs as usize) as u32) - BigInt::from(1));
^ ((BigInt::from(1) << (self.bits() - rhs as u32) as u32) - BigInt::from(1));
BitVec::from_bigint_with(BigInt::from(&*self.0 >> rhs) | mask, smask)
} else {
-BitVec::one(self.bits())
Expand All @@ -1754,7 +1761,7 @@ macro_rules! impl_from_for {
($t:ident) => {
impl From<$t> for BitVec {
fn from(t: $t) -> Self {
let bits = ::std::mem::size_of::<$t>() * 8;
let bits = ::std::mem::size_of::<$t>() as u32 * 8;
BitVec::from_bigint(BigInt::from(t), bits)
}
}
Expand Down Expand Up @@ -1837,7 +1844,7 @@ macro_rules! impl_from_t_for {
($t:ident) => {
impl BitVec {
::paste::paste! {
pub fn [< from_ $t >](t: $t, bits: usize) -> Self {
pub fn [< from_ $t >](t: $t, bits: u32) -> Self {
if bits == 0 { // || bits % 8 != 0 {
panic!("bits must be > 0")
// panic!("bits must be multiple of 8 and > 0")
Expand Down
Loading

0 comments on commit a32069d

Please sign in to comment.