pub struct FP<B>{ /* private fields */ }
Expand description
Fixed-Point generic struct for B
that implement Borrow<Bits>
and
BorrowMut<Bits>
. Adds on signedness and fixed-point information.
Implements many traits if B
also implements them.
In order to make many operations infallible, self.fp().unsigned_abs()
and
self.bw()
follow an invariant that they are never greater than
usize::MAX >> 2
.
NOTE: B
should not change the bitwidth of the underlying Bits
during the
lifetime of the FP
struct unless the invariants are upheld. Otherwise,
panics and arithmetic errors can occur. Preferably, into_b
and FP::new
should be used to create a fresh struct.
Implementations§
source§impl<B> FP<B>
impl<B> FP<B>
sourcepub fn new(signed: bool, bits: B, fp: isize) -> Option<FP<B>>
pub fn new(signed: bool, bits: B, fp: isize) -> Option<FP<B>>
Creates a fixed-point generic FP<B>
from a specified signedness
signed
, wrapped value B
, and fixed point fp
. This returns None
if bits.bw()
or fp.unsigned_abs()
are greater than
usize::MAX >> 2
.
sourcepub fn sign(&self) -> Option<bool>
pub fn sign(&self) -> Option<bool>
Returns the sign of self
, returning Some(self.msb())
if self.signed()
, and None
otherwise.
sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns if self.signed() && self.msb()
source§impl<B> FP<B>
impl<B> FP<B>
sourcepub fn f32_(this: &mut FP<B>, f: f32)
pub fn f32_(this: &mut FP<B>, f: f32)
Floating-assigns FP::from_f32(f)
to this
. Note that this modifies
this.fp
according to floating_.
sourcepub fn checked_f32_(this: &mut FP<B>, f: f32) -> Option<()>
pub fn checked_f32_(this: &mut FP<B>, f: f32) -> Option<()>
The same as f32_ except None
is returned if f
is an
infinity or NaN
sourcepub fn f64_(this: &mut FP<B>, f: f64)
pub fn f64_(this: &mut FP<B>, f: f64)
Floating-assigns FP::from_f64(f)
to this
. Note that this modifies
this.fp
according to floating_.
sourcepub fn checked_f64_(this: &mut FP<B>, f: f64) -> Option<()>
pub fn checked_f64_(this: &mut FP<B>, f: f64) -> Option<()>
The same as f64_ except None
is returned if f
is an
infinity or NaN
sourcepub fn try_to_f32(this: &mut FP<B>) -> Option<f32>
pub fn try_to_f32(this: &mut FP<B>) -> Option<f32>
Translates this
to its IEEE-754 32 bit floating point value, using
truncation rounding. Infinities and NaN are never returned. If the
significant numerical value would be unrepresentable (i.e. the most
significant numerical bit is 2^128 or greater), None
is returned.
sourcepub fn try_to_f64(this: &mut FP<B>) -> Option<f64>
pub fn try_to_f64(this: &mut FP<B>) -> Option<f64>
Translates this
to its IEEE-754 64 bit floating point value, using
truncation rounding. Infinities and NaN are never returned. If the
significant numerical value would be unrepresentable (i.e. the most
significant numerical bit is 2^1024 or greater), None
is returned.
source§impl<B> FP<B>
impl<B> FP<B>
These functions are associated to avoid name clashes.
Note: Adding new functions to FP
is a WIP
sourcepub fn one_(this: &mut FP<B>) -> Option<()>
pub fn one_(this: &mut FP<B>) -> Option<()>
One-assigns this
. Returns None
if a positive one value is not
representable.
sourcepub fn rel_sb(this: &FP<B>) -> (isize, isize)
pub fn rel_sb(this: &FP<B>) -> (isize, isize)
Relative significant bit positions, determines the bit positions (inclusive) of the least and most significant bits relative to the fixed point
Note: because the msb position is one less than the bitwidth, the bitwidth is equal to the difference in the bounds plus one
sourcepub fn utruncate_<C>(this: &mut FP<B>, rhs: &FP<C>)
pub fn utruncate_<C>(this: &mut FP<B>, rhs: &FP<C>)
The same as FP::truncate_ except it always intreprets arguments as unsigned
sourcepub fn truncate_<C>(this: &mut FP<B>, rhs: &mut FP<C>)
pub fn truncate_<C>(this: &mut FP<B>, rhs: &mut FP<C>)
Truncate-assigns rhs
to this
. For the unsigned case, logically what
this does is make this
and rhs
into concatenations with infinite
zeros on both ends, aligns the fixed points, and copies from rhs
to this
. For the case of rhs.signed()
, the absolute value of
rhs
is used for truncation to this
followed by
this.neg_(rhs.msb() && this.signed())
.
sourcepub fn outruncate_<C>(this: &mut FP<B>, rhs: &FP<C>) -> (bool, bool)
pub fn outruncate_<C>(this: &mut FP<B>, rhs: &FP<C>) -> (bool, bool)
The same as FP::otruncate_ except it always intreprets arguments as unsigned
sourcepub fn otruncate_<C>(this: &mut FP<B>, rhs: &mut FP<C>) -> (bool, bool)
pub fn otruncate_<C>(this: &mut FP<B>, rhs: &mut FP<C>) -> (bool, bool)
Overflow-truncate-assigns rhs
to this
. The same as
FP::truncate_, except that a tuple of booleans is returned. The
first indicates if the least significant numerical bit was truncated,
and the second indicates if the most significant numerical bit was
truncated. Additionally, if this.is_negative() != rhs.is_negative()
,
the second overflow is set.
What this means is that if transitive truncations return no overflow,
then numerical value is preserved. If only FP::otruncate_(...).0
is true, then less significant numerical values were changed and only
some kind of truncation rounding has occured to the numerical value. If
FP::otruncate_(...).1
is true, then the numerical value could be
dramatically changed.
sourcepub fn floating_<C>(this: &mut FP<B>, rhs: &mut FP<C>) -> Option<()>
pub fn floating_<C>(this: &mut FP<B>, rhs: &mut FP<C>) -> Option<()>
Floating-assigns rhs
to this
. This modifies the fp
of this
to
retain as much significant numerical precision as possible. If
this.signed()
, the msnb (most significant numerical bit) is moved to
the second msb of this
. Otherwise, the msnb is moved to the msb of
this
. If rhs.is_negative()
and this
is not signed, the absolute
value of rhs
is used. If rhs.is_zero()
, this
and its fp
are
zeroed. Returns None
if the fixed point invariant would be
violated.
sourcepub fn to_vec_general(
this: &FP<B>,
radix: u8,
upper: bool,
min_integer_chars: usize,
min_fraction_chars: usize,
max_ufp: usize
) -> Result<(Vec<u8>, Vec<u8>), SerdeError>
pub fn to_vec_general( this: &FP<B>, radix: u8, upper: bool, min_integer_chars: usize, min_fraction_chars: usize, max_ufp: usize ) -> Result<(Vec<u8>, Vec<u8>), SerdeError>
Creates a tuple of Vec<u8>
s representing the integer and fraction
parts this
(sign indicators, prefixes, points, and postfixes not
included). This function performs allocation. This is the inverse of
Awi::from_bytes_general and extends the abilities of
Awi::bits_to_vec_radix. Signedness and fixed point position
information is taken from this
. min_integer_chars
specifies the
minimum number of chars in the integer part, inserting leading ’0’s if
there are not enough chars. min_fraction_chars
works likewise for the
fraction part, inserting trailing ’0’s. For max_ufp
see the errors
section.
use awint::awi::*;
// note: a user may want to define their own helper functions to do
// this in one step and combine the output into one string using
// the notation they prefer.
// This creates a fixed point value of -42.1234_i32f16 (see `Awi::from_str`)
let val = Awi::from_str_general(Some(true), "42", "1234", 0, 10, bw(32), 16).unwrap();
let fp_awi = FP::new(true, val, 16).unwrap();
assert_eq!(
// note: in many situations users will want at least 1 zero for
// both parts so that zero parts result in "0" strings and not "",
// so `min_..._chars` will be 1. See also
// `FPType::unique_min_fraction_digits`.
FP::to_str_general(&fp_awi, 10, false, 1, 1, 4096),
Ok(("42".to_owned(), "1234".to_owned()))
);
§Errors
Because it would be trivial to cause resource exhaustion with extremely
large fixed points (the bitwidth is limited roughly by what it takes to
allocate this.b()
in the first place, but the fixed point can easily
be set to huge positive or negative values to result in extremely
long Vec<u8>
s and internal calculations), for practical reasons we
need a built in failsafe that triggers if
this.fp().unsigned_abs() > max_ufp
. If so, Overflow
is returned.
This can only return an error if radix
is not in the range 2..=36 or
if resource exhaustion occurs.
sourcepub fn to_str_general(
this: &FP<B>,
radix: u8,
upper: bool,
min_integer_chars: usize,
min_fraction_chars: usize,
max_ufp: usize
) -> Result<(String, String), SerdeError>
pub fn to_str_general( this: &FP<B>, radix: u8, upper: bool, min_integer_chars: usize, min_fraction_chars: usize, max_ufp: usize ) -> Result<(String, String), SerdeError>
Creates a tuple of String
s representing the integer and fraction
parts of this
. This does the same thing as FP::to_vec_general
but with String
s.
Methods from Deref<Target = Bits>§
sourcepub fn u8_slice_(&'a mut self, buf: &[u8])
pub fn u8_slice_(&'a mut self, buf: &[u8])
Assigns the bits of buf
to self
. If (buf.len() * 8) > self.bw()
then the corresponding bits in buf
beyond self.bw()
are ignored. If
(buf.len() * 8) < self.bw()
then the rest of the bits in self
are
zeroed. 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
. If (buf.len() * 8) > self.bw()
then the corresponding bits in buf
beyond self.bw()
are zeroed. If
(buf.len() * 8) < self.bw()
then the bits of self
beyond the buffer
do nothing. This function is portable across target architecture
pointer sizes and endianness.
sourcepub fn zero_(&mut self)
pub fn zero_(&mut self)
Zero-assigns. Same as the Unsigned-minimum-value. All bits are set to 0.
sourcepub fn imax_(&mut self)
pub fn imax_(&mut self)
Signed-maximum-value-assigns. All bits are set to 1, except for the most significant bit.
sourcepub fn uone_(&mut self)
pub fn uone_(&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 range_or_(&mut self, range: Range<usize>) -> Option<()>
pub fn range_or_(&mut self, range: Range<usize>) -> Option<()>
Or-assigns a range of ones to self
. An empty or reversed range does
nothing to self
. None
is returned if range.start > self.bw()
or range.end > self.bw()
.
sourcepub fn range_and_(&mut self, range: Range<usize>) -> Option<()>
pub fn range_and_(&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 range_xor_(&mut self, range: Range<usize>) -> Option<()>
pub fn range_xor_(&mut self, range: Range<usize>) -> Option<()>
Xor-assigns a range of ones to self
. An empty or reversed range does
nothing to self
. None
is returned if range.start > self.bw()
or range.end > self.bw()
.
sourcepub fn digit_or_(&mut self, rhs: usize, shl: usize)
pub fn digit_or_(&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_(&mut self, rhs: &Bits, extension: bool)
pub fn resize_(&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_(&mut self, rhs: &Bits) -> bool
pub fn zero_resize_(&mut self, rhs: &Bits) -> bool
Zero-resize-copy-assigns rhs
to self
and returns overflow. This is
the same as lhs.resize_(rhs, false)
, but returns true
if the
unsigned meaning of the integer is changed.
sourcepub fn sign_resize_(&mut self, rhs: &Bits) -> bool
pub fn sign_resize_(&mut self, rhs: &Bits) -> bool
Sign-resize-copy-assigns rhs
to self
and returns overflow. This is
the same as lhs.resize_(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 ile(&self, rhs: &Bits) -> Option<bool>
pub fn ile(&self, rhs: &Bits) -> Option<bool>
Signed-less-than-or-equal comparison, self <= rhs
sourcepub fn ige(&self, rhs: &Bits) -> Option<bool>
pub fn ige(&self, rhs: &Bits) -> Option<bool>
Signed-greater-than-or-equal comparison, self >= rhs
sourcepub fn total_cmp(&self, rhs: &Bits) -> Ordering
pub fn total_cmp(&self, rhs: &Bits) -> Ordering
Total ordering over bitstrings, including differentiation between
differing bitwidths of self
and rhs
. This orders first on bitwidth
and then on unsigned value.
sourcepub fn bytes_radix_(
&mut self,
sign: Option<bool>,
src: &[u8],
radix: u8,
pad0: &mut Bits,
pad1: &mut Bits
) -> Result<(), SerdeError>
pub fn bytes_radix_( &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 digit_udivide_inplace_(&mut self, div: usize) -> Option<usize>
pub fn digit_udivide_inplace_(&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 digit_udivide_(&mut self, duo: &Bits, div: usize) -> Option<usize>
sourcepub fn get(&self, inx: usize) -> Option<bool>
pub fn get(&self, inx: usize) -> Option<bool>
Gets the bit at inx
bits from the least significant bit, returning
None
if inx >= self.bw()
sourcepub fn set(&mut self, inx: usize, bit: bool) -> Option<()>
pub fn set(&mut self, inx: usize, bit: bool) -> Option<()>
Sets the bit at inx
bits from the least significant bit, returning
None
if inx >= self.bw()
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 width
sized bitfield from rhs
at
bitposition from
and overwriting width
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::{inlawi, Bits, 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 field_to(&mut self, to: usize, rhs: &Bits, width: usize) -> Option<()>
pub fn field_to(&mut self, to: usize, rhs: &Bits, width: usize) -> Option<()>
A specialization of Bits::field with from
set to 0.
sourcepub fn field_from(
&mut self,
rhs: &Bits,
from: usize,
width: usize
) -> Option<()>
pub fn field_from( &mut self, rhs: &Bits, from: usize, width: usize ) -> Option<()>
A specialization of Bits::field with to
set to 0.
sourcepub fn field_width(&mut self, rhs: &Bits, width: usize) -> Option<()>
pub fn field_width(&mut self, rhs: &Bits, width: usize) -> Option<()>
A specialization of Bits::field with to
and from
set to 0.
sourcepub fn field_bit(&mut self, to: usize, rhs: &Bits, from: usize) -> Option<()>
pub fn field_bit(&mut self, to: usize, rhs: &Bits, from: usize) -> Option<()>
A specialization of Bits::field with width
set to 1.
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::{inlawi, Bits, 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_(2);
out.lut_(&lut, &inx).unwrap();
assert_eq!(out, inlawi!(3u10));
sourcepub fn lut_set(&mut self, entry: &Bits, inx: &Bits) -> Option<()>
pub fn lut_set(&mut self, entry: &Bits, inx: &Bits) -> Option<()>
Set entry in lookup table. The inverse of Bits::lut_, this uses
entry
as a bitfield to overwrite part of self
at bit position
inx.to_usize() * entry.bw()
. If
self.bw() != (entry.bw() * (2^inx.bw()))
, None
will be returned.
sourcepub fn mux_(&mut self, rhs: &Bits, b: bool) -> Option<()>
pub fn mux_(&mut self, rhs: &Bits, b: bool) -> Option<()>
Multiplex by conditionally copy-assigning rhs
to self
if b
sourcepub fn repeat_(&mut self, rhs: &Bits)
pub fn repeat_(&mut self, rhs: &Bits)
Repeat-assigns self
by rhs
. This is logically equivalent to
concatenating an infinite number of rhs
bit strings together, then
resize-assigning to self
.
sourcepub fn digit_cin_mul_(&mut self, cin: usize, rhs: usize) -> usize
pub fn digit_cin_mul_(&mut self, cin: usize, rhs: usize) -> usize
Assigns cin + (self * rhs)
to self
and returns the overflow
sourcepub fn digit_mul_add_(&mut self, lhs: &Bits, rhs: usize) -> Option<bool>
pub fn digit_mul_add_(&mut self, lhs: &Bits, rhs: usize) -> Option<bool>
Add-assigns lhs * rhs
to self
and returns if overflow happened
sourcepub fn mul_add_(&mut self, lhs: &Bits, rhs: &Bits) -> Option<()>
pub fn mul_add_(&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_(&mut self, rhs: &Bits, pad: &mut Bits) -> Option<()>
pub fn mul_(&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_(&mut self, lhs: &Bits, rhs: &Bits)
pub fn arb_umul_add_(&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::awi::*;
fn arb_umul_(add: &mut Bits, lhs: &Bits, rhs: &Bits) {
let mut resized_lhs = Awi::zero(add.nzbw());
// Note that this function is specified as unsigned,
// because we use `zero_resize_`
resized_lhs.zero_resize_(lhs);
let mut resized_rhs = Awi::zero(add.nzbw());
resized_rhs.zero_resize_(rhs);
add.mul_add_(&resized_lhs, &resized_rhs).unwrap();
}
except that it avoids allocation and is more efficient overall
sourcepub fn arb_imul_add_(&mut self, lhs: &mut Bits, rhs: &mut Bits)
pub fn arb_imul_add_(&mut self, lhs: &mut Bits, rhs: &mut Bits)
Arbitrarily-signed-multiplies lhs
by rhs
and add-assigns the product
to self
. Has the same behavior as Bits::arb_umul_add_ except that is
interprets the arguments as signed. lhs
and rhs
are marked
mutable but their values are not changed by this function.
sourcepub fn shl_(&mut self, s: usize) -> Option<()>
pub fn shl_(&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_(&mut self, s: usize) -> Option<()>
pub fn lshr_(&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_(&mut self, s: usize) -> Option<()>
pub fn ashr_(&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_(&mut self, s: usize) -> Option<()>
pub fn rotl_(&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::awi::*;
let mut input = inlawi!(0x4321u16);
let mut output = inlawi!(0u16);
// rotate left by 4 bits or one hexadecimal digit
let shift = 4;
// temporary clone of the input
let mut tmp = Awi::from(input);
cc!(input; output).unwrap();
if shift != 0 {
if shift >= input.bw() {
// the actual function would return `None`
panic!();
}
output.shl_(shift).unwrap();
tmp.lshr_(input.bw() - shift).unwrap();
output.or_(&tmp);
};
assert_eq!(output, inlawi!(0x3214u16));
let mut using_rotate = Awi::from(input);
using_rotate.rotl_(shift).unwrap();
assert_eq!(using_rotate, awi!(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_(4).unwrap();
// `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_(&mut self, s: usize) -> Option<()>
pub fn rotr_(&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_ for more details.
sourcepub fn rev_(&mut self)
pub fn rev_(&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
.
use awint::awi::*;
let mut lhs = inlawi!(0xffff_ffffu32);
let mut rhs = inlawi!(0xfedc_ba98_7654_3210u64);
// `lhs.bw()` must be a power of two, `s.bw()` here is
// `log_2(32) == 5`. The value of `s` is set to what bit
// of `rhs` should be the starting bit for `lhs`.
let mut s = inlawi!(12u5);
lhs.funnel_(&rhs, &s).unwrap();
assert_eq!(lhs, inlawi!(0xa9876543_u32))
pub fn u8_(&mut self, x: u8)
pub fn i8_(&mut self, x: i8)
pub fn u16_(&mut self, x: u16)
pub fn i16_(&mut self, x: i16)
pub fn u32_(&mut self, x: u32)
pub fn i32_(&mut self, x: i32)
pub fn u64_(&mut self, x: u64)
pub fn i64_(&mut self, x: i64)
pub fn u128_(&mut self, x: u128)
pub fn i128_(&mut self, x: i128)
pub fn usize_(&mut self, x: usize)
pub fn isize_(&mut self, x: isize)
pub fn bool_(&mut self, x: bool)
pub fn digit_(&mut self, x: usize)
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_usize(&self) -> usize
pub fn to_isize(&self) -> isize
pub fn to_bool(&self) -> bool
pub fn to_digit(&self) -> usize
sourcepub fn inc_(&mut self, cin: bool) -> bool
pub fn inc_(&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_(&mut self, cin: bool) -> bool
pub fn dec_(&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_(&mut self, neg: bool)
pub fn neg_(&mut self, neg: bool)
Negate-assigns self
if neg
is true. Note that signed minimum values
will overflow.
sourcepub fn abs_(&mut self)
pub fn abs_(&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 rsb_(&mut self, rhs: &Bits) -> Option<()>
pub fn rsb_(&mut self, rhs: &Bits) -> Option<()>
Reverse-subtract-assigns by rhs
. Sets self
to (-self) + rhs
.
sourcepub fn neg_add_(&mut self, neg: bool, rhs: &Bits) -> Option<()>
pub fn neg_add_(&mut self, neg: bool, rhs: &Bits) -> Option<()>
Negate-add-assigns by rhs
. Negates conditionally on neg
.
sourcepub fn cin_sum_(
&mut self,
cin: bool,
lhs: &Bits,
rhs: &Bits
) -> Option<(bool, bool)>
pub fn cin_sum_( &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§
source§impl<B> Binary for FP<B>
impl<B> Binary for FP<B>
Note: max_ufp
for the internal FP::to_str_general call
is set to 4096, if it results in an overflow then the
formatting is a no-op rather than causing format!
to panic.
source§impl<B> Debug for FP<B>
impl<B> Debug for FP<B>
Note: max_ufp
for the internal FP::to_str_general call
is set to 4096, if it results in an overflow then the
formatting is a no-op rather than causing format!
to panic.
source§impl<B> Display for FP<B>
impl<B> Display for FP<B>
Note: max_ufp
for the internal FP::to_str_general call
is set to 4096, if it results in an overflow then the
formatting is a no-op rather than causing format!
to panic.
source§impl<B> Hash for FP<B>
impl<B> Hash for FP<B>
source§impl<B> LowerHex for FP<B>
impl<B> LowerHex for FP<B>
Note: max_ufp
for the internal FP::to_str_general call
is set to 4096, if it results in an overflow then the
formatting is a no-op rather than causing format!
to panic.
source§impl<B> Octal for FP<B>
impl<B> Octal for FP<B>
Note: max_ufp
for the internal FP::to_str_general call
is set to 4096, if it results in an overflow then the
formatting is a no-op rather than causing format!
to panic.
source§impl<B> PartialEq for FP<B>
impl<B> PartialEq for FP<B>
source§impl<B> UpperHex for FP<B>
impl<B> UpperHex for FP<B>
Note: max_ufp
for the internal FP::to_str_general call
is set to 4096, if it results in an overflow then the
formatting is a no-op rather than causing format!
to panic.