[Gmp-commit] /var/hg/gmp-proj/mini-gmp: Namespace cleanup. MP_*_SWAP macros r...

mercurial at gmplib.org mercurial at gmplib.org
Tue Jan 3 22:32:27 CET 2012


details:   /var/hg/gmp-proj/mini-gmp/rev/0a5e174f762f
changeset: 42:0a5e174f762f
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Tue Jan 03 22:31:57 2012 +0100
description:
Namespace cleanup. MP_*_SWAP macros remain.

diffstat:

 mini-gmp.c |  309 +++++++++++++++++++++++++++++-------------------------------
 1 files changed, 148 insertions(+), 161 deletions(-)

diffs (truncated from 1012 to 300 lines):

diff -r 1b62bce64f48 -r 0a5e174f762f mini-gmp.c
--- a/mini-gmp.c	Tue Jan 03 22:14:16 2012 +0100
+++ b/mini-gmp.c	Tue Jan 03 22:31:57 2012 +0100
@@ -54,23 +54,23 @@
 /* Macros */
 #define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
 
-#define MP_LIMB_T_MAX (~ (mp_limb_t) 0)
+#define GMP_LIMB_MAX (~ (mp_limb_t) 0)
 #define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
 
 #define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
 #define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
 
-#define ABS(x) ((x) >= 0 ? (x) : -(x))
-
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-
-#define assert_nocarry(x) do { \
-    mp_limb_t __cy = x;	       \
-    assert (__cy == 0);	       \
+#define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
+
+#define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
+#define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
+
+#define gmp_assert_nocarry(x) do { \
+    mp_limb_t __cy = x;		   \
+    assert (__cy == 0);		   \
   } while (0)
 
-#define count_leading_zeros(count, x) do {				\
+#define gmp_clz(count, x) do {						\
     mp_limb_t __clz_x = (x);						\
     unsigned __clz_c;							\
     for (__clz_c = 0;							\
@@ -82,14 +82,14 @@
     (count) = __clz_c;							\
   } while (0)
 
-#define count_trailing_zeros(count, x) do {				\
+#define gmp_ctz(count, x) do {						\
     mp_limb_t __ctz_x = (x);						\
     unsigned __ctz_c = 0;						\
-    count_leading_zeros (__ctz_c, __ctz_x & - __ctz_x);			\
+    gmp_clz (__ctz_c, __ctz_x & - __ctz_x);				\
     (count) = GMP_LIMB_BITS - 1 - __ctz_c;				\
   } while (0)
 
-#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
+#define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
   do {									\
     mp_limb_t __x;							\
     __x = (al) + (bl);							\
@@ -97,7 +97,7 @@
     (sl) = __x;								\
   } while (0)
 
-#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
+#define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
   do {									\
     mp_limb_t __x;							\
     __x = (al) - (bl);							\
@@ -105,7 +105,7 @@
     (sl) = __x;								\
   } while (0)
 
-#define umul_ppmm(w1, w0, u, v)						\
+#define gmp_umul_ppmm(w1, w0, u, v)					\
   do {									\
     mp_limb_t __x0, __x1, __x2, __x3;					\
     unsigned __ul, __vl, __uh, __vh;					\
@@ -130,11 +130,11 @@
     (w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK);	\
   } while (0)
 
-#define udiv_qrnnd_preinv(q, r, nh, nl, d, di)				\
+#define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di)			\
   do {									\
     mp_limb_t _qh, _ql, _r, _mask;					\
-    umul_ppmm (_qh, _ql, (nh), (di));					\
-    add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl));			\
+    gmp_umul_ppmm (_qh, _ql, (nh), (di));				\
+    gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl));		\
     _r = (nl) - _qh * (d);						\
     _mask = -(mp_limb_t) (_r > _ql); /* both > and >= are OK */		\
     _qh += _mask;							\
@@ -149,40 +149,34 @@
     (q) = _qh;								\
   } while (0)
 
-#define udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv)		\
+#define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv)		\
   do {									\
     mp_limb_t _q0, _t1, _t0, _mask;					\
-    umul_ppmm ((q), _q0, (n2), (dinv));					\
-    add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1));			\
+    gmp_umul_ppmm ((q), _q0, (n2), (dinv));				\
+    gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1));			\
 									\
     /* Compute the two most significant limbs of n - q'd */		\
     (r1) = (n1) - (d1) * (q);						\
-    sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0));			\
-    umul_ppmm (_t1, _t0, (d0), (q));					\
-    sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0);			\
+    gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0));		\
+    gmp_umul_ppmm (_t1, _t0, (d0), (q));				\
+    gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0);			\
     (q)++;								\
 									\
     /* Conditionally adjust q and the remainders */			\
     _mask = - (mp_limb_t) ((r1) >= _q0);				\
     (q) += _mask;							\
-    add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0));	\
+    gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
     if ((r1) >= (d1))							\
       {									\
 	if ((r1) > (d1) || (r0) >= (d0))				\
 	  {								\
 	    (q)++;							\
-	    sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0));		\
+	    gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0));	\
 	  }								\
       }									\
   } while (0)
 
 /* Swap macros. */
