3 * $Id: mpx.c,v 1.16 2003/05/16 09:09:24 mdw Exp $
5 * Low-level multiprecision arithmetic
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.16 2003/05/16 09:09:24 mdw
34 * Fix @mp_lsl2c@. Turns out to be surprisingly tricky.
36 * Revision 1.15 2002/10/20 01:12:31 mdw
37 * Two's complement I/O fixes.
39 * Revision 1.14 2002/10/19 18:55:08 mdw
40 * Fix overflows in shift primitives.
42 * Revision 1.13 2002/10/19 17:56:50 mdw
43 * Fix bit operations. Test them (a bit) better.
45 * Revision 1.12 2002/10/06 22:52:50 mdw
46 * Pile of changes for supporting two's complement properly.
48 * Revision 1.11 2001/04/03 19:36:05 mdw
49 * Add some simple bitwise operations so that Perl can use them.
51 * Revision 1.10 2000/10/08 12:06:12 mdw
52 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
54 * Revision 1.9 2000/06/26 07:52:50 mdw
55 * Portability fix for the bug fix.
57 * Revision 1.8 2000/06/25 12:59:02 mdw
58 * (mpx_udiv): Fix bug in quotient digit estimation.
60 * Revision 1.7 1999/12/22 15:49:07 mdw
61 * New function for division by a small integer.
63 * Revision 1.6 1999/11/20 22:43:44 mdw
64 * Integrate testing for MPX routines.
66 * Revision 1.5 1999/11/20 22:23:27 mdw
67 * Add function versions of some low-level macros with wider use.
69 * Revision 1.4 1999/11/17 18:04:09 mdw
70 * Add two's-complement functionality. Improve mpx_udiv a little by
71 * performing the multiplication of the divisor by q with the subtraction
74 * Revision 1.3 1999/11/13 01:57:31 mdw
75 * Remove stray debugging code.
77 * Revision 1.2 1999/11/13 01:50:59 mdw
78 * Multiprecision routines finished and tested.
80 * Revision 1.1 1999/09/03 08:41:12 mdw
85 /*----- Header files ------------------------------------------------------*/
92 #include <mLib/bits.h>
98 /*----- Loading and storing -----------------------------------------------*/
100 /* --- @mpx_storel@ --- *
102 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
103 * @void *pp@ = pointer to octet array
104 * @size_t sz@ = size of octet array
108 * Use: Stores an MP in an octet array, least significant octet
109 * first. High-end octets are silently discarded if there
110 * isn't enough space for them.
113 void mpx_storel(const mpw *v, const mpw *vl, void *pp, size_t sz)
116 octet *p = pp, *q = p + sz;
126 *p++ = U8(w | n << bits);
128 bits += MPW_BITS - 8;
138 /* --- @mpx_loadl@ --- *
140 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
141 * @const void *pp@ = pointer to octet array
142 * @size_t sz@ = size of octet array
146 * Use: Loads an MP in an octet array, least significant octet
147 * first. High-end octets are ignored if there isn't enough
151 void mpx_loadl(mpw *v, mpw *vl, const void *pp, size_t sz)
155 const octet *p = pp, *q = p + sz;
164 if (bits >= MPW_BITS) {
166 w = n >> (MPW_BITS - bits + 8);
176 /* --- @mpx_storeb@ --- *
178 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
179 * @void *pp@ = pointer to octet array
180 * @size_t sz@ = size of octet array
184 * Use: Stores an MP in an octet array, most significant octet
185 * first. High-end octets are silently discarded if there
186 * isn't enough space for them.
189 void mpx_storeb(const mpw *v, const mpw *vl, void *pp, size_t sz)
192 octet *p = pp, *q = p + sz;
202 *--q = U8(w | n << bits);
204 bits += MPW_BITS - 8;
214 /* --- @mpx_loadb@ --- *
216 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
217 * @const void *pp@ = pointer to octet array
218 * @size_t sz@ = size of octet array
222 * Use: Loads an MP in an octet array, most significant octet
223 * first. High-end octets are ignored if there isn't enough
227 void mpx_loadb(mpw *v, mpw *vl, const void *pp, size_t sz)
231 const octet *p = pp, *q = p + sz;
240 if (bits >= MPW_BITS) {
242 w = n >> (MPW_BITS - bits + 8);
252 /* --- @mpx_storel2cn@ --- *
254 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
255 * @void *pp@ = pointer to octet array
256 * @size_t sz@ = size of octet array
260 * Use: Stores a negative MP in an octet array, least significant
261 * octet first, as two's complement. High-end octets are
262 * silently discarded if there isn't enough space for them.
263 * This obviously makes the output bad.
266 void mpx_storel2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
271 octet *p = pp, *q = p + sz;
283 bits += MPW_BITS - 8;
301 /* --- @mpx_loadl2cn@ --- *
303 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
304 * @const void *pp@ = pointer to octet array
305 * @size_t sz@ = size of octet array
309 * Use: Loads a negative MP in an octet array, least significant
310 * octet first, as two's complement. High-end octets are
311 * ignored if there isn't enough space for them. This probably
312 * means you made the wrong choice coming here.
315 void mpx_loadl2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
320 const octet *p = pp, *q = p + sz;
330 if (bits >= MPW_BITS) {
332 w = n >> (MPW_BITS - bits + 8);
342 /* --- @mpx_storeb2cn@ --- *
344 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
345 * @void *pp@ = pointer to octet array
346 * @size_t sz@ = size of octet array
350 * Use: Stores a negative MP in an octet array, most significant
351 * octet first, as two's complement. High-end octets are
352 * silently discarded if there isn't enough space for them,
353 * which probably isn't what you meant.
356 void mpx_storeb2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
361 octet *p = pp, *q = p + sz;
373 bits += MPW_BITS - 8;
385 c = c && !(b & 0xff);
391 /* --- @mpx_loadb2cn@ --- *
393 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
394 * @const void *pp@ = pointer to octet array
395 * @size_t sz@ = size of octet array
399 * Use: Loads a negative MP in an octet array, most significant octet
400 * first as two's complement. High-end octets are ignored if
401 * there isn't enough space for them. This probably means you
402 * chose this function wrongly.
405 void mpx_loadb2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
410 const octet *p = pp, *q = p + sz;
420 if (bits >= MPW_BITS) {
422 w = n >> (MPW_BITS - bits + 8);
432 /*----- Logical shifting --------------------------------------------------*/
434 /* --- @mpx_lsl@ --- *
436 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
437 * @const mpw *av, *avl@ = source vector base and limit
438 * @size_t n@ = number of bit positions to shift by
442 * Use: Performs a logical shift left operation on an integer.
445 void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
450 /* --- Trivial special case --- */
453 MPX_COPY(dv, dvl, av, avl);
455 /* --- Single bit shifting --- */
464 *dv++ = MPW((t << 1) | w);
465 w = t >> (MPW_BITS - 1);
474 /* --- Break out word and bit shifts for more sophisticated work --- */
479 /* --- Handle a shift by a multiple of the word size --- */
485 MPX_COPY(dv + nw, dvl, av, avl);
486 memset(dv, 0, MPWS(nw));
490 /* --- And finally the difficult case --- *
492 * This is a little convoluted, because I have to start from the end and
493 * work backwards to avoid overwriting the source, if they're both the same
499 size_t nr = MPW_BITS - nb;
500 size_t dvn = dvl - dv;
501 size_t avn = avl - av;
508 if (dvn > avn + nw) {
509 size_t off = avn + nw + 1;
510 MPX_ZERO(dv + off, dvl);
520 *--dvl = (t >> nr) | w;
531 /* --- @mpx_lslc@ --- *
533 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
534 * @const mpw *av, *avl@ = source vector base and limit
535 * @size_t n@ = number of bit positions to shift by
539 * Use: Performs a logical shift left operation on an integer, only
540 * it fills in the bits with ones instead of zeroes.
543 void mpx_lslc(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
548 /* --- Trivial special case --- */
551 MPX_COPY(dv, dvl, av, avl);
553 /* --- Single bit shifting --- */
562 *dv++ = MPW((t << 1) | w);
563 w = t >> (MPW_BITS - 1);
572 /* --- Break out word and bit shifts for more sophisticated work --- */
577 /* --- Handle a shift by a multiple of the word size --- */
583 MPX_COPY(dv + nw, dvl, av, avl);
584 MPX_ONE(dv, dv + nw);
588 /* --- And finally the difficult case --- *
590 * This is a little convoluted, because I have to start from the end and
591 * work backwards to avoid overwriting the source, if they're both the same
597 size_t nr = MPW_BITS - nb;
598 size_t dvn = dvl - dv;
599 size_t avn = avl - av;
606 if (dvn > avn + nw) {
607 size_t off = avn + nw + 1;
608 MPX_ZERO(dv + off, dvl);
618 *--dvl = (t >> nr) | w;
622 *--dvl = (MPW_MAX >> nr) | w;
629 /* --- @mpx_lsr@ --- *
631 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
632 * @const mpw *av, *avl@ = source vector base and limit
633 * @size_t n@ = number of bit positions to shift by
637 * Use: Performs a logical shift right operation on an integer.
640 void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
645 /* --- Trivial special case --- */
648 MPX_COPY(dv, dvl, av, avl);
650 /* --- Single bit shifting --- */
659 *dv++ = MPW((t << (MPW_BITS - 1)) | w);
669 /* --- Break out word and bit shifts for more sophisticated work --- */
674 /* --- Handle a shift by a multiple of the word size --- */
680 MPX_COPY(dv, dvl, av + nw, avl);
683 /* --- And finally the difficult case --- */
687 size_t nr = MPW_BITS - nb;
690 w = av < avl ? *av++ : 0;
696 *dv++ = MPW((w >> nb) | (t << nr));
700 *dv++ = MPW(w >> nb);
708 /*----- Bitwise operations ------------------------------------------------*/
710 /* --- @mpx_bitop@ --- *
712 * Arguments: @mpw *dv, *dvl@ = destination vector
713 * @const mpw *av, *avl@ = first source vector
714 * @const mpw *bv, *bvl@ = second source vector
718 * Use; Provides the dyadic boolean functions.
721 #define MPX_BITBINOP(string) \
723 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
724 const mpw *bv, const mpw *bvl) \
726 MPX_SHRINK(av, avl); \
727 MPX_SHRINK(bv, bvl); \
731 a = (av < avl) ? *av++ : 0; \
732 b = (bv < bvl) ? *bv++ : 0; \
733 *dv++ = B##string(a, b); \
737 MPX_DOBIN(MPX_BITBINOP)
739 void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
745 a = (av < avl) ? *av++ : 0;
750 /*----- Unsigned arithmetic -----------------------------------------------*/
752 /* --- @mpx_2c@ --- *
754 * Arguments: @mpw *dv, *dvl@ = destination vector
755 * @const mpw *v, *vl@ = source vector
759 * Use: Calculates the two's complement of @v@.
762 void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl)
765 while (dv < dvl && v < vl)
766 *dv++ = c = MPW(~*v++);
773 MPX_UADDN(dv, dvl, 1);
776 /* --- @mpx_ueq@ --- *
778 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
779 * @const mpw *bv, *bvl@ = second argument vector base and limit
781 * Returns: Nonzero if the two vectors are equal.
783 * Use: Performs an unsigned integer test for equality.
786 int mpx_ueq(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
790 if (avl - av != bvl - bv)
799 /* --- @mpx_ucmp@ --- *
801 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
802 * @const mpw *bv, *bvl@ = second argument vector base and limit
804 * Returns: Less than, equal to, or greater than zero depending on
805 * whether @a@ is less than, equal to or greater than @b@,
808 * Use: Performs an unsigned integer comparison.
811 int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl)
816 if (avl - av > bvl - bv)
818 else if (avl - av < bvl - bv)
820 else while (avl > av) {
821 mpw a = *--avl, b = *--bvl;
830 /* --- @mpx_uadd@ --- *
832 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
833 * @const mpw *av, *avl@ = first addend vector base and limit
834 * @const mpw *bv, *bvl@ = second addend vector base and limit
838 * Use: Performs unsigned integer addition. If the result overflows
839 * the destination vector, high-order bits are discarded. This
840 * means that two's complement addition happens more or less for
841 * free, although that's more a side-effect than anything else.
842 * The result vector may be equal to either or both source
843 * vectors, but may not otherwise overlap them.
846 void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
847 const mpw *bv, const mpw *bvl)
851 while (av < avl || bv < bvl) {
856 a = (av < avl) ? *av++ : 0;
857 b = (bv < bvl) ? *bv++ : 0;
858 x = (mpd)a + (mpd)b + c;
868 /* --- @mpx_uaddn@ --- *
870 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
871 * @mpw n@ = other addend
875 * Use: Adds a small integer to a multiprecision number.
878 void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); }
880 /* --- @mpx_usub@ --- *
882 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
883 * @const mpw *av, *avl@ = first argument vector base and limit
884 * @const mpw *bv, *bvl@ = second argument vector base and limit
888 * Use: Performs unsigned integer subtraction. If the result
889 * overflows the destination vector, high-order bits are
890 * discarded. This means that two's complement subtraction
891 * happens more or less for free, althuogh that's more a side-
892 * effect than anything else. The result vector may be equal to
893 * either or both source vectors, but may not otherwise overlap
897 void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
898 const mpw *bv, const mpw *bvl)
902 while (av < avl || bv < bvl) {
907 a = (av < avl) ? *av++ : 0;
908 b = (bv < bvl) ? *bv++ : 0;
909 x = (mpd)a - (mpd)b - c;
922 /* --- @mpx_usubn@ --- *
924 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
929 * Use: Subtracts a small integer from a multiprecision number.
932 void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); }
934 /* --- @mpx_umul@ --- *
936 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
937 * @const mpw *av, *avl@ = multiplicand vector base and limit
938 * @const mpw *bv, *bvl@ = multiplier vector base and limit
942 * Use: Performs unsigned integer multiplication. If the result
943 * overflows the desination vector, high-order bits are
944 * discarded. The result vector may not overlap the argument
945 * vectors in any way.
948 void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
949 const mpw *bv, const mpw *bvl)
951 /* --- This is probably worthwhile on a multiply --- */
956 /* --- Deal with a multiply by zero --- */
963 /* --- Do the initial multiply and initialize the accumulator --- */
965 MPX_UMULN(dv, dvl, av, avl, *bv++);
967 /* --- Do the remaining multiply/accumulates --- */
969 while (dv < dvl && bv < bvl) {
979 x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c;
983 MPX_UADDN(dvv, dvl, c);
988 /* --- @mpx_umuln@ --- *
990 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
991 * @const mpw *av, *avl@ = multiplicand vector base and limit
992 * @mpw m@ = multiplier
996 * Use: Multiplies a multiprecision integer by a single-word value.
997 * The destination and source may be equal. The destination
998 * is completely cleared after use.
1001 void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
1003 MPX_UMULN(dv, dvl, av, avl, m);
1006 /* --- @mpx_umlan@ --- *
1008 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
1009 * @const mpw *av, *avl@ = multiplicand vector base and limit
1010 * @mpw m@ = multiplier
1014 * Use: Multiplies a multiprecision integer by a single-word value
1015 * and adds the result to an accumulator.
1018 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
1020 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_BITS / 2; 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 free(a); free(c); free(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 free(a); free(c); free(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 free(a); free(c); free(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 free(a); free(b); free(c); free(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 free(a); free(b); free(c); free(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 free(a); free(b); free(c); free(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 free(a); free(c); free(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 free(a); free(b); free(r); free(q); free(s); free(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"/tests/mpx");
1747 /*----- That's all, folks -------------------------------------------------*/