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 * @fixfinal@ = statements to fix shift register at the end
126 * @clear@ = statements to clear remainder of output
128 * Use: Generates a function to convert between a sequence of
129 * multiprecision words and a vector of octets.
131 * The arguments @ibits@, @iavail@ and @getbits@ are passed on
132 * to @MPX_GETBITS@; similarly, @obits@, @oavail@, and @putbits@
133 * are passed on to @MPX_PUTBITS@.
135 * The following variables are in scope: @v@ and @vl are the
136 * current base and limit of the word vector; @p@ and @q@ are
137 * the base and limit of the octet vector; @w@ and @t@ form the
138 * shift register used during the conversion (see commentary
139 * above); and @bits@ tracks the number of live bits in the
143 #define MPX_LOADSTORE(name, wconst, oconst, decls, \
144 ibits, iavail, getbits, obits, oavail, putbits, \
147 void mpx_##name(wconst mpw *v, wconst mpw *vl, \
148 oconst void *pp, size_t sz) \
151 oconst octet *p = pp, *q = p + sz; \
156 while (bits < obits) MPX_GETBITS(ibits, obits, iavail, getbits); \
157 while (bits >= obits) MPX_PUTBITS(ibits, obits, oavail, putbits); \
163 while (bits > 0) MPX_PUTBITS(ibits, obits, oavail, putbits); \
170 /* --- Macros for @getbits@ and @putbits@ --- */
172 #define GETMPW(t) do { t = *v++; } while (0)
173 #define PUTMPW(x) do { *v++ = MPW(x); } while (0)
175 #define GETOCTETI(t) do { t = *p++; } while (0)
176 #define PUTOCTETD(x) do { *--q = U8(x); } while (0)
178 #define PUTOCTETI(x) do { *p++ = U8(x); } while (0)
179 #define GETOCTETD(t) do { t = *--q; } while (0)
181 /* --- Machinery for two's complement I/O --- */
186 #define GETMPW_2CN(t) do { \
187 t = MPW(~*v++ + c); \
191 #define PUTMPW_2CN(t) do { \
192 mpw _t = MPW(~(t) + c); \
197 #define FIXFINALW_2CN do { \
198 if (c && !w && !t); \
199 else if (bits == 8) t ^= ~(mpw)0xffu; \
200 else t ^= ((mpw)1 << (MPW_BITS - bits + 8)) - 256u; \
203 #define FLUSHO_2CN do { \
204 memset(p, c ? 0 : 0xff, q - p); \
207 /* --- @mpx_storel@ --- *
209 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
210 * @void *pp@ = pointer to octet array
211 * @size_t sz@ = size of octet array
215 * Use: Stores an MP in an octet array, least significant octet
216 * first. High-end octets are silently discarded if there
217 * isn't enough space for them.
220 MPX_LOADSTORE(storel, const, EMPTY, EMPTY,
221 MPW_BITS, (v < vl), GETMPW,
222 8, (p < q), PUTOCTETI,
223 EMPTY, { memset(p, 0, q - p); })
225 /* --- @mpx_loadl@ --- *
227 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
228 * @const void *pp@ = pointer to octet array
229 * @size_t sz@ = size of octet array
233 * Use: Loads an MP in an octet array, least significant octet
234 * first. High-end octets are ignored if there isn't enough
238 MPX_LOADSTORE(loadl, EMPTY, const, EMPTY,
239 8, (p < q), GETOCTETI,
240 MPW_BITS, (v < vl), PUTMPW,
241 EMPTY, { MPX_ZERO(v, vl); })
244 /* --- @mpx_storeb@ --- *
246 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
247 * @void *pp@ = pointer to octet array
248 * @size_t sz@ = size of octet array
252 * Use: Stores an MP in an octet array, most significant octet
253 * first. High-end octets are silently discarded if there
254 * isn't enough space for them.
257 MPX_LOADSTORE(storeb, const, EMPTY, EMPTY,
258 MPW_BITS, (v < vl), GETMPW,
259 8, (p < q), PUTOCTETD,
260 EMPTY, { memset(p, 0, q - p); })
262 /* --- @mpx_loadb@ --- *
264 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
265 * @const void *pp@ = pointer to octet array
266 * @size_t sz@ = size of octet array
270 * Use: Loads an MP in an octet array, most significant octet
271 * first. High-end octets are ignored if there isn't enough
275 MPX_LOADSTORE(loadb, EMPTY, const, EMPTY,
276 8, (p < q), GETOCTETD,
277 MPW_BITS, (v < vl), PUTMPW,
278 EMPTY, { MPX_ZERO(v, vl); })
280 /* --- @mpx_storel2cn@ --- *
282 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
283 * @void *pp@ = pointer to octet array
284 * @size_t sz@ = size of octet array
288 * Use: Stores a negative MP in an octet array, least significant
289 * octet first, as two's complement. High-end octets are
290 * silently discarded if there isn't enough space for them.
291 * This obviously makes the output bad.
294 MPX_LOADSTORE(storel2cn, const, EMPTY, DECL_2CN,
295 MPW_BITS, (v < vl), GETMPW_2CN,
296 8, (p < q), PUTOCTETI,
297 EMPTY, { FLUSHO_2CN; })
299 /* --- @mpx_loadl2cn@ --- *
301 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
302 * @const void *pp@ = pointer to octet array
303 * @size_t sz@ = size of octet array
307 * Use: Loads a negative MP in an octet array, least significant
308 * octet first, as two's complement. High-end octets are
309 * ignored if there isn't enough space for them. This probably
310 * means you made the wrong choice coming here.
313 MPX_LOADSTORE(loadl2cn, EMPTY, const, DECL_2CN,
314 8, (p < q), GETOCTETI,
315 MPW_BITS, (v < vl), PUTMPW_2CN,
316 { FIXFINALW_2CN; }, { MPX_ZERO(v, vl); })
318 /* --- @mpx_storeb2cn@ --- *
320 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
321 * @void *pp@ = pointer to octet array
322 * @size_t sz@ = size of octet array
326 * Use: Stores a negative MP in an octet array, most significant
327 * octet first, as two's complement. High-end octets are
328 * silently discarded if there isn't enough space for them,
329 * which probably isn't what you meant.
332 MPX_LOADSTORE(storeb2cn, const, EMPTY, DECL_2CN,
333 MPW_BITS, (v < vl), GETMPW_2CN,
334 8, (p < q), PUTOCTETD,
335 EMPTY, { FLUSHO_2CN; })
337 /* --- @mpx_loadb2cn@ --- *
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
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.
351 MPX_LOADSTORE(loadb2cn, EMPTY, const, DECL_2CN,
352 8, (p < q), GETOCTETD,
353 MPW_BITS, (v < vl), PUTMPW_2CN,
354 { FIXFINALW_2CN; }, { MPX_ZERO(v, vl); })
356 /*----- Logical shifting --------------------------------------------------*/
358 /* --- @MPX_SHIFT1@ --- *
360 * Arguments: @init@ = initial accumulator value
361 * @out@ = expression to store in each output word
362 * @next@ = expression for next accumulator value
364 * Use: Performs a single-position shift. The input is scanned
365 * right-to-left. In the expressions @out@ and @next@, the
366 * accumulator is available in @w@ and the current input word is
369 * This macro is intended to be used in the @shift1@ argument of
370 * @MPX_SHIFTOP@, and expects variables describing the operation
371 * to be set up accordingly.
374 #define MPX_SHIFT1(init, out, next) do { \
377 if (dv >= dvl) break; \
382 if (dv < dvl) { *dv++ = MPW(w); MPX_ZERO(dv, dvl); } \
385 /* --- @MPX_SHIFTW@ --- *
387 * Arguments: @max@ = the maximum shift (in words) which is nontrivial
388 * @clear@ = function (or macro) to clear low-order output words
389 * @copy@ = statement to copy words from input to output
391 * Use: Performs a shift by a whole number of words. If the shift
392 * amount is @max@ or more words, then the destination is
393 * @clear@ed entirely; otherwise, @copy@ is executed.
395 * This macro is intended to be used in the @shiftw@ argument of
396 * @MPX_SHIFTOP@, and expects variables describing the operation
397 * to be set up accordingly.
400 #define MPX_SHIFTW(max, clear, copy) do { \
401 if (nw >= (max)) clear(dv, dvl); \
405 /* --- @MPX_SHIFTOP@ --- *
407 * Arguments: @name@ = name of function to define (without `@mpx_@' prefix)
408 * @shift1@ = statement to shift by a single bit
409 * @shiftw@ = statement to shift by a whole number of words
410 * @shift@ = statement to perform a general shift
412 * Use: Emits a shift operation. The input is @av@..@avl@; the
413 * output is @dv@..@dvl@; and the shift amount (in bits) is
414 * @n@. In @shiftw@ and @shift@, @nw@ and @nb@ are set up such
415 * that @n = nw*MPW_BITS + nb@ and @nb < MPW_BITS@.
418 #define MPX_SHIFTOP(name, shift1, shiftw, shift) \
420 void mpx_##name(mpw *dv, mpw *dvl, \
421 const mpw *av, const mpw *avl, \
426 MPX_COPY(dv, dvl, av, avl); \
428 do shift1 while (0); \
430 size_t nw = n/MPW_BITS; \
431 unsigned nb = n%MPW_BITS; \
432 if (!nb) do shiftw while (0); \
433 else do shift while (0); \
437 /* --- @MPX_SHIFT_LEFT@ --- *
439 * Arguments: @name@ = name of function to define (without `@mpx_@' prefix)
440 * @init1@ = initializer for single-bit shift accumulator
441 * @clear@ = function (or macro) to clear low-order output words
442 * @flush@ = expression for low-order nontrivial output word
444 * Use: Emits a left-shift operation. This expands to a call on
445 * @MPX_SHIFTOP@, but implements the complicated @shift@
448 * The @init1@ argument is as for @MPX_SHIFT1@, and @clear@ is
449 * as for @MPX_SHIFTW@ (though is used elsewhere). In a general
450 * shift, @nw@ whole low-order output words are set using
451 * @clear@; high-order words are zeroed; and the remaining words
452 * set with a left-to-right pass across the input; at the end of
453 * the operation, the least significant output word above those
454 * @clear@ed is set using @flush@, which may use the accumulator
455 * @w@ = @av[0] << nb@.
458 #define MPX_SHIFT_LEFT(name, init1, clear, flush) \
459 MPX_SHIFTOP(name, { \
462 t >> (MPW_BITS - 1)); \
464 MPX_SHIFTW(dvl - dv, clear, { \
465 MPX_COPY(dv + nw, dvl, av, avl); \
466 clear(dv, dv + nw); \
469 size_t nr = MPW_BITS - nb; \
470 size_t dvn = dvl - dv; \
471 size_t avn = avl - av; \
479 if (dvn <= avn + nw) { \
480 avl = av + dvn - nw; \
483 size_t off = avn + nw + 1; \
484 MPX_ZERO(dv + off, dvl); \
491 *--dvl = MPW(w | (t >> nr)); \
495 *--dvl = MPW(flush); \
499 /* --- @mpx_lsl@ --- *
501 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
502 * @const mpw *av, *avl@ = source vector base and limit
503 * @size_t n@ = number of bit positions to shift by
507 * Use: Performs a logical shift left operation on an integer.
510 MPX_SHIFT_LEFT(lsl, 0, MPX_ZERO, w)
512 /* --- @mpx_lslc@ --- *
514 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
515 * @const mpw *av, *avl@ = source vector base and limit
516 * @size_t n@ = number of bit positions to shift by
520 * Use: Performs a logical shift left operation on an integer, only
521 * it fills in the bits with ones instead of zeroes.
524 MPX_SHIFT_LEFT(lslc, 1, MPX_ONE, w | (MPW_MAX >> nr))
526 /* --- @mpx_lsr@ --- *
528 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
529 * @const mpw *av, *avl@ = source vector base and limit
530 * @size_t n@ = number of bit positions to shift by
534 * Use: Performs a logical shift right operation on an integer.
538 MPX_SHIFT1(av < avl ? *av++ >> 1 : 0,
539 w | (t << (MPW_BITS - 1)),
542 MPX_SHIFTW(avl - av, MPX_ZERO,
543 { MPX_COPY(dv, dvl, av + nw, avl); });
545 size_t nr = MPW_BITS - nb;
556 if (dv >= dvl) goto done;
558 *dv++ = MPW((w >> nb) | (t << nr));
564 *dv++ = MPW(w >> nb);
570 /*----- Bitwise operations ------------------------------------------------*/
572 /* --- @mpx_bitop@ --- *
574 * Arguments: @mpw *dv, *dvl@ = destination vector
575 * @const mpw *av, *avl@ = first source vector
576 * @const mpw *bv, *bvl@ = second source vector
580 * Use; Provides the dyadic boolean functions.
583 #define MPX_BITBINOP(string) \
585 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
586 const mpw *bv, const mpw *bvl) \
588 MPX_SHRINK(av, avl); \
589 MPX_SHRINK(bv, bvl); \
593 a = (av < avl) ? *av++ : 0; \
594 b = (bv < bvl) ? *bv++ : 0; \
595 *dv++ = B##string(a, b); \
596 IGNORE(a); IGNORE(b); \
600 MPX_DOBIN(MPX_BITBINOP)
602 void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
608 a = (av < avl) ? *av++ : 0;
613 /*----- Unsigned arithmetic -----------------------------------------------*/
615 /* --- @mpx_2c@ --- *
617 * Arguments: @mpw *dv, *dvl@ = destination vector
618 * @const mpw *v, *vl@ = source vector
622 * Use: Calculates the two's complement of @v@.
625 void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl)
628 while (dv < dvl && v < vl)
629 *dv++ = c = MPW(~*v++);
636 MPX_UADDN(dv, dvl, 1);
639 /* --- @mpx_ueq@ --- *
641 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
642 * @const mpw *bv, *bvl@ = second argument vector base and limit
644 * Returns: Nonzero if the two vectors are equal.
646 * Use: Performs an unsigned integer test for equality.
649 int mpx_ueq(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
653 if (avl - av != bvl - bv)
662 /* --- @mpx_ucmp@ --- *
664 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
665 * @const mpw *bv, *bvl@ = second argument vector base and limit
667 * Returns: Less than, equal to, or greater than zero depending on
668 * whether @a@ is less than, equal to or greater than @b@,
671 * Use: Performs an unsigned integer comparison.
674 int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
679 if (avl - av > bvl - bv)
681 else if (avl - av < bvl - bv)
683 else while (avl > av) {
684 mpw a = *--avl, b = *--bvl;
693 /* --- @mpx_uadd@ --- *
695 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
696 * @const mpw *av, *avl@ = first addend vector base and limit
697 * @const mpw *bv, *bvl@ = second addend vector base and limit
701 * Use: Performs unsigned integer addition. If the result overflows
702 * the destination vector, high-order bits are discarded. This
703 * means that two's complement addition happens more or less for
704 * free, although that's more a side-effect than anything else.
705 * The result vector may be equal to either or both source
706 * vectors, but may not otherwise overlap them.
709 void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
710 const mpw *bv, const mpw *bvl)
714 while (av < avl || bv < bvl) {
719 a = (av < avl) ? *av++ : 0;
720 b = (bv < bvl) ? *bv++ : 0;
721 x = (mpd)a + (mpd)b + c;
731 /* --- @mpx_uaddn@ --- *
733 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
734 * @mpw n@ = other addend
738 * Use: Adds a small integer to a multiprecision number.
741 void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); }
743 /* --- @mpx_uaddnlsl@ --- *
745 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
746 * @mpw a@ = second argument
747 * @unsigned o@ = offset in bits
751 * Use: Computes %$d + 2^o a$%. If the result overflows then
752 * high-order bits are discarded, as usual. We must have
753 * @0 < o < MPW_BITS@.
756 void mpx_uaddnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
760 while (x && dv < dvl) {
767 /* --- @mpx_usub@ --- *
769 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
770 * @const mpw *av, *avl@ = first argument vector base and limit
771 * @const mpw *bv, *bvl@ = second argument vector base and limit
775 * Use: Performs unsigned integer subtraction. If the result
776 * overflows the destination vector, high-order bits are
777 * discarded. This means that two's complement subtraction
778 * happens more or less for free, althuogh that's more a side-
779 * effect than anything else. The result vector may be equal to
780 * either or both source vectors, but may not otherwise overlap
784 void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
785 const mpw *bv, const mpw *bvl)
789 while (av < avl || bv < bvl) {
794 a = (av < avl) ? *av++ : 0;
795 b = (bv < bvl) ? *bv++ : 0;
796 x = (mpd)a - (mpd)b - c;
809 /* --- @mpx_usubn@ --- *
811 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
816 * Use: Subtracts a small integer from a multiprecision number.
819 void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); }
821 /* --- @mpx_usubnlsl@ --- *
823 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
824 * @mpw a@ = second argument
825 * @unsigned o@ = offset in bits
829 * Use: Computes %$d + 2^o a$%. If the result overflows then
830 * high-order bits are discarded, as usual. We must have
831 * @0 < o < MPW_BITS@.
834 void mpx_usubnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
836 mpw b = a >> (MPW_BITS - o);
840 mpd x = (mpd)*dv - MPW(a);
844 MPX_USUBN(dv, dvl, b);
848 /* --- @mpx_umul@ --- *
850 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
851 * @const mpw *av, *avl@ = multiplicand vector base and limit
852 * @const mpw *bv, *bvl@ = multiplier vector base and limit
856 * Use: Performs unsigned integer multiplication. If the result
857 * overflows the desination vector, high-order bits are
858 * discarded. The result vector may not overlap the argument
859 * vectors in any way.
862 CPU_DISPATCH(EMPTY, (void), void, mpx_umul,
863 (mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
864 const mpw *bv, const mpw *bvl),
865 (dv, dvl, av, avl, bv, bvl), pick_umul, simple_umul);
867 static void simple_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
868 const mpw *bv, const mpw *bvl)
870 /* --- This is probably worthwhile on a multiply --- */
875 /* --- Deal with a multiply by zero --- */
882 /* --- Do the initial multiply and initialize the accumulator --- */
884 MPX_UMULN(dv, dvl, av, avl, *bv++);
886 /* --- Do the remaining multiply/accumulates --- */
888 while (dv < dvl && bv < bvl) {
898 x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c;
902 MPX_UADDN(dvv, dvl, c);
907 #define MAYBE_UMUL4(impl) \
908 extern void mpx_umul4_##impl(mpw */*dv*/, \
909 const mpw */*av*/, const mpw */*avl*/, \
910 const mpw */*bv*/, const mpw */*bvl*/); \
911 static void maybe_umul4_##impl(mpw *dv, mpw *dvl, \
912 const mpw *av, const mpw *avl, \
913 const mpw *bv, const mpw *bvl) \
915 size_t an = avl - av, bn = bvl - bv, dn = dvl - dv; \
916 if (!an || an%4 != 0 || !bn || bn%4 != 0 || dn < an + bn) \
917 simple_umul(dv, dvl, av, avl, bv, bvl); \
919 mpx_umul4_##impl(dv, av, avl, bv, bvl); \
920 MPX_ZERO(dv + an + bn, dvl); \
925 MAYBE_UMUL4(x86_sse2)
930 MAYBE_UMUL4(amd64_sse2)
931 MAYBE_UMUL4(amd64_avx)
934 static mpx_umul__functype *pick_umul(void)
937 DISPATCH_PICK_COND(mpx_umul, maybe_umul4_x86_avx,
938 cpu_feature_p(CPUFEAT_X86_AVX));
939 DISPATCH_PICK_COND(mpx_umul, maybe_umul4_x86_sse2,
940 cpu_feature_p(CPUFEAT_X86_SSE2));
943 DISPATCH_PICK_COND(mpx_umul, maybe_umul4_amd64_avx,
944 cpu_feature_p(CPUFEAT_X86_AVX));
945 DISPATCH_PICK_COND(mpx_umul, maybe_umul4_amd64_sse2,
946 cpu_feature_p(CPUFEAT_X86_SSE2));
948 DISPATCH_PICK_FALLBACK(mpx_umul, simple_umul);
951 /* --- @mpx_umuln@ --- *
953 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
954 * @const mpw *av, *avl@ = multiplicand vector base and limit
955 * @mpw m@ = multiplier
959 * Use: Multiplies a multiprecision integer by a single-word value.
960 * The destination and source may be equal. The destination
961 * is completely cleared after use.
964 void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
965 { MPX_UMULN(dv, dvl, av, avl, m); }
967 /* --- @mpx_umlan@ --- *
969 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
970 * @const mpw *av, *avl@ = multiplicand vector base and limit
971 * @mpw m@ = multiplier
975 * Use: Multiplies a multiprecision integer by a single-word value
976 * and adds the result to an accumulator.
979 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
980 { MPX_UMLAN(dv, dvl, av, avl, m); }
982 /* --- @mpx_usqr@ --- *
984 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
985 * @const mpw *av, *av@ = source vector base and limit
989 * Use: Performs unsigned integer squaring. The result vector must
990 * not overlap the source vector in any way.
993 void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
997 /* --- Main loop --- */
1000 const mpw *avv = av;
1005 /* --- Stop if I've run out of destination --- */
1010 /* --- Work out the square at this point in the proceedings --- */
1013 mpd x = (mpd)a * (mpd)a + *dvv;
1015 c = MPW(x >> MPW_BITS);
1018 /* --- Now fix up the rest of the vector upwards --- */
1021 while (dvv < dvl && avv < avl) {
1022 mpd x = (mpd)a * (mpd)*avv++;
1023 mpd y = ((x << 1) & MPW_MAX) + c + *dvv;
1024 c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS);
1027 while (dvv < dvl && c) {
1033 /* --- Get ready for the next round --- */
1040 /* --- @mpx_udiv@ --- *
1042 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1043 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1044 * @const mpw *dv, *dvl@ = divisor vector base and limit
1045 * @mpw *sv, *svl@ = scratch workspace
1049 * Use: Performs unsigned integer division. If the result overflows
1050 * the quotient vector, high-order bits are discarded. (Clearly
1051 * the remainder vector can't overflow.) The various vectors
1052 * may not overlap in any way. Yes, I know it's a bit odd
1053 * requiring the dividend to be in the result position but it
1054 * does make some sense really. The remainder must have
1055 * headroom for at least two extra words. The scratch space
1056 * must be at least one word larger than the divisor.
1059 void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
1060 const mpw *dv, const mpw *dvl,
1067 /* --- Initialize the quotient --- */
1071 /* --- Perform some sanity checks --- */
1073 MPX_SHRINK(dv, dvl);
1074 assert(((void)"division by zero in mpx_udiv", dv < dvl));
1076 /* --- Normalize the divisor --- *
1078 * The algorithm requires that the divisor be at least two digits long.
1079 * This is easy to fix.
1086 for (b = MPW_P2; b; b >>= 1) {
1087 if (d <= (MPW_MAX >> b)) {
1096 /* --- Normalize the dividend/remainder to match --- */
1099 mpx_lsl(rv, rvl, rv, rvl, norm);
1100 mpx_lsl(sv, svl, dv, dvl, norm);
1103 MPX_SHRINK(dv, dvl);
1106 MPX_SHRINK(rv, rvl);
1110 /* --- Work out the relative scales --- */
1113 size_t rvn = rvl - rv;
1114 size_t dvn = dvl - dv;
1116 /* --- If the divisor is clearly larger, notice this --- */
1119 mpx_lsr(rv, rvl, rv, rvl, norm);
1126 /* --- Calculate the most significant quotient digit --- *
1128 * Because the divisor has its top bit set, this can only happen once. The
1129 * pointer arithmetic is a little contorted, to make sure that the
1130 * behaviour is defined.
1133 if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) {
1134 mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1135 if (qvl - qv > scale)
1139 /* --- Now for the main loop --- */
1148 /* --- Get an estimate for the next quotient digit --- */
1155 rh = ((mpd)r << MPW_BITS) | rr;
1161 /* --- Refine the estimate --- */
1164 mpd yh = (mpd)d * q;
1165 mpd yy = (mpd)dd * q;
1169 yh += yy >> MPW_BITS;
1172 while (yh > rh || (yh == rh && yl > rrr)) {
1181 /* --- Remove a chunk from the dividend --- */
1188 /* --- Calculate the size of the chunk --- *
1190 * This does the whole job of calculating @r >> scale - qd@.
1193 for (svv = rv + scale, dvv = dv;
1194 dvv < dvl && svv < rvl;
1196 mpd x = (mpd)*dvv * (mpd)q + mc;
1198 x = (mpd)*svv - MPW(x) - sc;
1207 mpd x = (mpd)*svv - mc - sc;
1217 /* --- Fix if the quotient was too large --- *
1219 * This doesn't seem to happen very often.
1222 if (rvl[-1] > MPW_MAX / 2) {
1223 mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1228 /* --- Done for another iteration --- */
1230 if (qvl - qv > scale)
1237 /* --- Now fiddle with unnormalizing and things --- */
1239 mpx_lsr(rv, rvl, rv, rvl, norm);
1242 /* --- @mpx_udivn@ --- *
1244 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1246 * @const mpw *rv, *rvl@ = dividend
1247 * @mpw d@ = single-precision divisor
1249 * Returns: Remainder after divison.
1251 * Use: Performs a single-precision division operation.
1254 mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d)
1257 size_t ql = qvl - qv;
1263 r = (r << MPW_BITS) | rv[i];
1271 /*----- Test rig ----------------------------------------------------------*/
1275 #include <mLib/alloc.h>
1276 #include <mLib/dstr.h>
1277 #include <mLib/macros.h>
1278 #include <mLib/quis.h>
1279 #include <mLib/testrig.h>
1283 #define ALLOC(v, vl, sz) do { \
1284 size_t _sz = (sz); \
1285 mpw *_vv = xmalloc(MPWS(_sz)); \
1286 mpw *_vvl = _vv + _sz; \
1287 memset(_vv, 0xa5, MPWS(_sz)); \
1292 #define LOAD(v, vl, d) do { \
1293 const dstr *_d = (d); \
1295 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1296 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1301 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1303 static void dumpbits(const char *msg, const void *pp, size_t sz)
1305 const octet *p = pp;
1308 fprintf(stderr, " %02x", *p++);
1309 fputc('\n', stderr);
1312 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
1317 fprintf(stderr, " %08lx", (unsigned long)*--vl);
1318 fputc('\n', stderr);
1321 static int chkscan(const mpw *v, const mpw *vl,
1322 const void *pp, size_t sz, int step)
1325 const octet *p = pp;
1329 mpscan_initx(&mps, v, vl);
1334 for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) {
1335 if (MPSCAN_BIT(&mps) != (x & 1)) {
1337 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1338 step, bit, x & 1, MPSCAN_BIT(&mps));
1350 static int loadstore(dstr *v)
1353 size_t sz = MPW_RQ(v->len) * 2, diff;
1357 dstr_ensure(&d, v->len);
1358 m = xmalloc(MPWS(sz));
1360 for (diff = 0; diff < sz; diff += 5) {
1365 mpx_loadl(m, ml, v->buf, v->len);
1366 if (!chkscan(m, ml, v->buf, v->len, +1))
1368 MPX_OCTETS(oct, m, ml);
1369 mpx_storel(m, ml, d.buf, d.sz);
1370 if (MEMCMP(d.buf, !=, v->buf, oct)) {
1371 dumpbits("\n*** storel failed", d.buf, d.sz);
1375 mpx_loadb(m, ml, v->buf, v->len);
1376 if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1))
1378 MPX_OCTETS(oct, m, ml);
1379 mpx_storeb(m, ml, d.buf, d.sz);
1380 if (MEMCMP(d.buf + d.sz - oct, !=, v->buf + v->len - oct, oct)) {
1381 dumpbits("\n*** storeb failed", d.buf, d.sz);
1387 dumpbits("input data", v->buf, v->len);
1394 static int twocl(dstr *v)
1398 size_t sz0, sz1, szmax;
1402 sz0 = MPW_RQ(v[0].len); sz1 = MPW_RQ(v[1].len);
1403 dstr_ensure(&d, v[0].len > v[1].len ? v[0].len : v[1].len);
1405 szmax = sz0 > sz1 ? sz0 : sz1;
1406 m = xmalloc(MPWS(szmax));
1407 ml0 = m + sz0; ml1 = m + sz1;
1409 for (i = 0; i < 2; i++) {
1410 if (i) ml0 = ml1 = m + szmax;
1412 mpx_loadl(m, ml0, v[0].buf, v[0].len);
1413 mpx_storel2cn(m, ml0, d.buf, v[1].len);
1414 if (MEMCMP(d.buf, !=, v[1].buf, v[1].len)) {
1415 dumpbits("\n*** storel2cn failed", d.buf, v[1].len);
1419 mpx_loadl2cn(m, ml1, v[1].buf, v[1].len);
1420 mpx_storel(m, ml1, d.buf, v[0].len);
1421 if (MEMCMP(d.buf, !=, v[0].buf, v[0].len)) {
1422 dumpbits("\n*** loadl2cn failed", d.buf, v[0].len);
1428 dumpbits("pos", v[0].buf, v[0].len);
1429 dumpbits("neg", v[1].buf, v[1].len);
1438 static int twocb(dstr *v)
1442 size_t sz0, sz1, szmax;
1446 sz0 = MPW_RQ(v[0].len); sz1 = MPW_RQ(v[1].len);
1447 dstr_ensure(&d, v[0].len > v[1].len ? v[0].len : v[1].len);
1449 szmax = sz0 > sz1 ? sz0 : sz1;
1450 m = xmalloc(MPWS(szmax));
1451 ml0 = m + sz0; ml1 = m + sz1;
1453 for (i = 0; i < 2; i++) {
1454 if (i) ml0 = ml1 = m + szmax;
1456 mpx_loadb(m, ml0, v[0].buf, v[0].len);
1457 mpx_storeb2cn(m, ml0, d.buf, v[1].len);
1458 if (MEMCMP(d.buf, !=, v[1].buf, v[1].len)) {
1459 dumpbits("\n*** storeb2cn failed", d.buf, v[1].len);
1463 mpx_loadb2cn(m, ml1, v[1].buf, v[1].len);
1464 mpx_storeb(m, ml1, d.buf, v[0].len);
1465 if (MEMCMP(d.buf, !=, v[0].buf, v[0].len)) {
1466 dumpbits("\n*** loadb2cn failed", d.buf, v[0].len);
1472 dumpbits("pos", v[0].buf, v[0].len);
1473 dumpbits("neg", v[1].buf, v[1].len);
1482 static int lsl(dstr *v)
1485 int n = *(int *)v[1].buf;
1492 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1494 mpx_lsl(d, dl, a, al, n);
1495 if (!mpx_ueq(d, dl, c, cl)) {
1496 fprintf(stderr, "\n*** lsl(%i) failed\n", n);
1497 dumpmp(" a", a, al);
1498 dumpmp("expected", c, cl);
1499 dumpmp(" result", d, dl);
1503 xfree(a); xfree(c); xfree(d);
1507 static int lslc(dstr *v)
1510 int n = *(int *)v[1].buf;
1517 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1519 mpx_lslc(d, dl, a, al, n);
1520 if (!mpx_ueq(d, dl, c, cl)) {
1521 fprintf(stderr, "\n*** lslc(%i) failed\n", n);
1522 dumpmp(" a", a, al);
1523 dumpmp("expected", c, cl);
1524 dumpmp(" result", d, dl);
1528 xfree(a); xfree(c); xfree(d);
1532 static int lsr(dstr *v)
1535 int n = *(int *)v[1].buf;
1542 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1);
1544 mpx_lsr(d, dl, a, al, n);
1545 if (!mpx_ueq(d, dl, c, cl)) {
1546 fprintf(stderr, "\n*** lsr(%i) failed\n", n);
1547 dumpmp(" a", a, al);
1548 dumpmp("expected", c, cl);
1549 dumpmp(" result", d, dl);
1553 xfree(a); xfree(c); xfree(d);
1557 static int uadd(dstr *v)
1568 ALLOC(d, dl, MAX(al - a, bl - b) + 1);
1570 mpx_uadd(d, dl, a, al, b, bl);
1571 if (!mpx_ueq(d, dl, c, cl)) {
1572 fprintf(stderr, "\n*** uadd failed\n");
1573 dumpmp(" a", a, al);
1574 dumpmp(" b", b, bl);
1575 dumpmp("expected", c, cl);
1576 dumpmp(" result", d, dl);
1580 xfree(a); xfree(b); xfree(c); xfree(d);
1584 static int usub(dstr *v)
1595 ALLOC(d, dl, al - a);
1597 mpx_usub(d, dl, a, al, b, bl);
1598 if (!mpx_ueq(d, dl, c, cl)) {
1599 fprintf(stderr, "\n*** usub failed\n");
1600 dumpmp(" a", a, al);
1601 dumpmp(" b", b, bl);
1602 dumpmp("expected", c, cl);
1603 dumpmp(" result", d, dl);
1607 xfree(a); xfree(b); xfree(c); xfree(d);
1611 static int umul(dstr *v)
1622 ALLOC(d, dl, (al - a) + (bl - b));
1624 mpx_umul(d, dl, a, al, b, bl);
1625 if (!mpx_ueq(d, dl, c, cl)) {
1626 fprintf(stderr, "\n*** umul failed\n");
1627 dumpmp(" a", a, al);
1628 dumpmp(" b", b, bl);
1629 dumpmp("expected", c, cl);
1630 dumpmp(" result", d, dl);
1634 xfree(a); xfree(b); xfree(c); xfree(d);
1638 static int usqr(dstr *v)
1647 ALLOC(d, dl, 2 * (al - a));
1649 mpx_usqr(d, dl, a, al);
1650 if (!mpx_ueq(d, dl, c, cl)) {
1651 fprintf(stderr, "\n*** usqr failed\n");
1652 dumpmp(" a", a, al);
1653 dumpmp("expected", c, cl);
1654 dumpmp(" result", d, dl);
1658 xfree(a); xfree(c); xfree(d);
1662 static int udiv(dstr *v)
1672 ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len);
1676 ALLOC(qq, qql, al - a);
1677 ALLOC(s, sl, (bl - b) + 1);
1679 mpx_udiv(qq, qql, a, al, b, bl, s, sl);
1680 if (!mpx_ueq(qq, qql, q, ql) ||
1681 !mpx_ueq(a, al, r, rl)) {
1682 fprintf(stderr, "\n*** udiv failed\n");
1683 dumpmp(" divisor", b, bl);
1684 dumpmp("expect r", r, rl);
1685 dumpmp("result r", a, al);
1686 dumpmp("expect q", q, ql);
1687 dumpmp("result q", qq, qql);
1691 xfree(a); xfree(b); xfree(r); xfree(q); xfree(s); xfree(qq);
1695 static test_chunk defs[] = {
1696 { "load-store", loadstore, { &type_hex, 0 } },
1697 { "2cl", twocl, { &type_hex, &type_hex, } },
1698 { "2cb", twocb, { &type_hex, &type_hex, } },
1699 { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } },
1700 { "lslc", lslc, { &type_hex, &type_int, &type_hex, 0 } },
1701 { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } },
1702 { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } },
1703 { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } },
1704 { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
1705 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
1706 { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } },
1710 int main(int argc, char *argv[])
1712 test_run(argc, argv, defs, SRCDIR"/t/mpx");
1718 /*----- That's all, folks -------------------------------------------------*/