-#define UCHAR_SWAP(x, y)						\
-  do {									\
-    unsigned char __uchar_swap__tmp = (x);				\
-    (x) = (y);								\
-    (y) = __uchar_swap__tmp;						\
-  } while (0)
 #define MP_LIMB_T_SWAP(x, y)						\
   do {									\
     mp_limb_t __mp_limb_t_swap__tmp = (x);				\
@@ -241,14 +235,14 @@
 
 /* Helper functions. */
 static void
-die (const char *msg)
+gmp_die (const char *msg)
 {
   fprintf (stderr, "%s\n", msg);
   abort();
 }
 
 static void *
-xalloc (size_t size)
+gmp_xalloc (size_t size)
 {
   void *p;
 
@@ -256,19 +250,19 @@
 
   p = malloc (size);
   if (!p)
-    die("virtual memory exhausted.");
+    gmp_die("virtual memory exhausted.");
 
   return p;
 }
 
 static mp_ptr
-xalloc_limbs (mp_size_t size)
+gmp_xalloc_limbs (mp_size_t size)
 {
-  return xalloc (size * sizeof (mp_limb_t));
+  return gmp_xalloc (size * sizeof (mp_limb_t));
 }
 
 static mp_ptr
-xrealloc_limbs (mp_ptr old, mp_size_t size)
+gmp_xrealloc_limbs (mp_ptr old, mp_size_t size)
 {
   mp_ptr p;
 
@@ -277,7 +271,7 @@
   p = realloc (old, size * sizeof(mp_limb_t));
 
   if (!p)
-    die("virtual memory exhausted.");
+    gmp_die("virtual memory exhausted.");
 
   return p;
 }
@@ -441,7 +435,7 @@
   do
     {
       ul = *up++;
-      umul_ppmm (hpl, lpl, ul, vl);
+      gmp_umul_ppmm (hpl, lpl, ul, vl);
 
       lpl += cl;
       cl = (lpl < cl) + hpl;
@@ -464,7 +458,7 @@
   do
     {
       ul = *up++;
-      umul_ppmm (hpl, lpl, ul, vl);
+      gmp_umul_ppmm (hpl, lpl, ul, vl);
 
       lpl += cl;
       cl = (lpl < cl) + hpl;
@@ -490,7 +484,7 @@
   do
     {
       ul = *up++;
-      umul_ppmm (hpl, lpl, ul, vl);
+      gmp_umul_ppmm (hpl, lpl, ul, vl);
 
       lpl += cl;
       cl = (lpl < cl) + hpl;
@@ -667,7 +661,7 @@
 	    }
 	  r -= u1;
 	}
-      umul_ppmm (th, tl, u0, m);
+      gmp_umul_ppmm (th, tl, u0, m);
       r += th;
       if (r < th)
 	{
@@ -680,7 +674,7 @@
   return m;
 }
 
-struct div_qr_1_inverse
+struct gmp_div_qr_1_inverse
 {
   /* Normalization shift count. */
   unsigned shift;
@@ -691,10 +685,10 @@
 };
 
 static void
-mpn_div_qr_1_invert (struct div_qr_1_inverse *inv, mp_limb_t d)
+mpn_div_qr_1_invert (struct gmp_div_qr_1_inverse *inv, mp_limb_t d)
 {
   unsigned shift;
-  count_leading_zeros (shift, d);
+  gmp_clz (shift, d);
   inv->shift = shift;
   inv->d = d << shift;
   inv->di = mpn_invert_limb (inv->d);
@@ -704,7 +698,7 @@
    the sbpi1_div_* functions. */
 static mp_limb_t
 mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,
-		     const struct div_qr_1_inverse *inv)
+		     const struct gmp_div_qr_1_inverse *inv)
 {
   mp_limb_t d, di;
   mp_limb_t r;
@@ -712,7 +706,7 @@
 
   if (inv->shift > 0)
     {
-      tp = xalloc_limbs (nn);
+      tp = gmp_xalloc_limbs (nn);
       r = mpn_lshift (tp, np, nn, inv->shift);
       np = tp;
     }
@@ -726,7 +720,7 @@
     {
       mp_limb_t q;
 
-      udiv_qrnnd_preinv (q, r, r, np[nn], d, di);
+      gmp_udiv_qrnnd_preinv (q, r, r, np[nn], d, di);
       if (qp)
 	qp[nn] = q;
     }
@@ -746,14 +740,14 @@
       /* Power of two */
       unsigned shift;
       mp_limb_t r = np[0] & (d-1);
-      count_trailing_zeros (shift, d);
+      gmp_ctz (shift, d);
       mpn_rshift (qp, np, nn, shift);
 
       return r;
     }
   else
     {
-      struct div_qr_1_inverse inv;
+      struct gmp_div_qr_1_inverse inv;
       mpn_div_qr_1_invert (&inv, d);
       return mpn_div_qr_1_preinv (qp, np, nn, &inv);
     }
@@ -770,12 +764,12 @@
 
   assert (nn >= 2);
 
-  count_leading_zeros (shift, d1);
+  gmp_clz (shift, d1);
   if (shift > 0)
     {
       d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
       d0 <<= shift;
-      tp = xalloc_limbs (nn);


More information about the gmp-commit mailing list