├── .gitignore ├── performance.png ├── fuzz ├── .gitignore ├── Cargo.toml └── fuzz_targets │ └── fuzz_zmij.rs ├── chart ├── .gitignore └── performance.tex ├── gen-pow10 ├── Cargo.toml └── main.rs ├── tests ├── ryu_comparison.rs ├── exhaustive.rs └── test.rs ├── LICENSE-MIT ├── Cargo.toml ├── src ├── tests.rs ├── traits.rs └── lib.rs ├── README.md ├── benches └── bench.rs └── .github └── workflows └── ci.yml /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /performance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtolnay/zmij/HEAD/performance.png -------------------------------------------------------------------------------- /fuzz/.gitignore: -------------------------------------------------------------------------------- 1 | /artifacts/ 2 | /corpus/ 3 | /coverage/ 4 | /target/ 5 | /Cargo.lock 6 | -------------------------------------------------------------------------------- /chart/.gitignore: -------------------------------------------------------------------------------- 1 | /*.aux 2 | /*.fdb_latexmk 3 | /*.fls 4 | /*.log 5 | /*.pdf 6 | /*.png 7 | /*.svg 8 | -------------------------------------------------------------------------------- /gen-pow10/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gen-pow10" 3 | version = "0.0.0" 4 | edition = "2021" 5 | publish = false 6 | 7 | [[bin]] 8 | name = "gen-pow10" 9 | path = "main.rs" 10 | 11 | [dependencies] 12 | num-bigint = "0.4" 13 | num-integer = "0.1" 14 | -------------------------------------------------------------------------------- /fuzz/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "zmij-fuzz" 3 | version = "0.0.0" 4 | authors = ["David Tolnay "] 5 | edition = "2024" 6 | publish = false 7 | 8 | [package.metadata] 9 | cargo-fuzz = true 10 | 11 | [dependencies] 12 | libfuzzer-sys = "0.4" 13 | zmij = { path = ".." } 14 | 15 | [[bin]] 16 | name = "fuzz_zmij" 17 | path = "fuzz_targets/fuzz_zmij.rs" 18 | test = false 19 | doc = false 20 | 21 | [workspace] 22 | -------------------------------------------------------------------------------- /fuzz/fuzz_targets/fuzz_zmij.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | use libfuzzer_sys::fuzz_target; 4 | use std::mem; 5 | 6 | macro_rules! zmij_test { 7 | ($val:expr, $method:ident) => { 8 | match $val { 9 | val => { 10 | let mut buffer = zmij::Buffer::new(); 11 | let string = buffer.$method(val); 12 | assert!(string.len() <= mem::size_of::()); 13 | if val.is_finite() { 14 | assert_eq!(val, string.parse().unwrap()); 15 | } 16 | } 17 | } 18 | }; 19 | } 20 | 21 | fuzz_target!(|inputs: (f64, bool)| { 22 | let (input, finite) = inputs; 23 | match (input, finite) { 24 | (val, false) => zmij_test!(val, format), 25 | (val, true) => zmij_test!(val, format_finite), 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /tests/ryu_comparison.rs: -------------------------------------------------------------------------------- 1 | use rand::rngs::SmallRng; 2 | use rand::{RngCore as _, SeedableRng as _}; 3 | 4 | const N: usize = if cfg!(miri) { 5 | 500 6 | } else if let b"0" = opt_level::OPT_LEVEL.as_bytes() { 7 | 1_000_000 8 | } else { 9 | 100_000_000 10 | }; 11 | 12 | #[test] 13 | fn ryu_comparison() { 14 | let mut ryu_buffer = ryu::Buffer::new(); 15 | let mut zmij_buffer = zmij::Buffer::new(); 16 | let mut rng = SmallRng::from_os_rng(); 17 | let mut fail = 0; 18 | 19 | for _ in 0..N { 20 | let bits = rng.next_u64(); 21 | let float = f64::from_bits(bits); 22 | let ryu = ryu_buffer.format(float); 23 | let zmij = zmij_buffer.format(float); 24 | let matches = if ryu.contains('e') && !ryu.contains("e-") { 25 | ryu.split_once('e') == zmij.split_once("e+") 26 | } else { 27 | ryu == zmij 28 | }; 29 | if !matches { 30 | eprintln!("RYU={ryu} ZMIJ={zmij}"); 31 | fail += 1; 32 | } 33 | } 34 | 35 | assert!(fail == 0, "{fail} mismatches"); 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any 2 | person obtaining a copy of this software and associated 3 | documentation files (the "Software"), to deal in the 4 | Software without restriction, including without 5 | limitation the rights to use, copy, modify, merge, 6 | publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software 8 | is furnished to do so, subject to the following 9 | conditions: 10 | 11 | The above copyright notice and this permission notice 12 | shall be included in all copies or substantial portions 13 | of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 16 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 17 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 18 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 19 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 22 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "zmij" 3 | version = "0.1.8" 4 | authors = ["David Tolnay "] 5 | categories = ["value-formatting", "no-std", "no-std::no-alloc"] 6 | description = "A double-to-string conversion algorithm based on Schubfach and yy" 7 | documentation = "https://docs.rs/zmij" 8 | edition = "2021" 9 | exclude = ["build.rs", "performance.png", "chart/**"] 10 | keywords = ["float"] 11 | license = "MIT" 12 | repository = "https://github.com/dtolnay/zmij" 13 | rust-version = "1.68" 14 | 15 | [dependencies] 16 | no-panic = { version = "0.1", optional = true } 17 | 18 | [dev-dependencies] 19 | num_cpus = "1.8" 20 | opt-level = "1" 21 | rand = "0.9" 22 | ryu = "1" 23 | 24 | [target.'cfg(not(miri))'.dev-dependencies] 25 | criterion = { version = "0.8", default-features = false } 26 | 27 | [[bench]] 28 | name = "bench" 29 | harness = false 30 | 31 | [workspace] 32 | members = ["gen-pow10"] 33 | 34 | [package.metadata.docs.rs] 35 | targets = ["x86_64-unknown-linux-gnu"] 36 | rustdoc-args = [ 37 | "--generate-link-to-definition", 38 | "--generate-macro-expansion", 39 | "--extern-html-root-url=core=https://doc.rust-lang.org", 40 | ] 41 | -------------------------------------------------------------------------------- /gen-pow10/main.rs: -------------------------------------------------------------------------------- 1 | // Power of 10 significand generator for Żmij. 2 | // Copyright (c) 2025 - present, Victor Zverovich 3 | 4 | use num_bigint::BigUint as Uint; 5 | use std::f64::consts::LOG2_10; 6 | 7 | fn main() { 8 | // Range of decimal exponents [K_min, K_max] from the paper. 9 | let dec_exp_min = -324_i32; 10 | let dec_exp_max = 292_i32; 11 | 12 | let num_bits = 128_i32; 13 | 14 | // Negate dec_pow_min and dec_pow_max because we need negative powers 10^-k. 15 | for dec_exp in -dec_exp_max..=-dec_exp_min { 16 | // dec_exp is -k in the paper. 17 | let bin_exp = (f64::from(dec_exp) * LOG2_10).floor() as i32 - (num_bits - 1); 18 | let bin_pow = Uint::from(2_u8).pow(bin_exp.unsigned_abs()); 19 | let dec_pow = Uint::from(10_u8).pow(dec_exp.unsigned_abs()); 20 | let result = if dec_exp < 0 { 21 | bin_pow / dec_pow 22 | } else if bin_exp < 0 { 23 | dec_pow * bin_pow 24 | } else { 25 | dec_pow / bin_pow 26 | }; 27 | let hi = &result >> 64; 28 | let lo = result & (Uint::from(2_u8).pow(64) - Uint::from(1_u8)); 29 | println!("{{{hi:#x}, {lo:#018x}}}, // {dec_exp:4}"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/tests.rs: -------------------------------------------------------------------------------- 1 | use core::mem; 2 | 3 | const _: () = { 4 | let static_data = 5 | mem::size_of_val(&crate::POW10_SIGNIFICANDS) + mem::size_of_val(&crate::DIGITS2); 6 | assert!(static_data == 10072); // 9.8K 7 | }; 8 | 9 | #[cfg(target_endian = "little")] 10 | #[test] 11 | fn utilities() { 12 | let clz = u64::leading_zeros; 13 | assert_eq!(clz(1), 63); 14 | assert_eq!(clz(!0), 0); 15 | 16 | assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000000), 0); 17 | assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000001), 1); 18 | assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000009), 1); 19 | assert_eq!(crate::count_trailing_nonzeros(0x00090000_09000000), 7); 20 | assert_eq!(crate::count_trailing_nonzeros(0x01000000_00000000), 8); 21 | assert_eq!(crate::count_trailing_nonzeros(0x09000000_00000000), 8); 22 | } 23 | 24 | #[test] 25 | fn umul_upper_inexact_to_odd() { 26 | let (hi, lo) = crate::POW10_SIGNIFICANDS[0]; 27 | assert_eq!( 28 | crate::umul_upper_inexact_to_odd(hi, lo, 0x1234567890abcdefu64 << 1), 29 | 0x24554a3ce60a45f5, 30 | ); 31 | assert_eq!( 32 | crate::umul_upper_inexact_to_odd(hi, lo, 0x1234567890abce16u64 << 1), 33 | 0x24554a3ce60a4643, 34 | ); 35 | } 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Żmij 2 | 3 | [github](https://github.com/dtolnay/zmij) 4 | [crates.io](https://crates.io/crates/zmij) 5 | [docs.rs](https://docs.rs/zmij) 6 | [build status](https://github.com/dtolnay/zmij/actions?query=branch%3Amaster) 7 | 8 | Pure Rust implementation of Żmij, an algorithm to quickly convert floating point 9 | numbers to decimal strings. 10 | 11 | This Rust implementation is a line-by-line port of Victor Zverovich's 12 | implementation in C++, [https://github.com/vitaut/zmij][upstream]. 13 | 14 | [upstream]: https://github.com/vitaut/zmij/tree/028427dc616ab48784d23b577e17b05738ac13c8 15 | 16 | ## Example 17 | 18 | ```rust 19 | fn main() { 20 | let mut buffer = zmij::Buffer::new(); 21 | let printed = buffer.format(1.234); 22 | assert_eq!(printed, "1.234"); 23 | } 24 | ``` 25 | 26 | ## Performance (lower is better) 27 | 28 | ![performance](https://raw.githubusercontent.com/dtolnay/zmij/master/performance.png) 29 | 30 |
31 | 32 | #### License 33 | 34 | MIT license. 35 | -------------------------------------------------------------------------------- /benches/bench.rs: -------------------------------------------------------------------------------- 1 | #![allow(clippy::unreadable_literal)] 2 | 3 | use criterion::{criterion_group, criterion_main, Criterion}; 4 | use std::f64; 5 | use std::hint; 6 | use std::io::Write; 7 | 8 | fn do_bench(c: &mut Criterion, group_name: &str, float: f64) { 9 | let mut group = c.benchmark_group(group_name); 10 | group.bench_function("zmij", |b| { 11 | let mut buf = zmij::Buffer::new(); 12 | b.iter(move || { 13 | let float = hint::black_box(float); 14 | let formatted = buf.format_finite(float); 15 | hint::black_box(formatted); 16 | }); 17 | }); 18 | group.bench_function("ryu", |b| { 19 | let mut buf = ryu::Buffer::new(); 20 | b.iter(move || { 21 | let float = hint::black_box(float); 22 | let formatted = buf.format_finite(float); 23 | hint::black_box(formatted); 24 | }); 25 | }); 26 | group.bench_function("std::fmt", |b| { 27 | let mut buf = Vec::with_capacity(20); 28 | b.iter(|| { 29 | buf.clear(); 30 | let float = hint::black_box(float); 31 | write!(&mut buf, "{float}").unwrap(); 32 | hint::black_box(buf.as_slice()); 33 | }); 34 | }); 35 | group.finish(); 36 | } 37 | 38 | fn bench(c: &mut Criterion) { 39 | do_bench(c, "f64[0]", 0f64); 40 | do_bench(c, "f64[short]", 0.1234f64); 41 | do_bench(c, "f64[medium]", 0.123456789f64); 42 | do_bench(c, "f64[e]", f64::consts::E); 43 | do_bench(c, "f64[max]", f64::MAX); 44 | } 45 | 46 | criterion_group!(benches, bench); 47 | criterion_main!(benches); 48 | -------------------------------------------------------------------------------- /src/traits.rs: -------------------------------------------------------------------------------- 1 | use core::fmt::Display; 2 | use core::ops::{Add, BitAnd, BitOr, BitOrAssign, BitXorAssign, Div, Mul, Shl, Shr, Sub}; 3 | 4 | pub trait Float: Copy { 5 | type UInt: UInt; 6 | const MANTISSA_DIGITS: u32; 7 | const MAX_DIGITS10: u32; 8 | fn to_bits(self) -> Self::UInt; 9 | } 10 | 11 | impl Float for f32 { 12 | type UInt = u32; 13 | const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS; 14 | const MAX_DIGITS10: u32 = 9; 15 | fn to_bits(self) -> Self::UInt { 16 | self.to_bits() 17 | } 18 | } 19 | 20 | impl Float for f64 { 21 | type UInt = u64; 22 | const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS; 23 | const MAX_DIGITS10: u32 = 17; 24 | fn to_bits(self) -> Self::UInt { 25 | self.to_bits() 26 | } 27 | } 28 | 29 | pub trait UInt: 30 | Copy 31 | + From 32 | + From 33 | + Add 34 | + Sub 35 | + Mul 36 | + Div 37 | + BitAnd 38 | + BitOr 39 | + Shl 40 | + Shl 41 | + Shr 42 | + Shr 43 | + BitOrAssign 44 | + BitXorAssign 45 | + PartialOrd 46 | + Into 47 | + Display 48 | { 49 | fn truncate(big: u64) -> Self; 50 | fn enlarge(small: u32) -> Self; 51 | } 52 | 53 | impl UInt for u32 { 54 | fn truncate(big: u64) -> Self { 55 | big as u32 56 | } 57 | fn enlarge(small: u32) -> Self { 58 | small 59 | } 60 | } 61 | 62 | impl UInt for u64 { 63 | fn truncate(big: u64) -> Self { 64 | big 65 | } 66 | fn enlarge(small: u32) -> Self { 67 | u64::from(small) 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /tests/exhaustive.rs: -------------------------------------------------------------------------------- 1 | #![cfg_attr(not(check_cfg), allow(unexpected_cfgs))] 2 | 3 | use std::sync::atomic::{AtomicU32, Ordering}; 4 | use std::sync::Arc; 5 | use std::thread; 6 | 7 | #[test] 8 | #[cfg_attr(not(exhaustive), ignore = "requires cfg(exhaustive)")] 9 | fn test_exhaustive() { 10 | const BATCH_SIZE: u32 = 1_000_000; 11 | let counter = Arc::new(AtomicU32::new(0)); 12 | let finished = Arc::new(AtomicU32::new(0)); 13 | 14 | let mut workers = Vec::new(); 15 | for _ in 0..num_cpus::get() { 16 | let counter = counter.clone(); 17 | let finished = finished.clone(); 18 | workers.push(thread::spawn(move || loop { 19 | let batch = counter.fetch_add(1, Ordering::Relaxed); 20 | if batch > u32::MAX / BATCH_SIZE { 21 | return; 22 | } 23 | 24 | let min = batch * BATCH_SIZE; 25 | let max = if batch == u32::MAX / BATCH_SIZE { 26 | u32::MAX 27 | } else { 28 | min + BATCH_SIZE - 1 29 | }; 30 | 31 | let mut zmij_buffer = zmij::Buffer::new(); 32 | let mut ryu_buffer = ryu::Buffer::new(); 33 | for u in min..=max { 34 | let f = f32::from_bits(u); 35 | if !f.is_finite() { 36 | continue; 37 | } 38 | let zmij = zmij_buffer.format_finite(f); 39 | assert_eq!(Ok(f), zmij.parse()); 40 | let ryu = ryu_buffer.format_finite(f); 41 | let matches = if ryu.contains('e') && !ryu.contains("e-") { 42 | ryu.split_once('e') == zmij.split_once("e+") 43 | } else { 44 | ryu == zmij 45 | }; 46 | assert!(matches, "{ryu} != {zmij}"); 47 | } 48 | 49 | let increment = max - min + 1; 50 | let update = finished.fetch_add(increment, Ordering::Relaxed); 51 | println!("{}", u64::from(update) + u64::from(increment)); 52 | })); 53 | } 54 | 55 | for w in workers { 56 | w.join().unwrap(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /chart/performance.tex: -------------------------------------------------------------------------------- 1 | \documentclass{standalone} 2 | \usepackage{pgfplots} 3 | \usepackage{sansmath} 4 | \pgfplotsset{compat=1.16} 5 | \definecolor{zmij}{HTML}{3366FF} 6 | \definecolor{ryu}{HTML}{9494BB} 7 | \definecolor{std}{HTML}{949494} 8 | \definecolor{bg}{HTML}{CFCFCF} 9 | \begin{document} 10 | \pagecolor{white} 11 | \begin{tikzpicture} 12 | \edef\entries{ 13 | "$0.1234$", 14 | "$0.123456789$", 15 | "$2.718281828459045$", 16 | "$1.7976931348623157e308$", 17 | } 18 | \begin{axis}[ 19 | width=5in, 20 | height=3.5in, 21 | ybar, 22 | ymin=0, 23 | bar width=18pt, 24 | enlarge x limits={abs=39pt}, 25 | ylabel={nanos for one call to write}, 26 | legend style={ 27 | anchor=north west, 28 | at={(0.025,0.975)}, 29 | legend columns=1, 30 | draw=none, 31 | fill=none, 32 | }, 33 | legend entries={ 34 | zmij::Buffer::new().format\_finite(value)\\ 35 | ryu::Buffer::new().format\_finite(value)\\ 36 | std::write!(\&mut buf, ``\{\}'', value)\\ 37 | }, 38 | legend cell align=left, 39 | xtick={-0.5,0.5,1.5,2.5,3.5,4.5}, 40 | xticklabels={}, 41 | xtick pos=left, 42 | visualization depends on={y \as \rawy}, 43 | every node near coord/.append style={ 44 | shift={(axis direction cs:0,-\rawy/2)}, 45 | rotate=90, 46 | anchor=center, 47 | font=\sansmath\sffamily, 48 | }, 49 | axis background/.style={fill=bg}, 50 | tick label style={font=\sansmath\sffamily}, 51 | every axis label={font=\sansmath\sffamily}, 52 | legend style={font=\sansmath\sffamily}, 53 | label style={font=\sansmath\sffamily}, 54 | ] 55 | \addplot[ 56 | black, 57 | fill=zmij, 58 | area legend, 59 | nodes near coords={}, 60 | ] coordinates { 61 | (0, 25) 62 | (1, 25) 63 | (2, 25) 64 | (3, 27) 65 | }; 66 | \addplot[ 67 | black, 68 | fill=ryu, 69 | area legend, 70 | nodes near coords={}, 71 | ] coordinates { 72 | (0, 43) 73 | (1, 37) 74 | (2, 29) 75 | (3, 29) 76 | }; 77 | \addplot[ 78 | black, 79 | fill=std, 80 | area legend, 81 | nodes near coords=\pgfmathsetmacro{\input}{{\entries}[\coordindex]}\input, 82 | ] coordinates { 83 | (0, 65) 84 | (1, 68) 85 | (2, 85) 86 | (3, 111) 87 | }; 88 | \end{axis} 89 | \pgfresetboundingbox\path 90 | (current axis.south west) -- ++(-0.44in,-0.09in) 91 | rectangle (current axis.north east) -- ++(0.05in,0.05in); 92 | \end{tikzpicture} 93 | \end{document} 94 | -------------------------------------------------------------------------------- /tests/test.rs: -------------------------------------------------------------------------------- 1 | #![allow(clippy::float_cmp, clippy::unreadable_literal)] 2 | 3 | fn dtoa(value: f64) -> String { 4 | zmij::Buffer::new().format(value).to_owned() 5 | } 6 | 7 | fn ftoa(value: f32) -> String { 8 | zmij::Buffer::new().format(value).to_owned() 9 | } 10 | 11 | mod dtoa_test { 12 | use super::dtoa; 13 | 14 | #[test] 15 | fn normal() { 16 | assert_eq!(dtoa(6.62607015e-34), "6.62607015e-34"); 17 | } 18 | 19 | #[test] 20 | fn subnormal() { 21 | assert_eq!(dtoa(0.0f64.next_up()), "5e-324"); 22 | assert_eq!(dtoa(1e-323), "1e-323"); 23 | assert_eq!(dtoa(1.2e-322), "1.2e-322"); 24 | assert_eq!(dtoa(1.24e-322), "1.24e-322"); 25 | assert_eq!(dtoa(1.234e-320), "1.234e-320"); 26 | } 27 | 28 | #[test] 29 | fn small_int() { 30 | assert_eq!(dtoa(1.0), "1.0"); 31 | } 32 | 33 | #[test] 34 | fn zero() { 35 | assert_eq!(dtoa(0.0), "0.0"); 36 | assert_eq!(dtoa(-0.0), "-0.0"); 37 | } 38 | 39 | #[test] 40 | fn inf() { 41 | assert_eq!(dtoa(f64::INFINITY), "inf"); 42 | assert_eq!(dtoa(f64::NEG_INFINITY), "-inf"); 43 | } 44 | 45 | #[test] 46 | fn nan() { 47 | assert_eq!(dtoa(f64::NAN.copysign(1.0)), "NaN"); 48 | assert_eq!(dtoa(f64::NAN.copysign(-1.0)), "NaN"); 49 | } 50 | 51 | #[test] 52 | fn shorter() { 53 | // A possibly shorter underestimate is picked (u' in Schubfach). 54 | assert_eq!(dtoa(-4.932096661796888e-226), "-4.932096661796888e-226"); 55 | 56 | // A possibly shorter overestimate is picked (w' in Schubfach). 57 | assert_eq!(dtoa(3.439070283483335e+35), "3.439070283483335e+35"); 58 | } 59 | 60 | #[test] 61 | fn single_candidate() { 62 | // Only an underestimate is in the rounding region (u in Schubfach). 63 | assert_eq!(dtoa(6.606854224493745e-17), "6.606854224493745e-17"); 64 | 65 | // Only an overestimate is in the rounding region (w in Schubfach). 66 | assert_eq!(dtoa(6.079537928711555e+61), "6.079537928711555e+61"); 67 | } 68 | 69 | #[test] 70 | fn all_exponents() { 71 | for exp in f64::MIN_EXP..f64::MAX_EXP { 72 | let expected = f64::exp2(f64::from(exp)); 73 | let actual = dtoa(expected).parse::().unwrap(); 74 | assert_eq!(actual, expected); 75 | } 76 | } 77 | } 78 | 79 | mod ftoa_test { 80 | use super::ftoa; 81 | 82 | #[test] 83 | fn normal() { 84 | assert_eq!(ftoa(6.62607e-34), "6.62607e-34"); 85 | assert_eq!(ftoa(9.061488e15), "9.061488e+15"); 86 | assert_eq!(ftoa(1.342178e+08), "134217800.0"); 87 | assert_eq!(ftoa(1.3421781e+08), "134217810.0"); 88 | } 89 | 90 | #[test] 91 | fn subnormal() { 92 | assert_eq!(ftoa(0.0f32.next_up()), "1e-45"); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | pull_request: 6 | workflow_dispatch: 7 | schedule: [cron: "40 1 * * *"] 8 | 9 | permissions: 10 | contents: read 11 | 12 | env: 13 | RUSTFLAGS: -Dwarnings 14 | 15 | jobs: 16 | pre_ci: 17 | uses: dtolnay/.github/.github/workflows/pre_ci.yml@master 18 | 19 | test: 20 | name: Rust ${{matrix.rust}} 21 | needs: pre_ci 22 | if: needs.pre_ci.outputs.continue 23 | runs-on: ubuntu-latest 24 | strategy: 25 | fail-fast: false 26 | matrix: 27 | rust: [nightly, beta, stable, 1.86.0, 1.68.0] 28 | timeout-minutes: 45 29 | steps: 30 | - uses: actions/checkout@v6 31 | - uses: dtolnay/rust-toolchain@master 32 | with: 33 | toolchain: ${{matrix.rust}} 34 | - name: Enable type layout randomization 35 | run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout >> $GITHUB_ENV 36 | if: matrix.rust == 'nightly' 37 | - run: cargo check 38 | - run: cargo build --tests --features no-panic --release 39 | if: matrix.rust == 'nightly' 40 | - run: cargo test 41 | if: matrix.rust != '1.68.0' 42 | - run: cargo test --release 43 | if: matrix.rust != '1.68.0' 44 | - uses: actions/upload-artifact@v6 45 | if: matrix.rust == 'nightly' && always() 46 | with: 47 | name: Cargo.lock 48 | path: Cargo.lock 49 | continue-on-error: true 50 | 51 | doc: 52 | name: Documentation 53 | needs: pre_ci 54 | if: needs.pre_ci.outputs.continue 55 | runs-on: ubuntu-latest 56 | env: 57 | RUSTDOCFLAGS: -Dwarnings 58 | timeout-minutes: 45 59 | steps: 60 | - uses: actions/checkout@v6 61 | - uses: dtolnay/rust-toolchain@nightly 62 | - uses: dtolnay/install@cargo-docs-rs 63 | - run: cargo docs-rs 64 | 65 | clippy: 66 | name: Clippy 67 | runs-on: ubuntu-latest 68 | if: github.event_name != 'pull_request' 69 | timeout-minutes: 45 70 | steps: 71 | - uses: actions/checkout@v6 72 | - uses: dtolnay/rust-toolchain@clippy 73 | - run: cargo clippy --tests --benches -- -Dclippy::all -Dclippy::pedantic 74 | 75 | miri: 76 | name: Miri (${{matrix.name}}) 77 | needs: pre_ci 78 | if: needs.pre_ci.outputs.continue 79 | runs-on: ubuntu-latest 80 | strategy: 81 | fail-fast: false 82 | matrix: 83 | include: 84 | - name: 64-bit little endian 85 | target: x86_64-unknown-linux-gnu 86 | - name: 64-bit big endian 87 | target: powerpc64-unknown-linux-gnu 88 | - name: 32-bit little endian 89 | target: i686-unknown-linux-gnu 90 | - name: 32-bit big endian 91 | target: mips-unknown-linux-gnu 92 | timeout-minutes: 45 93 | steps: 94 | - uses: actions/checkout@v6 95 | - uses: dtolnay/rust-toolchain@miri 96 | - run: cargo miri setup 97 | - run: cargo miri test --target ${{matrix.target}} 98 | env: 99 | MIRIFLAGS: -Zmiri-strict-provenance 100 | 101 | outdated: 102 | name: Outdated 103 | runs-on: ubuntu-latest 104 | if: github.event_name != 'pull_request' 105 | timeout-minutes: 45 106 | steps: 107 | - uses: actions/checkout@v6 108 | - uses: dtolnay/rust-toolchain@stable 109 | - uses: dtolnay/install@cargo-outdated 110 | - run: cargo outdated --exit-code 1 111 | - run: cargo outdated --manifest-path fuzz/Cargo.toml --exit-code 1 112 | 113 | fuzz: 114 | name: Fuzz 115 | needs: pre_ci 116 | if: needs.pre_ci.outputs.continue 117 | runs-on: ubuntu-latest 118 | timeout-minutes: 45 119 | steps: 120 | - uses: actions/checkout@v6 121 | - uses: dtolnay/rust-toolchain@nightly 122 | - uses: dtolnay/install@cargo-fuzz 123 | - run: cargo fuzz check 124 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! [![github]](https://github.com/dtolnay/zmij) [![crates-io]](https://crates.io/crates/zmij) [![docs-rs]](https://docs.rs/zmij) 2 | //! 3 | //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github 4 | //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust 5 | //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs 6 | //! 7 | //!
8 | //! 9 | //! A double-to-string conversion algorithm based on [Schubfach] and [yy]. 10 | //! 11 | //! This Rust implementation is a line-by-line port of Victor Zverovich's 12 | //! implementation in C++, . 13 | //! 14 | //! [Schubfach]: https://fmt.dev/papers/Schubfach4.pdf 15 | //! [yy]: https://github.com/ibireme/c_numconv_benchmark/blob/master/vendor/yy_double/yy_double.c 16 | //! 17 | //!
18 | //! 19 | //! # Example 20 | //! 21 | //! ``` 22 | //! fn main() { 23 | //! let mut buffer = zmij::Buffer::new(); 24 | //! let printed = buffer.format(1.234); 25 | //! assert_eq!(printed, "1.234"); 26 | //! } 27 | //! ``` 28 | //! 29 | //!
30 | //! 31 | //! ## Performance (lower is better) 32 | //! 33 | //! ![performance](https://raw.githubusercontent.com/dtolnay/zmij/master/performance.png) 34 | 35 | #![no_std] 36 | #![doc(html_root_url = "https://docs.rs/zmij/0.1.8")] 37 | #![deny(unsafe_op_in_unsafe_fn)] 38 | #![allow( 39 | clippy::blocks_in_conditions, 40 | clippy::cast_possible_truncation, 41 | clippy::cast_possible_wrap, 42 | clippy::cast_sign_loss, 43 | clippy::doc_markdown, 44 | clippy::items_after_statements, 45 | clippy::must_use_candidate, 46 | clippy::needless_doctest_main, 47 | clippy::redundant_else, 48 | clippy::similar_names, 49 | clippy::too_many_lines, 50 | clippy::unreadable_literal 51 | )] 52 | 53 | #[cfg(test)] 54 | mod tests; 55 | mod traits; 56 | 57 | use core::mem::{self, MaybeUninit}; 58 | use core::ptr; 59 | use core::slice; 60 | use core::str; 61 | #[cfg(feature = "no-panic")] 62 | use no_panic::no_panic; 63 | 64 | const BUFFER_SIZE: usize = 24; 65 | const NAN: &str = "NaN"; 66 | const INFINITY: &str = "inf"; 67 | const NEG_INFINITY: &str = "-inf"; 68 | 69 | #[allow(non_camel_case_types)] 70 | struct uint128 { 71 | hi: u64, 72 | lo: u64, 73 | } 74 | 75 | // 128-bit significands of powers of 10 rounded down. 76 | // Generated with gen-pow10/main.rs. 77 | #[rustfmt::skip] 78 | static POW10_SIGNIFICANDS: [(u64, u64); 617] = [ 79 | (0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7a), // -292 80 | (0x9faacf3df73609b1, 0x77b191618c54e9ac), // -291 81 | (0xc795830d75038c1d, 0xd59df5b9ef6a2417), // -290 82 | (0xf97ae3d0d2446f25, 0x4b0573286b44ad1d), // -289 83 | (0x9becce62836ac577, 0x4ee367f9430aec32), // -288 84 | (0xc2e801fb244576d5, 0x229c41f793cda73f), // -287 85 | (0xf3a20279ed56d48a, 0x6b43527578c1110f), // -286 86 | (0x9845418c345644d6, 0x830a13896b78aaa9), // -285 87 | (0xbe5691ef416bd60c, 0x23cc986bc656d553), // -284 88 | (0xedec366b11c6cb8f, 0x2cbfbe86b7ec8aa8), // -283 89 | (0x94b3a202eb1c3f39, 0x7bf7d71432f3d6a9), // -282 90 | (0xb9e08a83a5e34f07, 0xdaf5ccd93fb0cc53), // -281 91 | (0xe858ad248f5c22c9, 0xd1b3400f8f9cff68), // -280 92 | (0x91376c36d99995be, 0x23100809b9c21fa1), // -279 93 | (0xb58547448ffffb2d, 0xabd40a0c2832a78a), // -278 94 | (0xe2e69915b3fff9f9, 0x16c90c8f323f516c), // -277 95 | (0x8dd01fad907ffc3b, 0xae3da7d97f6792e3), // -276 96 | (0xb1442798f49ffb4a, 0x99cd11cfdf41779c), // -275 97 | (0xdd95317f31c7fa1d, 0x40405643d711d583), // -274 98 | (0x8a7d3eef7f1cfc52, 0x482835ea666b2572), // -273 99 | (0xad1c8eab5ee43b66, 0xda3243650005eecf), // -272 100 | (0xd863b256369d4a40, 0x90bed43e40076a82), // -271 101 | (0x873e4f75e2224e68, 0x5a7744a6e804a291), // -270 102 | (0xa90de3535aaae202, 0x711515d0a205cb36), // -269 103 | (0xd3515c2831559a83, 0x0d5a5b44ca873e03), // -268 104 | (0x8412d9991ed58091, 0xe858790afe9486c2), // -267 105 | (0xa5178fff668ae0b6, 0x626e974dbe39a872), // -266 106 | (0xce5d73ff402d98e3, 0xfb0a3d212dc8128f), // -265 107 | (0x80fa687f881c7f8e, 0x7ce66634bc9d0b99), // -264 108 | (0xa139029f6a239f72, 0x1c1fffc1ebc44e80), // -263 109 | (0xc987434744ac874e, 0xa327ffb266b56220), // -262 110 | (0xfbe9141915d7a922, 0x4bf1ff9f0062baa8), // -261 111 | (0x9d71ac8fada6c9b5, 0x6f773fc3603db4a9), // -260 112 | (0xc4ce17b399107c22, 0xcb550fb4384d21d3), // -259 113 | (0xf6019da07f549b2b, 0x7e2a53a146606a48), // -258 114 | (0x99c102844f94e0fb, 0x2eda7444cbfc426d), // -257 115 | (0xc0314325637a1939, 0xfa911155fefb5308), // -256 116 | (0xf03d93eebc589f88, 0x793555ab7eba27ca), // -255 117 | (0x96267c7535b763b5, 0x4bc1558b2f3458de), // -254 118 | (0xbbb01b9283253ca2, 0x9eb1aaedfb016f16), // -253 119 | (0xea9c227723ee8bcb, 0x465e15a979c1cadc), // -252 120 | (0x92a1958a7675175f, 0x0bfacd89ec191ec9), // -251 121 | (0xb749faed14125d36, 0xcef980ec671f667b), // -250 122 | (0xe51c79a85916f484, 0x82b7e12780e7401a), // -249 123 | (0x8f31cc0937ae58d2, 0xd1b2ecb8b0908810), // -248 124 | (0xb2fe3f0b8599ef07, 0x861fa7e6dcb4aa15), // -247 125 | (0xdfbdcece67006ac9, 0x67a791e093e1d49a), // -246 126 | (0x8bd6a141006042bd, 0xe0c8bb2c5c6d24e0), // -245 127 | (0xaecc49914078536d, 0x58fae9f773886e18), // -244 128 | (0xda7f5bf590966848, 0xaf39a475506a899e), // -243 129 | (0x888f99797a5e012d, 0x6d8406c952429603), // -242 130 | (0xaab37fd7d8f58178, 0xc8e5087ba6d33b83), // -241 131 | (0xd5605fcdcf32e1d6, 0xfb1e4a9a90880a64), // -240 132 | (0x855c3be0a17fcd26, 0x5cf2eea09a55067f), // -239 133 | (0xa6b34ad8c9dfc06f, 0xf42faa48c0ea481e), // -238 134 | (0xd0601d8efc57b08b, 0xf13b94daf124da26), // -237 135 | (0x823c12795db6ce57, 0x76c53d08d6b70858), // -236 136 | (0xa2cb1717b52481ed, 0x54768c4b0c64ca6e), // -235 137 | (0xcb7ddcdda26da268, 0xa9942f5dcf7dfd09), // -234 138 | (0xfe5d54150b090b02, 0xd3f93b35435d7c4c), // -233 139 | (0x9efa548d26e5a6e1, 0xc47bc5014a1a6daf), // -232 140 | (0xc6b8e9b0709f109a, 0x359ab6419ca1091b), // -231 141 | (0xf867241c8cc6d4c0, 0xc30163d203c94b62), // -230 142 | (0x9b407691d7fc44f8, 0x79e0de63425dcf1d), // -229 143 | (0xc21094364dfb5636, 0x985915fc12f542e4), // -228 144 | (0xf294b943e17a2bc4, 0x3e6f5b7b17b2939d), // -227 145 | (0x979cf3ca6cec5b5a, 0xa705992ceecf9c42), // -226 146 | (0xbd8430bd08277231, 0x50c6ff782a838353), // -225 147 | (0xece53cec4a314ebd, 0xa4f8bf5635246428), // -224 148 | (0x940f4613ae5ed136, 0x871b7795e136be99), // -223 149 | (0xb913179899f68584, 0x28e2557b59846e3f), // -222 150 | (0xe757dd7ec07426e5, 0x331aeada2fe589cf), // -221 151 | (0x9096ea6f3848984f, 0x3ff0d2c85def7621), // -220 152 | (0xb4bca50b065abe63, 0x0fed077a756b53a9), // -219 153 | (0xe1ebce4dc7f16dfb, 0xd3e8495912c62894), // -218 154 | (0x8d3360f09cf6e4bd, 0x64712dd7abbbd95c), // -217 155 | (0xb080392cc4349dec, 0xbd8d794d96aacfb3), // -216 156 | (0xdca04777f541c567, 0xecf0d7a0fc5583a0), // -215 157 | (0x89e42caaf9491b60, 0xf41686c49db57244), // -214 158 | (0xac5d37d5b79b6239, 0x311c2875c522ced5), // -213 159 | (0xd77485cb25823ac7, 0x7d633293366b828b), // -212 160 | (0x86a8d39ef77164bc, 0xae5dff9c02033197), // -211 161 | (0xa8530886b54dbdeb, 0xd9f57f830283fdfc), // -210 162 | (0xd267caa862a12d66, 0xd072df63c324fd7b), // -209 163 | (0x8380dea93da4bc60, 0x4247cb9e59f71e6d), // -208 164 | (0xa46116538d0deb78, 0x52d9be85f074e608), // -207 165 | (0xcd795be870516656, 0x67902e276c921f8b), // -206 166 | (0x806bd9714632dff6, 0x00ba1cd8a3db53b6), // -205 167 | (0xa086cfcd97bf97f3, 0x80e8a40eccd228a4), // -204 168 | (0xc8a883c0fdaf7df0, 0x6122cd128006b2cd), // -203 169 | (0xfad2a4b13d1b5d6c, 0x796b805720085f81), // -202 170 | (0x9cc3a6eec6311a63, 0xcbe3303674053bb0), // -201 171 | (0xc3f490aa77bd60fc, 0xbedbfc4411068a9c), // -200 172 | (0xf4f1b4d515acb93b, 0xee92fb5515482d44), // -199 173 | (0x991711052d8bf3c5, 0x751bdd152d4d1c4a), // -198 174 | (0xbf5cd54678eef0b6, 0xd262d45a78a0635d), // -197 175 | (0xef340a98172aace4, 0x86fb897116c87c34), // -196 176 | (0x9580869f0e7aac0e, 0xd45d35e6ae3d4da0), // -195 177 | (0xbae0a846d2195712, 0x8974836059cca109), // -194 178 | (0xe998d258869facd7, 0x2bd1a438703fc94b), // -193 179 | (0x91ff83775423cc06, 0x7b6306a34627ddcf), // -192 180 | (0xb67f6455292cbf08, 0x1a3bc84c17b1d542), // -191 181 | (0xe41f3d6a7377eeca, 0x20caba5f1d9e4a93), // -190 182 | (0x8e938662882af53e, 0x547eb47b7282ee9c), // -189 183 | (0xb23867fb2a35b28d, 0xe99e619a4f23aa43), // -188 184 | (0xdec681f9f4c31f31, 0x6405fa00e2ec94d4), // -187 185 | (0x8b3c113c38f9f37e, 0xde83bc408dd3dd04), // -186 186 | (0xae0b158b4738705e, 0x9624ab50b148d445), // -185 187 | (0xd98ddaee19068c76, 0x3badd624dd9b0957), // -184 188 | (0x87f8a8d4cfa417c9, 0xe54ca5d70a80e5d6), // -183 189 | (0xa9f6d30a038d1dbc, 0x5e9fcf4ccd211f4c), // -182 190 | (0xd47487cc8470652b, 0x7647c3200069671f), // -181 191 | (0x84c8d4dfd2c63f3b, 0x29ecd9f40041e073), // -180 192 | (0xa5fb0a17c777cf09, 0xf468107100525890), // -179 193 | (0xcf79cc9db955c2cc, 0x7182148d4066eeb4), // -178 194 | (0x81ac1fe293d599bf, 0xc6f14cd848405530), // -177 195 | (0xa21727db38cb002f, 0xb8ada00e5a506a7c), // -176 196 | (0xca9cf1d206fdc03b, 0xa6d90811f0e4851c), // -175 197 | (0xfd442e4688bd304a, 0x908f4a166d1da663), // -174 198 | (0x9e4a9cec15763e2e, 0x9a598e4e043287fe), // -173 199 | (0xc5dd44271ad3cdba, 0x40eff1e1853f29fd), // -172 200 | (0xf7549530e188c128, 0xd12bee59e68ef47c), // -171 201 | (0x9a94dd3e8cf578b9, 0x82bb74f8301958ce), // -170 202 | (0xc13a148e3032d6e7, 0xe36a52363c1faf01), // -169 203 | (0xf18899b1bc3f8ca1, 0xdc44e6c3cb279ac1), // -168 204 | (0x96f5600f15a7b7e5, 0x29ab103a5ef8c0b9), // -167 205 | (0xbcb2b812db11a5de, 0x7415d448f6b6f0e7), // -166 206 | (0xebdf661791d60f56, 0x111b495b3464ad21), // -165 207 | (0x936b9fcebb25c995, 0xcab10dd900beec34), // -164 208 | (0xb84687c269ef3bfb, 0x3d5d514f40eea742), // -163 209 | (0xe65829b3046b0afa, 0x0cb4a5a3112a5112), // -162 210 | (0x8ff71a0fe2c2e6dc, 0x47f0e785eaba72ab), // -161 211 | (0xb3f4e093db73a093, 0x59ed216765690f56), // -160 212 | (0xe0f218b8d25088b8, 0x306869c13ec3532c), // -159 213 | (0x8c974f7383725573, 0x1e414218c73a13fb), // -158 214 | (0xafbd2350644eeacf, 0xe5d1929ef90898fa), // -157 215 | (0xdbac6c247d62a583, 0xdf45f746b74abf39), // -156 216 | (0x894bc396ce5da772, 0x6b8bba8c328eb783), // -155 217 | (0xab9eb47c81f5114f, 0x066ea92f3f326564), // -154 218 | (0xd686619ba27255a2, 0xc80a537b0efefebd), // -153 219 | (0x8613fd0145877585, 0xbd06742ce95f5f36), // -152 220 | (0xa798fc4196e952e7, 0x2c48113823b73704), // -151 221 | (0xd17f3b51fca3a7a0, 0xf75a15862ca504c5), // -150 222 | (0x82ef85133de648c4, 0x9a984d73dbe722fb), // -149 223 | (0xa3ab66580d5fdaf5, 0xc13e60d0d2e0ebba), // -148 224 | (0xcc963fee10b7d1b3, 0x318df905079926a8), // -147 225 | (0xffbbcfe994e5c61f, 0xfdf17746497f7052), // -146 226 | (0x9fd561f1fd0f9bd3, 0xfeb6ea8bedefa633), // -145 227 | (0xc7caba6e7c5382c8, 0xfe64a52ee96b8fc0), // -144 228 | (0xf9bd690a1b68637b, 0x3dfdce7aa3c673b0), // -143 229 | (0x9c1661a651213e2d, 0x06bea10ca65c084e), // -142 230 | (0xc31bfa0fe5698db8, 0x486e494fcff30a62), // -141 231 | (0xf3e2f893dec3f126, 0x5a89dba3c3efccfa), // -140 232 | (0x986ddb5c6b3a76b7, 0xf89629465a75e01c), // -139 233 | (0xbe89523386091465, 0xf6bbb397f1135823), // -138 234 | (0xee2ba6c0678b597f, 0x746aa07ded582e2c), // -137 235 | (0x94db483840b717ef, 0xa8c2a44eb4571cdc), // -136 236 | (0xba121a4650e4ddeb, 0x92f34d62616ce413), // -135 237 | (0xe896a0d7e51e1566, 0x77b020baf9c81d17), // -134 238 | (0x915e2486ef32cd60, 0x0ace1474dc1d122e), // -133 239 | (0xb5b5ada8aaff80b8, 0x0d819992132456ba), // -132 240 | (0xe3231912d5bf60e6, 0x10e1fff697ed6c69), // -131 241 | (0x8df5efabc5979c8f, 0xca8d3ffa1ef463c1), // -130 242 | (0xb1736b96b6fd83b3, 0xbd308ff8a6b17cb2), // -129 243 | (0xddd0467c64bce4a0, 0xac7cb3f6d05ddbde), // -128 244 | (0x8aa22c0dbef60ee4, 0x6bcdf07a423aa96b), // -127 245 | (0xad4ab7112eb3929d, 0x86c16c98d2c953c6), // -126 246 | (0xd89d64d57a607744, 0xe871c7bf077ba8b7), // -125 247 | (0x87625f056c7c4a8b, 0x11471cd764ad4972), // -124 248 | (0xa93af6c6c79b5d2d, 0xd598e40d3dd89bcf), // -123 249 | (0xd389b47879823479, 0x4aff1d108d4ec2c3), // -122 250 | (0x843610cb4bf160cb, 0xcedf722a585139ba), // -121 251 | (0xa54394fe1eedb8fe, 0xc2974eb4ee658828), // -120 252 | (0xce947a3da6a9273e, 0x733d226229feea32), // -119 253 | (0x811ccc668829b887, 0x0806357d5a3f525f), // -118 254 | (0xa163ff802a3426a8, 0xca07c2dcb0cf26f7), // -117 255 | (0xc9bcff6034c13052, 0xfc89b393dd02f0b5), // -116 256 | (0xfc2c3f3841f17c67, 0xbbac2078d443ace2), // -115 257 | (0x9d9ba7832936edc0, 0xd54b944b84aa4c0d), // -114 258 | (0xc5029163f384a931, 0x0a9e795e65d4df11), // -113 259 | (0xf64335bcf065d37d, 0x4d4617b5ff4a16d5), // -112 260 | (0x99ea0196163fa42e, 0x504bced1bf8e4e45), // -111 261 | (0xc06481fb9bcf8d39, 0xe45ec2862f71e1d6), // -110 262 | (0xf07da27a82c37088, 0x5d767327bb4e5a4c), // -109 263 | (0x964e858c91ba2655, 0x3a6a07f8d510f86f), // -108 264 | (0xbbe226efb628afea, 0x890489f70a55368b), // -107 265 | (0xeadab0aba3b2dbe5, 0x2b45ac74ccea842e), // -106 266 | (0x92c8ae6b464fc96f, 0x3b0b8bc90012929d), // -105 267 | (0xb77ada0617e3bbcb, 0x09ce6ebb40173744), // -104 268 | (0xe55990879ddcaabd, 0xcc420a6a101d0515), // -103 269 | (0x8f57fa54c2a9eab6, 0x9fa946824a12232d), // -102 270 | (0xb32df8e9f3546564, 0x47939822dc96abf9), // -101 271 | (0xdff9772470297ebd, 0x59787e2b93bc56f7), // -100 272 | (0x8bfbea76c619ef36, 0x57eb4edb3c55b65a), // -99 273 | (0xaefae51477a06b03, 0xede622920b6b23f1), // -98 274 | (0xdab99e59958885c4, 0xe95fab368e45eced), // -97 275 | (0x88b402f7fd75539b, 0x11dbcb0218ebb414), // -96 276 | (0xaae103b5fcd2a881, 0xd652bdc29f26a119), // -95 277 | (0xd59944a37c0752a2, 0x4be76d3346f0495f), // -94 278 | (0x857fcae62d8493a5, 0x6f70a4400c562ddb), // -93 279 | (0xa6dfbd9fb8e5b88e, 0xcb4ccd500f6bb952), // -92 280 | (0xd097ad07a71f26b2, 0x7e2000a41346a7a7), // -91 281 | (0x825ecc24c873782f, 0x8ed400668c0c28c8), // -90 282 | (0xa2f67f2dfa90563b, 0x728900802f0f32fa), // -89 283 | (0xcbb41ef979346bca, 0x4f2b40a03ad2ffb9), // -88 284 | (0xfea126b7d78186bc, 0xe2f610c84987bfa8), // -87 285 | (0x9f24b832e6b0f436, 0x0dd9ca7d2df4d7c9), // -86 286 | (0xc6ede63fa05d3143, 0x91503d1c79720dbb), // -85 287 | (0xf8a95fcf88747d94, 0x75a44c6397ce912a), // -84 288 | (0x9b69dbe1b548ce7c, 0xc986afbe3ee11aba), // -83 289 | (0xc24452da229b021b, 0xfbe85badce996168), // -82 290 | (0xf2d56790ab41c2a2, 0xfae27299423fb9c3), // -81 291 | (0x97c560ba6b0919a5, 0xdccd879fc967d41a), // -80 292 | (0xbdb6b8e905cb600f, 0x5400e987bbc1c920), // -79 293 | (0xed246723473e3813, 0x290123e9aab23b68), // -78 294 | (0x9436c0760c86e30b, 0xf9a0b6720aaf6521), // -77 295 | (0xb94470938fa89bce, 0xf808e40e8d5b3e69), // -76 296 | (0xe7958cb87392c2c2, 0xb60b1d1230b20e04), // -75 297 | (0x90bd77f3483bb9b9, 0xb1c6f22b5e6f48c2), // -74 298 | (0xb4ecd5f01a4aa828, 0x1e38aeb6360b1af3), // -73 299 | (0xe2280b6c20dd5232, 0x25c6da63c38de1b0), // -72 300 | (0x8d590723948a535f, 0x579c487e5a38ad0e), // -71 301 | (0xb0af48ec79ace837, 0x2d835a9df0c6d851), // -70 302 | (0xdcdb1b2798182244, 0xf8e431456cf88e65), // -69 303 | (0x8a08f0f8bf0f156b, 0x1b8e9ecb641b58ff), // -68 304 | (0xac8b2d36eed2dac5, 0xe272467e3d222f3f), // -67 305 | (0xd7adf884aa879177, 0x5b0ed81dcc6abb0f), // -66 306 | (0x86ccbb52ea94baea, 0x98e947129fc2b4e9), // -65 307 | (0xa87fea27a539e9a5, 0x3f2398d747b36224), // -64 308 | (0xd29fe4b18e88640e, 0x8eec7f0d19a03aad), // -63 309 | (0x83a3eeeef9153e89, 0x1953cf68300424ac), // -62 310 | (0xa48ceaaab75a8e2b, 0x5fa8c3423c052dd7), // -61 311 | (0xcdb02555653131b6, 0x3792f412cb06794d), // -60 312 | (0x808e17555f3ebf11, 0xe2bbd88bbee40bd0), // -59 313 | (0xa0b19d2ab70e6ed6, 0x5b6aceaeae9d0ec4), // -58 314 | (0xc8de047564d20a8b, 0xf245825a5a445275), // -57 315 | (0xfb158592be068d2e, 0xeed6e2f0f0d56712), // -56 316 | (0x9ced737bb6c4183d, 0x55464dd69685606b), // -55 317 | (0xc428d05aa4751e4c, 0xaa97e14c3c26b886), // -54 318 | (0xf53304714d9265df, 0xd53dd99f4b3066a8), // -53 319 | (0x993fe2c6d07b7fab, 0xe546a8038efe4029), // -52 320 | (0xbf8fdb78849a5f96, 0xde98520472bdd033), // -51 321 | (0xef73d256a5c0f77c, 0x963e66858f6d4440), // -50 322 | (0x95a8637627989aad, 0xdde7001379a44aa8), // -49 323 | (0xbb127c53b17ec159, 0x5560c018580d5d52), // -48 324 | (0xe9d71b689dde71af, 0xaab8f01e6e10b4a6), // -47 325 | (0x9226712162ab070d, 0xcab3961304ca70e8), // -46 326 | (0xb6b00d69bb55c8d1, 0x3d607b97c5fd0d22), // -45 327 | (0xe45c10c42a2b3b05, 0x8cb89a7db77c506a), // -44 328 | (0x8eb98a7a9a5b04e3, 0x77f3608e92adb242), // -43 329 | (0xb267ed1940f1c61c, 0x55f038b237591ed3), // -42 330 | (0xdf01e85f912e37a3, 0x6b6c46dec52f6688), // -41 331 | (0x8b61313bbabce2c6, 0x2323ac4b3b3da015), // -40 332 | (0xae397d8aa96c1b77, 0xabec975e0a0d081a), // -39 333 | (0xd9c7dced53c72255, 0x96e7bd358c904a21), // -38 334 | (0x881cea14545c7575, 0x7e50d64177da2e54), // -37 335 | (0xaa242499697392d2, 0xdde50bd1d5d0b9e9), // -36 336 | (0xd4ad2dbfc3d07787, 0x955e4ec64b44e864), // -35 337 | (0x84ec3c97da624ab4, 0xbd5af13bef0b113e), // -34 338 | (0xa6274bbdd0fadd61, 0xecb1ad8aeacdd58e), // -33 339 | (0xcfb11ead453994ba, 0x67de18eda5814af2), // -32 340 | (0x81ceb32c4b43fcf4, 0x80eacf948770ced7), // -31 341 | (0xa2425ff75e14fc31, 0xa1258379a94d028d), // -30 342 | (0xcad2f7f5359a3b3e, 0x096ee45813a04330), // -29 343 | (0xfd87b5f28300ca0d, 0x8bca9d6e188853fc), // -28 344 | (0x9e74d1b791e07e48, 0x775ea264cf55347d), // -27 345 | (0xc612062576589dda, 0x95364afe032a819d), // -26 346 | (0xf79687aed3eec551, 0x3a83ddbd83f52204), // -25 347 | (0x9abe14cd44753b52, 0xc4926a9672793542), // -24 348 | (0xc16d9a0095928a27, 0x75b7053c0f178293), // -23 349 | (0xf1c90080baf72cb1, 0x5324c68b12dd6338), // -22 350 | (0x971da05074da7bee, 0xd3f6fc16ebca5e03), // -21 351 | (0xbce5086492111aea, 0x88f4bb1ca6bcf584), // -20 352 | (0xec1e4a7db69561a5, 0x2b31e9e3d06c32e5), // -19 353 | (0x9392ee8e921d5d07, 0x3aff322e62439fcf), // -18 354 | (0xb877aa3236a4b449, 0x09befeb9fad487c2), // -17 355 | (0xe69594bec44de15b, 0x4c2ebe687989a9b3), // -16 356 | (0x901d7cf73ab0acd9, 0x0f9d37014bf60a10), // -15 357 | (0xb424dc35095cd80f, 0x538484c19ef38c94), // -14 358 | (0xe12e13424bb40e13, 0x2865a5f206b06fb9), // -13 359 | (0x8cbccc096f5088cb, 0xf93f87b7442e45d3), // -12 360 | (0xafebff0bcb24aafe, 0xf78f69a51539d748), // -11 361 | (0xdbe6fecebdedd5be, 0xb573440e5a884d1b), // -10 362 | (0x89705f4136b4a597, 0x31680a88f8953030), // -9 363 | (0xabcc77118461cefc, 0xfdc20d2b36ba7c3d), // -8 364 | (0xd6bf94d5e57a42bc, 0x3d32907604691b4c), // -7 365 | (0x8637bd05af6c69b5, 0xa63f9a49c2c1b10f), // -6 366 | (0xa7c5ac471b478423, 0x0fcf80dc33721d53), // -5 367 | (0xd1b71758e219652b, 0xd3c36113404ea4a8), // -4 368 | (0x83126e978d4fdf3b, 0x645a1cac083126e9), // -3 369 | (0xa3d70a3d70a3d70a, 0x3d70a3d70a3d70a3), // -2 370 | (0xcccccccccccccccc, 0xcccccccccccccccc), // -1 371 | (0x8000000000000000, 0x0000000000000000), // 0 372 | (0xa000000000000000, 0x0000000000000000), // 1 373 | (0xc800000000000000, 0x0000000000000000), // 2 374 | (0xfa00000000000000, 0x0000000000000000), // 3 375 | (0x9c40000000000000, 0x0000000000000000), // 4 376 | (0xc350000000000000, 0x0000000000000000), // 5 377 | (0xf424000000000000, 0x0000000000000000), // 6 378 | (0x9896800000000000, 0x0000000000000000), // 7 379 | (0xbebc200000000000, 0x0000000000000000), // 8 380 | (0xee6b280000000000, 0x0000000000000000), // 9 381 | (0x9502f90000000000, 0x0000000000000000), // 10 382 | (0xba43b74000000000, 0x0000000000000000), // 11 383 | (0xe8d4a51000000000, 0x0000000000000000), // 12 384 | (0x9184e72a00000000, 0x0000000000000000), // 13 385 | (0xb5e620f480000000, 0x0000000000000000), // 14 386 | (0xe35fa931a0000000, 0x0000000000000000), // 15 387 | (0x8e1bc9bf04000000, 0x0000000000000000), // 16 388 | (0xb1a2bc2ec5000000, 0x0000000000000000), // 17 389 | (0xde0b6b3a76400000, 0x0000000000000000), // 18 390 | (0x8ac7230489e80000, 0x0000000000000000), // 19 391 | (0xad78ebc5ac620000, 0x0000000000000000), // 20 392 | (0xd8d726b7177a8000, 0x0000000000000000), // 21 393 | (0x878678326eac9000, 0x0000000000000000), // 22 394 | (0xa968163f0a57b400, 0x0000000000000000), // 23 395 | (0xd3c21bcecceda100, 0x0000000000000000), // 24 396 | (0x84595161401484a0, 0x0000000000000000), // 25 397 | (0xa56fa5b99019a5c8, 0x0000000000000000), // 26 398 | (0xcecb8f27f4200f3a, 0x0000000000000000), // 27 399 | (0x813f3978f8940984, 0x4000000000000000), // 28 400 | (0xa18f07d736b90be5, 0x5000000000000000), // 29 401 | (0xc9f2c9cd04674ede, 0xa400000000000000), // 30 402 | (0xfc6f7c4045812296, 0x4d00000000000000), // 31 403 | (0x9dc5ada82b70b59d, 0xf020000000000000), // 32 404 | (0xc5371912364ce305, 0x6c28000000000000), // 33 405 | (0xf684df56c3e01bc6, 0xc732000000000000), // 34 406 | (0x9a130b963a6c115c, 0x3c7f400000000000), // 35 407 | (0xc097ce7bc90715b3, 0x4b9f100000000000), // 36 408 | (0xf0bdc21abb48db20, 0x1e86d40000000000), // 37 409 | (0x96769950b50d88f4, 0x1314448000000000), // 38 410 | (0xbc143fa4e250eb31, 0x17d955a000000000), // 39 411 | (0xeb194f8e1ae525fd, 0x5dcfab0800000000), // 40 412 | (0x92efd1b8d0cf37be, 0x5aa1cae500000000), // 41 413 | (0xb7abc627050305ad, 0xf14a3d9e40000000), // 42 414 | (0xe596b7b0c643c719, 0x6d9ccd05d0000000), // 43 415 | (0x8f7e32ce7bea5c6f, 0xe4820023a2000000), // 44 416 | (0xb35dbf821ae4f38b, 0xdda2802c8a800000), // 45 417 | (0xe0352f62a19e306e, 0xd50b2037ad200000), // 46 418 | (0x8c213d9da502de45, 0x4526f422cc340000), // 47 419 | (0xaf298d050e4395d6, 0x9670b12b7f410000), // 48 420 | (0xdaf3f04651d47b4c, 0x3c0cdd765f114000), // 49 421 | (0x88d8762bf324cd0f, 0xa5880a69fb6ac800), // 50 422 | (0xab0e93b6efee0053, 0x8eea0d047a457a00), // 51 423 | (0xd5d238a4abe98068, 0x72a4904598d6d880), // 52 424 | (0x85a36366eb71f041, 0x47a6da2b7f864750), // 53 425 | (0xa70c3c40a64e6c51, 0x999090b65f67d924), // 54 426 | (0xd0cf4b50cfe20765, 0xfff4b4e3f741cf6d), // 55 427 | (0x82818f1281ed449f, 0xbff8f10e7a8921a4), // 56 428 | (0xa321f2d7226895c7, 0xaff72d52192b6a0d), // 57 429 | (0xcbea6f8ceb02bb39, 0x9bf4f8a69f764490), // 58 430 | (0xfee50b7025c36a08, 0x02f236d04753d5b4), // 59 431 | (0x9f4f2726179a2245, 0x01d762422c946590), // 60 432 | (0xc722f0ef9d80aad6, 0x424d3ad2b7b97ef5), // 61 433 | (0xf8ebad2b84e0d58b, 0xd2e0898765a7deb2), // 62 434 | (0x9b934c3b330c8577, 0x63cc55f49f88eb2f), // 63 435 | (0xc2781f49ffcfa6d5, 0x3cbf6b71c76b25fb), // 64 436 | (0xf316271c7fc3908a, 0x8bef464e3945ef7a), // 65 437 | (0x97edd871cfda3a56, 0x97758bf0e3cbb5ac), // 66 438 | (0xbde94e8e43d0c8ec, 0x3d52eeed1cbea317), // 67 439 | (0xed63a231d4c4fb27, 0x4ca7aaa863ee4bdd), // 68 440 | (0x945e455f24fb1cf8, 0x8fe8caa93e74ef6a), // 69 441 | (0xb975d6b6ee39e436, 0xb3e2fd538e122b44), // 70 442 | (0xe7d34c64a9c85d44, 0x60dbbca87196b616), // 71 443 | (0x90e40fbeea1d3a4a, 0xbc8955e946fe31cd), // 72 444 | (0xb51d13aea4a488dd, 0x6babab6398bdbe41), // 73 445 | (0xe264589a4dcdab14, 0xc696963c7eed2dd1), // 74 446 | (0x8d7eb76070a08aec, 0xfc1e1de5cf543ca2), // 75 447 | (0xb0de65388cc8ada8, 0x3b25a55f43294bcb), // 76 448 | (0xdd15fe86affad912, 0x49ef0eb713f39ebe), // 77 449 | (0x8a2dbf142dfcc7ab, 0x6e3569326c784337), // 78 450 | (0xacb92ed9397bf996, 0x49c2c37f07965404), // 79 451 | (0xd7e77a8f87daf7fb, 0xdc33745ec97be906), // 80 452 | (0x86f0ac99b4e8dafd, 0x69a028bb3ded71a3), // 81 453 | (0xa8acd7c0222311bc, 0xc40832ea0d68ce0c), // 82 454 | (0xd2d80db02aabd62b, 0xf50a3fa490c30190), // 83 455 | (0x83c7088e1aab65db, 0x792667c6da79e0fa), // 84 456 | (0xa4b8cab1a1563f52, 0x577001b891185938), // 85 457 | (0xcde6fd5e09abcf26, 0xed4c0226b55e6f86), // 86 458 | (0x80b05e5ac60b6178, 0x544f8158315b05b4), // 87 459 | (0xa0dc75f1778e39d6, 0x696361ae3db1c721), // 88 460 | (0xc913936dd571c84c, 0x03bc3a19cd1e38e9), // 89 461 | (0xfb5878494ace3a5f, 0x04ab48a04065c723), // 90 462 | (0x9d174b2dcec0e47b, 0x62eb0d64283f9c76), // 91 463 | (0xc45d1df942711d9a, 0x3ba5d0bd324f8394), // 92 464 | (0xf5746577930d6500, 0xca8f44ec7ee36479), // 93 465 | (0x9968bf6abbe85f20, 0x7e998b13cf4e1ecb), // 94 466 | (0xbfc2ef456ae276e8, 0x9e3fedd8c321a67e), // 95 467 | (0xefb3ab16c59b14a2, 0xc5cfe94ef3ea101e), // 96 468 | (0x95d04aee3b80ece5, 0xbba1f1d158724a12), // 97 469 | (0xbb445da9ca61281f, 0x2a8a6e45ae8edc97), // 98 470 | (0xea1575143cf97226, 0xf52d09d71a3293bd), // 99 471 | (0x924d692ca61be758, 0x593c2626705f9c56), // 100 472 | (0xb6e0c377cfa2e12e, 0x6f8b2fb00c77836c), // 101 473 | (0xe498f455c38b997a, 0x0b6dfb9c0f956447), // 102 474 | (0x8edf98b59a373fec, 0x4724bd4189bd5eac), // 103 475 | (0xb2977ee300c50fe7, 0x58edec91ec2cb657), // 104 476 | (0xdf3d5e9bc0f653e1, 0x2f2967b66737e3ed), // 105 477 | (0x8b865b215899f46c, 0xbd79e0d20082ee74), // 106 478 | (0xae67f1e9aec07187, 0xecd8590680a3aa11), // 107 479 | (0xda01ee641a708de9, 0xe80e6f4820cc9495), // 108 480 | (0x884134fe908658b2, 0x3109058d147fdcdd), // 109 481 | (0xaa51823e34a7eede, 0xbd4b46f0599fd415), // 110 482 | (0xd4e5e2cdc1d1ea96, 0x6c9e18ac7007c91a), // 111 483 | (0x850fadc09923329e, 0x03e2cf6bc604ddb0), // 112 484 | (0xa6539930bf6bff45, 0x84db8346b786151c), // 113 485 | (0xcfe87f7cef46ff16, 0xe612641865679a63), // 114 486 | (0x81f14fae158c5f6e, 0x4fcb7e8f3f60c07e), // 115 487 | (0xa26da3999aef7749, 0xe3be5e330f38f09d), // 116 488 | (0xcb090c8001ab551c, 0x5cadf5bfd3072cc5), // 117 489 | (0xfdcb4fa002162a63, 0x73d9732fc7c8f7f6), // 118 490 | (0x9e9f11c4014dda7e, 0x2867e7fddcdd9afa), // 119 491 | (0xc646d63501a1511d, 0xb281e1fd541501b8), // 120 492 | (0xf7d88bc24209a565, 0x1f225a7ca91a4226), // 121 493 | (0x9ae757596946075f, 0x3375788de9b06958), // 122 494 | (0xc1a12d2fc3978937, 0x0052d6b1641c83ae), // 123 495 | (0xf209787bb47d6b84, 0xc0678c5dbd23a49a), // 124 496 | (0x9745eb4d50ce6332, 0xf840b7ba963646e0), // 125 497 | (0xbd176620a501fbff, 0xb650e5a93bc3d898), // 126 498 | (0xec5d3fa8ce427aff, 0xa3e51f138ab4cebe), // 127 499 | (0x93ba47c980e98cdf, 0xc66f336c36b10137), // 128 500 | (0xb8a8d9bbe123f017, 0xb80b0047445d4184), // 129 501 | (0xe6d3102ad96cec1d, 0xa60dc059157491e5), // 130 502 | (0x9043ea1ac7e41392, 0x87c89837ad68db2f), // 131 503 | (0xb454e4a179dd1877, 0x29babe4598c311fb), // 132 504 | (0xe16a1dc9d8545e94, 0xf4296dd6fef3d67a), // 133 505 | (0x8ce2529e2734bb1d, 0x1899e4a65f58660c), // 134 506 | (0xb01ae745b101e9e4, 0x5ec05dcff72e7f8f), // 135 507 | (0xdc21a1171d42645d, 0x76707543f4fa1f73), // 136 508 | (0x899504ae72497eba, 0x6a06494a791c53a8), // 137 509 | (0xabfa45da0edbde69, 0x0487db9d17636892), // 138 510 | (0xd6f8d7509292d603, 0x45a9d2845d3c42b6), // 139 511 | (0x865b86925b9bc5c2, 0x0b8a2392ba45a9b2), // 140 512 | (0xa7f26836f282b732, 0x8e6cac7768d7141e), // 141 513 | (0xd1ef0244af2364ff, 0x3207d795430cd926), // 142 514 | (0x8335616aed761f1f, 0x7f44e6bd49e807b8), // 143 515 | (0xa402b9c5a8d3a6e7, 0x5f16206c9c6209a6), // 144 516 | (0xcd036837130890a1, 0x36dba887c37a8c0f), // 145 517 | (0x802221226be55a64, 0xc2494954da2c9789), // 146 518 | (0xa02aa96b06deb0fd, 0xf2db9baa10b7bd6c), // 147 519 | (0xc83553c5c8965d3d, 0x6f92829494e5acc7), // 148 520 | (0xfa42a8b73abbf48c, 0xcb772339ba1f17f9), // 149 521 | (0x9c69a97284b578d7, 0xff2a760414536efb), // 150 522 | (0xc38413cf25e2d70d, 0xfef5138519684aba), // 151 523 | (0xf46518c2ef5b8cd1, 0x7eb258665fc25d69), // 152 524 | (0x98bf2f79d5993802, 0xef2f773ffbd97a61), // 153 525 | (0xbeeefb584aff8603, 0xaafb550ffacfd8fa), // 154 526 | (0xeeaaba2e5dbf6784, 0x95ba2a53f983cf38), // 155 527 | (0x952ab45cfa97a0b2, 0xdd945a747bf26183), // 156 528 | (0xba756174393d88df, 0x94f971119aeef9e4), // 157 529 | (0xe912b9d1478ceb17, 0x7a37cd5601aab85d), // 158 530 | (0x91abb422ccb812ee, 0xac62e055c10ab33a), // 159 531 | (0xb616a12b7fe617aa, 0x577b986b314d6009), // 160 532 | (0xe39c49765fdf9d94, 0xed5a7e85fda0b80b), // 161 533 | (0x8e41ade9fbebc27d, 0x14588f13be847307), // 162 534 | (0xb1d219647ae6b31c, 0x596eb2d8ae258fc8), // 163 535 | (0xde469fbd99a05fe3, 0x6fca5f8ed9aef3bb), // 164 536 | (0x8aec23d680043bee, 0x25de7bb9480d5854), // 165 537 | (0xada72ccc20054ae9, 0xaf561aa79a10ae6a), // 166 538 | (0xd910f7ff28069da4, 0x1b2ba1518094da04), // 167 539 | (0x87aa9aff79042286, 0x90fb44d2f05d0842), // 168 540 | (0xa99541bf57452b28, 0x353a1607ac744a53), // 169 541 | (0xd3fa922f2d1675f2, 0x42889b8997915ce8), // 170 542 | (0x847c9b5d7c2e09b7, 0x69956135febada11), // 171 543 | (0xa59bc234db398c25, 0x43fab9837e699095), // 172 544 | (0xcf02b2c21207ef2e, 0x94f967e45e03f4bb), // 173 545 | (0x8161afb94b44f57d, 0x1d1be0eebac278f5), // 174 546 | (0xa1ba1ba79e1632dc, 0x6462d92a69731732), // 175 547 | (0xca28a291859bbf93, 0x7d7b8f7503cfdcfe), // 176 548 | (0xfcb2cb35e702af78, 0x5cda735244c3d43e), // 177 549 | (0x9defbf01b061adab, 0x3a0888136afa64a7), // 178 550 | (0xc56baec21c7a1916, 0x088aaa1845b8fdd0), // 179 551 | (0xf6c69a72a3989f5b, 0x8aad549e57273d45), // 180 552 | (0x9a3c2087a63f6399, 0x36ac54e2f678864b), // 181 553 | (0xc0cb28a98fcf3c7f, 0x84576a1bb416a7dd), // 182 554 | (0xf0fdf2d3f3c30b9f, 0x656d44a2a11c51d5), // 183 555 | (0x969eb7c47859e743, 0x9f644ae5a4b1b325), // 184 556 | (0xbc4665b596706114, 0x873d5d9f0dde1fee), // 185 557 | (0xeb57ff22fc0c7959, 0xa90cb506d155a7ea), // 186 558 | (0x9316ff75dd87cbd8, 0x09a7f12442d588f2), // 187 559 | (0xb7dcbf5354e9bece, 0x0c11ed6d538aeb2f), // 188 560 | (0xe5d3ef282a242e81, 0x8f1668c8a86da5fa), // 189 561 | (0x8fa475791a569d10, 0xf96e017d694487bc), // 190 562 | (0xb38d92d760ec4455, 0x37c981dcc395a9ac), // 191 563 | (0xe070f78d3927556a, 0x85bbe253f47b1417), // 192 564 | (0x8c469ab843b89562, 0x93956d7478ccec8e), // 193 565 | (0xaf58416654a6babb, 0x387ac8d1970027b2), // 194 566 | (0xdb2e51bfe9d0696a, 0x06997b05fcc0319e), // 195 567 | (0x88fcf317f22241e2, 0x441fece3bdf81f03), // 196 568 | (0xab3c2fddeeaad25a, 0xd527e81cad7626c3), // 197 569 | (0xd60b3bd56a5586f1, 0x8a71e223d8d3b074), // 198 570 | (0x85c7056562757456, 0xf6872d5667844e49), // 199 571 | (0xa738c6bebb12d16c, 0xb428f8ac016561db), // 200 572 | (0xd106f86e69d785c7, 0xe13336d701beba52), // 201 573 | (0x82a45b450226b39c, 0xecc0024661173473), // 202 574 | (0xa34d721642b06084, 0x27f002d7f95d0190), // 203 575 | (0xcc20ce9bd35c78a5, 0x31ec038df7b441f4), // 204 576 | (0xff290242c83396ce, 0x7e67047175a15271), // 205 577 | (0x9f79a169bd203e41, 0x0f0062c6e984d386), // 206 578 | (0xc75809c42c684dd1, 0x52c07b78a3e60868), // 207 579 | (0xf92e0c3537826145, 0xa7709a56ccdf8a82), // 208 580 | (0x9bbcc7a142b17ccb, 0x88a66076400bb691), // 209 581 | (0xc2abf989935ddbfe, 0x6acff893d00ea435), // 210 582 | (0xf356f7ebf83552fe, 0x0583f6b8c4124d43), // 211 583 | (0x98165af37b2153de, 0xc3727a337a8b704a), // 212 584 | (0xbe1bf1b059e9a8d6, 0x744f18c0592e4c5c), // 213 585 | (0xeda2ee1c7064130c, 0x1162def06f79df73), // 214 586 | (0x9485d4d1c63e8be7, 0x8addcb5645ac2ba8), // 215 587 | (0xb9a74a0637ce2ee1, 0x6d953e2bd7173692), // 216 588 | (0xe8111c87c5c1ba99, 0xc8fa8db6ccdd0437), // 217 589 | (0x910ab1d4db9914a0, 0x1d9c9892400a22a2), // 218 590 | (0xb54d5e4a127f59c8, 0x2503beb6d00cab4b), // 219 591 | (0xe2a0b5dc971f303a, 0x2e44ae64840fd61d), // 220 592 | (0x8da471a9de737e24, 0x5ceaecfed289e5d2), // 221 593 | (0xb10d8e1456105dad, 0x7425a83e872c5f47), // 222 594 | (0xdd50f1996b947518, 0xd12f124e28f77719), // 223 595 | (0x8a5296ffe33cc92f, 0x82bd6b70d99aaa6f), // 224 596 | (0xace73cbfdc0bfb7b, 0x636cc64d1001550b), // 225 597 | (0xd8210befd30efa5a, 0x3c47f7e05401aa4e), // 226 598 | (0x8714a775e3e95c78, 0x65acfaec34810a71), // 227 599 | (0xa8d9d1535ce3b396, 0x7f1839a741a14d0d), // 228 600 | (0xd31045a8341ca07c, 0x1ede48111209a050), // 229 601 | (0x83ea2b892091e44d, 0x934aed0aab460432), // 230 602 | (0xa4e4b66b68b65d60, 0xf81da84d5617853f), // 231 603 | (0xce1de40642e3f4b9, 0x36251260ab9d668e), // 232 604 | (0x80d2ae83e9ce78f3, 0xc1d72b7c6b426019), // 233 605 | (0xa1075a24e4421730, 0xb24cf65b8612f81f), // 234 606 | (0xc94930ae1d529cfc, 0xdee033f26797b627), // 235 607 | (0xfb9b7cd9a4a7443c, 0x169840ef017da3b1), // 236 608 | (0x9d412e0806e88aa5, 0x8e1f289560ee864e), // 237 609 | (0xc491798a08a2ad4e, 0xf1a6f2bab92a27e2), // 238 610 | (0xf5b5d7ec8acb58a2, 0xae10af696774b1db), // 239 611 | (0x9991a6f3d6bf1765, 0xacca6da1e0a8ef29), // 240 612 | (0xbff610b0cc6edd3f, 0x17fd090a58d32af3), // 241 613 | (0xeff394dcff8a948e, 0xddfc4b4cef07f5b0), // 242 614 | (0x95f83d0a1fb69cd9, 0x4abdaf101564f98e), // 243 615 | (0xbb764c4ca7a4440f, 0x9d6d1ad41abe37f1), // 244 616 | (0xea53df5fd18d5513, 0x84c86189216dc5ed), // 245 617 | (0x92746b9be2f8552c, 0x32fd3cf5b4e49bb4), // 246 618 | (0xb7118682dbb66a77, 0x3fbc8c33221dc2a1), // 247 619 | (0xe4d5e82392a40515, 0x0fabaf3feaa5334a), // 248 620 | (0x8f05b1163ba6832d, 0x29cb4d87f2a7400e), // 249 621 | (0xb2c71d5bca9023f8, 0x743e20e9ef511012), // 250 622 | (0xdf78e4b2bd342cf6, 0x914da9246b255416), // 251 623 | (0x8bab8eefb6409c1a, 0x1ad089b6c2f7548e), // 252 624 | (0xae9672aba3d0c320, 0xa184ac2473b529b1), // 253 625 | (0xda3c0f568cc4f3e8, 0xc9e5d72d90a2741e), // 254 626 | (0x8865899617fb1871, 0x7e2fa67c7a658892), // 255 627 | (0xaa7eebfb9df9de8d, 0xddbb901b98feeab7), // 256 628 | (0xd51ea6fa85785631, 0x552a74227f3ea565), // 257 629 | (0x8533285c936b35de, 0xd53a88958f87275f), // 258 630 | (0xa67ff273b8460356, 0x8a892abaf368f137), // 259 631 | (0xd01fef10a657842c, 0x2d2b7569b0432d85), // 260 632 | (0x8213f56a67f6b29b, 0x9c3b29620e29fc73), // 261 633 | (0xa298f2c501f45f42, 0x8349f3ba91b47b8f), // 262 634 | (0xcb3f2f7642717713, 0x241c70a936219a73), // 263 635 | (0xfe0efb53d30dd4d7, 0xed238cd383aa0110), // 264 636 | (0x9ec95d1463e8a506, 0xf4363804324a40aa), // 265 637 | (0xc67bb4597ce2ce48, 0xb143c6053edcd0d5), // 266 638 | (0xf81aa16fdc1b81da, 0xdd94b7868e94050a), // 267 639 | (0x9b10a4e5e9913128, 0xca7cf2b4191c8326), // 268 640 | (0xc1d4ce1f63f57d72, 0xfd1c2f611f63a3f0), // 269 641 | (0xf24a01a73cf2dccf, 0xbc633b39673c8cec), // 270 642 | (0x976e41088617ca01, 0xd5be0503e085d813), // 271 643 | (0xbd49d14aa79dbc82, 0x4b2d8644d8a74e18), // 272 644 | (0xec9c459d51852ba2, 0xddf8e7d60ed1219e), // 273 645 | (0x93e1ab8252f33b45, 0xcabb90e5c942b503), // 274 646 | (0xb8da1662e7b00a17, 0x3d6a751f3b936243), // 275 647 | (0xe7109bfba19c0c9d, 0x0cc512670a783ad4), // 276 648 | (0x906a617d450187e2, 0x27fb2b80668b24c5), // 277 649 | (0xb484f9dc9641e9da, 0xb1f9f660802dedf6), // 278 650 | (0xe1a63853bbd26451, 0x5e7873f8a0396973), // 279 651 | (0x8d07e33455637eb2, 0xdb0b487b6423e1e8), // 280 652 | (0xb049dc016abc5e5f, 0x91ce1a9a3d2cda62), // 281 653 | (0xdc5c5301c56b75f7, 0x7641a140cc7810fb), // 282 654 | (0x89b9b3e11b6329ba, 0xa9e904c87fcb0a9d), // 283 655 | (0xac2820d9623bf429, 0x546345fa9fbdcd44), // 284 656 | (0xd732290fbacaf133, 0xa97c177947ad4095), // 285 657 | (0x867f59a9d4bed6c0, 0x49ed8eabcccc485d), // 286 658 | (0xa81f301449ee8c70, 0x5c68f256bfff5a74), // 287 659 | (0xd226fc195c6a2f8c, 0x73832eec6fff3111), // 288 660 | (0x83585d8fd9c25db7, 0xc831fd53c5ff7eab), // 289 661 | (0xa42e74f3d032f525, 0xba3e7ca8b77f5e55), // 290 662 | (0xcd3a1230c43fb26f, 0x28ce1bd2e55f35eb), // 291 663 | (0x80444b5e7aa7cf85, 0x7980d163cf5b81b3), // 292 664 | (0xa0555e361951c366, 0xd7e105bcc332621f), // 293 665 | (0xc86ab5c39fa63440, 0x8dd9472bf3fefaa7), // 294 666 | (0xfa856334878fc150, 0xb14f98f6f0feb951), // 295 667 | (0x9c935e00d4b9d8d2, 0x6ed1bf9a569f33d3), // 296 668 | (0xc3b8358109e84f07, 0x0a862f80ec4700c8), // 297 669 | (0xf4a642e14c6262c8, 0xcd27bb612758c0fa), // 298 670 | (0x98e7e9cccfbd7dbd, 0x8038d51cb897789c), // 299 671 | (0xbf21e44003acdd2c, 0xe0470a63e6bd56c3), // 300 672 | (0xeeea5d5004981478, 0x1858ccfce06cac74), // 301 673 | (0x95527a5202df0ccb, 0x0f37801e0c43ebc8), // 302 674 | (0xbaa718e68396cffd, 0xd30560258f54e6ba), // 303 675 | (0xe950df20247c83fd, 0x47c6b82ef32a2069), // 304 676 | (0x91d28b7416cdd27e, 0x4cdc331d57fa5441), // 305 677 | (0xb6472e511c81471d, 0xe0133fe4adf8e952), // 306 678 | (0xe3d8f9e563a198e5, 0x58180fddd97723a6), // 307 679 | (0x8e679c2f5e44ff8f, 0x570f09eaa7ea7648), // 308 680 | (0xb201833b35d63f73, 0x2cd2cc6551e513da), // 309 681 | (0xde81e40a034bcf4f, 0xf8077f7ea65e58d1), // 310 682 | (0x8b112e86420f6191, 0xfb04afaf27faf782), // 311 683 | (0xadd57a27d29339f6, 0x79c5db9af1f9b563), // 312 684 | (0xd94ad8b1c7380874, 0x18375281ae7822bc), // 313 685 | (0x87cec76f1c830548, 0x8f2293910d0b15b5), // 314 686 | (0xa9c2794ae3a3c69a, 0xb2eb3875504ddb22), // 315 687 | (0xd433179d9c8cb841, 0x5fa60692a46151eb), // 316 688 | (0x849feec281d7f328, 0xdbc7c41ba6bcd333), // 317 689 | (0xa5c7ea73224deff3, 0x12b9b522906c0800), // 318 690 | (0xcf39e50feae16bef, 0xd768226b34870a00), // 319 691 | (0x81842f29f2cce375, 0xe6a1158300d46640), // 320 692 | (0xa1e53af46f801c53, 0x60495ae3c1097fd0), // 321 693 | (0xca5e89b18b602368, 0x385bb19cb14bdfc4), // 322 694 | (0xfcf62c1dee382c42, 0x46729e03dd9ed7b5), // 323 695 | (0x9e19db92b4e31ba9, 0x6c07a2c26a8346d1), // 324 696 | ]; 697 | 698 | // Computes 128-bit result of multiplication of two 64-bit unsigned integers. 699 | #[cfg_attr(feature = "no-panic", no_panic)] 700 | fn umul128(x: u64, y: u64) -> u128 { 701 | u128::from(x) * u128::from(y) 702 | } 703 | 704 | #[cfg_attr(feature = "no-panic", no_panic)] 705 | fn umul192_upper128(x_hi: u64, x_lo: u64, y: u64) -> uint128 { 706 | let p = umul128(x_hi, y); 707 | let lo = (p as u64).wrapping_add((umul128(x_lo, y) >> 64) as u64); 708 | uint128 { 709 | hi: (p >> 64) as u64 + u64::from(lo < p as u64), 710 | lo, 711 | } 712 | } 713 | 714 | // Computes upper 64 bits of multiplication of x and y, discards the least 715 | // significant bit and rounds to odd, where x = uint128_t(x_hi << 64) | x_lo. 716 | #[cfg_attr(feature = "no-panic", no_panic)] 717 | fn umul_upper_inexact_to_odd(x_hi: u64, x_lo: u64, y: UInt) -> UInt 718 | where 719 | UInt: traits::UInt, 720 | { 721 | let num_bits = mem::size_of::() * 8; 722 | if num_bits == 64 { 723 | let uint128 { hi, lo } = umul192_upper128(x_hi, x_lo, y.into()); 724 | UInt::truncate(hi | u64::from((lo >> 1) != 0)) 725 | } else { 726 | let result = (umul128(x_hi, y.into()) >> 32) as u64; 727 | UInt::enlarge((result >> 32) as u32 | u32::from((result as u32 >> 1) != 0)) 728 | } 729 | } 730 | 731 | // Returns {value / 100, value % 100} correct for values of up to 4 digits. 732 | fn divmod100(value: u32) -> (u32, u32) { 733 | debug_assert!(value < 10_000); 734 | const EXP: u32 = 19; // 19 is faster or equal to 12 even for 3 digits. 735 | const SIG: u32 = (1 << EXP) / 100 + 1; 736 | let div = (value * SIG) >> EXP; // value / 100 737 | (div, value - div * 100) 738 | } 739 | 740 | #[cfg_attr(feature = "no-panic", no_panic)] 741 | fn count_trailing_nonzeros(x: u64) -> usize { 742 | // We count the number of bytes until there are only zeros left. 743 | // The code is equivalent to 744 | // 8 - x.leading_zeros() / 8 745 | // but if the BSR instruction is emitted (as gcc on x64 does with default 746 | // settings), subtracting the constant before dividing allows the compiler 747 | // to combine it with the subtraction which it inserts due to BSR counting 748 | // in the opposite direction. 749 | // 750 | // Additionally, the BSR instruction requires a zero check. Since the high 751 | // bit is unused we can avoid the zero check by shifting the datum left by 752 | // one and inserting a sentinel bit at the end. This can be faster than the 753 | // automatically inserted range check. 754 | (70 - ((x.to_le() << 1) | 1).leading_zeros()) as usize / 8 755 | } 756 | 757 | // Align data since unaligned access may be slower when crossing a 758 | // hardware-specific boundary. 759 | #[repr(align(2))] 760 | struct Digits2([u8; 200]); 761 | 762 | static DIGITS2: Digits2 = Digits2( 763 | *b"0001020304050607080910111213141516171819\ 764 | 2021222324252627282930313233343536373839\ 765 | 4041424344454647484950515253545556575859\ 766 | 6061626364656667686970717273747576777879\ 767 | 8081828384858687888990919293949596979899", 768 | ); 769 | 770 | // Converts value in the range [0, 100) to a string. GCC generates a bit better 771 | // code when value is pointer-size (https://www.godbolt.org/z/5fEPMT1cc). 772 | #[cfg_attr(feature = "no-panic", no_panic)] 773 | unsafe fn digits2(value: usize) -> &'static u16 { 774 | debug_assert!(value < 100); 775 | 776 | #[allow(clippy::cast_ptr_alignment)] 777 | unsafe { 778 | &*DIGITS2.0.as_ptr().cast::().add(value) 779 | } 780 | } 781 | 782 | #[cfg_attr(feature = "no-panic", no_panic)] 783 | fn to_bcd8(abcdefgh: u64) -> u64 { 784 | // An optimization from Xiang JunBo. 785 | // Three steps BCD. Base 10000 -> base 100 -> base 10. 786 | // div and mod are evaluated simultaneously as, e.g. 787 | // (abcdefgh / 10000) << 32 + (abcdefgh % 10000) 788 | // == abcdefgh + (2^32 - 10000) * (abcdefgh / 10000))) 789 | // where the division on the RHS is implemented by the usual multiply + shift 790 | // trick and the fractional bits are masked away. 791 | let abcd_efgh = abcdefgh + (0x100000000 - 10000) * ((abcdefgh * 0x68db8bb) >> 40); 792 | let ab_cd_ef_gh = abcd_efgh + (0x10000 - 100) * (((abcd_efgh * 0x147b) >> 19) & 0x7f0000007f); 793 | let a_b_c_d_e_f_g_h = 794 | ab_cd_ef_gh + (0x100 - 10) * (((ab_cd_ef_gh * 0x67) >> 10) & 0xf000f000f000f); 795 | a_b_c_d_e_f_g_h.to_be() 796 | } 797 | 798 | unsafe fn write_if_nonzero(buffer: *mut u8, digit: u32) -> *mut u8 { 799 | unsafe { 800 | *buffer = b'0' + digit as u8; 801 | buffer.add(usize::from(digit != 0)) 802 | } 803 | } 804 | 805 | unsafe fn write8(buffer: *mut u8, value: u64) { 806 | unsafe { 807 | buffer.cast::().write_unaligned(value); 808 | } 809 | } 810 | 811 | const ZEROS: u64 = 0x30303030_30303030; // 0x30 == '0' 812 | 813 | // Writes a significand consisting of up to 17 decimal digits (16-17 for 814 | // normals) and removes trailing zeros. 815 | #[cfg_attr(feature = "no-panic", no_panic)] 816 | unsafe fn write_significand17(mut buffer: *mut u8, value: u64) -> *mut u8 { 817 | // Each digits is denoted by a letter so value is abbccddeeffgghhii where 818 | // digit a can be zero. 819 | let abbccddee = (value / 100_000_000) as u32; 820 | let ffgghhii = (value % 100_000_000) as u32; 821 | unsafe { 822 | buffer = write_if_nonzero(buffer, abbccddee / 100_000_000); 823 | } 824 | let bcd = to_bcd8(u64::from(abbccddee % 100_000_000)); 825 | unsafe { 826 | write8(buffer, bcd | ZEROS); 827 | } 828 | if ffgghhii == 0 { 829 | return unsafe { buffer.add(count_trailing_nonzeros(bcd)) }; 830 | } 831 | let bcd = to_bcd8(u64::from(ffgghhii)); 832 | unsafe { 833 | write8(buffer.add(8), bcd | ZEROS); 834 | buffer.add(8).add(count_trailing_nonzeros(bcd)) 835 | } 836 | } 837 | 838 | // Writes a significand consisting of up to 9 decimal digits (8-9 for normals) 839 | // and removes trailing zeros. 840 | #[cfg_attr(feature = "no-panic", no_panic)] 841 | unsafe fn write_significand9(mut buffer: *mut u8, value: u32) -> *mut u8 { 842 | unsafe { 843 | buffer = write_if_nonzero(buffer, value / 100_000_000); 844 | } 845 | let bcd = to_bcd8(u64::from(value % 100_000_000)); 846 | unsafe { 847 | write8(buffer, bcd | ZEROS); 848 | buffer.add(count_trailing_nonzeros(bcd)) 849 | } 850 | } 851 | 852 | #[allow(non_camel_case_types)] 853 | struct fp { 854 | sig: u64, 855 | exp: i32, 856 | } 857 | 858 | fn normalize(mut dec: fp, subnormal: bool) -> fp 859 | where 860 | UInt: traits::UInt, 861 | { 862 | if !subnormal { 863 | return dec; 864 | } 865 | let num_bits = mem::size_of::() * 8; 866 | while dec.sig 867 | < if num_bits == 64 { 868 | 10_000_000_000_000_000 869 | } else { 870 | 100_000_000 871 | } 872 | { 873 | dec.sig *= 10; 874 | dec.exp -= 1; 875 | } 876 | dec 877 | } 878 | 879 | // Converts a binary FP number bin_sig * 2**bin_exp to the shortest decimal 880 | // representation. 881 | #[cfg_attr(feature = "no-panic", no_panic)] 882 | fn to_decimal(bin_sig: UInt, bin_exp: i32, regular: bool, subnormal: bool) -> fp 883 | where 884 | UInt: traits::UInt, 885 | { 886 | // Compute the decimal exponent as floor(log10(2**bin_exp)) if regular or 887 | // floor(log10(3/4 * 2**bin_exp)) otherwise, without branching. 888 | // log10_3_over_4_sig = round(log10(3/4) * 2**log10_2_exp) 889 | const LOG10_3_OVER_4_SIG: i32 = -131_008; 890 | // log10_2_sig = round(log10(2) * 2**log10_2_exp) 891 | const LOG10_2_SIG: i32 = 315_653; 892 | const LOG10_2_EXP: i32 = 20; 893 | debug_assert!((-1334..=2620).contains(&bin_exp)); 894 | let dec_exp = (bin_exp * LOG10_2_SIG + i32::from(!regular) * LOG10_3_OVER_4_SIG) >> LOG10_2_EXP; 895 | 896 | const DEC_EXP_MIN: i32 = -292; 897 | let (mut pow10_hi, mut pow10_lo) = 898 | *unsafe { POW10_SIGNIFICANDS.get_unchecked((-dec_exp - DEC_EXP_MIN) as usize) }; 899 | 900 | // log2_pow10_sig = round(log2(10) * 2**log2_pow10_exp) + 1 901 | const LOG2_POW10_SIG: i32 = 217_707; 902 | const LOG2_POW10_EXP: i32 = 16; 903 | debug_assert!((-350..=350).contains(&dec_exp)); 904 | // pow10_bin_exp = floor(log2(10**-dec_exp)) 905 | let pow10_bin_exp = (-dec_exp * LOG2_POW10_SIG) >> LOG2_POW10_EXP; 906 | // pow10 = ((pow10_hi << 64) | pow10_lo) * 2**(pow10_bin_exp - 127) 907 | 908 | // Shift to ensure the intermediate result of multiplying by a power of 10 909 | // has a fixed 128-bit fractional part. For example, 3 * 2**59 and 3 * 2**60 910 | // both have dec_exp = 2 and dividing them by 10**dec_exp would have the 911 | // decimal point in different (bit) positions without the shift: 912 | // 3 * 2**59 / 100 = 1.72...e+16 (exp_shift = 1 + 1) 913 | // 3 * 2**60 / 100 = 3.45...e+16 (exp_shift = 2 + 1) 914 | let exp_shift = bin_exp + pow10_bin_exp + 1; 915 | 916 | let num_bits = mem::size_of::() as i32 * 8; 917 | if regular && !subnormal { 918 | let integral; 919 | let fractional; 920 | if num_bits == 64 { 921 | let result = umul192_upper128(pow10_hi, pow10_lo, (bin_sig << exp_shift).into()); 922 | integral = UInt::truncate(result.hi); 923 | fractional = result.lo; 924 | } else { 925 | let result = umul128(pow10_hi, (bin_sig << exp_shift).into()); 926 | integral = UInt::truncate((result >> 64) as u64); 927 | fractional = result as u64; 928 | } 929 | let digit = integral.into() % 10; 930 | 931 | // Switch to a fixed-point representation with the least significant 932 | // integral digit in the upper bits and fractional digits in the lower 933 | // bits. 934 | let num_integral_bits = if num_bits == 64 { 4 } else { 32 }; 935 | let num_fractional_bits = 64 - num_integral_bits; 936 | let ten = 10u64 << num_fractional_bits; 937 | // Fixed-point remainder of the scaled significand modulo 10. 938 | let rem10 = (digit << num_fractional_bits) | (fractional >> num_integral_bits); 939 | // dec_exp is chosen so that 10**dec_exp <= 2**bin_exp < 10**(dec_exp + 1). 940 | // Since 1ulp == 2**bin_exp it will be in the range [1, 10) after scaling 941 | // by 10**dec_exp. Add 1 to combine the shift with division by two. 942 | let half_ulp10 = pow10_hi >> (num_integral_bits - exp_shift + 1); 943 | let upper = rem10 + half_ulp10; 944 | 945 | // An optimization from yy by Yaoyuan Guo: 946 | if { 947 | // Exact half-ulp tie when rounding to nearest integer. 948 | fractional != (1 << 63) && 949 | // Exact half-ulp tie when rounding to nearest 10. 950 | rem10 != half_ulp10 && 951 | // Near-boundary case for rounding to nearest 10. 952 | ten.wrapping_sub(upper) > 1 953 | } { 954 | let round = (upper >> num_fractional_bits) >= 10; 955 | let shorter = integral.into() - digit + u64::from(round) * 10; 956 | let longer = integral.into() + u64::from(fractional >= (1 << 63)); 957 | return fp { 958 | sig: if rem10 <= half_ulp10 || round { 959 | shorter 960 | } else { 961 | longer 962 | }, 963 | exp: dec_exp, 964 | }; 965 | } 966 | } 967 | 968 | // Fallback to Schubfach to guarantee correctness in boundary cases and 969 | // switch to strict overestimates of powers of 10. 970 | if num_bits == 64 { 971 | pow10_lo += 1; 972 | } else { 973 | pow10_hi += 1; 974 | } 975 | 976 | // Shift the significand so that boundaries are integer. 977 | const BOUND_SHIFT: u32 = 2; 978 | let bin_sig_shifted = bin_sig << BOUND_SHIFT; 979 | 980 | // Compute the estimates of lower and upper bounds of the rounding interval 981 | // by multiplying them by the power of 10 and applying modified rounding. 982 | let lsb = bin_sig & UInt::from(1); 983 | let lower = (bin_sig_shifted - (UInt::from(regular) + UInt::from(1))) << exp_shift; 984 | let lower = umul_upper_inexact_to_odd(pow10_hi, pow10_lo, lower) + lsb; 985 | let upper = (bin_sig_shifted + UInt::from(2)) << exp_shift; 986 | let upper = umul_upper_inexact_to_odd(pow10_hi, pow10_lo, upper) - lsb; 987 | 988 | // The idea of using a single shorter candidate is by Cassio Neri. 989 | // It is less or equal to the upper bound by construction. 990 | let shorter = UInt::from(10) * ((upper >> BOUND_SHIFT) / UInt::from(10)); 991 | if (shorter << BOUND_SHIFT) >= lower { 992 | return normalize::( 993 | fp { 994 | sig: shorter.into(), 995 | exp: dec_exp, 996 | }, 997 | subnormal, 998 | ); 999 | } 1000 | 1001 | let scaled_sig: u64 = 1002 | umul_upper_inexact_to_odd(pow10_hi, pow10_lo, bin_sig_shifted << exp_shift).into(); 1003 | let dec_sig_below = scaled_sig >> BOUND_SHIFT; 1004 | let dec_sig_above = dec_sig_below + 1; 1005 | 1006 | // Pick the closest of dec_sig_below and dec_sig_above and check if it's in 1007 | // the rounding interval. 1008 | let cmp = scaled_sig.wrapping_sub((dec_sig_below + dec_sig_above) << 1) as i64; 1009 | let below_closer = cmp < 0 || (cmp == 0 && (dec_sig_below & 1) == 0); 1010 | let below_in = (dec_sig_below << BOUND_SHIFT) >= lower.into(); 1011 | let dec_sig = if below_closer & below_in { 1012 | dec_sig_below 1013 | } else { 1014 | dec_sig_above 1015 | }; 1016 | normalize::( 1017 | fp { 1018 | sig: dec_sig, 1019 | exp: dec_exp, 1020 | }, 1021 | subnormal, 1022 | ) 1023 | } 1024 | 1025 | /// Writes the shortest correctly rounded decimal representation of `value` to 1026 | /// `buffer`. `buffer` should point to a buffer of size `buffer_size` or larger. 1027 | #[cfg_attr(feature = "no-panic", no_panic)] 1028 | unsafe fn to_string(value: Float, mut buffer: *mut u8) -> *mut u8 1029 | where 1030 | Float: traits::Float, 1031 | { 1032 | let num_bits = mem::size_of::() as i32 * 8; 1033 | let bits = value.to_bits(); 1034 | 1035 | unsafe { 1036 | *buffer = b'-'; 1037 | buffer = buffer.add((bits >> (num_bits - 1)).into() as usize); 1038 | } 1039 | 1040 | let num_sig_bits = Float::MANTISSA_DIGITS as i32 - 1; 1041 | let implicit_bit = Float::UInt::from(1) << num_sig_bits; 1042 | let mut bin_sig = bits & (implicit_bit - Float::UInt::from(1)); // binary significand 1043 | let mut regular = bin_sig != Float::UInt::from(0); 1044 | 1045 | let num_exp_bits = num_bits - num_sig_bits - 1; 1046 | let exp_mask = (1 << num_exp_bits) - 1; 1047 | let exp_bias = (1 << (num_exp_bits - 1)) - 1; 1048 | let mut bin_exp = (bits >> num_sig_bits).into() as i32 & exp_mask; // binary exponent 1049 | 1050 | let mut subnormal = false; 1051 | if bin_exp == 0 { 1052 | if bin_sig == Float::UInt::from(0) { 1053 | return unsafe { 1054 | *buffer = b'0'; 1055 | *buffer.add(1) = b'.'; 1056 | *buffer.add(2) = b'0'; 1057 | buffer.add(3) 1058 | }; 1059 | } 1060 | // Handle subnormals. 1061 | bin_sig |= implicit_bit; 1062 | bin_exp = 1; 1063 | regular = true; 1064 | subnormal = true; 1065 | } 1066 | bin_sig ^= implicit_bit; 1067 | bin_exp -= num_sig_bits + exp_bias; 1068 | 1069 | let fp { 1070 | sig: mut dec_sig, 1071 | exp: mut dec_exp, 1072 | } = to_decimal(bin_sig, bin_exp, regular, subnormal); 1073 | let num_digits = Float::MAX_DIGITS10 as i32 - 2; 1074 | let end = if num_bits == 64 { 1075 | dec_exp += num_digits + i32::from(dec_sig >= 10_000_000_000_000_000); 1076 | unsafe { write_significand17(buffer.add(1), dec_sig) } 1077 | } else { 1078 | if dec_sig < 10_000_000 { 1079 | dec_sig *= 10; 1080 | dec_exp -= 1; 1081 | } 1082 | dec_exp += num_digits + i32::from(dec_sig >= 100_000_000); 1083 | unsafe { write_significand9(buffer.add(1), dec_sig as u32) } 1084 | }; 1085 | 1086 | let length = unsafe { end.offset_from(buffer.add(1)) } as usize; 1087 | 1088 | if num_bits == 32 && (-6..=12).contains(&dec_exp) 1089 | || num_bits == 64 && (-5..=15).contains(&dec_exp) 1090 | { 1091 | if length as i32 - 1 <= dec_exp { 1092 | // 1234e7 -> 12340000000.0 1093 | return unsafe { 1094 | ptr::copy(buffer.add(1), buffer, length); 1095 | ptr::write_bytes(buffer.add(length), b'0', dec_exp as usize + 3 - length); 1096 | *buffer.add(dec_exp as usize + 1) = b'.'; 1097 | buffer.add(dec_exp as usize + 3) 1098 | }; 1099 | } else if 0 <= dec_exp { 1100 | // 1234e-2 -> 12.34 1101 | return unsafe { 1102 | ptr::copy(buffer.add(1), buffer, dec_exp as usize + 1); 1103 | *buffer.add(dec_exp as usize + 1) = b'.'; 1104 | buffer.add(length + 1) 1105 | }; 1106 | } else { 1107 | // 1234e-6 -> 0.001234 1108 | return unsafe { 1109 | ptr::copy(buffer.add(1), buffer.add((1 - dec_exp) as usize), length); 1110 | ptr::write_bytes(buffer, b'0', (1 - dec_exp) as usize); 1111 | *buffer.add(1) = b'.'; 1112 | buffer.add((1 - dec_exp) as usize + length) 1113 | }; 1114 | } 1115 | } 1116 | 1117 | unsafe { 1118 | // 1234e30 -> 1.234e33 1119 | *buffer = *buffer.add(1); 1120 | *buffer.add(1) = b'.'; 1121 | buffer = buffer.add(length + usize::from(length > 1)); 1122 | *buffer = b'e'; 1123 | buffer = buffer.add(1); 1124 | } 1125 | let sign_ptr = buffer; 1126 | let sign = b'-'.wrapping_add(u8::from(dec_exp >= 0) * b'+'.wrapping_sub(b'-')); 1127 | let mask = i32::from(dec_exp >= 0) - 1; 1128 | dec_exp = (dec_exp + mask) ^ mask; // absolute value 1129 | unsafe { 1130 | buffer = buffer.add(usize::from(dec_exp >= 10)); 1131 | } 1132 | let (a, bb) = divmod100(dec_exp as u32); 1133 | unsafe { 1134 | *buffer = b'0' + a as u8; 1135 | buffer = buffer.add(usize::from(dec_exp >= 100)); 1136 | buffer.cast::().write_unaligned(*digits2(bb as usize)); 1137 | *sign_ptr = sign; 1138 | buffer.add(2) 1139 | } 1140 | } 1141 | 1142 | /// Safe API for formatting floating point numbers to text. 1143 | /// 1144 | /// ## Example 1145 | /// 1146 | /// ``` 1147 | /// let mut buffer = zmij::Buffer::new(); 1148 | /// let printed = buffer.format_finite(1.234); 1149 | /// assert_eq!(printed, "1.234"); 1150 | /// ``` 1151 | pub struct Buffer { 1152 | bytes: [MaybeUninit; BUFFER_SIZE], 1153 | } 1154 | 1155 | impl Buffer { 1156 | /// This is a cheap operation; you don't need to worry about reusing buffers 1157 | /// for efficiency. 1158 | #[inline] 1159 | #[cfg_attr(feature = "no-panic", no_panic)] 1160 | pub fn new() -> Self { 1161 | let bytes = [MaybeUninit::::uninit(); BUFFER_SIZE]; 1162 | Buffer { bytes } 1163 | } 1164 | 1165 | /// Print a floating point number into this buffer and return a reference to 1166 | /// its string representation within the buffer. 1167 | /// 1168 | /// # Special cases 1169 | /// 1170 | /// This function formats NaN as the string "NaN", positive infinity as 1171 | /// "inf", and negative infinity as "-inf" to match std::fmt. 1172 | /// 1173 | /// If your input is known to be finite, you may get better performance by 1174 | /// calling the `format_finite` method instead of `format` to avoid the 1175 | /// checks for special cases. 1176 | #[cfg_attr(feature = "no-panic", no_panic)] 1177 | pub fn format(&mut self, f: F) -> &str { 1178 | if f.is_nonfinite() { 1179 | f.format_nonfinite() 1180 | } else { 1181 | self.format_finite(f) 1182 | } 1183 | } 1184 | 1185 | /// Print a floating point number into this buffer and return a reference to 1186 | /// its string representation within the buffer. 1187 | /// 1188 | /// # Special cases 1189 | /// 1190 | /// This function **does not** check for NaN or infinity. If the input 1191 | /// number is not a finite float, the printed representation will be some 1192 | /// correctly formatted but unspecified numerical value. 1193 | /// 1194 | /// Please check [`is_finite`] yourself before calling this function, or 1195 | /// check [`is_nan`] and [`is_infinite`] and handle those cases yourself. 1196 | /// 1197 | /// [`is_finite`]: f64::is_finite 1198 | /// [`is_nan`]: f64::is_nan 1199 | /// [`is_infinite`]: f64::is_infinite 1200 | #[cfg_attr(feature = "no-panic", no_panic)] 1201 | pub fn format_finite(&mut self, f: F) -> &str { 1202 | unsafe { 1203 | let end = f.write_to_zmij_buffer(self.bytes.as_mut_ptr().cast::()); 1204 | let len = end.offset_from(self.bytes.as_ptr().cast::()) as usize; 1205 | let slice = slice::from_raw_parts(self.bytes.as_ptr().cast::(), len); 1206 | str::from_utf8_unchecked(slice) 1207 | } 1208 | } 1209 | } 1210 | 1211 | /// A floating point number, f32 or f64, that can be written into a 1212 | /// [`zmij::Buffer`][Buffer]. 1213 | /// 1214 | /// This trait is sealed and cannot be implemented for types outside of the 1215 | /// `zmij` crate. 1216 | #[allow(unknown_lints)] // rustc older than 1.74 1217 | #[allow(private_bounds)] 1218 | pub trait Float: private::Sealed {} 1219 | impl Float for f32 {} 1220 | impl Float for f64 {} 1221 | 1222 | mod private { 1223 | pub trait Sealed: crate::traits::Float { 1224 | fn is_nonfinite(self) -> bool; 1225 | fn format_nonfinite(self) -> &'static str; 1226 | unsafe fn write_to_zmij_buffer(self, buffer: *mut u8) -> *mut u8; 1227 | } 1228 | 1229 | impl Sealed for f32 { 1230 | #[inline] 1231 | fn is_nonfinite(self) -> bool { 1232 | const EXP_MASK: u32 = 0x7f800000; 1233 | let bits = self.to_bits(); 1234 | bits & EXP_MASK == EXP_MASK 1235 | } 1236 | 1237 | #[cold] 1238 | #[cfg_attr(feature = "no-panic", inline)] 1239 | fn format_nonfinite(self) -> &'static str { 1240 | const MANTISSA_MASK: u32 = 0x007fffff; 1241 | const SIGN_MASK: u32 = 0x80000000; 1242 | let bits = self.to_bits(); 1243 | if bits & MANTISSA_MASK != 0 { 1244 | crate::NAN 1245 | } else if bits & SIGN_MASK != 0 { 1246 | crate::NEG_INFINITY 1247 | } else { 1248 | crate::INFINITY 1249 | } 1250 | } 1251 | 1252 | #[cfg_attr(feature = "no-panic", inline)] 1253 | unsafe fn write_to_zmij_buffer(self, buffer: *mut u8) -> *mut u8 { 1254 | unsafe { crate::to_string(self, buffer) } 1255 | } 1256 | } 1257 | 1258 | impl Sealed for f64 { 1259 | #[inline] 1260 | fn is_nonfinite(self) -> bool { 1261 | const EXP_MASK: u64 = 0x7ff0000000000000; 1262 | let bits = self.to_bits(); 1263 | bits & EXP_MASK == EXP_MASK 1264 | } 1265 | 1266 | #[cold] 1267 | #[cfg_attr(feature = "no-panic", inline)] 1268 | fn format_nonfinite(self) -> &'static str { 1269 | const MANTISSA_MASK: u64 = 0x000fffffffffffff; 1270 | const SIGN_MASK: u64 = 0x8000000000000000; 1271 | let bits = self.to_bits(); 1272 | if bits & MANTISSA_MASK != 0 { 1273 | crate::NAN 1274 | } else if bits & SIGN_MASK != 0 { 1275 | crate::NEG_INFINITY 1276 | } else { 1277 | crate::INFINITY 1278 | } 1279 | } 1280 | 1281 | #[cfg_attr(feature = "no-panic", inline)] 1282 | unsafe fn write_to_zmij_buffer(self, buffer: *mut u8) -> *mut u8 { 1283 | unsafe { crate::to_string(self, buffer) } 1284 | } 1285 | } 1286 | } 1287 | 1288 | impl Default for Buffer { 1289 | #[inline] 1290 | #[cfg_attr(feature = "no-panic", no_panic)] 1291 | fn default() -> Self { 1292 | Buffer::new() 1293 | } 1294 | } 1295 | --------------------------------------------------------------------------------