pub struct QValue<S, const FRAC: u32>(/* private fields */);Expand description
A signed fixed-point value stored in S (i16 or i32) with FRAC
fractional bits.
The derived PartialOrd/Ord compare the raw integer, which is exactly what
the C++ comparison operators do (they compare getValue()).
FRAC must be at least 1 and less than the storage width; anything else is a
compile-time error wherever the format’s arithmetic is used:
// 40 fractional bits do not fit in i32 storage.
let x = tinymind::QValue::<i32, 40>::default();
let _ = x.to_f64();Implementations§
Source§impl<const FRAC: u32> QValue<i32, FRAC>
impl<const FRAC: u32> QValue<i32, FRAC>
Sourcepub const FRACTIONAL_BITS: u32 = FRAC
pub const FRACTIONAL_BITS: u32 = FRAC
Number of fractional bits (FRAC).
Sourcepub const FIXED_BITS: u32
pub const FIXED_BITS: u32
Number of integer bits, including the sign bit (storage width - FRAC).
Sourcepub const ONE: Self
pub const ONE: Self
1.0. When FRAC is one less than the storage width (Q1.15, Q1.31)
there are no integer bits besides the sign, 1.0 is not
representable, and this wraps to -1.0 (the storage type’s minimum).
Sourcepub const fn from_raw(raw: i32) -> Self
pub const fn from_raw(raw: i32) -> Self
Build from raw LSBs (C++ QValue(FullWidthFieldType) / setValue(raw)).
Sourcepub const fn from_int(n: i32) -> Self
pub const fn from_int(n: i32) -> Self
Build from a whole number (C++ QValue(fixedPart, 0)). The fixed part
is truncated to FIXED_BITS, as the C++ fromParts does, which is the
same as shifting left and letting the high bits fall off: in Q8.8, 200
becomes -56.
Sourcepub const fn pow2_neg(n: u32) -> Self
pub const fn pow2_neg(n: u32) -> Self
2^-n, used for tinymind (C++)’s default learning/momentum/acceleration rates.
For n > FRAC, 2^-n is below the format’s resolution and the result
is ZERO: the value truncated to the format, as
TruncatePolicy rounds every other result. pow2_neg(0) is
ONE, with the same caveat for formats without integer bits.
Sourcepub fn to_f64(self) -> f64
pub fn to_f64(self) -> f64
Real-number view, computed as the C++ example does: raw / 2^FRAC in f64.
Sourcepub fn from_f64_trunc(x: f64) -> Self
pub fn from_f64_trunc(x: f64) -> Self
Truncating conversion from f64 (C++
setValue(static_cast<FullWidthValueType>(x * 2^FRAC))). Out-of-range
input saturates; in C++ it is undefined behaviour.
Sourcepub fn mul_round_toward_zero(self, rhs: Self) -> Self
pub fn mul_round_toward_zero(self, rhs: Self) -> Self
self * rhs with the product rounded toward zero instead of
floored (C++ multiplyRoundTowardZero): the same widening and
wrap to the storage width as *. SGD uses it for the momentum
and acceleration terms: floored, 0.5 * (-1 LSB) stays -1 LSB,
so a delta weight no gradient refreshes would never decay.
Sourcepub fn mul_round_nearest(self, rhs: Self) -> Self
pub fn mul_round_nearest(self, rhs: Self) -> Self
self * rhs with the product rounded to nearest, ties away
from zero, instead of floored (C++ multiplyRoundNearest): the
same widening and wrap to the storage width as *. SGD uses it
for the step learning rate * gradient: floored, a step smaller
than one LSB is 0 when positive but a whole -1 LSB when
negative, so weights with small gradients drift down.
Sourcepub const fn div_int(self, n: i32) -> Self
pub const fn div_int(self, n: i32) -> Self
Divide by a whole number, truncating toward zero (C++
QValue::operator/=(int)). This is not the same as dividing by
QValue::from_int(n).
Sourcepub const fn is_within_zero_tolerance(self) -> bool
pub const fn is_within_zero_tolerance(self) -> bool
C++ ZeroToleranceCalculator: true when |raw| < 1 << (FRAC - 7) for
formats with more than 7 fractional bits (512 LSBs for Q16.16, 2 for
Q8.8), else |raw| < 2.
Source§impl<const FRAC: u32> QValue<i16, FRAC>
impl<const FRAC: u32> QValue<i16, FRAC>
Sourcepub const FRACTIONAL_BITS: u32 = FRAC
pub const FRACTIONAL_BITS: u32 = FRAC
Number of fractional bits (FRAC).
Sourcepub const FIXED_BITS: u32
pub const FIXED_BITS: u32
Number of integer bits, including the sign bit (storage width - FRAC).
Sourcepub const ONE: Self
pub const ONE: Self
1.0. When FRAC is one less than the storage width (Q1.15, Q1.31)
there are no integer bits besides the sign, 1.0 is not
representable, and this wraps to -1.0 (the storage type’s minimum).
Sourcepub const fn from_raw(raw: i16) -> Self
pub const fn from_raw(raw: i16) -> Self
Build from raw LSBs (C++ QValue(FullWidthFieldType) / setValue(raw)).
Sourcepub const fn from_int(n: i32) -> Self
pub const fn from_int(n: i32) -> Self
Build from a whole number (C++ QValue(fixedPart, 0)). The fixed part
is truncated to FIXED_BITS, as the C++ fromParts does, which is the
same as shifting left and letting the high bits fall off: in Q8.8, 200
becomes -56.
Sourcepub const fn pow2_neg(n: u32) -> Self
pub const fn pow2_neg(n: u32) -> Self
2^-n, used for tinymind (C++)’s default learning/momentum/acceleration rates.
For n > FRAC, 2^-n is below the format’s resolution and the result
is ZERO: the value truncated to the format, as
TruncatePolicy rounds every other result. pow2_neg(0) is
ONE, with the same caveat for formats without integer bits.
Sourcepub fn to_f64(self) -> f64
pub fn to_f64(self) -> f64
Real-number view, computed as the C++ example does: raw / 2^FRAC in f64.
Sourcepub fn from_f64_trunc(x: f64) -> Self
pub fn from_f64_trunc(x: f64) -> Self
Truncating conversion from f64 (C++
setValue(static_cast<FullWidthValueType>(x * 2^FRAC))). Out-of-range
input saturates; in C++ it is undefined behaviour.
Sourcepub fn mul_round_toward_zero(self, rhs: Self) -> Self
pub fn mul_round_toward_zero(self, rhs: Self) -> Self
self * rhs with the product rounded toward zero instead of
floored (C++ multiplyRoundTowardZero): the same widening and
wrap to the storage width as *. SGD uses it for the momentum
and acceleration terms: floored, 0.5 * (-1 LSB) stays -1 LSB,
so a delta weight no gradient refreshes would never decay.
Sourcepub fn mul_round_nearest(self, rhs: Self) -> Self
pub fn mul_round_nearest(self, rhs: Self) -> Self
self * rhs with the product rounded to nearest, ties away
from zero, instead of floored (C++ multiplyRoundNearest): the
same widening and wrap to the storage width as *. SGD uses it
for the step learning rate * gradient: floored, a step smaller
than one LSB is 0 when positive but a whole -1 LSB when
negative, so weights with small gradients drift down.
Sourcepub const fn div_int(self, n: i32) -> Self
pub const fn div_int(self, n: i32) -> Self
Divide by a whole number, truncating toward zero (C++
QValue::operator/=(int)). This is not the same as dividing by
QValue::from_int(n).
Sourcepub const fn is_within_zero_tolerance(self) -> bool
pub const fn is_within_zero_tolerance(self) -> bool
C++ ZeroToleranceCalculator: true when |raw| < 1 << (FRAC - 7) for
formats with more than 7 fractional bits (512 LSBs for Q16.16, 2 for
Q8.8), else |raw| < 2.
Trait Implementations§
Source§impl<const FRAC: u32> AddAssign for QValue<i32, FRAC>
impl<const FRAC: u32> AddAssign for QValue<i32, FRAC>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<const FRAC: u32> AddAssign for QValue<i16, FRAC>
impl<const FRAC: u32> AddAssign for QValue<i16, FRAC>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreimpl<S: Copy, const FRAC: u32> Copy for QValue<S, FRAC>
Source§impl<S: Display, const FRAC: u32> Display for QValue<S, FRAC>
Prints the raw LSBs, matching the C++ operator<< (not the real value).
impl<S: Display, const FRAC: u32> Display for QValue<S, FRAC>
Prints the raw LSBs, matching the C++ operator<< (not the real value).
impl<S: Eq, const FRAC: u32> Eq for QValue<S, FRAC>
Source§impl<S: Ord, const FRAC: u32> Ord for QValue<S, FRAC>
impl<S: Ord, const FRAC: u32> Ord for QValue<S, FRAC>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<S: PartialOrd, const FRAC: u32> PartialOrd for QValue<S, FRAC>
impl<S: PartialOrd, const FRAC: u32> PartialOrd for QValue<S, FRAC>
Source§impl<const FRAC: u32> Scalar for QValue<i32, FRAC>
impl<const FRAC: u32> Scalar for QValue<i32, FRAC>
Source§fn mul_round_toward_zero(self, rhs: Self) -> Self
fn mul_round_toward_zero(self, rhs: Self) -> Self
self * rhs rounded toward zero (C++ multiplyRoundTowardZero), used
for SGD’s momentum and acceleration terms so that a delta weight no
gradient refreshes decays to exactly 0. The default is self * rhs,
right for floating point; the Q formats round their product toward
zero instead of flooring it.Source§fn mul_round_nearest(self, rhs: Self) -> Self
fn mul_round_nearest(self, rhs: Self) -> Self
self * rhs rounded to nearest, ties away from zero (C++
multiplyRoundNearest), used for SGD’s step learning rate * gradient, so that steps smaller than the format’s resolution do not
all drift one way. The default is self * rhs, right for floating
point; the Q formats round their product to nearest instead of
flooring it.Source§impl<const FRAC: u32> Scalar for QValue<i16, FRAC>
impl<const FRAC: u32> Scalar for QValue<i16, FRAC>
Source§fn mul_round_toward_zero(self, rhs: Self) -> Self
fn mul_round_toward_zero(self, rhs: Self) -> Self
self * rhs rounded toward zero (C++ multiplyRoundTowardZero), used
for SGD’s momentum and acceleration terms so that a delta weight no
gradient refreshes decays to exactly 0. The default is self * rhs,
right for floating point; the Q formats round their product toward
zero instead of flooring it.Source§fn mul_round_nearest(self, rhs: Self) -> Self
fn mul_round_nearest(self, rhs: Self) -> Self
self * rhs rounded to nearest, ties away from zero (C++
multiplyRoundNearest), used for SGD’s step learning rate * gradient, so that steps smaller than the format’s resolution do not
all drift one way. The default is self * rhs, right for floating
point; the Q formats round their product to nearest instead of
flooring it.