ph-curves 0.2.0

no-std, no-alloc curve lookup tables and tickless scheduling for embedded Rust
Documentation
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
//! Integer-only transfer functions for physical measurements.
//!
//! Transfer functions map an integer observation, such as an ADC code, to a
//! signed measurement value in a declared scale. Unlike normalized curves,
//! they are not coupled to [`crate::UnitValue`] or tickless scheduling.
//!
//! Inverse conversion maps a physical setpoint back to an observation using
//! the same sparse knot tables — no dense physical-domain LUT.

use crate::round::div_nearest_ties_away;

/// A conversion from an observation to a measurement.
pub trait TransferFunction {
    /// Input observation type.
    type Input: Copy;
    /// Output measurement type.
    type Output: Copy;

    /// Convert an observation to a measurement.
    ///
    /// Inputs outside the table domain follow the table's explicit lower and
    /// upper [`BoundaryBehavior`] settings. Transfer functions never
    /// extrapolate.
    fn convert(&self, input: Self::Input) -> Result<Self::Output, TransferError<Self::Input>>;
}

/// A conversion from a physical measurement back to an observation.
///
/// Parallel to [`TransferFunction`]; not a supertrait, because the map
/// direction and error type differ.
pub trait InverseTransferFunction {
    /// Physical measurement type (input to inverse).
    type Physical: Copy;
    /// Observation type (output of inverse).
    type Observation: Copy;

    /// Invert a physical measurement to an observation.
    ///
    /// Values outside the table's physical range follow the same
    /// [`BoundaryBehavior`] settings as the forward direction, mapped through
    /// the table's [`MonotonicDirection`] so both directions agree about the
    /// same out-of-range condition. On a decreasing table the codes above
    /// `domain_max` are the ones producing physical values below `range_min`,
    /// so `above` governs the low-physical side there. Transfer inverses never
    /// extrapolate.
    fn invert(
        &self,
        physical: Self::Physical,
    ) -> Result<Self::Observation, InverseTransferError<Self::Physical>>;
}

/// Monotonic direction of a transfer function's output.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum MonotonicDirection {
    /// Output values do not decrease as input increases.
    Increasing,
    /// Output values do not increase as input increases.
    Decreasing,
}

/// Behavior for observations outside one side of a transfer domain.
///
/// Both settings are declared against the **observation domain**. For
/// [`InverseTransferFunction`] they are mapped onto the physical range through
/// the table's [`MonotonicDirection`], so `below` governs whichever end of the
/// physical range corresponds to inputs under `domain_min` — the low end on an
/// increasing table, the high end on a decreasing one. See
/// [`PiecewiseLinearTransfer::range_behaviors`].
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum BoundaryBehavior {
    /// Return a domain/range error.
    Error,
    /// Return the nearest endpoint value.
    Clamp,
}

/// How to resolve a physical value that lands on a flat (non-unique) output run.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub enum FlatResolution {
    /// Return the smallest input of the flat run.
    #[default]
    PreferLowInput,
    /// Return the largest input of the flat run.
    PreferHighInput,
    /// Return `(low + high) / 2`, truncating toward the low input.
    Midpoint,
    /// Return [`InverseTransferError::AmbiguousFlat`].
    Error,
}

/// Error returned when an observation is outside a transfer domain, or when
/// affine calibration arithmetic cannot be represented.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TransferError<I> {
    /// The observation is below the minimum supported input.
    BelowDomain {
        /// Observation supplied by the caller.
        input: I,
        /// Smallest supported input.
        minimum: I,
    },
    /// The observation is above the maximum supported input.
    AboveDomain {
        /// Observation supplied by the caller.
        input: I,
        /// Largest supported input.
        maximum: I,
    },
    /// Affine calibration overflowed `i64` intermediates or the final `i32`
    /// result. Domain policy from the inner transfer is unchanged.
    Overflow,
}

/// Error returned when a physical value cannot be inverted uniquely.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum InverseTransferError<P> {
    /// The physical value is below the minimum supported output.
    BelowRange {
        /// Physical value supplied by the caller.
        physical: P,
        /// Smallest supported physical output.
        minimum: P,
    },
    /// The physical value is above the maximum supported output.
    AboveRange {
        /// Physical value supplied by the caller.
        physical: P,
        /// Largest supported physical output.
        maximum: P,
    },
    /// The physical value lies on a flat run and [`FlatResolution::Error`] is set.
    AmbiguousFlat {
        /// Physical value supplied by the caller.
        physical: P,
        /// Smallest observation on the flat run.
        low: u16,
        /// Largest observation on the flat run.
        high: u16,
    },
    /// Undoing affine calibration produced a value outside `i32`, or a range
    /// bound could not be re-expressed in calibrated units.
    Overflow,
}

/// Error returned for invalid standalone segment interpolation arguments.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum InterpolationError {
    /// Segment inputs are not strictly increasing.
    InvalidSpan,
    /// The interpolation input is outside the closed segment.
    OutsideSegment {
        /// Observation supplied by the caller.
        input: u16,
        /// Segment's lower input.
        minimum: u16,
        /// Segment's upper input.
        maximum: u16,
    },
    /// Segment outputs are equal, so the segment cannot be inverted uniquely.
    FlatSegment,
    /// The physical value is outside the closed segment's output span.
    OutsidePhysicalSpan {
        /// Physical value supplied by the caller.
        physical: i32,
        /// Segment's lower output.
        minimum: i32,
        /// Segment's upper output.
        maximum: i32,
    },
}

/// Compact facts recorded by the host generator for a transfer table.
///
/// The error fields describe numerical table and output-quantization error
/// against the configured ideal source. They do not include sensor, component,
/// ADC, model, self-heating, or calibration uncertainty.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct TransferMetadata {
    /// Human-readable input unit, such as `"adc_code"` or `"millivolt"`.
    pub input_unit: &'static str,
    /// Human-readable physical output unit, such as `"degree_celsius"`.
    pub output_unit: &'static str,
    /// Integer output quanta per physical output unit.
    pub output_scale: u32,
    /// Inclusive minimum input.
    pub domain_min: u16,
    /// Inclusive maximum input.
    pub domain_max: u16,
    /// Inclusive minimum physical output (endpoint of the knot range).
    pub range_min: i32,
    /// Inclusive maximum physical output.
    pub range_max: i32,
    /// Output monotonic direction.
    pub direction: MonotonicDirection,
    /// Number of piecewise-linear knots.
    pub knot_count: usize,
    /// True when every adjacent knot pair has unequal outputs.
    pub strictly_monotonic: bool,
    /// Count of adjacent knot pairs with equal outputs.
    pub flat_segment_count: usize,
    /// Requested maximum numerical error in output quanta.
    pub requested_max_error: u32,
    /// Exhaustively measured, conservatively rounded-up maximum error.
    pub achieved_max_error: u32,
    /// Input where the achieved maximum error first occurs.
    pub worst_case_input: u16,
    /// Exhaustively host-measured worst
    /// `|invert(convert(x)) as i32 − x as i32|` over the input domain, in
    /// codes, under the table's default [`FlatResolution`].
    ///
    /// This is a measured round-trip bound, not a promise of identity: a
    /// nonzero value means some observations do not survive a
    /// convert-then-invert cycle exactly. Flat runs are resolved with
    /// [`FlatResolution::PreferLowInput`]; overriding the policy with
    /// [`PiecewiseLinearTransfer::with_flat_resolution`] can exceed this
    /// bound on tables where `flat_segment_count` is nonzero.
    pub achieved_max_inverse_code_error: u16,
}

