3 * $Id: mpx.h,v 1.11 2000/10/08 15:48:35 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.11 2000/10/08 15:48:35 mdw
34 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
36 * Revision 1.10 2000/10/08 12:06:12 mdw
37 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
39 * Revision 1.9 1999/12/22 15:49:07 mdw
40 * New function for division by a small integer.
42 * Revision 1.8 1999/12/11 10:57:43 mdw
43 * Karatsuba squaring algorithm.
45 * Revision 1.7 1999/12/11 01:51:28 mdw
46 * Change Karatsuba parameters slightly.
48 * Revision 1.6 1999/12/10 23:23:51 mdw
49 * Karatsuba-Ofman multiplication algorithm.
51 * Revision 1.5 1999/11/20 22:23:27 mdw
52 * Add function versions of some low-level macros with wider use.
54 * Revision 1.4 1999/11/17 18:04:43 mdw
55 * Add two's complement support. Fix a bug in MPX_UMLAN.
57 * Revision 1.3 1999/11/13 01:51:29 mdw
58 * Minor interface changes. Should be stable now.
60 * Revision 1.2 1999/11/11 17:47:55 mdw
61 * Minor changes for different `mptypes.h' format.
63 * Revision 1.1 1999/09/03 08:41:12 mdw
68 #ifndef CATACOMB_MPX_H
69 #define CATACOMB_MPX_H
75 /*----- The idea ----------------------------------------------------------*
77 * This file provides functions and macros which work on vectors of words as
78 * unsigned multiprecision integers. The interface works in terms of base
79 * and limit pointers (i.e., a pointer to the start of a vector, and a
80 * pointer just past its end) rather than base pointer and length, because
81 * that requires more arithmetic and state to work on.
83 * The interfaces are slightly bizarre in other ways. Try to use the
84 * higher-level functions where you can: they're rather better designed to
85 * actually be friendly and useful.
88 /*----- Header files ------------------------------------------------------*/
92 #ifndef CATACOMB_MPW_H
96 /*----- General manipulation ----------------------------------------------*/
98 /* --- @MPX_SHRINK@ --- *
100 * Arguments: @const mpw *v@ = pointer to vector of words
101 * @const mpw *vl@ = (updated) current limit of vector
103 * Use: Shrinks down the limit of a multiprecision integer vector.
106 #define MPX_SHRINK(v, vl) do { \
107 const mpw *_vv = (v), *_vvl = (vl); \
108 while (_vvl > _vv && !_vvl[-1]) \
110 (vl) = (mpw *)_vvl; \
113 /* --- @MPX_BITS@ --- *
115 * Arguments: @unsigned long b@ = result variable
116 * @const mpw *v@ = pointer to array of words
117 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
119 * Use: Calculates the number of bits in a multiprecision value.
122 #define MPX_BITS(b, v, vl) do { \
123 const mpw *_v = (v), *_vl = (vl); \
124 MPX_SHRINK(_v, _vl); \
128 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
130 unsigned _k = MPW_BITS / 2; \
142 /* --- @MPX_OCTETS@ --- *
144 * Arguments: @size_t o@ = result variable
145 * @const mpw *v, *vl@ = pointer to array of words
147 * Use: Calculates the number of octets in a multiprecision value.
150 #define MPX_OCTETS(o, v, vl) do { \
151 const mpw *_v = (v), *_vl = (vl); \
152 MPX_SHRINK(_v, _vl); \
156 size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
158 unsigned _k = MPW_BITS / 2; \
172 /* --- @MPX_COPY@ --- *
174 * Arguments: @dv, dvl@ = destination vector base and limit
175 * @av, avl@ = source vector base and limit
177 * Use: Copies a multiprecision integer.
180 #define MPX_COPY(dv, dvl, av, avl) do { \
181 mpw *_dv = (dv), *_dvl = (dvl); \
182 size_t _dn = _dvl - _dv; \
183 const mpw *_av = (av), *_avl = (avl); \
184 size_t _an = _avl - _av; \
187 memset(_dv, 0, MPWS(_dn - _an)); \
188 } else if (_an >= _dn) \
189 memmove(_dv, _av, MPWS(_dn)); \
191 memmove(_dv, _av, MPWS(_an)); \
192 memset(_dv + _an, 0, MPWS(_dn - _an)); \
196 /* --- @MPX_ZERO@ --- *
198 * Arguments: @v, vl@ = base and limit of vector to clear
200 * Use: Zeroes the area between the two vector pointers.
203 #define MPX_ZERO(v, vl) do { \
204 mpw *_v = (v), *_vl = (vl); \
206 memset(_v, 0, MPWS(_vl - _v)); \
209 /*----- Loading and storing -----------------------------------------------*/
211 /* --- @mpx_storel@ --- *
213 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
214 * @void *p@ = pointer to octet array
215 * @size_t sz@ = size of octet array
219 * Use: Stores an MP in an octet array, least significant octet
220 * first. High-end octets are silently discarded if there
221 * isn't enough space for them.
224 extern void mpx_storel(const mpw */
*v*/
, const mpw */
*vl*/
,
225 void */
*p*/
, size_t /*sz*/);
227 /* --- @mpx_loadl@ --- *
229 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
230 * @const void *p@ = pointer to octet array
231 * @size_t sz@ = size of octet array
235 * Use: Loads an MP in an octet array, least significant octet
236 * first. High-end octets are ignored if there isn't enough
240 extern void mpx_loadl(mpw */
*v*/
, mpw */
*vl*/
,
241 const void */
*p*/
, size_t /*sz*/);
243 /* --- @mpx_storeb@ --- *
245 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
246 * @void *p@ = pointer to octet array
247 * @size_t sz@ = size of octet array
251 * Use: Stores an MP in an octet array, most significant octet
252 * first. High-end octets are silently discarded if there
253 * isn't enough space for them.
256 extern void mpx_storeb(const mpw */
*v*/
, const mpw */
*vl*/
,
257 void */
*p*/
, size_t /*sz*/);
259 /* --- @mpx_loadb@ --- *
261 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
262 * @const void *p@ = pointer to octet array
263 * @size_t sz@ = size of octet array
267 * Use: Loads an MP in an octet array, most significant octet
268 * first. High-end octets are ignored if there isn't enough
272 extern void mpx_loadb(mpw */
*v*/
, mpw */
*vl*/
,
273 const void */
*p*/
, size_t /*sz*/);
275 /*----- Logical shifting --------------------------------------------------*/
277 /* --- @mpx_lsl@ --- *
279 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
280 * @const mpw *av, *avl@ = source vector base and limit
281 * @size_t n@ = number of bit positions to shift by
285 * Use: Performs a logical shift left operation on an integer.
288 extern void mpx_lsl(mpw */
*dv*/
, mpw */
*dvl*/
,
289 const mpw */
*av*/
, const mpw */
*avl*/
,
292 /* --- @mpx_lsr@ --- *
294 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
295 * @const mpw *av, *avl@ = source vector base and limit
296 * @size_t n@ = number of bit positions to shift by
300 * Use: Performs a logical shift right operation on an integer.
303 extern void mpx_lsr(mpw */
*dv*/
, mpw */
*dvl*/
,
304 const mpw */
*av*/
, const mpw */
*avl*/
,
307 /*----- Unsigned arithmetic -----------------------------------------------*/
309 /* --- @mpx_2c@ --- *
311 * Arguments: @mpw *dv, *dvl@ = destination vector
312 * @const mpw *v, *vl@ = source vector
316 * Use: Calculates the two's complement of @v@.
319 extern void mpx_2c(mpw */
*dv*/
, mpw */
*dvl*/
,
320 const mpw */
*v*/
, const mpw */
*vl*/
);
322 /* --- @mpx_ueq@ --- *
324 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
325 * @const mpw *bv, *bvl@ = second argument vector base and limit
327 * Returns: Nonzero if the two vectors are equal.
329 * Use: Performs an unsigned integer test for equality.
332 extern int mpx_ueq(const mpw */
*av*/
, const mpw */
*avl*/
,
333 const mpw */
*bv*/
, const mpw */
*bvl*/
);
335 /* --- @mpx_ucmp@ --- *
337 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
338 * @const mpw *bv, *bvl@ = second argument vector base and limit
340 * Returns: Less than, equal to, or greater than zero depending on
341 * whether @a@ is less than, equal to or greater than @b@,
344 * Use: Performs an unsigned integer comparison.
347 #define MPX_UCMP(av, avl, op, dv, dvl) \
348 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
350 extern int mpx_ucmp(const mpw */
*av*/
, const mpw */
*avl*/
,
351 const mpw */
*bv*/
, const mpw */
*bvl*/
);
353 /* --- @mpx_uadd@ --- *
355 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
356 * @const mpw *av, *avl@ = first addend vector base and limit
357 * @const mpw *bv, *bvl@ = second addend vector base and limit
361 * Use: Performs unsigned integer addition. If the result overflows
362 * the destination vector, high-order bits are discarded. This
363 * means that two's complement addition happens more or less for
364 * free, although that's more a side-effect than anything else.
365 * The result vector may be equal to either or both source
366 * vectors, but may not otherwise overlap them.
369 extern void mpx_uadd(mpw */
*dv*/
, mpw */
*dvl*/
,
370 const mpw */
*av*/
, const mpw */
*avl*/
,
371 const mpw */
*bv*/
, const mpw */
*bvl*/
);
373 /* --- @mpx_uaddn@ --- *
375 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
376 * @mpw n@ = other addend
380 * Use: Adds a small integer to a multiprecision number.
383 #define MPX_UADDN(dv, dvl, n) do { \
384 mpw *_ddv = (dv), *_ddvl = (dvl); \
387 while (_c && _ddv < _ddvl) { \
388 mpd _x = (mpd)*_ddv + (mpd)_c; \
390 _c = _x >> MPW_BITS; \
394 extern void mpx_uaddn(mpw */
*dv*/
, mpw */
*dvl*/
, mpw
/*n*/);
396 /* --- @mpx_usub@ --- *
398 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
399 * @const mpw *av, *avl@ = first argument vector base and limit
400 * @const mpw *bv, *bvl@ = second argument vector base and limit
404 * Use: Performs unsigned integer subtraction. If the result
405 * overflows the destination vector, high-order bits are
406 * discarded. This means that two's complement subtraction
407 * happens more or less for free, although that's more a side-
408 * effect than anything else. The result vector may be equal to
409 * either or both source vectors, but may not otherwise overlap
413 extern void mpx_usub(mpw */
*dv*/
, mpw */
*dvl*/
,
414 const mpw */
*av*/
, const mpw */
*avl*/
,
415 const mpw */
*bv*/
, const mpw */
*bvl*/
);
417 /* --- @mpx_usubn@ --- *
419 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
424 * Use: Subtracts a small integer from a multiprecision number.
427 #define MPX_USUBN(dv, dvl, n) do { \
428 mpw *_ddv = (dv), *_ddvl = (dvl); \
431 while (_ddv < _ddvl) { \
432 mpd _x = (mpd)*_ddv - (mpd)_c; \
434 if (_x >> MPW_BITS) \
441 extern void mpx_usubn(mpw */
*dv*/
, mpw */
*dvl*/
, mpw
/*n*/);
443 /* --- @mpx_umul@ --- *
445 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
446 * @const mpw *av, *avl@ = multiplicand vector base and limit
447 * @const mpw *bv, *bvl@ = multiplier vector base and limit
451 * Use: Performs unsigned integer multiplication. If the result
452 * overflows the desination vector, high-order bits are
453 * discarded. The result vector may not overlap the argument
454 * vectors in any way.
457 extern void mpx_umul(mpw */
*dv*/
, mpw */
*dvl*/
,
458 const mpw */
*av*/
, const mpw */
*avl*/
,
459 const mpw */
*bv*/
, const mpw */
*bvl*/
);
461 /* --- @mpx_umuln@ --- *
463 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
464 * @const mpw *av, *avl@ = multiplicand vector base and limit
465 * @mpw m@ = multiplier
469 * Use: Multiplies a multiprecision integer by a single-word value.
470 * The destination and source may be equal. The destination
471 * is completely cleared after use.
474 #define MPX_UMULN(dv, dvl, av, avl, m) do { \
475 mpw *_dv = (dv), *_dvl = (dvl); \
476 const mpw *_av = (av), *_avl = (avl); \
480 while (_av < _avl) { \
484 _x = (mpd)_m * (mpd)*_av++ + _c; \
486 _c = _x >> MPW_BITS; \
490 MPX_ZERO(_dv, _dvl); \
494 extern void mpx_umuln(mpw */
*dv*/
, mpw */
*dvl*/
,
495 const mpw */
*av*/
, const mpw */
*avl*/
, mpw m
);
497 /* --- @mpx_umlan@ --- *
499 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
500 * @const mpw *av, *avl@ = multiplicand vector base and limit
501 * @mpw m@ = multiplier
505 * Use: Multiplies a multiprecision integer by a single-word value
506 * and adds the result to an accumulator.
509 #define MPX_UMLAN(dv, dvl, av, avl, m) do { \
510 mpw *_dv = (dv), *_dvl = (dvl); \
511 const mpw *_av = (av), *_avl = (avl); \
515 while (_dv < _dvl && _av < _avl) { \
517 _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc; \
519 _cc = _x >> MPW_BITS; \
521 MPX_UADDN(_dv, _dvl, _cc); \
524 extern void mpx_umlan(mpw */
*dv*/
, mpw */
*dvl*/
,
525 const mpw */
*av*/
, const mpw */
*avl*/
, mpw m
);
527 /* --- @mpx_usqr@ --- *
529 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
530 * @const mpw *av, *av@ = source vector base and limit
534 * Use: Performs unsigned integer squaring. The result vector must
535 * not overlap the source vector in any way.
538 extern void mpx_usqr(mpw */
*dv*/
, mpw */
*dvl*/
,
539 const mpw */
*av*/
, const mpw */
*avl*/
);
541 /* --- @mpx_udiv@ --- *
543 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
544 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
545 * @const mpw *dv, *dvl@ = divisor vector base and limit
546 * @mpw *sv, *svl@ = scratch workspace
550 * Use: Performs unsigned integer division. If the result overflows
551 * the quotient vector, high-order bits are discarded. (Clearly
552 * the remainder vector can't overflow.) The various vectors
553 * may not overlap in any way. Yes, I know it's a bit odd
554 * requiring the dividend to be in the result position but it
555 * does make some sense really. The remainder must have
556 * headroom for at least two extra words. The scratch space
557 * must be at least one word larger than the divisor.
560 extern void mpx_udiv(mpw */
*qv*/
, mpw */
*qvl*/
, mpw */
*rv*/
, mpw */
*rvl*/
,
561 const mpw */
*dv*/
, const mpw */
*dvl*/
,
562 mpw */
*sv*/
, mpw */
*svl*/
);
564 /* --- @mpx_udivn@ --- *
566 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
568 * @const mpw *rv, *rvl@ = dividend
569 * @mpw d@ = single-precision divisor
571 * Returns: Remainder after divison.
573 * Use: Performs a single-precision division operation.
576 extern mpw
mpx_udivn(mpw */
*qv*/
, mpw */
*qvl*/
,
577 const mpw */
*rv*/
, const mpw */
*rvl*/
, mpw
/*d*/);
579 /*----- Karatsuba multiplication algorithms -------------------------------*/
581 /* --- @MPK_THRESH@ --- *
583 * This is the limiting length for using Karatsuba algorithms. It's best to
584 * use the simpler classical multiplication method on numbers smaller than
588 #define MPK_THRESH 16
590 /* --- @MPK_SLOP@ --- *
592 * The extra number of words required as scratch space by the Karatsuba
593 * routines. This is a (generous) guess, since the actual amount of space
594 * required is proportional to the recursion depth.
599 /* --- @mpx_kmul@ --- *
601 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
602 * @const mpw *av, *avl@ = pointer to first argument
603 * @const mpw *bv, *bvl@ = pointer to second argument
604 * @mpw *sv, *svl@ = pointer to scratch workspace
608 * Use: Multiplies two multiprecision integers using Karatsuba's
609 * algorithm. This is rather faster than traditional long
610 * multiplication (e.g., @mpx_umul@) on large numbers, although
611 * more expensive on small ones.
613 * The destination and scratch buffers must be twice as large as
614 * the larger argument. The scratch space must be twice as
615 * large as the larger argument, plus the magic number
619 extern void mpx_kmul(mpw */
*dv*/
, mpw */
*dvl*/
,
620 const mpw */
*av*/
, const mpw */
*avl*/
,
621 const mpw */
*bv*/
, const mpw */
*bvl*/
,
622 mpw */
*sv*/
, mpw */
*svl*/
);
624 /* --- @mpx_ksqr@ --- *
626 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
627 * @const mpw *av, *avl@ = pointer to first argument
628 * @mpw *sv, *svl@ = pointer to scratch workspace
632 * Use: Squares a multiprecision integers using something similar to
633 * Karatsuba's multiplication algorithm. This is rather faster
634 * than traditional long multiplication (e.g., @mpx_umul@) on
635 * large numbers, although more expensive on small ones, and
636 * rather simpler than full-blown Karatsuba multiplication.
638 * The destination must be twice as large as the argument. The
639 * scratch space must be twice as large as the argument, plus
640 * the magic number @MPK_SLOP@.
643 extern void mpx_ksqr(mpw */
*dv*/
, mpw */
*dvl*/
,
644 const mpw */
*av*/
, const mpw */
*avl*/
,
645 mpw */
*sv*/
, mpw */
*svl*/
);
647 /*----- That's all, folks -------------------------------------------------*/