1//! Common utilities, for internal use only.
23/// Helper methods to process immutable bytes.
4pub(crate) trait ByteSlice {
5/// Reads 8 bytes as a 64-bit integer in little-endian order.
6fn read_u64(&self) -> u64;
78/// Writes a 64-bit integer as 8 bytes in little-endian order.
9fn write_u64(&mut self, value: u64);
1011/// Calculate the difference in length between two slices.
12fn offset_from(&self, other: &Self) -> isize;
1314/// Iteratively parse and consume digits from bytes.
15 ///
16 /// Returns the same bytes with consumed digits being elided. Breaks on invalid digits.
17fn parse_digits(&self, func: impl FnMut(u8)) -> &Self;
18}
1920impl ByteSlice for [u8] {
21#[inline(always)] // inlining this is crucial to remove bound checks
22fn read_u64(&self) -> u64 {
23let mut tmp = [0; 8];
24 tmp.copy_from_slice(&self[..8]);
25 u64::from_le_bytes(tmp)
26 }
2728#[inline(always)] // inlining this is crucial to remove bound checks
29fn write_u64(&mut self, value: u64) {
30self[..8].copy_from_slice(&value.to_le_bytes())
31 }
3233#[inline]
34fn offset_from(&self, other: &Self) -> isize {
35 other.len() as isize - self.len() as isize
36 }
3738#[inline]
39fn parse_digits(&self, mut func: impl FnMut(u8)) -> &Self {
40let mut s = self;
4142while let Some((c, rest)) = s.split_first() {
43let c = c.wrapping_sub(b'0');
44if c < 10 {
45 func(c);
46 s = rest;
47 } else {
48break;
49 }
50 }
5152 s
53 }
54}
5556/// Determine if all characters in an 8-byte byte string (represented as a `u64`) are all decimal
57/// digits.
58///
59/// This does not care about the order in which the bytes were loaded.
60pub(crate) fn is_8digits(v: u64) -> bool {
61let a = v.wrapping_add(0x4646_4646_4646_4646);
62let b = v.wrapping_sub(0x3030_3030_3030_3030);
63 (a | b) & 0x8080_8080_8080_8080 == 0
64}
6566/// A custom 64-bit floating point type, representing `m * 2^p`.
67/// p is biased, so it be directly shifted into the exponent bits.
68#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
69pub struct BiasedFp {
70/// The significant digits.
71pub m: u64,
72/// The biased, binary exponent.
73pub p_biased: i32,
74}
7576impl BiasedFp {
77/// Represent `0 ^ p`
78#[inline]
79pub const fn zero_pow2(p_biased: i32) -> Self {
80Self { m: 0, p_biased }
81 }
82}