pub struct DoubleWord(/* private fields */);Expand description
A double word, 54 balanced trits.
Stores the numeric value in the host integer; see the module docs.
Every value of this type is in range by construction, so value()
never returns something out of bounds.
Implementations§
Source§impl DoubleWord
impl DoubleWord
Sourcepub const MIN: Self
pub const MIN: Self
The smallest representable value, -(3^n-1)/2.
The range is symmetric, so there is no value whose negation overflows (D-01).
Sourcepub const fn from_value(v: i128) -> Self
pub const fn from_value(v: i128) -> Self
Wraps a host value, panicking if out of range.
For constants and tests where the range is known statically.
Sourcepub const fn from_i128_wrapping(v: i128) -> Self
pub const fn from_i128_wrapping(v: i128) -> Self
Reduces an i128 into range modulo 3^n. Always succeeds.
Sourcepub const fn from_i128(v: i128) -> Option<Self>
pub const fn from_i128(v: i128) -> Option<Self>
Reduces an i128 into range, or None if it does not fit.
Sourcepub const fn from_trits(trits: [Trit; 54]) -> Self
pub const fn from_trits(trits: [Trit; 54]) -> Self
Rebuilds a value from trits, least significant first.
Sourcepub const fn to_bct(self) -> u128
pub const fn to_bct(self) -> u128
Packs into BCT: two bits per trit, least significant trit in the low bits.
Sourcepub const fn from_bct(bits: u128) -> Result<Self, NatError>
pub const fn from_bct(bits: u128) -> Result<Self, NatError>
Unpacks from BCT, rejecting the 10 NaT pattern.
NaT marks poisoned host memory and must never reach a guest, so decoding it is an error rather than a silent zero (D-03).
Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Addition, None on overflow.
Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Subtraction, None on overflow.
Sourcepub const fn checked_mul(self, rhs: Self) -> Option<Self>
pub const fn checked_mul(self, rhs: Self) -> Option<Self>
Multiplication, None on overflow.
Sourcepub const fn checked_div(self, rhs: Self) -> Option<Self>
pub const fn checked_div(self, rhs: Self) -> Option<Self>
Truncating division, None only when rhs is zero.
Division cannot overflow: the range is symmetric, so there is no
MIN / -1 case (D-01).
Sourcepub const fn checked_rem(self, rhs: Self) -> Option<Self>
pub const fn checked_rem(self, rhs: Self) -> Option<Self>
Remainder, None only when rhs is zero.
Sourcepub const fn wrapping_add(self, rhs: Self) -> Self
pub const fn wrapping_add(self, rhs: Self) -> Self
Addition modulo 3^n.
Sourcepub const fn wrapping_sub(self, rhs: Self) -> Self
pub const fn wrapping_sub(self, rhs: Self) -> Self
Subtraction modulo 3^n.
Sourcepub const fn checked_shl_trits(self, k: u32) -> Option<Self>
pub const fn checked_shl_trits(self, k: u32) -> Option<Self>
Shift left by k trits, multiplying by 3^k. None on
overflow. Exact by construction.
Sourcepub const fn shr_trits(self, k: u32) -> Self
pub const fn shr_trits(self, k: u32) -> Self
Shift right by k trits, dropping the low k digits. This is
division by 3^k rounded to nearest: the dropped digits are worth
at most (3^k-1)/2, half a unit in the last place.
This is not the host’s /, which truncates toward zero. For
1,598,046,941,971 / 27 truncation leaves a remainder of 19
against a ULP of 27. Implementing this as self.0 / pow3(k) was
a bug, caught by truncation_is_round_to_nearest.
Ties cannot occur: 3^k is odd, so twice the remainder never equals it. No round-half-to-even rule is needed.
Sourcepub const fn cmp3(self, rhs: Self) -> Trit
pub const fn cmp3(self, rhs: Self) -> Trit
Three-way comparison returning one trit (D-06).
Neg for less, Zero for equal, Pos for greater.
Trait Implementations§
Source§impl Clone for DoubleWord
impl Clone for DoubleWord
Source§fn clone(&self) -> DoubleWord
fn clone(&self) -> DoubleWord
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for DoubleWord
Source§impl Debug for DoubleWord
impl Debug for DoubleWord
Source§impl Default for DoubleWord
impl Default for DoubleWord
Source§fn default() -> DoubleWord
fn default() -> DoubleWord
Source§impl Display for DoubleWord
impl Display for DoubleWord
impl Eq for DoubleWord
Source§impl FromStr for DoubleWord
impl FromStr for DoubleWord
Source§impl Hash for DoubleWord
impl Hash for DoubleWord
Source§impl Neg for DoubleWord
impl Neg for DoubleWord
Source§impl Ord for DoubleWord
impl Ord for DoubleWord
Source§fn cmp(&self, other: &DoubleWord) -> Ordering
fn cmp(&self, other: &DoubleWord) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for DoubleWord
impl PartialEq for DoubleWord
Source§fn eq(&self, other: &DoubleWord) -> bool
fn eq(&self, other: &DoubleWord) -> bool
self and other values to be equal, and is used by ==.