f1cbbd9d3ae5e730f3857935249fb31eea00cdd7
3 * $Id: mpx.c,v 1.17 2004/03/27 00:04:46 mdw Exp $
5 * Low-level multiprecision arithmetic
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.17 2004/03/27 00:04:46 mdw
34 * Implement efficient reduction for pleasant-looking primes.
36 * Revision 1.16 2003/05/16 09:09:24 mdw
37 * Fix @mp_lsl2c@. Turns out to be surprisingly tricky.
39 * Revision 1.15 2002/10/20 01:12:31 mdw
40 * Two's complement I/O fixes.
42 * Revision 1.14 2002/10/19 18:55:08 mdw
43 * Fix overflows in shift primitives.
45 * Revision 1.13 2002/10/19 17:56:50 mdw
46 * Fix bit operations. Test them (a bit) better.
48 * Revision 1.12 2002/10/06 22:52:50 mdw
49 * Pile of changes for supporting two's complement properly.
51 * Revision 1.11 2001/04/03 19:36:05 mdw
52 * Add some simple bitwise operations so that Perl can use them.
54 * Revision 1.10 2000/10/08 12:06:12 mdw
55 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
57 * Revision 1.9 2000/06/26 07:52:50 mdw
58 * Portability fix for the bug fix.
60 * Revision 1.8 2000/06/25 12:59:02 mdw
61 * (mpx_udiv): Fix bug in quotient digit estimation.
63 * Revision 1.7 1999/12/22 15:49:07 mdw
64 * New function for division by a small integer.
66 * Revision 1.6 1999/11/20 22:43:44 mdw
67 * Integrate testing for MPX routines.
69 * Revision 1.5 1999/11/20 22:23:27 mdw
70 * Add function versions of some low-level macros with wider use.
72 * Revision 1.4 1999/11/17 18:04:09 mdw
73 * Add two's-complement functionality. Improve mpx_udiv a little by
74 * performing the multiplication of the divisor by q with the subtraction
77 * Revision 1.3 1999/11/13 01:57:31 mdw
78 * Remove stray debugging code.
80 * Revision 1.2 1999/11/13 01:50:59 mdw
81 * Multiprecision routines finished and tested.
83 * Revision 1.1 1999/09/03 08:41:12 mdw
88 /*----- Header files ------------------------------------------------------*/
95 #include <mLib/bits.h>
101 /*----- Loading and storing -----------------------------------------------*/
103 /* --- @mpx_storel@ --- *
105 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
106 * @void *pp@ = pointer to octet array
107 * @size_t sz@ = size of octet array
111 * Use: Stores an MP in an octet array, least significant octet
112 * first. High-end octets are silently discarded if there
113 * isn't enough space for them.
116 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
119 octet
*p
= pp
, *q
= p
+ sz
;
129 *p
++ = U8(w
| n
<< bits
);
131 bits
+= MPW_BITS
- 8;
141 /* --- @mpx_loadl@ --- *
143 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
144 * @const void *pp@ = pointer to octet array
145 * @size_t sz@ = size of octet array
149 * Use: Loads an MP in an octet array, least significant octet
150 * first. High-end octets are ignored if there isn't enough
154 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
158 const octet
*p
= pp
, *q
= p
+ sz
;
167 if (bits
>= MPW_BITS
) {
169 w
= n
>> (MPW_BITS
- bits
+ 8);
179 /* --- @mpx_storeb@ --- *
181 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
182 * @void *pp@ = pointer to octet array
183 * @size_t sz@ = size of octet array
187 * Use: Stores an MP in an octet array, most significant octet
188 * first. High-end octets are silently discarded if there
189 * isn't enough space for them.
192 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
195 octet
*p
= pp
, *q
= p
+ sz
;
205 *--q
= U8(w
| n
<< bits
);
207 bits
+= MPW_BITS
- 8;
217 /* --- @mpx_loadb@ --- *
219 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
220 * @const void *pp@ = pointer to octet array
221 * @size_t sz@ = size of octet array
225 * Use: Loads an MP in an octet array, most significant octet
226 * first. High-end octets are ignored if there isn't enough
230 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
234 const octet
*p
= pp
, *q
= p
+ sz
;
243 if (bits
>= MPW_BITS
) {
245 w
= n
>> (MPW_BITS
- bits
+ 8);
255 /* --- @mpx_storel2cn@ --- *
257 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
258 * @void *pp@ = pointer to octet array
259 * @size_t sz@ = size of octet array
263 * Use: Stores a negative MP in an octet array, least significant
264 * octet first, as two's complement. High-end octets are
265 * silently discarded if there isn't enough space for them.
266 * This obviously makes the output bad.
269 void mpx_storel2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
274 octet
*p
= pp
, *q
= p
+ sz
;
286 bits
+= MPW_BITS
- 8;
304 /* --- @mpx_loadl2cn@ --- *
306 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
307 * @const void *pp@ = pointer to octet array
308 * @size_t sz@ = size of octet array
312 * Use: Loads a negative MP in an octet array, least significant
313 * octet first, as two's complement. High-end octets are
314 * ignored if there isn't enough space for them. This probably
315 * means you made the wrong choice coming here.
318 void mpx_loadl2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
323 const octet
*p
= pp
, *q
= p
+ sz
;
333 if (bits
>= MPW_BITS
) {
335 w
= n
>> (MPW_BITS
- bits
+ 8);
345 /* --- @mpx_storeb2cn@ --- *
347 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
348 * @void *pp@ = pointer to octet array
349 * @size_t sz@ = size of octet array
353 * Use: Stores a negative MP in an octet array, most significant
354 * octet first, as two's complement. High-end octets are
355 * silently discarded if there isn't enough space for them,
356 * which probably isn't what you meant.
359 void mpx_storeb2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
364 octet
*p
= pp
, *q
= p
+ sz
;
376 bits
+= MPW_BITS
- 8;
388 c
= c
&& !(b
& 0xff);
394 /* --- @mpx_loadb2cn@ --- *
396 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
397 * @const void *pp@ = pointer to octet array
398 * @size_t sz@ = size of octet array
402 * Use: Loads a negative MP in an octet array, most significant octet
403 * first as two's complement. High-end octets are ignored if
404 * there isn't enough space for them. This probably means you
405 * chose this function wrongly.
408 void mpx_loadb2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
413 const octet
*p
= pp
, *q
= p
+ sz
;
423 if (bits
>= MPW_BITS
) {
425 w
= n
>> (MPW_BITS
- bits
+ 8);
435 /*----- Logical shifting --------------------------------------------------*/
437 /* --- @mpx_lsl@ --- *
439 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
440 * @const mpw *av, *avl@ = source vector base and limit
441 * @size_t n@ = number of bit positions to shift by
445 * Use: Performs a logical shift left operation on an integer.
448 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
453 /* --- Trivial special case --- */
456 MPX_COPY(dv
, dvl
, av
, avl
);
458 /* --- Single bit shifting --- */
467 *dv
++ = MPW((t
<< 1) | w
);
468 w
= t
>> (MPW_BITS
- 1);
477 /* --- Break out word and bit shifts for more sophisticated work --- */
482 /* --- Handle a shift by a multiple of the word size --- */
488 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
489 memset(dv
, 0, MPWS(nw
));
493 /* --- And finally the difficult case --- *
495 * This is a little convoluted, because I have to start from the end and
496 * work backwards to avoid overwriting the source, if they're both the same
502 size_t nr
= MPW_BITS
- nb
;
503 size_t dvn
= dvl
- dv
;
504 size_t avn
= avl
- av
;
511 if (dvn
> avn
+ nw
) {
512 size_t off
= avn
+ nw
+ 1;
513 MPX_ZERO(dv
+ off
, dvl
);
523 *--dvl
= (t
>> nr
) | w
;
534 /* --- @mpx_lslc@ --- *
536 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
537 * @const mpw *av, *avl@ = source vector base and limit
538 * @size_t n@ = number of bit positions to shift by
542 * Use: Performs a logical shift left operation on an integer, only
543 * it fills in the bits with ones instead of zeroes.
546 void mpx_lslc(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
551 /* --- Trivial special case --- */
554 MPX_COPY(dv
, dvl
, av
, avl
);
556 /* --- Single bit shifting --- */
565 *dv
++ = MPW((t
<< 1) | w
);
566 w
= t
>> (MPW_BITS
- 1);
575 /* --- Break out word and bit shifts for more sophisticated work --- */
580 /* --- Handle a shift by a multiple of the word size --- */
586 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
587 MPX_ONE(dv
, dv
+ nw
);
591 /* --- And finally the difficult case --- *
593 * This is a little convoluted, because I have to start from the end and
594 * work backwards to avoid overwriting the source, if they're both the same
600 size_t nr
= MPW_BITS
- nb
;
601 size_t dvn
= dvl
- dv
;
602 size_t avn
= avl
- av
;
609 if (dvn
> avn
+ nw
) {
610 size_t off
= avn
+ nw
+ 1;
611 MPX_ZERO(dv
+ off
, dvl
);
621 *--dvl
= (t
>> nr
) | w
;
625 *--dvl
= (MPW_MAX
>> nr
) | w
;
632 /* --- @mpx_lsr@ --- *
634 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
635 * @const mpw *av, *avl@ = source vector base and limit
636 * @size_t n@ = number of bit positions to shift by
640 * Use: Performs a logical shift right operation on an integer.
643 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
648 /* --- Trivial special case --- */
651 MPX_COPY(dv
, dvl
, av
, avl
);
653 /* --- Single bit shifting --- */
662 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
672 /* --- Break out word and bit shifts for more sophisticated work --- */
677 /* --- Handle a shift by a multiple of the word size --- */
683 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
686 /* --- And finally the difficult case --- */
690 size_t nr
= MPW_BITS
- nb
;
693 w
= av
< avl ?
*av
++ : 0;
699 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
703 *dv
++ = MPW(w
>> nb
);
711 /*----- Bitwise operations ------------------------------------------------*/
713 /* --- @mpx_bitop@ --- *
715 * Arguments: @mpw *dv, *dvl@ = destination vector
716 * @const mpw *av, *avl@ = first source vector
717 * @const mpw *bv, *bvl@ = second source vector
721 * Use; Provides the dyadic boolean functions.
724 #define MPX_BITBINOP(string) \
726 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
727 const mpw *bv, const mpw *bvl) \
729 MPX_SHRINK(av, avl); \
730 MPX_SHRINK(bv, bvl); \
734 a = (av < avl) ? *av++ : 0; \
735 b = (bv < bvl) ? *bv++ : 0; \
736 *dv++ = B##string(a, b); \
740 MPX_DOBIN(MPX_BITBINOP
)
742 void mpx_not(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
748 a
= (av
< avl
) ?
*av
++ : 0;
753 /*----- Unsigned arithmetic -----------------------------------------------*/
755 /* --- @mpx_2c@ --- *
757 * Arguments: @mpw *dv, *dvl@ = destination vector
758 * @const mpw *v, *vl@ = source vector
762 * Use: Calculates the two's complement of @v@.
765 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
768 while (dv
< dvl
&& v
< vl
)
769 *dv
++ = c
= MPW(~*v
++);
776 MPX_UADDN(dv
, dvl
, 1);
779 /* --- @mpx_ueq@ --- *
781 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
782 * @const mpw *bv, *bvl@ = second argument vector base and limit
784 * Returns: Nonzero if the two vectors are equal.
786 * Use: Performs an unsigned integer test for equality.
789 int mpx_ueq(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
793 if (avl
- av
!= bvl
- bv
)
802 /* --- @mpx_ucmp@ --- *
804 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
805 * @const mpw *bv, *bvl@ = second argument vector base and limit
807 * Returns: Less than, equal to, or greater than zero depending on
808 * whether @a@ is less than, equal to or greater than @b@,
811 * Use: Performs an unsigned integer comparison.
814 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
819 if (avl
- av
> bvl
- bv
)
821 else if (avl
- av
< bvl
- bv
)
823 else while (avl
> av
) {
824 mpw a
= *--avl
, b
= *--bvl
;
833 /* --- @mpx_uadd@ --- *
835 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
836 * @const mpw *av, *avl@ = first addend vector base and limit
837 * @const mpw *bv, *bvl@ = second addend vector base and limit
841 * Use: Performs unsigned integer addition. If the result overflows
842 * the destination vector, high-order bits are discarded. This
843 * means that two's complement addition happens more or less for
844 * free, although that's more a side-effect than anything else.
845 * The result vector may be equal to either or both source
846 * vectors, but may not otherwise overlap them.
849 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
850 const mpw
*bv
, const mpw
*bvl
)
854 while (av
< avl
|| bv
< bvl
) {
859 a
= (av
< avl
) ?
*av
++ : 0;
860 b
= (bv
< bvl
) ?
*bv
++ : 0;
861 x
= (mpd
)a
+ (mpd
)b
+ c
;
871 /* --- @mpx_uaddn@ --- *
873 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
874 * @mpw n@ = other addend
878 * Use: Adds a small integer to a multiprecision number.
881 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
883 /* --- @mpx_uaddnlsl@ --- *
885 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
886 * @mpw a@ = second argument
887 * @unsigned o@ = offset in bits
891 * Use: Computes %$d + 2^o a$%. If the result overflows then
892 * high-order bits are discarded, as usual. We must have
893 * @0 < o < MPW_BITS@.
896 void mpx_uaddnlsl(mpw
*dv
, mpw
*dvl
, mpw a
, unsigned o
)
900 while (x
&& dv
< dvl
) {
907 /* --- @mpx_usub@ --- *
909 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
910 * @const mpw *av, *avl@ = first argument vector base and limit
911 * @const mpw *bv, *bvl@ = second argument vector base and limit
915 * Use: Performs unsigned integer subtraction. If the result
916 * overflows the destination vector, high-order bits are
917 * discarded. This means that two's complement subtraction
918 * happens more or less for free, althuogh that's more a side-
919 * effect than anything else. The result vector may be equal to
920 * either or both source vectors, but may not otherwise overlap
924 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
925 const mpw
*bv
, const mpw
*bvl
)
929 while (av
< avl
|| bv
< bvl
) {
934 a
= (av
< avl
) ?
*av
++ : 0;
935 b
= (bv
< bvl
) ?
*bv
++ : 0;
936 x
= (mpd
)a
- (mpd
)b
- c
;
949 /* --- @mpx_usubn@ --- *
951 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
956 * Use: Subtracts a small integer from a multiprecision number.
959 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
961 /* --- @mpx_uaddnlsl@ --- *
963 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
964 * @mpw a@ = second argument
965 * @unsigned o@ = offset in bits
969 * Use: Computes %$d + 2^o a$%. If the result overflows then
970 * high-order bits are discarded, as usual. We must have
971 * @0 < o < MPW_BITS@.
974 void mpx_usubnlsl(mpw
*dv
, mpw
*dvl
, mpw a
, unsigned o
)
976 mpw b
= a
>> (MPW_BITS
- o
);
980 mpd x
= (mpd
)*dv
- (mpd
)a
;
984 MPX_USUBN(dv
, dvl
, b
);
988 /* --- @mpx_umul@ --- *
990 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
991 * @const mpw *av, *avl@ = multiplicand vector base and limit
992 * @const mpw *bv, *bvl@ = multiplier vector base and limit
996 * Use: Performs unsigned integer multiplication. If the result
997 * overflows the desination vector, high-order bits are
998 * discarded. The result vector may not overlap the argument
999 * vectors in any way.
1002 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
1003 const mpw
*bv
, const mpw
*bvl
)
1005 /* --- This is probably worthwhile on a multiply --- */
1007 MPX_SHRINK(av
, avl
);
1008 MPX_SHRINK(bv
, bvl
);
1010 /* --- Deal with a multiply by zero --- */
1017 /* --- Do the initial multiply and initialize the accumulator --- */
1019 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
1021 /* --- Do the remaining multiply/accumulates --- */
1023 while (dv
< dvl
&& bv
< bvl
) {
1026 const mpw
*avv
= av
;
1033 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
1037 MPX_UADDN(dvv
, dvl
, c
);
1042 /* --- @mpx_umuln@ --- *
1044 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1045 * @const mpw *av, *avl@ = multiplicand vector base and limit
1046 * @mpw m@ = multiplier
1050 * Use: Multiplies a multiprecision integer by a single-word value.
1051 * The destination and source may be equal. The destination
1052 * is completely cleared after use.
1055 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
1057 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
1060 /* --- @mpx_umlan@ --- *
1062 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
1063 * @const mpw *av, *avl@ = multiplicand vector base and limit
1064 * @mpw m@ = multiplier
1068 * Use: Multiplies a multiprecision integer by a single-word value
1069 * and adds the result to an accumulator.
1072 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
1074 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
1077 /* --- @mpx_usqr@ --- *
1079 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1080 * @const mpw *av, *av@ = source vector base and limit
1084 * Use: Performs unsigned integer squaring. The result vector must
1085 * not overlap the source vector in any way.
1088 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
1092 /* --- Main loop --- */
1095 const mpw
*avv
= av
;
1100 /* --- Stop if I've run out of destination --- */
1105 /* --- Work out the square at this point in the proceedings --- */
1108 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
1110 c
= MPW(x
>> MPW_BITS
);
1113 /* --- Now fix up the rest of the vector upwards --- */
1116 while (dvv
< dvl
&& avv
< avl
) {
1117 mpd x
= (mpd
)a
* (mpd
)*avv
++;
1118 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
1119 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
1122 while (dvv
< dvl
&& c
) {
1128 /* --- Get ready for the next round --- */
1135 /* --- @mpx_udiv@ --- *
1137 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1138 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1139 * @const mpw *dv, *dvl@ = divisor vector base and limit
1140 * @mpw *sv, *svl@ = scratch workspace
1144 * Use: Performs unsigned integer division. If the result overflows
1145 * the quotient vector, high-order bits are discarded. (Clearly
1146 * the remainder vector can't overflow.) The various vectors
1147 * may not overlap in any way. Yes, I know it's a bit odd
1148 * requiring the dividend to be in the result position but it
1149 * does make some sense really. The remainder must have
1150 * headroom for at least two extra words. The scratch space
1151 * must be at least one word larger than the divisor.
1154 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
1155 const mpw
*dv
, const mpw
*dvl
,
1162 /* --- Initialize the quotient --- */
1166 /* --- Perform some sanity checks --- */
1168 MPX_SHRINK(dv
, dvl
);
1169 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
1171 /* --- Normalize the divisor --- *
1173 * The algorithm requires that the divisor be at least two digits long.
1174 * This is easy to fix.
1181 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
1182 if (d
< (MPW_MAX
>> b
)) {
1191 /* --- Normalize the dividend/remainder to match --- */
1194 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
1195 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
1198 MPX_SHRINK(dv
, dvl
);
1201 MPX_SHRINK(rv
, rvl
);
1205 /* --- Work out the relative scales --- */
1208 size_t rvn
= rvl
- rv
;
1209 size_t dvn
= dvl
- dv
;
1211 /* --- If the divisor is clearly larger, notice this --- */
1214 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1221 /* --- Calculate the most significant quotient digit --- *
1223 * Because the divisor has its top bit set, this can only happen once. The
1224 * pointer arithmetic is a little contorted, to make sure that the
1225 * behaviour is defined.
1228 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
1229 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1230 if (qvl
- qv
> scale
)
1234 /* --- Now for the main loop --- */
1243 /* --- Get an estimate for the next quotient digit --- */
1250 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
1256 /* --- Refine the estimate --- */
1259 mpd yh
= (mpd
)d
* q
;
1260 mpd yy
= (mpd
)dd
* q
;
1264 yh
+= yy
>> MPW_BITS
;
1267 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
1276 /* --- Remove a chunk from the dividend --- */
1283 /* --- Calculate the size of the chunk --- *
1285 * This does the whole job of calculating @r >> scale - qd@.
1288 for (svv
= rv
+ scale
, dvv
= dv
;
1289 dvv
< dvl
&& svv
< rvl
;
1291 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
1293 x
= (mpd
)*svv
- MPW(x
) - sc
;
1302 mpd x
= (mpd
)*svv
- mc
- sc
;
1312 /* --- Fix if the quotient was too large --- *
1314 * This doesn't seem to happen very often.
1317 if (rvl
[-1] > MPW_MAX
/ 2) {
1318 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1323 /* --- Done for another iteration --- */
1325 if (qvl
- qv
> scale
)
1332 /* --- Now fiddle with unnormalizing and things --- */
1334 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1337 /* --- @mpx_udivn@ --- *
1339 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1341 * @const mpw *rv, *rvl@ = dividend
1342 * @mpw d@ = single-precision divisor
1344 * Returns: Remainder after divison.
1346 * Use: Performs a single-precision division operation.
1349 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
1352 size_t ql
= qvl
- qv
;
1358 r
= (r
<< MPW_BITS
) | rv
[i
];
1366 /*----- Test rig ----------------------------------------------------------*/
1370 #include <mLib/alloc.h>
1371 #include <mLib/dstr.h>
1372 #include <mLib/quis.h>
1373 #include <mLib/testrig.h>
1377 #define ALLOC(v, vl, sz) do { \
1378 size_t _sz = (sz); \
1379 mpw *_vv = xmalloc(MPWS(_sz)); \
1380 mpw *_vvl = _vv + _sz; \
1385 #define LOAD(v, vl, d) do { \
1386 const dstr *_d = (d); \
1388 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1389 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1394 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1396 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
1398 const octet
*p
= pp
;
1401 fprintf(stderr
, " %02x", *p
++);
1402 fputc('\n', stderr
);
1405 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
1410 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
1411 fputc('\n', stderr
);
1414 static int chkscan(const mpw
*v
, const mpw
*vl
,
1415 const void *pp
, size_t sz
, int step
)
1418 const octet
*p
= pp
;
1422 mpscan_initx(&mps
, v
, vl
);
1427 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1428 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1430 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1431 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1443 static int loadstore(dstr
*v
)
1446 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1450 dstr_ensure(&d
, v
->len
);
1451 m
= xmalloc(MPWS(sz
));
1453 for (diff
= 0; diff
< sz
; diff
+= 5) {
1458 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1459 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1461 MPX_OCTETS(oct
, m
, ml
);
1462 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1463 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1464 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1468 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1469 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1471 MPX_OCTETS(oct
, m
, ml
);
1472 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1473 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1474 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1480 dumpbits("input data", v
->buf
, v
->len
);
1487 static int twocl(dstr
*v
)
1494 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1495 dstr_ensure(&d
, sz
);
1498 m
= xmalloc(MPWS(sz
));
1501 mpx_loadl(m
, ml
, v
[0].buf
, v
[0].len
);
1502 mpx_storel2cn(m
, ml
, d
.buf
, v
[1].len
);
1503 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1504 dumpbits("\n*** storel2cn failed", d
.buf
, v
[1].len
);
1508 mpx_loadl2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1509 mpx_storel(m
, ml
, d
.buf
, v
[0].len
);
1510 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1511 dumpbits("\n*** loadl2cn failed", d
.buf
, v
[0].len
);
1516 dumpbits("pos", v
[0].buf
, v
[0].len
);
1517 dumpbits("neg", v
[1].buf
, v
[1].len
);
1526 static int twocb(dstr
*v
)
1533 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1534 dstr_ensure(&d
, sz
);
1537 m
= xmalloc(MPWS(sz
));
1540 mpx_loadb(m
, ml
, v
[0].buf
, v
[0].len
);
1541 mpx_storeb2cn(m
, ml
, d
.buf
, v
[1].len
);
1542 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1543 dumpbits("\n*** storeb2cn failed", d
.buf
, v
[1].len
);
1547 mpx_loadb2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1548 mpx_storeb(m
, ml
, d
.buf
, v
[0].len
);
1549 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1550 dumpbits("\n*** loadb2cn failed", d
.buf
, v
[0].len
);
1555 dumpbits("pos", v
[0].buf
, v
[0].len
);
1556 dumpbits("neg", v
[1].buf
, v
[1].len
);
1565 static int lsl(dstr
*v
)
1568 int n
= *(int *)v
[1].buf
;
1575 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1577 mpx_lsl(d
, dl
, a
, al
, n
);
1578 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1579 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1580 dumpmp(" a", a
, al
);
1581 dumpmp("expected", c
, cl
);
1582 dumpmp(" result", d
, dl
);
1586 free(a
); free(c
); free(d
);
1590 static int lslc(dstr
*v
)
1593 int n
= *(int *)v
[1].buf
;
1600 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1602 mpx_lslc(d
, dl
, a
, al
, n
);
1603 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1604 fprintf(stderr
, "\n*** lslc(%i) failed\n", n
);
1605 dumpmp(" a", a
, al
);
1606 dumpmp("expected", c
, cl
);
1607 dumpmp(" result", d
, dl
);
1611 free(a
); free(c
); free(d
);
1615 static int lsr(dstr
*v
)
1618 int n
= *(int *)v
[1].buf
;
1625 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1627 mpx_lsr(d
, dl
, a
, al
, n
);
1628 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1629 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1630 dumpmp(" a", a
, al
);
1631 dumpmp("expected", c
, cl
);
1632 dumpmp(" result", d
, dl
);
1636 free(a
); free(c
); free(d
);
1640 static int uadd(dstr
*v
)
1651 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1653 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1654 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1655 fprintf(stderr
, "\n*** uadd failed\n");
1656 dumpmp(" a", a
, al
);
1657 dumpmp(" b", b
, bl
);
1658 dumpmp("expected", c
, cl
);
1659 dumpmp(" result", d
, dl
);
1663 free(a
); free(b
); free(c
); free(d
);
1667 static int usub(dstr
*v
)
1678 ALLOC(d
, dl
, al
- a
);
1680 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1681 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1682 fprintf(stderr
, "\n*** usub failed\n");
1683 dumpmp(" a", a
, al
);
1684 dumpmp(" b", b
, bl
);
1685 dumpmp("expected", c
, cl
);
1686 dumpmp(" result", d
, dl
);
1690 free(a
); free(b
); free(c
); free(d
);
1694 static int umul(dstr
*v
)
1705 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1707 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1708 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1709 fprintf(stderr
, "\n*** umul failed\n");
1710 dumpmp(" a", a
, al
);
1711 dumpmp(" b", b
, bl
);
1712 dumpmp("expected", c
, cl
);
1713 dumpmp(" result", d
, dl
);
1717 free(a
); free(b
); free(c
); free(d
);
1721 static int usqr(dstr
*v
)
1730 ALLOC(d
, dl
, 2 * (al
- a
));
1732 mpx_usqr(d
, dl
, a
, al
);
1733 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1734 fprintf(stderr
, "\n*** usqr failed\n");
1735 dumpmp(" a", a
, al
);
1736 dumpmp("expected", c
, cl
);
1737 dumpmp(" result", d
, dl
);
1741 free(a
); free(c
); free(d
);
1745 static int udiv(dstr
*v
)
1755 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1759 ALLOC(qq
, qql
, al
- a
);
1760 ALLOC(s
, sl
, (bl
- b
) + 1);
1762 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1763 if (!mpx_ueq(qq
, qql
, q
, ql
) ||
1764 !mpx_ueq(a
, al
, r
, rl
)) {
1765 fprintf(stderr
, "\n*** udiv failed\n");
1766 dumpmp(" divisor", b
, bl
);
1767 dumpmp("expect r", r
, rl
);
1768 dumpmp("result r", a
, al
);
1769 dumpmp("expect q", q
, ql
);
1770 dumpmp("result q", qq
, qql
);
1774 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1778 static test_chunk defs
[] = {
1779 { "load-store", loadstore
, { &type_hex
, 0 } },
1780 { "2cl", twocl
, { &type_hex
, &type_hex
, } },
1781 { "2cb", twocb
, { &type_hex
, &type_hex
, } },
1782 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1783 { "lslc", lslc
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1784 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1785 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1786 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1787 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1788 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1789 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1793 int main(int argc
, char *argv
[])
1795 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1801 /*----- That's all, folks -------------------------------------------------*/