3 * Textual representation of multiprecision numbers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
38 /*----- Magical numbers ---------------------------------------------------*/
40 /* --- Maximum recursion depth --- *
42 * This is the number of bits in a @size_t@ object. Why?
44 * To see this, let %$b = \textit{MPW\_MAX} + 1$% and let %$Z$% be the
45 * largest @size_t@ value. Then the largest possible @mp@ is %$M - 1$% where
46 * %$M = b^Z$%. Let %$r$% be a radix to read or write. Since the recursion
47 * squares the radix at each step, the highest number reached by the
48 * recursion is %$d$%, where:
52 * Solving gives that %$d = \lg \log_r b^Z$%. If %$r = 2$%, this is maximum,
53 * so choosing %$d = \lg \lg b^Z = \lg (Z \lg b) = \lg Z + \lg \lg b$%.
55 * Expressing %$\lg Z$% as @CHAR_BIT * sizeof(size_t)@ yields an
56 * overestimate, since a @size_t@ representation may contain `holes'.
57 * Choosing to represent %$\lg \lg b$% by 10 is almost certainly sufficient
58 * for `some time to come'.
61 #define DEPTH (CHAR_BIT * sizeof(size_t) + 10)
63 /*----- Input -------------------------------------------------------------*/
65 /* --- @mp_read@ --- *
67 * Arguments: @mp *m@ = destination multiprecision number
68 * @int radix@ = base to assume for data (or zero to guess)
69 * @const mptext_ops *ops@ = pointer to operations block
70 * @void *p@ = data for the operations block
72 * Returns: The integer read, or zero if it didn't work.
74 * Use: Reads an integer from some source. If the @radix@ is
75 * specified, the number is assumed to be given in that radix,
76 * with the letters `a' (either upper- or lower-case) upwards
77 * standing for digits greater than 9. Otherwise, base 10 is
78 * assumed unless the number starts with `0' (octal), `0x' (hex)
79 * or `nnn_' (base `nnn'). An arbitrary amount of whitespace
80 * before the number is ignored.
83 /* --- About the algorithm --- *
85 * The algorithm here is rather aggressive. I maintain an array of
86 * successive squarings of the radix, and a stack of partial results, each
87 * with a counter attached indicating which radix square to multiply by.
88 * Once the item at the top of the stack reaches the same counter level as
89 * the next item down, they are combined together and the result is given a
90 * counter level one higher than either of the results.
92 * Gluing the results together at the end is slightly tricky. Pay attention
95 * This is more complicated because of the need to handle the slightly
99 static int char_digit(int ch, int radix)
101 int r = radix < 0 ? -radix : radix;
104 if (ch < 0) return (-1);
105 if (radix < 0) d = ch;
106 else if ('0' <= ch && ch <= '9') d = ch - '0';
107 else if ('a' <= ch && ch <= 'z') d = ch - 'a' + 10;
108 else if ('A' <= ch && ch <= 'Z') d = ch - 'A' + (radix > 36 ? 36 : 10);
110 if (d >= r) return (-1);
114 static mp *read_binary(int radix, unsigned bit, unsigned nf,
115 const mptext_ops *ops, void *p)
118 unsigned b = MPW_BITS;
125 /* --- The fast binary algorithm --- *
127 * We stack bits up starting at the top end of a word. When one word is
128 * full, we write it to the integer, and start another with the left-over
129 * bits. When the array in the integer is full, we resize using low-level
130 * calls and copy the current data to the top end. Finally, we do a single
131 * bit-shift when we know where the end of the number is.
134 m = mp_dest(MP_NEW, 1, nf);
141 if ((d = char_digit(ch, radix)) < 0) break;
143 /* --- Ignore leading zeroes, but notice that the number is valid --- */
146 if (!d && !nz) continue;
149 /* --- Feed the digit into the accumulator --- */
155 a |= MPW(d) >> (bit - b);
160 v = mpalloc(m->a, len);
161 memcpy(v + n, m->v, MPWS(n));
163 m->v = v; v = m->v + n;
165 a = (b < MPW_BITS) ? MPW(d) << b : 0;
169 /* --- Finish up --- */
172 if (!any) { mp_drop(m); return (0); }
178 m = mp_lsr(m, m, (unsigned long)n * MPW_BITS + b);
185 /* --- State for the general-base reader --- *
187 * There are two arrays. The @pow@ array is set so that @pow[i]@ contains
188 * %$R^{2^i}$% for @i < pows@. The stack @s@ contains partial results:
189 * each entry contains a value @m@ corresponding to %$2^i$% digits.
190 * Inductively, an empty stack represents zero; if a stack represents %$x$%
191 * then pushing a new entry on the top causes the stack to represent
194 * It is an invariant that each entry has a strictly smaller @i@ than the
195 * items beneath it. This is achieved by coaslescing entries at the top if
196 * they have equal %$i$% values: if the top items are %$(m, i)$%, and
197 * %$(M', i)$%, and the rest of the stack represents the integer %$x$%,
198 * then %$R^{2^i} (R^{2^i} x + M) + m = R^{2^{i+1}} x + (R^{2^i} M + m)$%,
199 * so we replace the top two items by %$((R^{2^i} M + m), i + 1)$%, and
200 * repeat if necessary.
204 struct { unsigned i; mp *m; } s[DEPTH];
208 static void ensure_power(struct readstate *rs)
210 /* --- Make sure we have the necessary %$R^{2^i}$% computed --- */
212 if (rs->s[rs->sp].i >= rs->pows) {
213 assert(rs->pows < DEPTH);
214 rs->pow[rs->pows] = mp_sqr(MP_NEW, rs->pow[rs->pows - 1]);
219 static void read_digit(struct readstate *rs, unsigned nf, int d)
221 mp *m = mp_new(1, nf);
224 /* --- Put the new digit on top --- */
226 assert(rs->sp < DEPTH);
230 /* --- Restore the stack invariant --- */
232 while (rs->sp && rs->s[rs->sp - 1].i <= rs->s[rs->sp].i) {
238 m = mp_mul(m, m, rs->pow[rs->s[rs->sp + 1].i]);
239 m = mp_add(m, m, rs->s[rs->sp + 1].m);
240 MP_DROP(rs->s[rs->sp + 1].m);
245 /* --- Leave the stack pointer at an empty item --- */
250 static mp *read_general(int radix, unsigned t, unsigned nf,
251 const mptext_ops *ops, void *p)
262 /* --- Prepare the stack --- */
264 r = radix < 0 ? -radix : radix;
265 mp_build(&rr, &r, &r + 1);
270 /* --- If we've partially parsed some input then feed it in --- *
272 * Unfortunately, what we've got is backwards. Fortunately there's a
273 * fairly tight upper bound on how many digits @t@ might be, since we
274 * aborted that loop once it got too large.
279 while (t) { assert(i < sizeof(v)); v[i++] = t%r; t /= r; }
280 while (i) read_digit(&rs, nf, v[--i]);
284 /* --- Read more stuff --- */
288 if ((d = char_digit(ch, radix)) < 0) break;
289 read_digit(&rs, nf, d); any = 1;
293 /* --- Stitch all of the numbers together --- *
295 * This is not the same code as @read_digit@. In particular, here we must
296 * cope with the partial result being some inconvenient power of %$R$%,
297 * rather than %$R^{2^i}$%.
300 if (!any) return (0);
301 m = MP_ZERO; z = MP_ONE;
308 z = mp_mul(z, z, rs.pow[rs.s[rs.sp].i]);
311 for (i = 0; i < rs.pows; i++) MP_DROP(rs.pow[i]);
316 mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p)
326 /* --- We don't actually need a destination so throw it away --- *
328 * But note the flags before we lose it entirely.
336 /* --- Maintain a lookahead character --- */
340 /* --- If we're reading text, skip leading space, and maybe a sign --- */
343 while (isspace(ch)) ch = ops->get(p);
345 case '-': f |= f_neg; /* and on */
346 case '+': do ch = ops->get(p); while (isspace(ch));
350 /* --- If we don't have a fixed radix, then parse one from the input --- *
352 * This is moderately easy if the input starts with `0x' or similar. If it
353 * starts with `0' and something else, then it might be octal, or just a
354 * plain old zero. Finally, it might start with a leading `NN_', in which
355 * case we carefully collect the decimal number until we're sure it's
356 * either a radix prefix (in which case we accept it and start over) or it
357 * isn't (in which case it's actually the start of a large number we need
365 case 'x': case 'X': radix = 16; goto fetch;
366 case 'o': case 'O': radix = 8; goto fetch;
367 case 'b': case 'B': radix = 2; goto fetch;
368 fetch: ch = ops->get(p); break;
369 default: radix = 8; f |= f_ok; break;
372 if ((d = char_digit(ch, 10)) < 0) { ops->unget(ch, p); return (0); }
377 if ((d = char_digit(ch, 10)) < 0) break;
379 if (ch != '_' || t > 52) radix = 10;
387 /* --- We're now ready to dispatch to the correct handler --- */
389 rd = radix < 0 ? -radix : radix;
392 case 2: m = read_binary(radix, 1, nf, ops, p); break;
393 case 4: m = read_binary(radix, 2, nf, ops, p); break;
394 case 8: m = read_binary(radix, 3, nf, ops, p); break;
395 case 16: m = read_binary(radix, 4, nf, ops, p); break;
396 case 32: m = read_binary(radix, 5, nf, ops, p); break;
397 case 64: m = read_binary(radix, 6, nf, ops, p); break;
398 case 128: m = read_binary(radix, 7, nf, ops, p); break;
399 default: m = read_general(radix, t, nf, ops, p); break;
402 /* --- That didn't work --- *
404 * If we've already read something then return that. Otherwise it's an
409 if (f & f_ok) return (MP_ZERO);
413 /* --- Negate the result if we should do that --- */
415 if (f & f_neg) m = mp_neg(m, m);
417 /* --- And we're all done --- */
425 /*----- Output ------------------------------------------------------------*/
427 /* --- @mp_write@ --- *
429 * Arguments: @mp *m@ = pointer to a multi-precision integer
430 * @int radix@ = radix to use when writing the number out
431 * @const mptext_ops *ops@ = pointer to an operations block
432 * @void *p@ = data for the operations block
434 * Returns: Zero if it worked, nonzero otherwise.
436 * Use: Writes a large integer in textual form.
439 static int digit_char(int d, int radix)
441 if (radix < 0) return (d);
442 else if (d < 10) return (d + '0');
443 else if (d < 26) return (d - 10 + 'a');
444 else return (d - 36 + 'A');
447 /* --- Simple case --- *
449 * Use a fixed-sized buffer and single-precision arithmetic to pick off
450 * low-order digits. Put each digit in a buffer, working backwards from the
451 * end. If the buffer becomes full, recurse to get another one. Ensure that
452 * there are at least @z@ digits by writing leading zeroes if there aren't
453 * enough real digits.
456 static int write_simple(mpw n, int radix, unsigned z,
457 const mptext_ops *ops, void *p)
461 unsigned i = sizeof(buf);
462 int rd = radix > 0 ? radix : -radix;
467 buf[--i] = digit_char(x, radix);
472 rc = write_simple(n, radix, z, ops, p);
475 memset(zbuf, (radix < 0) ? 0 : '0', sizeof(zbuf));
476 while (!rc && z >= sizeof(zbuf)) {
477 rc = ops->put(zbuf, sizeof(zbuf), p);
480 if (!rc && z) rc = ops->put(zbuf, z, p);
482 if (!rc) rc = ops->put(buf + i, sizeof(buf) - i, p);
487 /* --- Complicated case --- *
489 * If the number is small, fall back to the simple case above. Otherwise
490 * divide and take remainder by current large power of the radix, and emit
491 * each separately. Don't emit a zero quotient. Be very careful about
492 * leading zeroes on the remainder part, because they're deeply significant.
495 static int write_complicated(mp *m, int radix, mp **pr,
496 unsigned i, unsigned z,
497 const mptext_ops *ops, void *p)
504 return (write_simple(MP_LEN(m) ? m->v[0] : 0, radix, z, ops, p));
507 mp_div(&q, &m, m, pr[i]);
508 if (MP_ZEROP(q)) d = z;
512 rc = write_complicated(q, radix, pr, i - 1, z, ops, p);
514 if (!rc) rc = write_complicated(m, radix, pr, i - 1, d, ops, p);
519 /* --- Binary case --- *
521 * Special case for binary output. Goes much faster.
524 static int write_binary(mp *m, int bit, int radix,
525 const mptext_ops *ops, void *p)
539 /* --- Work out where to start --- */
542 if (n % bit) n += bit - (n % bit);
546 if (n >= MP_LEN(m)) {
553 mask = (1 << bit) - 1;
556 /* --- Main code --- */
565 if (v == m->v) break;
567 if (b < MPW_BITS) x |= a >> b;
570 if (!x && !(f & f_out)) continue;
572 *q++ = digit_char(x, radix);
573 if (q >= buf + sizeof(buf)) {
574 if ((rc = ops->put(buf, sizeof(buf), p)) != 0) goto done;
581 *q++ = digit_char(x, radix);
582 rc = ops->put(buf, q - buf, p);
591 /* --- Main driver code --- */
593 int mp_write(mp *m, int radix, const mptext_ops *ops, void *p)
601 if (MP_EQ(m, MP_ZERO))
602 return (ops->put(radix > 0 ? "0" : "\0", 1, p));
604 /* --- Set various things up --- */
609 /* --- Check the radix for sensibleness --- */
612 assert(((void)"ascii radix must be <= 62", radix <= 62));
614 assert(((void)"binary radix must fit in a byte", -radix <= UCHAR_MAX));
616 assert(((void)"radix can't be zero in mp_write", 0));
618 /* --- If the number is negative, sort that out --- */
622 if (ops->put("-", 1, p)) return (EOF);
626 /* --- Handle binary radix --- */
629 case 2: case -2: return (write_binary(m, 1, radix, ops, p));
630 case 4: case -4: return (write_binary(m, 2, radix, ops, p));
631 case 8: case -8: return (write_binary(m, 3, radix, ops, p));
632 case 16: case -16: return (write_binary(m, 4, radix, ops, p));
633 case 32: case -32: return (write_binary(m, 5, radix, ops, p));
634 case -64: return (write_binary(m, 6, radix, ops, p));
635 case -128: return (write_binary(m, 7, radix, ops, p));
638 /* --- If the number is small, do it the easy way --- */
641 rc = write_simple(MP_LEN(m) ? m->v[0] : 0, radix, 0, ops, p);
643 /* --- Use a clever algorithm --- *
645 * Square the radix repeatedly, remembering old results, until I get
646 * something more than half the size of the number @m@. Use this to divide
647 * the number: the quotient and remainder will be approximately the same
648 * size, and I'll have split them on a digit boundary, so I can just emit
649 * the quotient and remainder recursively, in order.
653 target = (MP_LEN(m) + 1) / 2;
656 /* --- Set up the exponent table --- */
658 z->v[0] = (radix > 0 ? radix : -radix);
661 assert(((void)"Number is too unimaginably huge", i < DEPTH));
663 if (MP_LEN(z) > target) break;
664 z = mp_sqr(MP_NEW, z);
667 /* --- Write out the answer --- */
669 rc = write_complicated(m, radix, pr, i - 1, 0, ops, p);
671 /* --- Tidy away the array --- */
673 while (i > 0) mp_drop(pr[--i]);
676 /* --- Tidying up code --- */
682 /*----- Test rig ----------------------------------------------------------*/
686 #include <mLib/testrig.h>
688 static int verify(dstr *v)
691 int ib = *(int *)v[0].buf, ob = *(int *)v[2].buf;
694 mp *m = mp_readdstr(MP_NEW, &v[1], &off, ib);
697 fprintf(stderr, "*** unexpected successful parse\n"
698 "*** input [%2i] = ", ib);
700 type_hex.dump(&v[1], stderr);
702 fputs(v[1].buf, stderr);
703 mp_writedstr(m, &d, 10);
704 fprintf(stderr, "\n*** (value = %s)\n", d.buf);
707 mp_writedstr(m, &d, ob);
708 if (d.len != v[3].len || memcmp(d.buf, v[3].buf, d.len) != 0) {
709 fprintf(stderr, "*** failed read or write\n"
710 "*** input [%2i] = ", ib);
712 type_hex.dump(&v[1], stderr);
714 fputs(v[1].buf, stderr);
715 fprintf(stderr, "\n*** output [%2i] = ", ob);
717 type_hex.dump(&d, stderr);
719 fputs(d.buf, stderr);
720 fprintf(stderr, "\n*** expected [%2i] = ", ob);
722 type_hex.dump(&v[3], stderr);
724 fputs(v[3].buf, stderr);
732 fprintf(stderr, "*** unexpected parse failure\n"
733 "*** input [%2i] = ", ib);
735 type_hex.dump(&v[1], stderr);
737 fputs(v[1].buf, stderr);
738 fprintf(stderr, "\n*** expected [%2i] = ", ob);
740 type_hex.dump(&v[3], stderr);
742 fputs(v[3].buf, stderr);
748 if (v[1].len - off != v[4].len ||
749 memcmp(v[1].buf + off, v[4].buf, v[4].len) != 0) {
750 fprintf(stderr, "*** leftovers incorrect\n"
751 "*** input [%2i] = ", ib);
753 type_hex.dump(&v[1], stderr);
755 fputs(v[1].buf, stderr);
756 fprintf(stderr, "\n*** expected `%s'\n"
758 v[4].buf, v[1].buf + off);
763 assert(mparena_count(MPARENA_GLOBAL) == 0);
767 static test_chunk tests[] = {
768 { "mptext-ascii", verify,
769 { &type_int, &type_string, &type_int, &type_string, &type_string, 0 } },
770 { "mptext-bin-in", verify,
771 { &type_int, &type_hex, &type_int, &type_string, &type_string, 0 } },
772 { "mptext-bin-out", verify,
773 { &type_int, &type_string, &type_int, &type_hex, &type_string, 0 } },
777 int main(int argc, char *argv[])
780 test_run(argc, argv, tests, SRCDIR "/t/mptext");
786 /*----- That's all, folks -------------------------------------------------*/