Skip to main content

BitVec

Struct BitVec 

Source
pub struct BitVec { /* private fields */ }
Expand description

Implementation of the Lean BitVec in Rust. The Lean version is a wrapper around a Fin, a finite natural number that is guaranteed to be less than 2^width. In our implementation we use a BigUint and enforce the invariant that it is less than 2^width. Trying to create a bit-vector from a value greater than 2^width will truncate the value.

Implementations§

Source§

impl BitVec

Source

pub fn of_nat(width: Width, v: Nat) -> Self

Converts an (unsigned) Nat into a BitVec of the given width.

Silently wraps if v does not fit.

Source

pub fn of_int(width: Width, v: Int) -> Self

Converts a (signed) Int into a BitVec of the given width.

Silently wraps if v does not fit.

Source

pub fn of_u128(width: Width, val: u128) -> Self

Converts a u128 into a BitVec of the given width.

Silently wraps if val does not fit.

Source

pub fn of_i128(width: Width, val: i128) -> Self

Converts an i128 into a BitVec of the given width.

Silently wraps if val does not fit.

Source

pub fn to_nat(&self) -> Nat

Interprets a BitVec as a Nat.

Source

pub fn as_nat(&self) -> &Nat

Interprets a BitVec as a Nat.

Source

pub fn to_int(&self) -> Int

Interprets a BitVec as an Int.

Source

pub fn extract_bits(&self, low: u32, high: u32) -> Result<Self, BitVecError>

Returns an integer representing the extracted bits from low to high, inclusive.

low and/or high may be 0 without causing errors. However, we must have low <= high < self.width, else we’ll get Err (not panic).

Source

pub const fn width(&self) -> Width

Returns the bit-width of the bit-vector.

Source

pub fn signed_min(n: Width) -> Int

Returns (as Int) the minimum signed value that fits in the given bit-width.

Compare to int_min(), which returns the minimum signed value as a BitVec.

Source

pub fn signed_max(n: Width) -> Int

Returns the maximum signed value that fits in the given bit-width.

Source

pub fn overflows(n: Width, i: &Int) -> bool

Checks if the given Int fits in the bit-width.

Source

pub fn not(&self) -> Self

Bitwise not.

Source

pub fn neg(&self) -> Self

Bit-vector negation.

Source

pub fn int_min(width: Width) -> Self

Minimum signed value of the given bit-width, encoded as a BitVec.

Compare to signed_min(), which returns the minimum signed value as an Int.

Source

pub fn slt(lhs: &Self, rhs: &Self) -> Result<bool, BitVecError>

Bit-vector signed less-than.

Source

pub fn sle(lhs: &Self, rhs: &Self) -> Result<bool, BitVecError>

Bit-vector signed less-than-or-equal.

Source

pub fn ule(lhs: &Self, rhs: &Self) -> Result<bool, BitVecError>

Bit-vector unsigned less-than-or-equal.

Source

pub fn ult(lhs: &Self, rhs: &Self) -> Result<bool, BitVecError>

Bit-vector unsigned less-than.

Source

pub fn add(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector addition.

Only returns Err if the lhs and rhs widths mismatch. In particular, overflow is not an Err.

Source

pub fn sub(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector subtraction.

Only returns Err if the lhs and rhs widths mismatch. In particular, overflow is not an Err.

Source

pub fn mul(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector multiplication.

Only returns Err if the lhs and rhs widths mismatch. In particular, overflow is not an Err.

Source

pub fn udiv(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector unsigned division.

Semantics to match SMT bit-vector theory here: https://smt-lib.org/theories-FixedSizeBitVectors.shtml

Only returns Err if the lhs and rhs widths mismatch. In particular, overflow is not an Err.

Source

pub fn urem(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector unsigned remainder.

Semantics to match SMT bit-vector theory here: https://smt-lib.org/theories-FixedSizeBitVectors.shtml

Only returns Err if the lhs and rhs widths mismatch. In particular, overflow is not an Err.

Source

pub fn sdiv(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector signed division.

Semantics to match SMT bit-vector logic here: https://smt-lib.org/logics-all.shtml

Only returns Err if the lhs and rhs widths mismatch. In particular, overflow is not an Err.

Source

pub fn srem(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector signed remainder.

Semantics to match SMT bit-vector logic here: https://smt-lib.org/logics-all.shtml

Only returns Err if the lhs and rhs widths mismatch. In particular, overflow is not an Err.

Source

pub fn smod(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector signed modulus.

Semantics to match SMT bit-vector logic here: https://smt-lib.org/logics-all.shtml

Only returns Err if the lhs and rhs widths mismatch. In particular, overflow is not an Err.

Source

pub fn shl(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector left shift.

Returns Err if the lhs and rhs widths mismatch, or if the shift amount does not fit in a u32.

Source

pub fn lshr(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector logical right shift.

Returns Err if the lhs and rhs widths mismatch, or if the shift amount does not fit in a u32.

Source

pub fn concat(lhs: &Self, rhs: &Self) -> Result<Self, BitVecError>

Bit-vector concatenation.

Panics if the total width exceeds u32::MAX. As of this writing, we shouldn’t ever construct any bitvector longer than 128, which is an extremely long way from u32::MAX.

Source

pub fn zero_extend(bv: &Self, n: Width) -> Self

Bit-vector unsigned (zero) extension.

This matches the Lean implementation that just adjusts the length of the bit-vector to match n (and not the SMT-LIB implementation that zero extends the bit-vector by n bits). If n is less than the current bit-width it will truncate

Trait Implementations§

Source§

impl Clone for BitVec

Source§

fn clone(&self) -> BitVec

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for BitVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for BitVec

Source§

impl From<BitVec> for Term

Source§

fn from(bv: BitVec) -> Self

Converts to this type from the input type.
Source§

impl Hash for BitVec

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for BitVec

Source§

fn cmp(&self, other: &BitVec) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for BitVec

Source§

fn eq(&self, other: &BitVec) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for BitVec

Source§

fn partial_cmp(&self, other: &BitVec) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for BitVec

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.