Struct awint_core::Bits

source ·
#[repr(C)]
pub struct Bits { /* private fields */ }
Expand description

A reference to the bits in an InlAwi, ExtAwi, Awi, or other backing construct. If a function is written just in terms of Bits, it can work on mixed references to any of the storage structs and wrappers like FP<B>. const big integer arithmetic is possible if the backing type is InlAwi and the “const_support” flag is enabled.

Bits do not know signedness. Instead, the methods on Bits are specified to interpret the bits as unsigned or signed two’s complement integers. If a method’s documentation does not mention signedness, it either works for both kinds or views the bits as a plain bit string with no integral properties.

See the awint_core crate level documentation for understanding two’s complement and numerical limits.

§Note

Function names of the form *_ with a trailing underscore are shorthand for saying *_assign, which denotes an inplace assignment operation where the left hand side is used as an input before being reassigned the value of the output inplace. This is used instead of the typical 2-input 1-new-output kind of function, because:

  • Bits cannot allocate without choosing a storage type
  • In most cases during the course of computation, one value will not be needed after being used once as an input. It can take the left hand side self value of these inplace assignment operations.
  • For large bitwidth Bits, only two streams of addresses have to be considered by the CPU
  • In cases where we do need buffering, copying to some temporary is the fastest kind of operation (and in the future an optimizing macro for this is planned)

Unless otherwise specified, functions on Bits that return an Option<()> return None if the input bitwidths are not equal to each other. The Bits have been left unchanged if None is returned.

§Portability

This crate strives to maintain deterministic outputs across architectures with different usize::BITS, Digit::BITS, and different endiannesses. The Bits::u8_slice_ function, the Bits::to_u8_slice functions, the serialization impls enabled by serde_support, the strings produced by the const serialization functions, and functions like bits_to_string_radix in the awint_ext crate are all portable and should be used when sending representations of Bits between architectures.

The rand_ function enabled by rand_support uses a deterministic byte oriented implementation to avoid portability issues as long as the rng itself is portable.

The core::hash::Hash implementation is not deterministic across platforms and may not even be deterministic across compiler versions. This is because of technical problems, and the standard library docs say it is not intended to be portable anyway.

There are many functions that depend on Digit, usize, and NonZeroUsize. In cases where the usize describes the bitwidth, a bit shift, or a bit position, the user should not need to worry about portability, since if the values are close to usize::MAX, the user is already close to running out of possible memory any way.

There are a few usages of Digit that are actual views into a contiguous range of bits inside Bits, such as Bits::as_slice, Bits::first, and Bits::get_digit (which are all hidden from the documentation, please refer to the source code of bits.rs if needed). Most end users should not use these, since they have a strong dependence on the size of Digit. These functions are actual views into the inner building blocks of this crate that other functions are built around in such a way that they are portable (e.g. the addition functions may internally operate on differing numbers of Digits depending on the size of Digit, but the end result looks the same to users on different architectures). The only reason these functions are exposed, is that someone may want to write their own custom performant algorithms, and they want as few abstractions as possible in the way.

Visible functions that are not portable in general, but always start from the zeroeth bit or a given bit position like Bits::digit_cin_mul_, Bits::digit_udivide_, or Bits::digit_or_, are always portable as long as the digit inputs and/or outputs are restricted to 0..=u8::MAX, or special care is taken.

Implementations§

source§

impl<'a> Bits

§Basic functions

source

pub fn nzbw(&self) -> NonZeroUsize

Returns the bitwidth as a NonZeroUsize

source

pub fn bw(&self) -> usize

Returns the bitwidth as a usize

source

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.

source

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.

source§

impl Bits

§Bitwise

source

pub fn zero_(&mut self)

Zero-assigns. Same as the Unsigned-minimum-value. All bits are set to 0.

source

pub fn umax_(&mut self)

Unsigned-maximum-value-assigns. All bits are set to 1.

source

pub fn imax_(&mut self)

Signed-maximum-value-assigns. All bits are set to 1, except for the most significant bit.

source

pub fn imin_(&mut self)

Signed-minimum-value-assigns. Only the most significant bit is set.

source

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.

source

pub fn not_(&mut self)

Not-assigns self

source

pub fn copy_(&mut self, rhs: &Self) -> Option<()>

Copy-assigns the bits of rhs to self

source

pub fn or_(&mut self, rhs: &Self) -> Option<()>

Or-assigns rhs to self

source

pub fn and_(&mut self, rhs: &Self) -> Option<()>

And-assigns rhs to self

source

pub fn xor_(&mut self, rhs: &Self) -> Option<()>

Xor-assigns rhs to self

source

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().

source

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().

source

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().

source

