{"id":16370176,"url":"https://github.com/mrdcvlsc/extended-precision-integers","last_synced_at":"2026-02-05T07:32:04.200Z","repository":{"id":79335796,"uuid":"601468957","full_name":"mrdcvlsc/extended-precision-integers","owner":"mrdcvlsc","description":"A templated C++ library for big integers and large floating point numbers.","archived":false,"fork":false,"pushed_at":"2024-06-10T10:25:24.000Z","size":265,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-22T13:44:05.164Z","etag":null,"topics":["arbitrary-precision","arbitrary-precision-arithmetic","arbitrary-precision-integer","big-int","big-integer","big-num","big-number","bigint","biginteger","bignum","bignumber","compiletime","constexpr","cplusplus","cpp","cryptography","extended-precision-integer","large-number","library","math"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrdcvlsc.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":"2023-02-14T06:01:39.000Z","updated_at":"2024-07-09T08:49:13.000Z","dependencies_parsed_at":"2024-05-30T07:48:31.052Z","dependency_job_id":null,"html_url":"https://github.com/mrdcvlsc/extended-precision-integers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mrdcvlsc/extended-precision-integers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdcvlsc%2Fextended-precision-integers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdcvlsc%2Fextended-precision-integers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdcvlsc%2Fextended-precision-integers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdcvlsc%2Fextended-precision-integers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrdcvlsc","download_url":"https://codeload.github.com/mrdcvlsc/extended-precision-integers/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdcvlsc%2Fextended-precision-integers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29115537,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-05T05:31:32.482Z","status":"ssl_error","status_checked_at":"2026-02-05T05:31:29.075Z","response_time":65,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["arbitrary-precision","arbitrary-precision-arithmetic","arbitrary-precision-integer","big-int","big-integer","big-num","big-number","bigint","biginteger","bignum","bignumber","compiletime","constexpr","cplusplus","cpp","cryptography","extended-precision-integer","large-number","library","math"],"created_at":"2024-10-11T03:04:08.597Z","updated_at":"2026-02-05T07:32:04.172Z","avatar_url":"https://github.com/mrdcvlsc.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# extended-precision-integers\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)\n![tests](https://github.com/mrdcvlsc/extended-precision-integers/actions/workflows/tests.yml/badge.svg)\n![ctests](https://github.com/mrdcvlsc/extended-precision-integers/actions/workflows/ctests.yml/badge.svg)\n\nThis is a C++ header-only library that offers extended precision data types, which are number types with **fixed sizes** capable of holding larger values than traditional **Plain Old Data** types (PODs) like `int`, `uint8_t`, `unsigned long`, etc.\n\n## **Example Usage**\n\nThis library provides default synthesized integer types such as `epi::uint128_t`, `epi::uint256_t`, `epi::uint320_t`, and so on. To utilize them, include the `epi.hpp` header file in your project. These types can be treated just like regular integral types, for instance, `unsigned int` or `unsigned short`.\n\n```c++\n// example.cpp - get 3^600\n#include \u003ciostream\u003e\n#include \"path/to/epi/include/epi/epi.hpp\"\n\nint main() {\n  epi::uint1024_t a = 3;\n  epi::uint1024_t b = a;\n\n  for (size_t i = 1; i \u003c 600; ++i) {\n    a *= b;\n  }\n\n  std::cout \u003c\u003c \"0x\" \u003c\u003c std::hex \u003c\u003c a \u003c\u003c '\\n';\n\n  return 0;\n}\n```\n\n\u003e [!NOTE]  \n\u003e To see all the implemented types and operations for each types, click and see the **TO-DO** list below.\n\n## **Assigning a Large Value**\n\nWhile assigning to the types provided by the library using literal integer types is generally acceptable, there are cases where the compiler might generate an error due to exceeding the maximum width of built-in POD/primitive types. For example:\n\n```c++\n// error: integer literal is too large to be\n// represented in any integer type\nepi::uint256_t AES256KEY = 123456789101112131415ULL;\n```\n\nIn such situations, the library offers an alternative way of initialization using the `std::string_view` constructor, allowing support for very long integer values.\n\n```c++\n// this will compile successfully\nepi::uint256_t AES256KEY(\"123456789101112131415\");\n```\n\n\u003e [!IMPORTANT]  \n\u003e However, keep in mind that extended-precision types will throw an error during initialization if the input `string_view` value exceeds the type's bit width.\n\n## **Assigning With Different Base Representation**\n\nIt's possible to assign values with different base representations using the appropriate pre-fix format indicators: `0b` for binary, `0o` for octal, and `0x` for hexadecimal.\n\n**Hexadecimal:**\n```c++\nepi::uint256_t AES256KEY(\"0x6b14e9f95da1aff57\");\n```\n\n**Octadecimal:**\n```c++\nepi::uint256_t AES256KEY(\"0o15305164771273206577527\");\n```\n\n**Binary:**\n```c++\nepi::uint256_t AES256KEY(\"0b1101011000101001110100111111001010111011010000110101111111101010111\");\n```\n\nAll of these initializations are equivalent to `epi::uint256_t AES256KEY(\"123456789101112131415\");` mentioned earlier.\n\n## **Default Library Types**\n\nThe library provides default types as shown below:\n\n| type | min | max |\n| --- | :---: | :--- |\n| `epi::uint128_t` | 0 | 340282366920938463463374607431768211455 |\n| `epi::uint192_t` | 0 | 6277101735386680763835789423207666416102355444464034512895 |\n| `epi::uint256_t` | 0 | 115792089237316195423570985008687907853269984665640564039457584007913129639935 |\n| `epi::uint320_t` | 0 | 2135987035920910082395021706169552114602704522356652769947041607822219725780640550022962086936575 |\n| `epi::uint512_t` | 0 | 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084095 |\n| `epi::uint1024_t` | 0 | 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137215 |\n\nThe maximum values are calculated using the formula $2^{\\text{bit-width}} - 1$.\n\nFor instance, the `unsigned int` type is 32 bits wide, so its maximum value is $2^{32}-1$, and the `unsigned short int` type is 16 bits wide, so its maximum value is $2^{16}-1$.\n\n\u003e [!NOTE]  \n\u003e For smaller integer types that has a width of the probably around **800** bits and below, most operations are as fast, or if not just a tiny-tiny bit slower than other optimized or heavily optimized libraries (except for division \u0026 modulo operations), to give you an idea you can look a this [micro-benchmark](https://github.com/mrdcvlsc/extended-precision-integers/blob/benchmarks/benchmark/benchmark-and-comparison-Linux-clang%2B%2B.md) (this was run on github action runners for the sake of comparison only). \n\n## **Creating A New Extended Precision Type**\n\nTo create a custom extended precision integer type, let's say a **2048**-bit wide integer, you need to specify the desired type by providing an appropriate **unsigned integer** type to the template arguments of the `epi::whole_number\u003climb_t, cast_t, bit_width\u003e` class. Here's an example:\n\n```c++\ntypedef epi::whole_number\u003cstd::uint32_t, std::uint64_t, 2048\u003e uint2048_t;\n\nint main() {\n  uint2048_t large_number = 0;\n}\n```\n\nIn the example above:\n- `std::uint32_t` as `limb_t` is an alias for `unsigned int`, a 32-bit wide integer.\n- `std::uint64_t` as `cast_t` is an alias for `unsigned long int`, a 64-bit wide integer in most systems.\n- `2048` as `bit_width` represents the desired width of the type (`uint2048_t`).\n\nThe requirement is that the `cast_t` parameter should have a bit width exactly 2 times the bit width of `limb_t`. In the example, 32 x 2 = 64.\n\n\u003e [!NOTE]  \n\u003e Wider POD/primitive type as an argument for the limb_t template parameter, will equate to better performance.\n\u003e Just **make sure that you are indeed passing a POD/primitive type** since it can also accept other types like shown below.\n\n```c++\ntypedef epi::whole_number\u003cepi::uint128_t, epi::uint256_t, 2048\u003e uint2048_t;\n```\n\n\u003e [!IMPORTANT]  \n\u003e Using another `whole_number` type, or other user defined types, is not recommended since it will affect the performance, it is much better to use your systems available primitive/POD types to get the maximum performance out of the `whole_number` class or any related `epi` classes.\n\n## **Compile Time Support (`constexpr`)**\n\nThis library also supports compile-time calculations using the `constexpr` keyword.\n\n\u003e [!IMPORTANT]  \n\u003e\n\u003e For very large value computations with `constexpr`, consider adjusting the `-fconstexpr-steps` to a higher value.\n\u003e\n\u003e MSVC 2019 might not support some `constexpr` operations.\n\n```c++\nconstexpr epi::uint512_t factorial(size_t N) {\n  epi::uint512_t factorial = 1;\n\n  for(epi::uint512_t i = 1; i \u003c= N; ++i) {\n    factorial *= i;\n  }\n\n  return factorial;\n}\n\nint main() {\n  constexpr auto fac98 = factorial(98);\n  std::cout \u003c\u003c std::hex \u003c\u003c fac98 \u003c\u003c \"\\n\";\n}\n\n// OUTPUT:\n// b3fdae4141a01eb87d7eef7be85b2081adb3d8455d37d503f972677dba3450455447b6f366c6e85568b196e3bb65397be2e31f13800000000000000000000000\n```\n\n\u003e [!WARNING]  \n\u003e Be aware that **large/long calculations** at **compile time** could significantly **increase compilation duration** and **resource usage**, possibly leading to errors. So for extremely large calculations, it's recommended not to use `constexpr`.\n\n## **TO-DO**\n\nClick the links below to view what has already been implemented in the library. Additionally, there are numerous completed implementations that have not yet received optimization efforts and still require further work.\n\n- [X] - [Extended Unsigned Integers Operations](TODO/extended-unsigned-integers.md)\n- [ ] - [Extended Signed Integers Operations [N/A]](TODO/extended-signed-integers.md)\n- [ ] - [Extended Floating Point Operations [N/A]](TODO/extended-floating-point.md)\n\n## **Requirements**\n- Little Endian System\n- C++20 or Above\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrdcvlsc%2Fextended-precision-integers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrdcvlsc%2Fextended-precision-integers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrdcvlsc%2Fextended-precision-integers/lists"}