fast_posit/posit/quire/
convert.rs

1use super::*;
2
3impl<
4  const N: u32,
5  const ES: u32,
6  Int: crate::Int,
7  const SIZE: usize,
8> From<Posit<N, ES, Int>> for Quire<N, ES, SIZE> {
9  fn from(value: Posit<N, ES, Int>) -> Self {
10    if value == Posit::ZERO {
11      Self::ZERO
12    } else if value == Posit::NAR {
13      Self::NAR
14    } else {
15      let mut quire = Self::ZERO;
16      // SAFETY: `value` is not 0 or NaR
17      let decoded = unsafe { value.decode_regular() };
18      // SAFETY: `decoded` comes from `Posit::decode_regular`, therefore its `exp` is in bounds
19      unsafe { quire.accumulate_decoded(decoded) };
20      quire
21    }
22  }
23}
24
25#[cfg(test)]
26mod tests {
27  use super::*;
28  use malachite::rational::Rational;
29
30  /*#[test]
31  fn posit_10_0_exhaustive() {
32    type P = Posit::<10, 0, i16>;
33    type Q = Quire::<10, 0, 128>;
34    for a in P::cases_exhaustive_all() {
35      assert_eq!(Rational::try_from(a), Rational::try_from(Q::from(a)))
36    }
37  }*/
38
39  #[test]
40  fn posit_10_1_exhaustive() {
41    type P = Posit::<10, 1, i16>;
42    type Q = Quire::<10, 1, 128>;
43    for a in P::cases_exhaustive_all() {
44      assert_eq!(Rational::try_from(a), Rational::try_from(Q::from(a)))
45    }
46  }
47
48  #[test]
49  fn posit_10_2_exhaustive() {
50    type P = Posit::<10, 2, i16>;
51    type Q = Quire::<10, 2, 128>;
52    for a in P::cases_exhaustive_all() {
53      assert_eq!(Rational::try_from(a), Rational::try_from(Q::from(a)))
54    }
55  }
56
57  #[test]
58  fn posit_10_3_exhaustive() {
59    type P = Posit::<10, 3, i16>;
60    type Q = Quire::<10, 3, 128>;
61    for a in P::cases_exhaustive_all() {
62      assert_eq!(Rational::try_from(a), Rational::try_from(Q::from(a)))
63    }
64  }
65
66  /*#[test]
67  fn posit_8_0_exhaustive() {
68    type P = Posit::<8, 0, i8>;
69    type Q = Quire::<8, 0, 128>;
70    for a in P::cases_exhaustive_all() {
71      assert_eq!(Rational::try_from(a), Rational::try_from(Q::from(a)))
72    }
73  }*/
74
75  #[test]
76  fn p8_exhaustive() {
77    for a in crate::p8::cases_exhaustive_all() {
78      assert_eq!(Rational::try_from(a), Rational::try_from(crate::q8::from(a)))
79    }
80  }
81
82  #[test]
83  fn p16_exhaustive() {
84    for a in crate::p16::cases_exhaustive_all() {
85      assert_eq!(Rational::try_from(a), Rational::try_from(crate::q16::from(a)))
86    }
87  }
88
89  use proptest::prelude::*;
90  const PROPTEST_CASES: u32 = if cfg!(debug_assertions) {0x1_0000} else {0x80_0000};
91  proptest!{
92    #![proptest_config(ProptestConfig::with_cases(PROPTEST_CASES))]
93
94    #[test]
95    fn p32_proptest(a in crate::p32::cases_proptest()) {
96      assert_eq!(Rational::try_from(a), Rational::try_from(crate::q32::from(a)))
97    }
98
99    #[test]
100    fn p64_proptest(a in crate::p64::cases_proptest()) {
101      assert_eq!(Rational::try_from(a), Rational::try_from(crate::q64::from(a)))
102    }
103  }
104
105  /*#[test]
106  fn posit_3_0_exhaustive() {
107    type P = Posit::<3, 0, i8>;
108    type Q = Quire::<3, 0, 128>;
109    for a in P::cases_exhaustive_all() {
110      assert_eq!(Rational::try_from(a), Rational::try_from(Q::from(a)))
111    }
112  }*/
113
114  /*#[test]
115  fn posit_4_0_exhaustive() {
116    type P = Posit::<4, 0, i8>;
117    type Q = Quire::<4, 0, 128>;
118    for a in P::cases_exhaustive_all() {
119      assert_eq!(Rational::try_from(a), Rational::try_from(Q::from(a)))
120    }
121  }*/
122
123  /*#[test]
124  fn posit_4_1_exhaustive() {
125    type P = Posit::<4, 1, i8>;
126    type Q = Quire::<4, 1, 128>;
127    for a in P::cases_exhaustive_all() {
128      assert_eq!(Rational::try_from(a), Rational::try_from(Q::from(a)))
129    }
130  }*/
131}