Torbjorn Granlund <t...@gmplib.org> writes:

> It would be nicer to have it in a separate directory, both for GMP
> directory cleanness and for easy extraction by users.

Right, I guess that makes the most sense.

After copying the mini-gmp directory, I can build gmp with the attached
patch. For the functions in dumbmp.c:

* Most aren't needed any more.

* Functions used in only one file are copied to that file.

* The remaining few functions are moved to a new file, named
  mini-gmp-extra.c, at the gmp top-level.

Functions which get by with just mini-gmp include mini-gmp/mini-gmp.c,
the rest instead include mini-gmp-extra.c.

I did some other minor changes to the files: Use assert rather than
ASSERT. Use memmove rather than mem_copyi. Deleted casts of the return
value from xmalloc, instead expecting the return value to be of type
void *. In mini-gmp-extra.c, I defined xmalloc as an alias for
gmp_default_xalloc, an internal function in mini-gmp.c, rather than
including yet another copy of the same thing.

I'm not sure how to best do the automakery to get mini-gmp included in
the gmp distribution. Is it good enough to just add the mini-gmp
directory to EXTRA_DIST? Or do we need a list or glob pattern for the
wanted files somewhere? I'd like to avoid that backup files, build
products etc from the mini-gmp directory are picked up by accident, and
I'm not sure how smart the automake rules are for directories listed in
EXTRA_DIST.

> From GMP's perspective, I don't think mini-gmp unit testing should be
> necessary.

I see. But it would be nice with a top-level make targat check-mini-gmp,
to build and test mini-gmp with the same configuration (compiler,
builddir, etc) as set up for gmp.

Regards,
/Niels

diff -r b641c8181188 gen-bases.c
--- a/gen-bases.c       Wed Feb 15 14:46:40 2012 +0100
+++ b/gen-bases.c       Thu Feb 16 11:44:36 2012 +0100
@@ -20,7 +20,7 @@
 
 #include <math.h>
 
-#include "dumbmp.c"
+#include "mini-gmp/mini-gmp.c"
 
 
 int    chars_per_limb;
diff -r b641c8181188 gen-fac_ui.c
--- a/gen-fac_ui.c      Wed Feb 15 14:46:40 2012 +0100
+++ b/gen-fac_ui.c      Thu Feb 16 11:44:36 2012 +0100
@@ -20,7 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "dumbmp.c"
+#include "mini-gmp/mini-gmp.c"
 
 
 /* sets x=y*(y+2)*(y+4)*....*(y+2*(z-1))       */
diff -r b641c8181188 gen-fib.c
--- a/gen-fib.c Wed Feb 15 14:46:40 2012 +0100
+++ b/gen-fib.c Thu Feb 16 11:44:36 2012 +0100
@@ -18,7 +18,8 @@
 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
 
 #include <stdio.h>
-#include "dumbmp.c"
+
+#include "mini-gmp-extra.c"
 
 mpz_t  *f;
 int    fnum, fib_limit, luc_limit;
@@ -34,7 +35,7 @@
 
   /* fib(2n) > 2^n, so use 2n as a limit for the table size */
   falloc = 2 * numb_bits;
-  f = (mpz_t *) xmalloc (falloc * sizeof (*f));
+  f = xmalloc (falloc * sizeof (*f));
 
   mpz_init_set_ui (f[0], 1L);  /* F[-1] */
   mpz_init_set_ui (f[1], 0L);  /* F[0] */
