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