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