3 * $Id: mpx.h,v 1.12 2001/04/03 19:36:05 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.12 2001/04/03 19:36:05 mdw
34 * Add some simple bitwise operations so that Perl can use them.
36 * Revision 1.11 2000/10/08 15:48:35 mdw
37 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
39 * Revision 1.10 2000/10/08 12:06:12 mdw
40 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
42 * Revision 1.9 1999/12/22 15:49:07 mdw
43 * New function for division by a small integer.
45 * Revision 1.8 1999/12/11 10:57:43 mdw
46 * Karatsuba squaring algorithm.
48 * Revision 1.7 1999/12/11 01:51:28 mdw
49 * Change Karatsuba parameters slightly.
51 * Revision 1.6 1999/12/10 23:23:51 mdw
52 * Karatsuba-Ofman multiplication algorithm.
54 * Revision 1.5 1999/11/20 22:23:27 mdw
55 * Add function versions of some low-level macros with wider use.
57 * Revision 1.4 1999/11/17 18:04:43 mdw
58 * Add two's complement support. Fix a bug in MPX_UMLAN.
60 * Revision 1.3 1999/11/13 01:51:29 mdw
61 * Minor interface changes. Should be stable now.
63 * Revision 1.2 1999/11/11 17:47:55 mdw
64 * Minor changes for different `mptypes.h' format.
66 * Revision 1.1 1999/09/03 08:41:12 mdw
71 #ifndef CATACOMB_MPX_H
72 #define CATACOMB_MPX_H
78 /*----- The idea ----------------------------------------------------------*
80 * This file provides functions and macros which work on vectors of words as
81 * unsigned multiprecision integers. The interface works in terms of base
82 * and limit pointers (i.e., a pointer to the start of a vector, and a
83 * pointer just past its end) rather than base pointer and length, because
84 * that requires more arithmetic and state to work on.
86 * The interfaces are slightly bizarre in other ways. Try to use the
87 * higher-level functions where you can: they're rather better designed to
88 * actually be friendly and useful.
91 /*----- Header files ------------------------------------------------------*/
95 #ifndef CATACOMB_MPW_H
99 /*----- General manipulation ----------------------------------------------*/
101 /* --- @MPX_SHRINK@ --- *
103 * Arguments: @const mpw *v@ = pointer to vector of words
104 * @const mpw *vl@ = (updated) current limit of vector
106 * Use: Shrinks down the limit of a multiprecision integer vector.
109 #define MPX_SHRINK(v, vl) do { \
110 const mpw *_vv = (v), *_vvl = (vl); \
111 while (_vvl > _vv && !_vvl[-1]) \
113 (vl) = (mpw *)_vvl; \
116 /* --- @MPX_BITS@ --- *
118 * Arguments: @unsigned long b@ = result variable
119 * @const mpw *v@ = pointer to array of words
120 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
122 * Use: Calculates the number of bits in a multiprecision value.
125 #define MPX_BITS(b, v, vl) do { \
126 const mpw *_v = (v), *_vl = (vl); \
127 MPX_SHRINK(_v, _vl); \
131 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
133 unsigned _k = MPW_BITS / 2; \
145 /* --- @MPX_OCTETS@ --- *
147 * Arguments: @size_t o@ = result variable
148 * @const mpw *v, *vl@ = pointer to array of words
150 * Use: Calculates the number of octets in a multiprecision value.
153 #define MPX_OCTETS(o, v, vl) do { \
154 const mpw *_v = (v), *_vl = (vl); \
155 MPX_SHRINK(_v, _vl); \
159 size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
161 unsigned _k = MPW_BITS / 2; \
175 /* --- @MPX_COPY@ --- *
177 * Arguments: @dv, dvl@ = destination vector base and limit
178 * @av, avl@ = source vector base and limit
180 * Use: Copies a multiprecision integer.
183 #define MPX_COPY(dv, dvl, av, avl) do { \
184 mpw *_dv = (dv), *_dvl = (dvl); \
185 size_t _dn = _dvl - _dv; \
186 const mpw *_av = (av), *_avl = (avl); \
187 size_t _an = _avl - _av; \
190 memset(_dv, 0, MPWS(_dn - _an)); \
191 } else if (_an >= _dn) \
192 memmove(_dv, _av, MPWS(_dn)); \
194 memmove(_dv, _av, MPWS(_an)); \
195 memset(_dv + _an, 0, MPWS(_dn - _an)); \
199 /* --- @MPX_ZERO@ --- *
201 * Arguments: @v, vl@ = base and limit of vector to clear
203 * Use: Zeroes the area between the two vector pointers.
206 #define MPX_ZERO(v, vl) do { \
207 mpw *_v = (v), *_vl = (vl); \
209 memset(_v, 0, MPWS(_vl - _v)); \
212 /*----- Loading and storing -----------------------------------------------*/
214 /* --- @mpx_storel@ --- *
216 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
217 * @void *p@ = pointer to octet array
218 * @size_t sz@ = size of octet array
222 * Use: Stores an MP in an octet array, least significant octet
223 * first. High-end octets are silently discarded if there
224 * isn't enough space for them.
227 extern void mpx_storel(const mpw */
*v*/
, const mpw */
*vl*/
,
228 void */
*p*/
, size_t /*sz*/);
230 /* --- @mpx_loadl@ --- *
232 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
233 * @const void *p@ = pointer to octet array
234 * @size_t sz@ = size of octet array
238 * Use: Loads an MP in an octet array, least significant octet
239 * first. High-end octets are ignored if there isn't enough
243 extern void mpx_loadl(mpw */
*v*/
, mpw */
*vl*/
,
244 const void */
*p*/
, size_t /*sz*/);
246 /* --- @mpx_storeb@ --- *
248 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
249 * @void *p@ = pointer to octet array
250 * @size_t sz@ = size of octet array
254 * Use: Stores an MP in an octet array, most significant octet
255 * first. High-end octets are silently discarded if there
256 * isn't enough space for them.
259 extern void mpx_storeb(const mpw */
*v*/
, const mpw */
*vl*/
,
260 void */
*p*/
, size_t /*sz*/);
262 /* --- @mpx_loadb@ --- *
264 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
265 * @const void *p@ = pointer to octet array
266 * @size_t sz@ = size of octet array
270 * Use: Loads an MP in an octet array, most significant octet
271 * first. High-end octets are ignored if there isn't enough
275 extern void mpx_loadb(mpw */
*v*/
, mpw */
*vl*/
,
276 const void */
*p*/
, size_t /*sz*/);
278 /*----- Logical shifting --------------------------------------------------*/
280 /* --- @mpx_lsl@ --- *
282 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
283 * @const mpw *av, *avl@ = source vector base and limit
284 * @size_t n@ = number of bit positions to shift by
288 * Use: Performs a logical shift left operation on an integer.
291 extern void mpx_lsl(mpw */
*dv*/
, mpw */
*dvl*/
,
292 const mpw */
*av*/
, const mpw */
*avl*/
,
295 /* --- @mpx_lsr@ --- *
297 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
298 * @const mpw *av, *avl@ = source vector base and limit
299 * @size_t n@ = number of bit positions to shift by
303 * Use: Performs a logical shift right operation on an integer.
306 extern void mpx_lsr(mpw */
*dv*/
, mpw */
*dvl*/
,
307 const mpw */
*av*/
, const mpw */
*avl*/
,
310 /*----- Bitwise operations ------------------------------------------------*/
312 /* --- @mpx_and@, @mpx_or@, @mpx_xor@, @mpx_not@ --- *
314 * Arguments: @mpw *dv, *dvl@ = destination vector
315 * @const mpw *av, *avl@ = first source vector
316 * @const mpw *bv, *bvl@ = second source vector
320 * Use; Does the obvious bitwise operations.
323 extern void mpx_and(mpw */
*dv*/
, mpw */
*dvl*/
,
324 const mpw */
*av*/
, const mpw */
*avl*/
,
325 const mpw */
*bv*/
, const mpw */
*bvl*/
);
327 extern void mpx_or(mpw */
*dv*/
, mpw */
*dvl*/
,
328 const mpw */
*av*/
, const mpw */
*avl*/
,
329 const mpw */
*bv*/
, const mpw */
*bvl*/
);
331 extern void mpx_xor(mpw */
*dv*/
, mpw */
*dvl*/
,
332 const mpw */
*av*/
, const mpw */
*avl*/
,
333 const mpw */
*bv*/
, const mpw */
*bvl*/
);
335 extern void mpx_not(mpw */
*dv*/
, mpw */
*dvl*/
,
336 const mpw */
*av*/
, const mpw */
*avl*/
);
338 /*----- Unsigned arithmetic -----------------------------------------------*/
340 /* --- @mpx_2c@ --- *
342 * Arguments: @mpw *dv, *dvl@ = destination vector
343 * @const mpw *v, *vl@ = source vector
347 * Use: Calculates the two's complement of @v@.
350 extern void mpx_2c(mpw */
*dv*/
, mpw */
*dvl*/
,
351 const mpw */
*v*/
, const mpw */
*vl*/
);
353 /* --- @mpx_ueq@ --- *
355 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
356 * @const mpw *bv, *bvl@ = second argument vector base and limit
358 * Returns: Nonzero if the two vectors are equal.
360 * Use: Performs an unsigned integer test for equality.
363 extern int mpx_ueq(const mpw */
*av*/
, const mpw */
*avl*/
,
364 const mpw */
*bv*/
, const mpw */
*bvl*/
);
366 /* --- @mpx_ucmp@ --- *
368 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
369 * @const mpw *bv, *bvl@ = second argument vector base and limit
371 * Returns: Less than, equal to, or greater than zero depending on
372 * whether @a@ is less than, equal to or greater than @b@,
375 * Use: Performs an unsigned integer comparison.
378 #define MPX_UCMP(av, avl, op, dv, dvl) \
379 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
381 extern int mpx_ucmp(const mpw */
*av*/
, const mpw */
*avl*/
,
382 const mpw */
*bv*/
, const mpw */
*bvl*/
);
384 /* --- @mpx_uadd@ --- *
386 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
387 * @const mpw *av, *avl@ = first addend vector base and limit
388 * @const mpw *bv, *bvl@ = second addend vector base and limit
392 * Use: Performs unsigned integer addition. If the result overflows
393 * the destination vector, high-order bits are discarded. This
394 * means that two's complement addition happens more or less for
395 * free, although that's more a side-effect than anything else.
396 * The result vector may be equal to either or both source
397 * vectors, but may not otherwise overlap them.
400 extern void mpx_uadd(mpw */
*dv*/
, mpw */
*dvl*/
,
401 const mpw */
*av*/
, const mpw */
*avl*/
,
402 const mpw */
*bv*/
, const mpw */
*bvl*/
);
404 /* --- @mpx_uaddn@ --- *
406 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
407 * @mpw n@ = other addend
411 * Use: Adds a small integer to a multiprecision number.
414 #define MPX_UADDN(dv, dvl, n) do { \
415 mpw *_ddv = (dv), *_ddvl = (dvl); \
418 while (_c && _ddv < _ddvl) { \
419 mpd _x = (mpd)*_ddv + (mpd)_c; \
421 _c = _x >> MPW_BITS; \
425 extern void mpx_uaddn(mpw */
*dv*/
, mpw */
*dvl*/
, mpw
/*n*/);
427 /* --- @mpx_usub@ --- *
429 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
430 * @const mpw *av, *avl@ = first argument vector base and limit
431 * @const mpw *bv, *bvl@ = second argument vector base and limit
435 * Use: Performs unsigned integer subtraction. If the result
436 * overflows the destination vector, high-order bits are
437 * discarded. This means that two's complement subtraction
438 * happens more or less for free, although that's more a side-
439 * effect than anything else. The result vector may be equal to
440 * either or both source vectors, but may not otherwise overlap
444 extern void mpx_usub(mpw */
*dv*/
, mpw */
*dvl*/
,
445 const mpw */
*av*/
, const mpw */
*avl*/
,
446 const mpw */
*bv*/
, const mpw */
*bvl*/
);
448 /* --- @mpx_usubn@ --- *
450 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
455 * Use: Subtracts a small integer from a multiprecision number.
458 #define MPX_USUBN(dv, dvl, n) do { \
459 mpw *_ddv = (dv), *_ddvl = (dvl); \
462 while (_ddv < _ddvl) { \
463 mpd _x = (mpd)*_ddv - (mpd)_c; \
465 if (_x >> MPW_BITS) \
472 extern void mpx_usubn(mpw */
*dv*/
, mpw */
*dvl*/
, mpw
/*n*/);
474 /* --- @mpx_umul@ --- *
476 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
477 * @const mpw *av, *avl@ = multiplicand vector base and limit
478 * @const mpw *bv, *bvl@ = multiplier vector base and limit
482 * Use: Performs unsigned integer multiplication. If the result
483 * overflows the desination vector, high-order bits are
484 * discarded. The result vector may not overlap the argument
485 * vectors in any way.
488 extern void mpx_umul(mpw */
*dv*/
, mpw */
*dvl*/
,
489 const mpw */
*av*/
, const mpw */
*avl*/
,
490 const mpw */
*bv*/
, const mpw */
*bvl*/
);
492 /* --- @mpx_umuln@ --- *
494 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
495 * @const mpw *av, *avl@ = multiplicand vector base and limit
496 * @mpw m@ = multiplier
500 * Use: Multiplies a multiprecision integer by a single-word value.
501 * The destination and source may be equal. The destination
502 * is completely cleared after use.
505 #define MPX_UMULN(dv, dvl, av, avl, m) do { \
506 mpw *_dv = (dv), *_dvl = (dvl); \
507 const mpw *_av = (av), *_avl = (avl); \
511 while (_av < _avl) { \
515 _x = (mpd)_m * (mpd)*_av++ + _c; \
517 _c = _x >> MPW_BITS; \
521 MPX_ZERO(_dv, _dvl); \
525 extern void mpx_umuln(mpw */
*dv*/
, mpw */
*dvl*/
,
526 const mpw */
*av*/
, const mpw */
*avl*/
, mpw m
);
528 /* --- @mpx_umlan@ --- *
530 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
531 * @const mpw *av, *avl@ = multiplicand vector base and limit
532 * @mpw m@ = multiplier
536 * Use: Multiplies a multiprecision integer by a single-word value
537 * and adds the result to an accumulator.
540 #define MPX_UMLAN(dv, dvl, av, avl, m) do { \
541 mpw *_dv = (dv), *_dvl = (dvl); \
542 const mpw *_av = (av), *_avl = (avl); \
546 while (_dv < _dvl && _av < _avl) { \
548 _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc; \
550 _cc = _x >> MPW_BITS; \
552 MPX_UADDN(_dv, _dvl, _cc); \
555 extern void mpx_umlan(mpw */
*dv*/
, mpw */
*dvl*/
,
556 const mpw */
*av*/
, const mpw */
*avl*/
, mpw m
);
558 /* --- @mpx_usqr@ --- *
560 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
561 * @const mpw *av, *av@ = source vector base and limit
565 * Use: Performs unsigned integer squaring. The result vector must
566 * not overlap the source vector in any way.
569 extern void mpx_usqr(mpw */
*dv*/
, mpw */
*dvl*/
,
570 const mpw */
*av*/
, const mpw */
*avl*/
);
572 /* --- @mpx_udiv@ --- *
574 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
575 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
576 * @const mpw *dv, *dvl@ = divisor vector base and limit
577 * @mpw *sv, *svl@ = scratch workspace
581 * Use: Performs unsigned integer division. If the result overflows
582 * the quotient vector, high-order bits are discarded. (Clearly
583 * the remainder vector can't overflow.) The various vectors
584 * may not overlap in any way. Yes, I know it's a bit odd
585 * requiring the dividend to be in the result position but it
586 * does make some sense really. The remainder must have
587 * headroom for at least two extra words. The scratch space
588 * must be at least one word larger than the divisor.
591 extern void mpx_udiv(mpw */
*qv*/
, mpw */
*qvl*/
, mpw */
*rv*/
, mpw */
*rvl*/
,
592 const mpw */
*dv*/
, const mpw */
*dvl*/
,
593 mpw */
*sv*/
, mpw */
*svl*/
);
595 /* --- @mpx_udivn@ --- *
597 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
599 * @const mpw *rv, *rvl@ = dividend
600 * @mpw d@ = single-precision divisor
602 * Returns: Remainder after divison.
604 * Use: Performs a single-precision division operation.
607 extern mpw
mpx_udivn(mpw */
*qv*/
, mpw */
*qvl*/
,
608 const mpw */
*rv*/
, const mpw */
*rvl*/
, mpw
/*d*/);
610 /*----- Karatsuba multiplication algorithms -------------------------------*/
612 /* --- @MPK_THRESH@ --- *
614 * This is the limiting length for using Karatsuba algorithms. It's best to
615 * use the simpler classical multiplication method on numbers smaller than
619 #define MPK_THRESH 16
621 /* --- @MPK_SLOP@ --- *
623 * The extra number of words required as scratch space by the Karatsuba
624 * routines. This is a (generous) guess, since the actual amount of space
625 * required is proportional to the recursion depth.
630 /* --- @mpx_kmul@ --- *
632 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
633 * @const mpw *av, *avl@ = pointer to first argument
634 * @const mpw *bv, *bvl@ = pointer to second argument
635 * @mpw *sv, *svl@ = pointer to scratch workspace
639 * Use: Multiplies two multiprecision integers using Karatsuba's
640 * algorithm. This is rather faster than traditional long
641 * multiplication (e.g., @mpx_umul@) on large numbers, although
642 * more expensive on small ones.
644 * The destination and scratch buffers must be twice as large as
645 * the larger argument. The scratch space must be twice as
646 * large as the larger argument, plus the magic number
650 extern void mpx_kmul(mpw */
*dv*/
, mpw */
*dvl*/
,
651 const mpw */
*av*/
, const mpw */
*avl*/
,
652 const mpw */
*bv*/
, const mpw */
*bvl*/
,
653 mpw */
*sv*/
, mpw */
*svl*/
);
655 /* --- @mpx_ksqr@ --- *
657 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
658 * @const mpw *av, *avl@ = pointer to first argument
659 * @mpw *sv, *svl@ = pointer to scratch workspace
663 * Use: Squares a multiprecision integers using something similar to
664 * Karatsuba's multiplication algorithm. This is rather faster
665 * than traditional long multiplication (e.g., @mpx_umul@) on
666 * large numbers, although more expensive on small ones, and
667 * rather simpler than full-blown Karatsuba multiplication.
669 * The destination must be twice as large as the argument. The
670 * scratch space must be twice as large as the argument, plus
671 * the magic number @MPK_SLOP@.
674 extern void mpx_ksqr(mpw */
*dv*/
, mpw */
*dvl*/
,
675 const mpw */
*av*/
, const mpw */
*avl*/
,
676 mpw */
*sv*/
, mpw */
*svl*/
);
678 /*----- That's all, folks -------------------------------------------------*/