{"id":20597283,"url":"https://github.com/dtrifuno/quickdiv","last_synced_at":"2025-04-15T00:12:15.515Z","repository":{"id":194260578,"uuid":"690152402","full_name":"dtrifuno/quickdiv","owner":"dtrifuno","description":"Library for fast division and modulo operations by a fixed divisor","archived":false,"fork":false,"pushed_at":"2023-11-20T17:46:04.000Z","size":64,"stargazers_count":13,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-15T00:11:49.526Z","etag":null,"topics":["arithmetic","division","rust"],"latest_commit_sha":null,"homepage":"https://docs.rs/quickdiv/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dtrifuno.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-11T16:21:56.000Z","updated_at":"2024-10-16T13:08:36.000Z","dependencies_parsed_at":"2023-11-19T23:23:21.960Z","dependency_job_id":"ad3ef706-19e3-484e-8523-d85f42c67883","html_url":"https://github.com/dtrifuno/quickdiv","commit_stats":null,"previous_names":["dtrifuno/quickdiv"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtrifuno%2Fquickdiv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtrifuno%2Fquickdiv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtrifuno%2Fquickdiv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtrifuno%2Fquickdiv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dtrifuno","download_url":"https://codeload.github.com/dtrifuno/quickdiv/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248981270,"owners_count":21193147,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arithmetic","division","rust"],"created_at":"2024-11-16T08:21:36.335Z","updated_at":"2025-04-15T00:12:15.433Z","avatar_url":"https://github.com/dtrifuno.png","language":"Rust","readme":"# QuickDiv\n\n[![Latest Release]][crates.io] [![Documentation]][docs.rs] ![Minimum Supported Rust Version 1.54]\n\n[Latest Release]: https://img.shields.io/crates/v/quickdiv.svg\n[crates.io]: https://crates.io/crates/quickdiv\n[Documentation]: https://docs.rs/quickdiv/badge.svg\n[docs.rs]: https://docs.rs/quickdiv/\n[Minimum Supported Rust Version 1.54]: https://img.shields.io/badge/MSRV-1.54-blue.svg\n\nQuickDiv is a Rust crate that allows you to speed up repeated division and\nmodulo operations by the same divisor, based on the\n[libdivide C/C++ library](https://libdivide.com/).\n\nOn most hardware today integer division operations take longer to execute\ncompared to operations like multiplication and addition. Because of this,\ncompilers generally optimize division by a constant, by replacing it with a\ncheaper sequence of shifts, multiplications and additions. This crate lets you\napply a similar algorithm to optimize division by values that are only known at\nruntime.\n\nPerformance gains will vary between platforms, CPUs, and integer widths, but you\ncan expect dividing an integer by a precomputed divisor to be somewhere between\n2 to 10 times faster compared to the built-in hardware division method. Note\nthat preparing the divisor is more expensive than a single unoptimized\ndivision: it will take at least 2 divisions by the same divisor to break even.\n\nThis crate supports primitive integer types of all widths, in both signed and\nunsigned variants. It requires Rust version 1.54 or greater. It is `#![no_std]`\nand `#![forbid(unsafe_code)]`.\n\n## Example\n\n```rust\nuse quickdiv::DivisorU64;\n\nfn is_quadratic_residue(q: u64, modulus: u64) -\u003e bool {\n    // Initializing a divisor is more expensive than a single\n    // unoptimized division, to gain a benefit you must divide\n    // multiple times by the same divisor.\n    let modulus = DivisorU64::new(modulus);\n\n    // The original value can be recovered by using ::get().\n    for x in (0..modulus.get()) {\n        // A divisor can be used as the second operand with\n        // the / and % operators.\n        if (x * x) % modulus == q {\n            return true;\n        }\n    }\n\n    false\n}\n\nassert!(is_quadratic_residue(152, 169));\nassert!(!is_quadratic_residue(51, 111));\n```\n\n## Performance\n\nThe following benchmarks should give a rough sense of the kind of speed-up you\ncan expect. The numbers represent throughput in millions of elements per second\n(larger is better) on various tasks.\nFor `Quotient Sum` we divide a collection of random integers by a fixed\ndivisor, in `LCG` we compute the remainder given a fixed modulus\nand, finally, in `FizzBuzz` we check the divisibility of random integers by a\nfixed divisor.\n\n![](https://github.com/dtrifuno/quickdiv/blob/main/benchmarks/graph.png?raw=true)\n\n| Task         | CPU   | Compiler | QuickDiv |\n| ------------ | ----- | -------- | -------- |\n| Quotient Sum | 248.3 | 838      | 823.5    |\n| LCG          | 168.8 | 252.7    | 255      |\n| FizzBuzz     | 41.13 | 1350     | 556      |\n\nNote that while QuickDiv computes the remainder and checks if its\nzero, the compiler uses a different method to directly check divisibility,\nleading to faster performance on the `FizzBuzz` task.\n\n### Caveats\n\n- These results are for `u64` only. Performance can vary with width and\n  signedness.\n- Benchmarks were run on an AMD Ryzen 5 2600 CPU (i.e. an older x86-64 CPU).\n  Some newer high-end processors like the Apple M1/M2 have very fast hardware\n  division, and will experience a less dramatic speed-up.\n- All tasks involved at least 1000 repeated uses of the same divisor, making\n  branch prediction trivial. You will experience worse performance if you are\n  instead iterating over a collection of different divisors. If you want to\n  read more about this, check out\n  [Paul Khuong's post](https://pvk.ca/Blog/2021/05/14/baseline-implementations-should-be-predictable/)\n  about his branchfree Rust integer division library\n  [Reciprocal](https://crates.io/crates/reciprocal).\n\nIf you would like to run these benchmarks yourself, check out the [`benchmarks`\ncrate](https://github.com/dtrifuno/quickdiv/tree/main/benchmarks) in the GitHub\nrepository.\n\n## License\n\nLicensed under any of:\n\n- Apache License, Version 2.0, ([LICENSE-APACHE](https://raw.githubusercontent.com/dtrifuno/quickdiv/main/LICENSE-APACHE) or \u003chttps://www.apache.org/licenses/LICENSE-2.0\u003e)\n- MIT license ([LICENSE-MIT](https://raw.githubusercontent.com/dtrifuno/quickdiv/main/LICENSE-MIT) or \u003chttps://opensource.org/licenses/MIT\u003e)\n- zlib License ([LICENSE-ZLIB](https://raw.githubusercontent.com/dtrifuno/quickdiv/main/LICENSE-ZLIB) or \u003chttps://opensource.org/license/zlib/\u003e)\n\nby your choice.\n\n### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall\nbe multi-licensed as above, without any additional terms or conditions.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtrifuno%2Fquickdiv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdtrifuno%2Fquickdiv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtrifuno%2Fquickdiv/lists"}