[Gmp-commit] /var/hg/gmp: mpz/sqrt{,rem}.c: Further simplify.

mercurial at gmplib.org mercurial at gmplib.org
Wed May 23 12:03:42 CEST 2012


details:   /var/hg/gmp/rev/eed01cabb120
changeset: 14998:eed01cabb120
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed May 23 12:03:30 2012 +0200
description:
mpz/sqrt{,rem}.c: Further simplify.

diffstat:

 ChangeLog     |   5 +++++
 mpz/sqrt.c    |  39 ++++++++++++++-------------------------
 mpz/sqrtrem.c |  48 +++++++++++++++++-------------------------------
 3 files changed, 36 insertions(+), 56 deletions(-)

diffs (164 lines):

diff -r 42c56b9cc061 -r eed01cabb120 ChangeLog
--- a/ChangeLog	Tue May 22 22:54:31 2012 +0200
+++ b/ChangeLog	Wed May 23 12:03:30 2012 +0200
@@ -1,3 +1,8 @@
+2012-05-23 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mpz/sqrt.c: Further simplify.
+	* mpz/sqrtrem.c: Likewise.
+
 2012-05-22  Torbjorn Granlund  <tege at gmplib.org>
 
 	* mpz/sqrt.c: Allocate less for overlapping operands, simplify.
diff -r 42c56b9cc061 -r eed01cabb120 mpz/sqrt.c
--- a/mpz/sqrt.c	Tue May 22 22:54:31 2012 +0200
+++ b/mpz/sqrt.c	Wed May 23 12:03:30 2012 +0200
@@ -29,7 +29,7 @@
   mp_ptr root_ptr, op_ptr;
 
   op_size = SIZ (op);
-  if (op_size <= 0)
+  if (UNLIKELY (op_size <= 0))
     {
       if (op_size < 0)
 	SQRT_OF_NEGATIVE;
@@ -41,37 +41,26 @@
   root_size = (op_size + 1) / 2;
   SIZ (root) = root_size;
 
-  root_ptr = PTR (root);
   op_ptr = PTR (op);
 
-  if (ALLOC (root) < root_size)
+  if (root == op)
     {
-      /* From size relations, we can tell ROOT != OP.  */
-      ASSERT (root_ptr != op_ptr);
+      /* Allocate temp space for the root, which we then copy to the
+	 shared OP/ROOT variable.  */
+      TMP_DECL;
+      TMP_MARK;
 
-      root_ptr = __GMP_REALLOCATE_FUNC_LIMBS (root_ptr, ALLOC (root), root_size);
-      ALLOC (root) = root_size;
-      PTR (root) = root_ptr;
+      root_ptr = TMP_ALLOC_LIMBS (root_size);
+      mpn_sqrtrem (root_ptr, NULL, op_ptr, op_size);
+
+      MPN_COPY (op_ptr, root_ptr, root_size);
+
+      TMP_FREE;
     }
   else
     {
-      if (root_ptr == op_ptr)
-	{
-	  /* Allocate temp space for the root, which we then copy to the
-	     shared OP/ROOT variable.  */
-	  mp_ptr p;
-	  TMP_DECL;
-	  TMP_MARK;
+      root_ptr = MPZ_REALLOC (root, root_size);
 
-	  p = TMP_ALLOC_LIMBS (root_size);
-	  mpn_sqrtrem (p, NULL, root_ptr, op_size);
-
-	  MPN_COPY (root_ptr, p, root_size);
-
-	  TMP_FREE;
-	  return;
-	}
+      mpn_sqrtrem (root_ptr, NULL, op_ptr, op_size);
     }
-
-  mpn_sqrtrem (root_ptr, NULL, op_ptr, op_size);
 }
diff -r 42c56b9cc061 -r eed01cabb120 mpz/sqrtrem.c
--- a/mpz/sqrtrem.c	Tue May 22 22:54:31 2012 +0200
+++ b/mpz/sqrtrem.c	Wed May 23 12:03:30 2012 +0200
@@ -19,7 +19,6 @@
 You should have received a copy of the GNU Lesser General Public License
 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
 
-#include <stdio.h> /* for NULL */
 #include "gmp.h"
 #include "gmp-impl.h"
 
@@ -27,10 +26,10 @@
 mpz_sqrtrem (mpz_ptr root, mpz_ptr rem, mpz_srcptr op)
 {
   mp_size_t op_size, root_size, rem_size;
-  mp_ptr root_ptr, op_ptr;
+  mp_ptr root_ptr, op_ptr, rem_ptr;
 
   op_size = SIZ (op);
-  if (op_size <= 0)
+  if (UNLIKELY (op_size <= 0))
     {
       if (op_size < 0)
 	SQRT_OF_NEGATIVE;
@@ -39,49 +38,36 @@
       return;
     }
 
-  MPZ_REALLOC (rem, op_size);
+  rem_ptr = MPZ_REALLOC (rem, op_size);
 
   /* The size of the root is accurate after this simple calculation.  */
   root_size = (op_size + 1) / 2;
   SIZ (root) = root_size;
 
-  root_ptr = PTR (root);
   op_ptr = PTR (op);
 
-  if (ALLOC (root) < root_size)
+  if (root == op)
     {
-      /* From size relations, we can tell ROOT != OP.  */
-      ASSERT (root_ptr != op_ptr);
+      /* Allocate temp space for the root, which we then copy to the
+	 shared OP/ROOT variable.  */
+      TMP_DECL;
+      TMP_MARK;
 
-      root_ptr = __GMP_REALLOCATE_FUNC_LIMBS (root_ptr, ALLOC (root), root_size);
-      ALLOC (root) = root_size;
-      PTR (root) = root_ptr;
+      root_ptr = TMP_ALLOC_LIMBS (root_size);
+      rem_size = mpn_sqrtrem (root_ptr, rem_ptr, op_ptr, op_size);
+
+      if (rem != root)	/* Don't overwrite remainder */
+	MPN_COPY (op_ptr, root_ptr, root_size);
+
+      TMP_FREE;
     }
   else
     {
-      if (root_ptr == op_ptr)
-	{
-	  /* Allocate temp space for the root, which we then copy to the
-	     shared OP/ROOT variable.  */
-	  mp_ptr p;
-	  TMP_DECL;
-	  TMP_MARK;
+      root_ptr = MPZ_REALLOC (root, root_size);
 
-	  p = TMP_ALLOC_LIMBS (root_size);
-	  rem_size = mpn_sqrtrem (p, PTR (rem), root_ptr, op_size);
-
-	  if (rem != root)	/* Don't overwrite remainder */
-	    MPN_COPY (root_ptr, p, root_size);
-
-	  TMP_FREE;
-	  goto done;
-	}
+      rem_size = mpn_sqrtrem (root_ptr, rem_ptr, op_ptr, op_size);
     }
 
-  rem_size = mpn_sqrtrem (root_ptr, PTR (rem), op_ptr, op_size);
-
- done:
-
   /* Write remainder size last, to make this function give only the square root
      remainder, when passed ROOT == REM.  */
   SIZ (rem) = rem_size;


More information about the gmp-commit mailing list