[Gmp-commit] /var/hg/gmp: mini-gmp: Test mpn_mul_n and mpn_sqr.

mercurial at gmplib.org mercurial at gmplib.org
Tue Jan 15 22:37:32 CET 2013


details:   /var/hg/gmp/rev/f130ae5dcdf2
changeset: 15280:f130ae5dcdf2
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Tue Jan 15 22:37:28 2013 +0100
description:
mini-gmp: Test mpn_mul_n and mpn_sqr.

diffstat:

 ChangeLog                    |  15 +++++++++++++++
 mini-gmp/tests/hex-random.c  |  38 ++++++++++++++++++++++++++++++++++++--
 mini-gmp/tests/hex-random.h  |   9 ++++++---
 mini-gmp/tests/mini-random.c |  21 ++++++++++++++++++---
 mini-gmp/tests/mini-random.h |   3 ++-
 mini-gmp/tests/t-add.c       |   2 +-
 mini-gmp/tests/t-gcd.c       |   2 +-
 mini-gmp/tests/t-lcm.c       |   2 +-
 mini-gmp/tests/t-logops.c    |   6 +++---
 mini-gmp/tests/t-mul.c       |  42 +++++++++++++++++++++++++++++++++++++++++-
 mini-gmp/tests/t-sub.c       |   2 +-
 11 files changed, 125 insertions(+), 17 deletions(-)

diffs (truncated from 310 to 300 lines):

diff -r 0aaa3aa007f9 -r f130ae5dcdf2 ChangeLog
--- a/ChangeLog	Tue Jan 15 22:24:38 2013 +0100
+++ b/ChangeLog	Tue Jan 15 22:37:28 2013 +0100
@@ -1,3 +1,18 @@
+2013-01-15  Niels Möller  <nisse at lysator.liu.se>
+
+	* mini-gmp/tests/t-mul.c (main): Test mpn_mul_n and mpn_sqr.
+
+	* mini-gmp/tests/hex-random.h (enum hex_random_op): New value
+	OP_SQR.
+
+	* mini-gmp/tests/mini-random.c (mini_random_op3): Renamed, from...
+	(mini_random_op): ... old name. Updated callers.
+	(mini_random_op2): New function.
+
+	* mini-gmp/tests/hex-random.c (hex_random_op3): Renamed, from...
+	(hex_random_op): ... old name. Updated callers.
+	(hex_random_op2): New function.
+
 2013-01-15 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* mini-gmp/tests/t-logops.c: Improve popcount/hamdist testing.
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/hex-random.c
--- a/mini-gmp/tests/hex-random.c	Tue Jan 15 22:24:38 2013 +0100
+++ b/mini-gmp/tests/hex-random.c	Tue Jan 15 22:37:28 2013 +0100
@@ -94,9 +94,43 @@
   return res;
 }
 
+void hex_random_op2 (enum hex_random_op op,  unsigned long maxbits,
+		     char **ap, char **rp)
+{
+  mpz_t a, r;
+  unsigned long abits;
+  unsigned signs;
+
+  mpz_init (a);
+  mpz_init (r);
+
+  abits = gmp_urandomb_ui (state, 32) % maxbits;
+
+  mpz_rrandomb (a, state, abits);
+
+  signs = gmp_urandomb_ui (state, 1);
+  if (signs & 1)
+    mpz_neg (a, a);
+
+  switch (op)
+    {
+    default:
+      abort ();
+    case OP_SQR:
+      mpz_mul (r, a, a);
+      break;
+    }
+
+  gmp_asprintf (ap, "%Zx", a);
+  gmp_asprintf (rp, "%Zx", r);
+
+  mpz_clear (a);
+  mpz_clear (r);
+}
+
 void
