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)]
77mod tests {
78  use super::*;
79
80  #[test]
81  fn posit_nojunk() {
82    assert_eq!(
83      format!("{:?}", Posit::<8, 2, i8>::from_bits_unsigned(0b00101011)).as_str(),
84      "Posit(0b00101011)",
85    );
86    assert_eq!(
87      format!("{:?}", Posit::<8, 2, i8>::from_bits_unsigned(0b10101011)).as_str(),
88      "Posit(0b10101011)",
89    );
90  }
91
92  #[test]
93  fn posit_junk() {
94    assert_eq!(
95      format!("{:?}", Posit::<6, 2, i16>::from_bits_unsigned(0b001011)).as_str(),
96      "Posit(0b0000000000_001011)",
97    );
98    assert_eq!(
99      format!("{:?}", Posit::<6, 2, i16>::from_bits_unsigned(0b101011)).as_str(),
100      "Posit(0b1111111111_101011)",
101    );
102  }
103
104  #[test]
105  fn decoded() {
106    assert_eq!(
107      format!("{:?}", Decoded::<6, 2, i16>{ frac: 0b01_0010101110110_0, exp: 3 }).as_str(),
108      "Decoded { frac: 0b01_0010101110110_0, exp: 0b00000000000000_11 (+3) }",
109    );
110    assert_eq!(
111      format!("{:?}", Decoded::<6, 2, i16>{ frac: -0b01_0010101110110_0, exp: 3 }).as_str(),
112      "Decoded { frac: 0b10_1101010001010_0, exp: 0b00000000000000_11 (+3) }",
113    );
114    assert_eq!(
115      format!("{:?}", Decoded::<6, 2, i16>{ frac: 0b01_0000000000000_1, exp: -1 }).as_str(),
116      "Decoded { frac: 0b01_0000000000000_1, exp: 0b11111111111111_11 (-1) }",
117    );
118    assert_eq!(
119      format!("{:?}", Decoded::<6, 4, i16>{ frac: 0b01_0000000000000_1, exp: -20 }).as_str(),
120      "Decoded { frac: 0b01_0000000000000_1, exp: 0b111111111110_1100 (-20) }",
121    );
122    assert_eq!(
123      format!("{:?}", Decoded::<6, 0, i16>{ frac: 0b01_0000000000000_1, exp: -20 }).as_str(),
124      "Decoded { frac: 0b01_0000000000000_1, exp: 0b1111111111101100_ (-20) }",
125    );
126  }
127
128  #[test]
129  fn quire() {
130    let bits = [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78];
131    assert_eq!(
132      format!("{:?}", crate::q8::from_bits(bits)).as_str(),
133      "Quire(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78)",
134    );
135  }
136}