1use crate::options::{Arith, Dialect};
6use std::fmt;
7use zarch::wide::U256;
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub struct Places {
11 pub int: u32,
12 pub dec: u32,
13}
14
15impl Places {
16 #[inline]
17 pub const fn new(int: u32, dec: u32) -> Self {
18 Self { int, dec }
19 }
20
21 #[inline]
22 pub const fn total(self) -> u32 {
23 self.int + self.dec
24 }
25}
26
27#[inline]
28pub fn sum_places(a: Places, b: Places) -> Places {
29 Places::new(a.int.max(b.int) + 1, a.dec.max(b.dec))
30}
31
32#[inline]
33pub fn product_places(a: Places, b: Places) -> Places {
34 Places::new(a.int + b.int, a.dec + b.dec)
35}
36
37pub fn quotient_places(dividend: Places, divisor: Places, dmax: u32) -> Places {
38 Places::new(dividend.int + divisor.dec, dividend.dec.saturating_sub(divisor.dec).max(dmax))
39}
40
41#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
44pub struct Dmax {
45 pub last: u32,
46 pub inner: u32,
47}
48
49impl Dmax {
50 pub const fn receiver(scale: u32, rounded: bool, dialect: Dialect) -> Self {
54 let last = scale + rounded as u32;
55 match dialect {
56 Dialect::Ibm => Self { last, inner: last },
57 Dialect::Gnucobol => Self { last, inner: scale },
58 }
59 }
60
61 pub fn max(self, other: Self) -> Self {
62 Self { last: self.last.max(other.last), inner: self.inner.max(other.inner) }
63 }
64
65 pub fn with(self, places: u32) -> Self {
67 Self { last: self.last.max(places), inner: self.inner.max(places) }
68 }
69}
70
71pub fn carried(ir: Places, dmax: u32, arith: Arith) -> Places {
74 let n = arith.intermediate_digits();
75 if ir.total() <= n {
76 ir
77 } else if ir.dec <= dmax {
78 Places::new(n.saturating_sub(ir.dec), ir.dec)
79 } else if ir.int + dmax <= n {
80 Places::new(ir.int, n - ir.int)
81 } else {
82 Places::new(n.saturating_sub(dmax), dmax)
83 }
84}
85
86#[derive(Clone, Copy, Debug, PartialEq, Eq)]
87pub enum ArithError {
88 DivideByZero,
89 BeyondModel,
90}
91
92impl fmt::Display for ArithError {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 match self {
95 Self::DivideByZero => write!(f, "division by zero"),
96 Self::BeyondModel => write!(f, "an intermediate wider than 256 bits"),
97 }
98 }
99}
100
101impl std::error::Error for ArithError {}
102
103#[derive(Clone, Copy, Debug, PartialEq, Eq)]
105pub struct Fixed {
106 pub negative: bool,
107 pub magnitude: U256,
108 pub places: Places,
109}
110
111#[inline]
112fn pow10(n: u32) -> U256 {
113 U256::pow10(n)
114}
115
116impl Fixed {
117 #[inline]
118 pub fn new(value: i128, places: Places) -> Self {
119 Self::signed(value < 0, U256::from_u128(value.unsigned_abs()), places)
120 }
121
122 #[inline]
123 fn signed(negative: bool, magnitude: U256, places: Places) -> Self {
124 Self { negative: negative && !magnitude.is_zero(), magnitude, places }
125 }
126
127 pub fn to_i128(self) -> Option<i128> {
128 zarch::wide::signed_i128(self.negative, self.magnitude)
129 }
130
131 pub fn fit(self, to: Places) -> Self {
134 let from = self.places.dec;
135 let magnitude = if to.dec < from {
136 (self.magnitude.div_rem(pow10(from - to.dec)).0).div_rem(pow10(to.total())).1
137 } else {
138 self.magnitude.div_rem(pow10(to.int + from)).1.checked_mul(pow10(to.dec - from)).expect("fits by construction")
139 };
140 Self::signed(self.negative, magnitude, to)
141 }
142
143 fn aligned(self, dec: u32) -> Result<U256, ArithError> {
144 self.magnitude.checked_mul(pow10(dec - self.places.dec)).ok_or(ArithError::BeyondModel)
145 }
146
147 fn negated(self) -> Self {
148 Self::signed(!self.negative, self.magnitude, self.places)
149 }
150
151 pub fn add(self, other: Self, dmax: u32, arith: Arith) -> Result<Self, ArithError> {
152 let dec = self.places.dec.max(other.places.dec);
153 let (a, b) = (self.aligned(dec)?, other.aligned(dec)?);
154 let (negative, magnitude) = match (self.negative == other.negative, a >= b) {
155 (true, _) => (self.negative, a.checked_add(b).ok_or(ArithError::BeyondModel)?),
156 (false, true) => (self.negative, a - b),
157 (false, false) => (other.negative, b - a),
158 };
159 let ir = sum_places(self.places, other.places);
160 Ok(Self::signed(negative, magnitude, Places::new(ir.int, dec)).fit(carried(ir, dmax, arith)))
161 }
162
163 pub fn sub(self, other: Self, dmax: u32, arith: Arith) -> Result<Self, ArithError> {
164 self.add(other.negated(), dmax, arith)
165 }
166
167 pub fn mul(self, other: Self, dmax: u32, arith: Arith) -> Result<Self, ArithError> {
168 let ir = product_places(self.places, other.places);
169 let magnitude = self.magnitude.checked_mul(other.magnitude).ok_or(ArithError::BeyondModel)?;
170 Ok(Self::signed(self.negative != other.negative, magnitude, ir).fit(carried(ir, dmax, arith)))
171 }
172
173 pub fn div(self, divisor: Self, dmax: u32, arith: Arith) -> Result<Self, ArithError> {
174 if divisor.magnitude.is_zero() {
175 return Err(ArithError::DivideByZero);
176 }
177 let to = carried(quotient_places(self.places, divisor.places, dmax), dmax, arith);
178 let shift = (divisor.places.dec + to.dec) as i64 - self.places.dec as i64;
179 let (numerator, denominator) = if shift >= 0 {
180 (self.magnitude.checked_mul(pow10(shift as u32)), Some(divisor.magnitude))
181 } else {
182 (Some(self.magnitude), divisor.magnitude.checked_mul(pow10(shift.unsigned_abs() as u32)))
183 };
184 let (numerator, denominator) = (numerator.ok_or(ArithError::BeyondModel)?, denominator.ok_or(ArithError::BeyondModel)?);
185 let quotient = numerator.div_rem(denominator).0;
186 let exact = Self::signed(self.negative != divisor.negative, quotient, Places::new(u32::MAX / 2, to.dec));
187 Ok(exact.fit(to))
188 }
189
190 pub fn to_receiver(self, places: Places, rounded: bool) -> (Self, bool) {
193 let from = self.places.dec;
194 let magnitude = if places.dec >= from {
195 self.magnitude.checked_mul(pow10(places.dec - from))
196 } else {
197 let (kept, dropped) = self.magnitude.div_rem(pow10(from - places.dec));
198 let half = pow10(from - places.dec - 1).checked_mul(U256::from_u128(5)).unwrap();
199 Some(if rounded && dropped >= half { kept + U256::from_u128(1) } else { kept })
200 };
201 let cap = pow10(places.total());
202 match magnitude {
203 Some(m) if m < cap => (Self::signed(self.negative, m, places), false),
204 Some(m) => (Self::signed(self.negative, m.div_rem(cap).1, places), true),
205 None => (Self::signed(self.negative, U256::ZERO, places), true),
206 }
207 }
208}
209
210#[cfg(test)]
211mod tests {
212 use super::*;
213
214 const S18: Places = Places::new(18, 0);
215
216 #[test]
217 fn places_for_each_operation() {
218 assert_eq!(sum_places(Places::new(5, 2), Places::new(3, 4)), Places::new(6, 4));
219 assert_eq!(product_places(Places::new(5, 2), Places::new(3, 4)), Places::new(8, 6));
220 assert_eq!(quotient_places(Places::new(5, 2), Places::new(3, 4), 3), Places::new(9, 3));
221 }
222
223 #[test]
226 fn a_quotient_carries_the_dividend_s_places_less_the_divisor_s_or_dmax() {
227 assert_eq!(quotient_places(Places::new(2, 4), Places::new(1, 1), 2), Places::new(3, 3));
228 assert_eq!(quotient_places(Places::new(2, 4), Places::new(1, 3), 2), Places::new(5, 2));
229 let product = Fixed::new(12321, Places::new(2, 4));
230 let q = product.div(Fixed::new(7, Places::new(1, 1)), 2, Arith::Compat).unwrap();
231 assert_eq!((q.to_i128(), q.places.dec), (Some(1760), 3));
232 }
233
234 #[test]
235 fn the_carried_places_table() {
236 assert_eq!(carried(Places::new(20, 5), 5, Arith::Compat), Places::new(20, 5));
237 assert_eq!(carried(Places::new(28, 6), 6, Arith::Compat), Places::new(24, 6));
238 assert_eq!(carried(Places::new(20, 12), 4, Arith::Compat), Places::new(20, 10));
239 assert_eq!(carried(Places::new(28, 12), 4, Arith::Compat), Places::new(26, 4));
240 assert_eq!(carried(Places::new(28, 6), 6, Arith::Extend), Places::new(25, 6));
241 }
242
243 #[test]
244 fn an_18_by_18_digit_product_loses_high_order_digits_under_compat_but_fewer_under_extend() {
245 let big = Fixed::new(999_999_999_999_999_999, S18);
246 let compat = big.mul(big, 0, Arith::Compat).unwrap();
247 let extend = big.mul(big, 0, Arith::Extend).unwrap();
248 assert_eq!(compat.places, Places::new(30, 0));
249 assert_eq!(extend.places, Places::new(31, 0));
250 let exact = U256::widening_mul(999_999_999_999_999_999, 999_999_999_999_999_999);
251 assert_eq!(compat.magnitude, exact.div_rem(U256::pow10(30)).1);
252 assert_eq!(extend.magnitude, exact.div_rem(U256::pow10(31)).1);
253 }
254
255 #[test]
256 fn division_carries_dmax_decimal_places_and_truncates() {
257 let q = Fixed::new(10, Places::new(2, 0)).div(Fixed::new(3, Places::new(1, 0)), 2, Arith::Compat).unwrap();
258 assert_eq!((q.to_i128(), q.places.dec), (Some(333), 2));
259 let n = Fixed::new(-10, Places::new(2, 0)).div(Fixed::new(3, Places::new(1, 0)), 2, Arith::Compat).unwrap();
260 assert_eq!(n.to_i128(), Some(-333));
261 assert_eq!(Fixed::new(1, S18).div(Fixed::new(0, S18), 0, Arith::Compat), Err(ArithError::DivideByZero));
262 }
263
264 #[test]
265 fn a_rounded_quotient_carries_one_place_more_than_its_receiver() {
266 let (dividend, divisor) = (Fixed::new(16617, Places::new(4, 1)), Fixed::new(441, Places::new(2, 1)));
267 let receiver = Places::new(4, 1);
268 let truncated = dividend.div(divisor, Dmax::receiver(receiver.dec, false, Dialect::Ibm).last, Arith::Compat).unwrap();
269 assert_eq!(truncated.to_receiver(receiver, true).0.to_i128(), Some(376));
270 let rounded = dividend.div(divisor, Dmax::receiver(receiver.dec, true, Dialect::Ibm).last, Arith::Compat).unwrap();
271 assert_eq!((rounded.to_i128(), rounded.places.dec), (Some(3768), 2));
272 assert_eq!(rounded.to_receiver(receiver, true).0.to_i128(), Some(377));
273 }
274
275 #[test]
276 fn gnucobol_counts_the_rounded_place_in_the_last_operation_alone() {
277 assert_eq!(Dmax::receiver(2, true, Dialect::Ibm), Dmax { last: 3, inner: 3 });
278 assert_eq!(Dmax::receiver(2, true, Dialect::Gnucobol), Dmax { last: 3, inner: 2 });
279 assert_eq!(Dmax::receiver(2, false, Dialect::Gnucobol), Dmax { last: 2, inner: 2 });
280 let statement = Dmax::receiver(2, true, Dialect::Gnucobol).max(Dmax::receiver(1, false, Dialect::Gnucobol)).with(1);
281 assert_eq!(statement, Dmax { last: 3, inner: 2 });
282 assert_eq!(statement.with(4), Dmax { last: 4, inner: 4 });
283 }
284
285 #[test]
286 fn addition_aligns_decimal_points() {
287 let s = Fixed::new(125, Places::new(1, 2)).add(Fixed::new(-3, Places::new(1, 0)), 2, Arith::Compat).unwrap();
288 assert_eq!((s.to_i128(), s.places), (Some(-175), Places::new(2, 2)));
289 }
290
291 #[test]
292 fn receivers_truncate_or_round_half_away_and_flag_size_errors() {
293 let v = Fixed::new(-12345, Places::new(3, 2));
294 assert_eq!(v.to_receiver(Places::new(3, 1), false).0.to_i128(), Some(-1234));
295 assert_eq!(v.to_receiver(Places::new(3, 1), true).0.to_i128(), Some(-1235));
296 let (wrapped, size_error) = v.to_receiver(Places::new(2, 0), false);
297 assert_eq!((wrapped.to_i128(), size_error), (Some(-23), true));
298 let (rounded_over, size_error) = Fixed::new(999, Places::new(1, 2)).to_receiver(Places::new(1, 1), true);
299 assert_eq!((rounded_over.to_i128(), size_error), (Some(0), true));
300 }
301
302 #[test]
303 fn negative_zero_is_normalized() {
304 let z = Fixed::new(5, Places::new(1, 0)).sub(Fixed::new(5, Places::new(1, 0)), 0, Arith::Compat).unwrap();
305 assert!(!z.negative);
306 }
307}