mpz_set_si loses long long value on Windows
Dim XY
dim0071 at gmail.com
Wed Jul 24 07:35:56 UTC 2019
Hello,
I'd like to report a problem with mpz_set_si on Windows 64 (Mingw)
GMP version: 6.1.2
Platform: minw32 on linux ubuntu 18
the program to check:
test-mpz.cpp:
--------------------
#include <stdint.h>
#include <stdio.h>
#include <gmp.h>
int main() {
mpz_t mpz_a; int64_t a;
int64_t b = 71000000000;
mpz_init(mpz_a);
printf("mpz_a->_mp_size = %d\n",mpz_a->_mp_size);
mpz_set_si(mpz_a,b);
printf("mpz_a->_mp_size = %d\n",mpz_a->_mp_size);
a = mpz_get_si(mpz_a);
#if defined(_WINDOWS) || defined(_WIN32)
printf("a=%lli",a);
#else
printf("a=%li",a);
#endif
return 0;
}
-------------
How to build:
On mingw:
x86_64-w64-mingw32-gcc -I <path-to-mingw32>/x86_64-w64-mingw32/include
libgmp test-mpz.cpp
----------
What's wrong:
On Linux result will be following:
mpz_a->_mp_size = 0
mpz_a->_mp_size = 1
a=71000000000
And on Windows:
mpz_a->_mp_size = 0
mpz_a->_mp_size = -1
a=-2014444032 // <----- incorrect, lost value!!
-------------
Possible explanation:
mpz_set_si function has the signature:
void mpz_set_si (mpz_ptr dest, signed long int val);
This 'signed long int' type has different sizes of linux (8 bytes) and
windows 64bit (4 bytes) platforms
(https://en.cppreference.com/w/cpp/language/types)
Maybe it is better to use 'long long' type which always has the size
of 8 bytes on any platform?
Kindest
Dmitriy Menshchikov
Komodo platform developer
More information about the gmp-bugs
mailing list