[Gmp-commit] /var/hg/gmp: mini-gmp.c (mpn_div_qr_2_preinv): Avoid temporary a...

mercurial at gmplib.org mercurial at gmplib.org
Sun Mar 11 18:22:42 UTC 2018


details:   /var/hg/gmp/rev/384bf781a7ee
changeset: 17582:384bf781a7ee
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Sun Mar 11 19:22:32 2018 +0100
description:
mini-gmp.c (mpn_div_qr_2_preinv): Avoid temporary allocation

diffstat:

 mini-gmp/ChangeLog  |  10 ++++++++++
 mini-gmp/mini-gmp.c |  30 +++++-------------------------
 2 files changed, 15 insertions(+), 25 deletions(-)

diffs (87 lines):

diff -r 6e8ef617cfdc -r 384bf781a7ee mini-gmp/ChangeLog
--- a/mini-gmp/ChangeLog	Sun Mar 04 07:19:09 2018 +0100
+++ b/mini-gmp/ChangeLog	Sun Mar 11 19:22:32 2018 +0100
@@ -1,3 +1,13 @@
+2018-03-11  Niels Möller  <nisse at lysator.liu.se>
+
+	* mini-gmp.c (mpn_div_qr_2_preinv): Drop separate rp argument.
+	Clobber input np, and store remainder in np[0] and np[1]. This is
+	all the current caller needs, and it eliminates one allocation in
+	the common case of un-normalized d.
+	(mpn_div_qr_2): Delete #if:ed out function.
+	(mpn_div_qr_preinv): Update the only call site for
+	mpn_div_qr_2_preinv.
+
 2018-02-10  Niels Möller  <nisse at lysator.liu.se>
 
 	* mini-gmp.c (mpn_div_qr_1_preinv): When qp is non-NULL, reuse
diff -r 6e8ef617cfdc -r 384bf781a7ee mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Sun Mar 04 07:19:09 2018 +0100
+++ b/mini-gmp/mini-gmp.c	Sun Mar 11 19:22:32 2018 +0100
@@ -985,13 +985,12 @@
 }
 
 static void
-mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
+mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,
 		     const struct gmp_div_inverse *inv)
 {
   unsigned shift;
   mp_size_t i;
   mp_limb_t d1, d0, di, r1, r0;
-  mp_ptr tp;
 
   assert (nn >= 2);
   shift = inv->shift;
@@ -1000,11 +999,7 @@
   di = inv->di;
 
   if (shift > 0)
-    {
-      tp = gmp_xalloc_limbs (nn);
-      r1 = mpn_lshift (tp, np, nn, shift);
-      np = tp;
-    }
+    r1 = mpn_lshift (np, np, nn, shift);
   else
     r1 = 0;
 
@@ -1027,27 +1022,12 @@
       assert ((r0 << (GMP_LIMB_BITS - shift)) == 0);
       r0 = (r0 >> shift) | (r1 << (GMP_LIMB_BITS - shift));
       r1 >>= shift;
-
-      gmp_free (tp);
     }
 
-  rp[1] = r1;
-  rp[0] = r0;
+  np[1] = r1;
+  np[0] = r0;
 }
 
-#if 0
-static void
-mpn_div_qr_2 (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
-	      mp_limb_t d1, mp_limb_t d0)
-{
-  struct gmp_div_inverse inv;
-  assert (nn >= 2);
-
-  mpn_div_qr_2_invert (&inv, d1, d0);
-  mpn_div_qr_2_preinv (qp, rp, np, nn, &inv);
-}
-#endif
-
 static void
 mpn_div_qr_pi1 (mp_ptr qp,
 		mp_ptr np, mp_size_t nn, mp_limb_t n1,
@@ -1122,7 +1102,7 @@
   if (dn == 1)
     np[0] = mpn_div_qr_1_preinv (qp, np, nn, inv);
   else if (dn == 2)
-    mpn_div_qr_2_preinv (qp, np, np, nn, inv);
+    mpn_div_qr_2_preinv (qp, np, nn, inv);
   else
     {
       mp_limb_t nh;


More information about the gmp-commit mailing list