3 * $Id: mpx.h,v 1.15 2002/10/19 17:56:50 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.15 2002/10/19 17:56:50 mdw
34 * Fix bit operations. Test them (a bit) better.
36 * Revision 1.14 2002/10/09 00:36:03 mdw
37 * Fix bounds on workspace for Karatsuba operations.
39 * Revision 1.13 2002/10/06 22:52:50 mdw
40 * Pile of changes for supporting two's complement properly.
42 * Revision 1.12 2001/04/03 19:36:05 mdw
43 * Add some simple bitwise operations so that Perl can use them.
45 * Revision 1.11 2000/10/08 15:48:35 mdw
46 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
48 * Revision 1.10 2000/10/08 12:06:12 mdw
49 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
51 * Revision 1.9 1999/12/22 15:49:07 mdw
52 * New function for division by a small integer.
54 * Revision 1.8 1999/12/11 10:57:43 mdw
55 * Karatsuba squaring algorithm.
57 * Revision 1.7 1999/12/11 01:51:28 mdw
58 * Change Karatsuba parameters slightly.
60 * Revision 1.6 1999/12/10 23:23:51 mdw
61 * Karatsuba-Ofman multiplication algorithm.
63 * Revision 1.5 1999/11/20 22:23:27 mdw
64 * Add function versions of some low-level macros with wider use.
66 * Revision 1.4 1999/11/17 18:04:43 mdw
67 * Add two's complement support. Fix a bug in MPX_UMLAN.
69 * Revision 1.3 1999/11/13 01:51:29 mdw
70 * Minor interface changes. Should be stable now.
72 * Revision 1.2 1999/11/11 17:47:55 mdw
73 * Minor changes for different `mptypes.h' format.
75 * Revision 1.1 1999/09/03 08:41:12 mdw
80 #ifndef CATACOMB_MPX_H
81 #define CATACOMB_MPX_H
87 /*----- The idea ----------------------------------------------------------*
89 * This file provides functions and macros which work on vectors of words as
90 * unsigned multiprecision integers. The interface works in terms of base
91 * and limit pointers (i.e., a pointer to the start of a vector, and a
92 * pointer just past its end) rather than base pointer and length, because
93 * that requires more arithmetic and state to work on.
95 * The interfaces are slightly bizarre in other ways. Try to use the
96 * higher-level functions where you can: they're rather better designed to
97 * actually be friendly and useful.
100 /*----- Header files ------------------------------------------------------*/
104 #ifndef CATACOMB_MPW_H
108 /*----- General manipulation ----------------------------------------------*/
110 /* --- @MPX_SHRINK@ --- *
112 * Arguments: @const mpw *v@ = pointer to vector of words
113 * @const mpw *vl@ = (updated) current limit of vector
115 * Use: Shrinks down the limit of a multiprecision integer vector.
118 #define MPX_SHRINK(v, vl) do { \
119 const mpw *_vv = (v), *_vvl = (vl); \
120 while (_vvl > _vv && !_vvl[-1]) \
122 (vl) = (mpw *)_vvl; \
125 /* --- @MPX_BITS@ --- *
127 * Arguments: @unsigned long b@ = result variable
128 * @const mpw *v@ = pointer to array of words
129 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
131 * Use: Calculates the number of bits in a multiprecision value.
134 #define MPX_BITS(b, v, vl) do { \
135 const mpw *_v = (v), *_vl = (vl); \
136 MPX_SHRINK(_v, _vl); \
140 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
142 unsigned _k = MPW_BITS / 2; \
154 /* --- @MPX_OCTETS@ --- *
156 * Arguments: @size_t o@ = result variable
157 * @const mpw *v, *vl@ = pointer to array of words
159 * Use: Calculates the number of octets in a multiprecision value.
162 #define MPX_OCTETS(o, v, vl) do { \
164 MPX_BITS(_bb, (v), (vl)); \
165 (o) = (_bb + 7) >> 3; \
168 /* --- @MPX_OCTETS2C@ --- *
170 * Arguments: @size_t o@ = result variable
171 * @const mpw *v, *vl@ = pointer to array of words
173 * Use: Calculates the number of octets in a multiprecision value, if
174 * you represent it as two's complement.
177 #define MPX_OCTETS2C(o, v, vl) do { \
179 MPX_BITS(_bb, (v), (vl)); \
180 (o) = (_bb >> 3) + 1; \
183 /* --- @MPX_COPY@ --- *
185 * Arguments: @dv, dvl@ = destination vector base and limit
186 * @av, avl@ = source vector base and limit
188 * Use: Copies a multiprecision integer.
191 #define MPX_COPY(dv, dvl, av, avl) do { \
192 mpw *_dv = (dv), *_dvl = (dvl); \
193 size_t _dn = _dvl - _dv; \
194 const mpw *_av = (av), *_avl = (avl); \
195 size_t _an = _avl - _av; \
198 memset(_dv, 0, MPWS(_dn - _an)); \
199 } else if (_an >= _dn) \
200 memmove(_dv, _av, MPWS(_dn)); \
202 memmove(_dv, _av, MPWS(_an)); \
203 memset(_dv + _an, 0, MPWS(_dn - _an)); \
207 /* --- @MPX_ZERO@ --- *
209 * Arguments: @v, vl@ = base and limit of vector to clear
211 * Use: Zeroes the area between the two vector pointers.
214 #define MPX_ZERO(v, vl) do { \
215 mpw *_v = (v), *_vl = (vl); \
217 memset(_v, 0, MPWS(_vl - _v)); \
220 /*----- Loading and storing -----------------------------------------------*/
222 /* --- @mpx_storel@ --- *
224 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
225 * @void *p@ = pointer to octet array
226 * @size_t sz@ = size of octet array
230 * Use: Stores an MP in an octet array, least significant octet
231 * first. High-end octets are silently discarded if there
232 * isn't enough space for them.
235 extern void mpx_storel(const mpw */
*v*/
, const mpw */
*vl*/
,
236 void */
*p*/
, size_t /*sz*/);
238 /* --- @mpx_loadl@ --- *
240 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
241 * @const void *p@ = pointer to octet array
242 * @size_t sz@ = size of octet array
246 * Use: Loads an MP in an octet array, least significant octet
247 * first. High-end octets are ignored if there isn't enough
251 extern void mpx_loadl(mpw */
*v*/
, mpw */
*vl*/
,
252 const void */
*p*/
, size_t /*sz*/);
254 /* --- @mpx_storeb@ --- *
256 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
257 * @void *p@ = pointer to octet array
258 * @size_t sz@ = size of octet array
262 * Use: Stores an MP in an octet array, most significant octet
263 * first. High-end octets are silently discarded if there
264 * isn't enough space for them.
267 extern void mpx_storeb(const mpw */
*v*/
, const mpw */
*vl*/
,
268 void */
*p*/
, size_t /*sz*/);
270 /* --- @mpx_loadb@ --- *
272 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
273 * @const void *p@ = pointer to octet array
274 * @size_t sz@ = size of octet array
278 * Use: Loads an MP in an octet array, most significant octet
279 * first. High-end octets are ignored if there isn't enough
283 extern void mpx_loadb(mpw */
*v*/
, mpw */
*vl*/
,
284 const void */
*p*/
, size_t /*sz*/);
286 /* --- @mpx_storel2cn@ --- *
288 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
289 * @void *pp@ = pointer to octet array
290 * @size_t sz@ = size of octet array
294 * Use: Stores a negative MP in an octet array, least significant
295 * octet first, as two's complement. High-end octets are
296 * silently discarded if there isn't enough space for them.
297 * This obviously makes the output bad.
300 extern void mpx_storel2cn(const mpw */
*v*/
, const mpw */
*vl*/
,
301 void */
*p*/
, size_t /*sz*/);
303 /* --- @mpx_loadl2cn@ --- *
305 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
306 * @const void *pp@ = pointer to octet array
307 * @size_t sz@ = size of octet array
311 * Use: Loads a negative MP in an octet array, least significant
312 * octet first, as two's complement. High-end octets are
313 * ignored if there isn't enough space for them. This probably
314 * means you made the wrong choice coming here.
317 extern void mpx_loadl2cn(mpw */
*v*/
, mpw */
*vl*/
,
318 const void */
*p*/
, size_t /*sz*/);
320 /* --- @mpx_storeb2cn@ --- *
322 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
323 * @void *pp@ = pointer to octet array
324 * @size_t sz@ = size of octet array
328 * Use: Stores a negative MP in an octet array, most significant
329 * octet first, as two's complement. High-end octets are
330 * silently discarded if there isn't enough space for them,
331 * which probably isn't what you meant.
334 extern void mpx_storeb2cn(const mpw */
*v*/
, const mpw */
*vl*/
,
335 void */
*p*/
, size_t /*sz*/);
337 /* --- @mpx_loadb2cn@ --- *
339 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
340 * @const void *pp@ = pointer to octet array
341 * @size_t sz@ = size of octet array
345 * Use: Loads a negative MP in an octet array, most significant octet
346 * first as two's complement. High-end octets are ignored if
347 * there isn't enough space for them. This probably means you
348 * chose this function wrongly.
351 extern void mpx_loadb2cn(mpw */
*v*/
, mpw */
*vl*/
,
352 const void */
*p*/
, size_t /*sz*/);
355 /*----- Logical shifting --------------------------------------------------*/
357 /* --- @mpx_lsl@ --- *
359 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
360 * @const mpw *av, *avl@ = source vector base and limit
361 * @size_t n@ = number of bit positions to shift by
365 * Use: Performs a logical shift left operation on an integer.
368 extern void mpx_lsl(mpw */
*dv*/
, mpw */
*dvl*/
,
369 const mpw */
*av*/
, const mpw */
*avl*/
,
372 /* --- @mpx_lsr@ --- *
374 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
375 * @const mpw *av, *avl@ = source vector base and limit
376 * @size_t n@ = number of bit positions to shift by
380 * Use: Performs a logical shift right operation on an integer.
383 extern void mpx_lsr(mpw */
*dv*/
, mpw */
*dvl*/
,
384 const mpw */
*av*/
, const mpw */
*avl*/
,
387 /*----- Bitwise operations ------------------------------------------------*/
389 /* --- @mpx_bitop@ --- *
391 * Arguments: @mpw *dv, *dvl@ = destination vector
392 * @const mpw *av, *avl@ = first source vector
393 * @const mpw *bv, *bvl@ = second source vector
397 * Use: Provide the dyadic boolean functions. The functions are
398 * named after the truth table they generate:
405 #define MPX_DOBIN(what) \
406 what(0000) what(0001) what(0010) what(0011) \
407 what(0100) what(0101) what(0110) what(0111) \
408 what(1000) what(1001) what(1010) what(1011) \
409 what(1100) what(1101) what(1110) what(1111)
411 #define MPX_BITDECL(string) \
412 extern void mpx_bit##string(mpw */*dv*/, mpw */*dvl*/, \
413 const mpw */*av*/, const mpw */*avl*/, \
414 const mpw */*bv*/, const mpw */*bvl*/);
415 MPX_DOBIN(MPX_BITDECL
)
417 /* --- @mpx_[n]and@, @mpx_[n]or@, @mpx_xor@ --- *
419 * Synonyms for the commonly-used functions above.
422 #define mpx_and mpx_bit0001
423 #define mpx_or mpx_bit0111
424 #define mpx_nand mpx_bit1110
425 #define mpx_nor mpx_bit1000
426 #define mpx_xor mpx_bit0110
428 /* --- @mpx_not@ --- *
430 * Arguments: @mpw *dv, *dvl@ = destination vector
431 * @const mpw *av, *avl@ = first source vector
438 extern void mpx_not(mpw */
*dv*/
, mpw */
*dvl*/
,
439 const mpw */
*av*/
, const mpw */
*avl*/
);
441 /*----- Unsigned arithmetic -----------------------------------------------*/
443 /* --- @mpx_2c@ --- *
445 * Arguments: @mpw *dv, *dvl@ = destination vector
446 * @const mpw *v, *vl@ = source vector
450 * Use: Calculates the two's complement of @v@.
453 extern void mpx_2c(mpw */
*dv*/
, mpw */
*dvl*/
,
454 const mpw */
*v*/
, const mpw */
*vl*/
);
456 /* --- @mpx_ueq@ --- *
458 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
459 * @const mpw *bv, *bvl@ = second argument vector base and limit
461 * Returns: Nonzero if the two vectors are equal.
463 * Use: Performs an unsigned integer test for equality.
466 extern int mpx_ueq(const mpw */
*av*/
, const mpw */
*avl*/
,
467 const mpw */
*bv*/
, const mpw */
*bvl*/
);
469 /* --- @mpx_ucmp@ --- *
471 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
472 * @const mpw *bv, *bvl@ = second argument vector base and limit
474 * Returns: Less than, equal to, or greater than zero depending on
475 * whether @a@ is less than, equal to or greater than @b@,
478 * Use: Performs an unsigned integer comparison.
481 #define MPX_UCMP(av, avl, op, dv, dvl) \
482 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
484 extern int mpx_ucmp(const mpw */
*av*/
, const mpw */
*avl*/
,
485 const mpw */
*bv*/
, const mpw */
*bvl*/
);
487 /* --- @mpx_uadd@ --- *
489 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
490 * @const mpw *av, *avl@ = first addend vector base and limit
491 * @const mpw *bv, *bvl@ = second addend vector base and limit
495 * Use: Performs unsigned integer addition. If the result overflows
496 * the destination vector, high-order bits are discarded. This
497 * means that two's complement addition happens more or less for
498 * free, although that's more a side-effect than anything else.
499 * The result vector may be equal to either or both source
500 * vectors, but may not otherwise overlap them.
503 extern void mpx_uadd(mpw */
*dv*/
, mpw */
*dvl*/
,
504 const mpw */
*av*/
, const mpw */
*avl*/
,
505 const mpw */
*bv*/
, const mpw */
*bvl*/
);
507 /* --- @mpx_uaddn@ --- *
509 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
510 * @mpw n@ = other addend
514 * Use: Adds a small integer to a multiprecision number.
517 #define MPX_UADDN(dv, dvl, n) do { \
518 mpw *_ddv = (dv), *_ddvl = (dvl); \
521 while (_c && _ddv < _ddvl) { \
522 mpd _x = (mpd)*_ddv + (mpd)_c; \
524 _c = _x >> MPW_BITS; \
528 extern void mpx_uaddn(mpw */
*dv*/
, mpw */
*dvl*/
, mpw
/*n*/);
530 /* --- @mpx_usub@ --- *
532 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
533 * @const mpw *av, *avl@ = first argument vector base and limit
534 * @const mpw *bv, *bvl@ = second argument vector base and limit
538 * Use: Performs unsigned integer subtraction. If the result
539 * overflows the destination vector, high-order bits are
540 * discarded. This means that two's complement subtraction
541 * happens more or less for free, although that's more a side-
542 * effect than anything else. The result vector may be equal to
543 * either or both source vectors, but may not otherwise overlap
547 extern void mpx_usub(mpw */
*dv*/
, mpw */
*dvl*/
,
548 const mpw */
*av*/
, const mpw */
*avl*/
,
549 const mpw */
*bv*/
, const mpw */
*bvl*/
);
551 /* --- @mpx_usubn@ --- *
553 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
558 * Use: Subtracts a small integer from a multiprecision number.
561 #define MPX_USUBN(dv, dvl, n) do { \
562 mpw *_ddv = (dv), *_ddvl = (dvl); \
565 while (_ddv < _ddvl) { \
566 mpd _x = (mpd)*_ddv - (mpd)_c; \
568 if (_x >> MPW_BITS) \
575 extern void mpx_usubn(mpw */
*dv*/
, mpw */
*dvl*/
, mpw
/*n*/);
577 /* --- @mpx_umul@ --- *
579 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
580 * @const mpw *av, *avl@ = multiplicand vector base and limit
581 * @const mpw *bv, *bvl@ = multiplier vector base and limit
585 * Use: Performs unsigned integer multiplication. If the result
586 * overflows the desination vector, high-order bits are
587 * discarded. The result vector may not overlap the argument
588 * vectors in any way.
591 extern void mpx_umul(mpw */
*dv*/
, mpw */
*dvl*/
,
592 const mpw */
*av*/
, const mpw */
*avl*/
,
593 const mpw */
*bv*/
, const mpw */
*bvl*/
);
595 /* --- @mpx_umuln@ --- *
597 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
598 * @const mpw *av, *avl@ = multiplicand vector base and limit
599 * @mpw m@ = multiplier
603 * Use: Multiplies a multiprecision integer by a single-word value.
604 * The destination and source may be equal. The destination
605 * is completely cleared after use.
608 #define MPX_UMULN(dv, dvl, av, avl, m) do { \
609 mpw *_dv = (dv), *_dvl = (dvl); \
610 const mpw *_av = (av), *_avl = (avl); \
614 while (_av < _avl) { \
618 _x = (mpd)_m * (mpd)*_av++ + _c; \
620 _c = _x >> MPW_BITS; \
624 MPX_ZERO(_dv, _dvl); \
628 extern void mpx_umuln(mpw */
*dv*/
, mpw */
*dvl*/
,
629 const mpw */
*av*/
, const mpw */
*avl*/
, mpw m
);
631 /* --- @mpx_umlan@ --- *
633 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
634 * @const mpw *av, *avl@ = multiplicand vector base and limit
635 * @mpw m@ = multiplier
639 * Use: Multiplies a multiprecision integer by a single-word value
640 * and adds the result to an accumulator.
643 #define MPX_UMLAN(dv, dvl, av, avl, m) do { \
644 mpw *_dv = (dv), *_dvl = (dvl); \
645 const mpw *_av = (av), *_avl = (avl); \
649 while (_dv < _dvl && _av < _avl) { \
651 _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc; \
653 _cc = _x >> MPW_BITS; \
655 MPX_UADDN(_dv, _dvl, _cc); \
658 extern void mpx_umlan(mpw */
*dv*/
, mpw */
*dvl*/
,
659 const mpw */
*av*/
, const mpw */
*avl*/
, mpw m
);
661 /* --- @mpx_usqr@ --- *
663 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
664 * @const mpw *av, *av@ = source vector base and limit
668 * Use: Performs unsigned integer squaring. The result vector must
669 * not overlap the source vector in any way.
672 extern void mpx_usqr(mpw */
*dv*/
, mpw */
*dvl*/
,
673 const mpw */
*av*/
, const mpw */
*avl*/
);
675 /* --- @mpx_udiv@ --- *
677 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
678 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
679 * @const mpw *dv, *dvl@ = divisor vector base and limit
680 * @mpw *sv, *svl@ = scratch workspace
684 * Use: Performs unsigned integer division. If the result overflows
685 * the quotient vector, high-order bits are discarded. (Clearly
686 * the remainder vector can't overflow.) The various vectors
687 * may not overlap in any way. Yes, I know it's a bit odd
688 * requiring the dividend to be in the result position but it
689 * does make some sense really. The remainder must have
690 * headroom for at least two extra words. The scratch space
691 * must be at least one word larger than the divisor.
694 extern void mpx_udiv(mpw */
*qv*/
, mpw */
*qvl*/
, mpw */
*rv*/
, mpw */
*rvl*/
,
695 const mpw */
*dv*/
, const mpw */
*dvl*/
,
696 mpw */
*sv*/
, mpw */
*svl*/
);
698 /* --- @mpx_udivn@ --- *
700 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
702 * @const mpw *rv, *rvl@ = dividend
703 * @mpw d@ = single-precision divisor
705 * Returns: Remainder after divison.
707 * Use: Performs a single-precision division operation.
710 extern mpw
mpx_udivn(mpw */
*qv*/
, mpw */
*qvl*/
,
711 const mpw */
*rv*/
, const mpw */
*rvl*/
, mpw
/*d*/);
713 /*----- Karatsuba multiplication algorithms -------------------------------*/
715 /* --- @MPK_THRESH@ --- *
717 * This is the limiting length for using Karatsuba algorithms. It's best to
718 * use the simpler classical multiplication method on numbers smaller than
719 * this. It is unsafe to make this constant less than four (i.e., the
720 * algorithms will fail).
723 #define MPK_THRESH 16
725 /* --- @mpx_kmul@ --- *
727 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
728 * @const mpw *av, *avl@ = pointer to first argument
729 * @const mpw *bv, *bvl@ = pointer to second argument
730 * @mpw *sv, *svl@ = pointer to scratch workspace
734 * Use: Multiplies two multiprecision integers using Karatsuba's
735 * algorithm. This is rather faster than traditional long
736 * multiplication (e.g., @mpx_umul@) on large numbers, although
737 * more expensive on small ones.
739 * The destination must be three times as large as the larger
740 * argument. The scratch space must be five times as large as
741 * the larger argument.
744 extern void mpx_kmul(mpw */
*dv*/
, mpw */
*dvl*/
,
745 const mpw */
*av*/
, const mpw */
*avl*/
,
746 const mpw */
*bv*/
, const mpw */
*bvl*/
,
747 mpw */
*sv*/
, mpw */
*svl*/
);
749 /* --- @mpx_ksqr@ --- *
751 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
752 * @const mpw *av, *avl@ = pointer to first argument
753 * @mpw *sv, *svl@ = pointer to scratch workspace
757 * Use: Squares a multiprecision integers using something similar to
758 * Karatsuba's multiplication algorithm. This is rather faster
759 * than traditional long multiplication (e.g., @mpx_umul@) on
760 * large numbers, although more expensive on small ones, and
761 * rather simpler than full-blown Karatsuba multiplication.
763 * The destination must be three times as large as the larger
764 * argument. The scratch space must be five times as large as
765 * the larger argument.
768 extern void mpx_ksqr(mpw */
*dv*/
, mpw */
*dvl*/
,
769 const mpw */
*av*/
, const mpw */
*avl*/
,
770 mpw */
*sv*/
, mpw */
*svl*/
);
772 /*----- That's all, folks -------------------------------------------------*/