1//! Extended precision "soft float", for internal use only.
23// 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)]
1112/// 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.
17pub f: u64,
18/// The exponent in base 2.
19pub e: i16,
20}
2122impl Fp {
23/// Returns a correctly rounded product of itself and `other`.
24pub fn mul(self, other: Self) -> Self {
25let (lo, hi) = self.f.widening_mul(other.f);
26let f = hi + (lo >> 63) /* round */;
27let e = self.e + other.e + 64;
28Self { f, e }
29 }
3031/// Normalizes itself so that the resulting mantissa is at least `2^63`.
32pub fn normalize(self) -> Self {
33let lz = self.f.leading_zeros();
34let f = self.f << lz;
35let e = self.e - lz as i16;
36debug_assert!(f >= (1 << 63));
37Self { f, e }
38 }
3940/// Normalizes itself to have the shared exponent.
41 /// It can only decrease the exponent (and thus increase the mantissa).
42pub fn normalize_to(self, e: i16) -> Self {
43let edelta = self.e - e;
44assert!(edelta >= 0);
45let edelta = edelta as usize;
46assert_eq!(self.f << edelta >> edelta, self.f);
47Self { f: self.f << edelta, e }
48 }
49}