lexical_parse_float/
table_decimal.rs

1//! Pre-computed tables for writing decimal strings.
2
3#![doc(hidden)]
4#![cfg(not(feature = "compact"))]
5
6#[cfg(not(feature = "radix"))]
7use crate::bigint::Limb;
8use crate::limits::{f32_exponent_limit, f64_exponent_limit, f64_mantissa_limit, u64_power_limit};
9
10// HELPERS
11// -------
12
13/// Get lookup table for small int powers.
14#[must_use]
15#[inline(always)]
16#[cfg(not(feature = "power-of-two"))]
17pub const fn get_small_int_power(exponent: usize, radix: u32) -> u64 {
18    // NOTE: don't check the radix since we also use it for half radix, or 5.
19    match radix {
20        5 => get_small_int_power5(exponent),
21        10 => get_small_int_power10(exponent),
22        _ => unreachable!(),
23    }
24}
25
26/// Get lookup table for small f32 powers.
27#[must_use]
28#[inline(always)]
29#[cfg(not(feature = "power-of-two"))]
30pub const fn get_small_f32_power(exponent: usize, radix: u32) -> f32 {
31    _ = radix;
32    get_small_f32_power10(exponent)
33}
34
35/// Get lookup table for small f64 powers.
36#[must_use]
37#[inline(always)]
38#[cfg(not(feature = "power-of-two"))]
39pub const fn get_small_f64_power(exponent: usize, radix: u32) -> f64 {
40    _ = radix;
41    get_small_f64_power10(exponent)
42}
43
44/// Get pre-computed power for a large power of radix.
45#[must_use]
46#[inline(always)]
47#[cfg(not(feature = "radix"))]
48pub const fn get_large_int_power(_: u32) -> (&'static [Limb], u32) {
49    (&LARGE_POW5, LARGE_POW5_STEP)
50}
51
52/// Get pre-computed int power of 5.
53#[must_use]
54#[inline(always)]
55pub const fn get_small_int_power5(exponent: usize) -> u64 {
56    SMALL_INT_POW5[exponent]
57}
58
59/// Get pre-computed int power of 10.
60#[must_use]
61#[inline(always)]
62pub const fn get_small_int_power10(exponent: usize) -> u64 {
63    SMALL_INT_POW10[exponent]
64}
65
66/// Get pre-computed f32 power of 10.
67#[must_use]
68#[inline(always)]
69pub const fn get_small_f32_power10(exponent: usize) -> f32 {
70    SMALL_F32_POW10[exponent]
71}
72
73/// Get pre-computed f64 power of 10.
74#[must_use]
75#[inline(always)]
76pub const fn get_small_f64_power10(exponent: usize) -> f64 {
77    SMALL_F64_POW10[exponent]
78}
79
80// TABLES
81// ------
82
83/// Pre-computed, small powers-of-5.
84pub const SMALL_INT_POW5: [u64; 28] = [
85    1,
86    5,
87    25,
88    125,
89    625,
90    3125,
91    15625,
92    78125,
93    390625,
94    1953125,
95    9765625,
96    48828125,
97    244140625,
98    1220703125,
99    6103515625,
100    30517578125,
101    152587890625,
102    762939453125,
103    3814697265625,
104    19073486328125,
105    95367431640625,
106    476837158203125,
107    2384185791015625,
108    11920928955078125,
109    59604644775390625,
110    298023223876953125,
111    1490116119384765625,
112    7450580596923828125,
113];
114const _: () = assert!(SMALL_INT_POW5.len() > f64_mantissa_limit(5) as usize);
115const _: () = assert!(SMALL_INT_POW5.len() == u64_power_limit(5) as usize + 1);
116
117/// Pre-computed, small powers-of-10.
118pub const SMALL_INT_POW10: [u64; 20] = [
119    1,
120    10,
121    100,
122    1000,
123    10000,
124    100000,
125    1000000,
126    10000000,
127    100000000,
128    1000000000,
129    10000000000,
130    100000000000,
131    1000000000000,
132    10000000000000,
133    100000000000000,
134    1000000000000000,
135    10000000000000000,
136    100000000000000000,
137    1000000000000000000,
138    10000000000000000000,
139];
140const _: () = assert!(SMALL_INT_POW10.len() > f64_mantissa_limit(10) as usize);
141const _: () = assert!(SMALL_INT_POW10.len() == u64_power_limit(10) as usize + 1);
142
143/// Pre-computed, small powers-of-10.
144pub const SMALL_F32_POW10: [f32; 16] =
145    [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 0., 0., 0., 0., 0.];
146const _: () = assert!(SMALL_F32_POW10.len() > f32_exponent_limit(10).1 as usize);
147
148/// Pre-computed, small powers-of-10.
149pub const SMALL_F64_POW10: [f64; 32] = [
150    1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16,
151    1e17, 1e18, 1e19, 1e20, 1e21, 1e22, 0., 0., 0., 0., 0., 0., 0., 0., 0.,
152];
153const _: () = assert!(SMALL_F64_POW10.len() > f64_exponent_limit(10).1 as usize);
154
155/// Pre-computed large power-of-5 for 32-bit limbs.
156#[cfg(not(all(target_pointer_width = "64", not(target_arch = "sparc"))))]
157pub const LARGE_POW5: [u32; 10] = [
158    4279965485, 329373468, 4020270615, 2137533757, 4287402176, 1057042919, 1071430142, 2440757623,
159    381945767, 46164893,
160];
161
162/// Pre-computed large power-of-5 for 64-bit limbs.
163#[cfg(all(target_pointer_width = "64", not(target_arch = "sparc")))]
164pub const LARGE_POW5: [u64; 5] = [
165    1414648277510068013,
166    9180637584431281687,
167    4539964771860779200,
168    10482974169319127550,
169    198276706040285095,
170];
171
172/// Step for large power-of-5 for 32-bit limbs.
173pub const LARGE_POW5_STEP: u32 = 135;