1use crate::slot::ScalarType;
4
5#[derive(Debug, Clone, Copy, PartialEq)]
7pub struct QuantSpec {
8 pub step: f64,
9 pub range: Option<(f64, f64)>,
10 pub wire_width: u8,
11}
12
13fn round_to_i64(x: f64) -> i64 {
14 let rounded = libm::round(x);
15 if rounded >= i64::MAX as f64 {
16 i64::MAX
17 } else if rounded <= i64::MIN as f64 {
18 i64::MIN
19 } else {
20 rounded as i64
21 }
22}
23
24fn signed_bounds(width: u8) -> (i64, i64) {
25 match width {
26 0 => (0, 0),
27 64 => (i64::MIN, i64::MAX),
28 w => {
29 let half = 1i64 << (w - 1);
30 (-half, half - 1)
31 }
32 }
33}
34
35pub fn quantise_q(v: f64, spec: &QuantSpec) -> (i64, bool) {
37 let q = round_to_i64(v / spec.step);
38 let (lo, hi) = match spec.range {
39 Some((lo, hi)) => (round_to_i64(lo / spec.step), round_to_i64(hi / spec.step)),
40 None => signed_bounds(spec.wire_width),
41 };
42 let clamped = q.clamp(lo, hi);
43 (clamped, clamped != q)
44}
45
46fn clamp_f64(x: f64, lo: f64, hi: f64) -> f64 {
47 if x < lo {
48 lo
49 } else if x > hi {
50 hi
51 } else {
52 x
53 }
54}
55
56pub fn final_value(x: f64, ty: ScalarType) -> f64 {
58 match ty {
59 ScalarType::F32 => (x as f32) as f64,
60 ScalarType::F64 => x,
61 ScalarType::Bool => f64::from(u8::from(x >= 0.5)),
62 ScalarType::U8 => clamp_f64(libm::round(x), 0.0, u8::MAX as f64),
63 ScalarType::U16 => clamp_f64(libm::round(x), 0.0, u16::MAX as f64),
64 ScalarType::U32 => clamp_f64(libm::round(x), 0.0, u32::MAX as f64),
65 ScalarType::U64 => clamp_f64(libm::round(x), 0.0, u64::MAX as f64),
66 ScalarType::I8 => clamp_f64(libm::round(x), i8::MIN as f64, i8::MAX as f64),
67 ScalarType::I16 => clamp_f64(libm::round(x), i16::MIN as f64, i16::MAX as f64),
68 ScalarType::I32 => clamp_f64(libm::round(x), i32::MIN as f64, i32::MAX as f64),
69 ScalarType::I64 => clamp_f64(libm::round(x), i64::MIN as f64, i64::MAX as f64),
70 }
71}
72
73pub fn quantised_value(q: i64, spec: &QuantSpec, ty: ScalarType) -> f64 {
75 final_value((q as f64) * spec.step, ty)
76}
77
78pub fn raw_value(v: f64, ty: ScalarType) -> f64 {
80 final_value(v, ty)
81}
82
83pub fn deliver(p: f64, quant: Option<&QuantSpec>, ty: ScalarType) -> f64 {
85 match quant {
86 Some(spec) => {
87 let (q, _) = quantise_q(p, spec);
88 quantised_value(q, spec, ty)
89 }
90 None => raw_value(p, ty),
91 }
92}
93
94pub fn truth_value(v: f64, quant: Option<&QuantSpec>, ty: ScalarType) -> (f64, Option<i64>, bool) {
96 match quant {
97 Some(spec) => {
98 let (q, saturated) = quantise_q(v, spec);
99 (quantised_value(q, spec, ty), Some(q), saturated)
100 }
101 None => (raw_value(v, ty), None, false),
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108 use crate::tol::{Tolerance, ToleranceMode, within_tol};
109
110 #[test]
111 fn quantise_half_away_positive_negative() {
112 let spec = QuantSpec {
113 step: 1.0,
114 range: None,
115 wire_width: 32,
116 };
117 assert_eq!(quantise_q(0.5, &spec), (1, false));
118 assert_eq!(quantise_q(-0.5, &spec), (-1, false));
119 assert_eq!(quantise_q(1.5, &spec), (2, false));
120 assert_eq!(quantise_q(-1.5, &spec), (-2, false));
121 }
122
123 #[test]
124 fn quantise_clamps_to_range_and_reports() {
125 let spec = QuantSpec {
126 step: 0.5,
127 range: Some((0.0, 4.0)),
128 wire_width: 8,
129 };
130 assert_eq!(quantise_q(1.5, &spec), (3, false));
131 assert_eq!(quantise_q(10.0, &spec), (8, true));
132 assert_eq!(quantise_q(-10.0, &spec), (0, true));
133 }
134
135 #[test]
136 fn quantise_no_range_clamps_to_type_width() {
137 let spec = QuantSpec {
138 step: 1.0,
139 range: None,
140 wire_width: 8,
141 };
142 assert_eq!(quantise_q(100.0, &spec), (100, false));
143 assert_eq!(quantise_q(200.0, &spec), (127, true));
144 assert_eq!(quantise_q(-200.0, &spec), (-128, true));
145 }
146
147 #[test]
148 fn final_value_f32_rounds_to_f32() {
149 let x = 1.234_567_890_123_45f64;
150 assert_eq!(final_value(x, ScalarType::F32), (x as f32) as f64);
151 assert_eq!(final_value(x, ScalarType::F64), x);
152 }
153
154 #[test]
155 fn final_value_integer_rounds_and_saturates() {
156 assert_eq!(final_value(3.6, ScalarType::I8), 4.0);
157 assert_eq!(final_value(-3.6, ScalarType::I8), -4.0);
158 assert_eq!(final_value(200.0, ScalarType::U8), 200.0);
159 assert_eq!(final_value(300.0, ScalarType::U8), 255.0);
160 assert_eq!(final_value(-200.0, ScalarType::I8), -128.0);
161 }
162
163 #[test]
164 fn final_value_bool_threshold() {
165 assert_eq!(final_value(0.49, ScalarType::Bool), 0.0);
166 assert_eq!(final_value(0.5, ScalarType::Bool), 1.0);
167 assert_eq!(final_value(1.0, ScalarType::Bool), 1.0);
168 }
169
170 #[test]
171 fn integer_field_truth_11_prediction_10_9() {
172 let tol = Tolerance {
173 abs: Some(0.05),
174 rel: None,
175 mode: ToleranceMode::All,
176 };
177 let truth = final_value(11.0, ScalarType::I32);
178 let prediction = 10.9;
179 let delivered = deliver(prediction, None, ScalarType::I32);
180 assert_eq!(delivered, 11.0);
181 assert!(!within_tol(truth, prediction, &tol));
182 assert!(within_tol(truth, delivered, &tol));
183 }
184}