zmij-1.0.21/.cargo_vcs_info.json0000644000000001361046102023000121100ustar { "git": { "sha1": "6531ba31ccf5d14b604ca41f6e2414a8dd779af0" }, "path_in_vcs": "" }zmij-1.0.21/.github/workflows/ci.yml000064400000000000000000000110241046102023000153670ustar 00000000000000name: CI on: push: pull_request: workflow_dispatch: schedule: [cron: "40 1 * * *"] permissions: contents: read env: RUSTFLAGS: -Dwarnings jobs: pre_ci: uses: dtolnay/.github/.github/workflows/pre_ci.yml@master test: name: ${{matrix.os == 'macos' && 'macOS' || format('Rust {0} ({1})', matrix.rust, matrix.arch)}} needs: pre_ci if: needs.pre_ci.outputs.continue runs-on: ${{matrix.os == 'macos' && 'macos-latest' || matrix.arch == 'x86_64' && 'ubuntu-latest' || 'ubuntu-24.04-arm'}} strategy: fail-fast: false matrix: rust: [nightly, beta, stable, 1.86.0, 1.71.0] arch: [x86_64, aarch64] os: [ubuntu] include: - rust: nightly os: macos timeout-minutes: 45 steps: - uses: actions/checkout@v6 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{matrix.rust}} - name: Enable type layout randomization run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout >> $GITHUB_ENV if: matrix.rust == 'nightly' - run: cargo check - run: cargo build --tests --features no-panic --release if: matrix.rust == 'nightly' - run: cargo test if: matrix.rust != '1.71.0' - run: cargo test --release if: matrix.rust != '1.71.0' - run: cargo check env: RUSTFLAGS: ${{env.RUSTFLAGS}} -Ctarget-cpu=native - run: cargo build --tests --features no-panic --release if: matrix.rust == 'nightly' env: RUSTFLAGS: ${{env.RUSTFLAGS}} -Ctarget-cpu=native - run: cargo test if: matrix.rust != '1.71.0' env: RUSTFLAGS: ${{env.RUSTFLAGS}} -Ctarget-cpu=native - run: cargo test --release if: matrix.rust != '1.71.0' env: RUSTFLAGS: ${{env.RUSTFLAGS}} -Ctarget-cpu=native - uses: actions/upload-artifact@v6 if: matrix.rust == 'nightly' && matrix.arch == 'x86_64' && matrix.os == 'ubuntu' && always() with: name: Cargo.lock path: Cargo.lock continue-on-error: true doc: name: Documentation needs: pre_ci if: needs.pre_ci.outputs.continue runs-on: ubuntu-latest env: RUSTDOCFLAGS: -Dwarnings timeout-minutes: 45 steps: - uses: actions/checkout@v6 - uses: dtolnay/rust-toolchain@nightly - uses: dtolnay/install@cargo-docs-rs - run: cargo docs-rs clippy: name: Clippy runs-on: ubuntu-latest if: github.event_name != 'pull_request' timeout-minutes: 45 steps: - uses: actions/checkout@v6 - uses: dtolnay/rust-toolchain@clippy with: targets: aarch64-unknown-linux-gnu - run: cargo clippy --tests --benches -- -Dclippy::all -Dclippy::pedantic - run: cargo clippy --target=aarch64-unknown-linux-gnu -- -Dclippy::all -Dclippy::pedantic - run: cargo clippy -- -Dclippy::all -Dclippy::pedantic env: RUSTFLAGS: ${{env.RUSTFLAGS}} -Ctarget-cpu=native miri: name: Miri (${{matrix.name}}) needs: pre_ci if: needs.pre_ci.outputs.continue runs-on: ubuntu-latest strategy: fail-fast: false matrix: include: - name: 64-bit little endian target: x86_64-unknown-linux-gnu - name: 64-bit big endian target: powerpc64-unknown-linux-gnu - name: 32-bit little endian target: i686-unknown-linux-gnu - name: 32-bit big endian target: mips-unknown-linux-gnu timeout-minutes: 45 steps: - uses: actions/checkout@v6 - uses: dtolnay/rust-toolchain@master with: toolchain: nightly-2026-02-11 # https://github.com/rust-lang/miri/issues/4855 components: miri, rust-src - run: cargo miri setup - run: cargo miri test --target ${{matrix.target}} env: MIRIFLAGS: -Zmiri-strict-provenance outdated: name: Outdated runs-on: ubuntu-latest if: github.event_name != 'pull_request' timeout-minutes: 45 steps: - uses: actions/checkout@v6 - uses: dtolnay/rust-toolchain@stable - uses: dtolnay/install@cargo-outdated - run: cargo outdated --exit-code 1 - run: cargo outdated --manifest-path fuzz/Cargo.toml --exit-code 1 fuzz: name: Fuzz needs: pre_ci if: needs.pre_ci.outputs.continue runs-on: ubuntu-latest timeout-minutes: 45 steps: - uses: actions/checkout@v6 - uses: dtolnay/rust-toolchain@nightly - uses: dtolnay/install@cargo-fuzz - run: cargo fuzz check zmij-1.0.21/.gitignore000064400000000000000000000000241046102023000126420ustar 00000000000000/target /Cargo.lock zmij-1.0.21/Cargo.lock0000644000000502421046102023000100660ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "aho-corasick" version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" dependencies = [ "memchr", ] [[package]] name = "alloca" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5a7d05ea6aea7e9e64d25b9156ba2fee3fdd659e34e41063cd2fc7cd020d7f4" dependencies = [ "cc", ] [[package]] name = "anes" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "anstyle" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" [[package]] name = "anyhow" version = "1.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f0e0fee31ef5ed1ba1316088939cea399010ed7731dba877ed44aeb407a75ea" [[package]] name = "autocfg" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" [[package]] name = "bitflags" version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" [[package]] name = "cast" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" version = "1.2.55" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47b26a0954ae34af09b50f0de26458fa95369a0d478d8236d3f93082b219bd29" dependencies = [ "find-msvc-tools", "shlex", ] [[package]] name = "cfg-if" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" [[package]] name = "chacha20" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601" dependencies = [ "cfg-if", "cpufeatures", "rand_core", ] [[package]] name = "ciborium" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" dependencies = [ "ciborium-io", "ciborium-ll", "serde", ] [[package]] name = "ciborium-io" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" [[package]] name = "ciborium-ll" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" dependencies = [ "ciborium-io", "half", ] [[package]] name = "clap" version = "4.5.58" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63be97961acde393029492ce0be7a1af7e323e6bae9511ebfac33751be5e6806" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" version = "4.5.58" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f13174bda5dfd69d7e947827e5af4b0f2f94a4a3ee92912fba07a66150f21e2" dependencies = [ "anstyle", "clap_lex", ] [[package]] name = "clap_lex" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a822ea5bc7590f9d40f1ba12c0dc3c2760f3482c6984db1573ad11031420831" [[package]] name = "cpufeatures" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201" dependencies = [ "libc", ] [[package]] name = "criterion" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "950046b2aa2492f9a536f5f4f9a3de7b9e2476e575e05bd6c333371add4d98f3" dependencies = [ "alloca", "anes", "cast", "ciborium", "clap", "criterion-plot", "itertools", "num-traits", "oorandom", "page_size", "regex", "serde", "serde_json", "tinytemplate", "walkdir", ] [[package]] name = "criterion-plot" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8d80a2f4f5b554395e47b5d8305bc3d27813bacb73493eb1001e8f76dae29ea" dependencies = [ "cast", "itertools", ] [[package]] name = "crunchy" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "either" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "equivalent" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" [[package]] name = "find-msvc-tools" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" [[package]] name = "foldhash" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" [[package]] name = "getrandom" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "139ef39800118c7683f2fd3c98c1b23c09ae076556b435f8e9064ae108aaeeec" dependencies = [ "cfg-if", "libc", "r-efi", "rand_core", "wasip2", "wasip3", ] [[package]] name = "half" version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" dependencies = [ "cfg-if", "crunchy", "zerocopy", ] [[package]] name = "hashbrown" version = "0.15.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" dependencies = [ "foldhash", ] [[package]] name = "hashbrown" version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hermit-abi" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" [[package]] name = "id-arena" version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" [[package]] name = "indexmap" version = "2.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" dependencies = [ "equivalent", "hashbrown 0.16.1", "serde", "serde_core", ] [[package]] name = "itertools" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" dependencies = [ "either", ] [[package]] name = "itoa" version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" [[package]] name = "leb128fmt" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" [[package]] name = "libc" version = "0.2.181" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "459427e2af2b9c839b132acb702a1c654d95e10f8c326bfc2ad11310e458b1c5" [[package]] name = "log" version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" [[package]] name = "memchr" version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" [[package]] name = "no-panic" version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f967505aabc8af5752d098c34146544a43684817cdba8f9725b292530cabbf53" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "num-bigint" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" dependencies = [ "num-integer", "num-traits", ] [[package]] name = "num-integer" version = "0.1.46" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" dependencies = [ "num-traits", ] [[package]] name = "num-traits" version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] [[package]] name = "num_cpus" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" dependencies = [ "hermit-abi", "libc", ] [[package]] name = "oorandom" version = "11.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" [[package]] name = "opt-level" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e98329f1048d878d11b28949196b798b93e2f1e0f98121cca55d6b3e1a91030f" [[package]] name = "page_size" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30d5b2194ed13191c1999ae0704b7839fb18384fa22e49b57eeaa97d79ce40da" dependencies = [ "libc", "winapi", ] [[package]] name = "prettyplease" version = "0.2.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" dependencies = [ "proc-macro2", "syn", ] [[package]] name = "proc-macro2" version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" dependencies = [ "proc-macro2", ] [[package]] name = "r-efi" version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" [[package]] name = "rand" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc266eb313df6c5c09c1c7b1fbe2510961e5bcd3add930c1e31f7ed9da0feff8" dependencies = [ "chacha20", "getrandom", "rand_core", ] [[package]] name = "rand_core" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba" [[package]] name = "regex" version = "1.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c" [[package]] name = "ryu" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" [[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] [[package]] name = "semver" version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" [[package]] name = "serde" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", "serde_derive", ] [[package]] name = "serde_core" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" dependencies = [ "itoa", "memchr", "serde", "serde_core", "zmij 1.0.20", ] [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "syn" version = "2.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "tinytemplate" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" dependencies = [ "serde", "serde_json", ] [[package]] name = "unicode-ident" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "537dd038a89878be9b64dd4bd1b260315c1bb94f4d784956b81e27a088d9a09e" [[package]] name = "unicode-xid" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" [[package]] name = "walkdir" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", ] [[package]] name = "wasip2" version = "1.0.2+wasi-0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9517f9239f02c069db75e65f174b3da828fe5f5b945c4dd26bd25d89c03ebcf5" dependencies = [ "wit-bindgen", ] [[package]] name = "wasip3" version = "0.4.0+wasi-0.3.0-rc-2026-01-06" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" dependencies = [ "wit-bindgen", ] [[package]] name = "wasm-encoder" version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" dependencies = [ "leb128fmt", "wasmparser", ] [[package]] name = "wasm-metadata" version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" dependencies = [ "anyhow", "indexmap", "wasm-encoder", "wasmparser", ] [[package]] name = "wasmparser" version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" dependencies = [ "bitflags", "hashbrown 0.15.5", "indexmap", "semver", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ "windows-sys", ] [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" [[package]] name = "windows-sys" version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ "windows-link", ] [[package]] name = "wit-bindgen" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" dependencies = [ "wit-bindgen-rust-macro", ] [[package]] name = "wit-bindgen-core" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" dependencies = [ "anyhow", "heck", "wit-parser", ] [[package]] name = "wit-bindgen-rust" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" dependencies = [ "anyhow", "heck", "indexmap", "prettyplease", "syn", "wasm-metadata", "wit-bindgen-core", "wit-component", ] [[package]] name = "wit-bindgen-rust-macro" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" dependencies = [ "anyhow", "prettyplease", "proc-macro2", "quote", "syn", "wit-bindgen-core", "wit-bindgen-rust", ] [[package]] name = "wit-component" version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" dependencies = [ "anyhow", "bitflags", "indexmap", "log", "serde", "serde_derive", "serde_json", "wasm-encoder", "wasm-metadata", "wasmparser", "wit-parser", ] [[package]] name = "wit-parser" version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" dependencies = [ "anyhow", "id-arena", "indexmap", "log", "semver", "serde", "serde_derive", "serde_json", "unicode-xid", "wasmparser", ] [[package]] name = "zerocopy" version = "0.8.39" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db6d35d663eadb6c932438e763b262fe1a70987f9ae936e60158176d710cae4a" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" version = "0.8.39" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4122cd3169e94605190e77839c9a40d40ed048d305bfdc146e7df40ab0f3e517" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "zmij" version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4de98dfa5d5b7fef4ee834d0073d560c9ca7b6c46a71d058c48db7960f8cfaf7" [[package]] name = "zmij" version = "1.0.21" dependencies = [ "criterion", "no-panic", "num-bigint", "num-integer", "num_cpus", "opt-level", "rand", "ryu", ] zmij-1.0.21/Cargo.toml0000644000000036371046102023000101170ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.71" name = "zmij" version = "1.0.21" authors = ["David Tolnay "] build = "build.rs" exclude = ["*.png"] autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "A double-to-string conversion algorithm based on Schubfach and yy" documentation = "https://docs.rs/zmij" readme = "README.md" keywords = ["float"] categories = [ "value-formatting", "no-std", "no-std::no-alloc", ] license = "MIT" repository = "https://github.com/dtolnay/zmij" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] rustdoc-args = [ "--generate-link-to-definition", "--generate-macro-expansion", "--extern-html-root-url=core=https://doc.rust-lang.org", ] [lib] name = "zmij" path = "src/lib.rs" [[test]] name = "exhaustive" path = "tests/exhaustive.rs" [[test]] name = "ryu_comparison" path = "tests/ryu_comparison.rs" [[test]] name = "test" path = "tests/test.rs" [[bench]] name = "bench" path = "benches/bench.rs" harness = false [dependencies.no-panic] version = "0.1.36" optional = true [dev-dependencies.num-bigint] version = "0.4" [dev-dependencies.num-integer] version = "0.1" [dev-dependencies.num_cpus] version = "1.8" [dev-dependencies.opt-level] version = "1" [dev-dependencies.rand] version = "0.10" [dev-dependencies.ryu] version = "1" [target."cfg(not(miri))".dev-dependencies.criterion] version = "0.8" default-features = false zmij-1.0.21/Cargo.toml.orig000064400000000000000000000017031046102023000135460ustar 00000000000000[package] name = "zmij" version = "1.0.21" authors = ["David Tolnay "] categories = ["value-formatting", "no-std", "no-std::no-alloc"] description = "A double-to-string conversion algorithm based on Schubfach and yy" documentation = "https://docs.rs/zmij" edition = "2021" exclude = ["*.png"] keywords = ["float"] license = "MIT" repository = "https://github.com/dtolnay/zmij" rust-version = "1.71" [dependencies] no-panic = { version = "0.1.36", optional = true } [dev-dependencies] num-bigint = "0.4" num_cpus = "1.8" num-integer = "0.1" opt-level = "1" rand = "0.10" ryu = "1" [target.'cfg(not(miri))'.dev-dependencies] criterion = { version = "0.8", default-features = false } [[bench]] name = "bench" harness = false [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] rustdoc-args = [ "--generate-link-to-definition", "--generate-macro-expansion", "--extern-html-root-url=core=https://doc.rust-lang.org", ] zmij-1.0.21/LICENSE-MIT000064400000000000000000000017771046102023000123260ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. zmij-1.0.21/README.md000064400000000000000000000033201046102023000121330ustar 00000000000000# Żmij [github](https://github.com/dtolnay/zmij) [crates.io](https://crates.io/crates/zmij) [docs.rs](https://docs.rs/zmij) [build status](https://github.com/dtolnay/zmij/actions?query=branch%3Amaster) Pure Rust implementation of Żmij, an algorithm to quickly convert floating point numbers to decimal strings. This Rust implementation is a line-by-line port of Victor Zverovich's implementation in C++, [https://github.com/vitaut/zmij][upstream]. [upstream]: https://github.com/vitaut/zmij/tree/b35b64a2ba63da9ea7c0a72fd2651e89743966c9 ## Example ```rust fn main() { let mut buffer = zmij::Buffer::new(); let printed = buffer.format(1.234); assert_eq!(printed, "1.234"); } ``` ## Performance The [dtoa-benchmark] compares this library and other Rust floating point formatting implementations across a range of precisions. The vertical axis in this chart shows nanoseconds taken by a single execution of `zmij::Buffer::new().format_finite(value)` so a lower result indicates a faster library. [dtoa-benchmark]: https://github.com/dtolnay/dtoa-benchmark ![performance](https://raw.githubusercontent.com/dtolnay/zmij/master/dtoa-benchmark.png)
#### License MIT license. zmij-1.0.21/benches/bench.rs000064400000000000000000000026121046102023000137130ustar 00000000000000#![allow(clippy::unreadable_literal)] use criterion::{criterion_group, criterion_main, Criterion}; use std::f64; use std::hint; use std::io::Write; fn do_bench(c: &mut Criterion, group_name: &str, float: f64) { let mut group = c.benchmark_group(group_name); group.bench_function("zmij", |b| { let mut buf = zmij::Buffer::new(); b.iter(move || { let float = hint::black_box(float); let formatted = buf.format_finite(float); hint::black_box(formatted); }); }); group.bench_function("ryu", |b| { let mut buf = ryu::Buffer::new(); b.iter(move || { let float = hint::black_box(float); let formatted = buf.format_finite(float); hint::black_box(formatted); }); }); group.bench_function("std::fmt", |b| { let mut buf = Vec::with_capacity(20); b.iter(|| { buf.clear(); let float = hint::black_box(float); write!(&mut buf, "{float}").unwrap(); hint::black_box(buf.as_slice()); }); }); group.finish(); } fn bench(c: &mut Criterion) { do_bench(c, "f64[0]", 0f64); do_bench(c, "f64[short]", 0.1234f64); do_bench(c, "f64[medium]", 0.123456789f64); do_bench(c, "f64[e]", f64::consts::E); do_bench(c, "f64[max]", f64::MAX); } criterion_group!(benches, bench); criterion_main!(benches); zmij-1.0.21/build.rs000064400000000000000000000021251046102023000123230ustar 00000000000000use std::env; use std::ffi::OsString; use std::process::{self, Command}; use std::str; fn main() { println!("cargo:rerun-if-changed=build.rs"); let rustc = rustc_minor_version().unwrap_or(u32::MAX); if rustc >= 80 { println!("cargo:rustc-check-cfg=cfg(exhaustive)"); println!("cargo:rustc-check-cfg=cfg(zmij_no_select_unpredictable)"); } if rustc < 88 { // https://doc.rust-lang.org/std/hint/fn.select_unpredictable.html println!("cargo:rustc-cfg=zmij_no_select_unpredictable"); } } fn rustc_minor_version() -> Option { let rustc = cargo_env_var("RUSTC"); let output = Command::new(rustc).arg("--version").output().ok()?; let version = str::from_utf8(&output.stdout).ok()?; let mut pieces = version.split('.'); if pieces.next() != Some("rustc 1") { return None; } pieces.next()?.parse().ok() } fn cargo_env_var(key: &str) -> OsString { env::var_os(key).unwrap_or_else(|| { eprintln!("Environment variable ${key} is not set during execution of build script"); process::exit(1); }) } zmij-1.0.21/src/hint.rs000064400000000000000000000002351046102023000127550ustar 00000000000000pub fn select_unpredictable(condition: bool, true_val: T, false_val: T) -> T { if condition { true_val } else { false_val } } zmij-1.0.21/src/lib.rs000064400000000000000000001344641046102023000125750ustar 00000000000000//! [![github]](https://github.com/dtolnay/zmij) [![crates-io]](https://crates.io/crates/zmij) [![docs-rs]](https://docs.rs/zmij) //! //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs //! //!
//! //! A double-to-string conversion algorithm based on [Schubfach] and [yy]. //! //! This Rust implementation is a line-by-line port of Victor Zverovich's //! implementation in C++, . //! //! [Schubfach]: https://fmt.dev/papers/Schubfach4.pdf //! [yy]: https://github.com/ibireme/c_numconv_benchmark/blob/master/vendor/yy_double/yy_double.c //! //!
//! //! # Example //! //! ``` //! fn main() { //! let mut buffer = zmij::Buffer::new(); //! let printed = buffer.format(1.234); //! assert_eq!(printed, "1.234"); //! } //! ``` //! //!
//! //! ## Performance //! //! The [dtoa-benchmark] compares this library and other Rust floating point //! formatting implementations across a range of precisions. The vertical axis //! in this chart shows nanoseconds taken by a single execution of //! `zmij::Buffer::new().format_finite(value)` so a lower result indicates a //! faster library. //! //! [dtoa-benchmark]: https://github.com/dtolnay/dtoa-benchmark //! //! ![performance](https://raw.githubusercontent.com/dtolnay/zmij/master/dtoa-benchmark.png) #![no_std] #![doc(html_root_url = "https://docs.rs/zmij/1.0.21")] #![deny(unsafe_op_in_unsafe_fn)] #![allow(non_camel_case_types, non_snake_case)] #![allow( clippy::blocks_in_conditions, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_ptr_alignment, clippy::cast_sign_loss, clippy::doc_markdown, clippy::incompatible_msrv, clippy::items_after_statements, clippy::many_single_char_names, clippy::modulo_one, clippy::must_use_candidate, clippy::needless_doctest_main, clippy::never_loop, clippy::redundant_else, clippy::similar_names, clippy::too_many_arguments, clippy::too_many_lines, clippy::unreadable_literal, clippy::used_underscore_items, clippy::while_immutable_condition, clippy::wildcard_imports )] #[cfg(zmij_no_select_unpredictable)] mod hint; #[cfg(all(target_arch = "x86_64", target_feature = "sse2", not(miri)))] mod stdarch_x86; #[cfg(test)] mod tests; mod traits; #[cfg(all(any(target_arch = "aarch64", target_arch = "x86_64"), not(miri)))] use core::arch::asm; #[cfg(not(zmij_no_select_unpredictable))] use core::hint; use core::mem::{self, MaybeUninit}; use core::ptr; use core::slice; use core::str; #[cfg(feature = "no-panic")] use no_panic::no_panic; const BUFFER_SIZE: usize = 24; const NAN: &str = "NaN"; const INFINITY: &str = "inf"; const NEG_INFINITY: &str = "-inf"; // Returns true_value if lhs < rhs, else false_value, without branching. #[inline] fn select_if_less(lhs: u64, rhs: u64, true_value: i64, false_value: i64) -> i64 { hint::select_unpredictable(lhs < rhs, true_value, false_value) } #[derive(Copy, Clone)] #[cfg_attr(test, derive(Debug, PartialEq))] struct uint128 { hi: u64, lo: u64, } // Use umul128_hi64 for division. const USE_UMUL128_HI64: bool = cfg!(target_vendor = "apple"); // Computes 128-bit result of multiplication of two 64-bit unsigned integers. const fn umul128(x: u64, y: u64) -> u128 { x as u128 * y as u128 } const fn umul128_hi64(x: u64, y: u64) -> u64 { (umul128(x, y) >> 64) as u64 } #[cfg_attr(feature = "no-panic", no_panic)] fn umul192_hi128(x_hi: u64, x_lo: u64, y: u64) -> uint128 { let p = umul128(x_hi, y); let lo = (p as u64).wrapping_add((umul128(x_lo, y) >> 64) as u64); uint128 { hi: (p >> 64) as u64 + u64::from(lo < p as u64), lo, } } // Computes high 64 bits of multiplication of x and y, discards the least // significant bit and rounds to odd, where x = uint128_t(x_hi << 64) | x_lo. #[cfg_attr(feature = "no-panic", no_panic)] fn umulhi_inexact_to_odd(x_hi: u64, x_lo: u64, y: UInt) -> UInt where UInt: traits::UInt, { let num_bits = mem::size_of::() * 8; if num_bits == 64 { let p = umul192_hi128(x_hi, x_lo, y.into()); UInt::truncate(p.hi | u64::from((p.lo >> 1) != 0)) } else { let p = (umul128(x_hi, y.into()) >> 32) as u64; UInt::enlarge((p >> 32) as u32 | u32::from((p as u32 >> 1) != 0)) } } trait FloatTraits: traits::Float { const NUM_BITS: i32; const NUM_SIG_BITS: i32 = Self::MANTISSA_DIGITS as i32 - 1; const NUM_EXP_BITS: i32 = Self::NUM_BITS - Self::NUM_SIG_BITS - 1; const EXP_MASK: i32 = (1 << Self::NUM_EXP_BITS) - 1; const EXP_BIAS: i32 = (1 << (Self::NUM_EXP_BITS - 1)) - 1; const EXP_OFFSET: i32 = Self::EXP_BIAS + Self::NUM_SIG_BITS; type SigType: traits::UInt; const IMPLICIT_BIT: Self::SigType; fn to_bits(self) -> Self::SigType; fn is_negative(bits: Self::SigType) -> bool { (bits >> (Self::NUM_BITS - 1)) != Self::SigType::from(0) } fn get_sig(bits: Self::SigType) -> Self::SigType { bits & (Self::IMPLICIT_BIT - Self::SigType::from(1)) } fn get_exp(bits: Self::SigType) -> i64 { (bits << 1u8 >> (Self::NUM_SIG_BITS + 1)).into() as i64 } } impl FloatTraits for f32 { const NUM_BITS: i32 = 32; const IMPLICIT_BIT: u32 = 1 << Self::NUM_SIG_BITS; type SigType = u32; fn to_bits(self) -> Self::SigType { self.to_bits() } } impl FloatTraits for f64 { const NUM_BITS: i32 = 64; const IMPLICIT_BIT: u64 = 1 << Self::NUM_SIG_BITS; type SigType = u64; fn to_bits(self) -> Self::SigType { self.to_bits() } } #[rustfmt::skip] const POW10S: [u64; 28] = [ 0x8000000000000000, 0xa000000000000000, 0xc800000000000000, 0xfa00000000000000, 0x9c40000000000000, 0xc350000000000000, 0xf424000000000000, 0x9896800000000000, 0xbebc200000000000, 0xee6b280000000000, 0x9502f90000000000, 0xba43b74000000000, 0xe8d4a51000000000, 0x9184e72a00000000, 0xb5e620f480000000, 0xe35fa931a0000000, 0x8e1bc9bf04000000, 0xb1a2bc2ec5000000, 0xde0b6b3a76400000, 0x8ac7230489e80000, 0xad78ebc5ac620000, 0xd8d726b7177a8000, 0x878678326eac9000, 0xa968163f0a57b400, 0xd3c21bcecceda100, 0x84595161401484a0, 0xa56fa5b99019a5c8, 0xcecb8f27f4200f3a, ]; #[rustfmt::skip] const HIGH_PARTS: [uint128; 23] = [ uint128 { hi: 0xaf8e5410288e1b6f, lo: 0x07ecf0ae5ee44dda }, uint128 { hi: 0xb1442798f49ffb4a, lo: 0x99cd11cfdf41779d }, uint128 { hi: 0xb2fe3f0b8599ef07, lo: 0x861fa7e6dcb4aa15 }, uint128 { hi: 0xb4bca50b065abe63, lo: 0x0fed077a756b53aa }, uint128 { hi: 0xb67f6455292cbf08, lo: 0x1a3bc84c17b1d543 }, uint128 { hi: 0xb84687c269ef3bfb, lo: 0x3d5d514f40eea742 }, uint128 { hi: 0xba121a4650e4ddeb, lo: 0x92f34d62616ce413 }, uint128 { hi: 0xbbe226efb628afea, lo: 0x890489f70a55368c }, uint128 { hi: 0xbdb6b8e905cb600f, lo: 0x5400e987bbc1c921 }, uint128 { hi: 0xbf8fdb78849a5f96, lo: 0xde98520472bdd034 }, uint128 { hi: 0xc16d9a0095928a27, lo: 0x75b7053c0f178294 }, uint128 { hi: 0xc350000000000000, lo: 0x0000000000000000 }, uint128 { hi: 0xc5371912364ce305, lo: 0x6c28000000000000 }, uint128 { hi: 0xc722f0ef9d80aad6, lo: 0x424d3ad2b7b97ef6 }, uint128 { hi: 0xc913936dd571c84c, lo: 0x03bc3a19cd1e38ea }, uint128 { hi: 0xcb090c8001ab551c, lo: 0x5cadf5bfd3072cc6 }, uint128 { hi: 0xcd036837130890a1, lo: 0x36dba887c37a8c10 }, uint128 { hi: 0xcf02b2c21207ef2e, lo: 0x94f967e45e03f4bc }, uint128 { hi: 0xd106f86e69d785c7, lo: 0xe13336d701beba52 }, uint128 { hi: 0xd31045a8341ca07c, lo: 0x1ede48111209a051 }, uint128 { hi: 0xd51ea6fa85785631, lo: 0x552a74227f3ea566 }, uint128 { hi: 0xd732290fbacaf133, lo: 0xa97c177947ad4096 }, uint128 { hi: 0xd94ad8b1c7380874, lo: 0x18375281ae7822bc }, ]; #[rustfmt::skip] const FIXUPS: [u32; 20] = [ 0x05271b1f, 0x00000c20, 0x00003200, 0x12100020, 0x00000000, 0x06000000, 0xc16409c0, 0xaf26700f, 0xeb987b07, 0x0000000d, 0x00000000, 0x66fbfffe, 0xb74100ec, 0xa0669fe8, 0xedb21280, 0x00000686, 0x0a021200, 0x29b89c20, 0x08bc0eda, 0x00000000, ]; // 128-bit significands of powers of 10 rounded down. #[repr(C, align(64))] struct Pow10SignificandsTable { data: [u64; if Self::COMPRESS { 0 } else { Self::NUM_POW10 * 2 }], } impl Pow10SignificandsTable { const COMPRESS: bool = false; const SPLIT_TABLES: bool = !Self::COMPRESS && cfg!(target_arch = "aarch64"); const NUM_POW10: usize = 617; // Computes the 128-bit significand of 10**i using method by Dougall Johnson. const fn compute(i: u32) -> uint128 { let m = unsafe { *POW10S.as_ptr().add(((i + 11) % 28) as usize) }; let h = unsafe { *HIGH_PARTS.as_ptr().add(((i + 11) / 28) as usize) }; let h1 = umul128_hi64(h.lo, m); let c0 = h.lo.wrapping_mul(m); let c1 = h1.wrapping_add(h.hi.wrapping_mul(m)); let c2 = (c1 < h1) as u64 + umul128_hi64(h.hi, m); let mut result = if (c2 >> 63) != 0 { uint128 { hi: c2, lo: c1 } } else { uint128 { hi: (c2 << 1) | (c1 >> 63), lo: (c1 << 1) | (c0 >> 63), } }; result.lo -= ((unsafe { *FIXUPS.as_ptr().add((i >> 5) as usize) } >> (i & 31)) & 1) as u64; result } const fn new() -> Self { let mut data = [0; if Self::COMPRESS { 0 } else { Self::NUM_POW10 * 2 }]; let mut i = 0; while i < Self::NUM_POW10 && !Self::COMPRESS { let result = Self::compute(i as u32); if Self::SPLIT_TABLES { data[Self::NUM_POW10 - i - 1] = result.hi; data[Self::NUM_POW10 * 2 - i - 1] = result.lo; } else { data[i * 2] = result.hi; data[i * 2 + 1] = result.lo; } i += 1; } Pow10SignificandsTable { data } } unsafe fn get_unchecked(&self, dec_exp: i32) -> uint128 { const DEC_EXP_MIN: i32 = -292; if Self::COMPRESS { return Self::compute((dec_exp - DEC_EXP_MIN) as u32); } if !Self::SPLIT_TABLES { let index = ((dec_exp - DEC_EXP_MIN) * 2) as usize; return uint128 { hi: unsafe { *self.data.get_unchecked(index) }, lo: unsafe { *self.data.get_unchecked(index + 1) }, }; } unsafe { #[cfg_attr( not(all(any(target_arch = "x86_64", target_arch = "aarch64"), not(miri))), allow(unused_mut) )] let mut hi = self .data .as_ptr() .offset(Self::NUM_POW10 as isize + DEC_EXP_MIN as isize - 1); #[cfg_attr( not(all(any(target_arch = "x86_64", target_arch = "aarch64"), not(miri))), allow(unused_mut) )] let mut lo = hi.add(Self::NUM_POW10); // Force indexed loads. #[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), not(miri)))] asm!("/*{0}{1}*/", inout(reg) hi, inout(reg) lo); uint128 { hi: *hi.offset(-dec_exp as isize), lo: *lo.offset(-dec_exp as isize), } } } #[cfg(test)] fn get(&self, dec_exp: i32) -> uint128 { const DEC_EXP_MIN: i32 = -292; assert!((DEC_EXP_MIN..DEC_EXP_MIN + Self::NUM_POW10 as i32).contains(&dec_exp)); unsafe { self.get_unchecked(dec_exp) } } } static POW10_SIGNIFICANDS: Pow10SignificandsTable = Pow10SignificandsTable::new(); // Computes the decimal exponent as floor(log10(2**bin_exp)) if regular or // floor(log10(3/4 * 2**bin_exp)) otherwise, without branching. const fn compute_dec_exp(bin_exp: i32, regular: bool) -> i32 { debug_assert!(bin_exp >= -1334 && bin_exp <= 2620); // log10_3_over_4_sig = -log10(3/4) * 2**log10_2_exp rounded to a power of 2 const LOG10_3_OVER_4_SIG: i32 = 131_072; // log10_2_sig = round(log10(2) * 2**log10_2_exp) const LOG10_2_SIG: i32 = 315_653; const LOG10_2_EXP: i32 = 20; (bin_exp * LOG10_2_SIG - !regular as i32 * LOG10_3_OVER_4_SIG) >> LOG10_2_EXP } #[inline] const fn do_compute_exp_shift(bin_exp: i32, dec_exp: i32) -> u8 { debug_assert!(dec_exp >= -350 && dec_exp <= 350); // log2_pow10_sig = round(log2(10) * 2**log2_pow10_exp) + 1 const LOG2_POW10_SIG: i32 = 217_707; const LOG2_POW10_EXP: i32 = 16; // pow10_bin_exp = floor(log2(10**-dec_exp)) let pow10_bin_exp = (-dec_exp * LOG2_POW10_SIG) >> LOG2_POW10_EXP; // pow10 = ((pow10_hi << 64) | pow10_lo) * 2**(pow10_bin_exp - 127) (bin_exp + pow10_bin_exp + 1) as u8 } struct ExpShiftTable { data: [u8; if Self::ENABLE { f64::EXP_MASK as usize + 1 } else { 1 }], } impl ExpShiftTable { const ENABLE: bool = true; } static EXP_SHIFTS: ExpShiftTable = { let mut data = [0u8; if ExpShiftTable::ENABLE { f64::EXP_MASK as usize + 1 } else { 1 }]; let mut raw_exp = 0; while raw_exp < data.len() && ExpShiftTable::ENABLE { let mut bin_exp = raw_exp as i32 - f64::EXP_OFFSET; if raw_exp == 0 { bin_exp += 1; } let dec_exp = compute_dec_exp(bin_exp, true); data[raw_exp] = do_compute_exp_shift(bin_exp, dec_exp) as u8; raw_exp += 1; } ExpShiftTable { data } }; // Computes a shift so that, after scaling by a power of 10, the intermediate // result always has a fixed 128-bit fractional part (for double). // // Different binary exponents can map to the same decimal exponent, but place // the decimal point at different bit positions. The shift compensates for this. // // For example, both 3 * 2**59 and 3 * 2**60 have dec_exp = 2, but dividing by // 10^dec_exp puts the decimal point in different bit positions: // 3 * 2**59 / 100 = 1.72...e+16 (needs shift = 1 + 1) // 3 * 2**60 / 100 = 3.45...e+16 (needs shift = 2 + 1) #[inline] unsafe fn compute_exp_shift(bin_exp: i32, dec_exp: i32) -> u8 where UInt: traits::UInt, { let num_bits = mem::size_of::() * 8; if num_bits == 64 && ExpShiftTable::ENABLE && ONLY_REGULAR { unsafe { *EXP_SHIFTS .data .as_ptr() .add((bin_exp + f64::EXP_OFFSET) as usize) } } else { do_compute_exp_shift(bin_exp, dec_exp) } } #[cfg_attr(feature = "no-panic", no_panic)] fn count_trailing_nonzeros(x: u64) -> usize { // We count the number of bytes until there are only zeros left. // The code is equivalent to // 8 - x.leading_zeros() / 8 // but if the BSR instruction is emitted (as gcc on x64 does with default // settings), subtracting the constant before dividing allows the compiler // to combine it with the subtraction which it inserts due to BSR counting // in the opposite direction. // // Additionally, the BSR instruction requires a zero check. Since the high // bit is unused we can avoid the zero check by shifting the datum left by // one and inserting a sentinel bit at the end. This can be faster than the // automatically inserted range check. (70 - ((x.to_le() << 1) | 1).leading_zeros() as usize) / 8 } // Align data since unaligned access may be slower when crossing a // hardware-specific boundary. #[repr(C, align(2))] struct Digits2([u8; 200]); static DIGITS2: Digits2 = Digits2( *b"0001020304050607080910111213141516171819\ 2021222324252627282930313233343536373839\ 4041424344454647484950515253545556575859\ 6061626364656667686970717273747576777879\ 8081828384858687888990919293949596979899", ); // Converts value in the range [0, 100) to a string. GCC generates a bit better // code when value is pointer-size (https://www.godbolt.org/z/5fEPMT1cc). #[cfg_attr(feature = "no-panic", no_panic)] unsafe fn digits2(value: usize) -> &'static u16 { debug_assert!(value < 100); #[allow(clippy::cast_ptr_alignment)] unsafe { &*DIGITS2.0.as_ptr().cast::().add(value) } } const DIV10K_EXP: i32 = 40; const DIV10K_SIG: u32 = ((1u64 << DIV10K_EXP) / 10000 + 1) as u32; const NEG10K: u32 = ((1u64 << 32) - 10000) as u32; const DIV100_EXP: i32 = 19; const DIV100_SIG: u32 = (1 << DIV100_EXP) / 100 + 1; const NEG100: u32 = (1 << 16) - 100; const DIV10_EXP: i32 = 10; const DIV10_SIG: u32 = (1 << DIV10_EXP) / 10 + 1; const NEG10: u32 = (1 << 8) - 10; const ZEROS: u64 = 0x0101010101010101 * b'0' as u64; #[cfg_attr(feature = "no-panic", no_panic)] fn to_bcd8(abcdefgh: u64) -> u64 { // An optimization from Xiang JunBo. // Three steps BCD. Base 10000 -> base 100 -> base 10. // div and mod are evaluated simultaneously as, e.g. // (abcdefgh / 10000) << 32 + (abcdefgh % 10000) // == abcdefgh + (2**32 - 10000) * (abcdefgh / 10000))) // where the division on the RHS is implemented by the usual multiply + shift // trick and the fractional bits are masked away. let abcd_efgh = abcdefgh + u64::from(NEG10K) * ((abcdefgh * u64::from(DIV10K_SIG)) >> DIV10K_EXP); let ab_cd_ef_gh = abcd_efgh + u64::from(NEG100) * (((abcd_efgh * u64::from(DIV100_SIG)) >> DIV100_EXP) & 0x7f0000007f); let a_b_c_d_e_f_g_h = ab_cd_ef_gh + u64::from(NEG10) * (((ab_cd_ef_gh * u64::from(DIV10_SIG)) >> DIV10_EXP) & 0xf000f000f000f); a_b_c_d_e_f_g_h.to_be() } unsafe fn write_if(buffer: *mut u8, digit: u32, condition: bool) -> *mut u8 { unsafe { *buffer = b'0' + digit as u8; buffer.add(usize::from(condition)) } } unsafe fn write8(buffer: *mut u8, value: u64) { unsafe { buffer.cast::().write_unaligned(value); } } // Writes a significand and removes trailing zeros. value has up to 17 decimal // digits (16-17 for normals) for double (num_bits == 64) and up to 9 digits // (8-9 for normals) for float. The significant digits start from buffer[1]. // buffer[0] may contain '0' after this function if the leading digit is zero. #[cfg_attr(feature = "no-panic", no_panic)] #[inline] unsafe fn write_significand(mut buffer: *mut u8, value: u64, extra_digit: bool) -> *mut u8 where Float: FloatTraits, { if Float::NUM_BITS == 32 { buffer = unsafe { write_if(buffer, (value / 100_000_000) as u32, extra_digit) }; let bcd = to_bcd8(value % 100_000_000); unsafe { write8(buffer, bcd + ZEROS); return buffer.add(count_trailing_nonzeros(bcd)); } } #[cfg(not(any( all(target_arch = "aarch64", target_feature = "neon", not(miri)), all(target_arch = "x86_64", target_feature = "sse2", not(miri)), )))] { // Digits/pairs of digits are denoted by letters: value = abbccddeeffgghhii. let abbccddee = (value / 100_000_000) as u32; let ffgghhii = (value % 100_000_000) as u32; buffer = unsafe { write_if(buffer, abbccddee / 100_000_000, extra_digit) }; let bcd = to_bcd8(u64::from(abbccddee % 100_000_000)); unsafe { write8(buffer, bcd + ZEROS); } if ffgghhii == 0 { return unsafe { buffer.add(count_trailing_nonzeros(bcd)) }; } let bcd = to_bcd8(u64::from(ffgghhii)); unsafe { write8(buffer.add(8), bcd + ZEROS); buffer.add(8).add(count_trailing_nonzeros(bcd)) } } #[cfg(all(target_arch = "aarch64", target_feature = "neon", not(miri)))] { // An optimized version for NEON by Dougall Johnson. use core::arch::aarch64::*; const NEG10K: i32 = -10000 + 0x10000; #[repr(C, align(64))] struct Consts { mul_const: u64, hundred_million: u64, multipliers32: int32x4_t, multipliers16: int16x8_t, } static CONSTS: Consts = Consts { mul_const: 0xabcc77118461cefd, hundred_million: 100000000, multipliers32: unsafe { mem::transmute::<[i32; 4], int32x4_t>([ DIV10K_SIG as i32, NEG10K, (DIV100_SIG << 12) as i32, NEG100 as i32, ]) }, multipliers16: unsafe { mem::transmute::<[i16; 8], int16x8_t>([0xce0, NEG10 as i16, 0, 0, 0, 0, 0, 0]) }, }; let mut c = ptr::addr_of!(CONSTS); // Compiler barrier, or clang doesn't load from memory and generates 15 // more instructions. let c = unsafe { asm!("/*{0}*/", inout(reg) c); &*c }; let mut hundred_million = c.hundred_million; // Compiler barrier, or clang narrows the load to 32-bit and unpairs it. unsafe { asm!("/*{0}*/", inout(reg) hundred_million); } // Equivalent to abbccddee = value / 100000000, ffgghhii = value % 100000000. let abbccddee = (umul128(value, c.mul_const) >> 90) as u64; let ffgghhii = value - abbccddee * hundred_million; // We could probably make this bit faster, but we're preferring to // reuse the constants for now. let a = (umul128(abbccddee, c.mul_const) >> 90) as u64; let bbccddee = abbccddee - a * hundred_million; buffer = unsafe { write_if(buffer, a as u32, extra_digit) }; unsafe { let ffgghhii_bbccddee_64: uint64x1_t = mem::transmute::((ffgghhii << 32) | bbccddee); let bbccddee_ffgghhii: int32x2_t = vreinterpret_s32_u64(ffgghhii_bbccddee_64); let bbcc_ffgg: int32x2_t = vreinterpret_s32_u32(vshr_n_u32( vreinterpret_u32_s32(vqdmulh_n_s32( bbccddee_ffgghhii, mem::transmute::(c.multipliers32)[0], )), 9, )); let ddee_bbcc_hhii_ffgg_32: int32x2_t = vmla_n_s32( bbccddee_ffgghhii, bbcc_ffgg, mem::transmute::(c.multipliers32)[1], ); let mut ddee_bbcc_hhii_ffgg: int32x4_t = vreinterpretq_s32_u32(vshll_n_u16(vreinterpret_u16_s32(ddee_bbcc_hhii_ffgg_32), 0)); // Compiler barrier, or clang breaks the subsequent MLA into UADDW + // MUL. asm!("/*{:v}*/", inout(vreg) ddee_bbcc_hhii_ffgg); let dd_bb_hh_ff: int32x4_t = vqdmulhq_n_s32( ddee_bbcc_hhii_ffgg, mem::transmute::(c.multipliers32)[2], ); let ee_dd_cc_bb_ii_hh_gg_ff: int16x8_t = vreinterpretq_s16_s32(vmlaq_n_s32( ddee_bbcc_hhii_ffgg, dd_bb_hh_ff, mem::transmute::(c.multipliers32)[3], )); let high_10s: int16x8_t = vqdmulhq_n_s16( ee_dd_cc_bb_ii_hh_gg_ff, mem::transmute::(c.multipliers16)[0], ); let digits: uint8x16_t = vrev64q_u8(vreinterpretq_u8_s16(vmlaq_n_s16( ee_dd_cc_bb_ii_hh_gg_ff, high_10s, mem::transmute::(c.multipliers16)[1], ))); let str: uint16x8_t = vaddq_u16( vreinterpretq_u16_u8(digits), vreinterpretq_u16_s8(vdupq_n_s8(b'0' as i8)), ); buffer.cast::().write_unaligned(str); let is_not_zero: uint16x8_t = vreinterpretq_u16_u8(vcgtzq_s8(vreinterpretq_s8_u8(digits))); let zeros: u64 = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(is_not_zero, 4)), 0); buffer.add(16 - (zeros.leading_zeros() as usize >> 2)) } } #[cfg(all(target_arch = "x86_64", target_feature = "sse2", not(miri)))] { use crate::stdarch_x86::*; let abbccddee = (value / 100_000_000) as u32; let ffgghhii = (value % 100_000_000) as u32; let a = abbccddee / 100_000_000; let bbccddee = abbccddee % 100_000_000; buffer = unsafe { write_if(buffer, a, extra_digit) }; #[repr(C, align(64))] struct Consts { div10k: u128, neg10k: u128, div100: u128, div10: u128, #[cfg(target_feature = "sse4.1")] neg100: u128, #[cfg(target_feature = "sse4.1")] neg10: u128, #[cfg(target_feature = "sse4.1")] bswap: u128, #[cfg(not(target_feature = "sse4.1"))] hundred: u128, #[cfg(not(target_feature = "sse4.1"))] moddiv10: u128, zeros: u128, } impl Consts { const fn splat64(x: u64) -> u128 { ((x as u128) << 64) | x as u128 } const fn splat32(x: u32) -> u128 { Self::splat64(((x as u64) << 32) | x as u64) } const fn splat16(x: u16) -> u128 { Self::splat32(((x as u32) << 16) | x as u32) } #[cfg(target_feature = "sse4.1")] const fn pack8(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8, g: u8, h: u8) -> u64 { ((h as u64) << 56) | ((g as u64) << 48) | ((f as u64) << 40) | ((e as u64) << 32) | ((d as u64) << 24) | ((c as u64) << 16) | ((b as u64) << 8) | a as u64 } } static CONSTS: Consts = Consts { div10k: Consts::splat64(DIV10K_SIG as u64), neg10k: Consts::splat64(NEG10K as u64), div100: Consts::splat32(DIV100_SIG), div10: Consts::splat16(((1u32 << 16) / 10 + 1) as u16), #[cfg(target_feature = "sse4.1")] neg100: Consts::splat32(NEG100), #[cfg(target_feature = "sse4.1")] neg10: Consts::splat16((1 << 8) - 10), #[cfg(target_feature = "sse4.1")] bswap: Consts::pack8(15, 14, 13, 12, 11, 10, 9, 8) as u128 | (Consts::pack8(7, 6, 5, 4, 3, 2, 1, 0) as u128) << 64, #[cfg(not(target_feature = "sse4.1"))] hundred: Consts::splat32(100), #[cfg(not(target_feature = "sse4.1"))] moddiv10: Consts::splat16(10 * (1 << 8) - 1), zeros: Consts::splat64(ZEROS), }; let mut c = ptr::addr_of!(CONSTS); // Load constants from memory. unsafe { asm!("/*{0}*/", inout(reg) c); } let div10k = unsafe { _mm_load_si128(ptr::addr_of!((*c).div10k).cast::<__m128i>()) }; let neg10k = unsafe { _mm_load_si128(ptr::addr_of!((*c).neg10k).cast::<__m128i>()) }; let div100 = unsafe { _mm_load_si128(ptr::addr_of!((*c).div100).cast::<__m128i>()) }; let div10 = unsafe { _mm_load_si128(ptr::addr_of!((*c).div10).cast::<__m128i>()) }; #[cfg(target_feature = "sse4.1")] let neg100 = unsafe { _mm_load_si128(ptr::addr_of!((*c).neg100).cast::<__m128i>()) }; #[cfg(target_feature = "sse4.1")] let neg10 = unsafe { _mm_load_si128(ptr::addr_of!((*c).neg10).cast::<__m128i>()) }; #[cfg(target_feature = "sse4.1")] let bswap = unsafe { _mm_load_si128(ptr::addr_of!((*c).bswap).cast::<__m128i>()) }; #[cfg(not(target_feature = "sse4.1"))] let hundred = unsafe { _mm_load_si128(ptr::addr_of!((*c).hundred).cast::<__m128i>()) }; #[cfg(not(target_feature = "sse4.1"))] let moddiv10 = unsafe { _mm_load_si128(ptr::addr_of!((*c).moddiv10).cast::<__m128i>()) }; let zeros = unsafe { _mm_load_si128(ptr::addr_of!((*c).zeros).cast::<__m128i>()) }; // The BCD sequences are based on ones provided by Xiang JunBo. unsafe { let x: __m128i = _mm_set_epi64x(i64::from(bbccddee), i64::from(ffgghhii)); let y: __m128i = _mm_add_epi64( x, _mm_mul_epu32(neg10k, _mm_srli_epi64(_mm_mul_epu32(x, div10k), DIV10K_EXP)), ); #[cfg(target_feature = "sse4.1")] let bcd: __m128i = { // _mm_mullo_epi32 is SSE 4.1 let z: __m128i = _mm_add_epi64( y, _mm_mullo_epi32(neg100, _mm_srli_epi32(_mm_mulhi_epu16(y, div100), 3)), ); let big_endian_bcd: __m128i = _mm_add_epi64(z, _mm_mullo_epi16(neg10, _mm_mulhi_epu16(z, div10))); // SSSE3 _mm_shuffle_epi8(big_endian_bcd, bswap) }; #[cfg(not(target_feature = "sse4.1"))] let bcd: __m128i = { let y_div_100: __m128i = _mm_srli_epi16(_mm_mulhi_epu16(y, div100), 3); let y_mod_100: __m128i = _mm_sub_epi16(y, _mm_mullo_epi16(y_div_100, hundred)); let z: __m128i = _mm_or_si128(_mm_slli_epi32(y_mod_100, 16), y_div_100); let bcd_shuffled: __m128i = _mm_sub_epi16( _mm_slli_epi16(z, 8), _mm_mullo_epi16(moddiv10, _mm_mulhi_epu16(z, div10)), ); _mm_shuffle_epi32(bcd_shuffled, _MM_SHUFFLE(0, 1, 2, 3)) }; let digits = _mm_or_si128(bcd, zeros); // Count leading zeros. let mask128: __m128i = _mm_cmpgt_epi8(bcd, _mm_setzero_si128()); let mask = _mm_movemask_epi8(mask128) as u32; let len = 32 - mask.leading_zeros() as usize; _mm_storeu_si128(buffer.cast::<__m128i>(), digits); buffer.add(len) } } } struct ToDecimalResult { sig: i64, exp: i32, } #[cfg_attr(feature = "no-panic", no_panic)] #[inline] fn to_decimal_schubfach(bin_sig: UInt, bin_exp: i64, regular: bool) -> ToDecimalResult where UInt: traits::UInt, { let num_bits = mem::size_of::() as i32 * 8; let dec_exp = compute_dec_exp(bin_exp as i32, regular); let exp_shift = unsafe { compute_exp_shift::(bin_exp as i32, dec_exp) }; let mut pow10 = unsafe { POW10_SIGNIFICANDS.get_unchecked(-dec_exp) }; // Fallback to Schubfach to guarantee correctness in boundary cases. This // requires switching to strict overestimates of powers of 10. if num_bits == 64 { pow10.lo += 1; } else { pow10.hi += 1; } // Shift the significand so that boundaries are integer. const BOUND_SHIFT: u32 = 2; let bin_sig_shifted = bin_sig << BOUND_SHIFT; // Compute the estimates of lower and upper bounds of the rounding interval // by multiplying them by the power of 10 and applying modified rounding. let lsb = bin_sig & UInt::from(1); let lower = (bin_sig_shifted - (UInt::from(regular) + UInt::from(1))) << exp_shift; let lower = umulhi_inexact_to_odd(pow10.hi, pow10.lo, lower) + lsb; let upper = (bin_sig_shifted + UInt::from(2)) << exp_shift; let upper = umulhi_inexact_to_odd(pow10.hi, pow10.lo, upper) - lsb; // The idea of using a single shorter candidate is by Cassio Neri. // It is less or equal to the upper bound by construction. let shorter = (upper >> BOUND_SHIFT) / UInt::from(10) * UInt::from(10); if (shorter << BOUND_SHIFT) >= lower { return ToDecimalResult { sig: shorter.into() as i64, exp: dec_exp, }; } let scaled_sig = umulhi_inexact_to_odd(pow10.hi, pow10.lo, bin_sig_shifted << exp_shift); let longer_below = scaled_sig >> BOUND_SHIFT; let longer_above = longer_below + UInt::from(1); // Pick the closest of longer_below and longer_above and check if it's in // the rounding interval. let cmp = scaled_sig .wrapping_sub((longer_below + longer_above) << 1) .to_signed(); let below_closer = cmp < UInt::from(0).to_signed() || (cmp == UInt::from(0).to_signed() && (longer_below & UInt::from(1)) == UInt::from(0)); let below_in = (longer_below << BOUND_SHIFT) >= lower; let dec_sig = if below_closer & below_in { longer_below } else { longer_above }; ToDecimalResult { sig: dec_sig.into() as i64, exp: dec_exp, } } // Here be 🐉s. // Converts a binary FP number bin_sig * 2**bin_exp to the shortest decimal // representation, where bin_exp = raw_exp - exp_offset. #[cfg_attr(feature = "no-panic", no_panic)] #[inline] fn to_decimal_fast(bin_sig: UInt, raw_exp: i64, regular: bool) -> ToDecimalResult where Float: FloatTraits, UInt: traits::UInt, { let bin_exp = raw_exp - i64::from(Float::EXP_OFFSET); let num_bits = mem::size_of::() as i32 * 8; // An optimization from yy by Yaoyuan Guo: while regular { let dec_exp = if USE_UMUL128_HI64 { umul128_hi64(bin_exp as u64, 0x4d10500000000000) as i32 } else { compute_dec_exp(bin_exp as i32, true) }; let exp_shift = unsafe { compute_exp_shift::(bin_exp as i32, dec_exp) }; let pow10 = unsafe { POW10_SIGNIFICANDS.get_unchecked(-dec_exp) }; let integral; // integral part of bin_sig * pow10 let fractional; // fractional part of bin_sig * pow10 if num_bits == 64 { let p = umul192_hi128(pow10.hi, pow10.lo, (bin_sig << exp_shift).into()); integral = UInt::truncate(p.hi); fractional = p.lo; } else { let p = umul128(pow10.hi, (bin_sig << exp_shift).into()); integral = UInt::truncate((p >> 64) as u64); fractional = p as u64; } const HALF_ULP: u64 = 1 << 63; // Exact half-ulp tie when rounding to nearest integer. let cmp = fractional.wrapping_sub(HALF_ULP) as i64; if cmp == 0 { break; } // An optimization of integral % 10 by Dougall Johnson. Relies on range // calculation: (max_bin_sig << max_exp_shift) * max_u128. // (1 << 63) / 5 == (1 << 64) / 10 without an intermediate int128. const DIV10_SIG64: u64 = (1 << 63) / 5 + 1; let div10 = umul128_hi64(integral.into(), DIV10_SIG64); #[allow(unused_mut)] let mut digit = integral.into() - div10 * 10; // or it narrows to 32-bit and doesn't use madd/msub #[cfg(all(any(target_arch = "aarch64", target_arch = "x86_64"), not(miri)))] unsafe { asm!("/*{0}*/", inout(reg) digit); } // Switch to a fixed-point representation with the least significant // integral digit in the upper bits and fractional digits in the lower // bits. let num_integral_bits = if num_bits == 64 { 4 } else { 32 }; let num_fractional_bits = 64 - num_integral_bits; let ten = 10u64 << num_fractional_bits; // Fixed-point remainder of the scaled significand modulo 10. let scaled_sig_mod10 = (digit << num_fractional_bits) | (fractional >> num_integral_bits); // scaled_half_ulp = 0.5 * pow10 in the fixed-point format. // dec_exp is chosen so that 10**dec_exp <= 2**bin_exp < 10**(dec_exp + 1). // Since 1ulp == 2**bin_exp it will be in the range [1, 10) after scaling // by 10**dec_exp. Add 1 to combine the shift with division by two. let scaled_half_ulp = pow10.hi >> (num_integral_bits - exp_shift + 1); let upper = scaled_sig_mod10 + scaled_half_ulp; // value = 5.0507837461e-27 // next = 5.0507837461000010e-27 // // c = integral.fractional' = 50507837461000003.153987... (value) // 50507837461000010.328635... (next) // scaled_half_ulp = 3.587324... // // fractional' = fractional / 2**64, fractional = 2840565642863009226 // // 50507837461000000 c upper 50507837461000010 // s l| L | S // ───┬────┬────┼────┬────┬────┼*-──┼────┬────┬───*┬────┬────┬────┼-*--┬─── // 8 9 0 1 2 3 4 5 6 7 8 9 0 | 1 // └─────────────────┼─────────────────┘ next // 1ulp // // s - shorter underestimate, S - shorter overestimate // l - longer underestimate, L - longer overestimate // Check for boundary case when rounding down to nearest 10 and // near-boundary case when rounding up to nearest 10. // Case where upper == ten is insufficient: 1.342178e+08f. if ten.wrapping_sub(upper) <= 1 // upper == ten || upper == ten - 1 || scaled_sig_mod10 == scaled_half_ulp { break; } let shorter = (integral.into() - digit) as i64; let longer = (integral.into() + u64::from(cmp >= 0)) as i64; let dec_sig = select_if_less(scaled_sig_mod10, scaled_half_ulp, shorter, longer); return ToDecimalResult { sig: select_if_less(ten, upper, shorter + 10, dec_sig), exp: dec_exp, }; } to_decimal_schubfach(bin_sig, bin_exp, regular) } /// Writes the shortest correctly rounded decimal representation of `value` to /// `buffer`. `buffer` should point to a buffer of size `buffer_size` or larger. #[cfg_attr(feature = "no-panic", no_panic)] unsafe fn write(value: Float, mut buffer: *mut u8) -> *mut u8 where Float: FloatTraits, { let bits = value.to_bits(); // It is beneficial to extract exponent and significand early. let bin_exp = Float::get_exp(bits); // binary exponent let bin_sig = Float::get_sig(bits); // binary significand unsafe { *buffer = b'-'; } buffer = unsafe { buffer.add(usize::from(Float::is_negative(bits))) }; let mut dec; let threshold = if Float::NUM_BITS == 64 { 10_000_000_000_000_000 } else { 100_000_000 }; if bin_exp == 0 { if bin_sig == Float::SigType::from(0) { return unsafe { *buffer = b'0'; *buffer.add(1) = b'.'; *buffer.add(2) = b'0'; buffer.add(3) }; } dec = to_decimal_schubfach(bin_sig, i64::from(1 - Float::EXP_OFFSET), true); while dec.sig < threshold { dec.sig *= 10; dec.exp -= 1; } } else { dec = to_decimal_fast::( bin_sig | Float::IMPLICIT_BIT, bin_exp, bin_sig != Float::SigType::from(0), ); } let mut dec_exp = dec.exp; let extra_digit = dec.sig >= threshold; dec_exp += Float::MAX_DIGITS10 as i32 - 2 + i32::from(extra_digit); if Float::NUM_BITS == 32 && dec.sig < 10_000_000 { dec.sig *= 10; dec_exp -= 1; } // Write significand. let end = unsafe { write_significand::(buffer.add(1), dec.sig as u64, extra_digit) }; let length = unsafe { end.offset_from(buffer.add(1)) } as usize; if Float::NUM_BITS == 32 && (-6..=12).contains(&dec_exp) || Float::NUM_BITS == 64 && (-5..=15).contains(&dec_exp) { if length as i32 - 1 <= dec_exp { // 1234e7 -> 12340000000.0 return unsafe { ptr::copy(buffer.add(1), buffer, length); ptr::write_bytes(buffer.add(length), b'0', dec_exp as usize + 3 - length); *buffer.add(dec_exp as usize + 1) = b'.'; buffer.add(dec_exp as usize + 3) }; } else if 0 <= dec_exp { // 1234e-2 -> 12.34 return unsafe { ptr::copy(buffer.add(1), buffer, dec_exp as usize + 1); *buffer.add(dec_exp as usize + 1) = b'.'; buffer.add(length + 1) }; } else { // 1234e-6 -> 0.001234 return unsafe { ptr::copy(buffer.add(1), buffer.add((1 - dec_exp) as usize), length); ptr::write_bytes(buffer, b'0', (1 - dec_exp) as usize); *buffer.add(1) = b'.'; buffer.add((1 - dec_exp) as usize + length) }; } } unsafe { // 1234e30 -> 1.234e33 *buffer = *buffer.add(1); *buffer.add(1) = b'.'; } buffer = unsafe { buffer.add(length + usize::from(length > 1)) }; // Write exponent. let sign_ptr = buffer; let e_sign = if dec_exp >= 0 { (u16::from(b'+') << 8) | u16::from(b'e') } else { (u16::from(b'-') << 8) | u16::from(b'e') }; buffer = unsafe { buffer.add(1) }; dec_exp = if dec_exp >= 0 { dec_exp } else { -dec_exp }; buffer = unsafe { buffer.add(usize::from(dec_exp >= 10)) }; if Float::MIN_10_EXP > -100 && Float::MAX_10_EXP < 100 { unsafe { buffer .cast::() .write_unaligned(*digits2(dec_exp as usize)); sign_ptr.cast::().write_unaligned(e_sign.to_le()); return buffer.add(2); } } // digit = dec_exp / 100 let digit = if USE_UMUL128_HI64 { umul128_hi64(dec_exp as u64, 0x290000000000000) as u32 } else { (dec_exp as u32 * DIV100_SIG) >> DIV100_EXP }; unsafe { *buffer = b'0' + digit as u8; } buffer = unsafe { buffer.add(usize::from(dec_exp >= 100)) }; unsafe { buffer .cast::() .write_unaligned(*digits2((dec_exp as u32 - digit * 100) as usize)); sign_ptr.cast::().write_unaligned(e_sign.to_le()); buffer.add(2) } } /// Safe API for formatting floating point numbers to text. /// /// ## Example /// /// ``` /// let mut buffer = zmij::Buffer::new(); /// let printed = buffer.format_finite(1.234); /// assert_eq!(printed, "1.234"); /// ``` pub struct Buffer { bytes: [MaybeUninit; BUFFER_SIZE], } impl Buffer { /// This is a cheap operation; you don't need to worry about reusing buffers /// for efficiency. #[inline] #[cfg_attr(feature = "no-panic", no_panic)] pub fn new() -> Self { let bytes = [MaybeUninit::::uninit(); BUFFER_SIZE]; Buffer { bytes } } /// Print a floating point number into this buffer and return a reference to /// its string representation within the buffer. /// /// # Special cases /// /// This function formats NaN as the string "NaN", positive infinity as /// "inf", and negative infinity as "-inf" to match std::fmt. /// /// If your input is known to be finite, you may get better performance by /// calling the `format_finite` method instead of `format` to avoid the /// checks for special cases. #[cfg_attr(feature = "no-panic", no_panic)] pub fn format(&mut self, f: F) -> &str { if f.is_nonfinite() { f.format_nonfinite() } else { self.format_finite(f) } } /// Print a floating point number into this buffer and return a reference to /// its string representation within the buffer. /// /// # Special cases /// /// This function **does not** check for NaN or infinity. If the input /// number is not a finite float, the printed representation will be some /// correctly formatted but unspecified numerical value. /// /// Please check [`is_finite`] yourself before calling this function, or /// check [`is_nan`] and [`is_infinite`] and handle those cases yourself. /// /// [`is_finite`]: f64::is_finite /// [`is_nan`]: f64::is_nan /// [`is_infinite`]: f64::is_infinite #[cfg_attr(feature = "no-panic", no_panic)] pub fn format_finite(&mut self, f: F) -> &str { unsafe { let end = f.write_to_zmij_buffer(self.bytes.as_mut_ptr().cast::()); let len = end.offset_from(self.bytes.as_ptr().cast::()) as usize; let slice = slice::from_raw_parts(self.bytes.as_ptr().cast::(), len); str::from_utf8_unchecked(slice) } } } /// A floating point number, f32 or f64, that can be written into a /// [`zmij::Buffer`][Buffer]. /// /// This trait is sealed and cannot be implemented for types outside of the /// `zmij` crate. #[allow(unknown_lints)] // rustc older than 1.74 #[allow(private_bounds)] pub trait Float: private::Sealed {} impl Float for f32 {} impl Float for f64 {} mod private { pub trait Sealed: crate::traits::Float { fn is_nonfinite(self) -> bool; fn format_nonfinite(self) -> &'static str; unsafe fn write_to_zmij_buffer(self, buffer: *mut u8) -> *mut u8; } impl Sealed for f32 { #[inline] fn is_nonfinite(self) -> bool { const EXP_MASK: u32 = 0x7f800000; let bits = self.to_bits(); bits & EXP_MASK == EXP_MASK } #[cold] #[cfg_attr(feature = "no-panic", inline)] fn format_nonfinite(self) -> &'static str { const MANTISSA_MASK: u32 = 0x007fffff; const SIGN_MASK: u32 = 0x80000000; let bits = self.to_bits(); if bits & MANTISSA_MASK != 0 { crate::NAN } else if bits & SIGN_MASK != 0 { crate::NEG_INFINITY } else { crate::INFINITY } } #[cfg_attr(feature = "no-panic", inline)] unsafe fn write_to_zmij_buffer(self, buffer: *mut u8) -> *mut u8 { unsafe { crate::write(self, buffer) } } } impl Sealed for f64 { #[inline] fn is_nonfinite(self) -> bool { const EXP_MASK: u64 = 0x7ff0000000000000; let bits = self.to_bits(); bits & EXP_MASK == EXP_MASK } #[cold] #[cfg_attr(feature = "no-panic", inline)] fn format_nonfinite(self) -> &'static str { const MANTISSA_MASK: u64 = 0x000fffffffffffff; const SIGN_MASK: u64 = 0x8000000000000000; let bits = self.to_bits(); if bits & MANTISSA_MASK != 0 { crate::NAN } else if bits & SIGN_MASK != 0 { crate::NEG_INFINITY } else { crate::INFINITY } } #[cfg_attr(feature = "no-panic", inline)] unsafe fn write_to_zmij_buffer(self, buffer: *mut u8) -> *mut u8 { unsafe { crate::write(self, buffer) } } } } impl Default for Buffer { #[inline] #[cfg_attr(feature = "no-panic", no_panic)] fn default() -> Self { Buffer::new() } } zmij-1.0.21/src/stdarch_x86.rs000064400000000000000000000023311046102023000141470ustar 00000000000000use core::mem; pub use core::arch::x86_64::*; pub const fn _MM_SHUFFLE(z: u32, y: u32, x: u32, w: u32) -> i32 { ((z << 6) | (y << 4) | (x << 2) | w) as i32 } pub const fn _mm_set_epi64x(e1: i64, e0: i64) -> __m128i { unsafe { mem::transmute([e0, e1]) } } pub const fn _mm_set_epi32(e3: i32, e2: i32, e1: i32, e0: i32) -> __m128i { unsafe { mem::transmute([e0, e1, e2, e3]) } } pub const fn _mm_set_epi16( e7: i16, e6: i16, e5: i16, e4: i16, e3: i16, e2: i16, e1: i16, e0: i16, ) -> __m128i { unsafe { mem::transmute([e0, e1, e2, e3, e4, e5, e6, e7]) } } pub const fn _mm_set_epi8( e15: i8, e14: i8, e13: i8, e12: i8, e11: i8, e10: i8, e9: i8, e8: i8, e7: i8, e6: i8, e5: i8, e4: i8, e3: i8, e2: i8, e1: i8, e0: i8, ) -> __m128i { unsafe { mem::transmute([ e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, ]) } } pub const fn _mm_set1_epi64x(a: i64) -> __m128i { _mm_set_epi64x(a, a) } pub const fn _mm_set1_epi32(a: i32) -> __m128i { _mm_set_epi32(a, a, a, a) } pub const fn _mm_set1_epi16(a: i16) -> __m128i { _mm_set_epi16(a, a, a, a, a, a, a, a) } zmij-1.0.21/src/tests.rs000064400000000000000000000047141046102023000131630ustar 00000000000000use core::f64::consts::LOG2_10; use core::mem; use num_bigint::BigUint as Uint; const _: () = { let static_data = mem::size_of_val(&crate::POW10_SIGNIFICANDS) + mem::size_of_val(&crate::DIGITS2); if crate::Pow10SignificandsTable::COMPRESS { assert!(static_data == 200); } else { assert!(static_data == 10120); // 9.9K } }; #[cfg(target_endian = "little")] #[test] fn utilities() { let clz = u64::leading_zeros; assert_eq!(clz(1), 63); assert_eq!(clz(!0), 0); assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000000), 0); assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000001), 1); assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000009), 1); assert_eq!(crate::count_trailing_nonzeros(0x00090000_09000000), 7); assert_eq!(crate::count_trailing_nonzeros(0x01000000_00000000), 8); assert_eq!(crate::count_trailing_nonzeros(0x09000000_00000000), 8); } #[test] fn umulhi_inexact_to_odd() { let pow10 = crate::POW10_SIGNIFICANDS.get(-292); assert_eq!( crate::umulhi_inexact_to_odd(pow10.hi, pow10.lo, 0x1234567890abcdefu64 << 1), 0x24554a3ce60a45f5, ); assert_eq!( crate::umulhi_inexact_to_odd(pow10.hi, pow10.lo, 0x1234567890abce16u64 << 1), 0x24554a3ce60a4643, ); } #[test] fn pow10() { const DEC_EXP_MIN: i32 = -292; // Range of decimal exponents [K_min, K_max] from the paper. let dec_exp_min = -324_i32; let dec_exp_max = 292_i32; let num_bits = 128_i32; // Negate dec_pow_min and dec_pow_max because we need negative powers 10^-k. for (i, dec_exp) in (-dec_exp_max..=-dec_exp_min).enumerate() { // dec_exp is -k in the paper. let bin_exp = (f64::from(dec_exp) * LOG2_10).floor() as i32 - (num_bits - 1); let bin_pow = Uint::from(2_u8).pow(bin_exp.unsigned_abs()); let dec_pow = Uint::from(10_u8).pow(dec_exp.unsigned_abs()); let result = if dec_exp < 0 { bin_pow / dec_pow } else if bin_exp < 0 { dec_pow * bin_pow } else { dec_pow / bin_pow }; let hi = u64::try_from(&result >> 64).unwrap(); let lo = u64::try_from(result & (Uint::from(2_u8).pow(64) - Uint::from(1_u8))).unwrap(); if !crate::Pow10SignificandsTable::COMPRESS { assert_eq!( crate::POW10_SIGNIFICANDS.get(DEC_EXP_MIN + i as i32), crate::uint128 { hi, lo }, ); } } } zmij-1.0.21/src/traits.rs000064400000000000000000000036231046102023000133250ustar 00000000000000use core::fmt::Display; use core::ops::{Add, BitAnd, BitOr, BitOrAssign, BitXorAssign, Div, Mul, Shl, Shr, Sub}; pub trait Float: Copy { const MANTISSA_DIGITS: u32; const MIN_10_EXP: i32; const MAX_10_EXP: i32; const MAX_DIGITS10: u32; } impl Float for f32 { const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS; const MIN_10_EXP: i32 = Self::MIN_10_EXP; const MAX_10_EXP: i32 = Self::MAX_10_EXP; const MAX_DIGITS10: u32 = 9; } impl Float for f64 { const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS; const MIN_10_EXP: i32 = Self::MIN_10_EXP; const MAX_10_EXP: i32 = Self::MAX_10_EXP; const MAX_DIGITS10: u32 = 17; } pub trait UInt: Copy + From + From + Add + Sub + Mul + Div + BitAnd + BitOr + Shl + Shl + Shl + Shr + Shr + BitOrAssign + BitXorAssign + PartialOrd + Into + Display { type Signed: Ord; fn wrapping_sub(self, other: Self) -> Self; fn truncate(big: u64) -> Self; fn enlarge(small: u32) -> Self; fn to_signed(self) -> Self::Signed; } impl UInt for u32 { type Signed = i32; fn wrapping_sub(self, other: Self) -> Self { self.wrapping_sub(other) } fn truncate(big: u64) -> Self { big as u32 } fn enlarge(small: u32) -> Self { small } fn to_signed(self) -> Self::Signed { self as i32 } } impl UInt for u64 { type Signed = i64; fn wrapping_sub(self, other: Self) -> Self { self.wrapping_sub(other) } fn truncate(big: u64) -> Self { big } fn enlarge(small: u32) -> Self { u64::from(small) } fn to_signed(self) -> Self::Signed { self as i64 } } zmij-1.0.21/tests/exhaustive.rs000064400000000000000000000035521046102023000145600ustar 00000000000000#![cfg_attr(not(check_cfg), allow(unexpected_cfgs))] use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::Arc; use std::thread; #[test] #[cfg_attr(not(exhaustive), ignore = "requires cfg(exhaustive)")] fn test_exhaustive() { const BATCH_SIZE: u32 = 1_000_000; let counter = Arc::new(AtomicU32::new(0)); let finished = Arc::new(AtomicU32::new(0)); let mut workers = Vec::new(); for _ in 0..num_cpus::get() { let counter = counter.clone(); let finished = finished.clone(); workers.push(thread::spawn(move || loop { let batch = counter.fetch_add(1, Ordering::Relaxed); if batch > u32::MAX / BATCH_SIZE { return; } let min = batch * BATCH_SIZE; let max = if batch == u32::MAX / BATCH_SIZE { u32::MAX } else { min + BATCH_SIZE - 1 }; let mut zmij_buffer = zmij::Buffer::new(); let mut ryu_buffer = ryu::Buffer::new(); for u in min..=max { let f = f32::from_bits(u); if !f.is_finite() { continue; } let zmij = zmij_buffer.format_finite(f); assert_eq!(Ok(f), zmij.parse()); let ryu = ryu_buffer.format_finite(f); let matches = if ryu.contains('e') && !ryu.contains("e-") { ryu.split_once('e') == zmij.split_once("e+") } else { ryu == zmij }; assert!(matches, "{ryu} != {zmij}"); } let increment = max - min + 1; let update = finished.fetch_add(increment, Ordering::Relaxed); println!("{}", u64::from(update) + u64::from(increment)); })); } for w in workers { w.join().unwrap(); } } zmij-1.0.21/tests/ryu_comparison.rs000064400000000000000000000016621046102023000154440ustar 00000000000000use rand::rngs::{SmallRng, SysRng}; use rand::{Rng as _, SeedableRng as _}; const N: usize = if cfg!(miri) { 500 } else if let b"0" = opt_level::OPT_LEVEL.as_bytes() { 1_000_000 } else { 100_000_000 }; #[test] fn ryu_comparison() { let mut ryu_buffer = ryu::Buffer::new(); let mut zmij_buffer = zmij::Buffer::new(); let mut rng = SmallRng::try_from_rng(&mut SysRng).unwrap(); let mut fail = 0; for _ in 0..N { let bits = rng.next_u64(); let float = f64::from_bits(bits); let ryu = ryu_buffer.format(float); let zmij = zmij_buffer.format(float); let matches = if ryu.contains('e') && !ryu.contains("e-") { ryu.split_once('e') == zmij.split_once("e+") } else { ryu == zmij }; if !matches { eprintln!("RYU={ryu} ZMIJ={zmij}"); fail += 1; } } assert!(fail == 0, "{fail} mismatches"); } zmij-1.0.21/tests/test.rs000064400000000000000000000056771046102023000133640ustar 00000000000000#![allow(clippy::float_cmp, clippy::unreadable_literal)] fn dtoa(value: f64) -> String { zmij::Buffer::new().format(value).to_owned() } fn ftoa(value: f32) -> String { zmij::Buffer::new().format(value).to_owned() } mod dtoa_test { use super::dtoa; #[test] fn normal() { assert_eq!(dtoa(6.62607015e-34), "6.62607015e-34"); // Exact half-ulp tie when rounding to nearest integer. assert_eq!(dtoa(5.444310685350916e+14), "544431068535091.6"); } #[test] fn subnormal() { assert_eq!(dtoa(0.0f64.next_up()), "5e-324"); assert_eq!(dtoa(1e-323), "1e-323"); assert_eq!(dtoa(1.2e-322), "1.2e-322"); assert_eq!(dtoa(1.24e-322), "1.24e-322"); assert_eq!(dtoa(1.234e-320), "1.234e-320"); } #[test] fn all_irregular() { for exp in 1..0x3ff { let bits = exp << 52; let value = f64::from_bits(bits); assert_eq!(dtoa(value), ryu::Buffer::new().format(value)); } } #[test] fn all_exponents() { for exp in 0..=0x3ff { let bits = (exp << 52) | 1; let value = f64::from_bits(bits); assert_eq!(dtoa(value), ryu::Buffer::new().format(value)); } } #[test] fn small_int() { assert_eq!(dtoa(1.0), "1.0"); } #[test] fn zero() { assert_eq!(dtoa(0.0), "0.0"); assert_eq!(dtoa(-0.0), "-0.0"); } #[test] fn inf() { assert_eq!(dtoa(f64::INFINITY), "inf"); } #[test] fn nan() { assert_eq!(dtoa(f64::NAN.copysign(-1.0)), "NaN"); } #[test] fn shorter() { // A possibly shorter underestimate is picked (u' in Schubfach). assert_eq!(dtoa(-4.932096661796888e-226), "-4.932096661796888e-226"); // A possibly shorter overestimate is picked (w' in Schubfach). assert_eq!(dtoa(3.439070283483335e+35), "3.439070283483335e+35"); } #[test] fn single_candidate() { // Only an underestimate is in the rounding region (u in Schubfach). assert_eq!(dtoa(6.606854224493745e-17), "6.606854224493745e-17"); // Only an overestimate is in the rounding region (w in Schubfach). assert_eq!(dtoa(6.079537928711555e+61), "6.079537928711555e+61"); } #[test] fn null_terminated() { assert_eq!(dtoa(9.061488e15), "9061488000000000.0"); assert_eq!(dtoa(f64::NAN.copysign(1.0)), "NaN"); } #[test] fn no_buffer() { assert_eq!(dtoa(6.62607015e-34), "6.62607015e-34"); } } mod ftoa_test { use super::ftoa; #[test] fn normal() { assert_eq!(ftoa(6.62607e-34), "6.62607e-34"); assert_eq!(ftoa(1.342178e+08), "134217800.0"); assert_eq!(ftoa(1.3421781e+08), "134217810.0"); } #[test] fn subnormal() { assert_eq!(ftoa(0.0f32.next_up()), "1e-45"); } #[test] fn no_buffer() { assert_eq!(ftoa(6.62607e-34), "6.62607e-34"); } }