lexical_parse_float/lemire.rs
1//! Implementation of the Eisel-Lemire algorithm.
2//!
3//! This is adapted from [fast-float-rust](https://github.com/aldanor/fast-float-rust),
4//! a port of [fast_float](https://github.com/fastfloat/fast_float) to Rust.
5
6#![cfg(not(feature = "compact"))]
7#![doc(hidden)]
8
9use crate::float::{ExtendedFloat80, LemireFloat};
10use crate::number::Number;
11use crate::shared;
12use crate::table::{LARGEST_POWER_OF_FIVE, POWER_OF_FIVE_128, SMALLEST_POWER_OF_FIVE};
13
14/// Ensure truncation of digits doesn't affect our computation, by doing 2 passes.
15#[inline]
16pub fn lemire<F: LemireFloat>(num: &Number, lossy: bool) -> ExtendedFloat80 {
17 // If significant digits were truncated, then we can have rounding error
18 // only if `mantissa + 1` produces a different result. We also avoid
19 // redundantly using the Eisel-Lemire algorithm if it was unable to
20 // correctly round on the first pass.
21 let mut fp = compute_float::<F>(num.exponent, num.mantissa, lossy);
22 if !lossy
23 && num.many_digits
24 && fp.exp >= 0
25 && fp != compute_float::<F>(num.exponent, num.mantissa + 1, false)
26 {
27 // Need to re-calculate, since the previous values are rounded
28 // when the slow path algorithm expects a normalized extended float.
29 fp = compute_error::<F>(num.exponent, num.mantissa);
30 }
31 fp
32}
33
34/// Compute a float using an extended-precision representation.
35///
36/// Fast conversion of a the significant digits and decimal exponent
37/// a float to a extended representation with a binary float. This
38/// algorithm will accurately parse the vast majority of cases,
39/// and uses a 128-bit representation (with a fallback 192-bit
40/// representation).
41///
42/// This algorithm scales the exponent by the decimal exponent
43/// using pre-computed powers-of-5, and calculates if the
44/// representation can be unambiguously rounded to the nearest
45/// machine float. Near-halfway cases are not handled here,
46/// and are represented by a negative, biased binary exponent.
47///
48/// The algorithm is described in detail in "Daniel Lemire, Number Parsing
49/// at a Gigabyte per Second" in section 5, "Fast Algorithm", and
50/// section 6, "Exact Numbers And Ties", available online:
51/// <https://arxiv.org/abs/2101.11408.pdf>.
52pub fn compute_float<F: LemireFloat>(q: i64, mut w: u64, lossy: bool) -> ExtendedFloat80 {
53 let fp_zero = ExtendedFloat80 {
54 mant: 0,
55 exp: 0,
56 };
57 let fp_inf = ExtendedFloat80 {
58 mant: 0,
59 exp: F::INFINITE_POWER,
60 };
61
62 // Short-circuit if the value can only be a literal 0 or infinity.
63 if w == 0 || q < F::SMALLEST_POWER_OF_TEN as i64 {
64 return fp_zero;
65 } else if q > F::LARGEST_POWER_OF_TEN as i64 {
66 return fp_inf;
67 }
68 // Normalize our significant digits, so the most-significant bit is set.
69 let lz = w.leading_zeros() as i32;
70 w <<= lz;
71 let (lo, hi) = compute_product_approx(q, w, F::MANTISSA_SIZE as usize + 3);
72 if !lossy && lo == 0xFFFF_FFFF_FFFF_FFFF {
73 // If we have failed to approximate w x 5^-q with our 128-bit value.
74 // Since the addition of 1 could lead to an overflow which could then
75 // round up over the half-way point, this can lead to improper rounding
76 // of a float.
77 //
78 // However, this can only occur if q ∈ [-27, 55]. The upper bound of q
79 // is 55 because 5^55 < 2^128, however, this can only happen if 5^q > 2^64,
80 // since otherwise the product can be represented in 64-bits, producing
81 // an exact result. For negative exponents, rounding-to-even can
82 // only occur if 5^-q < 2^64.
83 //
84 // For detailed explanations of rounding for negative exponents, see
85 // <https://arxiv.org/pdf/2101.11408.pdf#section.9.1>. For detailed
86 // explanations of rounding for positive exponents, see
87 // <https://arxiv.org/pdf/2101.11408.pdf#section.8>.
88 let inside_safe_exponent = (q >= -27) && (q <= 55);
89 if !inside_safe_exponent {
90 return compute_error_scaled::<F>(q, hi, lz);
91 }
92 }
93 let upperbit = (hi >> 63) as i32;
94 let mut mantissa = hi >> (upperbit + 64 - F::MANTISSA_SIZE - 3);
95 let mut power2 = power(q as i32) + upperbit - lz - F::MINIMUM_EXPONENT;
96 if power2 <= 0 {
97 if -power2 + 1 >= 64 {
98 // Have more than 64 bits below the minimum exponent, must be 0.
99 return fp_zero;
100 }
101 // Have a subnormal value.
102 mantissa >>= -power2 + 1;
103 mantissa += mantissa & 1;
104 mantissa >>= 1;
105 power2 = (mantissa >= (1_u64 << F::MANTISSA_SIZE)) as i32;
106 return ExtendedFloat80 {
107 mant: mantissa,
108 exp: power2,
109 };
110 }
111 // Need to handle rounding ties. Normally, we need to round up,
112 // but if we fall right in between and and we have an even basis, we
113 // need to round down.
114 //
115 // This will only occur if:
116 // 1. The lower 64 bits of the 128-bit representation is 0.
117 // IE, 5^q fits in single 64-bit word.
118 // 2. The least-significant bit prior to truncated mantissa is odd.
119 // 3. All the bits truncated when shifting to mantissa bits + 1 are 0.
120 //
121 // Or, we may fall between two floats: we are exactly halfway.
122 if lo <= 1
123 && q >= F::MIN_EXPONENT_ROUND_TO_EVEN as i64
124 && q <= F::MAX_EXPONENT_ROUND_TO_EVEN as i64
125 && mantissa & 3 == 1
126 && (mantissa << (upperbit + 64 - F::MANTISSA_SIZE - 3)) == hi
127 {
128 // Zero the lowest bit, so we don't round up.
129 mantissa &= !1_u64;
130 }
131 // Round-to-even, then shift the significant digits into place.
132 mantissa += mantissa & 1;
133 mantissa >>= 1;
134 if mantissa >= (2_u64 << F::MANTISSA_SIZE) {
135 // Rounding up overflowed, so the carry bit is set. Set the
136 // mantissa to 1 (only the implicit, hidden bit is set) and
137 // increase the exponent.
138 mantissa = 1_u64 << F::MANTISSA_SIZE;
139 power2 += 1;
140 }
141 // Zero out the hidden bit.
142 mantissa &= !(1_u64 << F::MANTISSA_SIZE);
143 if power2 >= F::INFINITE_POWER {
144 // Exponent is above largest normal value, must be infinite.
145 return fp_inf;
146 }
147 ExtendedFloat80 {
148 mant: mantissa,
149 exp: power2,
150 }
151}
152
153/// Fallback algorithm to calculate the non-rounded representation.
154/// This calculates the extended representation, and then normalizes
155/// the resulting representation, so the high bit is set.
156#[inline]
157pub fn compute_error<F: LemireFloat>(q: i64, mut w: u64) -> ExtendedFloat80 {
158 let lz = w.leading_zeros() as i32;
159 w <<= lz;
160 let hi = compute_product_approx(q, w, F::MANTISSA_SIZE as usize + 3).1;
161 compute_error_scaled::<F>(q, hi, lz)
162}
163
164/// Compute the error from a mantissa scaled to the exponent.
165#[inline]
166pub fn compute_error_scaled<F: LemireFloat>(q: i64, mut w: u64, lz: i32) -> ExtendedFloat80 {
167 // Want to normalize the float, but this is faster than ctlz on most architectures.
168 let hilz = (w >> 63) as i32 ^ 1;
169 w <<= hilz;
170 let power2 = power(q as i32) + F::EXPONENT_BIAS - hilz - lz - 62;
171
172 ExtendedFloat80 {
173 mant: w,
174 exp: power2 + shared::INVALID_FP,
175 }
176}
177
178/// Calculate a base 2 exponent from a decimal exponent.
179/// This uses a pre-computed integer approximation for
180/// log2(10), where 217706 / 2^16 is accurate for the
181/// entire range of non-finite decimal exponents.
182#[inline]
183fn power(q: i32) -> i32 {
184 (q.wrapping_mul(152_170 + 65536) >> 16) + 63
185}
186
187#[inline]
188fn full_multiplication(a: u64, b: u64) -> (u64, u64) {
189 let r = (a as u128) * (b as u128);
190 (r as u64, (r >> 64) as u64)
191}
192
193// This will compute or rather approximate w * 5**q and return a pair of 64-bit words
194// approximating the result, with the "high" part corresponding to the most significant
195// bits and the low part corresponding to the least significant bits.
196fn compute_product_approx(q: i64, w: u64, precision: usize) -> (u64, u64) {
197 debug_assert!(q >= SMALLEST_POWER_OF_FIVE as i64);
198 debug_assert!(q <= LARGEST_POWER_OF_FIVE as i64);
199 debug_assert!(precision <= 64);
200
201 let mask = if precision < 64 {
202 0xFFFF_FFFF_FFFF_FFFF_u64 >> precision
203 } else {
204 0xFFFF_FFFF_FFFF_FFFF_u64
205 };
206
207 // 5^q < 2^64, then the multiplication always provides an exact value.
208 // That means whenever we need to round ties to even, we always have
209 // an exact value.
210 let index = (q - SMALLEST_POWER_OF_FIVE as i64) as usize;
211 let (lo5, hi5) = POWER_OF_FIVE_128[index];
212 // Only need one multiplication as long as there is 1 zero but
213 // in the explicit mantissa bits, +1 for the hidden bit, +1 to
214 // determine the rounding direction, +1 for if the computed
215 // product has a leading zero.
216 let (mut first_lo, mut first_hi) = full_multiplication(w, lo5);
217 if first_hi & mask == mask {
218 // Need to do a second multiplication to get better precision
219 // for the lower product. This will always be exact
220 // where q is < 55, since 5^55 < 2^128. If this wraps,
221 // then we need to need to round up the hi product.
222 let (_, second_hi) = full_multiplication(w, hi5);
223 first_lo = first_lo.wrapping_add(second_hi);
224 if second_hi > first_lo {
225 first_hi += 1;
226 }
227 }
228 (first_lo, first_hi)
229}