/// A sparse, nonuniform, piecewise-linear `u16` to `i32` transfer function.
///
/// Inputs are searched in `O(log N)` time. The two arrays use six bytes of
/// table payload per knot and require no allocation. Inverse conversion
/// binary-searches the same output knots — no dense physical→input LUT.
#[derive(Copy, Clone, Debug)]
pub struct PiecewiseLinearTransfer<const N: usize> {
    inputs: &'static [u16; N],
    outputs: &'static [i32; N],
    direction: MonotonicDirection,
    below: BoundaryBehavior,
    above: BoundaryBehavior,
    flat_resolution: FlatResolution,
}

impl<const N: usize> PiecewiseLinearTransfer<N> {
    /// Construct a transfer whose below/above behaviors both default to error.
    ///
    /// Flat runs default to [`FlatResolution::PreferLowInput`].
    ///
    /// # Panics
    ///
    /// Panics while defining the table if it has fewer than two knots, inputs
    /// are not strictly increasing, or outputs violate `direction`. Generated
    /// tables call this in a constant context, making invalid tables a compile
    /// error. Conversion of caller-supplied observations does not panic.
    pub const fn new(
        inputs: &'static [u16; N],
        outputs: &'static [i32; N],
        direction: MonotonicDirection,
    ) -> Self {
        assert!(N >= 2);

        let mut index = 1;
        while index < N {
            assert!(inputs[index] > inputs[index - 1]);
            match direction {
                MonotonicDirection::Increasing => {
                    assert!(outputs[index] >= outputs[index - 1]);
                }
                MonotonicDirection::Decreasing => {
                    assert!(outputs[index] <= outputs[index - 1]);
                }
            }
            index += 1;
        }

        Self {
            inputs,
            outputs,
            direction,
            below: BoundaryBehavior::Error,
            above: BoundaryBehavior::Error,
            flat_resolution: FlatResolution::PreferLowInput,
        }
    }

    /// Set independent below-domain and above-domain behavior.
    ///
    /// Both are declared against the observation domain. Inverse conversion
    /// maps them onto the physical range through the table's direction; see
    /// [`range_behaviors`](Self::range_behaviors).
    pub const fn with_boundaries(
        mut self,
        below: BoundaryBehavior,
        above: BoundaryBehavior,
    ) -> Self {
        self.below = below;
        self.above = above;
        self
    }

    /// Set how flat (equal-output) runs are resolved by [`invert`](InverseTransferFunction::invert).
    pub const fn with_flat_resolution(mut self, policy: FlatResolution) -> Self {
        self.flat_resolution = policy;
        self
    }

    /// Return the input knot array.
    pub const fn inputs(&self) -> &'static [u16; N] {
        self.inputs
    }

    /// Return the output knot array.
    pub const fn outputs(&self) -> &'static [i32; N] {
        self.outputs
    }

    /// Return the output monotonic direction.
    pub const fn direction(&self) -> MonotonicDirection {
        self.direction
    }

    /// Return the below-observation-domain behavior.
    ///
    /// Declared against the observation domain, not the physical range. For
    /// the physical-side policies used by inverse conversion, see
    /// [`range_behaviors`](Self::range_behaviors).
    pub const fn below_behavior(&self) -> BoundaryBehavior {
        self.below
    }

    /// Return the above-observation-domain behavior.
    ///
    /// Declared against the observation domain, not the physical range. For
    /// the physical-side policies used by inverse conversion, see
    /// [`range_behaviors`](Self::range_behaviors).
    pub const fn above_behavior(&self) -> BoundaryBehavior {
        self.above
    }

    /// Return the flat-run resolution policy.
    pub const fn flat_resolution(&self) -> FlatResolution {
        self.flat_resolution
    }

    /// Return the inclusive input domain.
    pub const fn domain(&self) -> (u16, u16) {
        (self.inputs[0], self.inputs[N - 1])
    }

    /// Return the inclusive physical output range as `(min, max)`.
    pub const fn physical_range(&self) -> (i32, i32) {
        let first = self.outputs[0];
        let last = self.outputs[N - 1];
        if first <= last {
            (first, last)
        } else {
            (last, first)
        }
    }

    /// Invert a physical measurement to an observation.
    ///
    /// Convenience alias for [`InverseTransferFunction::invert`].
    pub fn invert_physical(&self, physical: i32) -> Result<u16, InverseTransferError<i32>> {
        InverseTransferFunction::invert(self, physical)
    }

    /// Map the domain policies onto the physical range as
    /// `(low_physical, high_physical)`.
    ///
    /// `below` and `above` are declared against the *observation* domain, so
    /// on a decreasing table they swap: the codes above `domain_max` are the
    /// ones that produce physical values below `range_min`. Selecting by
    /// physical side alone would make a table configured
    /// `below = Error, above = Clamp` clamp in the forward direction and error
    /// in the inverse for the very same out-of-range condition.
    pub const fn range_behaviors(&self) -> (BoundaryBehavior, BoundaryBehavior) {
        match self.direction {
            MonotonicDirection::Increasing => (self.below, self.above),
            MonotonicDirection::Decreasing => (self.above, self.below),
        }
    }

    fn observation_at_physical_end(&self, low_physical: bool) -> u16 {
        match (self.direction, low_physical) {
            (MonotonicDirection::Increasing, true) | (MonotonicDirection::Decreasing, false) => {
                self.inputs[0]
            }
            (MonotonicDirection::Increasing, false) | (MonotonicDirection::Decreasing, true) => {
                self.inputs[N - 1]
            }
        }
    }

    fn resolve_flat_run(
        &self,
        physical: i32,
        left: usize,
        right: usize,
    ) -> Result<u16, InverseTransferError<i32>> {
        let low = self.inputs[left];
        let high = self.inputs[right];
        if left == right {
            return Ok(low);
        }
        match self.flat_resolution {
            FlatResolution::PreferLowInput => Ok(low),
            FlatResolution::PreferHighInput => Ok(high),
            FlatResolution::Midpoint => Ok(low + (high - low) / 2),
            FlatResolution::Error => Err(InverseTransferError::AmbiguousFlat {
                physical,
                low,
                high,
            }),
        }
    }

    fn expand_flat_run(&self, index: usize) -> (usize, usize) {
        let value = self.outputs[index];
        let mut left = index;
        while left > 0 && self.outputs[left - 1] == value {
            left -= 1;
        }
        let mut right = index;
        while right + 1 < N && self.outputs[right + 1] == value {
            right += 1;
        }
        (left, right)
    }

    /// Largest knot index whose output is on the inclusive low-physical side
    /// of `physical` for the table's monotonic direction.
    fn largest_index_at_or_past(&self, physical: i32) -> usize {
        let mut low = 0usize;
        let mut high = N - 1;
        while low < high {
            let middle = low + (high - low).div_ceil(2);
            let past = match self.direction {
                MonotonicDirection::Increasing => self.outputs[middle] <= physical,
                MonotonicDirection::Decreasing => self.outputs[middle] >= physical,
            };
            if past {
                low = middle;
            } else {
                high = middle - 1;
            }
        }
        low
    }
}

