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 let decoded = unsafe { posit.decode_regular() };
16 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 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 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!{
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!{
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 }