[Gmp-commit] /var/hg/gmp: mpz/remove.c: Support negative divisor, and test them.

mercurial at gmplib.org mercurial at gmplib.org
Thu May 24 10:12:34 CEST 2012


details:   /var/hg/gmp/rev/19070fb25759
changeset: 15001:19070fb25759
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Thu May 24 10:12:28 2012 +0200
description:
mpz/remove.c: Support negative divisor, and test them.

diffstat:

 ChangeLog            |   5 +++++
 mpz/remove.c         |   9 +++++----
 tests/mpz/t-remove.c |  21 ++++++++++++++++++++-
 3 files changed, 30 insertions(+), 5 deletions(-)

diffs (77 lines):

diff -r 5efefa9fff4e -r 19070fb25759 ChangeLog
--- a/ChangeLog	Wed May 23 23:34:53 2012 +0200
+++ b/ChangeLog	Thu May 24 10:12:28 2012 +0200
@@ -1,3 +1,8 @@
+2012-05-24 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mpz/remove.c: Support negative divisor.
+	* tests/mpz/t-remove.c: Test zero dividend and negative divisor.
+
 2012-05-23  Torbjorn Granlund  <tege at gmplib.org>
 
 	* tests/mpz/reuse.c: Major rewrite.
diff -r 5efefa9fff4e -r 19070fb25759 mpz/remove.c
--- a/mpz/remove.c	Wed May 23 23:34:53 2012 +0200
+++ b/mpz/remove.c	Thu May 24 10:12:28 2012 +0200
@@ -31,18 +31,19 @@
   if (UNLIKELY (mpz_cmpabs_ui (f, 1) <= 0))
     DIVIDE_BY_ZERO;
 
-  if (SIZ (src) == 0)
+  if (UNLIKELY (SIZ (src) == 0))
     {
-      if (src != dest)
-	mpz_set (dest, src);
+      SIZ (dest) = 0;
       return 0;
     }
 
-  if (mpz_cmp_ui (f, 2) == 0)
+  if (mpz_cmpabs_ui (f, 2) == 0)
     {
       mp_bitcnt_t s0;
       s0 = mpz_scan1 (src, 0);
       mpz_div_2exp (dest, src, s0);
+      if (s0 & (SIZ (f) < 0)) /*((s0 % 2 == 1) && (SIZ (f) < 0))*/
+	mpz_neg (dest, dest);
       return s0;
     }
 
diff -r 5efefa9fff4e -r 19070fb25759 tests/mpz/t-remove.c
--- a/tests/mpz/t-remove.c	Wed May 23 23:34:53 2012 +0200
+++ b/tests/mpz/t-remove.c	Thu May 24 10:12:28 2012 +0200
@@ -47,7 +47,24 @@
   if (argc == 2)
     reps = atoi (argv[1]);
 
-  mpz_inits (bs, t, dest, refdest, dividend, divisor, NULL);
+  mpz_inits (bs, t, refdest, dividend, NULL);
+
+  mpz_init_set_si (dest, -3);
+  mpz_init_set_ui (divisor, 3);
+  pwr = mpz_remove (dest, dividend, divisor);
+
+  /* Perhaps we shouldn't check pwr... */
+  if (pwr != 0 || mpz_cmp_ui (dest, 0) != 0)
+    {
+      fprintf (stderr, "ERROR with zero test\n");
+      fprintf (stderr, "refpower = %lu\n", 0);
+      fprintf (stderr, "   power = %lu\n", pwr);
+      fprintf (stderr, "    op1 = "); debug_mp (dividend);
+      fprintf (stderr, "    op2 = "); debug_mp (divisor);
+      fprintf (stderr, "refdest = 0\n");
+      fprintf (stderr, "   dest = "); debug_mp (dest);
+      abort ();
+    }
 
   for (i = 0; i < reps; i++)
     {
@@ -68,6 +85,8 @@
 
       mpz_urandomb (bs, rands, 32);
       exp = mpz_get_ui (bs) % (5 + 10000 / mpz_sizeinbase (divisor, 2));
+      if (mpz_get_ui (bs) & 2)
+	mpz_neg (divisor, divisor);
       mpz_pow_ui (t, divisor, exp);
       mpz_mul (dividend, dividend, t);
 


More information about the gmp-commit mailing list