#[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: i32Implementations§
Source§impl Q16
impl Q16
Sourcepub const ZERO: Q16
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.
Sourcepub const ONE: Q16
pub const ONE: Q16
Q16.16 representation of one. The fractional half is zero, the
integer half is one. 1 << 16 == 65_536.
Sourcepub const MIN: Q16
pub const MIN: Q16
The smallest representable value. Saturating arithmetic clamps here rather than wrapping.
Sourcepub const MAX: Q16
pub const MAX: Q16
The largest representable value. Saturating arithmetic clamps here rather than wrapping.
Sourcepub const fn from_int(x: i16) -> Q16
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.
Sourcepub const fn from_raw(raw: i32) -> Q16
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.
Sourcepub const fn raw(self) -> i32
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.
Sourcepub const fn sat_add(self, b: Q16) -> Q16
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.
Sourcepub const fn sat_mul(self, b: Q16) -> Q16
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.
Sourcepub const fn sat_div(self, b: Q16) -> Q16
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.
Sourcepub const fn abs(self) -> Q16
pub const fn abs(self) -> Q16
Absolute value, saturating. Q16::MIN.abs() returns Q16::MAX
rather than panicking or wrapping.
Sourcepub const fn is_zero(self) -> bool
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.
Sourcepub const fn lerp(self, other: Q16, alpha: Q16) -> Q16
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.