3 * $Id: mpx.h,v 1.6 1999/12/10 23:23:51 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.6 1999/12/10 23:23:51 mdw
34 * Karatsuba-Ofman multiplication algorithm.
36 * Revision 1.5 1999/11/20 22:23:27 mdw
37 * Add function versions of some low-level macros with wider use.
39 * Revision 1.4 1999/11/17 18:04:43 mdw
40 * Add two's complement support. Fix a bug in MPX_UMLAN.
42 * Revision 1.3 1999/11/13 01:51:29 mdw
43 * Minor interface changes. Should be stable now.
45 * Revision 1.2 1999/11/11 17:47:55 mdw
46 * Minor changes for different `mptypes.h' format.
48 * Revision 1.1 1999/09/03 08:41:12 mdw
53 #ifndef CATACOMB_MPX_H
54 #define CATACOMB_MPX_H
60 /*----- The idea ----------------------------------------------------------*
62 * This file provides functions and macros which work on vectors of words as
63 * unsigned multiprecision integers. The interface works in terms of base
64 * and limit pointers (i.e., a pointer to the start of a vector, and a
65 * pointer just past its end) rather than base pointer and length, because
66 * that requires more arithmetic and state to work on.
68 * The interfaces are slightly bizarre in other ways. Try to use the
69 * higher-level functions where you can: they're rather better designed to
70 * actually be friendly and useful.
73 /*----- Header files ------------------------------------------------------*/
77 #ifndef CATACOMB_MPW_H
81 /*----- General manipulation ----------------------------------------------*/
83 /* --- @MPX_SHRINK@ --- *
85 * Arguments: @const mpw *v@ = pointer to vector of words
86 * @const mpw *vl@ = (updated) current limit of vector
88 * Use: Shrinks down the limit of a multiprecision integer vector.
91 #define MPX_SHRINK(v, vl) do { \
92 const mpw *_vv = (v), *_vvl = (vl); \
93 while (_vvl > _vv && !_vvl[-1]) \
98 /* --- @MPX_BITS@ --- *
100 * Arguments: @unsigned long b@ = result variable
101 * @const mpw *v@ = pointer to array of words
102 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
104 * Use: Calculates the number of bits in a multiprecision value.
107 #define MPX_BITS(b, v, vl) do { \
108 const mpw *_v = (v), *_vl = (vl); \
109 MPX_SHRINK(_v, _vl); \
113 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
115 unsigned _k = MPW_BITS / 2; \
127 /* --- @MPX_OCTETS@ --- *
129 * Arguments: @size_t o@ = result variable
130 * @const mpw *v, *vl@ = pointer to array of words
132 * Use: Calculates the number of octets in a multiprecision value.
135 #define MPX_OCTETS(o, v, vl) do { \
136 const mpw *_v = (v), *_vl = (vl); \
137 MPX_SHRINK(_v, _vl); \
141 size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
143 unsigned _k = MPW_BITS / 2; \
157 /* --- @MPX_COPY@ --- *
159 * Arguments: @dv, dvl@ = destination vector base and limit
160 * @av, avl@ = source vector base and limit
162 * Use: Copies a multiprecision integer.
165 #define MPX_COPY(dv, dvl, av, avl) do { \
166 mpw *_dv = (dv), *_dvl = (dvl); \
167 size_t _dn = _dvl - _dv; \
168 const mpw *_av = (av), *_avl = (avl); \
169 size_t _an = _avl - _av; \
172 memset(_dv, 0, MPWS(_dn - _an)); \
173 } else if (_an >= _dn) \
174 memmove(_dv, _av, MPWS(_dn)); \
176 memmove(_dv, _av, MPWS(_an)); \
177 memset(_dv + _an, 0, MPWS(_dn - _an)); \
181 /* --- @MPX_ZERO@ --- *
183 * Arguments: @v, vl@ = base and limit of vector to clear
185 * Use: Zeroes the area between the two vector pointers.
188 #define MPX_ZERO(v, vl) do { \
189 mpw *_v = (v), *_vl = (vl); \
191 memset(_v, 0, MPWS(_vl - _v)); \
194 /*----- Loading and storing -----------------------------------------------*/
196 /* --- @mpx_storel@ --- *
198 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
199 * @void *p@ = pointer to octet array
200 * @size_t sz@ = size of octet array
204 * Use: Stores an MP in an octet array, least significant octet
205 * first. High-end octets are silently discarded if there
206 * isn't enough space for them.
209 extern void mpx_storel(const mpw */
*v*/
, const mpw */
*vl*/
,
210 void */
*p*/
, size_t /*sz*/);
212 /* --- @mpx_loadl@ --- *
214 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
215 * @const void *p@ = pointer to octet array
216 * @size_t sz@ = size of octet array
220 * Use: Loads an MP in an octet array, least significant octet
221 * first. High-end octets are ignored if there isn't enough
225 extern void mpx_loadl(mpw */
*v*/
, mpw */
*vl*/
,
226 const void */
*p*/
, size_t /*sz*/);
228 /* --- @mpx_storeb@ --- *
230 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
231 * @void *p@ = pointer to octet array
232 * @size_t sz@ = size of octet array
236 * Use: Stores an MP in an octet array, most significant octet
237 * first. High-end octets are silently discarded if there
238 * isn't enough space for them.
241 extern void mpx_storeb(const mpw */
*v*/
, const mpw */
*vl*/
,
242 void */
*p*/
, size_t /*sz*/);
244 /* --- @mpx_loadb@ --- *
246 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
247 * @const void *p@ = pointer to octet array
248 * @size_t sz@ = size of octet array
252 * Use: Loads an MP in an octet array, most significant octet
253 * first. High-end octets are ignored if there isn't enough
257 extern void mpx_loadb(mpw */
*v*/
, mpw */
*vl*/
,
258 const void */
*p*/
, size_t /*sz*/);
260 /*----- Logical shifting --------------------------------------------------*/
262 /* --- @mpx_lsl@ --- *
264 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
265 * @const mpw *av, *avl@ = source vector base and limit
266 * @size_t n@ = number of bit positions to shift by
270 * Use: Performs a logical shift left operation on an integer.
273 extern void mpx_lsl(mpw */
*dv*/
, mpw */
*dvl*/
,
274 const mpw */
*av*/
, const mpw */
*avl*/
,
277 /* --- @mpx_lsr@ --- *
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 right operation on an integer.
288 extern void mpx_lsr(mpw */
*dv*/
, mpw */
*dvl*/
,
289 const mpw */
*av*/
, const mpw */
*avl*/
,
292 /*----- Unsigned arithmetic -----------------------------------------------*/
294 /* --- @mpx_2c@ --- *
296 * Arguments: @mpw *dv, *dvl@ = destination vector
297 * @const mpw *v, *vl@ = source vector
301 * Use: Calculates the two's complement of @v@.
304 extern void mpx_2c(mpw */
*dv*/
, mpw */
*dvl*/
,
305 const mpw */
*v*/
, const mpw */
*vl*/
);
307 /* --- @mpx_ucmp@ --- *
309 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
310 * @const mpw *bv, *bvl@ = second argument vector base and limit
312 * Returns: Less than, equal to, or greater than zero depending on
313 * whether @a@ is less than, equal to or greater than @b@,
316 * Use: Performs an unsigned integer comparison.
319 #define MPX_UCMP(av, avl, op, dv, dvl) \
320 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
322 extern int mpx_ucmp(const mpw */
*av*/
, const mpw */
*avl*/
,
323 const mpw */
*bv*/
, const mpw */
*bvl*/
);
325 /* --- @mpx_uadd@ --- *
327 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
328 * @const mpw *av, *avl@ = first addend vector base and limit
329 * @const mpw *bv, *bvl@ = second addend vector base and limit
333 * Use: Performs unsigned integer addition. If the result overflows
334 * the destination vector, high-order bits are discarded. This
335 * means that two's complement addition happens more or less for
336 * free, although that's more a side-effect than anything else.
337 * The result vector may be equal to either or both source
338 * vectors, but may not otherwise overlap them.
341 extern void mpx_uadd(mpw */
*dv*/
, mpw */
*dvl*/
,
342 const mpw */
*av*/
, const mpw */
*avl*/
,
343 const mpw */
*bv*/
, const mpw */
*bvl*/
);
345 /* --- @mpx_uaddn@ --- *
347 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
348 * @mpw n@ = other addend
352 * Use: Adds a small integer to a multiprecision number.
355 #define MPX_UADDN(dv, dvl, n) do { \
356 mpw *_ddv = (dv), *_ddvl = (dvl); \
359 while (_c && _ddv < _ddvl) { \
360 mpd _x = (mpd)*_ddv + (mpd)_c; \
362 _c = _x >> MPW_BITS; \
366 extern void mpx_uaddn(mpw */
*dv*/
, mpw */
*dvl*/
, mpw
/*n*/);
368 /* --- @mpx_usub@ --- *
370 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
371 * @const mpw *av, *avl@ = first argument vector base and limit
372 * @const mpw *bv, *bvl@ = second argument vector base and limit
376 * Use: Performs unsigned integer subtraction. If the result
377 * overflows the destination vector, high-order bits are
378 * discarded. This means that two's complement subtraction
379 * happens more or less for free, although that's more a side-
380 * effect than anything else. The result vector may be equal to
381 * either or both source vectors, but may not otherwise overlap
385 extern void mpx_usub(mpw */
*dv*/
, mpw */
*dvl*/
,
386 const mpw */
*av*/
, const mpw */
*avl*/
,
387 const mpw */
*bv*/
, const mpw */
*bvl*/
);
389 /* --- @mpx_usubn@ --- *
391 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
396 * Use: Subtracts a small integer from a multiprecision number.
399 #define MPX_USUBN(dv, dvl, n) do { \
400 mpw *_ddv = (dv), *_ddvl = (dvl); \
403 while (_ddv < _ddvl) { \
404 mpd _x = (mpd)*_ddv - (mpd)_c; \
406 if (_x >> MPW_BITS) \
413 extern void mpx_usubn(mpw */
*dv*/
, mpw */
*dvl*/
, mpw
/*n*/);
415 /* --- @mpx_umul@ --- *
417 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
418 * @const mpw *av, *avl@ = multiplicand vector base and limit
419 * @const mpw *bv, *bvl@ = multiplier vector base and limit
423 * Use: Performs unsigned integer multiplication. If the result
424 * overflows the desination vector, high-order bits are
425 * discarded. The result vector may not overlap the argument
426 * vectors in any way.
429 extern void mpx_umul(mpw */
*dv*/
, mpw */
*dvl*/
,
430 const mpw */
*av*/
, const mpw */
*avl*/
,
431 const mpw */
*bv*/
, const mpw */
*bvl*/
);
433 /* --- @mpx_umuln@ --- *
435 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
436 * @const mpw *av, *avl@ = multiplicand vector base and limit
437 * @mpw m@ = multiplier
441 * Use: Multiplies a multiprecision integer by a single-word value.
442 * The destination and source may be equal. The destination
443 * is completely cleared after use.
446 #define MPX_UMULN(dv, dvl, av, avl, m) do { \
447 mpw *_dv = (dv), *_dvl = (dvl); \
448 const mpw *_av = (av), *_avl = (avl); \
452 while (_av < _avl) { \
456 _x = (mpd)_m * (mpd)*_av++ + _c; \
458 _c = _x >> MPW_BITS; \
462 MPX_ZERO(_dv, _dvl); \
466 extern void mpx_umuln(mpw */
*dv*/
, mpw */
*dvl*/
,
467 const mpw */
*av*/
, const mpw */
*avl*/
, mpw m
);
469 /* --- @mpx_umlan@ --- *
471 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
472 * @const mpw *av, *avl@ = multiplicand vector base and limit
473 * @mpw m@ = multiplier
477 * Use: Multiplies a multiprecision integer by a single-word value
478 * and adds the result to an accumulator.
481 #define MPX_UMLAN(dv, dvl, av, avl, m) do { \
482 mpw *_dv = (dv), *_dvl = (dvl); \
483 const mpw *_av = (av), *_avl = (avl); \
487 while (_av < _avl) { \
491 _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc; \
493 _cc = _x >> MPW_BITS; \
495 MPX_UADDN(_dv, _dvl, _cc); \
498 extern void mpx_umlan(mpw */
*dv*/
, mpw */
*dvl*/
,
499 const mpw */
*av*/
, const mpw */
*avl*/
, mpw m
);
501 /* --- @mpx_usqr@ --- *
503 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
504 * @const mpw *av, *av@ = source vector base and limit
508 * Use: Performs unsigned integer squaring. The result vector must
509 * not overlap the source vector in any way.
512 extern void mpx_usqr(mpw */
*dv*/
, mpw */
*dvl*/
,
513 const mpw */
*av*/
, const mpw */
*avl*/
);
515 /* --- @mpx_kmul@ --- *
517 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
518 * @const mpw *av, *avl@ = pointer to first argument
519 * @const mpw *bv, *bvl@ = pointer to second argument
520 * @mpw *sv, *svl@ = pointer to scratch workspace
524 * Use: Multiplies two multiprecision integers using Karatsuba's
525 * algorithm. This is rather faster than traditional long
526 * multiplication (e.g., @mpx_umul@) on large numbers, although
527 * more expensive on small ones.
529 * The destination and scratch buffers must be twice as large as
530 * the larger argument.
533 #define KARATSUBA_CUTOFF 16
534 #define KARATSUBA_SLOP 32
536 extern void mpx_kmul(mpw */
*dv*/
, mpw */
*dvl*/
,
537 const mpw */
*av*/
, const mpw */
*avl*/
,
538 const mpw */
*bv*/
, const mpw */
*bvl*/
,
539 mpw */
*sv*/
, mpw */
*svl*/
);
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 /*----- That's all, folks -------------------------------------------------*/