softposit/
quire32.rs

1use crate::{PxE2, P32E2};
2
3mod convert;
4mod math;
5mod ops;
6
7#[derive(Debug)]
8pub struct Q32E2(i64, u64, u64, u64, u64, u64, u64, u64);
9
10impl Q32E2 {
11    pub const ZERO: Self = Self(0, 0, 0, 0, 0, 0, 0, 0);
12    pub const ONE: Self = Self(0, 0, 0, 0, 0x_0001_0000_0000_0000, 0, 0, 0);
13    pub const NAR: Self = Self(-0x_8000_0000_0000_0000, 0, 0, 0, 0, 0, 0, 0);
14
15    #[inline]
16    pub const fn init() -> Self {
17        Self::ZERO
18    }
19
20    #[inline]
21    pub fn from_posit(p: P32E2) -> Self {
22        Self::from(p)
23    }
24
25    #[inline]
26    pub const fn from_bits(v: [u64; 8]) -> Self {
27        Self(v[0] as _, v[1], v[2], v[3], v[4], v[5], v[6], v[7])
28    }
29
30    #[inline]
31    pub const fn to_bits(&self) -> [u64; 8] {
32        [
33            self.0 as _,
34            self.1,
35            self.2,
36            self.3,
37            self.4,
38            self.5,
39            self.6,
40            self.7,
41        ]
42    }
43
44    #[inline]
45    pub const fn is_zero(&self) -> bool {
46        self.0 == 0
47            && self.1 == 0
48            && self.2 == 0
49            && self.3 == 0
50            && self.4 == 0
51            && self.5 == 0
52            && self.7 == 0
53    }
54
55    #[inline]
56    pub const fn is_nar(&self) -> bool {
57        self.0 as u64 == 0x8000_0000_0000_0000
58            && self.1 == 0
59            && self.2 == 0
60            && self.3 == 0
61            && self.4 == 0
62            && self.5 == 0
63            && self.7 == 0
64    }
65
66    #[inline]
67    pub fn add_product(&mut self, p_a: P32E2, p_b: P32E2) {
68        let ui_a = p_a.to_bits();
69        let ui_b = p_b.to_bits();
70        ops::fdp(self, ui_a, ui_b, true);
71    }
72
73    #[inline]
74    pub fn sub_product(&mut self, p_a: P32E2, p_b: P32E2) {
75        let ui_a = p_a.to_bits();
76        let ui_b = p_b.to_bits();
77        ops::fdp(self, ui_a, ui_b, false);
78    }
79
80    #[inline]
81    pub fn clear(&mut self) {
82        *self = Self::ZERO;
83    }
84
85    #[inline]
86    pub fn neg(&mut self) {
87        self.0 = self.0.wrapping_neg();
88    }
89
90    #[inline]
91    pub fn into_two_posits(mut self) -> (P32E2, P32E2) {
92        let p1 = self.to_posit();
93        self -= p1;
94        (p1, self.to_posit())
95    }
96
97    #[inline]
98    pub fn into_three_posits(mut self) -> (P32E2, P32E2, P32E2) {
99        let p1 = self.to_posit();
100        self -= p1;
101        let p2 = self.to_posit();
102        self -= p2;
103        (p1, p2, self.to_posit())
104    }
105}
106
107impl crate::Quire<P32E2> for Q32E2 {
108    type Bits = [u64; 8];
109    fn init() -> Self {
110        Self::init()
111    }
112    fn from_posit(p: P32E2) -> Self {
113        Self::from_posit(p)
114    }
115    fn to_posit(&self) -> P32E2 {
116        Self::to_posit(self)
117    }
118    fn from_bits(v: Self::Bits) -> Self {
119        Self::from_bits(v)
120    }
121    fn to_bits(&self) -> Self::Bits {
122        Self::to_bits(self)
123    }
124    fn is_zero(&self) -> bool {
125        Self::is_zero(self)
126    }
127    fn is_nar(&self) -> bool {
128        Self::is_nar(self)
129    }
130    fn add_product(&mut self, p_a: P32E2, p_b: P32E2) {
131        Self::add_product(self, p_a, p_b)
132    }
133    fn sub_product(&mut self, p_a: P32E2, p_b: P32E2) {
134        Self::sub_product(self, p_a, p_b)
135    }
136    fn clear(&mut self) {
137        Self::clear(self)
138    }
139    fn neg(&mut self) {
140        Self::neg(self)
141    }
142}
143
144impl<const N: u32> crate::Quire<PxE2<{ N }>> for Q32E2 {
145    type Bits = [u64; 8];
146    fn init() -> Self {
147        Self::init()
148    }
149    fn from_posit(p: PxE2<{ N }>) -> Self {
150        Self::from(p)
151    }
152    fn to_posit(&self) -> PxE2<{ N }> {
153        PxE2::<{ N }>::from(self)
154    }
155    fn from_bits(v: Self::Bits) -> Self {
156        Self::from_bits(v)
157    }
158    fn to_bits(&self) -> Self::Bits {
159        Self::to_bits(self)
160    }
161    fn is_zero(&self) -> bool {
162        Self::is_zero(self)
163    }
164    fn is_nar(&self) -> bool {
165        Self::is_nar(self)
166    }
167    fn add_product(&mut self, p_a: PxE2<{ N }>, p_b: PxE2<{ N }>) {
168        let ui_a = p_a.to_bits();
169        let ui_b = p_b.to_bits();
170        ops::fdp(self, ui_a, ui_b, true);
171    }
172    fn sub_product(&mut self, p_a: PxE2<{ N }>, p_b: PxE2<{ N }>) {
173        let ui_a = p_a.to_bits();
174        let ui_b = p_b.to_bits();
175        ops::fdp(self, ui_a, ui_b, false);
176    }
177    fn clear(&mut self) {
178        Self::clear(self)
179    }
180    fn neg(&mut self) {
181        Self::neg(self)
182    }
183}
184
185use core::fmt;
186impl fmt::Display for Q32E2 {
187    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
188        write!(f, "{}", f64::from(self.to_posit()))
189    }
190}