3 * $Id: mpx.h,v 1.3 1999/11/13 01:51:29 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.3 1999/11/13 01:51:29 mdw
34 * Minor interface changes. Should be stable now.
36 * Revision 1.2 1999/11/11 17:47:55 mdw
37 * Minor changes for different `mptypes.h' format.
39 * Revision 1.1 1999/09/03 08:41:12 mdw
51 /*----- The idea ----------------------------------------------------------*
53 * This file provides functions and macros which work on vectors of words as
54 * unsigned multiprecision integers. The interface works in terms of base
55 * and limit pointers (i.e., a pointer to the start of a vector, and a
56 * pointer just past its end) rather than base pointer and length, because
57 * that requires more arithmetic and state to work on.
59 * The interfaces are slightly bizarre in other ways. Try to use the
60 * higher-level functions where you can: they're rather better designed to
61 * actually be friendly and useful.
64 /*----- Header files ------------------------------------------------------*/
72 /*----- General manipulation ----------------------------------------------*/
74 /* --- @MPX_SHRINK@ --- *
76 * Arguments: @const mpw *v@ = pointer to vector of words
77 * @const mpw *vl@ = (updated) current limit of vector
79 * Use: Shrinks down the limit of a multiprecision integer vector.
82 #define MPX_SHRINK(v, vl) do { \
83 const mpw *_vv = (v), *_vvl = (vl); \
84 while (_vvl > _vv && !_vvl[-1]) \
89 /* --- @MPX_BITS@ --- *
91 * Arguments: @unsigned long b@ = result variable
92 * @const mpw *v@ = pointer to array of words
93 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
95 * Use: Calculates the number of bits in a multiprecision value.
98 #define MPX_BITS(b, v, vl) do { \
99 const mpw *_v = (v), *_vl = (vl); \
100 MPX_SHRINK(_v, _vl); \
104 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
106 unsigned _k = MPW_BITS / 2; \
118 /* --- @MPX_OCTETS@ --- *
120 * Arguments: @size_t o@ = result variable
121 * @const mpw *v, *vl@ = pointer to array of words
123 * Use: Calculates the number of octets in a multiprecision value.
126 #define MPX_OCTETS(o, v, vl) do { \
127 const mpw *_v = (v), *_vl = (vl); \
128 MPX_SHRINK(_v, _vl); \
132 size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
134 unsigned _k = MPW_BITS / 2; \
148 /* --- @MPX_COPY@ --- *
150 * Arguments: @dv, dvl@ = destination vector base and limit
151 * @av, avl@ = source vector base and limit
153 * Use: Copies a multiprecision integer.
156 #define MPX_COPY(dv, dvl, av, avl) do { \
157 mpw *_dv = (dv), *_dvl = (dvl); \
158 size_t _dn = _dvl - _dv; \
159 const mpw *_av = (av), *_avl = (avl); \
160 size_t _an = _avl - _av; \
163 memset(_dv, 0, MPWS(_dn - _an)); \
164 } else if (_an >= _dn) \
165 memmove(_dv, _av, MPWS(_dn)); \
167 memmove(_dv, _av, MPWS(_an)); \
168 memset(_dv + _an, 0, MPWS(_dn - _an)); \
172 /* --- @MPX_ZERO@ --- *
174 * Arguments: @v, vl@ = base and limit of vector to clear
176 * Use: Zeroes the area between the two vector pointers.
179 #define MPX_ZERO(v, vl) do { \
180 mpw *_v = (v), *_vl = (vl); \
182 memset(_v, 0, MPWS(_vl - _v)); \
185 /*----- Loading and storing -----------------------------------------------*/
187 /* --- @mpx_storel@ --- *
189 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
190 * @void *p@ = pointer to octet array
191 * @size_t sz@ = size of octet array
195 * Use: Stores an MP in an octet array, least significant octet
196 * first. High-end octets are silently discarded if there
197 * isn't enough space for them.
200 extern void mpx_storel(const mpw */
*v*/
, const mpw */
*vl*/
,
201 void */
*p*/
, size_t /*sz*/);
203 /* --- @mpx_loadl@ --- *
205 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
206 * @const void *p@ = pointer to octet array
207 * @size_t sz@ = size of octet array
211 * Use: Loads an MP in an octet array, least significant octet
212 * first. High-end octets are ignored if there isn't enough
216 extern void mpx_loadl(mpw */
*v*/
, mpw */
*vl*/
,
217 const void */
*p*/
, size_t /*sz*/);
219 /* --- @mpx_storeb@ --- *
221 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
222 * @void *p@ = pointer to octet array
223 * @size_t sz@ = size of octet array
227 * Use: Stores an MP in an octet array, most significant octet
228 * first. High-end octets are silently discarded if there
229 * isn't enough space for them.
232 extern void mpx_storeb(const mpw */
*v*/
, const mpw */
*vl*/
,
233 void */
*p*/
, size_t /*sz*/);
235 /* --- @mpx_loadb@ --- *
237 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
238 * @const void *p@ = pointer to octet array
239 * @size_t sz@ = size of octet array
243 * Use: Loads an MP in an octet array, most significant octet
244 * first. High-end octets are ignored if there isn't enough
248 extern void mpx_loadb(mpw */
*v*/
, mpw */
*vl*/
,
249 const void */
*p*/
, size_t /*sz*/);
251 /*----- Logical shifting --------------------------------------------------*/
253 /* --- @mpx_lsl@ --- *
255 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
256 * @const mpw *av, *avl@ = source vector base and limit
257 * @size_t n@ = number of bit positions to shift by
261 * Use: Performs a logical shift left operation on an integer.
264 extern void mpx_lsl(mpw */
*dv*/
, mpw */
*dvl*/
,
265 const mpw */
*av*/
, const mpw */
*avl*/
,
268 /* --- @mpx_lsr@ --- *
270 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
271 * @const mpw *av, *avl@ = source vector base and limit
272 * @size_t n@ = number of bit positions to shift by
276 * Use: Performs a logical shift right operation on an integer.
279 extern void mpx_lsr(mpw */
*dv*/
, mpw */
*dvl*/
,
280 const mpw */
*av*/
, const mpw */
*avl*/
,
283 /*----- Unsigned arithmetic -----------------------------------------------*/
285 /* --- @mpx_ucmp@ --- *
287 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
288 * @const mpw *bv, *bvl@ = second argument vector base and limit
290 * Returns: Less than, equal to, or greater than zero depending on
291 * whether @a@ is less than, equal to or greater than @b@,
294 * Use: Performs an unsigned integer comparison.
297 #define MPX_UCMP(av, avl, op, dv, dvl) \
298 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
300 extern int mpx_ucmp(const mpw */
*av*/
, const mpw */
*avl*/
,
301 const mpw */
*bv*/
, const mpw */
*bvl*/
);
303 /* --- @mpx_uadd@ --- *
305 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
306 * @const mpw *av, *avl@ = first addend vector base and limit
307 * @const mpw *bv, *bvl@ = second addend vector base and limit
311 * Use: Performs unsigned integer addition. If the result overflows
312 * the destination vector, high-order bits are discarded. This
313 * means that two's complement addition happens more or less for
314 * free, although that's more a side-effect than anything else.
315 * The result vector may be equal to either or both source
316 * vectors, but may not otherwise overlap them.
319 extern void mpx_uadd(mpw */
*dv*/
, mpw */
*dvl*/
,
320 const mpw */
*av*/
, const mpw */
*avl*/
,
321 const mpw */
*bv*/
, const mpw */
*bvl*/
);
323 /* --- @MPX_UADDN@ --- *
325 * Arguments: @dv, dvl@ = source and destination vector base and limit
328 * Use: Adds a small integer to a multiprecision number.
331 #define MPX_UADDN(dv, dvl, n) do { \
332 mpw *_ddv = (dv), *_ddvl = (dvl); \
335 while (_c && _ddv < _ddvl) { \
336 mpd _x = (mpd)*_ddv + (mpd)_c; \
338 _c = _x >> MPW_BITS; \
342 /* --- @mpx_usub@ --- *
344 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
345 * @const mpw *av, *avl@ = first argument vector base and limit
346 * @const mpw *bv, *bvl@ = second argument vector base and limit
350 * Use: Performs unsigned integer subtraction. If the result
351 * overflows the destination vector, high-order bits are
352 * discarded. This means that two's complement subtraction
353 * happens more or less for free, although that's more a side-
354 * effect than anything else. The result vector may be equal to
355 * either or both source vectors, but may not otherwise overlap
359 extern void mpx_usub(mpw */
*dv*/
, mpw */
*dvl*/
,
360 const mpw */
*av*/
, const mpw */
*avl*/
,
361 const mpw */
*bv*/
, const mpw */
*bvl*/
);
363 /* --- @MPX_USUBN@ --- *
365 * Arguments: @@dv, dvl@ = destination vector base and limit
368 * Use: Subtracts a small integer from a multiprecision number.
371 #define MPX_USUBN(dv, dvl, n) do { \
372 mpw *_ddv = (dv), *_ddvl = (dvl); \
375 while (_ddv < _ddvl) { \
376 mpd _x = (mpd)*_ddv - (mpd)_c; \
378 if (_x >> MPW_BITS) \
385 /* --- @mpx_umul@ --- *
387 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
388 * @const mpw *av, *avl@ = multiplicand vector base and limit
389 * @const mpw *bv, *bvl@ = multiplier vector base and limit
393 * Use: Performs unsigned integer multiplication. If the result
394 * overflows the desination vector, high-order bits are
395 * discarded. The result vector may not overlap the argument
396 * vectors in any way.
399 extern void mpx_umul(mpw */
*dv*/
, mpw */
*dvl*/
,
400 const mpw */
*av*/
, const mpw */
*avl*/
,
401 const mpw */
*bv*/
, const mpw */
*bvl*/
);
403 /* --- @MPX_UMULN@ --- *
405 * Arguments: @dv, dvl@ = destination vector base and limit
406 * @av, avl@ = multiplicand vector base and limit
409 * Use: Multiplies a multiprecision integer by a single-word value.
410 * The destination and source may be equal. The destination
411 * is completely cleared after use.
414 #define MPX_UMULN(dv, dvl, av, avl, m) do { \
415 mpw *_dv = (dv), *_dvl = (dvl); \
416 const mpw *_av = (av), *_avl = (avl); \
420 while (_av < _avl) { \
424 _x = (mpd)_m * (mpd)*_av++ + _c; \
426 _c = _x >> MPW_BITS; \
430 MPX_ZERO(_dv, _dvl); \
434 /* --- @MPX_UMLAN@ --- *
436 * Arguments: @dv, dvl@ = destination/accumulator vector base and limit
437 * @av, avl@ = multiplicand vector base and limit
440 * Use: Multiplies a multiprecision integer by a single-word value
441 * and adds the result to an accumulator.
444 #define MPX_UMLAN(dv, dvl, av, avl, m) do { \
445 mpw *_dv = (dv), *_dvl = (dvl); \
446 const mpw *_av = (av), *_avl = (avl); \
450 while (_av < _avl) { \
454 _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _c; \
456 _c = _x >> MPW_BITS; \
458 MPX_UADDN(_dv, _dvl, _c); \
461 /* --- @mpx_usqr@ --- *
463 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
464 * @const mpw *av, *av@ = source vector base and limit
468 * Use: Performs unsigned integer squaring. The result vector must
469 * not overlap the source vector in any way.
472 extern void mpx_usqr(mpw */
*dv*/
, mpw */
*dvl*/
,
473 const mpw */
*av*/
, const mpw */
*avl*/
);
475 /* --- @mpx_udiv@ --- *
477 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
478 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
479 * @const mpw *dv, *dvl@ = divisor vector base and limit
480 * @mpw *sv, *svl@ = scratch workspace
484 * Use: Performs unsigned integer division. If the result overflows
485 * the quotient vector, high-order bits are discarded. (Clearly
486 * the remainder vector can't overflow.) The various vectors
487 * may not overlap in any way. Yes, I know it's a bit odd
488 * requiring the dividend to be in the result position but it
489 * does make some sense really. The remainder must have
490 * headroom for at least two extra words. The scratch space
491 * must be at least two words larger than twice the size of the
495 extern void mpx_udiv(mpw */
*qv*/
, mpw */
*qvl*/
, mpw */
*rv*/
, mpw */
*rvl*/
,
496 const mpw */
*dv*/
, const mpw */
*dvl*/
,
497 mpw */
*sv*/
, mpw */
*svl*/
);
499 /*----- That's all, folks -------------------------------------------------*/