[Gmp-commit] /var/hg/gmp: Use macros like NUM, ALLOC, SIZ, etc in mpq/*.

mercurial at gmplib.org mercurial at gmplib.org
Fri Feb 24 11:14:25 CET 2012


details:   /var/hg/gmp/rev/0890e16b792f
changeset: 14670:0890e16b792f
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Fri Feb 24 11:14:11 2012 +0100
description:
Use macros like NUM, ALLOC, SIZ, etc in mpq/*.
Test some mpq functions that were not used in the testsuite.
Implement q=z (in gmpxx) with mpq_set_z.

diffstat:

 ChangeLog             |  41 ++++++++++++++++++++++++++++++++++
 gmp-impl.h            |   2 +
 gmpxx.h               |   5 ++++
 mpq/abs.c             |  13 +++++-----
 mpq/aors.c            |  38 +++++++++++++++---------------
 mpq/canonicalize.c    |  18 +++++++-------
 mpq/clear.c           |   8 +++---
 mpq/cmp.c             |  32 +++++++++++++-------------
 mpq/cmp_si.c          |  12 +++++-----
 mpq/cmp_ui.c          |   8 +++---
 mpq/div.c             |  36 +++++++++++++++---------------
 mpq/equal.c           |  16 ++++++------
 mpq/get_d.c           |   8 +++---
 mpq/get_den.c         |   9 +++----
 mpq/get_num.c         |   9 +++----
 mpq/get_str.c         |   2 +-
 mpq/init.c            |  16 ++++++------
 mpq/inp_str.c         |  10 ++++----
 mpq/inv.c             |  30 +++++++++++-------------
 mpq/md_2exp.c         |   6 ++--
 mpq/mul.c             |  30 ++++++++++++------------
 mpq/neg.c             |  12 +++++-----
 mpq/set.c             |  18 ++++++--------
 mpq/set_d.c           |  28 +++++++++++-----------
 mpq/set_den.c         |   9 +++----
 mpq/set_f.c           |  22 +++++++++---------
 mpq/set_num.c         |   9 +++----
 mpq/set_si.c          |  10 ++++----
 mpq/set_str.c         |   4 +-
 mpq/set_ui.c          |  10 ++++----
 mpq/set_z.c           |  13 +++++-----
 mpq/swap.c            |  48 ++++++++++++++++++++--------------------
 tests/mpq/Makefile.am |   2 +-
 tests/mpq/t-cmp.c     |   3 --
 tests/mpq/t-cmp_ui.c  |   3 --
 tests/mpq/t-inv.c     |  61 +++++++++++++++++++++++++++++++++++++++++++++++++++
 36 files changed, 347 insertions(+), 254 deletions(-)

diffs (truncated from 1202 to 300 lines):

diff -r c61e22c8e042 -r 0890e16b792f ChangeLog
--- a/ChangeLog	Fri Feb 24 10:31:01 2012 +0100
+++ b/ChangeLog	Fri Feb 24 11:14:11 2012 +0100
@@ -1,3 +1,44 @@
+2012-02-23  Marc Glisse  <marc.glisse at inria.fr>
+
+	* tests/mpq/t-cmp.c: Move NUM and DEN macros...
+	* tests/mpq/t-cmp_ui.c: Likewise...
+	* gmp-impl.h: ... to here.
+
+	* mpq/abs.c: Use NUM, DEN, SIZ, ALLOC, PTR, MPZ_REALLOC.
+	* mpq/aors.c: Likewise.
+	* mpq/canonicalize.c: Likewise.
+	* mpq/clear.c: Likewise.
+	* mpq/cmp.c: Likewise.
+	* mpq/cmp_si.c: Likewise.
+	* mpq/cmp_ui.c: Likewise.
+	* mpq/div.c: Likewise.
+	* mpq/equal.c: Likewise.
+	* mpq/get_d.c: Likewise.
+	* mpq/get_den.c: Likewise.
+	* mpq/get_num.c: Likewise.
+	* mpq/get_str.c: Likewise.
+	* mpq/init.c: Likewise.
+	* mpq/inp_str.c: Likewise.
+	* mpq/inv.c: Likewise.
+	* mpq/md_2exp.c: Likewise.
+	* mpq/mul.c: Likewise.
+	* mpq/neg.c: Likewise.
+	* mpq/set.c: Likewise.
+	* mpq/set_d.c: Likewise.
+	* mpq/set_den.c: Likewise.
+	* mpq/set_f.c: Likewise.
+	* mpq/set_num.c: Likewise.
+	* mpq/set_si.c: Likewise.
+	* mpq/set_str.c: Likewise.
+	* mpq/set_ui.c: Likewise.
+	* mpq/set_z.c: Likewise.
+	* mpq/swap.c: Likewise.
+
+	* tests/mpq/t-inv.c: New test file.
+	* tests/mpq/Makefile.am: Add the above.
+
+	* gmpxx.h (__gmp_set_expr): Use mpq_set_z.
+
 2012-02-24  Niels Möller  <nisse at lysator.liu.se>
 
 	* mpn/x86_64/core2/aorsmul_1.asm: Added mpn_addmul_1c and
diff -r c61e22c8e042 -r 0890e16b792f gmp-impl.h
--- a/gmp-impl.h	Fri Feb 24 10:31:01 2012 +0100
+++ b/gmp-impl.h	Fri Feb 24 11:14:11 2012 +0100
@@ -513,6 +513,8 @@
 #define EXP(x) ((x)->_mp_exp)
 #define PREC(x) ((x)->_mp_prec)
 #define ALLOC(x) ((x)->_mp_alloc)
+#define NUM(x) mpq_numref(x)
+#define DEN(x) mpq_denref(x)
 
 /* n-1 inverts any low zeros and the lowest one bit.  If n&(n-1) leaves zero
    then that lowest one bit must have been the only bit set.  n==0 will
diff -r c61e22c8e042 -r 0890e16b792f gmpxx.h
--- a/gmpxx.h	Fri Feb 24 10:31:01 2012 +0100
+++ b/gmpxx.h	Fri Feb 24 11:14:11 2012 +0100
@@ -2036,6 +2036,11 @@
   mpz_set_f(z, temp.get_mpf_t());
 }
 
+inline void __gmp_set_expr(mpq_ptr q, const mpz_class &z)
+{
+  mpq_set_z(q, z.get_mpz_t());
+}
+
 template <class T>
 inline void __gmp_set_expr(mpq_ptr q, const __gmp_expr<mpz_t, T> &expr)
 {
diff -r c61e22c8e042 -r 0890e16b792f mpq/abs.c
--- a/mpq/abs.c	Fri Feb 24 10:31:01 2012 +0100
+++ b/mpq/abs.c	Fri Feb 24 11:14:11 2012 +0100
@@ -26,21 +26,20 @@
 void
 mpq_abs (mpq_ptr dst, mpq_srcptr src)
 {
-  mp_size_t  num_size = src->_mp_num._mp_size;
-  mp_size_t  num_abs_size = ABS (num_size);
+  mp_size_t  num_abs_size = ABSIZ(NUM(src));
 
   if (dst != src)
     {
-      mp_size_t  den_size = src->_mp_den._mp_size;
+      mp_size_t  den_size = SIZ(DEN(src));
 
       MPZ_REALLOC (mpq_numref(dst), num_abs_size);
       MPZ_REALLOC (mpq_denref(dst), den_size);
 
-      MPN_COPY (dst->_mp_num._mp_d, src->_mp_num._mp_d, num_abs_size);
-      MPN_COPY (dst->_mp_den._mp_d, src->_mp_den._mp_d, den_size);
+      MPN_COPY (PTR(NUM(dst)), PTR(NUM(src)), num_abs_size);
+      MPN_COPY (PTR(DEN(dst)), PTR(DEN(src)), den_size);
 
-      dst->_mp_den._mp_size = den_size;
+      SIZ(DEN(dst)) = den_size;
     }
 
-  dst->_mp_num._mp_size = num_abs_size;
+  SIZ(NUM(dst)) = num_abs_size;
 }
diff -r c61e22c8e042 -r 0890e16b792f mpq/aors.c
--- a/mpq/aors.c	Fri Feb 24 10:31:01 2012 +0100
+++ b/mpq/aors.c	Fri Feb 24 11:14:11 2012 +0100
@@ -31,10 +31,10 @@
 {
   mpz_t gcd;
   mpz_t tmp1, tmp2;
-  mp_size_t op1_num_size = ABS (op1->_mp_num._mp_size);
-  mp_size_t op1_den_size =      op1->_mp_den._mp_size;
-  mp_size_t op2_num_size = ABS (op2->_mp_num._mp_size);
-  mp_size_t op2_den_size =      op2->_mp_den._mp_size;
+  mp_size_t op1_num_size = ABSIZ(NUM(op1));
+  mp_size_t op1_den_size =   SIZ(DEN(op1));
+  mp_size_t op2_num_size = ABSIZ(NUM(op2));
+  mp_size_t op2_den_size =   SIZ(DEN(op2));
   TMP_DECL;
 
   TMP_MARK;
@@ -47,43 +47,43 @@
      dare to overwrite the numerator of ROP when we are finished
      with the numerators of OP1 and OP2.  */
 
