Skip to content

Commit 7570024

Browse files
committed
dec2flt: Rename Decimal to DecimalSeq
This module currently contains two decimal types, `Decimal` and `Number`. These names don't provide a whole lot of insight into what exactly they are, and `Number` is actually the one that is more like an expected `Decimal` type. In accordance with this, rename the existing `Decimal` to `DecimalSeq`. This highlights that it contains a sequence of decimal digits, rather than representing a base-10 floating point (decimal) number. Additionally, add some tests to validate internal behavior.
1 parent bd7ba9a commit 7570024

File tree

5 files changed

+70
-22
lines changed

5 files changed

+70
-22
lines changed

library/core/src/num/dec2flt/decimal.rs renamed to library/core/src/num/dec2flt/decimal_seq.rs

+34-17
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
1212
use crate::num::dec2flt::common::{ByteSlice, is_8digits};
1313

14-
/// A decimal floating-point number.
15-
#[derive(Clone)]
16-
pub(super) struct Decimal {
14+
/// A decimal floating-point number, represented as a sequence of decimal digits.
15+
#[derive(Clone, Debug, PartialEq)]
16+
pub struct DecimalSeq {
1717
/// The number of significant digits in the decimal.
1818
pub num_digits: usize,
1919
/// The offset of the decimal point in the significant digits.
@@ -24,13 +24,13 @@ pub(super) struct Decimal {
2424
pub digits: [u8; Self::MAX_DIGITS],
2525
}
2626

27-
impl Default for Decimal {
27+
impl Default for DecimalSeq {
2828
fn default() -> Self {
2929
Self { num_digits: 0, decimal_point: 0, truncated: false, digits: [0; Self::MAX_DIGITS] }
3030
}
3131
}
3232

33-
impl Decimal {
33+
impl DecimalSeq {
3434
/// The maximum number of digits required to unambiguously round up to a 64-bit float.
3535
///
3636
/// For an IEEE 754 binary64 float, this required 767 digits. So we store the max digits + 1.
@@ -55,7 +55,8 @@ impl Decimal {
5555
///
5656
/// In Python:
5757
/// `-emin + p2 + math.floor((emin+ 1)*math.log(2, b)-math.log(1-2**(-p2), b))`
58-
pub(super) const MAX_DIGITS: usize = 768;
58+
pub const MAX_DIGITS: usize = 768;
59+
5960
/// The max decimal digits that can be exactly represented in a 64-bit integer.
6061
pub(super) const MAX_DIGITS_WITHOUT_OVERFLOW: usize = 19;
6162
pub(super) const DECIMAL_POINT_RANGE: i32 = 2047;
@@ -73,12 +74,12 @@ impl Decimal {
7374

7475
/// Trim trailing zeros from the buffer.
7576
// FIXME(tgross35): this could be `.rev().position()` if perf is okay
76-
pub(super) fn trim(&mut self) {
77-
// All of the following calls to `Decimal::trim` can't panic because:
77+
pub fn trim(&mut self) {
78+
// All of the following calls to `DecimalSeq::trim` can't panic because:
7879
//
79-
// 1. `parse_decimal` sets `num_digits` to a max of `Decimal::MAX_DIGITS`.
80+
// 1. `parse_decimal` sets `num_digits` to a max of `DecimalSeq::MAX_DIGITS`.
8081
// 2. `right_shift` sets `num_digits` to `write_index`, which is bounded by `num_digits`.
81-
// 3. `left_shift` `num_digits` to a max of `Decimal::MAX_DIGITS`.
82+
// 3. `left_shift` `num_digits` to a max of `DecimalSeq::MAX_DIGITS`.
8283
//
8384
// Trim is only called in `right_shift` and `left_shift`.
8485
debug_assert!(self.num_digits <= Self::MAX_DIGITS);
@@ -93,21 +94,26 @@ impl Decimal {
9394
} else if self.decimal_point >= Self::MAX_DIGITS_WITHOUT_OVERFLOW as i32 {
9495
return 0xFFFF_FFFF_FFFF_FFFF_u64;
9596
}
97+
9698
let dp = self.decimal_point as usize;
9799
let mut n = 0_u64;
100+
98101
for i in 0..dp {
99102
n *= 10;
100103
if i < self.num_digits {
101104
n += self.digits[i] as u64;
102105
}
103106
}
107+
104108
let mut round_up = false;
109+
105110
if dp < self.num_digits {
106111
round_up = self.digits[dp] >= 5;
107112
if self.digits[dp] == 5 && dp + 1 == self.num_digits {
108113
round_up = self.truncated || ((dp != 0) && (1 & self.digits[dp - 1] != 0))
109114
}
110115
}
116+
111117
if round_up {
112118
n += 1;
113119
}
@@ -123,6 +129,7 @@ impl Decimal {
123129
let mut read_index = self.num_digits;
124130
let mut write_index = self.num_digits + num_new_digits;
125131
let mut n = 0_u64;
132+
126133
while read_index != 0 {
127134
read_index -= 1;
128135
write_index -= 1;
@@ -136,6 +143,7 @@ impl Decimal {
136143
}
137144
n = quotient;
138145
}
146+
139147
while n > 0 {
140148
write_index -= 1;
141149
let quotient = n / 10;
@@ -147,10 +155,13 @@ impl Decimal {
147155
}
148156
n = quotient;
149157
}
158+
150159
self.num_digits += num_new_digits;
160+
151161
if self.num_digits > Self::MAX_DIGITS {
152162
self.num_digits = Self::MAX_DIGITS;
153163
}
164+
154165
self.decimal_point += num_new_digits as i32;
155166
self.trim();
156167
}
@@ -206,8 +217,8 @@ impl Decimal {
206217
}
207218

208219
/// Parse a big integer representation of the float as a decimal.
209-
pub(super) fn parse_decimal(mut s: &[u8]) -> Decimal {
210-
let mut d = Decimal::default();
220+
pub fn parse_decimal_seq(mut s: &[u8]) -> DecimalSeq {
221+
let mut d = DecimalSeq::default();
211222
let start = s;
212223

213224
while let Some((&b'0', s_next)) = s.split_first() {
@@ -225,7 +236,7 @@ pub(super) fn parse_decimal(mut s: &[u8]) -> Decimal {
225236
s = s_next;
226237
}
227238
}
228-
while s.len() >= 8 && d.num_digits + 8 < Decimal::MAX_DIGITS {
239+
while s.len() >= 8 && d.num_digits + 8 < DecimalSeq::MAX_DIGITS {
229240
let v = s.read_u64();
230241
if !is_8digits(v) {
231242
break;
@@ -237,6 +248,7 @@ pub(super) fn parse_decimal(mut s: &[u8]) -> Decimal {
237248
s = s.parse_digits(|digit| d.try_add_digit(digit));
238249
d.decimal_point = s.len() as i32 - first.len() as i32;
239250
}
251+
240252
if d.num_digits != 0 {
241253
// Ignore the trailing zeros if there are any
242254
let mut n_trailing_zeros = 0;
@@ -250,11 +262,12 @@ pub(super) fn parse_decimal(mut s: &[u8]) -> Decimal {
250262
d.decimal_point += n_trailing_zeros as i32;
251263
d.num_digits -= n_trailing_zeros;
252264
d.decimal_point += d.num_digits as i32;
253-
if d.num_digits > Decimal::MAX_DIGITS {
265+
if d.num_digits > DecimalSeq::MAX_DIGITS {
254266
d.truncated = true;
255-
d.num_digits = Decimal::MAX_DIGITS;
267+
d.num_digits = DecimalSeq::MAX_DIGITS;
256268
}
257269
}
270+
258271
if let Some((&ch, s_next)) = s.split_first() {
259272
if ch == b'e' || ch == b'E' {
260273
s = s_next;
@@ -276,13 +289,15 @@ pub(super) fn parse_decimal(mut s: &[u8]) -> Decimal {
276289
d.decimal_point += if neg_exp { -exp_num } else { exp_num };
277290
}
278291
}
279-
for i in d.num_digits..Decimal::MAX_DIGITS_WITHOUT_OVERFLOW {
292+
293+
for i in d.num_digits..DecimalSeq::MAX_DIGITS_WITHOUT_OVERFLOW {
280294
d.digits[i] = 0;
281295
}
296+
282297
d
283298
}
284299

285-
fn number_of_digits_decimal_left_shift(d: &Decimal, mut shift: usize) -> usize {
300+
fn number_of_digits_decimal_left_shift(d: &DecimalSeq, mut shift: usize) -> usize {
286301
#[rustfmt::skip]
287302
const TABLE: [u16; 65] = [
288303
0x0000, 0x0800, 0x0801, 0x0803, 0x1006, 0x1009, 0x100D, 0x1812, 0x1817, 0x181D, 0x2024,
@@ -347,6 +362,7 @@ fn number_of_digits_decimal_left_shift(d: &Decimal, mut shift: usize) -> usize {
347362
let pow5_a = (0x7FF & x_a) as usize;
348363
let pow5_b = (0x7FF & x_b) as usize;
349364
let pow5 = &TABLE_POW5[pow5_a..];
365+
350366
for (i, &p5) in pow5.iter().enumerate().take(pow5_b - pow5_a) {
351367
if i >= d.num_digits {
352368
return num_new_digits - 1;
@@ -358,5 +374,6 @@ fn number_of_digits_decimal_left_shift(d: &Decimal, mut shift: usize) -> usize {
358374
return num_new_digits;
359375
}
360376
}
377+
361378
num_new_digits
362379
}

library/core/src/num/dec2flt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ use crate::fmt;
9797
use crate::str::FromStr;
9898

9999
mod common;
100-
mod decimal;
100+
pub mod decimal_seq;
101101
mod fpu;
102102
mod slow;
103103
mod table;

library/core/src/num/dec2flt/slow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Slow, fallback algorithm for cases the Eisel-Lemire algorithm cannot round.
22
33
use crate::num::dec2flt::common::BiasedFp;
4-
use crate::num::dec2flt::decimal::{Decimal, parse_decimal};
4+
use crate::num::dec2flt::decimal_seq::{DecimalSeq, parse_decimal_seq};
55
use crate::num::dec2flt::float::RawFloat;
66

77
/// Parse the significant digits and biased, binary exponent of a float.
@@ -36,7 +36,7 @@ pub(crate) fn parse_long_mantissa<F: RawFloat>(s: &[u8]) -> BiasedFp {
3636
let fp_zero = BiasedFp::zero_pow2(0);
3737
let fp_inf = BiasedFp::zero_pow2(F::INFINITE_POWER);
3838

39-
let mut d = parse_decimal(s);
39+
let mut d = parse_decimal_seq(s);
4040

4141
// Short-circuit if the value can only be a literal 0 or infinity.
4242
if d.num_digits == 0 || d.decimal_point < -324 {
@@ -50,7 +50,7 @@ pub(crate) fn parse_long_mantissa<F: RawFloat>(s: &[u8]) -> BiasedFp {
5050
let n = d.decimal_point as usize;
5151
let shift = get_shift(n);
5252
d.right_shift(shift);
53-
if d.decimal_point < -Decimal::DECIMAL_POINT_RANGE {
53+
if d.decimal_point < -DecimalSeq::DECIMAL_POINT_RANGE {
5454
return fp_zero;
5555
}
5656
exp2 += shift as i32;
@@ -67,7 +67,7 @@ pub(crate) fn parse_long_mantissa<F: RawFloat>(s: &[u8]) -> BiasedFp {
6767
get_shift((-d.decimal_point) as _)
6868
};
6969
d.left_shift(shift);
70-
if d.decimal_point > Decimal::DECIMAL_POINT_RANGE {
70+
if d.decimal_point > DecimalSeq::DECIMAL_POINT_RANGE {
7171
return fp_inf;
7272
}
7373
exp2 -= shift as i32;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use core::num::dec2flt::decimal_seq::{DecimalSeq, parse_decimal_seq};
2+
3+
#[test]
4+
fn test_trim() {
5+
let mut dec = DecimalSeq::default();
6+
let digits = [1, 2, 3, 4];
7+
8+
dec.digits[0..4].copy_from_slice(&digits);
9+
dec.num_digits = 8;
10+
dec.trim();
11+
12+
assert_eq!(dec.digits[0..4], digits);
13+
assert_eq!(dec.num_digits, 4);
14+
}
15+
16+
#[test]
17+
fn test_parse() {
18+
let tests = [("1.234", [1, 2, 3, 4], 1)];
19+
20+
for (s, exp_digits, decimal_point) in tests {
21+
let actual = parse_decimal_seq(s.as_bytes());
22+
let mut digits = [0; DecimalSeq::MAX_DIGITS];
23+
digits[..exp_digits.len()].copy_from_slice(&exp_digits);
24+
25+
let expected =
26+
DecimalSeq { num_digits: exp_digits.len(), decimal_point, truncated: false, digits };
27+
28+
assert_eq!(actual, expected);
29+
}
30+
}

library/coretests/tests/num/dec2flt/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(overflowing_literals)]
22

3+
mod decimal_seq;
34
mod float;
45
mod lemire;
56
mod parse;

0 commit comments

Comments
 (0)