pub struct UInt { /* private fields */ }
Expand description
Unsigned machine integer with arbitrary bitwidths and modulo arithmetics.
Thin convenience wrapper around ApInt
for static unsigned interpretation of the value.
This very cheaply transformes to and from ApInt
and Int
instances and together with
Int
offers a more elegant and higher-level abstraction interface to the lower-level ApInt
.
Implementations§
Source§impl UInt
impl UInt
Sourcepub fn into_apint(self) -> ApInt
pub fn into_apint(self) -> ApInt
Transforms this UInt
into an equivalent ApInt
instance.
Sourcepub fn into_signed(self) -> Int
pub fn into_signed(self) -> Int
Transforms this UInt
into an equivalent Int
instance.
Source§impl UInt
§Constructors
impl UInt
§Constructors
Sourcepub fn from_bit<B>(bit: B) -> UInt
pub fn from_bit<B>(bit: B) -> UInt
Creates a new UInt
from the given Bit
value with a bit width of 1
.
This function is generic over types that are convertible to Bit
such as bool
.
Sourcepub fn from_u8(val: u8) -> UInt
pub fn from_u8(val: u8) -> UInt
Creates a new UInt
from a given u8
value with a bit-width of 8.
Sourcepub fn from_u16(val: u16) -> UInt
pub fn from_u16(val: u16) -> UInt
Creates a new UInt
from a given u16
value with a bit-width of 16.
Sourcepub fn from_u32(val: u32) -> UInt
pub fn from_u32(val: u32) -> UInt
Creates a new UInt
from a given u32
value with a bit-width of 32.
Sourcepub fn from_u64(val: u64) -> UInt
pub fn from_u64(val: u64) -> UInt
Creates a new UInt
from a given u64
value with a bit-width of 64.
Sourcepub fn from_u128(val: u128) -> UInt
pub fn from_u128(val: u128) -> UInt
Creates a new UInt
from a given u64
value with a bit-width of 64.
Sourcepub fn zero(width: BitWidth) -> UInt
pub fn zero(width: BitWidth) -> UInt
Creates a new UInt
with the given bit width that represents zero.
Sourcepub fn one(width: BitWidth) -> UInt
pub fn one(width: BitWidth) -> UInt
Creates a new UInt
with the given bit width that represents one.
Sourcepub fn all_unset(width: BitWidth) -> UInt
pub fn all_unset(width: BitWidth) -> UInt
Creates a new UInt
with the given bit width that has all bits unset.
Note: This is equal to calling UInt::zero
with the given width
.
Sourcepub fn all_set(width: BitWidth) -> UInt
pub fn all_set(width: BitWidth) -> UInt
Creates a new UInt
with the given bit width that has all bits set.
§Note
- This is equal to minus one on any twos-complement machine.
Source§impl UInt
§Utilities
impl UInt
§Utilities
Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Returns true
if this UInt
represents the value zero (0
).
§Note
- Zero (
0
) is also called the additive neutral element. - This operation is more efficient than comparing two instances
of
UInt
for the same reason.
Source§impl UInt
impl UInt
Sourcepub fn checked_lt(&self, rhs: &UInt) -> Result<bool>
pub fn checked_lt(&self, rhs: &UInt) -> Result<bool>
Sourcepub fn checked_le(&self, rhs: &UInt) -> Result<bool>
pub fn checked_le(&self, rhs: &UInt) -> Result<bool>
Sourcepub fn checked_gt(&self, rhs: &UInt) -> Result<bool>
pub fn checked_gt(&self, rhs: &UInt) -> Result<bool>
Source§impl UInt
§To Primitive (Resize)
impl UInt
§To Primitive (Resize)
Sourcepub fn resize_to_bool(&self) -> bool
pub fn resize_to_bool(&self) -> bool
Resizes this UInt
to a bool
primitive type.
Bits in this UInt
that are not within the bounds
of the bool
are being ignored.
§Note
- Basically this returns
true
if the least significant bit of thisUInt
is1
andfalse
otherwise.
Sourcepub fn resize_to_u8(&self) -> u8
pub fn resize_to_u8(&self) -> u8
Resizes this UInt
to a u8
primitive type.
§Note
- All bits but the least significant
8
bits are being ignored by this operation to construct the result.
Sourcepub fn resize_to_u16(&self) -> u16
pub fn resize_to_u16(&self) -> u16
Resizes this UInt
to a u16
primitive type.
§Note
- All bits but the least significant
16
bits are being ignored by this operation to construct the result.
Sourcepub fn resize_to_u32(&self) -> u32
pub fn resize_to_u32(&self) -> u32
Resizes this UInt
to a u32
primitive type.
§Note
- All bits but the least significant
32
bits are being ignored by this operation to construct the result.
Sourcepub fn resize_to_u64(&self) -> u64
pub fn resize_to_u64(&self) -> u64
Resizes this UInt
to a u64
primitive type.
§Note
- All bits but the least significant
64
bits are being ignored by this operation to construct the result.
Sourcepub fn resize_to_u128(&self) -> u128
pub fn resize_to_u128(&self) -> u128
Resizes this UInt
to a u128
primitive type.
§Note
- All bits but the least significant
128
bits are being ignored by this operation to construct the result.
Source§impl UInt
§To Primitive (Try-Cast)
impl UInt
§To Primitive (Try-Cast)
Sourcepub fn try_to_bool(&self) -> Result<bool>
pub fn try_to_bool(&self) -> Result<bool>
Sourcepub fn try_to_u16(&self) -> Result<u16>
pub fn try_to_u16(&self) -> Result<u16>
Sourcepub fn try_to_u32(&self) -> Result<u32>
pub fn try_to_u32(&self) -> Result<u32>
Sourcepub fn try_to_u64(&self) -> Result<u64>
pub fn try_to_u64(&self) -> Result<u64>
Sourcepub fn try_to_u128(&self) -> Result<u128>
pub fn try_to_u128(&self) -> Result<u128>
Tries to represent the value of this UInt
as a u128
.
§Note
- This conversion is possible as long as the value represented
by this
UInt
does not exceed the maximum value ofu128
.
§Complexity
- 𝒪(n) where n is the number of digits of this
UInt
.
§Errors
- If the value represented by this
UInt
can not be represented by au128
.
Source§impl UInt
§Shifts
impl UInt
§Shifts
Sourcepub fn checked_shl_assign<S>(&mut self, shift_amount: S) -> Result<()>where
S: Into<ShiftAmount>,
pub fn checked_shl_assign<S>(&mut self, shift_amount: S) -> Result<()>where
S: Into<ShiftAmount>,
Shift this UInt
left by the given shift_amount
bits.
This operation is inplace and will not allocate memory.
§Errors
- If the given
shift_amount
is invalid for the bit width of thisUInt
.
Sourcepub fn into_checked_shl<S>(self, shift_amount: S) -> Result<UInt>where
S: Into<ShiftAmount>,
pub fn into_checked_shl<S>(self, shift_amount: S) -> Result<UInt>where
S: Into<ShiftAmount>,
Shift this UInt
left by the given shift_amount
bits and returns the result.
This operation is inplace and will not allocate memory.
§Errors
- If the given
shift_amount
is invalid for the bit width of thisUInt
.
Sourcepub fn checked_shr_assign<S>(&mut self, shift_amount: S) -> Result<()>where
S: Into<ShiftAmount>,
pub fn checked_shr_assign<S>(&mut self, shift_amount: S) -> Result<()>where
S: Into<ShiftAmount>,
Right-shifts this UInt
by the given shift_amount
bits.
This operation is inplace and will not allocate memory.
§Errors
- If the given
shift_amount
is invalid for the bit width of thisUInt
.
Sourcepub fn into_checked_shr<S>(self, shift_amount: S) -> Result<UInt>where
S: Into<ShiftAmount>,
pub fn into_checked_shr<S>(self, shift_amount: S) -> Result<UInt>where
S: Into<ShiftAmount>,
Right-shifts this UInt
by the given shift_amount
bits
and returns the result.
This operation is inplace and will not allocate memory.
§Errors
- If the given
shift_amount
is invalid for the bit width of thisUInt
.
Source§impl UInt
§Random Utilities using rand
crate.
impl UInt
§Random Utilities using rand
crate.
Sourcepub fn random_with_width(width: BitWidth) -> UInt
pub fn random_with_width(width: BitWidth) -> UInt
Creates a new UInt
with the given BitWidth
and random Digit
s.
Sourcepub fn random_with_width_using<R>(width: BitWidth, rng: &mut R) -> UIntwhere
R: Rng,
pub fn random_with_width_using<R>(width: BitWidth, rng: &mut R) -> UIntwhere
R: Rng,
Creates a new UInt
with the given BitWidth
and random Digit
s
using the given random number generator.
Note: This is useful for cryptographic or testing purposes.
Sourcepub fn randomize(&mut self)
pub fn randomize(&mut self)
Randomizes the digits of this UInt
inplace.
This won’t change its BitWidth
.
Sourcepub fn randomize_using<R>(&mut self, rng: &mut R)where
R: Rng,
pub fn randomize_using<R>(&mut self, rng: &mut R)where
R: Rng,
Randomizes the digits of this UInt
inplace using the given
random number generator.
This won’t change its BitWidth
.
Source§impl UInt
impl UInt
Sourcepub fn assign(&mut self, rhs: &UInt)
pub fn assign(&mut self, rhs: &UInt)
Assigns rhs
to this UInt
.
This mutates digits and may affect the bitwidth of self
which might result in an expensive operations.
After this operation rhs
and self
are equal to each other.
Sourcepub fn strict_assign(&mut self, rhs: &UInt) -> Result<()>
pub fn strict_assign(&mut self, rhs: &UInt) -> Result<()>
Strictly assigns rhs
to this UInt
.
After this operation rhs
and self
are equal to each other.
Note: Strict assigns protect against mutating the bit width
of self
and thus return an error instead of executing a probably
expensive assign
operation.
§Errors
- If
rhs
andself
have unmatching bit widths.
Source§impl UInt
§Casting: Truncation & Extension
impl UInt
§Casting: Truncation & Extension
Sourcepub fn into_truncate<W>(self, target_width: W) -> Result<UInt>
pub fn into_truncate<W>(self, target_width: W) -> Result<UInt>
Sourcepub fn truncate<W>(&mut self, target_width: W) -> Result<()>
pub fn truncate<W>(&mut self, target_width: W) -> Result<()>
Tries to truncate this UInt
inplace to the given target_width
.
§Note
- This is a no-op if
self.width()
andtarget_width
are equal. - This operation is inplace as long as
self.width()
andtarget_width
require the same amount of digits for their representation.
§Errors
- If the
target_width
is greater than the current width.
Sourcepub fn into_extend<W>(self, target_width: W) -> Result<UInt>
pub fn into_extend<W>(self, target_width: W) -> Result<UInt>
Sourcepub fn extend<W>(&mut self, target_width: W) -> Result<()>
pub fn extend<W>(&mut self, target_width: W) -> Result<()>
Tries to extend this UInt
inplace to the given target_width
.
§Note
- This is a no-op if
self.width()
andtarget_width
are equal. - This operation is inplace as long as
self.width()
andtarget_width
require the same amount of digits for their representation.
§Errors
- If the
target_width
is less than the current width.
Sourcepub fn into_resize<W>(self, target_width: W) -> UInt
pub fn into_resize<W>(self, target_width: W) -> UInt
Source§impl UInt
§Bitwise Operations
impl UInt
§Bitwise Operations
Sourcepub fn into_bitnot(self) -> Self
pub fn into_bitnot(self) -> Self
Flips all bits of self
and returns the result.
Sourcepub fn into_checked_bitand(self, rhs: &UInt) -> Result<UInt>
pub fn into_checked_bitand(self, rhs: &UInt) -> Result<UInt>
Tries to bit-and assign this UInt
inplace to rhs
and returns the result.
Note: This forwards to
checked_bitand
.
§Errors
If self
and rhs
have unmatching bit widths.
Sourcepub fn checked_bitand_assign(&mut self, rhs: &UInt) -> Result<()>
pub fn checked_bitand_assign(&mut self, rhs: &UInt) -> Result<()>
Bit-and assigns all bits of this UInt
with the bits of rhs
.
Note: This operation is inplace of self
and won’t allocate memory.
§Errors
If self
and rhs
have unmatching bit widths.
Sourcepub fn into_checked_bitor(self, rhs: &UInt) -> Result<UInt>
pub fn into_checked_bitor(self, rhs: &UInt) -> Result<UInt>
Tries to bit-and assign this UInt
inplace to rhs
and returns the result.
Note: This forwards to
checked_bitor
.
§Errors
If self
and rhs
have unmatching bit widths.
Sourcepub fn checked_bitor_assign(&mut self, rhs: &UInt) -> Result<()>
pub fn checked_bitor_assign(&mut self, rhs: &UInt) -> Result<()>
Bit-or assigns all bits of this UInt
with the bits of rhs
.
Note: This operation is inplace of self
and won’t allocate memory.
§Errors
If self
and rhs
have unmatching bit widths.
Sourcepub fn into_checked_bitxor(self, rhs: &UInt) -> Result<UInt>
pub fn into_checked_bitxor(self, rhs: &UInt) -> Result<UInt>
Tries to bit-xor assign this UInt
inplace to rhs
and returns the result.
Note: This forwards to
checked_bitxor
.
§Errors
If self
and rhs
have unmatching bit widths.
Source§impl UInt
§Bitwise Access
impl UInt
§Bitwise Access
Sourcepub fn get_bit_at<P>(&self, pos: P) -> Result<Bit>
pub fn get_bit_at<P>(&self, pos: P) -> Result<Bit>
Returns the bit at the given bit position pos
.
This returns
Bit::Set
if the bit atpos
is1
Bit::Unset
otherwise
§Errors
- If
pos
is not a valid bit position for the width of thisUInt
.
Sourcepub fn set_bit_at<P>(&mut self, pos: P) -> Result<()>
pub fn set_bit_at<P>(&mut self, pos: P) -> Result<()>
Sets the bit at the given bit position pos
to one (1
).
§Errors
- If
pos
is not a valid bit position for the width of thisUInt
.
Sourcepub fn unset_bit_at<P>(&mut self, pos: P) -> Result<()>
pub fn unset_bit_at<P>(&mut self, pos: P) -> Result<()>
Sets the bit at the given bit position pos
to zero (0
).
§Errors
- If
pos
is not a valid bit position for the width of thisUInt
.
Sourcepub fn flip_bit_at<P>(&mut self, pos: P) -> Result<()>
pub fn flip_bit_at<P>(&mut self, pos: P) -> Result<()>
Sourcepub fn is_all_set(&self) -> bool
pub fn is_all_set(&self) -> bool
Returns true
if all bits in this UInt
are set.
Sourcepub fn is_all_unset(&self) -> bool
pub fn is_all_unset(&self) -> bool
Returns true
if all bits in this UInt
are unset.
Source§impl UInt
§Bitwise utility methods.
impl UInt
§Bitwise utility methods.
Sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Returns the number of ones in the binary representation of this UInt
.
Sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Returns the number of zeros in the binary representation of this UInt
.
Sourcepub fn leading_zeros(&self) -> usize
pub fn leading_zeros(&self) -> usize
Returns the number of leading zeros in the binary representation of this UInt
.
Sourcepub fn trailing_zeros(&self) -> usize
pub fn trailing_zeros(&self) -> usize
Returns the number of trailing zeros in the binary representation of this UInt
.
Source§impl UInt
§Arithmetic Operations
impl UInt
§Arithmetic Operations
Sourcepub fn into_checked_add(self, rhs: &UInt) -> Result<UInt>
pub fn into_checked_add(self, rhs: &UInt) -> Result<UInt>
Adds rhs
to self
and returns the result.
Note: This will not allocate memory.
§Errors
- If
self
andrhs
have unmatching bit widths.
Sourcepub fn checked_add_assign(&mut self, rhs: &UInt) -> Result<()>
pub fn checked_add_assign(&mut self, rhs: &UInt) -> Result<()>
Add-assigns rhs
to self
inplace.
Note: This will not allocate memory.
§Errors
- If
self
andrhs
have unmatching bit widths.
Sourcepub fn into_checked_sub(self, rhs: &UInt) -> Result<UInt>
pub fn into_checked_sub(self, rhs: &UInt) -> Result<UInt>
Sourcepub fn checked_sub_assign(&mut self, rhs: &UInt) -> Result<()>
pub fn checked_sub_assign(&mut self, rhs: &UInt) -> Result<()>
Sourcepub fn into_checked_mul(self, rhs: &UInt) -> Result<UInt>
pub fn into_checked_mul(self, rhs: &UInt) -> Result<UInt>
Sourcepub fn checked_mul_assign(&mut self, rhs: &UInt) -> Result<()>
pub fn checked_mul_assign(&mut self, rhs: &UInt) -> Result<()>
Sourcepub fn into_checked_div(self, rhs: &UInt) -> Result<UInt>
pub fn into_checked_div(self, rhs: &UInt) -> Result<UInt>
Sourcepub fn checked_div_assign(&mut self, rhs: &UInt) -> Result<()>
pub fn checked_div_assign(&mut self, rhs: &UInt) -> Result<()>
Sourcepub fn into_checked_rem(self, rhs: &UInt) -> Result<UInt>
pub fn into_checked_rem(self, rhs: &UInt) -> Result<UInt>
Calculates the unsigned remainder of self
by rhs
and returns the result.
§Note
- This operation will not allocate memory and computes inplace of
self
. - In the low-level machine abstraction signed division and unsigned division are two different operations.
§Errors
- If
self
andrhs
have unmatching bit widths.
Sourcepub fn checked_rem_assign(&mut self, rhs: &UInt) -> Result<()>
pub fn checked_rem_assign(&mut self, rhs: &UInt) -> Result<()>
Trait Implementations§
Source§impl<'a> AddAssign<&'a UInt> for UInt
impl<'a> AddAssign<&'a UInt> for UInt
Source§fn add_assign(&mut self, rhs: &'a UInt)
fn add_assign(&mut self, rhs: &'a UInt)
+=
operation. Read moreSource§impl<'a> BitAndAssign<&'a UInt> for UInt
impl<'a> BitAndAssign<&'a UInt> for UInt
Source§fn bitand_assign(&mut self, rhs: &'a UInt)
fn bitand_assign(&mut self, rhs: &'a UInt)
&=
operation. Read moreSource§impl<'a> BitOrAssign<&'a UInt> for UInt
impl<'a> BitOrAssign<&'a UInt> for UInt
Source§fn bitor_assign(&mut self, rhs: &'a UInt)
fn bitor_assign(&mut self, rhs: &'a UInt)
|=
operation. Read moreSource§impl<'a> BitXorAssign<&'a UInt> for UInt
impl<'a> BitXorAssign<&'a UInt> for UInt
Source§fn bitxor_assign(&mut self, rhs: &'a UInt)
fn bitxor_assign(&mut self, rhs: &'a UInt)
^=
operation. Read moreSource§impl<'de> Deserialize<'de> for UInt
impl<'de> Deserialize<'de> for UInt
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<'a> DivAssign<&'a UInt> for UInt
impl<'a> DivAssign<&'a UInt> for UInt
Source§fn div_assign(&mut self, rhs: &'a UInt)
fn div_assign(&mut self, rhs: &'a UInt)
/=
operation. Read moreSource§impl<'a> MulAssign<&'a UInt> for UInt
impl<'a> MulAssign<&'a UInt> for UInt
Source§fn mul_assign(&mut self, rhs: &'a UInt)
fn mul_assign(&mut self, rhs: &'a UInt)
*=
operation. Read moreSource§impl PartialOrd for UInt
impl PartialOrd for UInt
Source§impl<'a> RemAssign<&'a UInt> for UInt
impl<'a> RemAssign<&'a UInt> for UInt
Source§fn rem_assign(&mut self, rhs: &'a UInt)
fn rem_assign(&mut self, rhs: &'a UInt)
%=
operation. Read moreSource§impl<S> ShlAssign<S> for UIntwhere
S: Into<ShiftAmount>,
impl<S> ShlAssign<S> for UIntwhere
S: Into<ShiftAmount>,
Source§fn shl_assign(&mut self, shift_amount: S)
fn shl_assign(&mut self, shift_amount: S)
<<=
operation. Read moreSource§impl<S> ShrAssign<S> for UIntwhere
S: Into<ShiftAmount>,
impl<S> ShrAssign<S> for UIntwhere
S: Into<ShiftAmount>,
Source§fn shr_assign(&mut self, shift_amount: S)
fn shr_assign(&mut self, shift_amount: S)
>>=
operation. Read moreSource§impl<'a> SubAssign<&'a UInt> for UInt
impl<'a> SubAssign<&'a UInt> for UInt
Source§fn sub_assign(&mut self, rhs: &'a UInt)
fn sub_assign(&mut self, rhs: &'a UInt)
-=
operation. Read more