3 * $Id: mptext.c,v 1.15 2002/10/15 19:18:15 mdw Exp $
5 * Textual representation of multiprecision numbers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.15 2002/10/15 19:18:15 mdw
34 * Fix fencepost bugs in binary radix writing.
36 * Revision 1.14 2002/10/09 00:33:44 mdw
37 * Allow `0o' and `0b' prefixes for octal and binary (from Haskell)
39 * Revision 1.13 2002/10/09 00:21:06 mdw
40 * Allow user-specified `r_xx' bases to be up to 62.
42 * Revision 1.12 2002/01/13 19:51:18 mdw
43 * Extend the textual format to bases up to 62 by distinguishing case.
45 * Revision 1.11 2001/06/16 23:42:17 mdw
48 * Revision 1.10 2001/06/16 13:22:39 mdw
49 * Added fast-track code for binary output bases, and tests.
51 * Revision 1.9 2001/02/03 16:05:17 mdw
52 * Make flags be unsigned. Improve the write algorithm: recurse until the
53 * parts are one word long and use single-precision arithmetic from there.
54 * Fix off-by-one bug when breaking the number apart.
56 * Revision 1.8 2000/12/06 20:32:42 mdw
57 * Reduce binary bytes (to allow marker bits to be ignored). Fix error
58 * message string a bit. Allow leading `+' signs.
60 * Revision 1.7 2000/07/15 10:01:08 mdw
61 * Bug fix in binary input.
63 * Revision 1.6 2000/06/25 12:58:23 mdw
64 * Fix the derivation of `depth' commentary.
66 * Revision 1.5 2000/06/17 11:46:19 mdw
67 * New and much faster stack-based algorithm for reading integers. Support
68 * reading and writing binary integers in bases between 2 and 256.
70 * Revision 1.4 1999/12/22 15:56:56 mdw
71 * Use clever recursive algorithm for writing numbers out.
73 * Revision 1.3 1999/12/10 23:23:26 mdw
74 * Allocate slightly less memory.
76 * Revision 1.2 1999/11/20 22:24:15 mdw
77 * Use function versions of MPX_UMULN and MPX_UADDN.
79 * Revision 1.1 1999/11/17 18:02:16 mdw
80 * New multiprecision integer arithmetic suite.
84 /*----- Header files ------------------------------------------------------*/
94 /*----- Magical numbers ---------------------------------------------------*/
96 /* --- Maximum recursion depth --- *
98 * This is the number of bits in a @size_t@ object. Why?
100 * To see this, let %$b = \textit{MPW\_MAX} + 1$% and let %$Z$% be the
101 * largest @size_t@ value. Then the largest possible @mp@ is %$M - 1$% where
102 * %$M = b^Z$%. Let %$r$% be a radix to read or write. Since the recursion
103 * squares the radix at each step, the highest number reached by the
104 * recursion is %$d$%, where:
108 * Solving gives that %$d = \lg \log_r b^Z$%. If %$r = 2$%, this is maximum,
109 * so choosing %$d = \lg \lg b^Z = \lg (Z \lg b) = \lg Z + \lg \lg b$%.
111 * Expressing %$\lg Z$% as @CHAR_BIT * sizeof(size_t)@ yields an
112 * overestimate, since a @size_t@ representation may contain `holes'.
113 * Choosing to represent %$\lg \lg b$% by 10 is almost certainly sufficient
114 * for `some time to come'.
117 #define DEPTH (CHAR_BIT * sizeof(size_t) + 10)
119 /*----- Main code ---------------------------------------------------------*/
121 /* --- @mp_read@ --- *
123 * Arguments: @mp *m@ = destination multiprecision number
124 * @int radix@ = base to assume for data (or zero to guess)
125 * @const mptext_ops *ops@ = pointer to operations block
126 * @void *p@ = data for the operations block
128 * Returns: The integer read, or zero if it didn't work.
130 * Use: Reads an integer from some source. If the @radix@ is
131 * specified, the number is assumed to be given in that radix,
132 * with the letters `a' (either upper- or lower-case) upwards
133 * standing for digits greater than 9. Otherwise, base 10 is
134 * assumed unless the number starts with `0' (octal), `0x' (hex)
135 * or `nnn_' (base `nnn'). An arbitrary amount of whitespace
136 * before the number is ignored.
139 /* --- About the algorithm --- *
141 * The algorithm here is rather aggressive. I maintain an array of
142 * successive squarings of the radix, and a stack of partial results, each
143 * with a counter attached indicating which radix square to multiply by.
144 * Once the item at the top of the stack reaches the same counter level as
145 * the next item down, they are combined together and the result is given a
146 * counter level one higher than either of the results.
148 * Gluing the results together at the end is slightly tricky. Pay attention
151 * This is more complicated because of the need to handle the slightly
155 mp
*mp_read(mp
*m
, int radix
, const mptext_ops
*ops
, void *p
)
157 int ch
; /* Current char being considered */
158 unsigned f
= 0; /* Flags about the current number */
159 int r
; /* Radix to switch over to */
160 mpw rd
; /* Radix as an @mp@ digit */
161 mp rr
; /* The @mp@ for the radix */
162 unsigned nf
= m ? m
->f
& MP_BURN
: 0; /* New @mp@ flags */
166 mp
*pow
[DEPTH
]; /* List of powers */
167 unsigned pows
; /* Next index to fill */
168 struct { unsigned i
; mp
*m
; } s
[DEPTH
]; /* Main stack */
169 unsigned sp
; /* Current stack pointer */
177 /* --- Initialize the stacks --- */
179 mp_build(&rr
, &rd
, &rd
+ 1);
185 /* --- Initialize the destination number --- */
190 /* --- Read an initial character --- */
196 /* --- Handle an initial sign --- */
198 if (radix
>= 0 && (ch
== '-' || ch
== '+')) {
201 do ch
= ops
->get(p
); while isspace(ch
);
204 /* --- If the radix is zero, look for leading zeros --- */
207 assert(((void)"ascii radix must be <= 62", radix
<= 62));
210 } else if (radix
< 0) {
212 assert(((void)"binary radix must fit in a byte", rd
< UCHAR_MAX
));
214 } else if (ch
!= '0') {
239 /* --- Use fast algorithm for binary radix --- *
241 * This is the restart point after having parsed a radix number from the
242 * input. We check whether the radix is binary, and if so use a fast
243 * algorithm which just stacks the bits up in the right order.
250 case 2: bit
= 1; goto bin
;
251 case 4: bit
= 2; goto bin
;
252 case 8: bit
= 3; goto bin
;
253 case 16: bit
= 4; goto bin
;
254 case 32: bit
= 5; goto bin
;
255 case 64: bit
= 6; goto bin
;
256 case 128: bit
= 7; goto bin
;
260 /* --- The fast binary algorithm --- *
262 * We stack bits up starting at the top end of a word. When one word is
263 * full, we write it to the integer, and start another with the left-over
264 * bits. When the array in the integer is full, we resize using low-level
265 * calls and copy the current data to the top end. Finally, we do a single
266 * bit-shift when we know where the end of the number is.
271 unsigned b
= MPW_BITS
;
275 m
= mp_dest(MP_NEW
, 1, nf
);
279 for (;; ch
= ops
->get(p
)) {
285 /* --- Check that the character is a digit and in range --- */
292 if (ch
>= '0' && ch
<= '9')
297 if (ch
>= 'a' && ch
<= 'z') /* ASCII dependent! */
299 else if (ch
>= 'A' && ch
<= 'Z')
308 /* --- Feed the digit into the accumulator --- */
311 if (!x
&& !(f
& f_start
))
318 a
|= MPW(x
) >> (bit
- b
);
325 v
= mpalloc(m
->a
, len
);
326 memcpy(v
+ n
, m
->v
, MPWS(n
));
331 a
= (b
< MPW_BITS
) ?
MPW(x
) << b
: 0;
335 /* --- Finish up --- */
346 m
= mp_lsr(m
, m
, (unsigned long)n
* MPW_BITS
+ b
);
351 /* --- Time to start --- */
353 for (;; ch
= ops
->get(p
)) {
359 /* --- An underscore indicates a numbered base --- */
361 if (ch
== '_' && r
> 0 && r
<= 62) {
364 /* --- Clear out the stacks --- */
366 for (i
= 1; i
< pows
; i
++)
369 for (i
= 0; i
< sp
; i
++)
373 /* --- Restart the search --- */
382 /* --- Check that the character is a digit and in range --- */
389 if (ch
>= '0' && ch
<= '9')
394 if (ch
>= 'a' && ch
<= 'z') /* ASCII dependent! */
396 else if (ch
>= 'A' && ch
<= 'Z')
403 /* --- Sort out what to do with the character --- */
405 if (x
>= 10 && r
>= 0)
413 /* --- Stick the character on the end of my integer --- */
415 assert(((void)"Number is too unimaginably huge", sp
< DEPTH
));
416 s
[sp
].m
= m
= mp_new(1, nf
);
420 /* --- Now grind through the stack --- */
422 while (sp
> 0 && s
[sp
- 1].i
== s
[sp
].i
) {
424 /* --- Combine the top two items --- */
428 m
= mp_mul(m
, m
, pow
[s
[sp
].i
]);
429 m
= mp_add(m
, m
, s
[sp
+ 1].m
);
431 MP_DROP(s
[sp
+ 1].m
);
434 /* --- Make a new radix power if necessary --- */
436 if (s
[sp
].i
>= pows
) {
437 assert(((void)"Number is too unimaginably huge", pows
< DEPTH
));
438 pow
[pows
] = mp_sqr(MP_NEW
, pow
[pows
- 1]);
448 /* --- If we're done, compute the rest of the number --- */
459 /* --- Combine the top two items --- */
463 z
= mp_mul(z
, z
, pow
[s
[sp
+ 1].i
]);
465 m
= mp_add(m
, m
, s
[sp
+ 1].m
);
467 MP_DROP(s
[sp
+ 1].m
);
469 /* --- Make a new radix power if necessary --- */
471 if (s
[sp
].i
>= pows
) {
472 assert(((void)"Number is too unimaginably huge", pows
< DEPTH
));
473 pow
[pows
] = mp_sqr(MP_NEW
, pow
[pows
- 1]);
482 for (i
= 0; i
< sp
; i
++)
486 /* --- Clear the radix power list --- */
490 for (i
= 1; i
< pows
; i
++)
494 /* --- Bail out if the number was bad --- */
500 /* --- Set the sign and return --- */
511 /* --- @mp_write@ --- *
513 * Arguments: @mp *m@ = pointer to a multi-precision integer
514 * @int radix@ = radix to use when writing the number out
515 * @const mptext_ops *ops@ = pointer to an operations block
516 * @void *p@ = data for the operations block
518 * Returns: Zero if it worked, nonzero otherwise.
520 * Use: Writes a large integer in textual form.
523 /* --- Simple case --- *
525 * Use a fixed-sized buffer and single-precision arithmetic to pick off
526 * low-order digits. Put each digit in a buffer, working backwards from the
527 * end. If the buffer becomes full, recurse to get another one. Ensure that
528 * there are at least @z@ digits by writing leading zeroes if there aren't
529 * enough real digits.
532 static int simple(mpw n
, int radix
, unsigned z
,
533 const mptext_ops
*ops
, void *p
)
537 unsigned i
= sizeof(buf
);
538 int rd
= radix
> 0 ? radix
: -radix
;
550 else if (x
< 36) /* Ascii specific */
560 rc
= simple(n
, radix
, z
, ops
, p
);
563 memset(zbuf
, (radix
< 0) ?
0 : '0', sizeof(zbuf
));
564 while (!rc
&& z
>= sizeof(zbuf
)) {
565 rc
= ops
->put(zbuf
, sizeof(zbuf
), p
);
569 rc
= ops
->put(zbuf
, z
, p
);
572 rc
= ops
->put(buf
+ i
, sizeof(buf
) - i
, p
);
577 /* --- Complicated case --- *
579 * If the number is small, fall back to the simple case above. Otherwise
580 * divide and take remainder by current large power of the radix, and emit
581 * each separately. Don't emit a zero quotient. Be very careful about
582 * leading zeroes on the remainder part, because they're deeply significant.
585 static int complicated(mp
*m
, int radix
, mp
**pr
, unsigned i
, unsigned z
,
586 const mptext_ops
*ops
, void *p
)
593 return (simple(MP_LEN(m
) ? m
->v
[0] : 0, radix
, z
, ops
, p
));
596 mp_div(&q
, &m
, m
, pr
[i
]);
604 rc
= complicated(q
, radix
, pr
, i
- 1, z
, ops
, p
);
607 rc
= complicated(m
, radix
, pr
, i
- 1, d
, ops
, p
);
612 /* --- Binary case --- *
614 * Special case for binary output. Goes much faster.
617 static int binary(mp
*m
, int bit
, int radix
, const mptext_ops
*ops
, void *p
)
632 /* --- Work out where to start --- */
636 n
+= bit
- (n
% bit
);
640 if (n
>= MP_LEN(m
)) {
647 mask
= (1 << bit
) - 1;
650 /* --- Main code --- */
666 if (!x
&& !(f
& f_out
))
674 ch
= 'a' + x
- 10; /* Ascii specific */
678 if (q
>= buf
+ sizeof(buf
)) {
679 if ((rc
= ops
->put(buf
, sizeof(buf
), p
)) != 0)
692 ch
= 'a' + x
- 10; /* Ascii specific */
696 rc
= ops
->put(buf
, q
- buf
, p
);
705 /* --- Main driver code --- */
707 int mp_write(mp
*m
, int radix
, const mptext_ops
*ops
, void *p
)
711 if (MP_EQ(m
, MP_ZERO
))
712 return (ops
->put("0", 1, p
));
714 /* --- Set various things up --- */
719 /* --- Check the radix for sensibleness --- */
722 assert(((void)"ascii radix must be <= 62", radix
<= 62));
724 assert(((void)"binary radix must fit in a byte", -radix
< UCHAR_MAX
));
726 assert(((void)"radix can't be zero in mp_write", 0));
728 /* --- If the number is negative, sort that out --- */
731 if (ops
->put("-", 1, p
))
736 /* --- Handle binary radix --- */
739 case 2: case -2: return (binary(m
, 1, radix
, ops
, p
));
740 case 4: case -4: return (binary(m
, 2, radix
, ops
, p
));
741 case 8: case -8: return (binary(m
, 3, radix
, ops
, p
));
742 case 16: case -16: return (binary(m
, 4, radix
, ops
, p
));
743 case 32: case -32: return (binary(m
, 5, radix
, ops
, p
));
744 case -64: return (binary(m
, 6, radix
, ops
, p
));
745 case -128: return (binary(m
, 7, radix
, ops
, p
));
748 /* --- If the number is small, do it the easy way --- */
751 rc
= simple(MP_LEN(m
) ? m
->v
[0] : 0, radix
, 0, ops
, p
);
753 /* --- Use a clever algorithm --- *
755 * Square the radix repeatedly, remembering old results, until I get
756 * something more than half the size of the number @m@. Use this to divide
757 * the number: the quotient and remainder will be approximately the same
758 * size, and I'll have split them on a digit boundary, so I can just emit
759 * the quotient and remainder recursively, in order.
764 size_t target
= (MP_LEN(m
) + 1) / 2;
766 mp
*z
= mp_new(1, 0);
768 /* --- Set up the exponent table --- */
770 z
->v
[0] = (radix
> 0 ? radix
: -radix
);
773 assert(((void)"Number is too unimaginably huge", i
< DEPTH
));
775 if (MP_LEN(z
) > target
)
777 z
= mp_sqr(MP_NEW
, z
);
780 /* --- Write out the answer --- */
782 rc
= complicated(m
, radix
, pr
, i
- 1, 0, ops
, p
);
784 /* --- Tidy away the array --- */
790 /* --- Tidying up code --- */
796 /*----- Test rig ----------------------------------------------------------*/
800 #include <mLib/testrig.h>
802 static int verify(dstr
*v
)
805 int ib
= *(int *)v
[0].buf
, ob
= *(int *)v
[2].buf
;
807 mp
*m
= mp_readdstr(MP_NEW
, &v
[1], 0, ib
);
810 fprintf(stderr
, "*** unexpected successful parse\n"
811 "*** input [%2i] = ", ib
);
813 type_hex
.dump(&v
[1], stderr
);
815 fputs(v
[1].buf
, stderr
);
816 mp_writedstr(m
, &d
, 10);
817 fprintf(stderr
, "\n*** (value = %s)\n", d
.buf
);
820 mp_writedstr(m
, &d
, ob
);
821 if (d
.len
!= v
[3].len
|| memcmp(d
.buf
, v
[3].buf
, d
.len
) != 0) {
822 fprintf(stderr
, "*** failed read or write\n"
823 "*** input [%2i] = ", ib
);
825 type_hex
.dump(&v
[1], stderr
);
827 fputs(v
[1].buf
, stderr
);
828 fprintf(stderr
, "\n*** output [%2i] = ", ob
);
830 type_hex
.dump(&d
, stderr
);
832 fputs(d
.buf
, stderr
);
833 fprintf(stderr
, "\n*** expected [%2i] = ", ob
);
835 type_hex
.dump(&v
[3], stderr
);
837 fputs(v
[3].buf
, stderr
);
845 fprintf(stderr
, "*** unexpected parse failure\n"
846 "*** input [%i] = ", ib
);
848 type_hex
.dump(&v
[1], stderr
);
850 fputs(v
[1].buf
, stderr
);
851 fprintf(stderr
, "\n*** expected [%i] = ", ob
);
853 type_hex
.dump(&v
[3], stderr
);
855 fputs(v
[3].buf
, stderr
);
862 assert(mparena_count(MPARENA_GLOBAL
) == 0);
866 static test_chunk tests
[] = {
867 { "mptext-ascii", verify
,
868 { &type_int
, &type_string
, &type_int
, &type_string
, 0 } },
869 { "mptext-bin-in", verify
,
870 { &type_int
, &type_hex
, &type_int
, &type_string
, 0 } },
871 { "mptext-bin-out", verify
,
872 { &type_int
, &type_string
, &type_int
, &type_hex
, 0 } },
876 int main(int argc
, char *argv
[])
879 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mptext");
885 /*----- That's all, folks -------------------------------------------------*/