3 * Basic arithmetic on multiprecision integers
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 ------------------------------------------------------*/
32 /*----- Macros ------------------------------------------------------------*/
34 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- *
40 * Arguments: @mp *d@ = destination
42 * @size_t n@ = number of bits to move
44 * Returns: Result, @a@ shifted left or right by @n@.
46 * Use: Bitwise shift operators. @mp_lslc@ fills the bits introduced
47 * on the right with ones instead of zeroes: it's used
48 * internally by @mp_lsl2c@, though it may be useful on its
52 mp
*mp_lsl(mp
*d
, mp
*a
, size_t n
)
54 MP_DEST(d
, MP_LEN(a
) + (n
+ MPW_BITS
- 1) / MPW_BITS
, a
->f
);
55 mpx_lsl(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
56 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
61 mp
*mp_lslc(mp
*d
, mp
*a
, size_t n
)
63 MP_DEST(d
, MP_LEN(a
) + (n
+ MPW_BITS
- 1) / MPW_BITS
, a
->f
);
64 mpx_lslc(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
65 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
70 mp
*mp_lsr(mp
*d
, mp
*a
, size_t n
)
72 MP_DEST(d
, MP_LEN(a
), a
->f
);
73 mpx_lsr(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
74 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
79 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
81 * Arguments: @mp *d@ = destination
83 * @size_t n@ = number of bits to move
85 * Returns: Result, @a@ shifted left or right by @n@. Handles the
86 * pretence of sign-extension for negative numbers.
89 mp
*mp_lsl2c(mp
*d
, mp
*a
, size_t n
)
92 return (mp_lsl(d
, a
, n
));
99 mp
*mp_lsr2c(mp
*d
, mp
*a
, size_t n
)
102 return (mp_lsr(d
, a
, n
));
109 /* --- @mp_testbit@ --- *
111 * Arguments: @mp *x@ = a large integer
112 * @unsigned long n@ = which bit to test
114 * Returns: Nonzero if the bit is set, zero if not.
117 int mp_testbit(mp
*x
, unsigned long n
)
119 if (n
> MPW_BITS
* MP_LEN(x
))
121 return ((x
->v
[n
/MPW_BITS
] >> n
%MPW_BITS
) & 1u);
124 /* --- @mp_testbit2c@ --- *
126 * Arguments: @mp *x@ = a large integer
127 * @unsigned long n@ = which bit to test
129 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
130 * complement representation.
133 int mp_testbit2c(mp
*x
, unsigned long n
)
137 return (mp_testbit(x
, n
));
138 x
= mp_not2c(MP_NEW
, x
);
139 r
= !mp_testbit(x
, n
);
144 /* --- @mp_setbit@, @mp_clearbit@ --- *
146 * Arguments: @mp *d@ = a destination
147 * @mp *x@ = a large integer
148 * @unsigned long n@ = which bit to modify
150 * Returns: The argument @x@, with the appropriate bit set or cleared.
153 mp
*mp_setbit(mp
*d
, mp
*x
, unsigned long n
)
157 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
162 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
163 d
->v
[n
/MPW_BITS
] |= 1 << n
%MPW_BITS
;
167 mp
*mp_clearbit(mp
*d
, mp
*x
, unsigned long n
)
171 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
176 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
177 d
->v
[n
/MPW_BITS
] &= ~(1 << n
%MPW_BITS
);
181 /* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
183 * Arguments: @mp *d@ = a destination
184 * @mp *x@ = a large integer
185 * @unsigned long n@ = which bit to modify
187 * Returns: The argument @x@, with the appropriate bit set or cleared.
188 * Fakes up two's complement representation.
191 mp
*mp_setbit2c(mp
*d
, mp
*x
, unsigned long n
)
194 return mp_setbit(d
, x
, n
);
196 d
= mp_clearbit(d
, d
, n
);
201 mp
*mp_clearbit2c(mp
*d
, mp
*x
, unsigned long n
)
204 return mp_clearbit(d
, x
, n
);
206 d
= mp_setbit(d
, d
, n
);
213 * Arguments: @const mp *a, *b@ = two numbers
215 * Returns: Nonzero if the numbers are equal.
218 int mp_eq(const mp
*a
, const mp
*b
) { return (MP_EQ(a
, b
)); }
220 /* --- @mp_cmp@ --- *
222 * Arguments: @const mp *a, *b@ = two numbers
224 * Returns: Less than, equal to or greater than zero, according to
225 * whether @a@ is less than, equal to or greater than @b@.
228 int mp_cmp(const mp
*a
, const mp
*b
)
230 if (!((a
->f
^ b
->f
) & MP_NEG
)) {
232 return (-mpx_ucmp(a
->v
, a
->vl
, b
->v
, b
->vl
));
234 return (mpx_ucmp(a
->v
, a
->vl
, b
->v
, b
->vl
));
235 } else if (a
->f
& MP_NEG
)
241 /* --- @mp_neg@ --- *
243 * Arguments: @mp *d@ = destination
246 * Returns: The negation of the argument.
248 * Use: Negates its argument.
251 mp
*mp_neg(mp
*d
, mp
*a
)
253 /* --- Surprising amounts of messing about required --- */
261 MP_DEST(a
, MP_LEN(a
), a
->f
);
266 /* --- @mp_bitop@ --- *
268 * Arguments: @mp *d@ = destination
269 * @mp *a, *b@ = sources
271 * Returns: The result of the given bitwise operation. These functions
272 * don't handle negative numbers at all sensibly. For that, use
273 * the @...2c@ variants. The functions are named after the
274 * truth tables they generate:
281 #define MP_BITBINOP(string) \
283 mp *mp_bit##string(mp *d, mp *a, mp *b) \
285 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \
286 mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \
287 d->f = (a->f | b->f) & MP_BURN; \
292 MPX_DOBIN(MP_BITBINOP
)
294 /* --- @mp_not@ --- *
296 * Arguments: @mp *d@ = destination
299 * Returns: The bitwise complement of the source.
302 mp
*mp_not(mp
*d
, mp
*a
)
304 MP_DEST(d
, MP_LEN(a
), a
->f
);
305 mpx_not(d
->v
, d
->vl
, a
->v
, a
->vl
);
306 d
->f
= a
->f
& MP_BURN
;
311 /* --- @mp_bitop2c@ --- *
313 * Arguments: @mp *d@ = destination
314 * @mp *a, *b@ = sources
316 * Returns: The result of the given bitwise operation. Negative numbers
317 * are treated as two's complement, sign-extended infinitely to
318 * the left. The functions are named after the truth tables
326 /* --- How this actually works --- *
328 * The two arguments are inverted (with a sign-swap) if they're currently
329 * negative. This means that we end up using a different function (one which
330 * reinverts as we go) for the main operation. Also, if the sign would be
331 * negative at the end, we preinvert the output and then invert again with a
334 * Start with: wxyz WXYZ
335 * If @a@ negative: yzwx or YZWX
336 * If @b@ negative: xwzy XWZY
337 * If both negative: zyxw ZYXW
340 #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \
342 mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \
344 if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \
345 d = mp_bit##base(d, a, b); \
347 } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \
349 d = mp_not2c(d, a); \
350 d = mp_bit##an(d, d, b); \
353 } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \
355 d = mp_not2c(d, b); \
356 d = mp_bit##bn(d, a, d); \
359 } else { /* Both negative */ \
360 mp *t = mp_not2c(MP_NEW, a); \
361 d = mp_not2c(d, b); \
362 d = mp_bit##abn(d, t, d); \
369 #define NEG d = mp_not2c(d, d);
371 MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS
, POS
, POS
, POS
)
372 MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS
, POS
, POS
, NEG
)
373 MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS
, NEG
, POS
, POS
)
374 MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS
, NEG
, POS
, NEG
)
375 MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS
, POS
, NEG
, POS
)
376 MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS
, POS
, NEG
, NEG
)
377 MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS
, NEG
, NEG
, POS
)
378 MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS
, NEG
, NEG
, NEG
)
379 MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG
, POS
, POS
, POS
)
380 MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG
, POS
, POS
, NEG
)
381 MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG
, NEG
, POS
, POS
)
382 MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG
, NEG
, POS
, NEG
)
383 MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG
, POS
, NEG
, POS
)
384 MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG
, POS
, NEG
, NEG
)
385 MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG
, NEG
, NEG
, POS
)
386 MP_BIT2CBINOP(1111, 0000, 0000, 0000, 0000, NEG
, NEG
, NEG
, NEG
)
390 /* --- @mp_not2c@ --- *
392 * Arguments: @mp *d@ = destination
395 * Returns: The sign-extended complement of the argument.
398 mp
*mp_not2c(mp
*d
, mp
*a
)
402 MP_DEST(d
, MP_LEN(a
) + 1, a
->f
);
405 MPX_USUBN(d
->v
, d
->vl
, 1);
407 MPX_UADDN(d
->v
, d
->vl
, 1);
410 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
412 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
414 d
->f
= (a
->f
& (MP_NEG
| MP_BURN
)) ^ MP_NEG
;
419 /* --- @mp_add@ --- *
421 * Arguments: @mp *d@ = destination
422 * @mp *a, *b@ = sources
424 * Returns: Result, @a@ added to @b@.
427 mp
*mp_add(mp
*d
, mp
*a
, mp
*b
)
429 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
430 if (!((a
->f
^ b
->f
) & MP_NEG
))
431 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
433 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
434 mp
*t
= a
; a
= b
; b
= t
;
436 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
438 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | (a
->f
& MP_NEG
);
443 /* --- @mp_sub@ --- *
445 * Arguments: @mp *d@ = destination
446 * @mp *a, *b@ = sources
448 * Returns: Result, @b@ subtracted from @a@.
451 mp
*mp_sub(mp
*d
, mp
*a
, mp
*b
)
454 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
455 if ((a
->f
^ b
->f
) & MP_NEG
)
456 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
458 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
459 mp
*t
= a
; a
= b
; b
= t
;
462 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
464 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ sgn
) & MP_NEG
);
469 /* --- @mp_mul@ --- *
471 * Arguments: @mp *d@ = destination
472 * @mp *a, *b@ = sources
474 * Returns: Result, @a@ multiplied by @b@.
477 mp
*mp_mul(mp
*d
, mp
*a
, mp
*b
)
482 if (MP_LEN(a
) <= MPK_THRESH
|| MP_LEN(b
) <= MPK_THRESH
) {
483 MP_DEST(d
, MP_LEN(a
) + MP_LEN(b
), a
->f
| b
->f
| MP_UNDEF
);
484 mpx_umul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
486 size_t m
= MAX(MP_LEN(a
), MP_LEN(b
));
488 MP_DEST(d
, 3 * m
, a
->f
| b
->f
| MP_UNDEF
);
489 s
= mpalloc(d
->a
, 5 * m
);
490 mpx_kmul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
, s
, s
+ 5 * m
);
494 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ b
->f
) & MP_NEG
);
501 /* --- @mp_sqr@ --- *
503 * Arguments: @mp *d@ = destination
506 * Returns: Result, @a@ squared.
509 mp
*mp_sqr(mp
*d
, mp
*a
)
511 size_t m
= MP_LEN(a
);
514 if (m
> MPK_THRESH
) {
516 MP_DEST(d
, 3 * m
, a
->f
| MP_UNDEF
);
517 s
= mpalloc(d
->a
, 5 * m
);
518 mpx_ksqr(d
->v
, d
->vl
, a
->v
, a
->vl
, s
, s
+ 5 * m
);
521 MP_DEST(d
, 2 * m
+ 2, a
->f
| MP_UNDEF
);
522 mpx_usqr(d
->v
, d
->vl
, a
->v
, a
->vl
);
524 d
->f
= a
->f
& MP_BURN
;
530 /* --- @mp_div@ --- *
532 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
533 * @mp *a, *b@ = sources
535 * Use: Calculates the quotient and remainder when @a@ is divided by
536 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
537 * Either of @qq@ or @rr@ may be null to indicate that the
538 * result is irrelevant. (Discarding both results is silly.)
539 * There is a performance advantage if @a == *rr@.
541 * The behaviour when @a@ and @b@ have the same sign is
542 * straightforward. When the signs differ, this implementation
543 * chooses @r@ to have the same sign as @b@, rather than the
544 * more normal choice that the remainder has the same sign as
545 * the dividend. This makes modular arithmetic a little more
549 void mp_div(mp
**qq
, mp
**rr
, mp
*a
, mp
*b
)
551 mp
*r
= rr ?
*rr
: MP_NEW
;
552 mp
*q
= qq ?
*qq
: MP_NEW
;
555 /* --- Set the remainder up right --- *
557 * Just in case the divisor is larger, be able to cope with this. It's not
558 * important in @mpx_udiv@, but it is here because of the sign correction.
566 MP_DEST(r
, MAX(MP_LEN(a
), MP_LEN(b
)) + 2, a
->f
| b
->f
);
568 /* --- Fix up the quotient too --- */
571 MP_DEST(q
, MP_LEN(r
), r
->f
| MP_UNDEF
);
574 /* --- Set up some temporary workspace --- */
577 size_t rq
= MP_LEN(b
) + 1;
578 sv
= mpalloc(r
->a
, rq
);
582 /* --- Perform the calculation --- */
584 mpx_udiv(q
->v
, q
->vl
, r
->v
, r
->vl
, b
->v
, b
->vl
, sv
, svl
);
586 /* --- Sort out the sign of the results --- *
588 * If the signs of the arguments differ, and the remainder is nonzero, I
589 * must add one to the absolute value of the quotient and subtract the
590 * remainder from @b@.
593 q
->f
= ((r
->f
| b
->f
) & MP_BURN
) | ((r
->f
^ b
->f
) & MP_NEG
);
596 for (v
= r
->v
; v
< r
->vl
; v
++) {
598 MPX_UADDN(q
->v
, q
->vl
, 1);
599 mpx_usub(r
->v
, r
->vl
, b
->v
, b
->vl
, r
->v
, r
->vl
);
605 r
->f
= ((r
->f
| b
->f
) & MP_BURN
) | (b
->f
& MP_NEG
);
607 /* --- Store the return values --- */
627 /* --- @mp_odd@ --- *
629 * Arguments: @mp *d@ = pointer to destination integer
630 * @mp *m@ = pointer to source integer
631 * @size_t *s@ = where to store the power of 2
633 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
635 * Use: Computes a power of two and an odd integer which, when
636 * multiplied, give a specified result. This sort of thing is
637 * useful in number theory quite often.
640 mp
*mp_odd(mp
*d
, mp
*m
, size_t *s
)
647 for (; !*v
&& v
< vl
; v
++)
654 mpw mask
= ((mpw
)1 << z
) - 1;
667 return (mp_lsr(d
, m
, ss
));
670 /*----- Test rig ----------------------------------------------------------*/
674 static int verify(const char *op
, mp
*expect
, mp
*result
, mp
*a
, mp
*b
)
676 if (!MP_EQ(expect
, result
)) {
677 fprintf(stderr
, "\n*** %s failed", op
);
678 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
679 fputs("\n*** b = ", stderr
); mp_writefile(b
, stderr
, 10);
680 fputs("\n*** result = ", stderr
); mp_writefile(result
, stderr
, 10);
681 fputs("\n*** expect = ", stderr
); mp_writefile(expect
, stderr
, 10);
688 #define RIG(name, op) \
689 static int t##name(dstr *v) \
691 mp *a = *(mp **)v[0].buf; \
692 mpw n = *(int *)v[1].buf; \
694 mp *r = *(mp **)v[2].buf; \
695 mp *c = op(MP_NEW, a, n); \
697 mp_build(&b, &n, &n + 1); \
698 ok = verify(#name, r, c, a, &b); \
699 mp_drop(a); mp_drop(c); mp_drop(r); \
700 assert(mparena_count(MPARENA_GLOBAL) == 0); \
711 #define RIG(name, op) \
712 static int t##name(dstr *v) \
714 mp *a = *(mp **)v[0].buf; \
715 mp *b = *(mp **)v[1].buf; \
716 mp *r = *(mp **)v[2].buf; \
717 mp *c = op(MP_NEW, a, b); \
718 int ok = verify(#name, r, c, a, b); \
719 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
720 assert(mparena_count(MPARENA_GLOBAL) == 0); \
731 static int tdiv(dstr
*v
)
733 mp
*a
= *(mp
**)v
[0].buf
;
734 mp
*b
= *(mp
**)v
[1].buf
;
735 mp
*q
= *(mp
**)v
[2].buf
;
736 mp
*r
= *(mp
**)v
[3].buf
;
737 mp
*c
= MP_NEW
, *d
= MP_NEW
;
739 mp_div(&c
, &d
, a
, b
);
740 ok
&= verify("div(quotient)", q
, c
, a
, b
);
741 ok
&= verify("div(remainder)", r
, d
, a
, b
);
742 mp_drop(a
); mp_drop(b
); mp_drop(c
); mp_drop(d
); mp_drop(r
); mp_drop(q
);
743 assert(mparena_count(MPARENA_GLOBAL
) == 0);
747 static int tbin(dstr
*v
)
749 static mp
*(*fn
[])(mp
*, mp
*, mp
*) = {
750 #define DO(string) mp_bit##string##2c,
756 mp
*a
= *(mp
**)v
[1].buf
;
757 mp
*b
= *(mp
**)v
[2].buf
;
758 mp
*r
= *(mp
**)v
[3].buf
;
761 if (strcmp(v
[0].buf
, "and") == 0) op
= 1;
762 else if (strcmp(v
[0].buf
, "or") == 0) op
= 7;
763 else if (strcmp(v
[0].buf
, "nand") == 0) op
= 14;
764 else if (strcmp(v
[0].buf
, "nor") == 0) op
= 8;
765 else if (strcmp(v
[0].buf
, "xor") == 0) op
= 6;
775 c
= fn
[op
](MP_NEW
, a
, b
);
776 ok
= verify(v
[0].buf
, r
, c
, a
, b
);
777 mp_drop(a
); mp_drop(b
); mp_drop(r
); mp_drop(c
);
778 assert(mparena_count(MPARENA_GLOBAL
) == 0);
782 static int tset(dstr
*v
)
784 mp
*a
= *(mp
**)v
[0].buf
;
785 unsigned long n
= *(unsigned long *)v
[1].buf
;
786 mp
*r
= *(mp
**)v
[2].buf
;
790 c
= mp_setbit2c(MP_NEW
, a
, n
);
793 fprintf(stderr
, "\n***setbit (set) failed");
794 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 16);
795 fprintf(stderr
, "\n*** n = %lu", n
);
796 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
797 fputs("\n*** c = ", stderr
); mp_writefile(c
, stderr
, 16);
800 if (!mp_testbit2c(r
, n
)) {
802 fprintf(stderr
, "\n***setbit (test) failed");
803 fprintf(stderr
, "\n*** n = %lu", n
);
804 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
810 assert(mparena_count(MPARENA_GLOBAL
) == 0);
814 static int tclr(dstr
*v
)
816 mp
*a
= *(mp
**)v
[0].buf
;
817 unsigned long n
= *(unsigned long *)v
[1].buf
;
818 mp
*r
= *(mp
**)v
[2].buf
;
822 c
= mp_clearbit2c(MP_NEW
, a
, n
);
825 fprintf(stderr
, "\n***clrbit (set) failed");
826 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 16);
827 fprintf(stderr
, "\n*** n = %lu", n
);
828 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
829 fputs("\n*** c = ", stderr
); mp_writefile(c
, stderr
, 16);
832 if (mp_testbit2c(r
, n
)) {
834 fprintf(stderr
, "\n***clrbit (test) failed");
835 fprintf(stderr
, "\n*** n = %lu", n
);
836 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
842 assert(mparena_count(MPARENA_GLOBAL
) == 0);
846 static int tneg(dstr
*v
)
848 mp
*a
= *(mp
**)v
[0].buf
;
849 mp
*r
= *(mp
**)v
[1].buf
;
851 mp
*n
= mp_neg(MP_NEW
, a
);
854 fprintf(stderr
, "\n*** neg failed\n");
855 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
856 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 10);
857 fputs("\n*** n = ", stderr
); mp_writefile(n
, stderr
, 10);
864 fprintf(stderr
, "\n*** neg failed\n");
865 fputs("\n*** a* = ", stderr
); mp_writefile(a
, stderr
, 10);
866 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 10);
867 fputs("\n*** n = ", stderr
); mp_writefile(n
, stderr
, 10);
872 assert(mparena_count(MPARENA_GLOBAL
) == 0);
876 static int todd(dstr
*v
)
878 mp
*a
= *(mp
**)v
[0].buf
;
879 size_t rs
= *(uint32
*)v
[1].buf
;
880 mp
*rt
= *(mp
**)v
[2].buf
;
884 t
= mp_odd(MP_NEW
, a
, &s
);
885 if (s
!= rs
|| !MP_EQ(t
, rt
)) {
887 fprintf(stderr
, "\n*** odd failed");
888 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
889 fprintf(stderr
, "\n*** s = %lu", (unsigned long)s
);
890 fputs("\n*** t = ", stderr
); mp_writefile(t
, stderr
, 10);
891 fprintf(stderr
, "\n*** rs = %lu", (unsigned long)rs
);
892 fputs("\n*** rt = ", stderr
); mp_writefile(rt
, stderr
, 10);
898 assert(mparena_count(MPARENA_GLOBAL
) == 0);
902 static test_chunk tests
[] = {
903 { "lsl", tlsl
, { &type_mp
, &type_int
, &type_mp
, 0 } },
904 { "lsr", tlsr
, { &type_mp
, &type_int
, &type_mp
, 0 } },
905 { "lsl2c", tlsl2c
, { &type_mp
, &type_int
, &type_mp
, 0 } },
906 { "lsr2c", tlsr2c
, { &type_mp
, &type_int
, &type_mp
, 0 } },
907 { "setbit", tset
, { &type_mp
, &type_ulong
, &type_mp
, 0 } },
908 { "clrbit", tclr
, { &type_mp
, &type_ulong
, &type_mp
, 0 } },
909 { "add", tadd
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
910 { "sub", tsub
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
911 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
912 { "div", tdiv
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
913 { "exp", texp
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
914 { "bin2c", tbin
, { &type_string
, &type_mp
, &type_mp
, &type_mp
, 0 } },
915 { "odd", todd
, { &type_mp
, &type_uint32
, &type_mp
, 0 } },
916 { "neg", tneg
, { &type_mp
, &type_mp
, 0 } },
920 int main(int argc
, char *argv
[])
923 test_run(argc
, argv
, tests
, SRCDIR
"/t/mp");
929 /*----- That's all, folks -------------------------------------------------*/