Debian Codegraph @

src/ops/div.rs

raw symbols SCIP
1 use crate::constants::{MAX_SCALE_I32, POWERS_10};
2 use crate::decimal::{CalculationResult, Decimal};
3 use crate::ops::common::{Buf12, Buf16, Dec64};
4
5 use core::cmp::Ordering;
6
7 impl Buf12 {
8 // Returns true if successful, else false for an overflow
9 fn add32(&mut self, value: u32) -> Result<(), DivError> {
10 let value = value as u64;
11 let new = self.low64().wrapping_add(value);
12 self.set_low64(new);
13 if new < value {
14 self.data[2] = self.data[2].wrapping_add(1);
15 if self.data[2] == 0 {
16 return Err(DivError::Overflow);
17 }
18 }
19 Ok(())
20 }
21
22 // Divide a Decimal union by a 32 bit divisor.
23 // Self is overwritten with the quotient.
24 // Return value is a 32 bit remainder.
25 fn div32(&mut self, divisor: u32) -> u32 {
26 let divisor64 = divisor as u64;
27 // See if we can get by using a simple u64 division
28 if self.data[2] != 0 {
29 let mut temp = self.high64();
30 let q64 = temp / divisor64;
31 self.set_high64(q64);
32
33 // Calculate the "remainder"
34 temp = ((temp - q64 * divisor64) << 32) | (self.data[0] as u64);
35 if temp == 0 {
36 return 0;
37 }
38 let q32 = (temp / divisor64) as u32;
39 self.data[0] = q32;
40 (temp as u32).wrapping_sub(q32.wrapping_mul(divisor))
41 } else {
42 // Super easy divisor
43 let low64 = self.low64();
44 if low64 == 0 {
45 // Nothing to do
46 return 0;
47 }
48 // Do the calc
49 let quotient = low64 / divisor64;
50 self.set_low64(quotient);
51 // Remainder is the leftover that wasn't used
52 (low64.wrapping_sub(quotient.wrapping_mul(divisor64))) as u32
53 }
54 }
55
56 // Divide the number by a power constant
57 // Returns true if division was successful
58 fn div32_const(&mut self, pow: u32) -> bool {
59 let pow64 = pow as u64;
60 let high64 = self.high64();
61 let lo = self.data[0] as u64;
62 let div64: u64 = high64 / pow64;
63 let div = ((((high64 - div64 * pow64) << 32) + lo) / pow64) as u32;
64 if self.data[0] == div.wrapping_mul(pow) {
65 self.set_high64(div64);
66 self.data[0] = div;
67 true
68 } else {
69 false
70 }
71 }
72 }
73
74 impl Buf16 {
75 // Does a partial divide with a 64 bit divisor. The divisor in this case must be 64 bits
76 // otherwise various assumptions fail (e.g. 32 bit quotient).
77 // To assist, the upper 64 bits must be greater than the divisor for this to succeed.
78 // Consequently, it will return the quotient as a 32 bit number and overwrite self with the
79 // 64 bit remainder.
80 #[inline]
81 pub(super) fn partial_divide_64(&mut self, divisor: u64) -> u32 {
82 // We make this assertion here, however below we pivot based on the data
83 debug_assert!(divisor > self.mid64());
84
85 // If we have an empty high bit, then divisor must be greater than the dividend due to
86 // the assumption that the divisor REQUIRES 64 bits.
87 if self.data[2] == 0 {
88 let low64 = self.low64();
89 if low64 < divisor {
90 // We can't divide at at all so result is 0. The dividend remains untouched since
91 // the full amount is the remainder.
92 return 0;
93 }
94
95 let quotient = low64 / divisor;
96 self.set_low64(low64 - (quotient * divisor));
97 return quotient as u32;
98 }
99
100 // Do a simple check to see if the hi portion of the dividend is greater than the hi
101 // portion of the divisor.
102 let divisor_hi32 = (divisor >> 32) as u32;
103 if self.data[2] >= divisor_hi32 {
104 // We know that the divisor goes into this at MOST u32::max times.
105 // So we kick things off, with that assumption
106 let mut low64 = self.low64();
107 low64 = low64.wrapping_sub(divisor << 32).wrapping_add(divisor);
108 let mut quotient = u32::MAX;
109
110 // If we went negative then keep adding it back in
111 loop {
112 if low64 < divisor {
113 break;
114 }
115 quotient = quotient.wrapping_sub(1);
116 low64 = low64.wrapping_add(divisor);
117 }
118 self.set_low64(low64);
119 return quotient;
120 }
121
122 let mid64 = self.mid64();
123 let divisor_hi32_64 = divisor_hi32 as u64;
124 if mid64 < divisor_hi32_64 {
125 // similar situation as above where we've got nothing left to divide
126 return 0;
127 }
128
129 let mut quotient = mid64 / divisor_hi32_64;
130 let mut remainder = self.data[0] as u64 | ((mid64 - quotient * divisor_hi32_64) << 32);
131
132 // Do quotient * lo divisor
133 let product = quotient * (divisor & 0xFFFF_FFFF);
134 remainder = remainder.wrapping_sub(product);
135
136 // Check if we've gone negative. If so, add it back
137 if remainder > !product {
138 loop {
139 quotient = quotient.wrapping_sub(1);
140 remainder = remainder.wrapping_add(divisor);
141 if remainder < divisor {
142 break;
143 }
144 }
145 }
146
147 self.set_low64(remainder);
148 quotient as u32
149 }
150
151 // Does a partial divide with a 96 bit divisor. The divisor in this case must require 96 bits
152 // otherwise various assumptions fail (e.g. 32 bit quotient).
153 #[inline]
154 pub(super) fn partial_divide_96(&mut self, divisor: &Buf12) -> u32 {
155 let dividend = self.high64();
156 let divisor_hi = divisor.data[2];
157 if dividend < divisor_hi as u64 {
158 // Dividend is too small - entire number is remainder
159 return 0;
160 }
161
162 let mut quo = (dividend / divisor_hi as u64) as u32;
163 let mut remainder = (dividend as u32).wrapping_sub(quo.wrapping_mul(divisor_hi));
164
165 // Compute full remainder
166 let mut prod1 = quo as u64 * divisor.data[0] as u64;
167 let mut prod2 = quo as u64 * divisor.data[1] as u64;
168 prod2 += prod1 >> 32;
169 prod1 = (prod1 & 0xFFFF_FFFF) | (prod2 << 32);
170 prod2 >>= 32;
171
172 let mut num = self.low64();
173 num = num.wrapping_sub(prod1);
174 remainder = remainder.wrapping_sub(prod2 as u32);
175
176 // If there are carries make sure they are propagated
177 if num > !prod1 {
178 remainder = remainder.wrapping_sub(1);
179 if remainder < !(prod2 as u32) {
180 self.set_low64(num);
181 self.data[2] = remainder;
182 return quo;
183 }
184 } else if remainder <= !(prod2 as u32) {
185 self.set_low64(num);
186 self.data[2] = remainder;
187 return quo;
188 }
189
190 // Remainder went negative, add divisor back until it's positive
191 prod1 = divisor.low64();
192 loop {
193 quo = quo.wrapping_sub(1);
194 num = num.wrapping_add(prod1);
195 remainder = remainder.wrapping_add(divisor_hi);
196
197 if num < prod1 {
198 // Detected carry.
199 let tmp = remainder;
200 remainder = remainder.wrapping_add(1);
201 if tmp < divisor_hi {
202 break;
203 }
204 }
205 if remainder < divisor_hi {
206 break; // detected carry
207 }
208 }
209
210 self.set_low64(num);
211 self.data[2] = remainder;
212 quo
213 }
214 }
215
216 enum DivError {
217 Overflow,
218 }
219
220 #[inline]
221 pub(crate) fn div_impl(dividend: &Decimal, divisor: &Decimal) -> CalculationResult {
222 if divisor.is_zero() {
223 return CalculationResult::DivByZero;
224 }
225 if dividend.is_zero() {
226 return CalculationResult::Ok(Decimal::ZERO);
227 }
228 let dividend = Dec64::new(dividend);
229 let divisor = Dec64::new(divisor);
230
231 // Pre calculate the scale and the sign
232 let mut scale = (dividend.scale as i32) - (divisor.scale as i32);
233 let sign_negative = dividend.negative ^ divisor.negative;
234
235 // Set up some variables for modification throughout
236 let mut require_unscale = false;
237 let mut quotient = Buf12::from_dec64(&dividend);
238 let divisor = Buf12::from_dec64(&divisor);
239
240 // Branch depending on the complexity of the divisor
241 if divisor.data[2] | divisor.data[1] == 0 {
242 // We have a simple(r) divisor (32 bit)
243 let divisor32 = divisor.data[0];
244
245 // Remainder can only be 32 bits since the divisor is 32 bits.
246 let remainder = quotient.div32(divisor32);
247
248 // If there's no remainder and scale is non-negative, we're done (fast path).
249 // Otherwise, enter the remainder processing loop.
250 if remainder != 0 || scale < 0 {
251 match div_remainder_32(remainder, divisor32, quotient, scale) {
252 Err(DivError::Overflow) => return CalculationResult::Overflow,
253 Ok(result) => {
254 quotient = result.quotient;
255 scale = result.scale;
256 require_unscale = result.require_unscale;
257 }
258 }
259 }
260 } else {
261 // We have a divisor greater than 32 bits. Both of these share some quick calculation wins
262 // so we'll do those before branching into separate logic.
263 // The win we can do is shifting the bits to the left as much as possible. We do this to both
264 // the dividend and the divisor to ensure the quotient is not changed.
265 // As a simple contrived example: if we have 4 / 2 then we could bit shift all the way to the
266 // left meaning that the lo portion would have nothing inside of it. Of course, shifting these
267 // left one has the same result (8/4) etc.
268 // The advantage is that we may be able to write off lower portions of the number making things
269 // easier.
270 let power_scale = if divisor.data[2] == 0 {
271 divisor.data[1].leading_zeros()
272 } else {
273 divisor.data[2].leading_zeros()
274 } as usize;
275 let mut remainder = Buf16::zero();
276 remainder.set_low64(quotient.low64() << power_scale);
277 let tmp_high = ((quotient.data[1] as u64) + ((quotient.data[2] as u64) << 32)) >> (32 - power_scale);
278 remainder.set_high64(tmp_high);
279
280 // Work out the divisor after it's shifted
281 let divisor64 = divisor.low64() << power_scale;
282 // Check if the divisor is 64 bit or the full 96 bits
283 if divisor.data[2] == 0 {
284 // It's 64 bits
285 quotient.data[2] = 0;
286
287 // Calc mid/lo by shifting accordingly
288 let rem_lo = remainder.data[0];
289 remainder.data[0] = remainder.data[1];
290 remainder.data[1] = remainder.data[2];
291 remainder.data[2] = remainder.data[3];
292 quotient.data[1] = remainder.partial_divide_64(divisor64);
293
294 remainder.data[2] = remainder.data[1];
295 remainder.data[1] = remainder.data[0];
296 remainder.data[0] = rem_lo;
297 quotient.data[0] = remainder.partial_divide_64(divisor64);
298
299 // If there's no remainder and scale is non-negative, we're done (fast path).
300 // Otherwise, enter the remainder processing loop.
301 if remainder.low64() != 0 || scale < 0 {
302 match div_remainder_64(remainder, divisor64, quotient, scale) {
303 Err(DivError::Overflow) => return CalculationResult::Overflow,
304 Ok(result) => {
305 quotient = result.quotient;
306 scale = result.scale;
307 require_unscale = result.require_unscale;
308 }
309 }
310 }
311 } else {
312 // It's 96 bits
313 // Start by finishing the shift left
314 let divisor_mid = divisor.data[1];
315 let divisor_hi = divisor.data[2];
316 let mut divisor = divisor;
317 divisor.set_low64(divisor64);
318 divisor.data[2] = ((divisor_mid as u64 + ((divisor_hi as u64) << 32)) >> (32 - power_scale)) as u32;
319
320 let quo = remainder.partial_divide_96(&divisor);
321 quotient.set_low64(quo as u64);
322 quotient.data[2] = 0;
323
324 // If there's no remainder and scale is non-negative, we're done (fast path).
325 // Otherwise, enter the remainder processing loop.
326 if remainder.low64() != 0 || remainder.data[2] != 0 || scale < 0 {
327 match div_remainder_96(remainder, divisor, quotient, scale) {
328 Err(DivError::Overflow) => return CalculationResult::Overflow,
329 Ok(result) => {
330 quotient = result.quotient;
331 scale = result.scale;
332 require_unscale = result.require_unscale;
333 }
334 }
335 }
336 }
337 }
338 if require_unscale {
339 scale = unscale(&mut quotient, scale);
340 }
341 CalculationResult::Ok(Decimal::from_parts(
342 quotient.data[0],
343 quotient.data[1],
344 quotient.data[2],
345 sign_negative,
346 scale as u32,
347 ))
348 }
349
350 struct DivResult {
351 quotient: Buf12,
352 scale: i32,
353 require_unscale: bool,
354 }
355
356 // Remainder processing loop for the 32-bit divisor path.
357 // Kept out-of-line to reduce code size of div_impl's fast path.
358 #[inline(never)]
359 fn div_remainder_32(
360 mut remainder: u32,
361 divisor32: u32,
362 mut quotient: Buf12,
363 mut scale: i32,
364 ) -> Result<DivResult, DivError> {
365 let mut require_unscale = false;
366 let mut power_scale = 0;
367
368 loop {
369 // Remainder is 0 so we have a simple situation
370 if remainder == 0 {
371 // If the scale is positive then we're actually done
372 if scale >= 0 {
373 break;
374 }
375 power_scale = 9usize.min((-scale) as usize);
376 } else {
377 // We may need to normalize later, so set the flag appropriately
378 require_unscale = true;
379
380 // We have a remainder so we effectively want to try to adjust the quotient and add
381 // the remainder into the quotient. We do this below, however first of all we want
382 // to try to avoid overflowing so we do that check first.
383 let will_overflow = if scale == MAX_SCALE_I32 {
384 true
385 } else {
386 // Figure out how much we can scale by
387 if let Some(s) = quotient.find_scale(scale) {
388 power_scale = s;
389 } else {
390 return Err(DivError::Overflow);
391 }
392 // If it comes back as 0 (i.e. 10^0 = 1) then we're going to overflow since
393 // we're doing nothing.
394 power_scale == 0
395 };
396 if will_overflow {
397 // No more scaling can be done, but remainder is non-zero so we round if necessary.
398 let tmp = remainder << 1;
399 let round = if tmp < remainder {
400 // We round if we wrapped around
401 true
402 } else if tmp >= divisor32 {
403 // If we're greater than the divisor (i.e. underflow)
404 // or if there is a lo bit set, we round
405 tmp > divisor32 || (quotient.data[0] & 0x1) > 0
406 } else {
407 false
408 };
409
410 // If we need to round, try to do so.
411 if round {
412 scale = round_up(&mut quotient, scale)?;
413 }
414 break;
415 }
416 }
417
418 // Do some scaling
419 let power = POWERS_10[power_scale];
420 scale += power_scale as i32;
421 // Increase the quotient by the power that was looked up
422 let overflow = increase_scale(&mut quotient, power as u64);
423 if overflow > 0 {
424 return Err(DivError::Overflow);
425 }
426
427 let remainder_scaled = (remainder as u64) * (power as u64);
428 let remainder_quotient = (remainder_scaled / (divisor32 as u64)) as u32;
429 remainder = (remainder_scaled - remainder_quotient as u64 * divisor32 as u64) as u32;
430 if let Err(DivError::Overflow) = quotient.add32(remainder_quotient) {
431 scale = unscale_from_overflow(&mut quotient, scale, remainder != 0)?;
432 break;
433 }
434 }
435
436 Ok(DivResult {
437 quotient,
438 scale,
439 require_unscale,
440 })
441 }
442
443 // Remainder processing loop for the 64-bit divisor path.
444 fn div_remainder_64(
445 mut remainder: Buf16,
446 divisor64: u64,
447 mut quotient: Buf12,
448 mut scale: i32,
449 ) -> Result<DivResult, DivError> {
450 let mut require_unscale = false;
451 let mut power_scale = 0;
452
453 loop {
454 let rem_low64 = remainder.low64();
455 if rem_low64 == 0 {
456 // If the scale is positive then we're actually done
457 if scale >= 0 {
458 break;
459 }
460 power_scale = 9usize.min((-scale) as usize);
461 } else {
462 // We may need to normalize later, so set the flag appropriately
463 require_unscale = true;
464
465 // We have a remainder so we effectively want to try to adjust the quotient and add
466 // the remainder into the quotient. We do this below, however first of all we want
467 // to try to avoid overflowing so we do that check first.
468 let will_overflow = if scale == MAX_SCALE_I32 {
469 true
470 } else {
471 // Figure out how much we can scale by
472 if let Some(s) = quotient.find_scale(scale) {
473 power_scale = s;
474 } else {
475 return Err(DivError::Overflow);
476 }
477 // If it comes back as 0 (i.e. 10^0 = 1) then we're going to overflow since
478 // we're doing nothing.
479 power_scale == 0
480 };
481 if will_overflow {
482 // No more scaling can be done, but remainder is non-zero so we round if necessary.
483 let mut tmp = remainder.low64();
484 let round = if (tmp as i64) < 0 {
485 // We round if we wrapped around
486 true
487 } else {
488 tmp <<= 1;
489 if tmp > divisor64 {
490 true
491 } else {
492 tmp == divisor64 && quotient.data[0] & 0x1 != 0
493 }
494 };
495
496 // If we need to round, try to do so.
497 if round {
498 scale = round_up(&mut quotient, scale)?;
499 }
500 break;
501 }
502 }
503
504 // Do some scaling
505 let power = POWERS_10[power_scale];
506 scale += power_scale as i32;
507
508 // Increase the quotient by the power that was looked up
509 let overflow = increase_scale(&mut quotient, power as u64);
510 if overflow > 0 {
511 return Err(DivError::Overflow);
512 }
513 increase_scale64(&mut remainder, power as u64);
514
515 let tmp = remainder.partial_divide_64(divisor64);
516 if let Err(DivError::Overflow) = quotient.add32(tmp) {
517 scale = unscale_from_overflow(&mut quotient, scale, remainder.low64() != 0)?;
518 break;
519 }
520 }
521
522 Ok(DivResult {
523 quotient,
524 scale,
525 require_unscale,
526 })
527 }
528
529 // Remainder processing loop for the 96-bit divisor path.
530 fn div_remainder_96(
531 mut remainder: Buf16,
532 divisor: Buf12,
533 mut quotient: Buf12,
534 mut scale: i32,
535 ) -> Result<DivResult, DivError> {
536 let mut require_unscale = false;
537 let mut power_scale = 0;
538
539 loop {
540 let mut rem_low64 = remainder.low64();
541 if rem_low64 == 0 && remainder.data[2] == 0 {
542 // If the scale is positive then we're actually done
543 if scale >= 0 {
544 break;
545 }
546 power_scale = 9usize.min((-scale) as usize);
547 } else {
548 // We may need to normalize later, so set the flag appropriately
549 require_unscale = true;
550
551 // We have a remainder so we effectively want to try to adjust the quotient and add
552 // the remainder into the quotient. We do this below, however first of all we want
553 // to try to avoid overflowing so we do that check first.
554 let will_overflow = if scale == MAX_SCALE_I32 {
555 true
556 } else {
557 // Figure out how much we can scale by
558 if let Some(s) = quotient.find_scale(scale) {
559 power_scale = s;
560 } else {
561 return Err(DivError::Overflow);
562 }
563 // If it comes back as 0 (i.e. 10^0 = 1) then we're going to overflow since
564 // we're doing nothing.
565 power_scale == 0
566 };
567 if will_overflow {
568 // No more scaling can be done, but remainder is non-zero so we round if necessary.
569 let round = if (remainder.data[2] as i32) < 0 {
570 // We round if we wrapped around
571 true
572 } else {
573 let tmp = remainder.data[1] >> 31;
574 rem_low64 <<= 1;
575 remainder.set_low64(rem_low64);
576 remainder.data[2] = (&remainder.data[2] << 1) + tmp;
577
578 match remainder.data[2].cmp(&divisor.data[2]) {
579 Ordering::Less => false,
580 Ordering::Equal => {
581 let divisor_low64 = divisor.low64();
582 if rem_low64 > divisor_low64 {
583 true
584 } else {
585 rem_low64 == divisor_low64 && (quotient.data[0] & 1) != 0
586 }
587 }
588 Ordering::Greater => true,
589 }
590 };
591
592 // If we need to round, try to do so.
593 if round {
594 scale = round_up(&mut quotient, scale)?;
595 }
596 break;
597 }
598 }
599
600 // Do some scaling
601 let power = POWERS_10[power_scale];
602 scale += power_scale as i32;
603
604 // Increase the quotient by the power that was looked up
605 let overflow = increase_scale(&mut quotient, power as u64);
606 if overflow > 0 {
607 return Err(DivError::Overflow);
608 }
609 let mut tmp_remainder = Buf12 {
610 data: [remainder.data[0], remainder.data[1], remainder.data[2]],
611 };
612 let overflow = increase_scale(&mut tmp_remainder, power as u64);
613 remainder.data[0] = tmp_remainder.data[0];
614 remainder.data[1] = tmp_remainder.data[1];
615 remainder.data[2] = tmp_remainder.data[2];
616 remainder.data[3] = overflow;
617
618 let tmp = remainder.partial_divide_96(&divisor);
619 if let Err(DivError::Overflow) = quotient.add32(tmp) {
620 scale = unscale_from_overflow(&mut quotient, scale, (remainder.low64() | remainder.high64()) != 0)?;
621 break;
622 }
623 }
624
625 Ok(DivResult {
626 quotient,
627 scale,
628 require_unscale,
629 })
630 }
631
632 // Multiply num by power (multiple of 10). Power must be 32 bits.
633 // Returns the overflow, if any
634 fn increase_scale(num: &mut Buf12, power: u64) -> u32 {
635 let mut tmp = (num.data[0] as u64) * power;
636 num.data[0] = tmp as u32;
637 tmp >>= 32;
638 tmp += (num.data[1] as u64) * power;
639 num.data[1] = tmp as u32;
640 tmp >>= 32;
641 tmp += (num.data[2] as u64) * power;
642 num.data[2] = tmp as u32;
643 (tmp >> 32) as u32
644 }
645
646 // Multiply num by power (multiple of 10). Power must be 32 bits.
647 fn increase_scale64(num: &mut Buf16, power: u64) {
648 let mut tmp = (num.data[0] as u64) * power;
649 num.data[0] = tmp as u32;
650 tmp >>= 32;
651 tmp += (num.data[1] as u64) * power;
652 num.set_mid64(tmp)
653 }
654
655 // Adjust the number to deal with an overflow. This function follows being scaled up (i.e. multiplied
656 // by 10, so this effectively tries to reverse that by dividing by 10 then feeding in the high bit
657 // to undo the overflow and rounding instead.
658 // Returns the updated scale.
659 #[inline]
660 fn unscale_from_overflow(num: &mut Buf12, scale: i32, sticky: bool) -> Result<i32, DivError> {
661 let scale = scale - 1;
662 if scale < 0 {
663 return Err(DivError::Overflow);
664 }
665
666 // This function is called when the hi portion has "overflowed" upon adding one and has wrapped
667 // back around to 0. Consequently, we need to "feed" that back in, but also rescaling down
668 // to reverse out the overflow.
669 const HIGH_BIT: u64 = 0x1_0000_0000;
670 num.data[2] = (HIGH_BIT / 10) as u32;
671
672 // Calc the mid
673 let mut tmp = ((HIGH_BIT % 10) << 32) + (num.data[1] as u64);
674 let mut val = (tmp / 10) as u32;
675 num.data[1] = val;
676
677 // Calc the lo using a similar method
678 tmp = ((tmp - (val as u64) * 10) << 32) + (num.data[0] as u64);
679 val = (tmp / 10) as u32;
680 num.data[0] = val;
681
682 // Work out the remainder, and round if we have one (since it doesn't fit)
683 let remainder = (tmp - (val as u64) * 10) as u32;
684 if remainder > 5 || (remainder == 5 && (sticky || num.data[0] & 0x1 > 0)) {
685 let _ = num.add32(1);
686 }
687 Ok(scale)
688 }
689
690 #[inline]
691 fn round_up(num: &mut Buf12, scale: i32) -> Result<i32, DivError> {
692 let low64 = num.low64().wrapping_add(1);
693 num.set_low64(low64);
694 if low64 != 0 {
695 return Ok(scale);
696 }
697 let hi = num.data[2].wrapping_add(1);
698 num.data[2] = hi;
699 if hi != 0 {
700 return Ok(scale);
701 }
702 unscale_from_overflow(num, scale, true)
703 }
704
705 fn unscale(num: &mut Buf12, scale: i32) -> i32 {
706 // Since 10 = 2 * 5, there must be a factor of 2 for every power of 10 we can extract.
707 // We use this as a quick test on whether to try a given power.
708 let mut scale = scale;
709 while num.data[0] == 0 && scale >= 8 && num.div32_const(100000000) {
710 scale -= 8;
711 }
712
713 if (num.data[0] & 0xF) == 0 && scale >= 4 && num.div32_const(10000) {
714 scale -= 4;
715 }
716
717 if (num.data[0] & 0x3) == 0 && scale >= 2 && num.div32_const(100) {
718 scale -= 2;
719 }
720
721 if (num.data[0] & 0x1) == 0 && scale >= 1 && num.div32_const(10) {
722 scale -= 1;
723 }
724 scale
725 }