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