Make flags be unsigned. Improve the write algorithm: recurse until the
authormdw <mdw>
Sat, 3 Feb 2001 16:05:17 +0000 (16:05 +0000)
committermdw <mdw>
Sat, 3 Feb 2001 16:05:17 +0000 (16:05 +0000)
parts are one word long and use single-precision arithmetic from there.
Fix off-by-one bug when breaking the number apart.

mptext.c

index a55f1c0..55ef579 100644 (file)
--- a/mptext.c
+++ b/mptext.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mptext.c,v 1.8 2000/12/06 20:32:42 mdw Exp $
+ * $Id: mptext.c,v 1.9 2001/02/03 16:05:17 mdw Exp $
  *
  * Textual representation of multiprecision numbers
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mptext.c,v $
+ * Revision 1.9  2001/02/03 16:05:17  mdw
+ * Make flags be unsigned.  Improve the write algorithm: recurse until the
+ * parts are one word long and use single-precision arithmetic from there.
+ * Fix off-by-one bug when breaking the number apart.
+ *
  * Revision 1.8  2000/12/06 20:32:42  mdw
  * Reduce binary bytes (to allow marker bits to be ignored).  Fix error
  * message string a bit.  Allow leading `+' signs.
@@ -147,10 +152,8 @@ mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p)
 
   /* --- Flags --- */
 
-  enum {
-    f_neg = 1u,
-    f_ok = 2u
-  };
+#define f_neg 1u
+#define f_ok 2u
 
   /* --- Initialize the stacks --- */
 
@@ -353,6 +356,9 @@ mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p)
   if (f & f_neg)
     m->f |= MP_NEG;
   return (m);
+
+#undef f_neg
+#undef f_ok
 }
 
 /* --- @mp_write@ --- *
@@ -369,14 +375,14 @@ mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p)
 
 /* --- Simple case --- *
  *
- * Use a fixed-sized buffer and the simple single-precision division
- * algorithm to pick off low-order digits.  Put each digit in a buffer,
- * working backwards from the end.  If the buffer becomes full, recurse to
- * get another one.  Ensure that there are at least @z@ digits by writing
- * leading zeroes if there aren't enough real digits.
+ * Use a fixed-sized buffer and single-precision arithmetic to pick off
+ * low-order digits.  Put each digit in a buffer, working backwards from the
+ * end.  If the buffer becomes full, recurse to get another one.  Ensure that
+ * there are at least @z@ digits by writing leading zeroes if there aren't
+ * enough real digits.
  */
 
-static int simple(mp *m, int radix, unsigned z,
+static int simple(mpw n, int radix, unsigned z,
                  const mptext_ops *ops, void *p)
 {
   int rc = 0;
@@ -388,23 +394,21 @@ static int simple(mp *m, int radix, unsigned z,
     int ch;
     mpw x;
 
-    x = mpx_udivn(m->v, m->vl, m->v, m->vl, rd);
-    MP_SHRINK(m);
+    x = n % rd;
+    n /= rd;
     if (radix < 0)
       ch = x;
-    else {
-      if (x < 10)
-       ch = '0' + x;
-      else
-       ch = 'a' + x - 10;
-    }
+    else if (x < 10)
+      ch = '0' + x;
+    else
+      ch = 'a' + x - 10;
     buf[--i] = ch;
     if (z)
       z--;
-  } while (i && MP_LEN(m));
+  } while (i && n);
 
-  if (MP_LEN(m))
-    rc = simple(m, radix, z, ops, p);
+  if (n)
+    rc = simple(n, radix, z, ops, p);
   else {
     static const char zero[32] = "00000000000000000000000000000000";
     while (!rc && z >= sizeof(zero)) {
@@ -415,9 +419,8 @@ static int simple(mp *m, int radix, unsigned z,
       rc = ops->put(zero, z, p);
   }
   if (!rc)
-    ops->put(buf + i, sizeof(buf) - i, p);
-  if (m->f & MP_BURN)
-    BURN(buf);
+    rc = ops->put(buf + i, sizeof(buf) - i, p);
+  BURN(buf);
   return (rc);
 }
 
@@ -436,9 +439,10 @@ static int complicated(mp *m, int radix, mp **pr, unsigned i, unsigned z,
   mp *q = MP_NEW;
   unsigned d = 1 << i;
 
-  if (MP_LEN(m) < 8)
-    return (simple(m, radix, z, ops, p));
+  if (MP_LEN(m) < 2)
+    return (simple(MP_LEN(m) ? m->v[0] : 0, radix, z, ops, p));
 
+  assert(i);
   mp_div(&q, &m, m, pr[i]);
   if (!MP_LEN(q))
     d = z;
@@ -485,8 +489,8 @@ int mp_write(mp *m, int radix, const mptext_ops *ops, void *p)
 
   /* --- If the number is small, do it the easy way --- */
 
-  if (MP_LEN(m) < 8)
-    rc = simple(m, radix, 0, ops, p);
+  if (MP_LEN(m) < 2)
+    rc = simple(MP_LEN(m) ? m->v[0] : 0, radix, 0, ops, p);
 
   /* --- Use a clever algorithm --- *
    *
@@ -499,7 +503,7 @@ int mp_write(mp *m, int radix, const mptext_ops *ops, void *p)
 
   else {
     mp *pr[DEPTH];
-    size_t target = MP_LEN(m) / 2;
+    size_t target = (MP_LEN(m) + 1) / 2;
     unsigned i = 0;
     mp *z = mp_new(1, 0);