Skip to content

Commit cfe2bfd

Browse files
Rollup merge of rust-lang#134063 - tgross35:dec2flt-refactoring, r=Noratrieb
dec2flt: Clean up float parsing modules This is the first portion of my work adding support for parsing and printing `f16`. Changes in `float.rs` replace the magic constants with expressions and add some use of generics to better support the new float types. Everything else is related to documentation or naming; there are no functional changes in this PR. This can be reviewed by commit.
2 parents ac951d3 + 37e223c commit cfe2bfd

File tree

18 files changed

+856
-605
lines changed

18 files changed

+856
-605
lines changed

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ pub(crate) trait ByteSlice {
88
/// Writes a 64-bit integer as 8 bytes in little-endian order.
99
fn write_u64(&mut self, value: u64);
1010

11-
/// Calculate the offset of a slice from another.
11+
/// Calculate the difference in length between two slices.
1212
fn offset_from(&self, other: &Self) -> isize;
1313

1414
/// Iteratively parse and consume digits from bytes.
15-
/// Returns the same bytes with consumed digits being
16-
/// elided.
15+
///
16+
/// Returns the same bytes with consumed digits being elided. Breaks on invalid digits.
1717
fn parse_digits(&self, func: impl FnMut(u8)) -> &Self;
1818
}
1919

@@ -39,11 +39,11 @@ impl ByteSlice for [u8] {
3939
fn parse_digits(&self, mut func: impl FnMut(u8)) -> &Self {
4040
let mut s = self;
4141

42-
while let Some((c, s_next)) = s.split_first() {
42+
while let Some((c, rest)) = s.split_first() {
4343
let c = c.wrapping_sub(b'0');
4444
if c < 10 {
4545
func(c);
46-
s = s_next;
46+
s = rest;
4747
} else {
4848
break;
4949
}
@@ -53,27 +53,30 @@ impl ByteSlice for [u8] {
5353
}
5454
}
5555

56-
/// Determine if 8 bytes are all decimal digits.
56+
/// Determine if all characters in an 8-byte byte string (represented as a `u64`) are all decimal
57+
/// digits.
58+
///
5759
/// This does not care about the order in which the bytes were loaded.
5860
pub(crate) fn is_8digits(v: u64) -> bool {
5961
let a = v.wrapping_add(0x4646_4646_4646_4646);
6062
let b = v.wrapping_sub(0x3030_3030_3030_3030);
6163
(a | b) & 0x8080_8080_8080_8080 == 0
6264
}
6365

64-
/// A custom 64-bit floating point type, representing `f * 2^e`.
65-
/// 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.
6668
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
6769
pub struct BiasedFp {
6870
/// The significant digits.
69-
pub f: u64,
71+
pub m: u64,
7072
/// The biased, binary exponent.
71-
pub e: i32,
73+
pub p_biased: i32,
7274
}
7375

7476
impl BiasedFp {
77+
/// Represent `0 ^ p`
7578
#[inline]
76-
pub const fn zero_pow2(e: i32) -> Self {
77-
Self { f: 0, e }
79+
pub const fn zero_pow2(p_biased: i32) -> Self {
80+
Self { m: 0, p_biased }
7881
}
7982
}

0 commit comments

Comments
 (0)