Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dkosmari/libxint
libxint is a C++20 headers-only library for fixed-width large integers.
https://github.com/dkosmari/libxint
cpp cpp20
Last synced: 5 days ago
JSON representation
libxint is a C++20 headers-only library for fixed-width large integers.
- Host: GitHub
- URL: https://github.com/dkosmari/libxint
- Owner: dkosmari
- License: gpl-3.0
- Created: 2023-03-20T14:32:49.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-01T03:20:26.000Z (8 months ago)
- Last Synced: 2024-04-02T03:50:55.244Z (8 months ago)
- Topics: cpp, cpp20
- Language: C++
- Homepage:
- Size: 485 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog
- License: COPYING
- Authors: AUTHORS
Awesome Lists containing this project
README
libxint - a C++ library for extended size unsigned integers
===========================================================This is a headers-only C++20 library to emulate large fixed-size integers.
It's licensed under the GPLv3+ license.Installation
------------When building from a tarball, you can skip step 0.
0. `./bootstrap`
1. `./configure`
2. `make`
3. `sudo make install`
To uninstall:
sudo make uninstall
This is a standard Automake package; run `./configure --help` or check `INSTALL` for more
details.Usage
-----First, include the header `libxint/uint.hpp` in your program.
The type `xint::uint` is now available; the template argument `B` is the number
of desired bits. It can be used as any ordinary unsigned integer:```cpp
#include
#includeusing namespace std;
using namespace xint::literals;int main()
{
xint::uint<1024> x = 42;
cout << x << endl;
x <<= 1000;
cout << x << endl;
++x;
cout << x << endl;
cout << popcount(x) << endl;
auto y = 0xdeadbeef0000babecafe0000dec0de_uint;
cout << hex << y << dec << " has " << y.num_bits << " bits." << endl;
}
```Implementation details
----------------------This extended integer is implemented through multiple native integers, called limbs.
The size of each limb can be controlled by defining the `XINT_LIMB_SIZE` macro. Valid
sizes are 8, 16 and 32 (default is 32.) For instance:g++ -std=c++20 -DXINT_LIMB_SIZE=16 my-program.cpp
The limbs can be accessed directly through the `limbs()` and `limb(i)` member functions.
NOTE: it's not possible to specify a bit size that is not a multiple of the limb size. No
attempt is made to mask out the "excess" bits (for performance reasons) so the results of
all operations will be inconsistent. A `static_assert()` exists to prevent invalid limb
sizes.