pub fn digit_or_(&mut self, rhs: Digit, 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.

source§

impl Bits

§Casting between Bits of arbitrary sizes

source

pub fn resize_(&mut self, rhs: &Self, 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.

source

pub fn zero_resize_(&mut self, rhs: &Self) -> 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.

source

pub fn sign_resize_(&mut self, rhs: &Self) -> 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.

source§

impl Bits

§Comparison

source

pub fn is_zero(&self) -> bool

If self is zero

source

pub fn is_umax(&self) -> bool

If self is unsigned-maximum

source

pub fn is_imax(&self) -> bool

If self is signed-maximum

source

pub fn is_imin(&self) -> bool

If self is signed-minimum

source

pub fn is_uone(&self) -> bool

If self is unsigned-one

source

pub fn const_eq(&self, rhs: &Self) -> Option<bool>

Equality comparison, self == rhs

source

pub fn const_ne(&self, rhs: &Self) -> Option<bool>

Not-equal comparison, self != rhs

source

pub fn ult(&self, rhs: &Self) -> Option<bool>

Unsigned-less-than comparison, self < rhs

source

pub fn ule(&self, rhs: &Self) -> Option<bool>

Unsigned-less-than-or-equal comparison, self <= rhs

source

pub fn ugt(&self, rhs: &Self) -> Option<bool>

Unsigned-greater-than comparison, self > rhs

source

pub fn uge(&self, rhs: &Self) -> Option<bool>

Unsigned-greater-than-or-equal comparison, self >= rhs

source

pub fn ilt(&self, rhs: &Self) -> Option<bool>

Signed-less-than comparison, self < rhs

source

pub fn ile(&self, rhs: &Self) -> Option<bool>

Signed-less-than-or-equal comparison, self <= rhs

source

pub fn igt(&self, rhs: &Self) -> Option<bool>

Signed-greater-than comparison, self > rhs

source

pub fn ige(&self, rhs: &Self) -> Option<bool>

Signed-greater-than-or-equal comparison, self >= rhs

source

pub fn total_cmp(&self, rhs: &Self) -> Ordering

Total ordering over bitstrings, including differentiation between differing bitwidths of self and rhs. This orders first on bitwidth and then on unsigned value.

source§

impl Bits

§const string representation conversion

Note: the awint_ext crate has higher level allocating functions Awi::bits_to_string_radix, Awi::bits_to_vec_radix, and <Awi as FromStr>::from_str

source

pub fn bytes_radix_( &mut self, sign: Option<bool>, src: &[u8], radix: u8, pad0: &mut Self, pad1: &mut Self ) -> 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.

source

pub fn to_bytes_radix( &self, signed: bool, dst: &mut [u8], radix: u8, upper: bool, pad: &mut Self ) -> 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.

source§

impl Bits

§Division

These operations are not inplace unlike many other functions in this crate, because extra mutable space is needed in order to avoid allocation.

Note that signed divisions can overflow when duo.is_imin() and div.is_umax() (negative one in signed interpretation). The overflow results in quo.is_imin() and rem.is_zero().

Note about terminology: we like short three letter shorthands, but run into a problem where the first three letters of “divide”, “dividend”, and “divisor” all clash with each other. Additionally, the standard Rust terminology for a function returning a quotient is things such as i64::wrapping_div, which should have been named i64::wrapping_quo instead. Here, we choose to type out “divide” in full whenever the operation involves both quotients and remainders. We don’t use “num” or “den”, because it may cause confusion later if an awint crate gains rational number capabilities. We use “quo” for quotient and “rem” for remainder. We use “div” for divisor. That still leaves a name clash with dividend, so we choose to use the shorthand “duo”. This originates from the fact that for inplace division operations (which this crate does not have for performance purposes and avoiding allocation), the dividend is often subtracted from in the internal algorithms until it becomes the remainder, so that it serves two purposes.

source

pub fn digit_udivide_inplace_(&mut self, div: Digit) -> Option<Digit>

Unsigned-divides self by div, sets self to the quotient, and returns the remainder. Returns None if div == 0.

source

pub fn digit_udivide_(&mut self, duo: &Self, div: Digit) -> Option<Digit>

source

pub fn udivide( quo: &mut Self, rem: &mut Self, duo: &Self, div: &Self ) -> Option<()>

Unsigned-divides duo by div and assigns the quotient to quo and remainder to rem. Returns None if any bitwidths are not equal or div.is_zero().

source

pub fn idivide( quo: &mut Self, rem: &mut Self, duo: &mut Self, div: &mut Self ) -> Option<()>

Signed-divides duo by div and assigns the quotient to quo and remainder to rem. Returns None if any bitwidths are not equal or div.is_zero(). duo and div are marked mutable but their values are not changed by this function.

source§

impl Bits

§Miscellanious

source

pub fn lsb(&self) -> bool

Returns the least significant bit

source

pub fn msb(&self) -> bool

Returns the most significant bit

source

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()

source

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()

source

pub fn lz(&self) -> usize

Returns the number of leading zero bits

source

pub fn tz(&self) -> usize

Returns the number of trailing zero bits

source

pub fn sig(&self) -> usize

Returns the number of significant bits, self.bw() - self.lz()

source

pub fn count_ones(&self) -> usize

Returns the number of set ones

source

pub fn field( &mut self, to: usize, rhs: &Self, 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));
source

pub fn field_to(&mut self, to: usize, rhs: &Self, width: usize) -> Option<()>

A specialization of Bits::field with from set to 0.

source

pub fn field_from( &mut self, rhs: &Self, from: usize, width: usize ) -> Option<()>

A specialization of Bits::field with to set to 0.

source

pub fn field_width(&mut self, rhs: &Self, width: usize) -> Option<()>

A specialization of Bits::field with to and from set to 0.

source

pub fn field_bit(&mut self, to: usize, rhs: &Bits, from: usize) -> Option<()>

A specialization of Bits::field with width set to 1.

source

pub fn lut_(&mut self, lut: &Self, inx: &Self) -> 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));
source

