Skip to content

Commit f0f8c89

Browse files
committed
dec2flt: Rename fields to be consistent with documented notation
1 parent 2bbf550 commit f0f8c89

File tree

6 files changed

+24
-20
lines changed

6 files changed

+24
-20
lines changed

library/core/src/num/dec2flt/common.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,20 @@ pub(crate) fn is_8digits(v: u64) -> bool {
6363
(a | b) & 0x8080_8080_8080_8080 == 0
6464
}
6565

66-
/// A custom 64-bit floating point type, representing `f * 2^e`.
67-
/// e is biased, so it be directly shifted into the exponent bits.
66+
/// A custom 64-bit floating point type, representing `m * 2^p`.
67+
/// p is biased, so it be directly shifted into the exponent bits.
6868
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
6969
pub struct BiasedFp {
7070
/// The significant digits.
71-
pub f: u64,
71+
pub m: u64,
7272
/// The biased, binary exponent.
73-
pub e: i32,
73+
pub p_biased: i32,
7474
}
7575

7676
impl BiasedFp {
77+
/// Represent `0 ^ p`
7778
#[inline]
78-
pub const fn zero_pow2(e: i32) -> Self {
79-
Self { f: 0, e }
79+
pub const fn zero_pow2(p_biased: i32) -> Self {
80+
Self { m: 0, p_biased }
8081
}
8182
}

library/core/src/num/dec2flt/lemire.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn compute_float<F: RawFloat>(q: i64, mut w: u64) -> BiasedFp {
7373
mantissa += mantissa & 1;
7474
mantissa >>= 1;
7575
power2 = (mantissa >= (1_u64 << F::MANTISSA_EXPLICIT_BITS)) as i32;
76-
return BiasedFp { f: mantissa, e: power2 };
76+
return BiasedFp { m: mantissa, p_biased: power2 };
7777
}
7878
// Need to handle rounding ties. Normally, we need to round up,
7979
// but if we fall right in between and we have an even basis, we
@@ -111,7 +111,7 @@ pub fn compute_float<F: RawFloat>(q: i64, mut w: u64) -> BiasedFp {
111111
// Exponent is above largest normal value, must be infinite.
112112
return fp_inf;
113113
}
114-
BiasedFp { f: mantissa, e: power2 }
114+
BiasedFp { m: mantissa, p_biased: power2 }
115115
}
116116

117117
/// Calculate a base 2 exponent from a decimal exponent.

library/core/src/num/dec2flt/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ pub fn pfe_invalid() -> ParseFloatError {
233233

234234
/// Converts a `BiasedFp` to the closest machine float type.
235235
fn biased_fp_to_float<T: RawFloat>(x: BiasedFp) -> T {
236-
let mut word = x.f;
237-
word |= (x.e as u64) << T::MANTISSA_EXPLICIT_BITS;
236+
let mut word = x.m;
237+
word |= (x.p_biased as u64) << T::MANTISSA_EXPLICIT_BITS;
238238
T::from_u64_bits(word)
239239
}
240240

@@ -272,12 +272,15 @@ pub fn dec2flt<F: RawFloat>(s: &str) -> Result<F, ParseFloatError> {
272272
// redundantly using the Eisel-Lemire algorithm if it was unable to
273273
// correctly round on the first pass.
274274
let mut fp = compute_float::<F>(num.exponent, num.mantissa);
275-
if num.many_digits && fp.e >= 0 && fp != compute_float::<F>(num.exponent, num.mantissa + 1) {
276-
fp.e = -1;
275+
if num.many_digits
276+
&& fp.p_biased >= 0
277+
&& fp != compute_float::<F>(num.exponent, num.mantissa + 1)
278+
{
279+
fp.p_biased = -1;
277280
}
278281
// Unable to correctly round the float using the Eisel-Lemire algorithm.
279282
// Fallback to a slower, but always correct algorithm.
280-
if fp.e < 0 {
283+
if fp.p_biased < 0 {
281284
fp = parse_long_mantissa::<F>(s);
282285
}
283286

library/core/src/num/dec2flt/slow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ pub(crate) fn parse_long_mantissa<F: RawFloat>(s: &[u8]) -> BiasedFp {
105105
}
106106
// Zero out all the bits above the explicit mantissa bits.
107107
mantissa &= (1_u64 << F::MANTISSA_EXPLICIT_BITS) - 1;
108-
BiasedFp { f: mantissa, e: power2 }
108+
BiasedFp { m: mantissa, p_biased: power2 }
109109
}

library/core/tests/num/dec2flt/float.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn test_f32_integer_decode() {
1212

1313
// Ignore the "sign" (quiet / signalling flag) of NAN.
1414
// It can vary between runtime operations and LLVM folding.
15-
let (nan_m, nan_e, _nan_s) = f32::NAN.integer_decode();
16-
assert_eq!((nan_m, nan_e), (12582912, 105));
15+
let (nan_m, nan_p, _nan_s) = f32::NAN.integer_decode();
16+
assert_eq!((nan_m, nan_p), (12582912, 105));
1717
}
1818

1919
#[test]
@@ -28,6 +28,6 @@ fn test_f64_integer_decode() {
2828

2929
// Ignore the "sign" (quiet / signalling flag) of NAN.
3030
// It can vary between runtime operations and LLVM folding.
31-
let (nan_m, nan_e, _nan_s) = f64::NAN.integer_decode();
32-
assert_eq!((nan_m, nan_e), (6755399441055744, 972));
31+
let (nan_m, nan_p, _nan_s) = f64::NAN.integer_decode();
32+
assert_eq!((nan_m, nan_p), (6755399441055744, 972));
3333
}

library/core/tests/num/dec2flt/lemire.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use core::num::dec2flt::lemire::compute_float;
22

33
fn compute_float32(q: i64, w: u64) -> (i32, u64) {
44
let fp = compute_float::<f32>(q, w);
5-
(fp.e, fp.f)
5+
(fp.p_biased, fp.m)
66
}
77

88
fn compute_float64(q: i64, w: u64) -> (i32, u64) {
99
let fp = compute_float::<f64>(q, w);
10-
(fp.e, fp.f)
10+
(fp.p_biased, fp.m)
1111
}
1212

1313
#[test]

0 commit comments

Comments
 (0)