3 * $Id: mpx.h,v 1.1 1999/09/03 08:41:12 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.1 1999/09/03 08:41:12 mdw
45 /*----- The idea ----------------------------------------------------------*
47 * This file provides functions and macros which work on vectors of words as
48 * unsigned multiprecision integers. The interface works in terms of base
49 * and limit pointers (i.e., a pointer to the start of a vector, and a
50 * pointer just past its end) rather than base pointer and length, because
51 * that requires more arithmetic and state to work on.
53 * The interfaces are slightly bizarre in other ways. Try to use the
54 * higher-level functions where you can: they're rather better designed to
55 * actually be friendly and useful.
58 /*----- Header files ------------------------------------------------------*/
66 /*----- General manipulation ----------------------------------------------*/
68 /* --- @MPX_SHRINK@ --- *
70 * Arguments: @const mpw *v@ = pointer to vector of words
71 * @const mpw *vl@ = (updated) current limit of vector
73 * Use: Shrinks down the limit of a multiprecision integer vector.
76 #define MPX_SHRINK(v, vl) do { \
77 const mpw *_v = (v), *_vl = (vl); \
78 while (_vl > _v && *--_vl) \
83 /* --- @MPX_BITS@ --- *
85 * Arguments: @unsigned long b@ = result variable
86 * @const mpw *v@ = pointer to array of words
87 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
89 * Use: Calculates the number of bits in a multiprecision value.
92 #define MPX_BITS(b, v, vl) do { \
93 const mpw *_v = (v), *_vl = (vl); \
97 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
99 unsigned _k = MPW_BITS / 2; \
111 /* --- @MPX_OCTETS@ --- *
113 * Arguments: @size_t o@ = result variable
114 * @const mpw *v@ = pointer to array of words
115 * @size_t len@ = length of the array (from @MPX_LEN@)
117 * Use: Calculates the number of octets in a multiprecision value.
120 #define MPX_OCTETS(o, v, len) do { \
121 const mpw *_v = (v), *_vl = (vl); \
125 _size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
127 unsigned _k = MPW_BITS / 2; \
141 /* --- @MPX_COPY@ --- *
143 * Arguments: @dv, dvl@ = destination vector base and limit
144 * @av, avl@ = source vector base and limit
146 * Use: Copies a multiprecision integer.
149 #define MPX_COPY(dv, dvl, av, dvl) do { \
151 size_t _dn = (dvl) - _dv; \
152 const mpw *_av = (av); \
153 size_t _an = (avl) - _av; \
156 memset(_avl, 0, MPWS(_dn - _an)); \
157 } else if (_an >= _dn) \
158 memmove(_dv, _av, MPWS(_dn)); \
160 memmove(_dv, _av, MPWS(_an)); \
161 memset(_dv + _an, 0, MPWS(_dn - _an)); \
165 /* --- @MPX_ZERO@ --- *
167 * Arguments: @v, vl@ = base and limit of vector to clear
169 * Use: Zeroes the area between the two vector pointers.
172 #define MPX_ZERO(v, vl) { \
173 mpw *_v = (v), *_vl = (vl); \
174 memset(_v, 0, MPWS(_vl - _v)); \
177 /*----- Loading and storing -----------------------------------------------*/
179 /* --- @mpx_storel@ --- *
181 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
182 * @octet *p@ = pointer to octet array
183 * @size_t sz@ = size of octet array
187 * Use: Stores an MP in an octet array, least significant octet
188 * first. High-end octets are silently discarded if there
189 * isn't enough space for them.
192 extern void mpx_storel(const mpw */
*v*/
, const mpw */
*vl*/
,
193 octet */
*p*/
, size_t /*sz*/);
195 /* --- @mpx_loadl@ --- *
197 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
198 * @const octet *p@ = pointer to octet array
199 * @size_t sz@ = size of octet array
203 * Use: Loads an MP in an octet array, least significant octet
204 * first. High-end octets are ignored if there isn't enough
208 extern void mpx_loadl(mpw */
*v*/
, mpw */
*vl*/
,
209 const octet */
*p*/
, size_t /*sz*/);
211 /* --- @mpx_storeb@ --- *
213 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
214 * @octet *p@ = pointer to octet array
215 * @size_t sz@ = size of octet array
219 * Use: Stores an MP in an octet array, most significant octet
220 * first. High-end octets are silently discarded if there
221 * isn't enough space for them.
224 extern void mpx_storeb(const mpw */
*v*/
, const mpw */
*vl*/
,
225 octet */
*p*/
, size_t /*sz*/);
227 /* --- @mpx_loadb@ --- *
229 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
230 * @const octet *p@ = pointer to octet array
231 * @size_t sz@ = size of octet array
235 * Use: Loads an MP in an octet array, most significant octet
236 * first. High-end octets are ignored if there isn't enough
240 extern void mpx_loadb(mpw */
*v*/
, mpw */
*vl*/
,
241 const octet */
*p*/
, size_t /*sz*/);
243 /*----- Logical shifting --------------------------------------------------*/
245 /* --- @mpx_lsl@ --- *
247 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
248 * @const mpw *av, *avl@ = source vector base and limit
249 * @size_t n@ = number of bit positions to shift by
253 * Use: Performs a logical shift left operation on an integer.
256 extern void mpx_lsl(mpw */
*dv*/
, mpw */
*dvl*/
,
257 const mpw */
*av*/
, const mpw */
*avl*/
,
260 /* --- @mpx_lsr@ --- *
262 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
263 * @const mpw *av, *avl@ = source vector base and limit
264 * @size_t n@ = number of bit positions to shift by
268 * Use: Performs a logical shift right operation on an integer.
271 extern void mpx_lsr(mpw */
*dv*/
, mpw */
*dvl*/
,
272 const mpw */
*av*/
, const mpw */
*avl*/
,
275 /*----- Unsigned arithmetic -----------------------------------------------*/
277 /* --- @mpx_ucmp@ --- *
279 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
280 * @const mpw *bv, *bvl@ = second argument vector base and limit
282 * Returns: Less than, equal to, or greater than zero depending on
283 * whether @a@ is less than, equal to or greater than @b@,
286 * Use: Performs an unsigned integer comparison.
289 #define MPX_UCMP(av, avl, op, dv, dvl) \
290 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
292 extern int mpx_ucmp(const mpw */
*av*/
, const mpw */
*avl*/
,
293 const mpw */
*bv*/
, const mpw */
*bvl*/
);
295 /* --- @mpx_uadd@ --- *
297 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
298 * @const mpw *av, *avl@ = first addend vector base and limit
299 * @const mpw *bv, *bvl@ = second addend vector base and limit
303 * Use: Performs unsigned integer addition. If the result overflows
304 * the destination vector, high-order bits are discarded. This
305 * means that two's complement addition happens more or less for
306 * free, although that's more a side-effect than anything else.
307 * The result vector may be equal to either or both source
308 * vectors, but may not otherwise overlap them.
311 extern void mpx_uadd(mpw */
*dv*/
, mpw */
*dvl*/
,
312 const mpw */
*av*/
, const mpw */
*avl*/
,
313 const mpw */
*bv*/
, const mpw */
*bvl*/
);
315 /* --- @mpx_usub@ --- *
317 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
318 * @const mpw *av, *avl@ = first argument vector base and limit
319 * @const mpw *bv, *bvl@ = second argument vector base and limit
323 * Use: Performs unsigned integer subtraction. If the result
324 * overflows the destination vector, high-order bits are
325 * discarded. This means that two's complement subtraction
326 * happens more or less for free, althuogh that's more a side-
327 * effect than anything else. The result vector may be equal to
328 * either or both source vectors, but may not otherwise overlap
332 extern void mpx_usub(mpw */
*dv*/
, mpw */
*dvl*/
,
333 const mpw */
*av*/
, const mpw */
*avl*/
,
334 const mpw */
*bv*/
, const mpw */
*bvl*/
);
336 /* --- @MPX_UMULN@ --- *
338 * Arguments: @dv, dvl@ = destination vector base and limit
339 * @av, avl@ = multiplicand vector base and limit
342 * Use: Multiplies a multiprecision integer by a single-word value.
343 * The destination and source may be equal. The destination
344 * is completely cleared after use.
347 #define MPX_UMULN(dv, dvl, av, avl, m) do { \
348 mpw *_dv = (dv), *_dvl = (dvl); \
349 const mpw *_av = (av), *_avl = (avl); \
353 while (_av < _avl) { \
357 _x = _m * *_av++ + c; \
359 _c = _x >> MPW_BITS; \
363 MPX_ZERO(_dv, _dvl); \
367 /* --- @MPX_UMLAN@ --- *
369 * Arguments: @dv, dvl@ = destination/accumulator vector base and limit
370 * @av, avl@ = multiplicand vector base and limit
373 * Use: Multiplies a multiprecision integer by a single-word value
374 * and adds the result to an accumulator.
377 #define MPX_UMLAN(dv, dvl, av, avl, m) do { \
378 mpw *_dv = (dv), *_dvl = (dvl); \
379 const mpw *_av = (av), *_avl = (avl); \
383 while (_av < _avl) { \
387 _x = *_dv + _m * *_av++ + _c; \
389 _c = _x >> MPW_BITS; \
393 MPX_ZERO(_dv, _dvl); \
397 /* --- @mpx_umul@ --- *
399 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
400 * @const mpw *av, *avl@ = multiplicand vector base and limit
401 * @const mpw *bv, *bvl@ = multiplier vector base and limit
405 * Use: Performs unsigned integer multiplication. If the result
406 * overflows the desination vector, high-order bits are
407 * discarded. The result vector may not overlap the argument
408 * vectors in any way.
411 extern void mpx_umul(mpw */
*dv*/
, mpw */
*dvl*/
,
412 const mpw */
*av*/
, const mpw */
*avl*/
,
413 const mpw */
*bv*/
, const mpw */
*bvl*/
);
415 /* --- @mpx_udiv@ --- *
417 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
418 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
419 * @const mpw *dv, *dvl@ = divisor vector base and limit
423 * Use: Performs unsigned integer division. If the result overflows
424 * the quotient vector, high-order bits are discarded. (Clearly
425 * the remainder vector can't overflow.) The various vectors
426 * may not overlap in any way. Yes, I know it's a bit odd
427 * requiring the dividend to be in the result position but it
428 * does make some sense really.
431 extern void mpx_udiv(mpw */
*qv*/
, mpw */
*qvl*/
, mpw */
*rv*/
, mpw */
*rvl*/
,
432 const mpw */
*dv*/
, const mpw */
*dvl*/
);
434 /*----- That's all, folks -------------------------------------------------*/