#[repr(transparent)]pub struct ExtAwi { /* private fields */ }
Expand description
An arbitrary width integer with manually controlled bitwidth. Most arithmetic is wrapping like Rust’s integers. All reallocations are explicit.
#![feature(const_mut_refs)]
use awint::{bw, inlawi, Bits, ExtAwi, InlAwi};
const fn example(x0: &mut Bits, x1: &Bits) {
// when dealing with `Bits` with different bitwidths, use the
// `_resize_assign` functions or the concatenations of components
// macros with unbounded fillers from `awint_macros`
x0.sign_resize_assign(x1);
// multiply in place by 2 for an example
x0.short_cin_mul(0, 2);
}
// using `bw` function for quick `NonZeroUsize` construction from a literal
let mut awi0 = ExtAwi::zero(bw(100));
// constructing an `ExtAwi` from an `InlAwi`
let awi1 = ExtAwi::from(inlawi!(-123i16));
// `ExtAwi` implements `DerefMut`, but a mutable `Bits` reference can also
// be acquired like this.
let x0: &mut Bits = awi0.const_as_mut();
assert!(x0.is_zero());
example(x0, awi1.const_as_ref());
assert_eq!(x0, inlawi!(-246i100).const_as_ref());
// you can freely mix references originating from both `ExtAwi` and `InlAwi`
example(x0, inlawi!(0x10u16).const_as_ref());
assert_eq!(awi0, ExtAwi::from(inlawi!(0x20u100)));
Implementations
sourceimpl<'a> ExtAwi
impl<'a> ExtAwi
sourcepub const fn const_as_ref(&'a self) -> &'a Bits
pub const fn const_as_ref(&'a self) -> &'a Bits
Returns a reference to self
in the form of &Bits
sourcepub const fn const_as_mut(&'a mut self) -> &'a mut Bits
pub const fn const_as_mut(&'a mut self) -> &'a mut Bits
Returns a reference to self
in the form of &mut Bits
sourcepub const fn nzbw(&self) -> NonZeroUsize
pub const fn nzbw(&self) -> NonZeroUsize
Returns the bitwidth of this ExtAwi
as a NonZeroUsize
sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the exact number of usize
digits needed to store all bits.
sourcepub fn from_bits(bits: &Bits) -> ExtAwi
pub fn from_bits(bits: &Bits) -> ExtAwi
Creates an ExtAwi
from copying a Bits
reference. The same
functionality is provided by an From<&Bits>
implementation for
ExtAwi
.
sourcepub fn zero(bw: NonZeroUsize) -> Self
pub fn zero(bw: NonZeroUsize) -> Self
Zero-value construction with bitwidth bw
sourcepub fn umax(bw: NonZeroUsize) -> Self
pub fn umax(bw: NonZeroUsize) -> Self
Unsigned-maximum-value construction with bitwidth bw
sourcepub fn imax(bw: NonZeroUsize) -> Self
pub fn imax(bw: NonZeroUsize) -> Self
Signed-maximum-value construction with bitwidth bw
sourcepub fn imin(bw: NonZeroUsize) -> Self
pub fn imin(bw: NonZeroUsize) -> Self
Signed-minimum-value construction with bitwidth bw
sourcepub fn uone(bw: NonZeroUsize) -> Self
pub fn uone(bw: NonZeroUsize) -> Self
Unsigned-one-value construction with bitwidth bw
sourceimpl ExtAwi
impl ExtAwi
sourcepub fn bits_to_vec_radix(
bits: &Bits,
signed: bool,
radix: u8,
upper: bool,
min_chars: usize
) -> Result<Vec<u8>, SerdeError>
pub fn bits_to_vec_radix(
bits: &Bits,
signed: bool,
radix: u8,
upper: bool,
min_chars: usize
) -> Result<Vec<u8>, SerdeError>
Creates a Vec<u8>
representing bits
(sign indicators, prefixes, and
postfixes not included). This function performs allocation. This is
a wrapper around awint_core::Bits::to_bytes_radix that truncates
leading zeros. An additional min_chars
specifies the minimum
number of characters that should exist. min_chars
specifies the
minimum number of chars in the integer part, inserting leading ’0’s if
there are not enough chars, just like Rust’s built in {:0d}
formatting. Note that an empty vector will be returned if
min_chars == 0 && bits.is_zero()
.
Errors
This can only return an error if radix
is not in the range 2..=36 or
if resource exhaustion occurs.
sourcepub fn bits_to_string_radix(
bits: &Bits,
signed: bool,
radix: u8,
upper: bool,
min_chars: usize
) -> Result<String, SerdeError>
pub fn bits_to_string_radix(
bits: &Bits,
signed: bool,
radix: u8,
upper: bool,
min_chars: usize
) -> Result<String, SerdeError>
Creates a string representing bits
. This function performs allocation.
This does the same thing as ExtAwi::bits_to_vec_radix but with a
String
.
sourcepub fn from_bytes_radix(
sign: Option<bool>,
src: &[u8],
radix: u8,
bw: NonZeroUsize
) -> Result<ExtAwi, SerdeError>
pub fn from_bytes_radix(
sign: Option<bool>,
src: &[u8],
radix: u8,
bw: NonZeroUsize
) -> Result<ExtAwi, SerdeError>
Creates an ExtAwi
representing the given arguments. This function
performs allocation. This is a wrapper around
awint_core::Bits::bytes_radix_assign that zero or sign resizes the
result to match bw
.
Errors
See the error conditions of Bits::bytes_radix_assign. Note that -
is
an invalid character even though to_vec_radix
can return -
. This
is because we need to handle both unsigned and signed integer
inputs, specified only by sign
. If the input is a negative signed
integer representation with -
appended to the front, the subslice
src[1..]
can be taken and sign
can be set to Some(true)
.
sourcepub fn from_str_radix(
sign: Option<bool>,
str: &str,
radix: u8,
bw: NonZeroUsize
) -> Result<ExtAwi, SerdeError>
pub fn from_str_radix(
sign: Option<bool>,
str: &str,
radix: u8,
bw: NonZeroUsize
) -> Result<ExtAwi, SerdeError>
Creates an ExtAwi
representing the given arguments. This does the same
thing as ExtAwi::from_bytes_radix but with an &str
.
sourcepub fn from_bytes_general(
sign: Option<bool>,
integer: &[u8],
fraction: &[u8],
exp: isize,
radix: u8,
bw: NonZeroUsize,
fp: isize
) -> Result<ExtAwi, SerdeError>
pub fn from_bytes_general(
sign: Option<bool>,
integer: &[u8],
fraction: &[u8],
exp: isize,
radix: u8,
bw: NonZeroUsize,
fp: isize
) -> Result<ExtAwi, SerdeError>
Creates an ExtAwi
representing the given arguments. This function
performs allocation. In addition to the arguments and semantics from
ExtAwi::from_bytes_radix, this function includes the ability to deal
with general fixed point integer deserialization. src
is now split
into separate integer
and fraction
parts. An exponent exp
further
multiplies the numerical value by radix^exp
. fp
is the location
of the fixed point in the output representation of the numerical
value (e.x. for a plain integer fp == 0
). fp
can be negative or
greater than the bitwidth.
This function uses a single rigorous round-to-even that occurs after the exponent and fixed point multiplier are applied and before any numerical information is lost.
See crate::FP::to_vec_general for the inverse of this function.
Errors
See the error conditions of ExtAwi::from_bytes_radix. The precision can now be arbitrarily large (any overflow in the low numerical significance direction will be rounded), but overflow can still happen in the more significant direction. Empty strings are interpreted as a zero value.
sourcepub fn from_str_general(
sign: Option<bool>,
integer: &str,
fraction: &str,
exp: isize,
radix: u8,
bw: NonZeroUsize,
fp: isize
) -> Result<ExtAwi, SerdeError>
pub fn from_str_general(
sign: Option<bool>,
integer: &str,
fraction: &str,
exp: isize,
radix: u8,
bw: NonZeroUsize,
fp: isize
) -> Result<ExtAwi, SerdeError>
Creates an ExtAwi
representing the given arguments. This does the same
thing as ExtAwi::from_bytes_general but with &str
s.
Methods from Deref<Target = Bits>
sourcepub fn nzbw(&self) -> NonZeroUsize
pub fn nzbw(&self) -> NonZeroUsize
Returns the bitwidth as a NonZeroUsize
sourcepub fn clear_unused_bits(&mut self)
pub fn clear_unused_bits(&mut self)
Clears the unused bits.
sourcepub fn u8_slice_assign(&'a mut self, buf: &[u8])
pub fn u8_slice_assign(&'a mut self, buf: &[u8])
Assigns the bits of buf
to self
. Any bits beyond self.bw()
are
ignored. This function is portable across target architecture pointer
sizes and endianness.
sourcepub fn to_u8_slice(&'a self, buf: &mut [u8])
pub fn to_u8_slice(&'a self, buf: &mut [u8])
Assigns the bits of self
to buf
. Any corresponding bits beyond
self.bw()
are zeroed. This function is portable across target
architecture pointer sizes and endianness.
sourcepub fn zero_assign(&mut self)
pub fn zero_assign(&mut self)
Zero-assigns. Same as the Unsigned-minimum-value. All bits are set to 0.
sourcepub fn umax_assign(&mut self)
pub fn umax_assign(&mut self)
Unsigned-maximum-value-assigns. All bits are set to 1.
sourcepub fn imax_assign(&mut self)
pub fn imax_assign(&mut self)
Signed-maximum-value-assigns. All bits are set to 1, except for the most significant bit.
sourcepub fn imin_assign(&mut self)
pub fn imin_assign(&mut self)
Signed-minimum-value-assigns. Only the most significant bit is set.
sourcepub fn uone_assign(&mut self)
pub fn uone_assign(&mut self)
Unsigned-one-assigns. Only the least significant bit is set. The unsigned distinction is important, because a positive one value does not exist for signed integers with a bitwidth of 1.
sourcepub fn not_assign(&mut self)
pub fn not_assign(&mut self)
Not-assigns self
sourcepub fn copy_assign(&mut self, rhs: &Bits) -> Option<()>
pub fn copy_assign(&mut self, rhs: &Bits) -> Option<()>
Copy-assigns the bits of rhs
to self
sourcepub fn and_assign(&mut self, rhs: &Bits) -> Option<()>
pub fn and_assign(&mut self, rhs: &Bits) -> Option<()>
And-assigns rhs
to self
sourcepub fn xor_assign(&mut self, rhs: &Bits) -> Option<()>
pub fn xor_assign(&mut self, rhs: &Bits) -> Option<()>
Xor-assigns rhs
to self
sourcepub fn range_and_assign(&mut self, range: Range<usize>) -> Option<()>
pub fn range_and_assign(&mut self, range: Range<usize>) -> Option<()>
And-assigns a range of ones to self
. Useful for masking. An empty or
reversed range zeroes self
. None
is returned if range.start > self.bw()
or range.end > self.bw()
.
sourcepub fn usize_or_assign(&mut self, rhs: usize, shl: usize)
pub fn usize_or_assign(&mut self, rhs: usize, shl: usize)
Or-assigns rhs
to self
at a position shl
. Set bits of rhs
that
are shifted beyond the bitwidth of self
are truncated.
sourcepub fn resize_assign(&mut self, rhs: &Bits, extension: bool)
pub fn resize_assign(&mut self, rhs: &Bits, extension: bool)
Resize-copy-assigns rhs
to self
. If self.bw() >= rhs.bw()
, the
copied value of rhs
will be extended with bits set to extension
. If
self.bw() < rhs.bw()
, the copied value of rhs
will be truncated.
sourcepub fn zero_resize_assign(&mut self, rhs: &Bits) -> bool
pub fn zero_resize_assign(&mut self, rhs: &Bits) -> bool
Zero-resize-copy-assigns rhs
to self
and returns overflow. This is
the same as lhs.resize_assign(rhs, false)
, but returns true
if the
unsigned meaning of the integer is changed.
sourcepub fn sign_resize_assign(&mut self, rhs: &Bits) -> bool
pub fn sign_resize_assign(&mut self, rhs: &Bits) -> bool
Sign-resize-copy-assigns rhs
to self
and returns overflow. This is
the same as lhs.resize_assign(rhs, rhs.msb())
, but returns true
if
the signed meaning of the integer is changed.
sourcepub fn ule(&self, rhs: &Bits) -> Option<bool>
pub fn ule(&self, rhs: &Bits) -> Option<bool>
Unsigned-less-than-or-equal comparison, self <= rhs
sourcepub fn uge(&self, rhs: &Bits) -> Option<bool>
pub fn uge(&self, rhs: &Bits) -> Option<bool>
Unsigned-greater-than-or-equal comparison, self >= rhs
sourcepub fn bytes_radix_assign(
&mut self,
sign: Option<bool>,
src: &[u8],
radix: u8,
pad0: &mut Bits,
pad1: &mut Bits
) -> Result<(), SerdeError>
pub fn bytes_radix_assign(
&mut self,
sign: Option<bool>,
src: &[u8],
radix: u8,
pad0: &mut Bits,
pad1: &mut Bits
) -> Result<(), SerdeError>
Assigns to self
the integer value represented by src
in the given
radix
. If src
should be interpreted as unsigned, sign
should be
None
, otherwise it should be set to the sign. In order for this
function to be const
, two scratchpads pad0
and pad1
with the
same bitwidth as self
must be supplied, which can be mutated by
the function in arbitrary ways.
Errors
self
is not mutated if an error occurs. See crate::SerdeError for
error conditions. The characters 0..=9
, a..=z
, and A..=Z
are
allowed depending on the radix. The char _
is ignored, and all
other chars result in an error. src
cannot be empty. The value of
the string must be representable in the bitwidth of self
with the
specified sign, otherwise an overflow error is returned.
sourcepub fn to_bytes_radix(
&self,
signed: bool,
dst: &mut [u8],
radix: u8,
upper: bool,
pad: &mut Bits
) -> Result<(), SerdeError>
pub fn to_bytes_radix(
&self,
signed: bool,
dst: &mut [u8],
radix: u8,
upper: bool,
pad: &mut Bits
) -> Result<(), SerdeError>
Assigns the [u8]
representation of self
to dst
(sign indicators,
prefixes, and postfixes not included). signed
specifies if self
should be interpreted as signed. radix
specifies the radix, and
upper
specifies if letters should be uppercase. In order for this
function to be const
, a scratchpad pad
with the same bitwidth as
self
must be supplied. Note that if dst.len()
is more than what
is needed to store the representation, the leading bytes will all be
set to b’0’.
Errors
Note: If an error is returned, dst
may be set to anything
This function can fail from NonEqualWidths
, InvalidRadix
, and
Overflow
(if dst
cannot represent the value of self
). See
crate::SerdeError.
sourcepub fn short_udivide_inplace_assign(&mut self, div: usize) -> Option<usize>
pub fn short_udivide_inplace_assign(&mut self, div: usize) -> Option<usize>
Unsigned-divides self
by div
, sets self
to the quotient, and
returns the remainder. Returns None
if div == 0
.
pub fn short_udivide_assign(&mut self, duo: &Bits, div: usize) -> Option<usize>
sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Returns the number of set ones
sourcepub fn field(
&mut self,
to: usize,
rhs: &Bits,
from: usize,
width: usize
) -> Option<()>
pub fn field(
&mut self,
to: usize,
rhs: &Bits,
from: usize,
width: usize
) -> Option<()>
“Fielding” bitfields with targeted copy assigns. The bitwidths of self
and rhs
do not have to be equal, but the inputs must collectively obey
width <= self.bw() && width <= rhs.bw() && to <= (self.bw() - width) && from <= (rhs.bw() - width)
or else None
is
returned. width
can be zero, in which case this function just checks
the input correctness and does not mutate self
.
This function works by copying a bw
sized bitfield from rhs
at
bitposition from
and overwriting bw
bits at bitposition to
in
self
. Only the width
bits in self
are mutated, any bits before and
after the bitfield are left unchanged.
use awint::{Bits, inlawi, InlAwi};
// As an example, two hexadecimal digits will be overwritten
// starting with the 12th digit in `y` using a bitfield with
// value 0x42u8 extracted from `x`.
let x = inlawi!(0x11142111u50);
// the underscores are just for emphasis
let mut y = inlawi!(0xfd_ec_ba9876543210u100);
// from `x` digit place 3, we copy 2 digits to `y` digit place 12.
y.field(12 * 4, &x, 3 * 4, 2 * 4);
assert_eq!(y, inlawi!(0xfd_42_ba9876543210u100));
sourcepub fn lut(&mut self, lut: &Bits, inx: &Bits) -> Option<()>
pub fn lut(&mut self, lut: &Bits, inx: &Bits) -> Option<()>
Copy entry from lookup table. Copies a self.bw()
sized bitfield from
lut
at bit position inx.to_usize() * self.bw()
. If lut.bw() != (self.bw() * (2^inx.bw()))
, None
will be returned.
use awint::{Bits, inlawi, InlAwi};
let mut out = inlawi!(0u10);
// lookup table consisting of 4 10-bit entries
let lut = inlawi!(4u10, 3u10, 2u10, 1u10);
// the indexer has to have a bitwidth of 2 to index 2^2 = 4 entries
let mut inx = inlawi!(0u2);
// get the third entry (this is using zero indexing)
inx.usize_assign(2);
out.lut(&lut, &inx).unwrap();
assert_eq!(out, inlawi!(3u10));
sourcepub fn short_cin_mul(&mut self, cin: usize, rhs: usize) -> usize
pub fn short_cin_mul(&mut self, cin: usize, rhs: usize) -> usize
Assigns cin + (self * rhs)
to self
and returns the overflow
sourcepub fn short_mul_add_assign(&mut self, lhs: &Bits, rhs: usize) -> Option<bool>
pub fn short_mul_add_assign(&mut self, lhs: &Bits, rhs: usize) -> Option<bool>
Add-assigns lhs * rhs
to self
and returns if overflow happened
sourcepub fn mul_add_assign(&mut self, lhs: &Bits, rhs: &Bits) -> Option<()>
pub fn mul_add_assign(&mut self, lhs: &Bits, rhs: &Bits) -> Option<()>
Multiplies lhs
by rhs
and add-assigns the product to self
. Three
operands eliminates the need for an allocating temporary.
sourcepub fn mul_assign(&mut self, rhs: &Bits, pad: &mut Bits) -> Option<()>
pub fn mul_assign(&mut self, rhs: &Bits, pad: &mut Bits) -> Option<()>
Multiply-assigns self
by rhs
. pad
is a scratchpad that will be
mutated arbitrarily.
sourcepub fn arb_umul_add_assign(&mut self, lhs: &Bits, rhs: &Bits)
pub fn arb_umul_add_assign(&mut self, lhs: &Bits, rhs: &Bits)
Arbitrarily-unsigned-multiplies lhs
by rhs
and add-assigns the
product to self
. This function is equivalent to:
use awint::prelude::*;
fn arb_umul_assign(add: &mut Bits, lhs: &Bits, rhs: &Bits) {
let mut resized_lhs = ExtAwi::zero(add.nzbw());
// Note that this function is specified as unsigned,
// because we use `zero_resize_assign`
resized_lhs.zero_resize_assign(lhs);
let mut resized_rhs = ExtAwi::zero(add.nzbw());
resized_rhs.zero_resize_assign(rhs);
add.mul_add_assign(&resized_lhs, &resized_rhs).unwrap();
}
except that it avoids allocation and is more efficient overall
sourcepub fn arb_imul_add_assign(&mut self, lhs: &mut Bits, rhs: &mut Bits)
pub fn arb_imul_add_assign(&mut self, lhs: &mut Bits, rhs: &mut Bits)
Arbitrarily-signed-multiplies lhs
by rhs
and add-assigns the product
to self
. duo
and div
are marked mutable but their values are
not changed by this function.
sourcepub fn shl_assign(&mut self, s: usize) -> Option<()>
pub fn shl_assign(&mut self, s: usize) -> Option<()>
Left-shifts-assigns by s
bits. If s >= self.bw()
, then
None
is returned and the Bits
are left unchanged.
Left shifts can act as a very fast multiplication by a power of two for
both the signed and unsigned interpretation of Bits
.
sourcepub fn lshr_assign(&mut self, s: usize) -> Option<()>
pub fn lshr_assign(&mut self, s: usize) -> Option<()>
Logically-right-shift-assigns by s
bits. If s >= self.bw()
, then
None
is returned and the Bits
are left unchanged.
Logical right shifts do not copy the sign bit, and thus can act as a
very fast floored division by a power of two for the unsigned
interpretation of Bits
.
sourcepub fn ashr_assign(&mut self, s: usize) -> Option<()>
pub fn ashr_assign(&mut self, s: usize) -> Option<()>
Arithmetically-right-shift-assigns by s
bits. If s >= self.bw()
,
then None
is returned and the Bits
are left unchanged.
Arithmetic right shifts copy the sign bit, and thus can act as a very
fast floored division by a power of two for the signed interpretation
of Bits
.
sourcepub fn rotl_assign(&mut self, s: usize) -> Option<()>
pub fn rotl_assign(&mut self, s: usize) -> Option<()>
Left-rotate-assigns by s
bits. If s >= self.bw()
, then
None
is returned and the Bits
are left unchanged.
This function is equivalent to the following:
use awint::{extawi, inlawi, Bits, ExtAwi, InlAwi};
let mut input = inlawi!(0x4321u16);
let mut output = inlawi!(0u16);
// rotate left by 4 bits or one hexadecimal digit
let shift = 4;
output.copy_assign(&input).unwrap();
// temporary clone of the input
let mut tmp = ExtAwi::from(input);
if shift != 0 {
if shift >= input.bw() {
panic!();
}
output.shl_assign(shift).unwrap();
tmp.lshr_assign(input.bw() - shift).unwrap();
output.or_assign(&tmp);
};
assert_eq!(output, inlawi!(0x3214u16));
let mut using_rotate = ExtAwi::from(input);
using_rotate.rotl_assign(shift).unwrap();
assert_eq!(using_rotate, extawi!(0x3214u16));
// Note that slices are typed in a little-endian order opposite of
// how integers are typed, but they still visually rotate in the
// same way. This means `Rust`s built in slice rotation is in the
// opposite direction to integers and `Bits`
let mut array = [4, 3, 2, 1];
array.rotate_left(1);
assert_eq!(array, [3, 2, 1, 4]);
assert_eq!(0x4321u16.rotate_left(4), 0x3214);
let mut x = inlawi!(0x4321u16);
x.rotl_assign(4);
// `Bits` has the preferred endianness
assert_eq!(x, inlawi!(0x3214u16));
Unlike the example above which needs cloning, this function avoids any allocation and has many optimized branches for different input sizes and shifts.
sourcepub fn rotr_assign(&mut self, s: usize) -> Option<()>
pub fn rotr_assign(&mut self, s: usize) -> Option<()>
Right-rotate-assigns by s
bits. If s >= self.bw()
, then
None
is returned and the Bits
are left unchanged.
See Bits::rotl_assign
for more details.
sourcepub fn rev_assign(&mut self)
pub fn rev_assign(&mut self)
Reverse-bit-order-assigns self
. The least significant bit becomes the
most significant bit, the second least significant bit becomes the
second most significant bit, etc.
sourcepub fn funnel(&mut self, rhs: &Bits, s: &Bits) -> Option<()>
pub fn funnel(&mut self, rhs: &Bits, s: &Bits) -> Option<()>
Funnel shift with power-of-two bitwidths. Returns None
if
2*self.bw() != rhs.bw() || 2^s.bw() != self.bw()
. A self.bw()
sized
field is assigned to self
from rhs
starting from the bit position
s
. The shift cannot overflow because of the restriction on the
bitwidth of s
.
pub fn usize_assign(&mut self, x: usize)
pub fn isize_assign(&mut self, x: isize)
pub fn u8_assign(&mut self, x: u8)
pub fn i8_assign(&mut self, x: i8)
pub fn u16_assign(&mut self, x: u16)
pub fn i16_assign(&mut self, x: i16)
pub fn u32_assign(&mut self, x: u32)
pub fn i32_assign(&mut self, x: i32)
pub fn u64_assign(&mut self, x: u64)
pub fn i64_assign(&mut self, x: i64)
pub fn u128_assign(&mut self, x: u128)
pub fn i128_assign(&mut self, x: i128)
pub fn bool_assign(&mut self, x: bool)
pub fn to_usize(&self) -> usize
pub fn to_isize(&self) -> isize
pub fn to_u8(&self) -> u8
pub fn to_i8(&self) -> i8
pub fn to_u16(&self) -> u16
pub fn to_i16(&self) -> i16
pub fn to_u32(&self) -> u32
pub fn to_i32(&self) -> i32
pub fn to_u64(&self) -> u64
pub fn to_i64(&self) -> i64
pub fn to_u128(&self) -> u128
pub fn to_i128(&self) -> i128
pub fn to_bool(&self) -> bool
sourcepub fn inc_assign(&mut self, cin: bool) -> bool
pub fn inc_assign(&mut self, cin: bool) -> bool
Increment-assigns self
with a carry-in cin
and returns the carry-out
bit. If cin == true
then one is added to self
, otherwise nothing
happens. false
is always returned unless self.is_umax()
.
sourcepub fn dec_assign(&mut self, cin: bool) -> bool
pub fn dec_assign(&mut self, cin: bool) -> bool
Decrement-assigns self
with a carry-in cin
and returns the carry-out
bit. If cin == false
then one is subtracted from self
, otherwise
nothing happens. true
is always returned unless self.is_zero()
.
sourcepub fn neg_assign(&mut self, neg: bool)
pub fn neg_assign(&mut self, neg: bool)
Negate-assigns self
if neg
is true. Note that signed minimum values
will overflow.
sourcepub fn abs_assign(&mut self)
pub fn abs_assign(&mut self)
Absolute-value-assigns self
. Note that signed minimum values will
overflow, unless self
is interpreted as unsigned after a call to this
function.
sourcepub fn add_assign(&mut self, rhs: &Bits) -> Option<()>
pub fn add_assign(&mut self, rhs: &Bits) -> Option<()>
Add-assigns by rhs
sourcepub fn sub_assign(&mut self, rhs: &Bits) -> Option<()>
pub fn sub_assign(&mut self, rhs: &Bits) -> Option<()>
Subtract-assigns by rhs
sourcepub fn rsb_assign(&mut self, rhs: &Bits) -> Option<()>
pub fn rsb_assign(&mut self, rhs: &Bits) -> Option<()>
Reverse-subtract-assigns by rhs
. Sets self
to (-self) + rhs
.
sourcepub fn neg_add_assign(&mut self, neg: bool, rhs: &Bits) -> Option<()>
pub fn neg_add_assign(&mut self, neg: bool, rhs: &Bits) -> Option<()>
Negate-add-assigns by rhs
. Negates conditionally on neg
.
sourcepub fn cin_sum_assign(
&mut self,
cin: bool,
lhs: &Bits,
rhs: &Bits
) -> Option<(bool, bool)>
pub fn cin_sum_assign(
&mut self,
cin: bool,
lhs: &Bits,
rhs: &Bits
) -> Option<(bool, bool)>
A general summation with carry-in cin
and two inputs lhs
and rhs
.
self
is set to the sum. The unsigned overflow (equivalent to the
carry-out bit) and the signed overflow is returned as a tuple. None
is
returned if any bitwidths do not match. If subtraction is desired,
one of the operands can be negated.
Trait Implementations
sourceimpl BorrowMut<Bits> for ExtAwi
impl BorrowMut<Bits> for ExtAwi
sourcefn borrow_mut(&mut self) -> &mut Bits
fn borrow_mut(&mut self) -> &mut Bits
Mutably borrows from an owned value. Read more
sourceimpl<const BW: usize, const LEN: usize> From<InlAwi<BW, LEN>> for ExtAwi
impl<const BW: usize, const LEN: usize> From<InlAwi<BW, LEN>> for ExtAwi
Creates an ExtAwi
from copying an InlAwi
sourceimpl FromStr for ExtAwi
impl FromStr for ExtAwi
sourcefn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Creates an ExtAwi
described by s
. There are two modes of operation
which use ExtAwi::from_str_radix differently.
In general mode, the bitwidth must be specified after a ‘u’ (unsigned)
or ‘i’ (signed) suffix. A prefix of “0b” specifies a binary radix, “0o”
specifies an octal radix, “0x” specifies hexadecimal, else decimal.
For some examples, “42u10” entered into this function creates an
ExtAwi
with bitwidth 10 and unsigned value 42. “-42i10” results in
bitwidth 10 and signed value of -42. “0xffff_ffffu32” results in
bitwidth 32 and an unsigned value of 0xffffffff (also 4294967295 in
decimal and u32::MAX). “0x1_0000_0000u32” results in an error with
SerdeError::Overflow
, because it exceeds the maximum unsigned
value for a 32 bit integer. “123” results in
SerdeError::InvalidChar
, because no bitwidth suffix
has been supplied and this function has assumed binary mode, in which
‘2’ and ‘3’ are invalid chars.
If no ‘u’ or ‘i’ chars are present, this function will use binary mode
and assume the input is a radix 2 string with only the chars ‘0’ and
‘1’. In this mode, the bitwidth will be equal to the number of
chars, including leading zeros. For some examples, 42 in binary is
101010. If “101010” is entered into this function, it will return an
ExtAwi
with bitwidth 6 and unsigned value 42. “0000101010” results
in bitwidth 10 and unsigned value 42. “1111_1111” results in bitwidth
8 and signed value -128 or equivalently unsigned value 255.
A missing significand or suffix will result in SerdeError::Empty
. Even
if the value is zero, there must be at least one ‘0’ char in the
significand (e.x. 0x0u8
not 0xu8
), otherwise SerdeError::Empty
is
returned.
type Err = SerdeError
type Err = SerdeError
The associated error which can be returned from parsing.
sourceimpl PartialEq<ExtAwi> for ExtAwi
impl PartialEq<ExtAwi> for ExtAwi
If self
and other
have unmatching bit widths, false
will be returned.
impl Eq for ExtAwi
If self
and other
have unmatching bit widths, false
will be returned.
impl Send for ExtAwi
ExtAwi
is safe to send between threads since it does not own
aliasing memory and has no reference counting mechanism like Rc
.
impl Sync for ExtAwi
ExtAwi
is safe to share between threads since it does not own
aliasing memory and has no mutable internal state like Cell
or RefCell
.
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more