post-scarcity/notes/bignums.md
Simon Brooke d9d789fdd0 Now creating the correct internal bignum representation
add_integers returns an integer which by inspection of the internal representation is correct, but the print representation is not correct.
2019-01-03 11:21:08 +00:00

815 B

All integers are potentially bignums

Each integer comprises at least one cell of type INTR, holding a signed 64 bit integer with a value in the range 0 ... MAX-INTEGER, where the actual value of MAX-INTEGER does not need to be the same as the C language LONG_MAX, provided that it is less than this. It seems to me that a convenient number would be the largest number less than LONG_MAX which has all bits set

LONG_MAX is 0x7FFFFFFFFFFFFFFF, so the number we're looking for is 0x0FFFFFFFFFFFFFFF, which is 1,152,921,504,606,846,975, which is 2^60 - 1. This means we can use bit masking with 0xFFFFFFFFFFFFFFF to extract the part of int64_t which will fit in a single cell.

It also means that if we multiply two int64_ts into an __int128_t, we can then right-shift by 60 places to get the carry.