1- //! Helper trait for generic float types .
1+ //! Numeric traits used for internal implementations .
22
3- use core:: f64;
3+ #![ doc( hidden) ]
4+ #![ unstable(
5+ feature = "num_internals" ,
6+ reason = "internal routines only exposed for testing" ,
7+ issue = "none"
8+ ) ]
49
5- use crate :: fmt:: { Debug , LowerExp } ;
610use crate :: num:: FpCategory ;
7- use crate :: ops :: { self , Add , Div , Mul , Neg } ;
11+ use crate :: { f64 , fmt , ops } ;
812
913/// Lossy `as` casting between two types.
1014pub trait CastInto < T : Copy > : Copy {
@@ -16,7 +20,7 @@ pub trait Int:
1620 Sized
1721 + Clone
1822 + Copy
19- + Debug
23+ + fmt :: Debug
2024 + ops:: Shr < u32 , Output = Self >
2125 + ops:: Shl < u32 , Output = Self >
2226 + ops:: BitAnd < Output = Self >
@@ -51,17 +55,17 @@ int!(u16, u32, u64);
5155#[ doc( hidden) ]
5256pub trait Float :
5357 Sized
54- + Div < Output = Self >
55- + Neg < Output = Self >
56- + Mul < Output = Self >
57- + Add < Output = Self >
58- + LowerExp
58+ + ops:: Div < Output = Self >
59+ + ops:: Neg < Output = Self >
60+ + ops:: Mul < Output = Self >
61+ + ops:: Add < Output = Self >
62+ + fmt:: Debug
63+ + fmt:: LowerExp
5964 + PartialEq
6065 + PartialOrd
6166 + Default
6267 + Clone
6368 + Copy
64- + Debug
6569{
6670 /// The unsigned integer with the same size as the float
6771 type Int : Int + Into < u64 > ;
@@ -184,41 +188,6 @@ pub trait FloatExt: Float {
184188 }
185189}
186190
187- /// Extension to `Float` that are necessary for parsing using the Lemire method.
188- ///
189- /// See the parent module's doc comment for why this is necessary.
190- ///
191- /// Not intended for use outside of the `dec2flt` module.
192- #[ doc( hidden) ]
193- pub trait Lemire : FloatExt {
194- /// Maximum exponent for a fast path case, or `⌊(SIG_BITS+1)/log2(5)⌋`
195- // assuming FLT_EVAL_METHOD = 0
196- const MAX_EXPONENT_FAST_PATH : i64 = {
197- let log2_5 = f64:: consts:: LOG2_10 - 1.0 ;
198- ( Self :: SIG_TOTAL_BITS as f64 / log2_5) as i64
199- } ;
200-
201- /// Minimum exponent for a fast path case, or `-⌊(SIG_BITS+1)/log2(5)⌋`
202- const MIN_EXPONENT_FAST_PATH : i64 = -Self :: MAX_EXPONENT_FAST_PATH ;
203-
204- /// Maximum exponent that can be represented for a disguised-fast path case.
205- /// This is `MAX_EXPONENT_FAST_PATH + ⌊(SIG_BITS+1)/log2(10)⌋`
206- const MAX_EXPONENT_DISGUISED_FAST_PATH : i64 =
207- Self :: MAX_EXPONENT_FAST_PATH + ( Self :: SIG_TOTAL_BITS as f64 / f64:: consts:: LOG2_10 ) as i64 ;
208-
209- /// Maximum mantissa for the fast-path (`1 << 53` for f64).
210- const MAX_MANTISSA_FAST_PATH : u64 = 1 << Self :: SIG_TOTAL_BITS ;
211-
212- /// Gets a small power-of-ten for fast-path multiplication.
213- fn pow10_fast_path ( exponent : usize ) -> Self ;
214-
215- /// Converts integer into float through an as cast.
216- /// This is only called in the fast-path algorithm, and therefore
217- /// will not lose precision, since the value will always have
218- /// only if the value is <= Self::MAX_MANTISSA_FAST_PATH.
219- fn from_u64 ( v : u64 ) -> Self ;
220- }
221-
222191/// Solve for `b` in `10^b = 2^a`
223192const fn pow2_to_pow10 ( a : i64 ) -> i64 {
224193 let res = ( a as f64 ) / f64:: consts:: LOG2_10 ;
@@ -260,21 +229,6 @@ impl FloatExt for f16 {
260229 }
261230}
262231
263- #[ cfg( target_has_reliable_f16) ]
264- impl Lemire for f16 {
265- fn pow10_fast_path ( exponent : usize ) -> Self {
266- #[ allow( clippy:: use_self) ]
267- const TABLE : [ f16 ; 8 ] = [ 1e0 , 1e1 , 1e2 , 1e3 , 1e4 , 0.0 , 0.0 , 0. ] ;
268- TABLE [ exponent & 7 ]
269- }
270-
271- #[ inline]
272- fn from_u64 ( v : u64 ) -> Self {
273- debug_assert ! ( v <= Self :: MAX_MANTISSA_FAST_PATH ) ;
274- v as _
275- }
276- }
277-
278232impl Float for f32 {
279233 type Int = u32 ;
280234
@@ -308,21 +262,6 @@ impl FloatExt for f32 {
308262 }
309263}
310264
311- impl Lemire for f32 {
312- fn pow10_fast_path ( exponent : usize ) -> Self {
313- #[ allow( clippy:: use_self) ]
314- const TABLE : [ f32 ; 16 ] =
315- [ 1e0 , 1e1 , 1e2 , 1e3 , 1e4 , 1e5 , 1e6 , 1e7 , 1e8 , 1e9 , 1e10 , 0. , 0. , 0. , 0. , 0. ] ;
316- TABLE [ exponent & 15 ]
317- }
318-
319- #[ inline]
320- fn from_u64 ( v : u64 ) -> Self {
321- debug_assert ! ( v <= Self :: MAX_MANTISSA_FAST_PATH ) ;
322- v as _
323- }
324- }
325-
326265impl Float for f64 {
327266 type Int = u64 ;
328267
@@ -355,19 +294,3 @@ impl FloatExt for f64 {
355294 f64:: from_bits ( v)
356295 }
357296}
358-
359- impl Lemire for f64 {
360- fn pow10_fast_path ( exponent : usize ) -> Self {
361- const TABLE : [ f64 ; 32 ] = [
362- 1e0 , 1e1 , 1e2 , 1e3 , 1e4 , 1e5 , 1e6 , 1e7 , 1e8 , 1e9 , 1e10 , 1e11 , 1e12 , 1e13 , 1e14 , 1e15 ,
363- 1e16 , 1e17 , 1e18 , 1e19 , 1e20 , 1e21 , 1e22 , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
364- ] ;
365- TABLE [ exponent & 31 ]
366- }
367-
368- #[ inline]
369- fn from_u64 ( v : u64 ) -> Self {
370- debug_assert ! ( v <= Self :: MAX_MANTISSA_FAST_PATH ) ;
371- v as _
372- }
373- }
0 commit comments