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 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
43 /*----- Loading and storing -----------------------------------------------*/
45 /* --- @mpx_storel@ --- *
47 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
48 * @void *pp@ = pointer to octet array
49 * @size_t sz@ = size of octet array
53 * Use: Stores an MP in an octet array, least significant octet
54 * first. High-end octets are silently discarded if there
55 * isn't enough space for them.
58 void mpx_storel(const mpw *v, const mpw *vl, void *pp, size_t sz)
61 octet *p = pp, *q = p + sz;
71 *p++ = U8(w | n << bits);
83 /* --- @mpx_loadl@ --- *
85 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
86 * @const void *pp@ = pointer to octet array
87 * @size_t sz@ = size of octet array
91 * Use: Loads an MP in an octet array, least significant octet
92 * first. High-end octets are ignored if there isn't enough
96 void mpx_loadl(mpw *v, mpw *vl, const void *pp, size_t sz)
100 const octet *p = pp, *q = p + sz;
109 if (bits >= MPW_BITS) {
111 w = n >> (MPW_BITS - bits + 8);
121 /* --- @mpx_storeb@ --- *
123 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
124 * @void *pp@ = pointer to octet array
125 * @size_t sz@ = size of octet array
129 * Use: Stores an MP in an octet array, most significant octet
130 * first. High-end octets are silently discarded if there
131 * isn't enough space for them.
134 void mpx_storeb(const mpw *v, const mpw *vl, void *pp, size_t sz)
137 octet *p = pp, *q = p + sz;
147 *--q = U8(w | n << bits);
149 bits += MPW_BITS - 8;
159 /* --- @mpx_loadb@ --- *
161 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
162 * @const void *pp@ = pointer to octet array
163 * @size_t sz@ = size of octet array
167 * Use: Loads an MP in an octet array, most significant octet
168 * first. High-end octets are ignored if there isn't enough
172 void mpx_loadb(mpw *v, mpw *vl, const void *pp, size_t sz)
176 const octet *p = pp, *q = p + sz;
185 if (bits >= MPW_BITS) {
187 w = n >> (MPW_BITS - bits + 8);
197 /* --- @mpx_storel2cn@ --- *
199 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
200 * @void *pp@ = pointer to octet array
201 * @size_t sz@ = size of octet array
205 * Use: Stores a negative MP in an octet array, least significant
206 * octet first, as two's complement. High-end octets are
207 * silently discarded if there isn't enough space for them.
208 * This obviously makes the output bad.
211 void mpx_storel2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
216 octet *p = pp, *q = p + sz;
228 bits += MPW_BITS - 8;
246 /* --- @mpx_loadl2cn@ --- *
248 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
249 * @const void *pp@ = pointer to octet array
250 * @size_t sz@ = size of octet array
254 * Use: Loads a negative MP in an octet array, least significant
255 * octet first, as two's complement. High-end octets are
256 * ignored if there isn't enough space for them. This probably
257 * means you made the wrong choice coming here.
260 void mpx_loadl2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
265 const octet *p = pp, *q = p + sz;
275 if (bits >= MPW_BITS) {
277 w = n >> (MPW_BITS - bits + 8);
287 /* --- @mpx_storeb2cn@ --- *
289 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
290 * @void *pp@ = pointer to octet array
291 * @size_t sz@ = size of octet array
295 * Use: Stores a negative MP in an octet array, most significant
296 * octet first, as two's complement. High-end octets are
297 * silently discarded if there isn't enough space for them,
298 * which probably isn't what you meant.
301 void mpx_storeb2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
306 octet *p = pp, *q = p + sz;
318 bits += MPW_BITS - 8;
330 c = c && !(b & 0xff);
336 /* --- @mpx_loadb2cn@ --- *
338 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
339 * @const void *pp@ = pointer to octet array
340 * @size_t sz@ = size of octet array
344 * Use: Loads a negative MP in an octet array, most significant octet
345 * first as two's complement. High-end octets are ignored if
346 * there isn't enough space for them. This probably means you
347 * chose this function wrongly.
350 void mpx_loadb2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
355 const octet *p = pp, *q = p + sz;
365 if (bits >= MPW_BITS) {
367 w = n >> (MPW_BITS - bits + 8);
377 /*----- Logical shifting --------------------------------------------------*/
379 /* --- @mpx_lsl@ --- *
381 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
382 * @const mpw *av, *avl@ = source vector base and limit
383 * @size_t n@ = number of bit positions to shift by
387 * Use: Performs a logical shift left operation on an integer.
390 void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
395 /* --- Trivial special case --- */
398 MPX_COPY(dv, dvl, av, avl);
400 /* --- Single bit shifting --- */
409 *dv++ = MPW((t << 1) | w);
410 w = t >> (MPW_BITS - 1);
419 /* --- Break out word and bit shifts for more sophisticated work --- */
424 /* --- Handle a shift by a multiple of the word size --- */
430 MPX_COPY(dv + nw, dvl, av, avl);
431 memset(dv, 0, MPWS(nw));
435 /* --- And finally the difficult case --- *
437 * This is a little convoluted, because I have to start from the end and
438 * work backwards to avoid overwriting the source, if they're both the same
444 size_t nr = MPW_BITS - nb;
445 size_t dvn = dvl - dv;
446 size_t avn = avl - av;
453 if (dvn > avn + nw) {
454 size_t off = avn + nw + 1;
455 MPX_ZERO(dv + off, dvl);
465 *--dvl = (t >> nr) | w;
476 /* --- @mpx_lslc@ --- *
478 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
479 * @const mpw *av, *avl@ = source vector base and limit
480 * @size_t n@ = number of bit positions to shift by
484 * Use: Performs a logical shift left operation on an integer, only
485 * it fills in the bits with ones instead of zeroes.
488 void mpx_lslc(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
493 /* --- Trivial special case --- */
496 MPX_COPY(dv, dvl, av, avl);
498 /* --- Single bit shifting --- */
507 *dv++ = MPW((t << 1) | w);
508 w = t >> (MPW_BITS - 1);
517 /* --- Break out word and bit shifts for more sophisticated work --- */
522 /* --- Handle a shift by a multiple of the word size --- */
528 MPX_COPY(dv + nw, dvl, av, avl);
529 MPX_ONE(dv, dv + nw);
533 /* --- And finally the difficult case --- *
535 * This is a little convoluted, because I have to start from the end and
536 * work backwards to avoid overwriting the source, if they're both the same
542 size_t nr = MPW_BITS - nb;
543 size_t dvn = dvl - dv;
544 size_t avn = avl - av;
551 if (dvn > avn + nw) {
552 size_t off = avn + nw + 1;
553 MPX_ZERO(dv + off, dvl);
563 *--dvl = (t >> nr) | w;
567 *--dvl = (MPW_MAX >> nr) | w;
574 /* --- @mpx_lsr@ --- *
576 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
577 * @const mpw *av, *avl@ = source vector base and limit
578 * @size_t n@ = number of bit positions to shift by
582 * Use: Performs a logical shift right operation on an integer.
585 void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
590 /* --- Trivial special case --- */
593 MPX_COPY(dv, dvl, av, avl);
595 /* --- Single bit shifting --- */
598 mpw w = av < avl ? *av++ >> 1 : 0;
604 *dv++ = MPW((t << (MPW_BITS - 1)) | w);
614 /* --- Break out word and bit shifts for more sophisticated work --- */
619 /* --- Handle a shift by a multiple of the word size --- */
625 MPX_COPY(dv, dvl, av + nw, avl);
628 /* --- And finally the difficult case --- */
632 size_t nr = MPW_BITS - nb;
635 w = av < avl ? *av++ : 0;
641 *dv++ = MPW((w >> nb) | (t << nr));
645 *dv++ = MPW(w >> nb);
653 /*----- Bitwise operations ------------------------------------------------*/
655 /* --- @mpx_bitop@ --- *
657 * Arguments: @mpw *dv, *dvl@ = destination vector
658 * @const mpw *av, *avl@ = first source vector
659 * @const mpw *bv, *bvl@ = second source vector
663 * Use; Provides the dyadic boolean functions.
666 #define MPX_BITBINOP(string) \
668 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
669 const mpw *bv, const mpw *bvl) \
671 MPX_SHRINK(av, avl); \
672 MPX_SHRINK(bv, bvl); \
676 a = (av < avl) ? *av++ : 0; \
677 b = (bv < bvl) ? *bv++ : 0; \
678 *dv++ = B##string(a, b); \
682 MPX_DOBIN(MPX_BITBINOP)
684 void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
690 a = (av < avl) ? *av++ : 0;
695 /*----- Unsigned arithmetic -----------------------------------------------*/
697 /* --- @mpx_2c@ --- *
699 * Arguments: @mpw *dv, *dvl@ = destination vector
700 * @const mpw *v, *vl@ = source vector
704 * Use: Calculates the two's complement of @v@.
707 void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl)
710 while (dv < dvl && v < vl)
711 *dv++ = c = MPW(~*v++);
718 MPX_UADDN(dv, dvl, 1);
721 /* --- @mpx_ueq@ --- *
723 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
724 * @const mpw *bv, *bvl@ = second argument vector base and limit
726 * Returns: Nonzero if the two vectors are equal.
728 * Use: Performs an unsigned integer test for equality.
731 int mpx_ueq(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
735 if (avl - av != bvl - bv)
744 /* --- @mpx_ucmp@ --- *
746 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
747 * @const mpw *bv, *bvl@ = second argument vector base and limit
749 * Returns: Less than, equal to, or greater than zero depending on
750 * whether @a@ is less than, equal to or greater than @b@,
753 * Use: Performs an unsigned integer comparison.
756 int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
761 if (avl - av > bvl - bv)
763 else if (avl - av < bvl - bv)
765 else while (avl > av) {
766 mpw a = *--avl, b = *--bvl;
775 /* --- @mpx_uadd@ --- *
777 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
778 * @const mpw *av, *avl@ = first addend vector base and limit
779 * @const mpw *bv, *bvl@ = second addend vector base and limit
783 * Use: Performs unsigned integer addition. If the result overflows
784 * the destination vector, high-order bits are discarded. This
785 * means that two's complement addition happens more or less for
786 * free, although that's more a side-effect than anything else.
787 * The result vector may be equal to either or both source
788 * vectors, but may not otherwise overlap them.
791 void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
792 const mpw *bv, const mpw *bvl)
796 while (av < avl || bv < bvl) {
801 a = (av < avl) ? *av++ : 0;
802 b = (bv < bvl) ? *bv++ : 0;
803 x = (mpd)a + (mpd)b + c;
813 /* --- @mpx_uaddn@ --- *
815 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
816 * @mpw n@ = other addend
820 * Use: Adds a small integer to a multiprecision number.
823 void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); }
825 /* --- @mpx_uaddnlsl@ --- *
827 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
828 * @mpw a@ = second argument
829 * @unsigned o@ = offset in bits
833 * Use: Computes %$d + 2^o a$%. If the result overflows then
834 * high-order bits are discarded, as usual. We must have
835 * @0 < o < MPW_BITS@.
838 void mpx_uaddnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
842 while (x && dv < dvl) {
849 /* --- @mpx_usub@ --- *
851 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
852 * @const mpw *av, *avl@ = first argument vector base and limit
853 * @const mpw *bv, *bvl@ = second argument vector base and limit
857 * Use: Performs unsigned integer subtraction. If the result
858 * overflows the destination vector, high-order bits are
859 * discarded. This means that two's complement subtraction
860 * happens more or less for free, althuogh that's more a side-
861 * effect than anything else. The result vector may be equal to
862 * either or both source vectors, but may not otherwise overlap
866 void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
867 const mpw *bv, const mpw *bvl)
871 while (av < avl || bv < bvl) {
876 a = (av < avl) ? *av++ : 0;
877 b = (bv < bvl) ? *bv++ : 0;
878 x = (mpd)a - (mpd)b - c;
891 /* --- @mpx_usubn@ --- *
893 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
898 * Use: Subtracts a small integer from a multiprecision number.
901 void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); }
903 /* --- @mpx_uaddnlsl@ --- *
905 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
906 * @mpw a@ = second argument
907 * @unsigned o@ = offset in bits
911 * Use: Computes %$d + 2^o a$%. If the result overflows then
912 * high-order bits are discarded, as usual. We must have
913 * @0 < o < MPW_BITS@.
916 void mpx_usubnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
918 mpw b = a >> (MPW_BITS - o);
922 mpd x = (mpd)*dv - (mpd)a;
926 MPX_USUBN(dv, dvl, b);
930 /* --- @mpx_umul@ --- *
932 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
933 * @const mpw *av, *avl@ = multiplicand vector base and limit
934 * @const mpw *bv, *bvl@ = multiplier vector base and limit
938 * Use: Performs unsigned integer multiplication. If the result
939 * overflows the desination vector, high-order bits are
940 * discarded. The result vector may not overlap the argument
941 * vectors in any way.
944 void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
945 const mpw *bv, const mpw *bvl)
947 /* --- This is probably worthwhile on a multiply --- */
952 /* --- Deal with a multiply by zero --- */
959 /* --- Do the initial multiply and initialize the accumulator --- */
961 MPX_UMULN(dv, dvl, av, avl, *bv++);
963 /* --- Do the remaining multiply/accumulates --- */
965 while (dv < dvl && bv < bvl) {
975 x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c;
979 MPX_UADDN(dvv, dvl, c);
984 /* --- @mpx_umuln@ --- *
986 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
987 * @const mpw *av, *avl@ = multiplicand vector base and limit
988 * @mpw m@ = multiplier
992 * Use: Multiplies a multiprecision integer by a single-word value.
993 * The destination and source may be equal. The destination
994 * is completely cleared after use.
997 void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
999 MPX_UMULN(dv, dvl, av, avl, m);
1002 /* --- @mpx_umlan@ --- *
1004 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
1005 * @const mpw *av, *avl@ = multiplicand vector base and limit
1006 * @mpw m@ = multiplier
1010 * Use: Multiplies a multiprecision integer by a single-word value
1011 * and adds the result to an accumulator.
1014 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
1016 MPX_UMLAN(dv, dvl, av, avl, m);
1019 /* --- @mpx_usqr@ --- *
1021 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1022 * @const mpw *av, *av@ = source vector base and limit
1026 * Use: Performs unsigned integer squaring. The result vector must
1027 * not overlap the source vector in any way.
1030 void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
1034 /* --- Main loop --- */
1037 const mpw *avv = av;
1042 /* --- Stop if I've run out of destination --- */
1047 /* --- Work out the square at this point in the proceedings --- */
1050 mpd x = (mpd)a * (mpd)a + *dvv;
1052 c = MPW(x >> MPW_BITS);
1055 /* --- Now fix up the rest of the vector upwards --- */
1058 while (dvv < dvl && avv < avl) {
1059 mpd x = (mpd)a * (mpd)*avv++;
1060 mpd y = ((x << 1) & MPW_MAX) + c + *dvv;
1061 c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS);
1064 while (dvv < dvl && c) {
1070 /* --- Get ready for the next round --- */
1077 /* --- @mpx_udiv@ --- *
1079 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1080 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1081 * @const mpw *dv, *dvl@ = divisor vector base and limit
1082 * @mpw *sv, *svl@ = scratch workspace
1086 * Use: Performs unsigned integer division. If the result overflows
1087 * the quotient vector, high-order bits are discarded. (Clearly
1088 * the remainder vector can't overflow.) The various vectors
1089 * may not overlap in any way. Yes, I know it's a bit odd
1090 * requiring the dividend to be in the result position but it
1091 * does make some sense really. The remainder must have
1092 * headroom for at least two extra words. The scratch space
1093 * must be at least one word larger than the divisor.
1096 void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
1097 const mpw *dv, const mpw *dvl,
1104 /* --- Initialize the quotient --- */
1108 /* --- Perform some sanity checks --- */
1110 MPX_SHRINK(dv, dvl);
1111 assert(((void)"division by zero in mpx_udiv", dv < dvl));
1113 /* --- Normalize the divisor --- *
1115 * The algorithm requires that the divisor be at least two digits long.
1116 * This is easy to fix.
1123 for (b = MPW_BITS / 2; b; b >>= 1) {
1124 if (d <= (MPW_MAX >> b)) {
1133 /* --- Normalize the dividend/remainder to match --- */
1136 mpx_lsl(rv, rvl, rv, rvl, norm);
1137 mpx_lsl(sv, svl, dv, dvl, norm);
1140 MPX_SHRINK(dv, dvl);
1143 MPX_SHRINK(rv, rvl);
1147 /* --- Work out the relative scales --- */
1150 size_t rvn = rvl - rv;
1151 size_t dvn = dvl - dv;
1153 /* --- If the divisor is clearly larger, notice this --- */
1156 mpx_lsr(rv, rvl, rv, rvl, norm);
1163 /* --- Calculate the most significant quotient digit --- *
1165 * Because the divisor has its top bit set, this can only happen once. The
1166 * pointer arithmetic is a little contorted, to make sure that the
1167 * behaviour is defined.
1170 if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) {
1171 mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1172 if (qvl - qv > scale)
1176 /* --- Now for the main loop --- */
1185 /* --- Get an estimate for the next quotient digit --- */
1192 rh = ((mpd)r << MPW_BITS) | rr;
1198 /* --- Refine the estimate --- */
1201 mpd yh = (mpd)d * q;
1202 mpd yy = (mpd)dd * q;
1206 yh += yy >> MPW_BITS;
1209 while (yh > rh || (yh == rh && yl > rrr)) {
1218 /* --- Remove a chunk from the dividend --- */
1225 /* --- Calculate the size of the chunk --- *
1227 * This does the whole job of calculating @r >> scale - qd@.
1230 for (svv = rv + scale, dvv = dv;
1231 dvv < dvl && svv < rvl;
1233 mpd x = (mpd)*dvv * (mpd)q + mc;
1235 x = (mpd)*svv - MPW(x) - sc;
1244 mpd x = (mpd)*svv - mc - sc;
1254 /* --- Fix if the quotient was too large --- *
1256 * This doesn't seem to happen very often.
1259 if (rvl[-1] > MPW_MAX / 2) {
1260 mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1265 /* --- Done for another iteration --- */
1267 if (qvl - qv > scale)
1274 /* --- Now fiddle with unnormalizing and things --- */
1276 mpx_lsr(rv, rvl, rv, rvl, norm);
1279 /* --- @mpx_udivn@ --- *
1281 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1283 * @const mpw *rv, *rvl@ = dividend
1284 * @mpw d@ = single-precision divisor
1286 * Returns: Remainder after divison.
1288 * Use: Performs a single-precision division operation.
1291 mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d)
1294 size_t ql = qvl - qv;
1300 r = (r << MPW_BITS) | rv[i];
1308 /*----- Test rig ----------------------------------------------------------*/
1312 #include <mLib/alloc.h>
1313 #include <mLib/dstr.h>
1314 #include <mLib/quis.h>
1315 #include <mLib/testrig.h>
1319 #define ALLOC(v, vl, sz) do { \
1320 size_t _sz = (sz); \
1321 mpw *_vv = xmalloc(MPWS(_sz)); \
1322 mpw *_vvl = _vv + _sz; \
1327 #define LOAD(v, vl, d) do { \
1328 const dstr *_d = (d); \
1330 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1331 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1336 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1338 static void dumpbits(const char *msg, const void *pp, size_t sz)
1340 const octet *p = pp;
1343 fprintf(stderr, " %02x", *p++);
1344 fputc('\n', stderr);
1347 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
1352 fprintf(stderr, " %08lx", (unsigned long)*--vl);
1353 fputc('\n', stderr);
1356 static int chkscan(const mpw *v, const mpw *vl,
1357 const void *pp, size_t sz, int step)
1360 const octet *p = pp;
1364 mpscan_initx(&mps, v, vl);
1369 for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) {
1370 if (MPSCAN_BIT(&mps) != (x & 1)) {
1372 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1373 step, bit, x & 1, MPSCAN_BIT(&mps));
1385 static int loadstore(dstr *v)
1388 size_t sz = MPW_RQ(v->len) * 2, diff;
1392 dstr_ensure(&d, v->len);
1393 m = xmalloc(MPWS(sz));
1395 for (diff = 0; diff < sz; diff += 5) {
1400 mpx_loadl(m, ml, v->buf, v->len);
1401 if (!chkscan(m, ml, v->buf, v->len, +1))
1403 MPX_OCTETS(oct, m, ml);
1404 mpx_storel(m, ml, d.buf, d.sz);
1405 if (memcmp(d.buf, v->buf, oct) != 0) {
1406 dumpbits("\n*** storel failed", d.buf, d.sz);
1410 mpx_loadb(m, ml, v->buf, v->len);
1411 if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1))
1413 MPX_OCTETS(oct, m, ml);
1414 mpx_storeb(m, ml, d.buf, d.sz);
1415 if (memcmp(d.buf + d.sz - oct, v->buf + v->len - oct, oct) != 0) {
1416 dumpbits("\n*** storeb failed", d.buf, d.sz);
1422 dumpbits("input data", v->buf, v->len);
1429 static int twocl(dstr *v)
1436 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1437 dstr_ensure(&d, sz);
1440 m = xmalloc(MPWS(sz));
1443 mpx_loadl(m, ml, v[0].buf, v[0].len);
1444 mpx_storel2cn(m, ml, d.buf, v[1].len);
1445 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1446 dumpbits("\n*** storel2cn failed", d.buf, v[1].len);
1450 mpx_loadl2cn(m, ml, v[1].buf, v[1].len);
1451 mpx_storel(m, ml, d.buf, v[0].len);
1452 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1453 dumpbits("\n*** loadl2cn failed", d.buf, v[0].len);
1458 dumpbits("pos", v[0].buf, v[0].len);
1459 dumpbits("neg", v[1].buf, v[1].len);
1468 static int twocb(dstr *v)
1475 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1476 dstr_ensure(&d, sz);
1479 m = xmalloc(MPWS(sz));
1482 mpx_loadb(m, ml, v[0].buf, v[0].len);
1483 mpx_storeb2cn(m, ml, d.buf, v[1].len);
1484 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1485 dumpbits("\n*** storeb2cn failed", d.buf, v[1].len);
1489 mpx_loadb2cn(m, ml, v[1].buf, v[1].len);
1490 mpx_storeb(m, ml, d.buf, v[0].len);
1491 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1492 dumpbits("\n*** loadb2cn failed", d.buf, v[0].len);
1497 dumpbits("pos", v[0].buf, v[0].len);
1498 dumpbits("neg", v[1].buf, v[1].len);
1507 static int lsl(dstr *v)
1510 int n = *(int *)v[1].buf;
1517 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1519 mpx_lsl(d, dl, a, al, n);
1520 if (!mpx_ueq(d, dl, c, cl)) {
1521 fprintf(stderr, "\n*** lsl(%i) failed\n", n);
1522 dumpmp(" a", a, al);
1523 dumpmp("expected", c, cl);
1524 dumpmp(" result", d, dl);
1528 xfree(a); xfree(c); xfree(d);
1532 static int lslc(dstr *v)
1535 int n = *(int *)v[1].buf;
1542 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1544 mpx_lslc(d, dl, a, al, n);
1545 if (!mpx_ueq(d, dl, c, cl)) {
1546 fprintf(stderr, "\n*** lslc(%i) failed\n", n);
1547 dumpmp(" a", a, al);
1548 dumpmp("expected", c, cl);
1549 dumpmp(" result", d, dl);
1553 xfree(a); xfree(c); xfree(d);
1557 static int lsr(dstr *v)
1560 int n = *(int *)v[1].buf;
1567 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1);
1569 mpx_lsr(d, dl, a, al, n);
1570 if (!mpx_ueq(d, dl, c, cl)) {
1571 fprintf(stderr, "\n*** lsr(%i) failed\n", n);
1572 dumpmp(" a", a, al);
1573 dumpmp("expected", c, cl);
1574 dumpmp(" result", d, dl);
1578 xfree(a); xfree(c); xfree(d);
1582 static int uadd(dstr *v)
1593 ALLOC(d, dl, MAX(al - a, bl - b) + 1);
1595 mpx_uadd(d, dl, a, al, b, bl);
1596 if (!mpx_ueq(d, dl, c, cl)) {
1597 fprintf(stderr, "\n*** uadd failed\n");
1598 dumpmp(" a", a, al);
1599 dumpmp(" b", b, bl);
1600 dumpmp("expected", c, cl);
1601 dumpmp(" result", d, dl);
1605 xfree(a); xfree(b); xfree(c); xfree(d);
1609 static int usub(dstr *v)
1620 ALLOC(d, dl, al - a);
1622 mpx_usub(d, dl, a, al, b, bl);
1623 if (!mpx_ueq(d, dl, c, cl)) {
1624 fprintf(stderr, "\n*** usub failed\n");
1625 dumpmp(" a", a, al);
1626 dumpmp(" b", b, bl);
1627 dumpmp("expected", c, cl);
1628 dumpmp(" result", d, dl);
1632 xfree(a); xfree(b); xfree(c); xfree(d);
1636 static int umul(dstr *v)
1647 ALLOC(d, dl, (al - a) + (bl - b));
1649 mpx_umul(d, dl, a, al, b, bl);
1650 if (!mpx_ueq(d, dl, c, cl)) {
1651 fprintf(stderr, "\n*** umul failed\n");
1652 dumpmp(" a", a, al);
1653 dumpmp(" b", b, bl);
1654 dumpmp("expected", c, cl);
1655 dumpmp(" result", d, dl);
1659 xfree(a); xfree(b); xfree(c); xfree(d);
1663 static int usqr(dstr *v)
1672 ALLOC(d, dl, 2 * (al - a));
1674 mpx_usqr(d, dl, a, al);
1675 if (!mpx_ueq(d, dl, c, cl)) {
1676 fprintf(stderr, "\n*** usqr failed\n");
1677 dumpmp(" a", a, al);
1678 dumpmp("expected", c, cl);
1679 dumpmp(" result", d, dl);
1683 xfree(a); xfree(c); xfree(d);
1687 static int udiv(dstr *v)
1697 ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len);
1701 ALLOC(qq, qql, al - a);
1702 ALLOC(s, sl, (bl - b) + 1);
1704 mpx_udiv(qq, qql, a, al, b, bl, s, sl);
1705 if (!mpx_ueq(qq, qql, q, ql) ||
1706 !mpx_ueq(a, al, r, rl)) {
1707 fprintf(stderr, "\n*** udiv failed\n");
1708 dumpmp(" divisor", b, bl);
1709 dumpmp("expect r", r, rl);
1710 dumpmp("result r", a, al);
1711 dumpmp("expect q", q, ql);
1712 dumpmp("result q", qq, qql);
1716 xfree(a); xfree(b); xfree(r); xfree(q); xfree(s); xfree(qq);
1720 static test_chunk defs[] = {
1721 { "load-store", loadstore, { &type_hex, 0 } },
1722 { "2cl", twocl, { &type_hex, &type_hex, } },
1723 { "2cb", twocb, { &type_hex, &type_hex, } },
1724 { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } },
1725 { "lslc", lslc, { &type_hex, &type_int, &type_hex, 0 } },
1726 { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } },
1727 { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } },
1728 { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } },
1729 { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
1730 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
1731 { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } },
1735 int main(int argc, char *argv[])
1737 test_run(argc, argv, defs, SRCDIR"/tests/mpx");
1743 /*----- That's all, folks -------------------------------------------------*/