1cbd624adSopenharmony_ci//! Utilities for Rust numbers.
2cbd624adSopenharmony_ci
3cbd624adSopenharmony_ci#![doc(hidden)]
4cbd624adSopenharmony_ci
5cbd624adSopenharmony_ci#[cfg(all(not(feature = "std"), feature = "compact"))]
6cbd624adSopenharmony_ciuse crate::libm::{powd, powf};
7cbd624adSopenharmony_ci#[cfg(not(feature = "compact"))]
8cbd624adSopenharmony_ciuse crate::table::{SMALL_F32_POW10, SMALL_F64_POW10, SMALL_INT_POW10, SMALL_INT_POW5};
9cbd624adSopenharmony_ci#[cfg(not(feature = "compact"))]
10cbd624adSopenharmony_ciuse core::hint;
11cbd624adSopenharmony_ciuse core::ops;
12cbd624adSopenharmony_ci
13cbd624adSopenharmony_ci/// Generic floating-point type, to be used in generic code for parsing.
14cbd624adSopenharmony_ci///
15cbd624adSopenharmony_ci/// Although the trait is part of the public API, the trait provides methods
16cbd624adSopenharmony_ci/// and constants that are effectively non-public: they may be removed
17cbd624adSopenharmony_ci/// at any time without any breaking changes.
18cbd624adSopenharmony_cipub trait Float:
19cbd624adSopenharmony_ci    Sized
20cbd624adSopenharmony_ci    + Copy
21cbd624adSopenharmony_ci    + PartialEq
22cbd624adSopenharmony_ci    + PartialOrd
23cbd624adSopenharmony_ci    + Send
24cbd624adSopenharmony_ci    + Sync
25cbd624adSopenharmony_ci    + ops::Add<Output = Self>
26cbd624adSopenharmony_ci    + ops::AddAssign
27cbd624adSopenharmony_ci    + ops::Div<Output = Self>
28cbd624adSopenharmony_ci    + ops::DivAssign
29cbd624adSopenharmony_ci    + ops::Mul<Output = Self>
30cbd624adSopenharmony_ci    + ops::MulAssign
31cbd624adSopenharmony_ci    + ops::Rem<Output = Self>
32cbd624adSopenharmony_ci    + ops::RemAssign
33cbd624adSopenharmony_ci    + ops::Sub<Output = Self>
34cbd624adSopenharmony_ci    + ops::SubAssign
35cbd624adSopenharmony_ci    + ops::Neg<Output = Self>
36cbd624adSopenharmony_ci{
37cbd624adSopenharmony_ci    /// Maximum number of digits that can contribute in the mantissa.
38cbd624adSopenharmony_ci    ///
39cbd624adSopenharmony_ci    /// We can exactly represent a float in radix `b` from radix 2 if
40cbd624adSopenharmony_ci    /// `b` is divisible by 2. This function calculates the exact number of
41cbd624adSopenharmony_ci    /// digits required to exactly represent that float.
42cbd624adSopenharmony_ci    ///
43cbd624adSopenharmony_ci    /// According to the "Handbook of Floating Point Arithmetic",
44cbd624adSopenharmony_ci    /// for IEEE754, with emin being the min exponent, p2 being the
45cbd624adSopenharmony_ci    /// precision, and b being the radix, the number of digits follows as:
46cbd624adSopenharmony_ci    ///
47cbd624adSopenharmony_ci    /// `−emin + p2 + ⌊(emin + 1) log(2, b) − log(1 − 2^(−p2), b)⌋`
48cbd624adSopenharmony_ci    ///
49cbd624adSopenharmony_ci    /// For f32, this follows as:
50cbd624adSopenharmony_ci    ///     emin = -126
51cbd624adSopenharmony_ci    ///     p2 = 24
52cbd624adSopenharmony_ci    ///
53cbd624adSopenharmony_ci    /// For f64, this follows as:
54cbd624adSopenharmony_ci    ///     emin = -1022
55cbd624adSopenharmony_ci    ///     p2 = 53
56cbd624adSopenharmony_ci    ///
57cbd624adSopenharmony_ci    /// In Python:
58cbd624adSopenharmony_ci    ///     `-emin + p2 + math.floor((emin+1)*math.log(2, b) - math.log(1-2**(-p2), b))`
59cbd624adSopenharmony_ci    ///
60cbd624adSopenharmony_ci    /// This was used to calculate the maximum number of digits for [2, 36].
61cbd624adSopenharmony_ci    const MAX_DIGITS: usize;
62cbd624adSopenharmony_ci
63cbd624adSopenharmony_ci    // MASKS
64cbd624adSopenharmony_ci
65cbd624adSopenharmony_ci    /// Bitmask for the sign bit.
66cbd624adSopenharmony_ci    const SIGN_MASK: u64;
67cbd624adSopenharmony_ci    /// Bitmask for the exponent, including the hidden bit.
68cbd624adSopenharmony_ci    const EXPONENT_MASK: u64;
69cbd624adSopenharmony_ci    /// Bitmask for the hidden bit in exponent, which is an implicit 1 in the fraction.
70cbd624adSopenharmony_ci    const HIDDEN_BIT_MASK: u64;
71cbd624adSopenharmony_ci    /// Bitmask for the mantissa (fraction), excluding the hidden bit.
72cbd624adSopenharmony_ci    const MANTISSA_MASK: u64;
73cbd624adSopenharmony_ci
74cbd624adSopenharmony_ci    // PROPERTIES
75cbd624adSopenharmony_ci
76cbd624adSopenharmony_ci    /// Size of the significand (mantissa) without hidden bit.
77cbd624adSopenharmony_ci    const MANTISSA_SIZE: i32;
78cbd624adSopenharmony_ci    /// Bias of the exponet
79cbd624adSopenharmony_ci    const EXPONENT_BIAS: i32;
80cbd624adSopenharmony_ci    /// Exponent portion of a denormal float.
81cbd624adSopenharmony_ci    const DENORMAL_EXPONENT: i32;
82cbd624adSopenharmony_ci    /// Maximum exponent value in float.
83cbd624adSopenharmony_ci    const MAX_EXPONENT: i32;
84cbd624adSopenharmony_ci
85cbd624adSopenharmony_ci    // ROUNDING
86cbd624adSopenharmony_ci
87cbd624adSopenharmony_ci    /// Mask to determine if a full-carry occurred (1 in bit above hidden bit).
88cbd624adSopenharmony_ci    const CARRY_MASK: u64;
89cbd624adSopenharmony_ci
90cbd624adSopenharmony_ci    /// Bias for marking an invalid extended float.
91cbd624adSopenharmony_ci    // Value is `i16::MIN`, using hard-coded constants for older Rustc versions.
92cbd624adSopenharmony_ci    const INVALID_FP: i32 = -0x8000;
93cbd624adSopenharmony_ci
94cbd624adSopenharmony_ci    // Maximum mantissa for the fast-path (`1 << 53` for f64).
95cbd624adSopenharmony_ci    const MAX_MANTISSA_FAST_PATH: u64 = 2_u64 << Self::MANTISSA_SIZE;
96cbd624adSopenharmony_ci
97cbd624adSopenharmony_ci    // Largest exponent value `(1 << EXP_BITS) - 1`.
98cbd624adSopenharmony_ci    const INFINITE_POWER: i32 = Self::MAX_EXPONENT + Self::EXPONENT_BIAS;
99cbd624adSopenharmony_ci
100cbd624adSopenharmony_ci    // Round-to-even only happens for negative values of q
101cbd624adSopenharmony_ci    // when q ≥ −4 in the 64-bit case and when q ≥ −17 in
102cbd624adSopenharmony_ci    // the 32-bitcase.
103cbd624adSopenharmony_ci    //
104cbd624adSopenharmony_ci    // When q ≥ 0,we have that 5^q ≤ 2m+1. In the 64-bit case,we
105cbd624adSopenharmony_ci    // have 5^q ≤ 2m+1 ≤ 2^54 or q ≤ 23. In the 32-bit case,we have
106cbd624adSopenharmony_ci    // 5^q ≤ 2m+1 ≤ 2^25 or q ≤ 10.
107cbd624adSopenharmony_ci    //
108cbd624adSopenharmony_ci    // When q < 0, we have w ≥ (2m+1)×5^−q. We must have that w < 2^64
109cbd624adSopenharmony_ci    // so (2m+1)×5^−q < 2^64. We have that 2m+1 > 2^53 (64-bit case)
110cbd624adSopenharmony_ci    // or 2m+1 > 2^24 (32-bit case). Hence,we must have 2^53×5^−q < 2^64
111cbd624adSopenharmony_ci    // (64-bit) and 2^24×5^−q < 2^64 (32-bit). Hence we have 5^−q < 2^11
112cbd624adSopenharmony_ci    // or q ≥ −4 (64-bit case) and 5^−q < 2^40 or q ≥ −17 (32-bitcase).
113cbd624adSopenharmony_ci    //
114cbd624adSopenharmony_ci    // Thus we have that we only need to round ties to even when
115cbd624adSopenharmony_ci    // we have that q ∈ [−4,23](in the 64-bit case) or q∈[−17,10]
116cbd624adSopenharmony_ci    // (in the 32-bit case). In both cases,the power of five(5^|q|)
117cbd624adSopenharmony_ci    // fits in a 64-bit word.
118cbd624adSopenharmony_ci    const MIN_EXPONENT_ROUND_TO_EVEN: i32;
119cbd624adSopenharmony_ci    const MAX_EXPONENT_ROUND_TO_EVEN: i32;
120cbd624adSopenharmony_ci
121cbd624adSopenharmony_ci    /// Minimum normal exponent value `-(1 << (EXPONENT_SIZE - 1)) + 1`.
122cbd624adSopenharmony_ci    const MINIMUM_EXPONENT: i32;
123cbd624adSopenharmony_ci
124cbd624adSopenharmony_ci    /// Smallest decimal exponent for a non-zero value.
125cbd624adSopenharmony_ci    const SMALLEST_POWER_OF_TEN: i32;
126cbd624adSopenharmony_ci
127cbd624adSopenharmony_ci    /// Largest decimal exponent for a non-infinite value.
128cbd624adSopenharmony_ci    const LARGEST_POWER_OF_TEN: i32;
129cbd624adSopenharmony_ci
130cbd624adSopenharmony_ci    /// Minimum exponent that for a fast path case, or `-⌊(MANTISSA_SIZE+1)/log2(10)⌋`
131cbd624adSopenharmony_ci    const MIN_EXPONENT_FAST_PATH: i32;
132cbd624adSopenharmony_ci
133cbd624adSopenharmony_ci    /// Maximum exponent that for a fast path case, or `⌊(MANTISSA_SIZE+1)/log2(5)⌋`
134cbd624adSopenharmony_ci    const MAX_EXPONENT_FAST_PATH: i32;
135cbd624adSopenharmony_ci
136cbd624adSopenharmony_ci    /// Maximum exponent that can be represented for a disguised-fast path case.
137cbd624adSopenharmony_ci    /// This is `MAX_EXPONENT_FAST_PATH + ⌊(MANTISSA_SIZE+1)/log2(10)⌋`
138cbd624adSopenharmony_ci    const MAX_EXPONENT_DISGUISED_FAST_PATH: i32;
139cbd624adSopenharmony_ci
140cbd624adSopenharmony_ci    /// Convert 64-bit integer to float.
141cbd624adSopenharmony_ci    fn from_u64(u: u64) -> Self;
142cbd624adSopenharmony_ci
143cbd624adSopenharmony_ci    // Re-exported methods from std.
144cbd624adSopenharmony_ci    fn from_bits(u: u64) -> Self;
145cbd624adSopenharmony_ci    fn to_bits(self) -> u64;
146cbd624adSopenharmony_ci
147cbd624adSopenharmony_ci    /// Get a small power-of-radix for fast-path multiplication.
148cbd624adSopenharmony_ci    ///
149cbd624adSopenharmony_ci    /// # Safety
150cbd624adSopenharmony_ci    ///
151cbd624adSopenharmony_ci    /// Safe as long as the exponent is smaller than the table size.
152cbd624adSopenharmony_ci    unsafe fn pow_fast_path(exponent: usize) -> Self;
153cbd624adSopenharmony_ci
154cbd624adSopenharmony_ci    /// Get a small, integral power-of-radix for fast-path multiplication.
155cbd624adSopenharmony_ci    ///
156cbd624adSopenharmony_ci    /// # Safety
157cbd624adSopenharmony_ci    ///
158cbd624adSopenharmony_ci    /// Safe as long as the exponent is smaller than the table size.
159cbd624adSopenharmony_ci    #[inline(always)]
160cbd624adSopenharmony_ci    unsafe fn int_pow_fast_path(exponent: usize, radix: u32) -> u64 {
161cbd624adSopenharmony_ci        // SAFETY: safe as long as the exponent is smaller than the radix table.
162cbd624adSopenharmony_ci        #[cfg(not(feature = "compact"))]
163cbd624adSopenharmony_ci        return match radix {
164cbd624adSopenharmony_ci            5 => unsafe { *SMALL_INT_POW5.get_unchecked(exponent) },
165cbd624adSopenharmony_ci            10 => unsafe { *SMALL_INT_POW10.get_unchecked(exponent) },
166cbd624adSopenharmony_ci            _ => unsafe { hint::unreachable_unchecked() },
167cbd624adSopenharmony_ci        };
168cbd624adSopenharmony_ci
169cbd624adSopenharmony_ci        #[cfg(feature = "compact")]
170cbd624adSopenharmony_ci        return (radix as u64).pow(exponent as u32);
171cbd624adSopenharmony_ci    }
172cbd624adSopenharmony_ci
173cbd624adSopenharmony_ci    /// Returns true if the float is a denormal.
174cbd624adSopenharmony_ci    #[inline]
175cbd624adSopenharmony_ci    fn is_denormal(self) -> bool {
176cbd624adSopenharmony_ci        self.to_bits() & Self::EXPONENT_MASK == 0
177cbd624adSopenharmony_ci    }
178cbd624adSopenharmony_ci
179cbd624adSopenharmony_ci    /// Get exponent component from the float.
180cbd624adSopenharmony_ci    #[inline]
181cbd624adSopenharmony_ci    fn exponent(self) -> i32 {
182cbd624adSopenharmony_ci        if self.is_denormal() {
183cbd624adSopenharmony_ci            return Self::DENORMAL_EXPONENT;
184cbd624adSopenharmony_ci        }
185cbd624adSopenharmony_ci
186cbd624adSopenharmony_ci        let bits = self.to_bits();
187cbd624adSopenharmony_ci        let biased_e: i32 = ((bits & Self::EXPONENT_MASK) >> Self::MANTISSA_SIZE) as i32;
188cbd624adSopenharmony_ci        biased_e - Self::EXPONENT_BIAS
189cbd624adSopenharmony_ci    }
190cbd624adSopenharmony_ci
191cbd624adSopenharmony_ci    /// Get mantissa (significand) component from float.
192cbd624adSopenharmony_ci    #[inline]
193cbd624adSopenharmony_ci    fn mantissa(self) -> u64 {
194cbd624adSopenharmony_ci        let bits = self.to_bits();
195cbd624adSopenharmony_ci        let s = bits & Self::MANTISSA_MASK;
196cbd624adSopenharmony_ci        if !self.is_denormal() {
197cbd624adSopenharmony_ci            s + Self::HIDDEN_BIT_MASK
198cbd624adSopenharmony_ci        } else {
199cbd624adSopenharmony_ci            s
200cbd624adSopenharmony_ci        }
201cbd624adSopenharmony_ci    }
202cbd624adSopenharmony_ci}
203cbd624adSopenharmony_ci
204cbd624adSopenharmony_ciimpl Float for f32 {
205cbd624adSopenharmony_ci    const MAX_DIGITS: usize = 114;
206cbd624adSopenharmony_ci    const SIGN_MASK: u64 = 0x80000000;
207cbd624adSopenharmony_ci    const EXPONENT_MASK: u64 = 0x7F800000;
208cbd624adSopenharmony_ci    const HIDDEN_BIT_MASK: u64 = 0x00800000;
209cbd624adSopenharmony_ci    const MANTISSA_MASK: u64 = 0x007FFFFF;
210cbd624adSopenharmony_ci    const MANTISSA_SIZE: i32 = 23;
211cbd624adSopenharmony_ci    const EXPONENT_BIAS: i32 = 127 + Self::MANTISSA_SIZE;
212cbd624adSopenharmony_ci    const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
213cbd624adSopenharmony_ci    const MAX_EXPONENT: i32 = 0xFF - Self::EXPONENT_BIAS;
214cbd624adSopenharmony_ci    const CARRY_MASK: u64 = 0x1000000;
215cbd624adSopenharmony_ci    const MIN_EXPONENT_ROUND_TO_EVEN: i32 = -17;
216cbd624adSopenharmony_ci    const MAX_EXPONENT_ROUND_TO_EVEN: i32 = 10;
217cbd624adSopenharmony_ci    const MINIMUM_EXPONENT: i32 = -127;
218cbd624adSopenharmony_ci    const SMALLEST_POWER_OF_TEN: i32 = -65;
219cbd624adSopenharmony_ci    const LARGEST_POWER_OF_TEN: i32 = 38;
220cbd624adSopenharmony_ci    const MIN_EXPONENT_FAST_PATH: i32 = -10;
221cbd624adSopenharmony_ci    const MAX_EXPONENT_FAST_PATH: i32 = 10;
222cbd624adSopenharmony_ci    const MAX_EXPONENT_DISGUISED_FAST_PATH: i32 = 17;
223cbd624adSopenharmony_ci
224cbd624adSopenharmony_ci    #[inline(always)]
225cbd624adSopenharmony_ci    unsafe fn pow_fast_path(exponent: usize) -> Self {
226cbd624adSopenharmony_ci        // SAFETY: safe as long as the exponent is smaller than the radix table.
227cbd624adSopenharmony_ci        #[cfg(not(feature = "compact"))]
228cbd624adSopenharmony_ci        return unsafe { *SMALL_F32_POW10.get_unchecked(exponent) };
229cbd624adSopenharmony_ci
230cbd624adSopenharmony_ci        #[cfg(feature = "compact")]
231cbd624adSopenharmony_ci        return powf(10.0f32, exponent as f32);
232cbd624adSopenharmony_ci    }
233cbd624adSopenharmony_ci
234cbd624adSopenharmony_ci    #[inline]
235cbd624adSopenharmony_ci    fn from_u64(u: u64) -> f32 {
236cbd624adSopenharmony_ci        u as _
237cbd624adSopenharmony_ci    }
238cbd624adSopenharmony_ci
239cbd624adSopenharmony_ci    #[inline]
240cbd624adSopenharmony_ci    fn from_bits(u: u64) -> f32 {
241cbd624adSopenharmony_ci        // Constant is `u32::MAX` for older Rustc versions.
242cbd624adSopenharmony_ci        debug_assert!(u <= 0xffff_ffff);
243cbd624adSopenharmony_ci        f32::from_bits(u as u32)
244cbd624adSopenharmony_ci    }
245cbd624adSopenharmony_ci
246cbd624adSopenharmony_ci    #[inline]
247cbd624adSopenharmony_ci    fn to_bits(self) -> u64 {
248cbd624adSopenharmony_ci        f32::to_bits(self) as u64
249cbd624adSopenharmony_ci    }
250cbd624adSopenharmony_ci}
251cbd624adSopenharmony_ci
252cbd624adSopenharmony_ciimpl Float for f64 {
253cbd624adSopenharmony_ci    const MAX_DIGITS: usize = 769;
254cbd624adSopenharmony_ci    const SIGN_MASK: u64 = 0x8000000000000000;
255cbd624adSopenharmony_ci    const EXPONENT_MASK: u64 = 0x7FF0000000000000;
256cbd624adSopenharmony_ci    const HIDDEN_BIT_MASK: u64 = 0x0010000000000000;
257cbd624adSopenharmony_ci    const MANTISSA_MASK: u64 = 0x000FFFFFFFFFFFFF;
258cbd624adSopenharmony_ci    const MANTISSA_SIZE: i32 = 52;
259cbd624adSopenharmony_ci    const EXPONENT_BIAS: i32 = 1023 + Self::MANTISSA_SIZE;
260cbd624adSopenharmony_ci    const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
261cbd624adSopenharmony_ci    const MAX_EXPONENT: i32 = 0x7FF - Self::EXPONENT_BIAS;
262cbd624adSopenharmony_ci    const CARRY_MASK: u64 = 0x20000000000000;
263cbd624adSopenharmony_ci    const MIN_EXPONENT_ROUND_TO_EVEN: i32 = -4;
264cbd624adSopenharmony_ci    const MAX_EXPONENT_ROUND_TO_EVEN: i32 = 23;
265cbd624adSopenharmony_ci    const MINIMUM_EXPONENT: i32 = -1023;
266cbd624adSopenharmony_ci    const SMALLEST_POWER_OF_TEN: i32 = -342;
267cbd624adSopenharmony_ci    const LARGEST_POWER_OF_TEN: i32 = 308;
268cbd624adSopenharmony_ci    const MIN_EXPONENT_FAST_PATH: i32 = -22;
269cbd624adSopenharmony_ci    const MAX_EXPONENT_FAST_PATH: i32 = 22;
270cbd624adSopenharmony_ci    const MAX_EXPONENT_DISGUISED_FAST_PATH: i32 = 37;
271cbd624adSopenharmony_ci
272cbd624adSopenharmony_ci    #[inline(always)]
273cbd624adSopenharmony_ci    unsafe fn pow_fast_path(exponent: usize) -> Self {
274cbd624adSopenharmony_ci        // SAFETY: safe as long as the exponent is smaller than the radix table.
275cbd624adSopenharmony_ci        #[cfg(not(feature = "compact"))]
276cbd624adSopenharmony_ci        return unsafe { *SMALL_F64_POW10.get_unchecked(exponent) };
277cbd624adSopenharmony_ci
278cbd624adSopenharmony_ci        #[cfg(feature = "compact")]
279cbd624adSopenharmony_ci        return powd(10.0f64, exponent as f64);
280cbd624adSopenharmony_ci    }
281cbd624adSopenharmony_ci
282cbd624adSopenharmony_ci    #[inline]
283cbd624adSopenharmony_ci    fn from_u64(u: u64) -> f64 {
284cbd624adSopenharmony_ci        u as _
285cbd624adSopenharmony_ci    }
286cbd624adSopenharmony_ci
287cbd624adSopenharmony_ci    #[inline]
288cbd624adSopenharmony_ci    fn from_bits(u: u64) -> f64 {
289cbd624adSopenharmony_ci        f64::from_bits(u)
290cbd624adSopenharmony_ci    }
291cbd624adSopenharmony_ci
292cbd624adSopenharmony_ci    #[inline]
293cbd624adSopenharmony_ci    fn to_bits(self) -> u64 {
294cbd624adSopenharmony_ci        f64::to_bits(self)
295cbd624adSopenharmony_ci    }
296cbd624adSopenharmony_ci}
297cbd624adSopenharmony_ci
298cbd624adSopenharmony_ci#[inline(always)]
299cbd624adSopenharmony_ci#[cfg(all(feature = "std", feature = "compact"))]
300cbd624adSopenharmony_cipub fn powf(x: f32, y: f32) -> f32 {
301cbd624adSopenharmony_ci    x.powf(y)
302cbd624adSopenharmony_ci}
303cbd624adSopenharmony_ci
304cbd624adSopenharmony_ci#[inline(always)]
305cbd624adSopenharmony_ci#[cfg(all(feature = "std", feature = "compact"))]
306cbd624adSopenharmony_cipub fn powd(x: f64, y: f64) -> f64 {
307cbd624adSopenharmony_ci    x.powf(y)
308cbd624adSopenharmony_ci}
309