[Gmp-commit] /var/hg/gmp: Rewrite mpz reuse.c.

mercurial at gmplib.org mercurial at gmplib.org
Wed May 23 23:34:56 CEST 2012


details:   /var/hg/gmp/rev/5efefa9fff4e
changeset: 15000:5efefa9fff4e
user:      Torbjorn Granlund <tege at gmplib.org>
date:      Wed May 23 23:34:53 2012 +0200
description:
Rewrite mpz reuse.c.

diffstat:

 ChangeLog         |    4 +
 tests/mpz/reuse.c |  305 ++++++++++++++++++++++++++++-------------------------
 2 files changed, 164 insertions(+), 145 deletions(-)

diffs (truncated from 550 to 300 lines):

diff -r 035b9bc70f68 -r 5efefa9fff4e ChangeLog
--- a/ChangeLog	Wed May 23 23:21:13 2012 +0200
+++ b/ChangeLog	Wed May 23 23:34:53 2012 +0200
@@ -1,3 +1,7 @@
+2012-05-23  Torbjorn Granlund  <tege at gmplib.org>
+
+	* tests/mpz/reuse.c: Major rewrite.
+
 2012-05-23 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* mpz/sqrt.c: Further simplify.
diff -r 035b9bc70f68 -r 5efefa9fff4e tests/mpz/reuse.c
--- a/tests/mpz/reuse.c	Wed May 23 23:21:13 2012 +0200
+++ b/tests/mpz/reuse.c	Wed May 23 23:34:53 2012 +0200
@@ -6,7 +6,8 @@
 	mpz_mul_si
 	mpz_addmul_ui (should this really allow a+=a*c?)
 
-Copyright 1996, 1999, 2000, 2001, 2002, 2009 Free Software Foundation, Inc.
+Copyright 1996, 1999, 2000, 2001, 2002, 2009, 2012 Free Software Foundation,
+Inc.
 
 This file is part of the GNU MP Library.
 
@@ -26,6 +27,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "gmp.h"
 #include "gmp-impl.h"
@@ -67,125 +69,140 @@
     mpz_set_ui (r, 0);
 }
 
-dss_func dss_funcs[] =
-{
-  mpz_add, mpz_sub, mpz_mul,
-  mpz_cdiv_q, mpz_cdiv_r, mpz_fdiv_q, mpz_fdiv_r, mpz_tdiv_q, mpz_tdiv_r, mpz_mod,
-  mpz_xinvert,
-  mpz_gcd, mpz_lcm, mpz_and, mpz_ior, mpz_xor
-};
-const char *dss_func_names[] =
-{
-  "mpz_add", "mpz_sub", "mpz_mul",
-  "mpz_cdiv_q", "mpz_cdiv_r", "mpz_fdiv_q", "mpz_fdiv_r", "mpz_tdiv_q", "mpz_tdiv_r", "mpz_mod",
-  "mpz_xinvert",
-  "mpz_gcd", "mpz_lcm", "mpz_and", "mpz_ior", "mpz_xor"
-};
-char dss_func_division[] = {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0};
+struct {
+  dss_func fptr;
+  const char *fname;
+  int isdivision;
+  int isslow;
+} dss[] =
+  { { mpz_add,     "mpz_add",	  0, 0 },
+    { mpz_sub,     "mpz_sub",	  0, 0 },
+    { mpz_mul,     "mpz_mul",	  0, 0 },
+    { mpz_cdiv_q,  "mpz_cdiv_q",  1, 0 },
+    { mpz_cdiv_r,  "mpz_cdiv_r",  1, 0 },
+    { mpz_fdiv_q,  "mpz_fdiv_q",  1, 0 },
+    { mpz_fdiv_r,  "mpz_fdiv_r",  1, 0 },
+    { mpz_tdiv_q,  "mpz_tdiv_q",  1, 0 },
+    { mpz_tdiv_r,  "mpz_tdiv_r",  1, 0 },
+    { mpz_mod,     "mpz_mod",	  1, 0 },
+    { mpz_xinvert, "mpz_xinvert", 1, 1 },
+    { mpz_gcd,     "mpz_gcd",	  0, 1 },
+    { mpz_lcm,     "mpz_lcm",	  0, 1 },
+    { mpz_and,     "mpz_and",	  0, 0 },
+    { mpz_ior,     "mpz_ior",	  0, 0 },
+    { mpz_xor,     "mpz_xor",     0, 0 }
+  };
 
