11
11
12
12
use crate :: num:: dec2flt:: common:: { ByteSlice , is_8digits} ;
13
13
14
- /// A decimal floating-point number.
15
- #[ derive( Clone ) ]
16
- pub struct Decimal {
14
+ /// A decimal floating-point number, represented as a sequence of decimal digits .
15
+ #[ derive( Clone , Debug , PartialEq ) ]
16
+ pub struct DecimalSeq {
17
17
/// The number of significant digits in the decimal.
18
18
pub num_digits : usize ,
19
19
/// The offset of the decimal point in the significant digits.
@@ -24,13 +24,13 @@ pub struct Decimal {
24
24
pub digits : [ u8 ; Self :: MAX_DIGITS ] ,
25
25
}
26
26
27
- impl Default for Decimal {
27
+ impl Default for DecimalSeq {
28
28
fn default ( ) -> Self {
29
29
Self { num_digits : 0 , decimal_point : 0 , truncated : false , digits : [ 0 ; Self :: MAX_DIGITS ] }
30
30
}
31
31
}
32
32
33
- impl Decimal {
33
+ impl DecimalSeq {
34
34
/// The maximum number of digits required to unambiguously round up to a 64-bit float.
35
35
///
36
36
/// For an IEEE 754 binary64 float, this required 767 digits. So we store the max digits + 1.
@@ -74,11 +74,11 @@ impl Decimal {
74
74
/// Trim trailing zeros from the buffer.
75
75
// FIXME(tgross35): this could be `.rev().position()` if perf is okay
76
76
pub fn trim ( & mut self ) {
77
- // All of the following calls to `Decimal ::trim` can't panic because:
77
+ // All of the following calls to `DecimalSeq ::trim` can't panic because:
78
78
//
79
- // 1. `parse_decimal` sets `num_digits` to a max of `Decimal ::MAX_DIGITS`.
79
+ // 1. `parse_decimal` sets `num_digits` to a max of `DecimalSeq ::MAX_DIGITS`.
80
80
// 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`.
81
+ // 3. `left_shift` `num_digits` to a max of `DecimalSeq ::MAX_DIGITS`.
82
82
//
83
83
// Trim is only called in `right_shift` and `left_shift`.
84
84
debug_assert ! ( self . num_digits <= Self :: MAX_DIGITS ) ;
@@ -93,21 +93,26 @@ impl Decimal {
93
93
} else if self . decimal_point >= Self :: MAX_DIGITS_WITHOUT_OVERFLOW as i32 {
94
94
return 0xFFFF_FFFF_FFFF_FFFF_u64 ;
95
95
}
96
+
96
97
let dp = self . decimal_point as usize ;
97
98
let mut n = 0_u64 ;
99
+
98
100
for i in 0 ..dp {
99
101
n *= 10 ;
100
102
if i < self . num_digits {
101
103
n += self . digits [ i] as u64 ;
102
104
}
103
105
}
106
+
104
107
let mut round_up = false ;
108
+
105
109
if dp < self . num_digits {
106
110
round_up = self . digits [ dp] >= 5 ;
107
111
if self . digits [ dp] == 5 && dp + 1 == self . num_digits {
108
112
round_up = self . truncated || ( ( dp != 0 ) && ( 1 & self . digits [ dp - 1 ] != 0 ) )
109
113
}
110
114
}
115
+
111
116
if round_up {
112
117
n += 1 ;
113
118
}
@@ -123,6 +128,7 @@ impl Decimal {
123
128
let mut read_index = self . num_digits ;
124
129
let mut write_index = self . num_digits + num_new_digits;
125
130
let mut n = 0_u64 ;
131
+
126
132
while read_index != 0 {
127
133
read_index -= 1 ;
128
134
write_index -= 1 ;
@@ -136,6 +142,7 @@ impl Decimal {
136
142
}
137
143
n = quotient;
138
144
}
145
+
139
146
while n > 0 {
140
147
write_index -= 1 ;
141
148
let quotient = n / 10 ;
@@ -147,10 +154,13 @@ impl Decimal {
147
154
}
148
155
n = quotient;
149
156
}
157
+
150
158
self . num_digits += num_new_digits;
159
+
151
160
if self . num_digits > Self :: MAX_DIGITS {
152
161
self . num_digits = Self :: MAX_DIGITS ;
153
162
}
163
+
154
164
self . decimal_point += num_new_digits as i32 ;
155
165
self . trim ( ) ;
156
166
}
@@ -206,8 +216,8 @@ impl Decimal {
206
216
}
207
217
208
218
/// Parse a big integer representation of the float as a decimal.
209
- pub fn parse_decimal ( mut s : & [ u8 ] ) -> Decimal {
210
- let mut d = Decimal :: default ( ) ;
219
+ pub fn parse_decimal_seq ( mut s : & [ u8 ] ) -> DecimalSeq {
220
+ let mut d = DecimalSeq :: default ( ) ;
211
221
let start = s;
212
222
213
223
while let Some ( ( & b'0' , s_next) ) = s. split_first ( ) {
@@ -225,7 +235,7 @@ pub fn parse_decimal(mut s: &[u8]) -> Decimal {
225
235
s = s_next;
226
236
}
227
237
}
228
- while s. len ( ) >= 8 && d. num_digits + 8 < Decimal :: MAX_DIGITS {
238
+ while s. len ( ) >= 8 && d. num_digits + 8 < DecimalSeq :: MAX_DIGITS {
229
239
let v = s. read_u64 ( ) ;
230
240
if !is_8digits ( v) {
231
241
break ;
@@ -237,6 +247,7 @@ pub fn parse_decimal(mut s: &[u8]) -> Decimal {
237
247
s = s. parse_digits ( |digit| d. try_add_digit ( digit) ) ;
238
248
d. decimal_point = s. len ( ) as i32 - first. len ( ) as i32 ;
239
249
}
250
+
240
251
if d. num_digits != 0 {
241
252
// Ignore the trailing zeros if there are any
242
253
let mut n_trailing_zeros = 0 ;
@@ -250,11 +261,12 @@ pub fn parse_decimal(mut s: &[u8]) -> Decimal {
250
261
d. decimal_point += n_trailing_zeros as i32 ;
251
262
d. num_digits -= n_trailing_zeros;
252
263
d. decimal_point += d. num_digits as i32 ;
253
- if d. num_digits > Decimal :: MAX_DIGITS {
264
+ if d. num_digits > DecimalSeq :: MAX_DIGITS {
254
265
d. truncated = true ;
255
- d. num_digits = Decimal :: MAX_DIGITS ;
266
+ d. num_digits = DecimalSeq :: MAX_DIGITS ;
256
267
}
257
268
}
269
+
258
270
if let Some ( ( & ch, s_next) ) = s. split_first ( ) {
259
271
if ch == b'e' || ch == b'E' {
260
272
s = s_next;
@@ -276,13 +288,15 @@ pub fn parse_decimal(mut s: &[u8]) -> Decimal {
276
288
d. decimal_point += if neg_exp { -exp_num } else { exp_num } ;
277
289
}
278
290
}
279
- for i in d. num_digits ..Decimal :: MAX_DIGITS_WITHOUT_OVERFLOW {
291
+
292
+ for i in d. num_digits ..DecimalSeq :: MAX_DIGITS_WITHOUT_OVERFLOW {
280
293
d. digits [ i] = 0 ;
281
294
}
295
+
282
296
d
283
297
}
284
298
285
- fn number_of_digits_decimal_left_shift ( d : & Decimal , mut shift : usize ) -> usize {
299
+ fn number_of_digits_decimal_left_shift ( d : & DecimalSeq , mut shift : usize ) -> usize {
286
300
#[ rustfmt:: skip]
287
301
const TABLE : [ u16 ; 65 ] = [
288
302
0x0000 , 0x0800 , 0x0801 , 0x0803 , 0x1006 , 0x1009 , 0x100D , 0x1812 , 0x1817 , 0x181D , 0x2024 ,
@@ -347,6 +361,7 @@ fn number_of_digits_decimal_left_shift(d: &Decimal, mut shift: usize) -> usize {
347
361
let pow5_a = ( 0x7FF & x_a) as usize ;
348
362
let pow5_b = ( 0x7FF & x_b) as usize ;
349
363
let pow5 = & TABLE_POW5 [ pow5_a..] ;
364
+
350
365
for ( i, & p5) in pow5. iter ( ) . enumerate ( ) . take ( pow5_b - pow5_a) {
351
366
if i >= d. num_digits {
352
367
return num_new_digits - 1 ;
@@ -358,5 +373,6 @@ fn number_of_digits_decimal_left_shift(d: &Decimal, mut shift: usize) -> usize {
358
373
return num_new_digits;
359
374
}
360
375
}
376
+
361
377
num_new_digits
362
378
}
0 commit comments