3 * Low-level multiprecision arithmetic
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
38 #include <mLib/macros.h>
45 /*----- Loading and storing -----------------------------------------------*/
47 /* --- These are all variations on a theme --- *
49 * Essentially we want to feed bits into a shift register, @ibits@ bits at a
50 * time, and extract them @obits@ bits at a time whenever there are enough.
51 * Of course, @i@ and @o@ will, in general, be different sizes, and we don't
52 * necessarily know which is larger.
54 * During an operation, we have a shift register @w@ and a most-recent input
55 * @t@. Together, these hold @bits@ significant bits of input. We arrange
56 * that @bits < ibits + obits <= 2*MPW_BITS@, so we can get away with using
57 * an @mpw@ for both of these quantitities.
60 /* --- @MPX_GETBITS@ --- *
62 * Arguments: @ibits@ = width of input units, in bits
63 * @obits@ = width of output units, in bits
64 * @iavail@ = condition expression: is input data available?
65 * @getbits@ = function or macro: set argument to next input
67 * Use: Read an input unit into @t@ and update the necessary
70 * It is assumed on entry that @bits < obits@. On exit, we have
71 * @bits < ibits + obits@, and @t@ is live.
74 #define MPX_GETBITS(ibits, obits, iavail, getbits) do { \
75 if (!iavail) goto flush; \
76 if (bits >= ibits) w |= t << (bits - ibits); \
81 /* --- @MPX_PUTBITS@ --- *
83 * Arguments: @ibits@ = width of input units, in bits
84 * @obits@ = width of output units, in bits
85 * @oavail@ = condition expression: is output space available?
86 * @putbits@ = function or macro: write its argument to output
88 * Use: Emit an output unit, and update the necessary variables. If
89 * the output buffer is full, then force an immediate return.
91 * We assume that @bits < ibits + obits@, and that @t@ is only
92 * relevant if @bits >= ibits@. (The @MPX_GETBITS@ macro
93 * ensures that this is true.)
96 #define SHRW(w, b) ((b) < MPW_BITS ? (w) >> (b) : 0)
98 #define MPX_PUTBITS(ibits, obits, oavail, putbits) do { \
99 if (!oavail) return; \
100 if (bits < ibits) { \
103 w = SHRW(w, obits); \
105 putbits(w | (t << (bits - ibits))); \
107 if (bits >= ibits) w = SHRW(w, obits) | (t << (bits - ibits)); \
108 else w = SHRW(w, obits) | (t >> (ibits - bits)); \
113 /* --- @MPX_LOADSTORE@ --- *
115 * Arguments: @name@ = name of function to create, without @mpx_@ prefix
116 * @wconst@ = qualifiers for @mpw *@ arguments
117 * @oconst@ = qualifiers for octet pointers
118 * @decls@ = additional declarations needed
119 * @ibits@ = width of input units, in bits
120 * @iavail@ = condition expression: is input data available?
121 * @getbits@ = function or macro: set argument to next input
122 * @obits@ = width of output units, in bits
123 * @oavail@ = condition expression: is output space available?
124 * @putbits@ = function or macro: write its argument to output
125 * @clear@ = statements to clear remainder of output
127 * Use: Generates a function to convert between a sequence of
128 * multiprecision words and a vector of octets.
130 * The arguments @ibits@, @iavail@ and @getbits@ are passed on
131 * to @MPX_GETBITS@; similarly, @obits@, @oavail@, and @putbits@
132 * are passed on to @MPX_PUTBITS@.
134 * The following variables are in scope: @v@ and @vl are the
135 * current base and limit of the word vector; @p@ and @q@ are
136 * the base and limit of the octet vector; @w@ and @t@ form the
137 * shift register used during the conversion (see commentary
138 * above); and @bits@ tracks the number of live bits in the
142 #define MPX_LOADSTORE(name, wconst, oconst, decls, \
143 ibits, iavail, getbits, obits, oavail, putbits, \
146 void mpx_##name(wconst mpw *v, wconst mpw *vl, \
147 oconst void *pp, size_t sz) \
150 oconst octet *p = pp, *q = p + sz; \
155 while (bits < obits) MPX_GETBITS(ibits, obits, iavail, getbits); \
156 while (bits >= obits) MPX_PUTBITS(ibits, obits, oavail, putbits); \
160 while (bits > 0) MPX_PUTBITS(ibits, obits, oavail, putbits); \
166 /* --- Macros for @getbits@ and @putbits@ --- */
168 #define GETMPW(t) do { t = *v++; } while (0)
169 #define PUTMPW(x) do { *v++ = MPW(x); } while (0)
171 #define GETOCTETI(t) do { t = *p++; } while (0)
172 #define PUTOCTETD(x) do { *--q = U8(x); } while (0)
174 #define PUTOCTETI(x) do { *p++ = U8(x); } while (0)
175 #define GETOCTETD(t) do { t = *--q; } while (0)
177 /* --- Machinery for two's complement I/O --- */
182 #define GETMPW_2CN(t) do { \
183 t = MPW(~*v++ + c); \
187 #define PUTMPW_2CN(t) do { \
188 mpw _t = MPW(~(t) + c); \
193 #define FLUSHW_2CN do { \
194 if (c) MPX_ONE(v, vl); \
195 else MPX_ZERO(v, vl); \
198 #define FLUSHO_2CN do { \
199 memset(p, c ? 0xff : 0, q - p); \
202 /* --- @mpx_storel@ --- *
204 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
205 * @void *pp@ = pointer to octet array
206 * @size_t sz@ = size of octet array
210 * Use: Stores an MP in an octet array, least significant octet
211 * first. High-end octets are silently discarded if there
212 * isn't enough space for them.
215 MPX_LOADSTORE(storel, const, EMPTY, EMPTY,
216 MPW_BITS, (v < vl), GETMPW,
217 8, (p < q), PUTOCTETI,
218 { memset(p, 0, q - p); })
220 /* --- @mpx_loadl@ --- *
222 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
223 * @const void *pp@ = pointer to octet array
224 * @size_t sz@ = size of octet array
228 * Use: Loads an MP in an octet array, least significant octet
229 * first. High-end octets are ignored if there isn't enough
233 MPX_LOADSTORE(loadl, EMPTY, const, EMPTY,
234 8, (p < q), GETOCTETI,
235 MPW_BITS, (v < vl), PUTMPW,
236 { MPX_ZERO(v, vl); })
239 /* --- @mpx_storeb@ --- *
241 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
242 * @void *pp@ = pointer to octet array
243 * @size_t sz@ = size of octet array
247 * Use: Stores an MP in an octet array, most significant octet
248 * first. High-end octets are silently discarded if there
249 * isn't enough space for them.
252 MPX_LOADSTORE(storeb, const, EMPTY, EMPTY,
253 MPW_BITS, (v < vl), GETMPW,
254 8, (p < q), PUTOCTETD,
255 { memset(p, 0, q - p); })
257 /* --- @mpx_loadb@ --- *
259 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
260 * @const void *pp@ = pointer to octet array
261 * @size_t sz@ = size of octet array
265 * Use: Loads an MP in an octet array, most significant octet
266 * first. High-end octets are ignored if there isn't enough
270 MPX_LOADSTORE(loadb, EMPTY, const, EMPTY,
271 8, (p < q), GETOCTETD,
272 MPW_BITS, (v < vl), PUTMPW,
273 { MPX_ZERO(v, vl); })
275 /* --- @mpx_storel2cn@ --- *
277 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
278 * @void *pp@ = pointer to octet array
279 * @size_t sz@ = size of octet array
283 * Use: Stores a negative MP in an octet array, least significant
284 * octet first, as two's complement. High-end octets are
285 * silently discarded if there isn't enough space for them.
286 * This obviously makes the output bad.
289 MPX_LOADSTORE(storel2cn, const, EMPTY, DECL_2CN,
290 MPW_BITS, (v < vl), GETMPW_2CN,
291 8, (p < q), PUTOCTETI,
294 /* --- @mpx_loadl2cn@ --- *
296 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
297 * @const void *pp@ = pointer to octet array
298 * @size_t sz@ = size of octet array
302 * Use: Loads a negative MP in an octet array, least significant
303 * octet first, as two's complement. High-end octets are
304 * ignored if there isn't enough space for them. This probably
305 * means you made the wrong choice coming here.
308 MPX_LOADSTORE(loadl2cn, EMPTY, const, DECL_2CN,
309 8, (p < q), GETOCTETI,
310 MPW_BITS, (v < vl), PUTMPW_2CN,
313 /* --- @mpx_storeb2cn@ --- *
315 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
316 * @void *pp@ = pointer to octet array
317 * @size_t sz@ = size of octet array
321 * Use: Stores a negative MP in an octet array, most significant
322 * octet first, as two's complement. High-end octets are
323 * silently discarded if there isn't enough space for them,
324 * which probably isn't what you meant.
327 MPX_LOADSTORE(storeb2cn, const, EMPTY, DECL_2CN,
328 MPW_BITS, (v < vl), GETMPW_2CN,
329 8, (p < q), PUTOCTETD,
332 /* --- @mpx_loadb2cn@ --- *
334 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
335 * @const void *pp@ = pointer to octet array
336 * @size_t sz@ = size of octet array
340 * Use: Loads a negative MP in an octet array, most significant octet
341 * first as two's complement. High-end octets are ignored if
342 * there isn't enough space for them. This probably means you
343 * chose this function wrongly.
346 MPX_LOADSTORE(loadb2cn, EMPTY, const, DECL_2CN,
347 8, (p < q), GETOCTETD,
348 MPW_BITS, (v < vl), PUTMPW_2CN,
351 /*----- Logical shifting --------------------------------------------------*/
353 /* --- @MPX_SHIFT1@ --- *
355 * Arguments: @init@ = initial accumulator value
356 * @out@ = expression to store in each output word
357 * @next@ = expression for next accumulator value
359 * Use: Performs a single-position shift. The input is scanned
360 * right-to-left. In the expressions @out@ and @next@, the
361 * accumulator is available in @w@ and the current input word is
364 * This macro is intended to be used in the @shift1@ argument of
365 * @MPX_SHIFTOP@, and expects variables describing the operation
366 * to be set up accordingly.
369 #define MPX_SHIFT1(init, out, next) do { \
372 if (dv >= dvl) break; \
377 if (dv < dvl) { *dv++ = MPW(w); MPX_ZERO(dv, dvl); } \
380 /* --- @MPX_SHIFTW@ --- *
382 * Arguments: @max@ = the maximum shift (in words) which is nontrivial
383 * @clear@ = function (or macro) to clear low-order output words
384 * @copy@ = statement to copy words from input to output
386 * Use: Performs a shift by a whole number of words. If the shift
387 * amount is @max@ or more words, then the destination is
388 * @clear@ed entirely; otherwise, @copy@ is executed.
390 * This macro is intended to be used in the @shiftw@ argument of
391 * @MPX_SHIFTOP@, and expects variables describing the operation
392 * to be set up accordingly.
395 #define MPX_SHIFTW(max, clear, copy) do { \
396 if (nw >= (max)) clear(dv, dvl); \
400 /* --- @MPX_SHIFTOP@ --- *
402 * Arguments: @name@ = name of function to define (without `@mpx_@' prefix)
403 * @shift1@ = statement to shift by a single bit
404 * @shiftw@ = statement to shift by a whole number of words
405 * @shift@ = statement to perform a general shift
407 * Use: Emits a shift operation. The input is @av@..@avl@; the
408 * output is @dv@..@dvl@; and the shift amount (in bits) is
409 * @n@. In @shiftw@ and @shift@, @nw@ and @nb@ are set up such
410 * that @n = nw*MPW_BITS + nb@ and @nb < MPW_BITS@.
413 #define MPX_SHIFTOP(name, shift1, shiftw, shift) \
415 void mpx_##name(mpw *dv, mpw *dvl, \
416 const mpw *av, const mpw *avl, \
421 MPX_COPY(dv, dvl, av, avl); \
423 do shift1 while (0); \
425 size_t nw = n/MPW_BITS; \
426 unsigned nb = n%MPW_BITS; \
427 if (!nb) do shiftw while (0); \
428 else do shift while (0); \
432 /* --- @MPX_SHIFT_LEFT@ --- *
434 * Arguments: @name@ = name of function to define (without `@mpx_@' prefix)
435 * @init1@ = initializer for single-bit shift accumulator
436 * @clear@ = function (or macro) to clear low-order output words
437 * @flush@ = expression for low-order nontrivial output word
439 * Use: Emits a left-shift operation. This expands to a call on
440 * @MPX_SHIFTOP@, but implements the complicated @shift@
443 * The @init1@ argument is as for @MPX_SHIFT1@, and @clear@ is
444 * as for @MPX_SHIFTW@ (though is used elsewhere). In a general
445 * shift, @nw@ whole low-order output words are set using
446 * @clear@; high-order words are zeroed; and the remaining words
447 * set with a left-to-right pass across the input; at the end of
448 * the operation, the least significant output word above those
449 * @clear@ed is set using @flush@, which may use the accumulator
450 * @w@ = @av[0] << nb@.
453 #define MPX_SHIFT_LEFT(name, init1, clear, flush) \
454 MPX_SHIFTOP(name, { \
457 t >> (MPW_BITS - 1)); \
459 MPX_SHIFTW(dvl - dv, clear, { \
460 MPX_COPY(dv + nw, dvl, av, avl); \
461 clear(dv, dv + nw); \
464 size_t nr = MPW_BITS - nb; \
465 size_t dvn = dvl - dv; \
466 size_t avn = avl - av; \
474 if (dvn <= avn + nw) { \
475 avl = av + dvn - nw; \
478 size_t off = avn + nw + 1; \
479 MPX_ZERO(dv + off, dvl); \
486 *--dvl = MPW(w | (t >> nr)); \
490 *--dvl = MPW(flush); \
494 /* --- @mpx_lsl@ --- *
496 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
497 * @const mpw *av, *avl@ = source vector base and limit
498 * @size_t n@ = number of bit positions to shift by
502 * Use: Performs a logical shift left operation on an integer.
505 MPX_SHIFT_LEFT(lsl, 0, MPX_ZERO, w)
507 /* --- @mpx_lslc@ --- *
509 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
510 * @const mpw *av, *avl@ = source vector base and limit
511 * @size_t n@ = number of bit positions to shift by
515 * Use: Performs a logical shift left operation on an integer, only
516 * it fills in the bits with ones instead of zeroes.
519 MPX_SHIFT_LEFT(lslc, 1, MPX_ONE, w | (MPW_MAX >> nr))
521 /* --- @mpx_lsr@ --- *
523 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
524 * @const mpw *av, *avl@ = source vector base and limit
525 * @size_t n@ = number of bit positions to shift by
529 * Use: Performs a logical shift right operation on an integer.
533 MPX_SHIFT1(av < avl ? *av++ >> 1 : 0,
534 w | (t << (MPW_BITS - 1)),
537 MPX_SHIFTW(avl - av, MPX_ZERO,
538 { MPX_COPY(dv, dvl, av + nw, avl); });
540 size_t nr = MPW_BITS - nb;
544 w = av < avl ? *av++ : 0;
547 if (dv >= dvl) goto done;
549 *dv++ = MPW((w >> nb) | (t << nr));
553 *dv++ = MPW(w >> nb);
559 /*----- Bitwise operations ------------------------------------------------*/
561 /* --- @mpx_bitop@ --- *
563 * Arguments: @mpw *dv, *dvl@ = destination vector
564 * @const mpw *av, *avl@ = first source vector
565 * @const mpw *bv, *bvl@ = second source vector
569 * Use; Provides the dyadic boolean functions.
572 #define MPX_BITBINOP(string) \
574 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
575 const mpw *bv, const mpw *bvl) \
577 MPX_SHRINK(av, avl); \
578 MPX_SHRINK(bv, bvl); \
582 a = (av < avl) ? *av++ : 0; \
583 b = (bv < bvl) ? *bv++ : 0; \
584 *dv++ = B##string(a, b); \
585 IGNORE(a); IGNORE(b); \
589 MPX_DOBIN(MPX_BITBINOP)
591 void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
597 a = (av < avl) ? *av++ : 0;
602 /*----- Unsigned arithmetic -----------------------------------------------*/
604 /* --- @mpx_2c@ --- *
606 * Arguments: @mpw *dv, *dvl@ = destination vector
607 * @const mpw *v, *vl@ = source vector
611 * Use: Calculates the two's complement of @v@.
614 void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl)
617 while (dv < dvl && v < vl)
618 *dv++ = c = MPW(~*v++);
625 MPX_UADDN(dv, dvl, 1);
628 /* --- @mpx_ueq@ --- *
630 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
631 * @const mpw *bv, *bvl@ = second argument vector base and limit
633 * Returns: Nonzero if the two vectors are equal.
635 * Use: Performs an unsigned integer test for equality.
638 int mpx_ueq(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
642 if (avl - av != bvl - bv)
651 /* --- @mpx_ucmp@ --- *
653 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
654 * @const mpw *bv, *bvl@ = second argument vector base and limit
656 * Returns: Less than, equal to, or greater than zero depending on
657 * whether @a@ is less than, equal to or greater than @b@,
660 * Use: Performs an unsigned integer comparison.
663 int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
668 if (avl - av > bvl - bv)
670 else if (avl - av < bvl - bv)
672 else while (avl > av) {
673 mpw a = *--avl, b = *--bvl;
682 /* --- @mpx_uadd@ --- *
684 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
685 * @const mpw *av, *avl@ = first addend vector base and limit
686 * @const mpw *bv, *bvl@ = second addend vector base and limit
690 * Use: Performs unsigned integer addition. If the result overflows
691 * the destination vector, high-order bits are discarded. This
692 * means that two's complement addition happens more or less for
693 * free, although that's more a side-effect than anything else.
694 * The result vector may be equal to either or both source
695 * vectors, but may not otherwise overlap them.
698 void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
699 const mpw *bv, const mpw *bvl)
703 while (av < avl || bv < bvl) {
708 a = (av < avl) ? *av++ : 0;
709 b = (bv < bvl) ? *bv++ : 0;
710 x = (mpd)a + (mpd)b + c;
720 /* --- @mpx_uaddn@ --- *
722 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
723 * @mpw n@ = other addend
727 * Use: Adds a small integer to a multiprecision number.
730 void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); }
732 /* --- @mpx_uaddnlsl@ --- *
734 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
735 * @mpw a@ = second argument
736 * @unsigned o@ = offset in bits
740 * Use: Computes %$d + 2^o a$%. If the result overflows then
741 * high-order bits are discarded, as usual. We must have
742 * @0 < o < MPW_BITS@.
745 void mpx_uaddnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
749 while (x && dv < dvl) {
756 /* --- @mpx_usub@ --- *
758 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
759 * @const mpw *av, *avl@ = first argument vector base and limit
760 * @const mpw *bv, *bvl@ = second argument vector base and limit
764 * Use: Performs unsigned integer subtraction. If the result
765 * overflows the destination vector, high-order bits are
766 * discarded. This means that two's complement subtraction
767 * happens more or less for free, althuogh that's more a side-
768 * effect than anything else. The result vector may be equal to
769 * either or both source vectors, but may not otherwise overlap
773 void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
774 const mpw *bv, const mpw *bvl)
778 while (av < avl || bv < bvl) {
783 a = (av < avl) ? *av++ : 0;
784 b = (bv < bvl) ? *bv++ : 0;
785 x = (mpd)a - (mpd)b - c;
798 /* --- @mpx_usubn@ --- *
800 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
805 * Use: Subtracts a small integer from a multiprecision number.
808 void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); }
810 /* --- @mpx_uaddnlsl@ --- *
812 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
813 * @mpw a@ = second argument
814 * @unsigned o@ = offset in bits
818 * Use: Computes %$d + 2^o a$%. If the result overflows then
819 * high-order bits are discarded, as usual. We must have
820 * @0 < o < MPW_BITS@.
823 void mpx_usubnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
825 mpw b = a >> (MPW_BITS - o);
829 mpd x = (mpd)*dv - MPW(a);
833 MPX_USUBN(dv, dvl, b);
837 /* --- @mpx_umul@ --- *
839 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
840 * @const mpw *av, *avl@ = multiplicand vector base and limit
841 * @const mpw *bv, *bvl@ = multiplier vector base and limit
845 * Use: Performs unsigned integer multiplication. If the result
846 * overflows the desination vector, high-order bits are
847 * discarded. The result vector may not overlap the argument
848 * vectors in any way.
851 CPU_DISPATCH(EMPTY, (void), void, mpx_umul,
852 (mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
853 const mpw *bv, const mpw *bvl),
854 (dv, dvl, av, avl, bv, bvl), pick_umul, simple_umul);
856 static void simple_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
857 const mpw *bv, const mpw *bvl)
859 /* --- This is probably worthwhile on a multiply --- */
864 /* --- Deal with a multiply by zero --- */
871 /* --- Do the initial multiply and initialize the accumulator --- */
873 MPX_UMULN(dv, dvl, av, avl, *bv++);
875 /* --- Do the remaining multiply/accumulates --- */
877 while (dv < dvl && bv < bvl) {
887 x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c;
891 MPX_UADDN(dvv, dvl, c);
896 #define MAYBE_UMUL4(impl) \
897 extern void mpx_umul4_##impl(mpw */*dv*/, \
898 const mpw */*av*/, const mpw */*avl*/, \
899 const mpw */*bv*/, const mpw */*bvl*/); \
900 static void maybe_umul4_##impl(mpw *dv, mpw *dvl, \
901 const mpw *av, const mpw *avl, \
902 const mpw *bv, const mpw *bvl) \
904 size_t an = avl - av, bn = bvl - bv, dn = dvl - dv; \
905 if (!an || an%4 != 0 || !bn || bn%4 != 0 || dn < an + bn) \
906 simple_umul(dv, dvl, av, avl, bv, bvl); \
908 mpx_umul4_##impl(dv, av, avl, bv, bvl); \
909 MPX_ZERO(dv + an + bn, dvl); \
914 MAYBE_UMUL4(x86_sse2)
918 MAYBE_UMUL4(amd64_sse2)
921 static mpx_umul__functype *pick_umul(void)
924 DISPATCH_PICK_COND(mpx_umul, maybe_umul4_x86_sse2,
925 cpu_feature_p(CPUFEAT_X86_SSE2));
928 DISPATCH_PICK_COND(mpx_umul, maybe_umul4_amd64_sse2,
929 cpu_feature_p(CPUFEAT_X86_SSE2));
931 DISPATCH_PICK_FALLBACK(mpx_umul, simple_umul);
934 /* --- @mpx_umuln@ --- *
936 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
937 * @const mpw *av, *avl@ = multiplicand vector base and limit
938 * @mpw m@ = multiplier
942 * Use: Multiplies a multiprecision integer by a single-word value.
943 * The destination and source may be equal. The destination
944 * is completely cleared after use.
947 void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
948 { MPX_UMULN(dv, dvl, av, avl, m); }
950 /* --- @mpx_umlan@ --- *
952 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
953 * @const mpw *av, *avl@ = multiplicand vector base and limit
954 * @mpw m@ = multiplier
958 * Use: Multiplies a multiprecision integer by a single-word value
959 * and adds the result to an accumulator.
962 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
963 { MPX_UMLAN(dv, dvl, av, avl, m); }
965 /* --- @mpx_usqr@ --- *
967 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
968 * @const mpw *av, *av@ = source vector base and limit
972 * Use: Performs unsigned integer squaring. The result vector must
973 * not overlap the source vector in any way.
976 void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
980 /* --- Main loop --- */
988 /* --- Stop if I've run out of destination --- */
993 /* --- Work out the square at this point in the proceedings --- */
996 mpd x = (mpd)a * (mpd)a + *dvv;
998 c = MPW(x >> MPW_BITS);
1001 /* --- Now fix up the rest of the vector upwards --- */
1004 while (dvv < dvl && avv < avl) {
1005 mpd x = (mpd)a * (mpd)*avv++;
1006 mpd y = ((x << 1) & MPW_MAX) + c + *dvv;
1007 c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS);
1010 while (dvv < dvl && c) {
1016 /* --- Get ready for the next round --- */
1023 /* --- @mpx_udiv@ --- *
1025 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1026 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1027 * @const mpw *dv, *dvl@ = divisor vector base and limit
1028 * @mpw *sv, *svl@ = scratch workspace
1032 * Use: Performs unsigned integer division. If the result overflows
1033 * the quotient vector, high-order bits are discarded. (Clearly
1034 * the remainder vector can't overflow.) The various vectors
1035 * may not overlap in any way. Yes, I know it's a bit odd
1036 * requiring the dividend to be in the result position but it
1037 * does make some sense really. The remainder must have
1038 * headroom for at least two extra words. The scratch space
1039 * must be at least one word larger than the divisor.
1042 void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
1043 const mpw *dv, const mpw *dvl,
1050 /* --- Initialize the quotient --- */
1054 /* --- Perform some sanity checks --- */
1056 MPX_SHRINK(dv, dvl);
1057 assert(((void)"division by zero in mpx_udiv", dv < dvl));
1059 /* --- Normalize the divisor --- *
1061 * The algorithm requires that the divisor be at least two digits long.
1062 * This is easy to fix.
1069 for (b = MPW_P2; b; b >>= 1) {
1070 if (d <= (MPW_MAX >> b)) {
1079 /* --- Normalize the dividend/remainder to match --- */
1082 mpx_lsl(rv, rvl, rv, rvl, norm);
1083 mpx_lsl(sv, svl, dv, dvl, norm);
1086 MPX_SHRINK(dv, dvl);
1089 MPX_SHRINK(rv, rvl);
1093 /* --- Work out the relative scales --- */
1096 size_t rvn = rvl - rv;
1097 size_t dvn = dvl - dv;
1099 /* --- If the divisor is clearly larger, notice this --- */
1102 mpx_lsr(rv, rvl, rv, rvl, norm);
1109 /* --- Calculate the most significant quotient digit --- *
1111 * Because the divisor has its top bit set, this can only happen once. The
1112 * pointer arithmetic is a little contorted, to make sure that the
1113 * behaviour is defined.
1116 if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) {
1117 mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1118 if (qvl - qv > scale)
1122 /* --- Now for the main loop --- */
1131 /* --- Get an estimate for the next quotient digit --- */
1138 rh = ((mpd)r << MPW_BITS) | rr;
1144 /* --- Refine the estimate --- */
1147 mpd yh = (mpd)d * q;
1148 mpd yy = (mpd)dd * q;
1152 yh += yy >> MPW_BITS;
1155 while (yh > rh || (yh == rh && yl > rrr)) {
1164 /* --- Remove a chunk from the dividend --- */
1171 /* --- Calculate the size of the chunk --- *
1173 * This does the whole job of calculating @r >> scale - qd@.
1176 for (svv = rv + scale, dvv = dv;
1177 dvv < dvl && svv < rvl;
1179 mpd x = (mpd)*dvv * (mpd)q + mc;
1181 x = (mpd)*svv - MPW(x) - sc;
1190 mpd x = (mpd)*svv - mc - sc;
1200 /* --- Fix if the quotient was too large --- *
1202 * This doesn't seem to happen very often.
1205 if (rvl[-1] > MPW_MAX / 2) {
1206 mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1211 /* --- Done for another iteration --- */
1213 if (qvl - qv > scale)
1220 /* --- Now fiddle with unnormalizing and things --- */
1222 mpx_lsr(rv, rvl, rv, rvl, norm);
1225 /* --- @mpx_udivn@ --- *
1227 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1229 * @const mpw *rv, *rvl@ = dividend
1230 * @mpw d@ = single-precision divisor
1232 * Returns: Remainder after divison.
1234 * Use: Performs a single-precision division operation.
1237 mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d)
1240 size_t ql = qvl - qv;
1246 r = (r << MPW_BITS) | rv[i];
1254 /*----- Test rig ----------------------------------------------------------*/
1258 #include <mLib/alloc.h>
1259 #include <mLib/dstr.h>
1260 #include <mLib/quis.h>
1261 #include <mLib/testrig.h>
1265 #define ALLOC(v, vl, sz) do { \
1266 size_t _sz = (sz); \
1267 mpw *_vv = xmalloc(MPWS(_sz)); \
1268 mpw *_vvl = _vv + _sz; \
1269 memset(_vv, 0xa5, MPWS(_sz)); \
1274 #define LOAD(v, vl, d) do { \
1275 const dstr *_d = (d); \
1277 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1278 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1283 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1285 static void dumpbits(const char *msg, const void *pp, size_t sz)
1287 const octet *p = pp;
1290 fprintf(stderr, " %02x", *p++);
1291 fputc('\n', stderr);
1294 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
1299 fprintf(stderr, " %08lx", (unsigned long)*--vl);
1300 fputc('\n', stderr);
1303 static int chkscan(const mpw *v, const mpw *vl,
1304 const void *pp, size_t sz, int step)
1307 const octet *p = pp;
1311 mpscan_initx(&mps, v, vl);
1316 for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) {
1317 if (MPSCAN_BIT(&mps) != (x & 1)) {
1319 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1320 step, bit, x & 1, MPSCAN_BIT(&mps));
1332 static int loadstore(dstr *v)
1335 size_t sz = MPW_RQ(v->len) * 2, diff;
1339 dstr_ensure(&d, v->len);
1340 m = xmalloc(MPWS(sz));
1342 for (diff = 0; diff < sz; diff += 5) {
1347 mpx_loadl(m, ml, v->buf, v->len);
1348 if (!chkscan(m, ml, v->buf, v->len, +1))
1350 MPX_OCTETS(oct, m, ml);
1351 mpx_storel(m, ml, d.buf, d.sz);
1352 if (memcmp(d.buf, v->buf, oct) != 0) {
1353 dumpbits("\n*** storel failed", d.buf, d.sz);
1357 mpx_loadb(m, ml, v->buf, v->len);
1358 if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1))
1360 MPX_OCTETS(oct, m, ml);
1361 mpx_storeb(m, ml, d.buf, d.sz);
1362 if (memcmp(d.buf + d.sz - oct, v->buf + v->len - oct, oct) != 0) {
1363 dumpbits("\n*** storeb failed", d.buf, d.sz);
1369 dumpbits("input data", v->buf, v->len);
1376 static int twocl(dstr *v)
1383 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1384 dstr_ensure(&d, sz);
1387 m = xmalloc(MPWS(sz));
1390 mpx_loadl(m, ml, v[0].buf, v[0].len);
1391 mpx_storel2cn(m, ml, d.buf, v[1].len);
1392 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1393 dumpbits("\n*** storel2cn failed", d.buf, v[1].len);
1397 mpx_loadl2cn(m, ml, v[1].buf, v[1].len);
1398 mpx_storel(m, ml, d.buf, v[0].len);
1399 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1400 dumpbits("\n*** loadl2cn failed", d.buf, v[0].len);
1405 dumpbits("pos", v[0].buf, v[0].len);
1406 dumpbits("neg", v[1].buf, v[1].len);
1415 static int twocb(dstr *v)
1422 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1423 dstr_ensure(&d, sz);
1426 m = xmalloc(MPWS(sz));
1429 mpx_loadb(m, ml, v[0].buf, v[0].len);
1430 mpx_storeb2cn(m, ml, d.buf, v[1].len);
1431 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1432 dumpbits("\n*** storeb2cn failed", d.buf, v[1].len);
1436 mpx_loadb2cn(m, ml, v[1].buf, v[1].len);
1437 mpx_storeb(m, ml, d.buf, v[0].len);
1438 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1439 dumpbits("\n*** loadb2cn failed", d.buf, v[0].len);
1444 dumpbits("pos", v[0].buf, v[0].len);
1445 dumpbits("neg", v[1].buf, v[1].len);
1454 static int lsl(dstr *v)
1457 int n = *(int *)v[1].buf;
1464 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1466 mpx_lsl(d, dl, a, al, n);
1467 if (!mpx_ueq(d, dl, c, cl)) {
1468 fprintf(stderr, "\n*** lsl(%i) failed\n", n);
1469 dumpmp(" a", a, al);
1470 dumpmp("expected", c, cl);
1471 dumpmp(" result", d, dl);
1475 xfree(a); xfree(c); xfree(d);
1479 static int lslc(dstr *v)
1482 int n = *(int *)v[1].buf;
1489 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1491 mpx_lslc(d, dl, a, al, n);
1492 if (!mpx_ueq(d, dl, c, cl)) {
1493 fprintf(stderr, "\n*** lslc(%i) failed\n", n);
1494 dumpmp(" a", a, al);
1495 dumpmp("expected", c, cl);
1496 dumpmp(" result", d, dl);
1500 xfree(a); xfree(c); xfree(d);
1504 static int lsr(dstr *v)
1507 int n = *(int *)v[1].buf;
1514 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1);
1516 mpx_lsr(d, dl, a, al, n);
1517 if (!mpx_ueq(d, dl, c, cl)) {
1518 fprintf(stderr, "\n*** lsr(%i) failed\n", n);
1519 dumpmp(" a", a, al);
1520 dumpmp("expected", c, cl);
1521 dumpmp(" result", d, dl);
1525 xfree(a); xfree(c); xfree(d);
1529 static int uadd(dstr *v)
1540 ALLOC(d, dl, MAX(al - a, bl - b) + 1);
1542 mpx_uadd(d, dl, a, al, b, bl);
1543 if (!mpx_ueq(d, dl, c, cl)) {
1544 fprintf(stderr, "\n*** uadd failed\n");
1545 dumpmp(" a", a, al);
1546 dumpmp(" b", b, bl);
1547 dumpmp("expected", c, cl);
1548 dumpmp(" result", d, dl);
1552 xfree(a); xfree(b); xfree(c); xfree(d);
1556 static int usub(dstr *v)
1567 ALLOC(d, dl, al - a);
1569 mpx_usub(d, dl, a, al, b, bl);
1570 if (!mpx_ueq(d, dl, c, cl)) {
1571 fprintf(stderr, "\n*** usub failed\n");
1572 dumpmp(" a", a, al);
1573 dumpmp(" b", b, bl);
1574 dumpmp("expected", c, cl);
1575 dumpmp(" result", d, dl);
1579 xfree(a); xfree(b); xfree(c); xfree(d);
1583 static int umul(dstr *v)
1594 ALLOC(d, dl, (al - a) + (bl - b));
1596 mpx_umul(d, dl, a, al, b, bl);
1597 if (!mpx_ueq(d, dl, c, cl)) {
1598 fprintf(stderr, "\n*** umul failed\n");
1599 dumpmp(" a", a, al);
1600 dumpmp(" b", b, bl);
1601 dumpmp("expected", c, cl);
1602 dumpmp(" result", d, dl);
1606 xfree(a); xfree(b); xfree(c); xfree(d);
1610 static int usqr(dstr *v)
1619 ALLOC(d, dl, 2 * (al - a));
1621 mpx_usqr(d, dl, a, al);
1622 if (!mpx_ueq(d, dl, c, cl)) {
1623 fprintf(stderr, "\n*** usqr failed\n");
1624 dumpmp(" a", a, al);
1625 dumpmp("expected", c, cl);
1626 dumpmp(" result", d, dl);
1630 xfree(a); xfree(c); xfree(d);
1634 static int udiv(dstr *v)
1644 ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len);
1648 ALLOC(qq, qql, al - a);
1649 ALLOC(s, sl, (bl - b) + 1);
1651 mpx_udiv(qq, qql, a, al, b, bl, s, sl);
1652 if (!mpx_ueq(qq, qql, q, ql) ||
1653 !mpx_ueq(a, al, r, rl)) {
1654 fprintf(stderr, "\n*** udiv failed\n");
1655 dumpmp(" divisor", b, bl);
1656 dumpmp("expect r", r, rl);
1657 dumpmp("result r", a, al);
1658 dumpmp("expect q", q, ql);
1659 dumpmp("result q", qq, qql);
1663 xfree(a); xfree(b); xfree(r); xfree(q); xfree(s); xfree(qq);
1667 static test_chunk defs[] = {
1668 { "load-store", loadstore, { &type_hex, 0 } },
1669 { "2cl", twocl, { &type_hex, &type_hex, } },
1670 { "2cb", twocb, { &type_hex, &type_hex, } },
1671 { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } },
1672 { "lslc", lslc, { &type_hex, &type_int, &type_hex, 0 } },
1673 { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } },
1674 { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } },
1675 { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } },
1676 { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
1677 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
1678 { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } },
1682 int main(int argc, char *argv[])
1684 test_run(argc, argv, defs, SRCDIR"/t/mpx");
1690 /*----- That's all, folks -------------------------------------------------*/