3 * $Id: mpx.h,v 1.2 1999/11/11 17:47:55 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.2 1999/11/11 17:47:55 mdw
34 * Minor changes for different `mptypes.h' format.
36 * Revision 1.1 1999/09/03 08:41:12 mdw
48 /*----- The idea ----------------------------------------------------------*
50 * This file provides functions and macros which work on vectors of words as
51 * unsigned multiprecision integers. The interface works in terms of base
52 * and limit pointers (i.e., a pointer to the start of a vector, and a
53 * pointer just past its end) rather than base pointer and length, because
54 * that requires more arithmetic and state to work on.
56 * The interfaces are slightly bizarre in other ways. Try to use the
57 * higher-level functions where you can: they're rather better designed to
58 * actually be friendly and useful.
61 /*----- Header files ------------------------------------------------------*/
69 /*----- Basic macros ------------------------------------------------------*/
73 * Arguments: @x@ = an unsigned value
75 * Use: Expands to the value of @x@ masked and typecast to a
76 * multiprecision integer word.
79 #define MPW(x) ((mpw)((x) & MPW_MAX))
83 * Arguments: @n@ = number of words
85 * Use: Expands to the number of bytes occupied by a given number of
89 #define MPWS(n) ((n) * sizeof(mpw))
91 /*----- General manipulation ----------------------------------------------*/
93 /* --- @MPX_SHRINK@ --- *
95 * Arguments: @const mpw *v@ = pointer to vector of words
96 * @const mpw *vl@ = (updated) current limit of vector
98 * Use: Shrinks down the limit of a multiprecision integer vector.
101 #define MPX_SHRINK(v, vl) do { \
102 const mpw *_v = (v), *_vl = (vl); \
103 while (_vl > _v && *--_vl) \
108 /* --- @MPX_BITS@ --- *
110 * Arguments: @unsigned long b@ = result variable
111 * @const mpw *v@ = pointer to array of words
112 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
114 * Use: Calculates the number of bits in a multiprecision value.
117 #define MPX_BITS(b, v, vl) do { \
118 const mpw *_v = (v), *_vl = (vl); \
122 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
124 unsigned _k = MPW_BITS / 2; \
136 /* --- @MPX_OCTETS@ --- *
138 * Arguments: @size_t o@ = result variable
139 * @const mpw *v@ = pointer to array of words
140 * @size_t len@ = length of the array (from @MPX_LEN@)
142 * Use: Calculates the number of octets in a multiprecision value.
145 #define MPX_OCTETS(o, v, len) do { \
146 const mpw *_v = (v), *_vl = (vl); \
150 _size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
152 unsigned _k = MPW_BITS / 2; \
166 /* --- @MPX_COPY@ --- *
168 * Arguments: @dv, dvl@ = destination vector base and limit
169 * @av, avl@ = source vector base and limit
171 * Use: Copies a multiprecision integer.
174 #define MPX_COPY(dv, dvl, av, dvl) do { \
176 size_t _dn = (dvl) - _dv; \
177 const mpw *_av = (av); \
178 size_t _an = (avl) - _av; \
181 memset(_avl, 0, MPWS(_dn - _an)); \
182 } else if (_an >= _dn) \
183 memmove(_dv, _av, MPWS(_dn)); \
185 memmove(_dv, _av, MPWS(_an)); \
186 memset(_dv + _an, 0, MPWS(_dn - _an)); \
190 /* --- @MPX_ZERO@ --- *
192 * Arguments: @v, vl@ = base and limit of vector to clear
194 * Use: Zeroes the area between the two vector pointers.
197 #define MPX_ZERO(v, vl) { \
198 mpw *_v = (v), *_vl = (vl); \
199 memset(_v, 0, MPWS(_vl - _v)); \
202 /*----- Loading and storing -----------------------------------------------*/
204 /* --- @mpx_storel@ --- *
206 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
207 * @octet *p@ = pointer to octet array
208 * @size_t sz@ = size of octet array
212 * Use: Stores an MP in an octet array, least significant octet
213 * first. High-end octets are silently discarded if there
214 * isn't enough space for them.
217 extern void mpx_storel(const mpw */
*v*/
, const mpw */
*vl*/
,
218 octet */
*p*/
, size_t /*sz*/);
220 /* --- @mpx_loadl@ --- *
222 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
223 * @const octet *p@ = pointer to octet array
224 * @size_t sz@ = size of octet array
228 * Use: Loads an MP in an octet array, least significant octet
229 * first. High-end octets are ignored if there isn't enough
233 extern void mpx_loadl(mpw */
*v*/
, mpw */
*vl*/
,
234 const octet */
*p*/
, size_t /*sz*/);
236 /* --- @mpx_storeb@ --- *
238 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
239 * @octet *p@ = pointer to octet array
240 * @size_t sz@ = size of octet array
244 * Use: Stores an MP in an octet array, most significant octet
245 * first. High-end octets are silently discarded if there
246 * isn't enough space for them.
249 extern void mpx_storeb(const mpw */
*v*/
, const mpw */
*vl*/
,
250 octet */
*p*/
, size_t /*sz*/);
252 /* --- @mpx_loadb@ --- *
254 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
255 * @const octet *p@ = pointer to octet array
256 * @size_t sz@ = size of octet array
260 * Use: Loads an MP in an octet array, most significant octet
261 * first. High-end octets are ignored if there isn't enough
265 extern void mpx_loadb(mpw */
*v*/
, mpw */
*vl*/
,
266 const octet */
*p*/
, size_t /*sz*/);
268 /*----- Logical shifting --------------------------------------------------*/
270 /* --- @mpx_lsl@ --- *
272 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
273 * @const mpw *av, *avl@ = source vector base and limit
274 * @size_t n@ = number of bit positions to shift by
278 * Use: Performs a logical shift left operation on an integer.
281 extern void mpx_lsl(mpw */
*dv*/
, mpw */
*dvl*/
,
282 const mpw */
*av*/
, const mpw */
*avl*/
,
285 /* --- @mpx_lsr@ --- *
287 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
288 * @const mpw *av, *avl@ = source vector base and limit
289 * @size_t n@ = number of bit positions to shift by
293 * Use: Performs a logical shift right operation on an integer.
296 extern void mpx_lsr(mpw */
*dv*/
, mpw */
*dvl*/
,
297 const mpw */
*av*/
, const mpw */
*avl*/
,
300 /*----- Unsigned arithmetic -----------------------------------------------*/
302 /* --- @mpx_ucmp@ --- *
304 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
305 * @const mpw *bv, *bvl@ = second argument vector base and limit
307 * Returns: Less than, equal to, or greater than zero depending on
308 * whether @a@ is less than, equal to or greater than @b@,
311 * Use: Performs an unsigned integer comparison.
314 #define MPX_UCMP(av, avl, op, dv, dvl) \
315 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
317 extern int mpx_ucmp(const mpw */
*av*/
, const mpw */
*avl*/
,
318 const mpw */
*bv*/
, const mpw */
*bvl*/
);
320 /* --- @mpx_uadd@ --- *
322 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
323 * @const mpw *av, *avl@ = first addend vector base and limit
324 * @const mpw *bv, *bvl@ = second addend vector base and limit
328 * Use: Performs unsigned integer addition. If the result overflows
329 * the destination vector, high-order bits are discarded. This
330 * means that two's complement addition happens more or less for
331 * free, although that's more a side-effect than anything else.
332 * The result vector may be equal to either or both source
333 * vectors, but may not otherwise overlap them.
336 extern void mpx_uadd(mpw */
*dv*/
, mpw */
*dvl*/
,
337 const mpw */
*av*/
, const mpw */
*avl*/
,
338 const mpw */
*bv*/
, const mpw */
*bvl*/
);
340 /* --- @mpx_usub@ --- *
342 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
343 * @const mpw *av, *avl@ = first argument vector base and limit
344 * @const mpw *bv, *bvl@ = second argument vector base and limit
348 * Use: Performs unsigned integer subtraction. If the result
349 * overflows the destination vector, high-order bits are
350 * discarded. This means that two's complement subtraction
351 * happens more or less for free, althuogh that's more a side-
352 * effect than anything else. The result vector may be equal to
353 * either or both source vectors, but may not otherwise overlap
357 extern void mpx_usub(mpw */
*dv*/
, mpw */
*dvl*/
,
358 const mpw */
*av*/
, const mpw */
*avl*/
,
359 const mpw */
*bv*/
, const mpw */
*bvl*/
);
361 /* --- @MPX_UMULN@ --- *
363 * Arguments: @dv, dvl@ = destination vector base and limit
364 * @av, avl@ = multiplicand vector base and limit
367 * Use: Multiplies a multiprecision integer by a single-word value.
368 * The destination and source may be equal. The destination
369 * is completely cleared after use.
372 #define MPX_UMULN(dv, dvl, av, avl, m) do { \
373 mpw *_dv = (dv), *_dvl = (dvl); \
374 const mpw *_av = (av), *_avl = (avl); \
378 while (_av < _avl) { \
382 _x = _m * *_av++ + c; \
384 _c = _x >> MPW_BITS; \
388 MPX_ZERO(_dv, _dvl); \
392 /* --- @MPX_UMLAN@ --- *
394 * Arguments: @dv, dvl@ = destination/accumulator vector base and limit
395 * @av, avl@ = multiplicand vector base and limit
398 * Use: Multiplies a multiprecision integer by a single-word value
399 * and adds the result to an accumulator.
402 #define MPX_UMLAN(dv, dvl, av, avl, m) do { \
403 mpw *_dv = (dv), *_dvl = (dvl); \
404 const mpw *_av = (av), *_avl = (avl); \
408 while (_av < _avl) { \
412 _x = *_dv + _m * *_av++ + _c; \
414 _c = _x >> MPW_BITS; \
418 MPX_ZERO(_dv, _dvl); \
422 /* --- @mpx_umul@ --- *
424 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
425 * @const mpw *av, *avl@ = multiplicand vector base and limit
426 * @const mpw *bv, *bvl@ = multiplier vector base and limit
430 * Use: Performs unsigned integer multiplication. If the result
431 * overflows the desination vector, high-order bits are
432 * discarded. The result vector may not overlap the argument
433 * vectors in any way.
436 extern void mpx_umul(mpw */
*dv*/
, mpw */
*dvl*/
,
437 const mpw */
*av*/
, const mpw */
*avl*/
,
438 const mpw */
*bv*/
, const mpw */
*bvl*/
);
440 /* --- @mpx_udiv@ --- *
442 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
443 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
444 * @const mpw *dv, *dvl@ = divisor vector base and limit
448 * Use: Performs unsigned integer division. If the result overflows
449 * the quotient vector, high-order bits are discarded. (Clearly
450 * the remainder vector can't overflow.) The various vectors
451 * may not overlap in any way. Yes, I know it's a bit odd
452 * requiring the dividend to be in the result position but it
453 * does make some sense really.
456 extern void mpx_udiv(mpw */
*qv*/
, mpw */
*qvl*/
, mpw */
*rv*/
, mpw */
*rvl*/
,
457 const mpw */
*dv*/
, const mpw */
*dvl*/
);
459 /*----- That's all, folks -------------------------------------------------*/