Skip to main content

Module qformat

Module qformat 

Source
Expand description

Signed fixed-point Q-format arithmetic, ported from tinymind (C++) cpp/qformat.hpp.

The C++ QValue<FixedBits, FractionalBits, IsSigned, RoundingPolicy, SaturatePolicy> picks its storage type from FixedBits + FractionalBits at compile time (QTypeChooser). Stable Rust cannot compute a type from const-generic arithmetic (that needs the nightly generic_const_exprs feature), so here the storage type is an explicit parameter: QValue<i32, 16> is Q16.16 and QValue<i16, 8> is Q8.8. The methods and operators are generated for each supported storage type by a macro, each with the wider type C++ uses for products and quotients (i64 for i32, i32 for i16).

Only tinymind (C++)’s default policies are ported: TruncatePolicy (multiplication drops the low bits) and WrapPolicy (addition and subtraction wrap modulo the storage width).

The module also defines Scalar, the value-type trait every network is generic over, implemented for these formats and for f32 and f64 (and the deprecated Fixed, its name up to 0.1.5).

Structs§

QValue
A signed fixed-point value stored in S (i16 or i32) with FRAC fractional bits.

Traits§

FixedDeprecated
The name of Scalar up to 0.1.5, kept so that bounds written V: Fixed still compile. Every Scalar is a Fixed; implement Scalar instead. Fixed has no items of its own, so paths that name them through it (<V as Fixed>::ONE, Fixed::div_int(..)) must name Scalar.
Scalar
A network’s value type: the arithmetic a network needs from the numbers it computes with. This is the Rust counterpart of the implicit “ValueType concept” that the C++ templates rely on: in C++ a missing operator is a template instantiation error, here it is a trait-bound error at the call site.
Sqrt
Square root of a non-negative value (C++ SquareRootApproximation): integer Newton iteration on raw << FRAC in the wide type, which gives the floor of the exact root in raw units. Zero and negative values give zero. The Adam and RMSprop optimizers use it.

Type Aliases§

Q8_8
Q8.8: 8 integer bits (including sign), 8 fractional bits, i16 storage.
Q16_16
Q16.16: 16 integer bits (including sign), 16 fractional bits, i32 storage.