[Gmp-commit] /var/hg/gmp: 2 new changesets

mercurial at gmplib.org mercurial at gmplib.org
Wed Jan 17 04:46:55 UTC 2018


details:   /var/hg/gmp/rev/7e1c88bcfba2
changeset: 17541:7e1c88bcfba2
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed Jan 17 05:27:30 2018 +0100
description:
mpz/{and,ior,xor}.c: Reorg. 3 cases: both <0, both >=0, one and one

details:   /var/hg/gmp/rev/bd031c4d736b
changeset: 17542:bd031c4d736b
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed Jan 17 05:34:55 2018 +0100
description:
mpz/{and,ior,xor}.c: Reindent

diffstat:

 mpz/and.c |  194 ++++++++++++++++++++++++++++++-------------------------------
 mpz/ior.c |  130 ++++++++++++++++++++---------------------
 mpz/xor.c |   84 ++++++++++++--------------
 3 files changed, 199 insertions(+), 209 deletions(-)

diffs (truncated from 521 to 300 lines):

diff -r 0cb65d333949 -r bd031c4d736b mpz/and.c
--- a/mpz/and.c	Tue Jan 16 07:37:51 2018 +0100
+++ b/mpz/and.c	Wed Jan 17 05:34:55 2018 +0100
@@ -39,7 +39,6 @@
   mp_ptr res_ptr;
   mp_size_t res_size;
   mp_size_t i;
-  TMP_DECL;
 
   op1_size = SIZ(op1);
   op2_size = SIZ(op2);
@@ -71,10 +70,12 @@
 	  }
 
       SIZ (res) = 0;
-      return;
     }
   else
     {
+      TMP_DECL;
+
+      op2_size = -op2_size;
       TMP_MARK;
       if (op1_size < 0)
 	{
@@ -91,7 +92,6 @@
 	     would get carry into the zero-limb in this situation...  */
 
 	  op1_size = -op1_size;
-	  op2_size = -op2_size;
 
 	  TMP_ALLOC_LIMBS_2 (opx, op1_size, opy, op2_size);
 	  mpn_sub_1 (opx, op1_ptr, op1_size, (mp_limb_t) 1);
@@ -116,111 +116,107 @@
 	  res_size += res_ptr[res_size];
 
 	  SIZ(res) = -res_size;
-	  return;
 	}
-    }
-
-  {
+      else
+	{
 #if ANDNEW
-    mp_size_t op2_lim;
-    mp_size_t count;
-
-    /* OP2 must be negated as with infinite precision.
-
-       Scan from the low end for a non-zero limb.  The first non-zero
-       limb is simply negated (two's complement).  Any subsequent
-       limbs are one's complemented.  Of course, we don't need to
-       handle more limbs than there are limbs in the other, positive
-       operand as the result for those limbs is going to become zero
-       anyway.  */
-
-    /* Scan for the least significant non-zero OP2 limb, and zero the
-       result meanwhile for those limb positions.  (We will surely
-       find a non-zero limb, so we can write the loop with one
-       termination condition only.)  */
-    for (i = 0; op2_ptr[i] == 0; i++)
-      res_ptr[i] = 0;
-    op2_lim = i;
+	  mp_size_t op2_lim;
+	  mp_size_t count;
 
-    op2_size = -op2_size;
+	  /* OP2 must be negated as with infinite precision.
 
-    if (op1_size <= op2_size)
-      {
-	/* The ones-extended OP2 is >= than the zero-extended OP1.
-	   RES_SIZE <= OP1_SIZE.  Find the exact size.  */
-	for (i = op1_size - 1; i > op2_lim; i--)
-	  if ((op1_ptr[i] & ~op2_ptr[i]) != 0)
-	    break;
-	res_size = i + 1;
-	for (i = res_size - 1; i > op2_lim; i--)
-	  res_ptr[i] = op1_ptr[i] & ~op2_ptr[i];
-	res_ptr[op2_lim] = op1_ptr[op2_lim] & -op2_ptr[op2_lim];
-	/* Yes, this *can* happen!  */
-	MPN_NORMALIZE (res_ptr, res_size);
-      }
-    else
-      {
-	/* The ones-extended OP2 is < than the zero-extended OP1.
-	   RES_SIZE == OP1_SIZE, since OP1 is normalized.  */
-	res_size = op1_size;
-	MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size, op1_size - op2_size);
-	for (i = op2_size - 1; i > op2_lim; i--)
-	  res_ptr[i] = op1_ptr[i] & ~op2_ptr[i];
-	res_ptr[op2_lim] = op1_ptr[op2_lim] & -op2_ptr[op2_lim];
-      }
+	     Scan from the low end for a non-zero limb.  The first non-zero
+	     limb is simply negated (two's complement).  Any subsequent
+	     limbs are one's complemented.  Of course, we don't need to
+	     handle more limbs than there are limbs in the other, positive
+	     operand as the result for those limbs is going to become zero
+	     anyway.  */
+
+	  /* Scan for the least significant non-zero OP2 limb, and zero the
+	     result meanwhile for those limb positions.  (We will surely
+	     find a non-zero limb, so we can write the loop with one
+	     termination condition only.)  */
+	  for (i = 0; op2_ptr[i] == 0; i++)
+	    res_ptr[i] = 0;
+	  op2_lim = i;
+
+	  if (op1_size <= op2_size)
+	    {
+	      /* The ones-extended OP2 is >= than the zero-extended OP1.
+		 RES_SIZE <= OP1_SIZE.  Find the exact size.  */
+	      for (i = op1_size - 1; i > op2_lim; i--)
+		if ((op1_ptr[i] & ~op2_ptr[i]) != 0)
+		  break;
+	      res_size = i + 1;
+	      for (i = res_size - 1; i > op2_lim; i--)
+		res_ptr[i] = op1_ptr[i] & ~op2_ptr[i];
+	      res_ptr[op2_lim] = op1_ptr[op2_lim] & -op2_ptr[op2_lim];
+	      /* Yes, this *can* happen!  */
+	      MPN_NORMALIZE (res_ptr, res_size);
+	    }
+	  else
+	    {
+	      /* The ones-extended OP2 is < than the zero-extended OP1.
+		 RES_SIZE == OP1_SIZE, since OP1 is normalized.  */
+	      res_size = op1_size;
+	      MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size, op1_size - op2_size);
+	      for (i = op2_size - 1; i > op2_lim; i--)
+		res_ptr[i] = op1_ptr[i] & ~op2_ptr[i];
+	      res_ptr[op2_lim] = op1_ptr[op2_lim] & -op2_ptr[op2_lim];
+	    }
 #else
 