impl<const N: usize> TransferFunction for PiecewiseLinearTransfer<N> {
    type Input = u16;
    type Output = i32;

    fn convert(&self, input: u16) -> Result<i32, TransferError<u16>> {
        let minimum = self.inputs[0];
        let maximum = self.inputs[N - 1];

        if input < minimum {
            return match self.below {
                BoundaryBehavior::Error => Err(TransferError::BelowDomain { input, minimum }),
                BoundaryBehavior::Clamp => Ok(self.outputs[0]),
            };
        }
        if input > maximum {
            return match self.above {
                BoundaryBehavior::Error => Err(TransferError::AboveDomain { input, maximum }),
                BoundaryBehavior::Clamp => Ok(self.outputs[N - 1]),
            };
        }
        if input == minimum {
            return Ok(self.outputs[0]);
        }
        if input == maximum {
            return Ok(self.outputs[N - 1]);
        }

        let mut low = 0usize;
        let mut high = N - 1;
        while low + 1 < high {
            let middle = low + (high - low) / 2;
            match input.cmp(&self.inputs[middle]) {
                core::cmp::Ordering::Less => high = middle,
                core::cmp::Ordering::Equal => return Ok(self.outputs[middle]),
                core::cmp::Ordering::Greater => low = middle,
            }
        }

        Ok(interpolate_valid_segment(
            input,
            self.inputs[low],
            self.outputs[low],
            self.inputs[high],
            self.outputs[high],
        ))
    }
}

impl<const N: usize> InverseTransferFunction for PiecewiseLinearTransfer<N> {
    type Physical = i32;
    type Observation = u16;

    fn invert(&self, physical: i32) -> Result<u16, InverseTransferError<i32>> {
        let (minimum, maximum) = self.physical_range();
        let (low_physical, high_physical) = self.range_behaviors();

        if physical < minimum {
            return match low_physical {
                BoundaryBehavior::Error => {
                    Err(InverseTransferError::BelowRange { physical, minimum })
                }
                BoundaryBehavior::Clamp => Ok(self.observation_at_physical_end(true)),
            };
        }
        if physical > maximum {
            return match high_physical {
                BoundaryBehavior::Error => {
                    Err(InverseTransferError::AboveRange { physical, maximum })
                }
                BoundaryBehavior::Clamp => Ok(self.observation_at_physical_end(false)),
            };
        }

        let index = self.largest_index_at_or_past(physical);
        if self.outputs[index] == physical {
            let (left, right) = self.expand_flat_run(index);
            return self.resolve_flat_run(physical, left, right);
        }

        // `index` is the last knot on the inclusive low-physical side, so the
        // bracketing sloped segment is `index .. index + 1`.
        debug_assert!(index + 1 < N);
        Ok(invert_valid_segment(
            physical,
            self.inputs[index],
            self.outputs[index],
            self.inputs[index + 1],
            self.outputs[index + 1],
        ))
    }
}

/// Interpolate one signed integer segment with nearest, ties-away rounding.
///
/// The input must lie within the closed segment and `x1` must be greater than
/// `x0`. All arithmetic uses `i64`; the full `u16`/`i32` ranges are safe.
pub fn interpolate_segment(
    input: u16,
    x0: u16,
    y0: i32,
    x1: u16,
    y1: i32,
) -> Result<i32, InterpolationError> {
    if x1 <= x0 {
        return Err(InterpolationError::InvalidSpan);
    }
    if input < x0 || input > x1 {
        return Err(InterpolationError::OutsideSegment {
            input,
            minimum: x0,
            maximum: x1,
        });
    }
    Ok(interpolate_valid_segment(input, x0, y0, x1, y1))
}

/// Invert one signed integer segment with nearest, ties-away rounding.
///
/// The mirror of [`interpolate_segment`]. `physical` must lie within the
/// closed output span, `x1` must be greater than `x0`, and the segment must
/// not be flat. All arithmetic uses `i64`; the full `u16`/`i32` ranges are
/// safe. Host tools use this so a generated round-trip audit measures the
/// same arithmetic the runtime performs.
pub fn invert_segment(
    physical: i32,
    x0: u16,
    y0: i32,
    x1: u16,
    y1: i32,
) -> Result<u16, InterpolationError> {
    if x1 <= x0 {
        return Err(InterpolationError::InvalidSpan);
    }
    if y0 == y1 {
        return Err(InterpolationError::FlatSegment);
    }
    let (minimum, maximum) = if y0 < y1 { (y0, y1) } else { (y1, y0) };
    if physical < minimum || physical > maximum {
        return Err(InterpolationError::OutsidePhysicalSpan {
            physical,
            minimum,
            maximum,
        });
    }
    Ok(invert_valid_segment(physical, x0, y0, x1, y1))
}

fn interpolate_valid_segment(input: u16, x0: u16, y0: i32, x1: u16, y1: i32) -> i32 {
    let offset = i64::from(input - x0);
    let span = i64::from(x1 - x0);
    let delta = i64::from(y1) - i64::from(y0);
    let numerator = i64::from(y0) * span + delta * offset;
    let result = div_nearest_ties_away(numerator, span);
    debug_assert!((i64::from(i32::MIN)..=i64::from(i32::MAX)).contains(&result));
    result as i32
}

fn invert_valid_segment(physical: i32, x0: u16, y0: i32, x1: u16, y1: i32) -> u16 {
    let dy = i64::from(y1) - i64::from(y0);
    debug_assert!(dy != 0);
    let dx = i64::from(x1) - i64::from(x0);
    let numerator = i64::from(x0) * dy + (i64::from(physical) - i64::from(y0)) * dx;
    let result = div_nearest_ties_away(numerator, dy);
    result.clamp(i64::from(x0), i64::from(x1)) as u16
}

