Search code examples
c++cbigintegernumerical

Fastest 128 bit integer library


I am working on a CPU-heavy numerical computation app. Without going into many details, it's a computational math research project that involves computing a certain function f(x) for large integer x.

Right now everything is implemented in C++ in x64 mode, using native 64-bit ints. That limits me to x<2^64~1.8*10^19. I want to go further, to do that, I need a library that does 128-bit arithmetic. And it has to be very fast. In particular, integer divisions should be fast. Otherwise I'll be sitting here waiting for the results till Thanksgiving. And I'd rather not reinvent the wheel.

I found a list of ~20 big integer libraries on Wikipedia, but most of those seem to be targeted towards arbitrary-precision numbers, which is overkill for my task, and I don't need extra costs associated with that.

Does anyone know what library can operate on 128 bit integers fastest?


Solution

  • You didn't mention your platform / portability requirements. If you are willing to use gcc or clang, on 64 bit platforms they have a builtin 128 bit types that come for free, __uint128_t and __int128_t. Maybe other platforms have similar type extensions.

    In any case it should be possible to find the corresponding generic code in the gcc sources that assembles two integers of width N to synthesize one integer of width 2N. This would probably be a good starting point to make a standalone library for that purpose.