Skip to main content

TritFloat

Struct TritFloat 

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

A floating-point number encoded in balanced ternary with a native confidence field.

The confidence field propagates automatically through arithmetic, giving any computation a live uncertainty estimate without a separate Bayesian layer.

Use TritFloat::from_f32 to construct, .to_f32() to read the value, and .confidence() to read the certainty in [0, 1].

Implementations§

Source§

impl TritFloat

Source

pub fn zero() -> Self

The canonical zero, with neutral confidence.

Source

pub fn from_f32(x: f32) -> Self

Convert an f32 to TritFloat with maximum confidence (certainty=1.0).

Source

pub fn from_f32_with_confidence(x: f32, confidence: f32) -> Self

Convert an f32 to TritFloat with a specified confidence in [0, 1].

Source

pub fn to_f32(self) -> f32

Convert to f32. Confidence is discarded; use .confidence() separately.

Source

pub fn phase(self) -> i8

The phase trit: -1 (negative), 0 (zero), or +1 (positive).

Source

pub fn exponent(self) -> i32

The exponent as a signed integer in [-121, +121].

Source

pub fn mantissa(self) -> u32

The mantissa as a u32 in [0, 728]. Represents fractional part as M/729.

Source

pub fn confidence(self) -> f32

Confidence as a float in [0.0, 1.0].

0.0 = completely unknown, 0.5 = neutral/unset, 1.0 = maximally certain.

Source

pub fn is_zero(self) -> bool

True if this value is zero (phase trit = 0).

Source

pub fn is_uncertain(self) -> bool

True if confidence is below 0.5 (both confidence trits ≤ 0).

Source

pub fn raw(self) -> u32

The raw u32 backing value (for serialization and hardware interop).

Source

pub fn from_raw(raw: u32) -> Self

Reconstruct from a raw u32 (as returned by .raw()).

Source

pub fn mul_confidence(a: Self, b: Self) -> f32

Propagation rule for multiplication: weakest link. The result is only as confident as the less certain operand.

Source

pub fn add_confidence(a: Self, b: Self) -> f32

Propagation rule for addition: average the evidence.

Source

pub fn neg(self) -> Self

Negate: flip phase, preserve all other fields including confidence.

Source

pub fn abs(self) -> Self

Absolute value: force phase to +1 (or 0 if zero).

Source

pub fn add(self, rhs: Self) -> Self

Addition with confidence propagation (average rule).

Source

pub fn sub(self, rhs: Self) -> Self

Subtraction with confidence propagation (average rule).

Source

pub fn mul(self, rhs: Self) -> Self

Multiplication with confidence propagation (weakest-link rule).

Source

pub fn dot(a: &[Self], b: &[Self]) -> Self

Dot product of two slices of TritFloats.

Confidence of the result = min confidence across all terms. Zero-phase terms are skipped entirely (@sparseskip at activation level).

Source

pub fn dot_with_skips(a: &[Self], b: &[Self]) -> (Self, usize)

Dot product returning (result, skip_count) for sparsity instrumentation.

Source

pub fn should_route(self, threshold: f32) -> bool

Returns true if this activation should be routed to an expert.

Uncertain activations (confidence < threshold) can skip expensive expert layers entirely — the confidence field directly gates MoE routing.

threshold = minimum confidence to route (suggested: 0.3–0.5)

Source

pub fn div(self, rhs: Self) -> Self

Division with weakest-link confidence. Division by zero returns zero with 0 confidence — the caller can detect this via is_uncertain.

Source

pub fn recip(self) -> Self

Reciprocal: 1/x. Confidence preserved; zero input returns 0-confidence zero.

Source

pub fn powi(self, n: i32) -> Self

Integer power. Confidence preserved — single-operand chain.

Source

pub fn sqrt(self) -> Self

Square root. Negative input returns 0-confidence zero (not a real number).

Source

pub fn clamp(self, lo: f32, hi: f32) -> Self

Clamp the value to [lo, hi]. Confidence is preserved unchanged.

Source

pub fn cmp_trit(self, rhs: Self) -> Self

Ternary comparison: returns +1 if self > rhs, −1 if self < rhs, 0 if equal. Confidence = min(conf_self, conf_rhs) — comparison is only as reliable as inputs.

Source

pub fn softmax(slice: &[Self]) -> Vec<Self>

Numerically stable softmax over a slice of TritFloats.

Values are computed in f32; each output element carries the minimum confidence of all inputs (softmax mixes every element, so the whole slice’s certainty bounds the result).

Source

pub fn phase_digits(slice: &[Self]) -> Vec<u8>

Extract phase digits (0=neg, 1=zero, 2=pos) for a slice into a Vec<u8>.

The pre-scan buffer: a single contiguous pass over raw u32 values (% 3) before the arithmetic loop. Separating phase-check from f32 math eliminates branch misprediction in the hot loop at high sparsity (≥50% zeros).

Source

pub fn pack_phases_u64(slice: &[Self]) -> u64

Pack zero-phase flags for up to 64 TritFloats into a u64 bitmask.

Bit i = 1 if slice[i].is_zero(), else 0. mask.count_ones() instantly gives the skip count for a 64-element chunk. mask == 0 means all elements are active — no branch needed in the arithmetic loop. This is the preparation layer for AVX2 vectorization of the dot product.

Source

pub fn dot_prescan(a: &[Self], b: &[Self]) -> (Self, usize)

Dot product with two-pass pre-scan for reduced branch misprediction.

Pass 1: extract all phase flags into u8 arrays (cache-hot, no branching). Pass 2: arithmetic only for active (non-zero-phase) pairs.

Outperforms dot_with_skips at ≥50% sparsity where misprediction of the inline zero-check dominates. At low sparsity the extra allocation cost makes it slightly slower — profile before choosing.

Trait Implementations§

Source§

impl Clone for TritFloat

Source§

fn clone(&self) -> TritFloat

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for TritFloat

Source§

impl Debug for TritFloat

Source§

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

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

impl<'de> Deserialize<'de> for TritFloat

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for TritFloat

Source§

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

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

impl Eq for TritFloat

Source§

impl Hash for TritFloat

Source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for TritFloat

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for TritFloat

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for TritFloat

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.