-    /* OP1 is positive and zero-extended,
-       OP2 is negative and ones-extended.
-       The result will be positive.
-       OP1 & -OP2 = OP1 & ~(OP2 - 1).  */
-
-    mp_ptr opx;
-
-    op2_size = -op2_size;
-    opx = TMP_ALLOC_LIMBS (op2_size);
-    mpn_sub_1 (opx, op2_ptr, op2_size, (mp_limb_t) 1);
-    op2_ptr = opx;
-
-    if (op1_size > op2_size)
-      {
-	/* The result has the same size as OP1, since OP1 is normalized
-	   and longer than the ones-extended OP2.  */
-	res_size = op1_size;
-
-	/* Handle allocation, now then we know exactly how much space is
-	   needed for the result.  */
-	res_ptr = MPZ_NEWALLOC (res, res_size);
-	/* Don't re-read OP1_PTR or OP2_PTR.  Since res_size = op1_size,
-	   op1 is not changed if it is identical to res.
-	   OP2_PTR points to temporary space.  */
+	  /* OP1 is positive and zero-extended,
+	     OP2 is negative and ones-extended.
+	     The result will be positive.
+	     OP1 & -OP2 = OP1 & ~(OP2 - 1).  */
 
-	mpn_andn_n (res_ptr, op1_ptr, op2_ptr, op2_size);
-	MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size, res_size - op2_size);
-      }
-    else
-      {
-	/* Find out the exact result size.  Ignore the high limbs of OP2,
-	   OP1 is zero-extended and would make the result zero.  */
-	res_size = 0;
-	for (i = op1_size; --i >= 0;)
-	  if ((op1_ptr[i] & ~op2_ptr[i]) != 0)
+	  mp_ptr opx;
+
+	  opx = TMP_ALLOC_LIMBS (op2_size);
+	  mpn_sub_1 (opx, op2_ptr, op2_size, (mp_limb_t) 1);
+	  op2_ptr = opx;
+
+	  if (op1_size > op2_size)
 	    {
-	      res_size = i + 1;
-	      /* Handle allocation, now then we know exactly how much
-		 space is needed for the result.  */
-	      /* Don't re-read OP1_PTR.  Since res_size <= op1_size,
-		 op1 is not changed if it is identical to res.  Don't
-		 re-read OP2_PTR.  It points to temporary space--never
-		 to the space PTR(res) used to point to before
-		 reallocation.  */
-	      mpn_andn_n (MPZ_NEWALLOC (res, res_size), op1_ptr, op2_ptr, res_size);
+	      /* The result has the same size as OP1, since OP1 is normalized
+		 and longer than the ones-extended OP2.  */
+	      res_size = op1_size;
 
-	      break;
+	      /* Handle allocation, now then we know exactly how much space is
+		 needed for the result.  */
+	      res_ptr = MPZ_NEWALLOC (res, res_size);
+	      /* Don't re-read OP1_PTR or OP2_PTR.  Since res_size = op1_size,
+		 op1 is not changed if it is identical to res.
+		 OP2_PTR points to temporary space.  */
+
+	      mpn_andn_n (res_ptr, op1_ptr, op2_ptr, op2_size);
+	      MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size, res_size - op2_size);
 	    }
