fast_posit/posit/
fmt.rs

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