@@ -27,34 +27,37 @@ pub trait Float:
27
27
28
28
const ZERO : Self ;
29
29
const ONE : Self ;
30
+ const INFINITY : Self ;
31
+ const NEG_INFINITY : Self ;
32
+ const NAN : Self ;
30
33
31
34
/// The bitwidth of the float type
32
35
const BITS : u32 ;
33
36
34
37
/// The bitwidth of the significand
35
- const SIGNIFICAND_BITS : u32 ;
38
+ const SIG_BITS : u32 ;
36
39
37
40
/// The bitwidth of the exponent
38
- const EXPONENT_BITS : u32 = Self :: BITS - Self :: SIGNIFICAND_BITS - 1 ;
41
+ const EXP_BITS : u32 = Self :: BITS - Self :: SIG_BITS - 1 ;
39
42
40
43
/// The saturated value of the exponent (infinite representation), in the rightmost postiion.
41
- const EXPONENT_MAX : u32 = ( 1 << Self :: EXPONENT_BITS ) - 1 ;
44
+ const EXP_MAX : u32 = ( 1 << Self :: EXP_BITS ) - 1 ;
42
45
43
46
/// The exponent bias value
44
- const EXPONENT_BIAS : u32 = Self :: EXPONENT_MAX >> 1 ;
47
+ const EXP_BIAS : u32 = Self :: EXP_MAX >> 1 ;
45
48
46
49
/// A mask for the sign bit
47
50
const SIGN_MASK : Self :: Int ;
48
51
49
52
/// A mask for the significand
50
- const SIGNIFICAND_MASK : Self :: Int ;
53
+ const SIG_MASK : Self :: Int ;
54
+
55
+ /// A mask for the exponent
56
+ const EXP_MASK : Self :: Int ;
51
57
52
58
/// The implicit bit of the float format
53
59
const IMPLICIT_BIT : Self :: Int ;
54
60
55
- /// A mask for the exponent
56
- const EXPONENT_MASK : Self :: Int ;
57
-
58
61
/// Returns `self` transmuted to `Self::Int`
59
62
fn to_bits ( self ) -> Self :: Int ;
60
63
@@ -105,14 +108,17 @@ macro_rules! float_impl {
105
108
106
109
const ZERO : Self = 0.0 ;
107
110
const ONE : Self = 1.0 ;
111
+ const INFINITY : Self = Self :: INFINITY ;
112
+ const NEG_INFINITY : Self = Self :: NEG_INFINITY ;
113
+ const NAN : Self = Self :: NAN ;
108
114
109
115
const BITS : u32 = $bits;
110
- const SIGNIFICAND_BITS : u32 = $significand_bits;
116
+ const SIG_BITS : u32 = $significand_bits;
111
117
112
118
const SIGN_MASK : Self :: Int = 1 << ( Self :: BITS - 1 ) ;
113
- const SIGNIFICAND_MASK : Self :: Int = ( 1 << Self :: SIGNIFICAND_BITS ) - 1 ;
114
- const IMPLICIT_BIT : Self :: Int = 1 << Self :: SIGNIFICAND_BITS ;
115
- const EXPONENT_MASK : Self :: Int = ! ( Self :: SIGN_MASK | Self :: SIGNIFICAND_MASK ) ;
119
+ const SIG_MASK : Self :: Int = ( 1 << Self :: SIG_BITS ) - 1 ;
120
+ const EXP_MASK : Self :: Int = ! ( Self :: SIGN_MASK | Self :: SIG_MASK ) ;
121
+ const IMPLICIT_BIT : Self :: Int = 1 << Self :: SIG_BITS ;
116
122
117
123
fn to_bits( self ) -> Self :: Int {
118
124
self . to_bits( )
@@ -126,19 +132,18 @@ macro_rules! float_impl {
126
132
// necessary builtin (__unordtf2) to test whether `f128` is NaN.
127
133
// FIXME(f16_f128): Remove once the nightly toolchain has the __unordtf2 builtin
128
134
// x is NaN if all the bits of the exponent are set and the significand is non-0
129
- x. to_bits( ) & $ty:: EXPONENT_MASK == $ty:: EXPONENT_MASK
130
- && x. to_bits( ) & $ty:: SIGNIFICAND_MASK != 0
135
+ x. to_bits( ) & $ty:: EXP_MASK == $ty:: EXP_MASK && x. to_bits( ) & $ty:: SIG_MASK != 0
131
136
}
132
137
if is_nan( self ) && is_nan( rhs) { true } else { self . to_bits( ) == rhs. to_bits( ) }
133
138
}
134
139
fn is_sign_negative( self ) -> bool {
135
140
self . is_sign_negative( )
136
141
}
137
142
fn exp( self ) -> Self :: ExpInt {
138
- ( ( self . to_bits( ) & Self :: EXPONENT_MASK ) >> Self :: SIGNIFICAND_BITS ) as Self :: ExpInt
143
+ ( ( self . to_bits( ) & Self :: EXP_MASK ) >> Self :: SIG_BITS ) as Self :: ExpInt
139
144
}
140
145
fn frac( self ) -> Self :: Int {
141
- self . to_bits( ) & Self :: SIGNIFICAND_MASK
146
+ self . to_bits( ) & Self :: SIG_MASK
142
147
}
143
148
fn imp_frac( self ) -> Self :: Int {
144
149
self . frac( ) | Self :: IMPLICIT_BIT
@@ -149,16 +154,16 @@ macro_rules! float_impl {
149
154
fn from_parts( negative: bool , exponent: Self :: Int , significand: Self :: Int ) -> Self {
150
155
Self :: from_bits(
151
156
( ( negative as Self :: Int ) << ( Self :: BITS - 1 ) )
152
- | ( ( exponent << Self :: SIGNIFICAND_BITS ) & Self :: EXPONENT_MASK )
153
- | ( significand & Self :: SIGNIFICAND_MASK ) ,
157
+ | ( ( exponent << Self :: SIG_BITS ) & Self :: EXP_MASK )
158
+ | ( significand & Self :: SIG_MASK ) ,
154
159
)
155
160
}
156
161
fn normalize( significand: Self :: Int ) -> ( i32 , Self :: Int ) {
157
- let shift = significand. leading_zeros( ) . wrapping_sub( Self :: EXPONENT_BITS ) ;
162
+ let shift = significand. leading_zeros( ) . wrapping_sub( Self :: EXP_BITS ) ;
158
163
( 1i32 . wrapping_sub( shift as i32 ) , significand << shift as Self :: Int )
159
164
}
160
165
fn is_subnormal( self ) -> bool {
161
- ( self . to_bits( ) & Self :: EXPONENT_MASK ) == Self :: Int :: ZERO
166
+ ( self . to_bits( ) & Self :: EXP_MASK ) == Self :: Int :: ZERO
162
167
}
163
168
}
164
169
} ;
0 commit comments