Skip to main content

core/num/imp/
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(#[automatically_derived]
impl crate::marker::Copy for Fp { }Copy, #[automatically_derived]
impl crate::clone::Clone for Fp {
    #[inline]
    fn clone(&self) -> Fp {
        let _: crate::clone::AssertParamIsClone<u64>;
        let _: crate::clone::AssertParamIsClone<i16>;
        *self
    }
}Clone, #[automatically_derived]
impl crate::fmt::Debug for Fp {
    #[inline]
    fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
        crate::fmt::Formatter::debug_struct_field2_finish(f, "Fp", "f",
            &self.f, "e", &&self.e)
    }
}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        if true {
    if !(f >= (1 << 63)) {
        crate::panicking::panic("assertion failed: f >= (1 << 63)")
    };
};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        if !(edelta >= 0) {
    crate::panicking::panic("assertion failed: edelta >= 0")
};assert!(edelta >= 0);
45        let edelta = edelta as usize;
46        match (&(self.f << edelta >> edelta), &self.f) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = crate::panicking::AssertKind::Eq;
            crate::panicking::assert_failed(kind, &*left_val, &*right_val,
                crate::option::Option::None);
        }
    }
};assert_eq!(self.f << edelta >> edelta, self.f);
47        Self { f: self.f << edelta, e }
48    }
49}