Skip to content

Commit d50c184

Browse files
authored
Unrolled build for rust-lang#139186
Rollup merge of rust-lang#139186 - TDecking:float, r=workingjubilee Refactor `diy_float` The refactor replaces bespoke algorithms with functions already inside the standard library, improving both codegen and readability.
2 parents 3350c1e + d81559a commit d50c184

File tree

2 files changed

+15
-47
lines changed

2 files changed

+15
-47
lines changed

library/core/src/num/diy_float.rs

+11-43
Original file line numberDiff line numberDiff line change
@@ -21,61 +21,29 @@ pub struct Fp {
2121

2222
impl Fp {
2323
/// 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);
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 */;
3627
let e = self.e + other.e + 64;
37-
Fp { f, e }
28+
Self { f, e }
3829
}
3930

4031
/// 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-
}
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;
6836
debug_assert!(f >= (1 << 63));
69-
Fp { f, e }
37+
Self { f, e }
7038
}
7139

7240
/// Normalizes itself to have the shared exponent.
7341
/// It can only decrease the exponent (and thus increase the mantissa).
74-
pub fn normalize_to(&self, e: i16) -> Fp {
42+
pub fn normalize_to(self, e: i16) -> Self {
7543
let edelta = self.e - e;
7644
assert!(edelta >= 0);
7745
let edelta = edelta as usize;
7846
assert_eq!(self.f << edelta >> edelta, self.f);
79-
Fp { f: self.f << edelta, e }
47+
Self { f: self.f << edelta, e }
8048
}
8149
}

library/core/src/num/flt2dec/strategy/grisu.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ pub fn format_shortest_opt<'a>(
196196
let (minusk, cached) = cached_power(ALPHA - plus.e - 64, GAMMA - plus.e - 64);
197197

198198
// scale fps. this gives the maximal error of 1 ulp (proved from Theorem 5.1).
199-
let plus = plus.mul(&cached);
200-
let minus = minus.mul(&cached);
201-
let v = v.mul(&cached);
199+
let plus = plus.mul(cached);
200+
let minus = minus.mul(cached);
201+
let v = v.mul(cached);
202202
debug_assert_eq!(plus.e, minus.e);
203203
debug_assert_eq!(plus.e, v.e);
204204

@@ -480,7 +480,7 @@ pub fn format_exact_opt<'a>(
480480
// normalize and scale `v`.
481481
let v = Fp { f: d.mant, e: d.exp }.normalize();
482482
let (minusk, cached) = cached_power(ALPHA - v.e - 64, GAMMA - v.e - 64);
483-
let v = v.mul(&cached);
483+
let v = v.mul(cached);
484484

485485
// divide `v` into integral and fractional parts.
486486
let e = -v.e as usize;

0 commit comments

Comments
 (0)