3 * $Id: mp-arith.c,v 1.18 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
34 /*----- Macros ------------------------------------------------------------*/
36 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- *
42 * Arguments: @mp *d@ = destination
44 * @size_t n@ = number of bits to move
46 * Returns: Result, @a@ shifted left or right by @n@.
48 * Use: Bitwise shift operators. @mp_lslc@ fills the bits introduced
49 * on the right with ones instead of zeroes: it's used
50 * internally by @mp_lsl2c@, though it may be useful on its
54 mp
*mp_lsl(mp
*d
, mp
*a
, size_t n
)
56 MP_DEST(d
, MP_LEN(a
) + (n
+ MPW_BITS
- 1) / MPW_BITS
, a
->f
);
57 mpx_lsl(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
58 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
63 mp
*mp_lslc(mp
*d
, mp
*a
, size_t n
)
65 MP_DEST(d
, MP_LEN(a
) + (n
+ MPW_BITS
- 1) / MPW_BITS
, a
->f
);
66 mpx_lslc(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
67 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
72 mp
*mp_lsr(mp
*d
, mp
*a
, size_t n
)
74 MP_DEST(d
, MP_LEN(a
), a
->f
);
75 mpx_lsr(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
76 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
81 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
83 * Arguments: @mp *d@ = destination
85 * @size_t n@ = number of bits to move
87 * Returns: Result, @a@ shifted left or right by @n@. Handles the
88 * pretence of sign-extension for negative numbers.
91 mp
*mp_lsl2c(mp
*d
, mp
*a
, size_t n
)
94 return (mp_lsl(d
, a
, n
));
101 mp
*mp_lsr2c(mp
*d
, mp
*a
, size_t n
)
103 if (!(a
->f
& MP_NEG
))
104 return (mp_lsr(d
, a
, n
));
111 /* --- @mp_testbit@ --- *
113 * Arguments: @mp *x@ = a large integer
114 * @unsigned long n@ = which bit to test
116 * Returns: Nonzero if the bit is set, zero if not.
119 int mp_testbit(mp
*x
, unsigned long n
)
121 if (n
> MPW_BITS
* MP_LEN(x
))
123 return ((x
->v
[n
/MPW_BITS
] >> n
%MPW_BITS
) & 1u);
126 /* --- @mp_testbit2c@ --- *
128 * Arguments: @mp *x@ = a large integer
129 * @unsigned long n@ = which bit to test
131 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
132 * complement representation.
135 int mp_testbit2c(mp
*x
, unsigned long n
)
138 if (!(x
->f
& MP_NEG
))
139 return (mp_testbit(x
, n
));
140 x
= mp_not2c(MP_NEW
, x
);
141 r
= !mp_testbit(x
, n
);
146 /* --- @mp_setbit@, @mp_clearbit@ --- *
148 * Arguments: @mp *d@ = a destination
149 * @mp *x@ = a large integer
150 * @unsigned long n@ = which bit to modify
152 * Returns: The argument @x@, with the appropriate bit set or cleared.
155 mp
*mp_setbit(mp
*d
, mp
*x
, unsigned long n
)
159 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
164 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
165 d
->v
[n
/MPW_BITS
] |= 1 << n
%MPW_BITS
;
169 mp
*mp_clearbit(mp
*d
, mp
*x
, unsigned long n
)
173 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
178 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
179 d
->v
[n
/MPW_BITS
] &= ~(1 << n
%MPW_BITS
);
183 /* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
185 * Arguments: @mp *d@ = a destination
186 * @mp *x@ = a large integer
187 * @unsigned long n@ = which bit to modify
189 * Returns: The argument @x@, with the appropriate bit set or cleared.
190 * Fakes up two's complement representation.
193 mp
*mp_setbit2c(mp
*d
, mp
*x
, unsigned long n
)
195 if (!(x
->f
& MP_NEG
))
196 return mp_setbit(d
, x
, n
);
198 d
= mp_clearbit(d
, d
, n
);
203 mp
*mp_clearbit2c(mp
*d
, mp
*x
, unsigned long n
)
205 if (!(x
->f
& MP_NEG
))
206 return mp_clearbit(d
, x
, n
);
208 d
= mp_setbit(d
, d
, n
);
215 * Arguments: @const mp *a, *b@ = two numbers
217 * Returns: Nonzero if the numbers are equal.
220 int mp_eq(const mp
*a
, const mp
*b
) { return (MP_EQ(a
, b
)); }
222 /* --- @mp_cmp@ --- *
224 * Arguments: @const mp *a, *b@ = two numbers
226 * Returns: Less than, equal to or greater than zero, according to
227 * whether @a@ is less than, equal to or greater than @b@.
230 int mp_cmp(const mp
*a
, const mp
*b
)
232 if (!((a
->f
^ b
->f
) & MP_NEG
))
233 return (mpx_ucmp(a
->v
, a
->vl
, b
->v
, b
->vl
));
234 else if (a
->f
& MP_NEG
)
240 /* --- @mp_neg@ --- *
242 * Arguments: @mp *d@ = destination
245 * Returns: The negation of the argument.
247 * Use: Negates its argument.
250 mp
*mp_neg(mp
*d
, mp
*a
)
252 /* --- Surprising amounts of messing about required --- */
260 MP_DEST(a
, MP_LEN(a
), a
->f
);
265 /* --- @mp_bitop@ --- *
267 * Arguments: @mp *d@ = destination
268 * @mp *a, *b@ = sources
270 * Returns: The result of the given bitwise operation. These functions
271 * don't handle negative numbers at all sensibly. For that, use
272 * the @...2c@ variants. The functions are named after the
273 * truth tables they generate:
280 #define MP_BITBINOP(string) \
282 mp *mp_bit##string(mp *d, mp *a, mp *b) \
284 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \
285 mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \
286 d->f = (a->f | b->f) & MP_BURN; \
291 MPX_DOBIN(MP_BITBINOP
)
293 /* --- @mp_not@ --- *
295 * Arguments: @mp *d@ = destination
298 * Returns: The bitwise complement of the source.
301 mp
*mp_not(mp
*d
, mp
*a
)
303 MP_DEST(d
, MP_LEN(a
), a
->f
);
304 mpx_not(d
->v
, d
->vl
, a
->v
, a
->vl
);
305 d
->f
= a
->f
& MP_BURN
;
310 /* --- @mp_bitop2c@ --- *
312 * Arguments: @mp *d@ = destination
313 * @mp *a, *b@ = sources
315 * Returns: The result of the given bitwise operation. Negative numbers
316 * are treated as two's complement, sign-extended infinitely to
317 * the left. The functions are named after the truth tables
325 /* --- How this actually works --- *
327 * The two arguments are inverted (with a sign-swap) if they're currently
328 * negative. This means that we end up using a different function (one which
329 * reinverts as we go) for the main operation. Also, if the sign would be
330 * negative at the end, we preinvert the output and then invert again with a
333 * Start with: wxyz WXYZ
334 * If @a@ negative: yzwx or YZWX
335 * If @b@ negative: xwzy XWZY
336 * If both negative: zyxw ZYXW
339 #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \
341 mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \
343 if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \
344 d = mp_bit##base(d, a, b); \
346 } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \
348 d = mp_not2c(d, a); \
349 d = mp_bit##an(d, d, b); \
352 } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \
354 d = mp_not2c(d, b); \
355 d = mp_bit##bn(d, a, d); \
358 } else { /* Both negative */ \
359 mp *t = mp_not2c(MP_NEW, a); \
360 mp *d = mp_not2c(d, b); \
361 d = mp_bit##abn(d, t, d); \
368 #define NEG d = mp_not2c(d, d);
370 MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS
, POS
, POS
, POS
)
371 MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS
, POS
, POS
, NEG
)
372 MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS
, NEG
, POS
, POS
)
373 MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS
, NEG
, POS
, NEG
)
374 MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS
, POS
, NEG
, POS
)
375 MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS
, POS
, NEG
, NEG
)
376 MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS
, NEG
, NEG
, POS
)
377 MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS
, NEG
, NEG
, NEG
)
378 MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG
, POS
, POS
, POS
)
379 MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG
, POS
, POS
, NEG
)
380 MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG
, NEG
, POS
, POS
)
381 MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG
, NEG
, POS
, NEG
)
382 MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG
, POS
, NEG
, POS
)
383 MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG
, POS
, NEG
, NEG
)
384 MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG
, NEG
, NEG
, POS
)
385 MP_BIT2CBINOP(1111, 0000, 0000, 0000, 0000, NEG
, NEG
, NEG
, NEG
)
389 /* --- @mp_not2c@ --- *
391 * Arguments: @mp *d@ = destination
394 * Returns: The sign-extended complement of the argument.
397 mp
*mp_not2c(mp
*d
, mp
*a
)
401 MP_DEST(d
, MP_LEN(a
) + 1, a
->f
);
404 MPX_USUBN(d
->v
, d
->vl
, 1);
406 MPX_UADDN(d
->v
, d
->vl
, 1);
409 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
411 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
413 d
->f
= (a
->f
& (MP_NEG
| MP_BURN
)) ^ MP_NEG
;
418 /* --- @mp_add@ --- *
420 * Arguments: @mp *d@ = destination
421 * @mp *a, *b@ = sources
423 * Returns: Result, @a@ added to @b@.
426 mp
*mp_add(mp
*d
, mp
*a
, mp
*b
)
428 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
429 if (!((a
->f
^ b
->f
) & MP_NEG
))
430 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
432 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
433 mp
*t
= a
; a
= b
; b
= t
;
435 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
437 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | (a
->f
& MP_NEG
);
442 /* --- @mp_sub@ --- *
444 * Arguments: @mp *d@ = destination
445 * @mp *a, *b@ = sources
447 * Returns: Result, @b@ subtracted from @a@.
450 mp
*mp_sub(mp
*d
, mp
*a
, mp
*b
)
453 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
454 if ((a
->f
^ b
->f
) & MP_NEG
)
455 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
457 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
458 mp
*t
= a
; a
= b
; b
= t
;
461 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
463 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ sgn
) & MP_NEG
);
468 /* --- @mp_mul@ --- *
470 * Arguments: @mp *d@ = destination
471 * @mp *a, *b@ = sources
473 * Returns: Result, @a@ multiplied by @b@.
476 mp
*mp_mul(mp
*d
, mp
*a
, mp
*b
)
481 if (MP_LEN(a
) <= MPK_THRESH
|| MP_LEN(b
) <= MPK_THRESH
) {
482 MP_DEST(d
, MP_LEN(a
) + MP_LEN(b
), a
->f
| b
->f
| MP_UNDEF
);
483 mpx_umul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
485 size_t m
= MAX(MP_LEN(a
), MP_LEN(b
));
487 MP_DEST(d
, 3 * m
, a
->f
| b
->f
| MP_UNDEF
);
488 s
= mpalloc(d
->a
, 5 * m
);
489 mpx_kmul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
, s
, s
+ 5 * m
);
493 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ b
->f
) & MP_NEG
);
500 /* --- @mp_sqr@ --- *
502 * Arguments: @mp *d@ = destination
505 * Returns: Result, @a@ squared.
508 mp
*mp_sqr(mp
*d
, mp
*a
)
510 size_t m
= MP_LEN(a
);
513 if (m
> MPK_THRESH
) {
515 MP_DEST(d
, 3 * m
, a
->f
| MP_UNDEF
);
516 s
= mpalloc(d
->a
, 5 * m
);
517 mpx_ksqr(d
->v
, d
->vl
, a
->v
, a
->vl
, s
, s
+ 5 * m
);
520 MP_DEST(d
, 2 * m
+ 2, a
->f
| MP_UNDEF
);
521 mpx_usqr(d
->v
, d
->vl
, a
->v
, a
->vl
);
523 d
->f
= a
->f
& MP_BURN
;
529 /* --- @mp_div@ --- *
531 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
532 * @mp *a, *b@ = sources
534 * Use: Calculates the quotient and remainder when @a@ is divided by
535 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
536 * Either of @qq@ or @rr@ may be null to indicate that the
537 * result is irrelevant. (Discarding both results is silly.)
538 * There is a performance advantage if @a == *rr@.
540 * The behaviour when @a@ and @b@ have the same sign is
541 * straightforward. When the signs differ, this implementation
542 * chooses @r@ to have the same sign as @b@, rather than the
543 * more normal choice that the remainder has the same sign as
544 * the dividend. This makes modular arithmetic a little more
548 void mp_div(mp
**qq
, mp
**rr
, mp
*a
, mp
*b
)
550 mp
*r
= rr ?
*rr
: MP_NEW
;
551 mp
*q
= qq ?
*qq
: MP_NEW
;
554 /* --- Set the remainder up right --- *
556 * Just in case the divisor is larger, be able to cope with this. It's not
557 * important in @mpx_udiv@, but it is here because of the sign correction.
565 MP_DEST(r
, MAX(MP_LEN(a
), MP_LEN(b
)) + 2, a
->f
| b
->f
);
567 /* --- Fix up the quotient too --- */
570 MP_DEST(q
, MP_LEN(r
), r
->f
| MP_UNDEF
);
573 /* --- Set up some temporary workspace --- */
576 size_t rq
= MP_LEN(b
) + 1;
577 sv
= mpalloc(r
->a
, rq
);
581 /* --- Perform the calculation --- */
583 mpx_udiv(q
->v
, q
->vl
, r
->v
, r
->vl
, b
->v
, b
->vl
, sv
, svl
);
585 /* --- Sort out the sign of the results --- *
587 * If the signs of the arguments differ, and the remainder is nonzero, I
588 * must add one to the absolute value of the quotient and subtract the
589 * remainder from @b@.
592 q
->f
= ((r
->f
| b
->f
) & MP_BURN
) | ((r
->f
^ b
->f
) & MP_NEG
);
595 for (v
= r
->v
; v
< r
->vl
; v
++) {
597 MPX_UADDN(q
->v
, q
->vl
, 1);
598 mpx_usub(r
->v
, r
->vl
, b
->v
, b
->vl
, r
->v
, r
->vl
);
604 r
->f
= ((r
->f
| b
->f
) & MP_BURN
) | (b
->f
& MP_NEG
);
606 /* --- Store the return values --- */
626 /* --- @mp_odd@ --- *
628 * Arguments: @mp *d@ = pointer to destination integer
629 * @mp *m@ = pointer to source integer
630 * @size_t *s@ = where to store the power of 2
632 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
634 * Use: Computes a power of two and an odd integer which, when
635 * multiplied, give a specified result. This sort of thing is
636 * useful in number theory quite often.
639 mp
*mp_odd(mp
*d
, mp
*m
, size_t *s
)
646 for (; !*v
&& v
< vl
; v
++)
653 unsigned z
= MPW_BITS
/ 2;
666 return (mp_lsr(d
, m
, ss
));
669 /*----- Test rig ----------------------------------------------------------*/
673 static int verify(const char *op
, mp
*expect
, mp
*result
, mp
*a
, mp
*b
)
675 if (!MP_EQ(expect
, result
)) {
676 fprintf(stderr
, "\n*** %s failed", op
);
677 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
678 fputs("\n*** b = ", stderr
); mp_writefile(b
, stderr
, 10);
679 fputs("\n*** result = ", stderr
); mp_writefile(result
, stderr
, 10);
680 fputs("\n*** expect = ", stderr
); mp_writefile(expect
, stderr
, 10);
687 #define RIG(name, op) \
688 static int t##name(dstr *v) \
690 mp *a = *(mp **)v[0].buf; \
691 mpw n = *(int *)v[1].buf; \
693 mp *r = *(mp **)v[2].buf; \
694 mp *c = op(MP_NEW, a, n); \
696 mp_build(&b, &n, &n + 1); \
697 ok = verify(#name, r, c, a, &b); \
698 mp_drop(a); mp_drop(c); mp_drop(r); \
699 assert(mparena_count(MPARENA_GLOBAL) == 0); \
710 #define RIG(name, op) \
711 static int t##name(dstr *v) \
713 mp *a = *(mp **)v[0].buf; \
714 mp *b = *(mp **)v[1].buf; \
715 mp *r = *(mp **)v[2].buf; \
716 mp *c = op(MP_NEW, a, b); \
717 int ok = verify(#name, r, c, a, b); \
718 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
719 assert(mparena_count(MPARENA_GLOBAL) == 0); \
729 static int tdiv(dstr
*v
)
731 mp
*a
= *(mp
**)v
[0].buf
;
732 mp
*b
= *(mp
**)v
[1].buf
;
733 mp
*q
= *(mp
**)v
[2].buf
;
734 mp
*r
= *(mp
**)v
[3].buf
;
735 mp
*c
= MP_NEW
, *d
= MP_NEW
;
737 mp_div(&c
, &d
, a
, b
);
738 ok
&= verify("div(quotient)", q
, c
, a
, b
);
739 ok
&= verify("div(remainder)", r
, d
, a
, b
);
740 mp_drop(a
); mp_drop(b
); mp_drop(c
); mp_drop(d
); mp_drop(r
); mp_drop(q
);
741 assert(mparena_count(MPARENA_GLOBAL
) == 0);
745 static int tbin(dstr
*v
)
747 static mp
*(*fn
[])(mp
*, mp
*, mp
*) = {
748 #define DO(string) mp_bit##string##2c,
754 mp
*a
= *(mp
**)v
[1].buf
;
755 mp
*b
= *(mp
**)v
[2].buf
;
756 mp
*r
= *(mp
**)v
[3].buf
;
759 if (strcmp(v
[0].buf
, "and") == 0) op
= 1;
760 else if (strcmp(v
[0].buf
, "or") == 0) op
= 7;
761 else if (strcmp(v
[0].buf
, "nand") == 0) op
= 14;
762 else if (strcmp(v
[0].buf
, "nor") == 0) op
= 8;
763 else if (strcmp(v
[0].buf
, "xor") == 0) op
= 6;
773 c
= fn
[op
](MP_NEW
, a
, b
);
774 ok
= verify(v
[0].buf
, r
, c
, a
, b
);
775 mp_drop(a
); mp_drop(b
); mp_drop(r
); mp_drop(c
);
776 assert(mparena_count(MPARENA_GLOBAL
) == 0);
780 static int tset(dstr
*v
)
782 mp
*a
= *(mp
**)v
[0].buf
;
783 unsigned long n
= *(unsigned long *)v
[1].buf
;
784 mp
*r
= *(mp
**)v
[2].buf
;
788 c
= mp_setbit2c(MP_NEW
, a
, n
);
791 fprintf(stderr
, "\n***setbit (set) failed");
792 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 16);
793 fprintf(stderr
, "\n*** n = %lu", n
);
794 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
795 fputs("\n*** c = ", stderr
); mp_writefile(c
, stderr
, 16);
798 if (!mp_testbit2c(r
, n
)) {
800 fprintf(stderr
, "\n***setbit (test) failed");
801 fprintf(stderr
, "\n*** n = %lu", n
);
802 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
808 assert(mparena_count(MPARENA_GLOBAL
) == 0);
812 static int tclr(dstr
*v
)
814 mp
*a
= *(mp
**)v
[0].buf
;
815 unsigned long n
= *(unsigned long *)v
[1].buf
;
816 mp
*r
= *(mp
**)v
[2].buf
;
820 c
= mp_clearbit2c(MP_NEW
, a
, n
);
823 fprintf(stderr
, "\n***clrbit (set) failed");
824 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 16);
825 fprintf(stderr
, "\n*** n = %lu", n
);
826 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
827 fputs("\n*** c = ", stderr
); mp_writefile(c
, stderr
, 16);
830 if (mp_testbit2c(r
, n
)) {
832 fprintf(stderr
, "\n***clrbit (test) failed");
833 fprintf(stderr
, "\n*** n = %lu", n
);
834 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
840 assert(mparena_count(MPARENA_GLOBAL
) == 0);
844 static int tneg(dstr
*v
)
846 mp
*a
= *(mp
**)v
[0].buf
;
847 mp
*r
= *(mp
**)v
[1].buf
;
849 mp
*n
= mp_neg(MP_NEW
, a
);
852 fprintf(stderr
, "\n*** neg failed\n");
853 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
854 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 10);
855 fputs("\n*** n = ", stderr
); mp_writefile(n
, stderr
, 10);
862 fprintf(stderr
, "\n*** neg failed\n");
863 fputs("\n*** a* = ", stderr
); mp_writefile(a
, stderr
, 10);
864 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 10);
865 fputs("\n*** n = ", stderr
); mp_writefile(n
, stderr
, 10);
870 assert(mparena_count(MPARENA_GLOBAL
) == 0);
874 static int todd(dstr
*v
)
876 mp
*a
= *(mp
**)v
[0].buf
;
877 size_t rs
= *(uint32
*)v
[1].buf
;
878 mp
*rt
= *(mp
**)v
[2].buf
;
882 t
= mp_odd(MP_NEW
, a
, &s
);
883 if (s
!= rs
|| !MP_EQ(t
, rt
)) {
885 fprintf(stderr
, "\n*** odd failed");
886 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
887 fprintf(stderr
, "\n*** s = %lu", (unsigned long)s
);
888 fputs("\n*** t = ", stderr
); mp_writefile(t
, stderr
, 10);
889 fprintf(stderr
, "\n*** rs = %lu", (unsigned long)rs
);
890 fputs("\n*** rt = ", stderr
); mp_writefile(rt
, stderr
, 10);
896 assert(mparena_count(MPARENA_GLOBAL
) == 0);
900 static test_chunk tests
[] = {
901 { "lsl", tlsl
, { &type_mp
, &type_int
, &type_mp
, 0 } },
902 { "lsr", tlsr
, { &type_mp
, &type_int
, &type_mp
, 0 } },
903 { "lsl2c", tlsl2c
, { &type_mp
, &type_int
, &type_mp
, 0 } },
904 { "lsr2c", tlsr2c
, { &type_mp
, &type_int
, &type_mp
, 0 } },
905 { "setbit", tset
, { &type_mp
, &type_ulong
, &type_mp
, 0 } },
906 { "clrbit", tclr
, { &type_mp
, &type_ulong
, &type_mp
, 0 } },
907 { "add", tadd
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
908 { "sub", tsub
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
909 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
910 { "div", tdiv
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
911 { "bin2c", tbin
, { &type_string
, &type_mp
, &type_mp
, &type_mp
, 0 } },
912 { "odd", todd
, { &type_mp
, &type_uint32
, &type_mp
, 0 } },
913 { "neg", tneg
, { &type_mp
, &type_mp
, 0 } },
917 int main(int argc
, char *argv
[])
920 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
926 /*----- That's all, folks -------------------------------------------------*/