-hex_random_op (enum hex_random_op op,  unsigned long maxbits,
-	       char **ap, char **bp, char **rp)
+hex_random_op3 (enum hex_random_op op,  unsigned long maxbits,
+		char **ap, char **bp, char **rp)
 {
   mpz_t a, b, r;
   unsigned long abits, bbits;
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/hex-random.h
--- a/mini-gmp/tests/hex-random.h	Tue Jan 15 22:24:38 2013 +0100
+++ b/mini-gmp/tests/hex-random.h	Tue Jan 15 22:37:28 2013 +0100
@@ -19,7 +19,8 @@
 
 enum hex_random_op
   {
-    OP_ADD, OP_SUB, OP_MUL, OP_CDIV, OP_FDIV, OP_TDIV,
+    OP_ADD, OP_SUB, OP_MUL, OP_SQR,
+    OP_CDIV, OP_FDIV, OP_TDIV,
     OP_CDIV_Q_2, OP_CDIV_R_2,
     OP_FDIV_Q_2, OP_FDIV_R_2,
     OP_TDIV_Q_2,  OP_TDIV_R_2,
@@ -35,8 +36,10 @@
 			   int order, size_t size, int endian,
 			   unsigned long bits);
 
-void hex_random_op (enum hex_random_op op,  unsigned long maxbits,
-		    char **ap, char **bp, char **rp);
+void hex_random_op2 (enum hex_random_op op,  unsigned long maxbits,
+		     char **ap, char **rp);
+void hex_random_op3 (enum hex_random_op op,  unsigned long maxbits,
+		     char **ap, char **bp, char **rp);
 void hex_random_op4 (enum hex_random_op op,  unsigned long maxbits,
 		     char **ap, char **bp, char **rp, char **qp);
 void hex_random_bit_op (enum hex_random_op op, unsigned long maxbits,
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/mini-random.c
--- a/mini-gmp/tests/mini-random.c	Tue Jan 15 22:24:38 2013 +0100
+++ b/mini-gmp/tests/mini-random.c	Tue Jan 15 22:37:28 2013 +0100
@@ -61,14 +61,29 @@
 }
 
 void
-mini_random_op (enum hex_random_op op, unsigned long maxbits,
-		mpz_t a, mpz_t b, mpz_t r)
+mini_random_op2 (enum hex_random_op op, unsigned long maxbits,
+		 mpz_t a, mpz_t r)
+{
+  char *ap;
+  char *rp;
+
+  hex_random_op2 (op, maxbits, &ap, &rp);
+  set_str (a, ap);
+  set_str (r, rp);
+
+  free (ap);
+  free (rp);
+}
+
+void
+mini_random_op3 (enum hex_random_op op, unsigned long maxbits,
+		 mpz_t a, mpz_t b, mpz_t r)
 {
   char *ap;
   char *bp;
   char *rp;
 
-  hex_random_op (op, maxbits, &ap, &bp, &rp);
+  hex_random_op3 (op, maxbits, &ap, &bp, &rp);
   set_str (a, ap);
   set_str (b, bp);
   set_str (r, rp);
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/mini-random.h
--- a/mini-gmp/tests/mini-random.h	Tue Jan 15 22:24:38 2013 +0100
+++ b/mini-gmp/tests/mini-random.h	Tue Jan 15 22:37:28 2013 +0100
@@ -26,7 +26,8 @@
 			   int order, size_t size, int endian,
 			   unsigned long bits);
 
-void mini_random_op (enum hex_random_op,  unsigned long, mpz_t, mpz_t, mpz_t);
+void mini_random_op2 (enum hex_random_op,  unsigned long, mpz_t, mpz_t);
+void mini_random_op3 (enum hex_random_op,  unsigned long, mpz_t, mpz_t, mpz_t);
 void mini_random_op4 (enum hex_random_op, unsigned long, mpz_t, mpz_t, mpz_t, mpz_t);
 void mini_random_scan_op (enum hex_random_op, unsigned long, mpz_t, mp_bitcnt_t *, mp_bitcnt_t *);
 void mini_random_bit_op (enum hex_random_op, unsigned long, mpz_t, mp_bitcnt_t *, mpz_t);
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/t-add.c
--- a/mini-gmp/tests/t-add.c	Tue Jan 15 22:24:38 2013 +0100
+++ b/mini-gmp/tests/t-add.c	Tue Jan 15 22:37:28 2013 +0100
@@ -48,7 +48,7 @@
 
   for (i = 0; i < COUNT; i++)
     {
-      mini_random_op (OP_ADD, MAXBITS, a, b, ref);
+      mini_random_op3 (OP_ADD, MAXBITS, a, b, ref);
       mpz_add (res, a, b);
       if (mpz_cmp (res, ref))
 	{
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/t-gcd.c
--- a/mini-gmp/tests/t-gcd.c	Tue Jan 15 22:24:38 2013 +0100
+++ b/mini-gmp/tests/t-gcd.c	Tue Jan 15 22:37:28 2013 +0100
@@ -119,7 +119,7 @@
 
   for (i = 0; i < COUNT; i++)
     {
-      mini_random_op (OP_GCD, MAXBITS, a, b, s);
+      mini_random_op3 (OP_GCD, MAXBITS, a, b, s);
       mpz_gcd (g, a, b);
       if (mpz_cmp (g, s))
 	{
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/t-lcm.c
--- a/mini-gmp/tests/t-lcm.c	Tue Jan 15 22:24:38 2013 +0100
+++ b/mini-gmp/tests/t-lcm.c	Tue Jan 15 22:37:28 2013 +0100
@@ -49,7 +49,7 @@
 
   for (i = 0; i < COUNT; i++)
     {
-      mini_random_op (OP_LCM, MAXBITS, a, b, s);
+      mini_random_op3 (OP_LCM, MAXBITS, a, b, s);
       mpz_lcm (g, a, b);
       if (mpz_cmp (g, s))
 	{
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/t-logops.c
--- a/mini-gmp/tests/t-logops.c	Tue Jan 15 22:24:38 2013 +0100
+++ b/mini-gmp/tests/t-logops.c	Tue Jan 15 22:37:28 2013 +0100
@@ -50,7 +50,7 @@
 
   for (i = 0; i < COUNT; i++)
     {
-      mini_random_op (OP_AND, MAXBITS, a, b, ref);
+      mini_random_op3 (OP_AND, MAXBITS, a, b, ref);
       mpz_and (res, a, b);
       if (mpz_cmp (res, ref))
 	{
@@ -62,7 +62,7 @@
 	  abort ();
 	}
 
-      mini_random_op (OP_IOR, MAXBITS, a, b, ref);
+      mini_random_op3 (OP_IOR, MAXBITS, a, b, ref);
       mpz_ior (res, a, b);
       if (mpz_cmp (res, ref))
 	{
@@ -74,7 +74,7 @@
 	  abort ();
 	}
 
-      mini_random_op (OP_XOR, MAXBITS, a, b, ref);
+      mini_random_op3 (OP_XOR, MAXBITS, a, b, ref);
       mpz_xor (res, a, b);
       if (mpz_cmp (res, ref))
 	{
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/t-mul.c
--- a/mini-gmp/tests/t-mul.c	Tue Jan 15 22:24:38 2013 +0100
+++ b/mini-gmp/tests/t-mul.c	Tue Jan 15 22:37:28 2013 +0100
@@ -20,12 +20,16 @@
 #include <limits.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "mini-random.h"
 
 #define MAXBITS 400
 #define COUNT 10000
 
+#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
+#define MAXLIMBS ((MAXBITS + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS)
+
 static void
 dump (const char *label, const mpz_t x)
 {
@@ -39,6 +43,8 @@
 {
   unsigned i;
   mpz_t a, b, res, res_ui, ref;
+  mp_limb_t t[2*MAXLIMBS];
+  mp_size_t an, rn;
 
   hex_random_init ();
 
@@ -50,7 +56,7 @@
 
   for (i = 0; i < COUNT; i++)
     {
-      mini_random_op (OP_MUL, MAXBITS, a, b, ref);
+      mini_random_op3 (OP_MUL, MAXBITS, a, b, ref);
       mpz_mul (res, a, b);
       if (mpz_cmp (res, ref))
 	{
@@ -61,6 +67,24 @@
 	  dump ("ref", ref);
 	  abort ();
 	}
+      if (mpz_size (a) == mpz_size (b))
+	{
+	  memset (t, 0, sizeof(t));
+	  an = mpz_size (a);
+	  if (an > 0)
+	    {
+	      mpn_mul_n (t, a->_mp_d, b->_mp_d, an);
+	      rn = 2*an - (res->_mp_d[2*an-1] == 0);
+	      if (rn != mpz_size (ref) || mpn_cmp (t, ref->_mp_d, rn))
+		{
+		  fprintf (stderr, "mpn_mul_n failed:\n");
+		  dump ("a", a);
+		  dump ("b", b);
+		  dump ("ref", ref);
+		  abort ();
+		}
+	    }
+	}
       if (mpz_fits_slong_p (b)) {
 	mpz_mul_si (res_ui, a, mpz_get_si (b));
 	if (mpz_cmp (res_ui, ref))
@@ -73,6 +97,22 @@
 	    abort ();
 	  }
       }
+      mini_random_op2 (OP_SQR, MAXBITS, a, ref);
+      an = mpz_size (a);
+      if (an > 0)
+	{
+	  memset (t, 0, sizeof(t));
+	  mpn_sqr (t, a->_mp_d, an);
+
+	  rn = 2*an - (t[2*an-1] == 0);
+	  if (rn != mpz_size (ref) || mpn_cmp (t, ref->_mp_d, rn))
+	    {
+	      fprintf (stderr, "mpn (squaring) failed:\n");
+	      dump ("a", a);
+	      dump ("ref", ref);
+	      abort ();
+	    }
+	}
     }
   mpz_clear (a);
   mpz_clear (b);
diff -r 0aaa3aa007f9 -r f130ae5dcdf2 mini-gmp/tests/t-sub.c
--- a/mini-gmp/tests/t-sub.c	Tue Jan 15 22:24:38 2013 +0100


More information about the gmp-commit mailing list