@@ -339,14 +339,78 @@ impl f128 {
339339 #[ unstable( feature = "float_exact_integer_constants" , issue = "152466" ) ]
340340 pub const MIN_EXACT_INTEGER : i128 = -Self :: MAX_EXACT_INTEGER ;
341341
342- /// Sign bit
343- pub ( crate ) const SIGN_MASK : u128 = 0x8000_0000_0000_0000_0000_0000_0000_0000 ;
342+ /// The mask of the bit used to encode the sign of an [`f128`].
343+ ///
344+ /// This bit is set when the sign is negative and unset when the sign is
345+ /// positive.
346+ /// If you only need to check whether a value is positive or negative,
347+ /// [`is_sign_positive`] or [`is_sign_negative`] can be used.
348+ ///
349+ /// [`is_sign_positive`]: f128::is_sign_positive
350+ /// [`is_sign_negative`]: f128::is_sign_negative
351+ /// ```rust
352+ /// #![feature(float_masks)]
353+ /// #![feature(f128)]
354+ /// # #[cfg(target_has_reliable_f128)] {
355+ /// let sign_mask = f128::SIGN_MASK;
356+ /// let a = 1.6552f128;
357+ /// let a_bits = a.to_bits();
358+ ///
359+ /// assert_eq!(a_bits & sign_mask, 0x0);
360+ /// assert_eq!(f128::from_bits(a_bits ^ sign_mask), -a);
361+ /// assert_eq!(sign_mask, (-0.0f128).to_bits());
362+ /// # }
363+ /// ```
364+ #[ unstable( feature = "float_masks" , issue = "154064" ) ]
365+ pub const SIGN_MASK : u128 = 0x8000_0000_0000_0000_0000_0000_0000_0000 ;
344366
345- /// Exponent mask
346- pub ( crate ) const EXP_MASK : u128 = 0x7fff_0000_0000_0000_0000_0000_0000_0000 ;
367+ /// The mask of the bits used to encode the exponent of an [`f128`].
368+ ///
369+ /// Note that the exponent is stored as a biased value, with a bias of 16383 for `f128`.
370+ ///
371+ /// ```rust
372+ /// #![feature(float_masks)]
373+ /// #![feature(f128)]
374+ /// # #[cfg(target_has_reliable_f128)] {
375+ /// fn get_exp(a: f128) -> i128 {
376+ /// let bias = 16383;
377+ /// let biased = a.to_bits() & f128::EXPONENT_MASK;
378+ /// (biased >> (f128::MANTISSA_DIGITS - 1)).cast_signed() - bias
379+ /// }
380+ ///
381+ /// assert_eq!(get_exp(0.5), -1);
382+ /// assert_eq!(get_exp(1.0), 0);
383+ /// assert_eq!(get_exp(2.0), 1);
384+ /// assert_eq!(get_exp(4.0), 2);
385+ /// # }
386+ /// ```
387+ #[ unstable( feature = "float_masks" , issue = "154064" ) ]
388+ pub const EXPONENT_MASK : u128 = 0x7fff_0000_0000_0000_0000_0000_0000_0000 ;
347389
348- /// Mantissa mask
349- pub ( crate ) const MAN_MASK : u128 = 0x0000_ffff_ffff_ffff_ffff_ffff_ffff_ffff ;
390+ /// The mask of the bits used to encode the mantissa of an [`f128`].
391+ ///
392+ /// ```rust
393+ /// #![feature(float_masks)]
394+ /// #![feature(f128)]
395+ /// # #[cfg(target_has_reliable_f128)] {
396+ /// let mantissa_mask = f128::MANTISSA_MASK;
397+ ///
398+ /// assert_eq!(0f128.to_bits() & mantissa_mask, 0x0);
399+ /// assert_eq!(1f128.to_bits() & mantissa_mask, 0x0);
400+ ///
401+ /// // multiplying a finite value by a power of 2 doesn't change its mantissa
402+ /// // unless the result or initial value is not normal.
403+ /// let a = 1.6552f128;
404+ /// let b = 4.0 * a;
405+ /// assert_eq!(a.to_bits() & mantissa_mask, b.to_bits() & mantissa_mask);
406+ ///
407+ /// // The maximum and minimum values have a saturated significand
408+ /// assert_eq!(f128::MAX.to_bits() & f128::MANTISSA_MASK, f128::MANTISSA_MASK);
409+ /// assert_eq!(f128::MIN.to_bits() & f128::MANTISSA_MASK, f128::MANTISSA_MASK);
410+ /// # }
411+ /// ```
412+ #[ unstable( feature = "float_masks" , issue = "154064" ) ]
413+ pub const MANTISSA_MASK : u128 = 0x0000_ffff_ffff_ffff_ffff_ffff_ffff_ffff ;
350414
351415 /// Minimum representable positive value (min subnormal)
352416 const TINY_BITS : u128 = 0x1 ;
@@ -511,9 +575,9 @@ impl f128 {
511575 #[ must_use]
512576 pub const fn classify ( self ) -> FpCategory {
513577 let bits = self . to_bits ( ) ;
514- match ( bits & Self :: MAN_MASK , bits & Self :: EXP_MASK ) {
515- ( 0 , Self :: EXP_MASK ) => FpCategory :: Infinite ,
516- ( _, Self :: EXP_MASK ) => FpCategory :: Nan ,
578+ match ( bits & Self :: MANTISSA_MASK , bits & Self :: EXPONENT_MASK ) {
579+ ( 0 , Self :: EXPONENT_MASK ) => FpCategory :: Infinite ,
580+ ( _, Self :: EXPONENT_MASK ) => FpCategory :: Nan ,
517581 ( 0 , 0 ) => FpCategory :: Zero ,
518582 ( _, 0 ) => FpCategory :: Subnormal ,
519583 _ => FpCategory :: Normal ,
0 commit comments