3 * $Id: mpx.c,v 1.19 2004/04/03 03:29:40 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.19 2004/04/03 03:29:40 mdw
34 * Fix overrun in @mpx_lsr@.
36 * Revision 1.18 2004/04/01 12:50:09 mdw
37 * Add cyclic group abstraction, with test code. Separate off exponentation
38 * functions for better static linking. Fix a buttload of bugs on the way.
39 * Generally ensure that negative exponents do inversion correctly. Add
40 * table of standard prime-field subgroups. (Binary field subgroups are
41 * currently unimplemented but easy to add if anyone ever finds a good one.)
43 * Revision 1.17 2004/03/27 00:04:46 mdw
44 * Implement efficient reduction for pleasant-looking primes.
46 * Revision 1.16 2003/05/16 09:09:24 mdw
47 * Fix @mp_lsl2c@. Turns out to be surprisingly tricky.
49 * Revision 1.15 2002/10/20 01:12:31 mdw
50 * Two's complement I/O fixes.
52 * Revision 1.14 2002/10/19 18:55:08 mdw
53 * Fix overflows in shift primitives.
55 * Revision 1.13 2002/10/19 17:56:50 mdw
56 * Fix bit operations. Test them (a bit) better.
58 * Revision 1.12 2002/10/06 22:52:50 mdw
59 * Pile of changes for supporting two's complement properly.
61 * Revision 1.11 2001/04/03 19:36:05 mdw
62 * Add some simple bitwise operations so that Perl can use them.
64 * Revision 1.10 2000/10/08 12:06:12 mdw
65 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
67 * Revision 1.9 2000/06/26 07:52:50 mdw
68 * Portability fix for the bug fix.
70 * Revision 1.8 2000/06/25 12:59:02 mdw
71 * (mpx_udiv): Fix bug in quotient digit estimation.
73 * Revision 1.7 1999/12/22 15:49:07 mdw
74 * New function for division by a small integer.
76 * Revision 1.6 1999/11/20 22:43:44 mdw
77 * Integrate testing for MPX routines.
79 * Revision 1.5 1999/11/20 22:23:27 mdw
80 * Add function versions of some low-level macros with wider use.
82 * Revision 1.4 1999/11/17 18:04:09 mdw
83 * Add two's-complement functionality. Improve mpx_udiv a little by
84 * performing the multiplication of the divisor by q with the subtraction
87 * Revision 1.3 1999/11/13 01:57:31 mdw
88 * Remove stray debugging code.
90 * Revision 1.2 1999/11/13 01:50:59 mdw
91 * Multiprecision routines finished and tested.
93 * Revision 1.1 1999/09/03 08:41:12 mdw
98 /*----- Header files ------------------------------------------------------*/
105 #include <mLib/bits.h>
111 /*----- Loading and storing -----------------------------------------------*/
113 /* --- @mpx_storel@ --- *
115 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
116 * @void *pp@ = pointer to octet array
117 * @size_t sz@ = size of octet array
121 * Use: Stores an MP in an octet array, least significant octet
122 * first. High-end octets are silently discarded if there
123 * isn't enough space for them.
126 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
129 octet
*p
= pp
, *q
= p
+ sz
;
139 *p
++ = U8(w
| n
<< bits
);
141 bits
+= MPW_BITS
- 8;
151 /* --- @mpx_loadl@ --- *
153 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
154 * @const void *pp@ = pointer to octet array
155 * @size_t sz@ = size of octet array
159 * Use: Loads an MP in an octet array, least significant octet
160 * first. High-end octets are ignored if there isn't enough
164 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
168 const octet
*p
= pp
, *q
= p
+ sz
;
177 if (bits
>= MPW_BITS
) {
179 w
= n
>> (MPW_BITS
- bits
+ 8);
189 /* --- @mpx_storeb@ --- *
191 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
192 * @void *pp@ = pointer to octet array
193 * @size_t sz@ = size of octet array
197 * Use: Stores an MP in an octet array, most significant octet
198 * first. High-end octets are silently discarded if there
199 * isn't enough space for them.
202 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
205 octet
*p
= pp
, *q
= p
+ sz
;
215 *--q
= U8(w
| n
<< bits
);
217 bits
+= MPW_BITS
- 8;
227 /* --- @mpx_loadb@ --- *
229 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
230 * @const void *pp@ = pointer to octet array
231 * @size_t sz@ = size of octet array
235 * Use: Loads an MP in an octet array, most significant octet
236 * first. High-end octets are ignored if there isn't enough
240 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
244 const octet
*p
= pp
, *q
= p
+ sz
;
253 if (bits
>= MPW_BITS
) {
255 w
= n
>> (MPW_BITS
- bits
+ 8);
265 /* --- @mpx_storel2cn@ --- *
267 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
268 * @void *pp@ = pointer to octet array
269 * @size_t sz@ = size of octet array
273 * Use: Stores a negative MP in an octet array, least significant
274 * octet first, as two's complement. High-end octets are
275 * silently discarded if there isn't enough space for them.
276 * This obviously makes the output bad.
279 void mpx_storel2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
284 octet
*p
= pp
, *q
= p
+ sz
;
296 bits
+= MPW_BITS
- 8;
314 /* --- @mpx_loadl2cn@ --- *
316 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
317 * @const void *pp@ = pointer to octet array
318 * @size_t sz@ = size of octet array
322 * Use: Loads a negative MP in an octet array, least significant
323 * octet first, as two's complement. High-end octets are
324 * ignored if there isn't enough space for them. This probably
325 * means you made the wrong choice coming here.
328 void mpx_loadl2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
333 const octet
*p
= pp
, *q
= p
+ sz
;
343 if (bits
>= MPW_BITS
) {
345 w
= n
>> (MPW_BITS
- bits
+ 8);
355 /* --- @mpx_storeb2cn@ --- *
357 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
358 * @void *pp@ = pointer to octet array
359 * @size_t sz@ = size of octet array
363 * Use: Stores a negative MP in an octet array, most significant
364 * octet first, as two's complement. High-end octets are
365 * silently discarded if there isn't enough space for them,
366 * which probably isn't what you meant.
369 void mpx_storeb2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
374 octet
*p
= pp
, *q
= p
+ sz
;
386 bits
+= MPW_BITS
- 8;
398 c
= c
&& !(b
& 0xff);
404 /* --- @mpx_loadb2cn@ --- *
406 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
407 * @const void *pp@ = pointer to octet array
408 * @size_t sz@ = size of octet array
412 * Use: Loads a negative MP in an octet array, most significant octet
413 * first as two's complement. High-end octets are ignored if
414 * there isn't enough space for them. This probably means you
415 * chose this function wrongly.
418 void mpx_loadb2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
423 const octet
*p
= pp
, *q
= p
+ sz
;
433 if (bits
>= MPW_BITS
) {
435 w
= n
>> (MPW_BITS
- bits
+ 8);
445 /*----- Logical shifting --------------------------------------------------*/
447 /* --- @mpx_lsl@ --- *
449 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
450 * @const mpw *av, *avl@ = source vector base and limit
451 * @size_t n@ = number of bit positions to shift by
455 * Use: Performs a logical shift left operation on an integer.
458 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
463 /* --- Trivial special case --- */
466 MPX_COPY(dv
, dvl
, av
, avl
);
468 /* --- Single bit shifting --- */
477 *dv
++ = MPW((t
<< 1) | w
);
478 w
= t
>> (MPW_BITS
- 1);
487 /* --- Break out word and bit shifts for more sophisticated work --- */
492 /* --- Handle a shift by a multiple of the word size --- */
498 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
499 memset(dv
, 0, MPWS(nw
));
503 /* --- And finally the difficult case --- *
505 * This is a little convoluted, because I have to start from the end and
506 * work backwards to avoid overwriting the source, if they're both the same
512 size_t nr
= MPW_BITS
- nb
;
513 size_t dvn
= dvl
- dv
;
514 size_t avn
= avl
- av
;
521 if (dvn
> avn
+ nw
) {
522 size_t off
= avn
+ nw
+ 1;
523 MPX_ZERO(dv
+ off
, dvl
);
533 *--dvl
= (t
>> nr
) | w
;
544 /* --- @mpx_lslc@ --- *
546 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
547 * @const mpw *av, *avl@ = source vector base and limit
548 * @size_t n@ = number of bit positions to shift by
552 * Use: Performs a logical shift left operation on an integer, only
553 * it fills in the bits with ones instead of zeroes.
556 void mpx_lslc(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
561 /* --- Trivial special case --- */
564 MPX_COPY(dv
, dvl
, av
, avl
);
566 /* --- Single bit shifting --- */
575 *dv
++ = MPW((t
<< 1) | w
);
576 w
= t
>> (MPW_BITS
- 1);
585 /* --- Break out word and bit shifts for more sophisticated work --- */
590 /* --- Handle a shift by a multiple of the word size --- */
596 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
597 MPX_ONE(dv
, dv
+ nw
);
601 /* --- And finally the difficult case --- *
603 * This is a little convoluted, because I have to start from the end and
604 * work backwards to avoid overwriting the source, if they're both the same
610 size_t nr
= MPW_BITS
- nb
;
611 size_t dvn
= dvl
- dv
;
612 size_t avn
= avl
- av
;
619 if (dvn
> avn
+ nw
) {
620 size_t off
= avn
+ nw
+ 1;
621 MPX_ZERO(dv
+ off
, dvl
);
631 *--dvl
= (t
>> nr
) | w
;
635 *--dvl
= (MPW_MAX
>> nr
) | w
;
642 /* --- @mpx_lsr@ --- *
644 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
645 * @const mpw *av, *avl@ = source vector base and limit
646 * @size_t n@ = number of bit positions to shift by
650 * Use: Performs a logical shift right operation on an integer.
653 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
658 /* --- Trivial special case --- */
661 MPX_COPY(dv
, dvl
, av
, avl
);
663 /* --- Single bit shifting --- */
666 mpw w
= av
< avl ?
*av
++ >> 1 : 0;
672 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
682 /* --- Break out word and bit shifts for more sophisticated work --- */
687 /* --- Handle a shift by a multiple of the word size --- */
693 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
696 /* --- And finally the difficult case --- */
700 size_t nr
= MPW_BITS
- nb
;
703 w
= av
< avl ?
*av
++ : 0;
709 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
713 *dv
++ = MPW(w
>> nb
);
721 /*----- Bitwise operations ------------------------------------------------*/
723 /* --- @mpx_bitop@ --- *
725 * Arguments: @mpw *dv, *dvl@ = destination vector
726 * @const mpw *av, *avl@ = first source vector
727 * @const mpw *bv, *bvl@ = second source vector
731 * Use; Provides the dyadic boolean functions.
734 #define MPX_BITBINOP(string) \
736 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
737 const mpw *bv, const mpw *bvl) \
739 MPX_SHRINK(av, avl); \
740 MPX_SHRINK(bv, bvl); \
744 a = (av < avl) ? *av++ : 0; \
745 b = (bv < bvl) ? *bv++ : 0; \
746 *dv++ = B##string(a, b); \
750 MPX_DOBIN(MPX_BITBINOP
)
752 void mpx_not(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
758 a
= (av
< avl
) ?
*av
++ : 0;
763 /*----- Unsigned arithmetic -----------------------------------------------*/
765 /* --- @mpx_2c@ --- *
767 * Arguments: @mpw *dv, *dvl@ = destination vector
768 * @const mpw *v, *vl@ = source vector
772 * Use: Calculates the two's complement of @v@.
775 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
778 while (dv
< dvl
&& v
< vl
)
779 *dv
++ = c
= MPW(~*v
++);
786 MPX_UADDN(dv
, dvl
, 1);
789 /* --- @mpx_ueq@ --- *
791 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
792 * @const mpw *bv, *bvl@ = second argument vector base and limit
794 * Returns: Nonzero if the two vectors are equal.
796 * Use: Performs an unsigned integer test for equality.
799 int mpx_ueq(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
803 if (avl
- av
!= bvl
- bv
)
812 /* --- @mpx_ucmp@ --- *
814 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
815 * @const mpw *bv, *bvl@ = second argument vector base and limit
817 * Returns: Less than, equal to, or greater than zero depending on
818 * whether @a@ is less than, equal to or greater than @b@,
821 * Use: Performs an unsigned integer comparison.
824 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
829 if (avl
- av
> bvl
- bv
)
831 else if (avl
- av
< bvl
- bv
)
833 else while (avl
> av
) {
834 mpw a
= *--avl
, b
= *--bvl
;
843 /* --- @mpx_uadd@ --- *
845 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
846 * @const mpw *av, *avl@ = first addend vector base and limit
847 * @const mpw *bv, *bvl@ = second addend vector base and limit
851 * Use: Performs unsigned integer addition. If the result overflows
852 * the destination vector, high-order bits are discarded. This
853 * means that two's complement addition happens more or less for
854 * free, although that's more a side-effect than anything else.
855 * The result vector may be equal to either or both source
856 * vectors, but may not otherwise overlap them.
859 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
860 const mpw
*bv
, const mpw
*bvl
)
864 while (av
< avl
|| bv
< bvl
) {
869 a
= (av
< avl
) ?
*av
++ : 0;
870 b
= (bv
< bvl
) ?
*bv
++ : 0;
871 x
= (mpd
)a
+ (mpd
)b
+ c
;
881 /* --- @mpx_uaddn@ --- *
883 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
884 * @mpw n@ = other addend
888 * Use: Adds a small integer to a multiprecision number.
891 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
893 /* --- @mpx_uaddnlsl@ --- *
895 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
896 * @mpw a@ = second argument
897 * @unsigned o@ = offset in bits
901 * Use: Computes %$d + 2^o a$%. If the result overflows then
902 * high-order bits are discarded, as usual. We must have
903 * @0 < o < MPW_BITS@.
906 void mpx_uaddnlsl(mpw
*dv
, mpw
*dvl
, mpw a
, unsigned o
)
910 while (x
&& dv
< dvl
) {
917 /* --- @mpx_usub@ --- *
919 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
920 * @const mpw *av, *avl@ = first argument vector base and limit
921 * @const mpw *bv, *bvl@ = second argument vector base and limit
925 * Use: Performs unsigned integer subtraction. If the result
926 * overflows the destination vector, high-order bits are
927 * discarded. This means that two's complement subtraction
928 * happens more or less for free, althuogh that's more a side-
929 * effect than anything else. The result vector may be equal to
930 * either or both source vectors, but may not otherwise overlap
934 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
935 const mpw
*bv
, const mpw
*bvl
)
939 while (av
< avl
|| bv
< bvl
) {
944 a
= (av
< avl
) ?
*av
++ : 0;
945 b
= (bv
< bvl
) ?
*bv
++ : 0;
946 x
= (mpd
)a
- (mpd
)b
- c
;
959 /* --- @mpx_usubn@ --- *
961 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
966 * Use: Subtracts a small integer from a multiprecision number.
969 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
971 /* --- @mpx_uaddnlsl@ --- *
973 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
974 * @mpw a@ = second argument
975 * @unsigned o@ = offset in bits
979 * Use: Computes %$d + 2^o a$%. If the result overflows then
980 * high-order bits are discarded, as usual. We must have
981 * @0 < o < MPW_BITS@.
984 void mpx_usubnlsl(mpw
*dv
, mpw
*dvl
, mpw a
, unsigned o
)
986 mpw b
= a
>> (MPW_BITS
- o
);
990 mpd x
= (mpd
)*dv
- (mpd
)a
;
994 MPX_USUBN(dv
, dvl
, b
);
998 /* --- @mpx_umul@ --- *
1000 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1001 * @const mpw *av, *avl@ = multiplicand vector base and limit
1002 * @const mpw *bv, *bvl@ = multiplier vector base and limit
1006 * Use: Performs unsigned integer multiplication. If the result
1007 * overflows the desination vector, high-order bits are
1008 * discarded. The result vector may not overlap the argument
1009 * vectors in any way.
1012 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
1013 const mpw
*bv
, const mpw
*bvl
)
1015 /* --- This is probably worthwhile on a multiply --- */
1017 MPX_SHRINK(av
, avl
);
1018 MPX_SHRINK(bv
, bvl
);
1020 /* --- Deal with a multiply by zero --- */
1027 /* --- Do the initial multiply and initialize the accumulator --- */
1029 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
1031 /* --- Do the remaining multiply/accumulates --- */
1033 while (dv
< dvl
&& bv
< bvl
) {
1036 const mpw
*avv
= av
;
1043 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
1047 MPX_UADDN(dvv
, dvl
, c
);
1052 /* --- @mpx_umuln@ --- *
1054 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1055 * @const mpw *av, *avl@ = multiplicand vector base and limit
1056 * @mpw m@ = multiplier
1060 * Use: Multiplies a multiprecision integer by a single-word value.
1061 * The destination and source may be equal. The destination
1062 * is completely cleared after use.
1065 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
1067 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
1070 /* --- @mpx_umlan@ --- *
1072 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
1073 * @const mpw *av, *avl@ = multiplicand vector base and limit
1074 * @mpw m@ = multiplier
1078 * Use: Multiplies a multiprecision integer by a single-word value
1079 * and adds the result to an accumulator.
1082 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
1084 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
1087 /* --- @mpx_usqr@ --- *
1089 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1090 * @const mpw *av, *av@ = source vector base and limit
1094 * Use: Performs unsigned integer squaring. The result vector must
1095 * not overlap the source vector in any way.
1098 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
1102 /* --- Main loop --- */
1105 const mpw
*avv
= av
;
1110 /* --- Stop if I've run out of destination --- */
1115 /* --- Work out the square at this point in the proceedings --- */
1118 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
1120 c
= MPW(x
>> MPW_BITS
);
1123 /* --- Now fix up the rest of the vector upwards --- */
1126 while (dvv
< dvl
&& avv
< avl
) {
1127 mpd x
= (mpd
)a
* (mpd
)*avv
++;
1128 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
1129 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
1132 while (dvv
< dvl
&& c
) {
1138 /* --- Get ready for the next round --- */
1145 /* --- @mpx_udiv@ --- *
1147 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1148 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1149 * @const mpw *dv, *dvl@ = divisor vector base and limit
1150 * @mpw *sv, *svl@ = scratch workspace
1154 * Use: Performs unsigned integer division. If the result overflows
1155 * the quotient vector, high-order bits are discarded. (Clearly
1156 * the remainder vector can't overflow.) The various vectors
1157 * may not overlap in any way. Yes, I know it's a bit odd
1158 * requiring the dividend to be in the result position but it
1159 * does make some sense really. The remainder must have
1160 * headroom for at least two extra words. The scratch space
1161 * must be at least one word larger than the divisor.
1164 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
1165 const mpw
*dv
, const mpw
*dvl
,
1172 /* --- Initialize the quotient --- */
1176 /* --- Perform some sanity checks --- */
1178 MPX_SHRINK(dv
, dvl
);
1179 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
1181 /* --- Normalize the divisor --- *
1183 * The algorithm requires that the divisor be at least two digits long.
1184 * This is easy to fix.
1191 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
1192 if (d
<= (MPW_MAX
>> b
)) {
1201 /* --- Normalize the dividend/remainder to match --- */
1204 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
1205 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
1208 MPX_SHRINK(dv
, dvl
);
1211 MPX_SHRINK(rv
, rvl
);
1215 /* --- Work out the relative scales --- */
1218 size_t rvn
= rvl
- rv
;
1219 size_t dvn
= dvl
- dv
;
1221 /* --- If the divisor is clearly larger, notice this --- */
1224 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1231 /* --- Calculate the most significant quotient digit --- *
1233 * Because the divisor has its top bit set, this can only happen once. The
1234 * pointer arithmetic is a little contorted, to make sure that the
1235 * behaviour is defined.
1238 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
1239 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1240 if (qvl
- qv
> scale
)
1244 /* --- Now for the main loop --- */
1253 /* --- Get an estimate for the next quotient digit --- */
1260 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
1266 /* --- Refine the estimate --- */
1269 mpd yh
= (mpd
)d
* q
;
1270 mpd yy
= (mpd
)dd
* q
;
1274 yh
+= yy
>> MPW_BITS
;
1277 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
1286 /* --- Remove a chunk from the dividend --- */
1293 /* --- Calculate the size of the chunk --- *
1295 * This does the whole job of calculating @r >> scale - qd@.
1298 for (svv
= rv
+ scale
, dvv
= dv
;
1299 dvv
< dvl
&& svv
< rvl
;
1301 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
1303 x
= (mpd
)*svv
- MPW(x
) - sc
;
1312 mpd x
= (mpd
)*svv
- mc
- sc
;
1322 /* --- Fix if the quotient was too large --- *
1324 * This doesn't seem to happen very often.
1327 if (rvl
[-1] > MPW_MAX
/ 2) {
1328 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1333 /* --- Done for another iteration --- */
1335 if (qvl
- qv
> scale
)
1342 /* --- Now fiddle with unnormalizing and things --- */
1344 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1347 /* --- @mpx_udivn@ --- *
1349 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1351 * @const mpw *rv, *rvl@ = dividend
1352 * @mpw d@ = single-precision divisor
1354 * Returns: Remainder after divison.
1356 * Use: Performs a single-precision division operation.
1359 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
1362 size_t ql
= qvl
- qv
;
1368 r
= (r
<< MPW_BITS
) | rv
[i
];
1376 /*----- Test rig ----------------------------------------------------------*/
1380 #include <mLib/alloc.h>
1381 #include <mLib/dstr.h>
1382 #include <mLib/quis.h>
1383 #include <mLib/testrig.h>
1387 #define ALLOC(v, vl, sz) do { \
1388 size_t _sz = (sz); \
1389 mpw *_vv = xmalloc(MPWS(_sz)); \
1390 mpw *_vvl = _vv + _sz; \
1395 #define LOAD(v, vl, d) do { \
1396 const dstr *_d = (d); \
1398 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1399 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1404 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1406 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
1408 const octet
*p
= pp
;
1411 fprintf(stderr
, " %02x", *p
++);
1412 fputc('\n', stderr
);
1415 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
1420 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
1421 fputc('\n', stderr
);
1424 static int chkscan(const mpw
*v
, const mpw
*vl
,
1425 const void *pp
, size_t sz
, int step
)
1428 const octet
*p
= pp
;
1432 mpscan_initx(&mps
, v
, vl
);
1437 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1438 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1440 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1441 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1453 static int loadstore(dstr
*v
)
1456 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1460 dstr_ensure(&d
, v
->len
);
1461 m
= xmalloc(MPWS(sz
));
1463 for (diff
= 0; diff
< sz
; diff
+= 5) {
1468 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1469 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1471 MPX_OCTETS(oct
, m
, ml
);
1472 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1473 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1474 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1478 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1479 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1481 MPX_OCTETS(oct
, m
, ml
);
1482 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1483 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1484 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1490 dumpbits("input data", v
->buf
, v
->len
);
1497 static int twocl(dstr
*v
)
1504 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1505 dstr_ensure(&d
, sz
);
1508 m
= xmalloc(MPWS(sz
));
1511 mpx_loadl(m
, ml
, v
[0].buf
, v
[0].len
);
1512 mpx_storel2cn(m
, ml
, d
.buf
, v
[1].len
);
1513 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1514 dumpbits("\n*** storel2cn failed", d
.buf
, v
[1].len
);
1518 mpx_loadl2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1519 mpx_storel(m
, ml
, d
.buf
, v
[0].len
);
1520 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1521 dumpbits("\n*** loadl2cn failed", d
.buf
, v
[0].len
);
1526 dumpbits("pos", v
[0].buf
, v
[0].len
);
1527 dumpbits("neg", v
[1].buf
, v
[1].len
);
1536 static int twocb(dstr
*v
)
1543 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1544 dstr_ensure(&d
, sz
);
1547 m
= xmalloc(MPWS(sz
));
1550 mpx_loadb(m
, ml
, v
[0].buf
, v
[0].len
);
1551 mpx_storeb2cn(m
, ml
, d
.buf
, v
[1].len
);
1552 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1553 dumpbits("\n*** storeb2cn failed", d
.buf
, v
[1].len
);
1557 mpx_loadb2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1558 mpx_storeb(m
, ml
, d
.buf
, v
[0].len
);
1559 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1560 dumpbits("\n*** loadb2cn failed", d
.buf
, v
[0].len
);
1565 dumpbits("pos", v
[0].buf
, v
[0].len
);
1566 dumpbits("neg", v
[1].buf
, v
[1].len
);
1575 static int lsl(dstr
*v
)
1578 int n
= *(int *)v
[1].buf
;
1585 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1587 mpx_lsl(d
, dl
, a
, al
, n
);
1588 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1589 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1590 dumpmp(" a", a
, al
);
1591 dumpmp("expected", c
, cl
);
1592 dumpmp(" result", d
, dl
);
1596 free(a
); free(c
); free(d
);
1600 static int lslc(dstr
*v
)
1603 int n
= *(int *)v
[1].buf
;
1610 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1612 mpx_lslc(d
, dl
, a
, al
, n
);
1613 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1614 fprintf(stderr
, "\n*** lslc(%i) failed\n", n
);
1615 dumpmp(" a", a
, al
);
1616 dumpmp("expected", c
, cl
);
1617 dumpmp(" result", d
, dl
);
1621 free(a
); free(c
); free(d
);
1625 static int lsr(dstr
*v
)
1628 int n
= *(int *)v
[1].buf
;
1635 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1637 mpx_lsr(d
, dl
, a
, al
, n
);
1638 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1639 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1640 dumpmp(" a", a
, al
);
1641 dumpmp("expected", c
, cl
);
1642 dumpmp(" result", d
, dl
);
1646 free(a
); free(c
); free(d
);
1650 static int uadd(dstr
*v
)
1661 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1663 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1664 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1665 fprintf(stderr
, "\n*** uadd failed\n");
1666 dumpmp(" a", a
, al
);
1667 dumpmp(" b", b
, bl
);
1668 dumpmp("expected", c
, cl
);
1669 dumpmp(" result", d
, dl
);
1673 free(a
); free(b
); free(c
); free(d
);
1677 static int usub(dstr
*v
)
1688 ALLOC(d
, dl
, al
- a
);
1690 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1691 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1692 fprintf(stderr
, "\n*** usub failed\n");
1693 dumpmp(" a", a
, al
);
1694 dumpmp(" b", b
, bl
);
1695 dumpmp("expected", c
, cl
);
1696 dumpmp(" result", d
, dl
);
1700 free(a
); free(b
); free(c
); free(d
);
1704 static int umul(dstr
*v
)
1715 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1717 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1718 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1719 fprintf(stderr
, "\n*** umul failed\n");
1720 dumpmp(" a", a
, al
);
1721 dumpmp(" b", b
, bl
);
1722 dumpmp("expected", c
, cl
);
1723 dumpmp(" result", d
, dl
);
1727 free(a
); free(b
); free(c
); free(d
);
1731 static int usqr(dstr
*v
)
1740 ALLOC(d
, dl
, 2 * (al
- a
));
1742 mpx_usqr(d
, dl
, a
, al
);
1743 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1744 fprintf(stderr
, "\n*** usqr failed\n");
1745 dumpmp(" a", a
, al
);
1746 dumpmp("expected", c
, cl
);
1747 dumpmp(" result", d
, dl
);
1751 free(a
); free(c
); free(d
);
1755 static int udiv(dstr
*v
)
1765 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1769 ALLOC(qq
, qql
, al
- a
);
1770 ALLOC(s
, sl
, (bl
- b
) + 1);
1772 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1773 if (!mpx_ueq(qq
, qql
, q
, ql
) ||
1774 !mpx_ueq(a
, al
, r
, rl
)) {
1775 fprintf(stderr
, "\n*** udiv failed\n");
1776 dumpmp(" divisor", b
, bl
);
1777 dumpmp("expect r", r
, rl
);
1778 dumpmp("result r", a
, al
);
1779 dumpmp("expect q", q
, ql
);
1780 dumpmp("result q", qq
, qql
);
1784 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1788 static test_chunk defs
[] = {
1789 { "load-store", loadstore
, { &type_hex
, 0 } },
1790 { "2cl", twocl
, { &type_hex
, &type_hex
, } },
1791 { "2cb", twocb
, { &type_hex
, &type_hex
, } },
1792 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1793 { "lslc", lslc
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1794 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1795 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1796 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1797 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1798 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1799 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1803 int main(int argc
, char *argv
[])
1805 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1811 /*----- That's all, folks -------------------------------------------------*/