@@ -34,6 +34,7 @@ pub trait Float:
3434 const INFINITY : Self ;
3535 const NEG_INFINITY : Self ;
3636 const NAN : Self ;
37+ const NEG_NAN : Self ;
3738 const MAX : Self ;
3839 const MIN : Self ;
3940 const EPSILON : Self ;
@@ -187,6 +188,7 @@ macro_rules! float_impl {
187188 $bits: expr,
188189 $significand_bits: expr,
189190 $from_bits: path,
191+ $to_bits: path,
190192 $fma_fn: ident,
191193 $fma_intrinsic: ident
192194 ) => {
@@ -201,6 +203,9 @@ macro_rules! float_impl {
201203 const INFINITY : Self = Self :: INFINITY ;
202204 const NEG_INFINITY : Self = Self :: NEG_INFINITY ;
203205 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 ) ;
204209 const MAX : Self = -Self :: MIN ;
205210 // Sign bit set, saturated mantissa, saturated exponent with last bit zeroed
206211 const MIN : Self = $from_bits( Self :: Int :: MAX & !( 1 << Self :: SIG_BITS ) ) ;
@@ -275,11 +280,11 @@ macro_rules! float_impl {
275280}
276281
277282#[ 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) ;
281286#[ 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) ;
283288
284289/* FIXME(msrv): vendor some things that are not const stable at our MSRV */
285290
@@ -289,12 +294,24 @@ pub const fn f32_from_bits(bits: u32) -> f32 {
289294 unsafe { mem:: transmute :: < u32 , f32 > ( bits) }
290295}
291296
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+
292303/// `f64::from_bits`
293304pub const fn f64_from_bits ( bits : u64 ) -> f64 {
294305 // SAFETY: POD cast with no preconditions
295306 unsafe { mem:: transmute :: < u64 , f64 > ( bits) }
296307}
297308
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+
298315/// Trait for floats twice the bit width of another integer.
299316pub trait DFloat : Float {
300317 /// Float that is half the bit width of the floatthis trait is implemented for.
0 commit comments