diy_float.rs - source
Description: Source of the Rust file `library/core/src/num/diy_float.rs`.

#
core/num/
diy_float.rs

    1//! Extended precision "soft float", for internal use only.
    2
    3// This module is only for dec2flt and flt2dec, and only public because of coretests.
    4// It is not intended to ever be stabilized.
    5#![doc(hidden)]
    6#![unstable(
    7    feature = "core_private_diy_float",
    8    reason = "internal routines only exposed for testing",
    9    issue = "none"
    10)]
    11
    12/// A custom 64-bit floating point type, representing `f * 2^e`.
    13#[derive(Copy, Clone, Debug)]
    14#[doc(hidden)]
    15pub struct Fp {
    16    /// The integer mantissa.
    17    pub f: u64,
    18    /// The exponent in base 2.
    19    pub e: i16,
    20}
    21
    22impl Fp {
    23    /// Returns a correctly rounded product of itself and `other`.
    24    pub fn mul(self, other: Self) -> Self {
    25        let (lo, hi) = self.f.widening_mul(other.f);
    26        let f = hi + (lo >> 63) /* round */;
    27        let e = self.e + other.e + 64;
    28        Self { f, e }
    29    }
    30
    31    /// Normalizes itself so that the resulting mantissa is at least `2^63`.
    32    pub fn normalize(self) -> Self {
    33        let lz = self.f.leading_zeros();
    34        let f = self.f << lz;
    35        let e = self.e - lz as i16;
    36        debug_assert!(f >= (1 << 63));
    37        Self { f, e }
    38    }
    39
    40    /// Normalizes itself to have the shared exponent.
    41    /// It can only decrease the exponent (and thus increase the mantissa).
    42    pub fn normalize_to(self, e: i16) -> Self {
    43        let edelta = self.e - e;
    44        assert!(edelta >= 0);
    45        let edelta = edelta as usize;
    46        assert_eq!(self.f << edelta >> edelta, self.f);
    47        Self { f: self.f << edelta, e }
    48    }
    49}