]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/diy_float.rs
Auto merge of #43710 - zackmdavis:field_init_shorthand_power_slam, r=Mark-Simulacrum
[rust.git] / src / libcore / num / diy_float.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Extended precision "soft float", for internal use only.
12
13 // This module is only for dec2flt and flt2dec, and only public because of coretests.
14 // It is not intended to ever be stabilized.
15 #![doc(hidden)]
16 #![unstable(feature = "core_private_diy_float",
17             reason = "internal routines only exposed for testing",
18             issue = "0")]
19
20 /// A custom 64-bit floating point type, representing `f * 2^e`.
21 #[derive(Copy, Clone, Debug)]
22 #[doc(hidden)]
23 pub struct Fp {
24     /// The integer mantissa.
25     pub f: u64,
26     /// The exponent in base 2.
27     pub e: i16,
28 }
29
30 impl Fp {
31     /// Returns a correctly rounded product of itself and `other`.
32     pub fn mul(&self, other: &Fp) -> Fp {
33         const MASK: u64 = 0xffffffff;
34         let a = self.f >> 32;
35         let b = self.f & MASK;
36         let c = other.f >> 32;
37         let d = other.f & MASK;
38         let ac = a * c;
39         let bc = b * c;
40         let ad = a * d;
41         let bd = b * d;
42         let tmp = (bd >> 32) + (ad & MASK) + (bc & MASK) + (1 << 31) /* round */;
43         let f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
44         let e = self.e + other.e + 64;
45         Fp { f: f, e: e }
46     }
47
48     /// Normalizes itself so that the resulting mantissa is at least `2^63`.
49     pub fn normalize(&self) -> Fp {
50         let mut f = self.f;
51         let mut e = self.e;
52         if f >> (64 - 32) == 0 {
53             f <<= 32;
54             e -= 32;
55         }
56         if f >> (64 - 16) == 0 {
57             f <<= 16;
58             e -= 16;
59         }
60         if f >> (64 - 8) == 0 {
61             f <<= 8;
62             e -= 8;
63         }
64         if f >> (64 - 4) == 0 {
65             f <<= 4;
66             e -= 4;
67         }
68         if f >> (64 - 2) == 0 {
69             f <<= 2;
70             e -= 2;
71         }
72         if f >> (64 - 1) == 0 {
73             f <<= 1;
74             e -= 1;
75         }
76         debug_assert!(f >= (1 >> 63));
77         Fp { f: f, e: e }
78     }
79
80     /// Normalizes itself to have the shared exponent.
81     /// It can only decrease the exponent (and thus increase the mantissa).
82     pub fn normalize_to(&self, e: i16) -> Fp {
83         let edelta = self.e - e;
84         assert!(edelta >= 0);
85         let edelta = edelta as usize;
86         assert_eq!(self.f << edelta >> edelta, self.f);
87         Fp {
88             f: self.f << edelta,
89             e,
90         }
91     }
92 }