{"id":17724775,"url":"https://github.com/jibsen/wideint","last_synced_at":"2025-03-14T05:32:07.580Z","repository":{"id":72895662,"uuid":"451633559","full_name":"jibsen/wideint","owner":"jibsen","description":"C++ implementation of wide exact-width integer types","archived":false,"fork":false,"pushed_at":"2024-03-17T11:11:35.000Z","size":351,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-03-18T11:47:54.664Z","etag":null,"topics":["cpp","uint128","uint256"],"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/jibsen.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":"2022-01-24T21:08:19.000Z","updated_at":"2024-03-18T11:47:54.664Z","dependencies_parsed_at":null,"dependency_job_id":"36f49564-08fc-423b-bb5b-37ad2049856f","html_url":"https://github.com/jibsen/wideint","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jibsen%2Fwideint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jibsen%2Fwideint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jibsen%2Fwideint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jibsen%2Fwideint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jibsen","download_url":"https://codeload.github.com/jibsen/wideint/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243532517,"owners_count":20306151,"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":["cpp","uint128","uint256"],"created_at":"2024-10-25T15:48:44.133Z","updated_at":"2025-03-14T05:32:07.170Z","avatar_url":"https://github.com/jibsen.png","language":"C++","readme":"\nwideint - wide exact-width integer types\n========================================\n\nCopyright (c) 2022 Joergen Ibsen\n\n[![wideint CI](https://github.com/jibsen/wideint/actions/workflows/wideint-ci-workflow.yaml/badge.svg)](https://github.com/jibsen/wideint/actions)\n\nAbout\n-----\n\nwideint is a C++ implementation of wide *exact-width* integer types.\n\n~~~.cpp\n#include \u003ciostream\u003e\n#include \"wideint.hpp\"\n\nint main()\n{\n\t// Type alias for convenience\n\tusing uint128 = wideint::wuint\u003c4\u003e;\n\n\tconstexpr auto p = uint128(\"9223372036854775337\");\n\tconstexpr auto q = uint128(\"4611686018427387847\");\n\n\t// Print product (42535295865117305235085505148949129439)\n\tstd::cout \u003c\u003c p * q \u003c\u003c '\\n';\n}\n~~~\n\nWhile doing [Advent of Code](https://adventofcode.com/) to pick up some\nC++20, I came across a problem where I wanted to do a computation on values\nlarger than what fits in an `unsigned long long`. The C++ standard library\ndoes not include a big integer type yet, so I would either have to use one\nof the stable, well-tested, highly efficient libraries available, or write\nmy own ad hoc solution, and hopefully learn a few things along the way.\n\n\nCaveat\n------\n\nwideint has not been thoroughly tested, and some of the algorithms used may\nbe slow compared to what you would get with one of the established big number\nlibraries. Also, being exact-width and stack-allocated means it is suitable\nfor bounded computations on relatively small values.\n\n\nRequirements\n------------\n\nwideint requires `std::uint32_t` and `std::uint64_t` to be available. It\nuses some C++20 features which may not be available in all compilers.\n\n\nDetails\n-------\n\nA wideint stores its value in a `std::array` of `std::uint32_t`. It takes\nthe size of this array as a template argument. So a `wuint\u003c3\u003e`, for example,\nstores a 96-bit value, while a `wuint\u003c4\u003e` stores a 128-bit value.\n\nWhile it is possible to create a wideint with 32- or 64 bits, use the\nbuilt-in types `std::uint32_t` and `std::uint64_t` instead.\n\nThere is no implicit conversion from `std::uint32_t` or `std::string_view`\nto wideint. If you want to initialize a wideint with either, you will have\nto do so explicitly:\n\n~~~.cpp\n// This does not compile, there is no implicit conversion\n// uint128 a = 5;\n\n// Use either of these instead\nuint128 b(5);\nuint128 c = uint128(5);\nauto d = uint128(5);\n~~~\n\nAs special cases, modulus (`wuint % uint32_t`) and bitwise AND\n(`wuint \u0026 uint32_t`) with a `std::uint32_t` on the right return a\n`std::uint32_t` instead of a `wuint`.\n\nMost operations are `constexpr`.\n\n\nFunctionality\n-------------\n\nBesides the usual arithmetic and comparison operators, the following is\navailable.\n\nFor both signed and unsigned wideints:\n  - `is_zero` and `is_negative` member functions\n  - `getbit` and `setbit` member functions\n  - `abs`\n  - `to_string`\n  - `from_chars` and `to_chars`\n  - specialization of `std::hash`\n\nFor unsigned wideints:\n  - `gcd`, `lcm`, and `sqrt`\n  - `has_single_bit`, `bit_ceil`, `bit_floor` `bit_width`, `countl_zero`,\n    `countl_one`, `countr_zero`, and `countr_one` analogous to the `\u003cbit\u003e`\n    header\n\n\nSigned values\n-------------\n\nThere is a signed wideint type, `wideint::wint`, which interprets the value\nit stores as a two's complement representation.\n\n~~~.cpp\n#include \u003ciostream\u003e\n#include \u003cutility\u003e\n#include \"wideint.hpp\"\n\ntemplate\u003ctypename T\u003e\nconstexpr T gcd_euclid(const T \u0026x, const T \u0026y)\n{\n\tauto a = x;\n\tauto b = y;\n\n\twhile (b != 0) {\n\t\ta = std::exchange(b, a % b);\n\t}\n\n\treturn abs(a);\n}\n\nint main()\n{\n\t// Type alias for convenience\n\tusing int128 = wideint::wint\u003c4\u003e;\n\n\t// Three large primes\n\tconstexpr auto p = int128(\"9223372036854775337\");\n\tconstexpr auto q = int128(\"4611686018427387847\");\n\tconstexpr auto r = int128(\"2305843009213693907\");\n\n\t// Print GCD of p * r and -q * r (2305843009213693907)\n\tstd::cout \u003c\u003c gcd_euclid(p * r, -q * r) \u003c\u003c '\\n';\n}\n~~~\n\n\nOutput\n------\n\nThere is a `to_string()` function that converts a wideint to a `std::string`.\n\nThere are operator overloads for stream input and output. You can disable\nthese by defining `WIDEINT_NO_IOSTREAMS`.\n\n\nAlternatives\n------------\n\n  - [Abseil Numerics](https://abseil.io/docs/cpp/guides/numeric)\n  - [Boost.Multiprecision](https://www.boost.org/doc/libs/release/libs/multiprecision/)\n  - [GMP](https://gmplib.org/)\n  - [InfInt](https://github.com/sercantutar/infint)\n  - [mp++](https://github.com/bluescarni/mppp)\n  - [MPIR](https://github.com/wbhart/mpir)\n  - [wide-integer](https://github.com/ckormanyos/wide-integer)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjibsen%2Fwideint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjibsen%2Fwideint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjibsen%2Fwideint/lists"}