verif_arithmetic/fixed.rs
1//! Q16.16 fixed point.
2//!
3//! The workhorse representation in flight control and motor control:
4//! deterministic, no floating point, no soft-float dependency. The properties
5//! that make it safe to add and to scale are proved in [`crate::prove`].
6
7/// A Q16.16 fixed-point number: 16 integer bits, 16 fractional bits.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
9pub struct Fixed(pub i32);
10
11impl Fixed {
12 /// The scaling factor, `2^16`.
13 pub const SCALE: i32 = 1 << 16;
14 /// One, as Q16.16.
15 pub const ONE: Fixed = Fixed(Self::SCALE);
16 /// Zero.
17 pub const ZERO: Fixed = Fixed(0);
18 /// The largest representable value.
19 pub const MAX: Fixed = Fixed(i32::MAX);
20 /// The smallest representable value.
21 pub const MIN: Fixed = Fixed(i32::MIN);
22
23 /// From an integer, saturating.
24 ///
25 /// The range of Q16.16 in `i32` is `[-32768, 32767]` integers, so `v << 16`
26 /// silently wraps for `|v| >= 32768`: `from_int(32768)` used to give
27 /// `-32768`. A shift is a total function, which is exactly why the wrap is
28 /// invisible. Saturating keeps this module's promise that no primitive
29 /// overflows without saying so.
30 pub const fn from_int(v: i32) -> Fixed {
31 if v > i32::MAX >> 16 {
32 Fixed(i32::MAX)
33 } else if v < i32::MIN >> 16 {
34 Fixed(i32::MIN)
35 } else {
36 Fixed(v << 16)
37 }
38 }
39
40 /// The integer part, truncated toward zero.
41 ///
42 /// Truncation toward zero is not `self.0 >> 16` for negative values: an
43 /// arithmetic shift is a floor, so `-0.5 >> 16` gives `-1`, not `0`. That
44 /// one-bit bias on every negative fractional input is the kind of error a
45 /// control law cannot see. The magnitude is truncated and the sign restored,
46 /// which is `as i32` behaviour.
47 pub const fn to_int(self) -> i32 {
48 if self.0 >= 0 {
49 self.0 >> 16
50 } else {
51 -((self.0.unsigned_abs() >> 16) as i32)
52 }
53 }
54
55 /// Construct from raw bits.
56 pub const fn from_bits(bits: i32) -> Fixed {
57 Fixed(bits)
58 }
59
60 /// The raw bits.
61 pub const fn to_bits(self) -> i32 {
62 self.0
63 }
64
65 /// Saturating addition.
66 pub const fn saturating_add(self, other: Fixed) -> Fixed {
67 Fixed(self.0.saturating_add(other.0))
68 }
69
70 /// Saturating subtraction.
71 pub const fn saturating_sub(self, other: Fixed) -> Fixed {
72 Fixed(self.0.saturating_sub(other.0))
73 }
74
75 /// Multiply by an integer, saturating.
76 pub const fn saturating_mul_int(self, k: i32) -> Fixed {
77 match self.0.checked_mul(k) {
78 Some(v) => Fixed(v),
79 None => {
80 if (self.0 < 0) ^ (k < 0) {
81 Fixed(i32::MIN)
82 } else {
83 Fixed(i32::MAX)
84 }
85 }
86 }
87 }
88
89 /// Truncating division by an integer. `None` when `k == 0`, and also when
90 /// the quotient is not representable -- the single overflowing case,
91 /// `i32::MIN / -1`, whose true result `2^31` is one past `i32::MAX`.
92 /// `checked_div` reports both, so this returns `None` for either rather than
93 /// wrapping to `i32::MIN`.
94 pub const fn checked_div_int(self, k: i32) -> Option<Fixed> {
95 match self.0.checked_div(k) {
96 Some(v) => Some(Fixed(v)),
97 None => None,
98 }
99 }
100}