@@ -43,7 +44,7 @@
 
   for (i = 2; ; i++)
     {
-      ASSERT (i < falloc);
+      assert (i < falloc);
 
       /* F[i] = F[i-1] + F[i-2] */
       mpz_init (f[i]);
diff -r b641c8181188 gen-psqr.c
--- a/gen-psqr.c        Wed Feb 15 14:46:40 2012 +0100
+++ b/gen-psqr.c        Thu Feb 16 11:44:36 2012 +0100
@@ -20,7 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "dumbmp.c"
+#include "mini-gmp-extra.c"
 
 
 /* The aim of this program is to choose either mpn_mod_34lsub1 or mpn_mod_1
@@ -152,9 +152,9 @@
    accordingly.  */
 #define COLLAPSE_ELEMENT(array, idx, narray)                    \
   do {                                                          \
-    mem_copyi ((char *) &(array)[idx],                          \
-               (char *) &(array)[idx+1],                        \
-               ((narray)-((idx)+1)) * sizeof (array[0]));       \
+    memmove (&(array)[idx],                                    \
+            &(array)[idx+1],                                   \
+            ((narray)-((idx)+1)) * sizeof (array[0]));         \
     (narray)--;                                                 \
   } while (0)
 
@@ -173,10 +173,42 @@
 int
 neg_mod (int n, int m)
 {
-  ASSERT (n >= 0 && n < m);
+  assert (n >= 0 && n < m);
   return (n == 0 ? 0 : m-n);
 }
 
+int
+log2_ceil (int n)
+{
+  int  e;
+  assert (n >= 1);
+  for (e = 0; ; e++)
+    if ((1 << e) >= n)
+      break;
+  return e;
+}
+
+/* Set inv to the inverse of d, in the style of invert_limb, ie. for
+   udiv_qrnnd_preinv.  */
+void
+mpz_preinv_invert (mpz_t inv, mpz_t d, int numb_bits)
+{
+  mpz_t  t;
+  int    norm;
+  assert (SIZ(d) > 0);
+
+  norm = numb_bits - mpz_sizeinbase (d, 2);
+  assert (norm >= 0);
+  mpz_init_set_ui (t, 1L);
+  mpz_mul_2exp (t, t, 2*numb_bits - norm);
+  mpz_tdiv_q (inv, t, d);
+  mpz_set_ui (t, 1L);
+  mpz_mul_2exp (t, t, numb_bits);
+  mpz_sub (inv, inv, t);
+
+  mpz_clear (t);
+}
+
 /* Set "mask" to a value such that "mask & (1<<idx)" is non-zero if
    "-(idx<<mod_bits)" can be a square modulo m.  */
 void
@@ -202,7 +234,7 @@
   int  i, res;
 
   nsq_res_0x100 = (0x100 + limb_bits - 1) / limb_bits;
-  sq_res_0x100 = (mpz_t *) xmalloc (nsq_res_0x100 * sizeof (*sq_res_0x100));
+  sq_res_0x100 = xmalloc (nsq_res_0x100 * sizeof (*sq_res_0x100));
 
   for (i = 0; i < nsq_res_0x100; i++)
     mpz_init_set_ui (sq_res_0x100[i], 0L);
@@ -233,9 +265,8 @@
   /* no more than limb_bits many factors in a one limb modulus (and of
      course in reality nothing like that many) */
   factor_alloc = limb_bits;
-  factor = (struct factor_t *) xmalloc (factor_alloc * sizeof (*factor));
-  rawfactor = (struct rawfactor_t *)
-    xmalloc (factor_alloc * sizeof (*rawfactor));
+  factor = xmalloc (factor_alloc * sizeof (*factor));
+  rawfactor = xmalloc (factor_alloc * sizeof (*rawfactor));
 
   if (numb_bits % 4 != 0)
     {
@@ -301,7 +332,7 @@
           }
         while (mpz_sgn (r) == 0);
 
-        ASSERT (nrawfactor < factor_alloc);
+        assert (nrawfactor < factor_alloc);
         rawfactor[nrawfactor].divisor = i;
         rawfactor[nrawfactor].multiplicity = multiplicity;
         nrawfactor++;
@@ -341,7 +372,7 @@
             break;
           mpz_set (pp, new_pp);
 
-          ASSERT (nrawfactor < factor_alloc);
+          assert (nrawfactor < factor_alloc);
           rawfactor[nrawfactor].divisor = i;
           rawfactor[nrawfactor].multiplicity = 1;
           nrawfactor++;
@@ -377,7 +408,7 @@
   for (i = 0; i < nrawfactor; i++)
     {
       int  j;
-      ASSERT (nfactor < factor_alloc);
+      assert (nfactor < factor_alloc);
       factor[nfactor].divisor = 1;
       for (j = 0; j < rawfactor[i].multiplicity; j++)
         factor[nfactor].divisor *= rawfactor[i].divisor;
diff -r b641c8181188 gen-trialdivtab.c
--- a/gen-trialdivtab.c Wed Feb 15 14:46:40 2012 +0100
+++ b/gen-trialdivtab.c Thu Feb 16 11:44:36 2012 +0100
@@ -36,7 +36,8 @@
 
 #include <stdlib.h>
 #include <stdio.h>
-#include "dumbmp.c"
+
+#include "mini-gmp-extra.c"
 
 int sumspills (mpz_t, mpz_t *, int);
 void mpn_mod_1s_4p_cps (mpz_t [7], mpz_t);
diff -r b641c8181188 mini-gmp-extra.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/mini-gmp-extra.c  Thu Feb 16 11:44:36 2012 +0100
@@ -0,0 +1,115 @@
+/* Some additional needed functions, not included mini-gmp
+
+Copyright 2001, 2002, 2004, 2011, 2012 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library.
+
+The GNU MP Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+The GNU MP Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
+
+#include <assert.h>
+
+#include "mini-gmp/mini-gmp.c"
+
+#define MIN(l,o) ((l) < (o) ? (l) : (o))
+#define PTR(x)   ((x)->_mp_d)
+#define SIZ(x)   ((x)->_mp_size)
+
+void *
+xmalloc (int n)
+{
+  void  *p;
+  p = malloc (n);
+  if (p == NULL)
+    {
+      fprintf (stderr, "Out of memory (alloc %d bytes)\n", n);
+      abort ();
+    }
+  return p;
+}
+
+int
+isprime (unsigned long int t)
+{
+  unsigned long int q, r, d;
+
+  if (t < 32)
+    return (0xa08a28acUL >> t) & 1;
+  if ((t & 1) == 0)
+    return 0;
+
+  if (t % 3 == 0)
+    return 0;
+  if (t % 5 == 0)
+    return 0;
+  if (t % 7 == 0)
+    return 0;
+
+  for (d = 11;;)
+    {
+      q = t / d;
+      r = t - q * d;
+      if (q < d)
+       return 1;
+      if (r == 0)
+       break;
+      d += 2;
+      q = t / d;
+      r = t - q * d;
+      if (q < d)
+       return 1;
+      if (r == 0)
+       break;
+      d += 4;
+    }
+  return 0;
+}
+
+/* Calculate r satisfying r*d == 1 mod 2^n. */
+void
+mpz_invert_2exp (mpz_t r, mpz_t a, unsigned long n)
+{
+  unsigned long  i;
+  mpz_t  inv, prod;
+
+  assert (mpz_odd_p (a));
+
+  mpz_init_set_ui (inv, 1L);
+  mpz_init (prod);
+
+  for (i = 1; i < n; i++)
+    {
+      mpz_mul (prod, inv, a);
+      if (mpz_tstbit (prod, i) != 0)
+        mpz_setbit (inv, i);
+    }
+
+  mpz_mul (prod, inv, a);
+  mpz_tdiv_r_2exp (prod, prod, n);
+  assert (mpz_cmp_ui (prod, 1L) == 0);
+
+  mpz_set (r, inv);
+
+  mpz_clear (inv);
+  mpz_clear (prod);
+}
+
+/* Calculate inv satisfying r*a == 1 mod 2^n. */
+void
+mpz_invert_ui_2exp (mpz_t r, unsigned long a, unsigned long n)
+{
+  mpz_t  az;
+  mpz_init_set_ui (az, a);
+  mpz_invert_2exp (r, az, n);
+  mpz_clear (az);
+}
-- 
Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.
_______________________________________________
gmp-devel mailing list
gmp-devel@gmplib.org
http://gmplib.org/mailman/listinfo/gmp-devel

Reply via email to