ryu-js-1.0.1/.cargo_vcs_info.json0000644000000001360000000000100122640ustar { "git": { "sha1": "8659928b16c9ba60f785ce475845c23070a1394d" }, "path_in_vcs": "" }ryu-js-1.0.1/.clippy.toml000064400000000000000000000000201046102023000133170ustar 00000000000000msrv = "1.64.0" ryu-js-1.0.1/.github/ISSUE_TEMPLATE/bug_report.md000064400000000000000000000016231046102023000172730ustar 00000000000000--- name: "\U0001F41B Bug report" about: Create a report to help us improve title: "" labels: bug assignees: "" --- **Describe the bug** **To Reproduce** **Expected behavior** **Build environment (please complete the following information):** - OS: [e.g. Fedora Linux] - Version: [e.g. 32] - Target triple: [e.g. x86_64-unknown-linux-gnu] - Rustc version: [e.g. rustc 1.43.0 (4fb7144ed 2020-04-20), running `rustc -V`] **Additional context** ryu-js-1.0.1/.github/ISSUE_TEMPLATE/config.yml000064400000000000000000000002321046102023000165640ustar 00000000000000blank_issues_enabled: false contact_links: - name: Discord channel url: https://discord.gg/tUFFk9Y about: Please ask and answer questions here. ryu-js-1.0.1/.github/ISSUE_TEMPLATE/custom.md000064400000000000000000000003461046102023000164360ustar 00000000000000--- name: Custom about: Open an issue in the repo that is neither a bug or a feature, such a new idea title: "" labels: "" assignees: "" --- ryu-js-1.0.1/.github/ISSUE_TEMPLATE/feature_request.md000064400000000000000000000005611046102023000203260ustar 00000000000000--- name: "\U0001F680 Feature request" about: Suggest a new feature to be implemented. title: "" labels: enhancement assignees: "" --- **Feature description** ryu-js-1.0.1/.github/PULL_REQUEST_TEMPLATE.md000064400000000000000000000003361046102023000162170ustar 00000000000000 This Pull Request fixes/closes #{issue_num}. It changes the following: - - - ryu-js-1.0.1/.github/dependabot.yml000064400000000000000000000003011046102023000152360ustar 00000000000000version: 2 updates: - package-ecosystem: github-actions directory: / schedule: interval: daily - package-ecosystem: cargo directory: / schedule: interval: daily ryu-js-1.0.1/.github/workflows/benchmark.yml000064400000000000000000000014741046102023000171340ustar 00000000000000name: Benchmarks on: pull_request: branches: - main workflow_dispatch: permissions: contents: read jobs: benchmarks: name: Benchmarks runs-on: ubuntu-latest timeout-minutes: 45 steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - uses: actions-rs/toolchain@v1.0.7 with: toolchain: stable override: true profile: minimal - name: Cache cargo uses: actions/cache@v4 with: path: | target ~/.cargo/git ~/.cargo/registry key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - uses: boa-dev/criterion-compare-action@v3.2.4 with: token: ${{ secrets.GITHUB_TOKEN }} branchName: ${{ github.base_ref }} ryu-js-1.0.1/.github/workflows/ci.yml000064400000000000000000000046741046102023000156020ustar 00000000000000name: CI on: pull_request: branches: - main push: branches: - main workflow_dispatch: schedule: [cron: "40 1 * * *"] merge_group: types: [checks_requested] permissions: contents: read env: RUSTFLAGS: -Dwarnings jobs: test: name: Rust ${{matrix.rust}} runs-on: ubuntu-latest strategy: fail-fast: false matrix: rust: [nightly, beta, stable] timeout-minutes: 45 steps: - uses: actions/checkout@v4 - 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 test - run: cargo test --features small - run: cargo build --tests --features no-panic --release if: matrix.rust == 'nightly' msrv: name: Rust 1.64.0 runs-on: ubuntu-latest timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@1.64.0 - run: cargo build - run: cargo build --features small doc: name: Documentation runs-on: ubuntu-latest timeout-minutes: 45 env: RUSTDOCFLAGS: -Dwarnings steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly - uses: dtolnay/install@cargo-docs-rs - run: cargo docs-rs miri: name: Miri runs-on: ubuntu-latest timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@miri - run: cargo miri setup - run: cargo miri test env: MIRIFLAGS: -Zmiri-strict-provenance clippy: name: Clippy runs-on: ubuntu-latest timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@clippy - run: cargo clippy --tests --benches -- -Dclippy::all -Dclippy::pedantic outdated: name: Outdated runs-on: ubuntu-latest timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/install@cargo-outdated - run: cargo outdated --workspace --exit-code 1 - run: cargo outdated --manifest-path fuzz/Cargo.toml --exit-code 1 fuzz: name: Fuzz runs-on: ubuntu-latest timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly - uses: dtolnay/install@cargo-fuzz - run: cargo fuzz check ryu-js-1.0.1/.github/workflows/release.yml000064400000000000000000000013331046102023000166140ustar 00000000000000name: Publish Release on: release: types: [published] jobs: publish: name: Publish crate runs-on: ubuntu-latest timeout-minutes: 60 env: RUSTFLAGS: -D warnings steps: - name: Checkout repository uses: actions/checkout@v4 - name: Install Rust toolchain uses: dtolnay/rust-toolchain@stable - name: Package (dry-run) run: cargo package - name: Publish env: CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} PATCH: ${{ github.run_number }} shell: bash run: | git config --global user.email "runner@gha.local" git config --global user.name "Github Action" cargo publish ryu-js-1.0.1/.gitignore000064400000000000000000000000501046102023000130370ustar 00000000000000/target /upstream **/*.rs.bk Cargo.lock ryu-js-1.0.1/.vscode/launch.json000064400000000000000000000152741046102023000145730ustar 00000000000000{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "lldb", "request": "launch", "name": "Debug unit tests in library 'ryu-js'", "cargo": { "args": [ "test", "--no-run", "--lib", "--package=ryu-js" ], "filter": { "name": "ryu-js", "kind": "lib" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug example 'upstream_benchmark'", "cargo": { "args": [ "build", "--example=upstream_benchmark", "--package=ryu-js" ], "filter": { "name": "upstream_benchmark", "kind": "example" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug unit tests in example 'upstream_benchmark'", "cargo": { "args": [ "test", "--no-run", "--example=upstream_benchmark", "--package=ryu-js" ], "filter": { "name": "upstream_benchmark", "kind": "example" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug integration test 'd2s_test'", "cargo": { "args": [ "test", "--no-run", "--test=d2s_test", "--package=ryu-js" ], "filter": { "name": "d2s_test", "kind": "test" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug integration test 's2f_test'", "cargo": { "args": [ "test", "--no-run", "--test=s2f_test", "--package=ryu-js" ], "filter": { "name": "s2f_test", "kind": "test" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug integration test 'to_fixed'", "cargo": { "args": [ "test", "--no-run", "--test=to_fixed", "--package=ryu-js" ], "filter": { "name": "to_fixed", "kind": "test" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug integration test 's2d_test'", "cargo": { "args": [ "test", "--no-run", "--test=s2d_test", "--package=ryu-js" ], "filter": { "name": "s2d_test", "kind": "test" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug integration test 'common_test'", "cargo": { "args": [ "test", "--no-run", "--test=common_test", "--package=ryu-js" ], "filter": { "name": "common_test", "kind": "test" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug integration test 'exhaustive'", "cargo": { "args": [ "test", "--no-run", "--test=exhaustive", "--package=ryu-js" ], "filter": { "name": "exhaustive", "kind": "test" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug integration test 'f2s_test'", "cargo": { "args": [ "test", "--no-run", "--test=f2s_test", "--package=ryu-js" ], "filter": { "name": "f2s_test", "kind": "test" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug integration test 'd2s_table_test'", "cargo": { "args": [ "test", "--no-run", "--test=d2s_table_test", "--package=ryu-js" ], "filter": { "name": "d2s_table_test", "kind": "test" } }, "args": [], "cwd": "${workspaceFolder}" }, { "type": "lldb", "request": "launch", "name": "Debug benchmark 'bench'", "cargo": { "args": [ "test", "--no-run", "--bench=bench", "--package=ryu-js" ], "filter": { "name": "bench", "kind": "bench" } }, "args": [], "cwd": "${workspaceFolder}" } ] } ryu-js-1.0.1/CHANGELOG.md000064400000000000000000000071301046102023000126660ustar 00000000000000# CHANGELOG ## [# 1.0.1 (2024-03-02)](https://github.com/boa-dev/ryu-js/compare/v1.0.0...v1.0.1) ### Internal Improvements - [INTERNAL #45](https://github.com/boa-dev/ryu-js/pull/45): Add release workflow. (@HalidOdat) - [INTERNAL #43](https://github.com/boa-dev/ryu-js/pull/43): Add `#[inline]` to public functions. (@HalidOdat) - [INTERNAL #42](https://github.com/boa-dev/ryu-js/pull/42): Sync upstream/master. (@HalidOdat, @jedel1043) ## [# 1.0.0 (2023-10-05)](https://github.com/boa-dev/ryu-js/compare/v0.2.2...v1.0.0) - ECMAScript compliant implementation of `Number.prototype.toFixed()` ### Breaking Changes - Minimum rust version has been bumped from `1.36.0` to `1.64.0`. ### Feature Enhancements - [FEATURE](https://github.com/boa-dev/ryu-js/pull/35): ECMAScript specification compliant `Number.prototype.toFixed()` implementation. (@HalidOdat) ### Internal Improvements - [INTERNAL #1](https://github.com/boa-dev/ryu-js/pull/19): Added dependabot PRs. (@Razican) - [INTERNAL #2](https://github.com/boa-dev/ryu-js/pull/21): Sync upstream/master. (@HalidOdat) - [INTERNAL #3](https://github.com/boa-dev/ryu-js/pull/27): Add issue and PR templates. (@HalidOdat) - [INTERNAL #4](https://github.com/boa-dev/ryu-js/pull/28): Switch to criterion for benchmarking. (@HalidOdat) - [INTERNAL #5](https://github.com/boa-dev/ryu-js/pull/29): Benchmark CI. (@HalidOdat) - [INTERNAL #6](https://github.com/boa-dev/ryu-js/pull/38): Enable merge queue. (@jedel1043) ## [# 0.2.2 (2020-12-16)](https://github.com/boa-dev/ryu-js/compare/v0.2.1...v0.2.2) ### Internal Improvements - [INTERNAL #17](https://github.com/boa-dev/ryu-js/pull/17) Sync to `dtolnay/ryu` master - [INTERNAL #16](https://github.com/boa-dev/ryu-js/pull/16) Sync to `dtolnay/ryu` master ## [# 0.2.1 (2020-11-11)](https://github.com/boa-dev/ryu-js/compare/v0.2.0...v0.2.1) ### Feature Enhancements - [FEATURE #11](https://github.com/boa-dev/ryu-js/pull/11): Null check in unsafe `format32` and `format64` (in debug mode). (@HalidOdat) ### Bug Fixes - BUG [#12](https://github.com/boa-dev/ryu-js/pull/12) [#13](https://github.com/boa-dev/ryu-js/pull/13): Documentation fixes (@HalidOdat) ## [# 0.2.0 (2020-07-14) - ECMAScript compliant `f32` conversions Release](https://github.com/boa-dev/ryu-js/compare/v0.1.0...v0.2.0) ### Feature Enhancements - [FEATURE #6](https://github.com/boa-dev/ryu-js/pull/6): ECMAScript specification complaint `f32` to string conversions. (@HalidOdat) ### Bug Fixes - [BUG #2](https://github.com/boa-dev/ryu-js/pull/2) (@HalidOdat): - Fixed compatibility with rust `1.31.0`. - Fixed converting from `-0.0` to `0`. - Fixed max length docs for `format32` and `format64`. ### Internal Improvements - [INTERNAL #2](https://github.com/boa-dev/ryu-js/pull/2): Optimized `0` and `-0` to string conversion (@HalidOdat) # # 0.1.0 (2020-07-13) - ECMAScript compliant `f64` conversions Release This is the initial release of this crate, it introduces ECMAScript compliant `f64` to string conversions. ### Feature Enhancements - [FEATURE](https://github.com/boa-dev/ryu-js/commit/ed781f5772882e38c53d40707a60b4f11414b9c8): ECMAScript specification complaint `f64` to string conversions. (@Tropid) - [FEATURE](https://github.com/boa-dev/ryu-js/commit/fe366fa397d04324fa693b5d85134851b09719b3): Change name from `ryu` to `ryu-js`. (@Tropid) ### Bug Fixes - [BUG #1](https://github.com/boa-dev/ryu-js/pull/1): Fixed buffer overflow with length greater than 24 (max is 25). (@HalidOdat) ### Internal Improvements - [INTERNAL #1](https://github.com/boa-dev/ryu-js/pull/2): Fixed all clippy warnings/errors and tests (@HalidOdat) ryu-js-1.0.1/CODE_OF_CONDUCT.md000064400000000000000000000127101046102023000136540ustar 00000000000000# Contributor Covenant Code of Conduct ## Our Pledge We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, caste, color, religion, or sexual identity and orientation. We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. ## Our Standards Examples of behavior that contributes to a positive environment for our community include: - Demonstrating empathy and kindness toward other people - Being respectful of differing opinions, viewpoints, and experiences - Giving and gracefully accepting constructive feedback - Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience - Focusing on what is best not just for us as individuals, but for the overall community Examples of unacceptable behavior include: - The use of sexualized language or imagery, and sexual attention or advances of any kind - Trolling, insulting or derogatory comments, and personal or political attacks - Public or private harassment - Publishing others' private information, such as a physical or email address, without their explicit permission - Other conduct which could reasonably be considered inappropriate in a professional setting ## Enforcement Responsibilities Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. ## Scope This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at [discord](https://discord.gg/tUFFk9Y) by contacting anyone in the _@boa_dev_ group (check the yellow usernames). All complaints will be reviewed and investigated promptly and fairly. All community leaders are obligated to respect the privacy and security of the reporter of any incident. ## Enforcement Guidelines Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: ### 1. Correction **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. ### 2. Warning **Community Impact**: A violation through a single incident or series of actions. **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. ### 3. Temporary Ban **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. ### 4. Permanent Ban **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. **Consequence**: A permanent ban from any sort of public interaction within the community. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder][mozilla coc]. For answers to common questions about this code of conduct, see the FAQ at [https://www.contributor-covenant.org/faq][faq]. Translations are available at [https://www.contributor-covenant.org/translations][translations]. [homepage]: https://www.contributor-covenant.org [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html [mozilla coc]: https://github.com/mozilla/diversity [faq]: https://www.contributor-covenant.org/faq [translations]: https://www.contributor-covenant.org/translations ryu-js-1.0.1/Cargo.lock0000644000000436150000000000100102500ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "aho-corasick" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" dependencies = [ "memchr", ] [[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.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bumpalo" version = "3.15.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea184aa71bb362a1157c896979544cc23974e08fd265f29ea96b59f0b4a555b" [[package]] name = "cast" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[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.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" version = "4.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f3e7391dad68afb0c2ede1bf619f579a3dc9c2ec67f089baa397123a2f3d1eb" dependencies = [ "anstyle", "clap_lex", ] [[package]] name = "clap_lex" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" [[package]] name = "criterion" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" dependencies = [ "anes", "cast", "ciborium", "clap", "criterion-plot", "is-terminal", "itertools", "num-traits", "once_cell", "oorandom", "plotters", "rayon", "regex", "serde", "serde_derive", "serde_json", "tinytemplate", "walkdir", ] [[package]] name = "criterion-plot" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" dependencies = [ "cast", "itertools", ] [[package]] name = "crossbeam-deque" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" dependencies = [ "crossbeam-epoch", "crossbeam-utils", ] [[package]] name = "crossbeam-epoch" version = "0.9.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" dependencies = [ "crossbeam-utils", ] [[package]] name = "crossbeam-utils" version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" [[package]] name = "crunchy" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" [[package]] name = "either" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" [[package]] name = "getrandom" version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" dependencies = [ "cfg-if", "libc", "wasi", ] [[package]] name = "half" version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5eceaaeec696539ddaf7b333340f1af35a5aa87ae3e4f3ead0532f72affab2e" dependencies = [ "cfg-if", "crunchy", ] [[package]] name = "hermit-abi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "is-terminal" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" dependencies = [ "hermit-abi", "libc", "windows-sys", ] [[package]] name = "itertools" version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" dependencies = [ "either", ] [[package]] name = "itoa" version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" [[package]] name = "js-sys" version = "0.3.68" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" dependencies = [ "wasm-bindgen", ] [[package]] name = "libc" version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "log" version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "memchr" version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "no-panic" version = "0.1.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c711522eedec2a96bb3672ad60a03561cb28934ab1e9b97d2ecb58e07c79ef52" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "num-traits" version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" dependencies = [ "autocfg", ] [[package]] name = "num_cpus" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ "hermit-abi", "libc", ] [[package]] name = "once_cell" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "oorandom" version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" [[package]] name = "plotters" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" dependencies = [ "num-traits", "plotters-backend", "plotters-svg", "wasm-bindgen", "web-sys", ] [[package]] name = "plotters-backend" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609" [[package]] name = "plotters-svg" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab" dependencies = [ "plotters-backend", ] [[package]] name = "ppv-lite86" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] [[package]] name = "rand" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha", "rand_core", ] [[package]] name = "rand_chacha" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", "rand_core", ] [[package]] name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ "getrandom", ] [[package]] name = "rand_xorshift" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" dependencies = [ "rand_core", ] [[package]] name = "rayon" version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4963ed1bc86e4f3ee217022bd855b297cef07fb9eac5dfa1f788b220b49b3bd" dependencies = [ "either", "rayon-core", ] [[package]] name = "rayon-core" version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" dependencies = [ "crossbeam-deque", "crossbeam-utils", ] [[package]] name = "regex" version = "1.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "ryu" version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" [[package]] name = "ryu-js" version = "1.0.1" dependencies = [ "criterion", "no-panic", "num_cpus", "rand", "rand_xorshift", ] [[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 = "serde" version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" dependencies = [ "itoa", "ryu", "serde", ] [[package]] name = "syn" version = "2.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" 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.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[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 = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" dependencies = [ "cfg-if", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", "syn", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" dependencies = [ "quote", "wasm-bindgen-macro-support", ] [[package]] name = "wasm-bindgen-macro-support" version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" dependencies = [ "proc-macro2", "quote", "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" [[package]] name = "web-sys" version = "0.3.68" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96565907687f7aceb35bc5fc03770a8a0471d82e479f25832f54a0e3f4b28446" dependencies = [ "js-sys", "wasm-bindgen", ] [[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.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] [[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-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" [[package]] name = "windows_aarch64_msvc" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" [[package]] name = "windows_i686_gnu" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" [[package]] name = "windows_i686_msvc" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" [[package]] name = "windows_x86_64_gnu" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" [[package]] name = "windows_x86_64_msvc" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" ryu-js-1.0.1/Cargo.toml0000644000000026720000000000100102710ustar # 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 = "2018" rust-version = "1.64" name = "ryu-js" version = "1.0.1" authors = [ "David Tolnay ", "boa-dev", ] exclude = [ "performance.png", "chart/**", ] description = "Fast floating point to string conversion, ECMAScript compliant." documentation = "https://docs.rs/ryu-js" readme = "README.md" keywords = ["float"] categories = [ "value-formatting", "no-std", "no-std::no-alloc", ] license = "Apache-2.0 OR BSL-1.0" repository = "https://github.com/boa-dev/ryu-js" [package.metadata.docs.rs] rustdoc-args = ["--generate-link-to-definition"] targets = ["x86_64-unknown-linux-gnu"] [lib] bench = false doc-scrape-examples = false [[bench]] name = "bench" harness = false [dependencies.no-panic] version = "0.1" optional = true [dev-dependencies.criterion] version = "0.5" [dev-dependencies.num_cpus] version = "1.8" [dev-dependencies.rand] version = "0.8" [dev-dependencies.rand_xorshift] version = "0.3" [features] small = [] ryu-js-1.0.1/Cargo.toml.orig000064400000000000000000000022411046102023000137420ustar 00000000000000[package] name = "ryu-js" version = "1.0.1" authors = ["David Tolnay ", "boa-dev"] categories = ["value-formatting", "no-std", "no-std::no-alloc"] description = "Fast floating point to string conversion, ECMAScript compliant." documentation = "https://docs.rs/ryu-js" edition = "2018" exclude = ["performance.png", "chart/**"] keywords = ["float"] license = "Apache-2.0 OR BSL-1.0" repository = "https://github.com/boa-dev/ryu-js" rust-version = "1.64" [features] # Use smaller lookup tables. Instead of storing every required power of # 5, only store every 26th entry, and compute intermediate values with a # multiplication. This reduces the lookup table size by about 10x (only # one case, and only f64) at the cost of some performance. small = [] [dependencies] no-panic = { version = "0.1", optional = true } [dev-dependencies] num_cpus = "1.8" rand = "0.8" rand_xorshift = "0.3" criterion = "0.5" [lib] bench = false doc-scrape-examples = false [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] # See: https://github.com/rust-lang/rust/pull/84176 rustdoc-args = ["--generate-link-to-definition"] [[bench]] name = "bench" harness = false ryu-js-1.0.1/LICENSE-APACHE000064400000000000000000000227731046102023000130130ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS ryu-js-1.0.1/LICENSE-BOOST000064400000000000000000000024721046102023000127520ustar 00000000000000Boost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. 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, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ryu-js-1.0.1/README.md000064400000000000000000000065701046102023000123430ustar 00000000000000# Ryū-js Ryū-js is a fork of the [ryu][ryu-crate] crate adjusted to comply to the ECMAScript [number-to-string][number-to-string] algorithm, also implementing [`Number.prototype.toFixed()`][number-to-fixed] compliant conversion. [ryu-crate]: https://crates.io/crates/ryu [number-to-string]: https://tc39.es/ecma262/#sec-numeric-types-number-tostring [number-to-fixed]: https://tc39.es/ecma262/#sec-number.prototype.tofixed Pure Rust implementation of Ryū, an algorithm to quickly convert floating point numbers to decimal strings. The PLDI'18 paper [*Ryū: fast float-to-string conversion*][paper] by Ulf Adams includes a complete correctness proof of the algorithm. The paper is available under the creative commons CC-BY-SA license. This Rust implementation is a line-by-line port of Ulf Adams' implementation in C, [https://github.com/ulfjack/ryu][upstream]. *Requirements: This crate supports any compiler version back to rustc 1.64; it uses nothing from the Rust standard library so is usable from no_std crates.* [paper]: https://dl.acm.org/citation.cfm?id=3192369 [upstream]: https://github.com/ulfjack/ryu/tree/abf76d252bc97300354857e64e80d4a2bf664291 ```toml [dependencies] ryu-js = "1.0" ```
## Example ```rust fn main() { let mut buffer = ryu_js::Buffer::new(); let printed = buffer.format(1.234); assert_eq!(printed, "1.234"); } ``` ## Performance You can run upstream's benchmarks with: ```console $ git clone https://github.com/ulfjack/ryu c-ryu $ cd c-ryu $ bazel run -c opt //ryu/benchmark:ryu_benchmark ``` And the same benchmark against our implementation with: ```console $ git clone https://github.com/boa-dev/ryu-js rust-ryu $ cd rust-ryu $ cargo run --example upstream_benchmark --release ``` The benchmarks measure the average time to print a 32-bit float and average time to print a 64-bit float, where the inputs are distributed as uniform random bit patterns 32 and 64 bits wide. The upstream C code, the unsafe direct Rust port, and the safe pretty Rust API all perform the same, taking around 21 nanoseconds to format a 32-bit float and 31 nanoseconds to format a 64-bit float. There is also a Rust-specific benchmark comparing this implementation to the standard library which you can run with: ```console $ cargo bench ``` The benchmark shows Ryū approximately 2-5x faster than the standard library across a range of f32 and f64 inputs. Measurements are in nanoseconds per iteration; smaller is better. ## Formatting This library tends to produce more human-readable output than the standard library's to\_string, which never uses scientific notation. Here are two examples: - *ryu:* 1.23e40, *std:* 12300000000000000000000000000000000000000 - *ryu:* 1.23e-40, *std:* 0.000000000000000000000000000000000000000123 Both libraries print short decimals such as 0.0000123 without scientific notation.
#### License Licensed under either of Apache License, Version 2.0 or Boost Software License 1.0 at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. ryu-js-1.0.1/benches/bench.rs000064400000000000000000000034241046102023000141130ustar 00000000000000// cargo bench #![allow( clippy::approx_constant, clippy::excessive_precision, clippy::unreadable_literal )] use criterion::{black_box, criterion_group, criterion_main, Criterion}; use std::io::Write; macro_rules! benches { ($($name:ident($value:expr),)*) => { mod bench_ryu_js { use super::*; $( pub fn $name(c: &mut Criterion) { let mut buf = ryu_js::Buffer::new(); c.bench_function(concat!("ryu_js_", stringify!($name)), move |b| b.iter(move || { let value = black_box($value); let formatted = buf.format_finite(value); black_box(formatted); })); } )* } criterion_group!(bench_ryu_js, $( bench_ryu_js::$name, )*); mod bench_std_fmt { use super::*; $( pub fn $name(c: &mut Criterion) { let mut buf = Vec::with_capacity(20); c.bench_function(concat!("std_fmt_", stringify!($name)), move |b| b.iter(|| { buf.clear(); let value = black_box($value); write!(&mut buf, "{}", value).unwrap(); black_box(buf.as_slice()); })); } )* } criterion_group!(bench_std_fmt, $( bench_std_fmt::$name, )*); criterion_main!(bench_ryu_js, bench_std_fmt); }; } benches! { bench_0_f64(0_f64), bench_short_f64(0.1234_f64), bench_e_f64(2.718281828459045_f64), bench_max_f64(f64::MAX), bench_0_f32(0_f32), bench_short_f32(0.1234_f32), bench_e_f32(2.718281828459045_f32), bench_max_f32(f32::MAX), } ryu-js-1.0.1/examples/upstream_benchmark.rs000064400000000000000000000042201046102023000171100ustar 00000000000000// cargo run --example upstream_benchmark --release use rand::{Rng, SeedableRng}; const SAMPLES: usize = 10000; const ITERATIONS: usize = 1000; struct MeanAndVariance { n: i64, mean: f64, m2: f64, } impl MeanAndVariance { fn new() -> Self { MeanAndVariance { n: 0, mean: 0.0, m2: 0.0, } } fn update(&mut self, x: f64) { self.n += 1; let d = x - self.mean; self.mean += d / self.n as f64; let d2 = x - self.mean; self.m2 += d * d2; } fn variance(&self) -> f64 { self.m2 / (self.n - 1) as f64 } fn stddev(&self) -> f64 { self.variance().sqrt() } } macro_rules! benchmark { ($name:ident, $ty:ident) => { fn $name() -> usize { let mut rng = rand_xorshift::XorShiftRng::from_seed([123u8; 16]); let mut mv = MeanAndVariance::new(); let mut throwaway = 0; for _ in 0..SAMPLES { let f = loop { let f = $ty::from_bits(rng.gen()); if f.is_finite() { break f; } }; let t1 = std::time::SystemTime::now(); for _ in 0..ITERATIONS { throwaway += ryu_js::Buffer::new().format_finite(f).len(); } let duration = t1.elapsed().unwrap(); let nanos = duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as u64; mv.update(nanos as f64 / ITERATIONS as f64); } println!( "{:12} {:8.3} {:8.3}", concat!(stringify!($name), ":"), mv.mean, mv.stddev(), ); throwaway } }; } benchmark!(pretty32, f32); benchmark!(pretty64, f64); fn main() { println!("{:>20}{:>9}", "Average", "Stddev"); let mut throwaway = 0; throwaway += pretty32(); throwaway += pretty64(); if std::env::var_os("ryu-benchmark").is_some() { // Prevent the compiler from optimizing the code away. println!("{}", throwaway); } } ryu-js-1.0.1/src/buffer/mod.rs000064400000000000000000000166311046102023000142500ustar 00000000000000use crate::pretty::to_fixed::MAX_BUFFER_SIZE; use crate::raw; use core::mem::MaybeUninit; use core::{slice, str}; #[cfg(feature = "no-panic")] use no_panic::no_panic; const NAN: &str = "NaN"; const INFINITY: &str = "Infinity"; const NEG_INFINITY: &str = "-Infinity"; const BUFFER_SIZE: usize = MAX_BUFFER_SIZE; /// Safe API for formatting floating point numbers to text. /// /// ## Example /// /// ``` /// let mut buffer = ryu_js::Buffer::new(); /// let printed = buffer.format_finite(1.234); /// assert_eq!(printed, "1.234"); /// ``` #[derive(Copy)] 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 /// "Infinity", and negative infinity as "-Infinity" to match the [ECMAScript specification][spec]. /// /// 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. /// /// [spec]: https://tc39.es/ecma262/#sec-numeric-types-number-tostring #[inline] #[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`]: https://doc.rust-lang.org/std/primitive.f64.html#method.is_finite /// [`is_nan`]: https://doc.rust-lang.org/std/primitive.f64.html#method.is_nan /// [`is_infinite`]: https://doc.rust-lang.org/std/primitive.f64.html#method.is_infinite #[inline] #[cfg_attr(feature = "no-panic", no_panic)] pub fn format_finite(&mut self, f: F) -> &str { unsafe { let n = f.write_to_ryu_buffer(self.bytes.as_mut_ptr().cast::()); debug_assert!(n <= self.bytes.len()); let slice = slice::from_raw_parts(self.bytes.as_ptr().cast::(), n); str::from_utf8_unchecked(slice) } } /// Print a floating point number into this buffer using the `Number.prototype.toFixed()` notation /// and return a reference to its string representation within the buffer. /// /// The `fraction_digits` argument must be between `[0, 100]` inclusive, /// If a values value that is greater than the max is passed in will be clamped to max. /// /// # Special cases /// /// This function formats NaN as the string "NaN", positive infinity as /// "Infinity", and negative infinity as "-Infinity" to match the [ECMAScript specification][spec]. /// /// [spec]: https://tc39.es/ecma262/#sec-numeric-types-number-tofixed #[inline] #[cfg_attr(feature = "no-panic", no_panic)] pub fn format_to_fixed(&mut self, f: F, fraction_digits: u8) -> &str { let fraction_digits = fraction_digits.min(100); if f.is_nonfinite() { return f.format_nonfinite(); } unsafe { let n = f.write_to_ryu_buffer_to_fixed( fraction_digits, self.bytes.as_mut_ptr().cast::(), ); debug_assert!(n <= self.bytes.len()); let slice = slice::from_raw_parts(self.bytes.as_ptr().cast::(), n); str::from_utf8_unchecked(slice) } } } impl Clone for Buffer { #[inline] #[allow(clippy::non_canonical_clone_impl)] // false positive https://github.com/rust-lang/rust-clippy/issues/11072 fn clone(&self) -> Self { Buffer::new() } } impl Default for Buffer { #[inline] #[cfg_attr(feature = "no-panic", no_panic)] fn default() -> Self { Buffer::new() } } /// A floating point number, [`f32`] or [`f64`], that can be written into a /// [`ryu_js::Buffer`][Buffer]. /// /// This trait is sealed and cannot be implemented for types outside of the /// `ryu-js` crate. pub trait Float: Sealed {} impl Float for f32 {} impl Float for f64 {} /// A floating point number that can be written into a /// [`ryu_js::Buffer`][Buffer] using the fixed notation as defined in the /// [`Number.prototype.toFixed( fractionDigits )`][spec] ECMAScript specification. /// /// This trait is sealed and cannot be implemented for types outside of the /// `ryu-js` crate. /// /// [spec]: https://tc39.es/ecma262/#sec-number.prototype.tofixed pub trait FloatToFixed: Sealed {} impl FloatToFixed for f64 {} pub trait Sealed: Copy { fn is_nonfinite(self) -> bool; fn format_nonfinite(self) -> &'static str; unsafe fn write_to_ryu_buffer(self, result: *mut u8) -> usize; unsafe fn write_to_ryu_buffer_to_fixed(self, fraction_digits: u8, result: *mut u8) -> usize; } 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 { NAN } else if bits & SIGN_MASK != 0 { NEG_INFINITY } else { INFINITY } } #[inline] unsafe fn write_to_ryu_buffer(self, result: *mut u8) -> usize { raw::format32(self, result) } #[inline] unsafe fn write_to_ryu_buffer_to_fixed(self, _fraction_digits: u8, _result: *mut u8) -> usize { panic!("toFixed for f32 type is not implemented yet!") } } 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 { NAN } else if bits & SIGN_MASK != 0 { NEG_INFINITY } else { INFINITY } } #[inline] unsafe fn write_to_ryu_buffer(self, result: *mut u8) -> usize { raw::format64(self, result) } #[inline] unsafe fn write_to_ryu_buffer_to_fixed(self, fraction_digits: u8, result: *mut u8) -> usize { raw::format64_to_fixed(self, fraction_digits, result) } } ryu-js-1.0.1/src/common.rs000064400000000000000000000062451046102023000135100ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. // Returns the number of decimal digits in v, which must not contain more than 9 // digits. #[cfg_attr(feature = "no-panic", inline)] pub fn decimal_length9(v: u32) -> u32 { // Function precondition: v is not a 10-digit number. // (f2s: 9 digits are sufficient for round-tripping.) debug_assert!(v < 1000000000); if v >= 100000000 { 9 } else if v >= 10000000 { 8 } else if v >= 1000000 { 7 } else if v >= 100000 { 6 } else if v >= 10000 { 5 } else if v >= 1000 { 4 } else if v >= 100 { 3 } else if v >= 10 { 2 } else { 1 } } // Returns e == 0 ? 1 : [log_2(5^e)]; requires 0 <= e <= 3528. #[cfg_attr(feature = "no-panic", inline)] #[allow(dead_code)] pub fn log2_pow5(e: i32) -> i32 /* or u32 -> u32 */ { // This approximation works up to the point that the multiplication // overflows at e = 3529. If the multiplication were done in 64 bits, it // would fail at 5^4004 which is just greater than 2^9297. debug_assert!(e >= 0); debug_assert!(e <= 3528); ((e as u32 * 1217359) >> 19) as i32 } // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528. #[cfg_attr(feature = "no-panic", inline)] pub fn pow5bits(e: i32) -> i32 /* or u32 -> u32 */ { // This approximation works up to the point that the multiplication // overflows at e = 3529. If the multiplication were done in 64 bits, it // would fail at 5^4004 which is just greater than 2^9297. debug_assert!(e >= 0); debug_assert!(e <= 3528); (((e as u32 * 1217359) >> 19) + 1) as i32 } #[cfg_attr(feature = "no-panic", inline)] #[allow(dead_code)] pub fn ceil_log2_pow5(e: i32) -> i32 /* or u32 -> u32 */ { log2_pow5(e) + 1 } // Returns floor(log_10(2^e)); requires 0 <= e <= 1650. #[cfg_attr(feature = "no-panic", inline)] pub fn log10_pow2(e: i32) -> u32 /* or u32 -> u32 */ { // The first value this approximation fails for is 2^1651 which is just greater than 10^297. debug_assert!(e >= 0); debug_assert!(e <= 1650); (e as u32 * 78913) >> 18 } // Returns floor(log_10(5^e)); requires 0 <= e <= 2620. #[cfg_attr(feature = "no-panic", inline)] pub fn log10_pow5(e: i32) -> u32 /* or u32 -> u32 */ { // The first value this approximation fails for is 5^2621 which is just greater than 10^1832. debug_assert!(e >= 0); debug_assert!(e <= 2620); (e as u32 * 732923) >> 20 } ryu-js-1.0.1/src/d2s.rs000064400000000000000000000263321046102023000127070ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. use crate::common::{log10_pow2, log10_pow5, pow5bits}; #[cfg(not(feature = "small"))] pub use crate::d2s_full_table::{DOUBLE_POW5_INV_SPLIT, DOUBLE_POW5_SPLIT}; use crate::d2s_intrinsics::{ div10, div100, div5, mul_shift_all_64, multiple_of_power_of_2, multiple_of_power_of_5, }; #[cfg(feature = "small")] pub use crate::d2s_small_table::{compute_inv_pow5, compute_pow5}; use core::mem::MaybeUninit; pub const DOUBLE_MANTISSA_BITS: u32 = 52; pub const DOUBLE_EXPONENT_BITS: u32 = 11; pub const DOUBLE_BIAS: i32 = 1023; pub const DOUBLE_POW5_INV_BITCOUNT: i32 = 125; pub const DOUBLE_POW5_BITCOUNT: i32 = 125; #[cfg_attr(feature = "no-panic", inline)] pub fn decimal_length17(v: u64) -> u32 { // This is slightly faster than a loop. // The average output length is 16.38 digits, so we check high-to-low. // Function precondition: v is not an 18, 19, or 20-digit number. // (17 digits are sufficient for round-tripping.) debug_assert!(v < 100000000000000000); if v >= 10000000000000000 { 17 } else if v >= 1000000000000000 { 16 } else if v >= 100000000000000 { 15 } else if v >= 10000000000000 { 14 } else if v >= 1000000000000 { 13 } else if v >= 100000000000 { 12 } else if v >= 10000000000 { 11 } else if v >= 1000000000 { 10 } else if v >= 100000000 { 9 } else if v >= 10000000 { 8 } else if v >= 1000000 { 7 } else if v >= 100000 { 6 } else if v >= 10000 { 5 } else if v >= 1000 { 4 } else if v >= 100 { 3 } else if v >= 10 { 2 } else { 1 } } // A floating decimal representing m * 10^e. pub struct FloatingDecimal64 { pub mantissa: u64, // Decimal exponent's range is -324 to 308 // inclusive, and can fit in i16 if needed. pub exponent: i32, } #[cfg_attr(feature = "no-panic", inline)] pub fn d2d(ieee_mantissa: u64, ieee_exponent: u32) -> FloatingDecimal64 { let (e2, m2) = if ieee_exponent == 0 { ( // We subtract 2 so that the bounds computation has 2 additional bits. 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS as i32 - 2, ieee_mantissa, ) } else { ( ieee_exponent as i32 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS as i32 - 2, (1u64 << DOUBLE_MANTISSA_BITS) | ieee_mantissa, ) }; let even = (m2 & 1) == 0; let accept_bounds = even; // Step 2: Determine the interval of valid decimal representations. let mv = 4 * m2; // Implicit bool -> int conversion. True is 1, false is 0. let mm_shift = (ieee_mantissa != 0 || ieee_exponent <= 1) as u32; // We would compute mp and mm like this: // uint64_t mp = 4 * m2 + 2; // uint64_t mm = mv - 1 - mm_shift; // Step 3: Convert to a decimal power base using 128-bit arithmetic. let mut vr: u64; let mut vp: u64; let mut vm: u64; let mut vp_uninit: MaybeUninit = MaybeUninit::uninit(); let mut vm_uninit: MaybeUninit = MaybeUninit::uninit(); let e10: i32; let mut vm_is_trailing_zeros = false; let mut vr_is_trailing_zeros = false; if e2 >= 0 { // I tried special-casing q == 0, but there was no effect on performance. // This expression is slightly faster than max(0, log10_pow2(e2) - 1). let q = log10_pow2(e2) - (e2 > 3) as u32; e10 = q as i32; let k = DOUBLE_POW5_INV_BITCOUNT + pow5bits(q as i32) - 1; let i = -e2 + q as i32 + k; vr = unsafe { mul_shift_all_64( m2, #[cfg(feature = "small")] &compute_inv_pow5(q), #[cfg(not(feature = "small"))] { debug_assert!(q < DOUBLE_POW5_INV_SPLIT.len() as u32); DOUBLE_POW5_INV_SPLIT.get_unchecked(q as usize) }, i as u32, vp_uninit.as_mut_ptr(), vm_uninit.as_mut_ptr(), mm_shift, ) }; vp = unsafe { vp_uninit.assume_init() }; vm = unsafe { vm_uninit.assume_init() }; if q <= 21 { // This should use q <= 22, but I think 21 is also safe. Smaller values // may still be safe, but it's more difficult to reason about them. // Only one of mp, mv, and mm can be a multiple of 5, if any. let mv_mod5 = (mv as u32).wrapping_sub(5u32.wrapping_mul(div5(mv) as u32)); if mv_mod5 == 0 { vr_is_trailing_zeros = multiple_of_power_of_5(mv, q); } else if accept_bounds { // Same as min(e2 + (~mm & 1), pow5_factor(mm)) >= q // <=> e2 + (~mm & 1) >= q && pow5_factor(mm) >= q // <=> true && pow5_factor(mm) >= q, since e2 >= q. vm_is_trailing_zeros = multiple_of_power_of_5(mv - 1 - mm_shift as u64, q); } else { // Same as min(e2 + 1, pow5_factor(mp)) >= q. vp -= multiple_of_power_of_5(mv + 2, q) as u64; } } } else { // This expression is slightly faster than max(0, log10_pow5(-e2) - 1). let q = log10_pow5(-e2) - (-e2 > 1) as u32; e10 = q as i32 + e2; let i = -e2 - q as i32; let k = pow5bits(i) - DOUBLE_POW5_BITCOUNT; let j = q as i32 - k; vr = unsafe { mul_shift_all_64( m2, #[cfg(feature = "small")] &compute_pow5(i as u32), #[cfg(not(feature = "small"))] { debug_assert!(i < DOUBLE_POW5_SPLIT.len() as i32); DOUBLE_POW5_SPLIT.get_unchecked(i as usize) }, j as u32, vp_uninit.as_mut_ptr(), vm_uninit.as_mut_ptr(), mm_shift, ) }; vp = unsafe { vp_uninit.assume_init() }; vm = unsafe { vm_uninit.assume_init() }; if q <= 1 { // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits. // mv = 4 * m2, so it always has at least two trailing 0 bits. vr_is_trailing_zeros = true; if accept_bounds { // mm = mv - 1 - mm_shift, so it has 1 trailing 0 bit iff mm_shift == 1. vm_is_trailing_zeros = mm_shift == 1; } else { // mp = mv + 2, so it always has at least one trailing 0 bit. vp -= 1; } } else if q < 63 { // TODO(ulfjack): Use a tighter bound here. // We want to know if the full product has at least q trailing zeros. // We need to compute min(p2(mv), p5(mv) - e2) >= q // <=> p2(mv) >= q && p5(mv) - e2 >= q // <=> p2(mv) >= q (because -e2 >= q) vr_is_trailing_zeros = multiple_of_power_of_2(mv, q); } } // Step 4: Find the shortest decimal representation in the interval of valid representations. let mut removed = 0i32; let mut last_removed_digit = 0u8; // On average, we remove ~2 digits. let output = if vm_is_trailing_zeros || vr_is_trailing_zeros { // General case, which happens rarely (~0.7%). loop { let vp_div10 = div10(vp); let vm_div10 = div10(vm); if vp_div10 <= vm_div10 { break; } let vm_mod10 = (vm as u32).wrapping_sub(10u32.wrapping_mul(vm_div10 as u32)); let vr_div10 = div10(vr); let vr_mod10 = (vr as u32).wrapping_sub(10u32.wrapping_mul(vr_div10 as u32)); vm_is_trailing_zeros &= vm_mod10 == 0; vr_is_trailing_zeros &= last_removed_digit == 0; last_removed_digit = vr_mod10 as u8; vr = vr_div10; vp = vp_div10; vm = vm_div10; removed += 1; } if vm_is_trailing_zeros { loop { let vm_div10 = div10(vm); let vm_mod10 = (vm as u32).wrapping_sub(10u32.wrapping_mul(vm_div10 as u32)); if vm_mod10 != 0 { break; } let vp_div10 = div10(vp); let vr_div10 = div10(vr); let vr_mod10 = (vr as u32).wrapping_sub(10u32.wrapping_mul(vr_div10 as u32)); vr_is_trailing_zeros &= last_removed_digit == 0; last_removed_digit = vr_mod10 as u8; vr = vr_div10; vp = vp_div10; vm = vm_div10; removed += 1; } } if vr_is_trailing_zeros && last_removed_digit == 5 && vr % 2 == 0 { // Round even if the exact number is .....50..0. last_removed_digit = 4; } // We need to take vr + 1 if vr is outside bounds or we need to round up. vr + ((vr == vm && (!accept_bounds || !vm_is_trailing_zeros)) || last_removed_digit >= 5) as u64 } else { // Specialized for the common case (~99.3%). Percentages below are relative to this. let mut round_up = false; let vp_div100 = div100(vp); let vm_div100 = div100(vm); // Optimization: remove two digits at a time (~86.2%). if vp_div100 > vm_div100 { let vr_div100 = div100(vr); let vr_mod100 = (vr as u32).wrapping_sub(100u32.wrapping_mul(vr_div100 as u32)); round_up = vr_mod100 >= 50; vr = vr_div100; vp = vp_div100; vm = vm_div100; removed += 2; } // Loop iterations below (approximately), without optimization above: // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02% // Loop iterations below (approximately), with optimization above: // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02% loop { let vp_div10 = div10(vp); let vm_div10 = div10(vm); if vp_div10 <= vm_div10 { break; } let vr_div10 = div10(vr); let vr_mod10 = (vr as u32).wrapping_sub(10u32.wrapping_mul(vr_div10 as u32)); round_up = vr_mod10 >= 5; vr = vr_div10; vp = vp_div10; vm = vm_div10; removed += 1; } // We need to take vr + 1 if vr is outside bounds or we need to round up. vr + (vr == vm || round_up) as u64 }; let exp = e10 + removed; FloatingDecimal64 { exponent: exp, mantissa: output, } } ryu-js-1.0.1/src/d2s_full_table.rs000064400000000000000000001001661046102023000150760ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. const DOUBLE_POW5_INV_TABLE_SIZE: usize = 342; const DOUBLE_POW5_TABLE_SIZE: usize = 326; pub static DOUBLE_POW5_INV_SPLIT: [(u64, u64); DOUBLE_POW5_INV_TABLE_SIZE] = [ (1, 2305843009213693952), (11068046444225730970, 1844674407370955161), (5165088340638674453, 1475739525896764129), (7821419487252849886, 1180591620717411303), (8824922364862649494, 1888946593147858085), (7059937891890119595, 1511157274518286468), (13026647942995916322, 1208925819614629174), (9774590264567735146, 1934281311383406679), (11509021026396098440, 1547425049106725343), (16585914450600699399, 1237940039285380274), (15469416676735388068, 1980704062856608439), (16064882156130220778, 1584563250285286751), (9162556910162266299, 1267650600228229401), (7281393426775805432, 2028240960365167042), (16893161185646375315, 1622592768292133633), (2446482504291369283, 1298074214633706907), (7603720821608101175, 2076918743413931051), (2393627842544570617, 1661534994731144841), (16672297533003297786, 1329227995784915872), (11918280793837635165, 2126764793255865396), (5845275820328197809, 1701411834604692317), (15744267100488289217, 1361129467683753853), (3054734472329800808, 2177807148294006166), (17201182836831481939, 1742245718635204932), (6382248639981364905, 1393796574908163946), (2832900194486363201, 2230074519853062314), (5955668970331000884, 1784059615882449851), (1075186361522890384, 1427247692705959881), (12788344622662355584, 2283596308329535809), (13920024512871794791, 1826877046663628647), (3757321980813615186, 1461501637330902918), (10384555214134712795, 1169201309864722334), (5547241898389809503, 1870722095783555735), (4437793518711847602, 1496577676626844588), (10928932444453298728, 1197262141301475670), (17486291911125277965, 1915619426082361072), (6610335899416401726, 1532495540865888858), (12666966349016942027, 1225996432692711086), (12888448528943286597, 1961594292308337738), (17689456452638449924, 1569275433846670190), (14151565162110759939, 1255420347077336152), (7885109000409574610, 2008672555323737844), (9997436015069570011, 1606938044258990275), (7997948812055656009, 1285550435407192220), (12796718099289049614, 2056880696651507552), (2858676849947419045, 1645504557321206042), (13354987924183666206, 1316403645856964833), (17678631863951955605, 2106245833371143733), (3074859046935833515, 1684996666696914987), (13527933681774397782, 1347997333357531989), (10576647446613305481, 2156795733372051183), (15840015586774465031, 1725436586697640946), (8982663654677661702, 1380349269358112757), (18061610662226169046, 2208558830972980411), (10759939715039024913, 1766847064778384329), (12297300586773130254, 1413477651822707463), (15986332124095098083, 2261564242916331941), (9099716884534168143, 1809251394333065553), (14658471137111155161, 1447401115466452442), (4348079280205103483, 1157920892373161954), (14335624477811986218, 1852673427797059126), (7779150767507678651, 1482138742237647301), (2533971799264232598, 1185710993790117841), (15122401323048503126, 1897137590064188545), (12097921058438802501, 1517710072051350836), (5988988032009131678, 1214168057641080669), (16961078480698431330, 1942668892225729070), (13568862784558745064, 1554135113780583256), (7165741412905085728, 1243308091024466605), (11465186260648137165, 1989292945639146568), (16550846638002330379, 1591434356511317254), (16930026125143774626, 1273147485209053803), (4951948911778577463, 2037035976334486086), (272210314680951647, 1629628781067588869), (3907117066486671641, 1303703024854071095), (6251387306378674625, 2085924839766513752), (16069156289328670670, 1668739871813211001), (9165976216721026213, 1334991897450568801), (7286864317269821294, 2135987035920910082), (16897537898041588005, 1708789628736728065), (13518030318433270404, 1367031702989382452), (6871453250525591353, 2187250724783011924), (9186511415162383406, 1749800579826409539), (11038557946871817048, 1399840463861127631), (10282995085511086630, 2239744742177804210), (8226396068408869304, 1791795793742243368), (13959814484210916090, 1433436634993794694), (11267656730511734774, 2293498615990071511), (5324776569667477496, 1834798892792057209), (7949170070475892320, 1467839114233645767), (17427382500606444826, 1174271291386916613), (5747719112518849781, 1878834066219066582), (15666221734240810795, 1503067252975253265), (12532977387392648636, 1202453802380202612), (5295368560860596524, 1923926083808324180), (4236294848688477220, 1539140867046659344), (7078384693692692099, 1231312693637327475), (11325415509908307358, 1970100309819723960), (9060332407926645887, 1576080247855779168), (14626963555825137356, 1260864198284623334), (12335095245094488799, 2017382717255397335), (9868076196075591040, 1613906173804317868), (15273158586344293478, 1291124939043454294), (13369007293925138595, 2065799902469526871), (7005857020398200553, 1652639921975621497), (16672732060544291412, 1322111937580497197), (11918976037903224966, 2115379100128795516), (5845832015580669650, 1692303280103036413), (12055363241948356366, 1353842624082429130), (841837113407818570, 2166148198531886609), (4362818505468165179, 1732918558825509287), (14558301248600263113, 1386334847060407429), (12225235553534690011, 2218135755296651887), (2401490813343931363, 1774508604237321510), (1921192650675145090, 1419606883389857208), (17831303500047873437, 2271371013423771532), (6886345170554478103, 1817096810739017226), (1819727321701672159, 1453677448591213781), (16213177116328979020, 1162941958872971024), (14873036941900635463, 1860707134196753639), (15587778368262418694, 1488565707357402911), (8780873879868024632, 1190852565885922329), (2981351763563108441, 1905364105417475727), (13453127855076217722, 1524291284333980581), (7073153469319063855, 1219433027467184465), (11317045550910502167, 1951092843947495144), (12742985255470312057, 1560874275157996115), (10194388204376249646, 1248699420126396892), (1553625868034358140, 1997919072202235028), (8621598323911307159, 1598335257761788022), (17965325103354776697, 1278668206209430417), (13987124906400001422, 2045869129935088668), (121653480894270168, 1636695303948070935), (97322784715416134, 1309356243158456748), (14913111714512307107, 2094969989053530796), (8241140556867935363, 1675975991242824637), (17660958889720079260, 1340780792994259709), (17189487779326395846, 2145249268790815535), (13751590223461116677, 1716199415032652428), (18379969808252713988, 1372959532026121942), (14650556434236701088, 2196735251241795108), (652398703163629901, 1757388200993436087), (11589965406756634890, 1405910560794748869), (7475898206584884855, 2249456897271598191), (2291369750525997561, 1799565517817278553), (9211793429904618695, 1439652414253822842), (18428218302589300235, 2303443862806116547), (7363877012587619542, 1842755090244893238), (13269799239553916280, 1474204072195914590), (10615839391643133024, 1179363257756731672), (2227947767661371545, 1886981212410770676), (16539753473096738529, 1509584969928616540), (13231802778477390823, 1207667975942893232), (6413489186596184024, 1932268761508629172), (16198837793502678189, 1545815009206903337), (5580372605318321905, 1236652007365522670), (8928596168509315048, 1978643211784836272), (18210923379033183008, 1582914569427869017), (7190041073742725760, 1266331655542295214), (436019273762630246, 2026130648867672343), (7727513048493924843, 1620904519094137874), (9871359253537050198, 1296723615275310299), (4726128361433549347, 2074757784440496479), (7470251503888749801, 1659806227552397183), (13354898832594820487, 1327844982041917746), (13989140502667892133, 2124551971267068394), (14880661216876224029, 1699641577013654715), (11904528973500979224, 1359713261610923772), (4289851098633925465, 2175541218577478036), (18189276137874781665, 1740432974861982428), (3483374466074094362, 1392346379889585943), (1884050330976640656, 2227754207823337509), (5196589079523222848, 1782203366258670007), (15225317707844309248, 1425762693006936005), (5913764258841343181, 2281220308811097609), (8420360221814984868, 1824976247048878087), (17804334621677718864, 1459980997639102469), (17932816512084085415, 1167984798111281975), (10245762345624985047, 1868775676978051161), (4507261061758077715, 1495020541582440929), (7295157664148372495, 1196016433265952743), (7982903447895485668, 1913626293225524389), (10075671573058298858, 1530901034580419511), (4371188443704728763, 1224720827664335609), (14372599139411386667, 1959553324262936974), (15187428126271019657, 1567642659410349579), (15839291315758726049, 1254114127528279663), (3206773216762499739, 2006582604045247462), (13633465017635730761, 1605266083236197969), (14596120828850494932, 1284212866588958375), (4907049252451240275, 2054740586542333401), (236290587219081897, 1643792469233866721), (14946427728742906810, 1315033975387093376), (16535586736504830250, 2104054360619349402), (5849771759720043554, 1683243488495479522), (15747863852001765813, 1346594790796383617), (10439186904235184007, 2154551665274213788), (15730047152871967852, 1723641332219371030), (12584037722297574282, 1378913065775496824), (9066413911450387881, 2206260905240794919), (10942479943902220628, 1765008724192635935), (8753983955121776503, 1412006979354108748), (10317025513452932081, 2259211166966573997), (874922781278525018, 1807368933573259198), (8078635854506640661, 1445895146858607358), (13841606313089133175, 1156716117486885886), (14767872471458792434, 1850745787979017418), (746251532941302978, 1480596630383213935), (597001226353042382, 1184477304306571148), (15712597221132509104, 1895163686890513836), (8880728962164096960, 1516130949512411069), (10793931984473187891, 1212904759609928855), (17270291175157100626, 1940647615375886168), (2748186495899949531, 1552518092300708935), (2198549196719959625, 1242014473840567148), (18275073973719576693, 1987223158144907436), (10930710364233751031, 1589778526515925949), (12433917106128911148, 1271822821212740759), (8826220925580526867, 2034916513940385215), (7060976740464421494, 1627933211152308172), (16716827836597268165, 1302346568921846537), (11989529279587987770, 2083754510274954460), (9591623423670390216, 1667003608219963568), (15051996368420132820, 1333602886575970854), (13015147745246481542, 2133764618521553367), (3033420566713364587, 1707011694817242694), (6116085268112601993, 1365609355853794155), (9785736428980163188, 2184974969366070648), (15207286772667951197, 1747979975492856518), (1097782973908629988, 1398383980394285215), (1756452758253807981, 2237414368630856344), (5094511021344956708, 1789931494904685075), (4075608817075965366, 1431945195923748060), (6520974107321544586, 2291112313477996896), (1527430471115325346, 1832889850782397517), (12289990821117991246, 1466311880625918013), (17210690286378213644, 1173049504500734410), (9090360384495590213, 1876879207201175057), (18340334751822203140, 1501503365760940045), (14672267801457762512, 1201202692608752036), (16096930852848599373, 1921924308174003258), (1809498238053148529, 1537539446539202607), (12515645034668249793, 1230031557231362085), (1578287981759648052, 1968050491570179337), (12330676829633449412, 1574440393256143469), (13553890278448669853, 1259552314604914775), (3239480371808320148, 2015283703367863641), (17348979556414297411, 1612226962694290912), (6500486015647617283, 1289781570155432730), (10400777625036187652, 2063650512248692368), (15699319729512770768, 1650920409798953894), (16248804598352126938, 1320736327839163115), (7551343283653851484, 2113178124542660985), (6041074626923081187, 1690542499634128788), (12211557331022285596, 1352433999707303030), (1091747655926105338, 2163894399531684849), (4562746939482794594, 1731115519625347879), (7339546366328145998, 1384892415700278303), (8053925371383123274, 2215827865120445285), (6443140297106498619, 1772662292096356228), (12533209867169019542, 1418129833677084982), (5295740528502789974, 2269007733883335972), (15304638867027962949, 1815206187106668777), (4865013464138549713, 1452164949685335022), (14960057215536570740, 1161731959748268017), (9178696285890871890, 1858771135597228828), (14721654658196518159, 1487016908477783062), (4398626097073393881, 1189613526782226450), (7037801755317430209, 1903381642851562320), (5630241404253944167, 1522705314281249856), (814844308661245011, 1218164251424999885), (1303750893857992017, 1949062802279999816), (15800395974054034906, 1559250241823999852), (5261619149759407279, 1247400193459199882), (12107939454356961969, 1995840309534719811), (5997002748743659252, 1596672247627775849), (8486951013736837725, 1277337798102220679), (2511075177753209390, 2043740476963553087), (13076906586428298482, 1634992381570842469), (14150874083884549109, 1307993905256673975), (4194654460505726958, 2092790248410678361), (18113118827372222859, 1674232198728542688), (3422448617672047318, 1339385758982834151), (16543964232501006678, 2143017214372534641), (9545822571258895019, 1714413771498027713), (15015355686490936662, 1371531017198422170), (5577825024675947042, 2194449627517475473), (11840957649224578280, 1755559702013980378), (16851463748863483271, 1404447761611184302), (12204946739213931940, 2247116418577894884), (13453306206113055875, 1797693134862315907), (3383947335406624054, 1438154507889852726), (16482362180876329456, 2301047212623764361), (9496540929959153242, 1840837770099011489), (11286581558709232917, 1472670216079209191), (5339916432225476010, 1178136172863367353), (4854517476818851293, 1885017876581387765), (3883613981455081034, 1508014301265110212), (14174937629389795797, 1206411441012088169), (11611853762797942306, 1930258305619341071), (5600134195496443521, 1544206644495472857), (15548153800622885787, 1235365315596378285), (6430302007287065643, 1976584504954205257), (16212288050055383484, 1581267603963364205), (12969830440044306787, 1265014083170691364), (9683682259845159889, 2024022533073106183), (15125643437359948558, 1619218026458484946), (8411165935146048523, 1295374421166787957), (17147214310975587960, 2072599073866860731), (10028422634038560045, 1658079259093488585), (8022738107230848036, 1326463407274790868), (9147032156827446534, 2122341451639665389), (11006974540203867551, 1697873161311732311), (5116230817421183718, 1358298529049385849), (15564666937357714594, 2173277646479017358), (1383687105660440706, 1738622117183213887), (12174996128754083534, 1390897693746571109), (8411947361780802685, 2225436309994513775), (6729557889424642148, 1780349047995611020), (5383646311539713719, 1424279238396488816), (1235136468979721303, 2278846781434382106), (15745504434151418335, 1823077425147505684), (16285752362063044992, 1458461940118004547), (5649904260166615347, 1166769552094403638), (5350498001524674232, 1866831283351045821), (591049586477829062, 1493465026680836657), (11540886113407994219, 1194772021344669325), (18673707743239135, 1911635234151470921), (14772334225162232601, 1529308187321176736), (8128518565387875758, 1223446549856941389), (1937583260394870242, 1957514479771106223), (8928764237799716840, 1566011583816884978), (14521709019723594119, 1252809267053507982), (8477339172590109297, 2004494827285612772), (17849917782297818407, 1603595861828490217), (6901236596354434079, 1282876689462792174), (18420676183650915173, 2052602703140467478), (3668494502695001169, 1642082162512373983), (10313493231639821582, 1313665730009899186), (9122891541139893884, 2101865168015838698), (14677010862395735754, 1681492134412670958), (673562245690857633, 1345193707530136767), ]; pub static DOUBLE_POW5_SPLIT: [(u64, u64); DOUBLE_POW5_TABLE_SIZE] = [ (0, 1152921504606846976), (0, 1441151880758558720), (0, 1801439850948198400), (0, 2251799813685248000), (0, 1407374883553280000), (0, 1759218604441600000), (0, 2199023255552000000), (0, 1374389534720000000), (0, 1717986918400000000), (0, 2147483648000000000), (0, 1342177280000000000), (0, 1677721600000000000), (0, 2097152000000000000), (0, 1310720000000000000), (0, 1638400000000000000), (0, 2048000000000000000), (0, 1280000000000000000), (0, 1600000000000000000), (0, 2000000000000000000), (0, 1250000000000000000), (0, 1562500000000000000), (0, 1953125000000000000), (0, 1220703125000000000), (0, 1525878906250000000), (0, 1907348632812500000), (0, 1192092895507812500), (0, 1490116119384765625), (4611686018427387904, 1862645149230957031), (9799832789158199296, 1164153218269348144), (12249790986447749120, 1455191522836685180), (15312238733059686400, 1818989403545856475), (14528612397897220096, 2273736754432320594), (13692068767113150464, 1421085471520200371), (12503399940464050176, 1776356839400250464), (15629249925580062720, 2220446049250313080), (9768281203487539200, 1387778780781445675), (7598665485932036096, 1734723475976807094), (274959820560269312, 2168404344971008868), (9395221924704944128, 1355252715606880542), (2520655369026404352, 1694065894508600678), (12374191248137781248, 2117582368135750847), (14651398557727195136, 1323488980084844279), (13702562178731606016, 1654361225106055349), (3293144668132343808, 2067951531382569187), (18199116482078572544, 1292469707114105741), (8913837547316051968, 1615587133892632177), (15753982952572452864, 2019483917365790221), (12152082354571476992, 1262177448353618888), (15190102943214346240, 1577721810442023610), (9764256642163156992, 1972152263052529513), (17631875447420442880, 1232595164407830945), (8204786253993389888, 1540743955509788682), (1032610780636961552, 1925929944387235853), (2951224747111794922, 1203706215242022408), (3689030933889743652, 1504632769052528010), (13834660704216955373, 1880790961315660012), (17870034976990372916, 1175494350822287507), (17725857702810578241, 1469367938527859384), (3710578054803671186, 1836709923159824231), (26536550077201078, 2295887403949780289), (11545800389866720434, 1434929627468612680), (14432250487333400542, 1793662034335765850), (8816941072311974870, 2242077542919707313), (17039803216263454053, 1401298464324817070), (12076381983474541759, 1751623080406021338), (5872105442488401391, 2189528850507526673), (15199280947623720629, 1368455531567204170), (9775729147674874978, 1710569414459005213), (16831347453020981627, 2138211768073756516), (1296220121283337709, 1336382355046097823), (15455333206886335848, 1670477943807622278), (10095794471753144002, 2088097429759527848), (6309871544845715001, 1305060893599704905), (12499025449484531656, 1631326116999631131), (11012095793428276666, 2039157646249538914), (11494245889320060820, 1274473528905961821), (532749306367912313, 1593091911132452277), (5277622651387278295, 1991364888915565346), (7910200175544436838, 1244603055572228341), (14499436237857933952, 1555753819465285426), (8900923260467641632, 1944692274331606783), (12480606065433357876, 1215432671457254239), (10989071563364309441, 1519290839321567799), (9124653435777998898, 1899113549151959749), (8008751406574943263, 1186945968219974843), (5399253239791291175, 1483682460274968554), (15972438586593889776, 1854603075343710692), (759402079766405302, 1159126922089819183), (14784310654990170340, 1448908652612273978), (9257016281882937117, 1811135815765342473), (16182956370781059300, 2263919769706678091), (7808504722524468110, 1414949856066673807), (5148944884728197234, 1768687320083342259), (1824495087482858639, 2210859150104177824), (1140309429676786649, 1381786968815111140), (1425386787095983311, 1727233711018888925), (6393419502297367043, 2159042138773611156), (13219259225790630210, 1349401336733506972), (16524074032238287762, 1686751670916883715), (16043406521870471799, 2108439588646104644), (803757039314269066, 1317774742903815403), (14839754354425000045, 1647218428629769253), (4714634887749086344, 2059023035787211567), (9864175832484260821, 1286889397367007229), (16941905809032713930, 1608611746708759036), (2730638187581340797, 2010764683385948796), (10930020904093113806, 1256727927116217997), (18274212148543780162, 1570909908895272496), (4396021111970173586, 1963637386119090621), (5053356204195052443, 1227273366324431638), (15540067292098591362, 1534091707905539547), (14813398096695851299, 1917614634881924434), (13870059828862294966, 1198509146801202771), (12725888767650480803, 1498136433501503464), (15907360959563101004, 1872670541876879330), (14553786618154326031, 1170419088673049581), (4357175217410743827, 1463023860841311977), (10058155040190817688, 1828779826051639971), (7961007781811134206, 2285974782564549964), (14199001900486734687, 1428734239102843727), (13137066357181030455, 1785917798878554659), (11809646928048900164, 2232397248598193324), (16604401366885338411, 1395248280373870827), (16143815690179285109, 1744060350467338534), (10956397575869330579, 2180075438084173168), (6847748484918331612, 1362547148802608230), (17783057643002690323, 1703183936003260287), (17617136035325974999, 2128979920004075359), (17928239049719816230, 1330612450002547099), (17798612793722382384, 1663265562503183874), (13024893955298202172, 2079081953128979843), (5834715712847682405, 1299426220705612402), (16516766677914378815, 1624282775882015502), (11422586310538197711, 2030353469852519378), (11750802462513761473, 1268970918657824611), (10076817059714813937, 1586213648322280764), (12596021324643517422, 1982767060402850955), (5566670318688504437, 1239229412751781847), (2346651879933242642, 1549036765939727309), (7545000868343941206, 1936295957424659136), (4715625542714963254, 1210184973390411960), (5894531928393704067, 1512731216738014950), (16591536947346905892, 1890914020922518687), (17287239619732898039, 1181821263076574179), (16997363506238734644, 1477276578845717724), (2799960309088866689, 1846595723557147156), (10973347230035317489, 1154122327223216972), (13716684037544146861, 1442652909029021215), (12534169028502795672, 1803316136286276519), (11056025267201106687, 2254145170357845649), (18439230838069161439, 1408840731473653530), (13825666510731675991, 1761050914342066913), (3447025083132431277, 2201313642927583642), (6766076695385157452, 1375821026829739776), (8457595869231446815, 1719776283537174720), (10571994836539308519, 2149720354421468400), (6607496772837067824, 1343575221513417750), (17482743002901110588, 1679469026891772187), (17241742735199000331, 2099336283614715234), (15387775227926763111, 1312085177259197021), (5399660979626290177, 1640106471573996277), (11361262242960250625, 2050133089467495346), (11712474920277544544, 1281333180917184591), (10028907631919542777, 1601666476146480739), (7924448521472040567, 2002083095183100924), (14176152362774801162, 1251301934489438077), (3885132398186337741, 1564127418111797597), (9468101516160310080, 1955159272639746996), (15140935484454969608, 1221974545399841872), (479425281859160394, 1527468181749802341), (5210967620751338397, 1909335227187252926), (17091912818251750210, 1193334516992033078), (12141518985959911954, 1491668146240041348), (15176898732449889943, 1864585182800051685), (11791404716994875166, 1165365739250032303), (10127569877816206054, 1456707174062540379), (8047776328842869663, 1820883967578175474), (836348374198811271, 2276104959472719343), (7440246761515338900, 1422565599670449589), (13911994470321561530, 1778206999588061986), (8166621051047176104, 2222758749485077483), (2798295147690791113, 1389224218428173427), (17332926989895652603, 1736530273035216783), (17054472718942177850, 2170662841294020979), (8353202440125167204, 1356664275808763112), (10441503050156459005, 1695830344760953890), (3828506775840797949, 2119787930951192363), (86973725686804766, 1324867456844495227), (13943775212390669669, 1656084321055619033), (3594660960206173375, 2070105401319523792), (2246663100128858359, 1293815875824702370), (12031700912015848757, 1617269844780877962), (5816254103165035138, 2021587305976097453), (5941001823691840913, 1263492066235060908), (7426252279614801142, 1579365082793826135), (4671129331091113523, 1974206353492282669), (5225298841145639904, 1233878970932676668), (6531623551432049880, 1542348713665845835), (3552843420862674446, 1927935892082307294), (16055585193321335241, 1204959932551442058), (10846109454796893243, 1506199915689302573), (18169322836923504458, 1882749894611628216), (11355826773077190286, 1176718684132267635), (9583097447919099954, 1470898355165334544), (11978871809898874942, 1838622943956668180), (14973589762373593678, 2298278679945835225), (2440964573842414192, 1436424174966147016), (3051205717303017741, 1795530218707683770), (13037379183483547984, 2244412773384604712), (8148361989677217490, 1402757983365377945), (14797138505523909766, 1753447479206722431), (13884737113477499304, 2191809349008403039), (15595489723564518921, 1369880843130251899), (14882676136028260747, 1712351053912814874), (9379973133180550126, 2140438817391018593), (17391698254306313589, 1337774260869386620), (3292878744173340370, 1672217826086733276), (4116098430216675462, 2090272282608416595), (266718509671728212, 1306420176630260372), (333398137089660265, 1633025220787825465), (5028433689789463235, 2041281525984781831), (10060300083759496378, 1275800953740488644), (12575375104699370472, 1594751192175610805), (1884160825592049379, 1993438990219513507), (17318501580490888525, 1245899368887195941), (7813068920331446945, 1557374211108994927), (5154650131986920777, 1946717763886243659), (915813323278131534, 1216698602428902287), (14979824709379828129, 1520873253036127858), (9501408849870009354, 1901091566295159823), (12855909558809837702, 1188182228934474889), (2234828893230133415, 1485227786168093612), (2793536116537666769, 1856534732710117015), (8663489100477123587, 1160334207943823134), (1605989338741628675, 1450417759929778918), (11230858710281811652, 1813022199912223647), (9426887369424876662, 2266277749890279559), (12809333633531629769, 1416423593681424724), (16011667041914537212, 1770529492101780905), (6179525747111007803, 2213161865127226132), (13085575628799155685, 1383226165704516332), (16356969535998944606, 1729032707130645415), (15834525901571292854, 2161290883913306769), (2979049660840976177, 1350806802445816731), (17558870131333383934, 1688508503057270913), (8113529608884566205, 2110635628821588642), (9682642023980241782, 1319147268013492901), (16714988548402690132, 1648934085016866126), (11670363648648586857, 2061167606271082658), (11905663298832754689, 1288229753919426661), (1047021068258779650, 1610287192399283327), (15143834390605638274, 2012858990499104158), (4853210475701136017, 1258036869061940099), (1454827076199032118, 1572546086327425124), (1818533845248790147, 1965682607909281405), (3442426662494187794, 1228551629943300878), (13526405364972510550, 1535689537429126097), (3072948650933474476, 1919611921786407622), (15755650962115585259, 1199757451116504763), (15082877684217093670, 1499696813895630954), (9630225068416591280, 1874621017369538693), (8324733676974063502, 1171638135855961683), (5794231077790191473, 1464547669819952104), (7242788847237739342, 1830684587274940130), (18276858095901949986, 2288355734093675162), (16034722328366106645, 1430222333808546976), (1596658836748081690, 1787777917260683721), (6607509564362490017, 2234722396575854651), (1823850468512862308, 1396701497859909157), (6891499104068465790, 1745876872324886446), (17837745916940358045, 2182346090406108057), (4231062170446641922, 1363966306503817536), (5288827713058302403, 1704957883129771920), (6611034641322878003, 2131197353912214900), (13355268687681574560, 1331998346195134312), (16694085859601968200, 1664997932743917890), (11644235287647684442, 2081247415929897363), (4971804045566108824, 1300779634956185852), (6214755056957636030, 1625974543695232315), (3156757802769657134, 2032468179619040394), (6584659645158423613, 1270292612261900246), (17454196593302805324, 1587865765327375307), (17206059723201118751, 1984832206659219134), (6142101308573311315, 1240520129162011959), (3065940617289251240, 1550650161452514949), (8444111790038951954, 1938312701815643686), (665883850346957067, 1211445438634777304), (832354812933696334, 1514306798293471630), (10263815553021896226, 1892883497866839537), (17944099766707154901, 1183052186166774710), (13206752671529167818, 1478815232708468388), (16508440839411459773, 1848519040885585485), (12623618533845856310, 1155324400553490928), (15779523167307320387, 1444155500691863660), (1277659885424598868, 1805194375864829576), (1597074856780748586, 2256492969831036970), (5609857803915355770, 1410308106144398106), (16235694291748970521, 1762885132680497632), (1847873790976661535, 2203606415850622041), (12684136165428883219, 1377254009906638775), (11243484188358716120, 1721567512383298469), (219297180166231438, 2151959390479123087), (7054589765244976505, 1344974619049451929), (13429923224983608535, 1681218273811814911), (12175718012802122765, 2101522842264768639), (14527352785642408584, 1313451776415480399), (13547504963625622826, 1641814720519350499), (12322695186104640628, 2052268400649188124), (16925056528170176201, 1282667750405742577), (7321262604930556539, 1603334688007178222), (18374950293017971482, 2004168360008972777), (4566814905495150320, 1252605225005607986), (14931890668723713708, 1565756531257009982), (9441491299049866327, 1957195664071262478), (1289246043478778550, 1223247290044539049), (6223243572775861092, 1529059112555673811), (3167368447542438461, 1911323890694592264), (1979605279714024038, 1194577431684120165), (7086192618069917952, 1493221789605150206), (18081112809442173248, 1866527237006437757), (13606538515115052232, 1166579523129023598), (7784801107039039482, 1458224403911279498), (507629346944023544, 1822780504889099373), (5246222702107417334, 2278475631111374216), (3278889188817135834, 1424047269444608885), (8710297504448807696, 1780059086805761106), ]; ryu-js-1.0.1/src/d2s_intrinsics.rs000064400000000000000000000051611046102023000151510ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. use core::ptr; #[cfg_attr(feature = "no-panic", inline)] pub fn div5(x: u64) -> u64 { x / 5 } #[cfg_attr(feature = "no-panic", inline)] pub fn div10(x: u64) -> u64 { x / 10 } #[cfg_attr(feature = "no-panic", inline)] pub fn div100(x: u64) -> u64 { x / 100 } #[cfg_attr(feature = "no-panic", inline)] pub(crate) fn pow5_factor(mut value: u64) -> u32 { const M_INV_5: u64 = 14757395258967641293; // 5 * m_inv_5 = 1 (mod 2^64) const N_DIV_5: u64 = 3689348814741910323; // #{ n | n = 0 (mod 2^64) } = 2^64 / 5 let mut count = 0u32; loop { debug_assert!(value != 0); value = value.wrapping_mul(M_INV_5); if value > N_DIV_5 { break; } count += 1; } count } // Returns true if value is divisible by 5^p. #[cfg_attr(feature = "no-panic", inline)] pub fn multiple_of_power_of_5(value: u64, p: u32) -> bool { // I tried a case distinction on p, but there was no performance difference. pow5_factor(value) >= p } // Returns true if value is divisible by 2^p. #[cfg_attr(feature = "no-panic", inline)] pub fn multiple_of_power_of_2(value: u64, p: u32) -> bool { debug_assert!(value != 0); debug_assert!(p < 64); // __builtin_ctzll doesn't appear to be faster here. (value & ((1u64 << p) - 1)) == 0 } #[cfg_attr(feature = "no-panic", inline)] pub fn mul_shift_64(m: u64, mul: &(u64, u64), j: u32) -> u64 { let b0 = m as u128 * mul.0 as u128; let b2 = m as u128 * mul.1 as u128; (((b0 >> 64) + b2) >> (j - 64)) as u64 } #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn mul_shift_all_64( m: u64, mul: &(u64, u64), j: u32, vp: *mut u64, vm: *mut u64, mm_shift: u32, ) -> u64 { ptr::write(vp, mul_shift_64(4 * m + 2, mul, j)); ptr::write(vm, mul_shift_64(4 * m - 1 - mm_shift as u64, mul, j)); mul_shift_64(4 * m, mul, j) } ryu-js-1.0.1/src/d2s_small_table.rs000064400000000000000000000124011046102023000152360ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. use crate::common::pow5bits; pub static DOUBLE_POW5_INV_SPLIT2: [(u64, u64); 15] = [ (1, 2305843009213693952), (5955668970331000884, 1784059615882449851), (8982663654677661702, 1380349269358112757), (7286864317269821294, 2135987035920910082), (7005857020398200553, 1652639921975621497), (17965325103354776697, 1278668206209430417), (8928596168509315048, 1978643211784836272), (10075671573058298858, 1530901034580419511), (597001226353042382, 1184477304306571148), (1527430471115325346, 1832889850782397517), (12533209867169019542, 1418129833677084982), (5577825024675947042, 2194449627517475473), (11006974540203867551, 1697873161311732311), (10313493231639821582, 1313665730009899186), (12701016819766672773, 2032799256770390445), ]; pub static POW5_INV_OFFSETS: [u32; 19] = [ 0x54544554, 0x04055545, 0x10041000, 0x00400414, 0x40010000, 0x41155555, 0x00000454, 0x00010044, 0x40000000, 0x44000041, 0x50454450, 0x55550054, 0x51655554, 0x40004000, 0x01000001, 0x00010500, 0x51515411, 0x05555554, 0x00000000, ]; pub static DOUBLE_POW5_SPLIT2: [(u64, u64); 13] = [ (0, 1152921504606846976), (0, 1490116119384765625), (1032610780636961552, 1925929944387235853), (7910200175544436838, 1244603055572228341), (16941905809032713930, 1608611746708759036), (13024893955298202172, 2079081953128979843), (6607496772837067824, 1343575221513417750), (17332926989895652603, 1736530273035216783), (13037379183483547984, 2244412773384604712), (1605989338741628675, 1450417759929778918), (9630225068416591280, 1874621017369538693), (665883850346957067, 1211445438634777304), (14931890668723713708, 1565756531257009982), ]; pub static POW5_OFFSETS: [u32; 21] = [ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x40000000, 0x59695995, 0x55545555, 0x56555515, 0x41150504, 0x40555410, 0x44555145, 0x44504540, 0x45555550, 0x40004000, 0x96440440, 0x55565565, 0x54454045, 0x40154151, 0x55559155, 0x51405555, 0x00000105, ]; pub static DOUBLE_POW5_TABLE: [u64; 26] = [ 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625, 1220703125, 6103515625, 30517578125, 152587890625, 762939453125, 3814697265625, 19073486328125, 95367431640625, 476837158203125, 2384185791015625, 11920928955078125, 59604644775390625, 298023223876953125, ]; // Computes 5^i in the form required by Ryū. #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn compute_pow5(i: u32) -> (u64, u64) { let base = i / DOUBLE_POW5_TABLE.len() as u32; let base2 = base * DOUBLE_POW5_TABLE.len() as u32; let offset = i - base2; debug_assert!(base < DOUBLE_POW5_SPLIT2.len() as u32); let mul = *DOUBLE_POW5_SPLIT2.get_unchecked(base as usize); if offset == 0 { return mul; } debug_assert!(offset < DOUBLE_POW5_TABLE.len() as u32); let m = *DOUBLE_POW5_TABLE.get_unchecked(offset as usize); let b0 = m as u128 * mul.0 as u128; let b2 = m as u128 * mul.1 as u128; let delta = pow5bits(i as i32) - pow5bits(base2 as i32); debug_assert!(i / 16 < POW5_OFFSETS.len() as u32); let shifted_sum = (b0 >> delta) + (b2 << (64 - delta)) + ((*POW5_OFFSETS.get_unchecked((i / 16) as usize) >> ((i % 16) << 1)) & 3) as u128; (shifted_sum as u64, (shifted_sum >> 64) as u64) } // Computes 5^-i in the form required by Ryū. #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn compute_inv_pow5(i: u32) -> (u64, u64) { let base = (i + DOUBLE_POW5_TABLE.len() as u32 - 1) / DOUBLE_POW5_TABLE.len() as u32; let base2 = base * DOUBLE_POW5_TABLE.len() as u32; let offset = base2 - i; debug_assert!(base < DOUBLE_POW5_INV_SPLIT2.len() as u32); let mul = *DOUBLE_POW5_INV_SPLIT2.get_unchecked(base as usize); // 1/5^base2 if offset == 0 { return mul; } debug_assert!(offset < DOUBLE_POW5_TABLE.len() as u32); let m = *DOUBLE_POW5_TABLE.get_unchecked(offset as usize); // 5^offset let b0 = m as u128 * (mul.0 - 1) as u128; let b2 = m as u128 * mul.1 as u128; // 1/5^base2 * 5^offset = 1/5^(base2-offset) = 1/5^i let delta = pow5bits(base2 as i32) - pow5bits(i as i32); debug_assert!(base < POW5_INV_OFFSETS.len() as u32); let shifted_sum = ((b0 >> delta) + (b2 << (64 - delta))) + 1 + ((*POW5_INV_OFFSETS.get_unchecked((i / 16) as usize) >> ((i % 16) << 1)) & 3) as u128; (shifted_sum as u64, (shifted_sum >> 64) as u64) } ryu-js-1.0.1/src/digit_table.rs000064400000000000000000000022631046102023000144630ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. // A table of all two-digit numbers. This is used to speed up decimal digit // generation by copying pairs of digits into the final output. pub static DIGIT_TABLE: [u8; 200] = *b"\ 0001020304050607080910111213141516171819\ 2021222324252627282930313233343536373839\ 4041424344454647484950515253545556575859\ 6061626364656667686970717273747576777879\ 8081828384858687888990919293949596979899"; ryu-js-1.0.1/src/f2s.rs000064400000000000000000000156661046102023000127210ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. use crate::common::{log10_pow2, log10_pow5, pow5bits}; use crate::f2s_intrinsics::{ mul_pow5_div_pow2, mul_pow5_inv_div_pow2, multiple_of_power_of_2_32, multiple_of_power_of_5_32, }; pub const FLOAT_MANTISSA_BITS: u32 = 23; pub const FLOAT_EXPONENT_BITS: u32 = 8; const FLOAT_BIAS: i32 = 127; pub use crate::f2s_intrinsics::{FLOAT_POW5_BITCOUNT, FLOAT_POW5_INV_BITCOUNT}; // A floating decimal representing m * 10^e. pub struct FloatingDecimal32 { pub mantissa: u32, // Decimal exponent's range is -45 to 38 // inclusive, and can fit in i16 if needed. pub exponent: i32, } #[cfg_attr(feature = "no-panic", inline)] pub fn f2d(ieee_mantissa: u32, ieee_exponent: u32) -> FloatingDecimal32 { let (e2, m2) = if ieee_exponent == 0 { ( // We subtract 2 so that the bounds computation has 2 additional bits. 1 - FLOAT_BIAS - FLOAT_MANTISSA_BITS as i32 - 2, ieee_mantissa, ) } else { ( ieee_exponent as i32 - FLOAT_BIAS - FLOAT_MANTISSA_BITS as i32 - 2, (1u32 << FLOAT_MANTISSA_BITS) | ieee_mantissa, ) }; let even = (m2 & 1) == 0; let accept_bounds = even; // Step 2: Determine the interval of valid decimal representations. let mv = 4 * m2; let mp = 4 * m2 + 2; // Implicit bool -> int conversion. True is 1, false is 0. let mm_shift = (ieee_mantissa != 0 || ieee_exponent <= 1) as u32; let mm = 4 * m2 - 1 - mm_shift; // Step 3: Convert to a decimal power base using 64-bit arithmetic. let mut vr: u32; let mut vp: u32; let mut vm: u32; let e10: i32; let mut vm_is_trailing_zeros = false; let mut vr_is_trailing_zeros = false; let mut last_removed_digit = 0u8; if e2 >= 0 { let q = log10_pow2(e2); e10 = q as i32; let k = FLOAT_POW5_INV_BITCOUNT + pow5bits(q as i32) - 1; let i = -e2 + q as i32 + k; vr = mul_pow5_inv_div_pow2(mv, q, i); vp = mul_pow5_inv_div_pow2(mp, q, i); vm = mul_pow5_inv_div_pow2(mm, q, i); if q != 0 && (vp - 1) / 10 <= vm / 10 { // We need to know one removed digit even if we are not going to loop below. We could use // q = X - 1 above, except that would require 33 bits for the result, and we've found that // 32-bit arithmetic is faster even on 64-bit machines. let l = FLOAT_POW5_INV_BITCOUNT + pow5bits(q as i32 - 1) - 1; last_removed_digit = (mul_pow5_inv_div_pow2(mv, q - 1, -e2 + q as i32 - 1 + l) % 10) as u8; } if q <= 9 { // The largest power of 5 that fits in 24 bits is 5^10, but q <= 9 seems to be safe as well. // Only one of mp, mv, and mm can be a multiple of 5, if any. if mv % 5 == 0 { vr_is_trailing_zeros = multiple_of_power_of_5_32(mv, q); } else if accept_bounds { vm_is_trailing_zeros = multiple_of_power_of_5_32(mm, q); } else { vp -= multiple_of_power_of_5_32(mp, q) as u32; } } } else { let q = log10_pow5(-e2); e10 = q as i32 + e2; let i = -e2 - q as i32; let k = pow5bits(i) - FLOAT_POW5_BITCOUNT; let mut j = q as i32 - k; vr = mul_pow5_div_pow2(mv, i as u32, j); vp = mul_pow5_div_pow2(mp, i as u32, j); vm = mul_pow5_div_pow2(mm, i as u32, j); if q != 0 && (vp - 1) / 10 <= vm / 10 { j = q as i32 - 1 - (pow5bits(i + 1) - FLOAT_POW5_BITCOUNT); last_removed_digit = (mul_pow5_div_pow2(mv, (i + 1) as u32, j) % 10) as u8; } if q <= 1 { // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits. // mv = 4 * m2, so it always has at least two trailing 0 bits. vr_is_trailing_zeros = true; if accept_bounds { // mm = mv - 1 - mm_shift, so it has 1 trailing 0 bit iff mm_shift == 1. vm_is_trailing_zeros = mm_shift == 1; } else { // mp = mv + 2, so it always has at least one trailing 0 bit. vp -= 1; } } else if q < 31 { // TODO(ulfjack): Use a tighter bound here. vr_is_trailing_zeros = multiple_of_power_of_2_32(mv, q - 1); } } // Step 4: Find the shortest decimal representation in the interval of valid representations. let mut removed = 0i32; let output = if vm_is_trailing_zeros || vr_is_trailing_zeros { // General case, which happens rarely (~4.0%). while vp / 10 > vm / 10 { vm_is_trailing_zeros &= vm - (vm / 10) * 10 == 0; vr_is_trailing_zeros &= last_removed_digit == 0; last_removed_digit = (vr % 10) as u8; vr /= 10; vp /= 10; vm /= 10; removed += 1; } if vm_is_trailing_zeros { while vm % 10 == 0 { vr_is_trailing_zeros &= last_removed_digit == 0; last_removed_digit = (vr % 10) as u8; vr /= 10; vp /= 10; vm /= 10; removed += 1; } } if vr_is_trailing_zeros && last_removed_digit == 5 && vr % 2 == 0 { // Round even if the exact number is .....50..0. last_removed_digit = 4; } // We need to take vr + 1 if vr is outside bounds or we need to round up. vr + ((vr == vm && (!accept_bounds || !vm_is_trailing_zeros)) || last_removed_digit >= 5) as u32 } else { // Specialized for the common case (~96.0%). Percentages below are relative to this. // Loop iterations below (approximately): // 0: 13.6%, 1: 70.7%, 2: 14.1%, 3: 1.39%, 4: 0.14%, 5+: 0.01% while vp / 10 > vm / 10 { last_removed_digit = (vr % 10) as u8; vr /= 10; vp /= 10; vm /= 10; removed += 1; } // We need to take vr + 1 if vr is outside bounds or we need to round up. vr + (vr == vm || last_removed_digit >= 5) as u32 }; let exp = e10 + removed; FloatingDecimal32 { exponent: exp, mantissa: output, } } ryu-js-1.0.1/src/f2s_intrinsics.rs000064400000000000000000000071641046102023000151600ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. use crate::d2s; pub const FLOAT_POW5_INV_BITCOUNT: i32 = d2s::DOUBLE_POW5_INV_BITCOUNT - 64; pub const FLOAT_POW5_BITCOUNT: i32 = d2s::DOUBLE_POW5_BITCOUNT - 64; #[cfg_attr(feature = "no-panic", inline)] fn pow5factor_32(mut value: u32) -> u32 { let mut count = 0u32; loop { debug_assert!(value != 0); let q = value / 5; let r = value % 5; if r != 0 { break; } value = q; count += 1; } count } // Returns true if value is divisible by 5^p. #[cfg_attr(feature = "no-panic", inline)] pub fn multiple_of_power_of_5_32(value: u32, p: u32) -> bool { pow5factor_32(value) >= p } // Returns true if value is divisible by 2^p. #[cfg_attr(feature = "no-panic", inline)] pub fn multiple_of_power_of_2_32(value: u32, p: u32) -> bool { // __builtin_ctz doesn't appear to be faster here. (value & ((1u32 << p) - 1)) == 0 } // It seems to be slightly faster to avoid uint128_t here, although the // generated code for uint128_t looks slightly nicer. #[cfg_attr(feature = "no-panic", inline)] fn mul_shift_32(m: u32, factor: u64, shift: i32) -> u32 { debug_assert!(shift > 32); // The casts here help MSVC to avoid calls to the __allmul library // function. let factor_lo = factor as u32; let factor_hi = (factor >> 32) as u32; let bits0 = m as u64 * factor_lo as u64; let bits1 = m as u64 * factor_hi as u64; let sum = (bits0 >> 32) + bits1; let shifted_sum = sum >> (shift - 32); debug_assert!(shifted_sum <= u32::max_value() as u64); shifted_sum as u32 } #[cfg_attr(feature = "no-panic", inline)] pub fn mul_pow5_inv_div_pow2(m: u32, q: u32, j: i32) -> u32 { #[cfg(feature = "small")] { // The inverse multipliers are defined as [2^x / 5^y] + 1; the upper 64 // bits from the double lookup table are the correct bits for [2^x / // 5^y], so we have to add 1 here. Note that we rely on the fact that // the added 1 that's already stored in the table never overflows into // the upper 64 bits. let pow5 = unsafe { d2s::compute_inv_pow5(q) }; mul_shift_32(m, pow5.1 + 1, j) } #[cfg(not(feature = "small"))] { debug_assert!(q < d2s::DOUBLE_POW5_INV_SPLIT.len() as u32); unsafe { mul_shift_32( m, d2s::DOUBLE_POW5_INV_SPLIT.get_unchecked(q as usize).1 + 1, j, ) } } } #[cfg_attr(feature = "no-panic", inline)] pub fn mul_pow5_div_pow2(m: u32, i: u32, j: i32) -> u32 { #[cfg(feature = "small")] { let pow5 = unsafe { d2s::compute_pow5(i) }; mul_shift_32(m, pow5.1, j) } #[cfg(not(feature = "small"))] { debug_assert!(i < d2s::DOUBLE_POW5_SPLIT.len() as u32); unsafe { mul_shift_32(m, d2s::DOUBLE_POW5_SPLIT.get_unchecked(i as usize).1, j) } } } ryu-js-1.0.1/src/lib.rs000064400000000000000000000060171046102023000127630ustar 00000000000000//! ECMAScript compliant pure Rust implementation of Ryū, an algorithm to quickly //! convert floating point numbers to decimal strings. //! //! The PLDI'18 paper [*Ryū: fast float-to-string conversion*][paper] by Ulf //! Adams includes a complete correctness proof of the algorithm. The paper is //! available under the creative commons CC-BY-SA license. //! //! This Rust implementation is a line-by-line port of Ulf Adams' implementation //! in C, [https://github.com/ulfjack/ryu][upstream]. //! //! [paper]: https://dl.acm.org/citation.cfm?id=3192369 //! [upstream]: https://github.com/ulfjack/ryu //! //! # Example //! //! ``` //! let mut buffer = ryu_js::Buffer::new(); //! let printed = buffer.format(1.234); //! assert_eq!(printed, "1.234"); //! ``` //! //! ## Performance (lower is better) //! //! The benchmarks measure the average time to print a 32-bit float and average //! time to print a 64-bit float, where the inputs are distributed as uniform random //! bit patterns 32 and 64 bits wide. //! //! The upstream C code, the unsafe direct Rust port, and the safe pretty Rust API //! all perform the same, taking around 21 nanoseconds to format a 32-bit float and //! 31 nanoseconds to format a 64-bit float. //! //! There is also a Rust-specific benchmark comparing this implementation to the //! standard library which you can run with: //! //! ```console //! $ cargo bench //! ``` //! //! The benchmark shows Ryū approximately 2-5x faster than the standard library //! across a range of f32 and f64 inputs. Measurements are in nanoseconds per //! iteration; smaller is better. //! //! ## Formatting //! //! This library tends to produce more human-readable output than the standard //! library's to\_string, which never uses scientific notation. Here are two //! examples: //! //! - *ryu:* 1.23e40, *std:* 12300000000000000000000000000000000000000 //! - *ryu:* 1.23e-40, *std:* 0.000000000000000000000000000000000000000123 //! //! Both libraries print short decimals such as 0.0000123 without scientific //! notation. #![no_std] #![allow( clippy::cast_lossless, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss, clippy::checked_conversions, clippy::doc_markdown, clippy::expl_impl_clone_on_copy, clippy::if_not_else, clippy::many_single_char_names, clippy::missing_panics_doc, clippy::module_name_repetitions, clippy::must_use_candidate, clippy::needless_doctest_main, clippy::similar_names, clippy::too_many_lines, clippy::unreadable_literal, clippy::unseparated_literal_suffix, clippy::wildcard_imports )] mod buffer; mod common; mod d2s; #[cfg(not(feature = "small"))] mod d2s_full_table; mod d2s_intrinsics; #[cfg(feature = "small")] mod d2s_small_table; mod digit_table; mod f2s; mod f2s_intrinsics; mod pretty; pub use crate::buffer::{Buffer, Float, FloatToFixed}; /// Unsafe functions that mirror the API of the C implementation of Ryū. pub mod raw { pub use crate::pretty::format64_to_fixed; pub use crate::pretty::{format32, format64}; } ryu-js-1.0.1/src/parse.rs000064400000000000000000000007371046102023000133320ustar 00000000000000use core::fmt::{self, Display}; #[derive(Copy, Clone, Debug)] pub enum Error { InputTooShort, InputTooLong, MalformedInput, } impl Display for Error { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { let msg = match self { Error::InputTooShort => "input too short", Error::InputTooLong => "input too long", Error::MalformedInput => "malformed input", }; formatter.write_str(msg) } } ryu-js-1.0.1/src/pretty/exponent.rs000064400000000000000000000024441046102023000154040ustar 00000000000000use crate::digit_table::DIGIT_TABLE; use core::ptr; #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn write_exponent3(mut k: isize, mut result: *mut u8) -> usize { let sign = k < 0; if sign { *result = b'-'; result = result.offset(1); k = -k; } else { *result = b'+'; result = result.offset(1); } debug_assert!(k < 1000); if k >= 100 { *result = b'0' + (k / 100) as u8; k %= 100; let d = DIGIT_TABLE.as_ptr().offset(k * 2); ptr::copy_nonoverlapping(d, result.offset(1), 2); 4 } else if k >= 10 { let d = DIGIT_TABLE.as_ptr().offset(k * 2); ptr::copy_nonoverlapping(d, result, 2); 3 } else { *result = b'0' + k as u8; 2 } } #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn write_exponent2(mut k: isize, mut result: *mut u8) -> usize { let sign = k < 0; if sign { *result = b'-'; result = result.offset(1); k = -k; } else { *result = b'+'; result = result.offset(1); } debug_assert!(k < 100); if k >= 10 { let d = DIGIT_TABLE.as_ptr().offset(k * 2); ptr::copy_nonoverlapping(d, result, 2); 3 } else { *result = b'0' + k as u8; 2 } } ryu-js-1.0.1/src/pretty/mantissa.rs000064400000000000000000000045411046102023000153630ustar 00000000000000use crate::digit_table::DIGIT_TABLE; use core::ptr; #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn write_mantissa_long(mut output: u64, mut result: *mut u8) { if (output >> 32) != 0 { // One expensive 64-bit division. let mut output2 = (output - 100_000_000 * (output / 100_000_000)) as u32; output /= 100_000_000; let c = output2 % 10_000; output2 /= 10_000; let d = output2 % 10_000; let c0 = (c % 100) << 1; let c1 = (c / 100) << 1; let d0 = (d % 100) << 1; let d1 = (d / 100) << 1; ptr::copy_nonoverlapping( DIGIT_TABLE.as_ptr().offset(c0 as isize), result.offset(-2), 2, ); ptr::copy_nonoverlapping( DIGIT_TABLE.as_ptr().offset(c1 as isize), result.offset(-4), 2, ); ptr::copy_nonoverlapping( DIGIT_TABLE.as_ptr().offset(d0 as isize), result.offset(-6), 2, ); ptr::copy_nonoverlapping( DIGIT_TABLE.as_ptr().offset(d1 as isize), result.offset(-8), 2, ); result = result.offset(-8); } write_mantissa(output as u32, result); } #[cfg_attr(feature = "no-panic", inline)] pub unsafe fn write_mantissa(mut output: u32, mut result: *mut u8) { while output >= 10_000 { let c = output - 10_000 * (output / 10_000); output /= 10_000; let c0 = (c % 100) << 1; let c1 = (c / 100) << 1; ptr::copy_nonoverlapping( DIGIT_TABLE.as_ptr().offset(c0 as isize), result.offset(-2), 2, ); ptr::copy_nonoverlapping( DIGIT_TABLE.as_ptr().offset(c1 as isize), result.offset(-4), 2, ); result = result.offset(-4); } if output >= 100 { let c = (output % 100) << 1; output /= 100; ptr::copy_nonoverlapping( DIGIT_TABLE.as_ptr().offset(c as isize), result.offset(-2), 2, ); result = result.offset(-2); } if output >= 10 { let c = output << 1; ptr::copy_nonoverlapping( DIGIT_TABLE.as_ptr().offset(c as isize), result.offset(-2), 2, ); } else { *result.offset(-1) = b'0' + output as u8; } } ryu-js-1.0.1/src/pretty/mod.rs000064400000000000000000000174741046102023000143340ustar 00000000000000mod exponent; mod mantissa; use self::exponent::{write_exponent2, write_exponent3}; use self::mantissa::{write_mantissa, write_mantissa_long}; use crate::common; use crate::d2s::{self, d2d, DOUBLE_EXPONENT_BITS, DOUBLE_MANTISSA_BITS}; use crate::f2s::{f2d, FLOAT_EXPONENT_BITS, FLOAT_MANTISSA_BITS}; use core::ptr; #[cfg(feature = "no-panic")] use no_panic::no_panic; pub mod to_fixed; pub use to_fixed::format64_to_fixed; /// Print f64 to the given buffer and return number of bytes written. /// /// At most 25 bytes will be written. /// /// ## 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`]: https://doc.rust-lang.org/std/primitive.f64.html#method.is_finite /// [`is_nan`]: https://doc.rust-lang.org/std/primitive.f64.html#method.is_nan /// [`is_infinite`]: https://doc.rust-lang.org/std/primitive.f64.html#method.is_infinite /// /// ## Safety /// /// The `result` pointer argument must point to sufficiently many writable bytes /// to hold Ryū's representation of `f`. /// /// ## Example /// /// ``` /// use std::{mem::MaybeUninit, slice, str}; /// /// let f = 1.234f64; /// /// unsafe { /// let mut buffer = [MaybeUninit::::uninit(); 25]; /// let len = ryu_js::raw::format64(f, buffer.as_mut_ptr() as *mut u8); /// let slice = slice::from_raw_parts(buffer.as_ptr() as *const u8, len); /// let print = str::from_utf8_unchecked(slice); /// assert_eq!(print, "1.234"); /// } /// ``` #[must_use] #[cfg_attr(feature = "no-panic", no_panic)] pub unsafe fn format64(f: f64, result: *mut u8) -> usize { debug_assert!(!result.is_null()); let bits = f.to_bits(); let sign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0; let ieee_mantissa = bits & ((1u64 << DOUBLE_MANTISSA_BITS) - 1); let ieee_exponent = (bits >> DOUBLE_MANTISSA_BITS) as u32 & ((1u32 << DOUBLE_EXPONENT_BITS) - 1); if ieee_exponent == 0 && ieee_mantissa == 0 { *result = b'0'; return 1; } let mut index = 0isize; if sign { *result = b'-'; index += 1; } let v = d2d(ieee_mantissa, ieee_exponent); let length = d2s::decimal_length17(v.mantissa) as isize; let k = v.exponent as isize; let kk = length + k; // 10^(kk-1) <= v < 10^kk debug_assert!(k >= -324); if 0 <= k && kk <= 21 { // 1234e7 -> 12340000000.0 write_mantissa_long(v.mantissa, result.offset(index + length)); for i in length..kk { *result.offset(index + i) = b'0'; } index as usize + kk as usize } else if 0 < kk && kk <= 21 { // 1234e-2 -> 12.34 write_mantissa_long(v.mantissa, result.offset(index + length + 1)); ptr::copy(result.offset(index + 1), result.offset(index), kk as usize); *result.offset(index + kk) = b'.'; index as usize + length as usize + 1 } else if -6 < kk && kk <= 0 { // 1234e-6 -> 0.001234 *result.offset(index) = b'0'; *result.offset(index + 1) = b'.'; let offset = 2 - kk; for i in 2..offset { *result.offset(index + i) = b'0'; } write_mantissa_long(v.mantissa, result.offset(index + length + offset)); index as usize + length as usize + offset as usize } else if length == 1 { // 1e+30 *result.offset(index) = b'0' + v.mantissa as u8; *result.offset(index + 1) = b'e'; index as usize + 2 + write_exponent3(kk - 1, result.offset(index + 2)) } else { // 1234e30 -> 1.234e+33 write_mantissa_long(v.mantissa, result.offset(index + length + 1)); *result.offset(index) = *result.offset(index + 1); *result.offset(index + 1) = b'.'; *result.offset(index + length + 1) = b'e'; index as usize + length as usize + 2 + write_exponent3(kk - 1, result.offset(index + length + 2)) } } /// Print f32 to the given buffer and return number of bytes written. /// /// At most 22 bytes will be written. /// /// ## 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`]: https://doc.rust-lang.org/std/primitive.f32.html#method.is_finite /// [`is_nan`]: https://doc.rust-lang.org/std/primitive.f32.html#method.is_nan /// [`is_infinite`]: https://doc.rust-lang.org/std/primitive.f32.html#method.is_infinite /// /// ## Safety /// /// The `result` pointer argument must point to sufficiently many writable bytes /// to hold Ryū's representation of `f`. /// /// ## Example /// /// ``` /// use std::{mem::MaybeUninit, slice, str}; /// /// let f = 1.234f32; /// /// unsafe { /// let mut buffer = [MaybeUninit::::uninit(); 22]; /// let len = ryu_js::raw::format32(f, buffer.as_mut_ptr() as *mut u8); /// let slice = slice::from_raw_parts(buffer.as_ptr() as *const u8, len); /// let print = str::from_utf8_unchecked(slice); /// assert_eq!(print, "1.234"); /// } /// ``` #[must_use] #[cfg_attr(feature = "no-panic", no_panic)] pub unsafe fn format32(f: f32, result: *mut u8) -> usize { debug_assert!(!result.is_null()); let bits = f.to_bits(); let sign = ((bits >> (FLOAT_MANTISSA_BITS + FLOAT_EXPONENT_BITS)) & 1) != 0; let ieee_mantissa = bits & ((1u32 << FLOAT_MANTISSA_BITS) - 1); let ieee_exponent = (bits >> FLOAT_MANTISSA_BITS) & ((1u32 << FLOAT_EXPONENT_BITS) - 1); if ieee_exponent == 0 && ieee_mantissa == 0 { *result = b'0'; return 1; } let mut index = 0isize; if sign { *result = b'-'; index += 1; } let v = f2d(ieee_mantissa, ieee_exponent); let length = common::decimal_length9(v.mantissa) as isize; let k = v.exponent as isize; let kk = length + k; // 10^(kk-1) <= v < 10^kk debug_assert!(k >= -45); if 0 <= k && kk <= 21 { // 1234e7 -> 12340000000.0 write_mantissa(v.mantissa, result.offset(index + length)); for i in length..kk { *result.offset(index + i) = b'0'; } index as usize + kk as usize } else if 0 < kk && kk <= 21 { // 1234e-2 -> 12.34 write_mantissa(v.mantissa, result.offset(index + length + 1)); ptr::copy(result.offset(index + 1), result.offset(index), kk as usize); *result.offset(index + kk) = b'.'; index as usize + length as usize + 1 } else if -6 < kk && kk <= 0 { // 1234e-6 -> 0.001234 *result.offset(index) = b'0'; *result.offset(index + 1) = b'.'; let offset = 2 - kk; for i in 2..offset { *result.offset(index + i) = b'0'; } write_mantissa(v.mantissa, result.offset(index + length + offset)); index as usize + length as usize + offset as usize } else if length == 1 { // 1e+30 *result.offset(index) = b'0' + v.mantissa as u8; *result.offset(index + 1) = b'e'; index as usize + 2 + write_exponent2(kk - 1, result.offset(index + 2)) } else { // 1234e30 -> 1.234e+33 write_mantissa(v.mantissa, result.offset(index + length + 1)); *result.offset(index) = *result.offset(index + 1); *result.offset(index + 1) = b'.'; *result.offset(index + length + 1) = b'e'; index as usize + length as usize + 2 + write_exponent2(kk - 1, result.offset(index + length + 2)) } } ryu-js-1.0.1/src/pretty/to_fixed/d2fixed_full_table.rs000064400000000000000000000630311046102023000210620ustar 00000000000000pub(crate) const TABLE_SIZE: usize = 3; pub(crate) const POW10_OFFSET: [u8; TABLE_SIZE] = [0, 2, 5]; #[rustfmt::skip] pub(crate) const POW10_SPLIT: [[u64; 3]; 8] = [ [ 1, 72057594037927936, 0], // 0 [ 699646928636035157, 72057594, 0], // 1 [ 1, 0, 256], // 2 [11902091922964236229, 4722366482869, 0], // 3 [ 6760415703743915872, 4722, 0], // 4 [ 1, 0, 16777216], // 5 [13369850649504950658, 309485009821345068, 0], // 5 (+1 length) [15151142278969419334, 309485009, 0], // 5 (+2 length) ]; pub(crate) const TABLE_SIZE_2: usize = 27; pub(crate) const ADDITIONAL_BITS_2: usize = 120; #[rustfmt::skip] pub(crate) const POW10_OFFSET_2: [u16; TABLE_SIZE_2] = [ /* 00 */ 0, /* 01 */ 2, /* 02 */ 6, /* 03 */ 12, /* 04 */ 20, /* 05 */ 29, /* 06 */ 40, /* 07 */ 52, /* 08 */ 66, /* 09 */ 80, /* 10 */ 95, /* 11 */ 112, /* 12 */ 130, /* 13 */ 150, /* 14 */ 170, /* 15 */ 192, /* 16 */ 215, /* 17 */ 240, /* 18 */ 265, /* 19 */ 292, /* 20 */ 320, /* 21 */ 350, /* 22 */ 381, /* 23 */ 413, /* 24 */ 446, /* 25 */ 480, /* 26 */ 516, ]; #[rustfmt::skip] pub(crate) const MIN_BLOCK_2: [u8; TABLE_SIZE_2] = [ /* 00 */ 0, /* 01 */ 0, /* 02 */ 0, /* 03 */ 0, /* 04 */ 0, /* 05 */ 0, /* 06 */ 1, /* 07 */ 1, /* 08 */ 2, /* 09 */ 3, /* 10 */ 3, /* 11 */ 4, /* 12 */ 4, /* 13 */ 5, /* 14 */ 5, /* 15 */ 6, /* 16 */ 6, /* 17 */ 7, /* 18 */ 7, /* 19 */ 8, /* 20 */ 8, /* 21 */ 9, /* 22 */ 9, /* 23 */ 10, /* 24 */ 11, /* 25 */ 11, /* 26 */ 12, ]; #[rustfmt::skip] pub(crate) const POW10_SPLIT_2: [[u64; 3]; 480 + 1] = [ [0, 0, 3906250], [0, 0, 202000000000], [0, 11153727427136454656, 59], [0, 7205759403792793600, 59604644775], [0, 0, 167390625000], [0, 0, 232000000000], [0, 16777216000000000, 0], [0, 12945425605062557696, 909494], [0, 4388757836872548352, 182701772928], [0, 1152921504606846976, 128237915039], [0, 0, 159062500000], [0, 0, 160000000000], [0, 256000000000, 0], [0, 16192327041775828992, 13], [0, 15024075324038053888, 13877787807], [0, 5449091666327633920, 159814456755], [0, 2494994193563254784, 179295395851], [0, 4611686018427387904, 11135253906], [0, 0, 146250000000], [0, 0, 128000000000], [0, 3906250, 0], [0, 3906250000000000, 0], [0, 4368439412768899072, 211758], [0, 1563676642168012800, 46236813575], [0, 11532349341402398720, 7084767080], [0, 9048364970084925440, 104625169910], [0, 16609275425742389248, 246490512847], [0, 0, 207900390625], [0, 0, 225000000000], [11153727427136454656, 59, 0], [7205759403792793600, 59604644775, 0], [0, 4264412554261970152, 3], [0, 14485570586272534528, 3231174267], [0, 17827675094632103936, 123785264354], [0, 7347197909193981952, 226966440203], [0, 13677404030777688064, 11398292396], [0, 3810326759732150272, 172741453558], [0, 9943947977234055168, 246206558227], [0, 0, 19539062500], [0, 0, 228000000000], [12945425605062557696, 909494, 0], [4388757836872548352, 909494701772928, 0], [1152921504606846976, 14878706826214591391, 49303], [0, 4387341015746028192, 151806576313], [0, 651726680428265472, 185237838233], [0, 2570638187944738816, 153035330174], [0, 7419175577111756800, 126139354575], [0, 17299322326264840192, 207402194313], [0, 7990511638862102528, 137937798142], [0, 16717361816799281152, 254433166503], [0, 0, 167906250000], [0, 0, 16000000000], [16192327041775828992, 13, 0], [15024075324038053888, 13877787807, 0], [5449091666327633920, 13877787807814456755, 0], [2494994193563254784, 9707857417284919307, 752316384], [4611686018427387904, 1844515466944871826, 224526264005], [0, 15167599819856275072, 197099991383], [0, 14830185305589481472, 87822237233], [0, 6163721531743535104, 49803945956], [0, 14122847407012052992, 228334136013], [0, 335491783960035328, 205765601092], [0, 941252322120433664, 68018187046], [0, 11529215046068469760, 38051025390], [0, 0, 238625000000], [0, 0, 64000000000], [4368439412768899072, 211758, 0], [1563676642168012800, 211758236813575, 0], [11532349341402398720, 8061591463141767016, 11479], [9048364970084925440, 16628725344207857142, 215437019748], [16609275425742389248, 3555541870038531535, 100901445007], [0, 18316647450161853665, 143192746310], [0, 16709574568378075648, 70992947447], [0, 7696022835795591168, 247905827852], [0, 16664449640376041472, 12417202233], [0, 3109186955116544000, 57903381625], [0, 10515518101817131008, 121168549362], [0, 9961962375743537152, 242570047378], [0, 9223372036854775808, 146540039062], [0, 0, 150500000000], [14485570586272534528, 3231174267, 0], [17827675094632103936, 3231174267785264354, 0], [7347197909193981952, 748977172262750475, 175162308], [13677404030777688064, 15965033457315095468, 196040602133], [3810326759732150272, 16809402149066729206, 21865466197], [9943947977234055168, 7563769067065700371, 85911239516], [0, 13550322810840051428, 92410032742], [0, 8663209637545764864, 102734564471], [0, 8969247575312957440, 119469633535], [0, 6193172891660451840, 255486223885], [0, 3427954273864908800, 13335732575], [0, 10058367555266936832, 95185829773], [0, 13907115649320091648, 141545265197], [0, 0, 45753906250], [0, 0, 74000000000], [14878706826214591391, 49303, 0], [4387341015746028192, 49303806576313, 0], [651726680428265472, 14106411361315920281, 2672], [2570638187944738816, 3609034283485221502, 112764710092], [7419175577111756800, 9896072247338192335, 204195646140], [17299322326264840192, 8889095178479228297, 188536467151], [7990511638862102528, 3631796911038383102, 207481878815], [16717361816799281152, 898318840772166823, 31196880105], [0, 17293677953982795024, 233048697961], [0, 7353628266884669440, 105937492160], [0, 2404693032470315008, 192398640987], [0, 9191155893041889280, 91130358670], [0, 6353946855033798656, 142498253559], [0, 3767824038248841216, 247344448149], [0, 7205759403792793600, 149204254150], [0, 0, 198390625000], [0, 0, 232000000000], [9707857417284919307, 752316384, 0], [1844515466944871826, 752316384526264005, 0], [15167599819856275072, 17063068157692817751, 40783152], [14830185305589481472, 5385330256507239985, 48924990778], [6163721531743535104, 3373050282752075748, 58291939338], [14122847407012052992, 4116064001262906061, 10182853422], [335491783960035328, 11306582046748043076, 46223132276], [941252322120433664, 17035410946089626406, 116612931040], [11529215046068469760, 15618595715183448558, 224923491477], [0, 5141740092277295680, 149846685770], [0, 16973644291514990592, 74278734288], [0, 14625255268443750400, 208920143100], [0, 14021170507320131584, 252792836676], [0, 4451355232865091584, 68760089176], [0, 12891553933348044800, 88241308450], [0, 1152921504606846976, 34698852539], [0, 0, 187062500000], [0, 0, 160000000000], [8061591463141767016, 11479, 0], [16628725344207857142, 11479437019748, 0], [3555541870038531535, 5562205901560339855, 622], [18316647450161853665, 2106077949367544134, 110301527786], [16709574568378075648, 7496855998374373623, 234114170714], [7696022835795591168, 229183437194837004, 90406405378], [16664449640376041472, 465169186276472889, 2012424059], [3109186955116544000, 2152980561625316473, 123025216872], [10515518101817131008, 2059790725449340402, 104116713310], [9961962375743537152, 17891190926410198930, 94111661478], [9223372036854775808, 9930696175609809814, 166969883403], [0, 7276914261609005312, 11538344118], [0, 10539762974036983808, 182394482312], [0, 12851089458992250880, 136571361695], [0, 9449311677678878720, 159696658955], [0, 8699564697382289408, 11512248212], [0, 4224376450473525248, 148471604347], [0, 4611686018427387904, 123229003906], [0, 0, 130250000000], [0, 0, 128000000000], [748977172262750475, 175162308, 0], [15965033457315095468, 175162308040602133, 0], [16809402149066729206, 13756840147955779925, 9495567], [7563769067065700371, 13788447602092505948, 15745759798], [13550322810840051428, 4972540435632173670, 54747473242], [8663209637545764864, 2844874687533091959, 90269561957], [8969247575312957440, 15377573779532804095, 101154220965], [6193172891660451840, 17824715805091194381, 165833619944], [3427954273864908800, 18277569135638159711, 232966279779], [10058367555266936832, 4254645803379752845, 99990829008], [13907115649320091648, 2933643244178200621, 208230644811], [0, 17188148801879487562, 75159033118], [0, 11069762501163246592, 30931771413], [0, 11676570643941818368, 21600093027], [0, 17840016768744030208, 99632988162], [0, 16463817321652158464, 2967109246], [0, 6954191143357644800, 126892505325], [0, 5080060379673919488, 237376987457], [0, 0, 65275390625], [0, 0, 161000000000], [14106411361315920281, 2672, 0], [3609034283485221502, 2672764710092, 0], [9896072247338192335, 16433563478020213436, 144], [8889095178479228297, 4194750497955655375, 144890865261], [3631796911038383102, 2691539602252904735, 109227397880], [898318840772166823, 3775467271962795241, 248145908654], [17293677953982795024, 16980212613224918121, 174204668490], [7353628266884669440, 4172857038337333440, 74920499170], [2404693032470315008, 5936867627376461659, 226226211033], [9191155893041889280, 17856837443266866062, 217321838238], [6353946855033798656, 8956297047799810807, 158968021097], [3767824038248841216, 15356974049716912789, 105485521835], [7205759403792793600, 6923608913322982854, 171832503231], [0, 4855902993563955944, 191375329591], [0, 13835893222288330752, 55263239028], [0, 9114973913760137216, 116750045274], [0, 17937099003422310400, 90494123725], [0, 7007960010734960640, 205972372085], [0, 7683422439270776832, 117379902273], [0, 720575940379279360, 65416519165], [0, 0, 253039062500], [0, 0, 228000000000], [17063068157692817751, 40783152, 0], [5385330256507239985, 40783152924990778, 0], [3373050282752075748, 2768933352715741194, 2210859], [4116064001262906061, 15201941611824153390, 43150104177], [11306582046748043076, 1418128541727000180, 113824098906], [17035410946089626406, 5353350204565757408, 90076876902], [15618595715183448558, 1721001680354286741, 102290205696], [5141740092277295680, 637631411660453962, 93295688], [16973644291514990592, 1630012588870568400, 72034566068], [14625255268443750400, 9253063571656828156, 180088363159], [14021170507320131584, 6029146854993203780, 151501609581], [4451355232865091584, 16987401965352759896, 109326840705], [12891553933348044800, 14499131620542087970, 129920888905], [1152921504606846976, 1978417255298660539, 73785999500], [0, 5790079354402454176, 140107250214], [0, 13748918935842078720, 38313880830], [0, 18047438014740692992, 254745330388], [0, 3116889656839372800, 212978353575], [0, 15995952446606147584, 167168966926], [0, 12530140063251562496, 14867142319], [0, 16717361816799281152, 175679260253], [0, 0, 93906250000], [0, 0, 16000000000], [5562205901560339855, 622, 0], [2106077949367544134, 622301527786, 0], [7496855998374373623, 13558973353698967386, 33], [229183437194837004, 6228991722850501890, 33735033418], [465169186276472889, 16886831391703377787, 74337674317], [2152980561625316473, 1181713637872883048, 77915436964], [2059790725449340402, 12393932434925221726, 164064060824], [17891190926410198930, 10684799845419711910, 152671876423], [9930696175609809814, 4590318792215640843, 71579224160], [7276914261609005312, 6383712187366189238, 96248841680], [10539762974036983808, 1904270214927675016, 208346061731], [12851089458992250880, 3711506775113308575, 163103230695], [9449311677678878720, 8091219444738793995, 231201201185], [8699564697382289408, 39436684991068052, 33438625885], [4224376450473525248, 18025182908196512891, 93002137866], [4611686018427387904, 7853924592034603138, 10977147123], [0, 4815749283615688320, 243425762105], [0, 14242399906544287744, 57261062291], [0, 76242322576113664, 147772082046], [0, 10858088421377703936, 126004133104], [0, 14293835879041466368, 240588618152], [0, 12182236992037191680, 168774870395], [0, 11529215046068469760, 123660400390], [0, 0, 6625000000], [0, 0, 64000000000], [13756840147955779925, 9495567, 0], [13788447602092505948, 9495567745759798, 0], [4972540435632173670, 14000097438505379162, 514755], [2844874687533091959, 16451062686452429925, 195758946802], [15377573779532804095, 4009347599785716645, 242891813895], [17824715805091194381, 16544162347546196456, 7217347168], [18277569135638159711, 17674258299745817187, 96896860837], [4254645803379752845, 5215238411201214416, 165958123462], [2933643244178200621, 14253990228345322571, 198282718640], [17188148801879487562, 11214836553940194590, 176772710358], [11069762501163246592, 14620711348380590101, 214607957507], [11676570643941818368, 6638710787931587427, 3792590350], [17840016768744030208, 17320000343692853250, 14359885232], [16463817321652158464, 75147386268843646, 176938919100], [6954191143357644800, 17938801582125480173, 188004073747], [5080060379673919488, 6573358613626446145, 19972464382], [0, 8688505427903736481, 254356342484], [0, 539870168696556032, 212471004823], [0, 9002861336394465280, 151029266420], [0, 17989846818158018560, 244488046090], [0, 2700938287723315200, 10975231550], [0, 17800090499088908288, 62146418157], [0, 8809040871136690176, 237964944839], [0, 9223372036854775808, 199477539062], [0, 0, 246500000000], [16433563478020213436, 144, 0], [4194750497955655375, 144890865261, 0], [2691539602252904735, 15763656745260536568, 7], [3775467271962795241, 8787336846248645550, 7854549544], [16980212613224918121, 17584084447880694346, 40476362484], [4172857038337333440, 18041672551129683938, 244953235127], [5936867627376461659, 14025886302294509785, 183978041028], [17856837443266866062, 18430498103283160734, 196760344819], [8956297047799810807, 3292348826238025833, 243999119304], [15356974049716912789, 9211721212658275243, 200178478587], [6923608913322982854, 10233245872666307519, 251499368407], [4855902993563955944, 6200995035623311671, 215554745370], [13835893222288330752, 8480542380570450804, 26336156614], [9114973913760137216, 11870363864499900506, 198459731123], [17937099003422310400, 9301051379839581901, 179643493714], [7007960010734960640, 11456694803569638005, 82504211005], [7683422439270776832, 14327208890643983169, 61621068669], [720575940379279360, 4510081789599866365, 125776679550], [0, 13255356976020303332, 126244492023], [0, 9658806854127314944, 247718574341], [0, 13708435528809971712, 5523604968], [0, 1580190652103131136, 232743135779], [0, 16557336970347413504, 35085662306], [0, 12751520132434493440, 98897575035], [0, 9295429630892703744, 123691261291], [0, 0, 107503906250], [0, 0, 202000000000], [2768933352715741194, 2210859, 0], [15201941611824153390, 2210859150104177, 0], [1418128541727000180, 16872870088062921306, 119850], [5353350204565757408, 5112979788807802982, 42914680120], [1721001680354286741, 13742728082020150272, 56277175189], [637631411660453962, 2217110934613627976, 149744994782], [1630012588870568400, 11021433940188610484, 222120189824], [9253063571656828156, 1713669895470733463, 128597473131], [6029146854993203780, 3313382510572018285, 107092898231], [16987401965352759896, 14976595232784069505, 183179618825], [14499131620542087970, 7213172372862496841, 9811882854], [1978417255298660539, 15836474542502248588, 102391026857], [5790079354402454176, 3221099285878340134, 169858497005], [13748918935842078720, 3265814602578095358, 237174616142], [18047438014740692992, 6502528252282225364, 78177040164], [3116889656839372800, 16392476834556790183, 36352502762], [15995952446606147584, 15167629413417091342, 234888637949], [12530140063251562496, 1366763272626280111, 253822238838], [16717361816799281152, 8720523635169216093, 118074092385], [0, 9649171375767398672, 97472740533], [0, 7647980704001073152, 181523082628], [0, 13286434495608651776, 132414597864], [0, 4358271637167013888, 232720259057], [0, 15954987941890097152, 241236262378], [0, 7911135695429697536, 234864921629], [0, 7205759403792793600, 29428863525], [0, 0, 37390625000], [0, 0, 232000000000], [13558973353698967386, 33, 0], [6228991722850501890, 33735033418, 0], [16886831391703377787, 15288289344628122701, 1], [1181713637872883048, 952589339068938148, 1828779826], [12393932434925221726, 10058155040190817688, 50051639971], [10684799845419711910, 5322725640026584391, 163545253677], [4590318792215640843, 2269982385930389600, 45288545535], [6383712187366189238, 13216683679976310224, 255123055991], [1904270214927675016, 17417440642083494819, 119716477857], [3711506775113308575, 3029180749090900711, 161944201349], [8091219444738793995, 8315443826261908513, 133164212217], [39436684991068052, 1488962797247197277, 249450781113], [18025182908196512891, 18009099634999034122, 185080716834], [7853924592034603138, 8092455412807497971, 34976275247], [4815749283615688320, 17808458047236758329, 47438692886], [14242399906544287744, 3164591817527425171, 22965398445], [76242322576113664, 3314036340472350590, 173171552866], [10858088421377703936, 33234902404332784, 98179654270], [14293835879041466368, 12349284717857274280, 126001801667], [12182236992037191680, 18209607903013119355, 195669456065], [11529215046068469760, 7891549145984268038, 193987144822], [0, 7703609897518594624, 118427801736], [0, 6336912652634587136, 136417613529], [0, 4461621834659397632, 217343524723], [0, 5484660635557953536, 115241865004], [0, 15142619273265938432, 44297324048], [0, 12170977992968765440, 16820883035], [0, 1152921504606846976, 91659790039], [0, 0, 215062500000], [0, 0, 160000000000], [14000097438505379162, 514755, 0], [16451062686452429925, 514755758946802, 0], [4009347599785716645, 17812314011563521031, 27904], [16544162347546196456, 7684138864490314336, 965607477], [17674258299745817187, 9740522787420029605, 53416558002], [5215238411201214416, 6701109407732989894, 178528034798], [14253990228345322571, 16534886227502443952, 238363267868], [11214836553940194590, 8908667306968317910, 28896357978], [14620711348380590101, 7531472173477105155, 90482939822], [6638710787931587427, 11527371604834801166, 174408281924], [17320000343692853250, 15688593496691078576, 68624900066], [75147386268843646, 11394944804253312188, 226850480357], [17938801582125480173, 11182279880854372627, 229617721195], [6573358613626446145, 150579373068361470, 107606192607], [8688505427903736481, 3147220002440857300, 223008162924], [539870168696556032, 3630514817795505815, 108170611138], [9002861336394465280, 11708796588334233588, 194196810602], [17989846818158018560, 16844495466426369546, 106634735134], [2700938287723315200, 17636655472325475902, 30913141928], [17800090499088908288, 17038926655686645229, 168956085008], [8809040871136690176, 15602838456783529415, 16923682064], [9223372036854775808, 10869815869248876790, 16845831567], [0, 18407124180939800832, 143589253898], [0, 5705018517251293184, 10997852201], [0, 9660452258743058432, 41309269673], [0, 5646292272224927744, 169523694166], [0, 7410409304047484928, 86306086117], [0, 5953758707383795712, 229401719093], [0, 4611686018427387904, 53322753906], [0, 0, 114250000000], [0, 0, 128000000000], [15763656745260536568, 7, 0], [8787336846248645550, 7854549544, 0], [17584084447880694346, 7854549544476362484, 0], [18041672551129683938, 15035424419724983, 425795984], [14025886302294509785, 18280822466032836292, 144000815071], [18430498103283160734, 11524250747302615283, 223991005371], [3292348826238025833, 15212285943691810760, 187624730884], [9211721212658275243, 7951804027551297019, 4824659673], [10233245872666307519, 1706416229965221847, 217431068160], [6200995035623311671, 3406023111930700826, 92505009], [8480542380570450804, 16132696204133391302, 177184640882], [11870363864499900506, 11593846688794356915, 114874555213], [9301051379839581901, 6875759884161133906, 77628503688], [11456694803569638005, 3593593325323835965, 136372735690], [14327208890643983169, 9542049733257388925, 202194809084], [4510081789599866365, 9926551925937787518, 252517275552], [13255356976020303332, 3128491553219547895, 160538119458], [9658806854127314944, 17158408656931354885, 34169595866], [13708435528809971712, 2065169543154992616, 218930159197], [1580190652103131136, 4832622393556232739, 93111953065], [16557336970347413504, 16505930714733656162, 169261976984], [12751520132434493440, 18270988073492888699, 152894788296], [9295429630892703744, 2525111411519708523, 200990472248], [0, 16728989342518570442, 56136886563], [0, 7974052022039438336, 35906880329], [0, 5356554962386550784, 73432274226], [0, 6693869495028547584, 50290379426], [0, 8157517147199766528, 162362875392], [0, 12065776720423157760, 442219890], [0, 11997589407315001344, 114654087066], [0, 0, 154650390625], [0, 0, 97000000000], [16872870088062921306, 119850, 0], [5112979788807802982, 119850914680120, 0], [13742728082020150272, 2418433229320326037, 6497], [2217110934613627976, 1143911773589293534, 97131103528], [11021433940188610484, 9276183703610924928, 40062011581], [1713669895470733463, 3532180128827684715, 189502862926], [3313382510572018285, 8563997501322031543, 78191479868], [14976595232784069505, 14843890409658460681, 60464255234], [7213172372862496841, 9489417861634552678, 2804688911], [15836474542502248588, 1113198223322322089, 15514422373], [3221099285878340134, 11190777557146597869, 101060346596], [3265814602578095358, 17764553645932638286, 228606653266], [6502528252282225364, 14900777150991234852, 82963018382], [16392476834556790183, 17364899863357893610, 142807772747], [15167629413417091342, 15537570181590167037, 75941353107], [1366763272626280111, 5558052627121307766, 147842293367], [8720523635169216093, 12095241565795232609, 119301302636], [9649171375767398672, 2187936505958366389, 108655684359], [7647980704001073152, 12009203621325860228, 7118608275], [13286434495608651776, 14814842834750302952, 147651020232], [4358271637167013888, 5965296499605198833, 200803114239], [15954987941890097152, 4051026394962148842, 255323379371], [7911135695429697536, 16799526299141688349, 171219606580], [7205759403792793600, 9460214166646215205, 52910704145], [0, 10750736995029068008, 17512839237], [0, 5377963045376430080, 69582798620], [0, 15996910350253424640, 28291539960], [0, 13651157529655246848, 248867194247], [0, 9771305410219737088, 135740030732], [0, 12709439623416250368, 12529703527], [0, 9943947977234055168, 103688980102], [0, 0, 134539062500], [0, 0, 228000000000], [952589339068938148, 1828779826, 0], [10058155040190817688, 1828779826051639971, 0], [5322725640026584391, 371564423966525229, 99138353], [2269982385930389600, 14464859121514339583, 49020142547], [13216683679976310224, 3913119023023056247, 211784141584], [17417440642083494819, 5493396321716566945, 16212130607], [3029180749090900711, 5837454566818211973, 47297797611], [8315443826261908513, 2886670683193253881, 235316449046], [1488962797247197277, 5504823105587173817, 22156486731], [18009099634999034122, 9431834277334851106, 75298417058], [8092455412807497971, 12921661346456247087, 162511300760], [17808458047236758329, 3643076516404724246, 152700484665], [3164591817527425171, 12559396953196866477, 57197491573], [3314036340472350590, 1626880974916825698, 117680846273], [33234902404332784, 6806994170946429566, 193088193394], [12349284717857274280, 7596631230206896579, 114369007893], [18209607903013119355, 3100480253729502401, 21411814204], [7891549145984268038, 6310570748781063286, 60168077371], [7703609897518594624, 14251867077375744136, 59342096725], [6336912652634587136, 6701165793751570137, 85772595262], [4461621834659397632, 10856833140463959923, 62363270925], [5484660635557953536, 15867563727561248556, 13588550103], [15142619273265938432, 5048961008671491600, 215860182353], [12170977992968765440, 13278183119599849051, 81273704724], [1152921504606846976, 4547591784941053655, 20719811749], [0, 11815437715887182496, 165246525444], [0, 398495392178782208, 4640516162], [0, 9154841240825495552, 66021602478], [0, 1902683298245640192, 174496284938], [0, 5081900962138816512, 10103144668], [0, 3234710432358858752, 220275490403], [0, 16717361816799281152, 99175354003], [0, 0, 147906250000], [0, 0, 16000000000], [17812314011563521031, 27904, 0], ]; ryu-js-1.0.1/src/pretty/to_fixed/mod.rs000064400000000000000000000517311046102023000161270ustar 00000000000000use crate::{ d2s::{DOUBLE_BIAS, DOUBLE_EXPONENT_BITS, DOUBLE_MANTISSA_BITS}, digit_table::DIGIT_TABLE, pretty::{ format64, to_fixed::d2fixed_full_table::{ ADDITIONAL_BITS_2, MIN_BLOCK_2, POW10_OFFSET, POW10_OFFSET_2, POW10_SPLIT, POW10_SPLIT_2, }, }, }; #[cfg(feature = "no-panic")] use no_panic::no_panic; mod d2fixed_full_table; /// Max bytes/characters required for `toFixed` representation of a [`f64`] value: /// /// - 1 byte for sign (-) /// - `22` bytes for whole part: /// Because we have a check for if `>= 1e21` (1 byte extra, just in case) /// - `1` byte for dot (`.`) /// - `108` (`9 * 12`) bytes for fraction part: /// We write digits in blocks, which consist of `9` digits. /// /// Total: `1 + 22 + 1 + 108 = 132` pub const MAX_BUFFER_SIZE: usize = 132; pub struct Cursor { buffer: *mut u8, len: isize, index: isize, } impl Cursor { #[cfg_attr(feature = "no-panic", no_panic)] pub fn new(buffer: *mut u8, len: usize) -> Self { debug_assert!(!buffer.is_null()); Self { buffer, len: len as isize, index: 0, } } /// Append one byte to buffer. /// /// # Safety /// /// The caller must ensure that there is enough space for the given byte. #[cfg_attr(feature = "no-panic", no_panic)] unsafe fn append_byte(&mut self, c: u8) { debug_assert!(self.index < self.len); *self.buffer.offset(self.index) = c; self.index += 1; } /// Append the byte `count` times into the buffer. /// /// # Safety /// /// The caller must ensure that there is enough space for the given bytes. #[cfg_attr(feature = "no-panic", no_panic)] unsafe fn append_bytes(&mut self, c: u8, count: usize) { debug_assert!(self.index + count as isize <= self.len); self.buffer.offset(self.index).write_bytes(c, count); self.index += count as isize; } /// Gets the current [`Cursor`] index. /// /// The `index` is also the amount of bytes that have been written into the buffer. #[cfg_attr(feature = "no-panic", no_panic)] fn index(&self) -> usize { self.index as usize } /// Convert `digits` to decimal and write the last 9 decimal digits to result. /// If `digits` contains additional digits, then those are silently ignored. /// /// # Safety /// /// The caller must ensure that the buffer has enough space for `9` bytes. #[cfg_attr(feature = "no-panic", no_panic)] unsafe fn append_nine_digits(&mut self, mut digits: u32) { let count = 9; debug_assert!(self.index + count <= self.len); if digits == 0 { self.append_bytes(b'0', 9); return; } let result = self.buffer.offset(self.index); for i in [0, 4] { let c = digits % 10000; digits /= 10000; let c0 = (c % 100) << 1; let c1 = (c / 100) << 1; // memcpy(result + 7 - i, DIGIT_TABLE + c0, 2); // memcpy(result + 5 - i, DIGIT_TABLE + c1, 2); result .offset(7 - i as isize) .copy_from_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c0 as isize), 2); result .offset(5 - i as isize) .copy_from_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c1 as isize), 2); } *(result.offset(0)) = b'0' + digits as u8; self.index += count; } /// Convert `digits` to a sequence of decimal digits. Append the digits to the result. /// /// # Safety /// /// The caller has to guarantee that: /// /// - 10^(olength-1) <= digits < 10^olength /// e.g., by passing `olength` as `decimalLength9(digits)`. /// /// - That the buffer has enough space for the decimal length of the given integer. #[cfg_attr(feature = "no-panic", no_panic)] unsafe fn append_n_digits(&mut self, mut digits: u32) { let olength = decimal_length9(digits); debug_assert!(self.index + olength as isize <= self.len); let result = self.buffer.offset(self.index); let mut i = 0; while digits >= 10000 { let c = digits % 10000; digits /= 10000; let c0 = (c % 100) << 1; let c1 = (c / 100) << 1; // memcpy(result + olength - i - 2, DIGIT_TABLE + c0, 2); // memcpy(result + olength - i - 4, DIGIT_TABLE + c1, 2); result .offset(olength as isize - i as isize - 2) .copy_from_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c0 as isize), 2); result .offset(olength as isize - i as isize - 4) .copy_from_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c1 as isize), 2); i += 4; } if digits >= 100 { let c = (digits % 100) << 1; digits /= 100; // memcpy(result + olength - i - 2, DIGIT_TABLE + c, 2); result .offset(olength as isize - i as isize - 2) .copy_from_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c as isize), 2); i += 2; } if digits >= 10 { let c = digits << 1; // memcpy(result + olength - i - 2, DIGIT_TABLE + c, 2); result .offset(olength as isize - i as isize - 2) .copy_from_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c as isize), 2); } else { *result = b'0' + digits as u8; } self.index += olength as isize; } /// Convert `digits` to decimal and write the last `count` decimal digits to result. /// If `digits` contains additional digits, then those are silently ignored. /// /// # Safety /// /// The caller must ensure that the buffer has enough space for the given `count`. #[cfg_attr(feature = "no-panic", no_panic)] unsafe fn append_c_digits(&mut self, count: u32, mut digits: u32) { debug_assert!(self.index + count as isize <= self.len); let result = self.buffer.offset(self.index); // Copy pairs of digits from DIGIT_TABLE. let mut i: u32 = 0; // for (; i < count - 1; i += 2) { while i < count - 1 { let c: u32 = (digits % 100) << 1; digits /= 100; // memcpy(result + count - i - 2, DIGIT_TABLE + c, 2); result .offset((count - i - 2) as isize) .copy_from_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c as isize), 2); i += 2; } // Generate the last digit if count is odd. if i < count { let c = b'0' + (digits % 10) as u8; // result[count - i - 1] = c; *result.offset((count - i - 1) as isize) = c; } self.index += count as isize; } /// Get the byte at the given index. /// /// # Safety /// /// The caller must ensure that the index is within `[0, len)`. #[cfg_attr(feature = "no-panic", no_panic)] unsafe fn get(&mut self, i: isize) -> u8 { debug_assert!((0..self.len).contains(&i)); *self.buffer.offset(i) } /// Set the byte at the given index with the value. /// /// # Safety /// /// The caller must ensure that the index is within `[0, len)`. #[cfg_attr(feature = "no-panic", no_panic)] unsafe fn set(&mut self, i: isize, c: u8) { debug_assert!((0..self.len).contains(&i)); *self.buffer.offset(i) = c; } } /// Because of the abs(f) >= 1e21 check, falls back to ToString ([`format64`]). /// /// See tests. const MAX_EXPONENT: u32 = 0b100_0100_0100; // 1029 const MIN_EXPONENT: u16 = 0b010_1001_0011; /// `e2 = exponent - bias - |mantissa|` const MAX_E2: i32 = MAX_EXPONENT as i32 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS as i32; const MIN_E2: i32 = MIN_EXPONENT as i32 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS as i32; const MAX_POW10_SPLIT_2_INX: i32 = -MIN_E2 / 16; const POW10_ADDITIONAL_BITS: u32 = 120; /// Returns `floor(log_10(2^e))` requires `0 <= e <= 1650`. #[cfg_attr(feature = "no-panic", no_panic)] fn log10_pow2(e: i32) -> u32 { // The first value this approximation fails for is 2^1651 which is just greater than 10^297. debug_assert!((0..=1650).contains(&e)); ((e as u32) * 78913) >> 18 } /// Get index from `e2` value. /// /// Range `[0, 2]` inclusive. #[cfg_attr(feature = "no-panic", no_panic)] fn index_for_exponent(e: u32) -> u32 { debug_assert!((0..=MAX_E2 as u32).contains(&e)); let result = (e + 15) / 16; debug_assert!((0..=2).contains(&result)); result } #[cfg_attr(feature = "no-panic", no_panic)] fn pow10_bits_for_index(idx: u32) -> u32 { 16 * idx + POW10_ADDITIONAL_BITS } /// Get the length from the index. /// /// Range `[2, 3]` inclusive. /// // TODO: Because the ranges are so small we could have tables, too speed up execution. #[cfg_attr(feature = "no-panic", no_panic)] fn length_for_index(idx: u32) -> u32 { // +1 for ceil, +16 for mantissa, +8 to round up when dividing by 9 (log10_pow2(16 * idx as i32) + 1 + 16 + 8) / 9 } #[cfg_attr(feature = "no-panic", no_panic)] fn umul256(a: u128, b_hi: u64, b_lo: u64) -> (u128, u128) { let a_lo = a as u64; let a_hi = (a >> 64) as u64; let b00 = (a_lo as u128) * (b_lo as u128); let b01 = (a_lo as u128) * (b_hi as u128); let b10 = (a_hi as u128) * (b_lo as u128); let b11 = (a_hi as u128) * (b_hi as u128); let b00_lo = b00 as u64; let b00_hi = (b00 >> 64) as u64; let mid1 = b10 + b00_hi as u128; let mid1_lo = (mid1) as u64; let mid1_hi = (mid1 >> 64) as u64; let mid2 = b01 + mid1_lo as u128; let mid2_lo = (mid2) as u64; let mid2_hi = (mid2 >> 64) as u64; let p_hi = b11 + mid1_hi as u128 + mid2_hi as u128; let p_lo = ((mid2_lo as u128) << 64) | b00_lo as u128; (p_hi, p_lo) } // Returns the high 128 bits of the 256-bit product of a and b. #[cfg_attr(feature = "no-panic", no_panic)] fn umul256_hi(a: u128, b_hi: u64, b_lo: u64) -> u128 { // Reuse the umul256 implementation. // Optimizers will likely eliminate the instructions used to compute the // low part of the product. let (hi, _lo) = umul256(a, b_hi, b_lo); hi } // Unfortunately, gcc/clang do not automatically turn a 128-bit integer division // into a multiplication, so we have to do it manually. #[cfg_attr(feature = "no-panic", no_panic)] fn uint128_mod1e9(v: u128) -> u32 { // After multiplying, we're going to shift right by 29, then truncate to uint32_t. // This means that we need only 29 + 32 = 61 bits, so we can truncate to uint64_t before shifting. let multiplied = umul256_hi(v, 0x89705F4136B4A597, 0x31680A88F8953031) as u64; // For uint32_t truncation, see the mod1e9() comment in d2s_intrinsics.rs let shifted = (multiplied >> 29) as u32; (v as u32).wrapping_sub(1000000000u32.wrapping_mul(shifted)) } // Best case: use 128-bit type. #[cfg_attr(feature = "no-panic", no_panic)] fn mul_shift_mod1e9(m: u64, mul: &[u64; 3], j: i32) -> u32 { let b0 = m as u128 * mul[0] as u128; // 0 let b1 = m as u128 * mul[1] as u128; // 64 let b2 = m as u128 * mul[2] as u128; // 128 debug_assert!((128..=180).contains(&j)); let mid = b1 + ((b0 >> 64) as u64) as u128; // 64 let s1 = b2 + ((mid >> 64) as u64) as u128; // 128 uint128_mod1e9(s1 >> (j - 128)) } // Returns the number of decimal digits in v, which must not contain more than 9 digits. #[cfg_attr(feature = "no-panic", no_panic)] fn decimal_length9(v: u32) -> u32 { // Function precondition: v is not a 10-digit number. // (f2s: 9 digits are sufficient for round-tripping.) // (d2fixed: We print 9-digit blocks.) debug_assert!(v < 1000000000); if v >= 100000000 { 9 } else if v >= 10000000 { 8 } else if v >= 1000000 { 7 } else if v >= 100000 { 6 } else if v >= 10000 { 5 } else if v >= 1000 { 4 } else if v >= 100 { 3 } else if v >= 10 { 2 } else { 1 } } /// Print [`f64`] to the given buffer using fixed notation, /// as defined in the ECMAScript `Number.prototype.toFixed()` method /// and return number of bytes written. /// /// At most 132 bytes will be written. /// /// ## 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`]: https://doc.rust-lang.org/std/primitive.f64.html#method.is_finite /// [`is_nan`]: https://doc.rust-lang.org/std/primitive.f64.html#method.is_nan /// [`is_infinite`]: https://doc.rust-lang.org/std/primitive.f64.html#method.is_infinite /// /// ## Safety /// /// The `result` pointer argument must point to sufficiently many writable bytes /// to hold Ryū's representation of `f`. /// /// ## Example /// /// ``` /// use std::{mem::MaybeUninit, slice, str}; /// /// let f = 1.235f64; /// /// unsafe { /// let mut buffer = [MaybeUninit::::uninit(); 132]; /// let len = ryu_js::raw::format64_to_fixed(f, 2, buffer.as_mut_ptr() as *mut u8); /// let slice = slice::from_raw_parts(buffer.as_ptr() as *const u8, len); /// let print = str::from_utf8_unchecked(slice); /// assert_eq!(print, "1.24"); /// } /// ``` #[must_use] #[cfg_attr(feature = "no-panic", no_panic)] pub unsafe fn format64_to_fixed(f: f64, fraction_digits: u8, result: *mut u8) -> usize { // SKIPPED: 1. Let x be ? thisNumberValue(this value). // SKIPPED: 2. Let f be ? ToIntegerOrInfinity(fractionDigits). // SKIPPED: 3. Assert: If fractionDigits is undefined, then f is 0. // SKIPPED: 4. If f is not finite, throw a RangeError exception. // 5. If f < 0 or f > 100, throw a RangeError exception. debug_assert!((0..=100).contains(&fraction_digits)); // 10. If x ≥ 10^21, then let f_abs = if f < 0.0 { -f } else { f }; if f_abs >= 1e21 { // a. Let m be ! ToString(𝔽(x)). return format64(f, result); } let mut result = Cursor::new(result, MAX_BUFFER_SIZE); let bits = f.to_bits(); let sign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0; let ieee_mantissa = bits & ((1u64 << DOUBLE_MANTISSA_BITS) - 1); let ieee_exponent = (bits >> DOUBLE_MANTISSA_BITS) as u32 & ((1u32 << DOUBLE_EXPONENT_BITS) - 1); // Special case when it's 0 or -0 it's the same. // // Return and append '.' and '0's is needed. // // See: https://tc39.es/ecma262/#%E2%84%9D if ieee_exponent == 0 && ieee_mantissa == 0 { result.append_byte(b'0'); if fraction_digits == 0 { return result.index(); } result.append_byte(b'.'); result.append_bytes(b'0', fraction_digits as usize); return result.index(); } debug_assert!((0..=MAX_EXPONENT).contains(&ieee_exponent)); if sign { result.append_byte(b'-'); } let (e2, m2) = if ieee_exponent == 0 { (1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS as i32, ieee_mantissa) } else { ( ieee_exponent as i32 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS as i32, (1 << DOUBLE_MANTISSA_BITS) | ieee_mantissa, ) }; debug_assert!((..=MAX_E2).contains(&e2)); let mut nonzero = false; // Write the whole part (integral part) of the floating point. // // xxxxxxx.1234567 (write xs) if e2 >= -(DOUBLE_MANTISSA_BITS as i32) { // 0 <= idx <= 2 let idx = if e2 < 0 { 0 } else { index_for_exponent(e2 as u32) }; let p10bits = pow10_bits_for_index(idx); let len = length_for_index(idx) as i32; for i in (0..len).rev() { let j = p10bits as i32 - e2; // SAFETY: 0 <= idx <= 2, putting idx inside the index bounds of `POW10_OFFSET`. let split_idx = *POW10_OFFSET.get_unchecked(idx as usize) as usize; // SAFETY: The max value inside `POW10_OFFSET` is 5, and the max value of `i` is 2, // putting `split_idx + i` inside the index bounds of `POW10_SPLIT`. let mul = POW10_SPLIT.get_unchecked(split_idx + i as usize); // Temporary: j is usually around 128, and by shifting a bit, we push it to 128 or above, which is // a slightly faster code path in mulShift_mod1e9. Instead, we can just increase the multipliers. let digits = mul_shift_mod1e9(m2 << 8, mul, j + 8); if nonzero { result.append_nine_digits(digits); } else if digits != 0 { result.append_n_digits(digits); nonzero = true; } } } // If the whole part is zero (nothing was writen), write a zero. if !nonzero { result.append_byte(b'0'); } // If fraction_digits is not zero, then write the dot. if fraction_digits != 0 { result.append_byte(b'.'); } // Check if it has fractional part. if e2 >= 0 { result.append_bytes(b'0', fraction_digits as usize); return result.index(); } // Write fractional part. // // 1234567.yyyyyyy (write ys) let fraction_digits = fraction_digits as u32; let idx = (-e2 / 16).min(MAX_POW10_SPLIT_2_INX) as usize; let min_block = MIN_BLOCK_2[idx]; // fraction_digits is defined to be [0, 100] inclusive. // // Therefore blocks can be [1, 12] inclusive. let blocks: u32 = fraction_digits / 9 + 1; if blocks <= min_block as u32 { result.append_bytes(b'0', fraction_digits as usize); return result.index(); } debug_assert!(idx <= 25); let mut round_up = false; for i in 0..blocks { let p: isize = POW10_OFFSET_2[idx] as isize + i as isize - min_block as isize; debug_assert!(p >= 0); let p = p as usize; // SAFETY: `idx` <= 26 per the min operation above. If `idx == 26` (which is the last index // of `POW10_OFFSET_2`), blocks <= min_block will always be true, since `1 <= blocks <= 12` // and `MIN_BLOCK_2[26]` = 12. Hence, for that value of `idx` this won't be executed. // Finally, for `idx <= 25` it is always true that `idx + 1 <= 26`, making this access always // in bounds for `POW10_OFFSET_2`. if p >= *POW10_OFFSET_2.get_unchecked(idx + 1) as usize { // If the remaining digits are all 0, then we might as well use memset. // No rounding required in this case. let fill = fraction_digits as usize - 9 * i as usize; // memset(result + index, '0', fill); result.append_bytes(b'0', fill); break; } debug_assert!(p <= 480); // Temporary: j is usually around 128, and by shifting a bit, we push it to 128 or above, which is // a slightly faster code path in mulShift_mod1e9. Instead, we can just increase the multipliers. let j: isize = ADDITIONAL_BITS_2 as isize + (-(e2 as isize) - 16 * idx as isize); // SAFETY: Since `idx <= 25`, the maximum value of `POW10_OFFSET_2[idx]` must be `480` for // `idx == 25`. // However, this also means that `min_block == 11` for that value of `idx`. // Hence, `POW10_OFFSET_2[25] - MIN_BLOCK_2[25] == 469`, and for that value of `blocks`, // `0 <= 1 <= 10`. // // This shows that the maximum value of `p` is `480`, which is exactly the biggest valid // index for `POW10_SPLIT_2`. let mut digits: u32 = mul_shift_mod1e9(m2 << 8, POW10_SPLIT_2.get_unchecked(p), j as i32 + 8); if i < blocks - 1 { result.append_nine_digits(digits); } else { let maximum: u32 = fraction_digits - 9 * i; let mut last_digit: u32 = 0; for _k in 0..(9 - maximum) { last_digit = digits % 10; digits /= 10; } // If last digit is 5 or above, round up. round_up = last_digit >= 5; if maximum != 0 { result.append_c_digits(maximum, digits); } break; } } // Roundup if needed. if round_up { let mut round_index = result.index; let mut dot_index = 0; // '.' can't be located at index 0 loop { round_index -= 1; let c = result.get(round_index); if round_index == -1 || c == b'-' { result.set(round_index + 1, b'1'); if dot_index > 0 { result.set(dot_index, b'0'); result.set(dot_index + 1, b'.'); } result.append_byte(b'0'); break; } if c == b'.' { dot_index = round_index; continue; } else if c == b'9' { result.set(round_index, b'0'); continue; } result.set(round_index, c + 1); break; } } result.index() } ryu-js-1.0.1/src/s2d.rs000064400000000000000000000171461046102023000127120ustar 00000000000000use crate::common::{ceil_log2_pow5, log2_pow5}; use crate::d2s; use crate::d2s_intrinsics::{mul_shift_64, multiple_of_power_of_2, multiple_of_power_of_5}; use crate::parse::Error; #[cfg(feature = "no-panic")] use no_panic::no_panic; const DOUBLE_EXPONENT_BIAS: usize = 1023; fn floor_log2(value: u64) -> u32 { 63_u32.wrapping_sub(value.leading_zeros()) } #[cfg_attr(feature = "no-panic", no_panic)] pub fn s2d(buffer: &[u8]) -> Result { let len = buffer.len(); if len == 0 { return Err(Error::InputTooShort); } let mut m10digits = 0; let mut e10digits = 0; let mut dot_index = len; let mut e_index = len; let mut m10 = 0u64; let mut e10 = 0i32; let mut signed_m = false; let mut signed_e = false; let mut i = 0; if unsafe { *buffer.get_unchecked(0) } == b'-' { signed_m = true; i += 1; } while let Some(c) = buffer.get(i).copied() { if c == b'.' { if dot_index != len { return Err(Error::MalformedInput); } dot_index = i; i += 1; continue; } if c < b'0' || c > b'9' { break; } if m10digits >= 17 { return Err(Error::InputTooLong); } m10 = 10 * m10 + (c - b'0') as u64; if m10 != 0 { m10digits += 1; } i += 1; } if let Some(b'e' | b'E') = buffer.get(i) { e_index = i; i += 1; match buffer.get(i) { Some(b'-') => { signed_e = true; i += 1; } Some(b'+') => i += 1, _ => {} } while let Some(c) = buffer.get(i).copied() { if c < b'0' || c > b'9' { return Err(Error::MalformedInput); } if e10digits > 3 { // TODO: Be more lenient. Return +/-Infinity or +/-0 instead. return Err(Error::InputTooLong); } e10 = 10 * e10 + (c - b'0') as i32; if e10 != 0 { e10digits += 1; } i += 1; } } if i < len { return Err(Error::MalformedInput); } if signed_e { e10 = -e10; } e10 -= if dot_index < e_index { (e_index - dot_index - 1) as i32 } else { 0 }; if m10 == 0 { return Ok(if signed_m { -0.0 } else { 0.0 }); } if m10digits + e10 <= -324 || m10 == 0 { // Number is less than 1e-324, which should be rounded down to 0; return // +/-0.0. let ieee = (signed_m as u64) << (d2s::DOUBLE_EXPONENT_BITS + d2s::DOUBLE_MANTISSA_BITS); return Ok(f64::from_bits(ieee)); } if m10digits + e10 >= 310 { // Number is larger than 1e+309, which should be rounded to +/-Infinity. let ieee = ((signed_m as u64) << (d2s::DOUBLE_EXPONENT_BITS + d2s::DOUBLE_MANTISSA_BITS)) | (0x7ff_u64 << d2s::DOUBLE_MANTISSA_BITS); return Ok(f64::from_bits(ieee)); } // Convert to binary float m2 * 2^e2, while retaining information about // whether the conversion was exact (trailing_zeros). let e2: i32; let m2: u64; let mut trailing_zeros = if e10 >= 0 { // The length of m * 10^e in bits is: // log2(m10 * 10^e10) = log2(m10) + e10 log2(10) = log2(m10) + e10 + e10 * log2(5) // // We want to compute the DOUBLE_MANTISSA_BITS + 1 top-most bits (+1 for // the implicit leading one in IEEE format). We therefore choose a // binary output exponent of // log2(m10 * 10^e10) - (DOUBLE_MANTISSA_BITS + 1). // // We use floor(log2(5^e10)) so that we get at least this many bits; // better to have an additional bit than to not have enough bits. e2 = floor_log2(m10) .wrapping_add(e10 as u32) .wrapping_add(log2_pow5(e10) as u32) .wrapping_sub(d2s::DOUBLE_MANTISSA_BITS + 1) as i32; // We now compute [m10 * 10^e10 / 2^e2] = [m10 * 5^e10 / 2^(e2-e10)]. // To that end, we use the DOUBLE_POW5_SPLIT table. let j = e2 .wrapping_sub(e10) .wrapping_sub(ceil_log2_pow5(e10)) .wrapping_add(d2s::DOUBLE_POW5_BITCOUNT); debug_assert!(j >= 0); debug_assert!(e10 < d2s::DOUBLE_POW5_SPLIT.len() as i32); m2 = mul_shift_64( m10, unsafe { d2s::DOUBLE_POW5_SPLIT.get_unchecked(e10 as usize) }, j as u32, ); // We also compute if the result is exact, i.e., // [m10 * 10^e10 / 2^e2] == m10 * 10^e10 / 2^e2. // This can only be the case if 2^e2 divides m10 * 10^e10, which in turn // requires that the largest power of 2 that divides m10 + e10 is // greater than e2. If e2 is less than e10, then the result must be // exact. Otherwise we use the existing multiple_of_power_of_2 function. e2 < e10 || e2 - e10 < 64 && multiple_of_power_of_2(m10, (e2 - e10) as u32) } else { e2 = floor_log2(m10) .wrapping_add(e10 as u32) .wrapping_sub(ceil_log2_pow5(-e10) as u32) .wrapping_sub(d2s::DOUBLE_MANTISSA_BITS + 1) as i32; let j = e2 .wrapping_sub(e10) .wrapping_add(ceil_log2_pow5(-e10)) .wrapping_sub(1) .wrapping_add(d2s::DOUBLE_POW5_INV_BITCOUNT); debug_assert!(-e10 < d2s::DOUBLE_POW5_INV_SPLIT.len() as i32); m2 = mul_shift_64( m10, unsafe { d2s::DOUBLE_POW5_INV_SPLIT.get_unchecked(-e10 as usize) }, j as u32, ); multiple_of_power_of_5(m10, -e10 as u32) }; // Compute the final IEEE exponent. let mut ieee_e2 = i32::max(0, e2 + DOUBLE_EXPONENT_BIAS as i32 + floor_log2(m2) as i32) as u32; if ieee_e2 > 0x7fe { // Final IEEE exponent is larger than the maximum representable; return +/-Infinity. let ieee = ((signed_m as u64) << (d2s::DOUBLE_EXPONENT_BITS + d2s::DOUBLE_MANTISSA_BITS)) | (0x7ff_u64 << d2s::DOUBLE_MANTISSA_BITS); return Ok(f64::from_bits(ieee)); } // We need to figure out how much we need to shift m2. The tricky part is // that we need to take the final IEEE exponent into account, so we need to // reverse the bias and also special-case the value 0. let shift = if ieee_e2 == 0 { 1 } else { ieee_e2 as i32 } .wrapping_sub(e2) .wrapping_sub(DOUBLE_EXPONENT_BIAS as i32) .wrapping_sub(d2s::DOUBLE_MANTISSA_BITS as i32); debug_assert!(shift >= 0); // We need to round up if the exact value is more than 0.5 above the value // we computed. That's equivalent to checking if the last removed bit was 1 // and either the value was not just trailing zeros or the result would // otherwise be odd. // // We need to update trailing_zeros given that we have the exact output // exponent ieee_e2 now. trailing_zeros &= (m2 & ((1_u64 << (shift - 1)) - 1)) == 0; let last_removed_bit = (m2 >> (shift - 1)) & 1; let round_up = last_removed_bit != 0 && (!trailing_zeros || ((m2 >> shift) & 1) != 0); let mut ieee_m2 = (m2 >> shift).wrapping_add(round_up as u64); debug_assert!(ieee_m2 <= 1_u64 << (d2s::DOUBLE_MANTISSA_BITS + 1)); ieee_m2 &= (1_u64 << d2s::DOUBLE_MANTISSA_BITS) - 1; if ieee_m2 == 0 && round_up { // Due to how the IEEE represents +/-Infinity, we don't need to check // for overflow here. ieee_e2 += 1; } let ieee = ((((signed_m as u64) << d2s::DOUBLE_EXPONENT_BITS) | ieee_e2 as u64) << d2s::DOUBLE_MANTISSA_BITS) | ieee_m2; Ok(f64::from_bits(ieee)) } ryu-js-1.0.1/src/s2f.rs000064400000000000000000000202611046102023000127040ustar 00000000000000use crate::common::{ceil_log2_pow5, log2_pow5}; use crate::f2s; use crate::f2s_intrinsics::{ mul_pow5_div_pow2, mul_pow5_inv_div_pow2, multiple_of_power_of_2_32, multiple_of_power_of_5_32, }; use crate::parse::Error; #[cfg(feature = "no-panic")] use no_panic::no_panic; const FLOAT_EXPONENT_BIAS: usize = 127; fn floor_log2(value: u32) -> u32 { 31_u32.wrapping_sub(value.leading_zeros()) } #[cfg_attr(feature = "no-panic", no_panic)] pub fn s2f(buffer: &[u8]) -> Result { let len = buffer.len(); if len == 0 { return Err(Error::InputTooShort); } let mut m10digits = 0; let mut e10digits = 0; let mut dot_index = len; let mut e_index = len; let mut m10 = 0u32; let mut e10 = 0i32; let mut signed_m = false; let mut signed_e = false; let mut i = 0; if unsafe { *buffer.get_unchecked(0) } == b'-' { signed_m = true; i += 1; } while let Some(c) = buffer.get(i).copied() { if c == b'.' { if dot_index != len { return Err(Error::MalformedInput); } dot_index = i; i += 1; continue; } if c < b'0' || c > b'9' { break; } if m10digits >= 9 { return Err(Error::InputTooLong); } m10 = 10 * m10 + (c - b'0') as u32; if m10 != 0 { m10digits += 1; } i += 1; } if let Some(b'e' | b'E') = buffer.get(i) { e_index = i; i += 1; match buffer.get(i) { Some(b'-') => { signed_e = true; i += 1; } Some(b'+') => i += 1, _ => {} } while let Some(c) = buffer.get(i).copied() { if c < b'0' || c > b'9' { return Err(Error::MalformedInput); } if e10digits > 3 { // TODO: Be more lenient. Return +/-Infinity or +/-0 instead. return Err(Error::InputTooLong); } e10 = 10 * e10 + (c - b'0') as i32; if e10 != 0 { e10digits += 1; } i += 1; } } if i < len { return Err(Error::MalformedInput); } if signed_e { e10 = -e10; } e10 -= if dot_index < e_index { (e_index - dot_index - 1) as i32 } else { 0 }; if m10 == 0 { return Ok(if signed_m { -0.0 } else { 0.0 }); } if m10digits + e10 <= -46 || m10 == 0 { // Number is less than 1e-46, which should be rounded down to 0; return // +/-0.0. let ieee = (signed_m as u32) << (f2s::FLOAT_EXPONENT_BITS + f2s::FLOAT_MANTISSA_BITS); return Ok(f32::from_bits(ieee)); } if m10digits + e10 >= 40 { // Number is larger than 1e+39, which should be rounded to +/-Infinity. let ieee = ((signed_m as u32) << (f2s::FLOAT_EXPONENT_BITS + f2s::FLOAT_MANTISSA_BITS)) | (0xff_u32 << f2s::FLOAT_MANTISSA_BITS); return Ok(f32::from_bits(ieee)); } // Convert to binary float m2 * 2^e2, while retaining information about // whether the conversion was exact (trailing_zeros). let e2: i32; let m2: u32; let mut trailing_zeros = if e10 >= 0 { // The length of m * 10^e in bits is: // log2(m10 * 10^e10) = log2(m10) + e10 log2(10) = log2(m10) + e10 + e10 * log2(5) // // We want to compute the FLOAT_MANTISSA_BITS + 1 top-most bits (+1 for // the implicit leading one in IEEE format). We therefore choose a // binary output exponent of // log2(m10 * 10^e10) - (FLOAT_MANTISSA_BITS + 1). // // We use floor(log2(5^e10)) so that we get at least this many bits; better to // have an additional bit than to not have enough bits. e2 = floor_log2(m10) .wrapping_add(e10 as u32) .wrapping_add(log2_pow5(e10) as u32) .wrapping_sub(f2s::FLOAT_MANTISSA_BITS + 1) as i32; // We now compute [m10 * 10^e10 / 2^e2] = [m10 * 5^e10 / 2^(e2-e10)]. // To that end, we use the FLOAT_POW5_SPLIT table. let j = e2 .wrapping_sub(e10) .wrapping_sub(ceil_log2_pow5(e10)) .wrapping_add(f2s::FLOAT_POW5_BITCOUNT); debug_assert!(j >= 0); m2 = mul_pow5_div_pow2(m10, e10 as u32, j); // We also compute if the result is exact, i.e., // [m10 * 10^e10 / 2^e2] == m10 * 10^e10 / 2^e2. // This can only be the case if 2^e2 divides m10 * 10^e10, which in turn // requires that the largest power of 2 that divides m10 + e10 is // greater than e2. If e2 is less than e10, then the result must be // exact. Otherwise we use the existing multiple_of_power_of_2 function. e2 < e10 || e2 - e10 < 32 && multiple_of_power_of_2_32(m10, (e2 - e10) as u32) } else { e2 = floor_log2(m10) .wrapping_add(e10 as u32) .wrapping_sub(ceil_log2_pow5(-e10) as u32) .wrapping_sub(f2s::FLOAT_MANTISSA_BITS + 1) as i32; // We now compute [m10 * 10^e10 / 2^e2] = [m10 / (5^(-e10) 2^(e2-e10))]. let j = e2 .wrapping_sub(e10) .wrapping_add(ceil_log2_pow5(-e10)) .wrapping_sub(1) .wrapping_add(f2s::FLOAT_POW5_INV_BITCOUNT); m2 = mul_pow5_inv_div_pow2(m10, -e10 as u32, j); // We also compute if the result is exact, i.e., // [m10 / (5^(-e10) 2^(e2-e10))] == m10 / (5^(-e10) 2^(e2-e10)) // // If e2-e10 >= 0, we need to check whether (5^(-e10) 2^(e2-e10)) // divides m10, which is the case iff pow5(m10) >= -e10 AND pow2(m10) >= // e2-e10. // // If e2-e10 < 0, we have actually computed [m10 * 2^(e10 e2) / // 5^(-e10)] above, and we need to check whether 5^(-e10) divides (m10 * // 2^(e10-e2)), which is the case iff pow5(m10 * 2^(e10-e2)) = pow5(m10) // >= -e10. (e2 < e10 || (e2 - e10 < 32 && multiple_of_power_of_2_32(m10, (e2 - e10) as u32))) && multiple_of_power_of_5_32(m10, -e10 as u32) }; // Compute the final IEEE exponent. let mut ieee_e2 = i32::max(0, e2 + FLOAT_EXPONENT_BIAS as i32 + floor_log2(m2) as i32) as u32; if ieee_e2 > 0xfe { // Final IEEE exponent is larger than the maximum representable; return // +/-Infinity. let ieee = ((signed_m as u32) << (f2s::FLOAT_EXPONENT_BITS + f2s::FLOAT_MANTISSA_BITS)) | (0xff_u32 << f2s::FLOAT_MANTISSA_BITS); return Ok(f32::from_bits(ieee)); } // We need to figure out how much we need to shift m2. The tricky part is // that we need to take the final IEEE exponent into account, so we need to // reverse the bias and also special-case the value 0. let shift = if ieee_e2 == 0 { 1 } else { ieee_e2 as i32 } .wrapping_sub(e2) .wrapping_sub(FLOAT_EXPONENT_BIAS as i32) .wrapping_sub(f2s::FLOAT_MANTISSA_BITS as i32); debug_assert!(shift >= 0); // We need to round up if the exact value is more than 0.5 above the value // we computed. That's equivalent to checking if the last removed bit was 1 // and either the value was not just trailing zeros or the result would // otherwise be odd. // // We need to update trailing_zeros given that we have the exact output // exponent ieee_e2 now. trailing_zeros &= (m2 & ((1_u32 << (shift - 1)) - 1)) == 0; let last_removed_bit = (m2 >> (shift - 1)) & 1; let round_up = last_removed_bit != 0 && (!trailing_zeros || ((m2 >> shift) & 1) != 0); let mut ieee_m2 = (m2 >> shift).wrapping_add(round_up as u32); debug_assert!(ieee_m2 <= 1_u32 << (f2s::FLOAT_MANTISSA_BITS + 1)); ieee_m2 &= (1_u32 << f2s::FLOAT_MANTISSA_BITS) - 1; if ieee_m2 == 0 && round_up { // Rounding up may overflow the mantissa. // In this case we move a trailing zero of the mantissa into the // exponent. // Due to how the IEEE represents +/-Infinity, we don't need to check // for overflow here. ieee_e2 += 1; } let ieee = ((((signed_m as u32) << f2s::FLOAT_EXPONENT_BITS) | ieee_e2) << f2s::FLOAT_MANTISSA_BITS) | ieee_m2; Ok(f32::from_bits(ieee)) } ryu-js-1.0.1/tests/common_test.rs000064400000000000000000000043071046102023000151170ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. #![allow(dead_code)] #![allow( clippy::approx_constant, clippy::cast_possible_wrap, clippy::cast_sign_loss, clippy::excessive_precision, clippy::unreadable_literal, clippy::wildcard_imports )] #[path = "../src/common.rs"] mod common; use common::{ceil_log2_pow5, decimal_length9, log10_pow2, log10_pow5}; #[test] fn test_decimal_length9() { assert_eq!(1, decimal_length9(0)); assert_eq!(1, decimal_length9(1)); assert_eq!(1, decimal_length9(9)); assert_eq!(2, decimal_length9(10)); assert_eq!(2, decimal_length9(99)); assert_eq!(3, decimal_length9(100)); assert_eq!(3, decimal_length9(999)); assert_eq!(9, decimal_length9(999999999)); } #[test] fn test_ceil_log2_pow5() { assert_eq!(1, ceil_log2_pow5(0)); assert_eq!(3, ceil_log2_pow5(1)); assert_eq!(5, ceil_log2_pow5(2)); assert_eq!(7, ceil_log2_pow5(3)); assert_eq!(10, ceil_log2_pow5(4)); assert_eq!(8192, ceil_log2_pow5(3528)); } #[test] fn test_log10_pow2() { assert_eq!(0, log10_pow2(0)); assert_eq!(0, log10_pow2(1)); assert_eq!(0, log10_pow2(2)); assert_eq!(0, log10_pow2(3)); assert_eq!(1, log10_pow2(4)); assert_eq!(496, log10_pow2(1650)); } #[test] fn test_log10_pow5() { assert_eq!(0, log10_pow5(0)); assert_eq!(0, log10_pow5(1)); assert_eq!(1, log10_pow5(2)); assert_eq!(2, log10_pow5(3)); assert_eq!(2, log10_pow5(4)); assert_eq!(1831, log10_pow5(2620)); } ryu-js-1.0.1/tests/d2s_intrinsics_test.rs000064400000000000000000000051231046102023000165610ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. #![allow(dead_code)] #![allow( clippy::cast_lossless, clippy::cast_possible_truncation, clippy::unreadable_literal )] #[path = "../src/d2s_intrinsics.rs"] mod d2s_intrinsics; use d2s_intrinsics::pow5_factor; #[test] fn test_pow5_factor() { assert_eq!(0, pow5_factor(1)); assert_eq!(0, pow5_factor(2)); assert_eq!(0, pow5_factor(3)); assert_eq!(0, pow5_factor(4)); assert_eq!(1, pow5_factor(5)); assert_eq!(0, pow5_factor(6)); assert_eq!(0, pow5_factor(7)); assert_eq!(0, pow5_factor(8)); assert_eq!(0, pow5_factor(9)); assert_eq!(1, pow5_factor(10)); assert_eq!(0, pow5_factor(12)); assert_eq!(0, pow5_factor(14)); assert_eq!(0, pow5_factor(16)); assert_eq!(0, pow5_factor(18)); assert_eq!(1, pow5_factor(20)); assert_eq!(2, pow5_factor(5 * 5)); assert_eq!(3, pow5_factor(5 * 5 * 5)); assert_eq!(4, pow5_factor(5 * 5 * 5 * 5)); assert_eq!(5, pow5_factor(5 * 5 * 5 * 5 * 5)); assert_eq!(6, pow5_factor(5 * 5 * 5 * 5 * 5 * 5)); assert_eq!(7, pow5_factor(5 * 5 * 5 * 5 * 5 * 5 * 5)); assert_eq!(8, pow5_factor(5 * 5 * 5 * 5 * 5 * 5 * 5 * 5)); assert_eq!(9, pow5_factor(5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5)); assert_eq!(10, pow5_factor(5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5)); assert_eq!(0, pow5_factor(42)); assert_eq!(1, pow5_factor(42 * 5)); assert_eq!(2, pow5_factor(42 * 5 * 5)); assert_eq!(3, pow5_factor(42 * 5 * 5 * 5)); assert_eq!(4, pow5_factor(42 * 5 * 5 * 5 * 5)); assert_eq!(5, pow5_factor(42 * 5 * 5 * 5 * 5 * 5)); assert_eq!(27, pow5_factor(7450580596923828125)); // 5^27, largest power of 5 < 2^64. assert_eq!(1, pow5_factor(18446744073709551615)); // 2^64 - 1, largest multiple of 5 < 2^64. assert_eq!(0, pow5_factor(18446744073709551614)); // 2^64 - 2, largest non-multiple of 5 < 2^64. } ryu-js-1.0.1/tests/d2s_table_test.rs000064400000000000000000000033461046102023000154700ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. #![allow(dead_code)] #![allow( clippy::cast_lossless, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss, clippy::unreadable_literal, clippy::unseparated_literal_suffix, clippy::wildcard_imports )] #[path = "../src/common.rs"] mod common; #[path = "../src/d2s_full_table.rs"] mod d2s_full_table; #[path = "../src/d2s_intrinsics.rs"] mod d2s_intrinsics; #[path = "../src/d2s_small_table.rs"] mod d2s_small_table; use d2s_full_table::{DOUBLE_POW5_INV_SPLIT, DOUBLE_POW5_SPLIT}; use d2s_small_table::{compute_inv_pow5, compute_pow5}; #[test] fn test_compute_pow5() { for (i, entry) in DOUBLE_POW5_SPLIT.iter().enumerate() { assert_eq!(*entry, unsafe { compute_pow5(i as u32) }, "entry {i}"); } } #[test] fn test_compute_inv_pow5() { for (i, entry) in DOUBLE_POW5_INV_SPLIT[..292].iter().enumerate() { assert_eq!(*entry, unsafe { compute_inv_pow5(i as u32) }, "entry {i}"); } } ryu-js-1.0.1/tests/d2s_test.rs000064400000000000000000000227331046102023000143220ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. #![allow( clippy::approx_constant, clippy::excessive_precision, clippy::cast_lossless, clippy::float_cmp, clippy::int_plus_one, clippy::non_ascii_literal, clippy::unreadable_literal, clippy::unseparated_literal_suffix )] #[macro_use] mod macros; use std::f64; fn pretty(f: f64) -> String { ryu_js::Buffer::new().format(f).to_owned() } #[allow(clippy::int_plus_one)] fn ieee_parts_to_double(sign: bool, ieee_exponent: u32, ieee_mantissa: u64) -> f64 { assert!(ieee_exponent <= 2047); assert!(ieee_mantissa <= (1u64 << 53) - 1); f64::from_bits(((sign as u64) << 63) | ((ieee_exponent as u64) << 52) | ieee_mantissa) } #[test] fn test_ryu() { check!(0.3); assert_eq!(pretty(1234000000000000.0), "1234000000000000"); assert_eq!(pretty(1.234e16), "12340000000000000"); check!(2.71828); assert_eq!(pretty(1.1e128), "1.1e+128"); check!(1.1e-64); check!(2.718281828459045); check!(5e-324); assert_eq!(pretty(1.7976931348623157e308), "1.7976931348623157e+308"); } #[test] fn test_random() { let n = if cfg!(miri) { 100 } else { 1000000 }; let mut buffer = ryu_js::Buffer::new(); for _ in 0..n { let f: f64 = rand::random(); assert_eq!(f, buffer.format_finite(f).parse().unwrap()); } } #[test] #[cfg_attr(miri, ignore)] fn test_non_finite() { for i in 0u64..1 << 23 { let f = f64::from_bits((((1 << 11) - 1) << 52) + (i << 29)); assert!(!f.is_finite(), "f={}", f); ryu_js::Buffer::new().format_finite(f); } } #[test] fn test_basic() { assert_eq!(pretty(0.0), "0"); assert_eq!(pretty(-0.0), "0"); assert_eq!(pretty(1.0), "1"); assert_eq!(pretty(-1.0), "-1"); assert_eq!(pretty(f64::NAN.copysign(1.0)), "NaN"); assert_eq!(pretty(f64::NAN.copysign(-1.0)), "NaN"); assert_eq!(pretty(f64::INFINITY), "Infinity"); assert_eq!(pretty(f64::NEG_INFINITY), "-Infinity"); } #[test] fn test_switch_to_subnormal() { check!(2.2250738585072014e-308); } #[test] fn test_min_and_max() { assert_eq!(f64::from_bits(0x7fefffffffffffff), 1.7976931348623157e308); check!(1.7976931348623157e+308); assert_eq!(f64::from_bits(1), 5e-324); check!(5e-324); } #[test] fn test_lots_of_trailing_zeros() { check!(2.9802322387695312e-8); } #[test] fn test_regression() { assert_eq!(pretty(-21098088986959630.0), "-21098088986959630"); check!(4.940656e-318); check!(1.18575755e-316); check!(2.989102097996e-312); assert_eq!(pretty(4.708356024711512e+18), "4708356024711512000"); assert_eq!(pretty(9.409340012568248e+18), "9409340012568248000"); check!(1.2345678); } #[test] fn test_looks_like_pow5() { // These numbers have a mantissa that is a multiple of the largest power of // 5 that fits, and an exponent that causes the computation for q to result // in 22, which is a corner case for Ryū. assert_eq!(f64::from_bits(0x4830F0CF064DD592), 5.764607523034235e39); check!(5.764607523034235e+39); assert_eq!(f64::from_bits(0x4840F0CF064DD592), 1.152921504606847e40); check!(1.152921504606847e+40); assert_eq!(f64::from_bits(0x4850F0CF064DD592), 2.305843009213694e+40); check!(2.305843009213694e+40); } #[test] fn test_output_length() { check!(1.2); check!(1.23); check!(1.234); check!(1.2345); check!(1.23456); check!(1.234567); check!(1.2345678); // already tested in Regression check!(1.23456789); check!(1.234567895); // 1.234567890 would be trimmed check!(1.2345678901); check!(1.23456789012); check!(1.234567890123); check!(1.2345678901234); check!(1.23456789012345); check!(1.234567890123456); check!(1.2345678901234567); // Test 32-bit chunking check!(4.294967294); // 2^32 - 2 check!(4.294967295); // 2^32 - 1 check!(4.294967296); // 2^32 check!(4.294967297); // 2^32 + 1 check!(4.294967298); // 2^32 + 2 } // Test min, max shift values in shiftright128 #[test] fn test_min_max_shift() { let max_mantissa = (1u64 << 53) - 1; // 32-bit opt-size=0: 49 <= dist <= 50 // 32-bit opt-size=1: 30 <= dist <= 50 // 64-bit opt-size=0: 50 <= dist <= 50 // 64-bit opt-size=1: 30 <= dist <= 50 assert_eq!(1.7800590868057611E-307, ieee_parts_to_double(false, 4, 0)); check!(1.7800590868057611e-307); // 32-bit opt-size=0: 49 <= dist <= 49 // 32-bit opt-size=1: 28 <= dist <= 49 // 64-bit opt-size=0: 50 <= dist <= 50 // 64-bit opt-size=1: 28 <= dist <= 50 assert_eq!( 2.8480945388892175E-306, ieee_parts_to_double(false, 6, max_mantissa) ); check!(2.8480945388892175e-306); // 32-bit opt-size=0: 52 <= dist <= 53 // 32-bit opt-size=1: 2 <= dist <= 53 // 64-bit opt-size=0: 53 <= dist <= 53 // 64-bit opt-size=1: 2 <= dist <= 53 assert_eq!(2.446494580089078E-296, ieee_parts_to_double(false, 41, 0)); check!(2.446494580089078e-296); // 32-bit opt-size=0: 52 <= dist <= 52 // 32-bit opt-size=1: 2 <= dist <= 52 // 64-bit opt-size=0: 53 <= dist <= 53 // 64-bit opt-size=1: 2 <= dist <= 53 assert_eq!( 4.8929891601781557E-296, ieee_parts_to_double(false, 40, max_mantissa) ); check!(4.8929891601781557e-296); // 32-bit opt-size=0: 57 <= dist <= 58 // 32-bit opt-size=1: 57 <= dist <= 58 // 64-bit opt-size=0: 58 <= dist <= 58 // 64-bit opt-size=1: 58 <= dist <= 58 assert_eq!(1.8014398509481984E16, ieee_parts_to_double(false, 1077, 0)); assert_eq!(pretty(1.8014398509481984e16), "18014398509481984"); // 32-bit opt-size=0: 57 <= dist <= 57 // 32-bit opt-size=1: 57 <= dist <= 57 // 64-bit opt-size=0: 58 <= dist <= 58 // 64-bit opt-size=1: 58 <= dist <= 58 assert_eq!( 3.6028797018963964E16, ieee_parts_to_double(false, 1076, max_mantissa) ); assert_eq!(pretty(3.6028797018963964e+16), "36028797018963964"); // 32-bit opt-size=0: 51 <= dist <= 52 // 32-bit opt-size=1: 51 <= dist <= 59 // 64-bit opt-size=0: 52 <= dist <= 52 // 64-bit opt-size=1: 52 <= dist <= 59 assert_eq!(2.900835519859558E-216, ieee_parts_to_double(false, 307, 0)); check!(2.900835519859558e-216); // 32-bit opt-size=0: 51 <= dist <= 51 // 32-bit opt-size=1: 51 <= dist <= 59 // 64-bit opt-size=0: 52 <= dist <= 52 // 64-bit opt-size=1: 52 <= dist <= 59 assert_eq!( 5.801671039719115E-216, ieee_parts_to_double(false, 306, max_mantissa) ); check!(5.801671039719115e-216); // https://github.com/ulfjack/ryu/commit/19e44d16d80236f5de25800f56d82606d1be00b9#commitcomment-30146483 // 32-bit opt-size=0: 49 <= dist <= 49 // 32-bit opt-size=1: 44 <= dist <= 49 // 64-bit opt-size=0: 50 <= dist <= 50 // 64-bit opt-size=1: 44 <= dist <= 50 assert_eq!( 3.196104012172126E-27, ieee_parts_to_double(false, 934, 0x000FA7161A4D6E0C) ); check!(3.196104012172126e-27); } #[test] fn test_ecma262_compliance() { assert_eq!(pretty(f64::NAN), "NaN"); assert_eq!(pretty(f64::INFINITY), "Infinity"); assert_eq!(pretty(f64::NEG_INFINITY), "-Infinity"); assert_eq!(pretty(0.0), "0"); assert_eq!(pretty(9.0), "9"); assert_eq!(pretty(90.0), "90"); assert_eq!(pretty(90.12), "90.12"); assert_eq!(pretty(0.000001), "0.000001"); assert_eq!(pretty(0.0000001), "1e-7"); assert_eq!(pretty(3e50), "3e+50"); assert_eq!(pretty(90.12), "90.12"); assert_eq!(pretty(111111111111111111111.0), "111111111111111110000"); assert_eq!(pretty(1111111111111111111111.0), "1.1111111111111111e+21"); assert_eq!(pretty(11111111111111111111111.0), "1.1111111111111111e+22"); assert_eq!(pretty(0.1), "0.1"); assert_eq!(pretty(0.01), "0.01"); assert_eq!(pretty(0.001), "0.001"); assert_eq!(pretty(0.0001), "0.0001"); assert_eq!(pretty(0.00001), "0.00001"); assert_eq!(pretty(0.000001), "0.000001"); assert_eq!(pretty(0.0000001), "1e-7"); assert_eq!(pretty(0.00000012), "1.2e-7"); assert_eq!(pretty(0.000000123), "1.23e-7"); assert_eq!(pretty(0.00000001), "1e-8"); assert_eq!(pretty(-0.0), "0"); assert_eq!(pretty(-9.0), "-9"); assert_eq!(pretty(-90.12), "-90.12"); assert_eq!(pretty(-0.0000000123), "-1.23e-8"); assert_eq!(pretty(-111111111111111111111.0), "-111111111111111110000"); assert_eq!(pretty(-1111111111111111111111.0), "-1.1111111111111111e+21"); assert_eq!(pretty(-0.000000123), "-1.23e-7"); assert_eq!( pretty(123456789010111213141516171819.0), "1.234567890101112e+29" ); } #[test] fn max_size_double_to_string() { // See: https://viewer.scuttlebot.io/%25LQo5KOMeR%2Baj%2BEj0JVg3qLRqr%2BwiKo74nS8Uz7o0LDM%3D.sha256 assert_eq!( pretty(-0.0000015809161985788154), "-0.0000015809161985788154" ); } ryu-js-1.0.1/tests/exhaustive.rs000064400000000000000000000031151046102023000147510ustar 00000000000000#![cfg(exhaustive)] use std::str; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; use std::thread; #[test] fn test_exhaustive() { const BATCH_SIZE: u32 = 1_000_000; let counter = Arc::new(AtomicUsize::new(0)); let finished = Arc::new(AtomicUsize::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) as u32; if batch > u32::max_value() / BATCH_SIZE { return; } let min = batch * BATCH_SIZE; let max = if batch == u32::max_value() / BATCH_SIZE { u32::max_value() } else { min + BATCH_SIZE - 1 }; let mut bytes = [0u8; 24]; let mut buffer = ryu::Buffer::new(); for u in min..=max { let f = f32::from_bits(u); if !f.is_finite() { continue; } let n = unsafe { ryu::raw::format32(f, &mut bytes[0]) }; assert_eq!(Ok(Ok(f)), str::from_utf8(&bytes[..n]).map(str::parse)); assert_eq!(Ok(f), buffer.format_finite(f).parse()); } let increment = (max - min + 1) as usize; let update = finished.fetch_add(increment, Ordering::Relaxed); println!("{}", update + increment); })); } for w in workers { w.join().unwrap(); } } ryu-js-1.0.1/tests/f2s_test.rs000064400000000000000000000140571046102023000143240ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. #![allow( clippy::approx_constant, clippy::float_cmp, clippy::non_ascii_literal, clippy::unreadable_literal, clippy::unseparated_literal_suffix )] #[macro_use] mod macros; use std::f32; fn pretty(f: f32) -> String { ryu_js::Buffer::new().format(f).to_owned() } #[test] fn test_ryu() { assert_eq!(pretty(1234000000000.0), "1234000000000"); assert_eq!(pretty(1.234e13), "12340000000000"); assert_eq!(pretty(2.71828), "2.71828"); assert_eq!(pretty(1.1e32), "1.1e+32"); assert_eq!(pretty(1.1e-32), "1.1e-32"); assert_eq!(pretty(2.7182817), "2.7182817"); assert_eq!(pretty(1e-45), "1e-45"); assert_eq!(pretty(3.4028235e38), "3.4028235e+38"); assert_eq!(pretty(-0.001234), "-0.001234"); } #[test] fn test_random() { let n = if cfg!(miri) { 100 } else { 1000000 }; let mut buffer = ryu_js::Buffer::new(); for _ in 0..n { let f: f32 = rand::random(); assert_eq!(f, buffer.format_finite(f).parse().unwrap()); } } #[test] #[cfg_attr(miri, ignore)] fn test_non_finite() { for i in 0u32..1 << 23 { let f = f32::from_bits((((1 << 8) - 1) << 23) + i); assert!(!f.is_finite(), "f={}", f); ryu_js::Buffer::new().format_finite(f); } } #[test] fn test_basic() { assert_eq!(pretty(0.0), "0"); assert_eq!(pretty(-0.0), "0"); assert_eq!(pretty(1.0), "1"); assert_eq!(pretty(-1.0), "-1"); assert_eq!(pretty(f32::NAN.copysign(1.0)), "NaN"); assert_eq!(pretty(f32::NAN.copysign(-1.0)), "NaN"); assert_eq!(pretty(f32::INFINITY), "Infinity"); assert_eq!(pretty(f32::NEG_INFINITY), "-Infinity"); } #[test] fn test_switch_to_subnormal() { check!(1.1754944e-38); } #[test] fn test_min_and_max() { assert_eq!(f32::from_bits(0x7f7fffff), 3.4028235e38); assert_eq!(pretty(3.4028235e38), "3.4028235e+38"); assert_eq!(f32::from_bits(1), 1e-45); assert_eq!(pretty(1e-45), "1e-45"); } // Check that we return the exact boundary if it is the shortest // representation, but only if the original floating point number is even. #[test] fn test_boundary_round_even() { assert_eq!(pretty(33554450.0), "33554450"); assert_eq!(pretty(9000000000.0), "9000000000"); assert_eq!(pretty(34366720000.0), "34366720000"); } // If the exact value is exactly halfway between two shortest representations, // then we round to even. It seems like this only makes a difference if the // last two digits are ...2|5 or ...7|5, and we cut off the 5. #[test] fn test_exact_value_round_even() { check!(305404.12); check!(8099.0312); } #[test] fn test_lots_of_trailing_zeros() { // Pattern for the first test: 00111001100000000000000000000000 check!(0.00024414062); check!(0.0024414062); check!(0.0043945312); check!(0.0063476562); } #[test] fn test_regression() { assert_eq!(pretty(4.7223665e21), "4.7223665e+21"); assert_eq!(pretty(8388608.0), "8388608"); assert_eq!(pretty(16777216.0), "16777216"); assert_eq!(pretty(33554436.0), "33554436"); assert_eq!(pretty(67131496.0), "67131496"); assert_eq!(pretty(1.9310392e-38), "1.9310392e-38"); assert_eq!(pretty(-2.47e-43), "-2.47e-43"); assert_eq!(pretty(1.993244e-38), "1.993244e-38"); assert_eq!(pretty(4103.9004), "4103.9004"); assert_eq!(pretty(5339999700.0), "5339999700"); assert_eq!(pretty(6.0898e-39), "6.0898e-39"); assert_eq!(pretty(0.0010310042), "0.0010310042"); assert_eq!(pretty(2.882326e17), "288232600000000000"); assert_eq!(pretty(7.038531e-26), "7.038531e-26"); assert_eq!(pretty(9.223404e17), "922340400000000000"); assert_eq!(pretty(67108870.0), "67108870"); assert_eq!(pretty(1e-44), "1e-44"); assert_eq!(pretty(2.816025e14), "281602500000000"); assert_eq!(pretty(9.223372e18), "9223372000000000000"); assert_eq!(pretty(1.5846086e29), "1.5846086e+29"); assert_eq!(pretty(1.1811161e19), "11811161000000000000"); assert_eq!(pretty(5.368709e18), "5368709000000000000"); assert_eq!(pretty(4.6143166e18), "4614316600000000000"); assert_eq!(pretty(0.007812537), "0.007812537"); assert_eq!(pretty(1e-45), "1e-45"); assert_eq!(pretty(1.18697725e20), "118697725000000000000"); assert_eq!(pretty(1.00014165e-36), "1.00014165e-36"); assert_eq!(pretty(200.0), "200"); assert_eq!(pretty(33554432.0), "33554432"); } #[test] fn test_looks_like_pow5() { // These numbers have a mantissa that is the largest power of 5 that fits, // and an exponent that causes the computation for q to result in 10, which // is a corner case for Ryū. assert_eq!(f32::from_bits(0x5D1502F9), 6.7108864e17); assert_eq!(pretty(6.7108864e17), "671088640000000000"); assert_eq!(f32::from_bits(0x5D9502F9), 1.3421773e18); assert_eq!(pretty(1.3421773e18), "1342177300000000000"); assert_eq!(f32::from_bits(0x5E1502F9), 2.6843546e18); assert_eq!(pretty(2.6843546e18), "2684354600000000000"); } #[test] fn test_output_length() { assert_eq!(pretty(1.0), "1"); // already tested in test_basic assert_eq!(pretty(1.2), "1.2"); assert_eq!(pretty(1.23), "1.23"); assert_eq!(pretty(1.234), "1.234"); assert_eq!(pretty(1.2345), "1.2345"); assert_eq!(pretty(1.23456), "1.23456"); assert_eq!(pretty(1.234567), "1.234567"); assert_eq!(pretty(1.2345678), "1.2345678"); assert_eq!(pretty(1.23456735e-36), "1.23456735e-36"); } ryu-js-1.0.1/tests/macros/mod.rs000064400000000000000000000002671046102023000146340ustar 00000000000000macro_rules! check { ($f:tt) => { assert_eq!(pretty($f), stringify!($f)); }; (-$f:tt) => { assert_eq!(pretty(-$f), concat!("-", stringify!($f))); }; } ryu-js-1.0.1/tests/s2d_test.rs000064400000000000000000000120061046102023000143120ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. #![allow(clippy::float_cmp)] #![cfg(not(feature = "small"))] #![allow(dead_code)] #![allow( clippy::cast_lossless, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss, clippy::excessive_precision, clippy::float_cmp, clippy::manual_range_contains, clippy::similar_names, clippy::too_many_lines, clippy::unreadable_literal, clippy::unseparated_literal_suffix, clippy::wildcard_imports )] #[path = "../src/common.rs"] mod common; #[cfg(not(feature = "small"))] #[path = "../src/d2s_full_table.rs"] mod d2s_full_table; #[path = "../src/d2s_intrinsics.rs"] mod d2s_intrinsics; #[cfg(feature = "small")] #[path = "../src/d2s_small_table.rs"] mod d2s_small_table; #[path = "../src/d2s.rs"] mod d2s; #[path = "../src/s2d.rs"] mod s2d; #[path = "../src/parse.rs"] mod parse; use crate::parse::Error; use crate::s2d::s2d; impl PartialEq for Error { fn eq(&self, other: &Self) -> bool { *self as u8 == *other as u8 } } #[test] fn test_bad_input() { assert_eq!(Error::MalformedInput, s2d(b"x").unwrap_err()); assert_eq!(Error::MalformedInput, s2d(b"1..1").unwrap_err()); assert_eq!(Error::MalformedInput, s2d(b"..").unwrap_err()); assert_eq!(Error::MalformedInput, s2d(b"1..1").unwrap_err()); assert_eq!(Error::MalformedInput, s2d(b"1ee1").unwrap_err()); assert_eq!(Error::MalformedInput, s2d(b"1e.1").unwrap_err()); assert_eq!(Error::InputTooShort, s2d(b"").unwrap_err()); assert_eq!(Error::InputTooLong, s2d(b"123456789012345678").unwrap_err()); assert_eq!(Error::InputTooLong, s2d(b"1e12345").unwrap_err()); } #[test] fn test_basic() { assert_eq!(0.0, s2d(b"0").unwrap()); assert_eq!(-0.0, s2d(b"-0").unwrap()); assert_eq!(1.0, s2d(b"1").unwrap()); assert_eq!(2.0, s2d(b"2").unwrap()); assert_eq!(123456789.0, s2d(b"123456789").unwrap()); assert_eq!(123.456, s2d(b"123.456").unwrap()); assert_eq!(123.456, s2d(b"123456e-3").unwrap()); assert_eq!(123.456, s2d(b"1234.56e-1").unwrap()); assert_eq!(1.453, s2d(b"1.453").unwrap()); assert_eq!(1453.0, s2d(b"1.453e+3").unwrap()); assert_eq!(0.0, s2d(b".0").unwrap()); assert_eq!(1.0, s2d(b"1e0").unwrap()); assert_eq!(1.0, s2d(b"1E0").unwrap()); assert_eq!(1.0, s2d(b"000001.000000").unwrap()); assert_eq!(0.2316419, s2d(b"0.2316419").unwrap()); } #[test] fn test_min_max() { assert_eq!( 1.7976931348623157e308, s2d(b"1.7976931348623157e308").unwrap(), ); assert_eq!(5E-324, s2d(b"5E-324").unwrap()); } #[test] fn test_mantissa_rounding_overflow() { // This results in binary mantissa that is all ones and requires rounding up // because it is closer to 1 than to the next smaller float. This is a // regression test that the mantissa overflow is handled correctly by // increasing the exponent. assert_eq!(1.0, s2d(b"0.99999999999999999").unwrap()); // This number overflows the mantissa *and* the IEEE exponent. assert_eq!(f64::INFINITY, s2d(b"1.7976931348623159e308").unwrap()); } #[test] fn test_underflow() { assert_eq!(0.0, s2d(b"2.4e-324").unwrap()); assert_eq!(0.0, s2d(b"1e-324").unwrap()); assert_eq!(0.0, s2d(b"9.99999e-325").unwrap()); // These are just about halfway between 0 and the smallest float. // The first is just below the halfway point, the second just above. assert_eq!(0.0, s2d(b"2.4703282292062327e-324").unwrap()); assert_eq!(5e-324, s2d(b"2.4703282292062328e-324").unwrap()); } #[test] fn test_overflow() { assert_eq!(f64::INFINITY, s2d(b"2e308").unwrap()); assert_eq!(f64::INFINITY, s2d(b"1e309").unwrap()); } #[test] fn test_table_size_denormal() { assert_eq!(5e-324, s2d(b"4.9406564584124654e-324").unwrap()); } #[test] #[allow(clippy::excessive_precision)] fn test_issue157() { assert_eq!( 1.2999999999999999E+154, s2d(b"1.2999999999999999E+154").unwrap(), ); } #[test] fn test_issue173() { // Denormal boundary assert_eq!( 2.2250738585072012e-308, s2d(b"2.2250738585072012e-308").unwrap(), ); assert_eq!( 2.2250738585072013e-308, s2d(b"2.2250738585072013e-308").unwrap(), ); assert_eq!( 2.2250738585072014e-308, s2d(b"2.2250738585072014e-308").unwrap(), ); } ryu-js-1.0.1/tests/s2f_test.rs000064400000000000000000000061201046102023000143140ustar 00000000000000// Translated from C to Rust. The original C code can be found at // https://github.com/ulfjack/ryu and carries the following license: // // Copyright 2018 Ulf Adams // // The contents of this file may be used under the terms of the Apache License, // Version 2.0. // // (See accompanying file LICENSE-Apache or copy at // http://www.apache.org/licenses/LICENSE-2.0) // // Alternatively, the contents of this file may be used under the terms of // the Boost Software License, Version 1.0. // (See accompanying file LICENSE-Boost or copy at // https://www.boost.org/LICENSE_1_0.txt) // // Unless required by applicable law or agreed to in writing, this software // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. #![allow(dead_code)] #![allow( clippy::cast_lossless, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_possible_wrap, clippy::cast_sign_loss, clippy::checked_conversions, clippy::float_cmp, clippy::manual_range_contains, clippy::similar_names, clippy::too_many_lines, clippy::unreadable_literal, clippy::unseparated_literal_suffix, clippy::wildcard_imports )] #[path = "../src/common.rs"] mod common; #[cfg(not(feature = "small"))] #[path = "../src/d2s_full_table.rs"] mod d2s_full_table; #[path = "../src/d2s_intrinsics.rs"] mod d2s_intrinsics; #[cfg(feature = "small")] #[path = "../src/d2s_small_table.rs"] mod d2s_small_table; #[path = "../src/d2s.rs"] mod d2s; #[path = "../src/f2s_intrinsics.rs"] mod f2s_intrinsics; #[path = "../src/f2s.rs"] mod f2s; #[path = "../src/s2f.rs"] mod s2f; #[path = "../src/parse.rs"] mod parse; use crate::parse::Error; use crate::s2f::s2f; impl PartialEq for Error { fn eq(&self, other: &Self) -> bool { *self as u8 == *other as u8 } } #[test] fn test_basic() { assert_eq!(0.0, s2f(b"0").unwrap()); assert_eq!(-0.0, s2f(b"-0").unwrap()); assert_eq!(1.0, s2f(b"1").unwrap()); assert_eq!(-1.0, s2f(b"-1").unwrap()); assert_eq!(123456792.0, s2f(b"123456789").unwrap()); assert_eq!(299792448.0, s2f(b"299792458").unwrap()); } #[test] fn test_min_max() { assert_eq!(1e-45, s2f(b"1e-45").unwrap()); assert_eq!(f32::MIN_POSITIVE, s2f(b"1.1754944e-38").unwrap()); assert_eq!(f32::MAX, s2f(b"3.4028235e+38").unwrap()); } #[test] fn test_mantissa_rounding_overflow() { assert_eq!(1.0, s2f(b"0.999999999").unwrap()); assert_eq!(f32::INFINITY, s2f(b"3.4028236e+38").unwrap()); assert_eq!(1.1754944e-38, s2f(b"1.17549430e-38").unwrap()); // FLT_MIN assert_eq!(1.1754944e-38, s2f(b"1.17549431e-38").unwrap()); assert_eq!(1.1754944e-38, s2f(b"1.17549432e-38").unwrap()); assert_eq!(1.1754944e-38, s2f(b"1.17549433e-38").unwrap()); assert_eq!(1.1754944e-38, s2f(b"1.17549434e-38").unwrap()); assert_eq!(1.1754944e-38, s2f(b"1.17549435e-38").unwrap()); } #[test] fn test_trailing_zeros() { assert_eq!(26843550.0, s2f(b"26843549.5").unwrap()); assert_eq!(50000004.0, s2f(b"50000002.5").unwrap()); assert_eq!(99999992.0, s2f(b"99999989.5").unwrap()); } ryu-js-1.0.1/tests/to_fixed.rs000064400000000000000000000272371046102023000144000ustar 00000000000000#![allow( clippy::approx_constant, clippy::excessive_precision, clippy::cast_lossless, clippy::float_cmp, clippy::int_plus_one, clippy::non_ascii_literal, clippy::unreadable_literal, clippy::unseparated_literal_suffix )] fn pretty_to_fixed(f: f64, exp: u8) -> String { ryu_js::Buffer::new().format_to_fixed(f, exp).to_owned() } fn pretty_to_string(f: f64) -> String { ryu_js::Buffer::new().format(f).to_owned() } #[test] fn range_over_100() { let expected = "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; assert_eq!(pretty_to_fixed(0.0, 101), expected); } #[test] fn nan() { for fraction_digits in 0..=100u8 { assert_eq!(pretty_to_fixed(f64::NAN, fraction_digits), "NaN"); } } #[test] fn infinity() { for fraction_digits in 0..=100u8 { assert_eq!(pretty_to_fixed(f64::INFINITY, fraction_digits), "Infinity"); } for fraction_digits in 0..=100u8 { assert_eq!( pretty_to_fixed(f64::NEG_INFINITY, fraction_digits), "-Infinity" ); } } #[test] fn positive_zero() { assert_eq!(pretty_to_fixed(0.0, 0), "0"); for fraction_digits in 1..=100u8 { let expected = "0".repeat(usize::from(fraction_digits)); assert_eq!( pretty_to_fixed(0.0, fraction_digits), format!("0.{expected}") ); } } #[test] fn negative_zero() { assert_eq!(pretty_to_fixed(-0.0, 0), "0"); for fraction_digits in 1..=100u8 { let expected = "0".repeat(usize::from(fraction_digits)); assert_eq!( pretty_to_fixed(-0.0, fraction_digits), format!("0.{expected}") ); } } const WHOLE_NUMBERS: &[f64] = &[ 1.0, 10.0, 100.0, 123.0, 1234567890.0, i32::MAX as f64, 12_345_678_910_111_213.0, 9_007_199_254_740_992.0, ]; #[track_caller] fn check_whole_number(test_case: usize, number: f64) { for fraction_digits in 0..=100u8 { let mut fraction = "0".repeat(usize::from(fraction_digits)); if fraction_digits != 0 { fraction = format!(".{fraction}"); } let expected = format!("{number}{fraction}"); assert_eq!( pretty_to_fixed(number, fraction_digits), expected, "Test case {test_case}. expected {number} with fraction_digits {fraction_digits} to equal {expected}" ); } } #[test] fn test_positive_whole_numbers() { for (test_case, number) in WHOLE_NUMBERS.iter().copied().enumerate() { check_whole_number(test_case, number); } } #[test] fn test_negative_whole_numbers() { for (test_case, number) in WHOLE_NUMBERS.iter().copied().map(|x| -x).enumerate() { check_whole_number(test_case, number); } } // https://github.com/boa-dev/boa/issues/2609 #[test] fn boa_issue_2609() { assert_eq!(pretty_to_fixed(1.25, 1), "1.3"); assert_eq!(pretty_to_fixed(1.35, 1), "1.4"); } #[test] fn test262() { // test262 commit: be0abd93cd799a758714b5707fa87c9048fc38ce // test/built-ins/Number/prototype/toFixed/S15.7.4.5_A1.1_T02.js assert_eq!(pretty_to_fixed(1.0, 0), "1"); assert_eq!(pretty_to_fixed(1.0, 1), "1.0"); // test/built-ins/Number/prototype/toFixed/S15.7.4.5_A1.4_T01.js assert_eq!(pretty_to_fixed(1e21, 1), pretty_to_string(1e21)); // test/built-ins/Number/prototype/toFixed/exactness.js assert_eq!( pretty_to_fixed(1000000000000000128.0, 0), "1000000000000000128" ); } #[test] fn rounding() { assert_eq!(pretty_to_fixed(1.5, 0), "2"); assert_eq!(pretty_to_fixed(2.9, 0), "3"); assert_eq!(pretty_to_fixed(2.55, 1), "2.5"); assert_eq!(pretty_to_fixed(2.449999999999999999, 1), "2.5"); assert_eq!(pretty_to_fixed(1010.954526123, 9), "1010.954526123"); assert_eq!(pretty_to_fixed(1010.954526123, 8), "1010.95452612"); assert_eq!(pretty_to_fixed(1010.954526123, 7), "1010.9545261"); assert_eq!(pretty_to_fixed(1010.954526123, 6), "1010.954526"); assert_eq!(pretty_to_fixed(1010.954526123, 5), "1010.95453"); assert_eq!(pretty_to_fixed(1010.954526123, 4), "1010.9545"); assert_eq!(pretty_to_fixed(1010.954526123, 3), "1010.955"); assert_eq!(pretty_to_fixed(1010.954526123, 2), "1010.95"); assert_eq!(pretty_to_fixed(1010.954526123, 1), "1011.0"); assert_eq!(pretty_to_fixed(1010.954526123, 0), "1011"); } #[test] fn test_to_fixed_fraction_digits_50() { assert_eq!( pretty_to_fixed(0.3, 50), "0.29999999999999998889776975374843459576368331909180" ); } #[test] fn test_to_fixed_fraction_digits_100() { assert_eq!(pretty_to_fixed(1.0, 100), "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); assert_eq!(pretty_to_fixed(1.256, 100), "1.2560000000000000053290705182007513940334320068359375000000000000000000000000000000000000000000000000"); assert_eq!(pretty_to_fixed(1.12345678910111213, 100), "1.1234567891011122409139488809159956872463226318359375000000000000000000000000000000000000000000000000"); } #[test] #[rustfmt::skip] fn test_exponential_notation() { assert_eq!(pretty_to_fixed(1.23, 2), "1.23"); assert_eq!(pretty_to_fixed(1.23e0, 2), "1.23"); assert_eq!(pretty_to_fixed(1.23e1, 2), "12.30"); assert_eq!(pretty_to_fixed(1.23e2, 2), "123.00"); assert_eq!(pretty_to_fixed(1.23e3, 2), "1230.00"); assert_eq!(pretty_to_fixed(1.23e4, 2), "12300.00"); assert_eq!(pretty_to_fixed(1.23e5, 2), "123000.00"); assert_eq!(pretty_to_fixed(1.23e6, 2), "1230000.00"); assert_eq!(pretty_to_fixed(1.23e7, 2), "12300000.00"); assert_eq!(pretty_to_fixed(1.23e8, 2), "123000000.00"); assert_eq!(pretty_to_fixed(1.23e9, 2), "1230000000.00"); assert_eq!(pretty_to_fixed(1.23e10, 2), "12300000000.00"); assert_eq!(pretty_to_fixed(1.23e11, 2), "123000000000.00"); assert_eq!(pretty_to_fixed(1.23e12, 2), "1230000000000.00"); assert_eq!(pretty_to_fixed(1.23e13, 2), "12300000000000.00"); assert_eq!(pretty_to_fixed(1.23e14, 2), "123000000000000.00"); assert_eq!(pretty_to_fixed(1.23e15, 2), "1230000000000000.00"); assert_eq!(pretty_to_fixed(1.23e16, 2), "12300000000000000.00"); assert_eq!(pretty_to_fixed(1.23e17, 2), "123000000000000000.00"); assert_eq!(pretty_to_fixed(1.23e18, 2), "1230000000000000000.00"); assert_eq!(pretty_to_fixed(1.23e19, 2), "12300000000000000000.00"); assert_eq!(pretty_to_fixed(1.23e20, 2), "123000000000000000000.00"); // fallback to exponential notation assert_eq!(pretty_to_fixed(1.23e21, 2), "1.23e+21"); assert_eq!(pretty_to_fixed(1.23e22, 2), "1.23e+22"); assert_eq!(pretty_to_fixed(1.23e23, 2), "1.23e+23"); assert_eq!(pretty_to_fixed(1.23e24, 2), "1.23e+24"); assert_eq!(pretty_to_fixed(1.23e25, 2), "1.23e+25"); } const DOUBLE_MANTISSA_BITS: u8 = 52; const DOUBLE_BIAS: i32 = 1023; fn f64_and_e2_from_parts(sign: bool, exponent: u16, mantissa: u64) -> (f64, i32) { assert!(exponent <= 0b111_1111_1111, "Invalid f64 exponent"); let mut bits: u64 = 0; bits |= mantissa; bits |= (u64::from(exponent)) << 52; bits |= u64::from(sign) << (52 + 11); let e2 = if exponent == 0 { 1 - DOUBLE_BIAS - i32::from(DOUBLE_MANTISSA_BITS) } else { i32::from(exponent) - DOUBLE_BIAS - i32::from(DOUBLE_MANTISSA_BITS) }; (f64::from_bits(bits), e2) } fn f64_from_parts(sign: bool, exponent: u16, mantissa: u64) -> f64 { f64_and_e2_from_parts(sign, exponent, mantissa).0 } #[test] fn test_f64_from_parts() { assert_eq!(pretty_to_fixed(f64_from_parts(false, 0, 0), 2), "0.00"); assert_eq!(pretty_to_fixed(f64_from_parts(true, 0, 0), 2), "0.00"); } #[test] fn test_max_exponent_boundry_zero_mantissa() { assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0000_0000, 0), 2), "2.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0011_1111, 0), 2), "18446744073709551616.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0000, 0), 2), "36893488147419103232.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0001, 0), 2), "73786976294838206464.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0010, 0), 2), "147573952589676412928.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0011, 0), 2), "295147905179352825856.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0100, 0), 2), "590295810358705651712.00" ); // ToString fallback assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0101, 0), 2), "1.1805916207174113e+21" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0110, 0), 2), "2.3611832414348226e+21" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0111, 0), 2), "4.722366482869645e+21" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0111_1111, 0), 2), "3.402823669209385e+38" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_1111_1111, 0), 2), "1.157920892373162e+77" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b101_1111_1111, 0), 2), "1.3407807929942597e+154" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b111_1111_1111, 0), 2), "Infinity" ); } #[test] fn test_max_exponent_boundry_and_full_mantissa() { let m = !(u64::MAX << u64::from(DOUBLE_MANTISSA_BITS)); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0000_0000, m), 2), "4.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0011_1111, m), 2), "36893488147419099136.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0000, m), 2), "73786976294838198272.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0001, m), 2), "147573952589676396544.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0010, m), 2), "295147905179352793088.00" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0011, m), 2), "590295810358705586176.00" ); // ToString fallback assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0100, m), 2), "1.1805916207174112e+21" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b100_0100_0101, m), 2), "2.3611832414348223e+21" ); assert_eq!( pretty_to_fixed(f64_from_parts(false, 0b111_1111_1111, m), 2), "NaN" ); } const MIN_EXPONENT: u16 = 0b010_1001_0011; #[test] fn test_min_exponent_boundry_zero_mantissa() { let m = 0; let expected = "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; for exponent in 0..=MIN_EXPONENT { assert_eq!( pretty_to_fixed(f64_from_parts(false, exponent, m), 100), expected ); } assert_ne!( pretty_to_fixed(f64_from_parts(false, MIN_EXPONENT + 1, m), 100), expected ); } #[test] fn test_min_exponent_boundry_full_mantissa() { let m = !(u64::MAX << u64::from(DOUBLE_MANTISSA_BITS)); let expected = "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; for exponent in 0..=MIN_EXPONENT { assert_eq!( pretty_to_fixed(f64_from_parts(false, exponent, m), 100), expected ); } assert_ne!( pretty_to_fixed(f64_from_parts(false, MIN_EXPONENT + 1, m), 100), expected ); }