A simple BigInteger library written in C (and lots of inline Assembly).
- Addition
- Subtraction (underflows are caught and return 0)
- Comparison
- Bitshifts
- Serialization to hex
- Creation from hex string
- Multiplication
- Integer division + Modulo
- Header amalgamation into
bigint.h
sammy relies heavily on inline assembly for most of its operations (specifically, x86_64 assembly). As such, sammy is not compatible with any other platform. Future support for arm64 may be considered once the library has reached a sufficient level of completion :)
I wrote this library to learn and have fun! If you want real performance, please just use the GNU Multiple Precision math library instead.
With very large integers, memory becomes a concern. sammy normally calls abort()
upon a memory allocation issue. However, there is an option to use a custom
allocator that will try its best to allocate memory for sammy, even in low-
memory conditions. Please see include/customization.h
to see how to change
the default allocators.
A bigint is an array of 64-bit unsigned integers. On x86_64, ints are stored in little endian, and the array of integers is also in little endian - least significant int first, most significant int last. One 64-bit unsigned integer will be referred to as a "block" or "qword" throughout the code base (to avoid ambiguity with "word"s in assembly, which are not 64-bits).
sammy as itself is not very useful due to it being restricted purely to unsigned integers. However, I'm also working on another library which uses sammy to manipulate integers, but is capable of representing all rational numbers (and approximates for real numbers). Check it out here!
- This x86 ISR - for listing lots of the instructions I needed, and the parameters for each!
- Wolfram Cloud - so I could test to see if the numbers were actually correct
- Valgrind - for debugging those weird segfaults I had
- gdb - for more debugging when I had issues with my assembly code