@@ -34,14 +34,15 @@ pub struct Decimal {
34
34
impl Decimal {
35
35
/// Detect if the float can be accurately reconstructed from native floats.
36
36
#[ inline]
37
- fn is_fast_path < F : RawFloat > ( & self ) -> bool {
37
+ fn can_use_fast_path < F : RawFloat > ( & self ) -> bool {
38
38
F :: MIN_EXPONENT_FAST_PATH <= self . exponent
39
39
&& self . exponent <= F :: MAX_EXPONENT_DISGUISED_FAST_PATH
40
40
&& self . mantissa <= F :: MAX_MANTISSA_FAST_PATH
41
41
&& !self . many_digits
42
42
}
43
43
44
- /// The fast path algorithm using machine-sized integers and floats.
44
+ /// Try turning the decimal into an exact float representation, using machine-sized integers
45
+ /// and floats.
45
46
///
46
47
/// This is extracted into a separate function so that it can be attempted before constructing
47
48
/// a Decimal. This only works if both the mantissa and the exponent
@@ -59,30 +60,28 @@ impl Decimal {
59
60
// require setting it by changing the global state (like the control word of the x87 FPU).
60
61
let _cw = set_precision :: < F > ( ) ;
61
62
62
- if self . is_fast_path :: < F > ( ) {
63
- let mut value = if self . exponent <= F :: MAX_EXPONENT_FAST_PATH {
64
- // normal fast path
65
- let value = F :: from_u64 ( self . mantissa ) ;
66
- if self . exponent < 0 {
67
- value / F :: pow10_fast_path ( ( - self . exponent ) as _ )
68
- } else {
69
- value * F :: pow10_fast_path ( self . exponent as _ )
70
- }
63
+ if ! self . can_use_fast_path :: < F > ( ) {
64
+ return None ;
65
+ }
66
+
67
+ let value = if self . exponent <= F :: MAX_EXPONENT_FAST_PATH {
68
+ // normal fast path
69
+ let value = F :: from_u64 ( self . mantissa ) ;
70
+ if self . exponent < 0 {
71
+ value / F :: pow10_fast_path ( ( - self . exponent ) as _ )
71
72
} else {
72
- // disguised fast path
73
- let shift = self . exponent - F :: MAX_EXPONENT_FAST_PATH ;
74
- let mantissa = self . mantissa . checked_mul ( INT_POW10 [ shift as usize ] ) ?;
75
- if mantissa > F :: MAX_MANTISSA_FAST_PATH {
76
- return None ;
77
- }
78
- F :: from_u64 ( mantissa) * F :: pow10_fast_path ( F :: MAX_EXPONENT_FAST_PATH as _ )
79
- } ;
80
- if self . negative {
81
- value = -value;
73
+ value * F :: pow10_fast_path ( self . exponent as _ )
82
74
}
83
- Some ( value)
84
75
} else {
85
- None
86
- }
76
+ // disguised fast path
77
+ let shift = self . exponent - F :: MAX_EXPONENT_FAST_PATH ;
78
+ let mantissa = self . mantissa . checked_mul ( INT_POW10 [ shift as usize ] ) ?;
79
+ if mantissa > F :: MAX_MANTISSA_FAST_PATH {
80
+ return None ;
81
+ }
82
+ F :: from_u64 ( mantissa) * F :: pow10_fast_path ( F :: MAX_EXPONENT_FAST_PATH as _ )
83
+ } ;
84
+
85
+ if self . negative { Some ( -value) } else { Some ( value) }
87
86
}
88
87
}
0 commit comments