remove extra division in mpq/aors.c

Marc Glisse marc.glisse at inria.fr
Sat Feb 19 18:28:28 CET 2011


Hello,

the file doc/tasks.html mentions an extra division in mpq/aors.c. Would 
something like the attached do?

As a bonus, one of the multiplications is not in-place any more. We can 
do the same for the other one as well if we want (file p2).

-- 
Marc Glisse
-------------- next part --------------
diff -r cb84fd36e8f2 mpq/aors.c
--- a/mpq/aors.c	Sat Feb 19 16:34:29 2011 +0100
+++ b/mpq/aors.c	Sat Feb 19 18:02:43 2011 +0100
@@ -53,15 +53,16 @@
       mpz_t t;
 
       mpz_divexact_gcd (tmp1, &(op2->_mp_den), gcd);
+      mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
+
       mpz_mul (tmp1, &(op1->_mp_num), tmp1);
 
-      mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
-      mpz_mul (tmp2, &(op2->_mp_num), tmp2);
+      MPZ_TMP_INIT (t, 1 + MAX (ABS (tmp1->_mp_size),
+                                op2_num_size + ABS (tmp2->_mp_size)));
 
-      MPZ_TMP_INIT (t, MAX (ABS (tmp1->_mp_size), ABS (tmp2->_mp_size)) + 1);
+      mpz_mul (t, &(op2->_mp_num), tmp2);
 
-      (*fun) (t, tmp1, tmp2);
-      mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
+      (*fun) (t, tmp1, t);
 
       mpz_gcd (gcd, t, gcd);
       if (MPZ_EQUAL_1_P (gcd))
-------------- next part --------------
diff -r cb84fd36e8f2 mpq/aors.c
--- a/mpq/aors.c	Sat Feb 19 16:34:29 2011 +0100
+++ b/mpq/aors.c	Sat Feb 19 18:22:40 2011 +0100
@@ -52,16 +52,17 @@
     {
       mpz_t t;
 
-      mpz_divexact_gcd (tmp1, &(op2->_mp_den), gcd);
-      mpz_mul (tmp1, &(op1->_mp_num), tmp1);
+      MPZ_TMP_INIT (t, MAX (op1_num_size + op2_den_size,
+	     op2_num_size + op1_den_size) + 2 - gcd->_mp_size);
 
+      mpz_divexact_gcd (t, &(op2->_mp_den), gcd);
       mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
-      mpz_mul (tmp2, &(op2->_mp_num), tmp2);
 
-      MPZ_TMP_INIT (t, MAX (ABS (tmp1->_mp_size), ABS (tmp2->_mp_size)) + 1);
+      mpz_mul (tmp1, &(op1->_mp_num), t);
 
-      (*fun) (t, tmp1, tmp2);
-      mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
+      mpz_mul (t, &(op2->_mp_num), tmp2);
+
+      (*fun) (t, tmp1, t);
 
       mpz_gcd (gcd, t, gcd);
       if (MPZ_EQUAL_1_P (gcd))


More information about the gmp-discuss mailing list