mini-gmp mpz_{get,set}_d not fully compatible with GMP

Torbjörn Granlund tg at
Tue Mar 13 12:55:27 UTC 2018

Vincent Lefevre <vincent at> writes:

  On 2018-03-12 18:53:37 +0100, Marco Bodrato wrote:
  > double
  > mpz_get_d (const mpz_t u)
  > {
  >   static int c = 0;
  >   static int __initialized = 0;
  >   if (__initialized != 1) {
  >     c = gmp_tests_dbl_mant_bits ();
  >     *((volatile int *) &__initialized) = 1;
  >   }

  FYI, this is not thread-safe.

I suppose the externally visible store order is not guaranteed to be the
same as the program store order, not even with "volatile".

One variable, declared as volatile, should work fine though.  Of course,
that assumes some value for the variable can be used to represent a
non-initialised state.  For c above, 0 seems to be a useable value as no
floating-point format with zero mantissa bits has yet been invented.

Please encrypt, key id 0xC8601622

More information about the gmp-devel mailing list