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