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

mercurial at gmplib.org mercurial at gmplib.org
Mon Feb 3 03:30:40 UTC 2014


details:   /var/hg/gmp/rev/ebe76f3fd99e
changeset: 16272:ebe76f3fd99e
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Feb 03 04:22:34 2014 +0100
description:
mini-gmp/tests/t-root.c: test also 1-th root, allow perfect powers.

details:   /var/hg/gmp/rev/34a519b4e126
changeset: 16273:34a519b4e126
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Feb 03 04:23:29 2014 +0100
description:
mini-gmp: add mpz_perfect_square_p

details:   /var/hg/gmp/rev/c242aabfe876
changeset: 16274:c242aabfe876
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Feb 03 04:24:07 2014 +0100
description:
mini-gmp/tests/t-sqrt.c: test mpz_perfect_square_p

details:   /var/hg/gmp/rev/b10663184cbc
changeset: 16275:b10663184cbc
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Feb 03 04:29:38 2014 +0100
description:
ChangeLog

diffstat:

 ChangeLog               |  11 +++++++++++
 mini-gmp/mini-gmp.c     |  14 ++++++++++++--
 mini-gmp/mini-gmp.h     |   1 +
 mini-gmp/tests/t-root.c |   4 ++--
 mini-gmp/tests/t-sqrt.c |  35 +++++++++++++++++++++++++++++++++++
 5 files changed, 61 insertions(+), 4 deletions(-)

diffs (140 lines):

diff -r 2c47f90ebae6 -r b10663184cbc ChangeLog
--- a/ChangeLog	Sun Feb 02 15:34:35 2014 +0100
+++ b/ChangeLog	Mon Feb 03 04:29:38 2014 +0100
@@ -1,3 +1,14 @@
+2014-02-03 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mini-gmp/mini-gmp.c (mpn_rootrem): Allow NULL argument.
+
+	* mini-gmp/mini-gmp.c (mpn_zero): New function.
+	(mpz_perfect_square_t): New function.
+	* mini-gmp/mini-gmp.h: Declare them.
+
+	* mini-gmp/tests/t-sqrt.c: test mpz_perfect_square_p
+	* mini-gmp/tests/t-root.c: test also 1-th root, allow perfect powers.
+
 2014-01-29  Torbjorn Granlund  <tege at gmplib.org>
 
 	* doc/gmp.texi (Floating-point Functions): Revise.
diff -r 2c47f90ebae6 -r b10663184cbc mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Sun Feb 02 15:34:35 2014 +0100
+++ b/mini-gmp/mini-gmp.c	Mon Feb 03 04:29:38 2014 +0100
@@ -3148,7 +3148,8 @@
     gmp_die ("mpz_rootrem: Zeroth root.");
 
   if (mpz_cmpabs_ui (y, 1) <= 0) {
-    mpz_set (x, y);
+    if (x)
+      mpz_set (x, y);
     if (r)
       r->_mp_size = 0;
     return;
@@ -3192,7 +3193,8 @@
     mpz_pow_ui (t, u, z);
     mpz_sub (r, y, t);
   }
-  mpz_swap (x, u);
+  if (x)
+    mpz_swap (x, u);
   mpz_clear (u);
   mpz_clear (t);
 }
@@ -3224,6 +3226,14 @@
   mpz_rootrem (s, NULL, u, 2);
 }
 
+int
+mpz_perfect_square_p (const mpz_t u)
+{
+  if (u->_mp_size <= 0)
+    return (u->_mp_size == 0);
+  else
+    return mpz_root (NULL, u, 2);
+}
 
 /* Combinatorics */
 
diff -r 2c47f90ebae6 -r b10663184cbc mini-gmp/mini-gmp.h
--- a/mini-gmp/mini-gmp.h	Sun Feb 02 15:34:35 2014 +0100
+++ b/mini-gmp/mini-gmp.h	Mon Feb 03 04:29:38 2014 +0100
@@ -199,6 +199,7 @@
 
 void mpz_sqrtrem (mpz_t, mpz_t, const mpz_t);
 void mpz_sqrt (mpz_t, const mpz_t);
+int mpz_perfect_square_p (const mpz_t);
 
 void mpz_pow_ui (mpz_t, const mpz_t, unsigned long);
 void mpz_ui_pow_ui (mpz_t, unsigned long, unsigned long);
diff -r 2c47f90ebae6 -r b10663184cbc mini-gmp/tests/t-root.c
--- a/mini-gmp/tests/t-root.c	Sun Feb 02 15:34:35 2014 +0100
+++ b/mini-gmp/tests/t-root.c	Mon Feb 03 04:29:38 2014 +0100
@@ -46,7 +46,7 @@
   else
     mpz_pow_ui (t, s, z);
   mpz_sub (t, u, t);
-  if (mpz_sgn (t) != mpz_sgn(u) || mpz_cmp (t, r) != 0)
+  if ((mpz_sgn (t) != mpz_sgn(u) && mpz_sgn (t) != 0) || mpz_cmp (t, r) != 0)
     {
       mpz_clear (t);
       return 0;
@@ -82,7 +82,7 @@
     {
       mini_rrandomb (u, MAXBITS);
       mini_rrandomb (bs, 12);
-      e = mpz_getlimbn (bs, 0) % mpz_sizeinbase (u, 2) + 2;
+      e = mpz_getlimbn (bs, 0) % mpz_sizeinbase (u, 2) + 1;
       if ((e & 1) && (mpz_getlimbn (bs, 0) & (1L<<10)))
 	mpz_neg (u, u);
       mpz_rootrem (s, r, u, e);
diff -r 2c47f90ebae6 -r b10663184cbc mini-gmp/tests/t-sqrt.c
--- a/mini-gmp/tests/t-sqrt.c	Sun Feb 02 15:34:35 2014 +0100
+++ b/mini-gmp/tests/t-sqrt.c	Mon Feb 03 04:29:38 2014 +0100
@@ -70,6 +70,20 @@
   mpz_init (s);
   mpz_init (r);
 
+  mpz_set_si (u, -1);
+  if (mpz_perfect_square_p (u))
+    {
+      fprintf (stderr, "mpz_perfect_square_p failed on -1.\n");
+      abort ();
+    }
+
+  mpz_set_ui (u, 0);
+  if (!mpz_perfect_square_p (u))
+    {
+      fprintf (stderr, "mpz_perfect_square_p failed on 0.\n");
+      abort ();
+    }
+
   for (i = 0; i < COUNT; i++)
     {
       mini_rrandomb (u, MAXBITS);
@@ -83,6 +97,27 @@
 	  dump ("rem", r);
 	  abort ();
 	}
+
+      if (mpz_sgn (r) == 0) {
+	mpz_neg (u, u);
+	mpz_sub_ui (u, u, 1);
+      }
+
+      if (mpz_perfect_square_p (u))
+	{
+	  fprintf (stderr, "mpz_perfect_square_p failed on non square:\n");
+	  dump ("u", u);
+	  abort ();
+	}
+
+      mpz_mul (u, s, s);
+      if (!mpz_perfect_square_p (u))
+	{
+	  fprintf (stderr, "mpz_perfect_square_p failed on square:\n");
+	  dump ("u", u);
+	  abort ();
+	}
+
     }
   mpz_clear (u);
   mpz_clear (s);


More information about the gmp-commit mailing list