@@ -70,10 +70,10 @@ pub(crate) trait Float:
70
70
const EXPONENT_MASK : Self :: Int ;
71
71
72
72
/// Returns `self` transmuted to `Self::Int`
73
- fn repr ( self ) -> Self :: Int ;
73
+ fn to_bits ( self ) -> Self :: Int ;
74
74
75
75
/// Returns `self` transmuted to `Self::SignedInt`
76
- fn signed_repr ( self ) -> Self :: SignedInt ;
76
+ fn to_bits_signed ( self ) -> Self :: SignedInt ;
77
77
78
78
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
79
79
/// represented in multiple different ways. This method returns `true` if two NaNs are
@@ -93,10 +93,15 @@ pub(crate) trait Float:
93
93
fn imp_frac( self ) -> Self :: Int ;
94
94
95
95
/// Returns a `Self::Int` transmuted back to `Self`
96
- fn from_repr ( a: Self :: Int ) -> Self ;
96
+ fn from_bits ( a: Self :: Int ) -> Self ;
97
97
98
98
/// Constructs a `Self` from its parts. Inputs are treated as bits and shifted into position.
99
- fn from_parts( sign: bool , exponent: Self :: Int , significand: Self :: Int ) -> Self ;
99
+ fn from_parts( negative: bool , exponent: Self :: Int , significand: Self :: Int ) -> Self ;
100
+
101
+ fn abs( self ) -> Self {
102
+ let abs_mask = !Self :: SIGN_MASK ;
103
+ Self :: from_bits( self . to_bits( ) & abs_mask)
104
+ }
100
105
101
106
/// Returns (normalized exponent, normalized significand)
102
107
fn normalize( significand: Self :: Int ) -> ( i32 , Self :: Int ) ;
@@ -124,10 +129,10 @@ macro_rules! float_impl {
124
129
const IMPLICIT_BIT : Self :: Int = 1 << Self :: SIGNIFICAND_BITS ;
125
130
const EXPONENT_MASK : Self :: Int = !( Self :: SIGN_MASK | Self :: SIGNIFICAND_MASK ) ;
126
131
127
- fn repr ( self ) -> Self :: Int {
132
+ fn to_bits ( self ) -> Self :: Int {
128
133
self . to_bits( )
129
134
}
130
- fn signed_repr ( self ) -> Self :: SignedInt {
135
+ fn to_bits_signed ( self ) -> Self :: SignedInt {
131
136
self . to_bits( ) as Self :: SignedInt
132
137
}
133
138
fn eq_repr( self , rhs: Self ) -> bool {
@@ -137,8 +142,8 @@ macro_rules! float_impl {
137
142
// necessary builtin (__unordtf2) to test whether `f128` is NaN.
138
143
// FIXME(f16_f128): Remove once the nightly toolchain has the __unordtf2 builtin
139
144
// x is NaN if all the bits of the exponent are set and the significand is non-0
140
- x. repr ( ) & $ty:: EXPONENT_MASK == $ty:: EXPONENT_MASK
141
- && x. repr ( ) & $ty:: SIGNIFICAND_MASK != 0
145
+ x. to_bits ( ) & $ty:: EXPONENT_MASK == $ty:: EXPONENT_MASK
146
+ && x. to_bits ( ) & $ty:: SIGNIFICAND_MASK != 0
142
147
}
143
148
#[ cfg( not( feature = "mangled-names" ) ) ]
144
149
fn is_nan( x: $ty) -> bool {
@@ -147,7 +152,7 @@ macro_rules! float_impl {
147
152
if is_nan( self ) && is_nan( rhs) {
148
153
true
149
154
} else {
150
- self . repr ( ) == rhs. repr ( )
155
+ self . to_bits ( ) == rhs. to_bits ( )
151
156
}
152
157
}
153
158
fn is_sign_negative( self ) -> bool {
@@ -162,12 +167,12 @@ macro_rules! float_impl {
162
167
fn imp_frac( self ) -> Self :: Int {
163
168
self . frac( ) | Self :: IMPLICIT_BIT
164
169
}
165
- fn from_repr ( a: Self :: Int ) -> Self {
170
+ fn from_bits ( a: Self :: Int ) -> Self {
166
171
Self :: from_bits( a)
167
172
}
168
- fn from_parts( sign : bool , exponent: Self :: Int , significand: Self :: Int ) -> Self {
169
- Self :: from_repr (
170
- ( ( sign as Self :: Int ) << ( Self :: BITS - 1 ) )
173
+ fn from_parts( negative : bool , exponent: Self :: Int , significand: Self :: Int ) -> Self {
174
+ Self :: from_bits (
175
+ ( ( negative as Self :: Int ) << ( Self :: BITS - 1 ) )
171
176
| ( ( exponent << Self :: SIGNIFICAND_BITS ) & Self :: EXPONENT_MASK )
172
177
| ( significand & Self :: SIGNIFICAND_MASK ) ,
173
178
)
@@ -182,7 +187,7 @@ macro_rules! float_impl {
182
187
)
183
188
}
184
189
fn is_subnormal( self ) -> bool {
185
- ( self . repr ( ) & Self :: EXPONENT_MASK ) == Self :: Int :: ZERO
190
+ ( self . to_bits ( ) & Self :: EXPONENT_MASK ) == Self :: Int :: ZERO
186
191
}
187
192
}
188
193
} ;
0 commit comments