pub fn lut_set(&mut self, entry: &Self, inx: &Self) -> 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.

source

pub fn mux_(&mut self, rhs: &Bits, b: bool) -> Option<()>

Multiplex by conditionally copy-assigning rhs to self if b

source

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.

source§

impl Bits

§Multiplication

source

pub fn digit_cin_mul_(&mut self, cin: Digit, rhs: Digit) -> Digit

Assigns cin + (self * rhs) to self and returns the overflow

source

pub fn digit_mul_add_(&mut self, lhs: &Self, rhs: Digit) -> Option<bool>

Add-assigns lhs * rhs to self and returns if overflow happened

source

pub fn mul_add_(&mut self, lhs: &Self, rhs: &Self) -> Option<()>

Multiplies lhs by rhs and add-assigns the product to self. Three operands eliminates the need for an allocating temporary.

source

pub fn mul_(&mut self, rhs: &Self, pad: &mut Self) -> Option<()>

Multiply-assigns self by rhs. pad is a scratchpad that will be mutated arbitrarily.

source

pub fn arb_umul_add_(&mut self, lhs: &Self, rhs: &Self)

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

source

pub fn arb_imul_add_(&mut self, lhs: &mut Self, rhs: &mut Self)

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.

source§

impl Bits

§Bit permutation

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn funnel_(&mut self, rhs: &Self, s: &Self) -> 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))
source§

impl Bits

§Primitive assignment

If self.bw() is smaller than the primitive bitwidth, truncation will be used when copying bits from x to self. If the primitive is unsigned (or is a boolean), then zero extension will be used if self.bw() is larger than the primitive bitwidth. If the primitive is signed, then sign extension will be used if self.bw() is larger than the primitive bitwidth.

source

pub fn u8_(&mut self, x: u8)

source

pub fn i8_(&mut self, x: i8)

source

pub fn u16_(&mut self, x: u16)

source

pub fn i16_(&mut self, x: i16)

source

pub fn u32_(&mut self, x: u32)

source

pub fn i32_(&mut self, x: i32)

source

pub fn u64_(&mut self, x: u64)

source

pub fn i64_(&mut self, x: i64)

source

pub fn u128_(&mut self, x: u128)

source

pub fn i128_(&mut self, x: i128)

source

pub fn usize_(&mut self, x: usize)

source

pub fn isize_(&mut self, x: isize)

source

pub fn bool_(&mut self, x: bool)

source

pub fn digit_(&mut self, x: Digit)

source§

impl Bits

§Primitive conversion

If self.bw() is larger than the primitive bitwidth, truncation will be used when copying the bits of self and returning them. If the primitive is unsigned, then zero extension will be used if self.bw() is smaller than the primitive bitwidth. If the primitive is signed, then sign extension will be used if self.bw() is smaller than the primitive bitwidth.

source

pub fn to_u8(&self) -> u8

source

pub fn to_i8(&self) -> i8

source

pub fn to_u16(&self) -> u16

source

pub fn to_i16(&self) -> i16

source

pub fn to_u32(&self) -> u32

source

pub fn to_i32(&self) -> i32

source

pub fn to_u64(&self) -> u64

source

pub fn to_i64(&self) -> i64

source

pub fn to_u128(&self) -> u128

source

pub fn to_i128(&self) -> i128

source

pub fn to_usize(&self) -> usize

source

pub fn to_isize(&self) -> isize

source

pub fn to_bool(&self) -> bool

source

pub fn to_digit(&self) -> Digit

source§

impl Bits

§Summation

source

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().

source

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().

source

pub fn neg_(&mut self, neg: bool)

Negate-assigns self if neg is true. Note that signed minimum values will overflow.

source

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.

source

pub fn add_(&mut self, rhs: &Self) -> Option<()>

Add-assigns by rhs

