Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest 128 bit integer library [closed]

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?

like image 399
Eugene Smith Avatar asked Sep 11 '10 20:09

Eugene Smith


People also ask

Is there a 128-bit integer limit?

128-bit refers to one hundred twenty-eight binary (0 or 1) units of integer data. This allows for up to 340,282,366,920,938,463,463,374,607,431,768,211,456 combinations of values.

What is the largest 128-bit number?

A 128-bit register can store 2128 (over 3.40 × 1038) different values. The range of integer values that can be stored in 128 bits depends on the integer representation used.

How long is a 128-bit integer?

The 128-bit data type can handle up to 31 significant digits (compared to 17 handled by the 64-bit long double). However, while this data type can store numbers with more precision than the 64-bit data type, it does not store numbers of greater magnitude.

How do you write a 128-bit integer?

Simply write __int128 for a signed 128-bit integer, or unsigned __int128 for an unsigned 128-bit integer. There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.


3 Answers

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.

like image 143
Jens Gustedt Avatar answered Oct 23 '22 12:10

Jens Gustedt


The ttmath library does what you want.

like image 32
rafak Avatar answered Oct 23 '22 13:10

rafak


This might not be for everyone, but what I would do is pick the highest-performance arbitrary integer library with source code and otherwise suitable for the job, and hack it to be for fixed integer sizes. Change some variable "nbits" to 128 hard-coded. It probably allocates memory at runtime, not knowing the number of bytes until then. Change it to use struct with data in-place, saving a pointer dereferencing every time data is read. Unroll certain critical loops by hand. Hard-code anything else that might be critical. Then the compiler will probaby have an easier time optimizing things. Of course, much of this will be assembly, using fancy SIMD with whatever the technology is in use this week.

It would be fun! But then, as a programmer I started off with machine code and very low level stuff.

But for those not as crazy as I am, perhaps one of the available libraries uses templates or has some means of generating code custom to some size. And, some compilers have a "long long" integer type which might be suitable.

like image 1
DarenW Avatar answered Oct 23 '22 12:10

DarenW