@@ -34,6 +34,7 @@ pub trait Float:
34
34
const INFINITY : Self ;
35
35
const NEG_INFINITY : Self ;
36
36
const NAN : Self ;
37
+ const NEG_NAN : Self ;
37
38
const MAX : Self ;
38
39
const MIN : Self ;
39
40
const EPSILON : Self ;
@@ -187,6 +188,7 @@ macro_rules! float_impl {
187
188
$bits: expr,
188
189
$significand_bits: expr,
189
190
$from_bits: path,
191
+ $to_bits: path,
190
192
$fma_fn: ident,
191
193
$fma_intrinsic: ident
192
194
) => {
@@ -201,6 +203,9 @@ macro_rules! float_impl {
201
203
const INFINITY : Self = Self :: INFINITY ;
202
204
const NEG_INFINITY : Self = Self :: NEG_INFINITY ;
203
205
const NAN : Self = Self :: NAN ;
206
+ // NAN isn't guaranteed to be positive but it usually is. We only use this for
207
+ // tests.
208
+ const NEG_NAN : Self = $from_bits( $to_bits( Self :: NAN ) | Self :: SIGN_MASK ) ;
204
209
const MAX : Self = -Self :: MIN ;
205
210
// Sign bit set, saturated mantissa, saturated exponent with last bit zeroed
206
211
const MIN : Self = $from_bits( Self :: Int :: MAX & !( 1 << Self :: SIG_BITS ) ) ;
@@ -275,11 +280,11 @@ macro_rules! float_impl {
275
280
}
276
281
277
282
#[ cfg( f16_enabled) ]
278
- float_impl ! ( f16, u16 , i16 , 16 , 10 , f16:: from_bits, fmaf16, fmaf16) ;
279
- float_impl ! ( f32 , u32 , i32 , 32 , 23 , f32_from_bits, fmaf, fmaf32) ;
280
- float_impl ! ( f64 , u64 , i64 , 64 , 52 , f64_from_bits, fma, fmaf64) ;
283
+ float_impl ! ( f16, u16 , i16 , 16 , 10 , f16:: from_bits, f16 :: to_bits , fmaf16, fmaf16) ;
284
+ float_impl ! ( f32 , u32 , i32 , 32 , 23 , f32_from_bits, f32_to_bits , fmaf, fmaf32) ;
285
+ float_impl ! ( f64 , u64 , i64 , 64 , 52 , f64_from_bits, f64_to_bits , fma, fmaf64) ;
281
286
#[ cfg( f128_enabled) ]
282
- float_impl ! ( f128, u128 , i128 , 128 , 112 , f128:: from_bits, fmaf128, fmaf128) ;
287
+ float_impl ! ( f128, u128 , i128 , 128 , 112 , f128:: from_bits, f128 :: to_bits , fmaf128, fmaf128) ;
283
288
284
289
/* FIXME(msrv): vendor some things that are not const stable at our MSRV */
285
290
@@ -289,12 +294,24 @@ pub const fn f32_from_bits(bits: u32) -> f32 {
289
294
unsafe { mem:: transmute :: < u32 , f32 > ( bits) }
290
295
}
291
296
297
+ /// `f32::to_bits`
298
+ pub const fn f32_to_bits ( x : f32 ) -> u32 {
299
+ // SAFETY: POD cast with no preconditions
300
+ unsafe { mem:: transmute :: < f32 , u32 > ( x) }
301
+ }
302
+
292
303
/// `f64::from_bits`
293
304
pub const fn f64_from_bits ( bits : u64 ) -> f64 {
294
305
// SAFETY: POD cast with no preconditions
295
306
unsafe { mem:: transmute :: < u64 , f64 > ( bits) }
296
307
}
297
308
309
+ /// `f64::to_bits`
310
+ pub const fn f64_to_bits ( x : f64 ) -> u64 {
311
+ // SAFETY: POD cast with no preconditions
312
+ unsafe { mem:: transmute :: < f64 , u64 > ( x) }
313
+ }
314
+
298
315
/// Trait for floats twice the bit width of another integer.
299
316
pub trait DFloat : Float {
300
317
/// Float that is half the bit width of the floatthis trait is implemented for.
0 commit comments