/// Runtime/factory gain-and-offset wrapper around an inner transfer.
///
/// Applies `y' = (y * gain + offset) / scale` with `i64` intermediates and
/// nearest, ties-away-from-zero rounding. The caller supplies the integer
/// triple (for example from EEPROM or flash); this type never reads NVM,
/// regenerates knot tables, or updates [`TransferMetadata`].
///
/// Identity (modulo rounding when `|scale| ≠ 1`) is `gain = scale` and
/// `offset = 0`. A negative `gain` or `scale` is allowed and flips sense.
/// Nesting multiple wrappers is permitted via [`TransferFunction`], but
/// precision loss and overflow risk stack with each layer.
///
/// # Inverse
///
/// When the inner type implements [`InverseTransferFunction`], so does the
/// wrapper: [`invert`](InverseTransferFunction::invert) undoes the affine with
/// `y = (y' * scale - offset) / gain` and then inverts the inner transfer.
/// That is what makes a calibrated setpoint — "which ADC code reads 25 °C
/// *after* this unit's factory calibration?" — a single call.
///
/// Because `gain` must be nonzero for the affine to be invertible,
/// [`AffineCalibration::new`] rejects `gain == 0` outright rather than
/// deferring the failure to `invert`.
///
/// # Numerical scope
///
/// For any `i32` `y`, `gain`, and `offset`, the product/sum
/// `y * gain + offset` always fits in `i64`; the same holds for
/// `y' * scale - offset` on the inverse path. Both directions still report
/// overflow — [`TransferError::Overflow`] forward,
/// [`InverseTransferError::Overflow`] inverse — when the result does not fit
/// `i32`.
///
/// Both directions round, so a convert-then-invert round trip through a
/// calibration is bounded, not exact. A calibration that compresses the
/// physical scale cannot restore what the forward quantization discarded.
/// When that compression would make `unapply` overshoot an inner endpoint
/// for a calibrated value that is still in the forward image,
/// [`invert`](InverseTransferFunction::invert) clamps to the endpoint rather
/// than returning a spurious range error — so `invert(convert(x))` stays
/// in-domain for every in-domain observation `x`.
#[derive(Copy, Clone, Debug)]
pub struct AffineCalibration<T> {
    inner: T,
    gain: i32,
    offset: i32,
    scale: i32,
}

/// Error returned when affine calibration constants are invalid.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum AffineCalibrationError {
    /// The scale divisor is zero.
    ZeroScale,
    /// The gain is zero, which collapses every observation onto one output.
    ZeroGain,
}

impl<T> AffineCalibration<T> {
    /// Wrap `inner` with affine calibration constants.
    ///
    /// # Errors
    ///
    /// Returns [`AffineCalibrationError::ZeroScale`] if `scale == 0`, or
    /// [`AffineCalibrationError::ZeroGain`] if `gain == 0`. A zero gain maps
    /// every observation onto the single value `offset / scale`, discarding
    /// the sensor and leaving the calibration non-invertible.
    pub fn new(
        inner: T,
        gain: i32,
        offset: i32,
        scale: i32,
    ) -> Result<Self, AffineCalibrationError> {
        if scale == 0 {
            return Err(AffineCalibrationError::ZeroScale);
        }
        if gain == 0 {
            return Err(AffineCalibrationError::ZeroGain);
        }
        Ok(Self {
            inner,
            gain,
            offset,
            scale,
        })
    }

    /// Return a reference to the inner transfer.
    pub const fn inner(&self) -> &T {
        &self.inner
    }

    /// Return the gain coefficient.
    pub const fn gain(&self) -> i32 {
        self.gain
    }

    /// Return the offset term.
    pub const fn offset(&self) -> i32 {
        self.offset
    }

    /// Return the nonzero scale divisor.
    pub const fn scale(&self) -> i32 {
        self.scale
    }
}

impl<T> TransferFunction for AffineCalibration<T>
where
    T: TransferFunction<Output = i32>,
{
    type Input = T::Input;
    type Output = i32;

    fn convert(&self, input: Self::Input) -> Result<i32, TransferError<Self::Input>> {
        let y = self.inner.convert(input)?;
        apply_affine_i64(y, self.gain, self.offset, self.scale)
    }
}

impl<T> InverseTransferFunction for AffineCalibration<T>
where
    T: InverseTransferFunction<Physical = i32>,
{
    type Physical = i32;
    type Observation = T::Observation;

    /// Undo the calibration, then invert the inner transfer.
    ///
    /// Range errors from the inner transfer are re-expressed in calibrated
    /// units, so `minimum` / `maximum` are directly comparable with the value
    /// the caller passed in. A calibration with `gain` and `scale` of opposite
    /// signs reverses orientation, which turns an inner `BelowRange` into an
    /// `AboveRange` and vice versa.
    ///
    /// When `|scale| > |gain|`, undoing the affine can land just outside the
    /// inner physical range even though `physical` is in the forward image of
    /// that range (the classic `invert(convert(endpoint))` compression case).
    /// Those values are clamped to the inner endpoint before the second
    /// invert attempt so in-domain calibrated setpoints never spuriously
    /// range-error. Values outside the calibrated forward image still report
    /// [`InverseTransferError::BelowRange`] /
    /// [`InverseTransferError::AboveRange`].
    fn invert(&self, physical: i32) -> Result<T::Observation, InverseTransferError<i32>> {
        let uncalibrated = unapply_affine_i64(physical, self.gain, self.offset, self.scale)?;
        match self.inner.invert(uncalibrated) {
            Ok(observation) => Ok(observation),
            Err(error) => self.recover_compressed_endpoint(physical, error),
        }
    }
}

impl<T> AffineCalibration<T> {
    /// True when the calibration preserves the inner transfer's orientation.
    const fn preserves_orientation(&self) -> bool {
        (self.gain > 0) == (self.scale > 0)
    }

    /// Re-express an inner range error in calibrated units.
    ///
    /// `physical` is echoed back unchanged — it is what the caller supplied.
    /// The bound is mapped forward through the affine, and the variant flips
    /// when the calibration reverses orientation.
    fn recalibrate_error(
        &self,
        physical: i32,
        error: InverseTransferError<i32>,
    ) -> InverseTransferError<i32> {
        let (bound, was_low) = match error {
            InverseTransferError::BelowRange { minimum, .. } => (minimum, true),
            InverseTransferError::AboveRange { maximum, .. } => (maximum, false),
            InverseTransferError::AmbiguousFlat { low, high, .. } => {
                return InverseTransferError::AmbiguousFlat {
                    physical,
                    low,
                    high,
                };
            }
            InverseTransferError::Overflow => return InverseTransferError::Overflow,
        };

        let Ok(calibrated) = apply_affine_i64::<i32>(bound, self.gain, self.offset, self.scale)
        else {
            return InverseTransferError::Overflow;
        };

        if was_low == self.preserves_orientation() {
            InverseTransferError::BelowRange {
                physical,
                minimum: calibrated,
            }
        } else {
            InverseTransferError::AboveRange {
                physical,
                maximum: calibrated,
            }
        }
    }

