Search code examples
memorygmp

GMP integer size limit reached?


I have written an implementation of Toom multiplication with GMP. I tested multiplication of 2 numbers of around 7000 digits (23,000 bits) and it worked perfectly. However when I tried it with 70000 digits (230,000 bits) the program started producing wrong answers.

I am using a 32 bit system. Could GMP run out of memory to use? I did not get any error so I think this is unlikely. If this is not the case I suspect some loss of precision somewhere.


Solution

  • In general GMP allocates data on heap with "malloc and friends". If data cannot be allocated properly, then it invokes standard abort() function (so process should be simply aborted), see memory.c:

    ret = malloc (size);
    if (ret == 0)
      {
        fprintf (stderr, "GNU MP: Cannot allocate memory (size=%lu)\n", (long) size);
        abort ();
      }
    

    It's rather impossible to tell what went wrong in your calculations, without taking much more details (see my comment).