{"id":13629969,"url":"https://github.com/lemire/fastmod","last_synced_at":"2025-04-05T23:10:08.555Z","repository":{"id":37413169,"uuid":"169750512","full_name":"lemire/fastmod","owner":"lemire","description":"A C/C++ header file for fast 32-bit division remainders (and divisibility tests) on 64-bit hardware.","archived":false,"fork":false,"pushed_at":"2024-07-29T19:09:39.000Z","size":144,"stargazers_count":302,"open_issues_count":0,"forks_count":27,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-10-12T21:28:16.534Z","etag":null,"topics":["performance"],"latest_commit_sha":null,"homepage":"","language":"C++","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/lemire.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2019-02-08T14:54:33.000Z","updated_at":"2024-10-09T21:39:23.000Z","dependencies_parsed_at":"2024-02-11T23:32:12.955Z","dependency_job_id":"e14ae47c-1249-4669-8dbf-fd8a11615c44","html_url":"https://github.com/lemire/fastmod","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2Ffastmod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2Ffastmod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2Ffastmod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2Ffastmod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lemire","download_url":"https://codeload.github.com/lemire/fastmod/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411235,"owners_count":20934653,"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":["performance"],"created_at":"2024-08-01T22:01:25.984Z","updated_at":"2025-04-05T23:10:08.533Z","avatar_url":"https://github.com/lemire.png","language":"C++","readme":"# fastmod\n[![Ubuntu 22.04 CI (GCC 11)](https://github.com/lemire/fastmod/actions/workflows/ubuntu22.yml/badge.svg)](https://github.com/lemire/fastmod/actions/workflows/ubuntu22.yml)\n[![VS17-CI](https://github.com/lemire/fastmod/actions/workflows/vs17.yml/badge.svg)](https://github.com/lemire/fastmod/actions/workflows/vs17.yml)\n\nA header file for fast 32-bit division remainders  on 64-bit hardware.\n\nHow fast? Faster than your compiler can do it!\n\nCompilers cleverly replace divisions by multiplications and shifts, if the divisor is known at compile time. In a hashing benchmark, our simple C code can beat  state-of-the-art compilers (e.g., LLVM clang, GNU GCC) on a recent Intel processor (Skylake).\n\n\u003cimg src=\"docs/hashbenches-skylake-clang.png\" width=\"90%\"\u003e\n\nFurther reading:\n\n- [Faster Remainder by Direct Computation: Applications to Compilers and Software Libraries](https://arxiv.org/abs/1902.01961), Software: Practice and Experience  49 (6), 2019.\n\n\n##  Usage\n\nWe support all major compilers (LLVM's clang, GNU GCC, Visual Studio). This library only makes sense when compiling 64-bit binaries.\n\nIt is a header-only library but we have unit tests. Assuming a Linux/macOS setting:\n\n```\nmake\n./unit\n```\n\nThe tests are exhaustive and take some time.\n\nYou can also build the tests using cmake which will work nearly everywhere (including under Windows).\n\n```\ncmake -B build \n```\n\nTo enable the exhaustive tests, do...\n\n```\ncmake -B build -D FASTMOD_EXHAUSTIVE_TESTS=ON\n```\n\nUnder Windows, you can run tests as follows:\n\n```\ncmake --build build --config Release\ncd build\nctest . --config Release\n```\n\nUsers of Visual Studio need to compile to a 64-bit binary, typically by selecting x64 or ARM64 in the build settings. Visual Studio should default on 64-bit builds on 64-bit systems.\n\n\n##  Code samples\n\nIn C, you can use the header as follows.\n\n```C\n#include \"fastmod.h\"\n\n// unsigned...\n\nuint32_t d = ... ; // divisor, should be non-zero\nuint64_t M = computeM_u32(d); // do once\n\nfastmod_u32(a,M,d);// is a % d for all 32-bit unsigned values a.\n\nfastdiv_u32(a,M);// is a / d for all 32-bit unsigned values a, d\u003e1.\n\n\nis_divisible(a,M);// tells you if a is divisible by d\n\n// signed...\n\nint32_t d = ... ; // should be non-zero and between [-2147483647,2147483647]\nint32_t positive_d = d \u003c 0 ? -d : d; // absolute value\nuint64_t M = computeM_s32(d); // do once\n\nfastmod_s32(a,M,positive_d);// is a % d for all 32-bit a\n\nfastdiv_s32(a,M,d);// is a / d for all 32-bit a,  d must not be one of -1, 1, or -2147483648\n\n```\n\nIn C++, it is much the same except that every function is in the `fastmod` namespace so you need to prefix the calls with `fastmod::` (e.g., `fastmod::is_divisible`).\n\n\n## Go version\n\n* There is a Go version of this library: https://github.com/bmkessler/fastdiv\n\n\n### (Speculative work) 64-bit benchmark\n\nIt is an open problem to derive 64-bit divisions that are faster than what the compiler can produce for constant divisors.\nFor comparisons to native `%` and `/` operations, as well as bitmasks, we have provided a benchmark with 64-bit div/mod. You can compile these benchmarks with `make benchmark`.\nThese require C++11. It is not currently supported under Visual Studio.\n\n","funding_links":[],"categories":["Math","C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemire%2Ffastmod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemire%2Ffastmod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemire%2Ffastmod/lists"}