Skip to main content

Q16

Struct Q16 

Source
#[repr(transparent)]
pub struct Q16(pub i32);
Expand description

Signed Q16.16 fixed-point value.

Layout: a single i32 where the high 16 bits are the integer part and the low 16 bits are the fractional part. #[repr(transparent)] so the type can cross the FFI boundary as a plain int32_t without conversion.

Tuple Fields§

§0: i32

Implementations§

Source§

impl Q16

Source

pub const ZERO: Q16

Q16.16 representation of zero. Used as an EWMA seed and as the “no evidence” axis value before any detector has fired.

Source

pub const ONE: Q16

Q16.16 representation of one. The fractional half is zero, the integer half is one. 1 << 16 == 65_536.

Source

pub const MIN: Q16

The smallest representable value. Saturating arithmetic clamps here rather than wrapping.

Source

pub const MAX: Q16

The largest representable value. Saturating arithmetic clamps here rather than wrapping.

Source

pub const fn from_int(x: i16) -> Q16

Construct a Q16.16 value from an integer. Sign-extended into the high 16 bits. Caller is responsible for keeping |x| ≤ 32_767.

Source

pub const fn from_raw(raw: i32) -> Q16

Construct a Q16.16 value from its raw i32 bit pattern. Used by the contract loader (ewma_alpha_q16_raw) and by tests that need to pin a specific bit value.

Source

pub const fn raw(self) -> i32

Return the raw i32 representation. The hash-chain serializer writes raw words as zero-padded big-endian hex so the canonical bytes are stable regardless of host endianness.

Source

pub const fn sat_add(self, b: Q16) -> Q16

Saturating addition. Both CPU and CUDA paths must use the same rule because per-cell results would otherwise disagree at the boundaries.

Source

pub const fn sat_sub(self, b: Q16) -> Q16

Saturating subtraction. Mirrors sat_add.

Source

pub const fn sat_mul(self, b: Q16) -> Q16

Saturating multiplication with round-half-to-even.

The product is widened to i64 to avoid overflow before the rounding shift. Banker’s rounding is applied at bit 15: ties round toward the even result. After rounding the value is shifted right by 16 (the fractional width) and saturated back to i32.

This function is the load-bearing primitive for CPU↔GPU bit equality. Any divergence in its definition between the two backends would break stage-by-stage hash equivalence. The CUDA implementation in cuda/common.cuh is intentionally a line-for-line transliteration.

Source

pub const fn sat_div(self, b: Q16) -> Q16

Saturating division. Returns Q16::ZERO when the divisor is zero. The pipeline never divides on the hot path; this exists for the occasional baseline computation and is included for completeness.

Source

pub const fn abs(self) -> Q16

Absolute value, saturating. Q16::MIN.abs() returns Q16::MAX rather than panicking or wrapping.

Source

pub const fn is_zero(self) -> bool

Test for the additive identity. Used by axis gates that need to know “no evidence” without comparing two Q16 values.

Source

pub const fn lerp(self, other: Q16, alpha: Q16) -> Q16

Linear interpolation: self + alpha * (other - self), computed in saturating Q16.16. Convenience wrapper used by the EWMA recurrence. alpha is expected to live in [0, ONE]; values outside that band still produce a deterministic result but the EWMA interpretation stops being meaningful.

Trait Implementations§

Source§

impl Clone for Q16

Source§

fn clone(&self) -> Q16

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 Debug for Q16

Source§

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

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

impl Default for Q16

Source§

fn default() -> Q16

Returns the “default value” for a type. Read more
Source§

impl Hash for Q16

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 Ord for Q16

Source§

fn cmp(&self, other: &Q16) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Q16

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PartialOrd for Q16

Source§

fn partial_cmp(&self, other: &Q16) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Q16

Source§

impl Eq for Q16

Source§

impl StructuralPartialEq for Q16

Auto Trait Implementations§

§

impl Freeze for Q16

§

impl RefUnwindSafe for Q16

§

impl Send for Q16

§

impl Sync for Q16

§

impl Unpin for Q16

§

impl UnsafeUnpin for Q16

§

impl UnwindSafe for Q16

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, 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.