3 * $Id: mp-arith.c,v 1.15 2002/10/19 17:56:50 mdw Exp $
5 * Basic arithmetic on multiprecision integers
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 --------------------------------------------------*
32 * $Log: mp-arith.c,v $
33 * Revision 1.15 2002/10/19 17:56:50 mdw
34 * Fix bit operations. Test them (a bit) better.
36 * Revision 1.14 2002/10/15 19:18:31 mdw
37 * New operation to negate numbers.
39 * Revision 1.13 2002/10/15 00:19:40 mdw
40 * Bit setting and clearing functions.
42 * Revision 1.12 2002/10/09 00:36:03 mdw
43 * Fix bounds on workspace for Karatsuba operations.
45 * Revision 1.11 2002/10/06 22:52:50 mdw
46 * Pile of changes for supporting two's complement properly.
48 * Revision 1.10 2001/04/03 19:36:05 mdw
49 * Add some simple bitwise operations so that Perl can use them.
51 * Revision 1.9 2000/10/08 15:48:35 mdw
52 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
54 * Revision 1.8 2000/10/08 12:02:21 mdw
55 * Use @MP_EQ@ instead of @MP_CMP@.
57 * Revision 1.7 2000/06/22 19:02:53 mdw
58 * New function @mp_odd@ to extract powers of two from an integer. This is
59 * common code from the Rabin-Miller test, RSA key recovery and modular
60 * square-root extraction.
62 * Revision 1.6 2000/06/17 11:45:09 mdw
63 * Major memory management overhaul. Added arena support. Use the secure
64 * arena for secret integers. Replace and improve the MP management macros
65 * (e.g., replace MP_MODIFY by MP_DEST).
67 * Revision 1.5 1999/12/22 15:54:41 mdw
68 * Adjust Karatsuba parameters. Calculate destination size better.
70 * Revision 1.4 1999/12/13 15:35:16 mdw
71 * Slightly different rules on memory allocation.
73 * Revision 1.3 1999/12/11 10:57:43 mdw
74 * Karatsuba squaring algorithm.
76 * Revision 1.2 1999/12/10 23:18:39 mdw
77 * Change interface for suggested destinations.
79 * Revision 1.1 1999/11/17 18:02:16 mdw
80 * New multiprecision integer arithmetic suite.
84 /*----- Header files ------------------------------------------------------*/
88 /*----- Macros ------------------------------------------------------------*/
90 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
92 /*----- Main code ---------------------------------------------------------*/
94 /* --- @mp_lsl@, @mp_lsr@ --- *
96 * Arguments: @mp *d@ = destination
98 * @size_t n@ = number of bits to move
100 * Returns: Result, @a@ shifted left or right by @n@.
103 mp *mp_lsl(mp *d, mp *a, size_t n)
105 MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f);
106 mpx_lsl(d->v, d->vl, a->v, a->vl, n);
107 d->f = a->f & (MP_NEG | MP_BURN);
112 mp *mp_lsr(mp *d, mp *a, size_t n)
114 MP_DEST(d, MP_LEN(a), a->f);
115 mpx_lsr(d->v, d->vl, a->v, a->vl, n);
116 d->f = a->f & (MP_NEG | MP_BURN);
121 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
123 * Arguments: @mp *d@ = destination
125 * @size_t n@ = number of bits to move
127 * Returns: Result, @a@ shifted left or right by @n@. Handles the
128 * pretence of sign-extension for negative numbers.
131 mp *mp_lsl2c(mp *d, mp *a, size_t n)
133 if (!(a->f & MP_NEG))
134 return (mp_lsl(d, a, n));
141 mp *mp_lsr2c(mp *d, mp *a, size_t n)
143 if (!(a->f & MP_NEG))
144 return (mp_lsr(d, a, n));
151 /* --- @mp_testbit@ --- *
153 * Arguments: @mp *x@ = a large integer
154 * @unsigned long n@ = which bit to test
156 * Returns: Nonzero if the bit is set, zero if not.
159 int mp_testbit(mp *x, unsigned long n)
161 if (n > MPW_BITS * MP_LEN(x))
163 return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u);
166 /* --- @mp_testbit2c@ --- *
168 * Arguments: @mp *x@ = a large integer
169 * @unsigned long n@ = which bit to test
171 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
172 * complement representation.
175 int mp_testbit2c(mp *x, unsigned long n)
178 if (!(x->f & MP_NEG))
179 return (mp_testbit(x, n));
180 x = mp_not2c(MP_NEW, x);
181 r = !mp_testbit(x, n);
186 /* --- @mp_setbit@, @mp_clearbit@ --- *
188 * Arguments: @mp *d@ = a destination
189 * @mp *x@ = a large integer
190 * @unsigned long n@ = which bit to modify
192 * Returns: The argument @x@, with the appropriate bit set or cleared.
195 mp *mp_setbit(mp *d, mp *x, unsigned long n)
199 rq = n + MPW_BITS; rq -= rq % MPW_BITS;
204 MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN));
205 d->v[n/MPW_BITS] |= 1 << n%MPW_BITS;
209 mp *mp_clearbit(mp *d, mp *x, unsigned long n)
213 rq = n + MPW_BITS; rq -= rq % MPW_BITS;
218 MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN));
219 d->v[n/MPW_BITS] &= ~(1 << n%MPW_BITS);
223 /* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
225 * Arguments: @mp *d@ = a destination
226 * @mp *x@ = a large integer
227 * @unsigned long n@ = which bit to modify
229 * Returns: The argument @x@, with the appropriate bit set or cleared.
230 * Fakes up two's complement representation.
233 mp *mp_setbit2c(mp *d, mp *x, unsigned long n)
235 if (!(x->f & MP_NEG))
236 return mp_setbit(d, x, n);
238 d = mp_clearbit(d, d, n);
243 mp *mp_clearbit2c(mp *d, mp *x, unsigned long n)
245 if (!(x->f & MP_NEG))
246 return mp_clearbit(d, x, n);
248 d = mp_setbit(d, d, n);
255 * Arguments: @const mp *a, *b@ = two numbers
257 * Returns: Nonzero if the numbers are equal.
260 int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); }
262 /* --- @mp_cmp@ --- *
264 * Arguments: @const mp *a, *b@ = two numbers
266 * Returns: Less than, equal to or greater than zero, according to
267 * whether @a@ is less than, equal to or greater than @b@.
270 int mp_cmp(const mp *a, const mp *b)
272 if (!((a->f ^ b->f) & MP_NEG))
273 return (mpx_ucmp(a->v, a->vl, b->v, b->vl));
274 else if (a->f & MP_NEG)
280 /* --- @mp_neg@ --- *
282 * Arguments: @mp *d@ = destination
285 * Returns: The negation of the argument.
287 * Use: Negates its argument.
290 mp *mp_neg(mp *d, mp *a)
292 /* --- Surprising amounts of messing about required --- */
300 MP_DEST(a, MP_LEN(a), a->f);
305 /* --- @mp_bitop@ --- *
307 * Arguments: @mp *d@ = destination
308 * @mp *a, *b@ = sources
310 * Returns: The result of the given bitwise operation. These functions
311 * don't handle negative numbers at all sensibly. For that, use
312 * the @...2c@ variants. The functions are named after the
313 * truth tables they generate:
320 #define MP_BITBINOP(string) \
322 mp *mp_bit##string(mp *d, mp *a, mp *b) \
324 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \
325 mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \
326 d->f = (a->f | b->f) & MP_BURN; \
331 MPX_DOBIN(MP_BITBINOP)
333 /* --- @mp_not@ --- *
335 * Arguments: @mp *d@ = destination
338 * Returns: The bitwise complement of the source.
341 mp *mp_not(mp *d, mp *a)
343 MP_DEST(d, MP_LEN(a), a->f);
344 mpx_not(d->v, d->vl, a->v, a->vl);
345 d->f = a->f & MP_BURN;
350 /* --- @mp_bitop2c@ --- *
352 * Arguments: @mp *d@ = destination
353 * @mp *a, *b@ = sources
355 * Returns: The result of the given bitwise operation. Negative numbers
356 * are treated as two's complement, sign-extended infinitely to
357 * the left. The functions are named after the truth tables
365 /* --- How this actually works --- *
367 * The two arguments are inverted (with a sign-swap) if they're currently
368 * negative. This means that we end up using a different function (one which
369 * reinverts as we go) for the main operation. Also, if the sign would be
370 * negative at the end, we preinvert the output and then invert again with a
373 * Start with: wxyz WXYZ
374 * If @a@ negative: yzwx or YZWX
375 * If @b@ negative: xwzy XWZY
376 * If both negative: zyxw ZYXW
379 #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \
381 mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \
383 if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \
384 d = mp_bit##base(d, a, b); \
386 } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \
388 d = mp_not2c(d, a); \
389 d = mp_bit##an(d, d, b); \
392 } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \
394 d = mp_not2c(d, b); \
395 d = mp_bit##bn(d, a, d); \
398 } else { /* Both negative */ \
399 mp *t = mp_not2c(MP_NEW, a); \
400 mp *d = mp_not2c(d, b); \
401 d = mp_bit##abn(d, t, d); \
408 #define NEG d = mp_not2c(d, d);
410 MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS, POS, POS, POS)
411 MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS, POS, POS, NEG)
412 MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS, NEG, POS, POS)
413 MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS, NEG, POS, NEG)
414 MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS, POS, NEG, POS)
415 MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS, POS, NEG, NEG)
416 MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS, NEG, NEG, POS)
417 MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS, NEG, NEG, NEG)
418 MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG, POS, POS, POS)
419 MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG, POS, POS, NEG)
420 MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG, NEG, POS, POS)
421 MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG, NEG, POS, NEG)
422 MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG, POS, NEG, POS)
423 MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG, POS, NEG, NEG)
424 MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG, NEG, NEG, POS)
425 MP_BIT2CBINOP(1111, 0000, 0000, 0000, 0000, NEG, NEG, NEG, NEG)
429 /* --- @mp_not2c@ --- *
431 * Arguments: @mp *d@ = destination
434 * Returns: The sign-extended complement of the argument.
437 mp *mp_not2c(mp *d, mp *a)
441 MP_DEST(d, MP_LEN(a) + 1, a->f);
444 MPX_USUBN(d->v, d->vl, 1);
446 MPX_UADDN(d->v, d->vl, 1);
449 mpx_usub(d->v, d->vl, a->v, a->vl, &one, &one + 1);
451 mpx_uadd(d->v, d->vl, a->v, a->vl, &one, &one + 1);
453 d->f = (a->f & (MP_NEG | MP_BURN)) ^ MP_NEG;
458 /* --- @mp_add@ --- *
460 * Arguments: @mp *d@ = destination
461 * @mp *a, *b@ = sources
463 * Returns: Result, @a@ added to @b@.
466 mp *mp_add(mp *d, mp *a, mp *b)
468 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
469 if (!((a->f ^ b->f) & MP_NEG))
470 mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
472 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
473 mp *t = a; a = b; b = t;
475 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
477 d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG);
482 /* --- @mp_sub@ --- *
484 * Arguments: @mp *d@ = destination
485 * @mp *a, *b@ = sources
487 * Returns: Result, @b@ subtracted from @a@.
490 mp *mp_sub(mp *d, mp *a, mp *b)
493 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
494 if ((a->f ^ b->f) & MP_NEG)
495 mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
497 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
498 mp *t = a; a = b; b = t;
501 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
503 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG);
508 /* --- @mp_mul@ --- *
510 * Arguments: @mp *d@ = destination
511 * @mp *a, *b@ = sources
513 * Returns: Result, @a@ multiplied by @b@.
516 mp *mp_mul(mp *d, mp *a, mp *b)
521 if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= MPK_THRESH) {
522 MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF);
523 mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
525 size_t m = MAX(MP_LEN(a), MP_LEN(b));
527 MP_DEST(d, 3 * m, a->f | b->f | MP_UNDEF);
528 s = mpalloc(d->a, 5 * m);
529 mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + 5 * m);
533 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
540 /* --- @mp_sqr@ --- *
542 * Arguments: @mp *d@ = destination
545 * Returns: Result, @a@ squared.
548 mp *mp_sqr(mp *d, mp *a)
550 size_t m = MP_LEN(a);
553 if (m > MPK_THRESH) {
555 MP_DEST(d, 3 * m, a->f | MP_UNDEF);
556 s = mpalloc(d->a, 5 * m);
557 mpx_ksqr(d->v, d->vl, a->v, a->vl, s, s + 5 * m);
560 MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF);
561 mpx_usqr(d->v, d->vl, a->v, a->vl);
563 d->f = a->f & MP_BURN;
569 /* --- @mp_div@ --- *
571 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
572 * @mp *a, *b@ = sources
574 * Use: Calculates the quotient and remainder when @a@ is divided by
575 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
576 * Either of @qq@ or @rr@ may be null to indicate that the
577 * result is irrelevant. (Discarding both results is silly.)
578 * There is a performance advantage if @a == *rr@.
580 * The behaviour when @a@ and @b@ have the same sign is
581 * straightforward. When the signs differ, this implementation
582 * chooses @r@ to have the same sign as @b@, rather than the
583 * more normal choice that the remainder has the same sign as
584 * the dividend. This makes modular arithmetic a little more
588 void mp_div(mp **qq, mp **rr, mp *a, mp *b)
590 mp *r = rr ? *rr : MP_NEW;
591 mp *q = qq ? *qq : MP_NEW;
594 /* --- Set the remainder up right --- *
596 * Just in case the divisor is larger, be able to cope with this. It's not
597 * important in @mpx_udiv@, but it is here because of the sign correction.
605 MP_DEST(r, MP_LEN(a) + 2, a->f | b->f);
607 /* --- Fix up the quotient too --- */
610 MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF);
613 /* --- Set up some temporary workspace --- */
616 size_t rq = MP_LEN(b) + 1;
617 sv = mpalloc(r->a, rq);
621 /* --- Perform the calculation --- */
623 mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl);
625 /* --- Sort out the sign of the results --- *
627 * If the signs of the arguments differ, and the remainder is nonzero, I
628 * must add one to the absolute value of the quotient and subtract the
629 * remainder from @b@.
632 q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG);
635 for (v = r->v; v < r->vl; v++) {
637 MPX_UADDN(q->v, q->vl, 1);
638 mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl);
644 r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG);
646 /* --- Store the return values --- */
666 /* --- @mp_odd@ --- *
668 * Arguments: @mp *d@ = pointer to destination integer
669 * @mp *m@ = pointer to source integer
670 * @size_t *s@ = where to store the power of 2
672 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
674 * Use: Computes a power of two and an odd integer which, when
675 * multiplied, give a specified result. This sort of thing is
676 * useful in number theory quite often.
679 mp *mp_odd(mp *d, mp *m, size_t *s)
686 for (; !*v && v < vl; v++)
693 unsigned z = MPW_BITS / 2;
706 return (mp_lsr(d, m, ss));
709 /*----- Test rig ----------------------------------------------------------*/
713 static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
715 if (!MP_EQ(expect, result)) {
716 fprintf(stderr, "\n*** %s failed", op);
717 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
718 fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10);
719 fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10);
720 fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10);
727 #define RIG(name, op) \
728 static int t##name(dstr *v) \
730 mp *a = *(mp **)v[0].buf; \
731 mpw n = *(int *)v[1].buf; \
733 mp *r = *(mp **)v[2].buf; \
734 mp *c = op(MP_NEW, a, n); \
736 mp_build(&b, &n, &n + 1); \
737 ok = verify(#name, r, c, a, &b); \
738 mp_drop(a); mp_drop(c); mp_drop(r); \
739 assert(mparena_count(MPARENA_GLOBAL) == 0); \
750 #define RIG(name, op) \
751 static int t##name(dstr *v) \
753 mp *a = *(mp **)v[0].buf; \
754 mp *b = *(mp **)v[1].buf; \
755 mp *r = *(mp **)v[2].buf; \
756 mp *c = op(MP_NEW, a, b); \
757 int ok = verify(#name, r, c, a, b); \
758 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
759 assert(mparena_count(MPARENA_GLOBAL) == 0); \
769 static int tdiv(dstr *v)
771 mp *a = *(mp **)v[0].buf;
772 mp *b = *(mp **)v[1].buf;
773 mp *q = *(mp **)v[2].buf;
774 mp *r = *(mp **)v[3].buf;
775 mp *c = MP_NEW, *d = MP_NEW;
777 mp_div(&c, &d, a, b);
778 ok &= verify("div(quotient)", q, c, a, b);
779 ok &= verify("div(remainder)", r, d, a, b);
780 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
781 assert(mparena_count(MPARENA_GLOBAL) == 0);
785 static int tbin(dstr *v)
787 static mp *(*fn[])(mp *, mp *, mp *) = {
788 #define DO(string) mp_bit##string##2c,
794 mp *a = *(mp **)v[1].buf;
795 mp *b = *(mp **)v[2].buf;
796 mp *r = *(mp **)v[3].buf;
799 if (strcmp(v[0].buf, "and") == 0) op = 1;
800 else if (strcmp(v[0].buf, "or") == 0) op = 7;
801 else if (strcmp(v[0].buf, "nand") == 0) op = 14;
802 else if (strcmp(v[0].buf, "nor") == 0) op = 8;
803 else if (strcmp(v[0].buf, "xor") == 0) op = 6;
813 c = fn[op](MP_NEW, a, b);
814 ok = verify(v[0].buf, r, c, a, b);
815 mp_drop(a); mp_drop(b); mp_drop(r); mp_drop(c);
816 assert(mparena_count(MPARENA_GLOBAL) == 0);
820 static int tset(dstr *v)
822 mp *a = *(mp **)v[0].buf;
823 unsigned long n = *(unsigned long *)v[1].buf;
824 mp *r = *(mp **)v[2].buf;
828 c = mp_setbit2c(MP_NEW, a, n);
831 fprintf(stderr, "\n***setbit (set) failed");
832 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
833 fprintf(stderr, "\n*** n = %lu", n);
834 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
835 fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16);
838 if (!mp_testbit2c(r, n)) {
840 fprintf(stderr, "\n***setbit (test) failed");
841 fprintf(stderr, "\n*** n = %lu", n);
842 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
848 assert(mparena_count(MPARENA_GLOBAL) == 0);
852 static int tclr(dstr *v)
854 mp *a = *(mp **)v[0].buf;
855 unsigned long n = *(unsigned long *)v[1].buf;
856 mp *r = *(mp **)v[2].buf;
860 c = mp_clearbit2c(MP_NEW, a, n);
863 fprintf(stderr, "\n***clrbit (set) failed");
864 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
865 fprintf(stderr, "\n*** n = %lu", n);
866 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
867 fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16);
870 if (mp_testbit2c(r, n)) {
872 fprintf(stderr, "\n***clrbit (test) failed");
873 fprintf(stderr, "\n*** n = %lu", n);
874 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
880 assert(mparena_count(MPARENA_GLOBAL) == 0);
884 static int tneg(dstr *v)
886 mp *a = *(mp **)v[0].buf;
887 mp *r = *(mp **)v[1].buf;
889 mp *n = mp_neg(MP_NEW, a);
892 fprintf(stderr, "\n*** neg failed\n");
893 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
894 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10);
895 fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10);
902 fprintf(stderr, "\n*** neg failed\n");
903 fputs("\n*** a* = ", stderr); mp_writefile(a, stderr, 10);
904 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10);
905 fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10);
910 assert(mparena_count(MPARENA_GLOBAL) == 0);
914 static int todd(dstr *v)
916 mp *a = *(mp **)v[0].buf;
917 size_t rs = *(uint32 *)v[1].buf;
918 mp *rt = *(mp **)v[2].buf;
922 t = mp_odd(MP_NEW, a, &s);
923 if (s != rs || !MP_EQ(t, rt)) {
925 fprintf(stderr, "\n*** odd failed");
926 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
927 fprintf(stderr, "\n*** s = %lu", (unsigned long)s);
928 fputs("\n*** t = ", stderr); mp_writefile(t, stderr, 10);
929 fprintf(stderr, "\n*** rs = %lu", (unsigned long)rs);
930 fputs("\n*** rt = ", stderr); mp_writefile(rt, stderr, 10);
936 assert(mparena_count(MPARENA_GLOBAL) == 0);
940 static test_chunk tests[] = {
941 { "lsl", tlsl, { &type_mp, &type_int, &type_mp, 0 } },
942 { "lsr", tlsr, { &type_mp, &type_int, &type_mp, 0 } },
943 { "lsl2c", tlsl2c, { &type_mp, &type_int, &type_mp, 0 } },
944 { "lsr2c", tlsr2c, { &type_mp, &type_int, &type_mp, 0 } },
945 { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } },
946 { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } },
947 { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } },
948 { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } },
949 { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } },
950 { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
951 { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } },
952 { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } },
953 { "neg", tneg, { &type_mp, &type_mp, 0 } },
957 int main(int argc, char *argv[])
960 test_run(argc, argv, tests, SRCDIR "/tests/mp");
966 /*----- That's all, folks -------------------------------------------------*/