3 * $Id: mpx.h,v 1.10 2000/10/08 12:06:12 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.10 2000/10/08 12:06:12 mdw
34 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
36 * Revision 1.9 1999/12/22 15:49:07 mdw
37 * New function for division by a small integer.
39 * Revision 1.8 1999/12/11 10:57:43 mdw
40 * Karatsuba squaring algorithm.
42 * Revision 1.7 1999/12/11 01:51:28 mdw
43 * Change Karatsuba parameters slightly.
45 * Revision 1.6 1999/12/10 23:23:51 mdw
46 * Karatsuba-Ofman multiplication algorithm.
48 * Revision 1.5 1999/11/20 22:23:27 mdw
49 * Add function versions of some low-level macros with wider use.
51 * Revision 1.4 1999/11/17 18:04:43 mdw
52 * Add two's complement support. Fix a bug in MPX_UMLAN.
54 * Revision 1.3 1999/11/13 01:51:29 mdw
55 * Minor interface changes. Should be stable now.
57 * Revision 1.2 1999/11/11 17:47:55 mdw
58 * Minor changes for different `mptypes.h' format.
60 * Revision 1.1 1999/09/03 08:41:12 mdw
65 #ifndef CATACOMB_MPX_H
66 #define CATACOMB_MPX_H
72 /*----- The idea ----------------------------------------------------------*
74 * This file provides functions and macros which work on vectors of words as
75 * unsigned multiprecision integers. The interface works in terms of base
76 * and limit pointers (i.e., a pointer to the start of a vector, and a
77 * pointer just past its end) rather than base pointer and length, because
78 * that requires more arithmetic and state to work on.
80 * The interfaces are slightly bizarre in other ways. Try to use the
81 * higher-level functions where you can: they're rather better designed to
82 * actually be friendly and useful.
85 /*----- Header files ------------------------------------------------------*/
89 #ifndef CATACOMB_MPW_H
93 /*----- General manipulation ----------------------------------------------*/
95 /* --- @MPX_SHRINK@ --- *
97 * Arguments: @const mpw *v@ = pointer to vector of words
98 * @const mpw *vl@ = (updated) current limit of vector
100 * Use: Shrinks down the limit of a multiprecision integer vector.
103 #define MPX_SHRINK(v, vl) do { \
104 const mpw *_vv = (v), *_vvl = (vl); \
105 while (_vvl > _vv && !_vvl[-1]) \
107 (vl) = (mpw *)_vvl; \
110 /* --- @MPX_BITS@ --- *
112 * Arguments: @unsigned long b@ = result variable
113 * @const mpw *v@ = pointer to array of words
114 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
116 * Use: Calculates the number of bits in a multiprecision value.
119 #define MPX_BITS(b, v, vl) do { \
120 const mpw *_v = (v), *_vl = (vl); \
121 MPX_SHRINK(_v, _vl); \
125 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
127 unsigned _k = MPW_BITS / 2; \
139 /* --- @MPX_OCTETS@ --- *
141 * Arguments: @size_t o@ = result variable
142 * @const mpw *v, *vl@ = pointer to array of words
144 * Use: Calculates the number of octets in a multiprecision value.
147 #define MPX_OCTETS(o, v, vl) do { \
148 const mpw *_v = (v), *_vl = (vl); \
149 MPX_SHRINK(_v, _vl); \
153 size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
155 unsigned _k = MPW_BITS / 2; \
169 /* --- @MPX_COPY@ --- *
171 * Arguments: @dv, dvl@ = destination vector base and limit
172 * @av, avl@ = source vector base and limit
174 * Use: Copies a multiprecision integer.
177 #define MPX_COPY(dv, dvl, av, avl) do { \
178 mpw *_dv = (dv), *_dvl = (dvl); \
179 size_t _dn = _dvl - _dv; \
180 const mpw *_av = (av), *_avl = (avl); \
181 size_t _an = _avl - _av; \
184 memset(_dv, 0, MPWS(_dn - _an)); \
185 } else if (_an >= _dn) \
186 memmove(_dv, _av, MPWS(_dn)); \
188 memmove(_dv, _av, MPWS(_an)); \
189 memset(_dv + _an, 0, MPWS(_dn - _an)); \
193 /* --- @MPX_ZERO@ --- *
195 * Arguments: @v, vl@ = base and limit of vector to clear
197 * Use: Zeroes the area between the two vector pointers.
200 #define MPX_ZERO(v, vl) do { \
201 mpw *_v = (v), *_vl = (vl); \
203 memset(_v, 0, MPWS(_vl - _v)); \
206 /*----- Loading and storing -----------------------------------------------*/
208 /* --- @mpx_storel@ --- *
210 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
211 * @void *p@ = pointer to octet array
212 * @size_t sz@ = size of octet array
216 * Use: Stores an MP in an octet array, least significant octet
217 * first. High-end octets are silently discarded if there
218 * isn't enough space for them.
221 extern void mpx_storel(const mpw */*v*/, const mpw */*vl*/,
222 void */*p*/, size_t /*sz*/);
224 /* --- @mpx_loadl@ --- *
226 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
227 * @const void *p@ = pointer to octet array
228 * @size_t sz@ = size of octet array
232 * Use: Loads an MP in an octet array, least significant octet
233 * first. High-end octets are ignored if there isn't enough
237 extern void mpx_loadl(mpw */*v*/, mpw */*vl*/,
238 const void */*p*/, size_t /*sz*/);
240 /* --- @mpx_storeb@ --- *
242 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
243 * @void *p@ = pointer to octet array
244 * @size_t sz@ = size of octet array
248 * Use: Stores an MP in an octet array, most significant octet
249 * first. High-end octets are silently discarded if there
250 * isn't enough space for them.
253 extern void mpx_storeb(const mpw */*v*/, const mpw */*vl*/,
254 void */*p*/, size_t /*sz*/);
256 /* --- @mpx_loadb@ --- *
258 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
259 * @const void *p@ = pointer to octet array
260 * @size_t sz@ = size of octet array
264 * Use: Loads an MP in an octet array, most significant octet
265 * first. High-end octets are ignored if there isn't enough
269 extern void mpx_loadb(mpw */*v*/, mpw */*vl*/,
270 const void */*p*/, size_t /*sz*/);
272 /*----- Logical shifting --------------------------------------------------*/
274 /* --- @mpx_lsl@ --- *
276 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
277 * @const mpw *av, *avl@ = source vector base and limit
278 * @size_t n@ = number of bit positions to shift by
282 * Use: Performs a logical shift left operation on an integer.
285 extern void mpx_lsl(mpw */*dv*/, mpw */*dvl*/,
286 const mpw */*av*/, const mpw */*avl*/,
289 /* --- @mpx_lsr@ --- *
291 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
292 * @const mpw *av, *avl@ = source vector base and limit
293 * @size_t n@ = number of bit positions to shift by
297 * Use: Performs a logical shift right operation on an integer.
300 extern void mpx_lsr(mpw */*dv*/, mpw */*dvl*/,
301 const mpw */*av*/, const mpw */*avl*/,
304 /*----- Unsigned arithmetic -----------------------------------------------*/
306 /* --- @mpx_2c@ --- *
308 * Arguments: @mpw *dv, *dvl@ = destination vector
309 * @const mpw *v, *vl@ = source vector
313 * Use: Calculates the two's complement of @v@.
316 extern void mpx_2c(mpw */*dv*/, mpw */*dvl*/,
317 const mpw */*v*/, const mpw */*vl*/);
319 /* --- @mpx_ueq@ --- *
321 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
322 * @const mpw *bv, *bvl@ = second argument vector base and limit
324 * Returns: Nonzero if the two vectors are equal.
326 * Use: Performs an unsigned integer test for equality.
329 extern int mpx_ueq(const mpw */*av*/, const mpw */*avl*/,
330 const mpw */*bv*/, const mpw */*bvl*/);
332 /* --- @mpx_ucmp@ --- *
334 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
335 * @const mpw *bv, *bvl@ = second argument vector base and limit
337 * Returns: Less than, equal to, or greater than zero depending on
338 * whether @a@ is less than, equal to or greater than @b@,
341 * Use: Performs an unsigned integer comparison.
344 #define MPX_UCMP(av, avl, op, dv, dvl) \
345 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
347 extern int mpx_ucmp(const mpw */*av*/, const mpw */*avl*/,
348 const mpw */*bv*/, const mpw */*bvl*/);
350 /* --- @mpx_uadd@ --- *
352 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
353 * @const mpw *av, *avl@ = first addend vector base and limit
354 * @const mpw *bv, *bvl@ = second addend vector base and limit
358 * Use: Performs unsigned integer addition. If the result overflows
359 * the destination vector, high-order bits are discarded. This
360 * means that two's complement addition happens more or less for
361 * free, although that's more a side-effect than anything else.
362 * The result vector may be equal to either or both source
363 * vectors, but may not otherwise overlap them.
366 extern void mpx_uadd(mpw */*dv*/, mpw */*dvl*/,
367 const mpw */*av*/, const mpw */*avl*/,
368 const mpw */*bv*/, const mpw */*bvl*/);
370 /* --- @mpx_uaddn@ --- *
372 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
373 * @mpw n@ = other addend
377 * Use: Adds a small integer to a multiprecision number.
380 #define MPX_UADDN(dv, dvl, n) do { \
381 mpw *_ddv = (dv), *_ddvl = (dvl); \
384 while (_c && _ddv < _ddvl) { \
385 mpd _x = (mpd)*_ddv + (mpd)_c; \
387 _c = _x >> MPW_BITS; \
391 extern void mpx_uaddn(mpw */*dv*/, mpw */*dvl*/, mpw /*n*/);
393 /* --- @mpx_usub@ --- *
395 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
396 * @const mpw *av, *avl@ = first argument vector base and limit
397 * @const mpw *bv, *bvl@ = second argument vector base and limit
401 * Use: Performs unsigned integer subtraction. If the result
402 * overflows the destination vector, high-order bits are
403 * discarded. This means that two's complement subtraction
404 * happens more or less for free, although that's more a side-
405 * effect than anything else. The result vector may be equal to
406 * either or both source vectors, but may not otherwise overlap
410 extern void mpx_usub(mpw */*dv*/, mpw */*dvl*/,
411 const mpw */*av*/, const mpw */*avl*/,
412 const mpw */*bv*/, const mpw */*bvl*/);
414 /* --- @mpx_usubn@ --- *
416 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
421 * Use: Subtracts a small integer from a multiprecision number.
424 #define MPX_USUBN(dv, dvl, n) do { \
425 mpw *_ddv = (dv), *_ddvl = (dvl); \
428 while (_ddv < _ddvl) { \
429 mpd _x = (mpd)*_ddv - (mpd)_c; \
431 if (_x >> MPW_BITS) \
438 extern void mpx_usubn(mpw */*dv*/, mpw */*dvl*/, mpw /*n*/);
440 /* --- @mpx_umul@ --- *
442 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
443 * @const mpw *av, *avl@ = multiplicand vector base and limit
444 * @const mpw *bv, *bvl@ = multiplier vector base and limit
448 * Use: Performs unsigned integer multiplication. If the result
449 * overflows the desination vector, high-order bits are
450 * discarded. The result vector may not overlap the argument
451 * vectors in any way.
454 extern void mpx_umul(mpw */*dv*/, mpw */*dvl*/,
455 const mpw */*av*/, const mpw */*avl*/,
456 const mpw */*bv*/, const mpw */*bvl*/);
458 /* --- @mpx_umuln@ --- *
460 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
461 * @const mpw *av, *avl@ = multiplicand vector base and limit
462 * @mpw m@ = multiplier
466 * Use: Multiplies a multiprecision integer by a single-word value.
467 * The destination and source may be equal. The destination
468 * is completely cleared after use.
471 #define MPX_UMULN(dv, dvl, av, avl, m) do { \
472 mpw *_dv = (dv), *_dvl = (dvl); \
473 const mpw *_av = (av), *_avl = (avl); \
477 while (_av < _avl) { \
481 _x = (mpd)_m * (mpd)*_av++ + _c; \
483 _c = _x >> MPW_BITS; \
487 MPX_ZERO(_dv, _dvl); \
491 extern void mpx_umuln(mpw */*dv*/, mpw */*dvl*/,
492 const mpw */*av*/, const mpw */*avl*/, mpw m);
494 /* --- @mpx_umlan@ --- *
496 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
497 * @const mpw *av, *avl@ = multiplicand vector base and limit
498 * @mpw m@ = multiplier
502 * Use: Multiplies a multiprecision integer by a single-word value
503 * and adds the result to an accumulator.
506 #define MPX_UMLAN(dv, dvl, av, avl, m) do { \
507 mpw *_dv = (dv), *_dvl = (dvl); \
508 const mpw *_av = (av), *_avl = (avl); \
512 while (_dv < _dvl && _av < _avl) { \
514 _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc; \
516 _cc = _x >> MPW_BITS; \
518 MPX_UADDN(_dv, _dvl, _cc); \
521 extern void mpx_umlan(mpw */*dv*/, mpw */*dvl*/,
522 const mpw */*av*/, const mpw */*avl*/, mpw m);
524 /* --- @mpx_usqr@ --- *
526 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
527 * @const mpw *av, *av@ = source vector base and limit
531 * Use: Performs unsigned integer squaring. The result vector must
532 * not overlap the source vector in any way.
535 extern void mpx_usqr(mpw */*dv*/, mpw */*dvl*/,
536 const mpw */*av*/, const mpw */*avl*/);
538 /* --- @mpx_udiv@ --- *
540 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
541 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
542 * @const mpw *dv, *dvl@ = divisor vector base and limit
543 * @mpw *sv, *svl@ = scratch workspace
547 * Use: Performs unsigned integer division. If the result overflows
548 * the quotient vector, high-order bits are discarded. (Clearly
549 * the remainder vector can't overflow.) The various vectors
550 * may not overlap in any way. Yes, I know it's a bit odd
551 * requiring the dividend to be in the result position but it
552 * does make some sense really. The remainder must have
553 * headroom for at least two extra words. The scratch space
554 * must be at least one word larger than the divisor.
557 extern void mpx_udiv(mpw */*qv*/, mpw */*qvl*/, mpw */*rv*/, mpw */*rvl*/,
558 const mpw */*dv*/, const mpw */*dvl*/,
559 mpw */*sv*/, mpw */*svl*/);
561 /* --- @mpx_udivn@ --- *
563 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
565 * @const mpw *rv, *rvl@ = dividend
566 * @mpw d@ = single-precision divisor
568 * Returns: Remainder after divison.
570 * Use: Performs a single-precision division operation.
573 extern mpw mpx_udivn(mpw */*qv*/, mpw */*qvl*/,
574 const mpw */*rv*/, const mpw */*rvl*/, mpw /*d*/);
576 /*----- Karatsuba multiplication algorithms -------------------------------*/
578 /* --- @KARATSUBA_CUTOFF@ --- *
580 * This is the limiting length for using Karatsuba algorithms. It's best to
581 * use the simpler classical multiplication method on numbers smaller than
585 #define KARATSUBA_CUTOFF 16
587 /* --- @KARATSUBA_SLOP@ --- *
589 * The extra number of words required as scratch space by the Karatsuba
590 * routines. This is a (generous) guess, since the actual amount of space
591 * required is proportional to the recursion depth.
594 #define KARATSUBA_SLOP 64
596 /* --- @mpx_kmul@ --- *
598 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
599 * @const mpw *av, *avl@ = pointer to first argument
600 * @const mpw *bv, *bvl@ = pointer to second argument
601 * @mpw *sv, *svl@ = pointer to scratch workspace
605 * Use: Multiplies two multiprecision integers using Karatsuba's
606 * algorithm. This is rather faster than traditional long
607 * multiplication (e.g., @mpx_umul@) on large numbers, although
608 * more expensive on small ones.
610 * The destination and scratch buffers must be twice as large as
611 * the larger argument. The scratch space must be twice as
612 * large as the larger argument, plus the magic number
616 extern void mpx_kmul(mpw */*dv*/, mpw */*dvl*/,
617 const mpw */*av*/, const mpw */*avl*/,
618 const mpw */*bv*/, const mpw */*bvl*/,
619 mpw */*sv*/, mpw */*svl*/);
621 /* --- @mpx_ksqr@ --- *
623 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
624 * @const mpw *av, *avl@ = pointer to first argument
625 * @mpw *sv, *svl@ = pointer to scratch workspace
629 * Use: Squares a multiprecision integers using something similar to
630 * Karatsuba's multiplication algorithm. This is rather faster
631 * than traditional long multiplication (e.g., @mpx_umul@) on
632 * large numbers, although more expensive on small ones, and
633 * rather simpler than full-blown Karatsuba multiplication.
635 * The destination must be twice as large as the argument. The
636 * scratch space must be twice as large as the argument, plus
637 * the magic number @KARATSUBA_SLOP@.
640 extern void mpx_ksqr(mpw */*dv*/, mpw */*dvl*/,
641 const mpw */*av*/, const mpw */*avl*/,
642 mpw */*sv*/, mpw */*svl*/);
644 /*----- That's all, folks -------------------------------------------------*/