mpz_remove_ui

Andrew Friedley (CNS Student Support) saai@student.cns.uni.edu
Mon, 7 Jul 2003 19:51:44 -0500


--6c2NcOVqGQ03X4Wi
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

May I suggest the addition of a variation of mpz_remove, mpz_remove_ui?
As is apparent in the attached patch, the argument f is an unsigned int,
rather than an mpz_t.  I found this useful in my trial division
algorithm where my candidate factors are unsigned integers, rather than
full blown mpz_t's.

-- 
Andrew Friedley
CNS Computing - Student Staff
University of Northern Iowa

--6c2NcOVqGQ03X4Wi
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="gmp_remove_ui.diff"

diff -Naur gmp-4.1.2/gmp-h.in gmp-4.1.2-mod/gmp-h.in
--- gmp-4.1.2/gmp-h.in	2002-12-17 16:04:46.000000000 -0600
+++ gmp-4.1.2-mod/gmp-h.in	2003-07-07 19:37:49.000000000 -0500
@@ -941,6 +941,9 @@
 #define mpz_remove __gmpz_remove
 __GMP_DECLSPEC unsigned long int mpz_remove __GMP_PROTO ((mpz_ptr, mpz_srcptr, mpz_srcptr));
 
+#define mpz_remove_ui __gmpz_remove_ui
+__GMP_DECLSPEC unsigned long int mpz_remove_ui __GMP_PROTO ((mpz_ptr, mpz_srcptr, unsigned int));
+
 #define mpz_root __gmpz_root
 __GMP_DECLSPEC int mpz_root __GMP_PROTO ((mpz_ptr, mpz_srcptr, unsigned long int));
 
diff -Naur gmp-4.1.2/mpz/remove.c gmp-4.1.2-mod/mpz/remove.c
--- gmp-4.1.2/mpz/remove.c	2002-08-30 19:32:05.000000000 -0500
+++ gmp-4.1.2-mod/mpz/remove.c	2003-07-07 19:36:57.000000000 -0500
@@ -92,3 +92,74 @@
   mpz_clear (rem);
   return pwr;
 }
+
+unsigned long int
+mpz_remove_ui (mpz_ptr dest, mpz_srcptr src, unsigned int f)
+{
+  mpz_t fpow[40];		/* inexhaustible...until year 2020 or so */
+  mpz_t x, rem;
+  unsigned long int pwr;
+  int p;
+
+  if (f <= 0)
+    DIVIDE_BY_ZERO;
+
+  if (SIZ (src) == 0)
+    {
+      if (src != dest)
+        mpz_set (dest, src);
+      return 0;
+    }
+
+  if (f == 2)
+    {
+      unsigned long int s0;
+      s0 = mpz_scan1 (src, 0);
+      mpz_div_2exp (dest, src, s0);
+      return s0;
+    }
+
+  /* We could perhaps compute mpz_scan1(src,0)/mpz_scan1(f,0).  It is an
+     upper bound of the result we're seeking.  We could also shift down the
+     operands so that they become odd, to make intermediate values smaller.  */
+
+  mpz_init (rem);
+  mpz_init (x);
+
+  pwr = 0;
+  mpz_init (fpow[0]);
+  mpz_set_ui (fpow[0], f);
+  mpz_set (dest, src);
+
+  /* Divide by f, f^2, ..., f^(2^k) until we get a remainder for f^(2^k).  */
+  for (p = 0;; p++)
+    {
+      mpz_tdiv_qr (x, rem, dest, fpow[p]);
+      if (SIZ (rem) != 0)
+	break;
+      mpz_init (fpow[p + 1]);
+      mpz_mul (fpow[p + 1], fpow[p], fpow[p]);
+      mpz_set (dest, x);
+    }
+
+  pwr = (1 << p) - 1;
+
+  mpz_clear (fpow[p]);
+
+  /* Divide by f^(2^(k-1)), f^(2^(k-2)), ..., f for all divisors that give a
+     zero remainder.  */
+  while (--p >= 0)
+    {
+      mpz_tdiv_qr (x, rem, dest, fpow[p]);
+      if (SIZ (rem) == 0)
+	{
+	  pwr += 1 << p;
+	  mpz_set (dest, x);
+	}
+      mpz_clear (fpow[p]);
+    }
+
+  mpz_clear (x);
+  mpz_clear (rem);
+  return pwr;
+}

--6c2NcOVqGQ03X4Wi--