It depends on what you do with the numbers. You can compensate for a small loss in space efficiency and a small loss in multipoint arithmetic in exchange for a very efficient decimal and decimal conversion. The key is to do multiprecision arithmetic with a base that has a power of 10 rather than a power of 2.
For example, you can use a base of 10,000, where you pack one digit into a 16-bit word, and you do your arithmetic on digits in 32-bit integers. (If you are on a 64-bit machine, you can double this base and make the base 1,000,000,000.) This kind of code is relatively effective with a time interval, although not as fast as using your own power in two, because you cannot Take advantage of the carry bit on hardware. And you cannot represent as many integers in the same number of bits. But this is a whistle when converting to and from a decimal number, because you can convert individual digits without any long separation.
If you need to represent the full range of numbers from zero to ((1 << 128) - 1) , you can still do this, but add an extra digit so that your numbers are larger.
If you really need extra space / speed (maybe you do a lot of cryptographic 128-bit computing), then the 10 / div / mod simultaneous method is the fastest method I know. The only trick is that if small integers are common, you can deal with them on purpose. (That is, if the three most significant 32-bit words are zero, just use your own division for conversion.)
Is there a smart way to implement this feature that I don't see?
Dave Hanson C Interfaces and Implementations contains a long chapter on multipoint arithmetic. Dividing a large number into one digit is a special case that has this effective implementation:
int XP_quotient(int n, T z, T x, int y) { int i; unsigned carry = 0; for (i = n - 1; i >= 0; i--) { carry = carry*BASE + x[i]; z[i] = carry/y; carry %= y; } return carry; }
For a complete understanding, it really helps to have a book, but the source code is still much easier to understand than the GNU source code. And you can easily adapt it to use the base of 10,000 (it currently uses the base of 256).
Summary: if your performance bottleneck is converting to decimal, do multiple-value arithmetic with a base that has a power of 10 . If your computer’s native word size is 32 and you use C code, use £ 10,000 in the 16-bit word.