ryu_floating_decimal-0.1.0/.cargo_vcs_info.json0000644000000001121366524517200153150ustar00{ "git": { "sha1": "c984b4118b165c9053394fa3b0fdc9d7eb998ce2" } } ryu_floating_decimal-0.1.0/.gitignore010066400017500001750000000001371366523644600161230ustar0000000000000000/target #Added by cargo # #already existing elements were commented out #/target Cargo.lock ryu_floating_decimal-0.1.0/Cargo.toml0000644000000020241366524517200133170ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "ryu_floating_decimal" version = "0.1.0" authors = ["Matt Torrence"] build = "build.rs" description = "Fast floating point to floating decimal conversion. See the crate 'ryu' for more details" documentation = "https://docs.rs/ryu-floating-decimal" readme = "README.md" license = "Apache-2.0" repository = "https://github.com/torrencem/ryu-floating-decimal" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] [dependencies.no-panic] version = "0.1" optional = true [features] small = [] ryu_floating_decimal-0.1.0/Cargo.toml.orig010066400017500001750000000014441366524463600170240ustar0000000000000000[package] name = "ryu_floating_decimal" version = "0.1.0" authors = ["Matt Torrence"] license = "Apache-2.0" description = "Fast floating point to floating decimal conversion. See the crate 'ryu' for more details" repository = "https://github.com/torrencem/ryu-floating-decimal" documentation = "https://docs.rs/ryu-floating-decimal" readme = "README.md" build = "build.rs" [features] # Use smaller lookup tables. Instead of storing every required power of # 5, only store every 26th entry, and compute intermediate values with a # multiplication. This reduces the lookup table size by about 10x (only # one case, and only f64) at the cost of some performance. small = [] [dependencies] no-panic = { version = "0.1", optional = true } [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] ryu_floating_decimal-0.1.0/LICENSE-APACHE010066400017500001750000000261351366523735400160640ustar0000000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ryu_floating_decimal-0.1.0/README.md010066400017500001750000000015701366524451000154030ustar0000000000000000# Ryu Floating Decimal This crate directly copies the internals of the [ryu rust crate](https://github.com/dtolnay/ryu), exposing some useful functions and types for more flexible float printing. This crate exposes the functions `d2d` and `f2d`, which convert from `f64` to `FloatingDecimal64` and `f32` to `FloatingDecimal32` respectively. These floating decimals can be converted to strings in a custom way. See [the original crate](https://github.com/dtolnay/ryu) for benchmarks and other information. ## Example ```rust use ryu_floating_decimal::f2d; let value: f32 = 12.091; let decimal = f2d(value); assert_eq!(decimal.mantissa, 12091); assert_eq!(decimal.exponent, -3); ``` ## License Licensed under [Apache License, Version 2.0](https://github.com/torrencem/ryu-floating-decimal/blob/master/LICENSE-APACHE), copied from [the original crate](https://github.com/dtolnay/ryu) ryu_floating_decimal-0.1.0/build.rs010066400017500001750000000036061366523737600156070ustar0000000000000000use std::env; use std::process::Command; use std::str::{self, FromStr}; // The rustc-cfg strings below are *not* public API. Please let us know by // opening a GitHub issue if your build environment requires some way to enable // these cfgs other than by executing our build script. fn main() { let minor = match rustc_minor_version() { Some(minor) => minor, None => return, }; let target = env::var("TARGET").unwrap(); let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten"; // 128-bit integers stabilized in Rust 1.26: // https://blog.rust-lang.org/2018/05/10/Rust-1.26.html // // Disabled on Emscripten targets as Emscripten doesn't // currently support integers larger than 64 bits. if minor >= 26 && !emscripten { println!("cargo:rustc-cfg=integer128"); } // #[must_use] on functions stabilized in Rust 1.27: // https://blog.rust-lang.org/2018/06/21/Rust-1.27.html if minor >= 27 { println!("cargo:rustc-cfg=must_use_return"); } // MaybeUninit stabilized in Rust 1.36: // https://blog.rust-lang.org/2019/07/04/Rust-1.36.0.html if minor >= 36 { println!("cargo:rustc-cfg=maybe_uninit"); } } fn rustc_minor_version() -> Option { let rustc = match env::var_os("RUSTC") { Some(rustc) => rustc, None => return None, }; let output = match Command::new(rustc).arg("--version").output() { Ok(output) => output, Err(_) => return None, }; let version = match str::from_utf8(&output.stdout) { Ok(version) => version, Err(_) => return None, }; let mut pieces = version.split('.'); if pieces.next() != Some("rustc 1") { return None; } let next = match pieces.next() { Some(next) => next, None => return None, }; u32::from_str(next).ok() } ryu_floating_decimal-0.1.0/src/common.rs010066400017500001750000000034271366524067500165640ustar0000000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. // Returns e == 0 ? 1 : ceil(log_2(5^e)). #[cfg_attr(feature = "no-panic", inline)] pub fn pow5bits(e: i32) -> i32 { // This approximation works up to the point that the multiplication overflows at e = 3529. // If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater // than 2^9297. debug_assert!(e >= 0); debug_assert!(e <= 3528); (((e as u32 * 1217359) >> 19) + 1) as i32 } // Returns floor(log_10(2^e)). #[cfg_attr(feature = "no-panic", inline)] pub fn log10_pow2(e: i32) -> u32 { // The first value this approximation fails for is 2^1651 which is just greater than 10^297. debug_assert!(e >= 0); debug_assert!(e <= 1650); (e as u32 * 78913) >> 18 } // Returns floor(log_10(5^e)). #[cfg_attr(feature = "no-panic", inline)] pub fn log10_pow5(e: i32) -> u32 { // The first value this approximation fails for is 5^2621 which is just greater than 10^1832. debug_assert!(e >= 0); debug_assert!(e <= 2620); (e as u32 * 732923) >> 20 } ryu_floating_decimal-0.1.0/src/d2s.rs010066400017500001750000000317261366524114400157570ustar0000000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. use core::ptr; #[cfg(maybe_uninit)] use core::mem::MaybeUninit; #[cfg(not(maybe_uninit))] use core::mem; use common::*; #[cfg(not(feature = "small"))] use d2s_full_table::*; use d2s_intrinsics::*; #[cfg(feature = "small")] use d2s_small_table::*; pub const DOUBLE_MANTISSA_BITS: u32 = 52; pub const DOUBLE_EXPONENT_BITS: u32 = 11; const DOUBLE_BIAS: i32 = 1023; const DOUBLE_POW5_INV_BITCOUNT: i32 = 122; const DOUBLE_POW5_BITCOUNT: i32 = 121; #[cfg(integer128)] #[cfg_attr(feature = "no-panic", inline)] fn mul_shift(m: u64, mul: &(u64, u64), j: u32) -> u64 { let b0 = m as u128 * mul.0 as u128; let b2 = m as u128 * mul.1 as u128; (((b0 >> 64) + b2) >> (j - 64)) as u64 } #[cfg(integer128)] #[cfg_attr(feature = "no-panic", inline)] unsafe fn mul_shift_all( m: u64, mul: &(u64, u64), j: u32, vp: *mut u64, vm: *mut u64, mm_shift: u32, ) -> u64 { ptr::write(vp, mul_shift(4 * m + 2, mul, j)); ptr::write(vm, mul_shift(4 * m - 1 - mm_shift as u64, mul, j)); mul_shift(4 * m, mul, j) } #[cfg(not(integer128))] #[cfg_attr(feature = "no-panic", inline)] unsafe fn mul_shift_all( mut m: u64, mul: &(u64, u64), j: u32, vp: *mut u64, vm: *mut u64, mm_shift: u32, ) -> u64 { m <<= 1; // m is maximum 55 bits let (lo, tmp) = umul128(m, mul.0); let (mut mid, mut hi) = umul128(m, mul.1); mid = mid.wrapping_add(tmp); hi = hi.wrapping_add((mid < tmp) as u64); // overflow into hi let lo2 = lo.wrapping_add(mul.0); let mid2 = mid.wrapping_add(mul.1).wrapping_add((lo2 < lo) as u64); let hi2 = hi.wrapping_add((mid2 < mid) as u64); ptr::write(vp, shiftright128(mid2, hi2, j - 64 - 1)); if mm_shift == 1 { let lo3 = lo.wrapping_sub(mul.0); let mid3 = mid.wrapping_sub(mul.1).wrapping_sub((lo3 > lo) as u64); let hi3 = hi.wrapping_sub((mid3 > mid) as u64); ptr::write(vm, shiftright128(mid3, hi3, j - 64 - 1)); } else { let lo3 = lo + lo; let mid3 = mid.wrapping_add(mid).wrapping_add((lo3 < lo) as u64); let hi3 = hi.wrapping_add(hi).wrapping_add((mid3 < mid) as u64); let lo4 = lo3.wrapping_sub(mul.0); let mid4 = mid3.wrapping_sub(mul.1).wrapping_sub((lo4 > lo3) as u64); let hi4 = hi3.wrapping_sub((mid4 > mid3) as u64); ptr::write(vm, shiftright128(mid4, hi4, j - 64)); } shiftright128(mid, hi, j - 64 - 1) } /// A floating decimal representing mantissa * 10^exponent pub struct FloatingDecimal64 { pub mantissa: u64, // Decimal exponent's range is -324 to 308 // inclusive, and can fit in i16 if needed. pub exponent: i32, } #[cfg_attr(feature = "no-panic", inline)] pub fn d2d(ieee_mantissa: u64, ieee_exponent: u32) -> FloatingDecimal64 { let (e2, m2) = if ieee_exponent == 0 { ( // We subtract 2 so that the bounds computation has 2 additional bits. 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS as i32 - 2, ieee_mantissa, ) } else { ( ieee_exponent as i32 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS as i32 - 2, (1u64 << DOUBLE_MANTISSA_BITS) | ieee_mantissa, ) }; let even = (m2 & 1) == 0; let accept_bounds = even; // Step 2: Determine the interval of valid decimal representations. let mv = 4 * m2; // Implicit bool -> int conversion. True is 1, false is 0. let mm_shift = (ieee_mantissa != 0 || ieee_exponent <= 1) as u32; // We would compute mp and mm like this: // uint64_t mp = 4 * m2 + 2; // uint64_t mm = mv - 1 - mm_shift; // Step 3: Convert to a decimal power base using 128-bit arithmetic. let mut vr: u64; let mut vp: u64; let mut vm: u64; #[cfg(not(maybe_uninit))] { vp = unsafe { mem::uninitialized() }; vm = unsafe { mem::uninitialized() }; } #[cfg(maybe_uninit)] let mut vp_uninit: MaybeUninit = MaybeUninit::uninit(); #[cfg(maybe_uninit)] let mut vm_uninit: MaybeUninit = MaybeUninit::uninit(); let e10: i32; let mut vm_is_trailing_zeros = false; let mut vr_is_trailing_zeros = false; if e2 >= 0 { // I tried special-casing q == 0, but there was no effect on performance. // This expression is slightly faster than max(0, log10_pow2(e2) - 1). let q = log10_pow2(e2) - (e2 > 3) as u32; e10 = q as i32; let k = DOUBLE_POW5_INV_BITCOUNT + pow5bits(q as i32) - 1; let i = -e2 + q as i32 + k; vr = unsafe { mul_shift_all( m2, #[cfg(feature = "small")] &compute_inv_pow5(q), #[cfg(not(feature = "small"))] { debug_assert!(q < DOUBLE_POW5_INV_SPLIT.len() as u32); DOUBLE_POW5_INV_SPLIT.get_unchecked(q as usize) }, i as u32, #[cfg(maybe_uninit)] { vp_uninit.as_mut_ptr() }, #[cfg(not(maybe_uninit))] { &mut vp }, #[cfg(maybe_uninit)] { vm_uninit.as_mut_ptr() }, #[cfg(not(maybe_uninit))] { &mut vm }, mm_shift, ) }; #[cfg(maybe_uninit)] { vp = unsafe { vp_uninit.assume_init() }; vm = unsafe { vm_uninit.assume_init() }; } if q <= 21 { // This should use q <= 22, but I think 21 is also safe. Smaller values // may still be safe, but it's more difficult to reason about them. // Only one of mp, mv, and mm can be a multiple of 5, if any. let mv_mod5 = (mv as u32).wrapping_sub(5u32.wrapping_mul(div5(mv) as u32)); if mv_mod5 == 0 { vr_is_trailing_zeros = multiple_of_power_of_5(mv, q); } else if accept_bounds { // Same as min(e2 + (~mm & 1), pow5_factor(mm)) >= q // <=> e2 + (~mm & 1) >= q && pow5_factor(mm) >= q // <=> true && pow5_factor(mm) >= q, since e2 >= q. vm_is_trailing_zeros = multiple_of_power_of_5(mv - 1 - mm_shift as u64, q); } else { // Same as min(e2 + 1, pow5_factor(mp)) >= q. vp -= multiple_of_power_of_5(mv + 2, q) as u64; } } } else { // This expression is slightly faster than max(0, log10_pow5(-e2) - 1). let q = log10_pow5(-e2) - (-e2 > 1) as u32; e10 = q as i32 + e2; let i = -e2 - q as i32; let k = pow5bits(i) - DOUBLE_POW5_BITCOUNT; let j = q as i32 - k; vr = unsafe { mul_shift_all( m2, #[cfg(feature = "small")] &compute_pow5(i as u32), #[cfg(not(feature = "small"))] { debug_assert!(i < DOUBLE_POW5_SPLIT.len() as i32); DOUBLE_POW5_SPLIT.get_unchecked(i as usize) }, j as u32, #[cfg(maybe_uninit)] { vp_uninit.as_mut_ptr() }, #[cfg(not(maybe_uninit))] { &mut vp }, #[cfg(maybe_uninit)] { vm_uninit.as_mut_ptr() }, #[cfg(not(maybe_uninit))] { &mut vm }, mm_shift, ) }; #[cfg(maybe_uninit)] { vp = unsafe { vp_uninit.assume_init() }; vm = unsafe { vm_uninit.assume_init() }; } if q <= 1 { // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits. // mv = 4 * m2, so it always has at least two trailing 0 bits. vr_is_trailing_zeros = true; if accept_bounds { // mm = mv - 1 - mm_shift, so it has 1 trailing 0 bit iff mm_shift == 1. vm_is_trailing_zeros = mm_shift == 1; } else { // mp = mv + 2, so it always has at least one trailing 0 bit. vp -= 1; } } else if q < 63 { // TODO(ulfjack): Use a tighter bound here. // We want to know if the full product has at least q trailing zeros. // We need to compute min(p2(mv), p5(mv) - e2) >= q // <=> p2(mv) >= q && p5(mv) - e2 >= q // <=> p2(mv) >= q (because -e2 >= q) vr_is_trailing_zeros = multiple_of_power_of_2(mv, q); } } // Step 4: Find the shortest decimal representation in the interval of valid representations. let mut removed = 0i32; let mut last_removed_digit = 0u8; // On average, we remove ~2 digits. let output = if vm_is_trailing_zeros || vr_is_trailing_zeros { // General case, which happens rarely (~0.7%). loop { let vp_div10 = div10(vp); let vm_div10 = div10(vm); if vp_div10 <= vm_div10 { break; } let vm_mod10 = (vm as u32).wrapping_sub(10u32.wrapping_mul(vm_div10 as u32)); let vr_div10 = div10(vr); let vr_mod10 = (vr as u32).wrapping_sub(10u32.wrapping_mul(vr_div10 as u32)); vm_is_trailing_zeros &= vm_mod10 == 0; vr_is_trailing_zeros &= last_removed_digit == 0; last_removed_digit = vr_mod10 as u8; vr = vr_div10; vp = vp_div10; vm = vm_div10; removed += 1; } if vm_is_trailing_zeros { loop { let vm_div10 = div10(vm); let vm_mod10 = (vm as u32).wrapping_sub(10u32.wrapping_mul(vm_div10 as u32)); if vm_mod10 != 0 { break; } let vp_div10 = div10(vp); let vr_div10 = div10(vr); let vr_mod10 = (vr as u32).wrapping_sub(10u32.wrapping_mul(vr_div10 as u32)); vr_is_trailing_zeros &= last_removed_digit == 0; last_removed_digit = vr_mod10 as u8; vr = vr_div10; vp = vp_div10; vm = vm_div10; removed += 1; } } if vr_is_trailing_zeros && last_removed_digit == 5 && vr % 2 == 0 { // Round even if the exact number is .....50..0. last_removed_digit = 4; } // We need to take vr + 1 if vr is outside bounds or we need to round up. vr + ((vr == vm && (!accept_bounds || !vm_is_trailing_zeros)) || last_removed_digit >= 5) as u64 } else { // Specialized for the common case (~99.3%). Percentages below are relative to this. let mut round_up = false; let vp_div100 = div100(vp); let vm_div100 = div100(vm); // Optimization: remove two digits at a time (~86.2%). if vp_div100 > vm_div100 { let vr_div100 = div100(vr); let vr_mod100 = (vr as u32).wrapping_sub(100u32.wrapping_mul(vr_div100 as u32)); round_up = vr_mod100 >= 50; vr = vr_div100; vp = vp_div100; vm = vm_div100; removed += 2; } // Loop iterations below (approximately), without optimization above: // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02% // Loop iterations below (approximately), with optimization above: // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02% loop { let vp_div10 = div10(vp); let vm_div10 = div10(vm); if vp_div10 <= vm_div10 { break; } let vr_div10 = div10(vr); let vr_mod10 = (vr as u32).wrapping_sub(10u32.wrapping_mul(vr_div10 as u32)); round_up = vr_mod10 >= 5; vr = vr_div10; vp = vp_div10; vm = vm_div10; removed += 1; } // We need to take vr + 1 if vr is outside bounds or we need to round up. vr + (vr == vm || round_up) as u64 }; let exp = e10 + removed; FloatingDecimal64 { exponent: exp, mantissa: output, } } ryu_floating_decimal-0.1.0/src/d2s_full_table.rs010066400017500001750000000716601366523746100201600ustar0000000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. pub static DOUBLE_POW5_INV_SPLIT: [(u64, u64); 292] = [ (1, 288230376151711744), (3689348814741910324, 230584300921369395), (2951479051793528259, 184467440737095516), (17118578500402463900, 147573952589676412), (12632330341676300947, 236118324143482260), (10105864273341040758, 188894659314785808), (15463389048156653253, 151115727451828646), (17362724847566824558, 241785163922925834), (17579528692795369969, 193428131138340667), (6684925324752475329, 154742504910672534), (18074578149087781173, 247588007857076054), (18149011334012135262, 198070406285660843), (3451162622983977240, 158456325028528675), (5521860196774363583, 253530120045645880), (4417488157419490867, 202824096036516704), (7223339340677503017, 162259276829213363), (7867994130342094503, 259614842926741381), (2605046489531765280, 207691874341393105), (2084037191625412224, 166153499473114484), (10713157136084480204, 265845599156983174), (12259874523609494487, 212676479325586539), (13497248433629505913, 170141183460469231), (14216899864323388813, 272225893536750770), (11373519891458711051, 217780714829400616), (5409467098425058518, 174224571863520493), (4965798542738183305, 278759314981632789), (7661987648932456967, 223007451985306231), (2440241304404055250, 178405961588244985), (3904386087046488400, 285449538541191976), (17880904128604832013, 228359630832953580), (14304723302883865611, 182687704666362864), (15133127457049002812, 146150163733090291), (16834306301794583852, 233840261972944466), (9778096226693756759, 187072209578355573), (15201174610838826053, 149657767662684458), (2185786488890659746, 239452428260295134), (5437978005854438120, 191561942608236107), (15418428848909281466, 153249554086588885), (6222742084545298729, 245199286538542217), (16046240111861969953, 196159429230833773), (1768945645263844993, 156927543384667019), (10209010661905972635, 251084069415467230), (8167208529524778108, 200867255532373784), (10223115638361732810, 160693804425899027), (1599589762411131202, 257110087081438444), (4969020624670815285, 205688069665150755), (3975216499736652228, 164550455732120604), (13739044029062464211, 263280729171392966), (7301886408508061046, 210624583337114373), (13220206756290269483, 168499666669691498), (17462981995322520850, 269599466671506397), (6591687966774196033, 215679573337205118), (12652048002903177473, 172543658669764094), (9175230360419352987, 276069853871622551), (3650835473593572067, 220855883097298041), (17678063637842498946, 176684706477838432), (13527506561580357021, 282695530364541492), (3443307619780464970, 226156424291633194), (6443994910566282300, 180925139433306555), (5155195928453025840, 144740111546645244), (15627011115008661990, 231584178474632390), (12501608892006929592, 185267342779705912), (2622589484121723027, 148213874223764730), (4196143174594756843, 237142198758023568), (10735612169159626121, 189713759006418854), (12277838550069611220, 151771007205135083), (15955192865369467629, 242833611528216133), (1696107848069843133, 194266889222572907), (12424932722681605476, 155413511378058325), (1433148282581017146, 248661618204893321), (15903913885032455010, 198929294563914656), (9033782293284053685, 159143435651131725), (14454051669254485895, 254629497041810760), (11563241335403588716, 203703597633448608), (16629290697806691620, 162962878106758886), (781423413297334329, 260740604970814219), (4314487545379777786, 208592483976651375), (3451590036303822229, 166873987181321100), (5522544058086115566, 266998379490113760), (4418035246468892453, 213598703592091008), (10913125826658934609, 170878962873672806), (10082303693170474728, 273406340597876490), (8065842954536379782, 218725072478301192), (17520720807854834795, 174980057982640953), (5897060404116273733, 279968092772225526), (1028299508551108663, 223974474217780421), (15580034865808528224, 179179579374224336), (17549358155809824511, 286687326998758938), (2971440080422128639, 229349861599007151), (17134547323305344204, 183479889279205720), (13707637858644275364, 146783911423364576), (14553522944347019935, 234854258277383322), (4264120725993795302, 187883406621906658), (10789994210278856888, 150306725297525326), (9885293106962350374, 240490760476040522), (529536856086059653, 192392608380832418), (7802327114352668369, 153914086704665934), (1415676938738538420, 246262538727465495), (1132541550990830736, 197010030981972396), (15663428499760305882, 157608024785577916), (17682787970132668764, 252172839656924666), (10456881561364224688, 201738271725539733), (15744202878575200397, 161390617380431786), (17812026976236499989, 258224987808690858), (3181575136763469022, 206579990246952687), (13613306553636506187, 165263992197562149), (10713244041592678929, 264422387516099439), (12259944048016053467, 211537910012879551), (6118606423670932450, 169230328010303641), (2411072648389671274, 270768524816485826), (16686253377679378312, 216614819853188660), (13349002702143502650, 173291855882550928), (17669055508687693916, 277266969412081485), (14135244406950155133, 221813575529665188), (240149081334393137, 177450860423732151), (11452284974360759988, 283921376677971441), (5472479164746697667, 227137101342377153), (11756680961281178780, 181709681073901722), (2026647139541122378, 145367744859121378), (18000030682233437097, 232588391774594204), (18089373360528660001, 186070713419675363), (3403452244197197031, 148856570735740291), (16513570034941246220, 238170513177184465), (13210856027952996976, 190536410541747572), (3189987192878576934, 152429128433398058), (1414630693863812771, 243886605493436893), (8510402184574870864, 195109284394749514), (10497670562401807014, 156087427515799611), (9417575270359070576, 249739884025279378), (14912757845771077107, 199791907220223502), (4551508647133041040, 159833525776178802), (10971762650154775986, 255733641241886083), (16156107749607641435, 204586912993508866), (9235537384944202825, 163669530394807093), (11087511001168814197, 261871248631691349), (12559357615676961681, 209496998905353079), (13736834907283479668, 167597599124282463), (18289587036911657145, 268156158598851941), (10942320814787415393, 214524926879081553), (16132554281313752961, 171619941503265242), (11054691591134363444, 274591906405224388), (16222450902391311402, 219673525124179510), (12977960721913049122, 175738820099343608), (17075388340318968271, 281182112158949773), (2592264228029443648, 224945689727159819), (5763160197165465241, 179956551781727855), (9221056315464744386, 287930482850764568), (14755542681855616155, 230344386280611654), (15493782960226403247, 184275509024489323), (1326979923955391628, 147420407219591459), (9501865507812447252, 235872651551346334), (11290841220991868125, 188698121241077067), (1653975347309673853, 150958496992861654), (10025058185179298811, 241533595188578646), (4330697733401528726, 193226876150862917), (14532604630946953951, 154581500920690333), (1116074521063664381, 247330401473104534), (4582208431592841828, 197864321178483627), (14733813189500004432, 158291456942786901), (16195403473716186445, 253266331108459042), (5577625149489128510, 202613064886767234), (8151448934333213131, 162090451909413787), (16731667109675051333, 259344723055062059), (17074682502481951390, 207475778444049647), (6281048372501740465, 165980622755239718), (6360328581260874421, 265568996408383549), (8777611679750609860, 212455197126706839), (10711438158542398211, 169964157701365471), (9759603424184016492, 271942652322184754), (11497031554089123517, 217554121857747803), (16576322872755119460, 174043297486198242), (11764721337440549842, 278469275977917188), (16790474699436260520, 222775420782333750), (13432379759549008416, 178220336625867000), (3045063541568861850, 285152538601387201), (17193446092222730773, 228122030881109760), (13754756873778184618, 182497624704887808), (18382503128506368341, 145998099763910246), (3586563302416817083, 233596959622256395), (2869250641933453667, 186877567697805116), (17052795772514404226, 149502054158244092), (12527077977055405469, 239203286653190548), (17400360011128145022, 191362629322552438), (2852241564676785048, 153090103458041951), (15631632947708587046, 244944165532867121), (8815957543424959314, 195955332426293697), (18120812478965698421, 156764265941034957), (14235904707377476180, 250822825505655932), (4010026136418160298, 200658260404524746), (17965416168102169531, 160526608323619796), (2919224165770098987, 256842573317791675), (2335379332616079190, 205474058654233340), (1868303466092863352, 164379246923386672), (6678634360490491686, 263006795077418675), (5342907488392393349, 210405436061934940), (4274325990713914679, 168324348849547952), (10528270399884173809, 269318958159276723), (15801313949391159694, 215455166527421378), (1573004715287196786, 172364133221937103), (17274202803427156150, 275782613155099364), (17508711057483635243, 220626090524079491), (10317620031244997871, 176500872419263593), (12818843235250086271, 282401395870821749), (13944423402941979340, 225921116696657399), (14844887537095493795, 180736893357325919), (15565258844418305359, 144589514685860735), (6457670077359736959, 231343223497377177), (16234182506113520537, 185074578797901741), (9297997190148906106, 148059663038321393), (11187446689496339446, 236895460861314229), (12639306166338981880, 189516368689051383), (17490142562555006151, 151613094951241106), (2158786396894637579, 242580951921985771), (16484424376483351356, 194064761537588616), (9498190686444770762, 155251809230070893), (11507756283569722895, 248402894768113429), (12895553841597688639, 198722315814490743), (17695140702761971558, 158977852651592594), (17244178680193423523, 254364564242548151), (10105994129412828495, 203491651394038521), (4395446488788352473, 162793321115230817), (10722063196803274280, 260469313784369307), (1198952927958798777, 208375451027495446), (15716557601334680315, 166700360821996356), (17767794532651667857, 266720577315194170), (14214235626121334286, 213376461852155336), (7682039686155157106, 170701169481724269), (1223217053622520399, 273121871170758831), (15735968901865657612, 218497496936607064), (16278123936234436413, 174797997549285651), (219556594781725998, 279676796078857043), (7554342905309201445, 223741436863085634), (9732823138989271479, 178993149490468507), (815121763415193074, 286389039184749612), (11720143854957885429, 229111231347799689), (13065463898708218666, 183288985078239751), (6763022304224664610, 146631188062591801), (3442138057275642729, 234609900900146882), (13821756890046245153, 187687920720117505), (11057405512036996122, 150150336576094004), (6623802375033462826, 240240538521750407), (16367088344252501231, 192192430817400325), (13093670675402000985, 153753944653920260), (2503129006933649959, 246006311446272417), (13070549649772650937, 196805049157017933), (17835137349301941396, 157444039325614346), (2710778055689733971, 251910462920982955), (2168622444551787177, 201528370336786364), (5424246770383340065, 161222696269429091), (1300097203129523457, 257956314031086546), (15797473021471260058, 206365051224869236), (8948629602435097724, 165092040979895389), (3249760919670425388, 264147265567832623), (9978506365220160957, 211317812454266098), (15361502721659949412, 169054249963412878), (2442311466204457120, 270486799941460606), (16711244431931206989, 216389439953168484), (17058344360286875914, 173111551962534787), (12535955717491360170, 276978483140055660), (10028764573993088136, 221582786512044528), (15401709288678291155, 177266229209635622), (9885339602917624555, 283625966735416996), (4218922867592189321, 226900773388333597), (14443184738299482427, 181520618710666877), (4175850161155765295, 145216494968533502), (10370709072591134795, 232346391949653603), (15675264887556728482, 185877113559722882), (5161514280561562140, 148701690847778306), (879725219414678777, 237922705356445290), (703780175531743021, 190338164285156232), (11631070584651125387, 152270531428124985), (162968861732249003, 243632850284999977), (11198421533611530172, 194906280227999981), (5269388412147313814, 155925024182399985), (8431021459435702103, 249480038691839976), (3055468352806651359, 199584030953471981), (17201769941212962380, 159667224762777584), (16454785461715008838, 255467559620444135), (13163828369372007071, 204374047696355308), (17909760324981426303, 163499238157084246), (2830174816776909822, 261598781051334795), (2264139853421527858, 209279024841067836), (16568707141704863579, 167423219872854268), (4373838538276319787, 267877151796566830), (3499070830621055830, 214301721437253464), (6488605479238754987, 171441377149802771), (3003071137298187333, 274306203439684434), (6091805724580460189, 219444962751747547), (15941491023890099121, 175555970201398037), (10748990379256517301, 280889552322236860), (8599192303405213841, 224711641857789488), (14258051472207991719, 179769313486231590), ]; pub static DOUBLE_POW5_SPLIT: [(u64, u64); 326] = [ (0, 72057594037927936), (0, 90071992547409920), (0, 112589990684262400), (0, 140737488355328000), (0, 87960930222080000), (0, 109951162777600000), (0, 137438953472000000), (0, 85899345920000000), (0, 107374182400000000), (0, 134217728000000000), (0, 83886080000000000), (0, 104857600000000000), (0, 131072000000000000), (0, 81920000000000000), (0, 102400000000000000), (0, 128000000000000000), (0, 80000000000000000), (0, 100000000000000000), (0, 125000000000000000), (0, 78125000000000000), (0, 97656250000000000), (0, 122070312500000000), (0, 76293945312500000), (0, 95367431640625000), (0, 119209289550781250), (4611686018427387904, 74505805969238281), (10376293541461622784, 93132257461547851), (8358680908399640576, 116415321826934814), (612489549322387456, 72759576141834259), (14600669991935148032, 90949470177292823), (13639151471491547136, 113686837721616029), (3213881284082270208, 142108547152020037), (4314518811765112832, 88817841970012523), (781462496279003136, 111022302462515654), (10200200157203529728, 138777878078144567), (13292654125893287936, 86736173798840354), (7392445620511834112, 108420217248550443), (4628871007212404736, 135525271560688054), (16728102434789916672, 84703294725430033), (7075069988205232128, 105879118406787542), (18067209522111315968, 132348898008484427), (8986162942105878528, 82718061255302767), (6621017659204960256, 103397576569128459), (3664586055578812416, 129246970711410574), (16125424340018921472, 80779356694631608), (1710036351314100224, 100974195868289511), (15972603494424788992, 126217744835361888), (9982877184015493120, 78886090522101180), (12478596480019366400, 98607613152626475), (10986559581596820096, 123259516440783094), (2254913720070624656, 77037197775489434), (12042014186943056628, 96296497219361792), (15052517733678820785, 120370621524202240), (9407823583549262990, 75231638452626400), (11759779479436578738, 94039548065783000), (14699724349295723422, 117549435082228750), (4575641699882439235, 73468396926392969), (10331238143280436948, 91835496157991211), (8302361660673158281, 114794370197489014), (1154580038986672043, 143492962746861268), (9944984561221445835, 89683101716788292), (12431230701526807293, 112103877145985365), (1703980321626345405, 140129846432481707), (17205888765512323542, 87581154020301066), (12283988920035628619, 109476442525376333), (1519928094762372062, 136845553156720417), (12479170105294952299, 85528470722950260), (15598962631618690374, 106910588403687825), (5663645234241199255, 133638235504609782), (17374836326682913246, 83523897190381113), (7883487353071477846, 104404871487976392), (9854359191339347308, 130506089359970490), (10770660513014479971, 81566305849981556), (13463325641268099964, 101957882312476945), (2994098996302961243, 127447352890596182), (15706369927971514489, 79654595556622613), (5797904354682229399, 99568244445778267), (2635694424925398845, 124460305557222834), (6258995034005762182, 77787690973264271), (3212057774079814824, 97234613716580339), (17850130272881932242, 121543267145725423), (18073860448192289507, 75964541966078389), (8757267504958198172, 94955677457597987), (6334898362770359811, 118694596821997484), (13182683513586250689, 74184123013748427), (11866668373555425458, 92730153767185534), (5609963430089506015, 115912692208981918), (17341285199088104971, 72445432630613698), (12453234462005355406, 90556790788267123), (10954857059079306353, 113195988485333904), (13693571323849132942, 141494985606667380), (17781854114260483896, 88434366004167112), (3780573569116053255, 110542957505208891), (114030942967678664, 138178696881511114), (4682955357782187069, 86361685550944446), (15077066234082509644, 107952106938680557), (5011274737320973344, 134940133673350697), (14661261756894078100, 84337583545844185), (4491519140835433913, 105421979432305232), (5614398926044292391, 131777474290381540), (12732371365632458552, 82360921431488462), (6692092170185797382, 102951151789360578), (17588487249587022536, 128688939736700722), (15604490549419276989, 80430587335437951), (14893927168346708332, 100538234169297439), (14005722942005997511, 125672792711621799), (15671105866394830300, 78545495444763624), (1142138259283986260, 98181869305954531), (15262730879387146537, 122727336632443163), (7233363790403272633, 76704585395276977), (13653390756431478696, 95880731744096221), (3231680390257184658, 119850914680120277), (4325643253124434363, 74906821675075173), (10018740084832930858, 93633527093843966), (3300053069186387764, 117041908867304958), (15897591223523656064, 73151193042065598), (10648616992549794273, 91438991302581998), (4087399203832467033, 114298739128227498), (14332621041645359599, 142873423910284372), (18181260187883125557, 89295889943927732), (4279831161144355331, 111619862429909666), (14573160988285219972, 139524828037387082), (13719911636105650386, 87203017523366926), (7926517508277287175, 109003771904208658), (684774848491833161, 136254714880260823), (7345513307948477581, 85159196800163014), (18405263671790372785, 106448996000203767), (18394893571310578077, 133061245000254709), (13802651491282805250, 83163278125159193), (3418256308821342851, 103954097656448992), (4272820386026678563, 129942622070561240), (2670512741266674102, 81214138794100775), (17173198981865506339, 101517673492625968), (3019754653622331308, 126897091865782461), (4193189667727651020, 79310682416114038), (14464859121514339583, 99138353020142547), (13469387883465536574, 123922941275178184), (8418367427165960359, 77451838296986365), (15134645302384838353, 96814797871232956), (471562554271496325, 121018497339041196), (9518098633274461011, 75636560836900747), (7285937273165688360, 94545701046125934), (18330793628311886258, 118182126307657417), (4539216990053847055, 73863828942285886), (14897393274422084627, 92329786177857357), (4786683537745442072, 115412232722321697), (14520892257159371055, 72132645451451060), (18151115321449213818, 90165806814313825), (8853836096529353561, 112707258517892282), (1843923083806916143, 140884073147365353), (12681666973447792349, 88052545717103345), (2017025661527576725, 110065682146379182), (11744654113764246714, 137582102682973977), (422879793461572340, 85988814176858736), (528599741826965425, 107486017721073420), (660749677283706782, 134357522151341775), (7330497575943398595, 83973451344588609), (13774807988356636147, 104966814180735761), (3383451930163631472, 131208517725919702), (15949715511634433382, 82005323578699813), (6102086334260878016, 102506654473374767), (3015921899398709616, 128133318091718459), (18025852251620051174, 80083323807324036), (4085571240815512351, 100104154759155046), (14330336087874166247, 125130193448943807), (15873989082562435760, 78206370905589879), (15230800334775656796, 97757963631987349), (5203442363187407284, 122197454539984187), (946308467778435600, 76373409087490117), (5794571603150432404, 95466761359362646), (16466586540792816313, 119333451699203307), (7985773578781816244, 74583407312002067), (5370530955049882401, 93229259140002584), (6713163693812353001, 116536573925003230), (18030785363914884337, 72835358703127018), (13315109668038829614, 91044198378908773), (2808829029766373305, 113805247973635967), (17346094342490130344, 142256559967044958), (6229622945628943561, 88910349979403099), (3175342663608791547, 111137937474253874), (13192550366365765242, 138922421842817342), (3633657960551215372, 86826513651760839), (18377130505971182927, 108533142064701048), (4524669058754427043, 135666427580876311), (9745447189362598758, 84791517238047694), (2958436949848472639, 105989396547559618), (12921418224165366607, 132486745684449522), (12687572408530742033, 82804216052780951), (11247779492236039638, 103505270065976189), (224666310012885835, 129381587582470237), (2446259452971747599, 80863492239043898), (12281196353069460307, 101079365298804872), (15351495441336825384, 126349206623506090), (14206370669262903769, 78968254139691306), (8534591299723853903, 98710317674614133), (15279925143082205283, 123387897093267666), (14161639232853766206, 77117435683292291), (13090363022639819853, 96396794604115364), (16362953778299774816, 120495993255144205), (12532689120651053212, 75309995784465128), (15665861400813816515, 94137494730581410), (10358954714162494836, 117671868413226763), (4168503687137865320, 73544917758266727), (598943590494943747, 91931147197833409), (5360365506546067587, 114913933997291761), (11312142901609972388, 143642417496614701), (9375932322719926695, 89776510935384188), (11719915403399908368, 112220638669230235), (10038208235822497557, 140275798336537794), (10885566165816448877, 87672373960336121), (18218643725697949000, 109590467450420151), (18161618638695048346, 136988084313025189), (13656854658398099168, 85617552695640743), (12459382304570236056, 107021940869550929), (1739169825430631358, 133777426086938662), (14922039196176308311, 83610891304336663), (14040862976792997485, 104513614130420829), (3716020665709083144, 130642017663026037), (4628355925281870917, 81651261039391273), (10397130925029726550, 102064076299239091), (8384727637859770284, 127580095374048864), (5240454773662356427, 79737559608780540), (6550568467077945534, 99671949510975675), (3576524565420044014, 124589936888719594), (6847013871814915412, 77868710555449746), (17782139376623420074, 97335888194312182), (13004302183924499284, 121669860242890228), (17351060901807587860, 76043662651806392), (3242082053549933210, 95054578314757991), (17887660622219580224, 118818222893447488), (11179787888887237640, 74261389308404680), (13974734861109047050, 92826736635505850), (8245046539531533005, 116033420794382313), (16682369133275677888, 72520887996488945), (7017903361312433648, 90651109995611182), (17995751238495317868, 113313887494513977), (8659630992836983623, 141642359368142472), (5412269370523114764, 88526474605089045), (11377022731581281359, 110658093256361306), (4997906377621825891, 138322616570451633), (14652906532082110942, 86451635356532270), (9092761128247862869, 108064544195665338), (2142579373455052779, 135080680244581673), (12868327154477877747, 84425425152863545), (2250350887815183471, 105531781441079432), (2812938609768979339, 131914726801349290), (6369772649532999991, 82446704250843306), (17185587848771025797, 103058380313554132), (3035240737254230630, 128822975391942666), (6508711479211282048, 80514359619964166), (17359261385868878368, 100642949524955207), (17087390713908710056, 125803686906194009), (3762090168551861929, 78627304316371256), (4702612710689827411, 98284130395464070), (15101637925217060072, 122855162994330087), (16356052730901744401, 76784476871456304), (1998321839917628885, 95980596089320381), (7109588318324424010, 119975745111650476), (13666864735807540814, 74984840694781547), (12471894901332038114, 93731050868476934), (6366496589810271835, 117163813585596168), (3979060368631419896, 73227383490997605), (9585511479216662775, 91534229363747006), (2758517312166052660, 114417786704683758), (12671518677062341634, 143022233380854697), (1002170145522881665, 89388895863034186), (10476084718758377889, 111736119828792732), (13095105898447972362, 139670149785990915), (5878598177316288774, 87293843616244322), (16571619758500136775, 109117304520305402), (11491152661270395161, 136396630650381753), (264441385652915120, 85247894156488596), (330551732066143900, 106559867695610745), (5024875683510067779, 133199834619513431), (10058076329834874218, 83249896637195894), (3349223375438816964, 104062370796494868), (4186529219298521205, 130077963495618585), (14145795808130045513, 81298727184761615), (13070558741735168987, 101623408980952019), (11726512408741573330, 127029261226190024), (7329070255463483331, 79393288266368765), (13773023837756742068, 99241610332960956), (17216279797195927585, 124052012916201195), (8454331864033760789, 77532508072625747), (5956228811614813082, 96915635090782184), (7445286014518516353, 121144543863477730), (9264989777501460624, 75715339914673581), (16192923240304213684, 94644174893341976), (1794409976670715490, 118305218616677471), (8039035263060279037, 73940761635423419), (5437108060397960892, 92425952044279274), (16019757112352226923, 115532440055349092), (788976158365366019, 72207775034593183), (14821278253238871236, 90259718793241478), (9303225779693813237, 112824648491551848), (11629032224617266546, 141030810614439810), (11879831158813179495, 88144256634024881), (1014730893234310657, 110180320792531102), (10491785653397664129, 137725400990663877), (8863209042587234033, 86078375619164923), (6467325284806654637, 107597969523956154), (17307528642863094104, 134497461904945192), (10817205401789433815, 84060913690590745), (18133192770664180173, 105076142113238431), (18054804944902837312, 131345177641548039), (18201782118205355176, 82090736025967524), (4305483574047142354, 102613420032459406), (14605226504413703751, 128266775040574257), (2210737537617482988, 80166734400358911), (16598479977304017447, 100208418000448638), (11524727934775246001, 125260522500560798), (2591268940807140847, 78287826562850499), (17074144231291089770, 97859783203563123), (16730994270686474309, 122324729004453904), (10456871419179046443, 76452955627783690), (3847717237119032246, 95566194534729613), (9421332564826178211, 119457743168412016), (5888332853016361382, 74661089480257510), (16583788103125227536, 93326361850321887), (16118049110479146516, 116657952312902359), (16991309721690548428, 72911220195563974), (12015765115258409727, 91139025244454968), (15019706394073012159, 113923781555568710), (9551260955736489391, 142404726944460888), (5969538097335305869, 89002954340288055), (2850236603241744433, 111253692925360069), ]; ryu_floating_decimal-0.1.0/src/d2s_intrinsics.rs010066400017500001750000000057631366523746100202350ustar0000000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. // Returns (lo, hi). #[cfg(not(integer128))] #[cfg_attr(feature = "no-panic", inline)] pub fn umul128(a: u64, b: u64) -> (u64, u64) { let a_lo = a as u32; let a_hi = (a >> 32) as u32; let b_lo = b as u32; let b_hi = (b >> 32) as u32; let b00 = a_lo as u64 * b_lo as u64; let b01 = a_lo as u64 * b_hi as u64; let b10 = a_hi as u64 * b_lo as u64; let b11 = a_hi as u64 * b_hi as u64; let b00_lo = b00 as u32; let b00_hi = (b00 >> 32) as u32; let mid1 = b10 + b00_hi as u64; let mid1_lo = mid1 as u32; let mid1_hi = (mid1 >> 32) as u32; let mid2 = b01 + mid1_lo as u64; let mid2_lo = mid2 as u32; let mid2_hi = (mid2 >> 32) as u32; let p_hi = b11 + mid1_hi as u64 + mid2_hi as u64; let p_lo = ((mid2_lo as u64) << 32) | b00_lo as u64; (p_lo, p_hi) } #[cfg(not(integer128))] #[cfg_attr(feature = "no-panic", inline)] pub fn shiftright128(lo: u64, hi: u64, dist: u32) -> u64 { // We don't need to handle the case dist >= 64 here (see above). debug_assert!(dist > 0); debug_assert!(dist < 64); (hi << (64 - dist)) | (lo >> dist) } #[cfg_attr(feature = "no-panic", inline)] pub fn div5(x: u64) -> u64 { x / 5 } #[cfg_attr(feature = "no-panic", inline)] pub fn div10(x: u64) -> u64 { x / 10 } #[cfg_attr(feature = "no-panic", inline)] pub fn div100(x: u64) -> u64 { x / 100 } #[cfg_attr(feature = "no-panic", inline)] fn pow5_factor(mut value: u64) -> u32 { let mut count = 0u32; loop { debug_assert!(value != 0); let q = div5(value); let r = (value as u32).wrapping_sub(5u32.wrapping_mul(q as u32)); if r != 0 { break; } value = q; count += 1; } count } // Returns true if value is divisible by 5^p. #[cfg_attr(feature = "no-panic", inline)] pub fn multiple_of_power_of_5(value: u64, p: u32) -> bool { // I tried a case distinction on p, but there was no performance difference. pow5_factor(value) >= p } // Returns true if value is divisible by 2^p. #[cfg_attr(feature = "no-panic", inline)] pub fn multiple_of_power_of_2(value: u64, p: u32) -> bool { debug_assert!(value != 0); // return __builtin_ctzll(value) >= p; (value & ((1u64 << p) - 1)) == 0 } ryu_floating_decimal-0.1.0/src/d2s_small_table.rs010066400017500001750000000171331366523746100203210ustar0000000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. use common::*; #[cfg(not(integer128))] use d2s_intrinsics::*; pub static DOUBLE_POW5_TABLE: [u64; 26] = [ 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625, 1220703125, 6103515625, 30517578125, 152587890625, 762939453125, 3814697265625, 19073486328125, 95367431640625, 476837158203125, 2384185791015625, 11920928955078125, 59604644775390625, 298023223876953125, ]; pub static DOUBLE_POW5_SPLIT2: [(u64, u64); 13] = [ (0, 72057594037927936), (10376293541461622784, 93132257461547851), (15052517733678820785, 120370621524202240), (6258995034005762182, 77787690973264271), (14893927168346708332, 100538234169297439), (4272820386026678563, 129942622070561240), (7330497575943398595, 83973451344588609), (18377130505971182927, 108533142064701048), (10038208235822497557, 140275798336537794), (7017903361312433648, 90651109995611182), (6366496589810271835, 117163813585596168), (9264989777501460624, 75715339914673581), (17074144231291089770, 97859783203563123), ]; // Unfortunately, the results are sometimes off by one. We use an additional // lookup table to store those cases and adjust the result. pub static POW5_OFFSETS: [u32; 13] = [ 0x00000000, 0x00000000, 0x00000000, 0x033c55be, 0x03db77d8, 0x0265ffb2, 0x00000800, 0x01a8ff56, 0x00000000, 0x0037a200, 0x00004000, 0x03fffffc, 0x00003ffe, ]; pub static DOUBLE_POW5_INV_SPLIT2: [(u64, u64); 13] = [ (1, 288230376151711744), (7661987648932456967, 223007451985306231), (12652048002903177473, 172543658669764094), (5522544058086115566, 266998379490113760), (3181575136763469022, 206579990246952687), (4551508647133041040, 159833525776178802), (1116074521063664381, 247330401473104534), (17400360011128145022, 191362629322552438), (9297997190148906106, 148059663038321393), (11720143854957885429, 229111231347799689), (15401709288678291155, 177266229209635622), (3003071137298187333, 274306203439684434), (17516772882021341108, 212234145163966538), ]; pub static POW5_INV_OFFSETS: [u32; 20] = [ 0x51505404, 0x55054514, 0x45555545, 0x05511411, 0x00505010, 0x00000004, 0x00000000, 0x00000000, 0x55555040, 0x00505051, 0x00050040, 0x55554000, 0x51659559, 0x00001000, 0x15000010, 0x55455555, 0x41404051, 0x00001010, 0x00000014, 0x00000000, ]; // Computes 5^i in the form required by Ryu. #[cfg(integer128)] #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn compute_pow5(i: u32) -> (u64, u64) { let base = i / DOUBLE_POW5_TABLE.len() as u32; let base2 = base * DOUBLE_POW5_TABLE.len() as u32; let offset = i - base2; debug_assert!(base < DOUBLE_POW5_SPLIT2.len() as u32); let mul = *DOUBLE_POW5_SPLIT2.get_unchecked(base as usize); if offset == 0 { return mul; } debug_assert!(offset < DOUBLE_POW5_TABLE.len() as u32); let m = *DOUBLE_POW5_TABLE.get_unchecked(offset as usize); let b0 = m as u128 * mul.0 as u128; let b2 = m as u128 * mul.1 as u128; let delta = pow5bits(i as i32) - pow5bits(base2 as i32); debug_assert!(base < POW5_OFFSETS.len() as u32); let shifted_sum = (b0 >> delta) + (b2 << (64 - delta)) + ((*POW5_OFFSETS.get_unchecked(base as usize) >> offset) & 1) as u128; (shifted_sum as u64, (shifted_sum >> 64) as u64) } // Computes 5^-i in the form required by Ryu. #[cfg(integer128)] #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn compute_inv_pow5(i: u32) -> (u64, u64) { let base = (i + DOUBLE_POW5_TABLE.len() as u32 - 1) / DOUBLE_POW5_TABLE.len() as u32; let base2 = base * DOUBLE_POW5_TABLE.len() as u32; let offset = base2 - i; debug_assert!(base < DOUBLE_POW5_INV_SPLIT2.len() as u32); let mul = *DOUBLE_POW5_INV_SPLIT2.get_unchecked(base as usize); // 1/5^base2 if offset == 0 { return mul; } debug_assert!(offset < DOUBLE_POW5_TABLE.len() as u32); let m = *DOUBLE_POW5_TABLE.get_unchecked(offset as usize); // 5^offset let b0 = m as u128 * (mul.0 - 1) as u128; let b2 = m as u128 * mul.1 as u128; // 1/5^base2 * 5^offset = 1/5^(base2-offset) = 1/5^i let delta = pow5bits(base2 as i32) - pow5bits(i as i32); debug_assert!(base < POW5_INV_OFFSETS.len() as u32); let shifted_sum = ((b0 >> delta) + (b2 << (64 - delta))) + 1 + ((*POW5_INV_OFFSETS.get_unchecked((i / 16) as usize) >> ((i % 16) << 1)) & 3) as u128; (shifted_sum as u64, (shifted_sum >> 64) as u64) } // Computes 5^i in the form required by Ryu, and stores it in the given pointer. #[cfg(not(integer128))] #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn compute_pow5(i: u32) -> (u64, u64) { let base = i / DOUBLE_POW5_TABLE.len() as u32; let base2 = base * DOUBLE_POW5_TABLE.len() as u32; let offset = i - base2; debug_assert!(base < DOUBLE_POW5_SPLIT2.len() as u32); let mul = *DOUBLE_POW5_SPLIT2.get_unchecked(base as usize); if offset == 0 { return mul; } debug_assert!(offset < DOUBLE_POW5_TABLE.len() as u32); let m = *DOUBLE_POW5_TABLE.get_unchecked(offset as usize); let (low1, mut high1) = umul128(m, mul.1); let (low0, high0) = umul128(m, mul.0); let sum = high0 + low1; if sum < high0 { high1 += 1; // overflow into high1 } // high1 | sum | low0 let delta = pow5bits(i as i32) - pow5bits(base2 as i32); debug_assert!(base < POW5_OFFSETS.len() as u32); ( shiftright128(low0, sum, delta as u32) + ((*POW5_OFFSETS.get_unchecked(base as usize) >> offset) & 1) as u64, shiftright128(sum, high1, delta as u32), ) } // Computes 5^-i in the form required by Ryu, and stores it in the given pointer. #[cfg(not(integer128))] #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn compute_inv_pow5(i: u32) -> (u64, u64) { let base = (i + DOUBLE_POW5_TABLE.len() as u32 - 1) / DOUBLE_POW5_TABLE.len() as u32; let base2 = base * DOUBLE_POW5_TABLE.len() as u32; let offset = base2 - i; debug_assert!(base < DOUBLE_POW5_INV_SPLIT2.len() as u32); let mul = *DOUBLE_POW5_INV_SPLIT2.get_unchecked(base as usize); // 1/5^base2 if offset == 0 { return mul; } debug_assert!(offset < DOUBLE_POW5_TABLE.len() as u32); let m = *DOUBLE_POW5_TABLE.get_unchecked(offset as usize); let (low1, mut high1) = umul128(m, mul.1); let (low0, high0) = umul128(m, mul.0 - 1); let sum = high0 + low1; if sum < high0 { high1 += 1; // overflow into high1 } // high1 | sum | low0 let delta = pow5bits(base2 as i32) - pow5bits(i as i32); debug_assert!(base < POW5_INV_OFFSETS.len() as u32); ( shiftright128(low0, sum, delta as u32) + 1 + ((*POW5_INV_OFFSETS.get_unchecked((i / 16) as usize) >> ((i % 16) << 1)) & 3) as u64, shiftright128(sum, high1, delta as u32), ) } ryu_floating_decimal-0.1.0/src/f2s.rs010066400017500001750000000251551366524117200157610ustar0000000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. use common::*; pub const FLOAT_MANTISSA_BITS: u32 = 23; pub const FLOAT_EXPONENT_BITS: u32 = 8; const FLOAT_BIAS: i32 = 127; const FLOAT_POW5_INV_BITCOUNT: i32 = 59; const FLOAT_POW5_BITCOUNT: i32 = 61; // This table is generated by PrintFloatLookupTable. static FLOAT_POW5_INV_SPLIT: [u64; 32] = [ 576460752303423489, 461168601842738791, 368934881474191033, 295147905179352826, 472236648286964522, 377789318629571618, 302231454903657294, 483570327845851670, 386856262276681336, 309485009821345069, 495176015714152110, 396140812571321688, 316912650057057351, 507060240091291761, 405648192073033409, 324518553658426727, 519229685853482763, 415383748682786211, 332306998946228969, 531691198313966350, 425352958651173080, 340282366920938464, 544451787073501542, 435561429658801234, 348449143727040987, 557518629963265579, 446014903970612463, 356811923176489971, 570899077082383953, 456719261665907162, 365375409332725730, 1 << 63, ]; static FLOAT_POW5_SPLIT: [u64; 47] = [ 1152921504606846976, 1441151880758558720, 1801439850948198400, 2251799813685248000, 1407374883553280000, 1759218604441600000, 2199023255552000000, 1374389534720000000, 1717986918400000000, 2147483648000000000, 1342177280000000000, 1677721600000000000, 2097152000000000000, 1310720000000000000, 1638400000000000000, 2048000000000000000, 1280000000000000000, 1600000000000000000, 2000000000000000000, 1250000000000000000, 1562500000000000000, 1953125000000000000, 1220703125000000000, 1525878906250000000, 1907348632812500000, 1192092895507812500, 1490116119384765625, 1862645149230957031, 1164153218269348144, 1455191522836685180, 1818989403545856475, 2273736754432320594, 1421085471520200371, 1776356839400250464, 2220446049250313080, 1387778780781445675, 1734723475976807094, 2168404344971008868, 1355252715606880542, 1694065894508600678, 2117582368135750847, 1323488980084844279, 1654361225106055349, 2067951531382569187, 1292469707114105741, 1615587133892632177, 2019483917365790221, ]; #[cfg_attr(feature = "no-panic", inline)] fn pow5_factor(mut value: u32) -> u32 { let mut count = 0u32; loop { debug_assert!(value != 0); let q = value / 5; let r = value % 5; if r != 0 { break; } value = q; count += 1; } count } // Returns true if value is divisible by 5^p. #[cfg_attr(feature = "no-panic", inline)] fn multiple_of_power_of_5(value: u32, p: u32) -> bool { pow5_factor(value) >= p } // Returns true if value is divisible by 2^p. #[cfg_attr(feature = "no-panic", inline)] fn multiple_of_power_of_2(value: u32, p: u32) -> bool { // return __builtin_ctz(value) >= p; (value & ((1u32 << p) - 1)) == 0 } // It seems to be slightly faster to avoid uint128_t here, although the // generated code for uint128_t looks slightly nicer. #[cfg_attr(feature = "no-panic", inline)] fn mul_shift(m: u32, factor: u64, shift: i32) -> u32 { debug_assert!(shift > 32); // The casts here help MSVC to avoid calls to the __allmul library // function. let factor_lo = factor as u32; let factor_hi = (factor >> 32) as u32; let bits0 = m as u64 * factor_lo as u64; let bits1 = m as u64 * factor_hi as u64; let sum = (bits0 >> 32) + bits1; let shifted_sum = sum >> (shift - 32); debug_assert!(shifted_sum <= u32::max_value() as u64); shifted_sum as u32 } #[cfg_attr(feature = "no-panic", inline)] fn mul_pow5_inv_div_pow2(m: u32, q: u32, j: i32) -> u32 { debug_assert!(q < FLOAT_POW5_INV_SPLIT.len() as u32); unsafe { mul_shift(m, *FLOAT_POW5_INV_SPLIT.get_unchecked(q as usize), j) } } #[cfg_attr(feature = "no-panic", inline)] fn mul_pow5_div_pow2(m: u32, i: u32, j: i32) -> u32 { debug_assert!(i < FLOAT_POW5_SPLIT.len() as u32); unsafe { mul_shift(m, *FLOAT_POW5_SPLIT.get_unchecked(i as usize), j) } } /// A floating decimal representing mantissa * 10^exponent pub struct FloatingDecimal32 { pub mantissa: u32, // Decimal exponent's range is -45 to 38 // inclusive, and can fit in i16 if needed. pub exponent: i32, } #[cfg_attr(feature = "no-panic", inline)] pub fn f2d(ieee_mantissa: u32, ieee_exponent: u32) -> FloatingDecimal32 { let (e2, m2) = if ieee_exponent == 0 { ( // We subtract 2 so that the bounds computation has 2 additional bits. 1 - FLOAT_BIAS - FLOAT_MANTISSA_BITS as i32 - 2, ieee_mantissa, ) } else { ( ieee_exponent as i32 - FLOAT_BIAS - FLOAT_MANTISSA_BITS as i32 - 2, (1u32 << FLOAT_MANTISSA_BITS) | ieee_mantissa, ) }; let even = (m2 & 1) == 0; let accept_bounds = even; // Step 2: Determine the interval of valid decimal representations. let mv = 4 * m2; let mp = 4 * m2 + 2; // Implicit bool -> int conversion. True is 1, false is 0. let mm_shift = (ieee_mantissa != 0 || ieee_exponent <= 1) as u32; let mm = 4 * m2 - 1 - mm_shift; // Step 3: Convert to a decimal power base using 64-bit arithmetic. let mut vr: u32; let mut vp: u32; let mut vm: u32; let e10: i32; let mut vm_is_trailing_zeros = false; let mut vr_is_trailing_zeros = false; let mut last_removed_digit = 0u8; if e2 >= 0 { let q = log10_pow2(e2); e10 = q as i32; let k = FLOAT_POW5_INV_BITCOUNT + pow5bits(q as i32) - 1; let i = -e2 + q as i32 + k; vr = mul_pow5_inv_div_pow2(mv, q, i); vp = mul_pow5_inv_div_pow2(mp, q, i); vm = mul_pow5_inv_div_pow2(mm, q, i); if q != 0 && (vp - 1) / 10 <= vm / 10 { // We need to know one removed digit even if we are not going to loop below. We could use // q = X - 1 above, except that would require 33 bits for the result, and we've found that // 32-bit arithmetic is faster even on 64-bit machines. let l = FLOAT_POW5_INV_BITCOUNT + pow5bits(q as i32 - 1) - 1; last_removed_digit = (mul_pow5_inv_div_pow2(mv, q - 1, -e2 + q as i32 - 1 + l) % 10) as u8; } if q <= 9 { // The largest power of 5 that fits in 24 bits is 5^10, but q <= 9 seems to be safe as well. // Only one of mp, mv, and mm can be a multiple of 5, if any. if mv % 5 == 0 { vr_is_trailing_zeros = multiple_of_power_of_5(mv, q); } else if accept_bounds { vm_is_trailing_zeros = multiple_of_power_of_5(mm, q); } else { vp -= multiple_of_power_of_5(mp, q) as u32; } } } else { let q = log10_pow5(-e2); e10 = q as i32 + e2; let i = -e2 - q as i32; let k = pow5bits(i) - FLOAT_POW5_BITCOUNT; let mut j = q as i32 - k; vr = mul_pow5_div_pow2(mv, i as u32, j); vp = mul_pow5_div_pow2(mp, i as u32, j); vm = mul_pow5_div_pow2(mm, i as u32, j); if q != 0 && (vp - 1) / 10 <= vm / 10 { j = q as i32 - 1 - (pow5bits(i + 1) - FLOAT_POW5_BITCOUNT); last_removed_digit = (mul_pow5_div_pow2(mv, (i + 1) as u32, j) % 10) as u8; } if q <= 1 { // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits. // mv = 4 * m2, so it always has at least two trailing 0 bits. vr_is_trailing_zeros = true; if accept_bounds { // mm = mv - 1 - mm_shift, so it has 1 trailing 0 bit iff mm_shift == 1. vm_is_trailing_zeros = mm_shift == 1; } else { // mp = mv + 2, so it always has at least one trailing 0 bit. vp -= 1; } } else if q < 31 { // TODO(ulfjack): Use a tighter bound here. vr_is_trailing_zeros = multiple_of_power_of_2(mv, q - 1); } } // Step 4: Find the shortest decimal representation in the interval of valid representations. let mut removed = 0i32; let output = if vm_is_trailing_zeros || vr_is_trailing_zeros { // General case, which happens rarely (~4.0%). while vp / 10 > vm / 10 { vm_is_trailing_zeros &= vm - (vm / 10) * 10 == 0; vr_is_trailing_zeros &= last_removed_digit == 0; last_removed_digit = (vr % 10) as u8; vr /= 10; vp /= 10; vm /= 10; removed += 1; } if vm_is_trailing_zeros { while vm % 10 == 0 { vr_is_trailing_zeros &= last_removed_digit == 0; last_removed_digit = (vr % 10) as u8; vr /= 10; vp /= 10; vm /= 10; removed += 1; } } if vr_is_trailing_zeros && last_removed_digit == 5 && vr % 2 == 0 { // Round even if the exact number is .....50..0. last_removed_digit = 4; } // We need to take vr + 1 if vr is outside bounds or we need to round up. vr + ((vr == vm && (!accept_bounds || !vm_is_trailing_zeros)) || last_removed_digit >= 5) as u32 } else { // Specialized for the common case (~96.0%). Percentages below are relative to this. // Loop iterations below (approximately): // 0: 13.6%, 1: 70.7%, 2: 14.1%, 3: 1.39%, 4: 0.14%, 5+: 0.01% while vp / 10 > vm / 10 { last_removed_digit = (vr % 10) as u8; vr /= 10; vp /= 10; vm /= 10; removed += 1; } // We need to take vr + 1 if vr is outside bounds or we need to round up. vr + (vr == vm || last_removed_digit >= 5) as u32 }; let exp = e10 + removed; FloatingDecimal32 { exponent: exp, mantissa: output, } } ryu_floating_decimal-0.1.0/src/lib.rs010066400017500001750000000051141366524360000160240ustar0000000000000000//! This crate copies the internals of the [ryu rust crate](https://github.com/dtolnay/ryu), exposing some useful functions and types for more flexible float printing. //! This crate exposes the functions `d2d` and `f2d`, which convert from `f64` to `FloatingDecimal64` and `f32` to `FloatingDecimal32` respectively. These floating decimals can be converted to strings in a custom way. // License note: this main file is mostly unchanged, except for removing modules that // are no longer exported, and adding the exposed d2d and f2d functions. This module // is similarly provided under the terms of the Apache License, Version 2.0. #![no_std] #![doc(html_root_url = "https://docs.rs/ryu-floating-decimal/1.0.4")] #![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))] #![cfg_attr( feature = "cargo-clippy", allow(cast_lossless, many_single_char_names, unreadable_literal,) )] #[cfg(feature = "no-panic")] extern crate no_panic; mod common; mod d2s; #[cfg(not(feature = "small"))] mod d2s_full_table; mod d2s_intrinsics; #[cfg(feature = "small")] mod d2s_small_table; mod f2s; #[cfg(feature = "no-panic")] use no_panic::no_panic; pub use d2s::FloatingDecimal64; use core::mem; /// Convert an `f64` to a floating decimal using the ryu algorithm /// /// # Example /// /// ``` /// use ryu_floating_decimal::d2d; /// let value: f64 = 3.14; /// let decimal = d2d(value); /// assert_eq!(decimal.mantissa, 314); /// assert_eq!(decimal.exponent, -2); /// ``` #[cfg_attr(feature = "no-panic", inline)] #[cfg_attr(feature = "no-panic", no_panic)] pub fn d2d(val: f64) -> FloatingDecimal64 { let bits: u64 = unsafe { mem::transmute(val) }; let ieee_mantissa = bits & ((1u64 << d2s::DOUBLE_MANTISSA_BITS) - 1); let ieee_exponent = (bits >> d2s::DOUBLE_MANTISSA_BITS) as u32 & ((1u32 << d2s::DOUBLE_EXPONENT_BITS) - 1); d2s::d2d(ieee_mantissa, ieee_exponent) } pub use f2s::FloatingDecimal32; /// Convert an `f32` to a floating decimal using the ryu algorithm /// /// # Example /// /// ``` /// use ryu_floating_decimal::f2d; /// let value: f32 = 12.091; /// let decimal = f2d(value); /// assert_eq!(decimal.mantissa, 12091); /// assert_eq!(decimal.exponent, -3); /// ``` #[cfg_attr(feature = "no-panic", inline)] #[cfg_attr(feature = "no-panic", no_panic)] pub fn f2d(val: f32) -> FloatingDecimal32 { let bits: u32 = unsafe { mem::transmute(val) }; let ieee_mantissa = bits & ((1u32 << f2s::FLOAT_MANTISSA_BITS) - 1); let ieee_exponent = (bits >> f2s::FLOAT_MANTISSA_BITS) as u32 & ((1u32 << f2s::FLOAT_EXPONENT_BITS) - 1); f2s::f2d(ieee_mantissa, ieee_exponent) }