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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Deterministic float-to-integer quantisation.
//!
//! Crossing the float/integer boundary needs one house form, because the two
//! plausible conversions disagree: truncation drops the fractional part, and
//! round-to-nearest moves a value up to half a step. Both are stated here
//! explicitly so a caller picks the one its format documents rather than
//! inheriting whatever `as` happens to do.
//!
//! Every function is branch-free apart from its non-finite guard and costs a
//! multiply and a cast. They run per atom, per voxel and per primitive, so the
//! cost of the conversion is the cost of the arithmetic in it.
//!
//! Every conversion is preceded by a clamp onto the destination's exact range
//! and by a non-finite guard. The sibling tests check the boundary behavior.
use ToPrimitive as _;
/// Rounds a unit value to unorm8 with ties resolved upward.
///
/// `value` is clamped to `[0, 1]` first, so the result covers the full `0..=255`
/// range with 255 meaning exactly one. A non-finite input quantises to zero.
/// Rounds a value already expressed in `0..=255` units, ties upward.
///
/// This differs from [`unorm8`] only in the domain it accepts: the caller has
/// already scaled, so no second multiply is applied. A non-finite input
/// quantises to zero.
/// Truncates a non-negative value to `u16`, saturating at the maximum.
///
/// Truncation rather than rounding, because the callers encode a fixed-point
/// magnitude where rounding up could report an extent the source never had.
/// Truncates a unit value onto a grid of `levels + 1` steps.
///
/// Used for spatial cell indices, where the grid is a partition and a value on
/// a cell boundary belongs to the cell above it. A non-finite input lands in
/// cell zero.