-dsi_func dsi_funcs[] =
+
+struct {
+  dsi_func fptr;
+  const char *fname;
+  int mod;
+} dsi[] =
 {
   /* Don't change order here without changing the code in main(). */
-  mpz_add_ui, mpz_mul_ui, mpz_sub_ui,
-  mpz_fdiv_q_2exp, mpz_fdiv_r_2exp,
-  mpz_cdiv_q_2exp, mpz_cdiv_r_2exp,
-  mpz_tdiv_q_2exp, mpz_tdiv_r_2exp,
-  mpz_mul_2exp,
-  mpz_pow_ui
-};
-const char *dsi_func_names[] =
-{
-  "mpz_add_ui", "mpz_mul_ui", "mpz_sub_ui",
-  "mpz_fdiv_q_2exp", "mpz_fdiv_r_2exp",
-  "mpz_cdiv_q_2exp", "mpz_cdiv_r_2exp",
-  "mpz_tdiv_q_2exp", "mpz_tdiv_r_2exp",
-  "mpz_mul_2exp",
-  "mpz_pow_ui"
+  { mpz_add_ui,         "mpz_add_ui",	     0 },
+  { mpz_mul_ui,	     	"mpz_mul_ui",	     0 },
+  { mpz_sub_ui,	     	"mpz_sub_ui",	     0 },
+  { mpz_fdiv_q_2exp,    "mpz_fdiv_q_2exp",   0x1000 },
+  { mpz_fdiv_r_2exp,    "mpz_fdiv_r_2exp",   0x1000 },
+  { mpz_cdiv_q_2exp,    "mpz_cdiv_q_2exp",   0x1000 },
+  { mpz_cdiv_r_2exp,    "mpz_cdiv_r_2exp",   0x1000 },
+  { mpz_tdiv_q_2exp,    "mpz_tdiv_q_2exp",   0x1000 },
+  { mpz_tdiv_r_2exp,    "mpz_tdiv_r_2exp",   0x1000 },
+  { mpz_mul_2exp,	"mpz_mul_2exp",      0x100 },
+  { mpz_pow_ui,	     	"mpz_pow_ui",        0x10 }
 };
 
-dsi_div_func dsi_div_funcs[] =
+struct {
+  dsi_div_func fptr;
+  const char *fname;
+} dsi_div[] =
 {
-  mpz_cdiv_q_ui, mpz_cdiv_r_ui,
-  mpz_fdiv_q_ui, mpz_fdiv_r_ui,
-  mpz_tdiv_q_ui, mpz_tdiv_r_ui
-};
-const char *dsi_div_func_names[] =
-{
-  "mpz_cdiv_q_ui", "mpz_cdiv_r_ui",
-  "mpz_fdiv_q_ui", "mpz_fdiv_r_ui",
-  "mpz_tdiv_q_ui", "mpz_tdiv_r_ui"
+  { mpz_cdiv_q_ui,       "mpz_cdiv_q_ui" },
+  { mpz_cdiv_r_ui,       "mpz_cdiv_r_ui" },
+  { mpz_fdiv_q_ui,       "mpz_fdiv_q_ui" },
+  { mpz_fdiv_r_ui,       "mpz_fdiv_r_ui" },
+  { mpz_tdiv_q_ui,       "mpz_tdiv_q_ui" },
+  { mpz_tdiv_r_ui,       "mpz_tdiv_r_ui" }
 };
 
-ddsi_div_func ddsi_div_funcs[] =
+struct {
+  ddsi_div_func fptr;
+  const char *fname;
+  int isslow;
+} ddsi_div[] =
 {
-  mpz_cdiv_qr_ui,
-  mpz_fdiv_qr_ui,
-  mpz_tdiv_qr_ui
-};
-const char *ddsi_div_func_names[] =
-{
-  "mpz_cdiv_qr_ui",
-  "mpz_fdiv_qr_ui",
-  "mpz_tdiv_qr_ui"
+  { mpz_cdiv_qr_ui,     "mpz_cdiv_qr_ui",    0 },
+  { mpz_fdiv_qr_ui,     "mpz_fdiv_qr_ui",    0 },
+  { mpz_tdiv_qr_ui,     "mpz_tdiv_qr_ui",    0 },
 };
 
-ddss_div_func ddss_div_funcs[] =
+
+struct {
+  ddss_div_func fptr;
+  const char *fname;
+  int isslow;
+} ddss_div[] =
 {
-  mpz_cdiv_qr,
-  mpz_fdiv_qr,
-  mpz_tdiv_qr
-};
-const char *ddss_div_func_names[] =
-{
-  "mpz_cdiv_qr",
-  "mpz_fdiv_qr",
-  "mpz_tdiv_qr"
+  { mpz_cdiv_qr,  "mpz_cdiv_qr",    0 },
+  { mpz_fdiv_qr,  "mpz_fdiv_qr",    0 },
+  { mpz_tdiv_qr,  "mpz_tdiv_qr",    0 },
 };
 
