3 * $Id: mpx.c,v 1.15 2002/10/20 01:12:31 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.15 2002/10/20 01:12:31 mdw
34 * Two's complement I/O fixes.
36 * Revision 1.14 2002/10/19 18:55:08 mdw
37 * Fix overflows in shift primitives.
39 * Revision 1.13 2002/10/19 17:56:50 mdw
40 * Fix bit operations. Test them (a bit) better.
42 * Revision 1.12 2002/10/06 22:52:50 mdw
43 * Pile of changes for supporting two's complement properly.
45 * Revision 1.11 2001/04/03 19:36:05 mdw
46 * Add some simple bitwise operations so that Perl can use them.
48 * Revision 1.10 2000/10/08 12:06:12 mdw
49 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
51 * Revision 1.9 2000/06/26 07:52:50 mdw
52 * Portability fix for the bug fix.
54 * Revision 1.8 2000/06/25 12:59:02 mdw
55 * (mpx_udiv): Fix bug in quotient digit estimation.
57 * Revision 1.7 1999/12/22 15:49:07 mdw
58 * New function for division by a small integer.
60 * Revision 1.6 1999/11/20 22:43:44 mdw
61 * Integrate testing for MPX routines.
63 * Revision 1.5 1999/11/20 22:23:27 mdw
64 * Add function versions of some low-level macros with wider use.
66 * Revision 1.4 1999/11/17 18:04:09 mdw
67 * Add two's-complement functionality. Improve mpx_udiv a little by
68 * performing the multiplication of the divisor by q with the subtraction
71 * Revision 1.3 1999/11/13 01:57:31 mdw
72 * Remove stray debugging code.
74 * Revision 1.2 1999/11/13 01:50:59 mdw
75 * Multiprecision routines finished and tested.
77 * Revision 1.1 1999/09/03 08:41:12 mdw
82 /*----- Header files ------------------------------------------------------*/
89 #include <mLib/bits.h>
95 /*----- Loading and storing -----------------------------------------------*/
97 /* --- @mpx_storel@ --- *
99 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
100 * @void *pp@ = pointer to octet array
101 * @size_t sz@ = size of octet array
105 * Use: Stores an MP in an octet array, least significant octet
106 * first. High-end octets are silently discarded if there
107 * isn't enough space for them.
110 void mpx_storel(const mpw *v, const mpw *vl, void *pp, size_t sz)
113 octet *p = pp, *q = p + sz;
123 *p++ = U8(w | n << bits);
125 bits += MPW_BITS - 8;
135 /* --- @mpx_loadl@ --- *
137 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
138 * @const void *pp@ = pointer to octet array
139 * @size_t sz@ = size of octet array
143 * Use: Loads an MP in an octet array, least significant octet
144 * first. High-end octets are ignored if there isn't enough
148 void mpx_loadl(mpw *v, mpw *vl, const void *pp, size_t sz)
152 const octet *p = pp, *q = p + sz;
161 if (bits >= MPW_BITS) {
163 w = n >> (MPW_BITS - bits + 8);
173 /* --- @mpx_storeb@ --- *
175 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
176 * @void *pp@ = pointer to octet array
177 * @size_t sz@ = size of octet array
181 * Use: Stores an MP in an octet array, most significant octet
182 * first. High-end octets are silently discarded if there
183 * isn't enough space for them.
186 void mpx_storeb(const mpw *v, const mpw *vl, void *pp, size_t sz)
189 octet *p = pp, *q = p + sz;
199 *--q = U8(w | n << bits);
201 bits += MPW_BITS - 8;
211 /* --- @mpx_loadb@ --- *
213 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
214 * @const void *pp@ = pointer to octet array
215 * @size_t sz@ = size of octet array
219 * Use: Loads an MP in an octet array, most significant octet
220 * first. High-end octets are ignored if there isn't enough
224 void mpx_loadb(mpw *v, mpw *vl, const void *pp, size_t sz)
228 const octet *p = pp, *q = p + sz;
237 if (bits >= MPW_BITS) {
239 w = n >> (MPW_BITS - bits + 8);
249 /* --- @mpx_storel2cn@ --- *
251 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
252 * @void *pp@ = pointer to octet array
253 * @size_t sz@ = size of octet array
257 * Use: Stores a negative MP in an octet array, least significant
258 * octet first, as two's complement. High-end octets are
259 * silently discarded if there isn't enough space for them.
260 * This obviously makes the output bad.
263 void mpx_storel2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
268 octet *p = pp, *q = p + sz;
280 bits += MPW_BITS - 8;
298 /* --- @mpx_loadl2cn@ --- *
300 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
301 * @const void *pp@ = pointer to octet array
302 * @size_t sz@ = size of octet array
306 * Use: Loads a negative MP in an octet array, least significant
307 * octet first, as two's complement. High-end octets are
308 * ignored if there isn't enough space for them. This probably
309 * means you made the wrong choice coming here.
312 void mpx_loadl2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
317 const octet *p = pp, *q = p + sz;
327 if (bits >= MPW_BITS) {
329 w = n >> (MPW_BITS - bits + 8);
339 /* --- @mpx_storeb2cn@ --- *
341 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
342 * @void *pp@ = pointer to octet array
343 * @size_t sz@ = size of octet array
347 * Use: Stores a negative MP in an octet array, most significant
348 * octet first, as two's complement. High-end octets are
349 * silently discarded if there isn't enough space for them,
350 * which probably isn't what you meant.
353 void mpx_storeb2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
358 octet *p = pp, *q = p + sz;
370 bits += MPW_BITS - 8;
382 c = c && !(b & 0xff);
388 /* --- @mpx_loadb2cn@ --- *
390 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
391 * @const void *pp@ = pointer to octet array
392 * @size_t sz@ = size of octet array
396 * Use: Loads a negative MP in an octet array, most significant octet
397 * first as two's complement. High-end octets are ignored if
398 * there isn't enough space for them. This probably means you
399 * chose this function wrongly.
402 void mpx_loadb2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
407 const octet *p = pp, *q = p + sz;
417 if (bits >= MPW_BITS) {
419 w = n >> (MPW_BITS - bits + 8);
429 /*----- Logical shifting --------------------------------------------------*/
431 /* --- @mpx_lsl@ --- *
433 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
434 * @const mpw *av, *avl@ = source vector base and limit
435 * @size_t n@ = number of bit positions to shift by
439 * Use: Performs a logical shift left operation on an integer.
442 void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
447 /* --- Trivial special case --- */
450 MPX_COPY(dv, dvl, av, avl);
452 /* --- Single bit shifting --- */
461 *dv++ = MPW((t << 1) | w);
462 w = t >> (MPW_BITS - 1);
471 /* --- Break out word and bit shifts for more sophisticated work --- */
476 /* --- Handle a shift by a multiple of the word size --- */
482 MPX_COPY(dv + nw, dvl, av, avl);
483 memset(dv, 0, MPWS(nw));
487 /* --- And finally the difficult case --- *
489 * This is a little convoluted, because I have to start from the end and
490 * work backwards to avoid overwriting the source, if they're both the same
496 size_t nr = MPW_BITS - nb;
497 size_t dvn = dvl - dv;
498 size_t avn = avl - av;
505 if (dvn > avn + nw) {
506 size_t off = avn + nw + 1;
507 MPX_ZERO(dv + off, dvl);
517 *--dvl = (t >> nr) | w;
528 /* --- @mpx_lsr@ --- *
530 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
531 * @const mpw *av, *avl@ = source vector base and limit
532 * @size_t n@ = number of bit positions to shift by
536 * Use: Performs a logical shift right operation on an integer.
539 void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
544 /* --- Trivial special case --- */
547 MPX_COPY(dv, dvl, av, avl);
549 /* --- Single bit shifting --- */
558 *dv++ = MPW((t << (MPW_BITS - 1)) | w);
568 /* --- Break out word and bit shifts for more sophisticated work --- */
573 /* --- Handle a shift by a multiple of the word size --- */
579 MPX_COPY(dv, dvl, av + nw, avl);
582 /* --- And finally the difficult case --- */
586 size_t nr = MPW_BITS - nb;
589 w = av < avl ? *av++ : 0;
595 *dv++ = MPW((w >> nb) | (t << nr));
599 *dv++ = MPW(w >> nb);
607 /*----- Bitwise operations ------------------------------------------------*/
609 /* --- @mpx_bitop@ --- *
611 * Arguments: @mpw *dv, *dvl@ = destination vector
612 * @const mpw *av, *avl@ = first source vector
613 * @const mpw *bv, *bvl@ = second source vector
617 * Use; Provides the dyadic boolean functions.
620 #define MPX_BITBINOP(string) \
622 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
623 const mpw *bv, const mpw *bvl) \
625 MPX_SHRINK(av, avl); \
626 MPX_SHRINK(bv, bvl); \
630 a = (av < avl) ? *av++ : 0; \
631 b = (bv < bvl) ? *bv++ : 0; \
632 *dv++ = B##string(a, b); \
636 MPX_DOBIN(MPX_BITBINOP)
638 void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
644 a = (av < avl) ? *av++ : 0;
649 /*----- Unsigned arithmetic -----------------------------------------------*/
651 /* --- @mpx_2c@ --- *
653 * Arguments: @mpw *dv, *dvl@ = destination vector
654 * @const mpw *v, *vl@ = source vector
658 * Use: Calculates the two's complement of @v@.
661 void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl)
664 while (dv < dvl && v < vl)
665 *dv++ = c = MPW(~*v++);
672 MPX_UADDN(dv, dvl, 1);
675 /* --- @mpx_ueq@ --- *
677 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
678 * @const mpw *bv, *bvl@ = second argument vector base and limit
680 * Returns: Nonzero if the two vectors are equal.
682 * Use: Performs an unsigned integer test for equality.
685 int mpx_ueq(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
689 if (avl - av != bvl - bv)
698 /* --- @mpx_ucmp@ --- *
700 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
701 * @const mpw *bv, *bvl@ = second argument vector base and limit
703 * Returns: Less than, equal to, or greater than zero depending on
704 * whether @a@ is less than, equal to or greater than @b@,
707 * Use: Performs an unsigned integer comparison.
710 int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
715 if (avl - av > bvl - bv)
717 else if (avl - av < bvl - bv)
719 else while (avl > av) {
720 mpw a = *--avl, b = *--bvl;
729 /* --- @mpx_uadd@ --- *
731 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
732 * @const mpw *av, *avl@ = first addend vector base and limit
733 * @const mpw *bv, *bvl@ = second addend vector base and limit
737 * Use: Performs unsigned integer addition. If the result overflows
738 * the destination vector, high-order bits are discarded. This
739 * means that two's complement addition happens more or less for
740 * free, although that's more a side-effect than anything else.
741 * The result vector may be equal to either or both source
742 * vectors, but may not otherwise overlap them.
745 void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
746 const mpw *bv, const mpw *bvl)
750 while (av < avl || bv < bvl) {
755 a = (av < avl) ? *av++ : 0;
756 b = (bv < bvl) ? *bv++ : 0;
757 x = (mpd)a + (mpd)b + c;
767 /* --- @mpx_uaddn@ --- *
769 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
770 * @mpw n@ = other addend
774 * Use: Adds a small integer to a multiprecision number.
777 void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); }
779 /* --- @mpx_usub@ --- *
781 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
782 * @const mpw *av, *avl@ = first argument vector base and limit
783 * @const mpw *bv, *bvl@ = second argument vector base and limit
787 * Use: Performs unsigned integer subtraction. If the result
788 * overflows the destination vector, high-order bits are
789 * discarded. This means that two's complement subtraction
790 * happens more or less for free, althuogh that's more a side-
791 * effect than anything else. The result vector may be equal to
792 * either or both source vectors, but may not otherwise overlap
796 void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
797 const mpw *bv, const mpw *bvl)
801 while (av < avl || bv < bvl) {
806 a = (av < avl) ? *av++ : 0;
807 b = (bv < bvl) ? *bv++ : 0;
808 x = (mpd)a - (mpd)b - c;
821 /* --- @mpx_usubn@ --- *
823 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
828 * Use: Subtracts a small integer from a multiprecision number.
831 void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); }
833 /* --- @mpx_umul@ --- *
835 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
836 * @const mpw *av, *avl@ = multiplicand vector base and limit
837 * @const mpw *bv, *bvl@ = multiplier vector base and limit
841 * Use: Performs unsigned integer multiplication. If the result
842 * overflows the desination vector, high-order bits are
843 * discarded. The result vector may not overlap the argument
844 * vectors in any way.
847 void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
848 const mpw *bv, const mpw *bvl)
850 /* --- This is probably worthwhile on a multiply --- */
855 /* --- Deal with a multiply by zero --- */
862 /* --- Do the initial multiply and initialize the accumulator --- */
864 MPX_UMULN(dv, dvl, av, avl, *bv++);
866 /* --- Do the remaining multiply/accumulates --- */
868 while (dv < dvl && bv < bvl) {
878 x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c;
882 MPX_UADDN(dvv, dvl, c);
887 /* --- @mpx_umuln@ --- *
889 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
890 * @const mpw *av, *avl@ = multiplicand vector base and limit
891 * @mpw m@ = multiplier
895 * Use: Multiplies a multiprecision integer by a single-word value.
896 * The destination and source may be equal. The destination
897 * is completely cleared after use.
900 void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
902 MPX_UMULN(dv, dvl, av, avl, m);
905 /* --- @mpx_umlan@ --- *
907 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
908 * @const mpw *av, *avl@ = multiplicand vector base and limit
909 * @mpw m@ = multiplier
913 * Use: Multiplies a multiprecision integer by a single-word value
914 * and adds the result to an accumulator.
917 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
919 MPX_UMLAN(dv, dvl, av, avl, m);
922 /* --- @mpx_usqr@ --- *
924 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
925 * @const mpw *av, *av@ = source vector base and limit
929 * Use: Performs unsigned integer squaring. The result vector must
930 * not overlap the source vector in any way.
933 void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
937 /* --- Main loop --- */
945 /* --- Stop if I've run out of destination --- */
950 /* --- Work out the square at this point in the proceedings --- */
953 mpd x = (mpd)a * (mpd)a + *dvv;
955 c = MPW(x >> MPW_BITS);
958 /* --- Now fix up the rest of the vector upwards --- */
961 while (dvv < dvl && avv < avl) {
962 mpd x = (mpd)a * (mpd)*avv++;
963 mpd y = ((x << 1) & MPW_MAX) + c + *dvv;
964 c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS);
967 while (dvv < dvl && c) {
973 /* --- Get ready for the next round --- */
980 /* --- @mpx_udiv@ --- *
982 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
983 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
984 * @const mpw *dv, *dvl@ = divisor vector base and limit
985 * @mpw *sv, *svl@ = scratch workspace
989 * Use: Performs unsigned integer division. If the result overflows
990 * the quotient vector, high-order bits are discarded. (Clearly
991 * the remainder vector can't overflow.) The various vectors
992 * may not overlap in any way. Yes, I know it's a bit odd
993 * requiring the dividend to be in the result position but it
994 * does make some sense really. The remainder must have
995 * headroom for at least two extra words. The scratch space
996 * must be at least one word larger than the divisor.
999 void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
1000 const mpw *dv, const mpw *dvl,
1007 /* --- Initialize the quotient --- */
1011 /* --- Perform some sanity checks --- */
1013 MPX_SHRINK(dv, dvl);
1014 assert(((void)"division by zero in mpx_udiv", dv < dvl));
1016 /* --- Normalize the divisor --- *
1018 * The algorithm requires that the divisor be at least two digits long.
1019 * This is easy to fix.
1026 for (b = MPW_BITS / 2; b; b >>= 1) {
1027 if (d < (MPW_MAX >> b)) {
1036 /* --- Normalize the dividend/remainder to match --- */
1039 mpx_lsl(rv, rvl, rv, rvl, norm);
1040 mpx_lsl(sv, svl, dv, dvl, norm);
1043 MPX_SHRINK(dv, dvl);
1046 MPX_SHRINK(rv, rvl);
1050 /* --- Work out the relative scales --- */
1053 size_t rvn = rvl - rv;
1054 size_t dvn = dvl - dv;
1056 /* --- If the divisor is clearly larger, notice this --- */
1059 mpx_lsr(rv, rvl, rv, rvl, norm);
1066 /* --- Calculate the most significant quotient digit --- *
1068 * Because the divisor has its top bit set, this can only happen once. The
1069 * pointer arithmetic is a little contorted, to make sure that the
1070 * behaviour is defined.
1073 if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) {
1074 mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1075 if (qvl - qv > scale)
1079 /* --- Now for the main loop --- */
1088 /* --- Get an estimate for the next quotient digit --- */
1095 rh = ((mpd)r << MPW_BITS) | rr;
1101 /* --- Refine the estimate --- */
1104 mpd yh = (mpd)d * q;
1105 mpd yy = (mpd)dd * q;
1109 yh += yy >> MPW_BITS;
1112 while (yh > rh || (yh == rh && yl > rrr)) {
1121 /* --- Remove a chunk from the dividend --- */
1128 /* --- Calculate the size of the chunk --- *
1130 * This does the whole job of calculating @r >> scale - qd@.
1133 for (svv = rv + scale, dvv = dv;
1134 dvv < dvl && svv < rvl;
1136 mpd x = (mpd)*dvv * (mpd)q + mc;
1138 x = (mpd)*svv - MPW(x) - sc;
1147 mpd x = (mpd)*svv - mc - sc;
1157 /* --- Fix if the quotient was too large --- *
1159 * This doesn't seem to happen very often.
1162 if (rvl[-1] > MPW_MAX / 2) {
1163 mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1168 /* --- Done for another iteration --- */
1170 if (qvl - qv > scale)
1177 /* --- Now fiddle with unnormalizing and things --- */
1179 mpx_lsr(rv, rvl, rv, rvl, norm);
1182 /* --- @mpx_udivn@ --- *
1184 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1186 * @const mpw *rv, *rvl@ = dividend
1187 * @mpw d@ = single-precision divisor
1189 * Returns: Remainder after divison.
1191 * Use: Performs a single-precision division operation.
1194 mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d)
1197 size_t ql = qvl - qv;
1203 r = (r << MPW_BITS) | rv[i];
1211 /*----- Test rig ----------------------------------------------------------*/
1215 #include <mLib/alloc.h>
1216 #include <mLib/dstr.h>
1217 #include <mLib/quis.h>
1218 #include <mLib/testrig.h>
1222 #define ALLOC(v, vl, sz) do { \
1223 size_t _sz = (sz); \
1224 mpw *_vv = xmalloc(MPWS(_sz)); \
1225 mpw *_vvl = _vv + _sz; \
1230 #define LOAD(v, vl, d) do { \
1231 const dstr *_d = (d); \
1233 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1234 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1239 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1241 static void dumpbits(const char *msg, const void *pp, size_t sz)
1243 const octet *p = pp;
1246 fprintf(stderr, " %02x", *p++);
1247 fputc('\n', stderr);
1250 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
1255 fprintf(stderr, " %08lx", (unsigned long)*--vl);
1256 fputc('\n', stderr);
1259 static int chkscan(const mpw *v, const mpw *vl,
1260 const void *pp, size_t sz, int step)
1263 const octet *p = pp;
1267 mpscan_initx(&mps, v, vl);
1272 for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) {
1273 if (MPSCAN_BIT(&mps) != (x & 1)) {
1275 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1276 step, bit, x & 1, MPSCAN_BIT(&mps));
1288 static int loadstore(dstr *v)
1291 size_t sz = MPW_RQ(v->len) * 2, diff;
1295 dstr_ensure(&d, v->len);
1296 m = xmalloc(MPWS(sz));
1298 for (diff = 0; diff < sz; diff += 5) {
1303 mpx_loadl(m, ml, v->buf, v->len);
1304 if (!chkscan(m, ml, v->buf, v->len, +1))
1306 MPX_OCTETS(oct, m, ml);
1307 mpx_storel(m, ml, d.buf, d.sz);
1308 if (memcmp(d.buf, v->buf, oct) != 0) {
1309 dumpbits("\n*** storel failed", d.buf, d.sz);
1313 mpx_loadb(m, ml, v->buf, v->len);
1314 if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1))
1316 MPX_OCTETS(oct, m, ml);
1317 mpx_storeb(m, ml, d.buf, d.sz);
1318 if (memcmp(d.buf + d.sz - oct, v->buf + v->len - oct, oct) != 0) {
1319 dumpbits("\n*** storeb failed", d.buf, d.sz);
1325 dumpbits("input data", v->buf, v->len);
1332 static int twocl(dstr *v)
1339 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1340 dstr_ensure(&d, sz);
1343 m = xmalloc(MPWS(sz));
1346 mpx_loadl(m, ml, v[0].buf, v[0].len);
1347 mpx_storel2cn(m, ml, d.buf, v[1].len);
1348 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1349 dumpbits("\n*** storel2cn failed", d.buf, v[1].len);
1353 mpx_loadl2cn(m, ml, v[1].buf, v[1].len);
1354 mpx_storel(m, ml, d.buf, v[0].len);
1355 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1356 dumpbits("\n*** loadl2cn failed", d.buf, v[0].len);
1361 dumpbits("pos", v[0].buf, v[0].len);
1362 dumpbits("neg", v[1].buf, v[1].len);
1371 static int twocb(dstr *v)
1378 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1379 dstr_ensure(&d, sz);
1382 m = xmalloc(MPWS(sz));
1385 mpx_loadb(m, ml, v[0].buf, v[0].len);
1386 mpx_storeb2cn(m, ml, d.buf, v[1].len);
1387 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1388 dumpbits("\n*** storeb2cn failed", d.buf, v[1].len);
1392 mpx_loadb2cn(m, ml, v[1].buf, v[1].len);
1393 mpx_storeb(m, ml, d.buf, v[0].len);
1394 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1395 dumpbits("\n*** loadb2cn failed", d.buf, v[0].len);
1400 dumpbits("pos", v[0].buf, v[0].len);
1401 dumpbits("neg", v[1].buf, v[1].len);
1410 static int lsl(dstr *v)
1413 int n = *(int *)v[1].buf;
1420 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1422 mpx_lsl(d, dl, a, al, n);
1423 if (!mpx_ueq(d, dl, c, cl)) {
1424 fprintf(stderr, "\n*** lsl(%i) failed\n", n);
1425 dumpmp(" a", a, al);
1426 dumpmp("expected", c, cl);
1427 dumpmp(" result", d, dl);
1431 free(a); free(c); free(d);
1435 static int lsr(dstr *v)
1438 int n = *(int *)v[1].buf;
1445 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1);
1447 mpx_lsr(d, dl, a, al, n);
1448 if (!mpx_ueq(d, dl, c, cl)) {
1449 fprintf(stderr, "\n*** lsr(%i) failed\n", n);
1450 dumpmp(" a", a, al);
1451 dumpmp("expected", c, cl);
1452 dumpmp(" result", d, dl);
1456 free(a); free(c); free(d);
1460 static int uadd(dstr *v)
1471 ALLOC(d, dl, MAX(al - a, bl - b) + 1);
1473 mpx_uadd(d, dl, a, al, b, bl);
1474 if (!mpx_ueq(d, dl, c, cl)) {
1475 fprintf(stderr, "\n*** uadd failed\n");
1476 dumpmp(" a", a, al);
1477 dumpmp(" b", b, bl);
1478 dumpmp("expected", c, cl);
1479 dumpmp(" result", d, dl);
1483 free(a); free(b); free(c); free(d);
1487 static int usub(dstr *v)
1498 ALLOC(d, dl, al - a);
1500 mpx_usub(d, dl, a, al, b, bl);
1501 if (!mpx_ueq(d, dl, c, cl)) {
1502 fprintf(stderr, "\n*** usub failed\n");
1503 dumpmp(" a", a, al);
1504 dumpmp(" b", b, bl);
1505 dumpmp("expected", c, cl);
1506 dumpmp(" result", d, dl);
1510 free(a); free(b); free(c); free(d);
1514 static int umul(dstr *v)
1525 ALLOC(d, dl, (al - a) + (bl - b));
1527 mpx_umul(d, dl, a, al, b, bl);
1528 if (!mpx_ueq(d, dl, c, cl)) {
1529 fprintf(stderr, "\n*** umul failed\n");
1530 dumpmp(" a", a, al);
1531 dumpmp(" b", b, bl);
1532 dumpmp("expected", c, cl);
1533 dumpmp(" result", d, dl);
1537 free(a); free(b); free(c); free(d);
1541 static int usqr(dstr *v)
1550 ALLOC(d, dl, 2 * (al - a));
1552 mpx_usqr(d, dl, a, al);
1553 if (!mpx_ueq(d, dl, c, cl)) {
1554 fprintf(stderr, "\n*** usqr failed\n");
1555 dumpmp(" a", a, al);
1556 dumpmp("expected", c, cl);
1557 dumpmp(" result", d, dl);
1561 free(a); free(c); free(d);
1565 static int udiv(dstr *v)
1575 ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len);
1579 ALLOC(qq, qql, al - a);
1580 ALLOC(s, sl, (bl - b) + 1);
1582 mpx_udiv(qq, qql, a, al, b, bl, s, sl);
1583 if (!mpx_ueq(qq, qql, q, ql) ||
1584 !mpx_ueq(a, al, r, rl)) {
1585 fprintf(stderr, "\n*** udiv failed\n");
1586 dumpmp(" divisor", b, bl);
1587 dumpmp("expect r", r, rl);
1588 dumpmp("result r", a, al);
1589 dumpmp("expect q", q, ql);
1590 dumpmp("result q", qq, qql);
1594 free(a); free(b); free(r); free(q); free(s); free(qq);
1598 static test_chunk defs[] = {
1599 { "load-store", loadstore, { &type_hex, 0 } },
1600 { "2cl", twocl, { &type_hex, &type_hex, } },
1601 { "2cb", twocb, { &type_hex, &type_hex, } },
1602 { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } },
1603 { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } },
1604 { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } },
1605 { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } },
1606 { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
1607 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
1608 { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } },
1612 int main(int argc, char *argv[])
1614 test_run(argc, argv, defs, SRCDIR"/tests/mpx");
1620 /*----- That's all, folks -------------------------------------------------*/