progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / math / mptext.c
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
d3409d5e 3 * Textual representation of multiprecision numbers
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
d3409d5e 9 *
10 * This file is part of Catacomb.
11 *
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.
45c0fd36 16 *
d3409d5e 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.
45c0fd36 21 *
d3409d5e 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,
25 * MA 02111-1307, USA.
26 */
27
d3409d5e 28/*----- Header files ------------------------------------------------------*/
29
30#include <ctype.h>
2b26f2d7 31#include <limits.h>
d3409d5e 32#include <stdio.h>
33
141c1284
MW
34#include <mLib/macros.h>
35
d3409d5e 36#include "mp.h"
37#include "mptext.h"
e360a4f2 38#include "paranoia.h"
d3409d5e 39
2b26f2d7 40/*----- Magical numbers ---------------------------------------------------*/
41
42/* --- Maximum recursion depth --- *
43 *
45c0fd36 44 * This is the number of bits in a @size_t@ object. Why?
2b26f2d7 45 *
eaa515d8 46 * To see this, let %$b = \textit{MPW\_MAX} + 1$% and let %$Z$% be the
dd9199f0 47 * largest @size_t@ value. Then the largest possible @mp@ is %$M - 1$% where
48 * %$M = b^Z$%. Let %$r$% be a radix to read or write. Since the recursion
49 * squares the radix at each step, the highest number reached by the
50 * recursion is %$d$%, where:
2b26f2d7 51 *
dd9199f0 52 * %$r^{2^d} = b^Z$%.
2b26f2d7 53 *
54 * Solving gives that %$d = \lg \log_r b^Z$%. If %$r = 2$%, this is maximum,
55 * so choosing %$d = \lg \lg b^Z = \lg (Z \lg b) = \lg Z + \lg \lg b$%.
56 *
57 * Expressing %$\lg Z$% as @CHAR_BIT * sizeof(size_t)@ yields an
58 * overestimate, since a @size_t@ representation may contain `holes'.
59 * Choosing to represent %$\lg \lg b$% by 10 is almost certainly sufficient
60 * for `some time to come'.
61 */
62
63#define DEPTH (CHAR_BIT * sizeof(size_t) + 10)
64
626cd971 65/*----- Input -------------------------------------------------------------*/
d3409d5e 66
67/* --- @mp_read@ --- *
68 *
69 * Arguments: @mp *m@ = destination multiprecision number
70 * @int radix@ = base to assume for data (or zero to guess)
71 * @const mptext_ops *ops@ = pointer to operations block
72 * @void *p@ = data for the operations block
73 *
74 * Returns: The integer read, or zero if it didn't work.
75 *
76 * Use: Reads an integer from some source. If the @radix@ is
77 * specified, the number is assumed to be given in that radix,
78 * with the letters `a' (either upper- or lower-case) upwards
79 * standing for digits greater than 9. Otherwise, base 10 is
80 * assumed unless the number starts with `0' (octal), `0x' (hex)
81 * or `nnn_' (base `nnn'). An arbitrary amount of whitespace
82 * before the number is ignored.
83 */
84
2b26f2d7 85/* --- About the algorithm --- *
86 *
87 * The algorithm here is rather aggressive. I maintain an array of
88 * successive squarings of the radix, and a stack of partial results, each
89 * with a counter attached indicating which radix square to multiply by.
90 * Once the item at the top of the stack reaches the same counter level as
91 * the next item down, they are combined together and the result is given a
92 * counter level one higher than either of the results.
93 *
94 * Gluing the results together at the end is slightly tricky. Pay attention
95 * to the code.
96 *
97 * This is more complicated because of the need to handle the slightly
98 * bizarre syntax.
99 */
100
a6b6ae6b 101static int char_digit(int ch, int radix)
d3409d5e 102{
a6b6ae6b
MW
103 int r = radix < 0 ? -radix : radix;
104 int d;
105
106 if (ch < 0) return (-1);
107 if (radix < 0) d = ch;
108 else if ('0' <= ch && ch <= '9') d = ch - '0';
109 else if ('a' <= ch && ch <= 'z') d = ch - 'a' + 10;
110 else if ('A' <= ch && ch <= 'Z') d = ch - 'A' + (radix > 36 ? 36 : 10);
111 else return (-1);
112 if (d >= r) return (-1);
113 return (d);
114}
a951033d 115
a6b6ae6b
MW
116static mp *read_binary(int radix, unsigned bit, unsigned nf,
117 const mptext_ops *ops, void *p)
118{
119 mpw a = 0;
120 unsigned b = MPW_BITS;
121 int any = 0, nz = 0;
122 int ch, d;
123 size_t len, n;
124 mpw *v;
125 mp *m;
a951033d 126
127 /* --- The fast binary algorithm --- *
128 *
129 * We stack bits up starting at the top end of a word. When one word is
130 * full, we write it to the integer, and start another with the left-over
131 * bits. When the array in the integer is full, we resize using low-level
132 * calls and copy the current data to the top end. Finally, we do a single
133 * bit-shift when we know where the end of the number is.
134 */
135
a6b6ae6b
MW
136 m = mp_dest(MP_NEW, 1, nf);
137 len = n = m->sz;
138 n = len;
139 v = m->v + n;
a951033d 140
a6b6ae6b
MW
141 for (;;) {
142 ch = ops->get(p);
143 if ((d = char_digit(ch, radix)) < 0) break;
a951033d 144
a6b6ae6b 145 /* --- Ignore leading zeroes, but notice that the number is valid --- */
a951033d 146
a6b6ae6b
MW
147 any = 1;
148 if (!d && !nz) continue;
149 nz = 1;
a951033d 150
a6b6ae6b 151 /* --- Feed the digit into the accumulator --- */
a951033d 152
a6b6ae6b
MW
153 if (b > bit) {
154 b -= bit;
155 a |= MPW(d) << b;
a951033d 156 } else {
a6b6ae6b
MW
157 a |= MPW(d) >> (bit - b);
158 b += MPW_BITS - bit;
159 *--v = MPW(a); n--;
160 if (!n) {
161 n = len; len <<= 1;
162 v = mpalloc(m->a, len);
163 memcpy(v + n, m->v, MPWS(n));
164 mpfree(m->a, m->v);
165 m->v = v; v = m->v + n;
166 }
167 a = (b < MPW_BITS) ? MPW(d) << b : 0;
a951033d 168 }
a6b6ae6b 169 }
a951033d 170
a6b6ae6b 171 /* --- Finish up --- */
d3409d5e 172
a6b6ae6b
MW
173 ops->unget(ch, p);
174 if (!any) { mp_drop(m); return (0); }
d3409d5e 175
a6b6ae6b
MW
176 *--v = MPW(a); n--;
177 m->sz = len;
178 m->vl = m->v + len;
179 m->f &= ~MP_UNDEF;
180 m = mp_lsr(m, m, (unsigned long)n * MPW_BITS + b);
7d45ed6c 181
a6b6ae6b
MW
182 return (m);
183}
d3409d5e 184
a6b6ae6b 185struct readstate {
2b26f2d7 186
a6b6ae6b
MW
187 /* --- State for the general-base reader --- *
188 *
189 * There are two arrays. The @pow@ array is set so that @pow[i]@ contains
190 * %$R^{2^i}$% for @i < pows@. The stack @s@ contains partial results:
191 * each entry contains a value @m@ corresponding to %$2^i$% digits.
192 * Inductively, an empty stack represents zero; if a stack represents %$x$%
193 * then pushing a new entry on the top causes the stack to represent
194 * %$R^{2^i} x + m$%.
195 *
196 * It is an invariant that each entry has a strictly smaller @i@ than the
197 * items beneath it. This is achieved by coaslescing entries at the top if
198 * they have equal %$i$% values: if the top items are %$(m, i)$%, and
199 * %$(M', i)$%, and the rest of the stack represents the integer %$x$%,
200 * then %$R^{2^i} (R^{2^i} x + M) + m = R^{2^{i+1}} x + (R^{2^i} M + m)$%,
201 * so we replace the top two items by %$((R^{2^i} M + m), i + 1)$%, and
202 * repeat if necessary.
203 */
2b26f2d7 204
a6b6ae6b
MW
205 unsigned pows, sp;
206 struct { unsigned i; mp *m; } s[DEPTH];
207 mp *pow[DEPTH];
208};
2b26f2d7 209
a6b6ae6b
MW
210static void ensure_power(struct readstate *rs)
211{
212 /* --- Make sure we have the necessary %$R^{2^i}$% computed --- */
2b26f2d7 213
a6b6ae6b
MW
214 if (rs->s[rs->sp].i >= rs->pows) {
215 assert(rs->pows < DEPTH);
216 rs->pow[rs->pows] = mp_sqr(MP_NEW, rs->pow[rs->pows - 1]);
217 rs->pows++;
218 }
219}
d3409d5e 220
a6b6ae6b
MW
221static void read_digit(struct readstate *rs, unsigned nf, int d)
222{
223 mp *m = mp_new(1, nf);
224 m->v[0] = d;
d3409d5e 225
a6b6ae6b 226 /* --- Put the new digit on top --- */
d3409d5e 227
a6b6ae6b
MW
228 assert(rs->sp < DEPTH);
229 rs->s[rs->sp].m = m;
230 rs->s[rs->sp].i = 0;
d3409d5e 231
a6b6ae6b 232 /* --- Restore the stack invariant --- */
d3409d5e 233
a6b6ae6b
MW
234 while (rs->sp && rs->s[rs->sp - 1].i <= rs->s[rs->sp].i) {
235 assert(rs->sp > 0);
236 ensure_power(rs);
237 rs->sp--;
d3409d5e 238
a6b6ae6b
MW
239 m = rs->s[rs->sp].m;
240 m = mp_mul(m, m, rs->pow[rs->s[rs->sp + 1].i]);
241 m = mp_add(m, m, rs->s[rs->sp + 1].m);
242 MP_DROP(rs->s[rs->sp + 1].m);
243 rs->s[rs->sp].m = m;
244 rs->s[rs->sp].i++;
245 }
d3409d5e 246
a6b6ae6b 247 /* --- Leave the stack pointer at an empty item --- */
2b26f2d7 248
a6b6ae6b
MW
249 rs->sp++;
250}
2b26f2d7 251
a6b6ae6b
MW
252static mp *read_general(int radix, unsigned t, unsigned nf,
253 const mptext_ops *ops, void *p)
254{
255 struct readstate rs;
256 unsigned char v[4];
257 unsigned i;
258 mpw r;
259 int any = 0;
260 int ch, d;
261 mp rr;
262 mp *m, *z, *n;
263
264 /* --- Prepare the stack --- */
265
266 r = radix < 0 ? -radix : radix;
267 mp_build(&rr, &r, &r + 1);
268 rs.pow[0] = &rr;
269 rs.pows = 1;
270 rs.sp = 0;
271
272 /* --- If we've partially parsed some input then feed it in --- *
273 *
274 * Unfortunately, what we've got is backwards. Fortunately there's a
275 * fairly tight upper bound on how many digits @t@ might be, since we
276 * aborted that loop once it got too large.
277 */
278
279 if (t) {
280 i = 0;
281 while (t) { assert(i < sizeof(v)); v[i++] = t%r; t /= r; }
282 while (i) read_digit(&rs, nf, v[--i]);
283 any = 1;
284 }
2b26f2d7 285
a6b6ae6b 286 /* --- Read more stuff --- */
2b26f2d7 287
a6b6ae6b
MW
288 for (;;) {
289 ch = ops->get(p);
290 if ((d = char_digit(ch, radix)) < 0) break;
291 read_digit(&rs, nf, d); any = 1;
292 }
293 ops->unget(ch, p);
2b26f2d7 294
a6b6ae6b
MW
295 /* --- Stitch all of the numbers together --- *
296 *
297 * This is not the same code as @read_digit@. In particular, here we must
298 * cope with the partial result being some inconvenient power of %$R$%,
299 * rather than %$R^{2^i}$%.
300 */
2b26f2d7 301
a6b6ae6b
MW
302 if (!any) return (0);
303 m = MP_ZERO; z = MP_ONE;
304 while (rs.sp) {
305 rs.sp--;
306 ensure_power(&rs);
307 n = rs.s[rs.sp].m;
308 n = mp_mul(n, n, z);
309 m = mp_add(m, m, n);
310 z = mp_mul(z, z, rs.pow[rs.s[rs.sp].i]);
311 MP_DROP(n);
d3409d5e 312 }
a6b6ae6b
MW
313 for (i = 0; i < rs.pows; i++) MP_DROP(rs.pow[i]);
314 MP_DROP(z);
315 return (m);
316}
d3409d5e 317
a6b6ae6b
MW
318mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p)
319{
320 unsigned t = 0;
321 unsigned nf = 0;
322 int ch, d, rd;
323
324 unsigned f = 0;
325#define f_neg 1u
326#define f_ok 2u
d3409d5e 327
a6b6ae6b
MW
328 /* --- We don't actually need a destination so throw it away --- *
329 *
330 * But note the flags before we lose it entirely.
331 */
2b26f2d7 332
a6b6ae6b
MW
333 if (m) {
334 nf = m->f & MP_BURN;
335 MP_DROP(m);
336 }
2b26f2d7 337
a6b6ae6b 338 /* --- Maintain a lookahead character --- */
2b26f2d7 339
a6b6ae6b 340 ch = ops->get(p);
2b26f2d7 341
a6b6ae6b 342 /* --- If we're reading text, skip leading space, and maybe a sign --- */
2b26f2d7 343
a6b6ae6b 344 if (radix >= 0) {
141c1284 345 while (ISSPACE(ch)) ch = ops->get(p);
a6b6ae6b
MW
346 switch (ch) {
347 case '-': f |= f_neg; /* and on */
141c1284 348 case '+': do ch = ops->get(p); while (ISSPACE(ch));
a6b6ae6b
MW
349 }
350 }
351
352 /* --- If we don't have a fixed radix, then parse one from the input --- *
353 *
354 * This is moderately easy if the input starts with `0x' or similar. If it
355 * starts with `0' and something else, then it might be octal, or just a
356 * plain old zero. Finally, it might start with a leading `NN_', in which
357 * case we carefully collect the decimal number until we're sure it's
358 * either a radix prefix (in which case we accept it and start over) or it
359 * isn't (in which case it's actually the start of a large number we need
360 * to read).
361 */
2b26f2d7 362
a6b6ae6b
MW
363 if (radix == 0) {
364 if (ch == '0') {
365 ch = ops->get(p);
366 switch (ch) {
367 case 'x': case 'X': radix = 16; goto fetch;
368 case 'o': case 'O': radix = 8; goto fetch;
369 case 'b': case 'B': radix = 2; goto fetch;
370 fetch: ch = ops->get(p); break;
371 default: radix = 8; f |= f_ok; break;
372 }
373 } else {
374 if ((d = char_digit(ch, 10)) < 0) { ops->unget(ch, p); return (0); }
375 for (;;) {
376 t = 10*t + d;
377 ch = ops->get(p);
378 if (t > 52) break;
379 if ((d = char_digit(ch, 10)) < 0) break;
380 }
381 if (ch != '_' || t > 52) radix = 10;
382 else {
383 radix = t; t = 0;
384 ch = ops->get(p);
2b26f2d7 385 }
2b26f2d7 386 }
2b26f2d7 387 }
388
a6b6ae6b
MW
389 /* --- We're now ready to dispatch to the correct handler --- */
390
391 rd = radix < 0 ? -radix : radix;
392 ops->unget(ch, p);
393 switch (rd) {
394 case 2: m = read_binary(radix, 1, nf, ops, p); break;
395 case 4: m = read_binary(radix, 2, nf, ops, p); break;
396 case 8: m = read_binary(radix, 3, nf, ops, p); break;
397 case 16: m = read_binary(radix, 4, nf, ops, p); break;
398 case 32: m = read_binary(radix, 5, nf, ops, p); break;
399 case 64: m = read_binary(radix, 6, nf, ops, p); break;
400 case 128: m = read_binary(radix, 7, nf, ops, p); break;
401 default: m = read_general(radix, t, nf, ops, p); break;
402 }
403
404 /* --- That didn't work --- *
405 *
406 * If we've already read something then return that. Otherwise it's an
407 * error.
408 */
2b26f2d7 409
a6b6ae6b
MW
410 if (!m) {
411 if (f & f_ok) return (MP_ZERO);
412 else return (0);
2b26f2d7 413 }
414
a6b6ae6b 415 /* --- Negate the result if we should do that --- */
d3409d5e 416
a6b6ae6b 417 if (f & f_neg) m = mp_neg(m, m);
d3409d5e 418
a6b6ae6b 419 /* --- And we're all done --- */
d3409d5e 420
d3409d5e 421 return (m);
3bc9cb53 422
423#undef f_neg
424#undef f_ok
d3409d5e 425}
426
626cd971
MW
427/*----- Output ------------------------------------------------------------*/
428
d3409d5e 429/* --- @mp_write@ --- *
430 *
431 * Arguments: @mp *m@ = pointer to a multi-precision integer
432 * @int radix@ = radix to use when writing the number out
433 * @const mptext_ops *ops@ = pointer to an operations block
434 * @void *p@ = data for the operations block
435 *
436 * Returns: Zero if it worked, nonzero otherwise.
437 *
438 * Use: Writes a large integer in textual form.
439 */
440
626cd971
MW
441static int digit_char(int d, int radix)
442{
443 if (radix < 0) return (d);
444 else if (d < 10) return (d + '0');
445 else if (d < 26) return (d - 10 + 'a');
446 else return (d - 36 + 'A');
447}
448
e360a4f2 449/* --- Simple case --- *
450 *
3bc9cb53 451 * Use a fixed-sized buffer and single-precision arithmetic to pick off
452 * low-order digits. Put each digit in a buffer, working backwards from the
453 * end. If the buffer becomes full, recurse to get another one. Ensure that
454 * there are at least @z@ digits by writing leading zeroes if there aren't
455 * enough real digits.
e360a4f2 456 */
457
626cd971
MW
458static int write_simple(mpw n, int radix, unsigned z,
459 const mptext_ops *ops, void *p)
e360a4f2 460{
461 int rc = 0;
462 char buf[64];
463 unsigned i = sizeof(buf);
2b26f2d7 464 int rd = radix > 0 ? radix : -radix;
626cd971 465 mpw x;
e360a4f2 466
467 do {
626cd971
MW
468 x = n % rd; n /= rd;
469 buf[--i] = digit_char(x, radix);
470 if (z) z--;
3bc9cb53 471 } while (i && n);
e360a4f2 472
3bc9cb53 473 if (n)
626cd971 474 rc = write_simple(n, radix, z, ops, p);
e360a4f2 475 else {
a951033d 476 char zbuf[32];
477 memset(zbuf, (radix < 0) ? 0 : '0', sizeof(zbuf));
478 while (!rc && z >= sizeof(zbuf)) {
479 rc = ops->put(zbuf, sizeof(zbuf), p);
480 z -= sizeof(zbuf);
e360a4f2 481 }
626cd971 482 if (!rc && z) rc = ops->put(zbuf, z, p);
e360a4f2 483 }
626cd971 484 if (!rc) rc = ops->put(buf + i, sizeof(buf) - i, p);
3bc9cb53 485 BURN(buf);
e360a4f2 486 return (rc);
487}
488
489/* --- Complicated case --- *
490 *
491 * If the number is small, fall back to the simple case above. Otherwise
492 * divide and take remainder by current large power of the radix, and emit
493 * each separately. Don't emit a zero quotient. Be very careful about
494 * leading zeroes on the remainder part, because they're deeply significant.
495 */
496
626cd971
MW
497static int write_complicated(mp *m, int radix, mp **pr,
498 unsigned i, unsigned z,
499 const mptext_ops *ops, void *p)
e360a4f2 500{
501 int rc = 0;
502 mp *q = MP_NEW;
503 unsigned d = 1 << i;
504
3bc9cb53 505 if (MP_LEN(m) < 2)
626cd971 506 return (write_simple(MP_LEN(m) ? m->v[0] : 0, radix, z, ops, p));
e360a4f2 507
3bc9cb53 508 assert(i);
e360a4f2 509 mp_div(&q, &m, m, pr[i]);
626cd971 510 if (MP_ZEROP(q)) d = z;
e360a4f2 511 else {
626cd971
MW
512 if (z > d) z -= d;
513 else z = 0;
514 rc = write_complicated(q, radix, pr, i - 1, z, ops, p);
e360a4f2 515 }
626cd971 516 if (!rc) rc = write_complicated(m, radix, pr, i - 1, d, ops, p);
e360a4f2 517 mp_drop(q);
518 return (rc);
519}
520
a951033d 521/* --- Binary case --- *
522 *
523 * Special case for binary output. Goes much faster.
524 */
525
626cd971
MW
526static int write_binary(mp *m, int bit, int radix,
527 const mptext_ops *ops, void *p)
a951033d 528{
529 mpw *v;
530 mpw a;
531 int rc = 0;
532 unsigned b;
533 unsigned mask;
534 unsigned long n;
535 unsigned f = 0;
536 char buf[8], *q;
537 unsigned x;
a951033d 538
539#define f_out 1u
540
541 /* --- Work out where to start --- */
542
543 n = mp_bits(m);
626cd971 544 if (n % bit) n += bit - (n % bit);
a951033d 545 b = n % MPW_BITS;
546 n /= MPW_BITS;
afd054c1 547
548 if (n >= MP_LEN(m)) {
a951033d 549 n--;
550 b += MPW_BITS;
551 }
552
553 v = m->v + n;
554 a = *v;
555 mask = (1 << bit) - 1;
556 q = buf;
557
558 /* --- Main code --- */
559
560 for (;;) {
561 if (b > bit) {
562 b -= bit;
563 x = a >> b;
564 } else {
565 x = a << (bit - b);
566 b += MPW_BITS - bit;
626cd971 567 if (v == m->v) break;
a951033d 568 a = *--v;
626cd971 569 if (b < MPW_BITS) x |= a >> b;
a951033d 570 }
571 x &= mask;
626cd971 572 if (!x && !(f & f_out)) continue;
a951033d 573
626cd971 574 *q++ = digit_char(x, radix);
a951033d 575 if (q >= buf + sizeof(buf)) {
626cd971 576 if ((rc = ops->put(buf, sizeof(buf), p)) != 0) goto done;
a951033d 577 q = buf;
578 }
579 f |= f_out;
580 }
581
582 x &= mask;
626cd971 583 *q++ = digit_char(x, radix);
a951033d 584 rc = ops->put(buf, q - buf, p);
585
586done:
587 mp_drop(m);
588 return (rc);
589
590#undef f_out
591}
592
e360a4f2 593/* --- Main driver code --- */
594
d3409d5e 595int mp_write(mp *m, int radix, const mptext_ops *ops, void *p)
596{
e360a4f2 597 int rc;
626cd971
MW
598 mp *pr[DEPTH];
599 size_t target;
600 unsigned i = 0;
601 mp *z;
d3409d5e 602
afd054c1 603 if (MP_EQ(m, MP_ZERO))
572b324a 604 return (ops->put(radix > 0 ? "0" : "\0", 1, p));
afd054c1 605
d3409d5e 606 /* --- Set various things up --- */
607
608 m = MP_COPY(m);
e360a4f2 609 MP_SPLIT(m);
d3409d5e 610
2b26f2d7 611 /* --- Check the radix for sensibleness --- */
612
613 if (radix > 0)
631673a1 614 assert(((void)"ascii radix must be <= 62", radix <= 62));
2b26f2d7 615 else if (radix < 0)
25b5e686 616 assert(((void)"binary radix must fit in a byte", -radix <= UCHAR_MAX));
2b26f2d7 617 else
618 assert(((void)"radix can't be zero in mp_write", 0));
619
d3409d5e 620 /* --- If the number is negative, sort that out --- */
621
a69a3efd 622 if (MP_NEGP(m)) {
572b324a 623 assert(radix > 0);
626cd971 624 if (ops->put("-", 1, p)) return (EOF);
2b26f2d7 625 m->f &= ~MP_NEG;
d3409d5e 626 }
627
a951033d 628 /* --- Handle binary radix --- */
629
630 switch (radix) {
626cd971
MW
631 case 2: case -2: return (write_binary(m, 1, radix, ops, p));
632 case 4: case -4: return (write_binary(m, 2, radix, ops, p));
633 case 8: case -8: return (write_binary(m, 3, radix, ops, p));
634 case 16: case -16: return (write_binary(m, 4, radix, ops, p));
635 case 32: case -32: return (write_binary(m, 5, radix, ops, p));
636 case -64: return (write_binary(m, 6, radix, ops, p));
637 case -128: return (write_binary(m, 7, radix, ops, p));
a951033d 638 }
639
e360a4f2 640 /* --- If the number is small, do it the easy way --- */
641
3bc9cb53 642 if (MP_LEN(m) < 2)
626cd971 643 rc = write_simple(MP_LEN(m) ? m->v[0] : 0, radix, 0, ops, p);
e360a4f2 644
645 /* --- Use a clever algorithm --- *
646 *
647 * Square the radix repeatedly, remembering old results, until I get
648 * something more than half the size of the number @m@. Use this to divide
649 * the number: the quotient and remainder will be approximately the same
650 * size, and I'll have split them on a digit boundary, so I can just emit
651 * the quotient and remainder recursively, in order.
e360a4f2 652 */
653
654 else {
626cd971
MW
655 target = (MP_LEN(m) + 1) / 2;
656 z = mp_new(1, 0);
e360a4f2 657
658 /* --- Set up the exponent table --- */
659
2b26f2d7 660 z->v[0] = (radix > 0 ? radix : -radix);
e360a4f2 661 z->f = 0;
662 for (;;) {
2b26f2d7 663 assert(((void)"Number is too unimaginably huge", i < DEPTH));
e360a4f2 664 pr[i++] = z;
626cd971 665 if (MP_LEN(z) > target) break;
e360a4f2 666 z = mp_sqr(MP_NEW, z);
667 }
d3409d5e 668
e360a4f2 669 /* --- Write out the answer --- */
d3409d5e 670
626cd971 671 rc = write_complicated(m, radix, pr, i - 1, 0, ops, p);
d3409d5e 672
e360a4f2 673 /* --- Tidy away the array --- */
d3409d5e 674
626cd971 675 while (i > 0) mp_drop(pr[--i]);
d3409d5e 676 }
e360a4f2 677
678 /* --- Tidying up code --- */
679
680 MP_DROP(m);
681 return (rc);
d3409d5e 682}
683
684/*----- Test rig ----------------------------------------------------------*/
685
686#ifdef TEST_RIG
687
688#include <mLib/testrig.h>
689
690static int verify(dstr *v)
691{
692 int ok = 1;
693 int ib = *(int *)v[0].buf, ob = *(int *)v[2].buf;
694 dstr d = DSTR_INIT;
50bea2af 695 size_t off = 0;
696 mp *m = mp_readdstr(MP_NEW, &v[1], &off, ib);
d3409d5e 697 if (m) {
698 if (!ob) {
699 fprintf(stderr, "*** unexpected successful parse\n"
45c0fd36 700 "*** input [%2i] = ", ib);
2b26f2d7 701 if (ib < 0)
702 type_hex.dump(&v[1], stderr);
703 else
704 fputs(v[1].buf, stderr);
d3409d5e 705 mp_writedstr(m, &d, 10);
2b26f2d7 706 fprintf(stderr, "\n*** (value = %s)\n", d.buf);
d3409d5e 707 ok = 0;
708 } else {
709 mp_writedstr(m, &d, ob);
141c1284 710 if (d.len != v[3].len || MEMCMP(d.buf, !=, v[3].buf, d.len)) {
d3409d5e 711 fprintf(stderr, "*** failed read or write\n"
45c0fd36 712 "*** input [%2i] = ", ib);
2b26f2d7 713 if (ib < 0)
714 type_hex.dump(&v[1], stderr);
715 else
716 fputs(v[1].buf, stderr);
45c0fd36 717 fprintf(stderr, "\n*** output [%2i] = ", ob);
2b26f2d7 718 if (ob < 0)
719 type_hex.dump(&d, stderr);
720 else
721 fputs(d.buf, stderr);
45c0fd36 722 fprintf(stderr, "\n*** expected [%2i] = ", ob);
2b26f2d7 723 if (ob < 0)
724 type_hex.dump(&v[3], stderr);
725 else
726 fputs(v[3].buf, stderr);
727 fputc('\n', stderr);
d3409d5e 728 ok = 0;
729 }
730 }
731 mp_drop(m);
732 } else {
733 if (ob) {
734 fprintf(stderr, "*** unexpected parse failure\n"
45c0fd36 735 "*** input [%2i] = ", ib);
2b26f2d7 736 if (ib < 0)
737 type_hex.dump(&v[1], stderr);
738 else
739 fputs(v[1].buf, stderr);
50bea2af 740 fprintf(stderr, "\n*** expected [%2i] = ", ob);
2b26f2d7 741 if (ob < 0)
742 type_hex.dump(&v[3], stderr);
743 else
744 fputs(v[3].buf, stderr);
745 fputc('\n', stderr);
d3409d5e 746 ok = 0;
747 }
748 }
749
50bea2af 750 if (v[1].len - off != v[4].len ||
141c1284 751 MEMCMP(v[1].buf + off, !=, v[4].buf, v[4].len)) {
50bea2af 752 fprintf(stderr, "*** leftovers incorrect\n"
45c0fd36 753 "*** input [%2i] = ", ib);
50bea2af 754 if (ib < 0)
755 type_hex.dump(&v[1], stderr);
756 else
757 fputs(v[1].buf, stderr);
758 fprintf(stderr, "\n*** expected `%s'\n"
45c0fd36 759 "*** found `%s'\n",
50bea2af 760 v[4].buf, v[1].buf + off);
761 ok = 0;
762 }
45c0fd36 763
d3409d5e 764 dstr_destroy(&d);
9c3df6c0 765 assert(mparena_count(MPARENA_GLOBAL) == 0);
d3409d5e 766 return (ok);
767}
768
769static test_chunk tests[] = {
2b26f2d7 770 { "mptext-ascii", verify,
50bea2af 771 { &type_int, &type_string, &type_int, &type_string, &type_string, 0 } },
2b26f2d7 772 { "mptext-bin-in", verify,
50bea2af 773 { &type_int, &type_hex, &type_int, &type_string, &type_string, 0 } },
2b26f2d7 774 { "mptext-bin-out", verify,
50bea2af 775 { &type_int, &type_string, &type_int, &type_hex, &type_string, 0 } },
d3409d5e 776 { 0, 0, { 0 } }
777};
778
779int main(int argc, char *argv[])
780{
781 sub_init();
0f00dc4c 782 test_run(argc, argv, tests, SRCDIR "/t/mptext");
d3409d5e 783 return (0);
784}
785
786#endif
787
788/*----- That's all, folks -------------------------------------------------*/