pub struct Int { /* private fields */ }
Expand description
Signed machine integer with arbitrary bitwidths and modulo arithmetics.
Thin convenience wrapper around ApInt
for static signed interpretation of the value.
This very cheaply transformes to and from ApInt
and UInt
instances and together with
UInt
offers a more elegant and higher-level abstraction interface to the lower-level ApInt
.
Implementations§
Source§impl Int
impl Int
Sourcepub fn into_apint(self) -> ApInt
pub fn into_apint(self) -> ApInt
Transforms this Int
into an equivalent ApInt
instance.
Sourcepub fn into_unsigned(self) -> UInt
pub fn into_unsigned(self) -> UInt
Transforms this Int
into an equivalent UInt
instance.
Source§impl Int
§Constructors
impl Int
§Constructors
Sourcepub fn from_bit<B>(bit: B) -> Int
pub fn from_bit<B>(bit: B) -> Int
Creates a new Int
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_i16(val: i16) -> Int
pub fn from_i16(val: i16) -> Int
Creates a new Int
from a given i16
value with a bit-width of 16.
Sourcepub fn from_i32(val: i32) -> Int
pub fn from_i32(val: i32) -> Int
Creates a new Int
from a given i32
value with a bit-width of 32.
Sourcepub fn from_i64(val: i64) -> Int
pub fn from_i64(val: i64) -> Int
Creates a new Int
from a given i64
value with a bit-width of 64.
Sourcepub fn from_i128(val: i128) -> Int
pub fn from_i128(val: i128) -> Int
Creates a new Int
from a given i64
value with a bit-width of 64.
Sourcepub fn zero(width: BitWidth) -> Int
pub fn zero(width: BitWidth) -> Int
Creates a new Int
with the given bit width that represents zero.
Sourcepub fn one(width: BitWidth) -> Int
pub fn one(width: BitWidth) -> Int
Creates a new Int
with the given bit width that represents one.
Sourcepub fn all_unset(width: BitWidth) -> Int
pub fn all_unset(width: BitWidth) -> Int
Creates a new Int
with the given bit width that has all bits unset.
Note: This is equal to calling Int::zero
with the given width
.
Sourcepub fn all_set(width: BitWidth) -> Int
pub fn all_set(width: BitWidth) -> Int
Creates a new Int
with the given bit width that has all bits set.
§Note
- This is equal to minus one on any twos-complement machine.
Source§impl Int
§Utilities
impl Int
§Utilities
Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Returns true
if this Int
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
Int
for the same reason.
Sourcepub fn is_one(&self) -> bool
pub fn is_one(&self) -> bool
Returns true
if this Int
represents the value one (1
).
§Note
- One (
1
) is also called the multiplicative neutral element. - This operation is more efficient than comparing two instances
of
Int
for the same reason.
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true
if the value of this Int
is positive.
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true
if the value of this Int
is negative.
Sourcepub fn signum(&self) -> i8
pub fn signum(&self) -> i8
Returns a number representing sign of this ApInt
.
0
if the number is zero1
if the number is positive-1
if the number is negative
Source§impl Int
§Comparisons
impl Int
§Comparisons
Sourcepub fn checked_lt(&self, rhs: &Int) -> Result<bool>
pub fn checked_lt(&self, rhs: &Int) -> Result<bool>
Sourcepub fn checked_le(&self, rhs: &Int) -> Result<bool>
pub fn checked_le(&self, rhs: &Int) -> Result<bool>
Sourcepub fn checked_gt(&self, rhs: &Int) -> Result<bool>
pub fn checked_gt(&self, rhs: &Int) -> Result<bool>
Source§impl Int
§To Primitive (Resize)
impl Int
§To Primitive (Resize)
Sourcepub fn resize_to_bool(&self) -> bool
pub fn resize_to_bool(&self) -> bool
Resizes this Int
to a bool
primitive type.
Bits in this Int
that are not within the bounds
of the bool
are being ignored.
§Note
- Basically this returns
true
if the least significant bit of thisInt
is1
andfalse
otherwise.
Sourcepub fn resize_to_i8(&self) -> i8
pub fn resize_to_i8(&self) -> i8
Resizes this Int
to a i8
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_i16(&self) -> i16
pub fn resize_to_i16(&self) -> i16
Resizes this Int
to a i16
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_i32(&self) -> i32
pub fn resize_to_i32(&self) -> i32
Resizes this Int
to a i32
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_i64(&self) -> i64
pub fn resize_to_i64(&self) -> i64
Resizes this Int
to a i64
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_i128(&self) -> i128
pub fn resize_to_i128(&self) -> i128
Resizes this Int
to a i128
primitive type.
§Note
- All bits but the least significant
128
bits are being ignored by this operation to construct the result.
Source§impl Int
§To Primitive (Try-Cast)
impl Int
§To Primitive (Try-Cast)
Sourcepub fn try_to_bool(&self) -> Result<bool>
pub fn try_to_bool(&self) -> Result<bool>
Sourcepub fn try_to_i16(&self) -> Result<i16>
pub fn try_to_i16(&self) -> Result<i16>
Sourcepub fn try_to_i32(&self) -> Result<i32>
pub fn try_to_i32(&self) -> Result<i32>
Sourcepub fn try_to_i64(&self) -> Result<i64>
pub fn try_to_i64(&self) -> Result<i64>
Sourcepub fn try_to_i128(&self) -> Result<i128>
pub fn try_to_i128(&self) -> Result<i128>
Tries to represent the value of this Int
as a i128
.
§Note
- This conversion is possible as long as the value represented
by this
Int
does not exceed the maximum value ofi128
.
§Complexity
- 𝒪(n) where n is the number of digits of this
Int
.
§Errors
- If the value represented by this
Int
can not be represented by ai128
.
Source§impl Int
§Shifts
impl Int
§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 Int
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 thisInt
.
Sourcepub fn into_checked_shl<S>(self, shift_amount: S) -> Result<Int>where
S: Into<ShiftAmount>,
pub fn into_checked_shl<S>(self, shift_amount: S) -> Result<Int>where
S: Into<ShiftAmount>,
Shift this Int
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 thisInt
.
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 Int
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 thisInt
.
Sourcepub fn into_checked_shr<S>(self, shift_amount: S) -> Result<Int>where
S: Into<ShiftAmount>,
pub fn into_checked_shr<S>(self, shift_amount: S) -> Result<Int>where
S: Into<ShiftAmount>,
Right-shifts this Int
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 thisInt
.
Source§impl Int
§Random Utilities using rand
crate.
impl Int
§Random Utilities using rand
crate.
Sourcepub fn random_with_width(width: BitWidth) -> Int
pub fn random_with_width(width: BitWidth) -> Int
Creates a new Int
with the given BitWidth
and random Digit
s.
Sourcepub fn random_with_width_using<R>(width: BitWidth, rng: &mut R) -> Intwhere
R: Rng,
pub fn random_with_width_using<R>(width: BitWidth, rng: &mut R) -> Intwhere
R: Rng,
Creates a new Int
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 Int
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 Int
inplace using the given
random number generator.
This won’t change its BitWidth
.
Source§impl Int
impl Int
Sourcepub fn assign(&mut self, rhs: &Int)
pub fn assign(&mut self, rhs: &Int)
Assigns rhs
to this Int
.
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: &Int) -> Result<()>
pub fn strict_assign(&mut self, rhs: &Int) -> Result<()>
Strictly assigns rhs
to this Int
.
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 Int
§Casting: Truncation & Extension
impl Int
§Casting: Truncation & Extension
Sourcepub fn into_truncate<W>(self, target_width: W) -> Result<Int>
pub fn into_truncate<W>(self, target_width: W) -> Result<Int>
Sourcepub fn truncate<W>(&mut self, target_width: W) -> Result<()>
pub fn truncate<W>(&mut self, target_width: W) -> Result<()>
Tries to truncate this Int
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<Int>
pub fn into_extend<W>(self, target_width: W) -> Result<Int>
Sourcepub fn extend<W>(&mut self, target_width: W) -> Result<()>
pub fn extend<W>(&mut self, target_width: W) -> Result<()>
Tries to extend this Int
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) -> Int
pub fn into_resize<W>(self, target_width: W) -> Int
Source§impl Int
§Bitwise Operations
impl Int
§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: &Int) -> Result<Int>
pub fn into_checked_bitand(self, rhs: &Int) -> Result<Int>
Tries to bit-and assign this Int
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: &Int) -> Result<()>
pub fn checked_bitand_assign(&mut self, rhs: &Int) -> Result<()>
Bit-and assigns all bits of this Int
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: &Int) -> Result<Int>
pub fn into_checked_bitor(self, rhs: &Int) -> Result<Int>
Tries to bit-and assign this Int
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: &Int) -> Result<()>
pub fn checked_bitor_assign(&mut self, rhs: &Int) -> Result<()>
Bit-or assigns all bits of this Int
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: &Int) -> Result<Int>
pub fn into_checked_bitxor(self, rhs: &Int) -> Result<Int>
Tries to bit-xor assign this Int
inplace to rhs
and returns the result.
Note: This forwards to
checked_bitxor
.
§Errors
If self
and rhs
have unmatching bit widths.
Source§impl Int
§Bitwise Access
impl Int
§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 thisInt
.
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 thisInt
.
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 thisInt
.
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 Int
are set.
Sourcepub fn is_all_unset(&self) -> bool
pub fn is_all_unset(&self) -> bool
Returns true
if all bits in this Int
are unset.
Sourcepub fn sign_bit(&self) -> Bit
pub fn sign_bit(&self) -> Bit
Returns the sign bit of this Int
.
Note: This is equal to the most significant bit of this Int
.
Sourcepub fn set_sign_bit(&mut self)
pub fn set_sign_bit(&mut self)
Sets the sign bit of this Int
to one (1
).
Sourcepub fn unset_sign_bit(&mut self)
pub fn unset_sign_bit(&mut self)
Sets the sign bit of this Int
to zero (0
).
Sourcepub fn flip_sign_bit(&mut self)
pub fn flip_sign_bit(&mut self)
Flips the sign bit of this Int
.
§Note
- If the sign bit was
0
it will be1
after this operation and vice versa. - Depending on the interpretation of the
Int
this operation changes its signedness.
Source§impl Int
§Bitwise utility methods.
impl Int
§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 Int
.
Sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Returns the number of zeros in the binary representation of this Int
.
Sourcepub fn leading_zeros(&self) -> usize
pub fn leading_zeros(&self) -> usize
Returns the number of leading zeros in the binary representation of this Int
.
Sourcepub fn trailing_zeros(&self) -> usize
pub fn trailing_zeros(&self) -> usize
Returns the number of trailing zeros in the binary representation of this Int
.
Source§impl Int
§Arithmetic Operations
impl Int
§Arithmetic Operations
Sourcepub fn into_negate(self) -> Int
pub fn into_negate(self) -> Int
Negates this Int
inplace and returns the result.
Note: This will not allocate memory.
Sourcepub fn into_checked_add(self, rhs: &Int) -> Result<Int>
pub fn into_checked_add(self, rhs: &Int) -> Result<Int>
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: &Int) -> Result<()>
pub fn checked_add_assign(&mut self, rhs: &Int) -> 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: &Int) -> Result<Int>
pub fn into_checked_sub(self, rhs: &Int) -> Result<Int>
Sourcepub fn checked_sub_assign(&mut self, rhs: &Int) -> Result<()>
pub fn checked_sub_assign(&mut self, rhs: &Int) -> Result<()>
Sourcepub fn into_checked_mul(self, rhs: &Int) -> Result<Int>
pub fn into_checked_mul(self, rhs: &Int) -> Result<Int>
Sourcepub fn checked_mul_assign(&mut self, rhs: &Int) -> Result<()>
pub fn checked_mul_assign(&mut self, rhs: &Int) -> Result<()>
Sourcepub fn into_checked_div(self, rhs: &Int) -> Result<Int>
pub fn into_checked_div(self, rhs: &Int) -> Result<Int>
Sourcepub fn checked_div_assign(&mut self, rhs: &Int) -> Result<()>
pub fn checked_div_assign(&mut self, rhs: &Int) -> Result<()>
Sourcepub fn into_checked_rem(self, rhs: &Int) -> Result<Int>
pub fn into_checked_rem(self, rhs: &Int) -> Result<Int>
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: &Int) -> Result<()>
pub fn checked_rem_assign(&mut self, rhs: &Int) -> Result<()>
Trait Implementations§
Source§impl<'a> AddAssign<&'a Int> for Int
impl<'a> AddAssign<&'a Int> for Int
Source§fn add_assign(&mut self, rhs: &'a Int)
fn add_assign(&mut self, rhs: &'a Int)
+=
operation. Read moreSource§impl<'a> BitAndAssign<&'a Int> for Int
impl<'a> BitAndAssign<&'a Int> for Int
Source§fn bitand_assign(&mut self, rhs: &'a Int)
fn bitand_assign(&mut self, rhs: &'a Int)
&=
operation. Read moreSource§impl<'a> BitOrAssign<&'a Int> for Int
impl<'a> BitOrAssign<&'a Int> for Int
Source§fn bitor_assign(&mut self, rhs: &'a Int)
fn bitor_assign(&mut self, rhs: &'a Int)
|=
operation. Read moreSource§impl<'a> BitXorAssign<&'a Int> for Int
impl<'a> BitXorAssign<&'a Int> for Int
Source§fn bitxor_assign(&mut self, rhs: &'a Int)
fn bitxor_assign(&mut self, rhs: &'a Int)
^=
operation. Read moreSource§impl<'de> Deserialize<'de> for Int
impl<'de> Deserialize<'de> for Int
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 Int> for Int
impl<'a> DivAssign<&'a Int> for Int
Source§fn div_assign(&mut self, rhs: &'a Int)
fn div_assign(&mut self, rhs: &'a Int)
/=
operation. Read moreSource§impl<'a> MulAssign<&'a Int> for Int
impl<'a> MulAssign<&'a Int> for Int
Source§fn mul_assign(&mut self, rhs: &'a Int)
fn mul_assign(&mut self, rhs: &'a Int)
*=
operation. Read moreSource§impl PartialOrd for Int
impl PartialOrd for Int
Source§impl<'a> RemAssign<&'a Int> for Int
impl<'a> RemAssign<&'a Int> for Int
Source§fn rem_assign(&mut self, rhs: &'a Int)
fn rem_assign(&mut self, rhs: &'a Int)
%=
operation. Read moreSource§impl<S> ShlAssign<S> for Intwhere
S: Into<ShiftAmount>,
impl<S> ShlAssign<S> for Intwhere
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 Intwhere
S: Into<ShiftAmount>,
impl<S> ShrAssign<S> for Intwhere
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 Int> for Int
impl<'a> SubAssign<&'a Int> for Int
Source§fn sub_assign(&mut self, rhs: &'a Int)
fn sub_assign(&mut self, rhs: &'a Int)
-=
operation. Read more