d03ab969 |
1 | /* -*-c-*- |
2 | * |
81578196 |
3 | * $Id: mpx.h,v 1.16 2003/05/16 09:09:24 mdw Exp $ |
d03ab969 |
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 $ |
81578196 |
33 | * Revision 1.16 2003/05/16 09:09:24 mdw |
34 | * Fix @mp_lsl2c@. Turns out to be surprisingly tricky. |
35 | * |
75263f25 |
36 | * Revision 1.15 2002/10/19 17:56:50 mdw |
37 | * Fix bit operations. Test them (a bit) better. |
38 | * |
dd22938e |
39 | * Revision 1.14 2002/10/09 00:36:03 mdw |
40 | * Fix bounds on workspace for Karatsuba operations. |
41 | * |
f09e814a |
42 | * Revision 1.13 2002/10/06 22:52:50 mdw |
43 | * Pile of changes for supporting two's complement properly. |
44 | * |
0f32e0f8 |
45 | * Revision 1.12 2001/04/03 19:36:05 mdw |
46 | * Add some simple bitwise operations so that Perl can use them. |
47 | * |
52cdaca9 |
48 | * Revision 1.11 2000/10/08 15:48:35 mdw |
49 | * Rename Karatsuba constants now that we have @gfx_kmul@ too. |
50 | * |
1a05a8ef |
51 | * Revision 1.10 2000/10/08 12:06:12 mdw |
52 | * Provide @mpx_ueq@ for rapidly testing equality of two integers. |
53 | * |
698bd937 |
54 | * Revision 1.9 1999/12/22 15:49:07 mdw |
55 | * New function for division by a small integer. |
56 | * |
5bf74dea |
57 | * Revision 1.8 1999/12/11 10:57:43 mdw |
58 | * Karatsuba squaring algorithm. |
59 | * |
652a6acf |
60 | * Revision 1.7 1999/12/11 01:51:28 mdw |
61 | * Change Karatsuba parameters slightly. |
62 | * |
a86e33af |
63 | * Revision 1.6 1999/12/10 23:23:51 mdw |
64 | * Karatsuba-Ofman multiplication algorithm. |
65 | * |
dd517851 |
66 | * Revision 1.5 1999/11/20 22:23:27 mdw |
67 | * Add function versions of some low-level macros with wider use. |
68 | * |
7c13f461 |
69 | * Revision 1.4 1999/11/17 18:04:43 mdw |
70 | * Add two's complement support. Fix a bug in MPX_UMLAN. |
71 | * |
3c9ede17 |
72 | * Revision 1.3 1999/11/13 01:51:29 mdw |
73 | * Minor interface changes. Should be stable now. |
74 | * |
b9b1c853 |
75 | * Revision 1.2 1999/11/11 17:47:55 mdw |
76 | * Minor changes for different `mptypes.h' format. |
77 | * |
d03ab969 |
78 | * Revision 1.1 1999/09/03 08:41:12 mdw |
79 | * Initial import. |
80 | * |
81 | */ |
82 | |
a86e33af |
83 | #ifndef CATACOMB_MPX_H |
84 | #define CATACOMB_MPX_H |
d03ab969 |
85 | |
86 | #ifdef __cplusplus |
87 | extern "C" { |
88 | #endif |
89 | |
90 | /*----- The idea ----------------------------------------------------------* |
91 | * |
92 | * This file provides functions and macros which work on vectors of words as |
93 | * unsigned multiprecision integers. The interface works in terms of base |
94 | * and limit pointers (i.e., a pointer to the start of a vector, and a |
95 | * pointer just past its end) rather than base pointer and length, because |
96 | * that requires more arithmetic and state to work on. |
97 | * |
98 | * The interfaces are slightly bizarre in other ways. Try to use the |
99 | * higher-level functions where you can: they're rather better designed to |
100 | * actually be friendly and useful. |
101 | */ |
102 | |
103 | /*----- Header files ------------------------------------------------------*/ |
104 | |
105 | #include <string.h> |
106 | |
a86e33af |
107 | #ifndef CATACOMB_MPW_H |
3c9ede17 |
108 | # include "mpw.h" |
d03ab969 |
109 | #endif |
110 | |
111 | /*----- General manipulation ----------------------------------------------*/ |
112 | |
113 | /* --- @MPX_SHRINK@ --- * |
114 | * |
115 | * Arguments: @const mpw *v@ = pointer to vector of words |
116 | * @const mpw *vl@ = (updated) current limit of vector |
117 | * |
118 | * Use: Shrinks down the limit of a multiprecision integer vector. |
119 | */ |
120 | |
121 | #define MPX_SHRINK(v, vl) do { \ |
3c9ede17 |
122 | const mpw *_vv = (v), *_vvl = (vl); \ |
123 | while (_vvl > _vv && !_vvl[-1]) \ |
124 | _vvl--; \ |
125 | (vl) = (mpw *)_vvl; \ |
d03ab969 |
126 | } while (0) |
127 | |
128 | /* --- @MPX_BITS@ --- * |
129 | * |
130 | * Arguments: @unsigned long b@ = result variable |
131 | * @const mpw *v@ = pointer to array of words |
132 | * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@) |
133 | * |
134 | * Use: Calculates the number of bits in a multiprecision value. |
135 | */ |
136 | |
137 | #define MPX_BITS(b, v, vl) do { \ |
138 | const mpw *_v = (v), *_vl = (vl); \ |
3c9ede17 |
139 | MPX_SHRINK(_v, _vl); \ |
d03ab969 |
140 | if (_v == _vl) \ |
141 | (b) = 0; \ |
142 | else { \ |
143 | unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \ |
144 | mpw _w = _vl[-1]; \ |
145 | unsigned _k = MPW_BITS / 2; \ |
146 | while (_k) { \ |
147 | if (_w >> _k) { \ |
148 | _w >>= _k; \ |
149 | _b += _k; \ |
150 | } \ |
151 | _k >>= 1; \ |
152 | } \ |
153 | (b) = _b; \ |
154 | } \ |
155 | } while (0) |
156 | |
157 | /* --- @MPX_OCTETS@ --- * |
158 | * |
159 | * Arguments: @size_t o@ = result variable |
3c9ede17 |
160 | * @const mpw *v, *vl@ = pointer to array of words |
d03ab969 |
161 | * |
162 | * Use: Calculates the number of octets in a multiprecision value. |
163 | */ |
164 | |
3c9ede17 |
165 | #define MPX_OCTETS(o, v, vl) do { \ |
f09e814a |
166 | unsigned long _bb; \ |
167 | MPX_BITS(_bb, (v), (vl)); \ |
168 | (o) = (_bb + 7) >> 3; \ |
169 | } while (0) |
170 | |
171 | /* --- @MPX_OCTETS2C@ --- * |
172 | * |
173 | * Arguments: @size_t o@ = result variable |
174 | * @const mpw *v, *vl@ = pointer to array of words |
175 | * |
176 | * Use: Calculates the number of octets in a multiprecision value, if |
177 | * you represent it as two's complement. |
178 | */ |
179 | |
180 | #define MPX_OCTETS2C(o, v, vl) do { \ |
181 | unsigned long _bb; \ |
182 | MPX_BITS(_bb, (v), (vl)); \ |
183 | (o) = (_bb >> 3) + 1; \ |
d03ab969 |
184 | } while (0) |
185 | |
186 | /* --- @MPX_COPY@ --- * |
187 | * |
188 | * Arguments: @dv, dvl@ = destination vector base and limit |
189 | * @av, avl@ = source vector base and limit |
190 | * |
191 | * Use: Copies a multiprecision integer. |
192 | */ |
193 | |
3c9ede17 |
194 | #define MPX_COPY(dv, dvl, av, avl) do { \ |
195 | mpw *_dv = (dv), *_dvl = (dvl); \ |
196 | size_t _dn = _dvl - _dv; \ |
197 | const mpw *_av = (av), *_avl = (avl); \ |
198 | size_t _an = _avl - _av; \ |
d03ab969 |
199 | if (_av == _dv) { \ |
200 | if (_dvl > _avl) \ |
3c9ede17 |
201 | memset(_dv, 0, MPWS(_dn - _an)); \ |
d03ab969 |
202 | } else if (_an >= _dn) \ |
203 | memmove(_dv, _av, MPWS(_dn)); \ |
204 | else { \ |
205 | memmove(_dv, _av, MPWS(_an)); \ |
206 | memset(_dv + _an, 0, MPWS(_dn - _an)); \ |
207 | } \ |
208 | } while (0) |
209 | |
210 | /* --- @MPX_ZERO@ --- * |
211 | * |
212 | * Arguments: @v, vl@ = base and limit of vector to clear |
213 | * |
214 | * Use: Zeroes the area between the two vector pointers. |
215 | */ |
216 | |
3c9ede17 |
217 | #define MPX_ZERO(v, vl) do { \ |
d03ab969 |
218 | mpw *_v = (v), *_vl = (vl); \ |
3c9ede17 |
219 | if (_v < _vl) \ |
220 | memset(_v, 0, MPWS(_vl - _v)); \ |
d03ab969 |
221 | } while (0) |
222 | |
81578196 |
223 | /* --- @MPX_ONE@ --- * |
224 | * |
225 | * Arguments: @v, vl@ = base and limit of vector to clear |
226 | * |
227 | * Use: Fills the area between the two vector pointers with ones. |
228 | */ |
229 | |
230 | #define MPX_ONE(v, vl) do { \ |
231 | mpw * _v = (v); \ |
232 | const mpw *_vl = (vl); \ |
233 | while (_v < _vl) \ |
234 | *_v++ = MPW_MAX; \ |
235 | } while (0) |
236 | |
d03ab969 |
237 | /*----- Loading and storing -----------------------------------------------*/ |
238 | |
239 | /* --- @mpx_storel@ --- * |
240 | * |
241 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector |
3c9ede17 |
242 | * @void *p@ = pointer to octet array |
d03ab969 |
243 | * @size_t sz@ = size of octet array |
244 | * |
245 | * Returns: --- |
246 | * |
247 | * Use: Stores an MP in an octet array, least significant octet |
248 | * first. High-end octets are silently discarded if there |
249 | * isn't enough space for them. |
250 | */ |
251 | |
252 | extern void mpx_storel(const mpw */*v*/, const mpw */*vl*/, |
3c9ede17 |
253 | void */*p*/, size_t /*sz*/); |
d03ab969 |
254 | |
255 | /* --- @mpx_loadl@ --- * |
256 | * |
257 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector |
3c9ede17 |
258 | * @const void *p@ = pointer to octet array |
d03ab969 |
259 | * @size_t sz@ = size of octet array |
260 | * |
261 | * Returns: --- |
262 | * |
263 | * Use: Loads an MP in an octet array, least significant octet |
264 | * first. High-end octets are ignored if there isn't enough |
265 | * space for them. |
266 | */ |
267 | |
268 | extern void mpx_loadl(mpw */*v*/, mpw */*vl*/, |
3c9ede17 |
269 | const void */*p*/, size_t /*sz*/); |
d03ab969 |
270 | |
271 | /* --- @mpx_storeb@ --- * |
272 | * |
273 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector |
3c9ede17 |
274 | * @void *p@ = pointer to octet array |
d03ab969 |
275 | * @size_t sz@ = size of octet array |
276 | * |
277 | * Returns: --- |
278 | * |
279 | * Use: Stores an MP in an octet array, most significant octet |
280 | * first. High-end octets are silently discarded if there |
281 | * isn't enough space for them. |
282 | */ |
283 | |
284 | extern void mpx_storeb(const mpw */*v*/, const mpw */*vl*/, |
3c9ede17 |
285 | void */*p*/, size_t /*sz*/); |
d03ab969 |
286 | |
287 | /* --- @mpx_loadb@ --- * |
288 | * |
289 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector |
3c9ede17 |
290 | * @const void *p@ = pointer to octet array |
d03ab969 |
291 | * @size_t sz@ = size of octet array |
292 | * |
293 | * Returns: --- |
294 | * |
295 | * Use: Loads an MP in an octet array, most significant octet |
296 | * first. High-end octets are ignored if there isn't enough |
297 | * space for them. |
298 | */ |
299 | |
300 | extern void mpx_loadb(mpw */*v*/, mpw */*vl*/, |
3c9ede17 |
301 | const void */*p*/, size_t /*sz*/); |
d03ab969 |
302 | |
f09e814a |
303 | /* --- @mpx_storel2cn@ --- * |
304 | * |
305 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector |
306 | * @void *pp@ = pointer to octet array |
307 | * @size_t sz@ = size of octet array |
308 | * |
309 | * Returns: --- |
310 | * |
311 | * Use: Stores a negative MP in an octet array, least significant |
312 | * octet first, as two's complement. High-end octets are |
313 | * silently discarded if there isn't enough space for them. |
314 | * This obviously makes the output bad. |
315 | */ |
316 | |
317 | extern void mpx_storel2cn(const mpw */*v*/, const mpw */*vl*/, |
318 | void */*p*/, size_t /*sz*/); |
319 | |
320 | /* --- @mpx_loadl2cn@ --- * |
321 | * |
322 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector |
323 | * @const void *pp@ = pointer to octet array |
324 | * @size_t sz@ = size of octet array |
325 | * |
326 | * Returns: --- |
327 | * |
328 | * Use: Loads a negative MP in an octet array, least significant |
329 | * octet first, as two's complement. High-end octets are |
330 | * ignored if there isn't enough space for them. This probably |
331 | * means you made the wrong choice coming here. |
332 | */ |
333 | |
334 | extern void mpx_loadl2cn(mpw */*v*/, mpw */*vl*/, |
335 | const void */*p*/, size_t /*sz*/); |
336 | |
337 | /* --- @mpx_storeb2cn@ --- * |
338 | * |
339 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector |
340 | * @void *pp@ = pointer to octet array |
341 | * @size_t sz@ = size of octet array |
342 | * |
343 | * Returns: --- |
344 | * |
345 | * Use: Stores a negative MP in an octet array, most significant |
346 | * octet first, as two's complement. High-end octets are |
347 | * silently discarded if there isn't enough space for them, |
348 | * which probably isn't what you meant. |
349 | */ |
350 | |
351 | extern void mpx_storeb2cn(const mpw */*v*/, const mpw */*vl*/, |
352 | void */*p*/, size_t /*sz*/); |
353 | |
354 | /* --- @mpx_loadb2cn@ --- * |
355 | * |
356 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector |
357 | * @const void *pp@ = pointer to octet array |
358 | * @size_t sz@ = size of octet array |
359 | * |
360 | * Returns: --- |
361 | * |
362 | * Use: Loads a negative MP in an octet array, most significant octet |
363 | * first as two's complement. High-end octets are ignored if |
364 | * there isn't enough space for them. This probably means you |
365 | * chose this function wrongly. |
366 | */ |
367 | |
368 | extern void mpx_loadb2cn(mpw */*v*/, mpw */*vl*/, |
369 | const void */*p*/, size_t /*sz*/); |
370 | |
371 | |
d03ab969 |
372 | /*----- Logical shifting --------------------------------------------------*/ |
373 | |
374 | /* --- @mpx_lsl@ --- * |
375 | * |
376 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
377 | * @const mpw *av, *avl@ = source vector base and limit |
378 | * @size_t n@ = number of bit positions to shift by |
379 | * |
380 | * Returns: --- |
381 | * |
382 | * Use: Performs a logical shift left operation on an integer. |
383 | */ |
384 | |
385 | extern void mpx_lsl(mpw */*dv*/, mpw */*dvl*/, |
386 | const mpw */*av*/, const mpw */*avl*/, |
387 | size_t /*n*/); |
388 | |
81578196 |
389 | /* --- @mpx_lslc@ --- * |
390 | * |
391 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
392 | * @const mpw *av, *avl@ = source vector base and limit |
393 | * @size_t n@ = number of bit positions to shift by |
394 | * |
395 | * Returns: --- |
396 | * |
397 | * Use: Performs a logical shift left operation on an integer, only |
398 | * it fills in the bits with ones instead of zeroes. |
399 | */ |
400 | |
401 | extern void mpx_lslc(mpw */*dv*/, mpw */*dvl*/, |
402 | const mpw */*av*/, const mpw */*avl*/, |
403 | size_t /*n*/); |
404 | |
d03ab969 |
405 | /* --- @mpx_lsr@ --- * |
406 | * |
407 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
408 | * @const mpw *av, *avl@ = source vector base and limit |
409 | * @size_t n@ = number of bit positions to shift by |
410 | * |
411 | * Returns: --- |
412 | * |
413 | * Use: Performs a logical shift right operation on an integer. |
414 | */ |
415 | |
416 | extern void mpx_lsr(mpw */*dv*/, mpw */*dvl*/, |
417 | const mpw */*av*/, const mpw */*avl*/, |
418 | size_t /*n*/); |
419 | |
0f32e0f8 |
420 | /*----- Bitwise operations ------------------------------------------------*/ |
421 | |
f09e814a |
422 | /* --- @mpx_bitop@ --- * |
0f32e0f8 |
423 | * |
424 | * Arguments: @mpw *dv, *dvl@ = destination vector |
425 | * @const mpw *av, *avl@ = first source vector |
426 | * @const mpw *bv, *bvl@ = second source vector |
427 | * |
428 | * Returns: --- |
429 | * |
f09e814a |
430 | * Use: Provide the dyadic boolean functions. The functions are |
431 | * named after the truth table they generate: |
432 | * |
433 | * a: 0011 |
434 | * b: 0101 |
435 | * @mpx_bitXXXX@ |
0f32e0f8 |
436 | */ |
437 | |
f09e814a |
438 | #define MPX_DOBIN(what) \ |
439 | what(0000) what(0001) what(0010) what(0011) \ |
440 | what(0100) what(0101) what(0110) what(0111) \ |
441 | what(1000) what(1001) what(1010) what(1011) \ |
442 | what(1100) what(1101) what(1110) what(1111) |
0f32e0f8 |
443 | |
f09e814a |
444 | #define MPX_BITDECL(string) \ |
445 | extern void mpx_bit##string(mpw */*dv*/, mpw */*dvl*/, \ |
446 | const mpw */*av*/, const mpw */*avl*/, \ |
447 | const mpw */*bv*/, const mpw */*bvl*/); |
448 | MPX_DOBIN(MPX_BITDECL) |
0f32e0f8 |
449 | |
f09e814a |
450 | /* --- @mpx_[n]and@, @mpx_[n]or@, @mpx_xor@ --- * |
451 | * |
452 | * Synonyms for the commonly-used functions above. |
453 | */ |
454 | |
455 | #define mpx_and mpx_bit0001 |
456 | #define mpx_or mpx_bit0111 |
457 | #define mpx_nand mpx_bit1110 |
458 | #define mpx_nor mpx_bit1000 |
459 | #define mpx_xor mpx_bit0110 |
460 | |
461 | /* --- @mpx_not@ --- * |
462 | * |
463 | * Arguments: @mpw *dv, *dvl@ = destination vector |
464 | * @const mpw *av, *avl@ = first source vector |
465 | * |
466 | * Returns: --- |
467 | * |
468 | * Use; Bitwise NOT. |
469 | */ |
0f32e0f8 |
470 | |
471 | extern void mpx_not(mpw */*dv*/, mpw */*dvl*/, |
472 | const mpw */*av*/, const mpw */*avl*/); |
473 | |
d03ab969 |
474 | /*----- Unsigned arithmetic -----------------------------------------------*/ |
475 | |
7c13f461 |
476 | /* --- @mpx_2c@ --- * |
477 | * |
478 | * Arguments: @mpw *dv, *dvl@ = destination vector |
479 | * @const mpw *v, *vl@ = source vector |
480 | * |
481 | * Returns: --- |
482 | * |
483 | * Use: Calculates the two's complement of @v@. |
484 | */ |
485 | |
486 | extern void mpx_2c(mpw */*dv*/, mpw */*dvl*/, |
487 | const mpw */*v*/, const mpw */*vl*/); |
488 | |
1a05a8ef |
489 | /* --- @mpx_ueq@ --- * |
490 | * |
491 | * Arguments: @const mpw *av, *avl@ = first argument vector base and limit |
492 | * @const mpw *bv, *bvl@ = second argument vector base and limit |
493 | * |
494 | * Returns: Nonzero if the two vectors are equal. |
495 | * |
496 | * Use: Performs an unsigned integer test for equality. |
497 | */ |
498 | |
499 | extern int mpx_ueq(const mpw */*av*/, const mpw */*avl*/, |
500 | const mpw */*bv*/, const mpw */*bvl*/); |
501 | |
d03ab969 |
502 | /* --- @mpx_ucmp@ --- * |
503 | * |
504 | * Arguments: @const mpw *av, *avl@ = first argument vector base and limit |
505 | * @const mpw *bv, *bvl@ = second argument vector base and limit |
506 | * |
507 | * Returns: Less than, equal to, or greater than zero depending on |
508 | * whether @a@ is less than, equal to or greater than @b@, |
509 | * respectively. |
510 | * |
511 | * Use: Performs an unsigned integer comparison. |
512 | */ |
513 | |
514 | #define MPX_UCMP(av, avl, op, dv, dvl) \ |
515 | (mpx_ucmp((av), (avl), (dv), (dvl)) op 0) |
516 | |
517 | extern int mpx_ucmp(const mpw */*av*/, const mpw */*avl*/, |
518 | const mpw */*bv*/, const mpw */*bvl*/); |
519 | |
520 | /* --- @mpx_uadd@ --- * |
521 | * |
522 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
523 | * @const mpw *av, *avl@ = first addend vector base and limit |
524 | * @const mpw *bv, *bvl@ = second addend vector base and limit |
525 | * |
526 | * Returns: --- |
527 | * |
528 | * Use: Performs unsigned integer addition. If the result overflows |
529 | * the destination vector, high-order bits are discarded. This |
530 | * means that two's complement addition happens more or less for |
531 | * free, although that's more a side-effect than anything else. |
532 | * The result vector may be equal to either or both source |
533 | * vectors, but may not otherwise overlap them. |
534 | */ |
535 | |
536 | extern void mpx_uadd(mpw */*dv*/, mpw */*dvl*/, |
537 | const mpw */*av*/, const mpw */*avl*/, |
538 | const mpw */*bv*/, const mpw */*bvl*/); |
539 | |
dd517851 |
540 | /* --- @mpx_uaddn@ --- * |
541 | * |
542 | * Arguments: @mpw *dv, *dvl@ = source and destination base and limit |
543 | * @mpw n@ = other addend |
3c9ede17 |
544 | * |
dd517851 |
545 | * Returns: --- |
3c9ede17 |
546 | * |
547 | * Use: Adds a small integer to a multiprecision number. |
548 | */ |
549 | |
550 | #define MPX_UADDN(dv, dvl, n) do { \ |
551 | mpw *_ddv = (dv), *_ddvl = (dvl); \ |
552 | mpw _c = (n); \ |
553 | \ |
554 | while (_c && _ddv < _ddvl) { \ |
555 | mpd _x = (mpd)*_ddv + (mpd)_c; \ |
556 | *_ddv++ = MPW(_x); \ |
557 | _c = _x >> MPW_BITS; \ |
558 | } \ |
559 | } while (0) |
560 | |
dd517851 |
561 | extern void mpx_uaddn(mpw */*dv*/, mpw */*dvl*/, mpw /*n*/); |
562 | |
d03ab969 |
563 | /* --- @mpx_usub@ --- * |
564 | * |
565 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
566 | * @const mpw *av, *avl@ = first argument vector base and limit |
567 | * @const mpw *bv, *bvl@ = second argument vector base and limit |
568 | * |
569 | * Returns: --- |
570 | * |
571 | * Use: Performs unsigned integer subtraction. If the result |
572 | * overflows the destination vector, high-order bits are |
573 | * discarded. This means that two's complement subtraction |
3c9ede17 |
574 | * happens more or less for free, although that's more a side- |
d03ab969 |
575 | * effect than anything else. The result vector may be equal to |
576 | * either or both source vectors, but may not otherwise overlap |
577 | * them. |
578 | */ |
579 | |
580 | extern void mpx_usub(mpw */*dv*/, mpw */*dvl*/, |
581 | const mpw */*av*/, const mpw */*avl*/, |
582 | const mpw */*bv*/, const mpw */*bvl*/); |
583 | |
dd517851 |
584 | /* --- @mpx_usubn@ --- * |
3c9ede17 |
585 | * |
dd517851 |
586 | * Arguments: @mpw *dv, *dvl@ = source and destination base and limit |
587 | * @n@ = subtrahend |
588 | * |
589 | * Returns: --- |
3c9ede17 |
590 | * |
591 | * Use: Subtracts a small integer from a multiprecision number. |
592 | */ |
593 | |
594 | #define MPX_USUBN(dv, dvl, n) do { \ |
595 | mpw *_ddv = (dv), *_ddvl = (dvl); \ |
596 | mpw _c = (n); \ |
597 | \ |
598 | while (_ddv < _ddvl) { \ |
599 | mpd _x = (mpd)*_ddv - (mpd)_c; \ |
600 | *_ddv++ = MPW(_x); \ |
601 | if (_x >> MPW_BITS) \ |
602 | _c = 1; \ |
603 | else \ |
604 | break; \ |
605 | } \ |
606 | } while (0) |
607 | |
dd517851 |
608 | extern void mpx_usubn(mpw */*dv*/, mpw */*dvl*/, mpw /*n*/); |
609 | |
3c9ede17 |
610 | /* --- @mpx_umul@ --- * |
611 | * |
612 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
613 | * @const mpw *av, *avl@ = multiplicand vector base and limit |
614 | * @const mpw *bv, *bvl@ = multiplier vector base and limit |
615 | * |
616 | * Returns: --- |
617 | * |
618 | * Use: Performs unsigned integer multiplication. If the result |
619 | * overflows the desination vector, high-order bits are |
620 | * discarded. The result vector may not overlap the argument |
621 | * vectors in any way. |
622 | */ |
623 | |
624 | extern void mpx_umul(mpw */*dv*/, mpw */*dvl*/, |
625 | const mpw */*av*/, const mpw */*avl*/, |
626 | const mpw */*bv*/, const mpw */*bvl*/); |
627 | |
dd517851 |
628 | /* --- @mpx_umuln@ --- * |
d03ab969 |
629 | * |
dd517851 |
630 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
631 | * @const mpw *av, *avl@ = multiplicand vector base and limit |
632 | * @mpw m@ = multiplier |
633 | * |
634 | * Returns: --- |
d03ab969 |
635 | * |
636 | * Use: Multiplies a multiprecision integer by a single-word value. |
637 | * The destination and source may be equal. The destination |
638 | * is completely cleared after use. |
639 | */ |
640 | |
641 | #define MPX_UMULN(dv, dvl, av, avl, m) do { \ |
642 | mpw *_dv = (dv), *_dvl = (dvl); \ |
643 | const mpw *_av = (av), *_avl = (avl); \ |
644 | mpw _c = 0; \ |
645 | mpd _m = (m); \ |
646 | \ |
647 | while (_av < _avl) { \ |
648 | mpd _x; \ |
649 | if (_dv >= _dvl) \ |
650 | break; \ |
3c9ede17 |
651 | _x = (mpd)_m * (mpd)*_av++ + _c; \ |
d03ab969 |
652 | *_dv++ = MPW(_x); \ |
653 | _c = _x >> MPW_BITS; \ |
654 | } \ |
655 | if (_dv < _dvl) { \ |
656 | *_dv++ = MPW(_c); \ |
657 | MPX_ZERO(_dv, _dvl); \ |
658 | } \ |
659 | } while (0) |
660 | |
dd517851 |
661 | extern void mpx_umuln(mpw */*dv*/, mpw */*dvl*/, |
662 | const mpw */*av*/, const mpw */*avl*/, mpw m); |
663 | |
664 | /* --- @mpx_umlan@ --- * |
d03ab969 |
665 | * |
dd517851 |
666 | * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit |
667 | * @const mpw *av, *avl@ = multiplicand vector base and limit |
668 | * @mpw m@ = multiplier |
669 | * |
670 | * Returns: --- |
d03ab969 |
671 | * |
672 | * Use: Multiplies a multiprecision integer by a single-word value |
673 | * and adds the result to an accumulator. |
674 | */ |
675 | |
676 | #define MPX_UMLAN(dv, dvl, av, avl, m) do { \ |
677 | mpw *_dv = (dv), *_dvl = (dvl); \ |
678 | const mpw *_av = (av), *_avl = (avl); \ |
7c13f461 |
679 | mpw _cc = 0; \ |
d03ab969 |
680 | mpd _m = (m); \ |
681 | \ |
5bf74dea |
682 | while (_dv < _dvl && _av < _avl) { \ |
d03ab969 |
683 | mpd _x; \ |
7c13f461 |
684 | _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc; \ |
d03ab969 |
685 | *_dv++ = MPW(_x); \ |
7c13f461 |
686 | _cc = _x >> MPW_BITS; \ |
d03ab969 |
687 | } \ |
7c13f461 |
688 | MPX_UADDN(_dv, _dvl, _cc); \ |
d03ab969 |
689 | } while (0) |
690 | |
dd517851 |
691 | extern void mpx_umlan(mpw */*dv*/, mpw */*dvl*/, |
692 | const mpw */*av*/, const mpw */*avl*/, mpw m); |
693 | |
3c9ede17 |
694 | /* --- @mpx_usqr@ --- * |
d03ab969 |
695 | * |
696 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
3c9ede17 |
697 | * @const mpw *av, *av@ = source vector base and limit |
d03ab969 |
698 | * |
699 | * Returns: --- |
700 | * |
3c9ede17 |
701 | * Use: Performs unsigned integer squaring. The result vector must |
702 | * not overlap the source vector in any way. |
d03ab969 |
703 | */ |
704 | |
3c9ede17 |
705 | extern void mpx_usqr(mpw */*dv*/, mpw */*dvl*/, |
706 | const mpw */*av*/, const mpw */*avl*/); |
d03ab969 |
707 | |
5bf74dea |
708 | /* --- @mpx_udiv@ --- * |
709 | * |
710 | * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit |
711 | * @mpw *rv, *rvl@ = dividend/remainder vector base and limit |
712 | * @const mpw *dv, *dvl@ = divisor vector base and limit |
713 | * @mpw *sv, *svl@ = scratch workspace |
714 | * |
715 | * Returns: --- |
716 | * |
717 | * Use: Performs unsigned integer division. If the result overflows |
718 | * the quotient vector, high-order bits are discarded. (Clearly |
719 | * the remainder vector can't overflow.) The various vectors |
720 | * may not overlap in any way. Yes, I know it's a bit odd |
721 | * requiring the dividend to be in the result position but it |
722 | * does make some sense really. The remainder must have |
723 | * headroom for at least two extra words. The scratch space |
724 | * must be at least one word larger than the divisor. |
725 | */ |
726 | |
727 | extern void mpx_udiv(mpw */*qv*/, mpw */*qvl*/, mpw */*rv*/, mpw */*rvl*/, |
728 | const mpw */*dv*/, const mpw */*dvl*/, |
729 | mpw */*sv*/, mpw */*svl*/); |
730 | |
698bd937 |
731 | /* --- @mpx_udivn@ --- * |
732 | * |
733 | * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap |
734 | * dividend) |
735 | * @const mpw *rv, *rvl@ = dividend |
736 | * @mpw d@ = single-precision divisor |
737 | * |
738 | * Returns: Remainder after divison. |
739 | * |
740 | * Use: Performs a single-precision division operation. |
741 | */ |
742 | |
743 | extern mpw mpx_udivn(mpw */*qv*/, mpw */*qvl*/, |
744 | const mpw */*rv*/, const mpw */*rvl*/, mpw /*d*/); |
745 | |
5bf74dea |
746 | /*----- Karatsuba multiplication algorithms -------------------------------*/ |
747 | |
52cdaca9 |
748 | /* --- @MPK_THRESH@ --- * |
5bf74dea |
749 | * |
750 | * This is the limiting length for using Karatsuba algorithms. It's best to |
751 | * use the simpler classical multiplication method on numbers smaller than |
dd22938e |
752 | * this. It is unsafe to make this constant less than four (i.e., the |
753 | * algorithms will fail). |
5bf74dea |
754 | */ |
755 | |
52cdaca9 |
756 | #define MPK_THRESH 16 |
5bf74dea |
757 | |
a86e33af |
758 | /* --- @mpx_kmul@ --- * |
759 | * |
760 | * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer |
761 | * @const mpw *av, *avl@ = pointer to first argument |
762 | * @const mpw *bv, *bvl@ = pointer to second argument |
763 | * @mpw *sv, *svl@ = pointer to scratch workspace |
764 | * |
765 | * Returns: --- |
766 | * |
767 | * Use: Multiplies two multiprecision integers using Karatsuba's |
768 | * algorithm. This is rather faster than traditional long |
769 | * multiplication (e.g., @mpx_umul@) on large numbers, although |
770 | * more expensive on small ones. |
771 | * |
dd22938e |
772 | * The destination must be three times as large as the larger |
773 | * argument. The scratch space must be five times as large as |
774 | * the larger argument. |
a86e33af |
775 | */ |
776 | |
a86e33af |
777 | extern void mpx_kmul(mpw */*dv*/, mpw */*dvl*/, |
778 | const mpw */*av*/, const mpw */*avl*/, |
779 | const mpw */*bv*/, const mpw */*bvl*/, |
780 | mpw */*sv*/, mpw */*svl*/); |
781 | |
5bf74dea |
782 | /* --- @mpx_ksqr@ --- * |
d03ab969 |
783 | * |
5bf74dea |
784 | * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer |
785 | * @const mpw *av, *avl@ = pointer to first argument |
786 | * @mpw *sv, *svl@ = pointer to scratch workspace |
d03ab969 |
787 | * |
788 | * Returns: --- |
789 | * |
5bf74dea |
790 | * Use: Squares a multiprecision integers using something similar to |
791 | * Karatsuba's multiplication algorithm. This is rather faster |
792 | * than traditional long multiplication (e.g., @mpx_umul@) on |
793 | * large numbers, although more expensive on small ones, and |
794 | * rather simpler than full-blown Karatsuba multiplication. |
795 | * |
dd22938e |
796 | * The destination must be three times as large as the larger |
797 | * argument. The scratch space must be five times as large as |
798 | * the larger argument. |
d03ab969 |
799 | */ |
800 | |
5bf74dea |
801 | extern void mpx_ksqr(mpw */*dv*/, mpw */*dvl*/, |
802 | const mpw */*av*/, const mpw */*avl*/, |
3c9ede17 |
803 | mpw */*sv*/, mpw */*svl*/); |
d03ab969 |
804 | |
805 | /*----- That's all, folks -------------------------------------------------*/ |
806 | |
807 | #ifdef __cplusplus |
808 | } |
809 | #endif |
810 | |
811 | #endif |