3 * $Id: mpx.h,v 1.4 1999/11/17 18:04:43 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.4 1999/11/17 18:04:43 mdw
34 * Add two's complement support. Fix a bug in MPX_UMLAN.
36 * Revision 1.3 1999/11/13 01:51:29 mdw
37 * Minor interface changes. Should be stable now.
39 * Revision 1.2 1999/11/11 17:47:55 mdw
40 * Minor changes for different `mptypes.h' format.
42 * Revision 1.1 1999/09/03 08:41:12 mdw
54 /*----- The idea ----------------------------------------------------------*
56 * This file provides functions and macros which work on vectors of words as
57 * unsigned multiprecision integers. The interface works in terms of base
58 * and limit pointers (i.e., a pointer to the start of a vector, and a
59 * pointer just past its end) rather than base pointer and length, because
60 * that requires more arithmetic and state to work on.
62 * The interfaces are slightly bizarre in other ways. Try to use the
63 * higher-level functions where you can: they're rather better designed to
64 * actually be friendly and useful.
67 /*----- Header files ------------------------------------------------------*/
75 /*----- General manipulation ----------------------------------------------*/
77 /* --- @MPX_SHRINK@ --- *
79 * Arguments: @const mpw *v@ = pointer to vector of words
80 * @const mpw *vl@ = (updated) current limit of vector
82 * Use: Shrinks down the limit of a multiprecision integer vector.
85 #define MPX_SHRINK(v, vl) do { \
86 const mpw *_vv = (v), *_vvl = (vl); \
87 while (_vvl > _vv && !_vvl[-1]) \
92 /* --- @MPX_BITS@ --- *
94 * Arguments: @unsigned long b@ = result variable
95 * @const mpw *v@ = pointer to array of words
96 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
98 * Use: Calculates the number of bits in a multiprecision value.
101 #define MPX_BITS(b, v, vl) do { \
102 const mpw *_v = (v), *_vl = (vl); \
103 MPX_SHRINK(_v, _vl); \
107 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
109 unsigned _k = MPW_BITS / 2; \
121 /* --- @MPX_OCTETS@ --- *
123 * Arguments: @size_t o@ = result variable
124 * @const mpw *v, *vl@ = pointer to array of words
126 * Use: Calculates the number of octets in a multiprecision value.
129 #define MPX_OCTETS(o, v, vl) do { \
130 const mpw *_v = (v), *_vl = (vl); \
131 MPX_SHRINK(_v, _vl); \
135 size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
137 unsigned _k = MPW_BITS / 2; \
151 /* --- @MPX_COPY@ --- *
153 * Arguments: @dv, dvl@ = destination vector base and limit
154 * @av, avl@ = source vector base and limit
156 * Use: Copies a multiprecision integer.
159 #define MPX_COPY(dv, dvl, av, avl) do { \
160 mpw *_dv = (dv), *_dvl = (dvl); \
161 size_t _dn = _dvl - _dv; \
162 const mpw *_av = (av), *_avl = (avl); \
163 size_t _an = _avl - _av; \
166 memset(_dv, 0, MPWS(_dn - _an)); \
167 } else if (_an >= _dn) \
168 memmove(_dv, _av, MPWS(_dn)); \
170 memmove(_dv, _av, MPWS(_an)); \
171 memset(_dv + _an, 0, MPWS(_dn - _an)); \
175 /* --- @MPX_ZERO@ --- *
177 * Arguments: @v, vl@ = base and limit of vector to clear
179 * Use: Zeroes the area between the two vector pointers.
182 #define MPX_ZERO(v, vl) do { \
183 mpw *_v = (v), *_vl = (vl); \
185 memset(_v, 0, MPWS(_vl - _v)); \
188 /*----- Loading and storing -----------------------------------------------*/
190 /* --- @mpx_storel@ --- *
192 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
193 * @void *p@ = pointer to octet array
194 * @size_t sz@ = size of octet array
198 * Use: Stores an MP in an octet array, least significant octet
199 * first. High-end octets are silently discarded if there
200 * isn't enough space for them.
203 extern void mpx_storel(const mpw */
*v*/
, const mpw */
*vl*/
,
204 void */
*p*/
, size_t /*sz*/);
206 /* --- @mpx_loadl@ --- *
208 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
209 * @const void *p@ = pointer to octet array
210 * @size_t sz@ = size of octet array
214 * Use: Loads an MP in an octet array, least significant octet
215 * first. High-end octets are ignored if there isn't enough
219 extern void mpx_loadl(mpw */
*v*/
, mpw */
*vl*/
,
220 const void */
*p*/
, size_t /*sz*/);
222 /* --- @mpx_storeb@ --- *
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, most significant octet
231 * first. High-end octets are silently discarded if there
232 * isn't enough space for them.
235 extern void mpx_storeb(const mpw */
*v*/
, const mpw */
*vl*/
,
236 void */
*p*/
, size_t /*sz*/);
238 /* --- @mpx_loadb@ --- *
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, most significant octet
247 * first. High-end octets are ignored if there isn't enough
251 extern void mpx_loadb(mpw */
*v*/
, mpw */
*vl*/
,
252 const void */
*p*/
, size_t /*sz*/);
254 /*----- Logical shifting --------------------------------------------------*/
256 /* --- @mpx_lsl@ --- *
258 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
259 * @const mpw *av, *avl@ = source vector base and limit
260 * @size_t n@ = number of bit positions to shift by
264 * Use: Performs a logical shift left operation on an integer.
267 extern void mpx_lsl(mpw */
*dv*/
, mpw */
*dvl*/
,
268 const mpw */
*av*/
, const mpw */
*avl*/
,
271 /* --- @mpx_lsr@ --- *
273 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
274 * @const mpw *av, *avl@ = source vector base and limit
275 * @size_t n@ = number of bit positions to shift by
279 * Use: Performs a logical shift right operation on an integer.
282 extern void mpx_lsr(mpw */
*dv*/
, mpw */
*dvl*/
,
283 const mpw */
*av*/
, const mpw */
*avl*/
,
286 /*----- Unsigned arithmetic -----------------------------------------------*/
288 /* --- @mpx_2c@ --- *
290 * Arguments: @mpw *dv, *dvl@ = destination vector
291 * @const mpw *v, *vl@ = source vector
295 * Use: Calculates the two's complement of @v@.
298 extern void mpx_2c(mpw */
*dv*/
, mpw */
*dvl*/
,
299 const mpw */
*v*/
, const mpw */
*vl*/
);
301 /* --- @mpx_ucmp@ --- *
303 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
304 * @const mpw *bv, *bvl@ = second argument vector base and limit
306 * Returns: Less than, equal to, or greater than zero depending on
307 * whether @a@ is less than, equal to or greater than @b@,
310 * Use: Performs an unsigned integer comparison.
313 #define MPX_UCMP(av, avl, op, dv, dvl) \
314 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
316 extern int mpx_ucmp(const mpw */
*av*/
, const mpw */
*avl*/
,
317 const mpw */
*bv*/
, const mpw */
*bvl*/
);
319 /* --- @mpx_uadd@ --- *
321 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
322 * @const mpw *av, *avl@ = first addend vector base and limit
323 * @const mpw *bv, *bvl@ = second addend vector base and limit
327 * Use: Performs unsigned integer addition. If the result overflows
328 * the destination vector, high-order bits are discarded. This
329 * means that two's complement addition happens more or less for
330 * free, although that's more a side-effect than anything else.
331 * The result vector may be equal to either or both source
332 * vectors, but may not otherwise overlap them.
335 extern void mpx_uadd(mpw */
*dv*/
, mpw */
*dvl*/
,
336 const mpw */
*av*/
, const mpw */
*avl*/
,
337 const mpw */
*bv*/
, const mpw */
*bvl*/
);
339 /* --- @MPX_UADDN@ --- *
341 * Arguments: @dv, dvl@ = source and destination vector base and limit
344 * Use: Adds a small integer to a multiprecision number.
347 #define MPX_UADDN(dv, dvl, n) do { \
348 mpw *_ddv = (dv), *_ddvl = (dvl); \
351 while (_c && _ddv < _ddvl) { \
352 mpd _x = (mpd)*_ddv + (mpd)_c; \
354 _c = _x >> MPW_BITS; \
358 /* --- @mpx_usub@ --- *
360 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
361 * @const mpw *av, *avl@ = first argument vector base and limit
362 * @const mpw *bv, *bvl@ = second argument vector base and limit
366 * Use: Performs unsigned integer subtraction. If the result
367 * overflows the destination vector, high-order bits are
368 * discarded. This means that two's complement subtraction
369 * happens more or less for free, although that's more a side-
370 * effect than anything else. The result vector may be equal to
371 * either or both source vectors, but may not otherwise overlap
375 extern void mpx_usub(mpw */
*dv*/
, mpw */
*dvl*/
,
376 const mpw */
*av*/
, const mpw */
*avl*/
,
377 const mpw */
*bv*/
, const mpw */
*bvl*/
);
379 /* --- @MPX_USUBN@ --- *
381 * Arguments: @@dv, dvl@ = destination vector base and limit
384 * Use: Subtracts a small integer from a multiprecision number.
387 #define MPX_USUBN(dv, dvl, n) do { \
388 mpw *_ddv = (dv), *_ddvl = (dvl); \
391 while (_ddv < _ddvl) { \
392 mpd _x = (mpd)*_ddv - (mpd)_c; \
394 if (_x >> MPW_BITS) \
401 /* --- @mpx_umul@ --- *
403 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
404 * @const mpw *av, *avl@ = multiplicand vector base and limit
405 * @const mpw *bv, *bvl@ = multiplier vector base and limit
409 * Use: Performs unsigned integer multiplication. If the result
410 * overflows the desination vector, high-order bits are
411 * discarded. The result vector may not overlap the argument
412 * vectors in any way.
415 extern void mpx_umul(mpw */
*dv*/
, mpw */
*dvl*/
,
416 const mpw */
*av*/
, const mpw */
*avl*/
,
417 const mpw */
*bv*/
, const mpw */
*bvl*/
);
419 /* --- @MPX_UMULN@ --- *
421 * Arguments: @dv, dvl@ = destination vector base and limit
422 * @av, avl@ = multiplicand vector base and limit
425 * Use: Multiplies a multiprecision integer by a single-word value.
426 * The destination and source may be equal. The destination
427 * is completely cleared after use.
430 #define MPX_UMULN(dv, dvl, av, avl, m) do { \
431 mpw *_dv = (dv), *_dvl = (dvl); \
432 const mpw *_av = (av), *_avl = (avl); \
436 while (_av < _avl) { \
440 _x = (mpd)_m * (mpd)*_av++ + _c; \
442 _c = _x >> MPW_BITS; \
446 MPX_ZERO(_dv, _dvl); \
450 /* --- @MPX_UMLAN@ --- *
452 * Arguments: @dv, dvl@ = destination/accumulator vector base and limit
453 * @av, avl@ = multiplicand vector base and limit
456 * Use: Multiplies a multiprecision integer by a single-word value
457 * and adds the result to an accumulator.
460 #define MPX_UMLAN(dv, dvl, av, avl, m) do { \
461 mpw *_dv = (dv), *_dvl = (dvl); \
462 const mpw *_av = (av), *_avl = (avl); \
466 while (_av < _avl) { \
470 _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc; \
472 _cc = _x >> MPW_BITS; \
474 MPX_UADDN(_dv, _dvl, _cc); \
477 /* --- @mpx_usqr@ --- *
479 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
480 * @const mpw *av, *av@ = source vector base and limit
484 * Use: Performs unsigned integer squaring. The result vector must
485 * not overlap the source vector in any way.
488 extern void mpx_usqr(mpw */
*dv*/
, mpw */
*dvl*/
,
489 const mpw */
*av*/
, const mpw */
*avl*/
);
491 /* --- @mpx_udiv@ --- *
493 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
494 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
495 * @const mpw *dv, *dvl@ = divisor vector base and limit
496 * @mpw *sv, *svl@ = scratch workspace
500 * Use: Performs unsigned integer division. If the result overflows
501 * the quotient vector, high-order bits are discarded. (Clearly
502 * the remainder vector can't overflow.) The various vectors
503 * may not overlap in any way. Yes, I know it's a bit odd
504 * requiring the dividend to be in the result position but it
505 * does make some sense really. The remainder must have
506 * headroom for at least two extra words. The scratch space
507 * must be at least one word larger than the divisor.
510 extern void mpx_udiv(mpw */
*qv*/
, mpw */
*qvl*/
, mpw */
*rv*/
, mpw */
*rvl*/
,
511 const mpw */
*dv*/
, const mpw */
*dvl*/
,
512 mpw */
*sv*/
, mpw */
*svl*/
);
514 /*----- That's all, folks -------------------------------------------------*/