    /// If compression/rounding pushed `unapply` past an inner endpoint while
    /// `physical` is still inside the forward image, clamp to that endpoint
    /// and retry; otherwise surface the recalibrated range error.
    fn recover_compressed_endpoint(
        &self,
        physical: i32,
        error: InverseTransferError<i32>,
    ) -> Result<T::Observation, InverseTransferError<i32>>
    where
        T: InverseTransferFunction<Physical = i32>,
    {
        let bound = match error {
            InverseTransferError::BelowRange { minimum, .. } => minimum,
            InverseTransferError::AboveRange { maximum, .. } => maximum,
            other => return Err(self.recalibrate_error(physical, other)),
        };

        let calibrated_error = self.recalibrate_error(physical, error);
        let inside_forward_image = match calibrated_error {
            InverseTransferError::BelowRange { minimum, .. } => physical >= minimum,
            InverseTransferError::AboveRange { maximum, .. } => physical <= maximum,
            _ => false,
        };

        if !inside_forward_image {
            return Err(calibrated_error);
        }

        self.inner
            .invert(bound)
            .map_err(|retry_error| self.recalibrate_error(physical, retry_error))
    }
}

/// Apply `y' = (y * gain + offset) / scale` with checked `i64` math.
///
/// Rounding is nearest, ties away from zero. `scale` must be nonzero; callers
/// such as [`AffineCalibration::new`] validate that before invocation.
fn apply_affine_i64<I>(
    y: i32,
    gain: i32,
    offset: i32,
    scale: i32,
) -> Result<i32, TransferError<I>> {
    debug_assert!(scale != 0);

    let product = i64::from(y)
        .checked_mul(i64::from(gain))
        .ok_or(TransferError::Overflow)?;
    let numerator = product
        .checked_add(i64::from(offset))
        .ok_or(TransferError::Overflow)?;
    let scaled = div_nearest_ties_away(numerator, i64::from(scale));
    i32::try_from(scaled).map_err(|_| TransferError::Overflow)
}

/// Undo `y' = (y * gain + offset) / scale`, recovering `y`.
///
/// Solves `y = (y' * scale - offset) / gain` with the same nearest,
/// ties-away rounding. `gain` and `scale` must both be nonzero;
/// [`AffineCalibration::new`] enforces that.
///
/// Both directions round, so `unapply(apply(y))` is bounded rather than exact:
/// a calibration that compresses the physical scale cannot restore what the
/// forward quantization discarded.
fn unapply_affine_i64(
    calibrated: i32,
    gain: i32,
    offset: i32,
    scale: i32,
) -> Result<i32, InverseTransferError<i32>> {
    debug_assert!(gain != 0 && scale != 0);

    // `|calibrated * scale|` is at most `2^62`, so neither step can overflow
    // `i64` for any `i32` operands.
    let product = i64::from(calibrated)
        .checked_mul(i64::from(scale))
        .ok_or(InverseTransferError::Overflow)?;
    let numerator = product
        .checked_sub(i64::from(offset))
        .ok_or(InverseTransferError::Overflow)?;
    let uncalibrated = div_nearest_ties_away(numerator, i64::from(gain));
    i32::try_from(uncalibrated).map_err(|_| InverseTransferError::Overflow)
}

#[cfg(test)]
mod tests {
    extern crate std;

    use super::*;

    static INPUTS: [u16; 3] = [100, 200, 400];
    static OUTPUTS: [i32; 3] = [-1_000, 0, 2_000];
    static DECREASING: [i32; 3] = [2_000, 0, -1_000];
    static FLAT_OUTPUTS: [i32; 4] = [0, 10, 10, 20];
    static FLAT_INPUTS: [u16; 4] = [0, 10, 20, 30];

    #[test]
    fn exact_knots_and_binary_search() {
        let transfer =
            PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        assert_eq!(transfer.convert(100), Ok(-1_000));
        assert_eq!(transfer.convert(200), Ok(0));
        assert_eq!(transfer.convert(400), Ok(2_000));
        assert_eq!(transfer.convert(150), Ok(-500));
        assert_eq!(transfer.convert(300), Ok(1_000));
    }

