progs/perftest.c: Use from Glibc syscall numbers.
[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 /* --- @mp_leastcongruent@ --- *
671 *
672 * Arguments: @mp *d@ = pointer to destination
673 * @mp *b@ = lower bound
674 * @mp *r@ = representative
675 * @mp *m@ = modulus
676 *
677 * Returns: The smallest integer %$x \equiv r \pmod{m}$% such that
678 * %$x \ge b$%.
679 */
680
681 mp *mp_leastcongruent(mp *d, mp *b, mp *r, mp *m)
682 {
683 /* --- Strategy --- *
684 *
685 * Start by finding %$u \equiv b - r \pmod{m}$% with %$0 < u \le m$%. Then
686 * %$b \le x = b + m - u < b + m$%, and %$x \equiv r \pmod{m}$% as
687 * required.
688 */
689
690 MP_COPY(b); MP_COPY(m);
691 d = mp_sub(d, b, r);
692 mp_div(0, &d, d, m);
693 if (MP_ZEROP(d)) { MP_DROP(d); d = MP_COPY(b); }
694 else { d = mp_sub(d, b, d); d = mp_add(d, d, m); }
695 MP_DROP(b); MP_DROP(m);
696 return (d);
697 }
698
699 /*----- Test rig ----------------------------------------------------------*/
700
701 #ifdef TEST_RIG
702
703 #include <mLib/macros.h>
704
705 static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
706 {
707 if (!MP_EQ(expect, result)) {
708 fprintf(stderr, "\n*** %s failed", op);
709 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
710 fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10);
711 fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10);
712 fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10);
713 fputc('\n', stderr);
714 return (0);
715 }
716 return (1);
717 }
718
719 #define RIG(name, op) \
720 static int t##name(dstr *v) \
721 { \
722 mp *a = *(mp **)v[0].buf; \
723 mpw n = *(int *)v[1].buf; \
724 mp b; \
725 mp *r = *(mp **)v[2].buf; \
726 mp *c = op(MP_NEW, a, n); \
727 int ok; \
728 mp_build(&b, &n, &n + 1); \
729 ok = verify(#name, r, c, a, &b); \
730 mp_drop(a); mp_drop(c); mp_drop(r); \
731 assert(mparena_count(MPARENA_GLOBAL) == 0); \
732 return (ok); \
733 }
734
735 RIG(lsl, mp_lsl)
736 RIG(lsr, mp_lsr)
737 RIG(lsl2c, mp_lsl2c)
738 RIG(lsr2c, mp_lsr2c)
739
740 #undef RIG
741
742 #define RIG(name, op) \
743 static int t##name(dstr *v) \
744 { \
745 mp *a = *(mp **)v[0].buf; \
746 mp *b = *(mp **)v[1].buf; \
747 mp *r = *(mp **)v[2].buf; \
748 mp *c = op(MP_NEW, a, b); \
749 int ok = verify(#name, r, c, a, b); \
750 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
751 assert(mparena_count(MPARENA_GLOBAL) == 0); \
752 return (ok); \
753 }
754
755 RIG(add, mp_add)
756 RIG(sub, mp_sub)
757 RIG(mul, mp_mul)
758 RIG(exp, mp_exp)
759
760 #undef RIG
761
762 static int tdiv(dstr *v)
763 {
764 mp *a = *(mp **)v[0].buf;
765 mp *b = *(mp **)v[1].buf;
766 mp *q = *(mp **)v[2].buf;
767 mp *r = *(mp **)v[3].buf;
768 mp *c = MP_NEW, *d = MP_NEW;
769 int ok = 1;
770 mp_div(&c, &d, a, b);
771 ok &= verify("div(quotient)", q, c, a, b);
772 ok &= verify("div(remainder)", r, d, a, b);
773 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
774 assert(mparena_count(MPARENA_GLOBAL) == 0);
775 return (ok);
776 }
777
778 static int tbin(dstr *v)
779 {
780 static mp *(*fn[])(mp *, mp *, mp *) = {
781 #define DO(string) mp_bit##string##2c,
782 MPX_DOBIN(DO)
783 #undef DO
784 };
785 int ok = 1;
786 unsigned op = 0;
787 mp *a = *(mp **)v[1].buf;
788 mp *b = *(mp **)v[2].buf;
789 mp *r = *(mp **)v[3].buf;
790 mp *c;
791
792 if (STRCMP(v[0].buf, ==, "and")) op = 1;
793 else if (STRCMP(v[0].buf, ==, "or")) op = 7;
794 else if (STRCMP(v[0].buf, ==, "nand")) op = 14;
795 else if (STRCMP(v[0].buf, ==, "nor")) op = 8;
796 else if (STRCMP(v[0].buf, ==, "xor")) op = 6;
797 else {
798 char *p = v[0].buf;
799 while (*p) {
800 op <<= 1;
801 if (*p++ == '1')
802 op |= 1;
803 }
804 }
805
806 c = fn[op](MP_NEW, a, b);
807 ok = verify(v[0].buf, r, c, a, b);
808 mp_drop(a); mp_drop(b); mp_drop(r); mp_drop(c);
809 assert(mparena_count(MPARENA_GLOBAL) == 0);
810 return (ok);
811 }
812
813 static int tset(dstr *v)
814 {
815 mp *a = *(mp **)v[0].buf;
816 unsigned long n = *(unsigned long *)v[1].buf;
817 mp *r = *(mp **)v[2].buf;
818 mp *c;
819 int ok = 1;
820
821 c = mp_setbit2c(MP_NEW, a, n);
822 if (!MP_EQ(c, r)) {
823 ok = 0;
824 fprintf(stderr, "\n***setbit (set) failed");
825 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
826 fprintf(stderr, "\n*** n = %lu", n);
827 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
828 fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16);
829 fputc('\n', stderr);
830 }
831 if (!mp_testbit2c(r, n)) {
832 ok = 0;
833 fprintf(stderr, "\n***setbit (test) failed");
834 fprintf(stderr, "\n*** n = %lu", n);
835 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
836 fputc('\n', stderr);
837 }
838 mp_drop(a);
839 mp_drop(r);
840 mp_drop(c);
841 assert(mparena_count(MPARENA_GLOBAL) == 0);
842 return (ok);
843 }
844
845 static int tclr(dstr *v)
846 {
847 mp *a = *(mp **)v[0].buf;
848 unsigned long n = *(unsigned long *)v[1].buf;
849 mp *r = *(mp **)v[2].buf;
850 mp *c;
851 int ok = 1;
852
853 c = mp_clearbit2c(MP_NEW, a, n);
854 if (!MP_EQ(c, r)) {
855 ok = 0;
856 fprintf(stderr, "\n***clrbit (set) failed");
857 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
858 fprintf(stderr, "\n*** n = %lu", n);
859 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
860 fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16);
861 fputc('\n', stderr);
862 }
863 if (mp_testbit2c(r, n)) {
864 ok = 0;
865 fprintf(stderr, "\n***clrbit (test) failed");
866 fprintf(stderr, "\n*** n = %lu", n);
867 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
868 fputc('\n', stderr);
869 }
870 mp_drop(a);
871 mp_drop(c);
872 mp_drop(r);
873 assert(mparena_count(MPARENA_GLOBAL) == 0);
874 return (ok);
875 }
876
877 static int tneg(dstr *v)
878 {
879 mp *a = *(mp **)v[0].buf;
880 mp *r = *(mp **)v[1].buf;
881 int ok = 1;
882 mp *n = mp_neg(MP_NEW, a);
883 if (!MP_EQ(r, n)) {
884 ok = 0;
885 fprintf(stderr, "\n*** neg failed\n");
886 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
887 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10);
888 fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10);
889 fputc('\n', stderr);
890 }
891 mp_drop(n);
892 n = mp_neg(a, a);
893 if (!MP_EQ(r, n)) {
894 ok = 0;
895 fprintf(stderr, "\n*** neg failed\n");
896 fputs("\n*** a* = ", stderr); mp_writefile(a, stderr, 10);
897 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10);
898 fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10);
899 fputc('\n', stderr);
900 }
901 mp_drop(a);
902 mp_drop(r);
903 assert(mparena_count(MPARENA_GLOBAL) == 0);
904 return (ok);
905 }
906
907 static int todd(dstr *v)
908 {
909 mp *a = *(mp **)v[0].buf;
910 size_t rs = *(uint32 *)v[1].buf;
911 mp *rt = *(mp **)v[2].buf;
912 int ok = 1;
913 mp *t;
914 size_t s;
915 t = mp_odd(MP_NEW, a, &s);
916 if (s != rs || !MP_EQ(t, rt)) {
917 ok = 0;
918 fprintf(stderr, "\n*** odd failed");
919 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
920 fprintf(stderr, "\n*** s = %lu", (unsigned long)s);
921 fputs("\n*** t = ", stderr); mp_writefile(t, stderr, 10);
922 fprintf(stderr, "\n*** rs = %lu", (unsigned long)rs);
923 fputs("\n*** rt = ", stderr); mp_writefile(rt, stderr, 10);
924 fputc('\n', stderr);
925 }
926 mp_drop(a);
927 mp_drop(rt);
928 mp_drop(t);
929 assert(mparena_count(MPARENA_GLOBAL) == 0);
930 return (ok);
931 }
932
933 static test_chunk tests[] = {
934 { "lsl", tlsl, { &type_mp, &type_int, &type_mp, 0 } },
935 { "lsr", tlsr, { &type_mp, &type_int, &type_mp, 0 } },
936 { "lsl2c", tlsl2c, { &type_mp, &type_int, &type_mp, 0 } },
937 { "lsr2c", tlsr2c, { &type_mp, &type_int, &type_mp, 0 } },
938 { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } },
939 { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } },
940 { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } },
941 { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } },
942 { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } },
943 { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
944 { "exp", texp, { &type_mp, &type_mp, &type_mp, 0 } },
945 { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } },
946 { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } },
947 { "neg", tneg, { &type_mp, &type_mp, 0 } },
948 { 0, 0, { 0 } },
949 };
950
951 int main(int argc, char *argv[])
952 {
953 sub_init();
954 test_run(argc, argv, tests, SRCDIR "/t/mp");
955 return (0);
956 }
957
958 #endif
959
960 /*----- That's all, folks -------------------------------------------------*/