-  mpz_gcd (gcd, &(op1->_mp_den), &(op2->_mp_den));
+  mpz_gcd (gcd, DEN(op1), DEN(op2));
   if (! MPZ_EQUAL_1_P (gcd))
     {
       mpz_t t;
 
       MPZ_TMP_INIT (t, MAX (op1_num_size + op2_den_size,
-	     op2_num_size + op1_den_size) + 2 - gcd->_mp_size);
+	     op2_num_size + op1_den_size) + 2 - SIZ(gcd));
 
-      mpz_divexact_gcd (t, &(op2->_mp_den), gcd);
-      mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
+      mpz_divexact_gcd (t, DEN(op2), gcd);
+      mpz_divexact_gcd (tmp2, DEN(op1), gcd);
 
-      mpz_mul (tmp1, &(op1->_mp_num), t);
-      mpz_mul (t, &(op2->_mp_num), tmp2);
+      mpz_mul (tmp1, NUM(op1), t);
+      mpz_mul (t, NUM(op2), tmp2);
 
       (*fun) (t, tmp1, t);
 
       mpz_gcd (gcd, t, gcd);
       if (MPZ_EQUAL_1_P (gcd))
         {
-          mpz_set (&(rop->_mp_num), t);
-          mpz_mul (&(rop->_mp_den), &(op2->_mp_den), tmp2);
+          mpz_set (NUM(rop), t);
+          mpz_mul (DEN(rop), DEN(op2), tmp2);
         }
       else
         {
-          mpz_divexact_gcd (&(rop->_mp_num), t, gcd);
-          mpz_divexact_gcd (tmp1, &(op2->_mp_den), gcd);
-          mpz_mul (&(rop->_mp_den), tmp1, tmp2);
+          mpz_divexact_gcd (NUM(rop), t, gcd);
+          mpz_divexact_gcd (tmp1, DEN(op2), gcd);
+          mpz_mul (DEN(rop), tmp1, tmp2);
         }
     }
   else
     {
       /* The common divisor is 1.  This is the case (for random input) with
 	 probability 6/(pi**2), which is about 60.8%.  */
-      mpz_mul (tmp1, &(op1->_mp_num), &(op2->_mp_den));
-      mpz_mul (tmp2, &(op2->_mp_num), &(op1->_mp_den));
-      (*fun) (&(rop->_mp_num), tmp1, tmp2);
-      mpz_mul (&(rop->_mp_den), &(op1->_mp_den), &(op2->_mp_den));
+      mpz_mul (tmp1, NUM(op1), DEN(op2));
+      mpz_mul (tmp2, NUM(op2), DEN(op1));
+      (*fun) (NUM(rop), tmp1, tmp2);
+      mpz_mul (DEN(rop), DEN(op1), DEN(op2));
     }
   TMP_FREE;
 }
