Struct HeadByte

Source
pub struct HeadByte(/* private fields */);
Expand description

The Head Byte itself, containing information about the sign, presence of the exponent and the number of coefficients.

Follows the newtype pattern, meaning that it can be unwrapped into the inner byte.

Implementations§

Source§

impl HeadByte

Source

pub const SIGN_MASK: u8 = 128u8

The sign bit mask.

Use sign to easily retreive the sign.

Source

pub const ABS_MASK: u8 = 127u8

The mask used for retreiving the absolute value of the number.

Use abs or bitwise-AND (&) this with the Head Byte to retreive the absolute value.

Source

pub const HAS_EXPONENT_MASK: u8 = 64u8

The exponent presence bit mask.

Use has_exponent to easily retreive this.

Source

pub const NUM_COEFFICIENTS_MASK: u8 = 63u8

Mask for the number of coefficients.

Use num_coefficients to easily retreive this.

Source

pub const INFINITY: Self

The (positive infinity) value for the Head Byte.

No following exponent or coefficients are allowed in this case, despite the exponent bit being set.

Source

pub const NEG_INFINITY: Self

The -∞ (negative infinity) value for the Head Byte.

No following exponent or coefficients are allowed in this case, despite the exponent bit being set.

Source

pub const ZERO: Self

The zero value. There’s no distinction between positive and negative zero.

No following exponent or coefficients are allowed.

Source

pub const NAN: Self

The NaN (Not-a-Number) value. There’s no distinction between negative/positive NaN or signalling/quiet NaN. (This implementation always generates quiet NaN.)

NaN values aren’t equal to themselves, just like in IEEE 754. To check for NaN values, use either is_nan or, if you’re using the try_nan feature (which currently only works on Nightly), the Try trait, which returns an error if the value is NaN.

No following exponent or coefficients are allowed.

Source

pub fn sign(self) -> Sign

Extracts the sign from a Head Byte.

Source

pub const fn abs(self) -> Self

Calculates the absolute value from the number whose Head Byte is self.

Since the Head Byte stores the sign of the entire number, it’s enough to just perform bitwise AND with the ABS_MASK, which in turn is the bitwise NOT of the sign mask.

Source

pub const fn exponent_bit(self) -> bool

Checks whether the HAS_EXPONENT bit of the Head Byte is set, meaning either infinity or the presence of an actual exponent.

For a version which also checks for the infinity special case, see has_exponent.

Source

pub fn with_exponent_bit(self, op: bool) -> Self

Constructs a Head Byte which has the same sign flag and number of following bytes expected as the one specified but also sets the exponent presence bit to a new value.

The in-place counterpart is set_exponent_bit.

Source

pub fn set_exponent_bit(&mut self, op: bool)

Sets the exponent presence bit in the Head Byte. If the number of expected bytes which follow the number is zero, setting it to true produces the infinity special-case Head Byte.

For a version of this funciton which returns the result instead of performing the operation in-place, see with_exponent_bit.

Source

pub fn is_infinite(self) -> bool

Checks whether the Head Byte describes either positive or negative infinity.

This is mostly uesd in has_exponent to check for the infinity special case.

Source

pub fn is_nan(self) -> bool

Checks whether the Head Byte describes a NaN value.

Source

pub fn has_exponent(self) -> bool

Checks whether the Head Byte is supposed to be followed by an exponent byte.

This includes the check for the special infinity value. For a version which does not check for infinity and thus plays more nicely with branch prediction, see exponent_bit_set.

Source

pub const fn num_bytes(self) -> u8

Fetches the number of bytes in the number which are supposed to follow the Head Byte.

This is equal to the number of following coefficients if there is no exponent or that number plus one if there is an exponent.

Source

pub fn num_coefficients(self) -> u8

Fetches the number of following coefficients.

This is equal to the number of following bytes if there is no exponent or that number minus one if there is an exponent.

Source

pub fn with_num_bytes(self, op: u8) -> Self

Constructs a Head Byte which has the same sign and exponent flags as the one specified but also sets the number of following bytes expected to a new value.

Panics if the number cannot fit into the Head Byte’s byte count field.

The in-place counterpart is with_num_bytes.

Source

pub fn set_num_bytes(&mut self, op: u8)

Sets the number of bytes which are supposed to follow the Head Byte.

Panics if the number cannot fit into the Head Byte’s byte count field.

For a version of this funciton which returns the result instead of performing the operation in-place, see with_num_bytes.

Source

pub fn with_num_coefficients(self, op: u8) -> Self

Constructs a Head Byte which has the same sign and exponent flags as the one specified but also sets the number of following bytes expected to a new value. This is equivalent to with_num_bytes if an exponent is not expected, otherwise this adds 1 and calls the aforementioned function. If the Head Byte indicates the infinity special case, the lack of an exponent is assumed (i.e. 1 is not added to this number), despite the exponent bit being set in such situations.

Panics if the number cannot fit into the Head Byte’s byte count field. The panic message includes the exponent byte in the number of bytes, if expected.

The in-place counterpart is set_coefficient_bytes.

Source

pub fn set_num_coefficients(&mut self, op: u8)

Sets the number of coefficient bytes which are supposed to follow the Head Byte. This is equivalent to set_num_bytes if an exponent is not expected, otherwise this adds 1 and calls the aforementioned function. If the Head Byte indicates the infinity special case, the lack of an exponent is assumed (i.e. 1 is not added to this number), despite the exponent bit being set in such situations.

Panics if the number cannot fit into the Head Byte’s byte count field. The panic message includes the exponent byte in the number of bytes, if expected.

For a version of this funciton which returns the result instead of performing the operation in-place, see with_num_coefficients.

Source

pub const fn into_inner(self) -> u8

Consumes the value and returns the inner byte.

Trait Implementations§

Source§

impl Clone for HeadByte

Source§

fn clone(&self) -> HeadByte

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HeadByte

Source§

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

Formats the value using the given formatter. Read more
Source§

impl From<HeadByte> for u8

Source§

fn from(op: HeadByte) -> Self

Consumes the Head Byte and returns the underlying inner byte.

Source§

impl From<u8> for HeadByte

Source§

fn from(op: u8) -> Self

Wraps a byte into a Head Byte.

Source§

impl Neg for HeadByte

Source§

type Output = HeadByte

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for HeadByte

Source§

fn eq(&self, other: &HeadByte) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for HeadByte

Source§

impl Eq for HeadByte

Source§

impl StructuralPartialEq for HeadByte

Auto Trait Implementations§

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

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.