-      }
+	  else
+	    {
+	      /* Find out the exact result size.  Ignore the high limbs of OP2,
+		 OP1 is zero-extended and would make the result zero.  */
+	      res_size = 0;
+	      for (i = op1_size; --i >= 0;)
+		if ((op1_ptr[i] & ~op2_ptr[i]) != 0)
+		  {
+		    res_size = i + 1;
+		    /* Handle allocation, now then we know exactly how much
+		       space is needed for the result.  */
+		    /* Don't re-read OP1_PTR.  Since res_size <= op1_size,
+		       op1 is not changed if it is identical to res.  Don't
+		       re-read OP2_PTR.  It points to temporary space--never
+		       to the space PTR(res) used to point to before
+		       reallocation.  */
+		    mpn_andn_n (MPZ_NEWALLOC (res, res_size), op1_ptr, op2_ptr, res_size);
+
+		    break;
+		  }
+	    }
 #endif
-    SIZ(res) = res_size;
-  }
-  TMP_FREE;
+	  SIZ(res) = res_size;
+	  TMP_FREE;
+	}
+    }
 }
diff -r 0cb65d333949 -r bd031c4d736b mpz/ior.c
--- a/mpz/ior.c	Tue Jan 16 07:37:51 2018 +0100
+++ b/mpz/ior.c	Wed Jan 17 05:34:55 2018 +0100
@@ -39,7 +39,6 @@
   mp_ptr res_ptr;
   mp_size_t res_size;
   mp_size_t i;
-  TMP_DECL;
 
   op1_size = SIZ(op1);
   op2_size = SIZ(op2);
@@ -66,14 +65,16 @@
 	mpn_ior_n (res_ptr, op1_ptr, PTR(op2), op2_size);
 
       SIZ(res) = op1_size;
-      return;
     }
   else
     {
+      mp_ptr opx;
+      TMP_DECL;
+
       TMP_MARK;
       if (op1_size < 0)
 	{
-	  mp_ptr opx, opy;
+	  mp_ptr opy;
 
 	  /* Both operands are negative, so will be the result.
 	     -((-OP1) | (-OP2)) = -(~(OP1 - 1) | ~(OP2 - 1)) =
@@ -115,72 +116,69 @@
 	    }
 
 	  SIZ(res) = -res_size;
-	  TMP_FREE;
-	  return;
 	}
-    }
-
-  {
-    mp_ptr opx;
-    mp_limb_t cy;
-    mp_size_t count;
-
-    /* Operand 2 negative, so will be the result.
-       -(OP1 | (-OP2)) = -(OP1 | ~(OP2 - 1)) =
-       = ~(OP1 | ~(OP2 - 1)) + 1 =
-       = (~OP1 & (OP2 - 1)) + 1      */
-
-    op2_size = -op2_size;
-
-    res_ptr = MPZ_REALLOC (res, op2_size);
-    op1_ptr = PTR(op1);
-
-    opx = TMP_ALLOC_LIMBS (op2_size);
-    mpn_sub_1 (opx, PTR(op2), op2_size, (mp_limb_t) 1);
-    op2_ptr = opx;
-    op2_size -= op2_ptr[op2_size - 1] == 0;
-
-    if (op1_size >= op2_size)
-      {
-	/* We can just ignore the part of OP1 that stretches above OP2,
-	   because the result limbs are zero there.  */


More information about the gmp-commit mailing list