fixed_bigint/fixeduint/
abs_diff_impl.rs1use super::{FixedUInt, MachineWord};
18use crate::const_numtraits::ConstAbsDiff;
19use crate::machineword::ConstMachineWord;
20
21c0nst::c0nst! {
22 impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst ConstAbsDiff for FixedUInt<T, N> {
23 fn abs_diff(self, other: Self) -> Self {
24 if self >= other {
25 self - other
26 } else {
27 other - self
28 }
29 }
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn test_abs_diff() {
39 type U16 = FixedUInt<u8, 2>;
40
41 assert_eq!(
42 ConstAbsDiff::abs_diff(U16::from(10u8), U16::from(3u8)),
43 U16::from(7u8)
44 );
45 assert_eq!(
46 ConstAbsDiff::abs_diff(U16::from(3u8), U16::from(10u8)),
47 U16::from(7u8)
48 );
49 assert_eq!(
50 ConstAbsDiff::abs_diff(U16::from(5u8), U16::from(5u8)),
51 U16::from(0u8)
52 );
53 assert_eq!(
54 ConstAbsDiff::abs_diff(U16::from(0u8), U16::from(100u8)),
55 U16::from(100u8)
56 );
57 assert_eq!(
58 ConstAbsDiff::abs_diff(U16::from(255u8), U16::from(0u8)),
59 U16::from(255u8)
60 );
61 }
62
63 c0nst::c0nst! {
64 pub c0nst fn const_abs_diff<T: [c0nst] ConstMachineWord + MachineWord, const N: usize>(
65 a: FixedUInt<T, N>,
66 b: FixedUInt<T, N>,
67 ) -> FixedUInt<T, N> {
68 ConstAbsDiff::abs_diff(a, b)
69 }
70 }
71
72 #[test]
73 fn test_const_abs_diff() {
74 type U16 = FixedUInt<u8, 2>;
75
76 assert_eq!(
77 const_abs_diff(U16::from(10u8), U16::from(3u8)),
78 U16::from(7u8)
79 );
80
81 #[cfg(feature = "nightly")]
82 {
83 const A: U16 = FixedUInt { array: [10, 0] };
84 const B: U16 = FixedUInt { array: [3, 0] };
85 const DIFF: U16 = const_abs_diff(A, B);
86 assert_eq!(DIFF, FixedUInt { array: [7, 0] });
87 }
88 }
89}