/* -*-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.
/* --- Flags --- */
- enum {
- f_neg = 1u,
- f_ok = 2u
- };
+#define f_neg 1u
+#define f_ok 2u
/* --- Initialize the stacks --- */
if (f & f_neg)
m->f |= MP_NEG;
return (m);
+
+#undef f_neg
+#undef f_ok
}
/* --- @mp_write@ --- *
/* --- 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;
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)) {
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);
}
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;
/* --- 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 --- *
*
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);