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 /* GCC complains about the generated code, so try to silence it. */
665 #if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
666 # pragma GCC diagnostic push
667 # pragma GCC diagnostic ignored "-Wunused-but-set-variable"
670 #define MPX_BITBINOP(string) \
672 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
673 const mpw *bv, const mpw *bvl) \
675 MPX_SHRINK(av, avl); \
676 MPX_SHRINK(bv, bvl); \
680 a = (av < avl) ? *av++ : 0; \
681 b = (bv < bvl) ? *bv++ : 0; \
682 *dv++ = B##string(a, b); \
686 MPX_DOBIN(MPX_BITBINOP)
688 #if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
689 # pragma GCC diagnostic pop
692 void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
698 a = (av < avl) ? *av++ : 0;
703 /*----- Unsigned arithmetic -----------------------------------------------*/
705 /* --- @mpx_2c@ --- *
707 * Arguments: @mpw *dv, *dvl@ = destination vector
708 * @const mpw *v, *vl@ = source vector
712 * Use: Calculates the two's complement of @v@.
715 void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl)
718 while (dv < dvl && v < vl)
719 *dv++ = c = MPW(~*v++);
726 MPX_UADDN(dv, dvl, 1);
729 /* --- @mpx_ueq@ --- *
731 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
732 * @const mpw *bv, *bvl@ = second argument vector base and limit
734 * Returns: Nonzero if the two vectors are equal.
736 * Use: Performs an unsigned integer test for equality.
739 int mpx_ueq(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
743 if (avl - av != bvl - bv)
752 /* --- @mpx_ucmp@ --- *
754 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
755 * @const mpw *bv, *bvl@ = second argument vector base and limit
757 * Returns: Less than, equal to, or greater than zero depending on
758 * whether @a@ is less than, equal to or greater than @b@,
761 * Use: Performs an unsigned integer comparison.
764 int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
769 if (avl - av > bvl - bv)
771 else if (avl - av < bvl - bv)
773 else while (avl > av) {
774 mpw a = *--avl, b = *--bvl;
783 /* --- @mpx_uadd@ --- *
785 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
786 * @const mpw *av, *avl@ = first addend vector base and limit
787 * @const mpw *bv, *bvl@ = second addend vector base and limit
791 * Use: Performs unsigned integer addition. If the result overflows
792 * the destination vector, high-order bits are discarded. This
793 * means that two's complement addition happens more or less for
794 * free, although that's more a side-effect than anything else.
795 * The result vector may be equal to either or both source
796 * vectors, but may not otherwise overlap them.
799 void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
800 const mpw *bv, const mpw *bvl)
804 while (av < avl || bv < bvl) {
809 a = (av < avl) ? *av++ : 0;
810 b = (bv < bvl) ? *bv++ : 0;
811 x = (mpd)a + (mpd)b + c;
821 /* --- @mpx_uaddn@ --- *
823 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
824 * @mpw n@ = other addend
828 * Use: Adds a small integer to a multiprecision number.
831 void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); }
833 /* --- @mpx_uaddnlsl@ --- *
835 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
836 * @mpw a@ = second argument
837 * @unsigned o@ = offset in bits
841 * Use: Computes %$d + 2^o a$%. If the result overflows then
842 * high-order bits are discarded, as usual. We must have
843 * @0 < o < MPW_BITS@.
846 void mpx_uaddnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
850 while (x && dv < dvl) {
857 /* --- @mpx_usub@ --- *
859 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
860 * @const mpw *av, *avl@ = first argument vector base and limit
861 * @const mpw *bv, *bvl@ = second argument vector base and limit
865 * Use: Performs unsigned integer subtraction. If the result
866 * overflows the destination vector, high-order bits are
867 * discarded. This means that two's complement subtraction
868 * happens more or less for free, althuogh that's more a side-
869 * effect than anything else. The result vector may be equal to
870 * either or both source vectors, but may not otherwise overlap
874 void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
875 const mpw *bv, const mpw *bvl)
879 while (av < avl || bv < bvl) {
884 a = (av < avl) ? *av++ : 0;
885 b = (bv < bvl) ? *bv++ : 0;
886 x = (mpd)a - (mpd)b - c;
899 /* --- @mpx_usubn@ --- *
901 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
906 * Use: Subtracts a small integer from a multiprecision number.
909 void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); }
911 /* --- @mpx_uaddnlsl@ --- *
913 * Arguments: @mpw *dv, *dvl@ = destination and first argument vector
914 * @mpw a@ = second argument
915 * @unsigned o@ = offset in bits
919 * Use: Computes %$d + 2^o a$%. If the result overflows then
920 * high-order bits are discarded, as usual. We must have
921 * @0 < o < MPW_BITS@.
924 void mpx_usubnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
926 mpw b = a >> (MPW_BITS - o);
930 mpd x = (mpd)*dv - MPW(a);
934 MPX_USUBN(dv, dvl, b);
938 /* --- @mpx_umul@ --- *
940 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
941 * @const mpw *av, *avl@ = multiplicand vector base and limit
942 * @const mpw *bv, *bvl@ = multiplier vector base and limit
946 * Use: Performs unsigned integer multiplication. If the result
947 * overflows the desination vector, high-order bits are
948 * discarded. The result vector may not overlap the argument
949 * vectors in any way.
952 void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
953 const mpw *bv, const mpw *bvl)
955 /* --- This is probably worthwhile on a multiply --- */
960 /* --- Deal with a multiply by zero --- */
967 /* --- Do the initial multiply and initialize the accumulator --- */
969 MPX_UMULN(dv, dvl, av, avl, *bv++);
971 /* --- Do the remaining multiply/accumulates --- */
973 while (dv < dvl && bv < bvl) {
983 x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c;
987 MPX_UADDN(dvv, dvl, c);
992 /* --- @mpx_umuln@ --- *
994 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
995 * @const mpw *av, *avl@ = multiplicand vector base and limit
996 * @mpw m@ = multiplier
1000 * Use: Multiplies a multiprecision integer by a single-word value.
1001 * The destination and source may be equal. The destination
1002 * is completely cleared after use.
1005 void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
1006 { MPX_UMULN(dv, dvl, av, avl, m); }
1008 /* --- @mpx_umlan@ --- *
1010 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
1011 * @const mpw *av, *avl@ = multiplicand vector base and limit
1012 * @mpw m@ = multiplier
1016 * Use: Multiplies a multiprecision integer by a single-word value
1017 * and adds the result to an accumulator.
1020 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
1021 { MPX_UMLAN(dv, dvl, av, avl, m); }
1023 /* --- @mpx_usqr@ --- *
1025 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
1026 * @const mpw *av, *av@ = source vector base and limit
1030 * Use: Performs unsigned integer squaring. The result vector must
1031 * not overlap the source vector in any way.
1034 void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
1038 /* --- Main loop --- */
1041 const mpw *avv = av;
1046 /* --- Stop if I've run out of destination --- */
1051 /* --- Work out the square at this point in the proceedings --- */
1054 mpd x = (mpd)a * (mpd)a + *dvv;
1056 c = MPW(x >> MPW_BITS);
1059 /* --- Now fix up the rest of the vector upwards --- */
1062 while (dvv < dvl && avv < avl) {
1063 mpd x = (mpd)a * (mpd)*avv++;
1064 mpd y = ((x << 1) & MPW_MAX) + c + *dvv;
1065 c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS);
1068 while (dvv < dvl && c) {
1074 /* --- Get ready for the next round --- */
1081 /* --- @mpx_udiv@ --- *
1083 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
1084 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
1085 * @const mpw *dv, *dvl@ = divisor vector base and limit
1086 * @mpw *sv, *svl@ = scratch workspace
1090 * Use: Performs unsigned integer division. If the result overflows
1091 * the quotient vector, high-order bits are discarded. (Clearly
1092 * the remainder vector can't overflow.) The various vectors
1093 * may not overlap in any way. Yes, I know it's a bit odd
1094 * requiring the dividend to be in the result position but it
1095 * does make some sense really. The remainder must have
1096 * headroom for at least two extra words. The scratch space
1097 * must be at least one word larger than the divisor.
1100 void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
1101 const mpw *dv, const mpw *dvl,
1108 /* --- Initialize the quotient --- */
1112 /* --- Perform some sanity checks --- */
1114 MPX_SHRINK(dv, dvl);
1115 assert(((void)"division by zero in mpx_udiv", dv < dvl));
1117 /* --- Normalize the divisor --- *
1119 * The algorithm requires that the divisor be at least two digits long.
1120 * This is easy to fix.
1127 for (b = MPW_P2; b; b >>= 1) {
1128 if (d <= (MPW_MAX >> b)) {
1137 /* --- Normalize the dividend/remainder to match --- */
1140 mpx_lsl(rv, rvl, rv, rvl, norm);
1141 mpx_lsl(sv, svl, dv, dvl, norm);
1144 MPX_SHRINK(dv, dvl);
1147 MPX_SHRINK(rv, rvl);
1151 /* --- Work out the relative scales --- */
1154 size_t rvn = rvl - rv;
1155 size_t dvn = dvl - dv;
1157 /* --- If the divisor is clearly larger, notice this --- */
1160 mpx_lsr(rv, rvl, rv, rvl, norm);
1167 /* --- Calculate the most significant quotient digit --- *
1169 * Because the divisor has its top bit set, this can only happen once. The
1170 * pointer arithmetic is a little contorted, to make sure that the
1171 * behaviour is defined.
1174 if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) {
1175 mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1176 if (qvl - qv > scale)
1180 /* --- Now for the main loop --- */
1189 /* --- Get an estimate for the next quotient digit --- */
1196 rh = ((mpd)r << MPW_BITS) | rr;
1202 /* --- Refine the estimate --- */
1205 mpd yh = (mpd)d * q;
1206 mpd yy = (mpd)dd * q;
1210 yh += yy >> MPW_BITS;
1213 while (yh > rh || (yh == rh && yl > rrr)) {
1222 /* --- Remove a chunk from the dividend --- */
1229 /* --- Calculate the size of the chunk --- *
1231 * This does the whole job of calculating @r >> scale - qd@.
1234 for (svv = rv + scale, dvv = dv;
1235 dvv < dvl && svv < rvl;
1237 mpd x = (mpd)*dvv * (mpd)q + mc;
1239 x = (mpd)*svv - MPW(x) - sc;
1248 mpd x = (mpd)*svv - mc - sc;
1258 /* --- Fix if the quotient was too large --- *
1260 * This doesn't seem to happen very often.
1263 if (rvl[-1] > MPW_MAX / 2) {
1264 mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl);
1269 /* --- Done for another iteration --- */
1271 if (qvl - qv > scale)
1278 /* --- Now fiddle with unnormalizing and things --- */
1280 mpx_lsr(rv, rvl, rv, rvl, norm);
1283 /* --- @mpx_udivn@ --- *
1285 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1287 * @const mpw *rv, *rvl@ = dividend
1288 * @mpw d@ = single-precision divisor
1290 * Returns: Remainder after divison.
1292 * Use: Performs a single-precision division operation.
1295 mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d)
1298 size_t ql = qvl - qv;
1304 r = (r << MPW_BITS) | rv[i];
1312 /*----- Test rig ----------------------------------------------------------*/
1316 #include <mLib/alloc.h>
1317 #include <mLib/dstr.h>
1318 #include <mLib/quis.h>
1319 #include <mLib/testrig.h>
1323 #define ALLOC(v, vl, sz) do { \
1324 size_t _sz = (sz); \
1325 mpw *_vv = xmalloc(MPWS(_sz)); \
1326 mpw *_vvl = _vv + _sz; \
1331 #define LOAD(v, vl, d) do { \
1332 const dstr *_d = (d); \
1334 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1335 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1340 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1342 static void dumpbits(const char *msg, const void *pp, size_t sz)
1344 const octet *p = pp;
1347 fprintf(stderr, " %02x", *p++);
1348 fputc('\n', stderr);
1351 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
1356 fprintf(stderr, " %08lx", (unsigned long)*--vl);
1357 fputc('\n', stderr);
1360 static int chkscan(const mpw *v, const mpw *vl,
1361 const void *pp, size_t sz, int step)
1364 const octet *p = pp;
1368 mpscan_initx(&mps, v, vl);
1373 for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) {
1374 if (MPSCAN_BIT(&mps) != (x & 1)) {
1376 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1377 step, bit, x & 1, MPSCAN_BIT(&mps));
1389 static int loadstore(dstr *v)
1392 size_t sz = MPW_RQ(v->len) * 2, diff;
1396 dstr_ensure(&d, v->len);
1397 m = xmalloc(MPWS(sz));
1399 for (diff = 0; diff < sz; diff += 5) {
1404 mpx_loadl(m, ml, v->buf, v->len);
1405 if (!chkscan(m, ml, v->buf, v->len, +1))
1407 MPX_OCTETS(oct, m, ml);
1408 mpx_storel(m, ml, d.buf, d.sz);
1409 if (memcmp(d.buf, v->buf, oct) != 0) {
1410 dumpbits("\n*** storel failed", d.buf, d.sz);
1414 mpx_loadb(m, ml, v->buf, v->len);
1415 if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1))
1417 MPX_OCTETS(oct, m, ml);
1418 mpx_storeb(m, ml, d.buf, d.sz);
1419 if (memcmp(d.buf + d.sz - oct, v->buf + v->len - oct, oct) != 0) {
1420 dumpbits("\n*** storeb failed", d.buf, d.sz);
1426 dumpbits("input data", v->buf, v->len);
1433 static int twocl(dstr *v)
1440 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1441 dstr_ensure(&d, sz);
1444 m = xmalloc(MPWS(sz));
1447 mpx_loadl(m, ml, v[0].buf, v[0].len);
1448 mpx_storel2cn(m, ml, d.buf, v[1].len);
1449 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1450 dumpbits("\n*** storel2cn failed", d.buf, v[1].len);
1454 mpx_loadl2cn(m, ml, v[1].buf, v[1].len);
1455 mpx_storel(m, ml, d.buf, v[0].len);
1456 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1457 dumpbits("\n*** loadl2cn failed", d.buf, v[0].len);
1462 dumpbits("pos", v[0].buf, v[0].len);
1463 dumpbits("neg", v[1].buf, v[1].len);
1472 static int twocb(dstr *v)
1479 sz = v[0].len; if (v[1].len > sz) sz = v[1].len;
1480 dstr_ensure(&d, sz);
1483 m = xmalloc(MPWS(sz));
1486 mpx_loadb(m, ml, v[0].buf, v[0].len);
1487 mpx_storeb2cn(m, ml, d.buf, v[1].len);
1488 if (memcmp(d.buf, v[1].buf, v[1].len)) {
1489 dumpbits("\n*** storeb2cn failed", d.buf, v[1].len);
1493 mpx_loadb2cn(m, ml, v[1].buf, v[1].len);
1494 mpx_storeb(m, ml, d.buf, v[0].len);
1495 if (memcmp(d.buf, v[0].buf, v[0].len)) {
1496 dumpbits("\n*** loadb2cn failed", d.buf, v[0].len);
1501 dumpbits("pos", v[0].buf, v[0].len);
1502 dumpbits("neg", v[1].buf, v[1].len);
1511 static int lsl(dstr *v)
1514 int n = *(int *)v[1].buf;
1521 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1523 mpx_lsl(d, dl, a, al, n);
1524 if (!mpx_ueq(d, dl, c, cl)) {
1525 fprintf(stderr, "\n*** lsl(%i) failed\n", n);
1526 dumpmp(" a", a, al);
1527 dumpmp("expected", c, cl);
1528 dumpmp(" result", d, dl);
1532 xfree(a); xfree(c); xfree(d);
1536 static int lslc(dstr *v)
1539 int n = *(int *)v[1].buf;
1546 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
1548 mpx_lslc(d, dl, a, al, n);
1549 if (!mpx_ueq(d, dl, c, cl)) {
1550 fprintf(stderr, "\n*** lslc(%i) failed\n", n);
1551 dumpmp(" a", a, al);
1552 dumpmp("expected", c, cl);
1553 dumpmp(" result", d, dl);
1557 xfree(a); xfree(c); xfree(d);
1561 static int lsr(dstr *v)
1564 int n = *(int *)v[1].buf;
1571 ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1);
1573 mpx_lsr(d, dl, a, al, n);
1574 if (!mpx_ueq(d, dl, c, cl)) {
1575 fprintf(stderr, "\n*** lsr(%i) failed\n", n);
1576 dumpmp(" a", a, al);
1577 dumpmp("expected", c, cl);
1578 dumpmp(" result", d, dl);
1582 xfree(a); xfree(c); xfree(d);
1586 static int uadd(dstr *v)
1597 ALLOC(d, dl, MAX(al - a, bl - b) + 1);
1599 mpx_uadd(d, dl, a, al, b, bl);
1600 if (!mpx_ueq(d, dl, c, cl)) {
1601 fprintf(stderr, "\n*** uadd failed\n");
1602 dumpmp(" a", a, al);
1603 dumpmp(" b", b, bl);
1604 dumpmp("expected", c, cl);
1605 dumpmp(" result", d, dl);
1609 xfree(a); xfree(b); xfree(c); xfree(d);
1613 static int usub(dstr *v)
1624 ALLOC(d, dl, al - a);
1626 mpx_usub(d, dl, a, al, b, bl);
1627 if (!mpx_ueq(d, dl, c, cl)) {
1628 fprintf(stderr, "\n*** usub failed\n");
1629 dumpmp(" a", a, al);
1630 dumpmp(" b", b, bl);
1631 dumpmp("expected", c, cl);
1632 dumpmp(" result", d, dl);
1636 xfree(a); xfree(b); xfree(c); xfree(d);
1640 static int umul(dstr *v)
1651 ALLOC(d, dl, (al - a) + (bl - b));
1653 mpx_umul(d, dl, a, al, b, bl);
1654 if (!mpx_ueq(d, dl, c, cl)) {
1655 fprintf(stderr, "\n*** umul failed\n");
1656 dumpmp(" a", a, al);
1657 dumpmp(" b", b, bl);
1658 dumpmp("expected", c, cl);
1659 dumpmp(" result", d, dl);
1663 xfree(a); xfree(b); xfree(c); xfree(d);
1667 static int usqr(dstr *v)
1676 ALLOC(d, dl, 2 * (al - a));
1678 mpx_usqr(d, dl, a, al);
1679 if (!mpx_ueq(d, dl, c, cl)) {
1680 fprintf(stderr, "\n*** usqr failed\n");
1681 dumpmp(" a", a, al);
1682 dumpmp("expected", c, cl);
1683 dumpmp(" result", d, dl);
1687 xfree(a); xfree(c); xfree(d);
1691 static int udiv(dstr *v)
1701 ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len);
1705 ALLOC(qq, qql, al - a);
1706 ALLOC(s, sl, (bl - b) + 1);
1708 mpx_udiv(qq, qql, a, al, b, bl, s, sl);
1709 if (!mpx_ueq(qq, qql, q, ql) ||
1710 !mpx_ueq(a, al, r, rl)) {
1711 fprintf(stderr, "\n*** udiv failed\n");
1712 dumpmp(" divisor", b, bl);
1713 dumpmp("expect r", r, rl);
1714 dumpmp("result r", a, al);
1715 dumpmp("expect q", q, ql);
1716 dumpmp("result q", qq, qql);
1720 xfree(a); xfree(b); xfree(r); xfree(q); xfree(s); xfree(qq);
1724 static test_chunk defs[] = {
1725 { "load-store", loadstore, { &type_hex, 0 } },
1726 { "2cl", twocl, { &type_hex, &type_hex, } },
1727 { "2cb", twocb, { &type_hex, &type_hex, } },
1728 { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } },
1729 { "lslc", lslc, { &type_hex, &type_int, &type_hex, 0 } },
1730 { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } },
1731 { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } },
1732 { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } },
1733 { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
1734 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
1735 { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } },
1739 int main(int argc, char *argv[])
1741 test_run(argc, argv, defs, SRCDIR"/t/mpx");
1747 /*----- That's all, folks -------------------------------------------------*/