fast_posit/posit/quire/
ops.rs

1use super::*;
2
3impl<
4  const N: u32,
5  const ES: u32,
6  const SIZE: usize,
7> Quire<N, ES, SIZE> {
8  pub(crate) fn add<Int: crate::Int>(&mut self, posit: Posit<N, ES, Int>) {
9    if posit == Posit::ZERO {
10      ()
11    } else if posit == Posit::NAR || self.is_nar() {
12      *self = Quire::NAR
13    } else {
14      // SAFETY: `posit` is not 0 or NaR
15      let decoded = unsafe { posit.decode_regular() };
16      // SAFETY: `decoded` comes from `Posit::decode_regular`, therefore its `exp` is in bounds
17      unsafe { self.accumulate_decoded(decoded) }
18    }
19  }
20
21  pub(crate) fn sub<Int: crate::Int>(&mut self, posit: Posit<N, ES, Int>) {
22    self.add(-posit)
23  }
24}
25
26impl<
27  const N: u32,
28  const ES: u32,
29  Int: crate::Int,
30  const SIZE: usize,
31> core::ops::AddAssign<Posit<N, ES, Int>> for Quire<N, ES, SIZE> {
32  fn add_assign(&mut self, rhs: Posit<N, ES, Int>) {
33    self.add(rhs)
34  }
35}
36
37impl<
38  const N: u32,
39  const ES: u32,
40  Int: crate::Int,
41  const SIZE: usize,
42> core::ops::AddAssign<&Posit<N, ES, Int>> for Quire<N, ES, SIZE> {
43  fn add_assign(&mut self, rhs: &Posit<N, ES, Int>) {
44    self.add(*rhs)
45  }
46}
47
48impl<
49  const N: u32,
50  const ES: u32,
51  Int: crate::Int,
52  const SIZE: usize,
53> core::ops::SubAssign<Posit<N, ES, Int>> for Quire<N, ES, SIZE> {
54  fn sub_assign(&mut self, rhs: Posit<N, ES, Int>) {
55    self.sub(rhs)
56  }
57}
58
59impl<
60  const N: u32,
61  const ES: u32,
62  Int: crate::Int,
63  const SIZE: usize,
64> core::ops::SubAssign<&Posit<N, ES, Int>> for Quire<N, ES, SIZE> {
65  fn sub_assign(&mut self, rhs: &Posit<N, ES, Int>) {
66    self.sub(*rhs)
67  }
68}
69
70#[cfg(test)]
71mod tests {
72  // TODO these tests are basic: they test for two posits a and b whether summing them on the quire
73  // yields the correct result. But more tests are needed: summing vectors of n posits, not just 2.
74
75  use super::*;
76  use malachite::rational::Rational;
77  use proptest::prelude::*;
78
79  macro_rules! test_exhaustive {
80    ($name:ident, $posit:ty, $quire:ty,) => {
81      #[test]
82      fn $name() {
83        for a in <$posit>::cases_exhaustive_all() {
84          for b in <$posit>::cases_exhaustive_all() {
85            let posit = a + b;
86            let mut quire = <$quire>::from(a);
87            quire += b;
88            assert!(super::rational::try_is_correct_rounded(Rational::try_from(quire), posit))
89          }
90        }
91      }
92    };
93  }
94
95  macro_rules! test_proptest {
96    ($name:ident, $posit:ty, $quire:ty,) => {
97      proptest!{
98        // const PROPTEST_CASES: u32 = if cfg!(debug_assertions) {0x1_0000} else {0x80_0000};
99        // #![proptest_config(ProptestConfig::with_cases(PROPTEST_CASES))]
100        fn $name(
101          a in <$posit>::cases_proptest(),
102          b in <$posit>::cases_proptest(),
103        ) {
104          let posit = a + b;
105          let mut quire = <$quire>::from(a);
106          quire += b;
107          assert!(super::rational::try_is_correct_rounded(Rational::try_from(quire), posit))
108        }
109      }
110    };
111  }
112
113  /*test_exhaustive!{
114    posit_10_0_exhaustive,
115    Posit<10, 0, i16>,
116    Quire<10, 0, 128>,
117  }*/
118
119  test_exhaustive!{
120    posit_10_1_exhaustive,
121    Posit<10, 1, i16>,
122    Quire<10, 1, 128>,
123  }
124
125  test_exhaustive!{
126    posit_10_2_exhaustive,
127    Posit<10, 2, i16>,
128    Quire<10, 2, 128>,
129  }
130
131  test_exhaustive!{
132    posit_10_3_exhaustive,
133    Posit<10, 3, i16>,
134    Quire<10, 3, 128>,
135  }
136
137  /*test_exhaustive!{
138    posit_8_0_exhaustive,
139    Posit<8, 0, i8>,
140    Quire<8, 0, 128>,
141  }*/
142
143  test_exhaustive!{
144    p8_exhaustive,
145    crate::p8,
146    crate::q8,
147  }
148
149  test_proptest!{
150    p16_proptest,
151    crate::p16,
152    crate::q16,
153  }
154
155  test_proptest!{
156    p32_proptest,
157    crate::p32,
158    crate::q32,
159  }
160
161  test_proptest!{
162    p64_proptest,
163    crate::p64,
164    crate::q64,
165  }
166
167  /*test_exhaustive!{
168    posit_3_0_exhaustive,
169    Posit<3, 0, i8>,
170    Quire<3, 0, 128>,
171  }*/
172
173  /*test_exhaustive!{
174    posit_4_0_exhaustive,
175    Posit<4, 0, i8>,
176    Quire<4, 0, 128>,
177  }*/
178
179  /*test_exhaustive!{
180    posit_4_1_exhaustive,
181    Posit<4, 1, i8>,
182    Quire<4, 1, 128>,
183  }*/
184}