/rust/registry/src/index.crates.io-1949cf8c6b5b557f/ryu-1.0.23/src/f2s.rs
Line | Count | Source |
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 | | use crate::common::{log10_pow2, log10_pow5, pow5bits}; |
22 | | use crate::f2s_intrinsics::{ |
23 | | mul_pow5_div_pow2, mul_pow5_inv_div_pow2, multiple_of_power_of_2_32, multiple_of_power_of_5_32, |
24 | | }; |
25 | | |
26 | | pub const FLOAT_MANTISSA_BITS: u32 = 23; |
27 | | pub const FLOAT_EXPONENT_BITS: u32 = 8; |
28 | | const FLOAT_BIAS: i32 = 127; |
29 | | pub use crate::f2s_intrinsics::{FLOAT_POW5_BITCOUNT, FLOAT_POW5_INV_BITCOUNT}; |
30 | | |
31 | | // A floating decimal representing m * 10^e. |
32 | | pub struct FloatingDecimal32 { |
33 | | pub mantissa: u32, |
34 | | // Decimal exponent's range is -45 to 38 |
35 | | // inclusive, and can fit in i16 if needed. |
36 | | pub exponent: i32, |
37 | | } |
38 | | |
39 | | #[cfg_attr(feature = "no-panic", inline)] |
40 | 3.92k | pub fn f2d(ieee_mantissa: u32, ieee_exponent: u32) -> FloatingDecimal32 { |
41 | 3.92k | let (e2, m2) = if ieee_exponent == 0 { |
42 | 2 | ( |
43 | 2 | // We subtract 2 so that the bounds computation has 2 additional bits. |
44 | 2 | 1 - FLOAT_BIAS - FLOAT_MANTISSA_BITS as i32 - 2, |
45 | 2 | ieee_mantissa, |
46 | 2 | ) |
47 | | } else { |
48 | 3.91k | ( |
49 | 3.91k | ieee_exponent as i32 - FLOAT_BIAS - FLOAT_MANTISSA_BITS as i32 - 2, |
50 | 3.91k | (1u32 << FLOAT_MANTISSA_BITS) | ieee_mantissa, |
51 | 3.91k | ) |
52 | | }; |
53 | 3.92k | let even = (m2 & 1) == 0; |
54 | 3.92k | let accept_bounds = even; |
55 | | |
56 | | // Step 2: Determine the interval of valid decimal representations. |
57 | 3.92k | let mv = 4 * m2; |
58 | 3.92k | let mp = 4 * m2 + 2; |
59 | | // Implicit bool -> int conversion. True is 1, false is 0. |
60 | 3.92k | let mm_shift = (ieee_mantissa != 0 || ieee_exponent <= 1) as u32; |
61 | 3.92k | let mm = 4 * m2 - 1 - mm_shift; |
62 | | |
63 | | // Step 3: Convert to a decimal power base using 64-bit arithmetic. |
64 | | let mut vr: u32; |
65 | | let mut vp: u32; |
66 | | let mut vm: u32; |
67 | | let e10: i32; |
68 | 3.92k | let mut vm_is_trailing_zeros = false; |
69 | 3.92k | let mut vr_is_trailing_zeros = false; |
70 | 3.92k | let mut last_removed_digit = 0u8; |
71 | 3.92k | if e2 >= 0 { |
72 | 418 | let q = log10_pow2(e2); |
73 | 418 | e10 = q as i32; |
74 | 418 | let k = FLOAT_POW5_INV_BITCOUNT + pow5bits(q as i32) - 1; |
75 | 418 | let i = -e2 + q as i32 + k; |
76 | 418 | vr = mul_pow5_inv_div_pow2(mv, q, i); |
77 | 418 | vp = mul_pow5_inv_div_pow2(mp, q, i); |
78 | 418 | vm = mul_pow5_inv_div_pow2(mm, q, i); |
79 | 418 | if q != 0 && (vp - 1) / 10 <= vm / 10 { |
80 | 121 | // We need to know one removed digit even if we are not going to loop below. We could use |
81 | 121 | // q = X - 1 above, except that would require 33 bits for the result, and we've found that |
82 | 121 | // 32-bit arithmetic is faster even on 64-bit machines. |
83 | 121 | let l = FLOAT_POW5_INV_BITCOUNT + pow5bits(q as i32 - 1) - 1; |
84 | 121 | last_removed_digit = |
85 | 121 | (mul_pow5_inv_div_pow2(mv, q - 1, -e2 + q as i32 - 1 + l) % 10) as u8; |
86 | 297 | } |
87 | 418 | if q <= 9 { |
88 | | // The largest power of 5 that fits in 24 bits is 5^10, but q <= 9 seems to be safe as well. |
89 | | // Only one of mp, mv, and mm can be a multiple of 5, if any. |
90 | 281 | if mv % 5 == 0 { |
91 | 70 | vr_is_trailing_zeros = multiple_of_power_of_5_32(mv, q); |
92 | 211 | } else if accept_bounds { |
93 | 122 | vm_is_trailing_zeros = multiple_of_power_of_5_32(mm, q); |
94 | 122 | } else { |
95 | 89 | vp -= multiple_of_power_of_5_32(mp, q) as u32; |
96 | 89 | } |
97 | 137 | } |
98 | | } else { |
99 | 3.50k | let q = log10_pow5(-e2); |
100 | 3.50k | e10 = q as i32 + e2; |
101 | 3.50k | let i = -e2 - q as i32; |
102 | 3.50k | let k = pow5bits(i) - FLOAT_POW5_BITCOUNT; |
103 | 3.50k | let mut j = q as i32 - k; |
104 | 3.50k | vr = mul_pow5_div_pow2(mv, i as u32, j); |
105 | 3.50k | vp = mul_pow5_div_pow2(mp, i as u32, j); |
106 | 3.50k | vm = mul_pow5_div_pow2(mm, i as u32, j); |
107 | 3.50k | if q != 0 && (vp - 1) / 10 <= vm / 10 { |
108 | 38 | j = q as i32 - 1 - (pow5bits(i + 1) - FLOAT_POW5_BITCOUNT); |
109 | 38 | last_removed_digit = (mul_pow5_div_pow2(mv, (i + 1) as u32, j) % 10) as u8; |
110 | 3.46k | } |
111 | 3.50k | if q <= 1 { |
112 | | // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits. |
113 | | // mv = 4 * m2, so it always has at least two trailing 0 bits. |
114 | 23 | vr_is_trailing_zeros = true; |
115 | 23 | if accept_bounds { |
116 | 17 | // mm = mv - 1 - mm_shift, so it has 1 trailing 0 bit iff mm_shift == 1. |
117 | 17 | vm_is_trailing_zeros = mm_shift == 1; |
118 | 17 | } else { |
119 | 6 | // mp = mv + 2, so it always has at least one trailing 0 bit. |
120 | 6 | vp -= 1; |
121 | 6 | } |
122 | 3.48k | } else if q < 31 { |
123 | 3.44k | // TODO(ulfjack): Use a tighter bound here. |
124 | 3.44k | vr_is_trailing_zeros = multiple_of_power_of_2_32(mv, q - 1); |
125 | 3.44k | } |
126 | | } |
127 | | |
128 | | // Step 4: Find the shortest decimal representation in the interval of valid representations. |
129 | 3.92k | let mut removed = 0i32; |
130 | 3.92k | let output = if vm_is_trailing_zeros || vr_is_trailing_zeros { |
131 | | // General case, which happens rarely (~4.0%). |
132 | 26.0k | while vp / 10 > vm / 10 { |
133 | 22.6k | vm_is_trailing_zeros &= vm - (vm / 10) * 10 == 0; |
134 | 22.6k | vr_is_trailing_zeros &= last_removed_digit == 0; |
135 | 22.6k | last_removed_digit = (vr % 10) as u8; |
136 | 22.6k | vr /= 10; |
137 | 22.6k | vp /= 10; |
138 | 22.6k | vm /= 10; |
139 | 22.6k | removed += 1; |
140 | 22.6k | } |
141 | 3.38k | if vm_is_trailing_zeros { |
142 | 90 | while vm % 10 == 0 { |
143 | 42 | vr_is_trailing_zeros &= last_removed_digit == 0; |
144 | 42 | last_removed_digit = (vr % 10) as u8; |
145 | 42 | vr /= 10; |
146 | 42 | vm /= 10; |
147 | 42 | removed += 1; |
148 | 42 | } |
149 | 3.33k | } |
150 | 3.38k | if vr_is_trailing_zeros && last_removed_digit == 5 && vr % 2 == 0 { |
151 | 1 | // Round even if the exact number is .....50..0. |
152 | 1 | last_removed_digit = 4; |
153 | 3.37k | } |
154 | | // We need to take vr + 1 if vr is outside bounds or we need to round up. |
155 | 3.38k | vr + ((vr == vm && (!accept_bounds || !vm_is_trailing_zeros)) || last_removed_digit >= 5) |
156 | | as u32 |
157 | | } else { |
158 | | // Specialized for the common case (~96.0%). Percentages below are relative to this. |
159 | | // Loop iterations below (approximately): |
160 | | // 0: 13.6%, 1: 70.7%, 2: 14.1%, 3: 1.39%, 4: 0.14%, 5+: 0.01% |
161 | 2.06k | while vp / 10 > vm / 10 { |
162 | 1.52k | last_removed_digit = (vr % 10) as u8; |
163 | 1.52k | vr /= 10; |
164 | 1.52k | vp /= 10; |
165 | 1.52k | vm /= 10; |
166 | 1.52k | removed += 1; |
167 | 1.52k | } |
168 | | // We need to take vr + 1 if vr is outside bounds or we need to round up. |
169 | 541 | vr + (vr == vm || last_removed_digit >= 5) as u32 |
170 | | }; |
171 | 3.92k | let exp = e10 + removed; |
172 | | |
173 | 3.92k | FloatingDecimal32 { |
174 | 3.92k | exponent: exp, |
175 | 3.92k | mantissa: output, |
176 | 3.92k | } |
177 | 3.92k | } |