3 * $Id: mpx.c,v 1.7 1999/12/22 15:49:07 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.7 1999/12/22 15:49:07 mdw
34 * New function for division by a small integer.
36 * Revision 1.6 1999/11/20 22:43:44 mdw
37 * Integrate testing for MPX routines.
39 * Revision 1.5 1999/11/20 22:23:27 mdw
40 * Add function versions of some low-level macros with wider use.
42 * Revision 1.4 1999/11/17 18:04:09 mdw
43 * Add two's-complement functionality. Improve mpx_udiv a little by
44 * performing the multiplication of the divisor by q with the subtraction
47 * Revision 1.3 1999/11/13 01:57:31 mdw
48 * Remove stray debugging code.
50 * Revision 1.2 1999/11/13 01:50:59 mdw
51 * Multiprecision routines finished and tested.
53 * Revision 1.1 1999/09/03 08:41:12 mdw
58 /*----- Header files ------------------------------------------------------*/
65 #include <mLib/bits.h>
70 /*----- Loading and storing -----------------------------------------------*/
72 /* --- @mpx_storel@ --- *
74 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
75 * @void *pp@ = pointer to octet array
76 * @size_t sz@ = size of octet array
80 * Use: Stores an MP in an octet array, least significant octet
81 * first. High-end octets are silently discarded if there
82 * isn't enough space for them.
85 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
88 octet
*p
= pp
, *q
= p
+ sz
;
98 *p
++ = U8(w
| n
<< bits
);
100 bits
+= MPW_BITS
- 8;
110 /* --- @mpx_loadl@ --- *
112 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
113 * @const void *pp@ = pointer to octet array
114 * @size_t sz@ = size of octet array
118 * Use: Loads an MP in an octet array, least significant octet
119 * first. High-end octets are ignored if there isn't enough
123 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
127 const octet
*p
= pp
, *q
= p
+ sz
;
136 if (bits
>= MPW_BITS
) {
138 w
= n
>> (MPW_BITS
- bits
+ 8);
148 /* --- @mpx_storeb@ --- *
150 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
151 * @void *pp@ = pointer to octet array
152 * @size_t sz@ = size of octet array
156 * Use: Stores an MP in an octet array, most significant octet
157 * first. High-end octets are silently discarded if there
158 * isn't enough space for them.
161 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
164 octet
*p
= pp
, *q
= p
+ sz
;
174 *--q
= U8(w
| n
<< bits
);
176 bits
+= MPW_BITS
- 8;
186 /* --- @mpx_loadb@ --- *
188 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
189 * @const void *pp@ = pointer to octet array
190 * @size_t sz@ = size of octet array
194 * Use: Loads an MP in an octet array, most significant octet
195 * first. High-end octets are ignored if there isn't enough
199 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
203 const octet
*p
= pp
, *q
= p
+ sz
;
212 if (bits
>= MPW_BITS
) {
214 w
= n
>> (MPW_BITS
- bits
+ 8);
224 /*----- Logical shifting --------------------------------------------------*/
226 /* --- @mpx_lsl@ --- *
228 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
229 * @const mpw *av, *avl@ = source vector base and limit
230 * @size_t n@ = number of bit positions to shift by
234 * Use: Performs a logical shift left operation on an integer.
237 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
242 /* --- Trivial special case --- */
245 MPX_COPY(dv
, dvl
, av
, avl
);
247 /* --- Single bit shifting --- */
256 *dv
++ = MPW((t
<< 1) | w
);
257 w
= t
>> (MPW_BITS
- 1);
266 /* --- Break out word and bit shifts for more sophisticated work --- */
271 /* --- Handle a shift by a multiple of the word size --- */
274 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
275 memset(dv
, 0, MPWS(nw
));
278 /* --- And finally the difficult case --- *
280 * This is a little convoluted, because I have to start from the end and
281 * work backwards to avoid overwriting the source, if they're both the same
287 size_t nr
= MPW_BITS
- nb
;
288 size_t dvn
= dvl
- dv
;
289 size_t avn
= avl
- av
;
296 if (dvn
> avn
+ nw
) {
297 size_t off
= avn
+ nw
+ 1;
298 MPX_ZERO(dv
+ off
, dvl
);
308 *--dvl
= (t
>> nr
) | w
;
319 /* --- @mpx_lsr@ --- *
321 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
322 * @const mpw *av, *avl@ = source vector base and limit
323 * @size_t n@ = number of bit positions to shift by
327 * Use: Performs a logical shift right operation on an integer.
330 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
335 /* --- Trivial special case --- */
338 MPX_COPY(dv
, dvl
, av
, avl
);
340 /* --- Single bit shifting --- */
349 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
359 /* --- Break out word and bit shifts for more sophisticated work --- */
364 /* --- Handle a shift by a multiple of the word size --- */
367 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
369 /* --- And finally the difficult case --- */
373 size_t nr
= MPW_BITS
- nb
;
382 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
386 *dv
++ = MPW(w
>> nb
);
394 /*----- Unsigned arithmetic -----------------------------------------------*/
396 /* --- @mpx_2c@ --- *
398 * Arguments: @mpw *dv, *dvl@ = destination vector
399 * @const mpw *v, *vl@ = source vector
403 * Use: Calculates the two's complement of @v@.
406 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
409 while (dv
< dvl
&& v
< vl
)
410 *dv
++ = c
= MPW(~*v
++);
417 MPX_UADDN(dv
, dvl
, 1);
420 /* --- @mpx_ucmp@ --- *
422 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
423 * @const mpw *bv, *bvl@ = second argument vector base and limit
425 * Returns: Less than, equal to, or greater than zero depending on
426 * whether @a@ is less than, equal to or greater than @b@,
429 * Use: Performs an unsigned integer comparison.
432 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
437 if (avl
- av
> bvl
- bv
)
439 else if (avl
- av
< bvl
- bv
)
441 else while (avl
> av
) {
442 mpw a
= *--avl
, b
= *--bvl
;
451 /* --- @mpx_uadd@ --- *
453 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
454 * @const mpw *av, *avl@ = first addend vector base and limit
455 * @const mpw *bv, *bvl@ = second addend vector base and limit
459 * Use: Performs unsigned integer addition. If the result overflows
460 * the destination vector, high-order bits are discarded. This
461 * means that two's complement addition happens more or less for
462 * free, although that's more a side-effect than anything else.
463 * The result vector may be equal to either or both source
464 * vectors, but may not otherwise overlap them.
467 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
468 const mpw
*bv
, const mpw
*bvl
)
472 while (av
< avl
|| bv
< bvl
) {
477 a
= (av
< avl
) ?
*av
++ : 0;
478 b
= (bv
< bvl
) ?
*bv
++ : 0;
479 x
= (mpd
)a
+ (mpd
)b
+ c
;
489 /* --- @mpx_uaddn@ --- *
491 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
492 * @mpw n@ = other addend
496 * Use: Adds a small integer to a multiprecision number.
499 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
501 /* --- @mpx_usub@ --- *
503 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
504 * @const mpw *av, *avl@ = first argument vector base and limit
505 * @const mpw *bv, *bvl@ = second argument vector base and limit
509 * Use: Performs unsigned integer subtraction. If the result
510 * overflows the destination vector, high-order bits are
511 * discarded. This means that two's complement subtraction
512 * happens more or less for free, althuogh that's more a side-
513 * effect than anything else. The result vector may be equal to
514 * either or both source vectors, but may not otherwise overlap
518 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
519 const mpw
*bv
, const mpw
*bvl
)
523 while (av
< avl
|| bv
< bvl
) {
528 a
= (av
< avl
) ?
*av
++ : 0;
529 b
= (bv
< bvl
) ?
*bv
++ : 0;
530 x
= (mpd
)a
- (mpd
)b
- c
;
543 /* --- @mpx_usubn@ --- *
545 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
550 * Use: Subtracts a small integer from a multiprecision number.
553 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
555 /* --- @mpx_umul@ --- *
557 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
558 * @const mpw *av, *avl@ = multiplicand vector base and limit
559 * @const mpw *bv, *bvl@ = multiplier vector base and limit
563 * Use: Performs unsigned integer multiplication. If the result
564 * overflows the desination vector, high-order bits are
565 * discarded. The result vector may not overlap the argument
566 * vectors in any way.
569 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
570 const mpw
*bv
, const mpw
*bvl
)
572 /* --- This is probably worthwhile on a multiply --- */
577 /* --- Deal with a multiply by zero --- */
584 /* --- Do the initial multiply and initialize the accumulator --- */
586 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
588 /* --- Do the remaining multiply/accumulates --- */
590 while (dv
< dvl
&& bv
< bvl
) {
600 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
604 MPX_UADDN(dvv
, dvl
, c
);
609 /* --- @mpx_umuln@ --- *
611 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
612 * @const mpw *av, *avl@ = multiplicand vector base and limit
613 * @mpw m@ = multiplier
617 * Use: Multiplies a multiprecision integer by a single-word value.
618 * The destination and source may be equal. The destination
619 * is completely cleared after use.
622 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
624 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
627 /* --- @mpx_umlan@ --- *
629 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
630 * @const mpw *av, *avl@ = multiplicand vector base and limit
631 * @mpw m@ = multiplier
635 * Use: Multiplies a multiprecision integer by a single-word value
636 * and adds the result to an accumulator.
639 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
641 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
644 /* --- @mpx_usqr@ --- *
646 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
647 * @const mpw *av, *av@ = source vector base and limit
651 * Use: Performs unsigned integer squaring. The result vector must
652 * not overlap the source vector in any way.
655 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
659 /* --- Main loop --- */
667 /* --- Stop if I've run out of destination --- */
672 /* --- Work out the square at this point in the proceedings --- */
675 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
677 c
= MPW(x
>> MPW_BITS
);
680 /* --- Now fix up the rest of the vector upwards --- */
683 while (dvv
< dvl
&& avv
< avl
) {
684 mpd x
= (mpd
)a
* (mpd
)*avv
++;
685 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
686 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
689 while (dvv
< dvl
&& c
) {
695 /* --- Get ready for the next round --- */
702 /* --- @mpx_udiv@ --- *
704 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
705 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
706 * @const mpw *dv, *dvl@ = divisor vector base and limit
707 * @mpw *sv, *svl@ = scratch workspace
711 * Use: Performs unsigned integer division. If the result overflows
712 * the quotient vector, high-order bits are discarded. (Clearly
713 * the remainder vector can't overflow.) The various vectors
714 * may not overlap in any way. Yes, I know it's a bit odd
715 * requiring the dividend to be in the result position but it
716 * does make some sense really. The remainder must have
717 * headroom for at least two extra words. The scratch space
718 * must be at least one word larger than the divisor.
721 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
722 const mpw
*dv
, const mpw
*dvl
,
729 /* --- Initialize the quotient --- */
733 /* --- Perform some sanity checks --- */
736 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
738 /* --- Normalize the divisor --- *
740 * The algorithm requires that the divisor be at least two digits long.
741 * This is easy to fix.
748 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
749 if (d
< (MPW_MAX
>> b
)) {
758 /* --- Normalize the dividend/remainder to match --- */
761 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
762 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
772 /* --- Work out the relative scales --- */
775 size_t rvn
= rvl
- rv
;
776 size_t dvn
= dvl
- dv
;
778 /* --- If the divisor is clearly larger, notice this --- */
781 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
788 /* --- Calculate the most significant quotient digit --- *
790 * Because the divisor has its top bit set, this can only happen once. The
791 * pointer arithmetic is a little contorted, to make sure that the
792 * behaviour is defined.
795 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
796 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
797 if (qvl
- qv
> scale
)
801 /* --- Now for the main loop --- */
810 /* --- Get an estimate for the next quotient digit --- */
817 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
823 /* --- Refine the estimate --- */
827 mpd yl
= (mpd
)dd
* q
;
830 yh
+= yl
>> MPW_BITS
;
834 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
845 /* --- Remove a chunk from the dividend --- */
852 /* --- Calculate the size of the chunk --- *
854 * This does the whole job of calculating @r >> scale - qd@.
857 for (svv
= rv
+ scale
, dvv
= dv
;
858 dvv
< dvl
&& svv
< rvl
;
860 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
862 x
= (mpd
)*svv
- MPW(x
) - sc
;
871 mpd x
= (mpd
)*svv
- mc
- sc
;
881 /* --- Fix if the quotient was too large --- *
883 * This doesn't seem to happen very often.
886 if (rvl
[-1] > MPW_MAX
/ 2) {
887 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
892 /* --- Done for another iteration --- */
894 if (qvl
- qv
> scale
)
901 /* --- Now fiddle with unnormalizing and things --- */
903 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
906 /* --- @mpx_udivn@ --- *
908 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
910 * @const mpw *rv, *rvl@ = dividend
911 * @mpw d@ = single-precision divisor
913 * Returns: Remainder after divison.
915 * Use: Performs a single-precision division operation.
918 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
921 size_t ql
= qvl
- qv
;
927 r
= (r
<< MPW_BITS
) | rv
[i
];
935 /*----- Test rig ----------------------------------------------------------*/
939 #include <mLib/alloc.h>
940 #include <mLib/dstr.h>
941 #include <mLib/quis.h>
942 #include <mLib/testrig.h>
946 #define ALLOC(v, vl, sz) do { \
948 mpw *_vv = xmalloc(MPWS(_sz)); \
949 mpw *_vvl = _vv + _sz; \
954 #define LOAD(v, vl, d) do { \
955 const dstr *_d = (d); \
957 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
958 mpx_loadb(_v, _vl, _d->buf, _d->len); \
963 #define MAX(x, y) ((x) > (y) ? (x) : (y))
965 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
970 fprintf(stderr
, " %02x", *p
++);
974 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
979 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
983 static int chkscan(const mpw
*v
, const mpw
*vl
,
984 const void *pp
, size_t sz
, int step
)
991 mpscan_initx(&mps
, v
, vl
);
996 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
997 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
999 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1000 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1012 static int loadstore(dstr
*v
)
1015 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1019 dstr_ensure(&d
, v
->len
);
1020 m
= xmalloc(MPWS(sz
));
1022 for (diff
= 0; diff
< sz
; diff
+= 5) {
1027 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1028 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1030 MPX_OCTETS(oct
, m
, ml
);
1031 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1032 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1033 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1037 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1038 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1040 MPX_OCTETS(oct
, m
, ml
);
1041 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1042 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1043 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1049 dumpbits("input data", v
->buf
, v
->len
);
1056 static int lsl(dstr
*v
)
1059 int n
= *(int *)v
[1].buf
;
1066 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1068 mpx_lsl(d
, dl
, a
, al
, n
);
1069 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1070 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1071 dumpmp(" a", a
, al
);
1072 dumpmp("expected", c
, cl
);
1073 dumpmp(" result", d
, dl
);
1077 free(a
); free(c
); free(d
);
1081 static int lsr(dstr
*v
)
1084 int n
= *(int *)v
[1].buf
;
1091 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1093 mpx_lsr(d
, dl
, a
, al
, n
);
1094 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1095 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1096 dumpmp(" a", a
, al
);
1097 dumpmp("expected", c
, cl
);
1098 dumpmp(" result", d
, dl
);
1102 free(a
); free(c
); free(d
);
1106 static int uadd(dstr
*v
)
1117 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1119 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1120 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1121 fprintf(stderr
, "\n*** uadd failed\n");
1122 dumpmp(" a", a
, al
);
1123 dumpmp(" b", b
, bl
);
1124 dumpmp("expected", c
, cl
);
1125 dumpmp(" result", d
, dl
);
1129 free(a
); free(b
); free(c
); free(d
);
1133 static int usub(dstr
*v
)
1144 ALLOC(d
, dl
, al
- a
);
1146 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1147 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1148 fprintf(stderr
, "\n*** usub failed\n");
1149 dumpmp(" a", a
, al
);
1150 dumpmp(" b", b
, bl
);
1151 dumpmp("expected", c
, cl
);
1152 dumpmp(" result", d
, dl
);
1156 free(a
); free(b
); free(c
); free(d
);
1160 static int umul(dstr
*v
)
1171 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1173 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1174 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1175 fprintf(stderr
, "\n*** umul failed\n");
1176 dumpmp(" a", a
, al
);
1177 dumpmp(" b", b
, bl
);
1178 dumpmp("expected", c
, cl
);
1179 dumpmp(" result", d
, dl
);
1183 free(a
); free(b
); free(c
); free(d
);
1187 static int usqr(dstr
*v
)
1196 ALLOC(d
, dl
, 2 * (al
- a
));
1198 mpx_usqr(d
, dl
, a
, al
);
1199 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1200 fprintf(stderr
, "\n*** usqr failed\n");
1201 dumpmp(" a", a
, al
);
1202 dumpmp("expected", c
, cl
);
1203 dumpmp(" result", d
, dl
);
1207 free(a
); free(c
); free(d
);
1211 static int udiv(dstr
*v
)
1221 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1225 ALLOC(qq
, qql
, al
- a
);
1226 ALLOC(s
, sl
, (bl
- b
) + 1);
1228 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1229 if (MPX_UCMP(qq
, qql
, !=, q
, ql
) ||
1230 MPX_UCMP(a
, al
, !=, r
, rl
)) {
1231 fprintf(stderr
, "\n*** udiv failed\n");
1232 dumpmp(" divisor", b
, bl
);
1233 dumpmp("expect r", r
, rl
);
1234 dumpmp("result r", a
, al
);
1235 dumpmp("expect q", q
, ql
);
1236 dumpmp("result q", qq
, qql
);
1240 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1244 static test_chunk defs
[] = {
1245 { "load-store", loadstore
, { &type_hex
, 0 } },
1246 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1247 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1248 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1249 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1250 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1251 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1252 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1256 int main(int argc
, char *argv
[])
1258 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1265 /*----- That's all, folks -------------------------------------------------*/