Big Integer implementation, optimized for small numbers

57 Views Asked by At

I cannot seem to find much info on how to optimize any kind of bignum to work fast on small numbers. Big integers are usefull sometimes, when the numbers could be big. But most often the numbers will be small enough to fit in int64, only rarely exceeding it. It seems wasteful to severely slow down small int computations for the sake of a few, rare use cases.

From what I gather, often used solution is to inline small ints on stack, like

union BigInt {
  int64 small_int;
  ReallyBigInt* really_big_int;
}

but it mostly saves on memory, still any arithmetic operation needs to check which variant is in use and branch accordingly - seems orders of magnitude slower than simple additon of two registers.

Are there any known methods to solve the problem?

0

There are 0 best solutions below