3 * $Id: mpx.c,v 1.11 2001/04/03 19:36:05 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.11 2001/04/03 19:36:05 mdw
34 * Add some simple bitwise operations so that Perl can use them.
36 * Revision 1.10 2000/10/08 12:06:12 mdw
37 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
39 * Revision 1.9 2000/06/26 07:52:50 mdw
40 * Portability fix for the bug fix.
42 * Revision 1.8 2000/06/25 12:59:02 mdw
43 * (mpx_udiv): Fix bug in quotient digit estimation.
45 * Revision 1.7 1999/12/22 15:49:07 mdw
46 * New function for division by a small integer.
48 * Revision 1.6 1999/11/20 22:43:44 mdw
49 * Integrate testing for MPX routines.
51 * Revision 1.5 1999/11/20 22:23:27 mdw
52 * Add function versions of some low-level macros with wider use.
54 * Revision 1.4 1999/11/17 18:04:09 mdw
55 * Add two's-complement functionality. Improve mpx_udiv a little by
56 * performing the multiplication of the divisor by q with the subtraction
59 * Revision 1.3 1999/11/13 01:57:31 mdw
60 * Remove stray debugging code.
62 * Revision 1.2 1999/11/13 01:50:59 mdw
63 * Multiprecision routines finished and tested.
65 * Revision 1.1 1999/09/03 08:41:12 mdw
70 /*----- Header files ------------------------------------------------------*/
77 #include <mLib/bits.h>
82 /*----- Loading and storing -----------------------------------------------*/
84 /* --- @mpx_storel@ --- *
86 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
87 * @void *pp@ = pointer to octet array
88 * @size_t sz@ = size of octet array
92 * Use: Stores an MP in an octet array, least significant octet
93 * first. High-end octets are silently discarded if there
94 * isn't enough space for them.
97 void mpx_storel(const mpw *v, const mpw *vl, void *pp, size_t sz)
100 octet *p = pp, *q = p + sz;
110 *p++ = U8(w | n << bits);
112 bits += MPW_BITS - 8;
122 /* --- @mpx_loadl@ --- *
124 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
125 * @const void *pp@ = pointer to octet array
126 * @size_t sz@ = size of octet array
130 * Use: Loads an MP in an octet array, least significant octet
131 * first. High-end octets are ignored if there isn't enough
135 void mpx_loadl(mpw *v, mpw *vl, const void *pp, size_t sz)
139 const octet *p = pp, *q = p + sz;
148 if (bits >= MPW_BITS) {
150 w = n >> (MPW_BITS - bits + 8);
160 /* --- @mpx_storeb@ --- *
162 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
163 * @void *pp@ = pointer to octet array
164 * @size_t sz@ = size of octet array
168 * Use: Stores an MP in an octet array, most significant octet
169 * first. High-end octets are silently discarded if there
170 * isn't enough space for them.
173 void mpx_storeb(const mpw *v, const mpw *vl, void *pp, size_t sz)
176 octet *p = pp, *q = p + sz;
186 *--q = U8(w | n << bits);
188 bits += MPW_BITS - 8;
198 /* --- @mpx_loadb@ --- *
200 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
201 * @const void *pp@ = pointer to octet array
202 * @size_t sz@ = size of octet array
206 * Use: Loads an MP in an octet array, most significant octet
207 * first. High-end octets are ignored if there isn't enough
211 void mpx_loadb(mpw *v, mpw *vl, const void *pp, size_t sz)
215 const octet *p = pp, *q = p + sz;
224 if (bits >= MPW_BITS) {
226 w = n >> (MPW_BITS - bits + 8);
236 /*----- Logical shifting --------------------------------------------------*/
238 /* --- @mpx_lsl@ --- *
240 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
241 * @const mpw *av, *avl@ = source vector base and limit
242 * @size_t n@ = number of bit positions to shift by
246 * Use: Performs a logical shift left operation on an integer.
249 void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
254 /* --- Trivial special case --- */
257 MPX_COPY(dv, dvl, av, avl);
259 /* --- Single bit shifting --- */
268 *dv++ = MPW((t << 1) | w);
269 w = t >> (MPW_BITS - 1);
278 /* --- Break out word and bit shifts for more sophisticated work --- */
283 /* --- Handle a shift by a multiple of the word size --- */
286 MPX_COPY(dv + nw, dvl, av, avl);
287 memset(dv, 0, MPWS(nw));
290 /* --- And finally the difficult case --- *
292 * This is a little convoluted, because I have to start from the end and
293 * work backwards to avoid overwriting the source, if they're both the same
299 size_t nr = MPW_BITS - nb;
300 size_t dvn = dvl - dv;
301 size_t avn = avl - av;
308 if (dvn > avn + nw) {
309 size_t off = avn + nw + 1;
310 MPX_ZERO(dv + off, dvl);
320 *--dvl = (t >> nr) | w;
331 /* --- @mpx_lsr@ --- *
333 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
334 * @const mpw *av, *avl@ = source vector base and limit
335 * @size_t n@ = number of bit positions to shift by
339 * Use: Performs a logical shift right operation on an integer.
342 void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
347 /* --- Trivial special case --- */
350 MPX_COPY(dv, dvl, av, avl);
352 /* --- Single bit shifting --- */
361 *dv++ = MPW((t << (MPW_BITS - 1)) | w);
371 /* --- Break out word and bit shifts for more sophisticated work --- */
376 /* --- Handle a shift by a multiple of the word size --- */
379 MPX_COPY(dv, dvl, av + nw, avl);
381 /* --- And finally the difficult case --- */
385 size_t nr = MPW_BITS - nb;
394 *dv++ = MPW((w >> nb) | (t << nr));
398 *dv++ = MPW(w >> nb);
406 /*----- Bitwise operations ------------------------------------------------*/
408 /* --- @mpx_and@, @mpx_or@, @mpx_xor@, @mpx_not@ --- *
410 * Arguments: @mpw *dv, *dvl@ = destination vector
411 * @const mpw *av, *avl@ = first source vector
412 * @const mpw *bv, *bvl@ = second source vector
416 * Use; Does the obvious bitwise operations.
419 #define MPX_BITBINOP(name, op) \
421 void mpx_##name(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
422 const mpw *bv, const mpw *bvl) \
424 MPX_SHRINK(av, avl); \
425 MPX_SHRINK(bv, bvl); \
429 a = (av < avl) ? *av++ : 0; \
430 b = (bv < bvl) ? *bv++ : 0; \
439 void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
445 a = (av < avl) ? *av++ : 0;
450 /*----- Unsigned arithmetic -----------------------------------------------*/
452 /* --- @mpx_2c@ --- *
454 * Arguments: @mpw *dv, *dvl@ = destination vector
455 * @const mpw *v, *vl@ = source vector
459 * Use: Calculates the two's complement of @v@.
462 void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl)
465 while (dv < dvl && v < vl)
466 *dv++ = c = MPW(~*v++);
473 MPX_UADDN(dv, dvl, 1);
476 /* --- @mpx_ueq@ --- *
478 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
479 * @const mpw *bv, *bvl@ = second argument vector base and limit
481 * Returns: Nonzero if the two vectors are equal.
483 * Use: Performs an unsigned integer test for equality.
486 int mpx_ueq(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
490 if (avl - av != bvl - bv)
499 /* --- @mpx_ucmp@ --- *
501 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
502 * @const mpw *bv, *bvl@ = second argument vector base and limit
504 * Returns: Less than, equal to, or greater than zero depending on
505 * whether @a@ is less than, equal to or greater than @b@,
508 * Use: Performs an unsigned integer comparison.
511 int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
516 if (avl - av > bvl - bv)
518 else if (avl - av < bvl - bv)
520 else while (avl > av) {
521 mpw a = *--avl, b = *--bvl;
530 /* --- @mpx_uadd@ --- *
532 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
533 * @const mpw *av, *avl@ = first addend vector base and limit
534 * @const mpw *bv, *bvl@ = second addend vector base and limit
538 * Use: Performs unsigned integer addition. If the result overflows
539 * the destination vector, high-order bits are discarded. This
540 * means that two's complement addition happens more or less for
541 * free, although that's more a side-effect than anything else.
542 * The result vector may be equal to either or both source
543 * vectors, but may not otherwise overlap them.
546 void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
547 const mpw *bv, const mpw *bvl)
551 while (av < avl || bv < bvl) {
556 a = (av < avl) ? *av++ : 0;
557 b = (bv < bvl) ? *bv++ : 0;
558 x = (mpd)a + (mpd)b + c;
568 /* --- @mpx_uaddn@ --- *
570 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
571 * @mpw n@ = other addend
575 * Use: Adds a small integer to a multiprecision number.
578 void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); }
580 /* --- @mpx_usub@ --- *
582 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
583 * @const mpw *av, *avl@ = first argument vector base and limit
584 * @const mpw *bv, *bvl@ = second argument vector base and limit
588 * Use: Performs unsigned integer subtraction. If the result
589 * overflows the destination vector, high-order bits are
590 * discarded. This means that two's complement subtraction
591 * happens more or less for free, althuogh that's more a side-
592 * effect than anything else. The result vector may be equal to
593 * either or both source vectors, but may not otherwise overlap
597 void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
598 const mpw *bv, const mpw *bvl)
602 while (av < avl || bv < bvl) {
607 a = (av < avl) ? *av++ : 0;
608 b = (bv < bvl) ? *bv++ : 0;
609 x = (mpd)a - (mpd)b - c;
622 /* --- @mpx_usubn@ --- *
624 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
629 * Use: Subtracts a small integer from a multiprecision number.
632 void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); }
634 /* --- @mpx_umul@ --- *
636 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
637 * @const mpw *av, *avl@ = multiplicand vector base and limit
638 * @const mpw *bv, *bvl@ = multiplier vector base and limit
642 * Use: Performs unsigned integer multiplication. If the result
643 * overflows the desination vector, high-order bits are
644 * discarded. The result vector may not overlap the argument
645 * vectors in any way.
648 void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
649 const mpw *bv, const mpw *bvl)
651 /* --- This is probably worthwhile on a multiply --- */
656 /* --- Deal with a multiply by zero --- */
663 /* --- Do the initial multiply and initialize the accumulator --- */
665 MPX_UMULN(dv, dvl, av, avl, *bv++);
667 /* --- Do the remaining multiply/accumulates --- */
669 while (dv < dvl && bv < bvl) {
679 x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c;
683 MPX_UADDN(dvv, dvl, c);
688 /* --- @mpx_umuln@ --- *
690 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
691 * @const mpw *av, *avl@ = multiplicand vector base and limit
692 * @mpw m@ = multiplier
696 * Use: Multiplies a multiprecision integer by a single-word value.
697 * The destination and source may be equal. The destination
698 * is completely cleared after use.
701 void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
703 MPX_UMULN(dv, dvl, av, avl, m);
706 /* --- @mpx_umlan@ --- *
708 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
709 * @const mpw *av, *avl@ = multiplicand vector base and limit
710 * @mpw m@ = multiplier
714 * Use: Multiplies a multiprecision integer by a single-word value
715 * and adds the result to an accumulator.
718 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
720 MPX_UMLAN(dv, dvl, av, avl, m);
723 /* --- @mpx_usqr@ --- *
725 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
726 * @const mpw *av, *av@ = source vector base and limit
730 * Use: Performs unsigned integer squaring. The result vector must
731 * not overlap the source vector in any way.
734 void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
738 /* --- Main loop --- */
746 /* --- Stop if I've run out of destination --- */
751 /* --- Work out the square at this point in the proceedings --- */
754 mpd x = (mpd)a * (mpd)a + *dvv;
756 c = MPW(x >> MPW_BITS);
759 /* --- Now fix up the rest of the vector upwards --- */
762 while (dvv < dvl && avv < avl) {
763 mpd x = (mpd)a * (mpd)*avv++;
764 mpd y = ((x << 1) & MPW_MAX) + c + *dvv;
765 c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS);
768 while (dvv < dvl && c) {
774 /* --- Get ready for the next round --- */
781 /* --- @mpx_udiv@ --- *
783 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
784 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
785 * @const mpw *dv, *dvl@ = divisor vector base and limit
786 * @mpw *sv, *svl@ = scratch workspace
790 * Use: Performs unsigned integer division. If the result overflows
791 * the quotient vector, high-order bits are discarded. (Clearly
792 * the remainder vector can't overflow.) The various vectors
793 * may not overlap in any way. Yes, I know it's a bit odd
794 * requiring the dividend to be in the result position but it
795 * does make some sense really. The remainder must have
796 * headroom for at least two extra words. The scratch space
797 * must be at least one word larger than the divisor.
800 void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
801 const mpw *dv, const mpw *dvl,
808 /* --- Initialize the quotient --- */
812 /* --- Perform some sanity checks --- */
815 assert(((void)"division by zero in mpx_udiv", dv < dvl));
817 /* --- Normalize the divisor --- *
819 * The algorithm requires that the divisor be at least two digits long.
820 * This is easy to fix.
827 for (b = MPW_BITS / 2; b; b >>= 1) {
828 if (d < (MPW_MAX >> b)) {
837 /* --- Normalize the dividend/remainder to match --- */
840 mpx_lsl(rv, rvl, rv, rvl, norm);
841 mpx_lsl(sv, svl, dv, dvl, norm);
851 /* --- Work out the relative scales --- */
854 size_t rvn = rvl - rv;
855 size_t dvn = dvl - dv;
857 /* --- If the divisor is clearly larger, notice this --- */
860 mpx_lsr(rv, rvl, rv, rvl, norm);
867 /* --- Calculate the most significant quotient digit --- *
869 * Because the divisor has its top bit set, this can only happen once. The
870 * pointer arithmetic is a little contorted, to make sure that the
871 * behaviour is defined.
874 if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) {
875 mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl);
876 if (qvl - qv > scale)
880 /* --- Now for the main loop --- */
889 /* --- Get an estimate for the next quotient digit --- */
896 rh = ((mpd)r << MPW_BITS) | rr;
902 /* --- Refine the estimate --- */
906 mpd yy = (mpd)dd * q;
910 yh += yy >> MPW_BITS;
913 while (yh > rh || (yh == rh && yl > rrr)) {
922 /* --- Remove a chunk from the dividend --- */
929 /* --- Calculate the size of the chunk --- *
931 * This does the whole job of calculating @r >> scale - qd@.
934 for (svv = rv + scale, dvv = dv;
935 dvv < dvl && svv < rvl;
937 mpd x = (mpd)*dvv * (mpd)q + mc;
939 x = (mpd)*svv - MPW(x) - sc;
948 mpd x = (mpd)*svv - mc - sc;
958 /* --- Fix if the quotient was too large --- *
960 * This doesn't seem to happen very often.
963 if (rvl[-1] > MPW_MAX / 2) {
964 mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl);
969 /* --- Done for another iteration --- */
971 if (qvl - qv > scale)
978 /* --- Now fiddle with unnormalizing and things --- */
980 mpx_lsr(rv, rvl, rv, rvl, norm);
983 /* --- @mpx_udivn@ --- *
985 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
987 * @const mpw *rv, *rvl@ = dividend
988 * @mpw d@ = single-precision divisor
990 * Returns: Remainder after divison.
992 * Use: Performs a single-precision division operation.
995 mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d)
998 size_t ql = qvl - qv;
1004 r = (r << MPW_BITS) | rv[i];
1012 /*----- Test rig ----------------------------------------------------------*/
1016 #include <mLib/alloc.h>
1017 #include <mLib/dstr.h>
1018 #include <mLib/quis.h>
1019 #include <mLib/testrig.h>
1023 #define ALLOC(v, vl, sz) do { \
1024 size_t _sz = (sz); \
1025 mpw *_vv = xmalloc(MPWS(_sz)); \
1026 mpw *_vvl = _vv + _sz; \
1031 #define LOAD(v, vl, d) do { \
1032 const dstr *_d = (d); \
1034 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1035 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1040 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1042 static void dumpbits(const char *msg, const void *pp, size_t sz)
1044 const octet *p = pp;
1047 fprintf(stderr, " %02x", *p++);
1048 fputc('\n', stderr);
1051 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
1056 fprintf(stderr, " %08lx", (unsigned long)*--vl);
1057 fputc('\n', stderr);
1060 static int chkscan(const mpw *v, const mpw *vl,
1061 const void *pp, size_t sz, int step)
1064 const octet *p = pp;
1068 mpscan_initx(&mps, v, vl);
1073 for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) {
1074 if (MPSCAN_BIT(&mps) != (x & 1)) {
1076 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1077 step, bit, x & 1, MPSCAN_BIT(&mps));
1089 static int loadstore(dstr *v)
1092 size_t sz = MPW_RQ(v->len) * 2, diff;
1096 dstr_ensure(&d, v->len);
1097 m = xmalloc(MPWS(sz));
1099 for (diff = 0; diff < sz; diff += 5) {
1104 mpx_loadl(m, ml, v->buf, v->len);
1105 if (!chkscan(m, ml, v->buf, v->len, +1))
1107 MPX_OCTETS(oct, m, ml);
1108 mpx_storel(m, ml, d.buf, d.sz);
1109 if (memcmp(d.buf, v->buf, oct) != 0) {
1110 dumpbits("\n*** storel failed", d.buf, d.sz);
1114 mpx_loadb(m, ml, v->buf, v->len);
1115 if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1))
1117 MPX_OCTETS(oct, m, ml);
1118 mpx_storeb(m, ml, d.buf, d.sz);
1119 if (memcmp(d.buf + d.sz - oct, v->buf + v->len - oct, oct) != 0) {
1120 dumpbits("\n*** storeb failed", d.buf, d.sz);
1126 dumpbits("input data", v->buf, v->len);
1133 static int lsl(dstr *v)
1136 int n = *(int *)v[1].buf;
1143 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1145 mpx_lsl(d, dl, a, al, n);
1146 if (!mpx_ueq(d, dl, c, cl)) {
1147 fprintf(stderr, "\n*** lsl(%i) failed\n", n);
1148 dumpmp(" a", a, al);
1149 dumpmp("expected", c, cl);
1150 dumpmp(" result", d, dl);
1154 free(a); free(c); free(d);
1158 static int lsr(dstr *v)
1161 int n = *(int *)v[1].buf;
1168 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1);
1170 mpx_lsr(d, dl, a, al, n);
1171 if (!mpx_ueq(d, dl, c, cl)) {
1172 fprintf(stderr, "\n*** lsr(%i) failed\n", n);
1173 dumpmp(" a", a, al);
1174 dumpmp("expected", c, cl);
1175 dumpmp(" result", d, dl);
1179 free(a); free(c); free(d);
1183 static int uadd(dstr *v)
1194 ALLOC(d, dl, MAX(al - a, bl - b) + 1);
1196 mpx_uadd(d, dl, a, al, b, bl);
1197 if (!mpx_ueq(d, dl, c, cl)) {
1198 fprintf(stderr, "\n*** uadd failed\n");
1199 dumpmp(" a", a, al);
1200 dumpmp(" b", b, bl);
1201 dumpmp("expected", c, cl);
1202 dumpmp(" result", d, dl);
1206 free(a); free(b); free(c); free(d);
1210 static int usub(dstr *v)
1221 ALLOC(d, dl, al - a);
1223 mpx_usub(d, dl, a, al, b, bl);
1224 if (!mpx_ueq(d, dl, c, cl)) {
1225 fprintf(stderr, "\n*** usub failed\n");
1226 dumpmp(" a", a, al);
1227 dumpmp(" b", b, bl);
1228 dumpmp("expected", c, cl);
1229 dumpmp(" result", d, dl);
1233 free(a); free(b); free(c); free(d);
1237 static int umul(dstr *v)
1248 ALLOC(d, dl, (al - a) + (bl - b));
1250 mpx_umul(d, dl, a, al, b, bl);
1251 if (!mpx_ueq(d, dl, c, cl)) {
1252 fprintf(stderr, "\n*** umul failed\n");
1253 dumpmp(" a", a, al);
1254 dumpmp(" b", b, bl);
1255 dumpmp("expected", c, cl);
1256 dumpmp(" result", d, dl);
1260 free(a); free(b); free(c); free(d);
1264 static int usqr(dstr *v)
1273 ALLOC(d, dl, 2 * (al - a));
1275 mpx_usqr(d, dl, a, al);
1276 if (!mpx_ueq(d, dl, c, cl)) {
1277 fprintf(stderr, "\n*** usqr failed\n");
1278 dumpmp(" a", a, al);
1279 dumpmp("expected", c, cl);
1280 dumpmp(" result", d, dl);
1284 free(a); free(c); free(d);
1288 static int udiv(dstr *v)
1298 ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len);
1302 ALLOC(qq, qql, al - a);
1303 ALLOC(s, sl, (bl - b) + 1);
1305 mpx_udiv(qq, qql, a, al, b, bl, s, sl);
1306 if (!mpx_ueq(qq, qql, q, ql) ||
1307 !mpx_ueq(a, al, r, rl)) {
1308 fprintf(stderr, "\n*** udiv failed\n");
1309 dumpmp(" divisor", b, bl);
1310 dumpmp("expect r", r, rl);
1311 dumpmp("result r", a, al);
1312 dumpmp("expect q", q, ql);
1313 dumpmp("result q", qq, qql);
1317 free(a); free(b); free(r); free(q); free(s); free(qq);
1321 static test_chunk defs[] = {
1322 { "load-store", loadstore, { &type_hex, 0 } },
1323 { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } },
1324 { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } },
1325 { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } },
1326 { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } },
1327 { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
1328 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
1329 { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } },
1333 int main(int argc, char *argv[])
1335 test_run(argc, argv, defs, SRCDIR"/tests/mpx");
1341 /*----- That's all, folks -------------------------------------------------*/