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 ------------------------------------------------------*/
35 #include <mLib/bits.h>
36 #include <mLib/macros.h>
42 /*----- Loading and storing -----------------------------------------------*/
44 /* --- @mpx_storel@ --- *
46 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
47 * @void *pp@ = pointer to octet array
48 * @size_t sz@ = size of octet array
52 * Use: Stores an MP in an octet array, least significant octet
53 * first. High-end octets are silently discarded if there
54 * isn't enough space for them.
57 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
60 octet
*p
= pp
, *q
= p
+ sz
;
70 *p
++ = U8(w
| n
<< bits
);
82 /* --- @mpx_loadl@ --- *
84 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
85 * @const void *pp@ = pointer to octet array
86 * @size_t sz@ = size of octet array
90 * Use: Loads an MP in an octet array, least significant octet
91 * first. High-end octets are ignored if there isn't enough
95 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
99 const octet
*p
= pp
, *q
= p
+ sz
;
108 if (bits
>= MPW_BITS
) {
110 w
= n
>> (MPW_BITS
- bits
+ 8);
120 /* --- @mpx_storeb@ --- *
122 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
123 * @void *pp@ = pointer to octet array
124 * @size_t sz@ = size of octet array
128 * Use: Stores an MP in an octet array, most significant octet
129 * first. High-end octets are silently discarded if there
130 * isn't enough space for them.
133 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
136 octet
*p
= pp
, *q
= p
+ sz
;
146 *--q
= U8(w
| n
<< bits
);
148 bits
+= MPW_BITS
- 8;
158 /* --- @mpx_loadb@ --- *
160 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
161 * @const void *pp@ = pointer to octet array
162 * @size_t sz@ = size of octet array
166 * Use: Loads an MP in an octet array, most significant octet
167 * first. High-end octets are ignored if there isn't enough
171 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
175 const octet
*p
= pp
, *q
= p
+ sz
;
184 if (bits
>= MPW_BITS
) {
186 w
= n
>> (MPW_BITS
- bits
+ 8);
196 /* --- @mpx_storel2cn@ --- *
198 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
199 * @void *pp@ = pointer to octet array
200 * @size_t sz@ = size of octet array
204 * Use: Stores a negative MP in an octet array, least significant
205 * octet first, as two's complement. High-end octets are
206 * silently discarded if there isn't enough space for them.
207 * This obviously makes the output bad.
210 void mpx_storel2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
215 octet
*p
= pp
, *q
= p
+ sz
;
227 bits
+= MPW_BITS
- 8;
245 /* --- @mpx_loadl2cn@ --- *
247 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
248 * @const void *pp@ = pointer to octet array
249 * @size_t sz@ = size of octet array
253 * Use: Loads a negative MP in an octet array, least significant
254 * octet first, as two's complement. High-end octets are
255 * ignored if there isn't enough space for them. This probably
256 * means you made the wrong choice coming here.
259 void mpx_loadl2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
264 const octet
*p
= pp
, *q
= p
+ sz
;
274 if (bits
>= MPW_BITS
) {
276 w
= n
>> (MPW_BITS
- bits
+ 8);
286 /* --- @mpx_storeb2cn@ --- *
288 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
289 * @void *pp@ = pointer to octet array
290 * @size_t sz@ = size of octet array
294 * Use: Stores a negative MP in an octet array, most significant
295 * octet first, as two's complement. High-end octets are
296 * silently discarded if there isn't enough space for them,
297 * which probably isn't what you meant.
300 void mpx_storeb2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
305 octet
*p
= pp
, *q
= p
+ sz
;
317 bits
+= MPW_BITS
- 8;
329 c
= c
&& !(b
& 0xff);
335 /* --- @mpx_loadb2cn@ --- *
337 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
338 * @const void *pp@ = pointer to octet array
339 * @size_t sz@ = size of octet array
343 * Use: Loads a negative MP in an octet array, most significant octet
344 * first as two's complement. High-end octets are ignored if
345 * there isn't enough space for them. This probably means you
346 * chose this function wrongly.
349 void mpx_loadb2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
354 const octet
*p
= pp
, *q
= p
+ sz
;
364 if (bits
>= MPW_BITS
) {
366 w
= n
>> (MPW_BITS
- bits
+ 8);
376 /*----- Logical shifting --------------------------------------------------*/
378 /* --- @MPX_SHIFT1@ --- *
380 * Arguments: @init@ = initial accumulator value
381 * @out@ = expression to store in each output word
382 * @next@ = expression for next accumulator value
384 * Use: Performs a single-position shift. The input is scanned
385 * right-to-left. In the expressions @out@ and @next@, the
386 * accumulator is available in @w@ and the current input word is
389 * This macro is intended to be used in the @shift1@ argument of
390 * @MPX_SHIFTOP@, and expects variables describing the operation
391 * to be set up accordingly.
394 #define MPX_SHIFT1(init, out, next) do { \
397 if (dv >= dvl) break; \
402 if (dv < dvl) { *dv++ = MPW(w); MPX_ZERO(dv, dvl); } \
405 /* --- @MPX_SHIFTW@ --- *
407 * Arguments: @max@ = the maximum shift (in words) which is nontrivial
408 * @clear@ = function (or macro) to clear low-order output words
409 * @copy@ = statement to copy words from input to output
411 * Use: Performs a shift by a whole number of words. If the shift
412 * amount is @max@ or more words, then the destination is
413 * @clear@ed entirely; otherwise, @copy@ is executed.
415 * This macro is intended to be used in the @shiftw@ argument of
416 * @MPX_SHIFTOP@, and expects variables describing the operation
417 * to be set up accordingly.
420 #define MPX_SHIFTW(max, clear, copy) do { \
421 if (nw >= (max)) clear(dv, dvl); \
425 /* --- @MPX_SHIFTOP@ --- *
427 * Arguments: @name@ = name of function to define (without `@mpx_@' prefix)
428 * @shift1@ = statement to shift by a single bit
429 * @shiftw@ = statement to shift by a whole number of words
430 * @shift@ = statement to perform a general shift
432 * Use: Emits a shift operation. The input is @av@..@avl@; the
433 * output is @dv@..@dvl@; and the shift amount (in bits) is
434 * @n@. In @shiftw@ and @shift@, @nw@ and @nb@ are set up such
435 * that @n = nw*MPW_BITS + nb@ and @nb < MPW_BITS@.
438 #define MPX_SHIFTOP(name, shift1, shiftw, shift) \
440 void mpx_##name(mpw *dv, mpw *dvl, \
441 const mpw *av, const mpw *avl, \
446 MPX_COPY(dv, dvl, av, avl); \
448 do shift1 while (0); \
450 size_t nw = n/MPW_BITS; \
451 unsigned nb = n%MPW_BITS; \
452 if (!nb) do shiftw while (0); \
453 else do shift while (0); \
457 /* --- @MPX_SHIFT_LEFT@ --- *
459 * Arguments: @name@ = name of function to define (without `@mpx_@' prefix)
460 * @init1@ = initializer for single-bit shift accumulator
461 * @clear@ = function (or macro) to clear low-order output words
462 * @flush@ = expression for low-order nontrivial output word
464 * Use: Emits a left-shift operation. This expands to a call on
465 * @MPX_SHIFTOP@, but implements the complicated @shift@
468 * The @init1@ argument is as for @MPX_SHIFT1@, and @clear@ is
469 * as for @MPX_SHIFTW@ (though is used elsewhere). In a general
470 * shift, @nw@ whole low-order output words are set using
471 * @clear@; high-order words are zeroed; and the remaining words
472 * set with a left-to-right pass across the input; at the end of
473 * the operation, the least significant output word above those
474 * @clear@ed is set using @flush@, which may use the accumulator
475 * @w@ = @av[0] << nb@.
478 #define MPX_SHIFT_LEFT(name, init1, clear, flush) \
479 MPX_SHIFTOP(name, { \
482 t >> (MPW_BITS - 1)); \
484 MPX_SHIFTW(dvl - dv, clear, { \
485 MPX_COPY(dv + nw, dvl, av, avl); \
486 clear(dv, dv + nw); \
489 size_t nr = MPW_BITS - nb; \
490 size_t dvn = dvl - dv; \
491 size_t avn = avl - av; \
499 if (dvn <= avn + nw) { \
500 avl = av + dvn - nw; \
503 size_t off = avn + nw + 1; \
504 MPX_ZERO(dv + off, dvl); \
511 *--dvl = MPW(w | (t >> nr)); \
515 *--dvl = MPW(flush); \
519 /* --- @mpx_lsl@ --- *
521 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
522 * @const mpw *av, *avl@ = source vector base and limit
523 * @size_t n@ = number of bit positions to shift by
527 * Use: Performs a logical shift left operation on an integer.
530 MPX_SHIFT_LEFT(lsl
, 0, MPX_ZERO
, w
)
532 /* --- @mpx_lslc@ --- *
534 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
535 * @const mpw *av, *avl@ = source vector base and limit
536 * @size_t n@ = number of bit positions to shift by
540 * Use: Performs a logical shift left operation on an integer, only
541 * it fills in the bits with ones instead of zeroes.
544 MPX_SHIFT_LEFT(lslc
, 1, MPX_ONE
, w
| (MPW_MAX
>> nr
))
546 /* --- @mpx_lsr@ --- *
548 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
549 * @const mpw *av, *avl@ = source vector base and limit
550 * @size_t n@ = number of bit positions to shift by
554 * Use: Performs a logical shift right operation on an integer.
558 MPX_SHIFT1(av
< avl ?
*av
++ >> 1 : 0,
559 w
| (t
<< (MPW_BITS
- 1)),
562 MPX_SHIFTW(avl
- av
, MPX_ZERO
,
563 { MPX_COPY(dv
, dvl
, av
+ nw
, avl
); });
565 size_t nr
= MPW_BITS
- nb
;
569 w
= av
< avl ?
*av
++ : 0;
572 if (dv
>= dvl
) goto done
;
574 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
578 *dv
++ = MPW(w
>> nb
);
584 /*----- Bitwise operations ------------------------------------------------*/
586 /* --- @mpx_bitop@ --- *
588 * Arguments: @mpw *dv, *dvl@ = destination vector
589 * @const mpw *av, *avl@ = first source vector
590 * @const mpw *bv, *bvl@ = second source vector
594 * Use; Provides the dyadic boolean functions.
597 #define MPX_BITBINOP(string) \
599 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
600 const mpw *bv, const mpw *bvl) \
602 MPX_SHRINK(av, avl); \
603 MPX_SHRINK(bv, bvl); \
607 a = (av < avl) ? *av++ : 0; \
608 b = (bv < bvl) ? *bv++ : 0; \
609 *dv++ = B##string(a, b); \
610 IGNORE(a); IGNORE(b); \
614 MPX_DOBIN(MPX_BITBINOP
)
616 void mpx_not(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
622 a
= (av
< avl
) ?
*av
++ : 0;
627 /*----- Unsigned arithmetic -----------------------------------------------*/
629 /* --- @mpx_2c@ --- *
631 * Arguments: @mpw *dv, *dvl@ = destination vector
632 * @const mpw *v, *vl@ = source vector
636 * Use: Calculates the two's complement of @v@.
639 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
642 while (dv
< dvl
&& v
< vl
)
643 *dv
++ = c
= MPW(~*v
++);
650 MPX_UADDN(dv
, dvl
, 1);
653 /* --- @mpx_ueq@ --- *
655 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
656 * @const mpw *bv, *bvl@ = second argument vector base and limit
658 * Returns: Nonzero if the two vectors are equal.
660 * Use: Performs an unsigned integer test for equality.
663 int mpx_ueq(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
667 if (avl
- av
!= bvl
- bv
)
676 /* --- @mpx_ucmp@ --- *
678 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
679 * @const mpw *bv, *bvl@ = second argument vector base and limit
681 * Returns: Less than, equal to, or greater than zero depending on
682 * whether @a@ is less than, equal to or greater than @b@,
685 * Use: Performs an unsigned integer comparison.
688 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
693 if (avl
- av
> bvl
- bv
)
695 else if (avl
- av
< bvl
- bv
)
697 else while (avl
> av
) {
698 mpw a
= *--avl
, b
= *--bvl
;
707 /* --- @mpx_uadd@ --- *
709 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
710 * @const mpw *av, *avl@ = first addend vector base and limit
711 * @const mpw *bv, *bvl@ = second addend vector base and limit
715 * Use: Performs unsigned integer addition. If the result overflows
716 * the destination vector, high-order bits are discarded. This
717 * means that two's complement addition happens more or less for
718 * free, although that's more a side-effect than anything else.
719 * The result vector may be equal to either or both source
720 * vectors, but may not otherwise overlap them.
723 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
724 const mpw
*bv
, const mpw
*bvl
)
728 while (av
< avl
|| bv
< bvl
) {
733 a
= (av
< avl
) ?
*av
++ : 0;
734 b
= (bv
< bvl
) ?
*bv
++ : 0;
735 x
= (mpd
)a
+ (mpd
)b
+ c
;
745 /* --- @mpx_uaddn@ --- *
747 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
748 * @mpw n@ = other addend
752 * Use: Adds a small integer to a multiprecision number.
755 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
757 /* --- @mpx_uaddnlsl@ --- *
759 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
760 * @mpw a@ = second argument
761 * @unsigned o@ = offset in bits
765 * Use: Computes %$d + 2^o a$%. If the result overflows then
766 * high-order bits are discarded, as usual. We must have
767 * @0 < o < MPW_BITS@.
770 void mpx_uaddnlsl(mpw
*dv
, mpw
*dvl
, mpw a
, unsigned o
)
774 while (x
&& dv
< dvl
) {
781 /* --- @mpx_usub@ --- *
783 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
784 * @const mpw *av, *avl@ = first argument vector base and limit
785 * @const mpw *bv, *bvl@ = second argument vector base and limit
789 * Use: Performs unsigned integer subtraction. If the result
790 * overflows the destination vector, high-order bits are
791 * discarded. This means that two's complement subtraction
792 * happens more or less for free, althuogh that's more a side-
793 * effect than anything else. The result vector may be equal to
794 * either or both source vectors, but may not otherwise overlap
798 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
799 const mpw
*bv
, const mpw
*bvl
)
803 while (av
< avl
|| bv
< bvl
) {
808 a
= (av
< avl
) ?
*av
++ : 0;
809 b
= (bv
< bvl
) ?
*bv
++ : 0;
810 x
= (mpd
)a
- (mpd
)b
- c
;
823 /* --- @mpx_usubn@ --- *
825 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
830 * Use: Subtracts a small integer from a multiprecision number.
833 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
835 /* --- @mpx_uaddnlsl@ --- *
837 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
838 * @mpw a@ = second argument
839 * @unsigned o@ = offset in bits
843 * Use: Computes %$d + 2^o a$%. If the result overflows then
844 * high-order bits are discarded, as usual. We must have
845 * @0 < o < MPW_BITS@.
848 void mpx_usubnlsl(mpw
*dv
, mpw
*dvl
, mpw a
, unsigned o
)
850 mpw b
= a
>> (MPW_BITS
- o
);
854 mpd x
= (mpd
)*dv
- MPW(a
);
858 MPX_USUBN(dv
, dvl
, b
);
862 /* --- @mpx_umul@ --- *
864 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
865 * @const mpw *av, *avl@ = multiplicand vector base and limit
866 * @const mpw *bv, *bvl@ = multiplier vector base and limit
870 * Use: Performs unsigned integer multiplication. If the result
871 * overflows the desination vector, high-order bits are
872 * discarded. The result vector may not overlap the argument
873 * vectors in any way.
876 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
877 const mpw
*bv
, const mpw
*bvl
)
879 /* --- This is probably worthwhile on a multiply --- */
884 /* --- Deal with a multiply by zero --- */
891 /* --- Do the initial multiply and initialize the accumulator --- */
893 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
895 /* --- Do the remaining multiply/accumulates --- */
897 while (dv
< dvl
&& bv
< bvl
) {
907 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
911 MPX_UADDN(dvv
, dvl
, c
);
916 /* --- @mpx_umuln@ --- *
918 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
919 * @const mpw *av, *avl@ = multiplicand vector base and limit
920 * @mpw m@ = multiplier
924 * Use: Multiplies a multiprecision integer by a single-word value.
925 * The destination and source may be equal. The destination
926 * is completely cleared after use.
929 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
930 { MPX_UMULN(dv
, dvl
, av
, avl
, m
); }
932 /* --- @mpx_umlan@ --- *
934 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
935 * @const mpw *av, *avl@ = multiplicand vector base and limit
936 * @mpw m@ = multiplier
940 * Use: Multiplies a multiprecision integer by a single-word value
941 * and adds the result to an accumulator.
944 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
945 { MPX_UMLAN(dv
, dvl
, av
, avl
, m
); }
947 /* --- @mpx_usqr@ --- *
949 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
950 * @const mpw *av, *av@ = source vector base and limit
954 * Use: Performs unsigned integer squaring. The result vector must
955 * not overlap the source vector in any way.
958 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
962 /* --- Main loop --- */
970 /* --- Stop if I've run out of destination --- */
975 /* --- Work out the square at this point in the proceedings --- */
978 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
980 c
= MPW(x
>> MPW_BITS
);
983 /* --- Now fix up the rest of the vector upwards --- */
986 while (dvv
< dvl
&& avv
< avl
) {
987 mpd x
= (mpd
)a
* (mpd
)*avv
++;
988 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
989 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
992 while (dvv
< dvl
&& c
) {
998 /* --- Get ready for the next round --- */
1005 /* --- @mpx_udiv@ --- *
1007 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1008 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1009 * @const mpw *dv, *dvl@ = divisor vector base and limit
1010 * @mpw *sv, *svl@ = scratch workspace
1014 * Use: Performs unsigned integer division. If the result overflows
1015 * the quotient vector, high-order bits are discarded. (Clearly
1016 * the remainder vector can't overflow.) The various vectors
1017 * may not overlap in any way. Yes, I know it's a bit odd
1018 * requiring the dividend to be in the result position but it
1019 * does make some sense really. The remainder must have
1020 * headroom for at least two extra words. The scratch space
1021 * must be at least one word larger than the divisor.
1024 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
1025 const mpw
*dv
, const mpw
*dvl
,
1032 /* --- Initialize the quotient --- */
1036 /* --- Perform some sanity checks --- */
1038 MPX_SHRINK(dv
, dvl
);
1039 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
1041 /* --- Normalize the divisor --- *
1043 * The algorithm requires that the divisor be at least two digits long.
1044 * This is easy to fix.
1051 for (b
= MPW_P2
; b
; b
>>= 1) {
1052 if (d
<= (MPW_MAX
>> b
)) {
1061 /* --- Normalize the dividend/remainder to match --- */
1064 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
1065 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
1068 MPX_SHRINK(dv
, dvl
);
1071 MPX_SHRINK(rv
, rvl
);
1075 /* --- Work out the relative scales --- */
1078 size_t rvn
= rvl
- rv
;
1079 size_t dvn
= dvl
- dv
;
1081 /* --- If the divisor is clearly larger, notice this --- */
1084 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1091 /* --- Calculate the most significant quotient digit --- *
1093 * Because the divisor has its top bit set, this can only happen once. The
1094 * pointer arithmetic is a little contorted, to make sure that the
1095 * behaviour is defined.
1098 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
1099 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1100 if (qvl
- qv
> scale
)
1104 /* --- Now for the main loop --- */
1113 /* --- Get an estimate for the next quotient digit --- */
1120 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
1126 /* --- Refine the estimate --- */
1129 mpd yh
= (mpd
)d
* q
;
1130 mpd yy
= (mpd
)dd
* q
;
1134 yh
+= yy
>> MPW_BITS
;
1137 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
1146 /* --- Remove a chunk from the dividend --- */
1153 /* --- Calculate the size of the chunk --- *
1155 * This does the whole job of calculating @r >> scale - qd@.
1158 for (svv
= rv
+ scale
, dvv
= dv
;
1159 dvv
< dvl
&& svv
< rvl
;
1161 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
1163 x
= (mpd
)*svv
- MPW(x
) - sc
;
1172 mpd x
= (mpd
)*svv
- mc
- sc
;
1182 /* --- Fix if the quotient was too large --- *
1184 * This doesn't seem to happen very often.
1187 if (rvl
[-1] > MPW_MAX
/ 2) {
1188 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1193 /* --- Done for another iteration --- */
1195 if (qvl
- qv
> scale
)
1202 /* --- Now fiddle with unnormalizing and things --- */
1204 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1207 /* --- @mpx_udivn@ --- *
1209 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1211 * @const mpw *rv, *rvl@ = dividend
1212 * @mpw d@ = single-precision divisor
1214 * Returns: Remainder after divison.
1216 * Use: Performs a single-precision division operation.
1219 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
1222 size_t ql
= qvl
- qv
;
1228 r
= (r
<< MPW_BITS
) | rv
[i
];
1236 /*----- Test rig ----------------------------------------------------------*/
1240 #include <mLib/alloc.h>
1241 #include <mLib/dstr.h>
1242 #include <mLib/quis.h>
1243 #include <mLib/testrig.h>
1247 #define ALLOC(v, vl, sz) do { \
1248 size_t _sz = (sz); \
1249 mpw *_vv = xmalloc(MPWS(_sz)); \
1250 mpw *_vvl = _vv + _sz; \
1255 #define LOAD(v, vl, d) do { \
1256 const dstr *_d = (d); \
1258 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1259 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1264 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1266 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
1268 const octet
*p
= pp
;
1271 fprintf(stderr
, " %02x", *p
++);
1272 fputc('\n', stderr
);
1275 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
1280 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
1281 fputc('\n', stderr
);
1284 static int chkscan(const mpw
*v
, const mpw
*vl
,
1285 const void *pp
, size_t sz
, int step
)
1288 const octet
*p
= pp
;
1292 mpscan_initx(&mps
, v
, vl
);
1297 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1298 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1300 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1301 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1313 static int loadstore(dstr
*v
)
1316 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1320 dstr_ensure(&d
, v
->len
);
1321 m
= xmalloc(MPWS(sz
));
1323 for (diff
= 0; diff
< sz
; diff
+= 5) {
1328 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1329 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1331 MPX_OCTETS(oct
, m
, ml
);
1332 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1333 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1334 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1338 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1339 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1341 MPX_OCTETS(oct
, m
, ml
);
1342 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1343 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1344 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1350 dumpbits("input data", v
->buf
, v
->len
);
1357 static int twocl(dstr
*v
)
1364 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1365 dstr_ensure(&d
, sz
);
1368 m
= xmalloc(MPWS(sz
));
1371 mpx_loadl(m
, ml
, v
[0].buf
, v
[0].len
);
1372 mpx_storel2cn(m
, ml
, d
.buf
, v
[1].len
);
1373 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1374 dumpbits("\n*** storel2cn failed", d
.buf
, v
[1].len
);
1378 mpx_loadl2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1379 mpx_storel(m
, ml
, d
.buf
, v
[0].len
);
1380 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1381 dumpbits("\n*** loadl2cn failed", d
.buf
, v
[0].len
);
1386 dumpbits("pos", v
[0].buf
, v
[0].len
);
1387 dumpbits("neg", v
[1].buf
, v
[1].len
);
1396 static int twocb(dstr
*v
)
1403 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1404 dstr_ensure(&d
, sz
);
1407 m
= xmalloc(MPWS(sz
));
1410 mpx_loadb(m
, ml
, v
[0].buf
, v
[0].len
);
1411 mpx_storeb2cn(m
, ml
, d
.buf
, v
[1].len
);
1412 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1413 dumpbits("\n*** storeb2cn failed", d
.buf
, v
[1].len
);
1417 mpx_loadb2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1418 mpx_storeb(m
, ml
, d
.buf
, v
[0].len
);
1419 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1420 dumpbits("\n*** loadb2cn failed", d
.buf
, v
[0].len
);
1425 dumpbits("pos", v
[0].buf
, v
[0].len
);
1426 dumpbits("neg", v
[1].buf
, v
[1].len
);
1435 static int lsl(dstr
*v
)
1438 int n
= *(int *)v
[1].buf
;
1445 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1447 mpx_lsl(d
, dl
, a
, al
, n
);
1448 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1449 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1450 dumpmp(" a", a
, al
);
1451 dumpmp("expected", c
, cl
);
1452 dumpmp(" result", d
, dl
);
1456 xfree(a
); xfree(c
); xfree(d
);
1460 static int lslc(dstr
*v
)
1463 int n
= *(int *)v
[1].buf
;
1470 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1472 mpx_lslc(d
, dl
, a
, al
, n
);
1473 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1474 fprintf(stderr
, "\n*** lslc(%i) failed\n", n
);
1475 dumpmp(" a", a
, al
);
1476 dumpmp("expected", c
, cl
);
1477 dumpmp(" result", d
, dl
);
1481 xfree(a
); xfree(c
); xfree(d
);
1485 static int lsr(dstr
*v
)
1488 int n
= *(int *)v
[1].buf
;
1495 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1497 mpx_lsr(d
, dl
, a
, al
, n
);
1498 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1499 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1500 dumpmp(" a", a
, al
);
1501 dumpmp("expected", c
, cl
);
1502 dumpmp(" result", d
, dl
);
1506 xfree(a
); xfree(c
); xfree(d
);
1510 static int uadd(dstr
*v
)
1521 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1523 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1524 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1525 fprintf(stderr
, "\n*** uadd failed\n");
1526 dumpmp(" a", a
, al
);
1527 dumpmp(" b", b
, bl
);
1528 dumpmp("expected", c
, cl
);
1529 dumpmp(" result", d
, dl
);
1533 xfree(a
); xfree(b
); xfree(c
); xfree(d
);
1537 static int usub(dstr
*v
)
1548 ALLOC(d
, dl
, al
- a
);
1550 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1551 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1552 fprintf(stderr
, "\n*** usub failed\n");
1553 dumpmp(" a", a
, al
);
1554 dumpmp(" b", b
, bl
);
1555 dumpmp("expected", c
, cl
);
1556 dumpmp(" result", d
, dl
);
1560 xfree(a
); xfree(b
); xfree(c
); xfree(d
);
1564 static int umul(dstr
*v
)
1575 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1577 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1578 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1579 fprintf(stderr
, "\n*** umul failed\n");
1580 dumpmp(" a", a
, al
);
1581 dumpmp(" b", b
, bl
);
1582 dumpmp("expected", c
, cl
);
1583 dumpmp(" result", d
, dl
);
1587 xfree(a
); xfree(b
); xfree(c
); xfree(d
);
1591 static int usqr(dstr
*v
)
1600 ALLOC(d
, dl
, 2 * (al
- a
));
1602 mpx_usqr(d
, dl
, a
, al
);
1603 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1604 fprintf(stderr
, "\n*** usqr failed\n");
1605 dumpmp(" a", a
, al
);
1606 dumpmp("expected", c
, cl
);
1607 dumpmp(" result", d
, dl
);
1611 xfree(a
); xfree(c
); xfree(d
);
1615 static int udiv(dstr
*v
)
1625 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1629 ALLOC(qq
, qql
, al
- a
);
1630 ALLOC(s
, sl
, (bl
- b
) + 1);
1632 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1633 if (!mpx_ueq(qq
, qql
, q
, ql
) ||
1634 !mpx_ueq(a
, al
, r
, rl
)) {
1635 fprintf(stderr
, "\n*** udiv failed\n");
1636 dumpmp(" divisor", b
, bl
);
1637 dumpmp("expect r", r
, rl
);
1638 dumpmp("result r", a
, al
);
1639 dumpmp("expect q", q
, ql
);
1640 dumpmp("result q", qq
, qql
);
1644 xfree(a
); xfree(b
); xfree(r
); xfree(q
); xfree(s
); xfree(qq
);
1648 static test_chunk defs
[] = {
1649 { "load-store", loadstore
, { &type_hex
, 0 } },
1650 { "2cl", twocl
, { &type_hex
, &type_hex
, } },
1651 { "2cb", twocb
, { &type_hex
, &type_hex
, } },
1652 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1653 { "lslc", lslc
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1654 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1655 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1656 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1657 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1658 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1659 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1663 int main(int argc
, char *argv
[])
1665 test_run(argc
, argv
, defs
, SRCDIR
"/t/mpx");
1671 /*----- That's all, folks -------------------------------------------------*/