/rust/registry/src/index.crates.io-6f17d22bba15001f/ryu-1.0.20/src/common.rs
Line | Count | Source (jump to first uncovered line) |
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 | 0 | pub fn decimal_length9(v: u32) -> u32 { |
25 | 0 | // Function precondition: v is not a 10-digit number. |
26 | 0 | // (f2s: 9 digits are sufficient for round-tripping.) |
27 | 0 | debug_assert!(v < 1000000000); |
28 | | |
29 | 0 | if v >= 100000000 { |
30 | 0 | 9 |
31 | 0 | } else if v >= 10000000 { |
32 | 0 | 8 |
33 | 0 | } else if v >= 1000000 { |
34 | 0 | 7 |
35 | 0 | } else if v >= 100000 { |
36 | 0 | 6 |
37 | 0 | } else if v >= 10000 { |
38 | 0 | 5 |
39 | 0 | } else if v >= 1000 { |
40 | 0 | 4 |
41 | 0 | } else if v >= 100 { |
42 | 0 | 3 |
43 | 0 | } else if v >= 10 { |
44 | 0 | 2 |
45 | | } else { |
46 | 0 | 1 |
47 | | } |
48 | 0 | } |
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 | 0 | pub fn log2_pow5(e: i32) -> i32 /* or u32 -> u32 */ { |
54 | 0 | // This approximation works up to the point that the multiplication |
55 | 0 | // overflows at e = 3529. If the multiplication were done in 64 bits, it |
56 | 0 | // would fail at 5^4004 which is just greater than 2^9297. |
57 | 0 | debug_assert!(e >= 0); |
58 | 0 | debug_assert!(e <= 3528); |
59 | 0 | ((e as u32 * 1217359) >> 19) as i32 |
60 | 0 | } |
61 | | |
62 | | // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528. |
63 | | #[cfg_attr(feature = "no-panic", inline)] |
64 | 0 | pub fn pow5bits(e: i32) -> i32 /* or u32 -> u32 */ { |
65 | 0 | // This approximation works up to the point that the multiplication |
66 | 0 | // overflows at e = 3529. If the multiplication were done in 64 bits, it |
67 | 0 | // would fail at 5^4004 which is just greater than 2^9297. |
68 | 0 | debug_assert!(e >= 0); |
69 | 0 | debug_assert!(e <= 3528); |
70 | 0 | (((e as u32 * 1217359) >> 19) + 1) as i32 |
71 | 0 | } |
72 | | |
73 | | #[cfg_attr(feature = "no-panic", inline)] |
74 | | #[allow(dead_code)] |
75 | 0 | pub fn ceil_log2_pow5(e: i32) -> i32 /* or u32 -> u32 */ { |
76 | 0 | log2_pow5(e) + 1 |
77 | 0 | } |
78 | | |
79 | | // Returns floor(log_10(2^e)); requires 0 <= e <= 1650. |
80 | | #[cfg_attr(feature = "no-panic", inline)] |
81 | 0 | pub fn log10_pow2(e: i32) -> u32 /* or u32 -> u32 */ { |
82 | 0 | // The first value this approximation fails for is 2^1651 which is just greater than 10^297. |
83 | 0 | debug_assert!(e >= 0); |
84 | 0 | debug_assert!(e <= 1650); |
85 | 0 | (e as u32 * 78913) >> 18 |
86 | 0 | } |
87 | | |
88 | | // Returns floor(log_10(5^e)); requires 0 <= e <= 2620. |
89 | | #[cfg_attr(feature = "no-panic", inline)] |
90 | 0 | pub fn log10_pow5(e: i32) -> u32 /* or u32 -> u32 */ { |
91 | 0 | // The first value this approximation fails for is 5^2621 which is just greater than 10^1832. |
92 | 0 | debug_assert!(e >= 0); |
93 | 0 | debug_assert!(e <= 2620); |
94 | 0 | (e as u32 * 732923) >> 20 |
95 | 0 | } |