2
2
3
3
use crate :: fmt:: { Debug , LowerExp } ;
4
4
use crate :: num:: FpCategory ;
5
- use crate :: ops:: { Add , Div , Mul , Neg } ;
5
+ use crate :: ops:: { self , Add , Div , Mul , Neg } ;
6
6
7
- /// A helper trait to avoid duplicating basically all the conversion code for `f32` and `f64`.
7
+ /// Lossy `as` casting between two types.
8
+ pub trait CastInto < T : Copy > : Copy {
9
+ fn cast ( self ) -> T ;
10
+ }
11
+
12
+ /// Collection of traits that allow us to be generic over integer size.
13
+ pub trait Integer :
14
+ Sized
15
+ + Clone
16
+ + Copy
17
+ + Debug
18
+ + ops:: Shr < u32 , Output = Self >
19
+ + ops:: Shl < u32 , Output = Self >
20
+ + ops:: BitAnd < Output = Self >
21
+ + ops:: BitOr < Output = Self >
22
+ + PartialEq
23
+ + CastInto < i16 >
24
+ {
25
+ const ZERO : Self ;
26
+ const ONE : Self ;
27
+ }
28
+
29
+ macro_rules! int {
30
+ ( $( $ty: ty) ,+) => {
31
+ $(
32
+ impl CastInto <i16 > for $ty {
33
+ fn cast( self ) -> i16 {
34
+ self as i16
35
+ }
36
+ }
37
+
38
+ impl Integer for $ty {
39
+ const ZERO : Self = 0 ;
40
+ const ONE : Self = 1 ;
41
+ }
42
+ ) +
43
+ }
44
+ }
45
+
46
+ int ! ( u32 , u64 ) ;
47
+
48
+ /// A helper trait to avoid duplicating basically all the conversion code for IEEE floats.
8
49
///
9
50
/// See the parent module's doc comment for why this is necessary.
10
51
///
11
- /// Should **never ever** be implemented for other types or be used outside the dec2flt module.
52
+ /// Should **never ever** be implemented for other types or be used outside the ` dec2flt` module.
12
53
#[ doc( hidden) ]
13
54
pub trait RawFloat :
14
55
Sized
@@ -24,62 +65,93 @@ pub trait RawFloat:
24
65
+ Copy
25
66
+ Debug
26
67
{
68
+ /// The unsigned integer with the same size as the float
69
+ type Int : Integer + Into < u64 > ;
70
+
71
+ /* general constants */
72
+
27
73
const INFINITY : Self ;
28
74
const NEG_INFINITY : Self ;
29
75
const NAN : Self ;
30
76
const NEG_NAN : Self ;
31
77
78
+ /// Bit width of the float
79
+ const BITS : u32 ;
80
+
81
+ /// Mantissa digits including the hidden bit (provided by core)
82
+ const MANTISSA_BITS : u32 ;
83
+
84
+ const EXPONENT_MASK : Self :: Int ;
85
+ const MANTISSA_MASK : Self :: Int ;
86
+
32
87
/// The number of bits in the significand, *excluding* the hidden bit.
33
- const MANTISSA_EXPLICIT_BITS : usize ;
34
-
35
- // Round-to-even only happens for negative values of q
36
- // when q ≥ −4 in the 64-bit case and when q ≥ −17 in
37
- // the 32-bitcase.
38
- //
39
- // When q ≥ 0,we have that 5^q ≤ 2m+1. In the 64-bit case,we
40
- // have 5^q ≤ 2m+1 ≤ 2^54 or q ≤ 23. In the 32-bit case,we have
41
- // 5^q ≤ 2m+1 ≤ 2^25 or q ≤ 10.
42
- //
43
- // When q < 0, we have w ≥ (2m+1)×5^−q. We must have that w < 2^64
44
- // so (2m+1)×5^−q < 2^64. We have that 2m+1 > 2^53 (64-bit case)
45
- // or 2m+1 > 2^24 (32-bit case). Hence,we must have 2^53×5^−q < 2^64
46
- // (64-bit) and 2^24×5^−q < 2^64 (32-bit). Hence we have 5^−q < 2^11
47
- // or q ≥ −4 (64-bit case) and 5^−q < 2^40 or q ≥ −17 (32-bitcase).
48
- //
49
- // Thus we have that we only need to round ties to even when
50
- // we have that q ∈ [−4,23](in the 64-bit case) or q∈[−17,10]
51
- // (in the 32-bit case). In both cases,the power of five(5^|q|)
52
- // fits in a 64-bit word.
88
+ const MANTISSA_EXPLICIT_BITS : u32 = Self :: MANTISSA_BITS - 1 ;
89
+
90
+ /// Bits for the exponent
91
+ const EXPONENT_BITS : u32 = Self :: BITS - Self :: MANTISSA_EXPLICIT_BITS - 1 ;
92
+
93
+ /// Minimum exponent value `-(1 << (EXP_BITS - 1)) + 1`.
94
+ const MINIMUM_EXPONENT : i32 = -( 1 << ( Self :: EXPONENT_BITS - 1 ) ) + 1 ;
95
+
96
+ /// Maximum exponent without overflowing to infinity
97
+ const MAXIMUM_EXPONENT : u32 = ( 1 << Self :: EXPONENT_BITS ) - 1 ;
98
+
99
+ /// The exponent bias value
100
+ const EXPONENT_BIAS : u32 = Self :: MAXIMUM_EXPONENT >> 1 ;
101
+
102
+ /// Largest exponent value `(1 << EXP_BITS) - 1`.
103
+ const INFINITE_POWER : i32 = ( 1 << Self :: EXPONENT_BITS ) - 1 ;
104
+
105
+ /// Round-to-even only happens for negative values of q
106
+ /// when q ≥ −4 in the 64-bit case and when q ≥ −17 in
107
+ /// the 32-bitcase.
108
+ ///
109
+ /// When q ≥ 0,we have that 5^q ≤ 2m+1. In the 64-bit case,we
110
+ /// have 5^q ≤ 2m+1 ≤ 2^54 or q ≤ 23. In the 32-bit case,we have
111
+ /// 5^q ≤ 2m+1 ≤ 2^25 or q ≤ 10.
112
+ ///
113
+ /// When q < 0, we have w ≥ (2m+1)×5^−q. We must have that w < 2^64
114
+ /// so (2m+1)×5^−q < 2^64. We have that 2m+1 > 2^53 (64-bit case)
115
+ /// or 2m+1 > 2^24 (32-bit case). Hence,we must have 2^53×5^−q < 2^64
116
+ /// (64-bit) and 2^24×5^−q < 2^64 (32-bit). Hence we have 5^−q < 2^11
117
+ /// or q ≥ −4 (64-bit case) and 5^−q < 2^40 or q ≥ −17 (32-bitcase).
118
+ ///
119
+ /// Thus we have that we only need to round ties to even when
120
+ /// we have that q ∈ [−4,23](in the 64-bit case) or q∈[−17,10]
121
+ /// (in the 32-bit case). In both cases,the power of five(5^|q|)
122
+ /// fits in a 64-bit word.
53
123
const MIN_EXPONENT_ROUND_TO_EVEN : i32 ;
54
124
const MAX_EXPONENT_ROUND_TO_EVEN : i32 ;
55
125
56
- // Minimum exponent that for a fast path case, or `-⌊(MANTISSA_EXPLICIT_BITS+1)/log2(5)⌋`
57
- const MIN_EXPONENT_FAST_PATH : i64 ;
58
-
59
- // Maximum exponent that for a fast path case, or `⌊(MANTISSA_EXPLICIT_BITS+1)/log2(5)⌋`
60
- const MAX_EXPONENT_FAST_PATH : i64 ;
126
+ /* limits related to Fast pathing */
61
127
62
- // Maximum exponent that can be represented for a disguised-fast path case.
63
- // This is `MAX_EXPONENT_FAST_PATH + ⌊(MANTISSA_EXPLICIT_BITS+1)/log2(10)⌋`
64
- const MAX_EXPONENT_DISGUISED_FAST_PATH : i64 ;
128
+ /// Largest decimal exponent for a non-infinite value.
129
+ ///
130
+ /// This is the max exponent in binary converted to the max exponent in decimal. Allows fast
131
+ /// pathing anything larger than `10^LARGEST_POWER_OF_TEN`, which will round to infinity.
132
+ const LARGEST_POWER_OF_TEN : i32 =
133
+ ( ( Self :: EXPONENT_BIAS as f64 + 1.0 ) / f64:: consts:: LOG2_10 ) as i32 ;
65
134
66
- // Minimum exponent value `-(1 << (EXP_BITS - 1)) + 1`.
67
- const MINIMUM_EXPONENT : i32 ;
135
+ /// Smallest decimal exponent for a non-zero value. This allows for fast pathing anything
136
+ /// smaller than `10^SMALLEST_POWER_OF_TEN`, which will round to zero.
137
+ const SMALLEST_POWER_OF_TEN : i32 =
138
+ -( ( ( Self :: EXPONENT_BIAS + Self :: MANTISSA_BITS + 64 ) as f64 ) / f64:: consts:: LOG2_10 ) as i32 ;
68
139
69
- // Largest exponent value `(1 << EXP_BITS) - 1`.
70
- const INFINITE_POWER : i32 ;
140
+ /// Maximum exponent for a fast path case, or `⌊(MANTISSA_EXPLICIT_BITS+1)/log2(5)⌋`
141
+ // assuming FLT_EVAL_METHOD = 0
142
+ const MAX_EXPONENT_FAST_PATH : i64 =
143
+ ( ( Self :: MANTISSA_BITS as f64 ) / ( f64:: consts:: LOG2_10 - 1.0 ) ) as i64 ;
71
144
72
- // Index (in bits) of the sign.
73
- const SIGN_INDEX : usize ;
145
+ /// Minimum exponent for a fast path case, or `-⌊(MANTISSA_EXPLICIT_BITS+1)/log2(5)⌋`
146
+ const MIN_EXPONENT_FAST_PATH : i64 = - Self :: MAX_EXPONENT_FAST_PATH ;
74
147
75
- // Smallest decimal exponent for a non-zero value.
76
- const SMALLEST_POWER_OF_TEN : i32 ;
148
+ /// Maximum exponent that can be represented for a disguised-fast path case.
149
+ /// This is `MAX_EXPONENT_FAST_PATH + ⌊(MANTISSA_EXPLICIT_BITS+1)/log2(10)⌋`
150
+ const MAX_EXPONENT_DISGUISED_FAST_PATH : i64 =
151
+ Self :: MAX_EXPONENT_FAST_PATH + ( Self :: MANTISSA_BITS as f64 / f64:: consts:: LOG2_10 ) as i64 ;
77
152
78
- // Largest decimal exponent for a non-infinite value.
79
- const LARGEST_POWER_OF_TEN : i32 ;
80
-
81
- // Maximum mantissa for the fast-path (`1 << 53` for f64).
82
- const MAX_MANTISSA_FAST_PATH : u64 = 2_u64 << Self :: MANTISSA_EXPLICIT_BITS ;
153
+ /// Maximum mantissa for the fast-path (`1 << 53` for f64).
154
+ const MAX_MANTISSA_FAST_PATH : u64 = 1 << Self :: MANTISSA_BITS ;
83
155
84
156
/// Converts integer into float through an as cast.
85
157
/// This is only called in the fast-path algorithm, and therefore
@@ -96,27 +168,45 @@ pub trait RawFloat:
96
168
/// Returns the category that this number falls into.
97
169
fn classify ( self ) -> FpCategory ;
98
170
171
+ /// Transmute to the integer representation
172
+ fn to_bits ( self ) -> Self :: Int ;
173
+
99
174
/// Returns the mantissa, exponent and sign as integers.
100
- fn integer_decode ( self ) -> ( u64 , i16 , i8 ) ;
175
+ ///
176
+ /// That is, this returns `(m, p, s)` such that `s * m * 2^p` represents the original float.
177
+ /// For 0, the exponent will be `-(EXPONENT_BIAS + MANTISSA_EXPLICIT_BITS`, which is the
178
+ /// minimum subnormal power.
179
+ fn integer_decode ( self ) -> ( u64 , i16 , i8 ) {
180
+ let bits = self . to_bits ( ) ;
181
+ let sign: i8 = if bits >> ( Self :: BITS - 1 ) == Self :: Int :: ZERO { 1 } else { -1 } ;
182
+ let mut exponent: i16 =
183
+ ( ( bits & Self :: EXPONENT_MASK ) >> Self :: MANTISSA_EXPLICIT_BITS ) . cast ( ) ;
184
+ let mantissa = if exponent == 0 {
185
+ ( bits & Self :: MANTISSA_MASK ) << 1
186
+ } else {
187
+ ( bits & Self :: MANTISSA_MASK ) | ( Self :: Int :: ONE << Self :: MANTISSA_EXPLICIT_BITS )
188
+ } ;
189
+ // Exponent bias + mantissa shift
190
+ exponent -= ( Self :: EXPONENT_BIAS + Self :: MANTISSA_EXPLICIT_BITS ) as i16 ;
191
+ ( mantissa. into ( ) , exponent, sign)
192
+ }
101
193
}
102
194
103
195
impl RawFloat for f32 {
196
+ type Int = u32 ;
197
+
104
198
const INFINITY : Self = f32:: INFINITY ;
105
199
const NEG_INFINITY : Self = f32:: NEG_INFINITY ;
106
200
const NAN : Self = f32:: NAN ;
107
201
const NEG_NAN : Self = -f32:: NAN ;
108
202
109
- const MANTISSA_EXPLICIT_BITS : usize = 23 ;
203
+ const BITS : u32 = 32 ;
204
+ const MANTISSA_BITS : u32 = Self :: MANTISSA_DIGITS ;
205
+ const EXPONENT_MASK : Self :: Int = Self :: EXP_MASK ;
206
+ const MANTISSA_MASK : Self :: Int = Self :: MAN_MASK ;
207
+
110
208
const MIN_EXPONENT_ROUND_TO_EVEN : i32 = -17 ;
111
209
const MAX_EXPONENT_ROUND_TO_EVEN : i32 = 10 ;
112
- const MIN_EXPONENT_FAST_PATH : i64 = -10 ; // assuming FLT_EVAL_METHOD = 0
113
- const MAX_EXPONENT_FAST_PATH : i64 = 10 ;
114
- const MAX_EXPONENT_DISGUISED_FAST_PATH : i64 = 17 ;
115
- const MINIMUM_EXPONENT : i32 = -127 ;
116
- const INFINITE_POWER : i32 = 0xFF ;
117
- const SIGN_INDEX : usize = 31 ;
118
- const SMALLEST_POWER_OF_TEN : i32 = -65 ;
119
- const LARGEST_POWER_OF_TEN : i32 = 38 ;
120
210
121
211
#[ inline]
122
212
fn from_u64 ( v : u64 ) -> Self {
@@ -136,16 +226,8 @@ impl RawFloat for f32 {
136
226
TABLE [ exponent & 15 ]
137
227
}
138
228
139
- /// Returns the mantissa, exponent and sign as integers.
140
- fn integer_decode ( self ) -> ( u64 , i16 , i8 ) {
141
- let bits = self . to_bits ( ) ;
142
- let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 } ;
143
- let mut exponent: i16 = ( ( bits >> 23 ) & 0xff ) as i16 ;
144
- let mantissa =
145
- if exponent == 0 { ( bits & 0x7fffff ) << 1 } else { ( bits & 0x7fffff ) | 0x800000 } ;
146
- // Exponent bias + mantissa shift
147
- exponent -= 127 + 23 ;
148
- ( mantissa as u64 , exponent, sign)
229
+ fn to_bits ( self ) -> Self :: Int {
230
+ self . to_bits ( )
149
231
}
150
232
151
233
fn classify ( self ) -> FpCategory {
@@ -154,22 +236,20 @@ impl RawFloat for f32 {
154
236
}
155
237
156
238
impl RawFloat for f64 {
157
- const INFINITY : Self = f64:: INFINITY ;
158
- const NEG_INFINITY : Self = f64:: NEG_INFINITY ;
159
- const NAN : Self = f64:: NAN ;
160
- const NEG_NAN : Self = -f64:: NAN ;
239
+ type Int = u64 ;
240
+
241
+ const INFINITY : Self = Self :: INFINITY ;
242
+ const NEG_INFINITY : Self = Self :: NEG_INFINITY ;
243
+ const NAN : Self = Self :: NAN ;
244
+ const NEG_NAN : Self = -Self :: NAN ;
245
+
246
+ const BITS : u32 = 64 ;
247
+ const MANTISSA_BITS : u32 = Self :: MANTISSA_DIGITS ;
248
+ const EXPONENT_MASK : Self :: Int = Self :: EXP_MASK ;
249
+ const MANTISSA_MASK : Self :: Int = Self :: MAN_MASK ;
161
250
162
- const MANTISSA_EXPLICIT_BITS : usize = 52 ;
163
251
const MIN_EXPONENT_ROUND_TO_EVEN : i32 = -4 ;
164
252
const MAX_EXPONENT_ROUND_TO_EVEN : i32 = 23 ;
165
- const MIN_EXPONENT_FAST_PATH : i64 = -22 ; // assuming FLT_EVAL_METHOD = 0
166
- const MAX_EXPONENT_FAST_PATH : i64 = 22 ;
167
- const MAX_EXPONENT_DISGUISED_FAST_PATH : i64 = 37 ;
168
- const MINIMUM_EXPONENT : i32 = -1023 ;
169
- const INFINITE_POWER : i32 = 0x7FF ;
170
- const SIGN_INDEX : usize = 63 ;
171
- const SMALLEST_POWER_OF_TEN : i32 = -342 ;
172
- const LARGEST_POWER_OF_TEN : i32 = 308 ;
173
253
174
254
#[ inline]
175
255
fn from_u64 ( v : u64 ) -> Self {
@@ -190,19 +270,8 @@ impl RawFloat for f64 {
190
270
TABLE [ exponent & 31 ]
191
271
}
192
272
193
- /// Returns the mantissa, exponent and sign as integers.
194
- fn integer_decode ( self ) -> ( u64 , i16 , i8 ) {
195
- let bits = self . to_bits ( ) ;
196
- let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 } ;
197
- let mut exponent: i16 = ( ( bits >> 52 ) & 0x7ff ) as i16 ;
198
- let mantissa = if exponent == 0 {
199
- ( bits & 0xfffffffffffff ) << 1
200
- } else {
201
- ( bits & 0xfffffffffffff ) | 0x10000000000000
202
- } ;
203
- // Exponent bias + mantissa shift
204
- exponent -= 1023 + 52 ;
205
- ( mantissa, exponent, sign)
273
+ fn to_bits ( self ) -> Self :: Int {
274
+ self . to_bits ( )
206
275
}
207
276
208
277
fn classify ( self ) -> FpCategory {
0 commit comments