Skip to main content

core/num/imp/dec2flt/
slow.rs

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