3 * $Id: mpx.c,v 1.2 1999/11/13 01:50:59 mdw Exp $
5 * Low-level multiprecision arithmetic
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 --------------------------------------------------*
33 * Revision 1.2 1999/11/13 01:50:59 mdw
34 * Multiprecision routines finished and tested.
36 * Revision 1.1 1999/09/03 08:41:12 mdw
41 /*----- Header files ------------------------------------------------------*/
48 #include <mLib/bits.h>
53 /*----- Loading and storing -----------------------------------------------*/
55 /* --- @mpx_storel@ --- *
57 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
58 * @void *pp@ = pointer to octet array
59 * @size_t sz@ = size of octet array
63 * Use: Stores an MP in an octet array, least significant octet
64 * first. High-end octets are silently discarded if there
65 * isn't enough space for them.
68 void mpx_storel(const mpw *v, const mpw *vl, void *pp, size_t sz)
71 octet *p = pp, *q = p + sz;
81 *p++ = U8(w | n << bits);
93 /* --- @mpx_loadl@ --- *
95 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
96 * @const void *pp@ = pointer to octet array
97 * @size_t sz@ = size of octet array
101 * Use: Loads an MP in an octet array, least significant octet
102 * first. High-end octets are ignored if there isn't enough
106 void mpx_loadl(mpw *v, mpw *vl, const void *pp, size_t sz)
110 const octet *p = pp, *q = p + sz;
119 if (bits >= MPW_BITS) {
121 w = n >> (MPW_BITS - bits + 8);
131 /* --- @mpx_storeb@ --- *
133 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
134 * @void *pp@ = pointer to octet array
135 * @size_t sz@ = size of octet array
139 * Use: Stores an MP in an octet array, most significant octet
140 * first. High-end octets are silently discarded if there
141 * isn't enough space for them.
144 void mpx_storeb(const mpw *v, const mpw *vl, void *pp, size_t sz)
147 octet *p = pp, *q = p + sz;
157 *--q = U8(w | n << bits);
159 bits += MPW_BITS - 8;
169 /* --- @mpx_loadb@ --- *
171 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
172 * @const void *pp@ = pointer to octet array
173 * @size_t sz@ = size of octet array
177 * Use: Loads an MP in an octet array, most significant octet
178 * first. High-end octets are ignored if there isn't enough
182 void mpx_loadb(mpw *v, mpw *vl, const void *pp, size_t sz)
186 const octet *p = pp, *q = p + sz;
195 if (bits >= MPW_BITS) {
197 w = n >> (MPW_BITS - bits + 8);
207 /*----- Logical shifting --------------------------------------------------*/
209 /* --- @mpx_lsl@ --- *
211 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
212 * @const mpw *av, *avl@ = source vector base and limit
213 * @size_t n@ = number of bit positions to shift by
217 * Use: Performs a logical shift left operation on an integer.
220 void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
225 /* --- Trivial special case --- */
228 MPX_COPY(dv, dvl, av, avl);
230 /* --- Single bit shifting --- */
239 *dv++ = MPW((t << 1) | w);
240 w = t >> (MPW_BITS - 1);
249 /* --- Break out word and bit shifts for more sophisticated work --- */
254 /* --- Handle a shift by a multiple of the word size --- */
257 MPX_COPY(dv + nw, dvl, av, avl);
258 memset(dv, 0, MPWS(nw));
261 /* --- And finally the difficult case --- *
263 * This is a little convoluted, because I have to start from the end and
264 * work backwards to avoid overwriting the source, if they're both the same
270 size_t nr = MPW_BITS - nb;
271 size_t dvn = dvl - dv;
272 size_t avn = avl - av;
279 if (dvn > avn + nw) {
280 size_t off = avn + nw + 1;
281 MPX_ZERO(dv + off, dvl);
291 *--dvl = (t >> nr) | w;
302 /* --- @mpx_lsr@ --- *
304 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
305 * @const mpw *av, *avl@ = source vector base and limit
306 * @size_t n@ = number of bit positions to shift by
310 * Use: Performs a logical shift right operation on an integer.
313 void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
318 /* --- Trivial special case --- */
321 MPX_COPY(dv, dvl, av, avl);
323 /* --- Single bit shifting --- */
332 *dv++ = MPW((t << (MPW_BITS - 1)) | w);
342 /* --- Break out word and bit shifts for more sophisticated work --- */
347 /* --- Handle a shift by a multiple of the word size --- */
350 MPX_COPY(dv, dvl, av + nw, avl);
352 /* --- And finally the difficult case --- */
356 size_t nr = MPW_BITS - nb;
365 *dv++ = MPW((w >> nb) | (t << nr));
369 *dv++ = MPW(w >> nb);
377 /*----- Unsigned arithmetic -----------------------------------------------*/
379 /* --- @mpx_ucmp@ --- *
381 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
382 * @const mpw *bv, *bvl@ = second argument vector base and limit
384 * Returns: Less than, equal to, or greater than zero depending on
385 * whether @a@ is less than, equal to or greater than @b@,
388 * Use: Performs an unsigned integer comparison.
391 int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
396 if (avl - av > bvl - bv)
398 else if (avl - av < bvl - bv)
400 else while (avl > av) {
401 mpw a = *--avl, b = *--bvl;
410 /* --- @mpx_uadd@ --- *
412 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
413 * @const mpw *av, *avl@ = first addend vector base and limit
414 * @const mpw *bv, *bvl@ = second addend vector base and limit
418 * Use: Performs unsigned integer addition. If the result overflows
419 * the destination vector, high-order bits are discarded. This
420 * means that two's complement addition happens more or less for
421 * free, although that's more a side-effect than anything else.
422 * The result vector may be equal to either or both source
423 * vectors, but may not otherwise overlap them.
426 void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
427 const mpw *bv, const mpw *bvl)
431 while (av < avl || bv < bvl) {
436 a = (av < avl) ? *av++ : 0;
437 b = (bv < bvl) ? *bv++ : 0;
438 x = (mpd)a + (mpd)b + c;
448 /* --- @mpx_usub@ --- *
450 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
451 * @const mpw *av, *avl@ = first argument vector base and limit
452 * @const mpw *bv, *bvl@ = second argument vector base and limit
456 * Use: Performs unsigned integer subtraction. If the result
457 * overflows the destination vector, high-order bits are
458 * discarded. This means that two's complement subtraction
459 * happens more or less for free, althuogh that's more a side-
460 * effect than anything else. The result vector may be equal to
461 * either or both source vectors, but may not otherwise overlap
465 void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
466 const mpw *bv, const mpw *bvl)
470 while (av < avl || bv < bvl) {
475 a = (av < avl) ? *av++ : 0;
476 b = (bv < bvl) ? *bv++ : 0;
477 x = (mpd)a - (mpd)b - c;
490 /* --- @mpx_umul@ --- *
492 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
493 * @const mpw *av, *avl@ = multiplicand vector base and limit
494 * @const mpw *bv, *bvl@ = multiplier vector base and limit
498 * Use: Performs unsigned integer multiplication. If the result
499 * overflows the desination vector, high-order bits are
500 * discarded. The result vector may not overlap the argument
501 * vectors in any way.
504 void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
505 const mpw *bv, const mpw *bvl)
507 /* --- This is probably worthwhile on a multiply --- */
512 /* --- Deal with a multiply by zero --- */
519 /* --- Do the initial multiply and initialize the accumulator --- */
521 MPX_UMULN(dv, dvl, av, avl, *bv++);
523 /* --- Do the remaining multiply/accumulates --- */
525 while (dv < dvl && bv < bvl) {
535 x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c;
539 MPX_UADDN(dvv, dvl, c);
544 /* --- @mpx_usqr@ --- *
546 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
547 * @const mpw *av, *av@ = source vector base and limit
551 * Use: Performs unsigned integer squaring. The result vector must
552 * not overlap the source vector in any way.
555 void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
559 /* --- Main loop --- */
567 /* --- Stop if I've run out of destination --- */
572 /* --- Work out the square at this point in the proceedings --- */
576 mpd x = (mpd)a * (mpd)a + *dvv;
578 c = MPW(x >> MPW_BITS);
581 /* --- Now fix up the rest of the vector upwards --- */
584 while (dvv < dvl && avv < avl) {
586 mpd x = (mpd)a * (mpd)*avv++;
587 mpd y = ((x << 1) & MPW_MAX) + c + *dvv;
588 c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS);
591 while (dvv < dvl && c) {
597 /* --- Get ready for the next round --- */
604 /* --- @mpx_udiv@ --- *
606 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
607 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
608 * @const mpw *dv, *dvl@ = divisor vector base and limit
609 * @mpw *sv, *svl@ = scratch workspace
613 * Use: Performs unsigned integer division. If the result overflows
614 * the quotient vector, high-order bits are discarded. (Clearly
615 * the remainder vector can't overflow.) The various vectors
616 * may not overlap in any way. Yes, I know it's a bit odd
617 * requiring the dividend to be in the result position but it
618 * does make some sense really. The remainder must have
619 * headroom for at least two extra words. The scratch space
620 * must be at least two words larger than twice the size of the
624 void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
625 const mpw *dv, const mpw *dvl,
632 /* --- Initialize the quotient --- */
636 /* --- Perform some sanity checks --- */
639 assert(((void)"division by zero in mpx_udiv", dv < dvl));
641 /* --- Normalize the divisor --- *
643 * The algorithm requires that the divisor be at least two digits long.
644 * This is easy to fix.
651 for (b = MPW_BITS / 2; b; b >>= 1) {
652 if (d < (MPW_MAX >> b)) {
661 /* --- Normalize the dividend/remainder to match --- */
664 mpw *svvl = sv + (dvl - dv) + 1;
665 mpx_lsl(rv, rvl, rv, rvl, norm);
666 mpx_lsl(sv, svvl, dv, dvl, norm);
677 /* --- Work out the relative scales --- */
680 size_t rvn = rvl - rv;
681 size_t dvn = dvl - dv;
683 /* --- If the divisor is clearly larger, notice this --- */
686 mpx_lsr(rv, rvl, rv, rvl, norm);
693 /* --- Calculate the most significant quotient digit --- *
695 * Because the divisor has its top bit set, this can only happen once. The
696 * pointer arithmetic is a little contorted, to make sure that the
697 * behaviour is defined.
700 if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) {
701 mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl);
702 if (qvl - qv > scale)
706 /* --- Now for the main loop --- */
715 /* --- Get an estimate for the next quotient digit --- */
722 rh = ((mpd)r << MPW_BITS) | rr;
728 /* --- Refine the estimate --- */
732 mpd yl = (mpd)dd * q;
735 yh += yl >> MPW_BITS;
739 while (yh > rh || (yh == rh && yl > rrr)) {
750 /* --- Remove a chunk from the dividend --- */
757 /* --- Calculate the size of the chunk --- */
759 for (svv = sv, dvv = dv; dvv < dvl; svv++, dvv++) {
760 mpd x = (mpd)*dvv * (mpd)q + c;
767 /* --- Now make sure that we can cope with the difference --- *
769 * Take advantage of the fact that subtraction works two's-
773 mpx_usub(rv + scale, rvl, rv + scale, rvl, sv, svv);
774 if (rvl[-1] > MPW_MAX / 2) {
775 mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl);
780 /* --- Done for another iteration --- */
782 if (qvl - qv > scale)
789 /* --- Now fiddle with unnormalizing and things --- */
791 mpx_lsr(rv, rvl, rv, rvl, norm);
794 /*----- That's all, folks -------------------------------------------------*/