Yi Kong | ce81bb6 | 2020-08-31 01:21:33 +0800 | [diff] [blame^] | 1 | // Translated from C to Rust. The original C code can be found at |
| 2 | // https://github.com/ulfjack/ryu and carries the following license: |
| 3 | // |
| 4 | // Copyright 2018 Ulf Adams |
| 5 | // |
| 6 | // The contents of this file may be used under the terms of the Apache License, |
| 7 | // Version 2.0. |
| 8 | // |
| 9 | // (See accompanying file LICENSE-Apache or copy at |
| 10 | // http://www.apache.org/licenses/LICENSE-2.0) |
| 11 | // |
| 12 | // Alternatively, the contents of this file may be used under the terms of |
| 13 | // the Boost Software License, Version 1.0. |
| 14 | // (See accompanying file LICENSE-Boost or copy at |
| 15 | // https://www.boost.org/LICENSE_1_0.txt) |
| 16 | // |
| 17 | // Unless required by applicable law or agreed to in writing, this software |
| 18 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 | // KIND, either express or implied. |
| 20 | |
| 21 | // Returns the number of decimal digits in v, which must not contain more than 9 |
| 22 | // digits. |
| 23 | #[cfg_attr(feature = "no-panic", inline)] |
| 24 | pub fn decimal_length9(v: u32) -> u32 { |
| 25 | // Function precondition: v is not a 10-digit number. |
| 26 | // (f2s: 9 digits are sufficient for round-tripping.) |
| 27 | debug_assert!(v < 1000000000); |
| 28 | |
| 29 | if v >= 100000000 { |
| 30 | 9 |
| 31 | } else if v >= 10000000 { |
| 32 | 8 |
| 33 | } else if v >= 1000000 { |
| 34 | 7 |
| 35 | } else if v >= 100000 { |
| 36 | 6 |
| 37 | } else if v >= 10000 { |
| 38 | 5 |
| 39 | } else if v >= 1000 { |
| 40 | 4 |
| 41 | } else if v >= 100 { |
| 42 | 3 |
| 43 | } else if v >= 10 { |
| 44 | 2 |
| 45 | } else { |
| 46 | 1 |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Returns e == 0 ? 1 : [log_2(5^e)]; requires 0 <= e <= 3528. |
| 51 | #[cfg_attr(feature = "no-panic", inline)] |
| 52 | #[allow(dead_code)] |
| 53 | pub fn log2_pow5(e: i32) -> i32 /* or u32 -> u32 */ { |
| 54 | // This approximation works up to the point that the multiplication |
| 55 | // overflows at e = 3529. If the multiplication were done in 64 bits, it |
| 56 | // would fail at 5^4004 which is just greater than 2^9297. |
| 57 | debug_assert!(e >= 0); |
| 58 | debug_assert!(e <= 3528); |
| 59 | ((e as u32 * 1217359) >> 19) as i32 |
| 60 | } |
| 61 | |
| 62 | // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528. |
| 63 | #[cfg_attr(feature = "no-panic", inline)] |
| 64 | pub fn pow5bits(e: i32) -> i32 /* or u32 -> u32 */ { |
| 65 | // This approximation works up to the point that the multiplication |
| 66 | // overflows at e = 3529. If the multiplication were done in 64 bits, it |
| 67 | // would fail at 5^4004 which is just greater than 2^9297. |
| 68 | debug_assert!(e >= 0); |
| 69 | debug_assert!(e <= 3528); |
| 70 | (((e as u32 * 1217359) >> 19) + 1) as i32 |
| 71 | } |
| 72 | |
| 73 | #[cfg_attr(feature = "no-panic", inline)] |
| 74 | #[allow(dead_code)] |
| 75 | pub fn ceil_log2_pow5(e: i32) -> i32 /* or u32 -> u32 */ { |
| 76 | log2_pow5(e) + 1 |
| 77 | } |
| 78 | |
| 79 | // Returns floor(log_10(2^e)); requires 0 <= e <= 1650. |
| 80 | #[cfg_attr(feature = "no-panic", inline)] |
| 81 | pub fn log10_pow2(e: i32) -> u32 /* or u32 -> u32 */ { |
| 82 | // The first value this approximation fails for is 2^1651 which is just greater than 10^297. |
| 83 | debug_assert!(e >= 0); |
| 84 | debug_assert!(e <= 1650); |
| 85 | (e as u32 * 78913) >> 18 |
| 86 | } |
| 87 | |
| 88 | // Returns floor(log_10(5^e)); requires 0 <= e <= 2620. |
| 89 | #[cfg_attr(feature = "no-panic", inline)] |
| 90 | pub fn log10_pow5(e: i32) -> u32 /* or u32 -> u32 */ { |
| 91 | // The first value this approximation fails for is 5^2621 which is just greater than 10^1832. |
| 92 | debug_assert!(e >= 0); |
| 93 | debug_assert!(e <= 2620); |
| 94 | (e as u32 * 732923) >> 20 |
| 95 | } |