How to use GMP.

Hashida jhashida@fls.fujitsu.com
Thu, 08 May 2003 12:08:48 +0900


Hi Kevin Ryde.

I rewrote the program.


Kevin Ryde wrote:

> Hashida <jhashida@fls.fujitsu.com> writes:
> >
> >         MP_INT *bbbbb=(MP_INT *)malloc(50);
>
> You should use sizeof(MP_INT) if you really want to do it with malloc.
>
> >         bbbbb->_mp_d=(mp_limb_t *)malloc(10);
>
> No, gmp manages the space at _mp_d, mpz_init and friends will set it.

As mpz_init_set_ui is used, "malloc" is unnecessary and is removed.

>
>
> > aaaaa._mp_alloc=2
> > aaaaa._mp_size=2
> > *(aaaaa._mp_d)=2147483645
>
> This is correct, but you need to look at _mp_d[1] too, since the value
> is two limbs of 32 bits.  This is all internals though, so if you want
> to be compatible with future releases, don't do it.

I got "*(aaaaa._mp_d+1)=1".

    aaaaa._mp_alloc=2
    aaaaa._mp_size=2
    *(aaaaa._mp_d)=2147483645
    *(aaaaa._mp_d+1)=1

How do the above four results express "(2^31-1)*3=6442450941"?
Though, if "gmp_printf("aaaaa=%Zd\n",&aaaaa);" is used,
"aaaaa=6442450941" is expressed.

In addition, what does "_mp_alloc" mean?
Is "aaaaa._mp_alloc" 2 because "aaaaa" is used twice in mpz_*** ?
(set 1 in "mpz_init" and set 2 in "mpz_mul_ui")

Thanks.

Hashida


#include <stdio.h>
#include <gmp.h>
#include <stdarg.h>
#include <obstack.h>

main()
{
        MP_INT *bbbbb;
        MP_INT aaaaa;

        mpz_init_set_ui(bbbbb,2147483647);
        printf("bbbbb->_mp_alloc=%d\n",bbbbb->_mp_alloc);
        printf("bbbbb->_mp_size=%d\n",bbbbb->_mp_size);
        printf("*(bbbbb->_mp_d)=%d\n",*(bbbbb->_mp_d));
        mpz_init(&aaaaa);
        mpz_mul_ui(&aaaaa, bbbbb, 3);
        printf("aaaaa._mp_alloc=%d\n",aaaaa._mp_alloc);
        printf("aaaaa._mp_size=%d\n",aaaaa._mp_size);
        printf("*(aaaaa._mp_d)=%d\n",*(aaaaa._mp_d));
        printf("*(aaaaa._mp_d+1)=%d\n",*(aaaaa._mp_d+1));
        gmp_printf("aaaaa=%Zd\n",&aaaaa);

}