[Gmp-commit] /var/hg/gmp: Unify handling of mpf with mpz/mpq. Function eval h...

mercurial at gmplib.org mercurial at gmplib.org
Sat Jun 9 16:18:55 CEST 2012


details:   /var/hg/gmp/rev/d227f10a15c8
changeset: 15053:d227f10a15c8
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Sat Jun 09 16:18:52 2012 +0200
description:
Unify handling of mpf with mpz/mpq. Function eval had 2 arguments for mpf:
p and prec, but prec was always equal to mpf_get_prec(p), so I am removing it.

diffstat:

 ChangeLog |    9 ++++
 gmpxx.h   |  118 ++++++++++++++++++++++---------------------------------------
 2 files changed, 52 insertions(+), 75 deletions(-)

diffs (294 lines):

diff -r 5047615e937b -r d227f10a15c8 ChangeLog
--- a/ChangeLog	Fri Jun 08 21:59:03 2012 +0200
+++ b/ChangeLog	Sat Jun 09 16:18:52 2012 +0200
@@ -1,3 +1,12 @@
+2012-06-09  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmpxx.h (__gmp_resolve_expr::srcptr_type): New typedef.
+	(__gmp_temp): Wrapper for mp*_class, the constructor copies the
+	precision of its second argument for mpf_t.
+	(__gmp_expr::eval(p, prec)): Remove.
+	(__gmp_expr::eval(p)): Use __gmp_temp.
+	(__gmp_set_expr): Never pass prec to eval().
+
 2012-06-08 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* gmp-impl.h (__GMP_WITHIN_CONFIGURE): Use the same #if as in gmp-h.in.
diff -r 5047615e937b -r d227f10a15c8 gmpxx.h
--- a/gmpxx.h	Fri Jun 08 21:59:03 2012 +0200
+++ b/gmpxx.h	Sat Jun 09 16:18:52 2012 +0200
@@ -1292,6 +1292,7 @@
 {
   typedef mpz_t value_type;
   typedef mpz_ptr ptr_type;
+  typedef mpz_srcptr srcptr_type;
 };
 
 template <>
@@ -1299,6 +1300,7 @@
 {
   typedef mpq_t value_type;
   typedef mpq_ptr ptr_type;
+  typedef mpq_srcptr srcptr_type;
 };
 
 template <>
@@ -1306,6 +1308,7 @@
 {
   typedef mpf_t value_type;
   typedef mpf_ptr ptr_type;
+  typedef mpf_srcptr srcptr_type;
 };
 
 template <>
@@ -2128,10 +2131,33 @@
 template <class T>
 inline void __gmp_set_expr(mpf_ptr f, const __gmp_expr<mpf_t, T> &expr)
 {
-  expr.eval(f, mpf_get_prec(f));
+  expr.eval(f);
 }
 
 
