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: &Fp) -> Fp {
25        const MASK: u64 = 0xffffffff;
26        let a = self.f >> 32;
27        let b = self.f & MASK;
28        let c = other.f >> 32;
29        let d = other.f & MASK;
30        let ac = a * c;
31        let bc = b * c;
32        let ad = a * d;
33        let bd = b * d;
34        let tmp = (bd >> 32) + (ad & MASK) + (bc & MASK) + (1 << 31) /* round */;
35        let f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
36        let e = self.e + other.e + 64;
37        Fp { f, e }
38    }
39
40    /// Normalizes itself so that the resulting mantissa is at least `2^63`.
41    pub fn normalize(&self) -> Fp {
42        let mut f = self.f;
43        let mut e = self.e;
44        if f >> (64 - 32) == 0 {
45            f <<= 32;
46            e -= 32;
47        }
48        if f >> (64 - 16) == 0 {
49            f <<= 16;
50            e -= 16;
51        }
52        if f >> (64 - 8) == 0 {
53            f <<= 8;
54            e -= 8;
55        }
56        if f >> (64 - 4) == 0 {
57            f <<= 4;
58            e -= 4;
59        }
60        if f >> (64 - 2) == 0 {
61            f <<= 2;
62            e -= 2;
63        }
64        if f >> (64 - 1) == 0 {
65            f <<= 1;
66            e -= 1;
67        }
68        debug_assert!(f >= (1 << 63));
69        Fp { f, e }
70    }
71
72    /// Normalizes itself to have the shared exponent.
73    /// It can only decrease the exponent (and thus increase the mantissa).
74    pub fn normalize_to(&self, e: i16) -> Fp {
75        let edelta = self.e - e;
76        assert!(edelta >= 0);
77        let edelta = edelta as usize;
78        assert_eq!(self.f << edelta >> edelta, self.f);
79        Fp { f: self.f << edelta, e }
80    }
81}