Using MPZ_ROINIT_N in mpz sources

Marco Bodrato bodrato at mail.dm.unipi.it
Sun Nov 27 07:48:30 UTC 2016


Ciao,

I do not remember if we require C99 to compile the library...

Can we use MPZ_ROINIT_N inside our sources to improve readability? e.g.

*** /tmp/mpz/gcdext.c	2016-11-27 08:23:37.281277360 +0100
--- /gmp-repo/mpz/gcdext.c	2016-11-27 08:18:22.512770938 +0100
***************
*** 91,113 ****
    ssize = ABS (tmp_ssize);
    tmp_ssize = SIZ (a) >= 0 ? tmp_ssize : -tmp_ssize;

    if (t != NULL)
      {
        mpz_t x;
!       __mpz_struct gtmp, stmp;
!
!       PTR (&gtmp) = tmp_gp;
!       SIZ (&gtmp) = gsize;
!
!       PTR (&stmp) = tmp_sp;
!       SIZ (&stmp) = tmp_ssize;

        MPZ_TMP_INIT (x, ssize + asize + 1);
!       mpz_mul (x, &stmp, a);
!       mpz_sub (x, &gtmp, x);
        mpz_divexact (t, x, b);
      }

    if (s != NULL)
      {
        mp_ptr sp;
--- 91,108 ----
    ssize = ABS (tmp_ssize);
    tmp_ssize = SIZ (a) >= 0 ? tmp_ssize : -tmp_ssize;

    if (t != NULL)
      {
        mpz_t x;
!       const mpz_t gtmp = MPZ_ROINIT_N (tmp_gp, gsize);
!       const mpz_t stmp = MPZ_ROINIT_N (tmp_sp, tmp_ssize);

        MPZ_TMP_INIT (x, ssize + asize + 1);
!       mpz_mul (x, stmp, a);
!       mpz_sub (x, gtmp, x);
        mpz_divexact (t, x, b);
      }

    if (s != NULL)
      {
        mp_ptr sp;


Moreover, can we slightly abuse it to obtain a static lazy init w/out
calling the _init function? E.g.

diff -r 3068f1d85563 mpz/bin_ui.c
--- a/mpz/bin_ui.c	Sun Nov 27 07:59:15 2016 +0100
+++ b/mpz/bin_ui.c	Sun Nov 27 08:39:42 2016 +0100
@@ -50,8 +50,8 @@
 void
 mpz_bin_ui (mpz_ptr r, mpz_srcptr n, unsigned long int k)
 {
-  mpz_t      ni;
   mp_limb_t  i;
+  mpz_t      ni = MPZ_ROINIT_N (&i, 0);
   mpz_t      nacc;
   mp_limb_t  kacc;
   mp_size_t  negate;
@@ -59,7 +59,6 @@
   if (SIZ (n) < 0)
     {
       /* bin(n,k) = (-1)^k * bin(-n+k-1,k) */
-      mpz_init (ni);
       mpz_add_ui (ni, n, 1L);
       mpz_neg (ni, ni);
       negate = (k & 1);   /* (-1)^k */
@@ -75,7 +74,6 @@
 	}

       /* set ni = n-k */
-      mpz_init (ni);
       mpz_sub_ui (ni, n, k);
       negate = 0;
     }

Maybe this second use does not make sense, if the symbols hiding process
will also allow some intra-library optimisation...

Best regards,
m

-- 
http://bodrato.it/papers/



More information about the gmp-devel mailing list