Debian Codegraph @

src/ops/common.rs

raw symbols SCIP
1 use crate::constants::{MAX_I32_SCALE, MAX_SCALE_I32, POWERS_10};
2 use crate::Decimal;
3
4 #[derive(Debug)]
5 pub struct Buf12 {
6 pub data: [u32; 3],
7 }
8
9 impl Buf12 {
10 pub(super) const fn from_dec64(value: &Dec64) -> Self {
11 Buf12 {
12 data: [value.low64 as u32, (value.low64 >> 32) as u32, value.hi],
13 }
14 }
15
16 pub(super) const fn from_decimal(value: &Decimal) -> Self {
17 Buf12 {
18 data: value.mantissa_array3(),
19 }
20 }
21
22 #[inline(always)]
23 pub const fn lo(&self) -> u32 {
24 self.data[0]
25 }
26 #[inline(always)]
27 pub const fn mid(&self) -> u32 {
28 self.data[1]
29 }
30 #[inline(always)]
31 pub const fn hi(&self) -> u32 {
32 self.data[2]
33 }
34 #[inline(always)]
35 pub fn set_lo(&mut self, value: u32) {
36 self.data[0] = value;
37 }
38 #[inline(always)]
39 pub fn set_mid(&mut self, value: u32) {
40 self.data[1] = value;
41 }
42 #[inline(always)]
43 pub fn set_hi(&mut self, value: u32) {
44 self.data[2] = value;
45 }
46
47 #[inline(always)]
48 pub const fn low64(&self) -> u64 {
49 ((self.data[1] as u64) << 32) | (self.data[0] as u64)
50 }
51
52 #[inline(always)]
53 pub fn set_low64(&mut self, value: u64) {
54 self.data[1] = (value >> 32) as u32;
55 self.data[0] = value as u32;
56 }
57
58 #[inline(always)]
59 pub const fn high64(&self) -> u64 {
60 ((self.data[2] as u64) << 32) | (self.data[1] as u64)
61 }
62
63 #[inline(always)]
64 pub fn set_high64(&mut self, value: u64) {
65 self.data[2] = (value >> 32) as u32;
66 self.data[1] = value as u32;
67 }
68
69 // Determine the maximum value of x that ensures that the quotient when scaled up by 10^x
70 // still fits in 96 bits. Ultimately, we want to make scale positive - if we can't then
71 // we're going to overflow. Because x is ultimately used to lookup inside the POWERS array, it
72 // must be a valid value 0 <= x <= 9
73 #[inline]
74 pub fn find_scale(&self, scale: i32) -> Option<usize> {
75 const OVERFLOW_MAX_9_HI: u32 = 4;
76 const OVERFLOW_MAX_8_HI: u32 = 42;
77 const OVERFLOW_MAX_7_HI: u32 = 429;
78 const OVERFLOW_MAX_6_HI: u32 = 4294;
79 const OVERFLOW_MAX_5_HI: u32 = 42949;
80 const OVERFLOW_MAX_4_HI: u32 = 429496;
81 const OVERFLOW_MAX_3_HI: u32 = 4294967;
82 const OVERFLOW_MAX_2_HI: u32 = 42949672;
83 const OVERFLOW_MAX_1_HI: u32 = 429496729;
84 const OVERFLOW_MAX_9_LOW64: u64 = 5441186219426131129;
85
86 let hi = self.data[2];
87 let low64 = self.low64();
88 let mut x = 0usize;
89
90 // Quick check to stop us from trying to scale any more.
91 //
92 if hi > OVERFLOW_MAX_1_HI {
93 // If it's less than 0, which it probably is - overflow. We can't do anything.
94 if scale < 0 {
95 return None;
96 }
97 return Some(x);
98 }
99
100 if scale > MAX_SCALE_I32 - 9 {
101 // We can't scale by 10^9 without exceeding the max scale factor.
102 // Instead, we'll try to scale by the most that we can and see if that works.
103 // This is safe to do due to the check above. e.g. scale > 19 in the above, so it will
104 // evaluate to 9 or less below.
105 x = (MAX_SCALE_I32 - scale) as usize;
106 if hi < POWER_OVERFLOW_VALUES[x - 1].data[2] {
107 if x as i32 + scale < 0 {
108 // We still overflow
109 return None;
110 }
111 return Some(x);
112 }
113 } else if hi < OVERFLOW_MAX_9_HI || hi == OVERFLOW_MAX_9_HI && low64 <= OVERFLOW_MAX_9_LOW64 {
114 return Some(9);
115 }
116
117 // Do a binary search to find a power to scale by that is less than 9
118 x = if hi > OVERFLOW_MAX_5_HI {
119 if hi > OVERFLOW_MAX_3_HI {
120 if hi > OVERFLOW_MAX_2_HI {
121 1
122 } else {
123 2
124 }
125 } else if hi > OVERFLOW_MAX_4_HI {
126 3
127 } else {
128 4
129 }
130 } else if hi > OVERFLOW_MAX_7_HI {
131 if hi > OVERFLOW_MAX_6_HI {
132 5
133 } else {
134 6
135 }
136 } else if hi > OVERFLOW_MAX_8_HI {
137 7
138 } else {
139 8
140 };
141
142 // Double check what we've found won't overflow. Otherwise, we go one below.
143 if hi == POWER_OVERFLOW_VALUES[x - 1].data[2] && low64 > POWER_OVERFLOW_VALUES[x - 1].low64() {
144 x -= 1;
145 }
146
147 // Confirm we've actually resolved things
148 if x as i32 + scale < 0 {
149 None
150 } else {
151 Some(x)
152 }
153 }
154 }
155
156 // This is a table of the largest values that will not overflow when multiplied
157 // by a given power as represented by the index.
158 static POWER_OVERFLOW_VALUES: [Buf12; 8] = [
159 Buf12 {
160 data: [2576980377, 2576980377, 429496729],
161 },
162 Buf12 {
163 data: [687194767, 4123168604, 42949672],
164 },
165 Buf12 {
166 data: [2645699854, 1271310319, 4294967],
167 },
168 Buf12 {
169 data: [694066715, 3133608139, 429496],
170 },
171 Buf12 {
172 data: [2216890319, 2890341191, 42949],
173 },
174 Buf12 {
175 data: [2369172679, 4154504685, 4294],
176 },
177 Buf12 {
178 data: [4102387834, 2133437386, 429],
179 },
180 Buf12 {
181 data: [410238783, 4078814305, 42],
182 },
183 ];
184
185 pub(super) struct Dec64 {
186 pub negative: bool,
187 pub scale: u32,
188 pub hi: u32,
189 pub low64: u64,
190 }
191
192 impl Dec64 {
193 #[inline(always)]
194 pub(super) const fn new(d: &Decimal) -> Dec64 {
195 let m = d.mantissa_array3();
196 Dec64 {
197 negative: d.is_sign_negative(),
198 scale: d.scale(),
199 hi: m[2],
200 low64: ((m[1] as u64) << 32) | (m[0] as u64),
201 }
202 }
203
204 #[inline(always)]
205 pub(super) const fn lo(&self) -> u32 {
206 self.low64 as u32
207 }
208 #[inline(always)]
209 pub(super) const fn mid(&self) -> u32 {
210 (self.low64 >> 32) as u32
211 }
212
213 #[inline(always)]
214 pub(super) const fn high64(&self) -> u64 {
215 (self.low64 >> 32) | ((self.hi as u64) << 32)
216 }
217
218 #[inline(always)]
219 pub(super) const fn to_decimal(&self) -> Decimal {
220 let lo = self.low64 as u32;
221 let mid = (self.low64 >> 32) as u32;
222 let is_zero = lo == 0 && mid == 0 && self.hi == 0;
223 Decimal::from_parts_raw_unchecked(
224 lo,
225 mid,
226 self.hi,
227 crate::decimal::flags(self.negative && !is_zero, self.scale),
228 )
229 }
230 }
231
232 pub struct Buf16 {
233 pub data: [u32; 4],
234 }
235
236 impl Buf16 {
237 pub const fn zero() -> Self {
238 Buf16 { data: [0, 0, 0, 0] }
239 }
240
241 pub const fn low64(&self) -> u64 {
242 ((self.data[1] as u64) << 32) | (self.data[0] as u64)
243 }
244
245 pub fn set_low64(&mut self, value: u64) {
246 self.data[1] = (value >> 32) as u32;
247 self.data[0] = value as u32;
248 }
249
250 pub const fn mid64(&self) -> u64 {
251 ((self.data[2] as u64) << 32) | (self.data[1] as u64)
252 }
253
254 pub fn set_mid64(&mut self, value: u64) {
255 self.data[2] = (value >> 32) as u32;
256 self.data[1] = value as u32;
257 }
258
259 pub const fn high64(&self) -> u64 {
260 ((self.data[3] as u64) << 32) | (self.data[2] as u64)
261 }
262
263 pub fn set_high64(&mut self, value: u64) {
264 self.data[3] = (value >> 32) as u32;
265 self.data[2] = value as u32;
266 }
267 }
268
269 #[derive(Debug)]
270 pub struct Buf24 {
271 pub data: [u32; 6],
272 }
273
274 impl Buf24 {
275 pub const fn zero() -> Self {
276 Buf24 {
277 data: [0, 0, 0, 0, 0, 0],
278 }
279 }
280
281 pub const fn low64(&self) -> u64 {
282 ((self.data[1] as u64) << 32) | (self.data[0] as u64)
283 }
284
285 pub fn set_low64(&mut self, value: u64) {
286 self.data[1] = (value >> 32) as u32;
287 self.data[0] = value as u32;
288 }
289
290 #[allow(dead_code)]
291 pub const fn mid64(&self) -> u64 {
292 ((self.data[3] as u64) << 32) | (self.data[2] as u64)
293 }
294
295 pub fn set_mid64(&mut self, value: u64) {
296 self.data[3] = (value >> 32) as u32;
297 self.data[2] = value as u32;
298 }
299
300 #[allow(dead_code)]
301 pub const fn high64(&self) -> u64 {
302 ((self.data[5] as u64) << 32) | (self.data[4] as u64)
303 }
304
305 pub fn set_high64(&mut self, value: u64) {
306 self.data[5] = (value >> 32) as u32;
307 self.data[4] = value as u32;
308 }
309
310 pub const fn upper_word(&self) -> usize {
311 if self.data[5] > 0 {
312 return 5;
313 }
314 if self.data[4] > 0 {
315 return 4;
316 }
317 if self.data[3] > 0 {
318 return 3;
319 }
320 if self.data[2] > 0 {
321 return 2;
322 }
323 if self.data[1] > 0 {
324 return 1;
325 }
326 0
327 }
328
329 // Attempt to rescale the number into 96 bits. If successful, the scale is returned wrapped
330 // in an Option. If it failed due to overflow, we return None.
331 // * `upper` - Index of last non-zero value in self.
332 // * `scale` - Current scale factor for this value.
333 pub fn rescale(&mut self, upper: usize, scale: u32) -> Option<u32> {
334 let mut scale = scale as i32;
335 let mut upper = upper;
336
337 // Determine a rescale target to start with
338 let mut rescale_target = 0i32;
339 if upper > 2 {
340 rescale_target = upper as i32 * 32 - 64 - 1;
341 rescale_target -= self.data[upper].leading_zeros() as i32;
342 rescale_target = ((rescale_target * 77) >> 8) + 1;
343 if rescale_target > scale {
344 return None;
345 }
346 }
347
348 // Make sure we scale enough to bring it into a valid range
349 if rescale_target < scale - MAX_SCALE_I32 {
350 rescale_target = scale - MAX_SCALE_I32;
351 }
352
353 if rescale_target > 0 {
354 // We're going to keep reducing by powers of 10. So, start by reducing the scale by
355 // that amount.
356 scale -= rescale_target;
357 let mut sticky = 0;
358 let mut remainder = 0;
359 loop {
360 sticky |= remainder;
361 let mut power = if rescale_target > 8 {
362 POWERS_10[9]
363 } else {
364 POWERS_10[rescale_target as usize]
365 };
366
367 let high = self.data[upper];
368 let high_quotient = high / power;
369 remainder = high - high_quotient * power;
370
371 for item in self.data.iter_mut().rev().skip(6 - upper) {
372 let num = (*item as u64).wrapping_add((remainder as u64) << 32);
373 *item = (num / power as u64) as u32;
374 remainder = (num as u32).wrapping_sub(item.wrapping_mul(power));
375 }
376
377 self.data[upper] = high_quotient;
378
379 // If the high quotient was zero then decrease the upper bound
380 if high_quotient == 0 && upper > 0 {
381 upper -= 1;
382 }
383 if rescale_target > MAX_I32_SCALE {
384 // Scale some more
385 rescale_target -= MAX_I32_SCALE;
386 continue;
387 }
388
389 // If we fit into 96 bits then we've scaled enough. Otherwise, scale once more.
390 if upper > 2 {
391 if scale == 0 {
392 return None;
393 }
394 // Equivalent to scaling down by 10
395 rescale_target = 1;
396 scale -= 1;
397 continue;
398 }
399
400 // Round the final result.
401 power >>= 1;
402 let carried = if power <= remainder {
403 // If we're less than half then we're fine. Otherwise, we round if odd or if the
404 // sticky bit is set.
405 if power < remainder || ((self.data[0] & 1) | sticky) != 0 {
406 // Round up
407 self.data[0] = self.data[0].wrapping_add(1);
408 // Check if we carried
409 self.data[0] == 0
410 } else {
411 false
412 }
413 } else {
414 false
415 };
416
417 // If we carried then propagate through the portions
418 if carried {
419 let mut pos = 0;
420 for (index, value) in self.data.iter_mut().enumerate().skip(1) {
421 pos = index;
422 *value = value.wrapping_add(1);
423 if *value != 0 {
424 break;
425 }
426 }
427
428 // If we ended up rounding over the 96 bits then we'll try to rescale down (again)
429 if pos > 2 {
430 // Nothing to scale down from will cause overflow
431 if scale == 0 {
432 return None;
433 }
434
435 // Loop back around using scale of 10.
436 // Reset the sticky bit and remainder before looping.
437 upper = pos;
438 sticky = 0;
439 remainder = 0;
440 rescale_target = 1;
441 scale -= 1;
442 continue;
443 }
444 }
445 break;
446 }
447 }
448
449 Some(scale as u32)
450 }
451 }