3 * Low-level multiprecision arithmetic
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
41 /*----- Loading and storing -----------------------------------------------*/
43 /* --- @mpx_storel@ --- *
45 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
46 * @void *pp@ = pointer to octet array
47 * @size_t sz@ = size of octet array
51 * Use: Stores an MP in an octet array, least significant octet
52 * first. High-end octets are silently discarded if there
53 * isn't enough space for them.
56 void mpx_storel(const mpw *v, const mpw *vl, void *pp, size_t sz)
59 octet *p = pp, *q = p + sz;
69 *p++ = U8(w | n << bits);
81 /* --- @mpx_loadl@ --- *
83 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
84 * @const void *pp@ = pointer to octet array
85 * @size_t sz@ = size of octet array
89 * Use: Loads an MP in an octet array, least significant octet
90 * first. High-end octets are ignored if there isn't enough
94 void mpx_loadl(mpw *v, mpw *vl, const void *pp, size_t sz)
98 const octet *p = pp, *q = p + sz;
107 if (bits >= MPW_BITS) {
109 w = n >> (MPW_BITS - bits + 8);
119 /* --- @mpx_storeb@ --- *
121 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
122 * @void *pp@ = pointer to octet array
123 * @size_t sz@ = size of octet array
127 * Use: Stores an MP in an octet array, most significant octet
128 * first. High-end octets are silently discarded if there
129 * isn't enough space for them.
132 void mpx_storeb(const mpw *v, const mpw *vl, void *pp, size_t sz)
135 octet *p = pp, *q = p + sz;
145 *--q = U8(w | n << bits);
147 bits += MPW_BITS - 8;
157 /* --- @mpx_loadb@ --- *
159 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
160 * @const void *pp@ = pointer to octet array
161 * @size_t sz@ = size of octet array
165 * Use: Loads an MP in an octet array, most significant octet
166 * first. High-end octets are ignored if there isn't enough
170 void mpx_loadb(mpw *v, mpw *vl, const void *pp, size_t sz)
174 const octet *p = pp, *q = p + sz;
183 if (bits >= MPW_BITS) {
185 w = n >> (MPW_BITS - bits + 8);
195 /* --- @mpx_storel2cn@ --- *
197 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
198 * @void *pp@ = pointer to octet array
199 * @size_t sz@ = size of octet array
203 * Use: Stores a negative MP in an octet array, least significant
204 * octet first, as two's complement. High-end octets are
205 * silently discarded if there isn't enough space for them.
206 * This obviously makes the output bad.
209 void mpx_storel2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
214 octet *p = pp, *q = p + sz;
226 bits += MPW_BITS - 8;
244 /* --- @mpx_loadl2cn@ --- *
246 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
247 * @const void *pp@ = pointer to octet array
248 * @size_t sz@ = size of octet array
252 * Use: Loads a negative MP in an octet array, least significant
253 * octet first, as two's complement. High-end octets are
254 * ignored if there isn't enough space for them. This probably
255 * means you made the wrong choice coming here.
258 void mpx_loadl2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
263 const octet *p = pp, *q = p + sz;
273 if (bits >= MPW_BITS) {
275 w = n >> (MPW_BITS - bits + 8);
285 /* --- @mpx_storeb2cn@ --- *
287 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
288 * @void *pp@ = pointer to octet array
289 * @size_t sz@ = size of octet array
293 * Use: Stores a negative MP in an octet array, most significant
294 * octet first, as two's complement. High-end octets are
295 * silently discarded if there isn't enough space for them,
296 * which probably isn't what you meant.
299 void mpx_storeb2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
304 octet *p = pp, *q = p + sz;
316 bits += MPW_BITS - 8;
328 c = c && !(b & 0xff);
334 /* --- @mpx_loadb2cn@ --- *
336 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
337 * @const void *pp@ = pointer to octet array
338 * @size_t sz@ = size of octet array
342 * Use: Loads a negative MP in an octet array, most significant octet
343 * first as two's complement. High-end octets are ignored if
344 * there isn't enough space for them. This probably means you
345 * chose this function wrongly.
348 void mpx_loadb2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
353 const octet *p = pp, *q = p + sz;
363 if (bits >= MPW_BITS) {
365 w = n >> (MPW_BITS - bits + 8);
375 /*----- Logical shifting --------------------------------------------------*/
377 /* --- @mpx_lsl@ --- *
379 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
380 * @const mpw *av, *avl@ = source vector base and limit
381 * @size_t n@ = number of bit positions to shift by
385 * Use: Performs a logical shift left operation on an integer.
388 void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
393 /* --- Trivial special case --- */
396 MPX_COPY(dv, dvl, av, avl);
398 /* --- Single bit shifting --- */
407 *dv++ = MPW((t << 1) | w);
408 w = t >> (MPW_BITS - 1);
417 /* --- Break out word and bit shifts for more sophisticated work --- */
422 /* --- Handle a shift by a multiple of the word size --- */
428 MPX_COPY(dv + nw, dvl, av, avl);
429 memset(dv, 0, MPWS(nw));
433 /* --- And finally the difficult case --- *
435 * This is a little convoluted, because I have to start from the end and
436 * work backwards to avoid overwriting the source, if they're both the same
442 size_t nr = MPW_BITS - nb;
443 size_t dvn = dvl - dv;
444 size_t avn = avl - av;
451 if (dvn > avn + nw) {
452 size_t off = avn + nw + 1;
453 MPX_ZERO(dv + off, dvl);
463 *--dvl = MPW((t >> nr) | w);
474 /* --- @mpx_lslc@ --- *
476 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
477 * @const mpw *av, *avl@ = source vector base and limit
478 * @size_t n@ = number of bit positions to shift by
482 * Use: Performs a logical shift left operation on an integer, only
483 * it fills in the bits with ones instead of zeroes.
486 void mpx_lslc(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
491 /* --- Trivial special case --- */
494 MPX_COPY(dv, dvl, av, avl);
496 /* --- Single bit shifting --- */
505 *dv++ = MPW((t << 1) | w);
506 w = t >> (MPW_BITS - 1);
515 /* --- Break out word and bit shifts for more sophisticated work --- */
520 /* --- Handle a shift by a multiple of the word size --- */
526 MPX_COPY(dv + nw, dvl, av, avl);
527 MPX_ONE(dv, dv + nw);
531 /* --- And finally the difficult case --- *
533 * This is a little convoluted, because I have to start from the end and
534 * work backwards to avoid overwriting the source, if they're both the same
540 size_t nr = MPW_BITS - nb;
541 size_t dvn = dvl - dv;
542 size_t avn = avl - av;
549 if (dvn > avn + nw) {
550 size_t off = avn + nw + 1;
551 MPX_ZERO(dv + off, dvl);
561 *--dvl = MPW((t >> nr) | w);
565 *--dvl = MPW((MPW_MAX >> nr) | w);
572 /* --- @mpx_lsr@ --- *
574 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
575 * @const mpw *av, *avl@ = source vector base and limit
576 * @size_t n@ = number of bit positions to shift by
580 * Use: Performs a logical shift right operation on an integer.
583 void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
588 /* --- Trivial special case --- */
591 MPX_COPY(dv, dvl, av, avl);
593 /* --- Single bit shifting --- */
596 mpw w = av < avl ? *av++ >> 1 : 0;
602 *dv++ = MPW((t << (MPW_BITS - 1)) | w);
612 /* --- Break out word and bit shifts for more sophisticated work --- */
617 /* --- Handle a shift by a multiple of the word size --- */
623 MPX_COPY(dv, dvl, av + nw, avl);
626 /* --- And finally the difficult case --- */
630 size_t nr = MPW_BITS - nb;
633 w = av < avl ? *av++ : 0;
639 *dv++ = MPW((w >> nb) | (t << nr));
643 *dv++ = MPW(w >> nb);
651 /*----- Bitwise operations ------------------------------------------------*/
653 /* --- @mpx_bitop@ --- *
655 * Arguments: @mpw *dv, *dvl@ = destination vector
656 * @const mpw *av, *avl@ = first source vector
657 * @const mpw *bv, *bvl@ = second source vector
661 * Use; Provides the dyadic boolean functions.
664 #define MPX_BITBINOP(string) \
666 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
667 const mpw *bv, const mpw *bvl) \
669 MPX_SHRINK(av, avl); \
670 MPX_SHRINK(bv, bvl); \
674 a = (av < avl) ? *av++ : 0; \
675 b = (bv < bvl) ? *bv++ : 0; \
676 *dv++ = B##string(a, b); \
680 MPX_DOBIN(MPX_BITBINOP)
682 void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
688 a = (av < avl) ? *av++ : 0;
693 /*----- Unsigned arithmetic -----------------------------------------------*/
695 /* --- @mpx_2c@ --- *
697 * Arguments: @mpw *dv, *dvl@ = destination vector
698 * @const mpw *v, *vl@ = source vector
702 * Use: Calculates the two's complement of @v@.
705 void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl)
708 while (dv < dvl && v < vl)
709 *dv++ = c = MPW(~*v++);
716 MPX_UADDN(dv, dvl, 1);
719 /* --- @mpx_ueq@ --- *
721 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
722 * @const mpw *bv, *bvl@ = second argument vector base and limit
724 * Returns: Nonzero if the two vectors are equal.
726 * Use: Performs an unsigned integer test for equality.
729 int mpx_ueq(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
733 if (avl - av != bvl - bv)
742 /* --- @mpx_ucmp@ --- *
744 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
745 * @const mpw *bv, *bvl@ = second argument vector base and limit
747 * Returns: Less than, equal to, or greater than zero depending on
748 * whether @a@ is less than, equal to or greater than @b@,
751 * Use: Performs an unsigned integer comparison.
754 int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
759 if (avl - av > bvl - bv)
761 else if (avl - av < bvl - bv)
763 else while (avl > av) {
764 mpw a = *--avl, b = *--bvl;
773 /* --- @mpx_uadd@ --- *
775 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
776 * @const mpw *av, *avl@ = first addend vector base and limit
777 * @const mpw *bv, *bvl@ = second addend vector base and limit
781 * Use: Performs unsigned integer addition. If the result overflows
782 * the destination vector, high-order bits are discarded. This
783 * means that two's complement addition happens more or less for
784 * free, although that's more a side-effect than anything else.
785 * The result vector may be equal to either or both source
786 * vectors, but may not otherwise overlap them.
789 void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
790 const mpw *bv, const mpw *bvl)
794 while (av < avl || bv < bvl) {
799 a = (av < avl) ? *av++ : 0;
800 b = (bv < bvl) ? *bv++ : 0;
801 x = (mpd)a + (mpd)b + c;
811 /* --- @mpx_uaddn@ --- *
813 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
814 * @mpw n@ = other addend
818 * Use: Adds a small integer to a multiprecision number.
821 void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); }
823 /* --- @mpx_uaddnlsl@ --- *
825 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
826 * @mpw a@ = second argument
827 * @unsigned o@ = offset in bits
831 * Use: Computes %$d + 2^o a$%. If the result overflows then
832 * high-order bits are discarded, as usual. We must have
833 * @0 < o < MPW_BITS@.
836 void mpx_uaddnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
840 while (x && dv < dvl) {
847 /* --- @mpx_usub@ --- *
849 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
850 * @const mpw *av, *avl@ = first argument vector base and limit
851 * @const mpw *bv, *bvl@ = second argument vector base and limit
855 * Use: Performs unsigned integer subtraction. If the result
856 * overflows the destination vector, high-order bits are
857 * discarded. This means that two's complement subtraction
858 * happens more or less for free, althuogh that's more a side-
859 * effect than anything else. The result vector may be equal to
860 * either or both source vectors, but may not otherwise overlap
864 void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
865 const mpw *bv, const mpw *bvl)
869 while (av < avl || bv < bvl) {
874 a = (av < avl) ? *av++ : 0;
875 b = (bv < bvl) ? *bv++ : 0;
876 x = (mpd)a - (mpd)b - c;
889 /* --- @mpx_usubn@ --- *
891 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
896 * Use: Subtracts a small integer from a multiprecision number.
899 void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); }
901 /* --- @mpx_uaddnlsl@ --- *
903 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
904 * @mpw a@ = second argument
905 * @unsigned o@ = offset in bits
909 * Use: Computes %$d + 2^o a$%. If the result overflows then
910 * high-order bits are discarded, as usual. We must have
911 * @0 < o < MPW_BITS@.
914 void mpx_usubnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
916 mpw b = a >> (MPW_BITS - o);
920 mpd x = (mpd)*dv - MPW(a);
924 MPX_USUBN(dv, dvl, b);
928 /* --- @mpx_umul@ --- *
930 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
931 * @const mpw *av, *avl@ = multiplicand vector base and limit
932 * @const mpw *bv, *bvl@ = multiplier vector base and limit
936 * Use: Performs unsigned integer multiplication. If the result
937 * overflows the desination vector, high-order bits are
938 * discarded. The result vector may not overlap the argument
939 * vectors in any way.
942 void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
943 const mpw *bv, const mpw *bvl)
945 /* --- This is probably worthwhile on a multiply --- */
950 /* --- Deal with a multiply by zero --- */
957 /* --- Do the initial multiply and initialize the accumulator --- */
959 MPX_UMULN(dv, dvl, av, avl, *bv++);
961 /* --- Do the remaining multiply/accumulates --- */
963 while (dv < dvl && bv < bvl) {
973 x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c;
977 MPX_UADDN(dvv, dvl, c);
982 /* --- @mpx_umuln@ --- *
984 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
985 * @const mpw *av, *avl@ = multiplicand vector base and limit
986 * @mpw m@ = multiplier
990 * Use: Multiplies a multiprecision integer by a single-word value.
991 * The destination and source may be equal. The destination
992 * is completely cleared after use.
995 void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
996 { MPX_UMULN(dv, dvl, av, avl, m); }
998 /* --- @mpx_umlan@ --- *
1000 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
1001 * @const mpw *av, *avl@ = multiplicand vector base and limit
1002 * @mpw m@ = multiplier
1006 * Use: Multiplies a multiprecision integer by a single-word value
1007 * and adds the result to an accumulator.
1010 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
1011 { MPX_UMLAN(dv, dvl, av, avl, m); }
1013 /* --- @mpx_usqr@ --- *
1015 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1016 * @const mpw *av, *av@ = source vector base and limit
1020 * Use: Performs unsigned integer squaring. The result vector must
1021 * not overlap the source vector in any way.
1024 void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
1028 /* --- Main loop --- */
1031 const mpw *avv = av;
1036 /* --- Stop if I've run out of destination --- */
1041 /* --- Work out the square at this point in the proceedings --- */
1044 mpd x = (mpd)a * (mpd)a + *dvv;
1046 c = MPW(x >> MPW_BITS);
1049 /* --- Now fix up the rest of the vector upwards --- */
1052 while (dvv < dvl && avv < avl) {
1053 mpd x = (mpd)a * (mpd)*avv++;
1054 mpd y = ((x << 1) & MPW_MAX) + c + *dvv;
1055 c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS);
1058 while (dvv < dvl && c) {
1064 /* --- Get ready for the next round --- */
1071 /* --- @mpx_udiv@ --- *
1073 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1074 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1075 * @const mpw *dv, *dvl@ = divisor vector base and limit
1076 * @mpw *sv, *svl@ = scratch workspace
1080 * Use: Performs unsigned integer division. If the result overflows
1081 * the quotient vector, high-order bits are discarded. (Clearly
1082 * the remainder vector can't overflow.) The various vectors
1083 * may not overlap in any way. Yes, I know it's a bit odd
1084 * requiring the dividend to be in the result position but it
1085 * does make some sense really. The remainder must have
1086 * headroom for at least two extra words. The scratch space
1087 * must be at least one word larger than the divisor.
1090 void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
1091 const mpw *dv, const mpw *dvl,
1098 /* --- Initialize the quotient --- */
1102 /* --- Perform some sanity checks --- */
1104 MPX_SHRINK(dv, dvl);
1105 assert(((void)"division by zero in mpx_udiv", dv < dvl));
1107 /* --- Normalize the divisor --- *
1109 * The algorithm requires that the divisor be at least two digits long.
1110 * This is easy to fix.
1117 for (b = MPW_P2; b; b >>= 1) {
1118 if (d <= (MPW_MAX >> b)) {
1127 /* --- Normalize the dividend/remainder to match --- */
1130 mpx_lsl(rv, rvl, rv, rvl, norm);
1131 mpx_lsl(sv, svl, dv, dvl, norm);
1134 MPX_SHRINK(dv, dvl);
1137 MPX_SHRINK(rv, rvl);
1141 /* --- Work out the relative scales --- */
1144 size_t rvn = rvl - rv;
1145 size_t dvn = dvl - dv;
1147 /* --- If the divisor is clearly larger, notice this --- */
1150 mpx_lsr(rv, rvl, rv, rvl, norm);
1157 /* --- Calculate the most significant quotient digit --- *
1159 * Because the divisor has its top bit set, this can only happen once. The
1160 * pointer arithmetic is a little contorted, to make sure that the
1161 * behaviour is defined.
1164 if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) {
1165 mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1166 if (qvl - qv > scale)
1170 /* --- Now for the main loop --- */
1179 /* --- Get an estimate for the next quotient digit --- */
1186 rh = ((mpd)r << MPW_BITS) | rr;
1192 /* --- Refine the estimate --- */
1195 mpd yh = (mpd)d * q;
1196 mpd yy = (mpd)dd * q;
1200 yh += yy >> MPW_BITS;
1203 while (yh > rh || (yh == rh && yl > rrr)) {
1212 /* --- Remove a chunk from the dividend --- */
1219 /* --- Calculate the size of the chunk --- *
1221 * This does the whole job of calculating @r >> scale - qd@.
1224 for (svv = rv + scale, dvv = dv;
1225 dvv < dvl && svv < rvl;
1227 mpd x = (mpd)*dvv * (mpd)q + mc;
1229 x = (mpd)*svv - MPW(x) - sc;
1238 mpd x = (mpd)*svv - mc - sc;
1248 /* --- Fix if the quotient was too large --- *
1250 * This doesn't seem to happen very often.
1253 if (rvl[-1] > MPW_MAX / 2) {
1254 mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1259 /* --- Done for another iteration --- */
1261 if (qvl - qv > scale)
1268 /* --- Now fiddle with unnormalizing and things --- */
1270 mpx_lsr(rv, rvl, rv, rvl, norm);
1273 /* --- @mpx_udivn@ --- *
1275 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1277 * @const mpw *rv, *rvl@ = dividend
1278 * @mpw d@ = single-precision divisor
1280 * Returns: Remainder after divison.
1282 * Use: Performs a single-precision division operation.
1285 mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d)
1288 size_t ql = qvl - qv;
1294 r = (r << MPW_BITS) | rv[i];
1302 /*----- Test rig ----------------------------------------------------------*/
1306 #include <mLib/alloc.h>
1307 #include <mLib/dstr.h>
1308 #include <mLib/quis.h>
1309 #include <mLib/testrig.h>
1313 #define ALLOC(v, vl, sz) do { \
1314 size_t _sz = (sz); \
1315 mpw *_vv = xmalloc(MPWS(_sz)); \
1316 mpw *_vvl = _vv + _sz; \
1321 #define LOAD(v, vl, d) do { \
1322 const dstr *_d = (d); \
1324 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1325 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1330 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1332 static void dumpbits(const char *msg, const void *pp, size_t sz)
1334 const octet *p = pp;
1337 fprintf(stderr, " %02x", *p++);
1338 fputc('\n', stderr);
1341 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
1346 fprintf(stderr, " %08lx", (unsigned long)*--vl);
1347 fputc('\n', stderr);
1350 static int chkscan(const mpw *v, const mpw *vl,
1351 const void *pp, size_t sz, int step)
1354 const octet *p = pp;
1358 mpscan_initx(&mps, v, vl);
1363 for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) {
1364 if (MPSCAN_BIT(&mps) != (x & 1)) {
1366 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1367 step, bit, x & 1, MPSCAN_BIT(&mps));
1379 static int loadstore(dstr *v)
1382 size_t sz = MPW_RQ(v->len) * 2, diff;
1386 dstr_ensure(&d, v->len);
1387 m = xmalloc(MPWS(sz));
1389 for (diff = 0; diff < sz; diff += 5) {
1394 mpx_loadl(m, ml, v->buf, v->len);
1395 if (!chkscan(m, ml, v->buf, v->len, +1))
1397 MPX_OCTETS(oct, m, ml);
1398 mpx_storel(m, ml, d.buf, d.sz);
1399 if (memcmp(d.buf, v->buf, oct) != 0) {
1400 dumpbits("\n*** storel failed", d.buf, d.sz);
1404 mpx_loadb(m, ml, v->buf, v->len);
1405 if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1))
1407 MPX_OCTETS(oct, m, ml);
1408 mpx_storeb(m, ml, d.buf, d.sz);
1409 if (memcmp(d.buf + d.sz - oct, v->buf + v->len - oct, oct) != 0) {
1410 dumpbits("\n*** storeb failed", d.buf, d.sz);
1416 dumpbits("input data", v->buf, v->len);
1423 static int twocl(dstr *v)
1430 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1431 dstr_ensure(&d, sz);
1434 m = xmalloc(MPWS(sz));
1437 mpx_loadl(m, ml, v[0].buf, v[0].len);
1438 mpx_storel2cn(m, ml, d.buf, v[1].len);
1439 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1440 dumpbits("\n*** storel2cn failed", d.buf, v[1].len);
1444 mpx_loadl2cn(m, ml, v[1].buf, v[1].len);
1445 mpx_storel(m, ml, d.buf, v[0].len);
1446 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1447 dumpbits("\n*** loadl2cn failed", d.buf, v[0].len);
1452 dumpbits("pos", v[0].buf, v[0].len);
1453 dumpbits("neg", v[1].buf, v[1].len);
1462 static int twocb(dstr *v)
1469 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1470 dstr_ensure(&d, sz);
1473 m = xmalloc(MPWS(sz));
1476 mpx_loadb(m, ml, v[0].buf, v[0].len);
1477 mpx_storeb2cn(m, ml, d.buf, v[1].len);
1478 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1479 dumpbits("\n*** storeb2cn failed", d.buf, v[1].len);
1483 mpx_loadb2cn(m, ml, v[1].buf, v[1].len);
1484 mpx_storeb(m, ml, d.buf, v[0].len);
1485 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1486 dumpbits("\n*** loadb2cn failed", d.buf, v[0].len);
1491 dumpbits("pos", v[0].buf, v[0].len);
1492 dumpbits("neg", v[1].buf, v[1].len);
1501 static int lsl(dstr *v)
1504 int n = *(int *)v[1].buf;
1511 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1513 mpx_lsl(d, dl, a, al, n);
1514 if (!mpx_ueq(d, dl, c, cl)) {
1515 fprintf(stderr, "\n*** lsl(%i) failed\n", n);
1516 dumpmp(" a", a, al);
1517 dumpmp("expected", c, cl);
1518 dumpmp(" result", d, dl);
1522 xfree(a); xfree(c); xfree(d);
1526 static int lslc(dstr *v)
1529 int n = *(int *)v[1].buf;
1536 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1538 mpx_lslc(d, dl, a, al, n);
1539 if (!mpx_ueq(d, dl, c, cl)) {
1540 fprintf(stderr, "\n*** lslc(%i) failed\n", n);
1541 dumpmp(" a", a, al);
1542 dumpmp("expected", c, cl);
1543 dumpmp(" result", d, dl);
1547 xfree(a); xfree(c); xfree(d);
1551 static int lsr(dstr *v)
1554 int n = *(int *)v[1].buf;
1561 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1);
1563 mpx_lsr(d, dl, a, al, n);
1564 if (!mpx_ueq(d, dl, c, cl)) {
1565 fprintf(stderr, "\n*** lsr(%i) failed\n", n);
1566 dumpmp(" a", a, al);
1567 dumpmp("expected", c, cl);
1568 dumpmp(" result", d, dl);
1572 xfree(a); xfree(c); xfree(d);
1576 static int uadd(dstr *v)
1587 ALLOC(d, dl, MAX(al - a, bl - b) + 1);
1589 mpx_uadd(d, dl, a, al, b, bl);
1590 if (!mpx_ueq(d, dl, c, cl)) {
1591 fprintf(stderr, "\n*** uadd failed\n");
1592 dumpmp(" a", a, al);
1593 dumpmp(" b", b, bl);
1594 dumpmp("expected", c, cl);
1595 dumpmp(" result", d, dl);
1599 xfree(a); xfree(b); xfree(c); xfree(d);
1603 static int usub(dstr *v)
1614 ALLOC(d, dl, al - a);
1616 mpx_usub(d, dl, a, al, b, bl);
1617 if (!mpx_ueq(d, dl, c, cl)) {
1618 fprintf(stderr, "\n*** usub failed\n");
1619 dumpmp(" a", a, al);
1620 dumpmp(" b", b, bl);
1621 dumpmp("expected", c, cl);
1622 dumpmp(" result", d, dl);
1626 xfree(a); xfree(b); xfree(c); xfree(d);
1630 static int umul(dstr *v)
1641 ALLOC(d, dl, (al - a) + (bl - b));
1643 mpx_umul(d, dl, a, al, b, bl);
1644 if (!mpx_ueq(d, dl, c, cl)) {
1645 fprintf(stderr, "\n*** umul failed\n");
1646 dumpmp(" a", a, al);
1647 dumpmp(" b", b, bl);
1648 dumpmp("expected", c, cl);
1649 dumpmp(" result", d, dl);
1653 xfree(a); xfree(b); xfree(c); xfree(d);
1657 static int usqr(dstr *v)
1666 ALLOC(d, dl, 2 * (al - a));
1668 mpx_usqr(d, dl, a, al);
1669 if (!mpx_ueq(d, dl, c, cl)) {
1670 fprintf(stderr, "\n*** usqr failed\n");
1671 dumpmp(" a", a, al);
1672 dumpmp("expected", c, cl);
1673 dumpmp(" result", d, dl);
1677 xfree(a); xfree(c); xfree(d);
1681 static int udiv(dstr *v)
1691 ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len);
1695 ALLOC(qq, qql, al - a);
1696 ALLOC(s, sl, (bl - b) + 1);
1698 mpx_udiv(qq, qql, a, al, b, bl, s, sl);
1699 if (!mpx_ueq(qq, qql, q, ql) ||
1700 !mpx_ueq(a, al, r, rl)) {
1701 fprintf(stderr, "\n*** udiv failed\n");
1702 dumpmp(" divisor", b, bl);
1703 dumpmp("expect r", r, rl);
1704 dumpmp("result r", a, al);
1705 dumpmp("expect q", q, ql);
1706 dumpmp("result q", qq, qql);
1710 xfree(a); xfree(b); xfree(r); xfree(q); xfree(s); xfree(qq);
1714 static test_chunk defs[] = {
1715 { "load-store", loadstore, { &type_hex, 0 } },
1716 { "2cl", twocl, { &type_hex, &type_hex, } },
1717 { "2cb", twocb, { &type_hex, &type_hex, } },
1718 { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } },
1719 { "lslc", lslc, { &type_hex, &type_int, &type_hex, 0 } },
1720 { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } },
1721 { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } },
1722 { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } },
1723 { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
1724 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
1725 { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } },
1729 int main(int argc, char *argv[])
1731 test_run(argc, argv, defs, SRCDIR"/t/mpx");
1737 /*----- That's all, folks -------------------------------------------------*/