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>
36 #include <mLib/macros.h>
42 /*----- Loading and storing -----------------------------------------------*/
44 /* --- @mpx_storel@ --- *
46 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
47 * @void *pp@ = pointer to octet array
48 * @size_t sz@ = size of octet array
52 * Use: Stores an MP in an octet array, least significant octet
53 * first. High-end octets are silently discarded if there
54 * isn't enough space for them.
57 void mpx_storel(const mpw *v, const mpw *vl, void *pp, size_t sz)
60 octet *p = pp, *q = p + sz;
70 *p++ = U8(w | n << bits);
82 /* --- @mpx_loadl@ --- *
84 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
85 * @const void *pp@ = pointer to octet array
86 * @size_t sz@ = size of octet array
90 * Use: Loads an MP in an octet array, least significant octet
91 * first. High-end octets are ignored if there isn't enough
95 void mpx_loadl(mpw *v, mpw *vl, const void *pp, size_t sz)
99 const octet *p = pp, *q = p + sz;
108 if (bits >= MPW_BITS) {
110 w = n >> (MPW_BITS - bits + 8);
120 /* --- @mpx_storeb@ --- *
122 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
123 * @void *pp@ = pointer to octet array
124 * @size_t sz@ = size of octet array
128 * Use: Stores an MP in an octet array, most significant octet
129 * first. High-end octets are silently discarded if there
130 * isn't enough space for them.
133 void mpx_storeb(const mpw *v, const mpw *vl, void *pp, size_t sz)
136 octet *p = pp, *q = p + sz;
146 *--q = U8(w | n << bits);
148 bits += MPW_BITS - 8;
158 /* --- @mpx_loadb@ --- *
160 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
161 * @const void *pp@ = pointer to octet array
162 * @size_t sz@ = size of octet array
166 * Use: Loads an MP in an octet array, most significant octet
167 * first. High-end octets are ignored if there isn't enough
171 void mpx_loadb(mpw *v, mpw *vl, const void *pp, size_t sz)
175 const octet *p = pp, *q = p + sz;
184 if (bits >= MPW_BITS) {
186 w = n >> (MPW_BITS - bits + 8);
196 /* --- @mpx_storel2cn@ --- *
198 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
199 * @void *pp@ = pointer to octet array
200 * @size_t sz@ = size of octet array
204 * Use: Stores a negative MP in an octet array, least significant
205 * octet first, as two's complement. High-end octets are
206 * silently discarded if there isn't enough space for them.
207 * This obviously makes the output bad.
210 void mpx_storel2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
215 octet *p = pp, *q = p + sz;
227 bits += MPW_BITS - 8;
245 /* --- @mpx_loadl2cn@ --- *
247 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
248 * @const void *pp@ = pointer to octet array
249 * @size_t sz@ = size of octet array
253 * Use: Loads a negative MP in an octet array, least significant
254 * octet first, as two's complement. High-end octets are
255 * ignored if there isn't enough space for them. This probably
256 * means you made the wrong choice coming here.
259 void mpx_loadl2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
264 const octet *p = pp, *q = p + sz;
274 if (bits >= MPW_BITS) {
276 w = n >> (MPW_BITS - bits + 8);
286 /* --- @mpx_storeb2cn@ --- *
288 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
289 * @void *pp@ = pointer to octet array
290 * @size_t sz@ = size of octet array
294 * Use: Stores a negative MP in an octet array, most significant
295 * octet first, as two's complement. High-end octets are
296 * silently discarded if there isn't enough space for them,
297 * which probably isn't what you meant.
300 void mpx_storeb2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
305 octet *p = pp, *q = p + sz;
317 bits += MPW_BITS - 8;
329 c = c && !(b & 0xff);
335 /* --- @mpx_loadb2cn@ --- *
337 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
338 * @const void *pp@ = pointer to octet array
339 * @size_t sz@ = size of octet array
343 * Use: Loads a negative MP in an octet array, most significant octet
344 * first as two's complement. High-end octets are ignored if
345 * there isn't enough space for them. This probably means you
346 * chose this function wrongly.
349 void mpx_loadb2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
354 const octet *p = pp, *q = p + sz;
364 if (bits >= MPW_BITS) {
366 w = n >> (MPW_BITS - bits + 8);
376 /*----- Logical shifting --------------------------------------------------*/
378 /* --- @mpx_lsl@ --- *
380 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
381 * @const mpw *av, *avl@ = source vector base and limit
382 * @size_t n@ = number of bit positions to shift by
386 * Use: Performs a logical shift left operation on an integer.
389 void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
394 /* --- Trivial special case --- */
397 MPX_COPY(dv, dvl, av, avl);
399 /* --- Single bit shifting --- */
408 *dv++ = MPW((t << 1) | w);
409 w = t >> (MPW_BITS - 1);
418 /* --- Break out word and bit shifts for more sophisticated work --- */
423 /* --- Handle a shift by a multiple of the word size --- */
429 MPX_COPY(dv + nw, dvl, av, avl);
430 memset(dv, 0, MPWS(nw));
434 /* --- And finally the difficult case --- *
436 * This is a little convoluted, because I have to start from the end and
437 * work backwards to avoid overwriting the source, if they're both the same
443 size_t nr = MPW_BITS - nb;
444 size_t dvn = dvl - dv;
445 size_t avn = avl - av;
452 if (dvn > avn + nw) {
453 size_t off = avn + nw + 1;
454 MPX_ZERO(dv + off, dvl);
464 *--dvl = MPW((t >> nr) | w);
475 /* --- @mpx_lslc@ --- *
477 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
478 * @const mpw *av, *avl@ = source vector base and limit
479 * @size_t n@ = number of bit positions to shift by
483 * Use: Performs a logical shift left operation on an integer, only
484 * it fills in the bits with ones instead of zeroes.
487 void mpx_lslc(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
492 /* --- Trivial special case --- */
495 MPX_COPY(dv, dvl, av, avl);
497 /* --- Single bit shifting --- */
506 *dv++ = MPW((t << 1) | w);
507 w = t >> (MPW_BITS - 1);
516 /* --- Break out word and bit shifts for more sophisticated work --- */
521 /* --- Handle a shift by a multiple of the word size --- */
527 MPX_COPY(dv + nw, dvl, av, avl);
528 MPX_ONE(dv, dv + nw);
532 /* --- And finally the difficult case --- *
534 * This is a little convoluted, because I have to start from the end and
535 * work backwards to avoid overwriting the source, if they're both the same
541 size_t nr = MPW_BITS - nb;
542 size_t dvn = dvl - dv;
543 size_t avn = avl - av;
550 if (dvn > avn + nw) {
551 size_t off = avn + nw + 1;
552 MPX_ZERO(dv + off, dvl);
562 *--dvl = MPW((t >> nr) | w);
566 *--dvl = MPW((MPW_MAX >> nr) | w);
573 /* --- @mpx_lsr@ --- *
575 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
576 * @const mpw *av, *avl@ = source vector base and limit
577 * @size_t n@ = number of bit positions to shift by
581 * Use: Performs a logical shift right operation on an integer.
584 void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
589 /* --- Trivial special case --- */
592 MPX_COPY(dv, dvl, av, avl);
594 /* --- Single bit shifting --- */
597 mpw w = av < avl ? *av++ >> 1 : 0;
603 *dv++ = MPW((t << (MPW_BITS - 1)) | w);
613 /* --- Break out word and bit shifts for more sophisticated work --- */
618 /* --- Handle a shift by a multiple of the word size --- */
624 MPX_COPY(dv, dvl, av + nw, avl);
627 /* --- And finally the difficult case --- */
631 size_t nr = MPW_BITS - nb;
634 w = av < avl ? *av++ : 0;
640 *dv++ = MPW((w >> nb) | (t << nr));
644 *dv++ = MPW(w >> nb);
652 /*----- Bitwise operations ------------------------------------------------*/
654 /* --- @mpx_bitop@ --- *
656 * Arguments: @mpw *dv, *dvl@ = destination vector
657 * @const mpw *av, *avl@ = first source vector
658 * @const mpw *bv, *bvl@ = second source vector
662 * Use; Provides the dyadic boolean functions.
665 #define MPX_BITBINOP(string) \
667 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
668 const mpw *bv, const mpw *bvl) \
670 MPX_SHRINK(av, avl); \
671 MPX_SHRINK(bv, bvl); \
675 a = (av < avl) ? *av++ : 0; \
676 b = (bv < bvl) ? *bv++ : 0; \
677 *dv++ = B##string(a, b); \
678 IGNORE(a); IGNORE(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 - MPW(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)
998 { MPX_UMULN(dv, dvl, av, avl, m); }
1000 /* --- @mpx_umlan@ --- *
1002 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
1003 * @const mpw *av, *avl@ = multiplicand vector base and limit
1004 * @mpw m@ = multiplier
1008 * Use: Multiplies a multiprecision integer by a single-word value
1009 * and adds the result to an accumulator.
1012 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
1013 { MPX_UMLAN(dv, dvl, av, avl, m); }
1015 /* --- @mpx_usqr@ --- *
1017 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1018 * @const mpw *av, *av@ = source vector base and limit
1022 * Use: Performs unsigned integer squaring. The result vector must
1023 * not overlap the source vector in any way.
1026 void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
1030 /* --- Main loop --- */
1033 const mpw *avv = av;
1038 /* --- Stop if I've run out of destination --- */
1043 /* --- Work out the square at this point in the proceedings --- */
1046 mpd x = (mpd)a * (mpd)a + *dvv;
1048 c = MPW(x >> MPW_BITS);
1051 /* --- Now fix up the rest of the vector upwards --- */
1054 while (dvv < dvl && avv < avl) {
1055 mpd x = (mpd)a * (mpd)*avv++;
1056 mpd y = ((x << 1) & MPW_MAX) + c + *dvv;
1057 c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS);
1060 while (dvv < dvl && c) {
1066 /* --- Get ready for the next round --- */
1073 /* --- @mpx_udiv@ --- *
1075 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1076 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1077 * @const mpw *dv, *dvl@ = divisor vector base and limit
1078 * @mpw *sv, *svl@ = scratch workspace
1082 * Use: Performs unsigned integer division. If the result overflows
1083 * the quotient vector, high-order bits are discarded. (Clearly
1084 * the remainder vector can't overflow.) The various vectors
1085 * may not overlap in any way. Yes, I know it's a bit odd
1086 * requiring the dividend to be in the result position but it
1087 * does make some sense really. The remainder must have
1088 * headroom for at least two extra words. The scratch space
1089 * must be at least one word larger than the divisor.
1092 void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
1093 const mpw *dv, const mpw *dvl,
1100 /* --- Initialize the quotient --- */
1104 /* --- Perform some sanity checks --- */
1106 MPX_SHRINK(dv, dvl);
1107 assert(((void)"division by zero in mpx_udiv", dv < dvl));
1109 /* --- Normalize the divisor --- *
1111 * The algorithm requires that the divisor be at least two digits long.
1112 * This is easy to fix.
1119 for (b = MPW_P2; b; b >>= 1) {
1120 if (d <= (MPW_MAX >> b)) {
1129 /* --- Normalize the dividend/remainder to match --- */
1132 mpx_lsl(rv, rvl, rv, rvl, norm);
1133 mpx_lsl(sv, svl, dv, dvl, norm);
1136 MPX_SHRINK(dv, dvl);
1139 MPX_SHRINK(rv, rvl);
1143 /* --- Work out the relative scales --- */
1146 size_t rvn = rvl - rv;
1147 size_t dvn = dvl - dv;
1149 /* --- If the divisor is clearly larger, notice this --- */
1152 mpx_lsr(rv, rvl, rv, rvl, norm);
1159 /* --- Calculate the most significant quotient digit --- *
1161 * Because the divisor has its top bit set, this can only happen once. The
1162 * pointer arithmetic is a little contorted, to make sure that the
1163 * behaviour is defined.
1166 if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) {
1167 mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1168 if (qvl - qv > scale)
1172 /* --- Now for the main loop --- */
1181 /* --- Get an estimate for the next quotient digit --- */
1188 rh = ((mpd)r << MPW_BITS) | rr;
1194 /* --- Refine the estimate --- */
1197 mpd yh = (mpd)d * q;
1198 mpd yy = (mpd)dd * q;
1202 yh += yy >> MPW_BITS;
1205 while (yh > rh || (yh == rh && yl > rrr)) {
1214 /* --- Remove a chunk from the dividend --- */
1221 /* --- Calculate the size of the chunk --- *
1223 * This does the whole job of calculating @r >> scale - qd@.
1226 for (svv = rv + scale, dvv = dv;
1227 dvv < dvl && svv < rvl;
1229 mpd x = (mpd)*dvv * (mpd)q + mc;
1231 x = (mpd)*svv - MPW(x) - sc;
1240 mpd x = (mpd)*svv - mc - sc;
1250 /* --- Fix if the quotient was too large --- *
1252 * This doesn't seem to happen very often.
1255 if (rvl[-1] > MPW_MAX / 2) {
1256 mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1261 /* --- Done for another iteration --- */
1263 if (qvl - qv > scale)
1270 /* --- Now fiddle with unnormalizing and things --- */
1272 mpx_lsr(rv, rvl, rv, rvl, norm);
1275 /* --- @mpx_udivn@ --- *
1277 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1279 * @const mpw *rv, *rvl@ = dividend
1280 * @mpw d@ = single-precision divisor
1282 * Returns: Remainder after divison.
1284 * Use: Performs a single-precision division operation.
1287 mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d)
1290 size_t ql = qvl - qv;
1296 r = (r << MPW_BITS) | rv[i];
1304 /*----- Test rig ----------------------------------------------------------*/
1308 #include <mLib/alloc.h>
1309 #include <mLib/dstr.h>
1310 #include <mLib/quis.h>
1311 #include <mLib/testrig.h>
1315 #define ALLOC(v, vl, sz) do { \
1316 size_t _sz = (sz); \
1317 mpw *_vv = xmalloc(MPWS(_sz)); \
1318 mpw *_vvl = _vv + _sz; \
1323 #define LOAD(v, vl, d) do { \
1324 const dstr *_d = (d); \
1326 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1327 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1332 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1334 static void dumpbits(const char *msg, const void *pp, size_t sz)
1336 const octet *p = pp;
1339 fprintf(stderr, " %02x", *p++);
1340 fputc('\n', stderr);
1343 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
1348 fprintf(stderr, " %08lx", (unsigned long)*--vl);
1349 fputc('\n', stderr);
1352 static int chkscan(const mpw *v, const mpw *vl,
1353 const void *pp, size_t sz, int step)
1356 const octet *p = pp;
1360 mpscan_initx(&mps, v, vl);
1365 for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) {
1366 if (MPSCAN_BIT(&mps) != (x & 1)) {
1368 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1369 step, bit, x & 1, MPSCAN_BIT(&mps));
1381 static int loadstore(dstr *v)
1384 size_t sz = MPW_RQ(v->len) * 2, diff;
1388 dstr_ensure(&d, v->len);
1389 m = xmalloc(MPWS(sz));
1391 for (diff = 0; diff < sz; diff += 5) {
1396 mpx_loadl(m, ml, v->buf, v->len);
1397 if (!chkscan(m, ml, v->buf, v->len, +1))
1399 MPX_OCTETS(oct, m, ml);
1400 mpx_storel(m, ml, d.buf, d.sz);
1401 if (memcmp(d.buf, v->buf, oct) != 0) {
1402 dumpbits("\n*** storel failed", d.buf, d.sz);
1406 mpx_loadb(m, ml, v->buf, v->len);
1407 if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1))
1409 MPX_OCTETS(oct, m, ml);
1410 mpx_storeb(m, ml, d.buf, d.sz);
1411 if (memcmp(d.buf + d.sz - oct, v->buf + v->len - oct, oct) != 0) {
1412 dumpbits("\n*** storeb failed", d.buf, d.sz);
1418 dumpbits("input data", v->buf, v->len);
1425 static int twocl(dstr *v)
1432 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1433 dstr_ensure(&d, sz);
1436 m = xmalloc(MPWS(sz));
1439 mpx_loadl(m, ml, v[0].buf, v[0].len);
1440 mpx_storel2cn(m, ml, d.buf, v[1].len);
1441 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1442 dumpbits("\n*** storel2cn failed", d.buf, v[1].len);
1446 mpx_loadl2cn(m, ml, v[1].buf, v[1].len);
1447 mpx_storel(m, ml, d.buf, v[0].len);
1448 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1449 dumpbits("\n*** loadl2cn failed", d.buf, v[0].len);
1454 dumpbits("pos", v[0].buf, v[0].len);
1455 dumpbits("neg", v[1].buf, v[1].len);
1464 static int twocb(dstr *v)
1471 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1472 dstr_ensure(&d, sz);
1475 m = xmalloc(MPWS(sz));
1478 mpx_loadb(m, ml, v[0].buf, v[0].len);
1479 mpx_storeb2cn(m, ml, d.buf, v[1].len);
1480 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1481 dumpbits("\n*** storeb2cn failed", d.buf, v[1].len);
1485 mpx_loadb2cn(m, ml, v[1].buf, v[1].len);
1486 mpx_storeb(m, ml, d.buf, v[0].len);
1487 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1488 dumpbits("\n*** loadb2cn failed", d.buf, v[0].len);
1493 dumpbits("pos", v[0].buf, v[0].len);
1494 dumpbits("neg", v[1].buf, v[1].len);
1503 static int lsl(dstr *v)
1506 int n = *(int *)v[1].buf;
1513 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1515 mpx_lsl(d, dl, a, al, n);
1516 if (!mpx_ueq(d, dl, c, cl)) {
1517 fprintf(stderr, "\n*** lsl(%i) failed\n", n);
1518 dumpmp(" a", a, al);
1519 dumpmp("expected", c, cl);
1520 dumpmp(" result", d, dl);
1524 xfree(a); xfree(c); xfree(d);
1528 static int lslc(dstr *v)
1531 int n = *(int *)v[1].buf;
1538 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1540 mpx_lslc(d, dl, a, al, n);
1541 if (!mpx_ueq(d, dl, c, cl)) {
1542 fprintf(stderr, "\n*** lslc(%i) failed\n", n);
1543 dumpmp(" a", a, al);
1544 dumpmp("expected", c, cl);
1545 dumpmp(" result", d, dl);
1549 xfree(a); xfree(c); xfree(d);
1553 static int lsr(dstr *v)
1556 int n = *(int *)v[1].buf;
1563 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1);
1565 mpx_lsr(d, dl, a, al, n);
1566 if (!mpx_ueq(d, dl, c, cl)) {
1567 fprintf(stderr, "\n*** lsr(%i) failed\n", n);
1568 dumpmp(" a", a, al);
1569 dumpmp("expected", c, cl);
1570 dumpmp(" result", d, dl);
1574 xfree(a); xfree(c); xfree(d);
1578 static int uadd(dstr *v)
1589 ALLOC(d, dl, MAX(al - a, bl - b) + 1);
1591 mpx_uadd(d, dl, a, al, b, bl);
1592 if (!mpx_ueq(d, dl, c, cl)) {
1593 fprintf(stderr, "\n*** uadd failed\n");
1594 dumpmp(" a", a, al);
1595 dumpmp(" b", b, bl);
1596 dumpmp("expected", c, cl);
1597 dumpmp(" result", d, dl);
1601 xfree(a); xfree(b); xfree(c); xfree(d);
1605 static int usub(dstr *v)
1616 ALLOC(d, dl, al - a);
1618 mpx_usub(d, dl, a, al, b, bl);
1619 if (!mpx_ueq(d, dl, c, cl)) {
1620 fprintf(stderr, "\n*** usub failed\n");
1621 dumpmp(" a", a, al);
1622 dumpmp(" b", b, bl);
1623 dumpmp("expected", c, cl);
1624 dumpmp(" result", d, dl);
1628 xfree(a); xfree(b); xfree(c); xfree(d);
1632 static int umul(dstr *v)
1643 ALLOC(d, dl, (al - a) + (bl - b));
1645 mpx_umul(d, dl, a, al, b, bl);
1646 if (!mpx_ueq(d, dl, c, cl)) {
1647 fprintf(stderr, "\n*** umul failed\n");
1648 dumpmp(" a", a, al);
1649 dumpmp(" b", b, bl);
1650 dumpmp("expected", c, cl);
1651 dumpmp(" result", d, dl);
1655 xfree(a); xfree(b); xfree(c); xfree(d);
1659 static int usqr(dstr *v)
1668 ALLOC(d, dl, 2 * (al - a));
1670 mpx_usqr(d, dl, a, al);
1671 if (!mpx_ueq(d, dl, c, cl)) {
1672 fprintf(stderr, "\n*** usqr failed\n");
1673 dumpmp(" a", a, al);
1674 dumpmp("expected", c, cl);
1675 dumpmp(" result", d, dl);
1679 xfree(a); xfree(c); xfree(d);
1683 static int udiv(dstr *v)
1693 ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len);
1697 ALLOC(qq, qql, al - a);
1698 ALLOC(s, sl, (bl - b) + 1);
1700 mpx_udiv(qq, qql, a, al, b, bl, s, sl);
1701 if (!mpx_ueq(qq, qql, q, ql) ||
1702 !mpx_ueq(a, al, r, rl)) {
1703 fprintf(stderr, "\n*** udiv failed\n");
1704 dumpmp(" divisor", b, bl);
1705 dumpmp("expect r", r, rl);
1706 dumpmp("result r", a, al);
1707 dumpmp("expect q", q, ql);
1708 dumpmp("result q", qq, qql);
1712 xfree(a); xfree(b); xfree(r); xfree(q); xfree(s); xfree(qq);
1716 static test_chunk defs[] = {
1717 { "load-store", loadstore, { &type_hex, 0 } },
1718 { "2cl", twocl, { &type_hex, &type_hex, } },
1719 { "2cb", twocb, { &type_hex, &type_hex, } },
1720 { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } },
1721 { "lslc", lslc, { &type_hex, &type_int, &type_hex, 0 } },
1722 { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } },
1723 { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } },
1724 { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } },
1725 { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
1726 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
1727 { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } },
1731 int main(int argc, char *argv[])
1733 test_run(argc, argv, defs, SRCDIR"/t/mpx");
1739 /*----- That's all, folks -------------------------------------------------*/