3 * $Id: mpx.c,v 1.9 2000/06/26 07:52:50 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.9 2000/06/26 07:52:50 mdw
34 * Portability fix for the bug fix.
36 * Revision 1.8 2000/06/25 12:59:02 mdw
37 * (mpx_udiv): Fix bug in quotient digit estimation.
39 * Revision 1.7 1999/12/22 15:49:07 mdw
40 * New function for division by a small integer.
42 * Revision 1.6 1999/11/20 22:43:44 mdw
43 * Integrate testing for MPX routines.
45 * Revision 1.5 1999/11/20 22:23:27 mdw
46 * Add function versions of some low-level macros with wider use.
48 * Revision 1.4 1999/11/17 18:04:09 mdw
49 * Add two's-complement functionality. Improve mpx_udiv a little by
50 * performing the multiplication of the divisor by q with the subtraction
53 * Revision 1.3 1999/11/13 01:57:31 mdw
54 * Remove stray debugging code.
56 * Revision 1.2 1999/11/13 01:50:59 mdw
57 * Multiprecision routines finished and tested.
59 * Revision 1.1 1999/09/03 08:41:12 mdw
64 /*----- Header files ------------------------------------------------------*/
71 #include <mLib/bits.h>
76 /*----- Loading and storing -----------------------------------------------*/
78 /* --- @mpx_storel@ --- *
80 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
81 * @void *pp@ = pointer to octet array
82 * @size_t sz@ = size of octet array
86 * Use: Stores an MP in an octet array, least significant octet
87 * first. High-end octets are silently discarded if there
88 * isn't enough space for them.
91 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
94 octet
*p
= pp
, *q
= p
+ sz
;
104 *p
++ = U8(w
| n
<< bits
);
106 bits
+= MPW_BITS
- 8;
116 /* --- @mpx_loadl@ --- *
118 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
119 * @const void *pp@ = pointer to octet array
120 * @size_t sz@ = size of octet array
124 * Use: Loads an MP in an octet array, least significant octet
125 * first. High-end octets are ignored if there isn't enough
129 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
133 const octet
*p
= pp
, *q
= p
+ sz
;
142 if (bits
>= MPW_BITS
) {
144 w
= n
>> (MPW_BITS
- bits
+ 8);
154 /* --- @mpx_storeb@ --- *
156 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
157 * @void *pp@ = pointer to octet array
158 * @size_t sz@ = size of octet array
162 * Use: Stores an MP in an octet array, most significant octet
163 * first. High-end octets are silently discarded if there
164 * isn't enough space for them.
167 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
170 octet
*p
= pp
, *q
= p
+ sz
;
180 *--q
= U8(w
| n
<< bits
);
182 bits
+= MPW_BITS
- 8;
192 /* --- @mpx_loadb@ --- *
194 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
195 * @const void *pp@ = pointer to octet array
196 * @size_t sz@ = size of octet array
200 * Use: Loads an MP in an octet array, most significant octet
201 * first. High-end octets are ignored if there isn't enough
205 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
209 const octet
*p
= pp
, *q
= p
+ sz
;
218 if (bits
>= MPW_BITS
) {
220 w
= n
>> (MPW_BITS
- bits
+ 8);
230 /*----- Logical shifting --------------------------------------------------*/
232 /* --- @mpx_lsl@ --- *
234 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
235 * @const mpw *av, *avl@ = source vector base and limit
236 * @size_t n@ = number of bit positions to shift by
240 * Use: Performs a logical shift left operation on an integer.
243 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
248 /* --- Trivial special case --- */
251 MPX_COPY(dv
, dvl
, av
, avl
);
253 /* --- Single bit shifting --- */
262 *dv
++ = MPW((t
<< 1) | w
);
263 w
= t
>> (MPW_BITS
- 1);
272 /* --- Break out word and bit shifts for more sophisticated work --- */
277 /* --- Handle a shift by a multiple of the word size --- */
280 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
281 memset(dv
, 0, MPWS(nw
));
284 /* --- And finally the difficult case --- *
286 * This is a little convoluted, because I have to start from the end and
287 * work backwards to avoid overwriting the source, if they're both the same
293 size_t nr
= MPW_BITS
- nb
;
294 size_t dvn
= dvl
- dv
;
295 size_t avn
= avl
- av
;
302 if (dvn
> avn
+ nw
) {
303 size_t off
= avn
+ nw
+ 1;
304 MPX_ZERO(dv
+ off
, dvl
);
314 *--dvl
= (t
>> nr
) | w
;
325 /* --- @mpx_lsr@ --- *
327 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
328 * @const mpw *av, *avl@ = source vector base and limit
329 * @size_t n@ = number of bit positions to shift by
333 * Use: Performs a logical shift right operation on an integer.
336 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
341 /* --- Trivial special case --- */
344 MPX_COPY(dv
, dvl
, av
, avl
);
346 /* --- Single bit shifting --- */
355 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
365 /* --- Break out word and bit shifts for more sophisticated work --- */
370 /* --- Handle a shift by a multiple of the word size --- */
373 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
375 /* --- And finally the difficult case --- */
379 size_t nr
= MPW_BITS
- nb
;
388 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
392 *dv
++ = MPW(w
>> nb
);
400 /*----- Unsigned arithmetic -----------------------------------------------*/
402 /* --- @mpx_2c@ --- *
404 * Arguments: @mpw *dv, *dvl@ = destination vector
405 * @const mpw *v, *vl@ = source vector
409 * Use: Calculates the two's complement of @v@.
412 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
415 while (dv
< dvl
&& v
< vl
)
416 *dv
++ = c
= MPW(~*v
++);
423 MPX_UADDN(dv
, dvl
, 1);
426 /* --- @mpx_ucmp@ --- *
428 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
429 * @const mpw *bv, *bvl@ = second argument vector base and limit
431 * Returns: Less than, equal to, or greater than zero depending on
432 * whether @a@ is less than, equal to or greater than @b@,
435 * Use: Performs an unsigned integer comparison.
438 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
443 if (avl
- av
> bvl
- bv
)
445 else if (avl
- av
< bvl
- bv
)
447 else while (avl
> av
) {
448 mpw a
= *--avl
, b
= *--bvl
;
457 /* --- @mpx_uadd@ --- *
459 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
460 * @const mpw *av, *avl@ = first addend vector base and limit
461 * @const mpw *bv, *bvl@ = second addend vector base and limit
465 * Use: Performs unsigned integer addition. If the result overflows
466 * the destination vector, high-order bits are discarded. This
467 * means that two's complement addition happens more or less for
468 * free, although that's more a side-effect than anything else.
469 * The result vector may be equal to either or both source
470 * vectors, but may not otherwise overlap them.
473 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
474 const mpw
*bv
, const mpw
*bvl
)
478 while (av
< avl
|| bv
< bvl
) {
483 a
= (av
< avl
) ?
*av
++ : 0;
484 b
= (bv
< bvl
) ?
*bv
++ : 0;
485 x
= (mpd
)a
+ (mpd
)b
+ c
;
495 /* --- @mpx_uaddn@ --- *
497 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
498 * @mpw n@ = other addend
502 * Use: Adds a small integer to a multiprecision number.
505 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
507 /* --- @mpx_usub@ --- *
509 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
510 * @const mpw *av, *avl@ = first argument vector base and limit
511 * @const mpw *bv, *bvl@ = second argument vector base and limit
515 * Use: Performs unsigned integer subtraction. If the result
516 * overflows the destination vector, high-order bits are
517 * discarded. This means that two's complement subtraction
518 * happens more or less for free, althuogh that's more a side-
519 * effect than anything else. The result vector may be equal to
520 * either or both source vectors, but may not otherwise overlap
524 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
525 const mpw
*bv
, const mpw
*bvl
)
529 while (av
< avl
|| bv
< bvl
) {
534 a
= (av
< avl
) ?
*av
++ : 0;
535 b
= (bv
< bvl
) ?
*bv
++ : 0;
536 x
= (mpd
)a
- (mpd
)b
- c
;
549 /* --- @mpx_usubn@ --- *
551 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
556 * Use: Subtracts a small integer from a multiprecision number.
559 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
561 /* --- @mpx_umul@ --- *
563 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
564 * @const mpw *av, *avl@ = multiplicand vector base and limit
565 * @const mpw *bv, *bvl@ = multiplier vector base and limit
569 * Use: Performs unsigned integer multiplication. If the result
570 * overflows the desination vector, high-order bits are
571 * discarded. The result vector may not overlap the argument
572 * vectors in any way.
575 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
576 const mpw
*bv
, const mpw
*bvl
)
578 /* --- This is probably worthwhile on a multiply --- */
583 /* --- Deal with a multiply by zero --- */
590 /* --- Do the initial multiply and initialize the accumulator --- */
592 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
594 /* --- Do the remaining multiply/accumulates --- */
596 while (dv
< dvl
&& bv
< bvl
) {
606 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
610 MPX_UADDN(dvv
, dvl
, c
);
615 /* --- @mpx_umuln@ --- *
617 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
618 * @const mpw *av, *avl@ = multiplicand vector base and limit
619 * @mpw m@ = multiplier
623 * Use: Multiplies a multiprecision integer by a single-word value.
624 * The destination and source may be equal. The destination
625 * is completely cleared after use.
628 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
630 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
633 /* --- @mpx_umlan@ --- *
635 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
636 * @const mpw *av, *avl@ = multiplicand vector base and limit
637 * @mpw m@ = multiplier
641 * Use: Multiplies a multiprecision integer by a single-word value
642 * and adds the result to an accumulator.
645 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
647 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
650 /* --- @mpx_usqr@ --- *
652 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
653 * @const mpw *av, *av@ = source vector base and limit
657 * Use: Performs unsigned integer squaring. The result vector must
658 * not overlap the source vector in any way.
661 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
665 /* --- Main loop --- */
673 /* --- Stop if I've run out of destination --- */
678 /* --- Work out the square at this point in the proceedings --- */
681 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
683 c
= MPW(x
>> MPW_BITS
);
686 /* --- Now fix up the rest of the vector upwards --- */
689 while (dvv
< dvl
&& avv
< avl
) {
690 mpd x
= (mpd
)a
* (mpd
)*avv
++;
691 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
692 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
695 while (dvv
< dvl
&& c
) {
701 /* --- Get ready for the next round --- */
708 /* --- @mpx_udiv@ --- *
710 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
711 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
712 * @const mpw *dv, *dvl@ = divisor vector base and limit
713 * @mpw *sv, *svl@ = scratch workspace
717 * Use: Performs unsigned integer division. If the result overflows
718 * the quotient vector, high-order bits are discarded. (Clearly
719 * the remainder vector can't overflow.) The various vectors
720 * may not overlap in any way. Yes, I know it's a bit odd
721 * requiring the dividend to be in the result position but it
722 * does make some sense really. The remainder must have
723 * headroom for at least two extra words. The scratch space
724 * must be at least one word larger than the divisor.
727 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
728 const mpw
*dv
, const mpw
*dvl
,
735 /* --- Initialize the quotient --- */
739 /* --- Perform some sanity checks --- */
742 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
744 /* --- Normalize the divisor --- *
746 * The algorithm requires that the divisor be at least two digits long.
747 * This is easy to fix.
754 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
755 if (d
< (MPW_MAX
>> b
)) {
764 /* --- Normalize the dividend/remainder to match --- */
767 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
768 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
778 /* --- Work out the relative scales --- */
781 size_t rvn
= rvl
- rv
;
782 size_t dvn
= dvl
- dv
;
784 /* --- If the divisor is clearly larger, notice this --- */
787 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
794 /* --- Calculate the most significant quotient digit --- *
796 * Because the divisor has its top bit set, this can only happen once. The
797 * pointer arithmetic is a little contorted, to make sure that the
798 * behaviour is defined.
801 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
802 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
803 if (qvl
- qv
> scale
)
807 /* --- Now for the main loop --- */
816 /* --- Get an estimate for the next quotient digit --- */
823 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
829 /* --- Refine the estimate --- */
833 mpd yy
= (mpd
)dd
* q
;
837 yh
+= yy
>> MPW_BITS
;
840 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
849 /* --- Remove a chunk from the dividend --- */
856 /* --- Calculate the size of the chunk --- *
858 * This does the whole job of calculating @r >> scale - qd@.
861 for (svv
= rv
+ scale
, dvv
= dv
;
862 dvv
< dvl
&& svv
< rvl
;
864 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
866 x
= (mpd
)*svv
- MPW(x
) - sc
;
875 mpd x
= (mpd
)*svv
- mc
- sc
;
885 /* --- Fix if the quotient was too large --- *
887 * This doesn't seem to happen very often.
890 if (rvl
[-1] > MPW_MAX
/ 2) {
891 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
896 /* --- Done for another iteration --- */
898 if (qvl
- qv
> scale
)
905 /* --- Now fiddle with unnormalizing and things --- */
907 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
910 /* --- @mpx_udivn@ --- *
912 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
914 * @const mpw *rv, *rvl@ = dividend
915 * @mpw d@ = single-precision divisor
917 * Returns: Remainder after divison.
919 * Use: Performs a single-precision division operation.
922 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
925 size_t ql
= qvl
- qv
;
931 r
= (r
<< MPW_BITS
) | rv
[i
];
939 /*----- Test rig ----------------------------------------------------------*/
943 #include <mLib/alloc.h>
944 #include <mLib/dstr.h>
945 #include <mLib/quis.h>
946 #include <mLib/testrig.h>
950 #define ALLOC(v, vl, sz) do { \
952 mpw *_vv = xmalloc(MPWS(_sz)); \
953 mpw *_vvl = _vv + _sz; \
958 #define LOAD(v, vl, d) do { \
959 const dstr *_d = (d); \
961 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
962 mpx_loadb(_v, _vl, _d->buf, _d->len); \
967 #define MAX(x, y) ((x) > (y) ? (x) : (y))
969 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
974 fprintf(stderr
, " %02x", *p
++);
978 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
983 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
987 static int chkscan(const mpw
*v
, const mpw
*vl
,
988 const void *pp
, size_t sz
, int step
)
995 mpscan_initx(&mps
, v
, vl
);
1000 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1001 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1003 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1004 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1016 static int loadstore(dstr
*v
)
1019 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1023 dstr_ensure(&d
, v
->len
);
1024 m
= xmalloc(MPWS(sz
));
1026 for (diff
= 0; diff
< sz
; diff
+= 5) {
1031 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1032 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1034 MPX_OCTETS(oct
, m
, ml
);
1035 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1036 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1037 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1041 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1042 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1044 MPX_OCTETS(oct
, m
, ml
);
1045 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1046 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1047 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1053 dumpbits("input data", v
->buf
, v
->len
);
1060 static int lsl(dstr
*v
)
1063 int n
= *(int *)v
[1].buf
;
1070 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1072 mpx_lsl(d
, dl
, a
, al
, n
);
1073 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1074 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1075 dumpmp(" a", a
, al
);
1076 dumpmp("expected", c
, cl
);
1077 dumpmp(" result", d
, dl
);
1081 free(a
); free(c
); free(d
);
1085 static int lsr(dstr
*v
)
1088 int n
= *(int *)v
[1].buf
;
1095 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1097 mpx_lsr(d
, dl
, a
, al
, n
);
1098 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1099 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1100 dumpmp(" a", a
, al
);
1101 dumpmp("expected", c
, cl
);
1102 dumpmp(" result", d
, dl
);
1106 free(a
); free(c
); free(d
);
1110 static int uadd(dstr
*v
)
1121 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1123 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1124 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1125 fprintf(stderr
, "\n*** uadd failed\n");
1126 dumpmp(" a", a
, al
);
1127 dumpmp(" b", b
, bl
);
1128 dumpmp("expected", c
, cl
);
1129 dumpmp(" result", d
, dl
);
1133 free(a
); free(b
); free(c
); free(d
);
1137 static int usub(dstr
*v
)
1148 ALLOC(d
, dl
, al
- a
);
1150 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1151 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1152 fprintf(stderr
, "\n*** usub failed\n");
1153 dumpmp(" a", a
, al
);
1154 dumpmp(" b", b
, bl
);
1155 dumpmp("expected", c
, cl
);
1156 dumpmp(" result", d
, dl
);
1160 free(a
); free(b
); free(c
); free(d
);
1164 static int umul(dstr
*v
)
1175 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1177 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1178 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1179 fprintf(stderr
, "\n*** umul failed\n");
1180 dumpmp(" a", a
, al
);
1181 dumpmp(" b", b
, bl
);
1182 dumpmp("expected", c
, cl
);
1183 dumpmp(" result", d
, dl
);
1187 free(a
); free(b
); free(c
); free(d
);
1191 static int usqr(dstr
*v
)
1200 ALLOC(d
, dl
, 2 * (al
- a
));
1202 mpx_usqr(d
, dl
, a
, al
);
1203 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1204 fprintf(stderr
, "\n*** usqr failed\n");
1205 dumpmp(" a", a
, al
);
1206 dumpmp("expected", c
, cl
);
1207 dumpmp(" result", d
, dl
);
1211 free(a
); free(c
); free(d
);
1215 static int udiv(dstr
*v
)
1225 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1229 ALLOC(qq
, qql
, al
- a
);
1230 ALLOC(s
, sl
, (bl
- b
) + 1);
1232 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1233 if (MPX_UCMP(qq
, qql
, !=, q
, ql
) ||
1234 MPX_UCMP(a
, al
, !=, r
, rl
)) {
1235 fprintf(stderr
, "\n*** udiv failed\n");
1236 dumpmp(" divisor", b
, bl
);
1237 dumpmp("expect r", r
, rl
);
1238 dumpmp("result r", a
, al
);
1239 dumpmp("expect q", q
, ql
);
1240 dumpmp("result q", qq
, qql
);
1244 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1248 static test_chunk defs
[] = {
1249 { "load-store", loadstore
, { &type_hex
, 0 } },
1250 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1251 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1252 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1253 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1254 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1255 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1256 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1260 int main(int argc
, char *argv
[])
1262 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1269 /*----- That's all, folks -------------------------------------------------*/