-ds_func ds_funcs[] =
+struct {
+  ds_func fptr;
+  const char *fname;
+  int nonneg;
+} ds[] =
 {
-  mpz_abs, mpz_com, mpz_neg, mpz_sqrt
-};
-const char *ds_func_names[] =
-{
-  "mpz_abs", "mpz_com", "mpz_neg", "mpz_sqrt"
+  { mpz_abs,    "mpz_abs",    0 },
+  { mpz_com,    "mpz_com",    0 },
+  { mpz_neg,    "mpz_neg",    0 },
+  { mpz_sqrt,   "mpz_sqrt",   1 },
 };
 
+#define FAIL(class,indx,op1,op2,op3)					\
+  do {									\
+    dump (class[indx].fname, op1, op2, op3);				\
+    exit (1);								\
+  } while (0)
 
-/* Really use `defined (__STDC__)' here; we want it to be true for Sun C */
-#if defined (__STDC__) || defined (__cplusplus)
-#define FAIL(class,indx,op1,op2,op3) \
+#define FAIL2(fname,op1,op2,op3)					\
   do {									\
-  class##_funcs[indx] = 0;						\
-  dump (class##_func_names[indx], op1, op2, op3);			\
-  failures++;								\
+    dump (#fname, op1, op2, op3);					\
+    exit (1);								\
   } while (0)
-#define FAIL2(fname,op1,op2,op3) \
+
+
+#define INVOKE_RRS(desc,r1,r2,i1)					\
   do {									\
-  dump (#fname, op1, op2, op3);						\
-  failures++;								\
+    if (pass & 1) _mpz_realloc (r1, ABSIZ(r1));				\
+    if (pass & 2) _mpz_realloc (r2, ABSIZ(r2));				\
+    (desc).fptr (r1, r2, i1);						\
   } while (0)
-#else
-#define FAIL(class,indx,op1,op2,op3) \
+#define INVOKE_RS(desc,r1,i1)						\
   do {									\
-  class/**/_funcs[indx] = 0;						\
-  dump (class/**/_func_names[indx], op1, op2, op3);			\
-  failures++;								\
+    if (pass & 1) _mpz_realloc (r1, ABSIZ(r1));				\
+    (desc).fptr (r1, i1);						\
   } while (0)
-#define FAIL2(fname,op1,op2,op3) \
+#define INVOKE_RRSS(desc,r1,r2,i1,i2)					\
   do {									\
-  dump ("fname", op1, op2, op3);					\
-  failures++;								\
+    if (pass & 1) _mpz_realloc (r1, ABSIZ(r1));				\
+    if (pass & 2) _mpz_realloc (r2, ABSIZ(r2));				\
+    (desc).fptr (r1, r2, i1, i2);					\
   } while (0)
-#endif
-
-
+#define INVOKE_RSS(desc,r1,i1,i2)					\
+  do {									\
+    if (pass & 1) _mpz_realloc (r1, ABSIZ(r1));				\
+    (desc).fptr (r1, i1, i2);						\
+  } while (0)
 
 int
 main (int argc, char **argv)
 {
   int i;
-  int pass, reps = 100;
+  int pass, reps = 200;
   mpz_t in1, in2, in3;
   unsigned long int in2i;
   mp_size_t size;
@@ -218,8 +235,14 @@
 
   for (pass = 1; pass <= reps; pass++)
     {
+      if (isatty (fileno (stdout)))
+	{
+	  printf ("\r%c", "-/|\\"[pass % 4]);
+	  fflush (stdout);
+	}
+
       mpz_urandomb (bs, rands, 32);
-      size_range = mpz_get_ui (bs) % 17 + 2;
+      size_range = mpz_get_ui (bs) % 21 + 2;
 
       mpz_urandomb (bs, rands, size_range);
       size = mpz_get_ui (bs);
@@ -242,82 +265,77 @@
       if ((bsi & 1) != 0)
 	mpz_neg (in3, in3);
 
-      for (i = 0; i < sizeof (dss_funcs) / sizeof (dss_func); i++)
+      for (i = 0; i < numberof (dss); i++)
 	{
-	  if (dss_funcs[i] == 0)
+	  if (dss[i].isdivision && mpz_sgn (in2) == 0)
 	    continue;
-	  if (dss_func_division[i] && mpz_sgn (in2) == 0)
+	  if (dss[i].isslow && size_range > 19)
 	    continue;
 
-	  (dss_funcs[i]) (ref1, in1, in2);
+	  (dss[i].fptr) (ref1, in1, in2);
 	  MPZ_CHECK_FORMAT (ref1);
 
 	  mpz_set (res1, in1);
-	  (dss_funcs[i]) (res1, res1, in2);
+	  INVOKE_RSS (dss[i], res1, res1, in2);
 	  MPZ_CHECK_FORMAT (res1);
 	  if (mpz_cmp (ref1, res1) != 0)


More information about the gmp-commit mailing list