Lazy mpz allocation

Marc Glisse marc.glisse at inria.fr
Mon Apr 11 09:50:54 UTC 2016


On Mon, 9 Nov 2015, Marco Bodrato wrote:

> Moreover, the mpq layer and mini-gmp need to migrate to lazy allocation
> too...

For mpq, the attached seems sufficient to pass make check. Of course, 
without a copy-on-write mechanism for the 1 in the denominator, the gains 
are not comparable to the mpz case, it is more for consistency.

-- 
Marc Glisse
-------------- next part --------------
diff -r 835f8974ff6e mpq/clear.c
--- a/mpq/clear.c	Thu Apr 07 22:50:07 2016 +0200
+++ b/mpq/clear.c	Mon Apr 11 11:42:59 2016 +0200
@@ -34,6 +34,7 @@
 void
 mpq_clear (mpq_t x)
 {
-  __GMP_FREE_FUNC_LIMBS (PTR(NUM(x)), ALLOC(NUM(x)));
+  if (ALLOC (NUM(x)))
+    __GMP_FREE_FUNC_LIMBS (PTR(NUM(x)), ALLOC(NUM(x)));
   __GMP_FREE_FUNC_LIMBS (PTR(DEN(x)), ALLOC(DEN(x)));
 }
diff -r 835f8974ff6e mpq/clears.c
--- a/mpq/clears.c	Thu Apr 07 22:50:07 2016 +0200
+++ b/mpq/clears.c	Mon Apr 11 11:42:59 2016 +0200
@@ -41,7 +41,8 @@
 
   do
     {
-      __GMP_FREE_FUNC_LIMBS (PTR(NUM(x)), ALLOC(NUM(x)));
+      if (ALLOC (NUM(x)))
+	__GMP_FREE_FUNC_LIMBS (PTR(NUM(x)), ALLOC(NUM(x)));
       __GMP_FREE_FUNC_LIMBS (PTR(DEN(x)), ALLOC(DEN(x)));
       x = va_arg (ap, mpq_ptr);
     }
diff -r 835f8974ff6e mpq/init.c
--- a/mpq/init.c	Thu Apr 07 22:50:07 2016 +0200
+++ b/mpq/init.c	Mon Apr 11 11:42:59 2016 +0200
@@ -34,8 +34,9 @@
 void
 mpq_init (mpq_t x)
 {
-  ALLOC(NUM(x)) = 1;
-  PTR(NUM(x)) = __GMP_ALLOCATE_FUNC_LIMBS (1);
+  static const mp_limb_t dummy_limb=0xc1a0;
+  ALLOC(NUM(x)) = 0;
+  PTR(NUM(x)) = (mp_ptr) &dummy_limb;
   SIZ(NUM(x)) = 0;
   ALLOC(DEN(x)) = 1;
   PTR(DEN(x)) = __GMP_ALLOCATE_FUNC_LIMBS (1);
diff -r 835f8974ff6e mpq/set_si.c
--- a/mpq/set_si.c	Thu Apr 07 22:50:07 2016 +0200
+++ b/mpq/set_si.c	Mon Apr 11 11:42:59 2016 +0200
@@ -56,7 +56,7 @@
     }
   else
     {
-      PTR(NUM(dest))[0] = abs_num;
+      MPZ_NEWALLOC (NUM(dest), 1)[0] = abs_num;
       SIZ(NUM(dest)) = num > 0 ? 1 : -1;
     }
 
diff -r 835f8974ff6e mpq/set_ui.c
--- a/mpq/set_ui.c	Thu Apr 07 22:50:07 2016 +0200
+++ b/mpq/set_ui.c	Mon Apr 11 11:42:59 2016 +0200
@@ -52,7 +52,7 @@
     }
   else
     {
-      PTR(NUM(dest))[0] = num;
+      MPZ_NEWALLOC (NUM(dest), 1)[0] = num;
       SIZ(NUM(dest)) = 1;
     }
 


More information about the gmp-devel mailing list