diff -r c61e22c8e042 -r 0890e16b792f mpq/canonicalize.c
--- a/mpq/canonicalize.c	Fri Feb 24 10:31:01 2012 +0100
+++ b/mpq/canonicalize.c	Fri Feb 24 11:14:11 2012 +0100
@@ -28,26 +28,26 @@
   mpz_t gcd;
   TMP_DECL;
 
-  if (op->_mp_den._mp_size == 0)
+  if (SIZ(DEN(op)) == 0)
     DIVIDE_BY_ZERO;
 
   TMP_MARK;
 
   /* ??? Dunno if the 1+ is needed.  */
-  MPZ_TMP_INIT (gcd, 1 + MAX (ABS (op->_mp_num._mp_size),
-			      ABS (op->_mp_den._mp_size)));
+  MPZ_TMP_INIT (gcd, 1 + MAX (ABSIZ(NUM(op)),
+			      ABSIZ(DEN(op))));
 
-  mpz_gcd (gcd, &(op->_mp_num), &(op->_mp_den));
+  mpz_gcd (gcd, NUM(op), DEN(op));
   if (! MPZ_EQUAL_1_P (gcd))
     {
-      mpz_divexact_gcd (&(op->_mp_num), &(op->_mp_num), gcd);
-      mpz_divexact_gcd (&(op->_mp_den), &(op->_mp_den), gcd);
+      mpz_divexact_gcd (NUM(op), NUM(op), gcd);
+      mpz_divexact_gcd (DEN(op), DEN(op), gcd);
     }
 
-  if (op->_mp_den._mp_size < 0)
+  if (SIZ(DEN(op)) < 0)
     {
-      op->_mp_num._mp_size = -op->_mp_num._mp_size;
-      op->_mp_den._mp_size = -op->_mp_den._mp_size;
+      SIZ(NUM(op)) = -SIZ(NUM(op));
+      SIZ(DEN(op)) = -SIZ(DEN(op));
     }
   TMP_FREE;
 }
