1use super::*;
2
3use core::fmt::Debug;
4
5impl<
6 const N: u32,
7 const ES: u32,
8 Int: crate::Int,
9> Debug for Posit<N, ES, Int> {
10 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11 if const { Self::JUNK_BITS == 0 } {
12 let bits = self.0;
13 f.debug_tuple("Posit")
14 .field(&format_args!("0b{bits:0w$b}", w=Int::BITS as usize))
15 .finish()
16 } else {
17 let bits_junk = self.0.lshr(Self::BITS);
18 let bits_significant = self.0.mask_lsb(Self::BITS);
19 f.debug_tuple("Posit")
20 .field(&format_args!("0b{bits_junk:0wj$b}_{bits_significant:0ws$b}", wj=Self::JUNK_BITS as usize, ws=Self::BITS as usize))
21 .finish()
22 }
23 }
24}
25
26impl<
27 const N: u32,
28 const ES: u32,
29 Int: crate::Int,
30> Debug for Decoded<N, ES, Int> {
31 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
32 let frac_hidden = self.frac.lshr(Self::FRAC_WIDTH);
33 let frac_explicit = (self.frac << 2).lshr(3);
34 let frac_round = self.frac & Int::ONE;
35 if const { Self::ES != 0 } {
36 let exp_regime = self.exp.lshr(ES);
37 let exp_exponent = self.exp.mask_lsb(ES);
38 let exp_total = self.exp;
39 f.debug_struct("Decoded")
40 .field("frac", &format_args!("0b{frac_hidden:02b}_{frac_explicit:0w$b}_{frac_round:b}",
41 w=Int::BITS as usize - 3
42 ))
43 .field("exp", &format_args!("0b{exp_regime:0wr$b}_{exp_exponent:0we$b} ({exp_total:+})",
44 wr=(Int::BITS - ES) as usize, we=ES as usize,
45 ))
46 .finish()
47 } else {
48 let exp_total = self.exp;
49 f.debug_struct("Decoded")
50 .field("frac", &format_args!("0b{frac_hidden:02b}_{frac_explicit:0w$b}_{frac_round:b}",
51 w=Int::BITS as usize - 3
52 ))
53 .field("exp", &format_args!("0b{exp_total:0wr$b}_ ({exp_total:+})",
54 wr=(Int::BITS - ES) as usize,
55 ))
56 .finish()
57 }
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn posit_nojunk() {
67 assert_eq!(
68 format!("{:?}", Posit::<8, 2, i8>::from_bits_unsigned(0b00101011)).as_str(),
69 "Posit(0b00101011)",
70 );
71 assert_eq!(
72 format!("{:?}", Posit::<8, 2, i8>::from_bits_unsigned(0b10101011)).as_str(),
73 "Posit(0b10101011)",
74 );
75 }
76
77 #[test]
78 fn posit_junk() {
79 assert_eq!(
80 format!("{:?}", Posit::<6, 2, i16>::from_bits_unsigned(0b001011)).as_str(),
81 "Posit(0b0000000000_001011)",
82 );
83 assert_eq!(
84 format!("{:?}", Posit::<6, 2, i16>::from_bits_unsigned(0b101011)).as_str(),
85 "Posit(0b1111111111_101011)",
86 );
87 }
88
89 #[test]
90 fn decoded() {
91 assert_eq!(
92 format!("{:?}", Decoded::<6, 2, i16>{ frac: 0b01_0010101110110_0, exp: 3 }).as_str(),
93 "Decoded { frac: 0b01_0010101110110_0, exp: 0b00000000000000_11 (+3) }",
94 );
95 assert_eq!(
96 format!("{:?}", Decoded::<6, 2, i16>{ frac: -0b01_0010101110110_0, exp: 3 }).as_str(),
97 "Decoded { frac: 0b10_1101010001010_0, exp: 0b00000000000000_11 (+3) }",
98 );
99 assert_eq!(
100 format!("{:?}", Decoded::<6, 2, i16>{ frac: 0b01_0000000000000_1, exp: -1 }).as_str(),
101 "Decoded { frac: 0b01_0000000000000_1, exp: 0b11111111111111_11 (-1) }",
102 );
103 assert_eq!(
104 format!("{:?}", Decoded::<6, 4, i16>{ frac: 0b01_0000000000000_1, exp: -20 }).as_str(),
105 "Decoded { frac: 0b01_0000000000000_1, exp: 0b111111111110_1100 (-20) }",
106 );
107 assert_eq!(
108 format!("{:?}", Decoded::<6, 0, i16>{ frac: 0b01_0000000000000_1, exp: -20 }).as_str(),
109 "Decoded { frac: 0b01_0000000000000_1, exp: 0b1111111111101100_ (-20) }",
110 );
111 }
112}