+/* Temporary objects */
+
+template <class T>
+class __gmp_temp
+{
+  __gmp_expr<T, T> val;
+  public:
+  template<class U, class V>
+  __gmp_temp(U const& u, V) : val (u) {}
+  typename __gmp_resolve_expr<T>::srcptr_type
+  __get_mp() const { return val.__get_mp(); }
+};
+
+template <>
+class __gmp_temp <mpf_t>
+{
+  mpf_class val;
+  public:
+  template<class U>
+  __gmp_temp(U const& u, mpf_ptr res) : val (u, mpf_get_prec(res)) {}
+  mpf_srcptr __get_mp() const { return val.__get_mp(); }
+};
+
 /**************** Specializations of __gmp_expr ****************/
 /* The eval() method of __gmp_expr<T, U> evaluates the corresponding
    expression and assigns the result to its argument, which is either an
@@ -2160,8 +2186,7 @@
   __gmp_unary_expr<val_type, Op> expr;
 public:
   explicit __gmp_expr(const val_type &val) : expr(val) { }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t = 0) const
+  void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   { Op::eval(p, expr.val.__get_mp()); }
   const val_type & get_val() const { return expr.val; }
   mp_bitcnt_t get_prec() const { return expr.val.get_prec(); }
@@ -2181,9 +2206,6 @@
   explicit __gmp_expr(const val_type &val) : expr(val) { }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   { expr.val.eval(p); Op::eval(p, p); }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  { expr.val.eval(p, prec); Op::eval(p, p); }
   const val_type & get_val() const { return expr.val; }
   mp_bitcnt_t get_prec() const { return expr.val.get_prec(); }
 };
@@ -2213,8 +2235,7 @@
 public:
   __gmp_expr(const val1_type &val1, const val2_type &val2)
     : expr(val1, val2) { }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t = 0) const
+  void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   { Op::eval(p, expr.val1.__get_mp(), expr.val2.__get_mp()); }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
@@ -2240,8 +2261,7 @@
 public:
   __gmp_expr(const val1_type &val1, const val2_type &val2)
     : expr(val1, val2) { }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t = 0) const
+  void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   { Op::eval(p, expr.val1.__get_mp(), expr.val2); }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
@@ -2259,8 +2279,7 @@
 public:
   __gmp_expr(const val1_type &val1, const val2_type &val2)
     : expr(val1, val2) { }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t = 0) const
+  void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   { Op::eval(p, expr.val1, expr.val2.__get_mp()); }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
@@ -2291,16 +2310,10 @@
     }
     else
     {
-      __gmp_expr<T, T> temp(expr.val2);
+      __gmp_temp<T> temp(expr.val2, p);
       Op::eval(p, expr.val1.__get_mp(), temp.__get_mp());
     }
   }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  {
-    __gmp_expr<T, T> temp(expr.val2, prec);
-    Op::eval(p, expr.val1.__get_mp(), temp.__get_mp());
-  }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
   mp_bitcnt_t get_prec() const
@@ -2332,16 +2345,10 @@
     }
     else
     {
-      __gmp_expr<T, T> temp(expr.val1);
+      __gmp_temp<T> temp(expr.val1, p);
       Op::eval(p, temp.__get_mp(), expr.val2.__get_mp());
     }
   }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  {
-    __gmp_expr<T, T> temp(expr.val1, prec);
-    Op::eval(p, temp.__get_mp(), expr.val2.__get_mp());
-  }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
   mp_bitcnt_t get_prec() const
@@ -2373,16 +2380,10 @@
     }
     else
     {
-      __gmp_expr<T, T> temp(expr.val2);
+      __gmp_temp<T> temp(expr.val2, p);
       Op::eval(p, expr.val1.__get_mp(), temp.__get_mp());
     }
   }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  {
-    __gmp_expr<T, T> temp(expr.val2, prec);
-    Op::eval(p, expr.val1.__get_mp(), temp.__get_mp());
-  }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
   mp_bitcnt_t get_prec() const
@@ -2414,16 +2415,10 @@
     }
     else
     {
-      __gmp_expr<T, T> temp(expr.val1);
+      __gmp_temp<T> temp(expr.val1, p);
       Op::eval(p, temp.__get_mp(), expr.val2.__get_mp());
     }
   }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  {
-    __gmp_expr<T, T> temp(expr.val1, prec);
-    Op::eval(p, temp.__get_mp(), expr.val2.__get_mp());
-  }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
   mp_bitcnt_t get_prec() const
@@ -2453,12 +2448,6 @@
     expr.val1.eval(p);
     Op::eval(p, p, expr.val2);
   }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  {
-    expr.val1.eval(p, prec);
-    Op::eval(p, p, expr.val2);
-  }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
   mp_bitcnt_t get_prec() const { return expr.val1.get_prec(); }
@@ -2480,12 +2469,6 @@
     expr.val2.eval(p);
     Op::eval(p, expr.val1, p);
   }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  {
-    expr.val2.eval(p, prec);
-    Op::eval(p, expr.val1, p);
-  }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
   mp_bitcnt_t get_prec() const { return expr.val2.get_prec(); }
@@ -2508,16 +2491,10 @@
     : expr(val1, val2) { }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   {
-    __gmp_expr<T, T> temp2(expr.val2);
+    __gmp_temp<T> temp2(expr.val2, p);
     expr.val1.eval(p);
     Op::eval(p, p, temp2.__get_mp());
   }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  {
-    __gmp_expr<T, T> temp1(expr.val1, prec), temp2(expr.val2, prec);
-    Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
-  }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
   mp_bitcnt_t get_prec() const
@@ -2542,16 +2519,10 @@
     : expr(val1, val2) { }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   {
-    __gmp_expr<T, T> temp1(expr.val1);
+    __gmp_temp<T> temp1(expr.val1, p);
     expr.val2.eval(p);
     Op::eval(p, temp1.__get_mp(), p);
   }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  {
-    __gmp_expr<T, T> temp1(expr.val1, prec), temp2(expr.val2, prec);
-    Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
-  }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
   mp_bitcnt_t get_prec() const
@@ -2576,16 +2547,10 @@
     : expr(val1, val2) { }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   {
-    __gmp_expr<T, T> temp2(expr.val2);
+    __gmp_temp<T> temp2(expr.val2, p);
     expr.val1.eval(p);
     Op::eval(p, p, temp2.__get_mp());
   }
-  void eval(typename __gmp_resolve_expr<T>::ptr_type p,
-	    mp_bitcnt_t prec) const
-  {
-    __gmp_expr<T, T> temp1(expr.val1, prec), temp2(expr.val2, prec);
-    Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
-  }
   const val1_type & get_val1() const { return expr.val1; }
   const val2_type & get_val2() const { return expr.val2; }
   mp_bitcnt_t get_prec() const
@@ -3192,8 +3157,11 @@
   mp_bitcnt_t bits;
 public:
   __gmp_expr(gmp_randstate_t s, mp_bitcnt_t l) : state(s), bits(l) { }
-  void eval(mpf_ptr f, mp_bitcnt_t prec) const
-  { __gmp_rand_function::eval(f, state, (bits>0) ? get_prec() : prec); }
+  void eval(mpf_ptr f) const
+  {
+    __gmp_rand_function::eval(f, state,
+	(bits>0) ? bits : mpf_get_prec(f));
+  }
   mp_bitcnt_t get_prec() const
   {
     if (bits == 0)


More information about the gmp-commit mailing list