pub struct Word(/* private fields */);Expand description
A word, 27 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 Word
impl Word
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: i64) -> Self
pub const fn from_value(v: i64) -> 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; 27]) -> Self
pub const fn from_trits(trits: [Trit; 27]) -> Self
Rebuilds a value from trits, least significant first.
Sourcepub const fn to_bct(self) -> u64
pub const fn to_bct(self) -> u64
Packs into BCT: two bits per trit, least significant trit in the low bits.
Sourcepub const fn from_bct(bits: u64) -> Result<Self, NatError>
pub const fn from_bct(bits: u64) -> 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.
Source§impl Word
impl Word
Source§impl Word
impl Word
Sourcepub const fn wrapping_mul(self, rhs: Self) -> Self
pub const fn wrapping_mul(self, rhs: Self) -> Self
Multiplication modulo 3^n.
Source§impl Word
impl Word
Sourcepub const fn widen(self) -> DoubleWord
pub const fn widen(self) -> DoubleWord
Widens to a DoubleWord. Always exact.
Sourcepub const fn widening_mul(self, rhs: Self) -> DoubleWord
pub const fn widening_mul(self, rhs: Self) -> DoubleWord
The exact product of two words, as a double word.
Cannot overflow: the product of two p-trit values is exactly 2p trits,
and 27 + 27 is the double-word width. A DoubleWord accumulator
therefore holds each product exactly and only the summation rounds
(D-02).