overflow in mpz type with a simple mpz_add_ui

Vincent Lefevre vincent at vinc17.net
Mon Sep 21 15:48:05 UTC 2020

With GMP 6.2.0 (Debian's package libgmp10:i386) on x86_64 in the
32-bit ABI, i.e. when compiling with -m32, I get the following

cventin:~> cat tst.c
#include <stdio.h>
#include <gmp.h>

int main (void)
  mpz_t z;

  mpz_init_set_ui (z, 17);
  mpz_mul_2exp (z, z, 0xffffffbc);
  printf ("OK\n");
  mpz_add_ui (z, z, 1);

  return 0;
cventin:~> gcc -m32 tst.c -o tst -lgmp
cventin:~> ./tst
gmp: overflow in mpz type

So the overflow occurs in "mpz_add_ui (z, z, 1);", though this
operation doesn't need a larger mpz_t and could even be done
in place: no carry occurs here, and note that a carry for a
huge number is very unlikely to occur.

Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

More information about the gmp-bugs mailing list