diff -r c61e22c8e042 -r 0890e16b792f mpq/clear.c
--- a/mpq/clear.c	Fri Feb 24 10:31:01 2012 +0100
+++ b/mpq/clear.c	Fri Feb 24 11:14:11 2012 +0100
@@ -23,8 +23,8 @@
 void
 mpq_clear (MP_RAT *m)
 {
-  (*__gmp_free_func) (m->_mp_num._mp_d,
-		    m->_mp_num._mp_alloc * BYTES_PER_MP_LIMB);
-  (*__gmp_free_func) (m->_mp_den._mp_d,
-		    m->_mp_den._mp_alloc * BYTES_PER_MP_LIMB);
+  (*__gmp_free_func) (PTR(NUM(m)),
+		    ALLOC(NUM(m)) * BYTES_PER_MP_LIMB);
+  (*__gmp_free_func) (PTR(DEN(m)),
+		    ALLOC(DEN(m)) * BYTES_PER_MP_LIMB);
 }
diff -r c61e22c8e042 -r 0890e16b792f mpq/cmp.c
--- a/mpq/cmp.c	Fri Feb 24 10:31:01 2012 +0100
+++ b/mpq/cmp.c	Fri Feb 24 11:14:11 2012 +0100
@@ -25,10 +25,10 @@
 int
 mpq_cmp (const MP_RAT *op1, const MP_RAT *op2)
 {
-  mp_size_t num1_size = op1->_mp_num._mp_size;
-  mp_size_t den1_size = op1->_mp_den._mp_size;
-  mp_size_t num2_size = op2->_mp_num._mp_size;
-  mp_size_t den2_size = op2->_mp_den._mp_size;
+  mp_size_t num1_size = SIZ(NUM(op1));
+  mp_size_t den1_size = SIZ(DEN(op1));
+  mp_size_t num2_size = SIZ(NUM(op2));
+  mp_size_t den2_size = SIZ(DEN(op2));
   mp_size_t tmp1_size, tmp2_size;
   mp_ptr tmp1_ptr, tmp2_ptr;
   mp_size_t num1_sign;
@@ -70,12 +70,12 @@
     int cnt1, cnt2;
     mp_bitcnt_t bits1, bits2;
 
-    count_leading_zeros (cnt1, op1->_mp_num._mp_d[num1_size - 1]);
-    count_leading_zeros (cnt2, op2->_mp_den._mp_d[den2_size - 1]);
+    count_leading_zeros (cnt1, PTR(NUM(op1))[num1_size - 1]);
+    count_leading_zeros (cnt2, PTR(DEN(op2))[den2_size - 1]);
     bits1 = tmp1_size * GMP_NUMB_BITS - cnt1 - cnt2 + 2 * GMP_NAIL_BITS;
 
-    count_leading_zeros (cnt1, op2->_mp_num._mp_d[num2_size - 1]);
-    count_leading_zeros (cnt2, op1->_mp_den._mp_d[den1_size - 1]);
+    count_leading_zeros (cnt1, PTR(NUM(op2))[num2_size - 1]);
+    count_leading_zeros (cnt2, PTR(DEN(op1))[den1_size - 1]);
     bits2 = tmp2_size * GMP_NUMB_BITS - cnt1 - cnt2 + 2 * GMP_NAIL_BITS;
 
     if (bits1 > bits2 + 1)
@@ -91,21 +91,21 @@
 
   if (num1_size >= den2_size)
     tmp1_size -= 0 == mpn_mul (tmp1_ptr,
-			       op1->_mp_num._mp_d, num1_size,
-			       op2->_mp_den._mp_d, den2_size);
+			       PTR(NUM(op1)), num1_size,
+			       PTR(DEN(op2)), den2_size);
   else
     tmp1_size -= 0 == mpn_mul (tmp1_ptr,
-			       op2->_mp_den._mp_d, den2_size,
-			       op1->_mp_num._mp_d, num1_size);
+			       PTR(DEN(op2)), den2_size,
+			       PTR(NUM(op1)), num1_size);
 
    if (num2_size >= den1_size)
      tmp2_size -= 0 == mpn_mul (tmp2_ptr,
-				op2->_mp_num._mp_d, num2_size,
-				op1->_mp_den._mp_d, den1_size);
+				PTR(NUM(op2)), num2_size,
+				PTR(DEN(op1)), den1_size);
    else
      tmp2_size -= 0 == mpn_mul (tmp2_ptr,
-				op1->_mp_den._mp_d, den1_size,
-				op2->_mp_num._mp_d, num2_size);
+				PTR(DEN(op1)), den1_size,
+				PTR(NUM(op2)), num2_size);


More information about the gmp-commit mailing list