A bit of reformatting. Initialize the uid and gid correctly.
[u/mdw/catacomb] / mpx.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
3 * $Id: mpx.h,v 1.1 1999/09/03 08:41:12 mdw Exp $
4 *
5 * Low level multiprecision arithmetic
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mpx.h,v $
33 * Revision 1.1 1999/09/03 08:41:12 mdw
34 * Initial import.
35 *
36 */
37
38#ifndef MPX_H
39#define MPX_H
40
41#ifdef __cplusplus
42 extern "C" {
43#endif
44
45/*----- The idea ----------------------------------------------------------*
46 *
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.
52 *
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.
56 */
57
58/*----- Header files ------------------------------------------------------*/
59
60#include <string.h>
61
62#ifndef MPTYPES_H
63# include "mptypes.h"
64#endif
65
66/*----- General manipulation ----------------------------------------------*/
67
68/* --- @MPX_SHRINK@ --- *
69 *
70 * Arguments: @const mpw *v@ = pointer to vector of words
71 * @const mpw *vl@ = (updated) current limit of vector
72 *
73 * Use: Shrinks down the limit of a multiprecision integer vector.
74 */
75
76#define MPX_SHRINK(v, vl) do { \
77 const mpw *_v = (v), *_vl = (vl); \
78 while (_vl > _v && *--_vl) \
79 ; \
80 (vl) = _vl; \
81} while (0)
82
83/* --- @MPX_BITS@ --- *
84 *
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@)
88 *
89 * Use: Calculates the number of bits in a multiprecision value.
90 */
91
92#define MPX_BITS(b, v, vl) do { \
93 const mpw *_v = (v), *_vl = (vl); \
94 if (_v == _vl) \
95 (b) = 0; \
96 else { \
97 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
98 mpw _w = _vl[-1]; \
99 unsigned _k = MPW_BITS / 2; \
100 while (_k) { \
101 if (_w >> _k) { \
102 _w >>= _k; \
103 _b += _k; \
104 } \
105 _k >>= 1; \
106 } \
107 (b) = _b; \
108 } \
109} while (0)
110
111/* --- @MPX_OCTETS@ --- *
112 *
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@)
116 *
117 * Use: Calculates the number of octets in a multiprecision value.
118 */
119
120#define MPX_OCTETS(o, v, len) do { \
121 const mpw *_v = (v), *_vl = (vl); \
122 if (_v == _vl) \
123 (o) = 0; \
124 else { \
125 _size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
126 mpw _w = _vl[-1]; \
127 unsigned _k = MPW_BITS / 2; \
128 while (_k > 3) { \
129 if (_w >> _k) { \
130 _w >>= _k; \
131 _o += _k - 3; \
132 } \
133 _k >>= 1; \
134 } \
135 if (_w) \
136 _o++; \
137 (o) = _o; \
138 } \
139} while (0)
140
141/* --- @MPX_COPY@ --- *
142 *
143 * Arguments: @dv, dvl@ = destination vector base and limit
144 * @av, avl@ = source vector base and limit
145 *
146 * Use: Copies a multiprecision integer.
147 */
148
149#define MPX_COPY(dv, dvl, av, dvl) do { \
150 mpw *_dv = (dv); \
151 size_t _dn = (dvl) - _dv; \
152 const mpw *_av = (av); \
153 size_t _an = (avl) - _av; \
154 if (_av == _dv) { \
155 if (_dvl > _avl) \
156 memset(_avl, 0, MPWS(_dn - _an)); \
157 } else if (_an >= _dn) \
158 memmove(_dv, _av, MPWS(_dn)); \
159 else { \
160 memmove(_dv, _av, MPWS(_an)); \
161 memset(_dv + _an, 0, MPWS(_dn - _an)); \
162 } \
163} while (0)
164
165/* --- @MPX_ZERO@ --- *
166 *
167 * Arguments: @v, vl@ = base and limit of vector to clear
168 *
169 * Use: Zeroes the area between the two vector pointers.
170 */
171
172#define MPX_ZERO(v, vl) { \
173 mpw *_v = (v), *_vl = (vl); \
174 memset(_v, 0, MPWS(_vl - _v)); \
175} while (0)
176
177/*----- Loading and storing -----------------------------------------------*/
178
179/* --- @mpx_storel@ --- *
180 *
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
184 *
185 * Returns: ---
186 *
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.
190 */
191
192extern void mpx_storel(const mpw */*v*/, const mpw */*vl*/,
193 octet */*p*/, size_t /*sz*/);
194
195/* --- @mpx_loadl@ --- *
196 *
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
200 *
201 * Returns: ---
202 *
203 * Use: Loads an MP in an octet array, least significant octet
204 * first. High-end octets are ignored if there isn't enough
205 * space for them.
206 */
207
208extern void mpx_loadl(mpw */*v*/, mpw */*vl*/,
209 const octet */*p*/, size_t /*sz*/);
210
211/* --- @mpx_storeb@ --- *
212 *
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
216 *
217 * Returns: ---
218 *
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.
222 */
223
224extern void mpx_storeb(const mpw */*v*/, const mpw */*vl*/,
225 octet */*p*/, size_t /*sz*/);
226
227/* --- @mpx_loadb@ --- *
228 *
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
232 *
233 * Returns: ---
234 *
235 * Use: Loads an MP in an octet array, most significant octet
236 * first. High-end octets are ignored if there isn't enough
237 * space for them.
238 */
239
240extern void mpx_loadb(mpw */*v*/, mpw */*vl*/,
241 const octet */*p*/, size_t /*sz*/);
242
243/*----- Logical shifting --------------------------------------------------*/
244
245/* --- @mpx_lsl@ --- *
246 *
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
250 *
251 * Returns: ---
252 *
253 * Use: Performs a logical shift left operation on an integer.
254 */
255
256extern void mpx_lsl(mpw */*dv*/, mpw */*dvl*/,
257 const mpw */*av*/, const mpw */*avl*/,
258 size_t /*n*/);
259
260/* --- @mpx_lsr@ --- *
261 *
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
265 *
266 * Returns: ---
267 *
268 * Use: Performs a logical shift right operation on an integer.
269 */
270
271extern void mpx_lsr(mpw */*dv*/, mpw */*dvl*/,
272 const mpw */*av*/, const mpw */*avl*/,
273 size_t /*n*/);
274
275/*----- Unsigned arithmetic -----------------------------------------------*/
276
277/* --- @mpx_ucmp@ --- *
278 *
279 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
280 * @const mpw *bv, *bvl@ = second argument vector base and limit
281 *
282 * Returns: Less than, equal to, or greater than zero depending on
283 * whether @a@ is less than, equal to or greater than @b@,
284 * respectively.
285 *
286 * Use: Performs an unsigned integer comparison.
287 */
288
289#define MPX_UCMP(av, avl, op, dv, dvl) \
290 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
291
292extern int mpx_ucmp(const mpw */*av*/, const mpw */*avl*/,
293 const mpw */*bv*/, const mpw */*bvl*/);
294
295/* --- @mpx_uadd@ --- *
296 *
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
300 *
301 * Returns: ---
302 *
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.
309 */
310
311extern void mpx_uadd(mpw */*dv*/, mpw */*dvl*/,
312 const mpw */*av*/, const mpw */*avl*/,
313 const mpw */*bv*/, const mpw */*bvl*/);
314
315/* --- @mpx_usub@ --- *
316 *
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
320 *
321 * Returns: ---
322 *
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
329 * them.
330 */
331
332extern void mpx_usub(mpw */*dv*/, mpw */*dvl*/,
333 const mpw */*av*/, const mpw */*avl*/,
334 const mpw */*bv*/, const mpw */*bvl*/);
335
336/* --- @MPX_UMULN@ --- *
337 *
338 * Arguments: @dv, dvl@ = destination vector base and limit
339 * @av, avl@ = multiplicand vector base and limit
340 * @m@ = multiplier
341 *
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.
345 */
346
347#define MPX_UMULN(dv, dvl, av, avl, m) do { \
348 mpw *_dv = (dv), *_dvl = (dvl); \
349 const mpw *_av = (av), *_avl = (avl); \
350 mpw _c = 0; \
351 mpd _m = (m); \
352 \
353 while (_av < _avl) { \
354 mpd _x; \
355 if (_dv >= _dvl) \
356 break; \
357 _x = _m * *_av++ + c; \
358 *_dv++ = MPW(_x); \
359 _c = _x >> MPW_BITS; \
360 } \
361 if (_dv < _dvl) { \
362 *_dv++ = MPW(_c); \
363 MPX_ZERO(_dv, _dvl); \
364 } \
365} while (0)
366
367/* --- @MPX_UMLAN@ --- *
368 *
369 * Arguments: @dv, dvl@ = destination/accumulator vector base and limit
370 * @av, avl@ = multiplicand vector base and limit
371 * @m@ = multiplier
372 *
373 * Use: Multiplies a multiprecision integer by a single-word value
374 * and adds the result to an accumulator.
375 */
376
377#define MPX_UMLAN(dv, dvl, av, avl, m) do { \
378 mpw *_dv = (dv), *_dvl = (dvl); \
379 const mpw *_av = (av), *_avl = (avl); \
380 mpw _c = 0; \
381 mpd _m = (m); \
382 \
383 while (_av < _avl) { \
384 mpd _x; \
385 if (_dv >= _dvl) \
386 break; \
387 _x = *_dv + _m * *_av++ + _c; \
388 *_dv++ = MPW(_x); \
389 _c = _x >> MPW_BITS; \
390 } \
391 if (_dv < _dvl) { \
392 *_dv++ = MPW(_c); \
393 MPX_ZERO(_dv, _dvl); \
394 } \
395} while (0)
396
397/* --- @mpx_umul@ --- *
398 *
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
402 *
403 * Returns: ---
404 *
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.
409 */
410
411extern void mpx_umul(mpw */*dv*/, mpw */*dvl*/,
412 const mpw */*av*/, const mpw */*avl*/,
413 const mpw */*bv*/, const mpw */*bvl*/);
414
415/* --- @mpx_udiv@ --- *
416 *
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
420 *
421 * Returns: ---
422 *
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.
429 */
430
431extern void mpx_udiv(mpw */*qv*/, mpw */*qvl*/, mpw */*rv*/, mpw */*rvl*/,
432 const mpw */*dv*/, const mpw */*dvl*/);
433
434/*----- That's all, folks -------------------------------------------------*/
435
436#ifdef __cplusplus
437 }
438#endif
439
440#endif