math/gfreduce.[ch]: Fix out-of-bounds memory access.
[u/mdw/catacomb] / math / mp-arith.c
1 /* -*-c-*-
2 *
3 * Basic arithmetic on multiprecision integers
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "mp.h"
31
32 /*----- Macros ------------------------------------------------------------*/
33
34 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
35
36 /*----- Main code ---------------------------------------------------------*/
37
38 /* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- *
39 *
40 * Arguments: @mp *d@ = destination
41 * @mp *a@ = source
42 * @size_t n@ = number of bits to move
43 *
44 * Returns: Result, @a@ shifted left or right by @n@.
45 *
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
49 * own.
50 */
51
52 mp *mp_lsl(mp *d, mp *a, size_t n)
53 {
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);
57 MP_SHRINK(d);
58 return (d);
59 }
60
61 mp *mp_lslc(mp *d, mp *a, size_t n)
62 {
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);
66 MP_SHRINK(d);
67 return (d);
68 }
69
70 mp *mp_lsr(mp *d, mp *a, size_t n)
71 {
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);
75 MP_SHRINK(d);
76 return (d);
77 }
78
79 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
80 *
81 * Arguments: @mp *d@ = destination
82 * @mp *a@ = source
83 * @size_t n@ = number of bits to move
84 *
85 * Returns: Result, @a@ shifted left or right by @n@. Handles the
86 * pretence of sign-extension for negative numbers.
87 */
88
89 mp *mp_lsl2c(mp *d, mp *a, size_t n)
90 {
91 if (!MP_NEGP(a))
92 return (mp_lsl(d, a, n));
93 d = mp_not2c(d, a);
94 d = mp_lslc(d, d, n);
95 d = mp_not2c(d, d);
96 return (d);
97 }
98
99 mp *mp_lsr2c(mp *d, mp *a, size_t n)
100 {
101 if (!MP_NEGP(a))
102 return (mp_lsr(d, a, n));
103 d = mp_not2c(d, a);
104 d = mp_lsr(d, d, n);
105 d = mp_not2c(d, d);
106 return (d);
107 }
108
109 /* --- @mp_testbit@ --- *
110 *
111 * Arguments: @mp *x@ = a large integer
112 * @unsigned long n@ = which bit to test
113 *
114 * Returns: Nonzero if the bit is set, zero if not.
115 */
116
117 int mp_testbit(mp *x, unsigned long n)
118 {
119 if (n > MPW_BITS * MP_LEN(x))
120 return (0);
121 return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u);
122 }
123
124 /* --- @mp_testbit2c@ --- *
125 *
126 * Arguments: @mp *x@ = a large integer
127 * @unsigned long n@ = which bit to test
128 *
129 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
130 * complement representation.
131 */
132
133 int mp_testbit2c(mp *x, unsigned long n)
134 {
135 int r;
136 if (!MP_NEGP(x))
137 return (mp_testbit(x, n));
138 x = mp_not2c(MP_NEW, x);
139 r = !mp_testbit(x, n);
140 MP_DROP(x);
141 return (r);
142 }
143
144 /* --- @mp_setbit@, @mp_clearbit@ --- *
145 *
146 * Arguments: @mp *d@ = a destination
147 * @mp *x@ = a large integer
148 * @unsigned long n@ = which bit to modify
149 *
150 * Returns: The argument @x@, with the appropriate bit set or cleared.
151 */
152
153 mp *mp_setbit(mp *d, mp *x, unsigned long n)
154 {
155 size_t rq;
156
157 rq = n + MPW_BITS; rq -= rq % MPW_BITS;
158 if (d != x) {
159 if (d) MP_DROP(d);
160 d = MP_COPY(x);
161 }
162 MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN));
163 d->v[n/MPW_BITS] |= 1 << n%MPW_BITS;
164 return (d);
165 }
166
167 mp *mp_clearbit(mp *d, mp *x, unsigned long n)
168 {
169 size_t rq;
170
171 rq = n + MPW_BITS; rq -= rq % MPW_BITS;
172 if (d != x) {
173 if (d) MP_DROP(d);
174 d = MP_COPY(x);
175 }
176 MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN));
177 d->v[n/MPW_BITS] &= ~(1 << n%MPW_BITS);
178 return (d);
179 }
180
181 /* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
182 *
183 * Arguments: @mp *d@ = a destination
184 * @mp *x@ = a large integer
185 * @unsigned long n@ = which bit to modify
186 *
187 * Returns: The argument @x@, with the appropriate bit set or cleared.
188 * Fakes up two's complement representation.
189 */
190
191 mp *mp_setbit2c(mp *d, mp *x, unsigned long n)
192 {
193 if (!MP_NEGP(x))
194 return mp_setbit(d, x, n);
195 d = mp_not2c(d, x);
196 d = mp_clearbit(d, d, n);
197 d = mp_not2c(d, d);
198 return (d);
199 }
200
201 mp *mp_clearbit2c(mp *d, mp *x, unsigned long n)
202 {
203 if (!MP_NEGP(x))
204 return mp_clearbit(d, x, n);
205 d = mp_not2c(d, x);
206 d = mp_setbit(d, d, n);
207 d = mp_not2c(d, d);
208 return (d);
209 }
210
211 /* --- @mp_eq@ --- *
212 *
213 * Arguments: @const mp *a, *b@ = two numbers
214 *
215 * Returns: Nonzero if the numbers are equal.
216 */
217
218 int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); }
219
220 /* --- @mp_cmp@ --- *
221 *
222 * Arguments: @const mp *a, *b@ = two numbers
223 *
224 * Returns: Less than, equal to or greater than zero, according to
225 * whether @a@ is less than, equal to or greater than @b@.
226 */
227
228 int mp_cmp(const mp *a, const mp *b)
229 {
230 if (!((a->f ^ b->f) & MP_NEG)) {
231 if (a->f & MP_NEG)
232 return (-mpx_ucmp(a->v, a->vl, b->v, b->vl));
233 else
234 return (mpx_ucmp(a->v, a->vl, b->v, b->vl));
235 } else if (a->f & MP_NEG)
236 return (-1);
237 else
238 return (+1);
239 }
240
241 /* --- @mp_neg@ --- *
242 *
243 * Arguments: @mp *d@ = destination
244 * @mp *a@ = argument
245 *
246 * Returns: The negation of the argument.
247 *
248 * Use: Negates its argument.
249 */
250
251 mp *mp_neg(mp *d, mp *a)
252 {
253 /* --- Surprising amounts of messing about required --- */
254
255 MP_SHRINK(a);
256 MP_COPY(a);
257 if (d)
258 MP_DROP(d);
259 if (a->v == a->vl)
260 return (a);
261 MP_DEST(a, MP_LEN(a), a->f);
262 a->f ^= MP_NEG;
263 return (a);
264 }
265
266 /* --- @mp_bitop@ --- *
267 *
268 * Arguments: @mp *d@ = destination
269 * @mp *a, *b@ = sources
270 *
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:
275 *
276 * a: 0011
277 * b: 0101
278 * @mpx_bitXXXX@
279 */
280
281 #define MP_BITBINOP(string) \
282 \
283 mp *mp_bit##string(mp *d, mp *a, mp *b) \
284 { \
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; \
288 MP_SHRINK(d); \
289 return (d); \
290 }
291
292 MPX_DOBIN(MP_BITBINOP)
293
294 /* --- @mp_not@ --- *
295 *
296 * Arguments: @mp *d@ = destination
297 * @mp *a@ = source
298 *
299 * Returns: The bitwise complement of the source.
300 */
301
302 mp *mp_not(mp *d, mp *a)
303 {
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;
307 MP_SHRINK(d);
308 return (d);
309 }
310
311 /* --- @mp_bitop2c@ --- *
312 *
313 * Arguments: @mp *d@ = destination
314 * @mp *a, *b@ = sources
315 *
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
319 * they generate:
320 *
321 * a: 0011
322 * b: 0101
323 * @mpx_bitXXXX@
324 */
325
326 /* --- How this actually works --- *
327 *
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
332 * sign-swap.
333 *
334 * Start with: wxyz WXYZ
335 * If @a@ negative: yzwx or YZWX
336 * If @b@ negative: xwzy XWZY
337 * If both negative: zyxw ZYXW
338 */
339
340 #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \
341 \
342 mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \
343 { \
344 if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \
345 d = mp_bit##base(d, a, b); \
346 p_base \
347 } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \
348 MP_COPY(b); \
349 d = mp_not2c(d, a); \
350 d = mp_bit##an(d, d, b); \
351 MP_DROP(b); \
352 p_an \
353 } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \
354 MP_COPY(a); \
355 d = mp_not2c(d, b); \
356 d = mp_bit##bn(d, a, d); \
357 MP_DROP(a); \
358 p_bn \
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); \
363 MP_DROP(t); \
364 p_abn \
365 } \
366 return (d); \
367 } \
368
369 #define NEG d = mp_not2c(d, d);
370 #define POS
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)
387 #undef NEG
388 #undef POS
389
390 /* --- @mp_not2c@ --- *
391 *
392 * Arguments: @mp *d@ = destination
393 * @mp *a@ = source
394 *
395 * Returns: The sign-extended complement of the argument.
396 */
397
398 mp *mp_not2c(mp *d, mp *a)
399 {
400 mpw one = 1;
401
402 MP_DEST(d, MP_LEN(a) + 1, a->f);
403 if (d == a) {
404 if (MP_NEGP(a))
405 MPX_USUBN(d->v, d->vl, 1);
406 else
407 MPX_UADDN(d->v, d->vl, 1);
408 } else {
409 if (MP_NEGP(a))
410 mpx_usub(d->v, d->vl, a->v, a->vl, &one, &one + 1);
411 else
412 mpx_uadd(d->v, d->vl, a->v, a->vl, &one, &one + 1);
413 }
414 d->f = (a->f & (MP_NEG | MP_BURN)) ^ MP_NEG;
415 MP_SHRINK(d);
416 return (d);
417 }
418
419 /* --- @mp_add@ --- *
420 *
421 * Arguments: @mp *d@ = destination
422 * @mp *a, *b@ = sources
423 *
424 * Returns: Result, @a@ added to @b@.
425 */
426
427 mp *mp_add(mp *d, mp *a, mp *b)
428 {
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);
432 else {
433 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
434 mp *t = a; a = b; b = t;
435 }
436 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
437 }
438 d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG);
439 MP_SHRINK(d);
440 return (d);
441 }
442
443 /* --- @mp_sub@ --- *
444 *
445 * Arguments: @mp *d@ = destination
446 * @mp *a, *b@ = sources
447 *
448 * Returns: Result, @b@ subtracted from @a@.
449 */
450
451 mp *mp_sub(mp *d, mp *a, mp *b)
452 {
453 unsigned sgn = 0;
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);
457 else {
458 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
459 mp *t = a; a = b; b = t;
460 sgn = MP_NEG;
461 }
462 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
463 }
464 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG);
465 MP_SHRINK(d);
466 return (d);
467 }
468
469 /* --- @mp_mul@ --- *
470 *
471 * Arguments: @mp *d@ = destination
472 * @mp *a, *b@ = sources
473 *
474 * Returns: Result, @a@ multiplied by @b@.
475 */
476
477 mp *mp_mul(mp *d, mp *a, mp *b)
478 {
479 a = MP_COPY(a);
480 b = MP_COPY(b);
481
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);
485 } else {
486 size_t m = MAX(MP_LEN(a), MP_LEN(b));
487 mpw *s;
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);
491 mpfree(d->a, s);
492 }
493
494 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
495 MP_SHRINK(d);
496 MP_DROP(a);
497 MP_DROP(b);
498 return (d);
499 }
500
501 /* --- @mp_sqr@ --- *
502 *
503 * Arguments: @mp *d@ = destination
504 * @mp *a@ = source
505 *
506 * Returns: Result, @a@ squared.
507 */
508
509 mp *mp_sqr(mp *d, mp *a)
510 {
511 size_t m = MP_LEN(a);
512
513 a = MP_COPY(a);
514 if (m > MPK_THRESH) {
515 mpw *s;
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);
519 mpfree(d->a, s);
520 } else {
521 MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF);
522 mpx_usqr(d->v, d->vl, a->v, a->vl);
523 }
524 d->f = a->f & MP_BURN;
525 MP_SHRINK(d);
526 MP_DROP(a);
527 return (d);
528 }
529
530 /* --- @mp_div@ --- *
531 *
532 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
533 * @mp *a, *b@ = sources
534 *
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@.
540 *
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
546 * straightforward.
547 */
548
549 void mp_div(mp **qq, mp **rr, mp *a, mp *b)
550 {
551 mp *r = rr ? *rr : MP_NEW;
552 mp *q = qq ? *qq : MP_NEW;
553 mpw *sv, *svl;
554
555 /* --- Set the remainder up right --- *
556 *
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.
559 */
560
561 b = MP_COPY(b);
562 a = MP_COPY(a);
563 if (r)
564 MP_DROP(r);
565 r = a;
566 MP_DEST(r, MAX(MP_LEN(a), MP_LEN(b)) + 2, a->f | b->f);
567
568 /* --- Fix up the quotient too --- */
569
570 r = MP_COPY(r);
571 MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF);
572 MP_DROP(r);
573
574 /* --- Set up some temporary workspace --- */
575
576 {
577 size_t rq = MP_LEN(b) + 1;
578 sv = mpalloc(r->a, rq);
579 svl = sv + rq;
580 }
581
582 /* --- Perform the calculation --- */
583
584 mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl);
585
586 /* --- Sort out the sign of the results --- *
587 *
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@.
591 */
592
593 q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG);
594 if (MP_NEGP(q)) {
595 mpw *v;
596 for (v = r->v; v < r->vl; v++) {
597 if (*v) {
598 MPX_UADDN(q->v, q->vl, 1);
599 mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl);
600 break;
601 }
602 }
603 }
604
605 r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG);
606
607 /* --- Store the return values --- */
608
609 mpfree(r->a, sv);
610 MP_DROP(b);
611
612 if (!qq)
613 MP_DROP(q);
614 else {
615 MP_SHRINK(q);
616 *qq = q;
617 }
618
619 if (!rr)
620 MP_DROP(r);
621 else {
622 MP_SHRINK(r);
623 *rr = r;
624 }
625 }
626
627 /* --- @mp_odd@ --- *
628 *
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
632 *
633 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
634 *
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.
638 */
639
640 mp *mp_odd(mp *d, mp *m, size_t *s)
641 {
642 size_t ss = 0;
643 const mpw *v, *vl;
644
645 v = m->v;
646 vl = m->vl;
647 for (; !*v && v < vl; v++)
648 ss += MPW_BITS;
649 if (v >= vl)
650 ss = 0;
651 else {
652 mpw x = *v;
653 unsigned z = MPW_P2;
654 mpw mask = ((mpw)1 << z) - 1;
655
656 while (z) {
657 if (!(x & mask)) {
658 x >>= z;
659 ss += z;
660 }
661 z >>= 1;
662 mask >>= z;
663 }
664 }
665
666 *s = ss;
667 return (mp_lsr(d, m, ss));
668 }
669
670 /*----- Test rig ----------------------------------------------------------*/
671
672 #ifdef TEST_RIG
673
674 static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
675 {
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);
682 fputc('\n', stderr);
683 return (0);
684 }
685 return (1);
686 }
687
688 #define RIG(name, op) \
689 static int t##name(dstr *v) \
690 { \
691 mp *a = *(mp **)v[0].buf; \
692 mpw n = *(int *)v[1].buf; \
693 mp b; \
694 mp *r = *(mp **)v[2].buf; \
695 mp *c = op(MP_NEW, a, n); \
696 int ok; \
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); \
701 return (ok); \
702 }
703
704 RIG(lsl, mp_lsl)
705 RIG(lsr, mp_lsr)
706 RIG(lsl2c, mp_lsl2c)
707 RIG(lsr2c, mp_lsr2c)
708
709 #undef RIG
710
711 #define RIG(name, op) \
712 static int t##name(dstr *v) \
713 { \
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); \
721 return (ok); \
722 }
723
724 RIG(add, mp_add)
725 RIG(sub, mp_sub)
726 RIG(mul, mp_mul)
727 RIG(exp, mp_exp)
728
729 #undef RIG
730
731 static int tdiv(dstr *v)
732 {
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;
738 int ok = 1;
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);
744 return (ok);
745 }
746
747 static int tbin(dstr *v)
748 {
749 static mp *(*fn[])(mp *, mp *, mp *) = {
750 #define DO(string) mp_bit##string##2c,
751 MPX_DOBIN(DO)
752 #undef DO
753 };
754 int ok = 1;
755 unsigned op = 0;
756 mp *a = *(mp **)v[1].buf;
757 mp *b = *(mp **)v[2].buf;
758 mp *r = *(mp **)v[3].buf;
759 mp *c;
760
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;
766 else {
767 char *p = v[0].buf;
768 while (*p) {
769 op <<= 1;
770 if (*p++ == '1')
771 op |= 1;
772 }
773 }
774
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);
779 return (ok);
780 }
781
782 static int tset(dstr *v)
783 {
784 mp *a = *(mp **)v[0].buf;
785 unsigned long n = *(unsigned long *)v[1].buf;
786 mp *r = *(mp **)v[2].buf;
787 mp *c;
788 int ok = 1;
789
790 c = mp_setbit2c(MP_NEW, a, n);
791 if (!MP_EQ(c, r)) {
792 ok = 0;
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);
798 fputc('\n', stderr);
799 }
800 if (!mp_testbit2c(r, n)) {
801 ok = 0;
802 fprintf(stderr, "\n***setbit (test) failed");
803 fprintf(stderr, "\n*** n = %lu", n);
804 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
805 fputc('\n', stderr);
806 }
807 mp_drop(a);
808 mp_drop(r);
809 mp_drop(c);
810 assert(mparena_count(MPARENA_GLOBAL) == 0);
811 return (ok);
812 }
813
814 static int tclr(dstr *v)
815 {
816 mp *a = *(mp **)v[0].buf;
817 unsigned long n = *(unsigned long *)v[1].buf;
818 mp *r = *(mp **)v[2].buf;
819 mp *c;
820 int ok = 1;
821
822 c = mp_clearbit2c(MP_NEW, a, n);
823 if (!MP_EQ(c, r)) {
824 ok = 0;
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);
830 fputc('\n', stderr);
831 }
832 if (mp_testbit2c(r, n)) {
833 ok = 0;
834 fprintf(stderr, "\n***clrbit (test) failed");
835 fprintf(stderr, "\n*** n = %lu", n);
836 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
837 fputc('\n', stderr);
838 }
839 mp_drop(a);
840 mp_drop(c);
841 mp_drop(r);
842 assert(mparena_count(MPARENA_GLOBAL) == 0);
843 return (ok);
844 }
845
846 static int tneg(dstr *v)
847 {
848 mp *a = *(mp **)v[0].buf;
849 mp *r = *(mp **)v[1].buf;
850 int ok = 1;
851 mp *n = mp_neg(MP_NEW, a);
852 if (!MP_EQ(r, n)) {
853 ok = 0;
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);
858 fputc('\n', stderr);
859 }
860 mp_drop(n);
861 n = mp_neg(a, a);
862 if (!MP_EQ(r, n)) {
863 ok = 0;
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);
868 fputc('\n', stderr);
869 }
870 mp_drop(a);
871 mp_drop(r);
872 assert(mparena_count(MPARENA_GLOBAL) == 0);
873 return (ok);
874 }
875
876 static int todd(dstr *v)
877 {
878 mp *a = *(mp **)v[0].buf;
879 size_t rs = *(uint32 *)v[1].buf;
880 mp *rt = *(mp **)v[2].buf;
881 int ok = 1;
882 mp *t;
883 size_t s;
884 t = mp_odd(MP_NEW, a, &s);
885 if (s != rs || !MP_EQ(t, rt)) {
886 ok = 0;
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);
893 fputc('\n', stderr);
894 }
895 mp_drop(a);
896 mp_drop(rt);
897 mp_drop(t);
898 assert(mparena_count(MPARENA_GLOBAL) == 0);
899 return (ok);
900 }
901
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 } },
917 { 0, 0, { 0 } },
918 };
919
920 int main(int argc, char *argv[])
921 {
922 sub_init();
923 test_run(argc, argv, tests, SRCDIR "/t/mp");
924 return (0);
925 }
926
927 #endif
928
929 /*----- That's all, folks -------------------------------------------------*/