    #[test]
    fn independent_boundary_behavior() {
        let transfer =
            PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing)
                .with_boundaries(BoundaryBehavior::Clamp, BoundaryBehavior::Error);
        assert_eq!(transfer.convert(99), Ok(-1_000));
        assert_eq!(
            transfer.convert(401),
            Err(TransferError::AboveDomain {
                input: 401,
                maximum: 400
            })
        );
    }

    #[test]
    fn decreasing_signed_transfer() {
        let transfer =
            PiecewiseLinearTransfer::new(&INPUTS, &DECREASING, MonotonicDirection::Decreasing);
        assert_eq!(transfer.convert(150), Ok(1_000));
        assert_eq!(transfer.convert(300), Ok(-500));
    }

    #[test]
    fn signed_rounding_ties_away_from_zero() {
        assert_eq!(interpolate_segment(1, 0, 0, 2, 1), Ok(1));
        assert_eq!(interpolate_segment(1, 0, 0, 2, -1), Ok(-1));
        assert_eq!(interpolate_segment(1, 0, -10, 2, -9), Ok(-10));
        assert_eq!(interpolate_segment(1, 0, 10, 2, 9), Ok(10));
        assert_eq!(interpolate_segment(1, 0, 10, 3, 11), Ok(10));
        assert_eq!(interpolate_segment(2, 0, 10, 3, 11), Ok(11));
        assert_eq!(interpolate_segment(1, 0, -10, 3, -11), Ok(-10));
        assert_eq!(interpolate_segment(2, 0, -10, 3, -11), Ok(-11));
    }

    #[test]
    fn full_integer_ranges_are_safe() {
        assert_eq!(
            interpolate_segment(0, 0, i32::MIN, u16::MAX, i32::MAX),
            Ok(i32::MIN)
        );
        assert_eq!(
            interpolate_segment(u16::MAX, 0, i32::MIN, u16::MAX, i32::MAX),
            Ok(i32::MAX)
        );
        assert_eq!(
            interpolate_segment(0, 0, i32::MAX, u16::MAX, i32::MIN),
            Ok(i32::MAX)
        );
        assert_eq!(
            interpolate_segment(u16::MAX, 0, i32::MAX, u16::MAX, i32::MIN),
            Ok(i32::MIN)
        );
    }

    #[test]
    fn exhaustive_full_span_is_bounded_and_monotonic() {
        let mut previous_increasing = i32::MIN;
        let mut previous_decreasing = i32::MAX;
        for input in 0..=u16::MAX {
            let increasing = interpolate_segment(input, 0, i32::MIN, u16::MAX, i32::MAX).unwrap();
            let decreasing = interpolate_segment(input, 0, i32::MAX, u16::MAX, i32::MIN).unwrap();
            assert!(increasing >= previous_increasing);
            assert!(decreasing <= previous_decreasing);
            previous_increasing = increasing;
            previous_decreasing = decreasing;
        }
        assert_eq!(previous_increasing, i32::MAX);
        assert_eq!(previous_decreasing, i32::MIN);
    }

    #[test]
    fn constructor_rejects_invalid_tables() {
        static DUPLICATE_INPUTS: [u16; 2] = [10, 10];
        static DESCENDING_OUTPUTS: [i32; 2] = [10, 0];

        assert!(
            std::panic::catch_unwind(|| {
                PiecewiseLinearTransfer::new(
                    &DUPLICATE_INPUTS,
                    &DESCENDING_OUTPUTS,
                    MonotonicDirection::Decreasing,
                )
            })
            .is_err()
        );
        assert!(
            std::panic::catch_unwind(|| {
                PiecewiseLinearTransfer::new(
                    &[10, 20],
                    &DESCENDING_OUTPUTS,
                    MonotonicDirection::Increasing,
                )
            })
            .is_err()
        );
    }

    #[test]
    fn standalone_interpolation_validates_arguments() {
        assert_eq!(
            interpolate_segment(10, 10, 0, 10, 1),
            Err(InterpolationError::InvalidSpan)
        );
        assert_eq!(
            interpolate_segment(9, 10, 0, 20, 1),
            Err(InterpolationError::OutsideSegment {
                input: 9,
                minimum: 10,
                maximum: 20
            })
        );
    }

    #[test]
    fn standalone_inversion_validates_arguments() {
        assert_eq!(
            invert_segment(0, 10, 0, 10, 1),
            Err(InterpolationError::InvalidSpan)
        );
        assert_eq!(
            invert_segment(5, 10, 7, 20, 7),
            Err(InterpolationError::FlatSegment)
        );
        assert_eq!(
            invert_segment(21, 10, 0, 20, 20),
            Err(InterpolationError::OutsidePhysicalSpan {
                physical: 21,
                minimum: 0,
                maximum: 20
            })
        );
        // A decreasing segment reports its span low-to-high.
        assert_eq!(
            invert_segment(25, 10, 20, 20, 0),
            Err(InterpolationError::OutsidePhysicalSpan {
                physical: 25,
                minimum: 0,
                maximum: 20
            })
        );
    }

    #[test]
    fn affine_identity_and_factory_scale() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        let identity = AffineCalibration::new(base, 1, 0, 1).unwrap();
        assert_eq!(identity.convert(150), Ok(-500));
        assert_eq!(identity.gain(), 1);
        assert_eq!(identity.offset(), 0);
        assert_eq!(identity.scale(), 1);
        assert_eq!(identity.inner().convert(150), Ok(-500));

        let cal = AffineCalibration::new(base, 1005, -120, 1000).unwrap();
        // (-500 * 1005 + -120) / 1000 = -502.62 → -503 (ties-away / nearest)
        assert_eq!(cal.convert(150), Ok(-503));
        // (0 * 1005 + -120) / 1000 = -0.12 → 0
        assert_eq!(cal.convert(200), Ok(0));
        // (2000 * 1005 + -120) / 1000 = 2009.88 → 2010
        assert_eq!(cal.convert(400), Ok(2_010));
    }

    #[test]
    fn affine_rounding_ties_away_and_negative_scale() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        let half_up = AffineCalibration::new(base, 1, 0, 2).unwrap();
        // 2000 / 2 = 1000 exact; 0 / 2 = 0; -1000 / 2 = -500
        assert_eq!(half_up.convert(400), Ok(1_000));
        assert_eq!(half_up.convert(200), Ok(0));

        // 1/2 → 1 and -1/2 → -1 (ties away from zero)
        assert_eq!(apply_affine_i64::<u16>(1, 1, 0, 2), Ok(1));
        assert_eq!(apply_affine_i64::<u16>(-1, 1, 0, 2), Ok(-1));
        assert_eq!(apply_affine_i64::<u16>(1, 1, 0, -2), Ok(-1));
        assert_eq!(apply_affine_i64::<u16>(-1, 1, 0, -2), Ok(1));
        assert_eq!(apply_affine_i64::<u16>(3, 1, 0, 2), Ok(2));
        assert_eq!(apply_affine_i64::<u16>(-3, 1, 0, 2), Ok(-2));
    }

    #[test]
    fn affine_propagates_domain_errors_and_rejects_overflow() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        let cal = AffineCalibration::new(base, 1, 0, 1).unwrap();
        assert_eq!(
            cal.convert(99),
            Err(TransferError::BelowDomain {
                input: 99,
                minimum: 100
            })
        );

        // Large gain maps an in-domain knot outside i32.
        let overflow = AffineCalibration::new(base, i32::MAX, 0, 1).unwrap();
        assert_eq!(overflow.convert(400), Err(TransferError::Overflow));
        assert_eq!(
            apply_affine_i64::<u16>(i32::MAX, 2, 0, 1),
            Err(TransferError::Overflow)
        );
    }

    #[test]
    fn standalone_inversion_matches_the_table_path() {
        // Same knots the increasing fixture table uses for its first segment.
        for physical in -1_000..=0 {
            assert_eq!(
                invert_segment(physical, 100, -1_000, 200, 0),
                Ok(invert_valid_segment(physical, 100, -1_000, 200, 0)),
                "physical {physical}"
            );
        }
        assert_eq!(invert_segment(-500, 100, -1_000, 200, 0), Ok(150));
        assert_eq!(invert_segment(1_000, 200, 0, 400, 2_000), Ok(300));
    }

    #[test]
    fn invert_exact_knots_and_midpoints() {
        let transfer =
            PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        assert_eq!(transfer.invert_physical(-1_000), Ok(100));
        assert_eq!(transfer.invert_physical(0), Ok(200));
        assert_eq!(transfer.invert_physical(2_000), Ok(400));
        assert_eq!(transfer.invert_physical(-500), Ok(150));
        assert_eq!(transfer.invert_physical(1_000), Ok(300));
    }

    #[test]
    fn invert_decreasing_maps_by_physical_range() {
        let transfer =
            PiecewiseLinearTransfer::new(&INPUTS, &DECREASING, MonotonicDirection::Decreasing)
                .with_boundaries(BoundaryBehavior::Clamp, BoundaryBehavior::Clamp);
        assert_eq!(transfer.invert_physical(1_000), Ok(150));
        assert_eq!(transfer.invert_physical(-500), Ok(300));
        // Below physical min (-1000) clamps to the high-input endpoint.
        assert_eq!(transfer.invert_physical(-2_000), Ok(400));
        // Above physical max (2000) clamps to the low-input endpoint.
        assert_eq!(transfer.invert_physical(3_000), Ok(100));
    }

    #[test]
    fn boundary_policy_agrees_between_directions_on_a_decreasing_table() {
        // NTC-shaped: decreasing, error under domain_min, clamp over domain_max.
        let transfer =
            PiecewiseLinearTransfer::new(&INPUTS, &DECREASING, MonotonicDirection::Decreasing)
                .with_boundaries(BoundaryBehavior::Error, BoundaryBehavior::Clamp);

        // `above` governs codes over domain_max, which are exactly the codes
        // producing physical values under range_min. Both directions clamp.
        assert_eq!(transfer.convert(500), Ok(-1_000));
        assert_eq!(transfer.invert_physical(-2_000), Ok(400));

        // `below` governs codes under domain_min, which produce physical
        // values over range_max. Both directions error.
        assert_eq!(
            transfer.convert(99),
            Err(TransferError::BelowDomain {
                input: 99,
                minimum: 100
            })
        );
        assert_eq!(
            transfer.invert_physical(3_000),
            Err(InverseTransferError::AboveRange {
                physical: 3_000,
                maximum: 2_000
            })
        );

        assert_eq!(
            transfer.range_behaviors(),
            (BoundaryBehavior::Clamp, BoundaryBehavior::Error)
        );
    }

    #[test]
    fn boundary_policy_is_unswapped_on_an_increasing_table() {
        let transfer =
            PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing)
                .with_boundaries(BoundaryBehavior::Error, BoundaryBehavior::Clamp);

        assert_eq!(
            transfer.range_behaviors(),
            (BoundaryBehavior::Error, BoundaryBehavior::Clamp)
        );
        assert_eq!(transfer.convert(500), Ok(2_000));
        assert_eq!(transfer.invert_physical(3_000), Ok(400));
        assert_eq!(
            transfer.invert_physical(-2_000),
            Err(InverseTransferError::BelowRange {
                physical: -2_000,
                minimum: -1_000
            })
        );
    }

    #[test]
    fn invert_range_errors_use_physical_bounds() {
        let transfer =
            PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        assert_eq!(
            transfer.invert(-1_001),
            Err(InverseTransferError::BelowRange {
                physical: -1_001,
                minimum: -1_000
            })
        );
        assert_eq!(
            transfer.invert(2_001),
            Err(InverseTransferError::AboveRange {
                physical: 2_001,
                maximum: 2_000
            })
        );
    }

    #[test]
    fn flat_resolution_policies() {
        let base = PiecewiseLinearTransfer::new(
            &FLAT_INPUTS,
            &FLAT_OUTPUTS,
            MonotonicDirection::Increasing,
        );

        assert_eq!(
            base.with_flat_resolution(FlatResolution::PreferLowInput)
                .invert(10),
            Ok(10)
        );
        assert_eq!(
            base.with_flat_resolution(FlatResolution::PreferHighInput)
                .invert(10),
            Ok(20)
        );
        assert_eq!(
            base.with_flat_resolution(FlatResolution::Midpoint)
                .invert(10),
            Ok(15)
        );
        assert_eq!(
            base.with_flat_resolution(FlatResolution::Error).invert(10),
            Err(InverseTransferError::AmbiguousFlat {
                physical: 10,
                low: 10,
                high: 20
            })
        );
        // Unique knot: FlatResolution::Error is unused.
        assert_eq!(
            base.with_flat_resolution(FlatResolution::Error).invert(0),
            Ok(0)
        );
        assert_eq!(base.invert(5), Ok(5));
    }

    #[test]
    fn invert_segment_rounding_ties_away() {
        // Forward: input 1 on [0,2] with y 0→1 rounds to 1.
        // Inverse of that physical should prefer the observation side of the tie.
        assert_eq!(invert_valid_segment(1, 0, 0, 2, 2), 1);
        assert_eq!(invert_valid_segment(-1, 0, 0, 2, -2), 1);
        // Half-quantum ties away from zero along the input axis from x0.
        assert_eq!(invert_valid_segment(1, 0, 0, 4, 2), 2);
        assert_eq!(invert_valid_segment(-1, 0, 0, 4, -2), 2);
    }

    #[test]
    fn round_trip_invert_convert_on_non_flat_table() {
        let transfer =
            PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        for input in INPUTS[0]..=INPUTS[INPUTS.len() - 1] {
            let physical = transfer.convert(input).unwrap();
            let recovered = transfer.invert(physical).unwrap();
            let distance = i32::from(recovered).abs_diff(i32::from(input));
            assert!(
                distance <= 1,
                "input {input}: invert(convert) -> {recovered} (Δ={distance})"
            );
        }
    }

    #[test]
    fn div_nearest_ties_away_matches_forward_policy() {
        assert_eq!(div_nearest_ties_away(1, 2), 1);
        assert_eq!(div_nearest_ties_away(-1, 2), -1);
        assert_eq!(div_nearest_ties_away(1, -2), -1);
        assert_eq!(div_nearest_ties_away(-1, -2), 1);
        assert_eq!(div_nearest_ties_away(3, 2), 2);
        assert_eq!(div_nearest_ties_away(-3, 2), -2);
    }

    #[test]
    fn affine_constructor_returns_error_for_zero_scale() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        assert!(matches!(
            AffineCalibration::new(base, 1, 0, 0),
            Err(AffineCalibrationError::ZeroScale)
        ));
    }

    #[test]
    fn affine_nesting_composes() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        let inner = AffineCalibration::new(base, 2, 10, 1).unwrap();
        let outer = AffineCalibration::new(inner, 1, -10, 2).unwrap();
        // y=0 → (0*2+10)=10 → (10-10)/2 = 0
        assert_eq!(outer.convert(200), Ok(0));
        // y=2000 → 4010 → (4010-10)/2 = 2000
        assert_eq!(outer.convert(400), Ok(2_000));
    }

    #[test]
    fn affine_constructor_returns_error_for_zero_gain() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        assert!(matches!(
            AffineCalibration::new(base, 0, 5, 1),
            Err(AffineCalibrationError::ZeroGain)
        ));
    }

    #[test]
    fn calibrated_inverse_round_trips_through_the_table() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        let cal = AffineCalibration::new(base, 1_005, -120, 1_000).unwrap();

        // Every in-domain code survives convert-then-invert on this table.
        for code in INPUTS[0]..=INPUTS[INPUTS.len() - 1] {
            let calibrated = cal.convert(code).unwrap();
            let recovered = cal.invert(calibrated).unwrap();
            assert!(
                recovered.abs_diff(code) <= 1,
                "code {code}: convert -> {calibrated} -> invert -> {recovered}"
            );
        }
    }

    #[test]
    fn calibrated_inverse_survives_compressing_calibration_at_endpoints() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        // |scale| > |gain|: unapply expands and can overshoot an inner endpoint
        // even when the calibrated value is exactly convert(endpoint).
        let cal = AffineCalibration::new(base, 2, 0, 3).unwrap();

        let low = cal.convert(100).unwrap();
        assert_eq!(low, -667);
        assert_eq!(cal.invert(low), Ok(100));

        let high = cal.convert(400).unwrap();
        assert_eq!(high, 1_333);
        assert_eq!(cal.invert(high), Ok(400));

        for code in INPUTS[0]..=INPUTS[INPUTS.len() - 1] {
            let calibrated = cal.convert(code).unwrap();
            let recovered = cal.invert(calibrated).unwrap();
            assert!(
                recovered.abs_diff(code) <= 1,
                "code {code}: convert -> {calibrated} -> invert -> {recovered}"
            );
        }

        // Truly outside the calibrated forward image still range-errors.
        assert_eq!(
            cal.invert(-668),
            Err(InverseTransferError::BelowRange {
                physical: -668,
                minimum: -667
            })
        );
        assert_eq!(
            cal.invert(1_334),
            Err(InverseTransferError::AboveRange {
                physical: 1_334,
                maximum: 1_333
            })
        );
    }

    #[test]
    fn compressing_calibration_still_flips_orientation_on_range_errors() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        let flipped = AffineCalibration::new(base, -2, 0, 3).unwrap();
        assert!(!flipped.preserves_orientation());

        let low_obs = flipped.convert(100).unwrap();
        let high_obs = flipped.convert(400).unwrap();
        assert_eq!(flipped.invert(low_obs), Ok(100));
        assert_eq!(flipped.invert(high_obs), Ok(400));

        // Past the calibrated image of the former low endpoint → AboveRange.
        assert_eq!(
            flipped.invert(low_obs + 1),
            Err(InverseTransferError::AboveRange {
                physical: low_obs + 1,
                maximum: low_obs
            })
        );
        assert_eq!(
            flipped.invert(high_obs - 1),
            Err(InverseTransferError::BelowRange {
                physical: high_obs - 1,
                minimum: high_obs
            })
        );
    }

    #[test]
    fn calibrated_inverse_undoes_the_affine_before_the_table() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        let identity = AffineCalibration::new(base, 1, 0, 1).unwrap();
        assert_eq!(identity.invert(-500), Ok(150));
        assert_eq!(identity.invert(0), Ok(200));

        // Scale by ten: a calibrated -5000 is an uncalibrated -500.
        let scaled = AffineCalibration::new(base, 10, 0, 1).unwrap();
        assert_eq!(scaled.invert(-5_000), Ok(150));
        assert_eq!(scaled.convert(150), Ok(-5_000));
    }

    #[test]
    fn calibrated_inverse_reports_bounds_in_calibrated_units() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        // Uncalibrated range is -1000..=2000; at gain 10 that is -10000..=20000.
        let cal = AffineCalibration::new(base, 10, 0, 1).unwrap();

        // Within half an uncalibrated quantum of the bound, undoing the affine
        // rounds back into range rather than failing: -10_001 / 10 is -1000.1,
        // which is the endpoint knot.
        assert_eq!(cal.invert(-10_001), Ok(100));
        assert_eq!(cal.invert(20_001), Ok(400));

        // Past that, the bound is reported in calibrated units so the caller
        // can compare it against the value they passed in.
        assert_eq!(
            cal.invert(-10_010),
            Err(InverseTransferError::BelowRange {
                physical: -10_010,
                minimum: -10_000
            })
        );
        assert_eq!(
            cal.invert(20_010),
            Err(InverseTransferError::AboveRange {
                physical: 20_010,
                maximum: 20_000
            })
        );
    }

    #[test]
    fn calibrated_inverse_flips_variants_when_orientation_reverses() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        // Negative gain reverses sense: uncalibrated -1000..=2000 becomes
        // calibrated -2000..=1000, so the inner low bound is the high one here.
        let flipped = AffineCalibration::new(base, -1, 0, 1).unwrap();
        assert!(!flipped.preserves_orientation());
        assert_eq!(flipped.convert(100), Ok(1_000));
        assert_eq!(flipped.convert(400), Ok(-2_000));
        assert_eq!(flipped.invert(1_000), Ok(100));
        assert_eq!(flipped.invert(-2_000), Ok(400));

        // The inner transfer reports BelowRange; calibrated, it is AboveRange.
        assert_eq!(
            flipped.invert(1_001),
            Err(InverseTransferError::AboveRange {
                physical: 1_001,
                maximum: 1_000
            })
        );
        assert_eq!(
            flipped.invert(-2_001),
            Err(InverseTransferError::BelowRange {
                physical: -2_001,
                minimum: -2_000
            })
        );
    }

    #[test]
    fn calibrated_inverse_honors_clamp_and_flat_policy() {
        let clamped =
            PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing)
                .with_boundaries(BoundaryBehavior::Clamp, BoundaryBehavior::Clamp);
        let cal = AffineCalibration::new(clamped, 10, 0, 1).unwrap();
        assert_eq!(cal.invert(-99_999), Ok(100));
        assert_eq!(cal.invert(99_999), Ok(400));

        let flat = PiecewiseLinearTransfer::new(
            &FLAT_INPUTS,
            &FLAT_OUTPUTS,
            MonotonicDirection::Increasing,
        )
        .with_flat_resolution(FlatResolution::Error);
        let cal = AffineCalibration::new(flat, 2, 0, 1).unwrap();
        // Flat run sits at uncalibrated 10, i.e. calibrated 20.
        assert_eq!(
            cal.invert(20),
            Err(InverseTransferError::AmbiguousFlat {
                physical: 20,
                low: 10,
                high: 20
            })
        );
    }

    #[test]
    fn calibrated_inverse_rejects_unrepresentable_input() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        // Undoing a large scale pushes the uncalibrated value outside i32.
        let cal = AffineCalibration::new(base, 1, 0, i32::MAX).unwrap();
        assert_eq!(cal.invert(i32::MAX), Err(InverseTransferError::Overflow));
    }

    #[test]
    fn nested_calibration_inverts_through_every_layer() {
        let base = PiecewiseLinearTransfer::new(&INPUTS, &OUTPUTS, MonotonicDirection::Increasing);
        let inner = AffineCalibration::new(base, 2, 10, 1).unwrap();
        let outer = AffineCalibration::new(inner, 1, -10, 2).unwrap();
        assert_eq!(outer.convert(200), Ok(0));
        assert_eq!(outer.invert(0), Ok(200));
        assert_eq!(outer.convert(400), Ok(2_000));
        assert_eq!(outer.invert(2_000), Ok(400));
    }
}