source

pub fn sub_(&mut self, rhs: &Self) -> Option<()>

Subtract-assigns by rhs

source

pub fn rsb_(&mut self, rhs: &Self) -> Option<()>

Reverse-subtract-assigns by rhs. Sets self to (-self) + rhs.

source

pub fn neg_add_(&mut self, neg: bool, rhs: &Self) -> Option<()>

Negate-add-assigns by rhs. Negates conditionally on neg.

source

pub fn cin_sum_( &mut self, cin: bool, lhs: &Self, rhs: &Self ) -> 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<const BW: usize, const LEN: usize> AsMut<Bits> for InlAwi<BW, LEN>

source§

fn as_mut(&mut self) -> &mut Bits

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<const BW: usize, const LEN: usize> AsRef<Bits> for InlAwi<BW, LEN>

source§

fn as_ref(&self) -> &Bits

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Binary for Bits

source§

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

Binary formatting.

use awint::{inlawi, Bits, InlAwi};
assert_eq!(format!("{:b}", inlawi!(11000101)), "0b11000101_u8");
source§

impl<const BW: usize, const LEN: usize> Borrow<Bits> for InlAwi<BW, LEN>

source§

fn borrow(&self) -> &Bits

Immutably borrows from an owned value. Read more
source§

impl<const BW: usize, const LEN: usize> BorrowMut<Bits> for InlAwi<BW, LEN>

source§

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

Mutably borrows from an owned value. Read more
source§

impl Debug for Bits

source§

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

Forwards to the LowerHex impl. We cannot use decimal because it would require allocation.

source§

impl Display for Bits

source§

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

Forwards to the Debug impl

source§

impl From<&Bits> for bool

source§

fn from(x: &Bits) -> bool

Returns the least significant bit

source§

impl From<&Bits> for i128

source§

fn from(x: &Bits) -> Self

Sign-resizes the Bits to this integer

source§

impl From<&Bits> for i16

source§

fn from(x: &Bits) -> Self

Sign-resizes the Bits to this integer

source§

impl From<&Bits> for i32

source§

fn from(x: &Bits) -> Self

Sign-resizes the Bits to this integer

source§

impl From<&Bits> for i64

source§

fn from(x: &Bits) -> Self

Sign-resizes the Bits to this integer

source§

impl From<&Bits> for i8

source§

fn from(x: &Bits) -> Self

Sign-resizes the Bits to this integer

source§

impl From<&Bits> for isize

source§

fn from(x: &Bits) -> Self

Sign-resizes the Bits to this integer

source§

impl From<&Bits> for u128

source§

fn from(x: &Bits) -> Self

Zero-resizes the Bits to this integer

source§

impl From<&Bits> for u16

source§

fn from(x: &Bits) -> Self

Zero-resizes the Bits to this integer

source§

impl From<&Bits> for u32

source§

fn from(x: &Bits) -> Self

Zero-resizes the Bits to this integer

source§

impl From<&Bits> for u64

source§

fn from(x: &Bits) -> Self

Zero-resizes the Bits to this integer

source§

impl From<&Bits> for u8

source§

fn from(x: &Bits) -> Self

Zero-resizes the Bits to this integer

source§

impl From<&Bits> for usize

source§

fn from(x: &Bits) -> Self

Zero-resizes the Bits to this integer

source§

impl Hash for Bits

source§

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

note: this function is not portable across platforms

source§

impl LowerHex for Bits

source§

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

Lowercase hexadecimal formatting.

use awint::{inlawi, Bits, InlAwi};
assert_eq!(
    format!("{:x}", inlawi!(0xfedcba9876543210u100)),
    "0xfedcba98_76543210_u100"
);
source§

impl Octal for Bits

source§

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

Octal formatting.

use awint::{inlawi, Bits, InlAwi};
assert_eq!(
    format!("{:o}", inlawi!(0o776543210u100)),
    "0o7_76543210_u100"
);
source§

impl PartialEq for Bits

If self and other have unmatching bit widths, false will be returned.

source§

fn eq(&self, rhs: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Pointer for Bits

source§

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

Formats the value using the given formatter.
source§

impl UpperHex for Bits

source§

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

Uppercase hexadecimal formatting.

use awint::{inlawi, Bits, InlAwi};
assert_eq!(
    format!("{:X}", inlawi!(0xFEDCBA9876543210u100)),
    "0xFEDCBA98_76543210_u100"
);
source§

impl Eq for Bits

If self and other have unmatching bit widths, false will be returned.

source§

impl Send for Bits

Bits is safe to send between threads since it does not own aliasing memory and has no reference counting mechanism like Rc.

source§

impl Sync for Bits

Bits 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§

§

impl RefUnwindSafe for Bits

§

impl !Sized for Bits

§

impl Unpin for Bits

§

impl UnwindSafe for Bits

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more