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
)
917 static mpx_umul__functype
*pick_umul(void)
920 DISPATCH_PICK_COND(mpx_umul
, maybe_umul4_x86_sse2
,
921 cpu_feature_p(CPUFEAT_X86_SSE2
));
923 DISPATCH_PICK_FALLBACK(mpx_umul
, simple_umul
);
926 /* --- @mpx_umuln@ --- *
928 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
929 * @const mpw *av, *avl@ = multiplicand vector base and limit
930 * @mpw m@ = multiplier
934 * Use: Multiplies a multiprecision integer by a single-word value.
935 * The destination and source may be equal. The destination
936 * is completely cleared after use.
939 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
940 { MPX_UMULN(dv
, dvl
, av
, avl
, m
); }
942 /* --- @mpx_umlan@ --- *
944 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
945 * @const mpw *av, *avl@ = multiplicand vector base and limit
946 * @mpw m@ = multiplier
950 * Use: Multiplies a multiprecision integer by a single-word value
951 * and adds the result to an accumulator.
954 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
955 { MPX_UMLAN(dv
, dvl
, av
, avl
, m
); }
957 /* --- @mpx_usqr@ --- *
959 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
960 * @const mpw *av, *av@ = source vector base and limit
964 * Use: Performs unsigned integer squaring. The result vector must
965 * not overlap the source vector in any way.
968 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
972 /* --- Main loop --- */
980 /* --- Stop if I've run out of destination --- */
985 /* --- Work out the square at this point in the proceedings --- */
988 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
990 c
= MPW(x
>> MPW_BITS
);
993 /* --- Now fix up the rest of the vector upwards --- */
996 while (dvv
< dvl
&& avv
< avl
) {
997 mpd x
= (mpd
)a
* (mpd
)*avv
++;
998 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
999 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
1002 while (dvv
< dvl
&& c
) {
1008 /* --- Get ready for the next round --- */
1015 /* --- @mpx_udiv@ --- *
1017 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1018 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1019 * @const mpw *dv, *dvl@ = divisor vector base and limit
1020 * @mpw *sv, *svl@ = scratch workspace
1024 * Use: Performs unsigned integer division. If the result overflows
1025 * the quotient vector, high-order bits are discarded. (Clearly
1026 * the remainder vector can't overflow.) The various vectors
1027 * may not overlap in any way. Yes, I know it's a bit odd
1028 * requiring the dividend to be in the result position but it
1029 * does make some sense really. The remainder must have
1030 * headroom for at least two extra words. The scratch space
1031 * must be at least one word larger than the divisor.
1034 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
1035 const mpw
*dv
, const mpw
*dvl
,
1042 /* --- Initialize the quotient --- */
1046 /* --- Perform some sanity checks --- */
1048 MPX_SHRINK(dv
, dvl
);
1049 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
1051 /* --- Normalize the divisor --- *
1053 * The algorithm requires that the divisor be at least two digits long.
1054 * This is easy to fix.
1061 for (b
= MPW_P2
; b
; b
>>= 1) {
1062 if (d
<= (MPW_MAX
>> b
)) {
1071 /* --- Normalize the dividend/remainder to match --- */
1074 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
1075 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
1078 MPX_SHRINK(dv
, dvl
);
1081 MPX_SHRINK(rv
, rvl
);
1085 /* --- Work out the relative scales --- */
1088 size_t rvn
= rvl
- rv
;
1089 size_t dvn
= dvl
- dv
;
1091 /* --- If the divisor is clearly larger, notice this --- */
1094 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1101 /* --- Calculate the most significant quotient digit --- *
1103 * Because the divisor has its top bit set, this can only happen once. The
1104 * pointer arithmetic is a little contorted, to make sure that the
1105 * behaviour is defined.
1108 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
1109 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1110 if (qvl
- qv
> scale
)
1114 /* --- Now for the main loop --- */
1123 /* --- Get an estimate for the next quotient digit --- */
1130 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
1136 /* --- Refine the estimate --- */
1139 mpd yh
= (mpd
)d
* q
;
1140 mpd yy
= (mpd
)dd
* q
;
1144 yh
+= yy
>> MPW_BITS
;
1147 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
1156 /* --- Remove a chunk from the dividend --- */
1163 /* --- Calculate the size of the chunk --- *
1165 * This does the whole job of calculating @r >> scale - qd@.
1168 for (svv
= rv
+ scale
, dvv
= dv
;
1169 dvv
< dvl
&& svv
< rvl
;
1171 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
1173 x
= (mpd
)*svv
- MPW(x
) - sc
;
1182 mpd x
= (mpd
)*svv
- mc
- sc
;
1192 /* --- Fix if the quotient was too large --- *
1194 * This doesn't seem to happen very often.
1197 if (rvl
[-1] > MPW_MAX
/ 2) {
1198 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1203 /* --- Done for another iteration --- */
1205 if (qvl
- qv
> scale
)
1212 /* --- Now fiddle with unnormalizing and things --- */
1214 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1217 /* --- @mpx_udivn@ --- *
1219 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1221 * @const mpw *rv, *rvl@ = dividend
1222 * @mpw d@ = single-precision divisor
1224 * Returns: Remainder after divison.
1226 * Use: Performs a single-precision division operation.
1229 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
1232 size_t ql
= qvl
- qv
;
1238 r
= (r
<< MPW_BITS
) | rv
[i
];
1246 /*----- Test rig ----------------------------------------------------------*/
1250 #include <mLib/alloc.h>
1251 #include <mLib/dstr.h>
1252 #include <mLib/quis.h>
1253 #include <mLib/testrig.h>
1257 #define ALLOC(v, vl, sz) do { \
1258 size_t _sz = (sz); \
1259 mpw *_vv = xmalloc(MPWS(_sz)); \
1260 mpw *_vvl = _vv + _sz; \
1261 memset(_vv, 0xa5, MPWS(_sz)); \
1266 #define LOAD(v, vl, d) do { \
1267 const dstr *_d = (d); \
1269 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1270 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1275 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1277 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
1279 const octet
*p
= pp
;
1282 fprintf(stderr
, " %02x", *p
++);
1283 fputc('\n', stderr
);
1286 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
1291 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
1292 fputc('\n', stderr
);
1295 static int chkscan(const mpw
*v
, const mpw
*vl
,
1296 const void *pp
, size_t sz
, int step
)
1299 const octet
*p
= pp
;
1303 mpscan_initx(&mps
, v
, vl
);
1308 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1309 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1311 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1312 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1324 static int loadstore(dstr
*v
)
1327 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1331 dstr_ensure(&d
, v
->len
);
1332 m
= xmalloc(MPWS(sz
));
1334 for (diff
= 0; diff
< sz
; diff
+= 5) {
1339 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1340 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1342 MPX_OCTETS(oct
, m
, ml
);
1343 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1344 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1345 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1349 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1350 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1352 MPX_OCTETS(oct
, m
, ml
);
1353 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1354 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1355 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1361 dumpbits("input data", v
->buf
, v
->len
);
1368 static int twocl(dstr
*v
)
1375 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1376 dstr_ensure(&d
, sz
);
1379 m
= xmalloc(MPWS(sz
));
1382 mpx_loadl(m
, ml
, v
[0].buf
, v
[0].len
);
1383 mpx_storel2cn(m
, ml
, d
.buf
, v
[1].len
);
1384 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1385 dumpbits("\n*** storel2cn failed", d
.buf
, v
[1].len
);
1389 mpx_loadl2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1390 mpx_storel(m
, ml
, d
.buf
, v
[0].len
);
1391 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1392 dumpbits("\n*** loadl2cn failed", d
.buf
, v
[0].len
);
1397 dumpbits("pos", v
[0].buf
, v
[0].len
);
1398 dumpbits("neg", v
[1].buf
, v
[1].len
);
1407 static int twocb(dstr
*v
)
1414 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1415 dstr_ensure(&d
, sz
);
1418 m
= xmalloc(MPWS(sz
));
1421 mpx_loadb(m
, ml
, v
[0].buf
, v
[0].len
);
1422 mpx_storeb2cn(m
, ml
, d
.buf
, v
[1].len
);
1423 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1424 dumpbits("\n*** storeb2cn failed", d
.buf
, v
[1].len
);
1428 mpx_loadb2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1429 mpx_storeb(m
, ml
, d
.buf
, v
[0].len
);
1430 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1431 dumpbits("\n*** loadb2cn failed", d
.buf
, v
[0].len
);
1436 dumpbits("pos", v
[0].buf
, v
[0].len
);
1437 dumpbits("neg", v
[1].buf
, v
[1].len
);
1446 static int lsl(dstr
*v
)
1449 int n
= *(int *)v
[1].buf
;
1456 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1458 mpx_lsl(d
, dl
, a
, al
, n
);
1459 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1460 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1461 dumpmp(" a", a
, al
);
1462 dumpmp("expected", c
, cl
);
1463 dumpmp(" result", d
, dl
);
1467 xfree(a
); xfree(c
); xfree(d
);
1471 static int lslc(dstr
*v
)
1474 int n
= *(int *)v
[1].buf
;
1481 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1483 mpx_lslc(d
, dl
, a
, al
, n
);
1484 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1485 fprintf(stderr
, "\n*** lslc(%i) failed\n", n
);
1486 dumpmp(" a", a
, al
);
1487 dumpmp("expected", c
, cl
);
1488 dumpmp(" result", d
, dl
);
1492 xfree(a
); xfree(c
); xfree(d
);
1496 static int lsr(dstr
*v
)
1499 int n
= *(int *)v
[1].buf
;
1506 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1508 mpx_lsr(d
, dl
, a
, al
, n
);
1509 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1510 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1511 dumpmp(" a", a
, al
);
1512 dumpmp("expected", c
, cl
);
1513 dumpmp(" result", d
, dl
);
1517 xfree(a
); xfree(c
); xfree(d
);
1521 static int uadd(dstr
*v
)
1532 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1534 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1535 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1536 fprintf(stderr
, "\n*** uadd failed\n");
1537 dumpmp(" a", a
, al
);
1538 dumpmp(" b", b
, bl
);
1539 dumpmp("expected", c
, cl
);
1540 dumpmp(" result", d
, dl
);
1544 xfree(a
); xfree(b
); xfree(c
); xfree(d
);
1548 static int usub(dstr
*v
)
1559 ALLOC(d
, dl
, al
- a
);
1561 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1562 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1563 fprintf(stderr
, "\n*** usub failed\n");
1564 dumpmp(" a", a
, al
);
1565 dumpmp(" b", b
, bl
);
1566 dumpmp("expected", c
, cl
);
1567 dumpmp(" result", d
, dl
);
1571 xfree(a
); xfree(b
); xfree(c
); xfree(d
);
1575 static int umul(dstr
*v
)
1586 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1588 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1589 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1590 fprintf(stderr
, "\n*** umul failed\n");
1591 dumpmp(" a", a
, al
);
1592 dumpmp(" b", b
, bl
);
1593 dumpmp("expected", c
, cl
);
1594 dumpmp(" result", d
, dl
);
1598 xfree(a
); xfree(b
); xfree(c
); xfree(d
);
1602 static int usqr(dstr
*v
)
1611 ALLOC(d
, dl
, 2 * (al
- a
));
1613 mpx_usqr(d
, dl
, a
, al
);
1614 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1615 fprintf(stderr
, "\n*** usqr failed\n");
1616 dumpmp(" a", a
, al
);
1617 dumpmp("expected", c
, cl
);
1618 dumpmp(" result", d
, dl
);
1622 xfree(a
); xfree(c
); xfree(d
);
1626 static int udiv(dstr
*v
)
1636 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1640 ALLOC(qq
, qql
, al
- a
);
1641 ALLOC(s
, sl
, (bl
- b
) + 1);
1643 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1644 if (!mpx_ueq(qq
, qql
, q
, ql
) ||
1645 !mpx_ueq(a
, al
, r
, rl
)) {
1646 fprintf(stderr
, "\n*** udiv failed\n");
1647 dumpmp(" divisor", b
, bl
);
1648 dumpmp("expect r", r
, rl
);
1649 dumpmp("result r", a
, al
);
1650 dumpmp("expect q", q
, ql
);
1651 dumpmp("result q", qq
, qql
);
1655 xfree(a
); xfree(b
); xfree(r
); xfree(q
); xfree(s
); xfree(qq
);
1659 static test_chunk defs
[] = {
1660 { "load-store", loadstore
, { &type_hex
, 0 } },
1661 { "2cl", twocl
, { &type_hex
, &type_hex
, } },
1662 { "2cb", twocb
, { &type_hex
, &type_hex
, } },
1663 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1664 { "lslc", lslc
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1665 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1666 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1667 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1668 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1669 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1670 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1674 int main(int argc
, char *argv
[])
1676 test_run(argc
, argv
, defs
, SRCDIR
"/t/mpx");
1682 /*----- That's all, folks -------------------------------------------------*/