1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Total integer arithmetic: the operations that *cannot* overflow.
//!
//! Every function here returns an `Option`, and `None` is exactly the case the
//! machine's `+`/`-`/`*` would have wrapped (or, in debug, panicked). [`crate::prove`]
//! carries the proof that the flag is exact rather than a heuristic.
/// `a + b`, or `None` on overflow. Total: no panic, no wrap-around.
pub const
/// `a - b`, or `None` on underflow.
pub const
/// `a * b`, or `None` on overflow.
pub const
/// `a + b`, clamped to the type's maximum.
pub const
/// `a - b`, clamped at zero.
pub const
/// `a * b / d` without overflow in the intermediate product, using a 64-bit
/// widening. Returns `None` when `d == 0` or the true quotient exceeds `u32`.
///
/// The naive `a * b / d` is wrong for exactly the PID-shaped inputs it is used
/// on: at `a = b = 1000` the product `10^6` is fine, but at `a = b = 100_000`
/// the product `10^10` wraps a `u32` before the division can bring it back. The
/// widening is the whole point of the function, and `prove::mul_div_is_exact`
/// is the obligation that says the widened result is the true quotient.
pub const