1//! Slow, fallback algorithm for cases the Eisel-Lemire algorithm cannot round.
23use crate::num::dec2flt::common::BiasedFp;
4use crate::num::dec2flt::decimal::{Decimal, parse_decimal};
5use crate::num::dec2flt::float::RawFloat;
67/// Parse the significant digits and biased, binary exponent of a float.
8///
9/// This is a fallback algorithm that uses a big-integer representation
10/// of the float, and therefore is considerably slower than faster
11/// approximations. However, it will always determine how to round
12/// the significant digits to the nearest machine float, allowing
13/// use to handle near half-way cases.
14///
15/// Near half-way cases are halfway between two consecutive machine floats.
16/// For example, the float `16777217.0` has a bitwise representation of
17/// `100000000000000000000000 1`. Rounding to a single-precision float,
18/// the trailing `1` is truncated. Using round-nearest, tie-even, any
19/// value above `16777217.0` must be rounded up to `16777218.0`, while
20/// any value before or equal to `16777217.0` must be rounded down
21/// to `16777216.0`. These near-halfway conversions therefore may require
22/// a large number of digits to unambiguously determine how to round.
23///
24/// The algorithms described here are based on "Processing Long Numbers Quickly",
25/// available here: <https://arxiv.org/pdf/2101.11408.pdf#section.11>.
26pub(crate) fn parse_long_mantissa<F: RawFloat>(s: &[u8]) -> BiasedFp {
27const MAX_SHIFT: usize = 60;
28const NUM_POWERS: usize = 19;
29const POWERS: [u8; 19] =
30 [0, 3, 6, 9, 13, 16, 19, 23, 26, 29, 33, 36, 39, 43, 46, 49, 53, 56, 59];
3132let get_shift = |n| {
33if n < NUM_POWERS { POWERS[n] as usize } else { MAX_SHIFT }
34 };
3536let fp_zero = BiasedFp::zero_pow2(0);
37let fp_inf = BiasedFp::zero_pow2(F::INFINITE_POWER);
3839let mut d = parse_decimal(s);
4041// Short-circuit if the value can only be a literal 0 or infinity.
42if d.num_digits == 0 || d.decimal_point < -324 {
43return fp_zero;
44 } else if d.decimal_point >= 310 {
45return fp_inf;
46 }
47let mut exp2 = 0_i32;
48// Shift right toward (1/2 ... 1].
49while d.decimal_point > 0 {
50let n = d.decimal_point as usize;
51let shift = get_shift(n);
52 d.right_shift(shift);
53if d.decimal_point < -Decimal::DECIMAL_POINT_RANGE {
54return fp_zero;
55 }
56 exp2 += shift as i32;
57 }
58// Shift left toward (1/2 ... 1].
59while d.decimal_point <= 0 {
60let shift = if d.decimal_point == 0 {
61match d.digits[0] {
62 digit if digit >= 5 => break,
630 | 1 => 2,
64_ => 1,
65 }
66 } else {
67 get_shift((-d.decimal_point) as _)
68 };
69 d.left_shift(shift);
70if d.decimal_point > Decimal::DECIMAL_POINT_RANGE {
71return fp_inf;
72 }
73 exp2 -= shift as i32;
74 }
75// We are now in the range [1/2 ... 1] but the binary format uses [1 ... 2].
76exp2 -= 1;
77while (F::MINIMUM_EXPONENT + 1) > exp2 {
78let mut n = ((F::MINIMUM_EXPONENT + 1) - exp2) as usize;
79if n > MAX_SHIFT {
80 n = MAX_SHIFT;
81 }
82 d.right_shift(n);
83 exp2 += n as i32;
84 }
85if (exp2 - F::MINIMUM_EXPONENT) >= F::INFINITE_POWER {
86return fp_inf;
87 }
88// Shift the decimal to the hidden bit, and then round the value
89 // to get the high mantissa+1 bits.
90d.left_shift(F::MANTISSA_EXPLICIT_BITS + 1);
91let mut mantissa = d.round();
92if mantissa >= (1_u64 << (F::MANTISSA_EXPLICIT_BITS + 1)) {
93// Rounding up overflowed to the carry bit, need to
94 // shift back to the hidden bit.
95d.right_shift(1);
96 exp2 += 1;
97 mantissa = d.round();
98if (exp2 - F::MINIMUM_EXPONENT) >= F::INFINITE_POWER {
99return fp_inf;
100 }
101 }
102let mut power2 = exp2 - F::MINIMUM_EXPONENT;
103if mantissa < (1_u64 << F::MANTISSA_EXPLICIT_BITS) {
104 power2 -= 1;
105 }
106// Zero out all the bits above the explicit mantissa bits.
107mantissa &= (1_u64 << F::MANTISSA_EXPLICIT_BITS) - 1;
108 BiasedFp { f: mantissa, e: power2 }
109}