| 1 | use crate::binary::compute_float; |
| 2 | use crate::float::Float; |
| 3 | use crate::number::{parse_inf_nan, parse_number}; |
| 4 | use crate::simple::parse_long_mantissa; |
| 5 | |
| 6 | #[inline] |
| 7 | pub fn parse_float<F: Float>(s: &[u8]) -> Option<(F, usize)> { |
| 8 | let (num, rest) = match parse_number(s) { |
| 9 | Some(r) => r, |
| 10 | None => return parse_inf_nan(s), |
| 11 | }; |
| 12 | if let Some(value) = num.try_fast_path::<F>() { |
| 13 | return Some((value, rest)); |
| 14 | } |
| 15 | |
| 16 | let mut am = compute_float::<F>(num.exponent, num.mantissa); |
| 17 | if num.many_digits && am != compute_float::<F>(num.exponent, num.mantissa + 1) { |
| 18 | am.power2 = -1; |
| 19 | } |
| 20 | if am.power2 < 0 { |
| 21 | am = parse_long_mantissa::<F>(s); |
| 22 | } |
| 23 | |
| 24 | let mut word = am.mantissa; |
| 25 | word |= (am.power2 as u64) << F::MANTISSA_EXPLICIT_BITS; |
| 26 | if num.negative { |
| 27 | word |= 1_u64 << F::SIGN_INDEX; |
| 28 | } |
| 29 | Some((F::from_u64_bits(word), rest)) |
| 30 | } |