3 * $Id: mpx.c,v 1.18 2004/04/01 12:50:09 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.18 2004/04/01 12:50:09 mdw
34 * Add cyclic group abstraction, with test code. Separate off exponentation
35 * functions for better static linking. Fix a buttload of bugs on the way.
36 * Generally ensure that negative exponents do inversion correctly. Add
37 * table of standard prime-field subgroups. (Binary field subgroups are
38 * currently unimplemented but easy to add if anyone ever finds a good one.)
40 * Revision 1.17 2004/03/27 00:04:46 mdw
41 * Implement efficient reduction for pleasant-looking primes.
43 * Revision 1.16 2003/05/16 09:09:24 mdw
44 * Fix @mp_lsl2c@. Turns out to be surprisingly tricky.
46 * Revision 1.15 2002/10/20 01:12:31 mdw
47 * Two's complement I/O fixes.
49 * Revision 1.14 2002/10/19 18:55:08 mdw
50 * Fix overflows in shift primitives.
52 * Revision 1.13 2002/10/19 17:56:50 mdw
53 * Fix bit operations. Test them (a bit) better.
55 * Revision 1.12 2002/10/06 22:52:50 mdw
56 * Pile of changes for supporting two's complement properly.
58 * Revision 1.11 2001/04/03 19:36:05 mdw
59 * Add some simple bitwise operations so that Perl can use them.
61 * Revision 1.10 2000/10/08 12:06:12 mdw
62 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
64 * Revision 1.9 2000/06/26 07:52:50 mdw
65 * Portability fix for the bug fix.
67 * Revision 1.8 2000/06/25 12:59:02 mdw
68 * (mpx_udiv): Fix bug in quotient digit estimation.
70 * Revision 1.7 1999/12/22 15:49:07 mdw
71 * New function for division by a small integer.
73 * Revision 1.6 1999/11/20 22:43:44 mdw
74 * Integrate testing for MPX routines.
76 * Revision 1.5 1999/11/20 22:23:27 mdw
77 * Add function versions of some low-level macros with wider use.
79 * Revision 1.4 1999/11/17 18:04:09 mdw
80 * Add two's-complement functionality. Improve mpx_udiv a little by
81 * performing the multiplication of the divisor by q with the subtraction
84 * Revision 1.3 1999/11/13 01:57:31 mdw
85 * Remove stray debugging code.
87 * Revision 1.2 1999/11/13 01:50:59 mdw
88 * Multiprecision routines finished and tested.
90 * Revision 1.1 1999/09/03 08:41:12 mdw
95 /*----- Header files ------------------------------------------------------*/
102 #include <mLib/bits.h>
108 /*----- Loading and storing -----------------------------------------------*/
110 /* --- @mpx_storel@ --- *
112 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
113 * @void *pp@ = pointer to octet array
114 * @size_t sz@ = size of octet array
118 * Use: Stores an MP in an octet array, least significant octet
119 * first. High-end octets are silently discarded if there
120 * isn't enough space for them.
123 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
126 octet
*p
= pp
, *q
= p
+ sz
;
136 *p
++ = U8(w
| n
<< bits
);
138 bits
+= MPW_BITS
- 8;
148 /* --- @mpx_loadl@ --- *
150 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
151 * @const void *pp@ = pointer to octet array
152 * @size_t sz@ = size of octet array
156 * Use: Loads an MP in an octet array, least significant octet
157 * first. High-end octets are ignored if there isn't enough
161 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
165 const octet
*p
= pp
, *q
= p
+ sz
;
174 if (bits
>= MPW_BITS
) {
176 w
= n
>> (MPW_BITS
- bits
+ 8);
186 /* --- @mpx_storeb@ --- *
188 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
189 * @void *pp@ = pointer to octet array
190 * @size_t sz@ = size of octet array
194 * Use: Stores an MP in an octet array, most significant octet
195 * first. High-end octets are silently discarded if there
196 * isn't enough space for them.
199 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
202 octet
*p
= pp
, *q
= p
+ sz
;
212 *--q
= U8(w
| n
<< bits
);
214 bits
+= MPW_BITS
- 8;
224 /* --- @mpx_loadb@ --- *
226 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
227 * @const void *pp@ = pointer to octet array
228 * @size_t sz@ = size of octet array
232 * Use: Loads an MP in an octet array, most significant octet
233 * first. High-end octets are ignored if there isn't enough
237 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
241 const octet
*p
= pp
, *q
= p
+ sz
;
250 if (bits
>= MPW_BITS
) {
252 w
= n
>> (MPW_BITS
- bits
+ 8);
262 /* --- @mpx_storel2cn@ --- *
264 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
265 * @void *pp@ = pointer to octet array
266 * @size_t sz@ = size of octet array
270 * Use: Stores a negative MP in an octet array, least significant
271 * octet first, as two's complement. High-end octets are
272 * silently discarded if there isn't enough space for them.
273 * This obviously makes the output bad.
276 void mpx_storel2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
281 octet
*p
= pp
, *q
= p
+ sz
;
293 bits
+= MPW_BITS
- 8;
311 /* --- @mpx_loadl2cn@ --- *
313 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
314 * @const void *pp@ = pointer to octet array
315 * @size_t sz@ = size of octet array
319 * Use: Loads a negative MP in an octet array, least significant
320 * octet first, as two's complement. High-end octets are
321 * ignored if there isn't enough space for them. This probably
322 * means you made the wrong choice coming here.
325 void mpx_loadl2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
330 const octet
*p
= pp
, *q
= p
+ sz
;
340 if (bits
>= MPW_BITS
) {
342 w
= n
>> (MPW_BITS
- bits
+ 8);
352 /* --- @mpx_storeb2cn@ --- *
354 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
355 * @void *pp@ = pointer to octet array
356 * @size_t sz@ = size of octet array
360 * Use: Stores a negative MP in an octet array, most significant
361 * octet first, as two's complement. High-end octets are
362 * silently discarded if there isn't enough space for them,
363 * which probably isn't what you meant.
366 void mpx_storeb2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
371 octet
*p
= pp
, *q
= p
+ sz
;
383 bits
+= MPW_BITS
- 8;
395 c
= c
&& !(b
& 0xff);
401 /* --- @mpx_loadb2cn@ --- *
403 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
404 * @const void *pp@ = pointer to octet array
405 * @size_t sz@ = size of octet array
409 * Use: Loads a negative MP in an octet array, most significant octet
410 * first as two's complement. High-end octets are ignored if
411 * there isn't enough space for them. This probably means you
412 * chose this function wrongly.
415 void mpx_loadb2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
420 const octet
*p
= pp
, *q
= p
+ sz
;
430 if (bits
>= MPW_BITS
) {
432 w
= n
>> (MPW_BITS
- bits
+ 8);
442 /*----- Logical shifting --------------------------------------------------*/
444 /* --- @mpx_lsl@ --- *
446 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
447 * @const mpw *av, *avl@ = source vector base and limit
448 * @size_t n@ = number of bit positions to shift by
452 * Use: Performs a logical shift left operation on an integer.
455 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
460 /* --- Trivial special case --- */
463 MPX_COPY(dv
, dvl
, av
, avl
);
465 /* --- Single bit shifting --- */
474 *dv
++ = MPW((t
<< 1) | w
);
475 w
= t
>> (MPW_BITS
- 1);
484 /* --- Break out word and bit shifts for more sophisticated work --- */
489 /* --- Handle a shift by a multiple of the word size --- */
495 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
496 memset(dv
, 0, MPWS(nw
));
500 /* --- And finally the difficult case --- *
502 * This is a little convoluted, because I have to start from the end and
503 * work backwards to avoid overwriting the source, if they're both the same
509 size_t nr
= MPW_BITS
- nb
;
510 size_t dvn
= dvl
- dv
;
511 size_t avn
= avl
- av
;
518 if (dvn
> avn
+ nw
) {
519 size_t off
= avn
+ nw
+ 1;
520 MPX_ZERO(dv
+ off
, dvl
);
530 *--dvl
= (t
>> nr
) | w
;
541 /* --- @mpx_lslc@ --- *
543 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
544 * @const mpw *av, *avl@ = source vector base and limit
545 * @size_t n@ = number of bit positions to shift by
549 * Use: Performs a logical shift left operation on an integer, only
550 * it fills in the bits with ones instead of zeroes.
553 void mpx_lslc(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
558 /* --- Trivial special case --- */
561 MPX_COPY(dv
, dvl
, av
, avl
);
563 /* --- Single bit shifting --- */
572 *dv
++ = MPW((t
<< 1) | w
);
573 w
= t
>> (MPW_BITS
- 1);
582 /* --- Break out word and bit shifts for more sophisticated work --- */
587 /* --- Handle a shift by a multiple of the word size --- */
593 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
594 MPX_ONE(dv
, dv
+ nw
);
598 /* --- And finally the difficult case --- *
600 * This is a little convoluted, because I have to start from the end and
601 * work backwards to avoid overwriting the source, if they're both the same
607 size_t nr
= MPW_BITS
- nb
;
608 size_t dvn
= dvl
- dv
;
609 size_t avn
= avl
- av
;
616 if (dvn
> avn
+ nw
) {
617 size_t off
= avn
+ nw
+ 1;
618 MPX_ZERO(dv
+ off
, dvl
);
628 *--dvl
= (t
>> nr
) | w
;
632 *--dvl
= (MPW_MAX
>> nr
) | w
;
639 /* --- @mpx_lsr@ --- *
641 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
642 * @const mpw *av, *avl@ = source vector base and limit
643 * @size_t n@ = number of bit positions to shift by
647 * Use: Performs a logical shift right operation on an integer.
650 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
655 /* --- Trivial special case --- */
658 MPX_COPY(dv
, dvl
, av
, avl
);
660 /* --- Single bit shifting --- */
669 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
679 /* --- Break out word and bit shifts for more sophisticated work --- */
684 /* --- Handle a shift by a multiple of the word size --- */
690 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
693 /* --- And finally the difficult case --- */
697 size_t nr
= MPW_BITS
- nb
;
700 w
= av
< avl ?
*av
++ : 0;
706 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
710 *dv
++ = MPW(w
>> nb
);
718 /*----- Bitwise operations ------------------------------------------------*/
720 /* --- @mpx_bitop@ --- *
722 * Arguments: @mpw *dv, *dvl@ = destination vector
723 * @const mpw *av, *avl@ = first source vector
724 * @const mpw *bv, *bvl@ = second source vector
728 * Use; Provides the dyadic boolean functions.
731 #define MPX_BITBINOP(string) \
733 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
734 const mpw *bv, const mpw *bvl) \
736 MPX_SHRINK(av, avl); \
737 MPX_SHRINK(bv, bvl); \
741 a = (av < avl) ? *av++ : 0; \
742 b = (bv < bvl) ? *bv++ : 0; \
743 *dv++ = B##string(a, b); \
747 MPX_DOBIN(MPX_BITBINOP
)
749 void mpx_not(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
755 a
= (av
< avl
) ?
*av
++ : 0;
760 /*----- Unsigned arithmetic -----------------------------------------------*/
762 /* --- @mpx_2c@ --- *
764 * Arguments: @mpw *dv, *dvl@ = destination vector
765 * @const mpw *v, *vl@ = source vector
769 * Use: Calculates the two's complement of @v@.
772 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
775 while (dv
< dvl
&& v
< vl
)
776 *dv
++ = c
= MPW(~*v
++);
783 MPX_UADDN(dv
, dvl
, 1);
786 /* --- @mpx_ueq@ --- *
788 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
789 * @const mpw *bv, *bvl@ = second argument vector base and limit
791 * Returns: Nonzero if the two vectors are equal.
793 * Use: Performs an unsigned integer test for equality.
796 int mpx_ueq(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
800 if (avl
- av
!= bvl
- bv
)
809 /* --- @mpx_ucmp@ --- *
811 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
812 * @const mpw *bv, *bvl@ = second argument vector base and limit
814 * Returns: Less than, equal to, or greater than zero depending on
815 * whether @a@ is less than, equal to or greater than @b@,
818 * Use: Performs an unsigned integer comparison.
821 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
826 if (avl
- av
> bvl
- bv
)
828 else if (avl
- av
< bvl
- bv
)
830 else while (avl
> av
) {
831 mpw a
= *--avl
, b
= *--bvl
;
840 /* --- @mpx_uadd@ --- *
842 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
843 * @const mpw *av, *avl@ = first addend vector base and limit
844 * @const mpw *bv, *bvl@ = second addend vector base and limit
848 * Use: Performs unsigned integer addition. If the result overflows
849 * the destination vector, high-order bits are discarded. This
850 * means that two's complement addition happens more or less for
851 * free, although that's more a side-effect than anything else.
852 * The result vector may be equal to either or both source
853 * vectors, but may not otherwise overlap them.
856 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
857 const mpw
*bv
, const mpw
*bvl
)
861 while (av
< avl
|| bv
< bvl
) {
866 a
= (av
< avl
) ?
*av
++ : 0;
867 b
= (bv
< bvl
) ?
*bv
++ : 0;
868 x
= (mpd
)a
+ (mpd
)b
+ c
;
878 /* --- @mpx_uaddn@ --- *
880 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
881 * @mpw n@ = other addend
885 * Use: Adds a small integer to a multiprecision number.
888 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
890 /* --- @mpx_uaddnlsl@ --- *
892 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
893 * @mpw a@ = second argument
894 * @unsigned o@ = offset in bits
898 * Use: Computes %$d + 2^o a$%. If the result overflows then
899 * high-order bits are discarded, as usual. We must have
900 * @0 < o < MPW_BITS@.
903 void mpx_uaddnlsl(mpw
*dv
, mpw
*dvl
, mpw a
, unsigned o
)
907 while (x
&& dv
< dvl
) {
914 /* --- @mpx_usub@ --- *
916 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
917 * @const mpw *av, *avl@ = first argument vector base and limit
918 * @const mpw *bv, *bvl@ = second argument vector base and limit
922 * Use: Performs unsigned integer subtraction. If the result
923 * overflows the destination vector, high-order bits are
924 * discarded. This means that two's complement subtraction
925 * happens more or less for free, althuogh that's more a side-
926 * effect than anything else. The result vector may be equal to
927 * either or both source vectors, but may not otherwise overlap
931 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
932 const mpw
*bv
, const mpw
*bvl
)
936 while (av
< avl
|| bv
< bvl
) {
941 a
= (av
< avl
) ?
*av
++ : 0;
942 b
= (bv
< bvl
) ?
*bv
++ : 0;
943 x
= (mpd
)a
- (mpd
)b
- c
;
956 /* --- @mpx_usubn@ --- *
958 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
963 * Use: Subtracts a small integer from a multiprecision number.
966 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
968 /* --- @mpx_uaddnlsl@ --- *
970 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
971 * @mpw a@ = second argument
972 * @unsigned o@ = offset in bits
976 * Use: Computes %$d + 2^o a$%. If the result overflows then
977 * high-order bits are discarded, as usual. We must have
978 * @0 < o < MPW_BITS@.
981 void mpx_usubnlsl(mpw
*dv
, mpw
*dvl
, mpw a
, unsigned o
)
983 mpw b
= a
>> (MPW_BITS
- o
);
987 mpd x
= (mpd
)*dv
- (mpd
)a
;
991 MPX_USUBN(dv
, dvl
, b
);
995 /* --- @mpx_umul@ --- *
997 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
998 * @const mpw *av, *avl@ = multiplicand vector base and limit
999 * @const mpw *bv, *bvl@ = multiplier vector base and limit
1003 * Use: Performs unsigned integer multiplication. If the result
1004 * overflows the desination vector, high-order bits are
1005 * discarded. The result vector may not overlap the argument
1006 * vectors in any way.
1009 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
1010 const mpw
*bv
, const mpw
*bvl
)
1012 /* --- This is probably worthwhile on a multiply --- */
1014 MPX_SHRINK(av
, avl
);
1015 MPX_SHRINK(bv
, bvl
);
1017 /* --- Deal with a multiply by zero --- */
1024 /* --- Do the initial multiply and initialize the accumulator --- */
1026 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
1028 /* --- Do the remaining multiply/accumulates --- */
1030 while (dv
< dvl
&& bv
< bvl
) {
1033 const mpw
*avv
= av
;
1040 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
1044 MPX_UADDN(dvv
, dvl
, c
);
1049 /* --- @mpx_umuln@ --- *
1051 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1052 * @const mpw *av, *avl@ = multiplicand vector base and limit
1053 * @mpw m@ = multiplier
1057 * Use: Multiplies a multiprecision integer by a single-word value.
1058 * The destination and source may be equal. The destination
1059 * is completely cleared after use.
1062 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
1064 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
1067 /* --- @mpx_umlan@ --- *
1069 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
1070 * @const mpw *av, *avl@ = multiplicand vector base and limit
1071 * @mpw m@ = multiplier
1075 * Use: Multiplies a multiprecision integer by a single-word value
1076 * and adds the result to an accumulator.
1079 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
1081 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
1084 /* --- @mpx_usqr@ --- *
1086 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1087 * @const mpw *av, *av@ = source vector base and limit
1091 * Use: Performs unsigned integer squaring. The result vector must
1092 * not overlap the source vector in any way.
1095 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
1099 /* --- Main loop --- */
1102 const mpw
*avv
= av
;
1107 /* --- Stop if I've run out of destination --- */
1112 /* --- Work out the square at this point in the proceedings --- */
1115 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
1117 c
= MPW(x
>> MPW_BITS
);
1120 /* --- Now fix up the rest of the vector upwards --- */
1123 while (dvv
< dvl
&& avv
< avl
) {
1124 mpd x
= (mpd
)a
* (mpd
)*avv
++;
1125 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
1126 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
1129 while (dvv
< dvl
&& c
) {
1135 /* --- Get ready for the next round --- */
1142 /* --- @mpx_udiv@ --- *
1144 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1145 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1146 * @const mpw *dv, *dvl@ = divisor vector base and limit
1147 * @mpw *sv, *svl@ = scratch workspace
1151 * Use: Performs unsigned integer division. If the result overflows
1152 * the quotient vector, high-order bits are discarded. (Clearly
1153 * the remainder vector can't overflow.) The various vectors
1154 * may not overlap in any way. Yes, I know it's a bit odd
1155 * requiring the dividend to be in the result position but it
1156 * does make some sense really. The remainder must have
1157 * headroom for at least two extra words. The scratch space
1158 * must be at least one word larger than the divisor.
1161 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
1162 const mpw
*dv
, const mpw
*dvl
,
1169 /* --- Initialize the quotient --- */
1173 /* --- Perform some sanity checks --- */
1175 MPX_SHRINK(dv
, dvl
);
1176 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
1178 /* --- Normalize the divisor --- *
1180 * The algorithm requires that the divisor be at least two digits long.
1181 * This is easy to fix.
1188 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
1189 if (d
<= (MPW_MAX
>> b
)) {
1198 /* --- Normalize the dividend/remainder to match --- */
1201 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
1202 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
1205 MPX_SHRINK(dv
, dvl
);
1208 MPX_SHRINK(rv
, rvl
);
1212 /* --- Work out the relative scales --- */
1215 size_t rvn
= rvl
- rv
;
1216 size_t dvn
= dvl
- dv
;
1218 /* --- If the divisor is clearly larger, notice this --- */
1221 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1228 /* --- Calculate the most significant quotient digit --- *
1230 * Because the divisor has its top bit set, this can only happen once. The
1231 * pointer arithmetic is a little contorted, to make sure that the
1232 * behaviour is defined.
1235 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
1236 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1237 if (qvl
- qv
> scale
)
1241 /* --- Now for the main loop --- */
1250 /* --- Get an estimate for the next quotient digit --- */
1257 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
1263 /* --- Refine the estimate --- */
1266 mpd yh
= (mpd
)d
* q
;
1267 mpd yy
= (mpd
)dd
* q
;
1271 yh
+= yy
>> MPW_BITS
;
1274 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
1283 /* --- Remove a chunk from the dividend --- */
1290 /* --- Calculate the size of the chunk --- *
1292 * This does the whole job of calculating @r >> scale - qd@.
1295 for (svv
= rv
+ scale
, dvv
= dv
;
1296 dvv
< dvl
&& svv
< rvl
;
1298 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
1300 x
= (mpd
)*svv
- MPW(x
) - sc
;
1309 mpd x
= (mpd
)*svv
- mc
- sc
;
1319 /* --- Fix if the quotient was too large --- *
1321 * This doesn't seem to happen very often.
1324 if (rvl
[-1] > MPW_MAX
/ 2) {
1325 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1330 /* --- Done for another iteration --- */
1332 if (qvl
- qv
> scale
)
1339 /* --- Now fiddle with unnormalizing and things --- */
1341 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1344 /* --- @mpx_udivn@ --- *
1346 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1348 * @const mpw *rv, *rvl@ = dividend
1349 * @mpw d@ = single-precision divisor
1351 * Returns: Remainder after divison.
1353 * Use: Performs a single-precision division operation.
1356 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
1359 size_t ql
= qvl
- qv
;
1365 r
= (r
<< MPW_BITS
) | rv
[i
];
1373 /*----- Test rig ----------------------------------------------------------*/
1377 #include <mLib/alloc.h>
1378 #include <mLib/dstr.h>
1379 #include <mLib/quis.h>
1380 #include <mLib/testrig.h>
1384 #define ALLOC(v, vl, sz) do { \
1385 size_t _sz = (sz); \
1386 mpw *_vv = xmalloc(MPWS(_sz)); \
1387 mpw *_vvl = _vv + _sz; \
1392 #define LOAD(v, vl, d) do { \
1393 const dstr *_d = (d); \
1395 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1396 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1401 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1403 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
1405 const octet
*p
= pp
;
1408 fprintf(stderr
, " %02x", *p
++);
1409 fputc('\n', stderr
);
1412 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
1417 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
1418 fputc('\n', stderr
);
1421 static int chkscan(const mpw
*v
, const mpw
*vl
,
1422 const void *pp
, size_t sz
, int step
)
1425 const octet
*p
= pp
;
1429 mpscan_initx(&mps
, v
, vl
);
1434 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1435 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1437 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1438 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1450 static int loadstore(dstr
*v
)
1453 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1457 dstr_ensure(&d
, v
->len
);
1458 m
= xmalloc(MPWS(sz
));
1460 for (diff
= 0; diff
< sz
; diff
+= 5) {
1465 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1466 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1468 MPX_OCTETS(oct
, m
, ml
);
1469 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1470 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1471 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1475 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1476 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1478 MPX_OCTETS(oct
, m
, ml
);
1479 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1480 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1481 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1487 dumpbits("input data", v
->buf
, v
->len
);
1494 static int twocl(dstr
*v
)
1501 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1502 dstr_ensure(&d
, sz
);
1505 m
= xmalloc(MPWS(sz
));
1508 mpx_loadl(m
, ml
, v
[0].buf
, v
[0].len
);
1509 mpx_storel2cn(m
, ml
, d
.buf
, v
[1].len
);
1510 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1511 dumpbits("\n*** storel2cn failed", d
.buf
, v
[1].len
);
1515 mpx_loadl2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1516 mpx_storel(m
, ml
, d
.buf
, v
[0].len
);
1517 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1518 dumpbits("\n*** loadl2cn failed", d
.buf
, v
[0].len
);
1523 dumpbits("pos", v
[0].buf
, v
[0].len
);
1524 dumpbits("neg", v
[1].buf
, v
[1].len
);
1533 static int twocb(dstr
*v
)
1540 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1541 dstr_ensure(&d
, sz
);
1544 m
= xmalloc(MPWS(sz
));
1547 mpx_loadb(m
, ml
, v
[0].buf
, v
[0].len
);
1548 mpx_storeb2cn(m
, ml
, d
.buf
, v
[1].len
);
1549 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1550 dumpbits("\n*** storeb2cn failed", d
.buf
, v
[1].len
);
1554 mpx_loadb2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1555 mpx_storeb(m
, ml
, d
.buf
, v
[0].len
);
1556 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1557 dumpbits("\n*** loadb2cn failed", d
.buf
, v
[0].len
);
1562 dumpbits("pos", v
[0].buf
, v
[0].len
);
1563 dumpbits("neg", v
[1].buf
, v
[1].len
);
1572 static int lsl(dstr
*v
)
1575 int n
= *(int *)v
[1].buf
;
1582 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1584 mpx_lsl(d
, dl
, a
, al
, n
);
1585 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1586 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1587 dumpmp(" a", a
, al
);
1588 dumpmp("expected", c
, cl
);
1589 dumpmp(" result", d
, dl
);
1593 free(a
); free(c
); free(d
);
1597 static int lslc(dstr
*v
)
1600 int n
= *(int *)v
[1].buf
;
1607 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1609 mpx_lslc(d
, dl
, a
, al
, n
);
1610 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1611 fprintf(stderr
, "\n*** lslc(%i) failed\n", n
);
1612 dumpmp(" a", a
, al
);
1613 dumpmp("expected", c
, cl
);
1614 dumpmp(" result", d
, dl
);
1618 free(a
); free(c
); free(d
);
1622 static int lsr(dstr
*v
)
1625 int n
= *(int *)v
[1].buf
;
1632 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1634 mpx_lsr(d
, dl
, a
, al
, n
);
1635 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1636 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1637 dumpmp(" a", a
, al
);
1638 dumpmp("expected", c
, cl
);
1639 dumpmp(" result", d
, dl
);
1643 free(a
); free(c
); free(d
);
1647 static int uadd(dstr
*v
)
1658 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1660 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1661 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1662 fprintf(stderr
, "\n*** uadd failed\n");
1663 dumpmp(" a", a
, al
);
1664 dumpmp(" b", b
, bl
);
1665 dumpmp("expected", c
, cl
);
1666 dumpmp(" result", d
, dl
);
1670 free(a
); free(b
); free(c
); free(d
);
1674 static int usub(dstr
*v
)
1685 ALLOC(d
, dl
, al
- a
);
1687 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1688 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1689 fprintf(stderr
, "\n*** usub failed\n");
1690 dumpmp(" a", a
, al
);
1691 dumpmp(" b", b
, bl
);
1692 dumpmp("expected", c
, cl
);
1693 dumpmp(" result", d
, dl
);
1697 free(a
); free(b
); free(c
); free(d
);
1701 static int umul(dstr
*v
)
1712 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1714 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1715 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1716 fprintf(stderr
, "\n*** umul failed\n");
1717 dumpmp(" a", a
, al
);
1718 dumpmp(" b", b
, bl
);
1719 dumpmp("expected", c
, cl
);
1720 dumpmp(" result", d
, dl
);
1724 free(a
); free(b
); free(c
); free(d
);
1728 static int usqr(dstr
*v
)
1737 ALLOC(d
, dl
, 2 * (al
- a
));
1739 mpx_usqr(d
, dl
, a
, al
);
1740 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1741 fprintf(stderr
, "\n*** usqr failed\n");
1742 dumpmp(" a", a
, al
);
1743 dumpmp("expected", c
, cl
);
1744 dumpmp(" result", d
, dl
);
1748 free(a
); free(c
); free(d
);
1752 static int udiv(dstr
*v
)
1762 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1766 ALLOC(qq
, qql
, al
- a
);
1767 ALLOC(s
, sl
, (bl
- b
) + 1);
1769 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1770 if (!mpx_ueq(qq
, qql
, q
, ql
) ||
1771 !mpx_ueq(a
, al
, r
, rl
)) {
1772 fprintf(stderr
, "\n*** udiv failed\n");
1773 dumpmp(" divisor", b
, bl
);
1774 dumpmp("expect r", r
, rl
);
1775 dumpmp("result r", a
, al
);
1776 dumpmp("expect q", q
, ql
);
1777 dumpmp("result q", qq
, qql
);
1781 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1785 static test_chunk defs
[] = {
1786 { "load-store", loadstore
, { &type_hex
, 0 } },
1787 { "2cl", twocl
, { &type_hex
, &type_hex
, } },
1788 { "2cb", twocb
, { &type_hex
, &type_hex
, } },
1789 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1790 { "lslc", lslc
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1791 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1792 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1793 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1794 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1795 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1796 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1800 int main(int argc
, char *argv
[])
1802 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1808 /*----- That's all, folks -------------------------------------------------*/