summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_json/src/lexical/cached.rs
blob: ef5a9fe54d060dd620a24a8027b3adfedb1037bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Adapted from https://github.com/Alexhuszagh/rust-lexical.

//! Cached powers trait for extended-precision floats.

use super::cached_float80;
use super::float::ExtendedFloat;

// POWERS

/// Precalculated powers that uses two-separate arrays for memory-efficiency.
#[doc(hidden)]
pub(crate) struct ExtendedFloatArray {
    // Pre-calculated mantissa for the powers.
    pub mant: &'static [u64],
    // Pre-calculated binary exponents for the powers.
    pub exp: &'static [i32],
}

/// Allow indexing of values without bounds checking
impl ExtendedFloatArray {
    #[inline]
    pub fn get_extended_float(&self, index: usize) -> ExtendedFloat {
        let mant = self.mant[index];
        let exp = self.exp[index];
        ExtendedFloat { mant, exp }
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.mant.len()
    }
}

// MODERATE PATH POWERS

/// Precalculated powers of base N for the moderate path.
#[doc(hidden)]
pub(crate) struct ModeratePathPowers {
    // Pre-calculated small powers.
    pub small: ExtendedFloatArray,
    // Pre-calculated large powers.
    pub large: ExtendedFloatArray,
    /// Pre-calculated small powers as 64-bit integers
    pub small_int: &'static [u64],
    // Step between large powers and number of small powers.
    pub step: i32,
    // Exponent bias for the large powers.
    pub bias: i32,
}

/// Allow indexing of values without bounds checking
impl ModeratePathPowers {
    #[inline]
    pub fn get_small(&self, index: usize) -> ExtendedFloat {
        self.small.get_extended_float(index)
    }

    #[inline]
    pub fn get_large(&self, index: usize) -> ExtendedFloat {
        self.large.get_extended_float(index)
    }

    #[inline]
    pub fn get_small_int(&self, index: usize) -> u64 {
        self.small_int[index]
    }
}

// CACHED EXTENDED POWERS

/// Cached powers as a trait for a floating-point type.
pub(crate) trait ModeratePathCache {
    /// Get cached powers.
    fn get_powers() -> &'static ModeratePathPowers;
}

impl ModeratePathCache for ExtendedFloat {
    #[inline]
    fn get_powers() -> &'static ModeratePathPowers {
        cached_float80::get_powers()
    }
}